1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Haniel Barbosa, Morgan Deters |
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 the propositional engine of cvc5. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "prop/prop_engine.h" |
17 |
|
|
18 |
|
#include <iomanip> |
19 |
|
#include <map> |
20 |
|
#include <utility> |
21 |
|
|
22 |
|
#include "base/check.h" |
23 |
|
#include "base/output.h" |
24 |
|
#include "decision/decision_engine_old.h" |
25 |
|
#include "decision/justification_strategy.h" |
26 |
|
#include "options/base_options.h" |
27 |
|
#include "options/decision_options.h" |
28 |
|
#include "options/main_options.h" |
29 |
|
#include "options/options.h" |
30 |
|
#include "options/proof_options.h" |
31 |
|
#include "options/smt_options.h" |
32 |
|
#include "prop/cnf_stream.h" |
33 |
|
#include "prop/minisat/minisat.h" |
34 |
|
#include "prop/prop_proof_manager.h" |
35 |
|
#include "prop/sat_solver.h" |
36 |
|
#include "prop/sat_solver_factory.h" |
37 |
|
#include "prop/theory_proxy.h" |
38 |
|
#include "smt/env.h" |
39 |
|
#include "smt/smt_statistics_registry.h" |
40 |
|
#include "theory/output_channel.h" |
41 |
|
#include "theory/theory_engine.h" |
42 |
|
#include "util/resource_manager.h" |
43 |
|
#include "util/result.h" |
44 |
|
|
45 |
|
namespace cvc5 { |
46 |
|
namespace prop { |
47 |
|
|
48 |
|
/** Keeps a boolean flag scoped */ |
49 |
|
class ScopedBool { |
50 |
|
|
51 |
|
private: |
52 |
|
|
53 |
|
bool d_original; |
54 |
|
bool& d_reference; |
55 |
|
|
56 |
|
public: |
57 |
|
|
58 |
15251 |
ScopedBool(bool& reference) : |
59 |
15251 |
d_reference(reference) { |
60 |
15251 |
d_original = reference; |
61 |
15251 |
} |
62 |
|
|
63 |
30502 |
~ScopedBool() { |
64 |
15251 |
d_reference = d_original; |
65 |
15251 |
} |
66 |
|
}; |
67 |
|
|
68 |
9994 |
PropEngine::PropEngine(TheoryEngine* te, Env& env) |
69 |
|
: d_inCheckSat(false), |
70 |
|
d_theoryEngine(te), |
71 |
|
d_env(env), |
72 |
9994 |
d_skdm(new SkolemDefManager(d_env.getContext(), d_env.getUserContext())), |
73 |
|
d_theoryProxy(nullptr), |
74 |
|
d_satSolver(nullptr), |
75 |
|
d_cnfStream(nullptr), |
76 |
|
d_pfCnfStream(nullptr), |
77 |
|
d_ppm(nullptr), |
78 |
|
d_interrupted(false), |
79 |
19988 |
d_assumptions(d_env.getUserContext()) |
80 |
|
{ |
81 |
9994 |
Debug("prop") << "Constructing the PropEngine" << std::endl; |
82 |
9994 |
context::Context* satContext = d_env.getContext(); |
83 |
9994 |
context::UserContext* userContext = d_env.getUserContext(); |
84 |
9994 |
ProofNodeManager* pnm = d_env.getProofNodeManager(); |
85 |
9994 |
ResourceManager* rm = d_env.getResourceManager(); |
86 |
|
|
87 |
9994 |
options::DecisionMode dmode = options::decisionMode(); |
88 |
9994 |
if (dmode == options::DecisionMode::JUSTIFICATION |
89 |
2626 |
|| dmode == options::DecisionMode::STOPONLY) |
90 |
|
{ |
91 |
15568 |
d_decisionEngine.reset(new decision::JustificationStrategy( |
92 |
7784 |
satContext, userContext, d_skdm.get(), rm)); |
93 |
|
} |
94 |
2210 |
else if (dmode == options::DecisionMode::JUSTIFICATION_OLD |
95 |
2208 |
|| dmode == options::DecisionMode::STOPONLY_OLD) |
96 |
|
{ |
97 |
2 |
d_decisionEngine.reset(new DecisionEngineOld(satContext, userContext, rm)); |
98 |
|
} |
99 |
|
else |
100 |
|
{ |
101 |
2208 |
d_decisionEngine.reset(new decision::DecisionEngineEmpty(satContext, rm)); |
102 |
|
} |
103 |
|
|
104 |
9994 |
d_satSolver = SatSolverFactory::createCDCLTMinisat(smtStatisticsRegistry()); |
105 |
|
|
106 |
|
// CNF stream and theory proxy required pointers to each other, make the |
107 |
|
// theory proxy first |
108 |
9994 |
d_theoryProxy = new TheoryProxy( |
109 |
9994 |
this, d_theoryEngine, d_decisionEngine.get(), d_skdm.get(), d_env); |
110 |
29982 |
d_cnfStream = new CnfStream(d_satSolver, |
111 |
9994 |
d_theoryProxy, |
112 |
|
userContext, |
113 |
9994 |
&d_env, |
114 |
|
rm, |
115 |
|
FormulaLitPolicy::TRACK, |
116 |
19988 |
"prop"); |
117 |
|
|
118 |
|
// connect theory proxy |
119 |
9994 |
d_theoryProxy->finishInit(d_cnfStream); |
120 |
9994 |
bool satProofs = d_env.isSatProofProducing(); |
121 |
|
// connect SAT solver |
122 |
29982 |
d_satSolver->initialize(d_env.getContext(), |
123 |
|
d_theoryProxy, |
124 |
9994 |
d_env.getUserContext(), |
125 |
9994 |
satProofs ? pnm : nullptr); |
126 |
|
|
127 |
9994 |
d_decisionEngine->finishInit(d_satSolver, d_cnfStream); |
128 |
9994 |
if (satProofs) |
129 |
|
{ |
130 |
2504 |
d_pfCnfStream.reset(new ProofCnfStream( |
131 |
|
userContext, |
132 |
1252 |
*d_cnfStream, |
133 |
1252 |
static_cast<MinisatSatSolver*>(d_satSolver)->getProofManager(), |
134 |
2504 |
pnm)); |
135 |
2504 |
d_ppm.reset( |
136 |
1252 |
new PropPfManager(userContext, pnm, d_satSolver, d_pfCnfStream.get())); |
137 |
|
} |
138 |
9994 |
} |
139 |
|
|
140 |
9994 |
void PropEngine::finishInit() |
141 |
|
{ |
142 |
9994 |
NodeManager* nm = NodeManager::currentNM(); |
143 |
9994 |
d_cnfStream->convertAndAssert(nm->mkConst(true), false, false); |
144 |
|
// this is necessary because if True is later asserted to the prop engine the |
145 |
|
// CNF stream will ignore it since the SAT solver already had it registered, |
146 |
|
// thus not having True as an assumption for the SAT proof. To solve this |
147 |
|
// issue we track it directly here |
148 |
9994 |
if (isProofEnabled()) |
149 |
|
{ |
150 |
1252 |
static_cast<MinisatSatSolver*>(d_satSolver) |
151 |
|
->getProofManager() |
152 |
1252 |
->registerSatAssumptions({nm->mkConst(true)}); |
153 |
|
} |
154 |
9994 |
d_cnfStream->convertAndAssert(nm->mkConst(false).notNode(), false, false); |
155 |
9994 |
} |
156 |
|
|
157 |
19982 |
PropEngine::~PropEngine() { |
158 |
9991 |
Debug("prop") << "Destructing the PropEngine" << std::endl; |
159 |
9991 |
d_decisionEngine.reset(nullptr); |
160 |
9991 |
delete d_cnfStream; |
161 |
9991 |
delete d_satSolver; |
162 |
9991 |
delete d_theoryProxy; |
163 |
9991 |
} |
164 |
|
|
165 |
105775 |
TrustNode PropEngine::preprocess(TNode node, |
166 |
|
std::vector<TrustNode>& newLemmas, |
167 |
|
std::vector<Node>& newSkolems) |
168 |
|
{ |
169 |
105775 |
return d_theoryProxy->preprocess(node, newLemmas, newSkolems); |
170 |
|
} |
171 |
|
|
172 |
2 |
TrustNode PropEngine::removeItes(TNode node, |
173 |
|
std::vector<TrustNode>& newLemmas, |
174 |
|
std::vector<Node>& newSkolems) |
175 |
|
{ |
176 |
2 |
return d_theoryProxy->removeItes(node, newLemmas, newSkolems); |
177 |
|
} |
178 |
|
|
179 |
13785 |
void PropEngine::assertInputFormulas( |
180 |
|
const std::vector<Node>& assertions, |
181 |
|
std::unordered_map<size_t, Node>& skolemMap) |
182 |
|
{ |
183 |
13785 |
Assert(!d_inCheckSat) << "Sat solver in solve()!"; |
184 |
|
// notify the theory engine of preprocessed assertions |
185 |
13785 |
d_theoryEngine->notifyPreprocessedAssertions(assertions); |
186 |
|
// Now, notify the theory proxy of the assertions and skolem definitions. |
187 |
|
// Notice we do this before asserting the formulas to the CNF stream below, |
188 |
|
// since (preregistration) lemmas may occur during calls to assertInternal. |
189 |
|
// These lemmas we want to be notified about after the theory proxy has |
190 |
|
// been notified about all input assertions. |
191 |
13785 |
std::unordered_map<size_t, Node>::iterator it; |
192 |
138571 |
for (size_t i = 0, asize = assertions.size(); i < asize; i++) |
193 |
|
{ |
194 |
|
// is the assertion a skolem definition? |
195 |
124786 |
it = skolemMap.find(i); |
196 |
249572 |
Node skolem; |
197 |
124786 |
if (it != skolemMap.end()) |
198 |
|
{ |
199 |
19349 |
skolem = it->second; |
200 |
|
} |
201 |
124786 |
d_theoryProxy->notifyAssertion(assertions[i], skolem); |
202 |
|
} |
203 |
138561 |
for (const Node& node : assertions) |
204 |
|
{ |
205 |
124780 |
Debug("prop") << "assertFormula(" << node << ")" << std::endl; |
206 |
124784 |
assertInternal(node, false, false, true); |
207 |
|
} |
208 |
13781 |
} |
209 |
|
|
210 |
437522 |
void PropEngine::assertLemma(TrustNode tlemma, theory::LemmaProperty p) |
211 |
|
{ |
212 |
437522 |
bool removable = isLemmaPropertyRemovable(p); |
213 |
|
|
214 |
|
// call preprocessor |
215 |
875044 |
std::vector<TrustNode> ppLemmas; |
216 |
875044 |
std::vector<Node> ppSkolems; |
217 |
|
TrustNode tplemma = |
218 |
875044 |
d_theoryProxy->preprocessLemma(tlemma, ppLemmas, ppSkolems); |
219 |
|
|
220 |
437520 |
Assert(ppSkolems.size() == ppLemmas.size()); |
221 |
|
|
222 |
|
// do final checks on the lemmas we are about to send |
223 |
875040 |
if (isProofEnabled() |
224 |
437520 |
&& options::proofCheck() == options::ProofCheckMode::EAGER) |
225 |
|
{ |
226 |
|
Assert(tplemma.getGenerator() != nullptr); |
227 |
|
// ensure closed, make the proof node eagerly here to debug |
228 |
|
tplemma.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma"); |
229 |
|
for (size_t i = 0, lsize = ppLemmas.size(); i < lsize; ++i) |
230 |
|
{ |
231 |
|
Assert(ppLemmas[i].getGenerator() != nullptr); |
232 |
|
ppLemmas[i].debugCheckClosed("te-proof-debug", "TheoryEngine::lemma_new"); |
233 |
|
} |
234 |
|
} |
235 |
|
|
236 |
437520 |
if (Trace.isOn("te-lemma")) |
237 |
|
{ |
238 |
|
Trace("te-lemma") << "Lemma, output: " << tplemma.getProven() << std::endl; |
239 |
|
for (size_t i = 0, lsize = ppLemmas.size(); i < lsize; ++i) |
240 |
|
{ |
241 |
|
Trace("te-lemma") << "Lemma, new lemma: " << ppLemmas[i].getProven() |
242 |
|
<< " (skolem is " << ppSkolems[i] << ")" << std::endl; |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
|
// now, assert the lemmas |
247 |
437520 |
assertLemmasInternal(tplemma, ppLemmas, ppSkolems, removable); |
248 |
437520 |
} |
249 |
|
|
250 |
447653 |
void PropEngine::assertTrustedLemmaInternal(TrustNode trn, bool removable) |
251 |
|
{ |
252 |
895306 |
Node node = trn.getNode(); |
253 |
447653 |
Debug("prop::lemmas") << "assertLemma(" << node << ")" << std::endl; |
254 |
447653 |
bool negated = trn.getKind() == TrustNodeKind::CONFLICT; |
255 |
447653 |
Assert( |
256 |
|
!isProofEnabled() || trn.getGenerator() != nullptr |
257 |
|
|| options::unsatCores() |
258 |
|
|| (options::unsatCores() |
259 |
|
&& options::unsatCoresMode() != options::UnsatCoresMode::FULL_PROOF)); |
260 |
447653 |
assertInternal(trn.getNode(), negated, removable, false, trn.getGenerator()); |
261 |
447653 |
} |
262 |
|
|
263 |
572433 |
void PropEngine::assertInternal( |
264 |
|
TNode node, bool negated, bool removable, bool input, ProofGenerator* pg) |
265 |
|
{ |
266 |
|
// Assert as (possibly) removable |
267 |
572433 |
if (options::unsatCoresMode() == options::UnsatCoresMode::ASSUMPTIONS) |
268 |
|
{ |
269 |
168055 |
if (input) |
270 |
|
{ |
271 |
33646 |
d_cnfStream->ensureLiteral(node); |
272 |
33646 |
if (negated) |
273 |
|
{ |
274 |
|
d_assumptions.push_back(node.notNode()); |
275 |
|
} |
276 |
|
else |
277 |
|
{ |
278 |
33646 |
d_assumptions.push_back(node); |
279 |
|
} |
280 |
|
} |
281 |
|
else |
282 |
|
{ |
283 |
134409 |
d_cnfStream->convertAndAssert(node, removable, negated, input); |
284 |
|
} |
285 |
|
} |
286 |
404378 |
else if (isProofEnabled()) |
287 |
|
{ |
288 |
88742 |
d_pfCnfStream->convertAndAssert(node, negated, removable, pg); |
289 |
|
// if input, register the assertion in the proof manager |
290 |
88742 |
if (input) |
291 |
|
{ |
292 |
19554 |
d_ppm->registerAssertion(node); |
293 |
|
} |
294 |
|
} |
295 |
|
else |
296 |
|
{ |
297 |
315640 |
d_cnfStream->convertAndAssert(node, removable, negated, input); |
298 |
|
} |
299 |
572429 |
} |
300 |
|
|
301 |
639411 |
void PropEngine::assertLemmasInternal(TrustNode trn, |
302 |
|
const std::vector<TrustNode>& ppLemmas, |
303 |
|
const std::vector<Node>& ppSkolems, |
304 |
|
bool removable) |
305 |
|
{ |
306 |
639411 |
if (!trn.isNull()) |
307 |
|
{ |
308 |
437520 |
assertTrustedLemmaInternal(trn, removable); |
309 |
|
} |
310 |
649544 |
for (const TrustNode& tnl : ppLemmas) |
311 |
|
{ |
312 |
10133 |
assertTrustedLemmaInternal(tnl, removable); |
313 |
|
} |
314 |
|
// assert to decision engine |
315 |
639411 |
if (!removable) |
316 |
|
{ |
317 |
|
// also add to the decision engine, where notice we don't need proofs |
318 |
514978 |
if (!trn.isNull()) |
319 |
|
{ |
320 |
|
// notify the theory proxy of the lemma |
321 |
313087 |
d_theoryProxy->notifyAssertion(trn.getProven()); |
322 |
|
} |
323 |
514978 |
Assert(ppSkolems.size() == ppLemmas.size()); |
324 |
525111 |
for (size_t i = 0, lsize = ppLemmas.size(); i < lsize; ++i) |
325 |
|
{ |
326 |
20266 |
Node lem = ppLemmas[i].getProven(); |
327 |
10133 |
d_theoryProxy->notifyAssertion(ppLemmas[i].getProven(), ppSkolems[i]); |
328 |
|
} |
329 |
|
} |
330 |
639411 |
} |
331 |
|
|
332 |
69431 |
void PropEngine::requirePhase(TNode n, bool phase) { |
333 |
69431 |
Debug("prop") << "requirePhase(" << n << ", " << phase << ")" << std::endl; |
334 |
|
|
335 |
69431 |
Assert(n.getType().isBoolean()); |
336 |
69431 |
SatLiteral lit = d_cnfStream->getLiteral(n); |
337 |
69431 |
d_satSolver->requirePhase(phase ? lit : ~lit); |
338 |
69431 |
} |
339 |
|
|
340 |
176887 |
bool PropEngine::isDecision(Node lit) const { |
341 |
176887 |
Assert(isSatLiteral(lit)); |
342 |
176887 |
return d_satSolver->isDecision(d_cnfStream->getLiteral(lit).getSatVariable()); |
343 |
|
} |
344 |
|
|
345 |
16 |
int32_t PropEngine::getDecisionLevel(Node lit) const |
346 |
|
{ |
347 |
16 |
Assert(isSatLiteral(lit)); |
348 |
32 |
return d_satSolver->getDecisionLevel( |
349 |
32 |
d_cnfStream->getLiteral(lit).getSatVariable()); |
350 |
|
} |
351 |
|
|
352 |
8 |
int32_t PropEngine::getIntroLevel(Node lit) const |
353 |
|
{ |
354 |
8 |
Assert(isSatLiteral(lit)); |
355 |
16 |
return d_satSolver->getIntroLevel( |
356 |
16 |
d_cnfStream->getLiteral(lit).getSatVariable()); |
357 |
|
} |
358 |
|
|
359 |
|
void PropEngine::printSatisfyingAssignment(){ |
360 |
|
const CnfStream::NodeToLiteralMap& transCache = |
361 |
|
d_cnfStream->getTranslationCache(); |
362 |
|
Debug("prop-value") << "Literal | Value | Expr" << std::endl |
363 |
|
<< "----------------------------------------" |
364 |
|
<< "-----------------" << std::endl; |
365 |
|
for(CnfStream::NodeToLiteralMap::const_iterator i = transCache.begin(), |
366 |
|
end = transCache.end(); |
367 |
|
i != end; |
368 |
|
++i) { |
369 |
|
std::pair<Node, SatLiteral> curr = *i; |
370 |
|
SatLiteral l = curr.second; |
371 |
|
if(!l.isNegated()) { |
372 |
|
Node n = curr.first; |
373 |
|
SatValue value = d_satSolver->modelValue(l); |
374 |
|
Debug("prop-value") << "'" << l << "' " << value << " " << n << std::endl; |
375 |
|
} |
376 |
|
} |
377 |
|
} |
378 |
|
|
379 |
15251 |
Result PropEngine::checkSat() { |
380 |
15251 |
Assert(!d_inCheckSat) << "Sat solver in solve()!"; |
381 |
15251 |
Debug("prop") << "PropEngine::checkSat()" << std::endl; |
382 |
|
|
383 |
|
// Mark that we are in the checkSat |
384 |
30502 |
ScopedBool scopedBool(d_inCheckSat); |
385 |
15251 |
d_inCheckSat = true; |
386 |
|
|
387 |
|
// TODO This currently ignores conflicts (a dangerous practice). |
388 |
15251 |
d_decisionEngine->presolve(); |
389 |
15251 |
d_theoryEngine->presolve(); |
390 |
|
|
391 |
15251 |
if(options::preprocessOnly()) { |
392 |
3 |
return Result(Result::SAT_UNKNOWN, Result::REQUIRES_FULL_CHECK); |
393 |
|
} |
394 |
|
|
395 |
|
// Reset the interrupted flag |
396 |
15248 |
d_interrupted = false; |
397 |
|
|
398 |
|
// Check the problem |
399 |
|
SatValue result; |
400 |
15248 |
if (d_assumptions.size() == 0) |
401 |
|
{ |
402 |
11599 |
result = d_satSolver->solve(); |
403 |
|
} |
404 |
|
else |
405 |
|
{ |
406 |
7298 |
std::vector<SatLiteral> assumptions; |
407 |
52112 |
for (const Node& node : d_assumptions) |
408 |
|
{ |
409 |
48463 |
assumptions.push_back(d_cnfStream->getLiteral(node)); |
410 |
|
} |
411 |
3649 |
result = d_satSolver->solve(assumptions); |
412 |
|
} |
413 |
|
|
414 |
15232 |
if( result == SAT_VALUE_UNKNOWN ) { |
415 |
|
ResourceManager* rm = d_env.getResourceManager(); |
416 |
|
Result::UnknownExplanation why = Result::INTERRUPTED; |
417 |
|
if (rm->outOfTime()) |
418 |
|
{ |
419 |
|
why = Result::TIMEOUT; |
420 |
|
} |
421 |
|
if (rm->outOfResources()) |
422 |
|
{ |
423 |
|
why = Result::RESOURCEOUT; |
424 |
|
} |
425 |
|
return Result(Result::SAT_UNKNOWN, why); |
426 |
|
} |
427 |
|
|
428 |
15232 |
if( result == SAT_VALUE_TRUE && Debug.isOn("prop") ) { |
429 |
|
printSatisfyingAssignment(); |
430 |
|
} |
431 |
|
|
432 |
15232 |
Debug("prop") << "PropEngine::checkSat() => " << result << std::endl; |
433 |
15232 |
if(result == SAT_VALUE_TRUE && d_theoryEngine->isIncomplete()) { |
434 |
110 |
return Result(Result::SAT_UNKNOWN, Result::INCOMPLETE); |
435 |
|
} |
436 |
15122 |
return Result(result == SAT_VALUE_TRUE ? Result::SAT : Result::UNSAT); |
437 |
|
} |
438 |
|
|
439 |
35115 |
Node PropEngine::getValue(TNode node) const |
440 |
|
{ |
441 |
35115 |
Assert(node.getType().isBoolean()); |
442 |
35115 |
Assert(d_cnfStream->hasLiteral(node)); |
443 |
|
|
444 |
35115 |
SatLiteral lit = d_cnfStream->getLiteral(node); |
445 |
|
|
446 |
35115 |
SatValue v = d_satSolver->value(lit); |
447 |
35115 |
if (v == SAT_VALUE_TRUE) |
448 |
|
{ |
449 |
35099 |
return NodeManager::currentNM()->mkConst(true); |
450 |
|
} |
451 |
16 |
else if (v == SAT_VALUE_FALSE) |
452 |
|
{ |
453 |
16 |
return NodeManager::currentNM()->mkConst(false); |
454 |
|
} |
455 |
|
else |
456 |
|
{ |
457 |
|
Assert(v == SAT_VALUE_UNKNOWN); |
458 |
|
return Node::null(); |
459 |
|
} |
460 |
|
} |
461 |
|
|
462 |
24557043 |
bool PropEngine::isSatLiteral(TNode node) const |
463 |
|
{ |
464 |
24557043 |
return d_cnfStream->hasLiteral(node); |
465 |
|
} |
466 |
|
|
467 |
9375282 |
bool PropEngine::hasValue(TNode node, bool& value) const |
468 |
|
{ |
469 |
9375282 |
Assert(node.getType().isBoolean()); |
470 |
9375282 |
Assert(d_cnfStream->hasLiteral(node)) << node; |
471 |
|
|
472 |
9375282 |
SatLiteral lit = d_cnfStream->getLiteral(node); |
473 |
|
|
474 |
9375282 |
SatValue v = d_satSolver->value(lit); |
475 |
9375282 |
if (v == SAT_VALUE_TRUE) |
476 |
|
{ |
477 |
6023760 |
value = true; |
478 |
6023760 |
return true; |
479 |
|
} |
480 |
3351522 |
else if (v == SAT_VALUE_FALSE) |
481 |
|
{ |
482 |
183392 |
value = false; |
483 |
183392 |
return true; |
484 |
|
} |
485 |
|
else |
486 |
|
{ |
487 |
3168130 |
Assert(v == SAT_VALUE_UNKNOWN); |
488 |
3168130 |
return false; |
489 |
|
} |
490 |
|
} |
491 |
|
|
492 |
16694 |
void PropEngine::getBooleanVariables(std::vector<TNode>& outputVariables) const |
493 |
|
{ |
494 |
16694 |
d_cnfStream->getBooleanVariables(outputVariables); |
495 |
16694 |
} |
496 |
|
|
497 |
197702 |
Node PropEngine::ensureLiteral(TNode n) |
498 |
|
{ |
499 |
|
// must preprocess |
500 |
197702 |
Node preprocessed = getPreprocessedTerm(n); |
501 |
395404 |
Trace("ensureLiteral") << "ensureLiteral preprocessed: " << preprocessed |
502 |
197702 |
<< std::endl; |
503 |
197702 |
if (isProofEnabled()) |
504 |
|
{ |
505 |
26409 |
d_pfCnfStream->ensureLiteral(preprocessed); |
506 |
|
} |
507 |
|
else |
508 |
|
{ |
509 |
171293 |
d_cnfStream->ensureLiteral(preprocessed); |
510 |
|
} |
511 |
197702 |
return preprocessed; |
512 |
|
} |
513 |
|
|
514 |
201891 |
Node PropEngine::getPreprocessedTerm(TNode n) |
515 |
|
{ |
516 |
|
// must preprocess |
517 |
403782 |
std::vector<TrustNode> newLemmas; |
518 |
403782 |
std::vector<Node> newSkolems; |
519 |
403782 |
TrustNode tpn = d_theoryProxy->preprocess(n, newLemmas, newSkolems); |
520 |
|
// send lemmas corresponding to the skolems introduced by preprocessing n |
521 |
403782 |
TrustNode trnNull; |
522 |
201891 |
assertLemmasInternal(trnNull, newLemmas, newSkolems, false); |
523 |
403782 |
return tpn.isNull() ? Node(n) : tpn.getNode(); |
524 |
|
} |
525 |
|
|
526 |
1586 |
Node PropEngine::getPreprocessedTerm(TNode n, |
527 |
|
std::vector<Node>& skAsserts, |
528 |
|
std::vector<Node>& sks) |
529 |
|
{ |
530 |
|
// get the preprocessed form of the term |
531 |
1586 |
Node pn = getPreprocessedTerm(n); |
532 |
|
// initialize the set of skolems and assertions to process |
533 |
3172 |
std::vector<Node> toProcessAsserts; |
534 |
3172 |
std::vector<Node> toProcess; |
535 |
1586 |
d_theoryProxy->getSkolems(pn, toProcessAsserts, toProcess); |
536 |
1586 |
size_t index = 0; |
537 |
|
// until fixed point is reached |
538 |
6380 |
while (index < toProcess.size()) |
539 |
|
{ |
540 |
3515 |
Node ka = toProcessAsserts[index]; |
541 |
3515 |
Node k = toProcess[index]; |
542 |
2397 |
index++; |
543 |
2397 |
if (std::find(sks.begin(), sks.end(), k) != sks.end()) |
544 |
|
{ |
545 |
|
// already added the skolem to the list |
546 |
1279 |
continue; |
547 |
|
} |
548 |
|
// must preprocess lemmas as well |
549 |
2236 |
Node kap = getPreprocessedTerm(ka); |
550 |
1118 |
skAsserts.push_back(kap); |
551 |
1118 |
sks.push_back(k); |
552 |
|
// get the skolems in the preprocessed form of the lemma ka |
553 |
1118 |
d_theoryProxy->getSkolems(kap, toProcessAsserts, toProcess); |
554 |
|
} |
555 |
|
// return the preprocessed term |
556 |
3172 |
return pn; |
557 |
|
} |
558 |
|
|
559 |
4869 |
void PropEngine::push() |
560 |
|
{ |
561 |
4869 |
Assert(!d_inCheckSat) << "Sat solver in solve()!"; |
562 |
4869 |
d_satSolver->push(); |
563 |
4869 |
Debug("prop") << "push()" << std::endl; |
564 |
4869 |
} |
565 |
|
|
566 |
4869 |
void PropEngine::pop() |
567 |
|
{ |
568 |
4869 |
Assert(!d_inCheckSat) << "Sat solver in solve()!"; |
569 |
4869 |
d_satSolver->pop(); |
570 |
4869 |
Debug("prop") << "pop()" << std::endl; |
571 |
4869 |
} |
572 |
|
|
573 |
15235 |
void PropEngine::resetTrail() |
574 |
|
{ |
575 |
15235 |
d_satSolver->resetTrail(); |
576 |
15235 |
Debug("prop") << "resetTrail()" << std::endl; |
577 |
15235 |
} |
578 |
|
|
579 |
18939 |
unsigned PropEngine::getAssertionLevel() const |
580 |
|
{ |
581 |
18939 |
return d_satSolver->getAssertionLevel(); |
582 |
|
} |
583 |
|
|
584 |
|
bool PropEngine::isRunning() const { return d_inCheckSat; } |
585 |
|
void PropEngine::interrupt() |
586 |
|
{ |
587 |
|
if (!d_inCheckSat) |
588 |
|
{ |
589 |
|
return; |
590 |
|
} |
591 |
|
|
592 |
|
d_interrupted = true; |
593 |
|
d_satSolver->interrupt(); |
594 |
|
Debug("prop") << "interrupt()" << std::endl; |
595 |
|
} |
596 |
|
|
597 |
2631 |
void PropEngine::spendResource(Resource r) |
598 |
|
{ |
599 |
2631 |
d_env.getResourceManager()->spendResource(r); |
600 |
2631 |
} |
601 |
|
|
602 |
|
bool PropEngine::properExplanation(TNode node, TNode expl) const |
603 |
|
{ |
604 |
|
if (!d_cnfStream->hasLiteral(node)) |
605 |
|
{ |
606 |
|
Trace("properExplanation") |
607 |
|
<< "properExplanation(): Failing because node " |
608 |
|
<< "being explained doesn't have a SAT literal ?!" << std::endl |
609 |
|
<< "properExplanation(): The node is: " << node << std::endl; |
610 |
|
return false; |
611 |
|
} |
612 |
|
|
613 |
|
SatLiteral nodeLit = d_cnfStream->getLiteral(node); |
614 |
|
|
615 |
|
for (TNode::kinded_iterator i = expl.begin(kind::AND), |
616 |
|
i_end = expl.end(kind::AND); |
617 |
|
i != i_end; |
618 |
|
++i) |
619 |
|
{ |
620 |
|
if (!d_cnfStream->hasLiteral(*i)) |
621 |
|
{ |
622 |
|
Trace("properExplanation") |
623 |
|
<< "properExplanation(): Failing because one of explanation " |
624 |
|
<< "nodes doesn't have a SAT literal" << std::endl |
625 |
|
<< "properExplanation(): The explanation node is: " << *i |
626 |
|
<< std::endl; |
627 |
|
return false; |
628 |
|
} |
629 |
|
|
630 |
|
SatLiteral iLit = d_cnfStream->getLiteral(*i); |
631 |
|
|
632 |
|
if (iLit == nodeLit) |
633 |
|
{ |
634 |
|
Trace("properExplanation") |
635 |
|
<< "properExplanation(): Failing because the node" << std::endl |
636 |
|
<< "properExplanation(): " << node << std::endl |
637 |
|
<< "properExplanation(): cannot be made to explain itself!" |
638 |
|
<< std::endl; |
639 |
|
return false; |
640 |
|
} |
641 |
|
|
642 |
|
if (!d_satSolver->properExplanation(nodeLit, iLit)) |
643 |
|
{ |
644 |
|
Trace("properExplanation") |
645 |
|
<< "properExplanation(): SAT solver told us that node" << std::endl |
646 |
|
<< "properExplanation(): " << *i << std::endl |
647 |
|
<< "properExplanation(): is not part of a proper explanation node for" |
648 |
|
<< std::endl |
649 |
|
<< "properExplanation(): " << node << std::endl |
650 |
|
<< "properExplanation(): Perhaps it one of the two isn't assigned or " |
651 |
|
"the explanation" |
652 |
|
<< std::endl |
653 |
|
<< "properExplanation(): node wasn't propagated before the node " |
654 |
|
"being explained" |
655 |
|
<< std::endl; |
656 |
|
return false; |
657 |
|
} |
658 |
|
} |
659 |
|
|
660 |
|
return true; |
661 |
|
} |
662 |
|
|
663 |
|
void PropEngine::checkProof(context::CDList<Node>* assertions) |
664 |
|
{ |
665 |
|
if (!d_env.isSatProofProducing()) |
666 |
|
{ |
667 |
|
return; |
668 |
|
} |
669 |
|
return d_ppm->checkProof(assertions); |
670 |
|
} |
671 |
|
|
672 |
18652 |
ProofCnfStream* PropEngine::getProofCnfStream() { return d_pfCnfStream.get(); } |
673 |
|
|
674 |
2823 |
std::shared_ptr<ProofNode> PropEngine::getProof() |
675 |
|
{ |
676 |
2823 |
if (!d_env.isSatProofProducing()) |
677 |
|
{ |
678 |
|
return nullptr; |
679 |
|
} |
680 |
2823 |
return d_ppm->getProof(); |
681 |
|
} |
682 |
|
|
683 |
1497247 |
bool PropEngine::isProofEnabled() const { return d_pfCnfStream != nullptr; } |
684 |
|
|
685 |
1375 |
void PropEngine::getUnsatCore(std::vector<Node>& core) |
686 |
|
{ |
687 |
1375 |
Assert(options::unsatCoresMode() == options::UnsatCoresMode::ASSUMPTIONS); |
688 |
2750 |
std::vector<SatLiteral> unsat_assumptions; |
689 |
1375 |
d_satSolver->getUnsatAssumptions(unsat_assumptions); |
690 |
8119 |
for (const SatLiteral& lit : unsat_assumptions) |
691 |
|
{ |
692 |
6744 |
core.push_back(d_cnfStream->getNode(lit)); |
693 |
|
} |
694 |
1375 |
} |
695 |
|
|
696 |
1375 |
std::shared_ptr<ProofNode> PropEngine::getRefutation() |
697 |
|
{ |
698 |
1375 |
Assert(options::unsatCoresMode() == options::UnsatCoresMode::ASSUMPTIONS); |
699 |
2750 |
std::vector<Node> core; |
700 |
1375 |
getUnsatCore(core); |
701 |
2750 |
CDProof cdp(d_env.getProofNodeManager()); |
702 |
2750 |
Node fnode = NodeManager::currentNM()->mkConst(false); |
703 |
1375 |
cdp.addStep(fnode, PfRule::SAT_REFUTATION, core, {}); |
704 |
2750 |
return cdp.getProofFor(fnode); |
705 |
|
} |
706 |
|
|
707 |
|
} // namespace prop |
708 |
29505 |
} // namespace cvc5 |