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 lazy proof utility. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "proof/lazy_proof.h" |
17 |
|
|
18 |
|
#include "proof/proof_ensure_closed.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 |
264778 |
LazyCDProof::LazyCDProof(ProofNodeManager* pnm, |
27 |
|
ProofGenerator* dpg, |
28 |
|
context::Context* c, |
29 |
264778 |
const std::string& name) |
30 |
264778 |
: CDProof(pnm, c, name), d_gens(c ? c : &d_context), d_defaultGen(dpg) |
31 |
|
{ |
32 |
264778 |
} |
33 |
|
|
34 |
319111 |
LazyCDProof::~LazyCDProof() {} |
35 |
|
|
36 |
1580682 |
std::shared_ptr<ProofNode> LazyCDProof::getProofFor(Node fact) |
37 |
|
{ |
38 |
1580682 |
Trace("lazy-cdproof") << "LazyCDProof::mkLazyProof " << fact << std::endl; |
39 |
|
// make the proof, which should always be non-null, since we construct an |
40 |
|
// assumption in the worst case. |
41 |
1580682 |
std::shared_ptr<ProofNode> opf = CDProof::getProofFor(fact); |
42 |
1580682 |
Assert(opf != nullptr); |
43 |
1580682 |
if (!hasGenerators()) |
44 |
|
{ |
45 |
670254 |
Trace("lazy-cdproof") << "...no generators, finished" << std::endl; |
46 |
|
// optimization: no generators, we are done |
47 |
670254 |
return opf; |
48 |
|
} |
49 |
|
// otherwise, we traverse the proof opf and fill in the ASSUME leafs that |
50 |
|
// have generators |
51 |
1820856 |
std::unordered_set<ProofNode*> visited; |
52 |
910428 |
std::unordered_set<ProofNode*>::iterator it; |
53 |
1820856 |
std::vector<ProofNode*> visit; |
54 |
|
ProofNode* cur; |
55 |
910428 |
visit.push_back(opf.get()); |
56 |
54356756 |
do |
57 |
|
{ |
58 |
55267184 |
cur = visit.back(); |
59 |
55267184 |
visit.pop_back(); |
60 |
55267184 |
it = visited.find(cur); |
61 |
|
|
62 |
55267184 |
if (it == visited.end()) |
63 |
|
{ |
64 |
15698426 |
visited.insert(cur); |
65 |
31396852 |
Node cfact = cur->getResult(); |
66 |
15698426 |
if (getProof(cfact).get() != cur) |
67 |
|
{ |
68 |
|
// We don't own this proof, skip it. This is to ensure that this method |
69 |
|
// is idempotent, since it may be the case that a previous call to |
70 |
|
// getProofFor connected a proof from a proof generator as a child of |
71 |
|
// a ProofNode in the range of the map in CDProof. Thus, this ensures |
72 |
|
// we don't touch such proofs. |
73 |
488785 |
Trace("lazy-cdproof") << "...skip unowned proof" << std::endl; |
74 |
|
} |
75 |
15209641 |
else if (cur->getRule() == PfRule::ASSUME) |
76 |
|
{ |
77 |
963754 |
bool isSym = false; |
78 |
963754 |
ProofGenerator* pg = getGeneratorFor(cfact, isSym); |
79 |
963754 |
if (pg != nullptr) |
80 |
|
{ |
81 |
1719792 |
Trace("lazy-cdproof") |
82 |
1719792 |
<< "LazyCDProof: Call generator " << pg->identify() |
83 |
859896 |
<< " for assumption " << cfact << std::endl; |
84 |
1719792 |
Node cfactGen = isSym ? CDProof::getSymmFact(cfact) : cfact; |
85 |
859896 |
Assert(!cfactGen.isNull()); |
86 |
|
// Do not use the addProofTo interface, instead use the update node |
87 |
|
// interface, since this ensures that we don't take ownership for |
88 |
|
// the current proof. Instead, it is only linked, and ignored on |
89 |
|
// future calls to getProofFor due to the check above. |
90 |
1719792 |
std::shared_ptr<ProofNode> pgc = pg->getProofFor(cfactGen); |
91 |
|
// If the proof was null, then the update is not performed. This is |
92 |
|
// not considered an error, since this behavior is equivalent to |
93 |
|
// if pg had provided the proof (ASSUME cfactGen). Ensuring the |
94 |
|
// proper behavior wrt closed proofs should be done outside this |
95 |
|
// method. |
96 |
859896 |
if (pgc != nullptr) |
97 |
|
{ |
98 |
1719792 |
Trace("lazy-cdproof-gen") |
99 |
859896 |
<< "LazyCDProof: stored proof: " << *pgc.get() << std::endl; |
100 |
|
|
101 |
859896 |
if (isSym) |
102 |
|
{ |
103 |
4410 |
d_manager->updateNode(cur, PfRule::SYMM, {pgc}, {}); |
104 |
|
} |
105 |
|
else |
106 |
|
{ |
107 |
855486 |
d_manager->updateNode(cur, pgc.get()); |
108 |
|
} |
109 |
1719792 |
Trace("lazy-cdproof") << "LazyCDProof: Successfully added fact for " |
110 |
859896 |
<< cfactGen << std::endl; |
111 |
|
} |
112 |
|
} |
113 |
|
else |
114 |
|
{ |
115 |
207716 |
Trace("lazy-cdproof") << "LazyCDProof: " << identify() |
116 |
103858 |
<< " : No generator for " << cfact << std::endl; |
117 |
|
} |
118 |
|
// Notice that we do not traverse the proofs that have been generated |
119 |
|
// lazily by the proof generators here. In other words, we assume that |
120 |
|
// the proofs from provided proof generators are final and need |
121 |
|
// no further modification by this class. |
122 |
|
} |
123 |
|
else |
124 |
|
{ |
125 |
14245887 |
const std::vector<std::shared_ptr<ProofNode>>& cc = cur->getChildren(); |
126 |
68602643 |
for (const std::shared_ptr<ProofNode>& cp : cc) |
127 |
|
{ |
128 |
54356756 |
visit.push_back(cp.get()); |
129 |
|
} |
130 |
|
} |
131 |
|
} |
132 |
55267184 |
} while (!visit.empty()); |
133 |
|
// we have now updated the ASSUME leafs of opf, return it |
134 |
910428 |
Trace("lazy-cdproof") << "...finished" << std::endl; |
135 |
910428 |
Assert(opf->getResult() == fact); |
136 |
910428 |
return opf; |
137 |
|
} |
138 |
|
|
139 |
1106790 |
void LazyCDProof::addLazyStep(Node expected, |
140 |
|
ProofGenerator* pg, |
141 |
|
PfRule idNull, |
142 |
|
bool isClosed, |
143 |
|
const char* ctx, |
144 |
|
bool forceOverwrite) |
145 |
|
{ |
146 |
1106790 |
if (pg == nullptr) |
147 |
|
{ |
148 |
|
// null generator, should have given a proof rule |
149 |
32291 |
if (idNull == PfRule::ASSUME) |
150 |
|
{ |
151 |
|
Unreachable() << "LazyCDProof::addLazyStep: " << identify() |
152 |
|
<< ": failed to provide proof generator for " << expected; |
153 |
|
return; |
154 |
|
} |
155 |
64582 |
Trace("lazy-cdproof") << "LazyCDProof::addLazyStep: " << expected |
156 |
32291 |
<< " set (trusted) step " << idNull << "\n"; |
157 |
32291 |
addStep(expected, idNull, {}, {expected}); |
158 |
32291 |
return; |
159 |
|
} |
160 |
2148998 |
Trace("lazy-cdproof") << "LazyCDProof::addLazyStep: " << expected |
161 |
1074499 |
<< " set to generator " << pg->identify() << "\n"; |
162 |
1074499 |
if (!forceOverwrite) |
163 |
|
{ |
164 |
1074499 |
NodeProofGeneratorMap::const_iterator it = d_gens.find(expected); |
165 |
1074499 |
if (it != d_gens.end()) |
166 |
|
{ |
167 |
|
// don't overwrite something that is already there |
168 |
441905 |
return; |
169 |
|
} |
170 |
|
} |
171 |
|
// just store now |
172 |
632594 |
d_gens.insert(expected, pg); |
173 |
|
// debug checking |
174 |
632594 |
if (isClosed) |
175 |
|
{ |
176 |
163985 |
Trace("lazy-cdproof-debug") << "Checking closed..." << std::endl; |
177 |
163985 |
pfgEnsureClosed(expected, pg, "lazy-cdproof-debug", ctx); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
963754 |
ProofGenerator* LazyCDProof::getGeneratorFor(Node fact, bool& isSym) |
182 |
|
{ |
183 |
963754 |
isSym = false; |
184 |
963754 |
NodeProofGeneratorMap::const_iterator it = d_gens.find(fact); |
185 |
963754 |
if (it != d_gens.end()) |
186 |
|
{ |
187 |
135640 |
return (*it).second; |
188 |
|
} |
189 |
1656228 |
Node factSym = CDProof::getSymmFact(fact); |
190 |
|
// could be symmetry |
191 |
828114 |
if (factSym.isNull()) |
192 |
|
{ |
193 |
|
// can't be symmetry, return the default generator |
194 |
265680 |
return d_defaultGen; |
195 |
|
} |
196 |
562434 |
it = d_gens.find(factSym); |
197 |
562434 |
if (it != d_gens.end()) |
198 |
|
{ |
199 |
4410 |
isSym = true; |
200 |
4410 |
return (*it).second; |
201 |
|
} |
202 |
|
// return the default generator |
203 |
558024 |
return d_defaultGen; |
204 |
|
} |
205 |
|
|
206 |
1580682 |
bool LazyCDProof::hasGenerators() const |
207 |
|
{ |
208 |
1580682 |
return !d_gens.empty() || d_defaultGen != nullptr; |
209 |
|
} |
210 |
|
|
211 |
385856 |
bool LazyCDProof::hasGenerator(Node fact) const |
212 |
|
{ |
213 |
385856 |
if (d_defaultGen != nullptr) |
214 |
|
{ |
215 |
|
return true; |
216 |
|
} |
217 |
385856 |
NodeProofGeneratorMap::const_iterator it = d_gens.find(fact); |
218 |
385856 |
if (it != d_gens.end()) |
219 |
|
{ |
220 |
1511 |
return true; |
221 |
|
} |
222 |
|
// maybe there is a symmetric fact? |
223 |
768690 |
Node factSym = CDProof::getSymmFact(fact); |
224 |
384345 |
if (!factSym.isNull()) |
225 |
|
{ |
226 |
12429 |
it = d_gens.find(factSym); |
227 |
|
} |
228 |
384345 |
return it != d_gens.end(); |
229 |
|
} |
230 |
|
|
231 |
29286 |
} // namespace cvc5 |