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