Branch data Line data Source code
1 : : /******************************************************************************
2 : : * This file is part of the cvc5 project.
3 : : *
4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
5 : : * in the top-level source directory and their institutional affiliations.
6 : : * All rights reserved. See the file COPYING in the top-level source
7 : : * directory for licensing information.
8 : : * ****************************************************************************
9 : : *
10 : : * Proof node manager utility.
11 : : */
12 : :
13 : : #include "cvc5_private.h"
14 : :
15 : : #ifndef CVC5__PROOF__PROOF_NODE_MANAGER_H
16 : : #define CVC5__PROOF__PROOF_NODE_MANAGER_H
17 : :
18 : : #include <vector>
19 : :
20 : : #include "cvc5/cvc5_proof_rule.h"
21 : : #include "expr/node.h"
22 : : #include "proof/trust_id.h"
23 : :
24 : : namespace cvc5::internal {
25 : :
26 : : class ProofChecker;
27 : : class ProofNode;
28 : : class Options;
29 : :
30 : : namespace theory {
31 : : class Rewriter;
32 : : }
33 : :
34 : : /**
35 : : * A manager for proof node objects. This is a trusted interface for creating
36 : : * and updating ProofNode objects.
37 : : *
38 : : * In more detail, we say a ProofNode is "well-formed (with respect to checker
39 : : * X)" if its d_proven field is non-null, and corresponds to the formula that
40 : : * the ProofNode proves according to X. The ProofNodeManager class constructs
41 : : * and update nodes that are well-formed with respect to its underlying checker.
42 : : *
43 : : * If no checker is provided, then the ProofNodeManager assigns the d_proven
44 : : * field of ProofNode based on the provided "expected" argument in mkNode below,
45 : : * which must be provided in this case.
46 : : *
47 : : * The ProofNodeManager is used as a trusted way of updating ProofNode objects
48 : : * via updateNode below. In particular, this method leaves the d_proven field
49 : : * unchanged and updates (if possible) the remaining content of a given proof
50 : : * node.
51 : : *
52 : : * Notice that ProofNode objects are mutable, and hence this class does not
53 : : * cache the results of mkNode. A version of this class that caches
54 : : * immutable version of ProofNode objects could be built as an extension
55 : : * or layer on top of this class.
56 : : */
57 : : class ProofNodeManager
58 : : {
59 : : public:
60 : : ProofNodeManager(NodeManager* nm,
61 : : const Options& opts,
62 : : theory::Rewriter* rr,
63 : : ProofChecker* pc = nullptr);
64 : 19898 : ~ProofNodeManager() {}
65 : : /**
66 : : * This constructs a ProofNode with the given arguments. The expected
67 : : * argument, when provided, indicates the formula that the returned node
68 : : * is expected to prove. If we find that it does not, based on the underlying
69 : : * checker, this method returns nullptr.
70 : : *
71 : : * @param id The id of the proof node.
72 : : * @param children The children of the proof node.
73 : : * @param args The arguments of the proof node.
74 : : * @param expected (Optional) the expected conclusion of the proof node.
75 : : * @return the proof node, or nullptr if the given arguments do not
76 : : * consistute a proof of the expected conclusion according to the underlying
77 : : * checker, if both are provided. It also returns nullptr if neither the
78 : : * checker nor the expected field is provided, since in this case the
79 : : * conclusion is unknown.
80 : : */
81 : : std::shared_ptr<ProofNode> mkNode(
82 : : ProofRule id,
83 : : const std::vector<std::shared_ptr<ProofNode>>& children,
84 : : const std::vector<Node>& args,
85 : : Node expected = Node::null());
86 : : /**
87 : : * This constructs a ProofNode with rule ProofRule::TRUST with the given
88 : : * children and arguments.
89 : : *
90 : : * @param id The id of the proof node.
91 : : * @param children The children of the proof node.
92 : : * @param args The arguments of the proof node, which are optional additional
93 : : * arguments of ProofRule::TRUST beyond id and conc.
94 : : * @param conc The conclusion of the proof node.
95 : : * @param expected The conclusion of the proof node.
96 : : * @return the proof node, or nullptr if the given arguments do not
97 : : * consistute a proof of the expected conclusion according to the underlying
98 : : * checker, if both are provided. It also returns nullptr if neither the
99 : : * checker nor the expected field is provided, since in this case the
100 : : * conclusion is unknown.
101 : : */
102 : : std::shared_ptr<ProofNode> mkTrustedNode(
103 : : TrustId id,
104 : : const std::vector<std::shared_ptr<ProofNode>>& children,
105 : : const std::vector<Node>& args,
106 : : const Node& conc);
107 : : /**
108 : : * Make the proof node corresponding to the assumption of fact.
109 : : *
110 : : * @param fact The fact to assume.
111 : : * @return The ASSUME proof of fact.
112 : : */
113 : : std::shared_ptr<ProofNode> mkAssume(Node fact);
114 : : /**
115 : : * Make symm, which accounts for whether the child is already a SYMM
116 : : * node, in which case we return its child.
117 : : */
118 : : std::shared_ptr<ProofNode> mkSymm(std::shared_ptr<ProofNode> child,
119 : : Node expected = Node::null());
120 : : /**
121 : : * Make transitivity proof, where children contains one or more proofs of
122 : : * equalities that form an ordered chain. In other words, the vector children
123 : : * is a legal set of children for an application of TRANS.
124 : : */
125 : : std::shared_ptr<ProofNode> mkTrans(
126 : : const std::vector<std::shared_ptr<ProofNode>>& children,
127 : : Node expected = Node::null());
128 : :
129 : : /**
130 : : * Make scope having body pf and arguments (assumptions-to-close) assumps.
131 : : * If ensureClosed is true, then this method throws an assertion failure if
132 : : * the returned proof is not closed. This is the case if a free assumption
133 : : * of pf is missing from the vector assumps.
134 : : *
135 : : * For conveinence, the proof pf may be modified to ensure that the overall
136 : : * result is closed. For instance, given input:
137 : : * pf = TRANS( ASSUME( x=y ), ASSUME( y=z ) )
138 : : * assumps = { y=x, y=z }
139 : : * This method will modify pf to be:
140 : : * pf = TRANS( SYMM( ASSUME( y=x ) ), ASSUME( y=z ) )
141 : : * so that y=x matches the free assumption. The returned proof is:
142 : : * SCOPE(TRANS( SYMM( ASSUME( y=x ) ), ASSUME( y=z ) ) :args { y=x, y=z })
143 : : *
144 : : * When ensureClosed is true, duplicates are eliminated from assumps. The
145 : : * reason for this is due to performance, since in this method, assumps is
146 : : * converted to an unordered_set to do the above check and hence it is a
147 : : * convienient time to eliminate duplicate literals.
148 : : *
149 : : * Additionally, if both ensureClosed and doMinimize are true, assumps is
150 : : * updated to contain exactly the free asumptions of pf. This also includes
151 : : * having no duplicates. Furthermore, if assumps is empty after minimization,
152 : : * this method is a no-op.
153 : : *
154 : : * In each case, the update vector assumps is passed as arguments to SCOPE.
155 : : *
156 : : * @param pf The body of the proof,
157 : : * @param assumps The assumptions-to-close of the scope,
158 : : * @param ensureClosed Whether to ensure that the proof is closed,
159 : : * @param doMinimize Whether to minimize assumptions.
160 : : * @param expected the node that the scope should prove.
161 : : * @return The scoped proof.
162 : : */
163 : : std::shared_ptr<ProofNode> mkScope(std::shared_ptr<ProofNode> pf,
164 : : std::vector<Node>& assumps,
165 : : bool ensureClosed = true,
166 : : bool doMinimize = false,
167 : : Node expected = Node::null());
168 : : /**
169 : : * This method updates pn to be a proof of the form <id>( children, args ),
170 : : * while maintaining its d_proven field. This method returns false if this
171 : : * proof manager is using a checker, and we compute that the above proof
172 : : * is not a proof of the fact that pn previously proved.
173 : : *
174 : : * @param pn The proof node to update.
175 : : * @param id The updated id of the proof node.
176 : : * @param children The updated children of the proof node.
177 : : * @param args The updated arguments of the proof node.
178 : : * @return true if the update was successful.
179 : : *
180 : : * Notice that updateNode always returns true if there is no underlying
181 : : * checker.
182 : : */
183 : : bool updateNode(ProofNode* pn,
184 : : ProofRule id,
185 : : const std::vector<std::shared_ptr<ProofNode>>& children,
186 : : const std::vector<Node>& args);
187 : : /**
188 : : * Update node pn to have the contents of pnr. It should be the case that
189 : : * pn and pnr prove the same fact, otherwise false is returned and pn is
190 : : * unchanged.
191 : : */
192 : : bool updateNode(ProofNode* pn, ProofNode* pnr);
193 : : /**
194 : : * Ensure that pn is checked, regardless of the proof check format.
195 : : */
196 : : void ensureChecked(ProofNode* pn);
197 : : /** Get the underlying proof checker */
198 : : ProofChecker* getChecker() const;
199 : : /**
200 : : * Cancel double SYMM. Returns a proof node that is not a double application
201 : : * of SYMM, e.g. for (SYMM (SYMM (r P))), this returns (r P) where r != SYMM.
202 : : */
203 : : static ProofNode* cancelDoubleSymm(ProofNode* pn);
204 : :
205 : : private:
206 : : /** Reference to the options */
207 : : const Options& d_opts;
208 : : /** The rewriter */
209 : : theory::Rewriter* d_rewriter;
210 : : /** The (optional) proof checker */
211 : : ProofChecker* d_checker;
212 : : /** the true node */
213 : : Node d_true;
214 : : /** Check internal
215 : : *
216 : : * This returns the result of proof checking a ProofNode with the provided
217 : : * arguments with an expected conclusion, which may not null if there is
218 : : * no expected conclusion.
219 : : *
220 : : * This throws an assertion error if we fail to check such a proof node, or
221 : : * if expected is provided (non-null) and is different what is proven by the
222 : : * other arguments.
223 : : *
224 : : * The flag didCheck is set to true if the underlying proof checker was
225 : : * invoked. This may be false if e.g. the proof checking mode is lazy.
226 : : */
227 : : Node checkInternal(ProofRule id,
228 : : const std::vector<std::shared_ptr<ProofNode>>& children,
229 : : const std::vector<Node>& args,
230 : : Node expected,
231 : : bool& didCheck);
232 : : /**
233 : : * Update node internal, return true if successful. This is called by
234 : : * the update node methods above. The argument needsCheck is whether we
235 : : * need to check the correctness of the rule application. This is false
236 : : * for the updateNode routine where pnr is an (already checked) proof node.
237 : : */
238 : : bool updateNodeInternal(
239 : : ProofNode* pn,
240 : : ProofRule id,
241 : : const std::vector<std::shared_ptr<ProofNode>>& children,
242 : : const std::vector<Node>& args,
243 : : bool needsCheck);
244 : : };
245 : :
246 : : } // namespace cvc5::internal
247 : :
248 : : #endif /* CVC5__PROOF__PROOF_NODE_H */
|