1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Aina Niemetz |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Implementation of proof. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "proof/proof.h" |
17 |
|
|
18 |
|
#include "proof/proof_checker.h" |
19 |
|
#include "proof/proof_node.h" |
20 |
|
#include "proof/proof_node_manager.h" |
21 |
|
|
22 |
|
using namespace cvc5::kind; |
23 |
|
|
24 |
|
namespace cvc5 { |
25 |
|
|
26 |
1459231 |
CDProof::CDProof(ProofNodeManager* pnm, |
27 |
|
context::Context* c, |
28 |
|
const std::string& name, |
29 |
1459231 |
bool autoSymm) |
30 |
|
: d_manager(pnm), |
31 |
|
d_context(), |
32 |
|
d_nodes(c ? c : &d_context), |
33 |
|
d_name(name), |
34 |
1459231 |
d_autoSymm(autoSymm) |
35 |
|
{ |
36 |
1459231 |
} |
37 |
|
|
38 |
1460476 |
CDProof::~CDProof() {} |
39 |
|
|
40 |
2541607 |
std::shared_ptr<ProofNode> CDProof::getProofFor(Node fact) |
41 |
|
{ |
42 |
5083214 |
std::shared_ptr<ProofNode> pf = getProofSymm(fact); |
43 |
2541607 |
if (pf != nullptr) |
44 |
|
{ |
45 |
1700723 |
return pf; |
46 |
|
} |
47 |
|
// add as assumption |
48 |
1681768 |
std::vector<Node> pargs = {fact}; |
49 |
1681768 |
std::vector<std::shared_ptr<ProofNode>> passume; |
50 |
|
std::shared_ptr<ProofNode> pfa = |
51 |
1681768 |
d_manager->mkNode(PfRule::ASSUME, passume, pargs, fact); |
52 |
840884 |
d_nodes.insert(fact, pfa); |
53 |
840884 |
return pfa; |
54 |
|
} |
55 |
|
|
56 |
45375297 |
std::shared_ptr<ProofNode> CDProof::getProof(Node fact) const |
57 |
|
{ |
58 |
45375297 |
NodeProofNodeMap::iterator it = d_nodes.find(fact); |
59 |
45375297 |
if (it != d_nodes.end()) |
60 |
|
{ |
61 |
27169774 |
return (*it).second; |
62 |
|
} |
63 |
18205523 |
return nullptr; |
64 |
|
} |
65 |
|
|
66 |
21037247 |
std::shared_ptr<ProofNode> CDProof::getProofSymm(Node fact) |
67 |
|
{ |
68 |
21037247 |
Trace("cdproof") << "CDProof::getProofSymm: " << fact << std::endl; |
69 |
42074494 |
std::shared_ptr<ProofNode> pf = getProof(fact); |
70 |
21037247 |
if (pf != nullptr && !isAssumption(pf.get())) |
71 |
|
{ |
72 |
9217718 |
Trace("cdproof") << "...existing non-assume " << pf->getRule() << std::endl; |
73 |
9217718 |
return pf; |
74 |
|
} |
75 |
11819529 |
else if (!d_autoSymm) |
76 |
|
{ |
77 |
|
Trace("cdproof") << "...not auto considering symmetry" << std::endl; |
78 |
|
return pf; |
79 |
|
} |
80 |
23639058 |
Node symFact = getSymmFact(fact); |
81 |
11819529 |
if (symFact.isNull()) |
82 |
|
{ |
83 |
7549835 |
Trace("cdproof") << "...no possible symm" << std::endl; |
84 |
|
// no symmetry possible, return original proof (possibly assumption) |
85 |
7549835 |
return pf; |
86 |
|
} |
87 |
|
// See if a proof exists for the opposite direction, if so, add the step. |
88 |
|
// Notice that SYMM is also disallowed. |
89 |
8539388 |
std::shared_ptr<ProofNode> pfs = getProof(symFact); |
90 |
4269694 |
if (pfs != nullptr) |
91 |
|
{ |
92 |
|
// The symmetric fact exists, and the current one either does not, or is |
93 |
|
// an assumption. We make a new proof that applies SYMM to pfs. |
94 |
669803 |
std::vector<std::shared_ptr<ProofNode>> pschild; |
95 |
507891 |
pschild.push_back(pfs); |
96 |
669803 |
std::vector<Node> args; |
97 |
507891 |
if (pf == nullptr) |
98 |
|
{ |
99 |
345979 |
Trace("cdproof") << "...fresh make symm" << std::endl; |
100 |
|
std::shared_ptr<ProofNode> psym = |
101 |
691958 |
d_manager->mkNode(PfRule::SYMM, pschild, args, fact); |
102 |
345979 |
Assert(psym != nullptr); |
103 |
345979 |
d_nodes.insert(fact, psym); |
104 |
345979 |
return psym; |
105 |
|
} |
106 |
161912 |
else if (!isAssumption(pfs.get())) |
107 |
|
{ |
108 |
|
// if its not an assumption, make the connection |
109 |
22 |
Trace("cdproof") << "...update symm" << std::endl; |
110 |
|
// update pf |
111 |
22 |
bool sret = d_manager->updateNode(pf.get(), PfRule::SYMM, pschild, args); |
112 |
22 |
AlwaysAssert(sret); |
113 |
|
} |
114 |
|
} |
115 |
|
else |
116 |
|
{ |
117 |
7523606 |
Trace("cdproof") << "...no symm, return " |
118 |
3761803 |
<< (pf == nullptr ? "null" : "non-null") << std::endl; |
119 |
|
} |
120 |
|
// return original proof (possibly assumption) |
121 |
3923715 |
return pf; |
122 |
|
} |
123 |
|
|
124 |
6563514 |
bool CDProof::addStep(Node expected, |
125 |
|
PfRule id, |
126 |
|
const std::vector<Node>& children, |
127 |
|
const std::vector<Node>& args, |
128 |
|
bool ensureChildren, |
129 |
|
CDPOverwrite opolicy) |
130 |
|
{ |
131 |
13127028 |
Trace("cdproof") << "CDProof::addStep: " << identify() << " : " << id << " " |
132 |
6563514 |
<< expected << ", ensureChildren = " << ensureChildren |
133 |
6563514 |
<< ", overwrite policy = " << opolicy << std::endl; |
134 |
13127028 |
Trace("cdproof-debug") << "CDProof::addStep: " << identify() |
135 |
6563514 |
<< " : children: " << children << "\n"; |
136 |
13127028 |
Trace("cdproof-debug") << "CDProof::addStep: " << identify() |
137 |
6563514 |
<< " : args: " << args << "\n"; |
138 |
|
// We must always provide expected to this method |
139 |
6563514 |
Assert(!expected.isNull()); |
140 |
|
|
141 |
13127028 |
std::shared_ptr<ProofNode> pprev = getProofSymm(expected); |
142 |
6563514 |
if (pprev != nullptr) |
143 |
|
{ |
144 |
2153903 |
if (!shouldOverwrite(pprev.get(), id, opolicy)) |
145 |
|
{ |
146 |
|
// we should not overwrite the current step |
147 |
2103135 |
Trace("cdproof") << "...success, no overwrite" << std::endl; |
148 |
2103135 |
return true; |
149 |
|
} |
150 |
101536 |
Trace("cdproof") << "existing proof " << pprev->getRule() |
151 |
50768 |
<< ", overwrite..." << std::endl; |
152 |
|
// we will overwrite the existing proof node by updating its contents below |
153 |
|
} |
154 |
|
// collect the child proofs, for each premise |
155 |
8920758 |
std::vector<std::shared_ptr<ProofNode>> pchildren; |
156 |
12854539 |
for (const Node& c : children) |
157 |
|
{ |
158 |
8394160 |
Trace("cdproof") << "- get child " << c << std::endl; |
159 |
16788320 |
std::shared_ptr<ProofNode> pc = getProofSymm(c); |
160 |
8394160 |
if (pc == nullptr) |
161 |
|
{ |
162 |
2244021 |
if (ensureChildren) |
163 |
|
{ |
164 |
|
// failed to get a proof for a child, fail |
165 |
|
Trace("cdproof") << "...fail, no child" << std::endl; |
166 |
|
return false; |
167 |
|
} |
168 |
2244021 |
Trace("cdproof") << "--- add assume" << std::endl; |
169 |
|
// otherwise, we initialize it as an assumption |
170 |
4488042 |
std::vector<Node> pcargs = {c}; |
171 |
4488042 |
std::vector<std::shared_ptr<ProofNode>> pcassume; |
172 |
2244021 |
pc = d_manager->mkNode(PfRule::ASSUME, pcassume, pcargs, c); |
173 |
|
// assumptions never fail to check |
174 |
2244021 |
Assert(pc != nullptr); |
175 |
2244021 |
d_nodes.insert(c, pc); |
176 |
|
} |
177 |
8394160 |
pchildren.push_back(pc); |
178 |
|
} |
179 |
|
|
180 |
|
// the user may have provided SYMM of an assumption |
181 |
4460379 |
if (id == PfRule::SYMM) |
182 |
|
{ |
183 |
119 |
Assert(pchildren.size() == 1); |
184 |
119 |
if (isAssumption(pchildren[0].get())) |
185 |
|
{ |
186 |
|
// the step we are constructing is a (symmetric fact of an) assumption, so |
187 |
|
// there is no use adding it to the proof. |
188 |
119 |
return true; |
189 |
|
} |
190 |
|
} |
191 |
|
|
192 |
4460260 |
bool ret = true; |
193 |
|
// create or update it |
194 |
8920520 |
std::shared_ptr<ProofNode> pthis; |
195 |
4460260 |
if (pprev == nullptr) |
196 |
|
{ |
197 |
4409492 |
Trace("cdproof") << " new node " << expected << "..." << std::endl; |
198 |
4409492 |
pthis = d_manager->mkNode(id, pchildren, args, expected); |
199 |
4409492 |
if (pthis == nullptr) |
200 |
|
{ |
201 |
|
// failed to construct the node, perhaps due to a proof checking failure |
202 |
|
Trace("cdproof") << "...fail, proof checking" << std::endl; |
203 |
|
return false; |
204 |
|
} |
205 |
4409492 |
d_nodes.insert(expected, pthis); |
206 |
|
} |
207 |
|
else |
208 |
|
{ |
209 |
50768 |
Trace("cdproof") << " update node " << expected << "..." << std::endl; |
210 |
|
// update its value |
211 |
50768 |
pthis = pprev; |
212 |
|
// We return the value of updateNode here. This means this method may return |
213 |
|
// false if this call failed, regardless of whether we already have a proof |
214 |
|
// step for expected. |
215 |
50768 |
ret = d_manager->updateNode(pthis.get(), id, pchildren, args); |
216 |
|
} |
217 |
4460260 |
if (ret) |
218 |
|
{ |
219 |
|
// the result of the proof node should be expected |
220 |
4460260 |
Assert(pthis->getResult() == expected); |
221 |
|
|
222 |
|
// notify new proof |
223 |
4460260 |
notifyNewProof(expected); |
224 |
|
} |
225 |
|
|
226 |
4460260 |
Trace("cdproof") << "...return " << ret << std::endl; |
227 |
4460260 |
return ret; |
228 |
|
} |
229 |
|
|
230 |
7996844 |
void CDProof::notifyNewProof(Node expected) |
231 |
|
{ |
232 |
7996844 |
if (!d_autoSymm) |
233 |
|
{ |
234 |
|
return; |
235 |
|
} |
236 |
|
// ensure SYMM proof is also linked to an existing proof, if it is an |
237 |
|
// assumption. |
238 |
15993688 |
Node symExpected = getSymmFact(expected); |
239 |
7996844 |
if (!symExpected.isNull()) |
240 |
|
{ |
241 |
2694195 |
Trace("cdproof") << " check connect symmetry " << symExpected << std::endl; |
242 |
|
// if it exists, we may need to update it |
243 |
5388390 |
std::shared_ptr<ProofNode> pfs = getProof(symExpected); |
244 |
2694195 |
if (pfs != nullptr) |
245 |
|
{ |
246 |
1382 |
Trace("cdproof") << " connect via getProofSymm method..." << std::endl; |
247 |
|
// call the get function with symmetry, which will do the update |
248 |
1382 |
std::shared_ptr<ProofNode> pfss = getProofSymm(symExpected); |
249 |
|
} |
250 |
|
else |
251 |
|
{ |
252 |
2692813 |
Trace("cdproof") << " no connect" << std::endl; |
253 |
|
} |
254 |
|
} |
255 |
|
} |
256 |
|
|
257 |
3159238 |
bool CDProof::addStep(Node expected, |
258 |
|
const ProofStep& step, |
259 |
|
bool ensureChildren, |
260 |
|
CDPOverwrite opolicy) |
261 |
|
{ |
262 |
9477714 |
return addStep(expected, |
263 |
3159238 |
step.d_rule, |
264 |
|
step.d_children, |
265 |
|
step.d_args, |
266 |
|
ensureChildren, |
267 |
6318476 |
opolicy); |
268 |
|
} |
269 |
|
|
270 |
19301 |
bool CDProof::addSteps(const ProofStepBuffer& psb, |
271 |
|
bool ensureChildren, |
272 |
|
CDPOverwrite opolicy) |
273 |
|
{ |
274 |
19301 |
const std::vector<std::pair<Node, ProofStep>>& steps = psb.getSteps(); |
275 |
169608 |
for (const std::pair<Node, ProofStep>& ps : steps) |
276 |
|
{ |
277 |
150307 |
if (!addStep(ps.first, ps.second, ensureChildren, opolicy)) |
278 |
|
{ |
279 |
|
return false; |
280 |
|
} |
281 |
|
} |
282 |
19301 |
return true; |
283 |
|
} |
284 |
|
|
285 |
3536584 |
bool CDProof::addProof(std::shared_ptr<ProofNode> pn, |
286 |
|
CDPOverwrite opolicy, |
287 |
|
bool doCopy) |
288 |
|
{ |
289 |
3536584 |
if (!doCopy) |
290 |
|
{ |
291 |
|
// If we aren't doing a deep copy, we either store pn or link its top |
292 |
|
// node into the existing pointer |
293 |
7073168 |
Node curFact = pn->getResult(); |
294 |
7073168 |
std::shared_ptr<ProofNode> cur = getProofSymm(curFact); |
295 |
3536584 |
if (cur == nullptr) |
296 |
|
{ |
297 |
|
// Assert that the checker of this class agrees with (the externally |
298 |
|
// provided) pn. This ensures that if pn was checked by a different |
299 |
|
// checker than the one of the manager in this class, then it is double |
300 |
|
// checked here, so that this class maintains the invariant that all of |
301 |
|
// its nodes in d_nodes have been checked by the underlying checker. |
302 |
2855597 |
Assert(d_manager->getChecker() == nullptr |
303 |
|
|| d_manager->getChecker()->check(pn.get(), curFact) == curFact); |
304 |
|
// just store the proof for fact |
305 |
2855597 |
d_nodes.insert(curFact, pn); |
306 |
|
} |
307 |
680987 |
else if (shouldOverwrite(cur.get(), pn->getRule(), opolicy)) |
308 |
|
{ |
309 |
|
// We update cur to have the structure of the top node of pn. Notice that |
310 |
|
// the interface to update this node will ensure that the proof apf is a |
311 |
|
// proof of the assumption. If it does not, then pn was wrong. |
312 |
|
if (!d_manager->updateNode( |
313 |
|
cur.get(), pn->getRule(), pn->getChildren(), pn->getArguments())) |
314 |
|
{ |
315 |
|
return false; |
316 |
|
} |
317 |
|
} |
318 |
|
// also need to connect via SYMM if necessary |
319 |
3536584 |
notifyNewProof(curFact); |
320 |
3536584 |
return true; |
321 |
|
} |
322 |
|
std::unordered_map<ProofNode*, bool> visited; |
323 |
|
std::unordered_map<ProofNode*, bool>::iterator it; |
324 |
|
std::vector<ProofNode*> visit; |
325 |
|
ProofNode* cur; |
326 |
|
Node curFact; |
327 |
|
visit.push_back(pn.get()); |
328 |
|
bool retValue = true; |
329 |
|
do |
330 |
|
{ |
331 |
|
cur = visit.back(); |
332 |
|
curFact = cur->getResult(); |
333 |
|
visit.pop_back(); |
334 |
|
it = visited.find(cur); |
335 |
|
if (it == visited.end()) |
336 |
|
{ |
337 |
|
// visit the children |
338 |
|
visited[cur] = false; |
339 |
|
visit.push_back(cur); |
340 |
|
const std::vector<std::shared_ptr<ProofNode>>& cs = cur->getChildren(); |
341 |
|
for (const std::shared_ptr<ProofNode>& c : cs) |
342 |
|
{ |
343 |
|
visit.push_back(c.get()); |
344 |
|
} |
345 |
|
} |
346 |
|
else if (!it->second) |
347 |
|
{ |
348 |
|
// we always call addStep, which may or may not overwrite the |
349 |
|
// current step |
350 |
|
std::vector<Node> pexp; |
351 |
|
const std::vector<std::shared_ptr<ProofNode>>& cs = cur->getChildren(); |
352 |
|
for (const std::shared_ptr<ProofNode>& c : cs) |
353 |
|
{ |
354 |
|
Assert(!c->getResult().isNull()); |
355 |
|
pexp.push_back(c->getResult()); |
356 |
|
} |
357 |
|
// can ensure children at this point |
358 |
|
bool res = addStep( |
359 |
|
curFact, cur->getRule(), pexp, cur->getArguments(), true, opolicy); |
360 |
|
// should always succeed |
361 |
|
Assert(res); |
362 |
|
retValue = retValue && res; |
363 |
|
visited[cur] = true; |
364 |
|
} |
365 |
|
} while (!visit.empty()); |
366 |
|
|
367 |
|
return retValue; |
368 |
|
} |
369 |
|
|
370 |
858243 |
bool CDProof::hasStep(Node fact) |
371 |
|
{ |
372 |
1716486 |
std::shared_ptr<ProofNode> pf = getProof(fact); |
373 |
858243 |
if (pf != nullptr && !isAssumption(pf.get())) |
374 |
|
{ |
375 |
698140 |
return true; |
376 |
|
} |
377 |
160103 |
else if (!d_autoSymm) |
378 |
|
{ |
379 |
|
return false; |
380 |
|
} |
381 |
320206 |
Node symFact = getSymmFact(fact); |
382 |
160103 |
if (symFact.isNull()) |
383 |
|
{ |
384 |
143187 |
return false; |
385 |
|
} |
386 |
16916 |
pf = getProof(symFact); |
387 |
16916 |
if (pf != nullptr && !isAssumption(pf.get())) |
388 |
|
{ |
389 |
|
return true; |
390 |
|
} |
391 |
16916 |
return false; |
392 |
|
} |
393 |
|
|
394 |
100959 |
ProofNodeManager* CDProof::getManager() const { return d_manager; } |
395 |
|
|
396 |
2834890 |
bool CDProof::shouldOverwrite(ProofNode* pn, PfRule newId, CDPOverwrite opol) |
397 |
|
{ |
398 |
2834890 |
Assert(pn != nullptr); |
399 |
|
// we overwrite only if opol is CDPOverwrite::ALWAYS, or if |
400 |
|
// opol is CDPOverwrite::ASSUME_ONLY and the previously |
401 |
|
// provided proof pn was an assumption and the currently provided step is not |
402 |
|
return opol == CDPOverwrite::ALWAYS |
403 |
2885658 |
|| (opol == CDPOverwrite::ASSUME_ONLY && isAssumption(pn) |
404 |
3296837 |
&& newId != PfRule::ASSUME); |
405 |
|
} |
406 |
|
|
407 |
14036216 |
bool CDProof::isAssumption(ProofNode* pn) |
408 |
|
{ |
409 |
14036216 |
PfRule rule = pn->getRule(); |
410 |
14036216 |
if (rule == PfRule::ASSUME) |
411 |
|
{ |
412 |
1527565 |
return true; |
413 |
|
} |
414 |
12508651 |
else if (rule == PfRule::SYMM) |
415 |
|
{ |
416 |
334444 |
const std::vector<std::shared_ptr<ProofNode>>& pc = pn->getChildren(); |
417 |
334444 |
Assert(pc.size() == 1); |
418 |
334444 |
return pc[0]->getRule() == PfRule::ASSUME; |
419 |
|
} |
420 |
12174207 |
return false; |
421 |
|
} |
422 |
|
|
423 |
947155 |
bool CDProof::isSame(TNode f, TNode g) |
424 |
|
{ |
425 |
947155 |
if (f == g) |
426 |
|
{ |
427 |
521516 |
return true; |
428 |
|
} |
429 |
425639 |
Kind fk = f.getKind(); |
430 |
425639 |
Kind gk = g.getKind(); |
431 |
425639 |
if (fk == EQUAL && gk == EQUAL && f[0] == g[1] && f[1] == g[0]) |
432 |
|
{ |
433 |
|
// symmetric equality |
434 |
170843 |
return true; |
435 |
|
} |
436 |
305025 |
if (fk == NOT && gk == NOT && f[0].getKind() == EQUAL |
437 |
275276 |
&& g[0].getKind() == EQUAL && f[0][0] == g[0][1] && f[0][1] == g[0][0]) |
438 |
|
{ |
439 |
|
// symmetric disequality |
440 |
586 |
return true; |
441 |
|
} |
442 |
254210 |
return false; |
443 |
|
} |
444 |
|
|
445 |
21540929 |
Node CDProof::getSymmFact(TNode f) |
446 |
|
{ |
447 |
21540929 |
bool polarity = f.getKind() != NOT; |
448 |
43081858 |
TNode fatom = polarity ? f : f[0]; |
449 |
21540929 |
if (fatom.getKind() != EQUAL || fatom[0] == fatom[1]) |
450 |
|
{ |
451 |
13506282 |
return Node::null(); |
452 |
|
} |
453 |
16069294 |
Node symFact = fatom[1].eqNode(fatom[0]); |
454 |
8034647 |
return polarity ? symFact : symFact.notNode(); |
455 |
|
} |
456 |
|
|
457 |
21939362 |
std::string CDProof::identify() const { return d_name; } |
458 |
|
|
459 |
29337 |
} // namespace cvc5 |