1 /********************* */
2 /*! \file theory_engine.cpp
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
12 ** \brief The theory engine
17 #include "theory/theory_engine.h"
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"
60 using namespace CVC4::preprocessing
;
61 using namespace CVC4::theory
;
65 /* -------------------------------------------------------------------------- */
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.
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)
91 /* -------------------------------------------------------------------------- */
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
){
97 if(curr
.getKind() == kind::AND
){
98 flattenAnd(curr
, out
);
105 inline Node
flattenAnd(Node n
){
106 std::vector
<TNode
> out
;
108 return NodeManager::currentNM()->mkNode(kind::AND
, out
);
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.
116 * @param id The theory id
117 * @return The string corresponding to the theory id
119 std::string
getTheoryString(theory::TheoryId id
)
121 if (id
== theory::THEORY_SAT_SOLVER
)
123 return "THEORY_SAT_SOLVER";
127 std::stringstream ss
;
133 void TheoryEngine::finishInit() {
134 // initialize the quantifiers engine
135 if (d_logicInfo
.isQuantified())
137 // initialize the quantifiers engine
138 d_quantEngine
= new QuantifiersEngine(d_context
, d_userContext
, this);
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();
146 // Initialize the model and model builder.
147 if (d_logicInfo
.isQuantified())
149 d_curr_model_builder
= d_quantEngine
->getModelBuilder();
150 d_curr_model
= d_quantEngine
->getModel();
152 d_curr_model
= new theory::TheoryModel(
153 d_userContext
, "DefaultModel", options::assignFunctionValues());
154 d_aloc_curr_model
= true;
156 // create the relevance filter if any option requires it
157 if (options::relevanceFilter())
160 new RelevanceManager(d_userContext
, theory::Valuation(this)));
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;
169 // Initialize the model
170 d_eeDistributed
->initializeModel(d_curr_model
);
172 // set the core equality engine on quantifiers engine
173 if (d_logicInfo
.isQuantified())
175 d_quantEngine
->setMasterEqualityEngine(
176 d_eeDistributed
->getCoreEqualityEngine());
179 // finish initializing the theories
180 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
181 Theory
* t
= d_theoryTable
[theoryId
];
186 // setup the pointers to the utilities
187 const EeTheoryInfo
* eeti
= d_eeDistributed
->getEeTheoryInfo(theoryId
);
188 Assert(eeti
!= nullptr);
189 // the theory's official equality engine is the one specified by the
190 // equality engine manager
191 t
->setEqualityEngine(eeti
->d_usedEe
);
192 // set the quantifiers engine
193 t
->setQuantifiersEngine(d_quantEngine
);
194 // set the decision manager for the theory
195 t
->setDecisionManager(d_decManager
.get());
196 // finish initializing the theory
201 TheoryEngine::TheoryEngine(context::Context
* context
,
202 context::UserContext
* userContext
,
204 RemoveTermFormulas
& iteRemover
,
205 const LogicInfo
& logicInfo
)
206 : d_propEngine(nullptr),
208 d_userContext(userContext
),
209 d_logicInfo(logicInfo
),
210 d_sharedTerms(this, context
),
211 d_eeDistributed(nullptr),
212 d_quantEngine(nullptr),
213 d_decManager(new DecisionManager(userContext
)),
214 d_relManager(nullptr),
215 d_curr_model(nullptr),
216 d_aloc_curr_model(false),
217 d_curr_model_builder(nullptr),
218 d_aloc_curr_model_builder(false),
219 d_eager_model_building(false),
220 d_possiblePropagations(context
),
221 d_hasPropagated(context
),
222 d_inConflict(context
, false),
224 d_hasShutDown(false),
225 d_incomplete(context
, false),
226 d_propagationMap(context
),
227 d_propagationMapTimestamp(context
, 0),
228 d_propagatedLiterals(context
),
229 d_propagatedLiteralsIndex(context
, 0),
230 d_atomRequests(context
),
231 d_tpp(*this, iteRemover
),
232 d_combineTheoriesTime("TheoryEngine::combineTheoriesTime"),
235 d_interrupted(false),
236 d_resourceManager(rm
),
237 d_inPreregister(false),
238 d_factsAsserted(context
, false),
239 d_preRegistrationVisitor(this, context
),
240 d_sharedTermsVisitor(d_sharedTerms
),
242 d_arithSubstitutionsAdded("theory::arith::zzz::arith::substitutions", 0)
244 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
;
247 d_theoryTable
[theoryId
] = NULL
;
248 d_theoryOut
[theoryId
] = NULL
;
251 smtStatisticsRegistry()->registerStat(&d_combineTheoriesTime
);
252 d_true
= NodeManager::currentNM()->mkConst
<bool>(true);
253 d_false
= NodeManager::currentNM()->mkConst
<bool>(false);
256 ProofManager::currentPM()->initTheoryProofEngine();
259 smtStatisticsRegistry()->registerStat(&d_arithSubstitutionsAdded
);
262 TheoryEngine::~TheoryEngine() {
263 Assert(d_hasShutDown
);
265 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
266 if(d_theoryTable
[theoryId
] != NULL
) {
267 delete d_theoryTable
[theoryId
];
268 delete d_theoryOut
[theoryId
];
272 if( d_aloc_curr_model_builder
){
273 delete d_curr_model_builder
;
275 if( d_aloc_curr_model
){
279 delete d_quantEngine
;
281 smtStatisticsRegistry()->unregisterStat(&d_combineTheoriesTime
);
282 smtStatisticsRegistry()->unregisterStat(&d_arithSubstitutionsAdded
);
285 void TheoryEngine::interrupt() { d_interrupted
= true; }
286 void TheoryEngine::preRegister(TNode preprocessed
) {
288 Debug("theory") << "TheoryEngine::preRegister( " << preprocessed
<< ")" << std::endl
;
289 if(Dump
.isOn("missed-t-propagations")) {
290 d_possiblePropagations
.push_back(preprocessed
);
292 d_preregisterQueue
.push(preprocessed
);
294 if (!d_inPreregister
) {
295 // We're in pre-register
296 d_inPreregister
= true;
298 // Process the pre-registration queue
299 while (!d_preregisterQueue
.empty()) {
300 // Get the next atom to pre-register
301 preprocessed
= d_preregisterQueue
.front();
302 d_preregisterQueue
.pop();
304 if (d_logicInfo
.isSharingEnabled() && preprocessed
.getKind() == kind::EQUAL
) {
305 // When sharing is enabled, we propagate from the shared terms manager also
306 d_sharedTerms
.addEqualityToPropagate(preprocessed
);
309 // the atom should not have free variables
310 Debug("theory") << "TheoryEngine::preRegister: " << preprocessed
312 Assert(!expr::hasFreeVar(preprocessed
));
313 // Pre-register the terms in the atom
314 Theory::Set theories
= NodeVisitor
<PreRegisterVisitor
>::run(d_preRegistrationVisitor
, preprocessed
);
315 theories
= Theory::setRemove(THEORY_BOOL
, theories
);
316 // Remove the top theory, if any more that means multiple theories were involved
317 bool multipleTheories
= Theory::setRemove(Theory::theoryOf(preprocessed
), theories
);
319 // These checks don't work with finite model finding, because it
320 // uses Rational constants to represent cardinality constraints,
321 // even though arithmetic isn't actually involved.
322 if(!options::finiteModelFind()) {
323 while((i
= Theory::setPop(theories
)) != THEORY_LAST
) {
324 if(!d_logicInfo
.isTheoryEnabled(i
)) {
325 LogicInfo newLogicInfo
= d_logicInfo
.getUnlockedCopy();
326 newLogicInfo
.enableTheory(i
);
329 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
330 << ", which doesn't include " << i
331 << ", but found a term in that theory." << endl
332 << "You might want to extend your logic to "
333 << newLogicInfo
.getLogicString() << endl
;
334 throw LogicException(ss
.str());
338 if (multipleTheories
) {
339 // Collect the shared terms if there are multiple theories
340 NodeVisitor
<SharedTermsVisitor
>::run(d_sharedTermsVisitor
, preprocessed
);
344 // Leaving pre-register
345 d_inPreregister
= false;
349 void TheoryEngine::printAssertions(const char* tag
) {
350 if (Trace
.isOn(tag
)) {
352 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
353 Theory
* theory
= d_theoryTable
[theoryId
];
354 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
355 Trace(tag
) << "--------------------------------------------" << endl
;
356 Trace(tag
) << "Assertions of " << theory
->getId() << ": " << endl
;
358 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
361 for (unsigned i
= 0; it
!= it_end
; ++it
, ++i
)
363 if ((*it
).d_isPreregistered
)
365 Trace(tag
) << "[" << i
<< "]: ";
369 Trace(tag
) << "(" << i
<< "): ";
371 Trace(tag
) << (*it
).d_assertion
<< endl
;
375 if (d_logicInfo
.isSharingEnabled()) {
376 Trace(tag
) << "Shared terms of " << theory
->getId() << ": " << endl
;
377 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
378 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
379 Trace(tag
) << "[" << i
<< "]: " << (*it
) << endl
;
387 void TheoryEngine::dumpAssertions(const char* tag
) {
388 if (Dump
.isOn(tag
)) {
389 Dump(tag
) << CommentCommand("Starting completeness check");
390 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
391 Theory
* theory
= d_theoryTable
[theoryId
];
392 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
393 Dump(tag
) << CommentCommand("Completeness check");
394 Dump(tag
) << PushCommand();
396 // Dump the shared terms
397 if (d_logicInfo
.isSharingEnabled()) {
398 Dump(tag
) << CommentCommand("Shared terms");
399 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
400 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
403 Dump(tag
) << CommentCommand(ss
.str());
407 // Dump the assertions
408 Dump(tag
) << CommentCommand("Assertions");
409 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(), it_end
= theory
->facts_end();
410 for (; it
!= it_end
; ++ it
) {
412 Node assertionNode
= (*it
).d_assertion
;
413 // Purify all the terms
415 if ((*it
).d_isPreregistered
)
417 Dump(tag
) << CommentCommand("Preregistered");
421 Dump(tag
) << CommentCommand("Shared assertion");
423 Dump(tag
) << AssertCommand(assertionNode
.toExpr());
425 Dump(tag
) << CheckSatCommand();
427 Dump(tag
) << PopCommand();
434 * Check all (currently-active) theories for conflicts.
435 * @param effort the effort level to use
437 void TheoryEngine::check(Theory::Effort effort
) {
440 // Reset the interrupt flag
441 d_interrupted
= false;
443 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
444 #undef CVC4_FOR_EACH_THEORY_STATEMENT
446 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
447 if (theory::TheoryTraits<THEORY>::hasCheck && d_logicInfo.isTheoryEnabled(THEORY)) { \
448 theoryOf(THEORY)->check(effort); \
449 if (d_inConflict) { \
450 Debug("conflict") << THEORY << " in conflict. " << std::endl; \
458 // Mark the output channel unused (if this is FULL_EFFORT, and nothing
459 // is done by the theories, no additional check will be needed)
460 d_outputChannelUsed
= false;
462 // Mark the lemmas flag (no lemmas added)
463 d_lemmasAdded
= false;
465 Debug("theory") << "TheoryEngine::check(" << effort
<< "): d_factsAsserted = " << (d_factsAsserted
? "true" : "false") << endl
;
467 // If in full effort, we have a fake new assertion just to jumpstart the checking
468 if (Theory::fullEffort(effort
)) {
469 d_factsAsserted
= true;
470 // Reset round for the relevance manager, which notice only sets a flag
471 // to indicate that its information must be recomputed.
472 if (d_relManager
!= nullptr)
474 d_relManager
->resetRound();
479 while (d_factsAsserted
&& !d_inConflict
&& !d_lemmasAdded
) {
481 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running check" << endl
;
483 Trace("theory::assertions") << endl
;
484 if (Trace
.isOn("theory::assertions")) {
485 printAssertions("theory::assertions");
488 if(Theory::fullEffort(effort
)) {
489 Trace("theory::assertions::fulleffort") << endl
;
490 if (Trace
.isOn("theory::assertions::fulleffort")) {
491 printAssertions("theory::assertions::fulleffort");
495 // Note that we've discharged all the facts
496 d_factsAsserted
= false;
499 CVC4_FOR_EACH_THEORY
;
501 if(Dump
.isOn("missed-t-conflicts")) {
502 Dump("missed-t-conflicts")
503 << CommentCommand("Completeness check for T-conflicts; expect sat")
504 << CheckSatCommand();
507 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running propagation after the initial check" << endl
;
509 // We are still satisfiable, propagate as much as possible
512 // We do combination if all has been processed and we are in fullcheck
513 if (Theory::fullEffort(effort
) && d_logicInfo
.isSharingEnabled() && !d_factsAsserted
&& !d_lemmasAdded
&& !d_inConflict
) {
514 // Do the combination
515 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running combination" << endl
;
517 if(d_logicInfo
.isQuantified()){
518 d_quantEngine
->notifyCombineTheories();
523 // Must consult quantifiers theory for last call to ensure sat, or otherwise add a lemma
524 if( Theory::fullEffort(effort
) && ! d_inConflict
&& ! needCheck() ) {
525 Trace("theory::assertions-model") << endl
;
526 if (Trace
.isOn("theory::assertions-model")) {
527 printAssertions("theory::assertions-model");
529 //checks for theories requiring the model go at last call
530 d_curr_model
->reset();
531 // !!! temporary, will be part of distributed model manager
532 context::Context
* meec
= d_eeDistributed
->getModelEqualityEngineContext();
535 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
536 if( theoryId
!=THEORY_QUANTIFIERS
){
537 Theory
* theory
= d_theoryTable
[theoryId
];
538 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
539 if( theory
->needsCheckLastEffort() ){
540 if( !d_curr_model
->isBuilt() ){
541 if( !d_curr_model_builder
->buildModel(d_curr_model
) ){
545 theory
->check(Theory::EFFORT_LAST_CALL
);
552 if(d_logicInfo
.isQuantified()) {
553 // quantifiers engine must check at last call effort
554 d_quantEngine
->check(Theory::EFFORT_LAST_CALL
);
557 if (!d_inConflict
&& !needCheck())
559 // If d_eager_model_building is false, then we only mark that we
560 // are in "SAT mode". We build the model later only if the user asks
561 // for it via getBuiltModel.
563 if (d_eager_model_building
&& !d_curr_model
->isBuilt())
565 d_curr_model_builder
->buildModel(d_curr_model
);
570 Debug("theory") << "TheoryEngine::check(" << effort
<< "): done, we are " << (d_inConflict
? "unsat" : "sat") << (d_lemmasAdded
? " with new lemmas" : " with no new lemmas");
571 Debug("theory") << ", need check = " << (needCheck() ? "YES" : "NO") << endl
;
573 if( Theory::fullEffort(effort
) && !d_inConflict
&& !needCheck()) {
574 // case where we are about to answer SAT, the master equality engine,
575 // if it exists, must be consistent.
576 eq::EqualityEngine
* mee
= d_eeDistributed
->getCoreEqualityEngine();
579 AlwaysAssert(mee
->consistent());
581 if (d_curr_model
->isBuilt())
583 // model construction should always succeed unless lemmas were added
584 AlwaysAssert(d_curr_model
->isBuiltSuccess());
585 if (options::produceModels())
587 // Do post-processing of model from the theories (used for THEORY_SEP
588 // to construct heap model)
589 postProcessModel(d_curr_model
);
590 // also call the model builder's post-process model
591 d_curr_model_builder
->postProcessModel(d_incomplete
.get(),
596 } catch(const theory::Interrupted
&) {
597 Trace("theory") << "TheoryEngine::check() => interrupted" << endl
;
599 // If fulleffort, check all theories
600 if(Dump
.isOn("theory::fullcheck") && Theory::fullEffort(effort
)) {
601 if (!d_inConflict
&& !needCheck()) {
602 dumpAssertions("theory::fullcheck");
607 void TheoryEngine::combineTheories() {
609 Trace("combineTheories") << "TheoryEngine::combineTheories()" << endl
;
611 TimerStat::CodeTimer
combineTheoriesTimer(d_combineTheoriesTime
);
613 // Care graph we'll be building
616 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
617 #undef CVC4_FOR_EACH_THEORY_STATEMENT
619 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
620 if (theory::TheoryTraits<THEORY>::isParametric && d_logicInfo.isTheoryEnabled(THEORY)) { \
621 theoryOf(THEORY)->getCareGraph(&careGraph); \
624 // Call on each parametric theory to give us its care graph
625 CVC4_FOR_EACH_THEORY
;
627 Trace("combineTheories") << "TheoryEngine::combineTheories(): care graph size = " << careGraph
.size() << endl
;
629 // Now add splitters for the ones we are interested in
630 CareGraph::const_iterator care_it
= careGraph
.begin();
631 CareGraph::const_iterator care_it_end
= careGraph
.end();
633 for (; care_it
!= care_it_end
; ++ care_it
) {
634 const CarePair
& carePair
= *care_it
;
636 Debug("combineTheories")
637 << "TheoryEngine::combineTheories(): checking " << carePair
.d_a
<< " = "
638 << carePair
.d_b
<< " from " << carePair
.d_theory
<< endl
;
640 Assert(d_sharedTerms
.isShared(carePair
.d_a
) || carePair
.d_a
.isConst());
641 Assert(d_sharedTerms
.isShared(carePair
.d_b
) || carePair
.d_b
.isConst());
643 // The equality in question (order for no repetition)
644 Node equality
= carePair
.d_a
.eqNode(carePair
.d_b
);
645 // EqualityStatus es = getEqualityStatus(carePair.d_a, carePair.d_b);
646 // Debug("combineTheories") << "TheoryEngine::combineTheories(): " <<
647 // (es == EQUALITY_TRUE_AND_PROPAGATED ? "EQUALITY_TRUE_AND_PROPAGATED" :
648 // es == EQUALITY_FALSE_AND_PROPAGATED ? "EQUALITY_FALSE_AND_PROPAGATED" :
649 // es == EQUALITY_TRUE ? "EQUALITY_TRUE" :
650 // es == EQUALITY_FALSE ? "EQUALITY_FALSE" :
651 // es == EQUALITY_TRUE_IN_MODEL ? "EQUALITY_TRUE_IN_MODEL" :
652 // es == EQUALITY_FALSE_IN_MODEL ? "EQUALITY_FALSE_IN_MODEL" :
653 // es == EQUALITY_UNKNOWN ? "EQUALITY_UNKNOWN" :
654 // "Unexpected case") << endl;
656 // We need to split on it
657 Debug("combineTheories") << "TheoryEngine::combineTheories(): requesting a split " << endl
;
659 lemma(equality
.orNode(equality
.notNode()),
665 // This code is supposed to force preference to follow what the theory models already have
666 // but it doesn't seem to make a big difference - need to explore more -Clark
668 // if (es == EQUALITY_TRUE || es == EQUALITY_TRUE_IN_MODEL) {
669 Node e
= ensureLiteral(equality
);
670 d_propEngine
->requirePhase(e
, true);
672 // else if (es == EQUALITY_FALSE_IN_MODEL) {
673 // Node e = ensureLiteral(equality);
674 // d_propEngine->requirePhase(e, false);
680 void TheoryEngine::propagate(Theory::Effort effort
) {
681 // Reset the interrupt flag
682 d_interrupted
= false;
684 // Definition of the statement that is to be run by every theory
685 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
686 #undef CVC4_FOR_EACH_THEORY_STATEMENT
688 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
689 if (theory::TheoryTraits<THEORY>::hasPropagate && d_logicInfo.isTheoryEnabled(THEORY)) { \
690 theoryOf(THEORY)->propagate(effort); \
693 // Reset the interrupt flag
694 d_interrupted
= false;
696 // Propagate for each theory using the statement above
697 CVC4_FOR_EACH_THEORY
;
699 if(Dump
.isOn("missed-t-propagations")) {
700 for(unsigned i
= 0; i
< d_possiblePropagations
.size(); ++i
) {
701 Node atom
= d_possiblePropagations
[i
];
703 if(d_propEngine
->hasValue(atom
, value
)) {
706 // Doesn't have a value, check it (and the negation)
707 if(d_hasPropagated
.find(atom
) == d_hasPropagated
.end()) {
708 Dump("missed-t-propagations")
709 << CommentCommand("Completeness check for T-propagations; expect invalid")
710 << EchoCommand(atom
.toString())
711 << QueryCommand(atom
.toExpr())
712 << EchoCommand(atom
.notNode().toString())
713 << QueryCommand(atom
.notNode().toExpr());
719 Node
TheoryEngine::getNextDecisionRequest()
721 return d_decManager
->getNextDecisionRequest();
724 bool TheoryEngine::properConflict(TNode conflict
) const {
726 if (conflict
.getKind() == kind::AND
) {
727 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++ i
) {
728 if (! getPropEngine()->hasValue(conflict
[i
], value
)) {
729 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
730 << conflict
[i
] << endl
;
734 Debug("properConflict") << "Bad conflict is due to false atom: "
735 << conflict
[i
] << endl
;
738 if (conflict
[i
] != Rewriter::rewrite(conflict
[i
])) {
739 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
740 << conflict
[i
] << " vs " << Rewriter::rewrite(conflict
[i
]) << endl
;
745 if (! getPropEngine()->hasValue(conflict
, value
)) {
746 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
751 Debug("properConflict") << "Bad conflict is due to false atom: "
755 if (conflict
!= Rewriter::rewrite(conflict
)) {
756 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
757 << conflict
<< " vs " << Rewriter::rewrite(conflict
) << endl
;
764 bool TheoryEngine::properPropagation(TNode lit
) const {
765 if(!getPropEngine()->isSatLiteral(lit
)) {
769 return !getPropEngine()->hasValue(lit
, b
);
772 bool TheoryEngine::properExplanation(TNode node
, TNode expl
) const {
773 // Explanation must be either a conjunction of true literals that have true SAT values already
774 // or a singled literal that has a true SAT value already.
775 if (expl
.getKind() == kind::AND
) {
776 for (unsigned i
= 0; i
< expl
.getNumChildren(); ++ i
) {
778 if (!d_propEngine
->hasValue(expl
[i
], value
) || !value
) {
784 return d_propEngine
->hasValue(expl
, value
) && value
;
789 bool TheoryEngine::collectModelInfo(theory::TheoryModel
* m
)
791 //have shared term engine collectModelInfo
792 // d_sharedTerms.collectModelInfo( m );
793 // Consult each active theory to get all relevant information
794 // concerning the model.
795 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
796 if(d_logicInfo
.isTheoryEnabled(theoryId
)) {
797 Trace("model-builder") << " CollectModelInfo on theory: " << theoryId
<< endl
;
798 if (!d_theoryTable
[theoryId
]->collectModelInfo(m
))
804 Trace("model-builder") << " CollectModelInfo boolean variables" << std::endl
;
805 // Get the Boolean variables
806 vector
<TNode
> boolVars
;
807 d_propEngine
->getBooleanVariables(boolVars
);
808 vector
<TNode
>::iterator it
, iend
= boolVars
.end();
809 bool hasValue
, value
;
810 for (it
= boolVars
.begin(); it
!= iend
; ++it
) {
812 hasValue
= d_propEngine
->hasValue(var
, value
);
813 // TODO: Assert that hasValue is true?
815 Trace("model-builder-assertions")
816 << " has no value : " << var
<< std::endl
;
819 Trace("model-builder-assertions") << "(assert" << (value
? " " : " (not ") << var
<< (value
? ");" : "));") << endl
;
820 if (!m
->assertPredicate(var
, value
))
828 void TheoryEngine::postProcessModel( theory::TheoryModel
* m
){
829 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
830 if(d_logicInfo
.isTheoryEnabled(theoryId
)) {
831 Trace("model-builder-debug") << " PostProcessModel on theory: " << theoryId
<< endl
;
832 d_theoryTable
[theoryId
]->postProcessModel( m
);
837 TheoryModel
* TheoryEngine::getModel() {
841 TheoryModel
* TheoryEngine::getBuiltModel()
843 if (!d_curr_model
->isBuilt())
845 // If this method was called, we should be in SAT mode, and produceModels
847 AlwaysAssert(options::produceModels());
850 // not available, perhaps due to interuption.
853 // must build model at this point
854 d_curr_model_builder
->buildModel(d_curr_model
);
859 bool TheoryEngine::getSynthSolutions(
860 std::map
<Node
, std::map
<Node
, Node
>>& sol_map
)
864 return d_quantEngine
->getSynthSolutions(sol_map
);
866 // we are not in a quantified logic, there is no synthesis solution
870 bool TheoryEngine::presolve() {
871 // Reset the interrupt flag
872 d_interrupted
= false;
874 // Reset the decision manager. This clears its decision strategies that are
875 // no longer valid in this user context.
876 d_decManager
->presolve();
879 // Definition of the statement that is to be run by every theory
880 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
881 #undef CVC4_FOR_EACH_THEORY_STATEMENT
883 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
884 if (theory::TheoryTraits<THEORY>::hasPresolve) { \
885 theoryOf(THEORY)->presolve(); \
891 // Presolve for each theory using the statement above
892 CVC4_FOR_EACH_THEORY
;
893 } catch(const theory::Interrupted
&) {
894 Trace("theory") << "TheoryEngine::presolve() => interrupted" << endl
;
896 // return whether we have a conflict
898 }/* TheoryEngine::presolve() */
900 void TheoryEngine::postsolve() {
901 // no longer in SAT mode
903 // Reset the interrupt flag
904 d_interrupted
= false;
905 bool CVC4_UNUSED wasInConflict
= d_inConflict
;
908 // Definition of the statement that is to be run by every theory
909 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
910 #undef CVC4_FOR_EACH_THEORY_STATEMENT
912 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
913 if (theory::TheoryTraits<THEORY>::hasPostsolve) \
915 theoryOf(THEORY)->postsolve(); \
916 Assert(!d_inConflict || wasInConflict) \
917 << "conflict raised during postsolve()"; \
920 // Postsolve for each theory using the statement above
921 CVC4_FOR_EACH_THEORY
;
922 } catch(const theory::Interrupted
&) {
923 Trace("theory") << "TheoryEngine::postsolve() => interrupted" << endl
;
925 }/* TheoryEngine::postsolve() */
928 void TheoryEngine::notifyRestart() {
929 // Reset the interrupt flag
930 d_interrupted
= false;
932 // Definition of the statement that is to be run by every theory
933 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
934 #undef CVC4_FOR_EACH_THEORY_STATEMENT
936 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
937 if (theory::TheoryTraits<THEORY>::hasNotifyRestart && d_logicInfo.isTheoryEnabled(THEORY)) { \
938 theoryOf(THEORY)->notifyRestart(); \
941 // notify each theory using the statement above
942 CVC4_FOR_EACH_THEORY
;
945 void TheoryEngine::ppStaticLearn(TNode in
, NodeBuilder
<>& learned
) {
946 // Reset the interrupt flag
947 d_interrupted
= false;
949 // Definition of the statement that is to be run by every theory
950 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
951 #undef CVC4_FOR_EACH_THEORY_STATEMENT
953 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
954 if (theory::TheoryTraits<THEORY>::hasPpStaticLearn) { \
955 theoryOf(THEORY)->ppStaticLearn(in, learned); \
958 // static learning for each theory using the statement above
959 CVC4_FOR_EACH_THEORY
;
962 bool TheoryEngine::isRelevant(Node lit
) const
964 if (d_relManager
!= nullptr)
966 return d_relManager
->isRelevant(lit
);
968 // otherwise must assume its relevant
972 void TheoryEngine::shutdown() {
973 // Set this first; if a Theory shutdown() throws an exception,
974 // at least the destruction of the TheoryEngine won't confound
976 d_hasShutDown
= true;
978 // Shutdown all the theories
979 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
980 if(d_theoryTable
[theoryId
]) {
981 theoryOf(theoryId
)->shutdown();
988 theory::Theory::PPAssertStatus
TheoryEngine::solve(TNode literal
, SubstitutionMap
& substitutionOut
) {
989 // Reset the interrupt flag
990 d_interrupted
= false;
992 TNode atom
= literal
.getKind() == kind::NOT
? literal
[0] : literal
;
993 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< "): solving with " << theoryOf(atom
)->getId() << endl
;
995 if(! d_logicInfo
.isTheoryEnabled(Theory::theoryOf(atom
)) &&
996 Theory::theoryOf(atom
) != THEORY_SAT_SOLVER
) {
998 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
999 << ", which doesn't include " << Theory::theoryOf(atom
)
1000 << ", but got a preprocessing-time fact for that theory." << endl
1001 << "The fact:" << endl
1003 throw LogicException(ss
.str());
1006 Theory::PPAssertStatus solveStatus
= theoryOf(atom
)->ppAssert(literal
, substitutionOut
);
1007 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< ") => " << solveStatus
<< endl
;
1011 void TheoryEngine::preprocessStart() { d_tpp
.clearCache(); }
1013 Node
TheoryEngine::preprocess(TNode assertion
) {
1014 TrustNode trn
= d_tpp
.theoryPreprocess(assertion
);
1020 return trn
.getNode();
1023 void TheoryEngine::notifyPreprocessedAssertions(
1024 const std::vector
<Node
>& assertions
) {
1025 // call all the theories
1026 for (TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
;
1028 if (d_theoryTable
[theoryId
]) {
1029 theoryOf(theoryId
)->ppNotifyAssertions(assertions
);
1032 if (d_relManager
!= nullptr)
1034 d_relManager
->notifyPreprocessedAssertions(assertions
);
1038 bool TheoryEngine::markPropagation(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
1040 // What and where we are asserting
1041 NodeTheoryPair
toAssert(assertion
, toTheoryId
, d_propagationMapTimestamp
);
1042 // What and where it came from
1043 NodeTheoryPair
toExplain(originalAssertion
, fromTheoryId
, d_propagationMapTimestamp
);
1045 // See if the theory already got this literal
1046 PropagationMap::const_iterator find
= d_propagationMap
.find(toAssert
);
1047 if (find
!= d_propagationMap
.end()) {
1048 // The theory already knows this
1049 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): already there" << endl
;
1053 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): marking [" << d_propagationMapTimestamp
<< "] " << assertion
<< ", " << toTheoryId
<< " from " << originalAssertion
<< ", " << fromTheoryId
<< endl
;
1055 // Mark the propagation
1056 d_propagationMap
[toAssert
] = toExplain
;
1057 d_propagationMapTimestamp
= d_propagationMapTimestamp
+ 1;
1063 void TheoryEngine::assertToTheory(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
1065 Trace("theory::assertToTheory") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << originalAssertion
<< "," << toTheoryId
<< ", " << fromTheoryId
<< ")" << endl
;
1067 Assert(toTheoryId
!= fromTheoryId
);
1068 if(toTheoryId
!= THEORY_SAT_SOLVER
&&
1069 ! d_logicInfo
.isTheoryEnabled(toTheoryId
)) {
1071 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
1072 << ", which doesn't include " << toTheoryId
1073 << ", but got an asserted fact to that theory." << endl
1074 << "The fact:" << endl
1076 throw LogicException(ss
.str());
1083 // If sharing is disabled, things are easy
1084 if (!d_logicInfo
.isSharingEnabled()) {
1085 Assert(assertion
== originalAssertion
);
1086 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
1087 // Send to the apropriate theory
1088 theory::Theory
* toTheory
= theoryOf(toTheoryId
);
1089 // We assert it, and we know it's preregistereed
1090 toTheory
->assertFact(assertion
, true);
1091 // Mark that we have more information
1092 d_factsAsserted
= true;
1094 Assert(toTheoryId
== THEORY_SAT_SOLVER
);
1095 // Check for propositional conflict
1097 if (d_propEngine
->hasValue(assertion
, value
)) {
1099 Trace("theory::propagate") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (no sharing)" << endl
;
1100 Trace("dtview::conflict")
1101 << ":THEORY-CONFLICT: " << assertion
<< std::endl
;
1102 d_inConflict
= true;
1107 d_propagatedLiterals
.push_back(assertion
);
1112 // Polarity of the assertion
1113 bool polarity
= assertion
.getKind() != kind::NOT
;
1115 // Atom of the assertion
1116 TNode atom
= polarity
? assertion
: assertion
[0];
1118 // If sending to the shared terms database, it's also simple
1119 if (toTheoryId
== THEORY_BUILTIN
) {
1120 Assert(atom
.getKind() == kind::EQUAL
)
1121 << "atom should be an EQUALity, not `" << atom
<< "'";
1122 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1123 d_sharedTerms
.assertEquality(atom
, polarity
, assertion
);
1128 // Things from the SAT solver are already normalized, so they go
1129 // directly to the apropriate theory
1130 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
1131 // We know that this is normalized, so just send it off to the theory
1132 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1133 // Is it preregistered
1134 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
1136 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
1137 // Mark that we have more information
1138 d_factsAsserted
= true;
1143 // Propagations to the SAT solver are just enqueued for pickup by
1144 // the SAT solver later
1145 if (toTheoryId
== THEORY_SAT_SOLVER
) {
1146 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1147 // Enqueue for propagation to the SAT solver
1148 d_propagatedLiterals
.push_back(assertion
);
1149 // Check for propositional conflicts
1151 if (d_propEngine
->hasValue(assertion
, value
) && !value
) {
1152 Trace("theory::propagate")
1153 << "TheoryEngine::assertToTheory(" << assertion
<< ", "
1154 << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (sharing)"
1156 Trace("dtview::conflict")
1157 << ":THEORY-CONFLICT: " << assertion
<< std::endl
;
1158 d_inConflict
= true;
1164 Assert(atom
.getKind() == kind::EQUAL
);
1167 Node normalizedLiteral
= Rewriter::rewrite(assertion
);
1169 // See if it rewrites false directly -> conflict
1170 if (normalizedLiteral
.isConst()) {
1171 if (!normalizedLiteral
.getConst
<bool>()) {
1172 // Mark the propagation for explanations
1173 if (markPropagation(normalizedLiteral
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1174 // Get the explanation (conflict will figure out where it came from)
1175 conflict(normalizedLiteral
, toTheoryId
);
1183 // Try and assert (note that we assert the non-normalized one)
1184 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1185 // Check if has been pre-registered with the theory
1186 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
1188 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
1189 d_factsAsserted
= true;
1195 void TheoryEngine::assertFact(TNode literal
)
1197 Trace("theory") << "TheoryEngine::assertFact(" << literal
<< ")" << endl
;
1201 // If we're in conflict, nothing to do
1207 bool polarity
= literal
.getKind() != kind::NOT
;
1208 TNode atom
= polarity
? literal
: literal
[0];
1210 if (d_logicInfo
.isSharingEnabled()) {
1212 // If any shared terms, it's time to do sharing work
1213 if (d_sharedTerms
.hasSharedTerms(atom
)) {
1214 // Notify the theories the shared terms
1215 SharedTermsDatabase::shared_terms_iterator it
= d_sharedTerms
.begin(atom
);
1216 SharedTermsDatabase::shared_terms_iterator it_end
= d_sharedTerms
.end(atom
);
1217 for (; it
!= it_end
; ++ it
) {
1219 Theory::Set theories
= d_sharedTerms
.getTheoriesToNotify(atom
, term
);
1220 for (TheoryId id
= THEORY_FIRST
; id
!= THEORY_LAST
; ++ id
) {
1221 if (Theory::setContains(id
, theories
)) {
1222 theoryOf(id
)->addSharedTerm(term
);
1225 d_sharedTerms
.markNotified(term
, theories
);
1229 // If it's an equality, assert it to the shared term manager, even though the terms are not
1230 // yet shared. As the terms become shared later, the shared terms manager will then add them
1231 // to the assert the equality to the interested theories
1232 if (atom
.getKind() == kind::EQUAL
) {
1233 // Assert it to the the owning theory
1234 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1235 // Shared terms manager will assert to interested theories directly, as the terms become shared
1236 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ THEORY_SAT_SOLVER
);
1238 // Now, let's check for any atom triggers from lemmas
1239 AtomRequests::atom_iterator it
= d_atomRequests
.getAtomIterator(atom
);
1240 while (!it
.done()) {
1241 const AtomRequests::Request
& request
= it
.get();
1243 polarity
? (Node
)request
.d_atom
: request
.d_atom
.notNode();
1244 Debug("theory::atoms") << "TheoryEngine::assertFact(" << literal
<< "): sending requested " << toAssert
<< endl
;
1246 toAssert
, literal
, request
.d_toTheory
, THEORY_SAT_SOLVER
);
1251 // Not an equality, just assert to the appropriate theory
1252 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1255 // Assert the fact to the appropriate theory directly
1256 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1260 bool TheoryEngine::propagate(TNode literal
, theory::TheoryId theory
) {
1262 Debug("theory::propagate") << "TheoryEngine::propagate(" << literal
<< ", " << theory
<< ")" << endl
;
1264 Trace("dtview::prop") << std::string(d_context
->getLevel(), ' ')
1265 << ":THEORY-PROP: " << literal
<< endl
;
1269 if(Dump
.isOn("t-propagations")) {
1270 Dump("t-propagations") << CommentCommand("negation of theory propagation: expect valid")
1271 << QueryCommand(literal
.toExpr());
1273 if(Dump
.isOn("missed-t-propagations")) {
1274 d_hasPropagated
.insert(literal
);
1278 bool polarity
= literal
.getKind() != kind::NOT
;
1279 TNode atom
= polarity
? literal
: literal
[0];
1281 if (d_logicInfo
.isSharingEnabled() && atom
.getKind() == kind::EQUAL
) {
1282 if (d_propEngine
->isSatLiteral(literal
)) {
1283 // We propagate SAT literals to SAT
1284 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1286 if (theory
!= THEORY_BUILTIN
) {
1287 // Assert to the shared terms database
1288 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ theory
);
1291 // We could be propagating a unit-clause lemma. In this case, we need to provide a
1293 // TODO: Consider putting this someplace else? This is the only refence to the proof
1294 // manager in this class.
1297 LemmaProofRecipe proofRecipe
;
1298 proofRecipe
.addBaseAssertion(literal
);
1301 LemmaProofRecipe::ProofStep
proofStep(theory
, emptyNode
);
1302 proofStep
.addAssertion(literal
);
1303 proofRecipe
.addStep(proofStep
);
1305 ProofManager::getCnfProof()->setProofRecipe(&proofRecipe
);
1308 // Just send off to the SAT solver
1309 Assert(d_propEngine
->isSatLiteral(literal
));
1310 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1313 return !d_inConflict
;
1316 const LogicInfo
& TheoryEngine::getLogicInfo() const { return d_logicInfo
; }
1318 theory::EqualityStatus
TheoryEngine::getEqualityStatus(TNode a
, TNode b
) {
1319 Assert(a
.getType().isComparableTo(b
.getType()));
1320 if (d_sharedTerms
.isShared(a
) && d_sharedTerms
.isShared(b
)) {
1321 if (d_sharedTerms
.areEqual(a
,b
)) {
1322 return EQUALITY_TRUE_AND_PROPAGATED
;
1324 else if (d_sharedTerms
.areDisequal(a
,b
)) {
1325 return EQUALITY_FALSE_AND_PROPAGATED
;
1328 return theoryOf(Theory::theoryOf(a
.getType()))->getEqualityStatus(a
, b
);
1331 Node
TheoryEngine::getModelValue(TNode var
) {
1334 // the model value of a constant must be itself
1337 Assert(d_sharedTerms
.isShared(var
));
1338 return theoryOf(Theory::theoryOf(var
.getType()))->getModelValue(var
);
1342 Node
TheoryEngine::ensureLiteral(TNode n
) {
1343 Debug("ensureLiteral") << "rewriting: " << n
<< std::endl
;
1344 Node rewritten
= Rewriter::rewrite(n
);
1345 Debug("ensureLiteral") << " got: " << rewritten
<< std::endl
;
1346 Node preprocessed
= preprocess(rewritten
);
1347 Debug("ensureLiteral") << "preprocessed: " << preprocessed
<< std::endl
;
1348 d_propEngine
->ensureLiteral(preprocessed
);
1349 return preprocessed
;
1353 void TheoryEngine::printInstantiations( std::ostream
& out
) {
1354 if( d_quantEngine
){
1355 d_quantEngine
->printInstantiations( out
);
1357 out
<< "Internal error : instantiations not available when quantifiers are not present." << std::endl
;
1362 void TheoryEngine::printSynthSolution( std::ostream
& out
) {
1363 if( d_quantEngine
){
1364 d_quantEngine
->printSynthSolution( out
);
1366 out
<< "Internal error : synth solution not available when quantifiers are not present." << std::endl
;
1371 void TheoryEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1372 if( d_quantEngine
){
1373 d_quantEngine
->getInstantiatedQuantifiedFormulas( qs
);
1379 void TheoryEngine::getInstantiations( Node q
, std::vector
< Node
>& insts
) {
1380 if( d_quantEngine
){
1381 d_quantEngine
->getInstantiations( q
, insts
);
1387 void TheoryEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1388 if( d_quantEngine
){
1389 d_quantEngine
->getInstantiationTermVectors( q
, tvecs
);
1395 void TheoryEngine::getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
) {
1396 if( d_quantEngine
){
1397 d_quantEngine
->getInstantiations( insts
);
1403 void TheoryEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1404 if( d_quantEngine
){
1405 d_quantEngine
->getInstantiationTermVectors( insts
);
1411 Node
TheoryEngine::getInstantiatedConjunction( Node q
) {
1412 if( d_quantEngine
){
1413 return d_quantEngine
->getInstantiatedConjunction( q
);
1416 return Node::null();
1421 static Node
mkExplanation(const std::vector
<NodeTheoryPair
>& explanation
) {
1423 std::set
<TNode
> all
;
1424 for (unsigned i
= 0; i
< explanation
.size(); ++ i
) {
1425 Assert(explanation
[i
].d_theory
== THEORY_SAT_SOLVER
);
1426 all
.insert(explanation
[i
].d_node
);
1429 if (all
.size() == 0) {
1430 // Normalize to true
1431 return NodeManager::currentNM()->mkConst
<bool>(true);
1434 if (all
.size() == 1) {
1435 // All the same, or just one
1436 return explanation
[0].d_node
;
1439 NodeBuilder
<> conjunction(kind::AND
);
1440 std::set
<TNode
>::const_iterator it
= all
.begin();
1441 std::set
<TNode
>::const_iterator it_end
= all
.end();
1442 while (it
!= it_end
) {
1450 Node
TheoryEngine::getExplanationAndRecipe(TNode node
, LemmaProofRecipe
* proofRecipe
) {
1451 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< "): current propagation index = " << d_propagationMapTimestamp
<< endl
;
1453 bool polarity
= node
.getKind() != kind::NOT
;
1454 TNode atom
= polarity
? node
: node
[0];
1456 // If we're not in shared mode, explanations are simple
1457 if (!d_logicInfo
.isSharingEnabled()) {
1458 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing is NOT enabled. "
1459 << " Responsible theory is: "
1460 << theoryOf(atom
)->getId() << std::endl
;
1462 TrustNode texplanation
= theoryOf(atom
)->explain(node
);
1463 Node explanation
= texplanation
.getNode();
1464 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< ") => " << explanation
<< endl
;
1468 LemmaProofRecipe::ProofStep
proofStep(theoryOf(atom
)->getId(), emptyNode
);
1469 proofStep
.addAssertion(node
);
1470 proofRecipe
->addBaseAssertion(node
);
1472 if (explanation
.getKind() == kind::AND
) {
1473 // If the explanation is a conjunction, the recipe for the corresponding lemma is
1474 // the negation of its conjuncts.
1475 Node flat
= flattenAnd(explanation
);
1476 for (unsigned i
= 0; i
< flat
.getNumChildren(); ++i
) {
1477 if (flat
[i
].isConst() && flat
[i
].getConst
<bool>()) {
1481 if (flat
[i
].getKind() == kind::NOT
&&
1482 flat
[i
][0].isConst() && !flat
[i
][0].getConst
<bool>()) {
1486 Debug("theory::explain") << "TheoryEngine::getExplanationAndRecipe: adding recipe assertion: "
1487 << flat
[i
].negate() << std::endl
;
1488 proofStep
.addAssertion(flat
[i
].negate());
1489 proofRecipe
->addBaseAssertion(flat
[i
].negate());
1492 // The recipe for proving it is by negating it. "True" is not an acceptable reason.
1493 if (!((explanation
.isConst() && explanation
.getConst
<bool>()) ||
1494 (explanation
.getKind() == kind::NOT
&&
1495 explanation
[0].isConst() && !explanation
[0].getConst
<bool>()))) {
1496 proofStep
.addAssertion(explanation
.negate());
1497 proofRecipe
->addBaseAssertion(explanation
.negate());
1501 proofRecipe
->addStep(proofStep
);
1508 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing IS enabled" << std::endl
;
1510 // Initial thing to explain
1511 NodeTheoryPair
toExplain(node
, THEORY_SAT_SOLVER
, d_propagationMapTimestamp
);
1512 Assert(d_propagationMap
.find(toExplain
) != d_propagationMap
.end());
1514 NodeTheoryPair nodeExplainerPair
= d_propagationMap
[toExplain
];
1515 Debug("theory::explain")
1516 << "TheoryEngine::getExplanation: explainer for node "
1517 << nodeExplainerPair
.d_node
1518 << " is theory: " << nodeExplainerPair
.d_theory
<< std::endl
;
1519 TheoryId explainer
= nodeExplainerPair
.d_theory
;
1521 // Create the workplace for explanations
1522 std::vector
<NodeTheoryPair
> explanationVector
;
1523 explanationVector
.push_back(d_propagationMap
[toExplain
]);
1524 // Process the explanation
1527 LemmaProofRecipe::ProofStep
proofStep(explainer
, emptyNode
);
1528 proofStep
.addAssertion(node
);
1529 proofRecipe
->addStep(proofStep
);
1530 proofRecipe
->addBaseAssertion(node
);
1533 getExplanation(explanationVector
, proofRecipe
);
1534 Node explanation
= mkExplanation(explanationVector
);
1536 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< ") => " << explanation
<< endl
;
1541 Node
TheoryEngine::getExplanation(TNode node
) {
1542 LemmaProofRecipe
*dontCareRecipe
= NULL
;
1543 return getExplanationAndRecipe(node
, dontCareRecipe
);
1546 struct AtomsCollect
{
1548 std::vector
<TNode
> d_atoms
;
1549 std::unordered_set
<TNode
, TNodeHashFunction
> d_visited
;
1553 typedef void return_type
;
1555 bool alreadyVisited(TNode current
, TNode parent
) {
1556 // Check if already visited
1557 if (d_visited
.find(current
) != d_visited
.end()) return true;
1558 // Don't visit non-boolean
1559 if (!current
.getType().isBoolean()) return true;
1564 void visit(TNode current
, TNode parent
) {
1565 if (Theory::theoryOf(current
) != theory::THEORY_BOOL
) {
1566 d_atoms
.push_back(current
);
1568 d_visited
.insert(current
);
1571 void start(TNode node
) {}
1572 void done(TNode node
) {}
1574 std::vector
<TNode
> getAtoms() const {
1579 void TheoryEngine::ensureLemmaAtoms(const std::vector
<TNode
>& atoms
, theory::TheoryId atomsTo
) {
1580 for (unsigned i
= 0; i
< atoms
.size(); ++ i
) {
1582 // Non-equality atoms are either owned by theory or they don't make sense
1583 if (atoms
[i
].getKind() != kind::EQUAL
) {
1589 // Simple normalization to not repeat stuff
1590 if (eq
[0] > eq
[1]) {
1591 eq
= eq
[1].eqNode(eq
[0]);
1594 // Rewrite the equality
1595 Node eqNormalized
= Rewriter::rewrite(atoms
[i
]);
1597 Debug("theory::atoms") << "TheoryEngine::ensureLemmaAtoms(): " << eq
<< " with nf " << eqNormalized
<< endl
;
1599 // If the equality is a boolean constant, we send immediately
1600 if (eqNormalized
.isConst()) {
1601 if (eqNormalized
.getConst
<bool>()) {
1602 assertToTheory(eq
, eqNormalized
, /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1604 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1607 }else if( eqNormalized
.getKind() != kind::EQUAL
){
1608 Assert(eqNormalized
.getKind() == kind::BOOLEAN_TERM_VARIABLE
1609 || (eqNormalized
.getKind() == kind::NOT
1610 && eqNormalized
[0].getKind() == kind::BOOLEAN_TERM_VARIABLE
));
1611 // this happens for Boolean term equalities V = true that are rewritten to V, we should skip
1612 // TODO : revisit this
1616 // If the normalization did the just flips, keep the flip
1617 if (eqNormalized
[0] == eq
[1] && eqNormalized
[1] == eq
[0]) {
1621 // Check if the equality is already known by the sat solver
1622 if (d_propEngine
->isSatLiteral(eqNormalized
)) {
1624 if (d_propEngine
->hasValue(eqNormalized
, value
)) {
1626 assertToTheory(eq
, eqNormalized
, atomsTo
, theory::THEORY_SAT_SOLVER
);
1629 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), atomsTo
, theory::THEORY_SAT_SOLVER
);
1635 // If the theory is asking about a different form, or the form is ok but if will go to a different theory
1636 // then we must figure it out
1637 if (eqNormalized
!= eq
|| Theory::theoryOf(eq
) != atomsTo
) {
1638 // If you get eqNormalized, send atoms[i] to atomsTo
1639 d_atomRequests
.add(eqNormalized
, eq
, atomsTo
);
1644 theory::LemmaStatus
TheoryEngine::lemma(TNode node
,
1647 theory::LemmaProperty p
,
1648 theory::TheoryId atomsTo
)
1650 // For resource-limiting (also does a time check).
1653 // Do we need to check atoms
1654 if (atomsTo
!= theory::THEORY_LAST
) {
1655 Debug("theory::atoms") << "TheoryEngine::lemma(" << node
<< ", " << atomsTo
<< ")" << endl
;
1656 AtomsCollect collectAtoms
;
1657 NodeVisitor
<AtomsCollect
>::run(collectAtoms
, node
);
1658 ensureLemmaAtoms(collectAtoms
.getAtoms(), atomsTo
);
1661 if(Dump
.isOn("t-lemmas")) {
1666 Dump("t-lemmas") << CommentCommand("theory lemma: expect valid")
1667 << CheckSatCommand(n
.toExpr());
1669 bool removable
= isLemmaPropertyRemovable(p
);
1670 bool preprocess
= isLemmaPropertyPreprocess(p
);
1672 // call preprocessor
1673 std::vector
<TrustNode
> newLemmas
;
1674 std::vector
<Node
> newSkolems
;
1675 TrustNode tlemma
= d_tpp
.preprocess(node
, newLemmas
, newSkolems
, preprocess
);
1677 // !!!!!!! temporary, until this method is fully updated from proof-new
1678 if (tlemma
.isNull())
1680 tlemma
= TrustNode::mkTrustLemma(node
);
1683 // must use an assertion pipeline due to decision engine below
1684 AssertionPipeline lemmas
;
1685 // make the assertion pipeline
1686 lemmas
.push_back(tlemma
.getNode());
1687 lemmas
.updateRealAssertionsEnd();
1688 Assert(newSkolems
.size() == newLemmas
.size());
1689 for (size_t i
= 0, nsize
= newLemmas
.size(); i
< nsize
; i
++)
1691 // store skolem mapping here
1692 IteSkolemMap
& imap
= lemmas
.getIteSkolemMap();
1693 imap
[newSkolems
[i
]] = lemmas
.size();
1694 lemmas
.push_back(newLemmas
[i
].getNode());
1697 // If specified, we must add this lemma to the set of those that need to be
1698 // justified, where note we pass all auxiliary lemmas in lemmas, since these
1699 // by extension must be justified as well.
1700 if (d_relManager
!= nullptr && isLemmaPropertyNeedsJustify(p
))
1702 d_relManager
->notifyPreprocessedAssertions(lemmas
.ref());
1705 // assert lemmas to prop engine
1706 for (size_t i
= 0, lsize
= lemmas
.size(); i
< lsize
; ++i
)
1708 d_propEngine
->assertLemma(
1709 lemmas
[i
], i
== 0 && negated
, removable
, rule
, node
);
1712 // WARNING: Below this point don't assume lemmas[0] to be not negated.
1714 lemmas
.replace(0, lemmas
[0].notNode());
1718 // assert to decision engine
1721 d_propEngine
->addAssertionsToDecisionEngine(lemmas
);
1724 // Mark that we added some lemmas
1725 d_lemmasAdded
= true;
1727 // Lemma analysis isn't online yet; this lemma may only live for this
1729 Node retLemma
= lemmas
[0];
1730 if (lemmas
.size() > 1)
1732 // the returned lemma is the conjunction of all additional lemmas.
1733 retLemma
= NodeManager::currentNM()->mkNode(kind::AND
, lemmas
.ref());
1735 return theory::LemmaStatus(retLemma
, d_userContext
->getLevel());
1738 void TheoryEngine::conflict(TNode conflict
, TheoryId theoryId
) {
1740 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", " << theoryId
<< ")" << endl
;
1742 Trace("dtview::conflict") << ":THEORY-CONFLICT: " << conflict
<< std::endl
;
1744 // Mark that we are in conflict
1745 d_inConflict
= true;
1747 if(Dump
.isOn("t-conflicts")) {
1748 Dump("t-conflicts") << CommentCommand("theory conflict: expect unsat")
1749 << CheckSatCommand(conflict
.toExpr());
1752 LemmaProofRecipe
* proofRecipe
= NULL
;
1754 proofRecipe
= new LemmaProofRecipe
;
1756 LemmaProofRecipe::ProofStep
proofStep(theoryId
, emptyNode
);
1758 if (conflict
.getKind() == kind::AND
) {
1759 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++i
) {
1760 proofStep
.addAssertion(conflict
[i
].negate());
1763 proofStep
.addAssertion(conflict
.negate());
1766 proofRecipe
->addStep(proofStep
);
1769 // In the multiple-theories case, we need to reconstruct the conflict
1770 if (d_logicInfo
.isSharingEnabled()) {
1771 // Create the workplace for explanations
1772 std::vector
<NodeTheoryPair
> explanationVector
;
1773 explanationVector
.push_back(NodeTheoryPair(conflict
, theoryId
, d_propagationMapTimestamp
));
1775 // Process the explanation
1776 getExplanation(explanationVector
, proofRecipe
);
1777 PROOF(ProofManager::getCnfProof()->setProofRecipe(proofRecipe
));
1778 Node fullConflict
= mkExplanation(explanationVector
);
1779 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", " << theoryId
<< "): full = " << fullConflict
<< endl
;
1780 Assert(properConflict(fullConflict
));
1784 LemmaProperty::REMOVABLE
,
1787 // When only one theory, the conflict should need no processing
1788 Assert(properConflict(conflict
));
1790 if (conflict
.getKind() == kind::AND
) {
1791 // If the conflict is a conjunction, the corresponding lemma is derived by negating
1793 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++i
) {
1794 if (conflict
[i
].isConst() && conflict
[i
].getConst
<bool>()) {
1798 if (conflict
[i
].getKind() == kind::NOT
&&
1799 conflict
[i
][0].isConst() && !conflict
[i
][0].getConst
<bool>()) {
1803 proofRecipe
->getStep(0)->addAssertion(conflict
[i
].negate());
1804 proofRecipe
->addBaseAssertion(conflict
[i
].negate());
1807 proofRecipe
->getStep(0)->addAssertion(conflict
.negate());
1808 proofRecipe
->addBaseAssertion(conflict
.negate());
1811 ProofManager::getCnfProof()->setProofRecipe(proofRecipe
);
1814 lemma(conflict
, RULE_CONFLICT
, true, LemmaProperty::REMOVABLE
, THEORY_LAST
);
1823 SharedTermsDatabase
* TheoryEngine::getSharedTermsDatabase()
1825 return &d_sharedTerms
;
1828 void TheoryEngine::getExplanation(std::vector
<NodeTheoryPair
>& explanationVector
, LemmaProofRecipe
* proofRecipe
) {
1829 Assert(explanationVector
.size() > 0);
1831 unsigned i
= 0; // Index of the current literal we are processing
1832 unsigned j
= 0; // Index of the last literal we are keeping
1834 std::unique_ptr
<std::set
<Node
>> inputAssertions
= nullptr;
1838 inputAssertions
.reset(
1839 new std::set
<Node
>(proofRecipe
->getStep(0)->getAssertions()));
1842 // cache of nodes we have already explained by some theory
1843 std::unordered_map
<Node
, size_t, NodeHashFunction
> cache
;
1845 while (i
< explanationVector
.size()) {
1846 // Get the current literal to explain
1847 NodeTheoryPair toExplain
= explanationVector
[i
];
1849 Debug("theory::explain")
1850 << "[i=" << i
<< "] TheoryEngine::explain(): processing ["
1851 << toExplain
.d_timestamp
<< "] " << toExplain
.d_node
<< " sent from "
1852 << toExplain
.d_theory
<< endl
;
1854 if (cache
.find(toExplain
.d_node
) != cache
.end()
1855 && cache
[toExplain
.d_node
] < toExplain
.d_timestamp
)
1860 cache
[toExplain
.d_node
] = toExplain
.d_timestamp
;
1862 // If a true constant or a negation of a false constant we can ignore it
1863 if (toExplain
.d_node
.isConst() && toExplain
.d_node
.getConst
<bool>())
1868 if (toExplain
.d_node
.getKind() == kind::NOT
&& toExplain
.d_node
[0].isConst()
1869 && !toExplain
.d_node
[0].getConst
<bool>())
1875 // If from the SAT solver, keep it
1876 if (toExplain
.d_theory
== THEORY_SAT_SOLVER
)
1878 Debug("theory::explain") << "\tLiteral came from THEORY_SAT_SOLVER. Kepping it." << endl
;
1879 explanationVector
[j
++] = explanationVector
[i
++];
1883 // If an and, expand it
1884 if (toExplain
.d_node
.getKind() == kind::AND
)
1886 Debug("theory::explain")
1887 << "TheoryEngine::explain(): expanding " << toExplain
.d_node
1888 << " got from " << toExplain
.d_theory
<< endl
;
1889 for (unsigned k
= 0; k
< toExplain
.d_node
.getNumChildren(); ++k
)
1891 NodeTheoryPair
newExplain(
1892 toExplain
.d_node
[k
], toExplain
.d_theory
, toExplain
.d_timestamp
);
1893 explanationVector
.push_back(newExplain
);
1899 // See if it was sent to the theory by another theory
1900 PropagationMap::const_iterator find
= d_propagationMap
.find(toExplain
);
1901 if (find
!= d_propagationMap
.end()) {
1902 Debug("theory::explain")
1903 << "\tTerm was propagated by another theory (theory = "
1904 << getTheoryString((*find
).second
.d_theory
) << ")" << std::endl
;
1905 // There is some propagation, check if its a timely one
1906 if ((*find
).second
.d_timestamp
< toExplain
.d_timestamp
)
1908 Debug("theory::explain")
1909 << "\tRelevant timetsamp, pushing " << (*find
).second
.d_node
1910 << "to index = " << explanationVector
.size() << std::endl
;
1911 explanationVector
.push_back((*find
).second
);
1915 if (toExplain
.d_node
!= (*find
).second
.d_node
)
1917 Debug("pf::explain")
1918 << "TheoryEngine::getExplanation: Rewrite alert! toAssert = "
1919 << toExplain
.d_node
<< ", toExplain = " << (*find
).second
.d_node
1924 proofRecipe
->addRewriteRule(toExplain
.d_node
,
1925 (*find
).second
.d_node
);
1935 if (toExplain
.d_theory
== THEORY_BUILTIN
)
1937 explanation
= d_sharedTerms
.explain(toExplain
.d_node
);
1938 Debug("theory::explain") << "\tTerm was propagated by THEORY_BUILTIN. Explanation: " << explanation
<< std::endl
;
1942 TrustNode texp
= theoryOf(toExplain
.d_theory
)->explain(toExplain
.d_node
);
1943 explanation
= texp
.getNode();
1944 Debug("theory::explain") << "\tTerm was propagated by owner theory: "
1945 << theoryOf(toExplain
.d_theory
)->getId()
1946 << ". Explanation: " << explanation
<< std::endl
;
1949 Debug("theory::explain")
1950 << "TheoryEngine::explain(): got explanation " << explanation
1951 << " got from " << toExplain
.d_theory
<< endl
;
1952 Assert(explanation
!= toExplain
.d_node
)
1953 << "wasn't sent to you, so why are you explaining it trivially";
1954 // Mark the explanation
1955 NodeTheoryPair
newExplain(
1956 explanation
, toExplain
.d_theory
, toExplain
.d_timestamp
);
1957 explanationVector
.push_back(newExplain
);
1962 if (proofRecipe
&& inputAssertions
)
1964 // If we're expanding the target node of the explanation (this is the
1965 // first expansion...), we don't want to add it as a separate proof
1966 // step. It is already part of the assertions.
1967 if (!ContainsKey(*inputAssertions
, toExplain
.d_node
))
1969 LemmaProofRecipe::ProofStep
proofStep(toExplain
.d_theory
,
1971 if (explanation
.getKind() == kind::AND
)
1973 Node flat
= flattenAnd(explanation
);
1974 for (unsigned k
= 0; k
< flat
.getNumChildren(); ++k
)
1976 // If a true constant or a negation of a false constant we can
1978 if (!((flat
[k
].isConst() && flat
[k
].getConst
<bool>())
1979 || (flat
[k
].getKind() == kind::NOT
&& flat
[k
][0].isConst()
1980 && !flat
[k
][0].getConst
<bool>())))
1982 proofStep
.addAssertion(flat
[k
].negate());
1988 if (!((explanation
.isConst() && explanation
.getConst
<bool>())
1989 || (explanation
.getKind() == kind::NOT
1990 && explanation
[0].isConst()
1991 && !explanation
[0].getConst
<bool>())))
1993 proofStep
.addAssertion(explanation
.negate());
1996 proofRecipe
->addStep(proofStep
);
2002 // Keep only the relevant literals
2003 explanationVector
.resize(j
);
2007 // The remaining literals are the base of the proof
2008 for (unsigned k
= 0; k
< explanationVector
.size(); ++k
) {
2009 proofRecipe
->addBaseAssertion(explanationVector
[k
].d_node
.negate());
2015 void TheoryEngine::setUserAttribute(const std::string
& attr
,
2017 const std::vector
<Node
>& node_values
,
2018 const std::string
& str_value
)
2020 Trace("te-attr") << "set user attribute " << attr
<< " " << n
<< endl
;
2021 if( d_attr_handle
.find( attr
)!=d_attr_handle
.end() ){
2022 for( size_t i
=0; i
<d_attr_handle
[attr
].size(); i
++ ){
2023 d_attr_handle
[attr
][i
]->setUserAttribute(attr
, n
, node_values
, str_value
);
2026 //unhandled exception?
2030 void TheoryEngine::handleUserAttribute(const char* attr
, Theory
* t
) {
2031 Trace("te-attr") << "Handle user attribute " << attr
<< " " << t
<< endl
;
2032 std::string
str( attr
);
2033 d_attr_handle
[ str
].push_back( t
);
2036 void TheoryEngine::checkTheoryAssertionsWithModel(bool hardFailure
) {
2037 for(TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
2038 Theory
* theory
= d_theoryTable
[theoryId
];
2039 if(theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
2040 for(context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
2041 it_end
= theory
->facts_end();
2044 Node assertion
= (*it
).d_assertion
;
2045 if (!isRelevant(assertion
))
2047 // not relevant, skip
2050 Node val
= getModel()->getValue(assertion
);
2053 std::stringstream ss
;
2054 ss
<< " " << theoryId
2055 << " has an asserted fact that the model doesn't satisfy." << endl
2056 << "The fact: " << assertion
<< endl
2057 << "Model value: " << val
<< endl
;
2062 // Always an error if it is false
2063 InternalError() << ss
.str();
2067 // Otherwise just a warning. Notice this case may happen for
2068 // assertions with unevaluable operators, e.g. transcendental
2069 // functions. It also may happen for separation logic, where
2070 // check-model support is limited.
2071 Warning() << ss
.str();
2080 std::pair
<bool, Node
> TheoryEngine::entailmentCheck(options::TheoryOfMode mode
,
2083 TNode atom
= (lit
.getKind() == kind::NOT
) ? lit
[0] : lit
;
2084 if( atom
.getKind()==kind::AND
|| atom
.getKind()==kind::OR
|| atom
.getKind()==kind::IMPLIES
){
2085 //Boolean connective, recurse
2086 std::vector
< Node
> children
;
2087 bool pol
= (lit
.getKind()!=kind::NOT
);
2088 bool is_conjunction
= pol
==(lit
.getKind()==kind::AND
);
2089 for( unsigned i
=0; i
<atom
.getNumChildren(); i
++ ){
2091 if( pol
==( lit
.getKind()==kind::IMPLIES
&& i
==0 ) ){
2092 ch
= atom
[i
].negate();
2094 std::pair
<bool, Node
> chres
= entailmentCheck(mode
, ch
);
2096 if( !is_conjunction
){
2099 children
.push_back( chres
.second
);
2101 }else if( !chres
.first
&& is_conjunction
){
2102 return std::pair
<bool, Node
>(false, Node::null());
2105 if( is_conjunction
){
2106 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, children
));
2108 return std::pair
<bool, Node
>(false, Node::null());
2110 }else if( atom
.getKind()==kind::ITE
|| ( atom
.getKind()==kind::EQUAL
&& atom
[0].getType().isBoolean() ) ){
2111 bool pol
= (lit
.getKind()!=kind::NOT
);
2112 for( unsigned r
=0; r
<2; r
++ ){
2117 std::pair
<bool, Node
> chres
= entailmentCheck(mode
, ch
);
2119 Node ch2
= atom
[ atom
.getKind()==kind::ITE
? r
+1 : 1 ];
2120 if( pol
==( atom
.getKind()==kind::ITE
? true : r
==1 ) ){
2123 std::pair
<bool, Node
> chres2
= entailmentCheck(mode
, ch2
);
2125 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, chres
.second
, chres2
.second
));
2131 return std::pair
<bool, Node
>(false, Node::null());
2133 //it is a theory atom
2134 theory::TheoryId tid
= theory::Theory::theoryOf(mode
, atom
);
2135 theory::Theory
* th
= theoryOf(tid
);
2138 Trace("theory-engine-entc") << "Entailment check : " << lit
<< std::endl
;
2140 std::pair
<bool, Node
> chres
= th
->entailmentCheck(lit
);
2145 void TheoryEngine::spendResource(ResourceManager::Resource r
)
2147 d_resourceManager
->spendResource(r
);
2150 }/* CVC4 namespace */