Update copyright header script to support CMake and Python files (#5067)
[cvc5.git] / src / theory / theory_engine.cpp
1 /********************* */
2 /*! \file theory_engine.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Dejan Jovanovic, 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/quantifiers_options.h"
32 #include "options/theory_options.h"
33 #include "preprocessing/assertion_pipeline.h"
34 #include "printer/printer.h"
35 #include "smt/dump.h"
36 #include "smt/logic_exception.h"
37 #include "smt/term_formula_removal.h"
38 #include "theory/arith/arith_ite_utils.h"
39 #include "theory/bv/theory_bv_utils.h"
40 #include "theory/care_graph.h"
41 #include "theory/combination_care_graph.h"
42 #include "theory/decision_manager.h"
43 #include "theory/quantifiers/first_order_model.h"
44 #include "theory/quantifiers/fmf/model_engine.h"
45 #include "theory/quantifiers/theory_quantifiers.h"
46 #include "theory/quantifiers_engine.h"
47 #include "theory/relevance_manager.h"
48 #include "theory/rewriter.h"
49 #include "theory/theory.h"
50 #include "theory/theory_engine_proof_generator.h"
51 #include "theory/theory_id.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 // NOTE: This seems to be required since
134 // theory::TheoryTraits<THEORY>::isParametric cannot be accessed without
135 // using the CVC4_FOR_EACH_THEORY_STATEMENT macro. -AJR
136 std::vector<theory::Theory*> paraTheories;
137 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
138 #undef CVC4_FOR_EACH_THEORY_STATEMENT
139 #endif
140 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
141 if (theory::TheoryTraits<THEORY>::isParametric \
142 && d_logicInfo.isTheoryEnabled(THEORY)) \
143 { \
144 paraTheories.push_back(theoryOf(THEORY)); \
145 }
146 // Collect the parametric theories, which are given to the theory combination
147 // manager below
148 CVC4_FOR_EACH_THEORY;
149
150 // Initialize the theory combination architecture
151 if (options::tcMode() == options::TcMode::CARE_GRAPH)
152 {
153 d_tc.reset(new CombinationCareGraph(*this, paraTheories, d_pnm));
154 }
155 else
156 {
157 Unimplemented() << "TheoryEngine::finishInit: theory combination mode "
158 << options::tcMode() << " not supported";
159 }
160 // create the relevance filter if any option requires it
161 if (options::relevanceFilter())
162 {
163 d_relManager.reset(
164 new RelevanceManager(d_userContext, theory::Valuation(this)));
165 }
166
167 // initialize the quantifiers engine
168 if (d_logicInfo.isQuantified())
169 {
170 // initialize the quantifiers engine
171 d_quantEngine = new QuantifiersEngine(this, *d_decManager.get(), d_pnm);
172 }
173 // initialize the theory combination manager, which decides and allocates the
174 // equality engines to use for all theories.
175 d_tc->finishInit();
176
177 // set the core equality engine on quantifiers engine
178 if (d_logicInfo.isQuantified())
179 {
180 d_quantEngine->setMasterEqualityEngine(d_tc->getCoreEqualityEngine());
181 }
182
183 // finish initializing the theories by linking them with the appropriate
184 // utilities and then calling their finishInit method.
185 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId != theory::THEORY_LAST; ++ theoryId) {
186 Theory* t = d_theoryTable[theoryId];
187 if (t == nullptr)
188 {
189 continue;
190 }
191 // setup the pointers to the utilities
192 const EeTheoryInfo* eeti = d_tc->getEeTheoryInfo(theoryId);
193 Assert(eeti != nullptr);
194 // the theory's official equality engine is the one specified by the
195 // equality engine manager
196 t->setEqualityEngine(eeti->d_usedEe);
197 // set the quantifiers engine
198 t->setQuantifiersEngine(d_quantEngine);
199 // set the decision manager for the theory
200 t->setDecisionManager(d_decManager.get());
201 // finish initializing the theory
202 t->finishInit();
203 }
204
205 // finish initializing the quantifiers engine
206 if (d_logicInfo.isQuantified())
207 {
208 d_quantEngine->finishInit();
209 }
210 }
211
212 TheoryEngine::TheoryEngine(context::Context* context,
213 context::UserContext* userContext,
214 ResourceManager* rm,
215 RemoveTermFormulas& iteRemover,
216 const LogicInfo& logicInfo,
217 OutputManager& outMgr)
218 : d_propEngine(nullptr),
219 d_context(context),
220 d_userContext(userContext),
221 d_logicInfo(logicInfo),
222 d_outMgr(outMgr),
223 d_pnm(nullptr),
224 d_lazyProof(
225 d_pnm != nullptr ? new LazyCDProof(
226 d_pnm, nullptr, d_userContext, "TheoryEngine::LazyCDProof")
227 : nullptr),
228 d_tepg(new TheoryEngineProofGenerator(d_pnm, d_userContext)),
229 d_sharedTerms(this, context),
230 d_tc(nullptr),
231 d_quantEngine(nullptr),
232 d_decManager(new DecisionManager(userContext)),
233 d_relManager(nullptr),
234 d_preRegistrationVisitor(this, context),
235 d_sharedTermsVisitor(d_sharedTerms),
236 d_eager_model_building(false),
237 d_inConflict(context, false),
238 d_inSatMode(false),
239 d_hasShutDown(false),
240 d_incomplete(context, false),
241 d_propagationMap(context),
242 d_propagationMapTimestamp(context, 0),
243 d_propagatedLiterals(context),
244 d_propagatedLiteralsIndex(context, 0),
245 d_atomRequests(context),
246 d_tpp(*this, iteRemover),
247 d_combineTheoriesTime("TheoryEngine::combineTheoriesTime"),
248 d_true(),
249 d_false(),
250 d_interrupted(false),
251 d_resourceManager(rm),
252 d_inPreregister(false),
253 d_factsAsserted(context, false),
254 d_attr_handle(),
255 d_arithSubstitutionsAdded("theory::arith::zzz::arith::substitutions", 0)
256 {
257 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId != theory::THEORY_LAST;
258 ++ theoryId)
259 {
260 d_theoryTable[theoryId] = NULL;
261 d_theoryOut[theoryId] = NULL;
262 }
263
264 smtStatisticsRegistry()->registerStat(&d_combineTheoriesTime);
265 d_true = NodeManager::currentNM()->mkConst<bool>(true);
266 d_false = NodeManager::currentNM()->mkConst<bool>(false);
267
268 smtStatisticsRegistry()->registerStat(&d_arithSubstitutionsAdded);
269 }
270
271 TheoryEngine::~TheoryEngine() {
272 Assert(d_hasShutDown);
273
274 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId != theory::THEORY_LAST; ++ theoryId) {
275 if(d_theoryTable[theoryId] != NULL) {
276 delete d_theoryTable[theoryId];
277 delete d_theoryOut[theoryId];
278 }
279 }
280
281 delete d_quantEngine;
282
283 smtStatisticsRegistry()->unregisterStat(&d_combineTheoriesTime);
284 smtStatisticsRegistry()->unregisterStat(&d_arithSubstitutionsAdded);
285 }
286
287 void TheoryEngine::interrupt() { d_interrupted = true; }
288 void TheoryEngine::preRegister(TNode preprocessed) {
289 Debug("theory") << "TheoryEngine::preRegister( " << preprocessed << ")"
290 << std::endl;
291 d_preregisterQueue.push(preprocessed);
292
293 if (!d_inPreregister) {
294 // We're in pre-register
295 d_inPreregister = true;
296
297 // Process the pre-registration queue
298 while (!d_preregisterQueue.empty()) {
299 // Get the next atom to pre-register
300 preprocessed = d_preregisterQueue.front();
301 d_preregisterQueue.pop();
302
303 if (d_logicInfo.isSharingEnabled() && preprocessed.getKind() == kind::EQUAL) {
304 // When sharing is enabled, we propagate from the shared terms manager also
305 d_sharedTerms.addEqualityToPropagate(preprocessed);
306 }
307
308 // the atom should not have free variables
309 Debug("theory") << "TheoryEngine::preRegister: " << preprocessed
310 << std::endl;
311 Assert(!expr::hasFreeVar(preprocessed));
312
313 // Pre-register the terms in the atom
314 theory::TheoryIdSet theories = NodeVisitor<PreRegisterVisitor>::run(
315 d_preRegistrationVisitor, preprocessed);
316 theories = TheoryIdSetUtil::setRemove(THEORY_BOOL, theories);
317 // Remove the top theory, if any more that means multiple theories were
318 // involved
319 bool multipleTheories =
320 TheoryIdSetUtil::setRemove(Theory::theoryOf(preprocessed), theories);
321 if (Configuration::isAssertionBuild())
322 {
323 TheoryId i;
324 // This should never throw an exception, since theories should be
325 // guaranteed to be initialized.
326 // These checks don't work with finite model finding, because it
327 // uses Rational constants to represent cardinality constraints,
328 // even though arithmetic isn't actually involved.
329 if (!options::finiteModelFind())
330 {
331 while ((i = TheoryIdSetUtil::setPop(theories)) != THEORY_LAST)
332 {
333 if (!d_logicInfo.isTheoryEnabled(i))
334 {
335 LogicInfo newLogicInfo = d_logicInfo.getUnlockedCopy();
336 newLogicInfo.enableTheory(i);
337 newLogicInfo.lock();
338 std::stringstream ss;
339 ss << "The logic was specified as " << d_logicInfo.getLogicString()
340 << ", which doesn't include " << i
341 << ", but found a term in that theory." << std::endl
342 << "You might want to extend your logic to "
343 << newLogicInfo.getLogicString() << std::endl;
344 throw LogicException(ss.str());
345 }
346 }
347 }
348 }
349 if (multipleTheories) {
350 // Collect the shared terms if there are multiple theories
351 NodeVisitor<SharedTermsVisitor>::run(d_sharedTermsVisitor,
352 preprocessed);
353 }
354 }
355
356 // Leaving pre-register
357 d_inPreregister = false;
358 }
359 }
360
361 void TheoryEngine::printAssertions(const char* tag) {
362 if (Trace.isOn(tag)) {
363
364 for (TheoryId theoryId = THEORY_FIRST; theoryId < THEORY_LAST; ++theoryId) {
365 Theory* theory = d_theoryTable[theoryId];
366 if (theory && d_logicInfo.isTheoryEnabled(theoryId)) {
367 Trace(tag) << "--------------------------------------------" << endl;
368 Trace(tag) << "Assertions of " << theory->getId() << ": " << endl;
369 {
370 context::CDList<Assertion>::const_iterator it = theory->facts_begin(),
371 it_end =
372 theory->facts_end();
373 for (unsigned i = 0; it != it_end; ++it, ++i)
374 {
375 if ((*it).d_isPreregistered)
376 {
377 Trace(tag) << "[" << i << "]: ";
378 }
379 else
380 {
381 Trace(tag) << "(" << i << "): ";
382 }
383 Trace(tag) << (*it).d_assertion << endl;
384 }
385 }
386
387 if (d_logicInfo.isSharingEnabled()) {
388 Trace(tag) << "Shared terms of " << theory->getId() << ": " << endl;
389 context::CDList<TNode>::const_iterator it = theory->shared_terms_begin(), it_end = theory->shared_terms_end();
390 for (unsigned i = 0; it != it_end; ++ it, ++i) {
391 Trace(tag) << "[" << i << "]: " << (*it) << endl;
392 }
393 }
394 }
395 }
396 }
397 }
398
399 void TheoryEngine::dumpAssertions(const char* tag) {
400 if (Dump.isOn(tag)) {
401 const Printer& printer = d_outMgr.getPrinter();
402 std::ostream& out = d_outMgr.getDumpOut();
403 printer.toStreamCmdComment(out, "Starting completeness check");
404 for (TheoryId theoryId = THEORY_FIRST; theoryId < THEORY_LAST; ++theoryId) {
405 Theory* theory = d_theoryTable[theoryId];
406 if (theory && d_logicInfo.isTheoryEnabled(theoryId)) {
407 printer.toStreamCmdComment(out, "Completeness check");
408 printer.toStreamCmdPush(out);
409
410 // Dump the shared terms
411 if (d_logicInfo.isSharingEnabled()) {
412 printer.toStreamCmdComment(out, "Shared terms");
413 context::CDList<TNode>::const_iterator it = theory->shared_terms_begin(), it_end = theory->shared_terms_end();
414 for (unsigned i = 0; it != it_end; ++ it, ++i) {
415 stringstream ss;
416 ss << (*it);
417 printer.toStreamCmdComment(out, ss.str());
418 }
419 }
420
421 // Dump the assertions
422 printer.toStreamCmdComment(out, "Assertions");
423 context::CDList<Assertion>::const_iterator it = theory->facts_begin(), it_end = theory->facts_end();
424 for (; it != it_end; ++ it) {
425 // Get the assertion
426 Node assertionNode = (*it).d_assertion;
427 // Purify all the terms
428
429 if ((*it).d_isPreregistered)
430 {
431 printer.toStreamCmdComment(out, "Preregistered");
432 }
433 else
434 {
435 printer.toStreamCmdComment(out, "Shared assertion");
436 }
437 printer.toStreamCmdAssert(out, assertionNode);
438 }
439 printer.toStreamCmdCheckSat(out);
440
441 printer.toStreamCmdPop(out);
442 }
443 }
444 }
445 }
446
447 /**
448 * Check all (currently-active) theories for conflicts.
449 * @param effort the effort level to use
450 */
451 void TheoryEngine::check(Theory::Effort effort) {
452 // spendResource();
453
454 // Reset the interrupt flag
455 d_interrupted = false;
456
457 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
458 #undef CVC4_FOR_EACH_THEORY_STATEMENT
459 #endif
460 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
461 if (theory::TheoryTraits<THEORY>::hasCheck \
462 && d_logicInfo.isTheoryEnabled(THEORY)) \
463 { \
464 theoryOf(THEORY)->check(effort); \
465 if (d_inConflict) \
466 { \
467 Debug("conflict") << THEORY << " in conflict. " << std::endl; \
468 break; \
469 } \
470 }
471
472 // Do the checking
473 try {
474
475 // Mark the output channel unused (if this is FULL_EFFORT, and nothing
476 // is done by the theories, no additional check will be needed)
477 d_outputChannelUsed = false;
478
479 // Mark the lemmas flag (no lemmas added)
480 d_lemmasAdded = false;
481
482 Debug("theory") << "TheoryEngine::check(" << effort << "): d_factsAsserted = " << (d_factsAsserted ? "true" : "false") << endl;
483
484 // If in full effort, we have a fake new assertion just to jumpstart the checking
485 if (Theory::fullEffort(effort)) {
486 d_factsAsserted = true;
487 // Reset round for the relevance manager, which notice only sets a flag
488 // to indicate that its information must be recomputed.
489 if (d_relManager != nullptr)
490 {
491 d_relManager->resetRound();
492 }
493 d_tc->resetRound();
494 }
495
496 // Check until done
497 while (d_factsAsserted && !d_inConflict && !d_lemmasAdded) {
498
499 Debug("theory") << "TheoryEngine::check(" << effort << "): running check" << endl;
500
501 Trace("theory::assertions") << endl;
502 if (Trace.isOn("theory::assertions")) {
503 printAssertions("theory::assertions");
504 }
505
506 if(Theory::fullEffort(effort)) {
507 Trace("theory::assertions::fulleffort") << endl;
508 if (Trace.isOn("theory::assertions::fulleffort")) {
509 printAssertions("theory::assertions::fulleffort");
510 }
511 }
512
513 // Note that we've discharged all the facts
514 d_factsAsserted = false;
515
516 // Do the checking
517 CVC4_FOR_EACH_THEORY;
518
519 Debug("theory") << "TheoryEngine::check(" << effort << "): running propagation after the initial check" << endl;
520
521 // We are still satisfiable, propagate as much as possible
522 propagate(effort);
523
524 // We do combination if all has been processed and we are in fullcheck
525 if (Theory::fullEffort(effort) && d_logicInfo.isSharingEnabled() && !d_factsAsserted && !d_lemmasAdded && !d_inConflict) {
526 // Do the combination
527 Debug("theory") << "TheoryEngine::check(" << effort << "): running combination" << endl;
528 {
529 TimerStat::CodeTimer combineTheoriesTimer(d_combineTheoriesTime);
530 d_tc->combineTheories();
531 }
532 if(d_logicInfo.isQuantified()){
533 d_quantEngine->notifyCombineTheories();
534 }
535 }
536 }
537
538 // Must consult quantifiers theory for last call to ensure sat, or otherwise add a lemma
539 if( Theory::fullEffort(effort) && ! d_inConflict && ! needCheck() ) {
540 Trace("theory::assertions-model") << endl;
541 if (Trace.isOn("theory::assertions-model")) {
542 printAssertions("theory::assertions-model");
543 }
544 // reset the model in the combination engine
545 d_tc->resetModel();
546 //checks for theories requiring the model go at last call
547 for (TheoryId theoryId = THEORY_FIRST; theoryId < THEORY_LAST; ++theoryId) {
548 if( theoryId!=THEORY_QUANTIFIERS ){
549 Theory* theory = d_theoryTable[theoryId];
550 if (theory && d_logicInfo.isTheoryEnabled(theoryId)) {
551 if( theory->needsCheckLastEffort() ){
552 if (!d_tc->buildModel())
553 {
554 break;
555 }
556 theory->check(Theory::EFFORT_LAST_CALL);
557 }
558 }
559 }
560 }
561 if (!d_inConflict)
562 {
563 if(d_logicInfo.isQuantified()) {
564 // quantifiers engine must check at last call effort
565 d_quantEngine->check(Theory::EFFORT_LAST_CALL);
566 }
567 }
568 if (!d_inConflict && !needCheck())
569 {
570 // If d_eager_model_building is false, then we only mark that we
571 // are in "SAT mode". We build the model later only if the user asks
572 // for it via getBuiltModel.
573 d_inSatMode = true;
574 if (d_eager_model_building)
575 {
576 d_tc->buildModel();
577 }
578 }
579 }
580
581 Debug("theory") << "TheoryEngine::check(" << effort << "): done, we are " << (d_inConflict ? "unsat" : "sat") << (d_lemmasAdded ? " with new lemmas" : " with no new lemmas");
582 Debug("theory") << ", need check = " << (needCheck() ? "YES" : "NO") << endl;
583
584 if( Theory::fullEffort(effort) && !d_inConflict && !needCheck()) {
585 // Do post-processing of model from the theories (e.g. used for THEORY_SEP
586 // to construct heap model)
587 d_tc->postProcessModel(d_incomplete.get());
588 }
589 } catch(const theory::Interrupted&) {
590 Trace("theory") << "TheoryEngine::check() => interrupted" << endl;
591 }
592 // If fulleffort, check all theories
593 if(Dump.isOn("theory::fullcheck") && Theory::fullEffort(effort)) {
594 if (!d_inConflict && !needCheck()) {
595 dumpAssertions("theory::fullcheck");
596 }
597 }
598 }
599
600 void TheoryEngine::propagate(Theory::Effort effort)
601 {
602 // Reset the interrupt flag
603 d_interrupted = false;
604
605 // Definition of the statement that is to be run by every theory
606 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
607 #undef CVC4_FOR_EACH_THEORY_STATEMENT
608 #endif
609 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
610 if (theory::TheoryTraits<THEORY>::hasPropagate && d_logicInfo.isTheoryEnabled(THEORY)) { \
611 theoryOf(THEORY)->propagate(effort); \
612 }
613
614 // Reset the interrupt flag
615 d_interrupted = false;
616
617 // Propagate for each theory using the statement above
618 CVC4_FOR_EACH_THEORY;
619 }
620
621 Node TheoryEngine::getNextDecisionRequest()
622 {
623 return d_decManager->getNextDecisionRequest();
624 }
625
626 bool TheoryEngine::properConflict(TNode conflict) const {
627 bool value;
628 if (conflict.getKind() == kind::AND) {
629 for (unsigned i = 0; i < conflict.getNumChildren(); ++ i) {
630 if (! getPropEngine()->hasValue(conflict[i], value)) {
631 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
632 << conflict[i] << endl;
633 return false;
634 }
635 if (! value) {
636 Debug("properConflict") << "Bad conflict is due to false atom: "
637 << conflict[i] << endl;
638 return false;
639 }
640 if (conflict[i] != Rewriter::rewrite(conflict[i])) {
641 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
642 << conflict[i] << " vs " << Rewriter::rewrite(conflict[i]) << endl;
643 return false;
644 }
645 }
646 } else {
647 if (! getPropEngine()->hasValue(conflict, value)) {
648 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
649 << conflict << endl;
650 return false;
651 }
652 if(! value) {
653 Debug("properConflict") << "Bad conflict is due to false atom: "
654 << conflict << endl;
655 return false;
656 }
657 if (conflict != Rewriter::rewrite(conflict)) {
658 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
659 << conflict << " vs " << Rewriter::rewrite(conflict) << endl;
660 return false;
661 }
662 }
663 return true;
664 }
665
666 TheoryModel* TheoryEngine::getModel()
667 {
668 Assert(d_tc != nullptr);
669 TheoryModel* m = d_tc->getModel();
670 Assert(m != nullptr);
671 return m;
672 }
673
674 TheoryModel* TheoryEngine::getBuiltModel()
675 {
676 Assert(d_tc != nullptr);
677 // If this method was called, we should be in SAT mode, and produceModels
678 // should be true.
679 AlwaysAssert(options::produceModels());
680 if (!d_inSatMode)
681 {
682 // not available, perhaps due to interuption.
683 return nullptr;
684 }
685 // must build model at this point
686 if (!d_tc->buildModel())
687 {
688 return nullptr;
689 }
690 return d_tc->getModel();
691 }
692
693 bool TheoryEngine::buildModel()
694 {
695 Assert(d_tc != nullptr);
696 return d_tc->buildModel();
697 }
698
699 bool TheoryEngine::getSynthSolutions(
700 std::map<Node, std::map<Node, Node>>& sol_map)
701 {
702 if (d_quantEngine)
703 {
704 return d_quantEngine->getSynthSolutions(sol_map);
705 }
706 // we are not in a quantified logic, there is no synthesis solution
707 return false;
708 }
709
710 bool TheoryEngine::presolve() {
711 // Reset the interrupt flag
712 d_interrupted = false;
713
714 // Reset the decision manager. This clears its decision strategies that are
715 // no longer valid in this user context.
716 d_decManager->presolve();
717
718 try {
719 // Definition of the statement that is to be run by every theory
720 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
721 #undef CVC4_FOR_EACH_THEORY_STATEMENT
722 #endif
723 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
724 if (theory::TheoryTraits<THEORY>::hasPresolve) { \
725 theoryOf(THEORY)->presolve(); \
726 if(d_inConflict) { \
727 return true; \
728 } \
729 }
730
731 // Presolve for each theory using the statement above
732 CVC4_FOR_EACH_THEORY;
733 } catch(const theory::Interrupted&) {
734 Trace("theory") << "TheoryEngine::presolve() => interrupted" << endl;
735 }
736 // return whether we have a conflict
737 return false;
738 }/* TheoryEngine::presolve() */
739
740 void TheoryEngine::postsolve() {
741 // no longer in SAT mode
742 d_inSatMode = false;
743 // Reset the interrupt flag
744 d_interrupted = false;
745 bool CVC4_UNUSED wasInConflict = d_inConflict;
746
747 try {
748 // Definition of the statement that is to be run by every theory
749 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
750 #undef CVC4_FOR_EACH_THEORY_STATEMENT
751 #endif
752 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
753 if (theory::TheoryTraits<THEORY>::hasPostsolve) \
754 { \
755 theoryOf(THEORY)->postsolve(); \
756 Assert(!d_inConflict || wasInConflict) \
757 << "conflict raised during postsolve()"; \
758 }
759
760 // Postsolve for each theory using the statement above
761 CVC4_FOR_EACH_THEORY;
762 } catch(const theory::Interrupted&) {
763 Trace("theory") << "TheoryEngine::postsolve() => interrupted" << endl;
764 }
765 }/* TheoryEngine::postsolve() */
766
767
768 void TheoryEngine::notifyRestart() {
769 // Reset the interrupt flag
770 d_interrupted = false;
771
772 // Definition of the statement that is to be run by every theory
773 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
774 #undef CVC4_FOR_EACH_THEORY_STATEMENT
775 #endif
776 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
777 if (theory::TheoryTraits<THEORY>::hasNotifyRestart && d_logicInfo.isTheoryEnabled(THEORY)) { \
778 theoryOf(THEORY)->notifyRestart(); \
779 }
780
781 // notify each theory using the statement above
782 CVC4_FOR_EACH_THEORY;
783 }
784
785 void TheoryEngine::ppStaticLearn(TNode in, NodeBuilder<>& learned) {
786 // Reset the interrupt flag
787 d_interrupted = false;
788
789 // Definition of the statement that is to be run by every theory
790 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
791 #undef CVC4_FOR_EACH_THEORY_STATEMENT
792 #endif
793 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
794 if (theory::TheoryTraits<THEORY>::hasPpStaticLearn) { \
795 theoryOf(THEORY)->ppStaticLearn(in, learned); \
796 }
797
798 // static learning for each theory using the statement above
799 CVC4_FOR_EACH_THEORY;
800 }
801
802 bool TheoryEngine::isRelevant(Node lit) const
803 {
804 if (d_relManager != nullptr)
805 {
806 return d_relManager->isRelevant(lit);
807 }
808 // otherwise must assume its relevant
809 return true;
810 }
811
812 void TheoryEngine::shutdown() {
813 // Set this first; if a Theory shutdown() throws an exception,
814 // at least the destruction of the TheoryEngine won't confound
815 // matters.
816 d_hasShutDown = true;
817
818 // Shutdown all the theories
819 for(TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST; ++theoryId) {
820 if(d_theoryTable[theoryId]) {
821 theoryOf(theoryId)->shutdown();
822 }
823 }
824
825 d_tpp.clearCache();
826 }
827
828 theory::Theory::PPAssertStatus TheoryEngine::solve(TNode literal, SubstitutionMap& substitutionOut) {
829 // Reset the interrupt flag
830 d_interrupted = false;
831
832 TNode atom = literal.getKind() == kind::NOT ? literal[0] : literal;
833 Trace("theory::solve") << "TheoryEngine::solve(" << literal << "): solving with " << theoryOf(atom)->getId() << endl;
834
835 if(! d_logicInfo.isTheoryEnabled(Theory::theoryOf(atom)) &&
836 Theory::theoryOf(atom) != THEORY_SAT_SOLVER) {
837 stringstream ss;
838 ss << "The logic was specified as " << d_logicInfo.getLogicString()
839 << ", which doesn't include " << Theory::theoryOf(atom)
840 << ", but got a preprocessing-time fact for that theory." << endl
841 << "The fact:" << endl
842 << literal;
843 throw LogicException(ss.str());
844 }
845
846 Theory::PPAssertStatus solveStatus = theoryOf(atom)->ppAssert(literal, substitutionOut);
847 Trace("theory::solve") << "TheoryEngine::solve(" << literal << ") => " << solveStatus << endl;
848 return solveStatus;
849 }
850
851 void TheoryEngine::preprocessStart() { d_tpp.clearCache(); }
852
853 Node TheoryEngine::preprocess(TNode assertion) {
854 TrustNode trn = d_tpp.theoryPreprocess(assertion);
855 if (trn.isNull())
856 {
857 // no change
858 return assertion;
859 }
860 return trn.getNode();
861 }
862
863 void TheoryEngine::notifyPreprocessedAssertions(
864 const std::vector<Node>& assertions) {
865 // call all the theories
866 for (TheoryId theoryId = theory::THEORY_FIRST; theoryId < theory::THEORY_LAST;
867 ++theoryId) {
868 if (d_theoryTable[theoryId]) {
869 theoryOf(theoryId)->ppNotifyAssertions(assertions);
870 }
871 }
872 if (d_relManager != nullptr)
873 {
874 d_relManager->notifyPreprocessedAssertions(assertions);
875 }
876 }
877
878 bool TheoryEngine::markPropagation(TNode assertion, TNode originalAssertion, theory::TheoryId toTheoryId, theory::TheoryId fromTheoryId) {
879
880 // What and where we are asserting
881 NodeTheoryPair toAssert(assertion, toTheoryId, d_propagationMapTimestamp);
882 // What and where it came from
883 NodeTheoryPair toExplain(originalAssertion, fromTheoryId, d_propagationMapTimestamp);
884
885 // See if the theory already got this literal
886 PropagationMap::const_iterator find = d_propagationMap.find(toAssert);
887 if (find != d_propagationMap.end()) {
888 // The theory already knows this
889 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): already there" << endl;
890 return false;
891 }
892
893 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): marking [" << d_propagationMapTimestamp << "] " << assertion << ", " << toTheoryId << " from " << originalAssertion << ", " << fromTheoryId << endl;
894
895 // Mark the propagation
896 d_propagationMap[toAssert] = toExplain;
897 d_propagationMapTimestamp = d_propagationMapTimestamp + 1;
898
899 return true;
900 }
901
902
903 void TheoryEngine::assertToTheory(TNode assertion, TNode originalAssertion, theory::TheoryId toTheoryId, theory::TheoryId fromTheoryId) {
904
905 Trace("theory::assertToTheory") << "TheoryEngine::assertToTheory(" << assertion << ", " << originalAssertion << "," << toTheoryId << ", " << fromTheoryId << ")" << endl;
906
907 Assert(toTheoryId != fromTheoryId);
908 if(toTheoryId != THEORY_SAT_SOLVER &&
909 ! d_logicInfo.isTheoryEnabled(toTheoryId)) {
910 stringstream ss;
911 ss << "The logic was specified as " << d_logicInfo.getLogicString()
912 << ", which doesn't include " << toTheoryId
913 << ", but got an asserted fact to that theory." << endl
914 << "The fact:" << endl
915 << assertion;
916 throw LogicException(ss.str());
917 }
918
919 if (d_inConflict) {
920 return;
921 }
922
923 // If sharing is disabled, things are easy
924 if (!d_logicInfo.isSharingEnabled()) {
925 Assert(assertion == originalAssertion);
926 if (fromTheoryId == THEORY_SAT_SOLVER) {
927 // Send to the apropriate theory
928 theory::Theory* toTheory = theoryOf(toTheoryId);
929 // We assert it, and we know it's preregistereed
930 toTheory->assertFact(assertion, true);
931 // Mark that we have more information
932 d_factsAsserted = true;
933 } else {
934 Assert(toTheoryId == THEORY_SAT_SOLVER);
935 // Check for propositional conflict
936 bool value;
937 if (d_propEngine->hasValue(assertion, value)) {
938 if (!value) {
939 Trace("theory::propagate") << "TheoryEngine::assertToTheory(" << assertion << ", " << toTheoryId << ", " << fromTheoryId << "): conflict (no sharing)" << endl;
940 Trace("dtview::conflict")
941 << ":THEORY-CONFLICT: " << assertion << std::endl;
942 d_inConflict = true;
943 } else {
944 return;
945 }
946 }
947 d_propagatedLiterals.push_back(assertion);
948 }
949 return;
950 }
951
952 // Polarity of the assertion
953 bool polarity = assertion.getKind() != kind::NOT;
954
955 // Atom of the assertion
956 TNode atom = polarity ? assertion : assertion[0];
957
958 // If sending to the shared terms database, it's also simple
959 if (toTheoryId == THEORY_BUILTIN) {
960 Assert(atom.getKind() == kind::EQUAL)
961 << "atom should be an EQUALity, not `" << atom << "'";
962 if (markPropagation(assertion, originalAssertion, toTheoryId, fromTheoryId)) {
963 d_sharedTerms.assertEquality(atom, polarity, assertion);
964 }
965 return;
966 }
967
968 // Things from the SAT solver are already normalized, so they go
969 // directly to the apropriate theory
970 if (fromTheoryId == THEORY_SAT_SOLVER) {
971 // We know that this is normalized, so just send it off to the theory
972 if (markPropagation(assertion, originalAssertion, toTheoryId, fromTheoryId)) {
973 // Is it preregistered
974 bool preregistered = d_propEngine->isSatLiteral(assertion) && Theory::theoryOf(assertion) == toTheoryId;
975 // We assert it
976 theoryOf(toTheoryId)->assertFact(assertion, preregistered);
977 // Mark that we have more information
978 d_factsAsserted = true;
979 }
980 return;
981 }
982
983 // Propagations to the SAT solver are just enqueued for pickup by
984 // the SAT solver later
985 if (toTheoryId == THEORY_SAT_SOLVER) {
986 if (markPropagation(assertion, originalAssertion, toTheoryId, fromTheoryId)) {
987 // Enqueue for propagation to the SAT solver
988 d_propagatedLiterals.push_back(assertion);
989 // Check for propositional conflicts
990 bool value;
991 if (d_propEngine->hasValue(assertion, value) && !value) {
992 Trace("theory::propagate")
993 << "TheoryEngine::assertToTheory(" << assertion << ", "
994 << toTheoryId << ", " << fromTheoryId << "): conflict (sharing)"
995 << endl;
996 Trace("dtview::conflict")
997 << ":THEORY-CONFLICT: " << assertion << std::endl;
998 d_inConflict = true;
999 }
1000 }
1001 return;
1002 }
1003
1004 Assert(atom.getKind() == kind::EQUAL);
1005
1006 // Normalize
1007 Node normalizedLiteral = Rewriter::rewrite(assertion);
1008
1009 // See if it rewrites false directly -> conflict
1010 if (normalizedLiteral.isConst()) {
1011 if (!normalizedLiteral.getConst<bool>()) {
1012 // Mark the propagation for explanations
1013 if (markPropagation(normalizedLiteral, originalAssertion, toTheoryId, fromTheoryId)) {
1014 // special case, trust node has no proof generator
1015 TrustNode trnn = TrustNode::mkTrustConflict(normalizedLiteral);
1016 // Get the explanation (conflict will figure out where it came from)
1017 conflict(trnn, toTheoryId);
1018 } else {
1019 Unreachable();
1020 }
1021 return;
1022 }
1023 }
1024
1025 // Try and assert (note that we assert the non-normalized one)
1026 if (markPropagation(assertion, originalAssertion, toTheoryId, fromTheoryId)) {
1027 // Check if has been pre-registered with the theory
1028 bool preregistered = d_propEngine->isSatLiteral(assertion) && Theory::theoryOf(assertion) == toTheoryId;
1029 // Assert away
1030 theoryOf(toTheoryId)->assertFact(assertion, preregistered);
1031 d_factsAsserted = true;
1032 }
1033
1034 return;
1035 }
1036
1037 void TheoryEngine::assertFact(TNode literal)
1038 {
1039 Trace("theory") << "TheoryEngine::assertFact(" << literal << ")" << endl;
1040
1041 // spendResource();
1042
1043 // If we're in conflict, nothing to do
1044 if (d_inConflict) {
1045 return;
1046 }
1047
1048 // Get the atom
1049 bool polarity = literal.getKind() != kind::NOT;
1050 TNode atom = polarity ? literal : literal[0];
1051
1052 if (d_logicInfo.isSharingEnabled()) {
1053 // If any shared terms, it's time to do sharing work
1054 if (d_sharedTerms.hasSharedTerms(atom)) {
1055 // Notify the theories the shared terms
1056 SharedTermsDatabase::shared_terms_iterator it = d_sharedTerms.begin(atom);
1057 SharedTermsDatabase::shared_terms_iterator it_end = d_sharedTerms.end(atom);
1058 for (; it != it_end; ++ it) {
1059 TNode term = *it;
1060 theory::TheoryIdSet theories =
1061 d_sharedTerms.getTheoriesToNotify(atom, term);
1062 for (TheoryId id = THEORY_FIRST; id != THEORY_LAST; ++ id) {
1063 if (TheoryIdSetUtil::setContains(id, theories))
1064 {
1065 theoryOf(id)->addSharedTerm(term);
1066 }
1067 }
1068 d_sharedTerms.markNotified(term, theories);
1069 }
1070 }
1071
1072 // If it's an equality, assert it to the shared term manager, even though the terms are not
1073 // yet shared. As the terms become shared later, the shared terms manager will then add them
1074 // to the assert the equality to the interested theories
1075 if (atom.getKind() == kind::EQUAL) {
1076 // Assert it to the the owning theory
1077 assertToTheory(literal, literal, /* to */ Theory::theoryOf(atom), /* from */ THEORY_SAT_SOLVER);
1078 // Shared terms manager will assert to interested theories directly, as
1079 // the terms become shared
1080 assertToTheory(literal, literal, /* to */ THEORY_BUILTIN, /* from */ THEORY_SAT_SOLVER);
1081
1082 // Now, let's check for any atom triggers from lemmas
1083 AtomRequests::atom_iterator it = d_atomRequests.getAtomIterator(atom);
1084 while (!it.done()) {
1085 const AtomRequests::Request& request = it.get();
1086 Node toAssert =
1087 polarity ? (Node)request.d_atom : request.d_atom.notNode();
1088 Debug("theory::atoms") << "TheoryEngine::assertFact(" << literal << "): sending requested " << toAssert << endl;
1089 assertToTheory(
1090 toAssert, literal, request.d_toTheory, THEORY_SAT_SOLVER);
1091 it.next();
1092 }
1093
1094 } else {
1095 // Not an equality, just assert to the appropriate theory
1096 assertToTheory(literal, literal, /* to */ Theory::theoryOf(atom), /* from */ THEORY_SAT_SOLVER);
1097 }
1098 } else {
1099 // Assert the fact to the appropriate theory directly
1100 assertToTheory(literal, literal, /* to */ Theory::theoryOf(atom), /* from */ THEORY_SAT_SOLVER);
1101 }
1102 }
1103
1104 bool TheoryEngine::propagate(TNode literal, theory::TheoryId theory) {
1105 Debug("theory::propagate") << "TheoryEngine::propagate(" << literal << ", " << theory << ")" << endl;
1106
1107 Trace("dtview::prop") << std::string(d_context->getLevel(), ' ')
1108 << ":THEORY-PROP: " << literal << endl;
1109
1110 // spendResource();
1111
1112 // Get the atom
1113 bool polarity = literal.getKind() != kind::NOT;
1114 TNode atom = polarity ? literal : literal[0];
1115
1116 if (d_logicInfo.isSharingEnabled() && atom.getKind() == kind::EQUAL) {
1117 if (d_propEngine->isSatLiteral(literal)) {
1118 // We propagate SAT literals to SAT
1119 assertToTheory(literal, literal, /* to */ THEORY_SAT_SOLVER, /* from */ theory);
1120 }
1121 if (theory != THEORY_BUILTIN) {
1122 // Assert to the shared terms database
1123 assertToTheory(literal, literal, /* to */ THEORY_BUILTIN, /* from */ theory);
1124 }
1125 } else {
1126 // Just send off to the SAT solver
1127 Assert(d_propEngine->isSatLiteral(literal));
1128 assertToTheory(literal, literal, /* to */ THEORY_SAT_SOLVER, /* from */ theory);
1129 }
1130
1131 return !d_inConflict;
1132 }
1133
1134 const LogicInfo& TheoryEngine::getLogicInfo() const { return d_logicInfo; }
1135
1136 theory::EqualityStatus TheoryEngine::getEqualityStatus(TNode a, TNode b) {
1137 Assert(a.getType().isComparableTo(b.getType()));
1138 if (d_sharedTerms.isShared(a) && d_sharedTerms.isShared(b)) {
1139 if (d_sharedTerms.areEqual(a,b)) {
1140 return EQUALITY_TRUE_AND_PROPAGATED;
1141 }
1142 else if (d_sharedTerms.areDisequal(a,b)) {
1143 return EQUALITY_FALSE_AND_PROPAGATED;
1144 }
1145 }
1146 return theoryOf(Theory::theoryOf(a.getType()))->getEqualityStatus(a, b);
1147 }
1148
1149 Node TheoryEngine::getModelValue(TNode var) {
1150 if (var.isConst())
1151 {
1152 // the model value of a constant must be itself
1153 return var;
1154 }
1155 Assert(d_sharedTerms.isShared(var));
1156 return theoryOf(Theory::theoryOf(var.getType()))->getModelValue(var);
1157 }
1158
1159
1160 Node TheoryEngine::ensureLiteral(TNode n) {
1161 Debug("ensureLiteral") << "rewriting: " << n << std::endl;
1162 Node rewritten = Rewriter::rewrite(n);
1163 Debug("ensureLiteral") << " got: " << rewritten << std::endl;
1164 Node preprocessed = preprocess(rewritten);
1165 Debug("ensureLiteral") << "preprocessed: " << preprocessed << std::endl;
1166 d_propEngine->ensureLiteral(preprocessed);
1167 return preprocessed;
1168 }
1169
1170
1171 void TheoryEngine::printInstantiations( std::ostream& out ) {
1172 if( d_quantEngine ){
1173 d_quantEngine->printInstantiations( out );
1174 }else{
1175 out << "Internal error : instantiations not available when quantifiers are not present." << std::endl;
1176 Assert(false);
1177 }
1178 }
1179
1180 void TheoryEngine::printSynthSolution( std::ostream& out ) {
1181 if( d_quantEngine ){
1182 d_quantEngine->printSynthSolution( out );
1183 }else{
1184 out << "Internal error : synth solution not available when quantifiers are not present." << std::endl;
1185 Assert(false);
1186 }
1187 }
1188
1189 void TheoryEngine::getInstantiatedQuantifiedFormulas( std::vector< Node >& qs ) {
1190 if( d_quantEngine ){
1191 d_quantEngine->getInstantiatedQuantifiedFormulas( qs );
1192 }else{
1193 Assert(false);
1194 }
1195 }
1196
1197 void TheoryEngine::getInstantiations( Node q, std::vector< Node >& insts ) {
1198 if( d_quantEngine ){
1199 d_quantEngine->getInstantiations( q, insts );
1200 }else{
1201 Assert(false);
1202 }
1203 }
1204
1205 void TheoryEngine::getInstantiationTermVectors( Node q, std::vector< std::vector< Node > >& tvecs ) {
1206 if( d_quantEngine ){
1207 d_quantEngine->getInstantiationTermVectors( q, tvecs );
1208 }else{
1209 Assert(false);
1210 }
1211 }
1212
1213 void TheoryEngine::getInstantiations( std::map< Node, std::vector< Node > >& insts ) {
1214 if( d_quantEngine ){
1215 d_quantEngine->getInstantiations( insts );
1216 }else{
1217 Assert(false);
1218 }
1219 }
1220
1221 void TheoryEngine::getInstantiationTermVectors( std::map< Node, std::vector< std::vector< Node > > >& insts ) {
1222 if( d_quantEngine ){
1223 d_quantEngine->getInstantiationTermVectors( insts );
1224 }else{
1225 Assert(false);
1226 }
1227 }
1228
1229 Node TheoryEngine::getInstantiatedConjunction( Node q ) {
1230 if( d_quantEngine ){
1231 return d_quantEngine->getInstantiatedConjunction( q );
1232 }else{
1233 Assert(false);
1234 return Node::null();
1235 }
1236 }
1237
1238
1239 static Node mkExplanation(const std::vector<NodeTheoryPair>& explanation) {
1240
1241 std::set<TNode> all;
1242 for (unsigned i = 0; i < explanation.size(); ++ i) {
1243 Assert(explanation[i].d_theory == THEORY_SAT_SOLVER);
1244 all.insert(explanation[i].d_node);
1245 }
1246
1247 if (all.size() == 0) {
1248 // Normalize to true
1249 return NodeManager::currentNM()->mkConst<bool>(true);
1250 }
1251
1252 if (all.size() == 1) {
1253 // All the same, or just one
1254 return explanation[0].d_node;
1255 }
1256
1257 NodeBuilder<> conjunction(kind::AND);
1258 std::set<TNode>::const_iterator it = all.begin();
1259 std::set<TNode>::const_iterator it_end = all.end();
1260 while (it != it_end) {
1261 conjunction << *it;
1262 ++ it;
1263 }
1264
1265 return conjunction;
1266 }
1267
1268 TrustNode TheoryEngine::getExplanation(TNode node)
1269 {
1270 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
1271 << "): current propagation index = "
1272 << d_propagationMapTimestamp << endl;
1273
1274 bool polarity = node.getKind() != kind::NOT;
1275 TNode atom = polarity ? node : node[0];
1276
1277 // If we're not in shared mode, explanations are simple
1278 if (!d_logicInfo.isSharingEnabled())
1279 {
1280 Debug("theory::explain")
1281 << "TheoryEngine::getExplanation: sharing is NOT enabled. "
1282 << " Responsible theory is: " << theoryOf(atom)->getId() << std::endl;
1283
1284 TrustNode texplanation = theoryOf(atom)->explain(node);
1285 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
1286 << ") => " << texplanation.getNode() << endl;
1287 return texplanation;
1288 }
1289
1290 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing IS enabled"
1291 << std::endl;
1292
1293 // Initial thing to explain
1294 NodeTheoryPair toExplain(node, THEORY_SAT_SOLVER, d_propagationMapTimestamp);
1295 Assert(d_propagationMap.find(toExplain) != d_propagationMap.end());
1296
1297 NodeTheoryPair nodeExplainerPair = d_propagationMap[toExplain];
1298 Debug("theory::explain")
1299 << "TheoryEngine::getExplanation: explainer for node "
1300 << nodeExplainerPair.d_node
1301 << " is theory: " << nodeExplainerPair.d_theory << std::endl;
1302
1303 // Create the workplace for explanations
1304 std::vector<NodeTheoryPair> explanationVector;
1305 explanationVector.push_back(d_propagationMap[toExplain]);
1306 // Process the explanation
1307 TrustNode texplanation = getExplanation(explanationVector);
1308 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node << ") => "
1309 << texplanation.getNode() << endl;
1310 return texplanation;
1311 }
1312
1313 struct AtomsCollect {
1314
1315 std::vector<TNode> d_atoms;
1316 std::unordered_set<TNode, TNodeHashFunction> d_visited;
1317
1318 public:
1319
1320 typedef void return_type;
1321
1322 bool alreadyVisited(TNode current, TNode parent) {
1323 // Check if already visited
1324 if (d_visited.find(current) != d_visited.end()) return true;
1325 // Don't visit non-boolean
1326 if (!current.getType().isBoolean()) return true;
1327 // New node
1328 return false;
1329 }
1330
1331 void visit(TNode current, TNode parent) {
1332 if (Theory::theoryOf(current) != theory::THEORY_BOOL) {
1333 d_atoms.push_back(current);
1334 }
1335 d_visited.insert(current);
1336 }
1337
1338 void start(TNode node) {}
1339 void done(TNode node) {}
1340
1341 std::vector<TNode> getAtoms() const {
1342 return d_atoms;
1343 }
1344 };
1345
1346 void TheoryEngine::ensureLemmaAtoms(const std::vector<TNode>& atoms, theory::TheoryId atomsTo) {
1347 for (unsigned i = 0; i < atoms.size(); ++ i) {
1348
1349 // Non-equality atoms are either owned by theory or they don't make sense
1350 if (atoms[i].getKind() != kind::EQUAL) {
1351 continue;
1352 }
1353
1354 // The equality
1355 Node eq = atoms[i];
1356 // Simple normalization to not repeat stuff
1357 if (eq[0] > eq[1]) {
1358 eq = eq[1].eqNode(eq[0]);
1359 }
1360
1361 // Rewrite the equality
1362 Node eqNormalized = Rewriter::rewrite(atoms[i]);
1363
1364 Debug("theory::atoms") << "TheoryEngine::ensureLemmaAtoms(): " << eq << " with nf " << eqNormalized << endl;
1365
1366 // If the equality is a boolean constant, we send immediately
1367 if (eqNormalized.isConst()) {
1368 if (eqNormalized.getConst<bool>()) {
1369 assertToTheory(eq, eqNormalized, /** to */ atomsTo, /** Sat solver */ theory::THEORY_SAT_SOLVER);
1370 } else {
1371 assertToTheory(eq.notNode(), eqNormalized.notNode(), /** to */ atomsTo, /** Sat solver */ theory::THEORY_SAT_SOLVER);
1372 }
1373 continue;
1374 }else if( eqNormalized.getKind() != kind::EQUAL){
1375 Assert(eqNormalized.getKind() == kind::BOOLEAN_TERM_VARIABLE
1376 || (eqNormalized.getKind() == kind::NOT
1377 && eqNormalized[0].getKind() == kind::BOOLEAN_TERM_VARIABLE));
1378 // this happens for Boolean term equalities V = true that are rewritten to V, we should skip
1379 // TODO : revisit this
1380 continue;
1381 }
1382
1383 // If the normalization did the just flips, keep the flip
1384 if (eqNormalized[0] == eq[1] && eqNormalized[1] == eq[0]) {
1385 eq = eqNormalized;
1386 }
1387
1388 // Check if the equality is already known by the sat solver
1389 if (d_propEngine->isSatLiteral(eqNormalized)) {
1390 bool value;
1391 if (d_propEngine->hasValue(eqNormalized, value)) {
1392 if (value) {
1393 assertToTheory(eq, eqNormalized, atomsTo, theory::THEORY_SAT_SOLVER);
1394 continue;
1395 } else {
1396 assertToTheory(eq.notNode(), eqNormalized.notNode(), atomsTo, theory::THEORY_SAT_SOLVER);
1397 continue;
1398 }
1399 }
1400 }
1401
1402 // If the theory is asking about a different form, or the form is ok but if will go to a different theory
1403 // then we must figure it out
1404 if (eqNormalized != eq || Theory::theoryOf(eq) != atomsTo) {
1405 // If you get eqNormalized, send atoms[i] to atomsTo
1406 d_atomRequests.add(eqNormalized, eq, atomsTo);
1407 }
1408 }
1409 }
1410
1411 theory::LemmaStatus TheoryEngine::lemma(theory::TrustNode tlemma,
1412 theory::LemmaProperty p,
1413 theory::TheoryId atomsTo,
1414 theory::TheoryId from)
1415 {
1416 // For resource-limiting (also does a time check).
1417 // spendResource();
1418 Assert(tlemma.getKind() == TrustNodeKind::LEMMA
1419 || tlemma.getKind() == TrustNodeKind::CONFLICT);
1420 // get the node
1421 Node node = tlemma.getNode();
1422 Node lemma = tlemma.getProven();
1423
1424 // when proofs are enabled, we ensure the trust node has a generator by
1425 // adding a trust step to the lazy proof maintained by this class
1426 if (isProofEnabled())
1427 {
1428 // ensure proof: set THEORY_LEMMA if no generator is provided
1429 if (tlemma.getGenerator() == nullptr)
1430 {
1431 // internal lemmas should have generators
1432 Assert(from != THEORY_LAST);
1433 // add theory lemma step to proof
1434 Node tidn = builtin::BuiltinProofRuleChecker::mkTheoryIdNode(from);
1435 d_lazyProof->addStep(lemma, PfRule::THEORY_LEMMA, {}, {lemma, tidn});
1436 // update the trust node
1437 tlemma = TrustNode::mkTrustLemma(lemma, d_lazyProof.get());
1438 }
1439 // ensure closed
1440 tlemma.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma_initial");
1441 }
1442
1443 // Do we need to check atoms
1444 if (atomsTo != theory::THEORY_LAST) {
1445 Debug("theory::atoms") << "TheoryEngine::lemma(" << node << ", " << atomsTo << ")" << endl;
1446 AtomsCollect collectAtoms;
1447 NodeVisitor<AtomsCollect>::run(collectAtoms, node);
1448 ensureLemmaAtoms(collectAtoms.getAtoms(), atomsTo);
1449 }
1450
1451 if(Dump.isOn("t-lemmas")) {
1452 // we dump the negation of the lemma, to show validity of the lemma
1453 Node n = lemma.negate();
1454 const Printer& printer = d_outMgr.getPrinter();
1455 std::ostream& out = d_outMgr.getDumpOut();
1456 printer.toStreamCmdComment(out, "theory lemma: expect valid");
1457 printer.toStreamCmdCheckSat(out, n);
1458 }
1459 bool removable = isLemmaPropertyRemovable(p);
1460 bool preprocess = isLemmaPropertyPreprocess(p);
1461
1462 // call preprocessor
1463 std::vector<TrustNode> newLemmas;
1464 std::vector<Node> newSkolems;
1465 TrustNode tplemma =
1466 d_tpp.preprocess(lemma, newLemmas, newSkolems, preprocess);
1467 // the preprocessed lemma
1468 Node lemmap;
1469 if (tplemma.isNull())
1470 {
1471 lemmap = lemma;
1472 }
1473 else
1474 {
1475 Assert(tplemma.getKind() == TrustNodeKind::REWRITE);
1476 lemmap = tplemma.getNode();
1477
1478 // must update the trust lemma
1479 if (lemmap != lemma)
1480 {
1481 // process the preprocessing
1482 if (isProofEnabled())
1483 {
1484 Assert(d_lazyProof != nullptr);
1485 // add the original proof to the lazy proof
1486 d_lazyProof->addLazyStep(tlemma.getProven(), tlemma.getGenerator());
1487 // only need to do anything if lemmap changed in a non-trivial way
1488 if (!CDProof::isSame(lemmap, lemma))
1489 {
1490 d_lazyProof->addLazyStep(tplemma.getProven(),
1491 tplemma.getGenerator(),
1492 true,
1493 "TheoryEngine::lemma_pp",
1494 false,
1495 PfRule::PREPROCESS_LEMMA);
1496 // ---------- from d_lazyProof -------------- from theory preprocess
1497 // lemma lemma = lemmap
1498 // ------------------------------------------ MACRO_SR_PRED_TRANSFORM
1499 // lemmap
1500 std::vector<Node> pfChildren;
1501 pfChildren.push_back(lemma);
1502 pfChildren.push_back(tplemma.getProven());
1503 std::vector<Node> pfArgs;
1504 pfArgs.push_back(lemmap);
1505 d_lazyProof->addStep(
1506 lemmap, PfRule::MACRO_SR_PRED_TRANSFORM, pfChildren, pfArgs);
1507 }
1508 }
1509 tlemma = TrustNode::mkTrustLemma(lemmap, d_lazyProof.get());
1510 }
1511 }
1512
1513 // must use an assertion pipeline due to decision engine below
1514 AssertionPipeline lemmas;
1515 // make the assertion pipeline
1516 lemmas.push_back(lemmap);
1517 lemmas.updateRealAssertionsEnd();
1518 Assert(newSkolems.size() == newLemmas.size());
1519 for (size_t i = 0, nsize = newLemmas.size(); i < nsize; i++)
1520 {
1521 // store skolem mapping here
1522 IteSkolemMap& imap = lemmas.getIteSkolemMap();
1523 imap[newSkolems[i]] = lemmas.size();
1524 lemmas.push_back(newLemmas[i].getNode());
1525 }
1526
1527 // If specified, we must add this lemma to the set of those that need to be
1528 // justified, where note we pass all auxiliary lemmas in lemmas, since these
1529 // by extension must be justified as well.
1530 if (d_relManager != nullptr && isLemmaPropertyNeedsJustify(p))
1531 {
1532 d_relManager->notifyPreprocessedAssertions(lemmas.ref());
1533 }
1534
1535 // do final checks on the lemmas we are about to send
1536 if (isProofEnabled())
1537 {
1538 Assert(tlemma.getGenerator() != nullptr);
1539 // ensure closed, make the proof node eagerly here to debug
1540 tlemma.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma");
1541 for (size_t i = 0, lsize = newLemmas.size(); i < lsize; ++i)
1542 {
1543 Assert(newLemmas[i].getGenerator() != nullptr);
1544 newLemmas[i].debugCheckClosed("te-proof-debug",
1545 "TheoryEngine::lemma_new");
1546 }
1547 }
1548
1549 // now, send the lemmas to the prop engine
1550 d_propEngine->assertLemma(tlemma.getProven(), false, removable);
1551 for (size_t i = 0, lsize = newLemmas.size(); i < lsize; ++i)
1552 {
1553 d_propEngine->assertLemma(newLemmas[i].getProven(), false, removable);
1554 }
1555
1556 // assert to decision engine
1557 if (!removable)
1558 {
1559 d_propEngine->addAssertionsToDecisionEngine(lemmas);
1560 }
1561
1562 // Mark that we added some lemmas
1563 d_lemmasAdded = true;
1564
1565 // Lemma analysis isn't online yet; this lemma may only live for this
1566 // user level.
1567 Node retLemma = lemmas[0];
1568 if (lemmas.size() > 1)
1569 {
1570 // the returned lemma is the conjunction of all additional lemmas.
1571 retLemma = NodeManager::currentNM()->mkNode(kind::AND, lemmas.ref());
1572 }
1573 return theory::LemmaStatus(retLemma, d_userContext->getLevel());
1574 }
1575
1576 void TheoryEngine::conflict(theory::TrustNode tconflict, TheoryId theoryId)
1577 {
1578 Assert(tconflict.getKind() == TrustNodeKind::CONFLICT);
1579 TNode conflict = tconflict.getNode();
1580 Trace("theory::conflict") << "TheoryEngine::conflict(" << conflict << ", "
1581 << theoryId << ")" << endl;
1582 Trace("te-proof-debug") << "Check closed conflict" << std::endl;
1583 // doesn't require proof generator, yet, since THEORY_LEMMA is added below
1584 tconflict.debugCheckClosed(
1585 "te-proof-debug", "TheoryEngine::conflict_initial", false);
1586
1587 Trace("dtview::conflict") << ":THEORY-CONFLICT: " << conflict << std::endl;
1588
1589 // Mark that we are in conflict
1590 d_inConflict = true;
1591
1592 if(Dump.isOn("t-conflicts")) {
1593 const Printer& printer = d_outMgr.getPrinter();
1594 std::ostream& out = d_outMgr.getDumpOut();
1595 printer.toStreamCmdComment(out, "theory conflict: expect unsat");
1596 printer.toStreamCmdCheckSat(out, conflict);
1597 }
1598
1599 // In the multiple-theories case, we need to reconstruct the conflict
1600 if (d_logicInfo.isSharingEnabled()) {
1601 // Create the workplace for explanations
1602 std::vector<NodeTheoryPair> vec;
1603 vec.push_back(
1604 NodeTheoryPair(conflict, theoryId, d_propagationMapTimestamp));
1605
1606 // Process the explanation
1607 TrustNode tncExp = getExplanation(vec);
1608 Trace("te-proof-debug")
1609 << "Check closed conflict explained with sharing" << std::endl;
1610 tncExp.debugCheckClosed("te-proof-debug",
1611 "TheoryEngine::conflict_explained_sharing");
1612 Node fullConflict = tncExp.getNode();
1613
1614 if (isProofEnabled())
1615 {
1616 Trace("te-proof-debug") << "Process conflict: " << conflict << std::endl;
1617 Trace("te-proof-debug") << "Conflict " << tconflict << " from "
1618 << tconflict.identifyGenerator() << std::endl;
1619 Trace("te-proof-debug") << "Explanation " << tncExp << " from "
1620 << tncExp.identifyGenerator() << std::endl;
1621 Assert(d_lazyProof != nullptr);
1622 if (tconflict.getGenerator() != nullptr)
1623 {
1624 d_lazyProof->addLazyStep(tconflict.getProven(),
1625 tconflict.getGenerator());
1626 }
1627 else
1628 {
1629 // add theory lemma step
1630 Node tidn = builtin::BuiltinProofRuleChecker::mkTheoryIdNode(theoryId);
1631 Node conf = tconflict.getProven();
1632 d_lazyProof->addStep(conf, PfRule::THEORY_LEMMA, {}, {conf, tidn});
1633 }
1634 // store the explicit step, which should come from a different
1635 // generator, e.g. d_tepg.
1636 Node proven = tncExp.getProven();
1637 Assert(tncExp.getGenerator() != d_lazyProof.get());
1638 Trace("te-proof-debug") << "add lazy step " << tncExp.identifyGenerator()
1639 << " for " << proven << std::endl;
1640 d_lazyProof->addLazyStep(proven, tncExp.getGenerator());
1641 pfgEnsureClosed(proven,
1642 d_lazyProof.get(),
1643 "te-proof-debug",
1644 "TheoryEngine::conflict_during");
1645 Node fullConflictNeg = fullConflict.notNode();
1646 std::vector<Node> children;
1647 children.push_back(proven);
1648 std::vector<Node> args;
1649 args.push_back(fullConflictNeg);
1650 if (conflict == d_false)
1651 {
1652 AlwaysAssert(proven == fullConflictNeg);
1653 }
1654 else
1655 {
1656 if (fullConflict != conflict)
1657 {
1658 // ------------------------- explained ---------- from theory
1659 // fullConflict => conflict ~conflict
1660 // --------------------------------------------
1661 // MACRO_SR_PRED_TRANSFORM ~fullConflict
1662 children.push_back(conflict.notNode());
1663 args.push_back(mkMethodId(MethodId::SB_LITERAL));
1664 d_lazyProof->addStep(
1665 fullConflictNeg, PfRule::MACRO_SR_PRED_TRANSFORM, children, args);
1666 }
1667 }
1668 }
1669 // pass the processed trust node
1670 TrustNode tconf =
1671 TrustNode::mkTrustConflict(fullConflict, d_lazyProof.get());
1672 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict << ", " << theoryId << "): full = " << fullConflict << endl;
1673 Assert(properConflict(fullConflict));
1674 Trace("te-proof-debug")
1675 << "Check closed conflict with sharing" << std::endl;
1676 tconf.debugCheckClosed("te-proof-debug", "TheoryEngine::conflict:sharing");
1677 lemma(tconf, LemmaProperty::REMOVABLE);
1678 } else {
1679 // When only one theory, the conflict should need no processing
1680 Assert(properConflict(conflict));
1681 // pass the trust node that was sent from the theory
1682 lemma(tconflict, LemmaProperty::REMOVABLE, THEORY_LAST, theoryId);
1683 }
1684 }
1685
1686 theory::TrustNode TheoryEngine::getExplanation(
1687 std::vector<NodeTheoryPair>& explanationVector)
1688 {
1689 Assert(explanationVector.size() > 0);
1690
1691 unsigned i = 0; // Index of the current literal we are processing
1692 unsigned j = 0; // Index of the last literal we are keeping
1693
1694 // cache of nodes we have already explained by some theory
1695 std::unordered_map<Node, size_t, NodeHashFunction> cache;
1696
1697 while (i < explanationVector.size()) {
1698 // Get the current literal to explain
1699 NodeTheoryPair toExplain = explanationVector[i];
1700
1701 Debug("theory::explain")
1702 << "[i=" << i << "] TheoryEngine::explain(): processing ["
1703 << toExplain.d_timestamp << "] " << toExplain.d_node << " sent from "
1704 << toExplain.d_theory << endl;
1705
1706 if (cache.find(toExplain.d_node) != cache.end()
1707 && cache[toExplain.d_node] < toExplain.d_timestamp)
1708 {
1709 ++i;
1710 continue;
1711 }
1712 cache[toExplain.d_node] = toExplain.d_timestamp;
1713
1714 // If a true constant or a negation of a false constant we can ignore it
1715 if (toExplain.d_node.isConst() && toExplain.d_node.getConst<bool>())
1716 {
1717 ++ i;
1718 continue;
1719 }
1720 if (toExplain.d_node.getKind() == kind::NOT && toExplain.d_node[0].isConst()
1721 && !toExplain.d_node[0].getConst<bool>())
1722 {
1723 ++ i;
1724 continue;
1725 }
1726
1727 // If from the SAT solver, keep it
1728 if (toExplain.d_theory == THEORY_SAT_SOLVER)
1729 {
1730 Debug("theory::explain") << "\tLiteral came from THEORY_SAT_SOLVER. Kepping it." << endl;
1731 explanationVector[j++] = explanationVector[i++];
1732 continue;
1733 }
1734
1735 // If an and, expand it
1736 if (toExplain.d_node.getKind() == kind::AND)
1737 {
1738 Debug("theory::explain")
1739 << "TheoryEngine::explain(): expanding " << toExplain.d_node
1740 << " got from " << toExplain.d_theory << endl;
1741 for (unsigned k = 0; k < toExplain.d_node.getNumChildren(); ++k)
1742 {
1743 NodeTheoryPair newExplain(
1744 toExplain.d_node[k], toExplain.d_theory, toExplain.d_timestamp);
1745 explanationVector.push_back(newExplain);
1746 }
1747 ++ i;
1748 continue;
1749 }
1750
1751 // See if it was sent to the theory by another theory
1752 PropagationMap::const_iterator find = d_propagationMap.find(toExplain);
1753 if (find != d_propagationMap.end()) {
1754 Debug("theory::explain")
1755 << "\tTerm was propagated by another theory (theory = "
1756 << getTheoryString((*find).second.d_theory) << ")" << std::endl;
1757 // There is some propagation, check if its a timely one
1758 if ((*find).second.d_timestamp < toExplain.d_timestamp)
1759 {
1760 Debug("theory::explain")
1761 << "\tRelevant timetsamp, pushing " << (*find).second.d_node
1762 << "to index = " << explanationVector.size() << std::endl;
1763 explanationVector.push_back((*find).second);
1764 ++i;
1765
1766 continue;
1767 }
1768 }
1769
1770 Node explanation;
1771 if (toExplain.d_theory == THEORY_BUILTIN)
1772 {
1773 explanation = d_sharedTerms.explain(toExplain.d_node);
1774 Debug("theory::explain") << "\tTerm was propagated by THEORY_BUILTIN. Explanation: " << explanation << std::endl;
1775 }
1776 else
1777 {
1778 TrustNode texp = theoryOf(toExplain.d_theory)->explain(toExplain.d_node);
1779 explanation = texp.getNode();
1780 Debug("theory::explain") << "\tTerm was propagated by owner theory: "
1781 << theoryOf(toExplain.d_theory)->getId()
1782 << ". Explanation: " << explanation << std::endl;
1783 }
1784
1785 Debug("theory::explain")
1786 << "TheoryEngine::explain(): got explanation " << explanation
1787 << " got from " << toExplain.d_theory << endl;
1788 Assert(explanation != toExplain.d_node)
1789 << "wasn't sent to you, so why are you explaining it trivially";
1790 // Mark the explanation
1791 NodeTheoryPair newExplain(
1792 explanation, toExplain.d_theory, toExplain.d_timestamp);
1793 explanationVector.push_back(newExplain);
1794
1795 ++ i;
1796 }
1797
1798 // Keep only the relevant literals
1799 explanationVector.resize(j);
1800
1801 Node expNode = mkExplanation(explanationVector);
1802 return theory::TrustNode::mkTrustLemma(expNode, nullptr);
1803 }
1804
1805 bool TheoryEngine::isProofEnabled() const { return d_pnm != nullptr; }
1806
1807 void TheoryEngine::setUserAttribute(const std::string& attr,
1808 Node n,
1809 const std::vector<Node>& node_values,
1810 const std::string& str_value)
1811 {
1812 Trace("te-attr") << "set user attribute " << attr << " " << n << endl;
1813 if( d_attr_handle.find( attr )!=d_attr_handle.end() ){
1814 for( size_t i=0; i<d_attr_handle[attr].size(); i++ ){
1815 d_attr_handle[attr][i]->setUserAttribute(attr, n, node_values, str_value);
1816 }
1817 } else {
1818 //unhandled exception?
1819 }
1820 }
1821
1822 void TheoryEngine::handleUserAttribute(const char* attr, Theory* t) {
1823 Trace("te-attr") << "Handle user attribute " << attr << " " << t << endl;
1824 std::string str( attr );
1825 d_attr_handle[ str ].push_back( t );
1826 }
1827
1828 void TheoryEngine::checkTheoryAssertionsWithModel(bool hardFailure) {
1829 for(TheoryId theoryId = THEORY_FIRST; theoryId < THEORY_LAST; ++theoryId) {
1830 Theory* theory = d_theoryTable[theoryId];
1831 if(theory && d_logicInfo.isTheoryEnabled(theoryId)) {
1832 for(context::CDList<Assertion>::const_iterator it = theory->facts_begin(),
1833 it_end = theory->facts_end();
1834 it != it_end;
1835 ++it) {
1836 Node assertion = (*it).d_assertion;
1837 if (!isRelevant(assertion))
1838 {
1839 // not relevant, skip
1840 continue;
1841 }
1842 Node val = d_tc->getModel()->getValue(assertion);
1843 if (val != d_true)
1844 {
1845 std::stringstream ss;
1846 ss << " " << theoryId
1847 << " has an asserted fact that the model doesn't satisfy." << endl
1848 << "The fact: " << assertion << endl
1849 << "Model value: " << val << endl;
1850 if (hardFailure)
1851 {
1852 if (val == d_false)
1853 {
1854 // Always an error if it is false
1855 InternalError() << ss.str();
1856 }
1857 else
1858 {
1859 // Otherwise just a warning. Notice this case may happen for
1860 // assertions with unevaluable operators, e.g. transcendental
1861 // functions. It also may happen for separation logic, where
1862 // check-model support is limited.
1863 Warning() << ss.str();
1864 }
1865 }
1866 }
1867 }
1868 }
1869 }
1870 }
1871
1872 std::pair<bool, Node> TheoryEngine::entailmentCheck(options::TheoryOfMode mode,
1873 TNode lit)
1874 {
1875 TNode atom = (lit.getKind() == kind::NOT) ? lit[0] : lit;
1876 if( atom.getKind()==kind::AND || atom.getKind()==kind::OR || atom.getKind()==kind::IMPLIES ){
1877 //Boolean connective, recurse
1878 std::vector< Node > children;
1879 bool pol = (lit.getKind()!=kind::NOT);
1880 bool is_conjunction = pol==(lit.getKind()==kind::AND);
1881 for( unsigned i=0; i<atom.getNumChildren(); i++ ){
1882 Node ch = atom[i];
1883 if( pol==( lit.getKind()==kind::IMPLIES && i==0 ) ){
1884 ch = atom[i].negate();
1885 }
1886 std::pair<bool, Node> chres = entailmentCheck(mode, ch);
1887 if( chres.first ){
1888 if( !is_conjunction ){
1889 return chres;
1890 }else{
1891 children.push_back( chres.second );
1892 }
1893 }else if( !chres.first && is_conjunction ){
1894 return std::pair<bool, Node>(false, Node::null());
1895 }
1896 }
1897 if( is_conjunction ){
1898 return std::pair<bool, Node>(true, NodeManager::currentNM()->mkNode(kind::AND, children));
1899 }else{
1900 return std::pair<bool, Node>(false, Node::null());
1901 }
1902 }else if( atom.getKind()==kind::ITE || ( atom.getKind()==kind::EQUAL && atom[0].getType().isBoolean() ) ){
1903 bool pol = (lit.getKind()!=kind::NOT);
1904 for( unsigned r=0; r<2; r++ ){
1905 Node ch = atom[0];
1906 if( r==1 ){
1907 ch = ch.negate();
1908 }
1909 std::pair<bool, Node> chres = entailmentCheck(mode, ch);
1910 if( chres.first ){
1911 Node ch2 = atom[ atom.getKind()==kind::ITE ? r+1 : 1 ];
1912 if( pol==( atom.getKind()==kind::ITE ? true : r==1 ) ){
1913 ch2 = ch2.negate();
1914 }
1915 std::pair<bool, Node> chres2 = entailmentCheck(mode, ch2);
1916 if( chres2.first ){
1917 return std::pair<bool, Node>(true, NodeManager::currentNM()->mkNode(kind::AND, chres.second, chres2.second));
1918 }else{
1919 break;
1920 }
1921 }
1922 }
1923 return std::pair<bool, Node>(false, Node::null());
1924 }else{
1925 //it is a theory atom
1926 theory::TheoryId tid = theory::Theory::theoryOf(mode, atom);
1927 theory::Theory* th = theoryOf(tid);
1928
1929 Assert(th != NULL);
1930 Trace("theory-engine-entc") << "Entailment check : " << lit << std::endl;
1931
1932 std::pair<bool, Node> chres = th->entailmentCheck(lit);
1933 return chres;
1934 }
1935 }
1936
1937 void TheoryEngine::spendResource(ResourceManager::Resource r)
1938 {
1939 d_resourceManager->spendResource(r);
1940 }
1941
1942 }/* CVC4 namespace */