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