Quantifiers engine stores a pointer to the master equality engine (#4908)
[cvc5.git] / src / theory / theory_engine.cpp
1 /********************* */
2 /*! \file theory_engine.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Dejan Jovanovic, Andrew Reynolds, Morgan Deters
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 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.\endverbatim
11 **
12 ** \brief The theory engine
13 **
14 ** The theory engine.
15 **/
16
17 #include "theory/theory_engine.h"
18
19 #include <list>
20 #include <vector>
21
22 #include "base/map_util.h"
23 #include "decision/decision_engine.h"
24 #include "expr/attribute.h"
25 #include "expr/node.h"
26 #include "expr/node_algorithm.h"
27 #include "expr/node_builder.h"
28 #include "expr/node_visitor.h"
29 #include "options/bv_options.h"
30 #include "options/options.h"
31 #include "options/proof_options.h"
32 #include "options/quantifiers_options.h"
33 #include "options/theory_options.h"
34 #include "preprocessing/assertion_pipeline.h"
35 #include "proof/cnf_proof.h"
36 #include "proof/lemma_proof.h"
37 #include "proof/proof_manager.h"
38 #include "proof/theory_proof.h"
39 #include "smt/logic_exception.h"
40 #include "smt/term_formula_removal.h"
41 #include "theory/arith/arith_ite_utils.h"
42 #include "theory/bv/theory_bv_utils.h"
43 #include "theory/care_graph.h"
44 #include "theory/decision_manager.h"
45 #include "theory/ee_manager_distributed.h"
46 #include "theory/quantifiers/first_order_model.h"
47 #include "theory/quantifiers/fmf/model_engine.h"
48 #include "theory/quantifiers/theory_quantifiers.h"
49 #include "theory/quantifiers_engine.h"
50 #include "theory/rewriter.h"
51 #include "theory/theory.h"
52 #include "theory/theory_model.h"
53 #include "theory/theory_traits.h"
54 #include "theory/uf/equality_engine.h"
55 #include "util/resource_manager.h"
56
57 using namespace std;
58
59 using namespace CVC4::preprocessing;
60 using namespace CVC4::theory;
61
62 namespace CVC4 {
63
64 /* -------------------------------------------------------------------------- */
65
66 namespace theory {
67
68 /**
69 * IMPORTANT: The order of the theories is important. For example, strings
70 * depends on arith, quantifiers needs to come as the very last.
71 * Do not change this order.
72 */
73
74 #define CVC4_FOR_EACH_THEORY \
75 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BUILTIN) \
76 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BOOL) \
77 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_UF) \
78 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_ARITH) \
79 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BV) \
80 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_FP) \
81 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_ARRAYS) \
82 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_DATATYPES) \
83 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_SEP) \
84 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_SETS) \
85 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_STRINGS) \
86 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_QUANTIFIERS)
87
88 } // namespace theory
89
90 /* -------------------------------------------------------------------------- */
91
92 inline void flattenAnd(Node n, std::vector<TNode>& out){
93 Assert(n.getKind() == kind::AND);
94 for(Node::iterator i=n.begin(), i_end=n.end(); i != i_end; ++i){
95 Node curr = *i;
96 if(curr.getKind() == kind::AND){
97 flattenAnd(curr, out);
98 }else{
99 out.push_back(curr);
100 }
101 }
102 }
103
104 inline Node flattenAnd(Node n){
105 std::vector<TNode> out;
106 flattenAnd(n, out);
107 return NodeManager::currentNM()->mkNode(kind::AND, out);
108 }
109
110 /**
111 * Compute the string for a given theory id. In this module, we use
112 * THEORY_SAT_SOLVER as an id, which is not a normal id but maps to
113 * THEORY_LAST. Thus, we need our own string conversion here.
114 *
115 * @param id The theory id
116 * @return The string corresponding to the theory id
117 */
118 std::string getTheoryString(theory::TheoryId id)
119 {
120 if (id == theory::THEORY_SAT_SOLVER)
121 {
122 return "THEORY_SAT_SOLVER";
123 }
124 else
125 {
126 std::stringstream ss;
127 ss << id;
128 return ss.str();
129 }
130 }
131
132 void TheoryEngine::finishInit() {
133 // initialize the quantifiers engine
134 if (d_logicInfo.isQuantified())
135 {
136 // initialize the quantifiers engine
137 d_quantEngine = new QuantifiersEngine(d_context, d_userContext, this);
138 }
139
140 // Initialize the equality engine architecture for all theories, which
141 // includes the master equality engine.
142 d_eeDistributed.reset(new EqEngineManagerDistributed(*this));
143 d_eeDistributed->finishInit();
144
145 // Initialize the model and model builder.
146 if (d_logicInfo.isQuantified())
147 {
148 d_curr_model_builder = d_quantEngine->getModelBuilder();
149 d_curr_model = d_quantEngine->getModel();
150 } else {
151 d_curr_model = new theory::TheoryModel(
152 d_userContext, "DefaultModel", options::assignFunctionValues());
153 d_aloc_curr_model = true;
154 }
155
156 //make the default builder, e.g. in the case that the quantifiers engine does not have a model builder
157 if( d_curr_model_builder==NULL ){
158 d_curr_model_builder = new theory::TheoryEngineModelBuilder(this);
159 d_aloc_curr_model_builder = true;
160 }
161
162 // set the core equality engine on quantifiers engine
163 if (d_logicInfo.isQuantified())
164 {
165 d_quantEngine->setMasterEqualityEngine(
166 d_eeDistributed->getMasterEqualityEngine());
167 }
168
169 // finish initializing the theories
170 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId != theory::THEORY_LAST; ++ theoryId) {
171 Theory* t = d_theoryTable[theoryId];
172 if (t == nullptr)
173 {
174 continue;
175 }
176 // setup the pointers to the utilities
177 const EeTheoryInfo* eeti = d_eeDistributed->getEeTheoryInfo(theoryId);
178 Assert(eeti != nullptr);
179 // the theory's official equality engine is the one specified by the
180 // equality engine manager
181 t->setEqualityEngine(eeti->d_usedEe);
182 // set the quantifiers engine
183 t->setQuantifiersEngine(d_quantEngine);
184 // set the decision manager for the theory
185 t->setDecisionManager(d_decManager.get());
186 // finish initializing the theory
187 t->finishInit();
188 }
189 }
190
191 TheoryEngine::TheoryEngine(context::Context* context,
192 context::UserContext* userContext,
193 ResourceManager* rm,
194 RemoveTermFormulas& iteRemover,
195 const LogicInfo& logicInfo)
196 : d_propEngine(nullptr),
197 d_context(context),
198 d_userContext(userContext),
199 d_logicInfo(logicInfo),
200 d_sharedTerms(this, context),
201 d_eeDistributed(nullptr),
202 d_quantEngine(nullptr),
203 d_decManager(new DecisionManager(userContext)),
204 d_curr_model(nullptr),
205 d_aloc_curr_model(false),
206 d_curr_model_builder(nullptr),
207 d_aloc_curr_model_builder(false),
208 d_eager_model_building(false),
209 d_possiblePropagations(context),
210 d_hasPropagated(context),
211 d_inConflict(context, false),
212 d_inSatMode(false),
213 d_hasShutDown(false),
214 d_incomplete(context, false),
215 d_propagationMap(context),
216 d_propagationMapTimestamp(context, 0),
217 d_propagatedLiterals(context),
218 d_propagatedLiteralsIndex(context, 0),
219 d_atomRequests(context),
220 d_tpp(*this, iteRemover),
221 d_combineTheoriesTime("TheoryEngine::combineTheoriesTime"),
222 d_true(),
223 d_false(),
224 d_interrupted(false),
225 d_resourceManager(rm),
226 d_inPreregister(false),
227 d_factsAsserted(context, false),
228 d_preRegistrationVisitor(this, context),
229 d_sharedTermsVisitor(d_sharedTerms),
230 d_attr_handle(),
231 d_arithSubstitutionsAdded("theory::arith::zzz::arith::substitutions", 0)
232 {
233 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId != theory::THEORY_LAST;
234 ++ theoryId)
235 {
236 d_theoryTable[theoryId] = NULL;
237 d_theoryOut[theoryId] = NULL;
238 }
239
240 smtStatisticsRegistry()->registerStat(&d_combineTheoriesTime);
241 d_true = NodeManager::currentNM()->mkConst<bool>(true);
242 d_false = NodeManager::currentNM()->mkConst<bool>(false);
243
244 #ifdef CVC4_PROOF
245 ProofManager::currentPM()->initTheoryProofEngine();
246 #endif
247
248 smtStatisticsRegistry()->registerStat(&d_arithSubstitutionsAdded);
249 }
250
251 TheoryEngine::~TheoryEngine() {
252 Assert(d_hasShutDown);
253
254 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId != theory::THEORY_LAST; ++ theoryId) {
255 if(d_theoryTable[theoryId] != NULL) {
256 delete d_theoryTable[theoryId];
257 delete d_theoryOut[theoryId];
258 }
259 }
260
261 if( d_aloc_curr_model_builder ){
262 delete d_curr_model_builder;
263 }
264 if( d_aloc_curr_model ){
265 delete d_curr_model;
266 }
267
268 delete d_quantEngine;
269
270 smtStatisticsRegistry()->unregisterStat(&d_combineTheoriesTime);
271 smtStatisticsRegistry()->unregisterStat(&d_arithSubstitutionsAdded);
272 }
273
274 void TheoryEngine::interrupt() { d_interrupted = true; }
275 void TheoryEngine::preRegister(TNode preprocessed) {
276
277 Debug("theory") << "TheoryEngine::preRegister( " << preprocessed << ")" << std::endl;
278 if(Dump.isOn("missed-t-propagations")) {
279 d_possiblePropagations.push_back(preprocessed);
280 }
281 d_preregisterQueue.push(preprocessed);
282
283 if (!d_inPreregister) {
284 // We're in pre-register
285 d_inPreregister = true;
286
287 // Process the pre-registration queue
288 while (!d_preregisterQueue.empty()) {
289 // Get the next atom to pre-register
290 preprocessed = d_preregisterQueue.front();
291 d_preregisterQueue.pop();
292
293 if (d_logicInfo.isSharingEnabled() && preprocessed.getKind() == kind::EQUAL) {
294 // When sharing is enabled, we propagate from the shared terms manager also
295 d_sharedTerms.addEqualityToPropagate(preprocessed);
296 }
297
298 // the atom should not have free variables
299 Debug("theory") << "TheoryEngine::preRegister: " << preprocessed
300 << std::endl;
301 Assert(!expr::hasFreeVar(preprocessed));
302 // Pre-register the terms in the atom
303 Theory::Set theories = NodeVisitor<PreRegisterVisitor>::run(d_preRegistrationVisitor, preprocessed);
304 theories = Theory::setRemove(THEORY_BOOL, theories);
305 // Remove the top theory, if any more that means multiple theories were involved
306 bool multipleTheories = Theory::setRemove(Theory::theoryOf(preprocessed), theories);
307 TheoryId i;
308 // These checks don't work with finite model finding, because it
309 // uses Rational constants to represent cardinality constraints,
310 // even though arithmetic isn't actually involved.
311 if(!options::finiteModelFind()) {
312 while((i = Theory::setPop(theories)) != THEORY_LAST) {
313 if(!d_logicInfo.isTheoryEnabled(i)) {
314 LogicInfo newLogicInfo = d_logicInfo.getUnlockedCopy();
315 newLogicInfo.enableTheory(i);
316 newLogicInfo.lock();
317 stringstream ss;
318 ss << "The logic was specified as " << d_logicInfo.getLogicString()
319 << ", which doesn't include " << i
320 << ", but found a term in that theory." << endl
321 << "You might want to extend your logic to "
322 << newLogicInfo.getLogicString() << endl;
323 throw LogicException(ss.str());
324 }
325 }
326 }
327 if (multipleTheories) {
328 // Collect the shared terms if there are multiple theories
329 NodeVisitor<SharedTermsVisitor>::run(d_sharedTermsVisitor, preprocessed);
330 }
331 }
332
333 // Leaving pre-register
334 d_inPreregister = false;
335 }
336 }
337
338 void TheoryEngine::printAssertions(const char* tag) {
339 if (Trace.isOn(tag)) {
340
341 for (TheoryId theoryId = THEORY_FIRST; theoryId < THEORY_LAST; ++theoryId) {
342 Theory* theory = d_theoryTable[theoryId];
343 if (theory && d_logicInfo.isTheoryEnabled(theoryId)) {
344 Trace(tag) << "--------------------------------------------" << endl;
345 Trace(tag) << "Assertions of " << theory->getId() << ": " << endl;
346 {
347 context::CDList<Assertion>::const_iterator it = theory->facts_begin(),
348 it_end =
349 theory->facts_end();
350 for (unsigned i = 0; it != it_end; ++it, ++i)
351 {
352 if ((*it).d_isPreregistered)
353 {
354 Trace(tag) << "[" << i << "]: ";
355 }
356 else
357 {
358 Trace(tag) << "(" << i << "): ";
359 }
360 Trace(tag) << (*it).d_assertion << endl;
361 }
362 }
363
364 if (d_logicInfo.isSharingEnabled()) {
365 Trace(tag) << "Shared terms of " << theory->getId() << ": " << endl;
366 context::CDList<TNode>::const_iterator it = theory->shared_terms_begin(), it_end = theory->shared_terms_end();
367 for (unsigned i = 0; it != it_end; ++ it, ++i) {
368 Trace(tag) << "[" << i << "]: " << (*it) << endl;
369 }
370 }
371 }
372 }
373 }
374 }
375
376 void TheoryEngine::dumpAssertions(const char* tag) {
377 if (Dump.isOn(tag)) {
378 Dump(tag) << CommentCommand("Starting completeness check");
379 for (TheoryId theoryId = THEORY_FIRST; theoryId < THEORY_LAST; ++theoryId) {
380 Theory* theory = d_theoryTable[theoryId];
381 if (theory && d_logicInfo.isTheoryEnabled(theoryId)) {
382 Dump(tag) << CommentCommand("Completeness check");
383 Dump(tag) << PushCommand();
384
385 // Dump the shared terms
386 if (d_logicInfo.isSharingEnabled()) {
387 Dump(tag) << CommentCommand("Shared terms");
388 context::CDList<TNode>::const_iterator it = theory->shared_terms_begin(), it_end = theory->shared_terms_end();
389 for (unsigned i = 0; it != it_end; ++ it, ++i) {
390 stringstream ss;
391 ss << (*it);
392 Dump(tag) << CommentCommand(ss.str());
393 }
394 }
395
396 // Dump the assertions
397 Dump(tag) << CommentCommand("Assertions");
398 context::CDList<Assertion>::const_iterator it = theory->facts_begin(), it_end = theory->facts_end();
399 for (; it != it_end; ++ it) {
400 // Get the assertion
401 Node assertionNode = (*it).d_assertion;
402 // Purify all the terms
403
404 if ((*it).d_isPreregistered)
405 {
406 Dump(tag) << CommentCommand("Preregistered");
407 }
408 else
409 {
410 Dump(tag) << CommentCommand("Shared assertion");
411 }
412 Dump(tag) << AssertCommand(assertionNode.toExpr());
413 }
414 Dump(tag) << CheckSatCommand();
415
416 Dump(tag) << PopCommand();
417 }
418 }
419 }
420 }
421
422 /**
423 * Check all (currently-active) theories for conflicts.
424 * @param effort the effort level to use
425 */
426 void TheoryEngine::check(Theory::Effort effort) {
427 // spendResource();
428
429 // Reset the interrupt flag
430 d_interrupted = false;
431
432 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
433 #undef CVC4_FOR_EACH_THEORY_STATEMENT
434 #endif
435 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
436 if (theory::TheoryTraits<THEORY>::hasCheck && d_logicInfo.isTheoryEnabled(THEORY)) { \
437 theoryOf(THEORY)->check(effort); \
438 if (d_inConflict) { \
439 Debug("conflict") << THEORY << " in conflict. " << std::endl; \
440 break; \
441 } \
442 }
443
444 // Do the checking
445 try {
446
447 // Mark the output channel unused (if this is FULL_EFFORT, and nothing
448 // is done by the theories, no additional check will be needed)
449 d_outputChannelUsed = false;
450
451 // Mark the lemmas flag (no lemmas added)
452 d_lemmasAdded = false;
453
454 Debug("theory") << "TheoryEngine::check(" << effort << "): d_factsAsserted = " << (d_factsAsserted ? "true" : "false") << endl;
455
456 // If in full effort, we have a fake new assertion just to jumpstart the checking
457 if (Theory::fullEffort(effort)) {
458 d_factsAsserted = true;
459 }
460
461 // Check until done
462 while (d_factsAsserted && !d_inConflict && !d_lemmasAdded) {
463
464 Debug("theory") << "TheoryEngine::check(" << effort << "): running check" << endl;
465
466 Trace("theory::assertions") << endl;
467 if (Trace.isOn("theory::assertions")) {
468 printAssertions("theory::assertions");
469 }
470
471 if(Theory::fullEffort(effort)) {
472 Trace("theory::assertions::fulleffort") << endl;
473 if (Trace.isOn("theory::assertions::fulleffort")) {
474 printAssertions("theory::assertions::fulleffort");
475 }
476 }
477
478 // Note that we've discharged all the facts
479 d_factsAsserted = false;
480
481 // Do the checking
482 CVC4_FOR_EACH_THEORY;
483
484 if(Dump.isOn("missed-t-conflicts")) {
485 Dump("missed-t-conflicts")
486 << CommentCommand("Completeness check for T-conflicts; expect sat")
487 << CheckSatCommand();
488 }
489
490 Debug("theory") << "TheoryEngine::check(" << effort << "): running propagation after the initial check" << endl;
491
492 // We are still satisfiable, propagate as much as possible
493 propagate(effort);
494
495 // We do combination if all has been processed and we are in fullcheck
496 if (Theory::fullEffort(effort) && d_logicInfo.isSharingEnabled() && !d_factsAsserted && !d_lemmasAdded && !d_inConflict) {
497 // Do the combination
498 Debug("theory") << "TheoryEngine::check(" << effort << "): running combination" << endl;
499 combineTheories();
500 if(d_logicInfo.isQuantified()){
501 d_quantEngine->notifyCombineTheories();
502 }
503 }
504 }
505
506 // Must consult quantifiers theory for last call to ensure sat, or otherwise add a lemma
507 if( Theory::fullEffort(effort) && ! d_inConflict && ! needCheck() ) {
508 Trace("theory::assertions-model") << endl;
509 if (Trace.isOn("theory::assertions-model")) {
510 printAssertions("theory::assertions-model");
511 }
512 //checks for theories requiring the model go at last call
513 d_curr_model->reset();
514 for (TheoryId theoryId = THEORY_FIRST; theoryId < THEORY_LAST; ++theoryId) {
515 if( theoryId!=THEORY_QUANTIFIERS ){
516 Theory* theory = d_theoryTable[theoryId];
517 if (theory && d_logicInfo.isTheoryEnabled(theoryId)) {
518 if( theory->needsCheckLastEffort() ){
519 if( !d_curr_model->isBuilt() ){
520 if( !d_curr_model_builder->buildModel(d_curr_model) ){
521 break;
522 }
523 }
524 theory->check(Theory::EFFORT_LAST_CALL);
525 }
526 }
527 }
528 }
529 if (!d_inConflict)
530 {
531 if(d_logicInfo.isQuantified()) {
532 // quantifiers engine must check at last call effort
533 d_quantEngine->check(Theory::EFFORT_LAST_CALL);
534 }
535 }
536 if (!d_inConflict && !needCheck())
537 {
538 // If d_eager_model_building is false, then we only mark that we
539 // are in "SAT mode". We build the model later only if the user asks
540 // for it via getBuiltModel.
541 d_inSatMode = true;
542 if (d_eager_model_building && !d_curr_model->isBuilt())
543 {
544 d_curr_model_builder->buildModel(d_curr_model);
545 }
546 }
547 }
548
549 Debug("theory") << "TheoryEngine::check(" << effort << "): done, we are " << (d_inConflict ? "unsat" : "sat") << (d_lemmasAdded ? " with new lemmas" : " with no new lemmas");
550 Debug("theory") << ", need check = " << (needCheck() ? "YES" : "NO") << endl;
551
552 if( Theory::fullEffort(effort) && !d_inConflict && !needCheck()) {
553 // case where we are about to answer SAT, the master equality engine,
554 // if it exists, must be consistent.
555 eq::EqualityEngine* mee = d_eeDistributed->getMasterEqualityEngine();
556 if (mee != NULL)
557 {
558 AlwaysAssert(mee->consistent());
559 }
560 if (d_curr_model->isBuilt())
561 {
562 // model construction should always succeed unless lemmas were added
563 AlwaysAssert(d_curr_model->isBuiltSuccess());
564 if (options::produceModels())
565 {
566 // Do post-processing of model from the theories (used for THEORY_SEP
567 // to construct heap model)
568 postProcessModel(d_curr_model);
569 // also call the model builder's post-process model
570 d_curr_model_builder->postProcessModel(d_incomplete.get(),
571 d_curr_model);
572 }
573 }
574 }
575 } catch(const theory::Interrupted&) {
576 Trace("theory") << "TheoryEngine::check() => interrupted" << endl;
577 }
578 // If fulleffort, check all theories
579 if(Dump.isOn("theory::fullcheck") && Theory::fullEffort(effort)) {
580 if (!d_inConflict && !needCheck()) {
581 dumpAssertions("theory::fullcheck");
582 }
583 }
584 }
585
586 void TheoryEngine::combineTheories() {
587
588 Trace("combineTheories") << "TheoryEngine::combineTheories()" << endl;
589
590 TimerStat::CodeTimer combineTheoriesTimer(d_combineTheoriesTime);
591
592 // Care graph we'll be building
593 CareGraph careGraph;
594
595 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
596 #undef CVC4_FOR_EACH_THEORY_STATEMENT
597 #endif
598 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
599 if (theory::TheoryTraits<THEORY>::isParametric && d_logicInfo.isTheoryEnabled(THEORY)) { \
600 theoryOf(THEORY)->getCareGraph(&careGraph); \
601 }
602
603 // Call on each parametric theory to give us its care graph
604 CVC4_FOR_EACH_THEORY;
605
606 Trace("combineTheories") << "TheoryEngine::combineTheories(): care graph size = " << careGraph.size() << endl;
607
608 // Now add splitters for the ones we are interested in
609 CareGraph::const_iterator care_it = careGraph.begin();
610 CareGraph::const_iterator care_it_end = careGraph.end();
611
612 for (; care_it != care_it_end; ++ care_it) {
613 const CarePair& carePair = *care_it;
614
615 Debug("combineTheories")
616 << "TheoryEngine::combineTheories(): checking " << carePair.d_a << " = "
617 << carePair.d_b << " from " << carePair.d_theory << endl;
618
619 Assert(d_sharedTerms.isShared(carePair.d_a) || carePair.d_a.isConst());
620 Assert(d_sharedTerms.isShared(carePair.d_b) || carePair.d_b.isConst());
621
622 // The equality in question (order for no repetition)
623 Node equality = carePair.d_a.eqNode(carePair.d_b);
624 // EqualityStatus es = getEqualityStatus(carePair.d_a, carePair.d_b);
625 // Debug("combineTheories") << "TheoryEngine::combineTheories(): " <<
626 // (es == EQUALITY_TRUE_AND_PROPAGATED ? "EQUALITY_TRUE_AND_PROPAGATED" :
627 // es == EQUALITY_FALSE_AND_PROPAGATED ? "EQUALITY_FALSE_AND_PROPAGATED" :
628 // es == EQUALITY_TRUE ? "EQUALITY_TRUE" :
629 // es == EQUALITY_FALSE ? "EQUALITY_FALSE" :
630 // es == EQUALITY_TRUE_IN_MODEL ? "EQUALITY_TRUE_IN_MODEL" :
631 // es == EQUALITY_FALSE_IN_MODEL ? "EQUALITY_FALSE_IN_MODEL" :
632 // es == EQUALITY_UNKNOWN ? "EQUALITY_UNKNOWN" :
633 // "Unexpected case") << endl;
634
635 // We need to split on it
636 Debug("combineTheories") << "TheoryEngine::combineTheories(): requesting a split " << endl;
637
638 lemma(equality.orNode(equality.notNode()),
639 RULE_INVALID,
640 false,
641 LemmaProperty::NONE,
642 carePair.d_theory);
643
644 // This code is supposed to force preference to follow what the theory models already have
645 // but it doesn't seem to make a big difference - need to explore more -Clark
646 // if (true) {
647 // if (es == EQUALITY_TRUE || es == EQUALITY_TRUE_IN_MODEL) {
648 Node e = ensureLiteral(equality);
649 d_propEngine->requirePhase(e, true);
650 // }
651 // else if (es == EQUALITY_FALSE_IN_MODEL) {
652 // Node e = ensureLiteral(equality);
653 // d_propEngine->requirePhase(e, false);
654 // }
655 // }
656 }
657 }
658
659 void TheoryEngine::propagate(Theory::Effort effort) {
660 // Reset the interrupt flag
661 d_interrupted = false;
662
663 // Definition of the statement that is to be run by every theory
664 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
665 #undef CVC4_FOR_EACH_THEORY_STATEMENT
666 #endif
667 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
668 if (theory::TheoryTraits<THEORY>::hasPropagate && d_logicInfo.isTheoryEnabled(THEORY)) { \
669 theoryOf(THEORY)->propagate(effort); \
670 }
671
672 // Reset the interrupt flag
673 d_interrupted = false;
674
675 // Propagate for each theory using the statement above
676 CVC4_FOR_EACH_THEORY;
677
678 if(Dump.isOn("missed-t-propagations")) {
679 for(unsigned i = 0; i < d_possiblePropagations.size(); ++i) {
680 Node atom = d_possiblePropagations[i];
681 bool value;
682 if(d_propEngine->hasValue(atom, value)) {
683 continue;
684 }
685 // Doesn't have a value, check it (and the negation)
686 if(d_hasPropagated.find(atom) == d_hasPropagated.end()) {
687 Dump("missed-t-propagations")
688 << CommentCommand("Completeness check for T-propagations; expect invalid")
689 << EchoCommand(atom.toString())
690 << QueryCommand(atom.toExpr())
691 << EchoCommand(atom.notNode().toString())
692 << QueryCommand(atom.notNode().toExpr());
693 }
694 }
695 }
696 }
697
698 Node TheoryEngine::getNextDecisionRequest()
699 {
700 return d_decManager->getNextDecisionRequest();
701 }
702
703 bool TheoryEngine::properConflict(TNode conflict) const {
704 bool value;
705 if (conflict.getKind() == kind::AND) {
706 for (unsigned i = 0; i < conflict.getNumChildren(); ++ i) {
707 if (! getPropEngine()->hasValue(conflict[i], value)) {
708 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
709 << conflict[i] << endl;
710 return false;
711 }
712 if (! value) {
713 Debug("properConflict") << "Bad conflict is due to false atom: "
714 << conflict[i] << endl;
715 return false;
716 }
717 if (conflict[i] != Rewriter::rewrite(conflict[i])) {
718 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
719 << conflict[i] << " vs " << Rewriter::rewrite(conflict[i]) << endl;
720 return false;
721 }
722 }
723 } else {
724 if (! getPropEngine()->hasValue(conflict, value)) {
725 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
726 << conflict << endl;
727 return false;
728 }
729 if(! value) {
730 Debug("properConflict") << "Bad conflict is due to false atom: "
731 << conflict << endl;
732 return false;
733 }
734 if (conflict != Rewriter::rewrite(conflict)) {
735 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
736 << conflict << " vs " << Rewriter::rewrite(conflict) << endl;
737 return false;
738 }
739 }
740 return true;
741 }
742
743 bool TheoryEngine::properPropagation(TNode lit) const {
744 if(!getPropEngine()->isSatLiteral(lit)) {
745 return false;
746 }
747 bool b;
748 return !getPropEngine()->hasValue(lit, b);
749 }
750
751 bool TheoryEngine::properExplanation(TNode node, TNode expl) const {
752 // Explanation must be either a conjunction of true literals that have true SAT values already
753 // or a singled literal that has a true SAT value already.
754 if (expl.getKind() == kind::AND) {
755 for (unsigned i = 0; i < expl.getNumChildren(); ++ i) {
756 bool value;
757 if (!d_propEngine->hasValue(expl[i], value) || !value) {
758 return false;
759 }
760 }
761 } else {
762 bool value;
763 return d_propEngine->hasValue(expl, value) && value;
764 }
765 return true;
766 }
767
768 bool TheoryEngine::collectModelInfo(theory::TheoryModel* m)
769 {
770 //have shared term engine collectModelInfo
771 // d_sharedTerms.collectModelInfo( m );
772 // Consult each active theory to get all relevant information
773 // concerning the model.
774 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST; ++theoryId) {
775 if(d_logicInfo.isTheoryEnabled(theoryId)) {
776 Trace("model-builder") << " CollectModelInfo on theory: " << theoryId << endl;
777 if (!d_theoryTable[theoryId]->collectModelInfo(m))
778 {
779 return false;
780 }
781 }
782 }
783 Trace("model-builder") << " CollectModelInfo boolean variables" << std::endl;
784 // Get the Boolean variables
785 vector<TNode> boolVars;
786 d_propEngine->getBooleanVariables(boolVars);
787 vector<TNode>::iterator it, iend = boolVars.end();
788 bool hasValue, value;
789 for (it = boolVars.begin(); it != iend; ++it) {
790 TNode var = *it;
791 hasValue = d_propEngine->hasValue(var, value);
792 // TODO: Assert that hasValue is true?
793 if (!hasValue) {
794 Trace("model-builder-assertions")
795 << " has no value : " << var << std::endl;
796 value = false;
797 }
798 Trace("model-builder-assertions") << "(assert" << (value ? " " : " (not ") << var << (value ? ");" : "));") << endl;
799 if (!m->assertPredicate(var, value))
800 {
801 return false;
802 }
803 }
804 return true;
805 }
806
807 void TheoryEngine::postProcessModel( theory::TheoryModel* m ){
808 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST; ++theoryId) {
809 if(d_logicInfo.isTheoryEnabled(theoryId)) {
810 Trace("model-builder-debug") << " PostProcessModel on theory: " << theoryId << endl;
811 d_theoryTable[theoryId]->postProcessModel( m );
812 }
813 }
814 }
815
816 TheoryModel* TheoryEngine::getModel() {
817 return d_curr_model;
818 }
819
820 TheoryModel* TheoryEngine::getBuiltModel()
821 {
822 if (!d_curr_model->isBuilt())
823 {
824 // If this method was called, we should be in SAT mode, and produceModels
825 // should be true.
826 AlwaysAssert(options::produceModels());
827 if (!d_inSatMode)
828 {
829 // not available, perhaps due to interuption.
830 return nullptr;
831 }
832 // must build model at this point
833 d_curr_model_builder->buildModel(d_curr_model);
834 }
835 return d_curr_model;
836 }
837
838 bool TheoryEngine::getSynthSolutions(
839 std::map<Node, std::map<Node, Node>>& sol_map)
840 {
841 if (d_quantEngine)
842 {
843 return d_quantEngine->getSynthSolutions(sol_map);
844 }
845 // we are not in a quantified logic, there is no synthesis solution
846 return false;
847 }
848
849 bool TheoryEngine::presolve() {
850 // Reset the interrupt flag
851 d_interrupted = false;
852
853 // Reset the decision manager. This clears its decision strategies that are
854 // no longer valid in this user context.
855 d_decManager->presolve();
856
857 try {
858 // Definition of the statement that is to be run by every theory
859 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
860 #undef CVC4_FOR_EACH_THEORY_STATEMENT
861 #endif
862 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
863 if (theory::TheoryTraits<THEORY>::hasPresolve) { \
864 theoryOf(THEORY)->presolve(); \
865 if(d_inConflict) { \
866 return true; \
867 } \
868 }
869
870 // Presolve for each theory using the statement above
871 CVC4_FOR_EACH_THEORY;
872 } catch(const theory::Interrupted&) {
873 Trace("theory") << "TheoryEngine::presolve() => interrupted" << endl;
874 }
875 // return whether we have a conflict
876 return false;
877 }/* TheoryEngine::presolve() */
878
879 void TheoryEngine::postsolve() {
880 // no longer in SAT mode
881 d_inSatMode = false;
882 // Reset the interrupt flag
883 d_interrupted = false;
884 bool CVC4_UNUSED wasInConflict = d_inConflict;
885
886 try {
887 // Definition of the statement that is to be run by every theory
888 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
889 #undef CVC4_FOR_EACH_THEORY_STATEMENT
890 #endif
891 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
892 if (theory::TheoryTraits<THEORY>::hasPostsolve) \
893 { \
894 theoryOf(THEORY)->postsolve(); \
895 Assert(!d_inConflict || wasInConflict) \
896 << "conflict raised during postsolve()"; \
897 }
898
899 // Postsolve for each theory using the statement above
900 CVC4_FOR_EACH_THEORY;
901 } catch(const theory::Interrupted&) {
902 Trace("theory") << "TheoryEngine::postsolve() => interrupted" << endl;
903 }
904 }/* TheoryEngine::postsolve() */
905
906
907 void TheoryEngine::notifyRestart() {
908 // Reset the interrupt flag
909 d_interrupted = false;
910
911 // Definition of the statement that is to be run by every theory
912 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
913 #undef CVC4_FOR_EACH_THEORY_STATEMENT
914 #endif
915 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
916 if (theory::TheoryTraits<THEORY>::hasNotifyRestart && d_logicInfo.isTheoryEnabled(THEORY)) { \
917 theoryOf(THEORY)->notifyRestart(); \
918 }
919
920 // notify each theory using the statement above
921 CVC4_FOR_EACH_THEORY;
922 }
923
924 void TheoryEngine::ppStaticLearn(TNode in, NodeBuilder<>& learned) {
925 // Reset the interrupt flag
926 d_interrupted = false;
927
928 // Definition of the statement that is to be run by every theory
929 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
930 #undef CVC4_FOR_EACH_THEORY_STATEMENT
931 #endif
932 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
933 if (theory::TheoryTraits<THEORY>::hasPpStaticLearn) { \
934 theoryOf(THEORY)->ppStaticLearn(in, learned); \
935 }
936
937 // static learning for each theory using the statement above
938 CVC4_FOR_EACH_THEORY;
939 }
940
941 void TheoryEngine::shutdown() {
942 // Set this first; if a Theory shutdown() throws an exception,
943 // at least the destruction of the TheoryEngine won't confound
944 // matters.
945 d_hasShutDown = true;
946
947 // Shutdown all the theories
948 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST; ++theoryId) {
949 if(d_theoryTable[theoryId]) {
950 theoryOf(theoryId)->shutdown();
951 }
952 }
953
954 d_tpp.clearCache();
955 }
956
957 theory::Theory::PPAssertStatus TheoryEngine::solve(TNode literal, SubstitutionMap& substitutionOut) {
958 // Reset the interrupt flag
959 d_interrupted = false;
960
961 TNode atom = literal.getKind() == kind::NOT ? literal[0] : literal;
962 Trace("theory::solve") << "TheoryEngine::solve(" << literal << "): solving with " << theoryOf(atom)->getId() << endl;
963
964 if(! d_logicInfo.isTheoryEnabled(Theory::theoryOf(atom)) &&
965 Theory::theoryOf(atom) != THEORY_SAT_SOLVER) {
966 stringstream ss;
967 ss << "The logic was specified as " << d_logicInfo.getLogicString()
968 << ", which doesn't include " << Theory::theoryOf(atom)
969 << ", but got a preprocessing-time fact for that theory." << endl
970 << "The fact:" << endl
971 << literal;
972 throw LogicException(ss.str());
973 }
974
975 Theory::PPAssertStatus solveStatus = theoryOf(atom)->ppAssert(literal, substitutionOut);
976 Trace("theory::solve") << "TheoryEngine::solve(" << literal << ") => " << solveStatus << endl;
977 return solveStatus;
978 }
979
980 void TheoryEngine::preprocessStart() { d_tpp.clearCache(); }
981
982 Node TheoryEngine::preprocess(TNode assertion) {
983 return d_tpp.theoryPreprocess(assertion);
984 }
985
986 void TheoryEngine::notifyPreprocessedAssertions(
987 const std::vector<Node>& assertions) {
988 // call all the theories
989 for (TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST;
990 ++theoryId) {
991 if (d_theoryTable[theoryId]) {
992 theoryOf(theoryId)->ppNotifyAssertions(assertions);
993 }
994 }
995 }
996
997 bool TheoryEngine::markPropagation(TNode assertion, TNode originalAssertion, theory::TheoryId toTheoryId, theory::TheoryId fromTheoryId) {
998
999 // What and where we are asserting
1000 NodeTheoryPair toAssert(assertion, toTheoryId, d_propagationMapTimestamp);
1001 // What and where it came from
1002 NodeTheoryPair toExplain(originalAssertion, fromTheoryId, d_propagationMapTimestamp);
1003
1004 // See if the theory already got this literal
1005 PropagationMap::const_iterator find = d_propagationMap.find(toAssert);
1006 if (find != d_propagationMap.end()) {
1007 // The theory already knows this
1008 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): already there" << endl;
1009 return false;
1010 }
1011
1012 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): marking [" << d_propagationMapTimestamp << "] " << assertion << ", " << toTheoryId << " from " << originalAssertion << ", " << fromTheoryId << endl;
1013
1014 // Mark the propagation
1015 d_propagationMap[toAssert] = toExplain;
1016 d_propagationMapTimestamp = d_propagationMapTimestamp + 1;
1017
1018 return true;
1019 }
1020
1021
1022 void TheoryEngine::assertToTheory(TNode assertion, TNode originalAssertion, theory::TheoryId toTheoryId, theory::TheoryId fromTheoryId) {
1023
1024 Trace("theory::assertToTheory") << "TheoryEngine::assertToTheory(" << assertion << ", " << originalAssertion << "," << toTheoryId << ", " << fromTheoryId << ")" << endl;
1025
1026 Assert(toTheoryId != fromTheoryId);
1027 if(toTheoryId != THEORY_SAT_SOLVER &&
1028 ! d_logicInfo.isTheoryEnabled(toTheoryId)) {
1029 stringstream ss;
1030 ss << "The logic was specified as " << d_logicInfo.getLogicString()
1031 << ", which doesn't include " << toTheoryId
1032 << ", but got an asserted fact to that theory." << endl
1033 << "The fact:" << endl
1034 << assertion;
1035 throw LogicException(ss.str());
1036 }
1037
1038 if (d_inConflict) {
1039 return;
1040 }
1041
1042 // If sharing is disabled, things are easy
1043 if (!d_logicInfo.isSharingEnabled()) {
1044 Assert(assertion == originalAssertion);
1045 if (fromTheoryId == THEORY_SAT_SOLVER) {
1046 // Send to the apropriate theory
1047 theory::Theory* toTheory = theoryOf(toTheoryId);
1048 // We assert it, and we know it's preregistereed
1049 toTheory->assertFact(assertion, true);
1050 // Mark that we have more information
1051 d_factsAsserted = true;
1052 } else {
1053 Assert(toTheoryId == THEORY_SAT_SOLVER);
1054 // Check for propositional conflict
1055 bool value;
1056 if (d_propEngine->hasValue(assertion, value)) {
1057 if (!value) {
1058 Trace("theory::propagate") << "TheoryEngine::assertToTheory(" << assertion << ", " << toTheoryId << ", " << fromTheoryId << "): conflict (no sharing)" << endl;
1059 Trace("dtview::conflict")
1060 << ":THEORY-CONFLICT: " << assertion << std::endl;
1061 d_inConflict = true;
1062 } else {
1063 return;
1064 }
1065 }
1066 d_propagatedLiterals.push_back(assertion);
1067 }
1068 return;
1069 }
1070
1071 // Polarity of the assertion
1072 bool polarity = assertion.getKind() != kind::NOT;
1073
1074 // Atom of the assertion
1075 TNode atom = polarity ? assertion : assertion[0];
1076
1077 // If sending to the shared terms database, it's also simple
1078 if (toTheoryId == THEORY_BUILTIN) {
1079 Assert(atom.getKind() == kind::EQUAL)
1080 << "atom should be an EQUALity, not `" << atom << "'";
1081 if (markPropagation(assertion, originalAssertion, toTheoryId, fromTheoryId)) {
1082 d_sharedTerms.assertEquality(atom, polarity, assertion);
1083 }
1084 return;
1085 }
1086
1087 // Things from the SAT solver are already normalized, so they go
1088 // directly to the apropriate theory
1089 if (fromTheoryId == THEORY_SAT_SOLVER) {
1090 // We know that this is normalized, so just send it off to the theory
1091 if (markPropagation(assertion, originalAssertion, toTheoryId, fromTheoryId)) {
1092 // Is it preregistered
1093 bool preregistered = d_propEngine->isSatLiteral(assertion) && Theory::theoryOf(assertion) == toTheoryId;
1094 // We assert it
1095 theoryOf(toTheoryId)->assertFact(assertion, preregistered);
1096 // Mark that we have more information
1097 d_factsAsserted = true;
1098 }
1099 return;
1100 }
1101
1102 // Propagations to the SAT solver are just enqueued for pickup by
1103 // the SAT solver later
1104 if (toTheoryId == THEORY_SAT_SOLVER) {
1105 if (markPropagation(assertion, originalAssertion, toTheoryId, fromTheoryId)) {
1106 // Enqueue for propagation to the SAT solver
1107 d_propagatedLiterals.push_back(assertion);
1108 // Check for propositional conflicts
1109 bool value;
1110 if (d_propEngine->hasValue(assertion, value) && !value) {
1111 Trace("theory::propagate")
1112 << "TheoryEngine::assertToTheory(" << assertion << ", "
1113 << toTheoryId << ", " << fromTheoryId << "): conflict (sharing)"
1114 << endl;
1115 Trace("dtview::conflict")
1116 << ":THEORY-CONFLICT: " << assertion << std::endl;
1117 d_inConflict = true;
1118 }
1119 }
1120 return;
1121 }
1122
1123 Assert(atom.getKind() == kind::EQUAL);
1124
1125 // Normalize
1126 Node normalizedLiteral = Rewriter::rewrite(assertion);
1127
1128 // See if it rewrites false directly -> conflict
1129 if (normalizedLiteral.isConst()) {
1130 if (!normalizedLiteral.getConst<bool>()) {
1131 // Mark the propagation for explanations
1132 if (markPropagation(normalizedLiteral, originalAssertion, toTheoryId, fromTheoryId)) {
1133 // Get the explanation (conflict will figure out where it came from)
1134 conflict(normalizedLiteral, toTheoryId);
1135 } else {
1136 Unreachable();
1137 }
1138 return;
1139 }
1140 }
1141
1142 // Try and assert (note that we assert the non-normalized one)
1143 if (markPropagation(assertion, originalAssertion, toTheoryId, fromTheoryId)) {
1144 // Check if has been pre-registered with the theory
1145 bool preregistered = d_propEngine->isSatLiteral(assertion) && Theory::theoryOf(assertion) == toTheoryId;
1146 // Assert away
1147 theoryOf(toTheoryId)->assertFact(assertion, preregistered);
1148 d_factsAsserted = true;
1149 }
1150
1151 return;
1152 }
1153
1154 void TheoryEngine::assertFact(TNode literal)
1155 {
1156 Trace("theory") << "TheoryEngine::assertFact(" << literal << ")" << endl;
1157
1158 // spendResource();
1159
1160 // If we're in conflict, nothing to do
1161 if (d_inConflict) {
1162 return;
1163 }
1164
1165 // Get the atom
1166 bool polarity = literal.getKind() != kind::NOT;
1167 TNode atom = polarity ? literal : literal[0];
1168
1169 if (d_logicInfo.isSharingEnabled()) {
1170
1171 // If any shared terms, it's time to do sharing work
1172 if (d_sharedTerms.hasSharedTerms(atom)) {
1173 // Notify the theories the shared terms
1174 SharedTermsDatabase::shared_terms_iterator it = d_sharedTerms.begin(atom);
1175 SharedTermsDatabase::shared_terms_iterator it_end = d_sharedTerms.end(atom);
1176 for (; it != it_end; ++ it) {
1177 TNode term = *it;
1178 Theory::Set theories = d_sharedTerms.getTheoriesToNotify(atom, term);
1179 for (TheoryId id = THEORY_FIRST; id != THEORY_LAST; ++ id) {
1180 if (Theory::setContains(id, theories)) {
1181 theoryOf(id)->addSharedTermInternal(term);
1182 }
1183 }
1184 d_sharedTerms.markNotified(term, theories);
1185 }
1186 }
1187
1188 // If it's an equality, assert it to the shared term manager, even though the terms are not
1189 // yet shared. As the terms become shared later, the shared terms manager will then add them
1190 // to the assert the equality to the interested theories
1191 if (atom.getKind() == kind::EQUAL) {
1192 // Assert it to the the owning theory
1193 assertToTheory(literal, literal, /* to */ Theory::theoryOf(atom), /* from */ THEORY_SAT_SOLVER);
1194 // Shared terms manager will assert to interested theories directly, as the terms become shared
1195 assertToTheory(literal, literal, /* to */ THEORY_BUILTIN, /* from */ THEORY_SAT_SOLVER);
1196
1197 // Now, let's check for any atom triggers from lemmas
1198 AtomRequests::atom_iterator it = d_atomRequests.getAtomIterator(atom);
1199 while (!it.done()) {
1200 const AtomRequests::Request& request = it.get();
1201 Node toAssert =
1202 polarity ? (Node)request.d_atom : request.d_atom.notNode();
1203 Debug("theory::atoms") << "TheoryEngine::assertFact(" << literal << "): sending requested " << toAssert << endl;
1204 assertToTheory(
1205 toAssert, literal, request.d_toTheory, THEORY_SAT_SOLVER);
1206 it.next();
1207 }
1208
1209 } else {
1210 // Not an equality, just assert to the appropriate theory
1211 assertToTheory(literal, literal, /* to */ Theory::theoryOf(atom), /* from */ THEORY_SAT_SOLVER);
1212 }
1213 } else {
1214 // Assert the fact to the appropriate theory directly
1215 assertToTheory(literal, literal, /* to */ Theory::theoryOf(atom), /* from */ THEORY_SAT_SOLVER);
1216 }
1217 }
1218
1219 bool TheoryEngine::propagate(TNode literal, theory::TheoryId theory) {
1220
1221 Debug("theory::propagate") << "TheoryEngine::propagate(" << literal << ", " << theory << ")" << endl;
1222
1223 Trace("dtview::prop") << std::string(d_context->getLevel(), ' ')
1224 << ":THEORY-PROP: " << literal << endl;
1225
1226 // spendResource();
1227
1228 if(Dump.isOn("t-propagations")) {
1229 Dump("t-propagations") << CommentCommand("negation of theory propagation: expect valid")
1230 << QueryCommand(literal.toExpr());
1231 }
1232 if(Dump.isOn("missed-t-propagations")) {
1233 d_hasPropagated.insert(literal);
1234 }
1235
1236 // Get the atom
1237 bool polarity = literal.getKind() != kind::NOT;
1238 TNode atom = polarity ? literal : literal[0];
1239
1240 if (d_logicInfo.isSharingEnabled() && atom.getKind() == kind::EQUAL) {
1241 if (d_propEngine->isSatLiteral(literal)) {
1242 // We propagate SAT literals to SAT
1243 assertToTheory(literal, literal, /* to */ THEORY_SAT_SOLVER, /* from */ theory);
1244 }
1245 if (theory != THEORY_BUILTIN) {
1246 // Assert to the shared terms database
1247 assertToTheory(literal, literal, /* to */ THEORY_BUILTIN, /* from */ theory);
1248 }
1249 } else {
1250 // We could be propagating a unit-clause lemma. In this case, we need to provide a
1251 // recipe.
1252 // TODO: Consider putting this someplace else? This is the only refence to the proof
1253 // manager in this class.
1254
1255 PROOF({
1256 LemmaProofRecipe proofRecipe;
1257 proofRecipe.addBaseAssertion(literal);
1258
1259 Node emptyNode;
1260 LemmaProofRecipe::ProofStep proofStep(theory, emptyNode);
1261 proofStep.addAssertion(literal);
1262 proofRecipe.addStep(proofStep);
1263
1264 ProofManager::getCnfProof()->setProofRecipe(&proofRecipe);
1265 });
1266
1267 // Just send off to the SAT solver
1268 Assert(d_propEngine->isSatLiteral(literal));
1269 assertToTheory(literal, literal, /* to */ THEORY_SAT_SOLVER, /* from */ theory);
1270 }
1271
1272 return !d_inConflict;
1273 }
1274
1275 const LogicInfo& TheoryEngine::getLogicInfo() const { return d_logicInfo; }
1276
1277 theory::EqualityStatus TheoryEngine::getEqualityStatus(TNode a, TNode b) {
1278 Assert(a.getType().isComparableTo(b.getType()));
1279 if (d_sharedTerms.isShared(a) && d_sharedTerms.isShared(b)) {
1280 if (d_sharedTerms.areEqual(a,b)) {
1281 return EQUALITY_TRUE_AND_PROPAGATED;
1282 }
1283 else if (d_sharedTerms.areDisequal(a,b)) {
1284 return EQUALITY_FALSE_AND_PROPAGATED;
1285 }
1286 }
1287 return theoryOf(Theory::theoryOf(a.getType()))->getEqualityStatus(a, b);
1288 }
1289
1290 Node TheoryEngine::getModelValue(TNode var) {
1291 if (var.isConst())
1292 {
1293 // the model value of a constant must be itself
1294 return var;
1295 }
1296 Assert(d_sharedTerms.isShared(var));
1297 return theoryOf(Theory::theoryOf(var.getType()))->getModelValue(var);
1298 }
1299
1300
1301 Node TheoryEngine::ensureLiteral(TNode n) {
1302 Debug("ensureLiteral") << "rewriting: " << n << std::endl;
1303 Node rewritten = Rewriter::rewrite(n);
1304 Debug("ensureLiteral") << " got: " << rewritten << std::endl;
1305 Node preprocessed = preprocess(rewritten);
1306 Debug("ensureLiteral") << "preprocessed: " << preprocessed << std::endl;
1307 d_propEngine->ensureLiteral(preprocessed);
1308 return preprocessed;
1309 }
1310
1311
1312 void TheoryEngine::printInstantiations( std::ostream& out ) {
1313 if( d_quantEngine ){
1314 d_quantEngine->printInstantiations( out );
1315 }else{
1316 out << "Internal error : instantiations not available when quantifiers are not present." << std::endl;
1317 Assert(false);
1318 }
1319 }
1320
1321 void TheoryEngine::printSynthSolution( std::ostream& out ) {
1322 if( d_quantEngine ){
1323 d_quantEngine->printSynthSolution( out );
1324 }else{
1325 out << "Internal error : synth solution not available when quantifiers are not present." << std::endl;
1326 Assert(false);
1327 }
1328 }
1329
1330 void TheoryEngine::getInstantiatedQuantifiedFormulas( std::vector< Node >& qs ) {
1331 if( d_quantEngine ){
1332 d_quantEngine->getInstantiatedQuantifiedFormulas( qs );
1333 }else{
1334 Assert(false);
1335 }
1336 }
1337
1338 void TheoryEngine::getInstantiations( Node q, std::vector< Node >& insts ) {
1339 if( d_quantEngine ){
1340 d_quantEngine->getInstantiations( q, insts );
1341 }else{
1342 Assert(false);
1343 }
1344 }
1345
1346 void TheoryEngine::getInstantiationTermVectors( Node q, std::vector< std::vector< Node > >& tvecs ) {
1347 if( d_quantEngine ){
1348 d_quantEngine->getInstantiationTermVectors( q, tvecs );
1349 }else{
1350 Assert(false);
1351 }
1352 }
1353
1354 void TheoryEngine::getInstantiations( std::map< Node, std::vector< Node > >& insts ) {
1355 if( d_quantEngine ){
1356 d_quantEngine->getInstantiations( insts );
1357 }else{
1358 Assert(false);
1359 }
1360 }
1361
1362 void TheoryEngine::getInstantiationTermVectors( std::map< Node, std::vector< std::vector< Node > > >& insts ) {
1363 if( d_quantEngine ){
1364 d_quantEngine->getInstantiationTermVectors( insts );
1365 }else{
1366 Assert(false);
1367 }
1368 }
1369
1370 Node TheoryEngine::getInstantiatedConjunction( Node q ) {
1371 if( d_quantEngine ){
1372 return d_quantEngine->getInstantiatedConjunction( q );
1373 }else{
1374 Assert(false);
1375 return Node::null();
1376 }
1377 }
1378
1379
1380 static Node mkExplanation(const std::vector<NodeTheoryPair>& explanation) {
1381
1382 std::set<TNode> all;
1383 for (unsigned i = 0; i < explanation.size(); ++ i) {
1384 Assert(explanation[i].d_theory == THEORY_SAT_SOLVER);
1385 all.insert(explanation[i].d_node);
1386 }
1387
1388 if (all.size() == 0) {
1389 // Normalize to true
1390 return NodeManager::currentNM()->mkConst<bool>(true);
1391 }
1392
1393 if (all.size() == 1) {
1394 // All the same, or just one
1395 return explanation[0].d_node;
1396 }
1397
1398 NodeBuilder<> conjunction(kind::AND);
1399 std::set<TNode>::const_iterator it = all.begin();
1400 std::set<TNode>::const_iterator it_end = all.end();
1401 while (it != it_end) {
1402 conjunction << *it;
1403 ++ it;
1404 }
1405
1406 return conjunction;
1407 }
1408
1409 Node TheoryEngine::getExplanationAndRecipe(TNode node, LemmaProofRecipe* proofRecipe) {
1410 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node << "): current propagation index = " << d_propagationMapTimestamp << endl;
1411
1412 bool polarity = node.getKind() != kind::NOT;
1413 TNode atom = polarity ? node : node[0];
1414
1415 // If we're not in shared mode, explanations are simple
1416 if (!d_logicInfo.isSharingEnabled()) {
1417 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing is NOT enabled. "
1418 << " Responsible theory is: "
1419 << theoryOf(atom)->getId() << std::endl;
1420
1421 TrustNode texplanation = theoryOf(atom)->explain(node);
1422 Node explanation = texplanation.getNode();
1423 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node << ") => " << explanation << endl;
1424 PROOF({
1425 if(proofRecipe) {
1426 Node emptyNode;
1427 LemmaProofRecipe::ProofStep proofStep(theoryOf(atom)->getId(), emptyNode);
1428 proofStep.addAssertion(node);
1429 proofRecipe->addBaseAssertion(node);
1430
1431 if (explanation.getKind() == kind::AND) {
1432 // If the explanation is a conjunction, the recipe for the corresponding lemma is
1433 // the negation of its conjuncts.
1434 Node flat = flattenAnd(explanation);
1435 for (unsigned i = 0; i < flat.getNumChildren(); ++i) {
1436 if (flat[i].isConst() && flat[i].getConst<bool>()) {
1437 ++ i;
1438 continue;
1439 }
1440 if (flat[i].getKind() == kind::NOT &&
1441 flat[i][0].isConst() && !flat[i][0].getConst<bool>()) {
1442 ++ i;
1443 continue;
1444 }
1445 Debug("theory::explain") << "TheoryEngine::getExplanationAndRecipe: adding recipe assertion: "
1446 << flat[i].negate() << std::endl;
1447 proofStep.addAssertion(flat[i].negate());
1448 proofRecipe->addBaseAssertion(flat[i].negate());
1449 }
1450 } else {
1451 // The recipe for proving it is by negating it. "True" is not an acceptable reason.
1452 if (!((explanation.isConst() && explanation.getConst<bool>()) ||
1453 (explanation.getKind() == kind::NOT &&
1454 explanation[0].isConst() && !explanation[0].getConst<bool>()))) {
1455 proofStep.addAssertion(explanation.negate());
1456 proofRecipe->addBaseAssertion(explanation.negate());
1457 }
1458 }
1459
1460 proofRecipe->addStep(proofStep);
1461 }
1462 });
1463
1464 return explanation;
1465 }
1466
1467 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing IS enabled" << std::endl;
1468
1469 // Initial thing to explain
1470 NodeTheoryPair toExplain(node, THEORY_SAT_SOLVER, d_propagationMapTimestamp);
1471 Assert(d_propagationMap.find(toExplain) != d_propagationMap.end());
1472
1473 NodeTheoryPair nodeExplainerPair = d_propagationMap[toExplain];
1474 Debug("theory::explain")
1475 << "TheoryEngine::getExplanation: explainer for node "
1476 << nodeExplainerPair.d_node
1477 << " is theory: " << nodeExplainerPair.d_theory << std::endl;
1478 TheoryId explainer = nodeExplainerPair.d_theory;
1479
1480 // Create the workplace for explanations
1481 std::vector<NodeTheoryPair> explanationVector;
1482 explanationVector.push_back(d_propagationMap[toExplain]);
1483 // Process the explanation
1484 if (proofRecipe) {
1485 Node emptyNode;
1486 LemmaProofRecipe::ProofStep proofStep(explainer, emptyNode);
1487 proofStep.addAssertion(node);
1488 proofRecipe->addStep(proofStep);
1489 proofRecipe->addBaseAssertion(node);
1490 }
1491
1492 getExplanation(explanationVector, proofRecipe);
1493 Node explanation = mkExplanation(explanationVector);
1494
1495 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node << ") => " << explanation << endl;
1496
1497 return explanation;
1498 }
1499
1500 Node TheoryEngine::getExplanation(TNode node) {
1501 LemmaProofRecipe *dontCareRecipe = NULL;
1502 return getExplanationAndRecipe(node, dontCareRecipe);
1503 }
1504
1505 struct AtomsCollect {
1506
1507 std::vector<TNode> d_atoms;
1508 std::unordered_set<TNode, TNodeHashFunction> d_visited;
1509
1510 public:
1511
1512 typedef void return_type;
1513
1514 bool alreadyVisited(TNode current, TNode parent) {
1515 // Check if already visited
1516 if (d_visited.find(current) != d_visited.end()) return true;
1517 // Don't visit non-boolean
1518 if (!current.getType().isBoolean()) return true;
1519 // New node
1520 return false;
1521 }
1522
1523 void visit(TNode current, TNode parent) {
1524 if (Theory::theoryOf(current) != theory::THEORY_BOOL) {
1525 d_atoms.push_back(current);
1526 }
1527 d_visited.insert(current);
1528 }
1529
1530 void start(TNode node) {}
1531 void done(TNode node) {}
1532
1533 std::vector<TNode> getAtoms() const {
1534 return d_atoms;
1535 }
1536 };
1537
1538 void TheoryEngine::ensureLemmaAtoms(const std::vector<TNode>& atoms, theory::TheoryId atomsTo) {
1539 for (unsigned i = 0; i < atoms.size(); ++ i) {
1540
1541 // Non-equality atoms are either owned by theory or they don't make sense
1542 if (atoms[i].getKind() != kind::EQUAL) {
1543 continue;
1544 }
1545
1546 // The equality
1547 Node eq = atoms[i];
1548 // Simple normalization to not repeat stuff
1549 if (eq[0] > eq[1]) {
1550 eq = eq[1].eqNode(eq[0]);
1551 }
1552
1553 // Rewrite the equality
1554 Node eqNormalized = Rewriter::rewrite(atoms[i]);
1555
1556 Debug("theory::atoms") << "TheoryEngine::ensureLemmaAtoms(): " << eq << " with nf " << eqNormalized << endl;
1557
1558 // If the equality is a boolean constant, we send immediately
1559 if (eqNormalized.isConst()) {
1560 if (eqNormalized.getConst<bool>()) {
1561 assertToTheory(eq, eqNormalized, /** to */ atomsTo, /** Sat solver */ theory::THEORY_SAT_SOLVER);
1562 } else {
1563 assertToTheory(eq.notNode(), eqNormalized.notNode(), /** to */ atomsTo, /** Sat solver */ theory::THEORY_SAT_SOLVER);
1564 }
1565 continue;
1566 }else if( eqNormalized.getKind() != kind::EQUAL){
1567 Assert(eqNormalized.getKind() == kind::BOOLEAN_TERM_VARIABLE
1568 || (eqNormalized.getKind() == kind::NOT
1569 && eqNormalized[0].getKind() == kind::BOOLEAN_TERM_VARIABLE));
1570 // this happens for Boolean term equalities V = true that are rewritten to V, we should skip
1571 // TODO : revisit this
1572 continue;
1573 }
1574
1575 // If the normalization did the just flips, keep the flip
1576 if (eqNormalized[0] == eq[1] && eqNormalized[1] == eq[0]) {
1577 eq = eqNormalized;
1578 }
1579
1580 // Check if the equality is already known by the sat solver
1581 if (d_propEngine->isSatLiteral(eqNormalized)) {
1582 bool value;
1583 if (d_propEngine->hasValue(eqNormalized, value)) {
1584 if (value) {
1585 assertToTheory(eq, eqNormalized, atomsTo, theory::THEORY_SAT_SOLVER);
1586 continue;
1587 } else {
1588 assertToTheory(eq.notNode(), eqNormalized.notNode(), atomsTo, theory::THEORY_SAT_SOLVER);
1589 continue;
1590 }
1591 }
1592 }
1593
1594 // If the theory is asking about a different form, or the form is ok but if will go to a different theory
1595 // then we must figure it out
1596 if (eqNormalized != eq || Theory::theoryOf(eq) != atomsTo) {
1597 // If you get eqNormalized, send atoms[i] to atomsTo
1598 d_atomRequests.add(eqNormalized, eq, atomsTo);
1599 }
1600 }
1601 }
1602
1603 theory::LemmaStatus TheoryEngine::lemma(TNode node,
1604 ProofRule rule,
1605 bool negated,
1606 theory::LemmaProperty p,
1607 theory::TheoryId atomsTo)
1608 {
1609 // For resource-limiting (also does a time check).
1610 // spendResource();
1611
1612 // Do we need to check atoms
1613 if (atomsTo != theory::THEORY_LAST) {
1614 Debug("theory::atoms") << "TheoryEngine::lemma(" << node << ", " << atomsTo << ")" << endl;
1615 AtomsCollect collectAtoms;
1616 NodeVisitor<AtomsCollect>::run(collectAtoms, node);
1617 ensureLemmaAtoms(collectAtoms.getAtoms(), atomsTo);
1618 }
1619
1620 if(Dump.isOn("t-lemmas")) {
1621 Node n = node;
1622 if (!negated) {
1623 n = node.negate();
1624 }
1625 Dump("t-lemmas") << CommentCommand("theory lemma: expect valid")
1626 << CheckSatCommand(n.toExpr());
1627 }
1628 bool removable = isLemmaPropertyRemovable(p);
1629 bool preprocess = isLemmaPropertyPreprocess(p);
1630
1631 // call preprocessor
1632 std::vector<TrustNode> newLemmas;
1633 std::vector<Node> newSkolems;
1634 TrustNode tlemma = d_tpp.preprocess(node, newLemmas, newSkolems, preprocess);
1635
1636 // must use an assertion pipeline due to decision engine below
1637 AssertionPipeline lemmas;
1638 // make the assertion pipeline
1639 lemmas.push_back(tlemma.getNode());
1640 lemmas.updateRealAssertionsEnd();
1641 Assert(newSkolems.size() == newLemmas.size());
1642 for (size_t i = 0, nsize = newLemmas.size(); i < nsize; i++)
1643 {
1644 // store skolem mapping here
1645 IteSkolemMap& imap = lemmas.getIteSkolemMap();
1646 imap[newSkolems[i]] = lemmas.size();
1647 lemmas.push_back(newLemmas[i].getNode());
1648 }
1649
1650 // assert lemmas to prop engine
1651 for (size_t i = 0, lsize = lemmas.size(); i < lsize; ++i)
1652 {
1653 d_propEngine->assertLemma(
1654 lemmas[i], i == 0 && negated, removable, rule, node);
1655 }
1656
1657 // WARNING: Below this point don't assume lemmas[0] to be not negated.
1658 if(negated) {
1659 lemmas.replace(0, lemmas[0].notNode());
1660 negated = false;
1661 }
1662
1663 // assert to decision engine
1664 if (!removable)
1665 {
1666 d_propEngine->addAssertionsToDecisionEngine(lemmas);
1667 }
1668
1669 // Mark that we added some lemmas
1670 d_lemmasAdded = true;
1671
1672 // Lemma analysis isn't online yet; this lemma may only live for this
1673 // user level.
1674 Node retLemma = lemmas[0];
1675 if (lemmas.size() > 1)
1676 {
1677 // the returned lemma is the conjunction of all additional lemmas.
1678 retLemma = NodeManager::currentNM()->mkNode(kind::AND, lemmas.ref());
1679 }
1680 return theory::LemmaStatus(retLemma, d_userContext->getLevel());
1681 }
1682
1683 void TheoryEngine::conflict(TNode conflict, TheoryId theoryId) {
1684
1685 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict << ", " << theoryId << ")" << endl;
1686
1687 Trace("dtview::conflict") << ":THEORY-CONFLICT: " << conflict << std::endl;
1688
1689 // Mark that we are in conflict
1690 d_inConflict = true;
1691
1692 if(Dump.isOn("t-conflicts")) {
1693 Dump("t-conflicts") << CommentCommand("theory conflict: expect unsat")
1694 << CheckSatCommand(conflict.toExpr());
1695 }
1696
1697 LemmaProofRecipe* proofRecipe = NULL;
1698 PROOF({
1699 proofRecipe = new LemmaProofRecipe;
1700 Node emptyNode;
1701 LemmaProofRecipe::ProofStep proofStep(theoryId, emptyNode);
1702
1703 if (conflict.getKind() == kind::AND) {
1704 for (unsigned i = 0; i < conflict.getNumChildren(); ++i) {
1705 proofStep.addAssertion(conflict[i].negate());
1706 }
1707 } else {
1708 proofStep.addAssertion(conflict.negate());
1709 }
1710
1711 proofRecipe->addStep(proofStep);
1712 });
1713
1714 // In the multiple-theories case, we need to reconstruct the conflict
1715 if (d_logicInfo.isSharingEnabled()) {
1716 // Create the workplace for explanations
1717 std::vector<NodeTheoryPair> explanationVector;
1718 explanationVector.push_back(NodeTheoryPair(conflict, theoryId, d_propagationMapTimestamp));
1719
1720 // Process the explanation
1721 getExplanation(explanationVector, proofRecipe);
1722 PROOF(ProofManager::getCnfProof()->setProofRecipe(proofRecipe));
1723 Node fullConflict = mkExplanation(explanationVector);
1724 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict << ", " << theoryId << "): full = " << fullConflict << endl;
1725 Assert(properConflict(fullConflict));
1726 lemma(fullConflict,
1727 RULE_CONFLICT,
1728 true,
1729 LemmaProperty::REMOVABLE,
1730 THEORY_LAST);
1731 } else {
1732 // When only one theory, the conflict should need no processing
1733 Assert(properConflict(conflict));
1734 PROOF({
1735 if (conflict.getKind() == kind::AND) {
1736 // If the conflict is a conjunction, the corresponding lemma is derived by negating
1737 // its conjuncts.
1738 for (unsigned i = 0; i < conflict.getNumChildren(); ++i) {
1739 if (conflict[i].isConst() && conflict[i].getConst<bool>()) {
1740 ++ i;
1741 continue;
1742 }
1743 if (conflict[i].getKind() == kind::NOT &&
1744 conflict[i][0].isConst() && !conflict[i][0].getConst<bool>()) {
1745 ++ i;
1746 continue;
1747 }
1748 proofRecipe->getStep(0)->addAssertion(conflict[i].negate());
1749 proofRecipe->addBaseAssertion(conflict[i].negate());
1750 }
1751 } else {
1752 proofRecipe->getStep(0)->addAssertion(conflict.negate());
1753 proofRecipe->addBaseAssertion(conflict.negate());
1754 }
1755
1756 ProofManager::getCnfProof()->setProofRecipe(proofRecipe);
1757 });
1758
1759 lemma(conflict, RULE_CONFLICT, true, LemmaProperty::REMOVABLE, THEORY_LAST);
1760 }
1761
1762 PROOF({
1763 delete proofRecipe;
1764 proofRecipe = NULL;
1765 });
1766 }
1767
1768 void TheoryEngine::staticInitializeBVOptions(
1769 const std::vector<Node>& assertions)
1770 {
1771 bool useSlicer = true;
1772 if (options::bitvectorEqualitySlicer() == options::BvSlicerMode::ON)
1773 {
1774 if (!d_logicInfo.isPure(theory::THEORY_BV) || d_logicInfo.isQuantified())
1775 throw ModalException(
1776 "Slicer currently only supports pure QF_BV formulas. Use "
1777 "--bv-eq-slicer=off");
1778 if (options::incrementalSolving())
1779 throw ModalException(
1780 "Slicer does not currently support incremental mode. Use "
1781 "--bv-eq-slicer=off");
1782 if (options::produceModels())
1783 throw ModalException(
1784 "Slicer does not currently support model generation. Use "
1785 "--bv-eq-slicer=off");
1786 }
1787 else if (options::bitvectorEqualitySlicer() == options::BvSlicerMode::OFF)
1788 {
1789 return;
1790 }
1791 else if (options::bitvectorEqualitySlicer() == options::BvSlicerMode::AUTO)
1792 {
1793 if ((!d_logicInfo.isPure(theory::THEORY_BV) || d_logicInfo.isQuantified())
1794 || options::incrementalSolving()
1795 || options::produceModels())
1796 return;
1797
1798 bv::utils::TNodeBoolMap cache;
1799 for (unsigned i = 0; i < assertions.size(); ++i)
1800 {
1801 useSlicer = useSlicer && bv::utils::isCoreTerm(assertions[i], cache);
1802 }
1803 }
1804
1805 if (useSlicer)
1806 {
1807 bv::TheoryBV* bv_theory = (bv::TheoryBV*)d_theoryTable[THEORY_BV];
1808 bv_theory->enableCoreTheorySlicer();
1809 }
1810 }
1811
1812 SharedTermsDatabase* TheoryEngine::getSharedTermsDatabase()
1813 {
1814 return &d_sharedTerms;
1815 }
1816
1817 void TheoryEngine::getExplanation(std::vector<NodeTheoryPair>& explanationVector, LemmaProofRecipe* proofRecipe) {
1818 Assert(explanationVector.size() > 0);
1819
1820 unsigned i = 0; // Index of the current literal we are processing
1821 unsigned j = 0; // Index of the last literal we are keeping
1822
1823 std::unique_ptr<std::set<Node>> inputAssertions = nullptr;
1824 PROOF({
1825 if (proofRecipe)
1826 {
1827 inputAssertions.reset(
1828 new std::set<Node>(proofRecipe->getStep(0)->getAssertions()));
1829 }
1830 });
1831 // cache of nodes we have already explained by some theory
1832 std::unordered_map<Node, size_t, NodeHashFunction> cache;
1833
1834 while (i < explanationVector.size()) {
1835 // Get the current literal to explain
1836 NodeTheoryPair toExplain = explanationVector[i];
1837
1838 Debug("theory::explain")
1839 << "[i=" << i << "] TheoryEngine::explain(): processing ["
1840 << toExplain.d_timestamp << "] " << toExplain.d_node << " sent from "
1841 << toExplain.d_theory << endl;
1842
1843 if (cache.find(toExplain.d_node) != cache.end()
1844 && cache[toExplain.d_node] < toExplain.d_timestamp)
1845 {
1846 ++i;
1847 continue;
1848 }
1849 cache[toExplain.d_node] = toExplain.d_timestamp;
1850
1851 // If a true constant or a negation of a false constant we can ignore it
1852 if (toExplain.d_node.isConst() && toExplain.d_node.getConst<bool>())
1853 {
1854 ++ i;
1855 continue;
1856 }
1857 if (toExplain.d_node.getKind() == kind::NOT && toExplain.d_node[0].isConst()
1858 && !toExplain.d_node[0].getConst<bool>())
1859 {
1860 ++ i;
1861 continue;
1862 }
1863
1864 // If from the SAT solver, keep it
1865 if (toExplain.d_theory == THEORY_SAT_SOLVER)
1866 {
1867 Debug("theory::explain") << "\tLiteral came from THEORY_SAT_SOLVER. Kepping it." << endl;
1868 explanationVector[j++] = explanationVector[i++];
1869 continue;
1870 }
1871
1872 // If an and, expand it
1873 if (toExplain.d_node.getKind() == kind::AND)
1874 {
1875 Debug("theory::explain")
1876 << "TheoryEngine::explain(): expanding " << toExplain.d_node
1877 << " got from " << toExplain.d_theory << endl;
1878 for (unsigned k = 0; k < toExplain.d_node.getNumChildren(); ++k)
1879 {
1880 NodeTheoryPair newExplain(
1881 toExplain.d_node[k], toExplain.d_theory, toExplain.d_timestamp);
1882 explanationVector.push_back(newExplain);
1883 }
1884 ++ i;
1885 continue;
1886 }
1887
1888 // See if it was sent to the theory by another theory
1889 PropagationMap::const_iterator find = d_propagationMap.find(toExplain);
1890 if (find != d_propagationMap.end()) {
1891 Debug("theory::explain")
1892 << "\tTerm was propagated by another theory (theory = "
1893 << getTheoryString((*find).second.d_theory) << ")" << std::endl;
1894 // There is some propagation, check if its a timely one
1895 if ((*find).second.d_timestamp < toExplain.d_timestamp)
1896 {
1897 Debug("theory::explain")
1898 << "\tRelevant timetsamp, pushing " << (*find).second.d_node
1899 << "to index = " << explanationVector.size() << std::endl;
1900 explanationVector.push_back((*find).second);
1901 ++i;
1902
1903 PROOF({
1904 if (toExplain.d_node != (*find).second.d_node)
1905 {
1906 Debug("pf::explain")
1907 << "TheoryEngine::getExplanation: Rewrite alert! toAssert = "
1908 << toExplain.d_node << ", toExplain = " << (*find).second.d_node
1909 << std::endl;
1910
1911 if (proofRecipe)
1912 {
1913 proofRecipe->addRewriteRule(toExplain.d_node,
1914 (*find).second.d_node);
1915 }
1916 }
1917 })
1918
1919 continue;
1920 }
1921 }
1922
1923 Node explanation;
1924 if (toExplain.d_theory == THEORY_BUILTIN)
1925 {
1926 explanation = d_sharedTerms.explain(toExplain.d_node);
1927 Debug("theory::explain") << "\tTerm was propagated by THEORY_BUILTIN. Explanation: " << explanation << std::endl;
1928 }
1929 else
1930 {
1931 TrustNode texp = theoryOf(toExplain.d_theory)->explain(toExplain.d_node);
1932 explanation = texp.getNode();
1933 Debug("theory::explain") << "\tTerm was propagated by owner theory: "
1934 << theoryOf(toExplain.d_theory)->getId()
1935 << ". Explanation: " << explanation << std::endl;
1936 }
1937
1938 Debug("theory::explain")
1939 << "TheoryEngine::explain(): got explanation " << explanation
1940 << " got from " << toExplain.d_theory << endl;
1941 Assert(explanation != toExplain.d_node)
1942 << "wasn't sent to you, so why are you explaining it trivially";
1943 // Mark the explanation
1944 NodeTheoryPair newExplain(
1945 explanation, toExplain.d_theory, toExplain.d_timestamp);
1946 explanationVector.push_back(newExplain);
1947
1948 ++ i;
1949
1950 PROOF({
1951 if (proofRecipe && inputAssertions)
1952 {
1953 // If we're expanding the target node of the explanation (this is the
1954 // first expansion...), we don't want to add it as a separate proof
1955 // step. It is already part of the assertions.
1956 if (!ContainsKey(*inputAssertions, toExplain.d_node))
1957 {
1958 LemmaProofRecipe::ProofStep proofStep(toExplain.d_theory,
1959 toExplain.d_node);
1960 if (explanation.getKind() == kind::AND)
1961 {
1962 Node flat = flattenAnd(explanation);
1963 for (unsigned k = 0; k < flat.getNumChildren(); ++k)
1964 {
1965 // If a true constant or a negation of a false constant we can
1966 // ignore it
1967 if (!((flat[k].isConst() && flat[k].getConst<bool>())
1968 || (flat[k].getKind() == kind::NOT && flat[k][0].isConst()
1969 && !flat[k][0].getConst<bool>())))
1970 {
1971 proofStep.addAssertion(flat[k].negate());
1972 }
1973 }
1974 }
1975 else
1976 {
1977 if (!((explanation.isConst() && explanation.getConst<bool>())
1978 || (explanation.getKind() == kind::NOT
1979 && explanation[0].isConst()
1980 && !explanation[0].getConst<bool>())))
1981 {
1982 proofStep.addAssertion(explanation.negate());
1983 }
1984 }
1985 proofRecipe->addStep(proofStep);
1986 }
1987 }
1988 });
1989 }
1990
1991 // Keep only the relevant literals
1992 explanationVector.resize(j);
1993
1994 PROOF({
1995 if (proofRecipe) {
1996 // The remaining literals are the base of the proof
1997 for (unsigned k = 0; k < explanationVector.size(); ++k) {
1998 proofRecipe->addBaseAssertion(explanationVector[k].d_node.negate());
1999 }
2000 }
2001 });
2002 }
2003
2004 void TheoryEngine::setUserAttribute(const std::string& attr,
2005 Node n,
2006 const std::vector<Node>& node_values,
2007 const std::string& str_value)
2008 {
2009 Trace("te-attr") << "set user attribute " << attr << " " << n << endl;
2010 if( d_attr_handle.find( attr )!=d_attr_handle.end() ){
2011 for( size_t i=0; i<d_attr_handle[attr].size(); i++ ){
2012 d_attr_handle[attr][i]->setUserAttribute(attr, n, node_values, str_value);
2013 }
2014 } else {
2015 //unhandled exception?
2016 }
2017 }
2018
2019 void TheoryEngine::handleUserAttribute(const char* attr, Theory* t) {
2020 Trace("te-attr") << "Handle user attribute " << attr << " " << t << endl;
2021 std::string str( attr );
2022 d_attr_handle[ str ].push_back( t );
2023 }
2024
2025 void TheoryEngine::checkTheoryAssertionsWithModel(bool hardFailure) {
2026 for(TheoryId theoryId = THEORY_FIRST; theoryId < THEORY_LAST; ++theoryId) {
2027 Theory* theory = d_theoryTable[theoryId];
2028 if(theory && d_logicInfo.isTheoryEnabled(theoryId)) {
2029 for(context::CDList<Assertion>::const_iterator it = theory->facts_begin(),
2030 it_end = theory->facts_end();
2031 it != it_end;
2032 ++it) {
2033 Node assertion = (*it).d_assertion;
2034 Node val = getModel()->getValue(assertion);
2035 if (val != d_true)
2036 {
2037 std::stringstream ss;
2038 ss << " " << theoryId
2039 << " has an asserted fact that the model doesn't satisfy." << endl
2040 << "The fact: " << assertion << endl
2041 << "Model value: " << val << endl;
2042 if (hardFailure)
2043 {
2044 if (val == d_false)
2045 {
2046 // Always an error if it is false
2047 InternalError() << ss.str();
2048 }
2049 else
2050 {
2051 // Otherwise just a warning. Notice this case may happen for
2052 // assertions with unevaluable operators, e.g. transcendental
2053 // functions. It also may happen for separation logic, where
2054 // check-model support is limited.
2055 Warning() << ss.str();
2056 }
2057 }
2058 }
2059 }
2060 }
2061 }
2062 }
2063
2064 std::pair<bool, Node> TheoryEngine::entailmentCheck(options::TheoryOfMode mode,
2065 TNode lit)
2066 {
2067 TNode atom = (lit.getKind() == kind::NOT) ? lit[0] : lit;
2068 if( atom.getKind()==kind::AND || atom.getKind()==kind::OR || atom.getKind()==kind::IMPLIES ){
2069 //Boolean connective, recurse
2070 std::vector< Node > children;
2071 bool pol = (lit.getKind()!=kind::NOT);
2072 bool is_conjunction = pol==(lit.getKind()==kind::AND);
2073 for( unsigned i=0; i<atom.getNumChildren(); i++ ){
2074 Node ch = atom[i];
2075 if( pol==( lit.getKind()==kind::IMPLIES && i==0 ) ){
2076 ch = atom[i].negate();
2077 }
2078 std::pair<bool, Node> chres = entailmentCheck(mode, ch);
2079 if( chres.first ){
2080 if( !is_conjunction ){
2081 return chres;
2082 }else{
2083 children.push_back( chres.second );
2084 }
2085 }else if( !chres.first && is_conjunction ){
2086 return std::pair<bool, Node>(false, Node::null());
2087 }
2088 }
2089 if( is_conjunction ){
2090 return std::pair<bool, Node>(true, NodeManager::currentNM()->mkNode(kind::AND, children));
2091 }else{
2092 return std::pair<bool, Node>(false, Node::null());
2093 }
2094 }else if( atom.getKind()==kind::ITE || ( atom.getKind()==kind::EQUAL && atom[0].getType().isBoolean() ) ){
2095 bool pol = (lit.getKind()!=kind::NOT);
2096 for( unsigned r=0; r<2; r++ ){
2097 Node ch = atom[0];
2098 if( r==1 ){
2099 ch = ch.negate();
2100 }
2101 std::pair<bool, Node> chres = entailmentCheck(mode, ch);
2102 if( chres.first ){
2103 Node ch2 = atom[ atom.getKind()==kind::ITE ? r+1 : 1 ];
2104 if( pol==( atom.getKind()==kind::ITE ? true : r==1 ) ){
2105 ch2 = ch2.negate();
2106 }
2107 std::pair<bool, Node> chres2 = entailmentCheck(mode, ch2);
2108 if( chres2.first ){
2109 return std::pair<bool, Node>(true, NodeManager::currentNM()->mkNode(kind::AND, chres.second, chres2.second));
2110 }else{
2111 break;
2112 }
2113 }
2114 }
2115 return std::pair<bool, Node>(false, Node::null());
2116 }else{
2117 //it is a theory atom
2118 theory::TheoryId tid = theory::Theory::theoryOf(mode, atom);
2119 theory::Theory* th = theoryOf(tid);
2120
2121 Assert(th != NULL);
2122 Trace("theory-engine-entc") << "Entailment check : " << lit << std::endl;
2123
2124 std::pair<bool, Node> chres = th->entailmentCheck(lit);
2125 return chres;
2126 }
2127 }
2128
2129 void TheoryEngine::spendResource(ResourceManager::Resource r)
2130 {
2131 d_resourceManager->spendResource(r);
2132 }
2133
2134 }/* CVC4 namespace */