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/rewriter.h"
51 #include "theory/theory.h"
52 #include "theory/theory_model.h"
53 #include "theory/theory_traits.h"
54 #include "theory/uf/equality_engine.h"
55 #include "util/resource_manager.h"
59 using namespace CVC4::preprocessing
;
60 using namespace CVC4::theory
;
64 /* -------------------------------------------------------------------------- */
69 * IMPORTANT: The order of the theories is important. For example, strings
70 * depends on arith, quantifiers needs to come as the very last.
71 * Do not change this order.
74 #define CVC4_FOR_EACH_THEORY \
75 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BUILTIN) \
76 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BOOL) \
77 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_UF) \
78 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_ARITH) \
79 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BV) \
80 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_FP) \
81 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_ARRAYS) \
82 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_DATATYPES) \
83 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_SEP) \
84 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_SETS) \
85 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_STRINGS) \
86 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_QUANTIFIERS)
90 /* -------------------------------------------------------------------------- */
92 inline void flattenAnd(Node n
, std::vector
<TNode
>& out
){
93 Assert(n
.getKind() == kind::AND
);
94 for(Node::iterator i
=n
.begin(), i_end
=n
.end(); i
!= i_end
; ++i
){
96 if(curr
.getKind() == kind::AND
){
97 flattenAnd(curr
, out
);
104 inline Node
flattenAnd(Node n
){
105 std::vector
<TNode
> out
;
107 return NodeManager::currentNM()->mkNode(kind::AND
, out
);
111 * Compute the string for a given theory id. In this module, we use
112 * THEORY_SAT_SOLVER as an id, which is not a normal id but maps to
113 * THEORY_LAST. Thus, we need our own string conversion here.
115 * @param id The theory id
116 * @return The string corresponding to the theory id
118 std::string
getTheoryString(theory::TheoryId id
)
120 if (id
== theory::THEORY_SAT_SOLVER
)
122 return "THEORY_SAT_SOLVER";
126 std::stringstream ss
;
132 void TheoryEngine::finishInit() {
133 // initialize the quantifiers engine
134 if (d_logicInfo
.isQuantified())
136 // initialize the quantifiers engine
137 d_quantEngine
= new QuantifiersEngine(d_context
, d_userContext
, this);
140 // Initialize the equality engine architecture for all theories, which
141 // includes the master equality engine.
142 d_eeDistributed
.reset(new EqEngineManagerDistributed(*this));
143 d_eeDistributed
->finishInit();
145 // Initialize the model and model builder.
146 if (d_logicInfo
.isQuantified())
148 d_curr_model_builder
= d_quantEngine
->getModelBuilder();
149 d_curr_model
= d_quantEngine
->getModel();
151 d_curr_model
= new theory::TheoryModel(
152 d_userContext
, "DefaultModel", options::assignFunctionValues());
153 d_aloc_curr_model
= true;
156 //make the default builder, e.g. in the case that the quantifiers engine does not have a model builder
157 if( d_curr_model_builder
==NULL
){
158 d_curr_model_builder
= new theory::TheoryEngineModelBuilder(this);
159 d_aloc_curr_model_builder
= true;
162 // set the core equality engine on quantifiers engine
163 if (d_logicInfo
.isQuantified())
165 d_quantEngine
->setMasterEqualityEngine(
166 d_eeDistributed
->getMasterEqualityEngine());
169 // finish initializing the theories
170 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
171 Theory
* t
= d_theoryTable
[theoryId
];
176 // setup the pointers to the utilities
177 const EeTheoryInfo
* eeti
= d_eeDistributed
->getEeTheoryInfo(theoryId
);
178 Assert(eeti
!= nullptr);
179 // the theory's official equality engine is the one specified by the
180 // equality engine manager
181 t
->setEqualityEngine(eeti
->d_usedEe
);
182 // set the quantifiers engine
183 t
->setQuantifiersEngine(d_quantEngine
);
184 // set the decision manager for the theory
185 t
->setDecisionManager(d_decManager
.get());
186 // finish initializing the theory
191 TheoryEngine::TheoryEngine(context::Context
* context
,
192 context::UserContext
* userContext
,
194 RemoveTermFormulas
& iteRemover
,
195 const LogicInfo
& logicInfo
)
196 : d_propEngine(nullptr),
198 d_userContext(userContext
),
199 d_logicInfo(logicInfo
),
200 d_sharedTerms(this, context
),
201 d_eeDistributed(nullptr),
202 d_quantEngine(nullptr),
203 d_decManager(new DecisionManager(userContext
)),
204 d_curr_model(nullptr),
205 d_aloc_curr_model(false),
206 d_curr_model_builder(nullptr),
207 d_aloc_curr_model_builder(false),
208 d_eager_model_building(false),
209 d_possiblePropagations(context
),
210 d_hasPropagated(context
),
211 d_inConflict(context
, false),
213 d_hasShutDown(false),
214 d_incomplete(context
, false),
215 d_propagationMap(context
),
216 d_propagationMapTimestamp(context
, 0),
217 d_propagatedLiterals(context
),
218 d_propagatedLiteralsIndex(context
, 0),
219 d_atomRequests(context
),
220 d_tpp(*this, iteRemover
),
221 d_combineTheoriesTime("TheoryEngine::combineTheoriesTime"),
224 d_interrupted(false),
225 d_resourceManager(rm
),
226 d_inPreregister(false),
227 d_factsAsserted(context
, false),
228 d_preRegistrationVisitor(this, context
),
229 d_sharedTermsVisitor(d_sharedTerms
),
231 d_arithSubstitutionsAdded("theory::arith::zzz::arith::substitutions", 0)
233 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
;
236 d_theoryTable
[theoryId
] = NULL
;
237 d_theoryOut
[theoryId
] = NULL
;
240 smtStatisticsRegistry()->registerStat(&d_combineTheoriesTime
);
241 d_true
= NodeManager::currentNM()->mkConst
<bool>(true);
242 d_false
= NodeManager::currentNM()->mkConst
<bool>(false);
245 ProofManager::currentPM()->initTheoryProofEngine();
248 smtStatisticsRegistry()->registerStat(&d_arithSubstitutionsAdded
);
251 TheoryEngine::~TheoryEngine() {
252 Assert(d_hasShutDown
);
254 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
255 if(d_theoryTable
[theoryId
] != NULL
) {
256 delete d_theoryTable
[theoryId
];
257 delete d_theoryOut
[theoryId
];
261 if( d_aloc_curr_model_builder
){
262 delete d_curr_model_builder
;
264 if( d_aloc_curr_model
){
268 delete d_quantEngine
;
270 smtStatisticsRegistry()->unregisterStat(&d_combineTheoriesTime
);
271 smtStatisticsRegistry()->unregisterStat(&d_arithSubstitutionsAdded
);
274 void TheoryEngine::interrupt() { d_interrupted
= true; }
275 void TheoryEngine::preRegister(TNode preprocessed
) {
277 Debug("theory") << "TheoryEngine::preRegister( " << preprocessed
<< ")" << std::endl
;
278 if(Dump
.isOn("missed-t-propagations")) {
279 d_possiblePropagations
.push_back(preprocessed
);
281 d_preregisterQueue
.push(preprocessed
);
283 if (!d_inPreregister
) {
284 // We're in pre-register
285 d_inPreregister
= true;
287 // Process the pre-registration queue
288 while (!d_preregisterQueue
.empty()) {
289 // Get the next atom to pre-register
290 preprocessed
= d_preregisterQueue
.front();
291 d_preregisterQueue
.pop();
293 if (d_logicInfo
.isSharingEnabled() && preprocessed
.getKind() == kind::EQUAL
) {
294 // When sharing is enabled, we propagate from the shared terms manager also
295 d_sharedTerms
.addEqualityToPropagate(preprocessed
);
298 // the atom should not have free variables
299 Debug("theory") << "TheoryEngine::preRegister: " << preprocessed
301 Assert(!expr::hasFreeVar(preprocessed
));
302 // Pre-register the terms in the atom
303 Theory::Set theories
= NodeVisitor
<PreRegisterVisitor
>::run(d_preRegistrationVisitor
, preprocessed
);
304 theories
= Theory::setRemove(THEORY_BOOL
, theories
);
305 // Remove the top theory, if any more that means multiple theories were involved
306 bool multipleTheories
= Theory::setRemove(Theory::theoryOf(preprocessed
), theories
);
308 // These checks don't work with finite model finding, because it
309 // uses Rational constants to represent cardinality constraints,
310 // even though arithmetic isn't actually involved.
311 if(!options::finiteModelFind()) {
312 while((i
= Theory::setPop(theories
)) != THEORY_LAST
) {
313 if(!d_logicInfo
.isTheoryEnabled(i
)) {
314 LogicInfo newLogicInfo
= d_logicInfo
.getUnlockedCopy();
315 newLogicInfo
.enableTheory(i
);
318 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
319 << ", which doesn't include " << i
320 << ", but found a term in that theory." << endl
321 << "You might want to extend your logic to "
322 << newLogicInfo
.getLogicString() << endl
;
323 throw LogicException(ss
.str());
327 if (multipleTheories
) {
328 // Collect the shared terms if there are multiple theories
329 NodeVisitor
<SharedTermsVisitor
>::run(d_sharedTermsVisitor
, preprocessed
);
333 // Leaving pre-register
334 d_inPreregister
= false;
338 void TheoryEngine::printAssertions(const char* tag
) {
339 if (Trace
.isOn(tag
)) {
341 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
342 Theory
* theory
= d_theoryTable
[theoryId
];
343 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
344 Trace(tag
) << "--------------------------------------------" << endl
;
345 Trace(tag
) << "Assertions of " << theory
->getId() << ": " << endl
;
347 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
350 for (unsigned i
= 0; it
!= it_end
; ++it
, ++i
)
352 if ((*it
).d_isPreregistered
)
354 Trace(tag
) << "[" << i
<< "]: ";
358 Trace(tag
) << "(" << i
<< "): ";
360 Trace(tag
) << (*it
).d_assertion
<< endl
;
364 if (d_logicInfo
.isSharingEnabled()) {
365 Trace(tag
) << "Shared terms of " << theory
->getId() << ": " << endl
;
366 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
367 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
368 Trace(tag
) << "[" << i
<< "]: " << (*it
) << endl
;
376 void TheoryEngine::dumpAssertions(const char* tag
) {
377 if (Dump
.isOn(tag
)) {
378 Dump(tag
) << CommentCommand("Starting completeness check");
379 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
380 Theory
* theory
= d_theoryTable
[theoryId
];
381 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
382 Dump(tag
) << CommentCommand("Completeness check");
383 Dump(tag
) << PushCommand();
385 // Dump the shared terms
386 if (d_logicInfo
.isSharingEnabled()) {
387 Dump(tag
) << CommentCommand("Shared terms");
388 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
389 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
392 Dump(tag
) << CommentCommand(ss
.str());
396 // Dump the assertions
397 Dump(tag
) << CommentCommand("Assertions");
398 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(), it_end
= theory
->facts_end();
399 for (; it
!= it_end
; ++ it
) {
401 Node assertionNode
= (*it
).d_assertion
;
402 // Purify all the terms
404 if ((*it
).d_isPreregistered
)
406 Dump(tag
) << CommentCommand("Preregistered");
410 Dump(tag
) << CommentCommand("Shared assertion");
412 Dump(tag
) << AssertCommand(assertionNode
.toExpr());
414 Dump(tag
) << CheckSatCommand();
416 Dump(tag
) << PopCommand();
423 * Check all (currently-active) theories for conflicts.
424 * @param effort the effort level to use
426 void TheoryEngine::check(Theory::Effort effort
) {
429 // Reset the interrupt flag
430 d_interrupted
= false;
432 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
433 #undef CVC4_FOR_EACH_THEORY_STATEMENT
435 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
436 if (theory::TheoryTraits<THEORY>::hasCheck && d_logicInfo.isTheoryEnabled(THEORY)) { \
437 theoryOf(THEORY)->check(effort); \
438 if (d_inConflict) { \
439 Debug("conflict") << THEORY << " in conflict. " << std::endl; \
447 // Mark the output channel unused (if this is FULL_EFFORT, and nothing
448 // is done by the theories, no additional check will be needed)
449 d_outputChannelUsed
= false;
451 // Mark the lemmas flag (no lemmas added)
452 d_lemmasAdded
= false;
454 Debug("theory") << "TheoryEngine::check(" << effort
<< "): d_factsAsserted = " << (d_factsAsserted
? "true" : "false") << endl
;
456 // If in full effort, we have a fake new assertion just to jumpstart the checking
457 if (Theory::fullEffort(effort
)) {
458 d_factsAsserted
= true;
462 while (d_factsAsserted
&& !d_inConflict
&& !d_lemmasAdded
) {
464 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running check" << endl
;
466 Trace("theory::assertions") << endl
;
467 if (Trace
.isOn("theory::assertions")) {
468 printAssertions("theory::assertions");
471 if(Theory::fullEffort(effort
)) {
472 Trace("theory::assertions::fulleffort") << endl
;
473 if (Trace
.isOn("theory::assertions::fulleffort")) {
474 printAssertions("theory::assertions::fulleffort");
478 // Note that we've discharged all the facts
479 d_factsAsserted
= false;
482 CVC4_FOR_EACH_THEORY
;
484 if(Dump
.isOn("missed-t-conflicts")) {
485 Dump("missed-t-conflicts")
486 << CommentCommand("Completeness check for T-conflicts; expect sat")
487 << CheckSatCommand();
490 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running propagation after the initial check" << endl
;
492 // We are still satisfiable, propagate as much as possible
495 // We do combination if all has been processed and we are in fullcheck
496 if (Theory::fullEffort(effort
) && d_logicInfo
.isSharingEnabled() && !d_factsAsserted
&& !d_lemmasAdded
&& !d_inConflict
) {
497 // Do the combination
498 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running combination" << endl
;
500 if(d_logicInfo
.isQuantified()){
501 d_quantEngine
->notifyCombineTheories();
506 // Must consult quantifiers theory for last call to ensure sat, or otherwise add a lemma
507 if( Theory::fullEffort(effort
) && ! d_inConflict
&& ! needCheck() ) {
508 Trace("theory::assertions-model") << endl
;
509 if (Trace
.isOn("theory::assertions-model")) {
510 printAssertions("theory::assertions-model");
512 //checks for theories requiring the model go at last call
513 d_curr_model
->reset();
514 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
515 if( theoryId
!=THEORY_QUANTIFIERS
){
516 Theory
* theory
= d_theoryTable
[theoryId
];
517 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
518 if( theory
->needsCheckLastEffort() ){
519 if( !d_curr_model
->isBuilt() ){
520 if( !d_curr_model_builder
->buildModel(d_curr_model
) ){
524 theory
->check(Theory::EFFORT_LAST_CALL
);
531 if(d_logicInfo
.isQuantified()) {
532 // quantifiers engine must check at last call effort
533 d_quantEngine
->check(Theory::EFFORT_LAST_CALL
);
536 if (!d_inConflict
&& !needCheck())
538 // If d_eager_model_building is false, then we only mark that we
539 // are in "SAT mode". We build the model later only if the user asks
540 // for it via getBuiltModel.
542 if (d_eager_model_building
&& !d_curr_model
->isBuilt())
544 d_curr_model_builder
->buildModel(d_curr_model
);
549 Debug("theory") << "TheoryEngine::check(" << effort
<< "): done, we are " << (d_inConflict
? "unsat" : "sat") << (d_lemmasAdded
? " with new lemmas" : " with no new lemmas");
550 Debug("theory") << ", need check = " << (needCheck() ? "YES" : "NO") << endl
;
552 if( Theory::fullEffort(effort
) && !d_inConflict
&& !needCheck()) {
553 // case where we are about to answer SAT, the master equality engine,
554 // if it exists, must be consistent.
555 eq::EqualityEngine
* mee
= d_eeDistributed
->getMasterEqualityEngine();
558 AlwaysAssert(mee
->consistent());
560 if (d_curr_model
->isBuilt())
562 // model construction should always succeed unless lemmas were added
563 AlwaysAssert(d_curr_model
->isBuiltSuccess());
564 if (options::produceModels())
566 // Do post-processing of model from the theories (used for THEORY_SEP
567 // to construct heap model)
568 postProcessModel(d_curr_model
);
569 // also call the model builder's post-process model
570 d_curr_model_builder
->postProcessModel(d_incomplete
.get(),
575 } catch(const theory::Interrupted
&) {
576 Trace("theory") << "TheoryEngine::check() => interrupted" << endl
;
578 // If fulleffort, check all theories
579 if(Dump
.isOn("theory::fullcheck") && Theory::fullEffort(effort
)) {
580 if (!d_inConflict
&& !needCheck()) {
581 dumpAssertions("theory::fullcheck");
586 void TheoryEngine::combineTheories() {
588 Trace("combineTheories") << "TheoryEngine::combineTheories()" << endl
;
590 TimerStat::CodeTimer
combineTheoriesTimer(d_combineTheoriesTime
);
592 // Care graph we'll be building
595 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
596 #undef CVC4_FOR_EACH_THEORY_STATEMENT
598 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
599 if (theory::TheoryTraits<THEORY>::isParametric && d_logicInfo.isTheoryEnabled(THEORY)) { \
600 theoryOf(THEORY)->getCareGraph(&careGraph); \
603 // Call on each parametric theory to give us its care graph
604 CVC4_FOR_EACH_THEORY
;
606 Trace("combineTheories") << "TheoryEngine::combineTheories(): care graph size = " << careGraph
.size() << endl
;
608 // Now add splitters for the ones we are interested in
609 CareGraph::const_iterator care_it
= careGraph
.begin();
610 CareGraph::const_iterator care_it_end
= careGraph
.end();
612 for (; care_it
!= care_it_end
; ++ care_it
) {
613 const CarePair
& carePair
= *care_it
;
615 Debug("combineTheories")
616 << "TheoryEngine::combineTheories(): checking " << carePair
.d_a
<< " = "
617 << carePair
.d_b
<< " from " << carePair
.d_theory
<< endl
;
619 Assert(d_sharedTerms
.isShared(carePair
.d_a
) || carePair
.d_a
.isConst());
620 Assert(d_sharedTerms
.isShared(carePair
.d_b
) || carePair
.d_b
.isConst());
622 // The equality in question (order for no repetition)
623 Node equality
= carePair
.d_a
.eqNode(carePair
.d_b
);
624 // EqualityStatus es = getEqualityStatus(carePair.d_a, carePair.d_b);
625 // Debug("combineTheories") << "TheoryEngine::combineTheories(): " <<
626 // (es == EQUALITY_TRUE_AND_PROPAGATED ? "EQUALITY_TRUE_AND_PROPAGATED" :
627 // es == EQUALITY_FALSE_AND_PROPAGATED ? "EQUALITY_FALSE_AND_PROPAGATED" :
628 // es == EQUALITY_TRUE ? "EQUALITY_TRUE" :
629 // es == EQUALITY_FALSE ? "EQUALITY_FALSE" :
630 // es == EQUALITY_TRUE_IN_MODEL ? "EQUALITY_TRUE_IN_MODEL" :
631 // es == EQUALITY_FALSE_IN_MODEL ? "EQUALITY_FALSE_IN_MODEL" :
632 // es == EQUALITY_UNKNOWN ? "EQUALITY_UNKNOWN" :
633 // "Unexpected case") << endl;
635 // We need to split on it
636 Debug("combineTheories") << "TheoryEngine::combineTheories(): requesting a split " << endl
;
638 lemma(equality
.orNode(equality
.notNode()),
644 // This code is supposed to force preference to follow what the theory models already have
645 // but it doesn't seem to make a big difference - need to explore more -Clark
647 // if (es == EQUALITY_TRUE || es == EQUALITY_TRUE_IN_MODEL) {
648 Node e
= ensureLiteral(equality
);
649 d_propEngine
->requirePhase(e
, true);
651 // else if (es == EQUALITY_FALSE_IN_MODEL) {
652 // Node e = ensureLiteral(equality);
653 // d_propEngine->requirePhase(e, false);
659 void TheoryEngine::propagate(Theory::Effort effort
) {
660 // Reset the interrupt flag
661 d_interrupted
= false;
663 // Definition of the statement that is to be run by every theory
664 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
665 #undef CVC4_FOR_EACH_THEORY_STATEMENT
667 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
668 if (theory::TheoryTraits<THEORY>::hasPropagate && d_logicInfo.isTheoryEnabled(THEORY)) { \
669 theoryOf(THEORY)->propagate(effort); \
672 // Reset the interrupt flag
673 d_interrupted
= false;
675 // Propagate for each theory using the statement above
676 CVC4_FOR_EACH_THEORY
;
678 if(Dump
.isOn("missed-t-propagations")) {
679 for(unsigned i
= 0; i
< d_possiblePropagations
.size(); ++i
) {
680 Node atom
= d_possiblePropagations
[i
];
682 if(d_propEngine
->hasValue(atom
, value
)) {
685 // Doesn't have a value, check it (and the negation)
686 if(d_hasPropagated
.find(atom
) == d_hasPropagated
.end()) {
687 Dump("missed-t-propagations")
688 << CommentCommand("Completeness check for T-propagations; expect invalid")
689 << EchoCommand(atom
.toString())
690 << QueryCommand(atom
.toExpr())
691 << EchoCommand(atom
.notNode().toString())
692 << QueryCommand(atom
.notNode().toExpr());
698 Node
TheoryEngine::getNextDecisionRequest()
700 return d_decManager
->getNextDecisionRequest();
703 bool TheoryEngine::properConflict(TNode conflict
) const {
705 if (conflict
.getKind() == kind::AND
) {
706 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++ i
) {
707 if (! getPropEngine()->hasValue(conflict
[i
], value
)) {
708 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
709 << conflict
[i
] << endl
;
713 Debug("properConflict") << "Bad conflict is due to false atom: "
714 << conflict
[i
] << endl
;
717 if (conflict
[i
] != Rewriter::rewrite(conflict
[i
])) {
718 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
719 << conflict
[i
] << " vs " << Rewriter::rewrite(conflict
[i
]) << endl
;
724 if (! getPropEngine()->hasValue(conflict
, value
)) {
725 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
730 Debug("properConflict") << "Bad conflict is due to false atom: "
734 if (conflict
!= Rewriter::rewrite(conflict
)) {
735 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
736 << conflict
<< " vs " << Rewriter::rewrite(conflict
) << endl
;
743 bool TheoryEngine::properPropagation(TNode lit
) const {
744 if(!getPropEngine()->isSatLiteral(lit
)) {
748 return !getPropEngine()->hasValue(lit
, b
);
751 bool TheoryEngine::properExplanation(TNode node
, TNode expl
) const {
752 // Explanation must be either a conjunction of true literals that have true SAT values already
753 // or a singled literal that has a true SAT value already.
754 if (expl
.getKind() == kind::AND
) {
755 for (unsigned i
= 0; i
< expl
.getNumChildren(); ++ i
) {
757 if (!d_propEngine
->hasValue(expl
[i
], value
) || !value
) {
763 return d_propEngine
->hasValue(expl
, value
) && value
;
768 bool TheoryEngine::collectModelInfo(theory::TheoryModel
* m
)
770 //have shared term engine collectModelInfo
771 // d_sharedTerms.collectModelInfo( m );
772 // Consult each active theory to get all relevant information
773 // concerning the model.
774 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
775 if(d_logicInfo
.isTheoryEnabled(theoryId
)) {
776 Trace("model-builder") << " CollectModelInfo on theory: " << theoryId
<< endl
;
777 if (!d_theoryTable
[theoryId
]->collectModelInfo(m
))
783 Trace("model-builder") << " CollectModelInfo boolean variables" << std::endl
;
784 // Get the Boolean variables
785 vector
<TNode
> boolVars
;
786 d_propEngine
->getBooleanVariables(boolVars
);
787 vector
<TNode
>::iterator it
, iend
= boolVars
.end();
788 bool hasValue
, value
;
789 for (it
= boolVars
.begin(); it
!= iend
; ++it
) {
791 hasValue
= d_propEngine
->hasValue(var
, value
);
792 // TODO: Assert that hasValue is true?
794 Trace("model-builder-assertions")
795 << " has no value : " << var
<< std::endl
;
798 Trace("model-builder-assertions") << "(assert" << (value
? " " : " (not ") << var
<< (value
? ");" : "));") << endl
;
799 if (!m
->assertPredicate(var
, value
))
807 void TheoryEngine::postProcessModel( theory::TheoryModel
* m
){
808 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
809 if(d_logicInfo
.isTheoryEnabled(theoryId
)) {
810 Trace("model-builder-debug") << " PostProcessModel on theory: " << theoryId
<< endl
;
811 d_theoryTable
[theoryId
]->postProcessModel( m
);
816 TheoryModel
* TheoryEngine::getModel() {
820 TheoryModel
* TheoryEngine::getBuiltModel()
822 if (!d_curr_model
->isBuilt())
824 // If this method was called, we should be in SAT mode, and produceModels
826 AlwaysAssert(options::produceModels());
829 // not available, perhaps due to interuption.
832 // must build model at this point
833 d_curr_model_builder
->buildModel(d_curr_model
);
838 bool TheoryEngine::getSynthSolutions(
839 std::map
<Node
, std::map
<Node
, Node
>>& sol_map
)
843 return d_quantEngine
->getSynthSolutions(sol_map
);
845 // we are not in a quantified logic, there is no synthesis solution
849 bool TheoryEngine::presolve() {
850 // Reset the interrupt flag
851 d_interrupted
= false;
853 // Reset the decision manager. This clears its decision strategies that are
854 // no longer valid in this user context.
855 d_decManager
->presolve();
858 // Definition of the statement that is to be run by every theory
859 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
860 #undef CVC4_FOR_EACH_THEORY_STATEMENT
862 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
863 if (theory::TheoryTraits<THEORY>::hasPresolve) { \
864 theoryOf(THEORY)->presolve(); \
870 // Presolve for each theory using the statement above
871 CVC4_FOR_EACH_THEORY
;
872 } catch(const theory::Interrupted
&) {
873 Trace("theory") << "TheoryEngine::presolve() => interrupted" << endl
;
875 // return whether we have a conflict
877 }/* TheoryEngine::presolve() */
879 void TheoryEngine::postsolve() {
880 // no longer in SAT mode
882 // Reset the interrupt flag
883 d_interrupted
= false;
884 bool CVC4_UNUSED wasInConflict
= d_inConflict
;
887 // Definition of the statement that is to be run by every theory
888 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
889 #undef CVC4_FOR_EACH_THEORY_STATEMENT
891 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
892 if (theory::TheoryTraits<THEORY>::hasPostsolve) \
894 theoryOf(THEORY)->postsolve(); \
895 Assert(!d_inConflict || wasInConflict) \
896 << "conflict raised during postsolve()"; \
899 // Postsolve for each theory using the statement above
900 CVC4_FOR_EACH_THEORY
;
901 } catch(const theory::Interrupted
&) {
902 Trace("theory") << "TheoryEngine::postsolve() => interrupted" << endl
;
904 }/* TheoryEngine::postsolve() */
907 void TheoryEngine::notifyRestart() {
908 // Reset the interrupt flag
909 d_interrupted
= false;
911 // Definition of the statement that is to be run by every theory
912 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
913 #undef CVC4_FOR_EACH_THEORY_STATEMENT
915 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
916 if (theory::TheoryTraits<THEORY>::hasNotifyRestart && d_logicInfo.isTheoryEnabled(THEORY)) { \
917 theoryOf(THEORY)->notifyRestart(); \
920 // notify each theory using the statement above
921 CVC4_FOR_EACH_THEORY
;
924 void TheoryEngine::ppStaticLearn(TNode in
, NodeBuilder
<>& learned
) {
925 // Reset the interrupt flag
926 d_interrupted
= false;
928 // Definition of the statement that is to be run by every theory
929 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
930 #undef CVC4_FOR_EACH_THEORY_STATEMENT
932 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
933 if (theory::TheoryTraits<THEORY>::hasPpStaticLearn) { \
934 theoryOf(THEORY)->ppStaticLearn(in, learned); \
937 // static learning for each theory using the statement above
938 CVC4_FOR_EACH_THEORY
;
941 void TheoryEngine::shutdown() {
942 // Set this first; if a Theory shutdown() throws an exception,
943 // at least the destruction of the TheoryEngine won't confound
945 d_hasShutDown
= true;
947 // Shutdown all the theories
948 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
949 if(d_theoryTable
[theoryId
]) {
950 theoryOf(theoryId
)->shutdown();
957 theory::Theory::PPAssertStatus
TheoryEngine::solve(TNode literal
, SubstitutionMap
& substitutionOut
) {
958 // Reset the interrupt flag
959 d_interrupted
= false;
961 TNode atom
= literal
.getKind() == kind::NOT
? literal
[0] : literal
;
962 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< "): solving with " << theoryOf(atom
)->getId() << endl
;
964 if(! d_logicInfo
.isTheoryEnabled(Theory::theoryOf(atom
)) &&
965 Theory::theoryOf(atom
) != THEORY_SAT_SOLVER
) {
967 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
968 << ", which doesn't include " << Theory::theoryOf(atom
)
969 << ", but got a preprocessing-time fact for that theory." << endl
970 << "The fact:" << endl
972 throw LogicException(ss
.str());
975 Theory::PPAssertStatus solveStatus
= theoryOf(atom
)->ppAssert(literal
, substitutionOut
);
976 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< ") => " << solveStatus
<< endl
;
980 void TheoryEngine::preprocessStart() { d_tpp
.clearCache(); }
982 Node
TheoryEngine::preprocess(TNode assertion
) {
983 return d_tpp
.theoryPreprocess(assertion
);
986 void TheoryEngine::notifyPreprocessedAssertions(
987 const std::vector
<Node
>& assertions
) {
988 // call all the theories
989 for (TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
;
991 if (d_theoryTable
[theoryId
]) {
992 theoryOf(theoryId
)->ppNotifyAssertions(assertions
);
997 bool TheoryEngine::markPropagation(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
999 // What and where we are asserting
1000 NodeTheoryPair
toAssert(assertion
, toTheoryId
, d_propagationMapTimestamp
);
1001 // What and where it came from
1002 NodeTheoryPair
toExplain(originalAssertion
, fromTheoryId
, d_propagationMapTimestamp
);
1004 // See if the theory already got this literal
1005 PropagationMap::const_iterator find
= d_propagationMap
.find(toAssert
);
1006 if (find
!= d_propagationMap
.end()) {
1007 // The theory already knows this
1008 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): already there" << endl
;
1012 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): marking [" << d_propagationMapTimestamp
<< "] " << assertion
<< ", " << toTheoryId
<< " from " << originalAssertion
<< ", " << fromTheoryId
<< endl
;
1014 // Mark the propagation
1015 d_propagationMap
[toAssert
] = toExplain
;
1016 d_propagationMapTimestamp
= d_propagationMapTimestamp
+ 1;
1022 void TheoryEngine::assertToTheory(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
1024 Trace("theory::assertToTheory") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << originalAssertion
<< "," << toTheoryId
<< ", " << fromTheoryId
<< ")" << endl
;
1026 Assert(toTheoryId
!= fromTheoryId
);
1027 if(toTheoryId
!= THEORY_SAT_SOLVER
&&
1028 ! d_logicInfo
.isTheoryEnabled(toTheoryId
)) {
1030 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
1031 << ", which doesn't include " << toTheoryId
1032 << ", but got an asserted fact to that theory." << endl
1033 << "The fact:" << endl
1035 throw LogicException(ss
.str());
1042 // If sharing is disabled, things are easy
1043 if (!d_logicInfo
.isSharingEnabled()) {
1044 Assert(assertion
== originalAssertion
);
1045 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
1046 // Send to the apropriate theory
1047 theory::Theory
* toTheory
= theoryOf(toTheoryId
);
1048 // We assert it, and we know it's preregistereed
1049 toTheory
->assertFact(assertion
, true);
1050 // Mark that we have more information
1051 d_factsAsserted
= true;
1053 Assert(toTheoryId
== THEORY_SAT_SOLVER
);
1054 // Check for propositional conflict
1056 if (d_propEngine
->hasValue(assertion
, value
)) {
1058 Trace("theory::propagate") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (no sharing)" << endl
;
1059 Trace("dtview::conflict")
1060 << ":THEORY-CONFLICT: " << assertion
<< std::endl
;
1061 d_inConflict
= true;
1066 d_propagatedLiterals
.push_back(assertion
);
1071 // Polarity of the assertion
1072 bool polarity
= assertion
.getKind() != kind::NOT
;
1074 // Atom of the assertion
1075 TNode atom
= polarity
? assertion
: assertion
[0];
1077 // If sending to the shared terms database, it's also simple
1078 if (toTheoryId
== THEORY_BUILTIN
) {
1079 Assert(atom
.getKind() == kind::EQUAL
)
1080 << "atom should be an EQUALity, not `" << atom
<< "'";
1081 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1082 d_sharedTerms
.assertEquality(atom
, polarity
, assertion
);
1087 // Things from the SAT solver are already normalized, so they go
1088 // directly to the apropriate theory
1089 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
1090 // We know that this is normalized, so just send it off to the theory
1091 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1092 // Is it preregistered
1093 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
1095 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
1096 // Mark that we have more information
1097 d_factsAsserted
= true;
1102 // Propagations to the SAT solver are just enqueued for pickup by
1103 // the SAT solver later
1104 if (toTheoryId
== THEORY_SAT_SOLVER
) {
1105 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1106 // Enqueue for propagation to the SAT solver
1107 d_propagatedLiterals
.push_back(assertion
);
1108 // Check for propositional conflicts
1110 if (d_propEngine
->hasValue(assertion
, value
) && !value
) {
1111 Trace("theory::propagate")
1112 << "TheoryEngine::assertToTheory(" << assertion
<< ", "
1113 << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (sharing)"
1115 Trace("dtview::conflict")
1116 << ":THEORY-CONFLICT: " << assertion
<< std::endl
;
1117 d_inConflict
= true;
1123 Assert(atom
.getKind() == kind::EQUAL
);
1126 Node normalizedLiteral
= Rewriter::rewrite(assertion
);
1128 // See if it rewrites false directly -> conflict
1129 if (normalizedLiteral
.isConst()) {
1130 if (!normalizedLiteral
.getConst
<bool>()) {
1131 // Mark the propagation for explanations
1132 if (markPropagation(normalizedLiteral
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1133 // Get the explanation (conflict will figure out where it came from)
1134 conflict(normalizedLiteral
, toTheoryId
);
1142 // Try and assert (note that we assert the non-normalized one)
1143 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1144 // Check if has been pre-registered with the theory
1145 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
1147 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
1148 d_factsAsserted
= true;
1154 void TheoryEngine::assertFact(TNode literal
)
1156 Trace("theory") << "TheoryEngine::assertFact(" << literal
<< ")" << endl
;
1160 // If we're in conflict, nothing to do
1166 bool polarity
= literal
.getKind() != kind::NOT
;
1167 TNode atom
= polarity
? literal
: literal
[0];
1169 if (d_logicInfo
.isSharingEnabled()) {
1171 // If any shared terms, it's time to do sharing work
1172 if (d_sharedTerms
.hasSharedTerms(atom
)) {
1173 // Notify the theories the shared terms
1174 SharedTermsDatabase::shared_terms_iterator it
= d_sharedTerms
.begin(atom
);
1175 SharedTermsDatabase::shared_terms_iterator it_end
= d_sharedTerms
.end(atom
);
1176 for (; it
!= it_end
; ++ it
) {
1178 Theory::Set theories
= d_sharedTerms
.getTheoriesToNotify(atom
, term
);
1179 for (TheoryId id
= THEORY_FIRST
; id
!= THEORY_LAST
; ++ id
) {
1180 if (Theory::setContains(id
, theories
)) {
1181 theoryOf(id
)->addSharedTermInternal(term
);
1184 d_sharedTerms
.markNotified(term
, theories
);
1188 // If it's an equality, assert it to the shared term manager, even though the terms are not
1189 // yet shared. As the terms become shared later, the shared terms manager will then add them
1190 // to the assert the equality to the interested theories
1191 if (atom
.getKind() == kind::EQUAL
) {
1192 // Assert it to the the owning theory
1193 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1194 // Shared terms manager will assert to interested theories directly, as the terms become shared
1195 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ THEORY_SAT_SOLVER
);
1197 // Now, let's check for any atom triggers from lemmas
1198 AtomRequests::atom_iterator it
= d_atomRequests
.getAtomIterator(atom
);
1199 while (!it
.done()) {
1200 const AtomRequests::Request
& request
= it
.get();
1202 polarity
? (Node
)request
.d_atom
: request
.d_atom
.notNode();
1203 Debug("theory::atoms") << "TheoryEngine::assertFact(" << literal
<< "): sending requested " << toAssert
<< endl
;
1205 toAssert
, literal
, request
.d_toTheory
, THEORY_SAT_SOLVER
);
1210 // Not an equality, just assert to the appropriate theory
1211 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1214 // Assert the fact to the appropriate theory directly
1215 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1219 bool TheoryEngine::propagate(TNode literal
, theory::TheoryId theory
) {
1221 Debug("theory::propagate") << "TheoryEngine::propagate(" << literal
<< ", " << theory
<< ")" << endl
;
1223 Trace("dtview::prop") << std::string(d_context
->getLevel(), ' ')
1224 << ":THEORY-PROP: " << literal
<< endl
;
1228 if(Dump
.isOn("t-propagations")) {
1229 Dump("t-propagations") << CommentCommand("negation of theory propagation: expect valid")
1230 << QueryCommand(literal
.toExpr());
1232 if(Dump
.isOn("missed-t-propagations")) {
1233 d_hasPropagated
.insert(literal
);
1237 bool polarity
= literal
.getKind() != kind::NOT
;
1238 TNode atom
= polarity
? literal
: literal
[0];
1240 if (d_logicInfo
.isSharingEnabled() && atom
.getKind() == kind::EQUAL
) {
1241 if (d_propEngine
->isSatLiteral(literal
)) {
1242 // We propagate SAT literals to SAT
1243 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1245 if (theory
!= THEORY_BUILTIN
) {
1246 // Assert to the shared terms database
1247 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ theory
);
1250 // We could be propagating a unit-clause lemma. In this case, we need to provide a
1252 // TODO: Consider putting this someplace else? This is the only refence to the proof
1253 // manager in this class.
1256 LemmaProofRecipe proofRecipe
;
1257 proofRecipe
.addBaseAssertion(literal
);
1260 LemmaProofRecipe::ProofStep
proofStep(theory
, emptyNode
);
1261 proofStep
.addAssertion(literal
);
1262 proofRecipe
.addStep(proofStep
);
1264 ProofManager::getCnfProof()->setProofRecipe(&proofRecipe
);
1267 // Just send off to the SAT solver
1268 Assert(d_propEngine
->isSatLiteral(literal
));
1269 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1272 return !d_inConflict
;
1275 const LogicInfo
& TheoryEngine::getLogicInfo() const { return d_logicInfo
; }
1277 theory::EqualityStatus
TheoryEngine::getEqualityStatus(TNode a
, TNode b
) {
1278 Assert(a
.getType().isComparableTo(b
.getType()));
1279 if (d_sharedTerms
.isShared(a
) && d_sharedTerms
.isShared(b
)) {
1280 if (d_sharedTerms
.areEqual(a
,b
)) {
1281 return EQUALITY_TRUE_AND_PROPAGATED
;
1283 else if (d_sharedTerms
.areDisequal(a
,b
)) {
1284 return EQUALITY_FALSE_AND_PROPAGATED
;
1287 return theoryOf(Theory::theoryOf(a
.getType()))->getEqualityStatus(a
, b
);
1290 Node
TheoryEngine::getModelValue(TNode var
) {
1293 // the model value of a constant must be itself
1296 Assert(d_sharedTerms
.isShared(var
));
1297 return theoryOf(Theory::theoryOf(var
.getType()))->getModelValue(var
);
1301 Node
TheoryEngine::ensureLiteral(TNode n
) {
1302 Debug("ensureLiteral") << "rewriting: " << n
<< std::endl
;
1303 Node rewritten
= Rewriter::rewrite(n
);
1304 Debug("ensureLiteral") << " got: " << rewritten
<< std::endl
;
1305 Node preprocessed
= preprocess(rewritten
);
1306 Debug("ensureLiteral") << "preprocessed: " << preprocessed
<< std::endl
;
1307 d_propEngine
->ensureLiteral(preprocessed
);
1308 return preprocessed
;
1312 void TheoryEngine::printInstantiations( std::ostream
& out
) {
1313 if( d_quantEngine
){
1314 d_quantEngine
->printInstantiations( out
);
1316 out
<< "Internal error : instantiations not available when quantifiers are not present." << std::endl
;
1321 void TheoryEngine::printSynthSolution( std::ostream
& out
) {
1322 if( d_quantEngine
){
1323 d_quantEngine
->printSynthSolution( out
);
1325 out
<< "Internal error : synth solution not available when quantifiers are not present." << std::endl
;
1330 void TheoryEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1331 if( d_quantEngine
){
1332 d_quantEngine
->getInstantiatedQuantifiedFormulas( qs
);
1338 void TheoryEngine::getInstantiations( Node q
, std::vector
< Node
>& insts
) {
1339 if( d_quantEngine
){
1340 d_quantEngine
->getInstantiations( q
, insts
);
1346 void TheoryEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1347 if( d_quantEngine
){
1348 d_quantEngine
->getInstantiationTermVectors( q
, tvecs
);
1354 void TheoryEngine::getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
) {
1355 if( d_quantEngine
){
1356 d_quantEngine
->getInstantiations( insts
);
1362 void TheoryEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1363 if( d_quantEngine
){
1364 d_quantEngine
->getInstantiationTermVectors( insts
);
1370 Node
TheoryEngine::getInstantiatedConjunction( Node q
) {
1371 if( d_quantEngine
){
1372 return d_quantEngine
->getInstantiatedConjunction( q
);
1375 return Node::null();
1380 static Node
mkExplanation(const std::vector
<NodeTheoryPair
>& explanation
) {
1382 std::set
<TNode
> all
;
1383 for (unsigned i
= 0; i
< explanation
.size(); ++ i
) {
1384 Assert(explanation
[i
].d_theory
== THEORY_SAT_SOLVER
);
1385 all
.insert(explanation
[i
].d_node
);
1388 if (all
.size() == 0) {
1389 // Normalize to true
1390 return NodeManager::currentNM()->mkConst
<bool>(true);
1393 if (all
.size() == 1) {
1394 // All the same, or just one
1395 return explanation
[0].d_node
;
1398 NodeBuilder
<> conjunction(kind::AND
);
1399 std::set
<TNode
>::const_iterator it
= all
.begin();
1400 std::set
<TNode
>::const_iterator it_end
= all
.end();
1401 while (it
!= it_end
) {
1409 Node
TheoryEngine::getExplanationAndRecipe(TNode node
, LemmaProofRecipe
* proofRecipe
) {
1410 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< "): current propagation index = " << d_propagationMapTimestamp
<< endl
;
1412 bool polarity
= node
.getKind() != kind::NOT
;
1413 TNode atom
= polarity
? node
: node
[0];
1415 // If we're not in shared mode, explanations are simple
1416 if (!d_logicInfo
.isSharingEnabled()) {
1417 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing is NOT enabled. "
1418 << " Responsible theory is: "
1419 << theoryOf(atom
)->getId() << std::endl
;
1421 TrustNode texplanation
= theoryOf(atom
)->explain(node
);
1422 Node explanation
= texplanation
.getNode();
1423 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< ") => " << explanation
<< endl
;
1427 LemmaProofRecipe::ProofStep
proofStep(theoryOf(atom
)->getId(), emptyNode
);
1428 proofStep
.addAssertion(node
);
1429 proofRecipe
->addBaseAssertion(node
);
1431 if (explanation
.getKind() == kind::AND
) {
1432 // If the explanation is a conjunction, the recipe for the corresponding lemma is
1433 // the negation of its conjuncts.
1434 Node flat
= flattenAnd(explanation
);
1435 for (unsigned i
= 0; i
< flat
.getNumChildren(); ++i
) {
1436 if (flat
[i
].isConst() && flat
[i
].getConst
<bool>()) {
1440 if (flat
[i
].getKind() == kind::NOT
&&
1441 flat
[i
][0].isConst() && !flat
[i
][0].getConst
<bool>()) {
1445 Debug("theory::explain") << "TheoryEngine::getExplanationAndRecipe: adding recipe assertion: "
1446 << flat
[i
].negate() << std::endl
;
1447 proofStep
.addAssertion(flat
[i
].negate());
1448 proofRecipe
->addBaseAssertion(flat
[i
].negate());
1451 // The recipe for proving it is by negating it. "True" is not an acceptable reason.
1452 if (!((explanation
.isConst() && explanation
.getConst
<bool>()) ||
1453 (explanation
.getKind() == kind::NOT
&&
1454 explanation
[0].isConst() && !explanation
[0].getConst
<bool>()))) {
1455 proofStep
.addAssertion(explanation
.negate());
1456 proofRecipe
->addBaseAssertion(explanation
.negate());
1460 proofRecipe
->addStep(proofStep
);
1467 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing IS enabled" << std::endl
;
1469 // Initial thing to explain
1470 NodeTheoryPair
toExplain(node
, THEORY_SAT_SOLVER
, d_propagationMapTimestamp
);
1471 Assert(d_propagationMap
.find(toExplain
) != d_propagationMap
.end());
1473 NodeTheoryPair nodeExplainerPair
= d_propagationMap
[toExplain
];
1474 Debug("theory::explain")
1475 << "TheoryEngine::getExplanation: explainer for node "
1476 << nodeExplainerPair
.d_node
1477 << " is theory: " << nodeExplainerPair
.d_theory
<< std::endl
;
1478 TheoryId explainer
= nodeExplainerPair
.d_theory
;
1480 // Create the workplace for explanations
1481 std::vector
<NodeTheoryPair
> explanationVector
;
1482 explanationVector
.push_back(d_propagationMap
[toExplain
]);
1483 // Process the explanation
1486 LemmaProofRecipe::ProofStep
proofStep(explainer
, emptyNode
);
1487 proofStep
.addAssertion(node
);
1488 proofRecipe
->addStep(proofStep
);
1489 proofRecipe
->addBaseAssertion(node
);
1492 getExplanation(explanationVector
, proofRecipe
);
1493 Node explanation
= mkExplanation(explanationVector
);
1495 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< ") => " << explanation
<< endl
;
1500 Node
TheoryEngine::getExplanation(TNode node
) {
1501 LemmaProofRecipe
*dontCareRecipe
= NULL
;
1502 return getExplanationAndRecipe(node
, dontCareRecipe
);
1505 struct AtomsCollect
{
1507 std::vector
<TNode
> d_atoms
;
1508 std::unordered_set
<TNode
, TNodeHashFunction
> d_visited
;
1512 typedef void return_type
;
1514 bool alreadyVisited(TNode current
, TNode parent
) {
1515 // Check if already visited
1516 if (d_visited
.find(current
) != d_visited
.end()) return true;
1517 // Don't visit non-boolean
1518 if (!current
.getType().isBoolean()) return true;
1523 void visit(TNode current
, TNode parent
) {
1524 if (Theory::theoryOf(current
) != theory::THEORY_BOOL
) {
1525 d_atoms
.push_back(current
);
1527 d_visited
.insert(current
);
1530 void start(TNode node
) {}
1531 void done(TNode node
) {}
1533 std::vector
<TNode
> getAtoms() const {
1538 void TheoryEngine::ensureLemmaAtoms(const std::vector
<TNode
>& atoms
, theory::TheoryId atomsTo
) {
1539 for (unsigned i
= 0; i
< atoms
.size(); ++ i
) {
1541 // Non-equality atoms are either owned by theory or they don't make sense
1542 if (atoms
[i
].getKind() != kind::EQUAL
) {
1548 // Simple normalization to not repeat stuff
1549 if (eq
[0] > eq
[1]) {
1550 eq
= eq
[1].eqNode(eq
[0]);
1553 // Rewrite the equality
1554 Node eqNormalized
= Rewriter::rewrite(atoms
[i
]);
1556 Debug("theory::atoms") << "TheoryEngine::ensureLemmaAtoms(): " << eq
<< " with nf " << eqNormalized
<< endl
;
1558 // If the equality is a boolean constant, we send immediately
1559 if (eqNormalized
.isConst()) {
1560 if (eqNormalized
.getConst
<bool>()) {
1561 assertToTheory(eq
, eqNormalized
, /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1563 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1566 }else if( eqNormalized
.getKind() != kind::EQUAL
){
1567 Assert(eqNormalized
.getKind() == kind::BOOLEAN_TERM_VARIABLE
1568 || (eqNormalized
.getKind() == kind::NOT
1569 && eqNormalized
[0].getKind() == kind::BOOLEAN_TERM_VARIABLE
));
1570 // this happens for Boolean term equalities V = true that are rewritten to V, we should skip
1571 // TODO : revisit this
1575 // If the normalization did the just flips, keep the flip
1576 if (eqNormalized
[0] == eq
[1] && eqNormalized
[1] == eq
[0]) {
1580 // Check if the equality is already known by the sat solver
1581 if (d_propEngine
->isSatLiteral(eqNormalized
)) {
1583 if (d_propEngine
->hasValue(eqNormalized
, value
)) {
1585 assertToTheory(eq
, eqNormalized
, atomsTo
, theory::THEORY_SAT_SOLVER
);
1588 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), atomsTo
, theory::THEORY_SAT_SOLVER
);
1594 // If the theory is asking about a different form, or the form is ok but if will go to a different theory
1595 // then we must figure it out
1596 if (eqNormalized
!= eq
|| Theory::theoryOf(eq
) != atomsTo
) {
1597 // If you get eqNormalized, send atoms[i] to atomsTo
1598 d_atomRequests
.add(eqNormalized
, eq
, atomsTo
);
1603 theory::LemmaStatus
TheoryEngine::lemma(TNode node
,
1606 theory::LemmaProperty p
,
1607 theory::TheoryId atomsTo
)
1609 // For resource-limiting (also does a time check).
1612 // Do we need to check atoms
1613 if (atomsTo
!= theory::THEORY_LAST
) {
1614 Debug("theory::atoms") << "TheoryEngine::lemma(" << node
<< ", " << atomsTo
<< ")" << endl
;
1615 AtomsCollect collectAtoms
;
1616 NodeVisitor
<AtomsCollect
>::run(collectAtoms
, node
);
1617 ensureLemmaAtoms(collectAtoms
.getAtoms(), atomsTo
);
1620 if(Dump
.isOn("t-lemmas")) {
1625 Dump("t-lemmas") << CommentCommand("theory lemma: expect valid")
1626 << CheckSatCommand(n
.toExpr());
1628 bool removable
= isLemmaPropertyRemovable(p
);
1629 bool preprocess
= isLemmaPropertyPreprocess(p
);
1631 // call preprocessor
1632 std::vector
<TrustNode
> newLemmas
;
1633 std::vector
<Node
> newSkolems
;
1634 TrustNode tlemma
= d_tpp
.preprocess(node
, newLemmas
, newSkolems
, preprocess
);
1636 // must use an assertion pipeline due to decision engine below
1637 AssertionPipeline lemmas
;
1638 // make the assertion pipeline
1639 lemmas
.push_back(tlemma
.getNode());
1640 lemmas
.updateRealAssertionsEnd();
1641 Assert(newSkolems
.size() == newLemmas
.size());
1642 for (size_t i
= 0, nsize
= newLemmas
.size(); i
< nsize
; i
++)
1644 // store skolem mapping here
1645 IteSkolemMap
& imap
= lemmas
.getIteSkolemMap();
1646 imap
[newSkolems
[i
]] = lemmas
.size();
1647 lemmas
.push_back(newLemmas
[i
].getNode());
1650 // assert lemmas to prop engine
1651 for (size_t i
= 0, lsize
= lemmas
.size(); i
< lsize
; ++i
)
1653 d_propEngine
->assertLemma(
1654 lemmas
[i
], i
== 0 && negated
, removable
, rule
, node
);
1657 // WARNING: Below this point don't assume lemmas[0] to be not negated.
1659 lemmas
.replace(0, lemmas
[0].notNode());
1663 // assert to decision engine
1666 d_propEngine
->addAssertionsToDecisionEngine(lemmas
);
1669 // Mark that we added some lemmas
1670 d_lemmasAdded
= true;
1672 // Lemma analysis isn't online yet; this lemma may only live for this
1674 Node retLemma
= lemmas
[0];
1675 if (lemmas
.size() > 1)
1677 // the returned lemma is the conjunction of all additional lemmas.
1678 retLemma
= NodeManager::currentNM()->mkNode(kind::AND
, lemmas
.ref());
1680 return theory::LemmaStatus(retLemma
, d_userContext
->getLevel());
1683 void TheoryEngine::conflict(TNode conflict
, TheoryId theoryId
) {
1685 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", " << theoryId
<< ")" << endl
;
1687 Trace("dtview::conflict") << ":THEORY-CONFLICT: " << conflict
<< std::endl
;
1689 // Mark that we are in conflict
1690 d_inConflict
= true;
1692 if(Dump
.isOn("t-conflicts")) {
1693 Dump("t-conflicts") << CommentCommand("theory conflict: expect unsat")
1694 << CheckSatCommand(conflict
.toExpr());
1697 LemmaProofRecipe
* proofRecipe
= NULL
;
1699 proofRecipe
= new LemmaProofRecipe
;
1701 LemmaProofRecipe::ProofStep
proofStep(theoryId
, emptyNode
);
1703 if (conflict
.getKind() == kind::AND
) {
1704 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++i
) {
1705 proofStep
.addAssertion(conflict
[i
].negate());
1708 proofStep
.addAssertion(conflict
.negate());
1711 proofRecipe
->addStep(proofStep
);
1714 // In the multiple-theories case, we need to reconstruct the conflict
1715 if (d_logicInfo
.isSharingEnabled()) {
1716 // Create the workplace for explanations
1717 std::vector
<NodeTheoryPair
> explanationVector
;
1718 explanationVector
.push_back(NodeTheoryPair(conflict
, theoryId
, d_propagationMapTimestamp
));
1720 // Process the explanation
1721 getExplanation(explanationVector
, proofRecipe
);
1722 PROOF(ProofManager::getCnfProof()->setProofRecipe(proofRecipe
));
1723 Node fullConflict
= mkExplanation(explanationVector
);
1724 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", " << theoryId
<< "): full = " << fullConflict
<< endl
;
1725 Assert(properConflict(fullConflict
));
1729 LemmaProperty::REMOVABLE
,
1732 // When only one theory, the conflict should need no processing
1733 Assert(properConflict(conflict
));
1735 if (conflict
.getKind() == kind::AND
) {
1736 // If the conflict is a conjunction, the corresponding lemma is derived by negating
1738 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++i
) {
1739 if (conflict
[i
].isConst() && conflict
[i
].getConst
<bool>()) {
1743 if (conflict
[i
].getKind() == kind::NOT
&&
1744 conflict
[i
][0].isConst() && !conflict
[i
][0].getConst
<bool>()) {
1748 proofRecipe
->getStep(0)->addAssertion(conflict
[i
].negate());
1749 proofRecipe
->addBaseAssertion(conflict
[i
].negate());
1752 proofRecipe
->getStep(0)->addAssertion(conflict
.negate());
1753 proofRecipe
->addBaseAssertion(conflict
.negate());
1756 ProofManager::getCnfProof()->setProofRecipe(proofRecipe
);
1759 lemma(conflict
, RULE_CONFLICT
, true, LemmaProperty::REMOVABLE
, THEORY_LAST
);
1768 void TheoryEngine::staticInitializeBVOptions(
1769 const std::vector
<Node
>& assertions
)
1771 bool useSlicer
= true;
1772 if (options::bitvectorEqualitySlicer() == options::BvSlicerMode::ON
)
1774 if (!d_logicInfo
.isPure(theory::THEORY_BV
) || d_logicInfo
.isQuantified())
1775 throw ModalException(
1776 "Slicer currently only supports pure QF_BV formulas. Use "
1777 "--bv-eq-slicer=off");
1778 if (options::incrementalSolving())
1779 throw ModalException(
1780 "Slicer does not currently support incremental mode. Use "
1781 "--bv-eq-slicer=off");
1782 if (options::produceModels())
1783 throw ModalException(
1784 "Slicer does not currently support model generation. Use "
1785 "--bv-eq-slicer=off");
1787 else if (options::bitvectorEqualitySlicer() == options::BvSlicerMode::OFF
)
1791 else if (options::bitvectorEqualitySlicer() == options::BvSlicerMode::AUTO
)
1793 if ((!d_logicInfo
.isPure(theory::THEORY_BV
) || d_logicInfo
.isQuantified())
1794 || options::incrementalSolving()
1795 || options::produceModels())
1798 bv::utils::TNodeBoolMap cache
;
1799 for (unsigned i
= 0; i
< assertions
.size(); ++i
)
1801 useSlicer
= useSlicer
&& bv::utils::isCoreTerm(assertions
[i
], cache
);
1807 bv::TheoryBV
* bv_theory
= (bv::TheoryBV
*)d_theoryTable
[THEORY_BV
];
1808 bv_theory
->enableCoreTheorySlicer();
1812 SharedTermsDatabase
* TheoryEngine::getSharedTermsDatabase()
1814 return &d_sharedTerms
;
1817 void TheoryEngine::getExplanation(std::vector
<NodeTheoryPair
>& explanationVector
, LemmaProofRecipe
* proofRecipe
) {
1818 Assert(explanationVector
.size() > 0);
1820 unsigned i
= 0; // Index of the current literal we are processing
1821 unsigned j
= 0; // Index of the last literal we are keeping
1823 std::unique_ptr
<std::set
<Node
>> inputAssertions
= nullptr;
1827 inputAssertions
.reset(
1828 new std::set
<Node
>(proofRecipe
->getStep(0)->getAssertions()));
1831 // cache of nodes we have already explained by some theory
1832 std::unordered_map
<Node
, size_t, NodeHashFunction
> cache
;
1834 while (i
< explanationVector
.size()) {
1835 // Get the current literal to explain
1836 NodeTheoryPair toExplain
= explanationVector
[i
];
1838 Debug("theory::explain")
1839 << "[i=" << i
<< "] TheoryEngine::explain(): processing ["
1840 << toExplain
.d_timestamp
<< "] " << toExplain
.d_node
<< " sent from "
1841 << toExplain
.d_theory
<< endl
;
1843 if (cache
.find(toExplain
.d_node
) != cache
.end()
1844 && cache
[toExplain
.d_node
] < toExplain
.d_timestamp
)
1849 cache
[toExplain
.d_node
] = toExplain
.d_timestamp
;
1851 // If a true constant or a negation of a false constant we can ignore it
1852 if (toExplain
.d_node
.isConst() && toExplain
.d_node
.getConst
<bool>())
1857 if (toExplain
.d_node
.getKind() == kind::NOT
&& toExplain
.d_node
[0].isConst()
1858 && !toExplain
.d_node
[0].getConst
<bool>())
1864 // If from the SAT solver, keep it
1865 if (toExplain
.d_theory
== THEORY_SAT_SOLVER
)
1867 Debug("theory::explain") << "\tLiteral came from THEORY_SAT_SOLVER. Kepping it." << endl
;
1868 explanationVector
[j
++] = explanationVector
[i
++];
1872 // If an and, expand it
1873 if (toExplain
.d_node
.getKind() == kind::AND
)
1875 Debug("theory::explain")
1876 << "TheoryEngine::explain(): expanding " << toExplain
.d_node
1877 << " got from " << toExplain
.d_theory
<< endl
;
1878 for (unsigned k
= 0; k
< toExplain
.d_node
.getNumChildren(); ++k
)
1880 NodeTheoryPair
newExplain(
1881 toExplain
.d_node
[k
], toExplain
.d_theory
, toExplain
.d_timestamp
);
1882 explanationVector
.push_back(newExplain
);
1888 // See if it was sent to the theory by another theory
1889 PropagationMap::const_iterator find
= d_propagationMap
.find(toExplain
);
1890 if (find
!= d_propagationMap
.end()) {
1891 Debug("theory::explain")
1892 << "\tTerm was propagated by another theory (theory = "
1893 << getTheoryString((*find
).second
.d_theory
) << ")" << std::endl
;
1894 // There is some propagation, check if its a timely one
1895 if ((*find
).second
.d_timestamp
< toExplain
.d_timestamp
)
1897 Debug("theory::explain")
1898 << "\tRelevant timetsamp, pushing " << (*find
).second
.d_node
1899 << "to index = " << explanationVector
.size() << std::endl
;
1900 explanationVector
.push_back((*find
).second
);
1904 if (toExplain
.d_node
!= (*find
).second
.d_node
)
1906 Debug("pf::explain")
1907 << "TheoryEngine::getExplanation: Rewrite alert! toAssert = "
1908 << toExplain
.d_node
<< ", toExplain = " << (*find
).second
.d_node
1913 proofRecipe
->addRewriteRule(toExplain
.d_node
,
1914 (*find
).second
.d_node
);
1924 if (toExplain
.d_theory
== THEORY_BUILTIN
)
1926 explanation
= d_sharedTerms
.explain(toExplain
.d_node
);
1927 Debug("theory::explain") << "\tTerm was propagated by THEORY_BUILTIN. Explanation: " << explanation
<< std::endl
;
1931 TrustNode texp
= theoryOf(toExplain
.d_theory
)->explain(toExplain
.d_node
);
1932 explanation
= texp
.getNode();
1933 Debug("theory::explain") << "\tTerm was propagated by owner theory: "
1934 << theoryOf(toExplain
.d_theory
)->getId()
1935 << ". Explanation: " << explanation
<< std::endl
;
1938 Debug("theory::explain")
1939 << "TheoryEngine::explain(): got explanation " << explanation
1940 << " got from " << toExplain
.d_theory
<< endl
;
1941 Assert(explanation
!= toExplain
.d_node
)
1942 << "wasn't sent to you, so why are you explaining it trivially";
1943 // Mark the explanation
1944 NodeTheoryPair
newExplain(
1945 explanation
, toExplain
.d_theory
, toExplain
.d_timestamp
);
1946 explanationVector
.push_back(newExplain
);
1951 if (proofRecipe
&& inputAssertions
)
1953 // If we're expanding the target node of the explanation (this is the
1954 // first expansion...), we don't want to add it as a separate proof
1955 // step. It is already part of the assertions.
1956 if (!ContainsKey(*inputAssertions
, toExplain
.d_node
))
1958 LemmaProofRecipe::ProofStep
proofStep(toExplain
.d_theory
,
1960 if (explanation
.getKind() == kind::AND
)
1962 Node flat
= flattenAnd(explanation
);
1963 for (unsigned k
= 0; k
< flat
.getNumChildren(); ++k
)
1965 // If a true constant or a negation of a false constant we can
1967 if (!((flat
[k
].isConst() && flat
[k
].getConst
<bool>())
1968 || (flat
[k
].getKind() == kind::NOT
&& flat
[k
][0].isConst()
1969 && !flat
[k
][0].getConst
<bool>())))
1971 proofStep
.addAssertion(flat
[k
].negate());
1977 if (!((explanation
.isConst() && explanation
.getConst
<bool>())
1978 || (explanation
.getKind() == kind::NOT
1979 && explanation
[0].isConst()
1980 && !explanation
[0].getConst
<bool>())))
1982 proofStep
.addAssertion(explanation
.negate());
1985 proofRecipe
->addStep(proofStep
);
1991 // Keep only the relevant literals
1992 explanationVector
.resize(j
);
1996 // The remaining literals are the base of the proof
1997 for (unsigned k
= 0; k
< explanationVector
.size(); ++k
) {
1998 proofRecipe
->addBaseAssertion(explanationVector
[k
].d_node
.negate());
2004 void TheoryEngine::setUserAttribute(const std::string
& attr
,
2006 const std::vector
<Node
>& node_values
,
2007 const std::string
& str_value
)
2009 Trace("te-attr") << "set user attribute " << attr
<< " " << n
<< endl
;
2010 if( d_attr_handle
.find( attr
)!=d_attr_handle
.end() ){
2011 for( size_t i
=0; i
<d_attr_handle
[attr
].size(); i
++ ){
2012 d_attr_handle
[attr
][i
]->setUserAttribute(attr
, n
, node_values
, str_value
);
2015 //unhandled exception?
2019 void TheoryEngine::handleUserAttribute(const char* attr
, Theory
* t
) {
2020 Trace("te-attr") << "Handle user attribute " << attr
<< " " << t
<< endl
;
2021 std::string
str( attr
);
2022 d_attr_handle
[ str
].push_back( t
);
2025 void TheoryEngine::checkTheoryAssertionsWithModel(bool hardFailure
) {
2026 for(TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
2027 Theory
* theory
= d_theoryTable
[theoryId
];
2028 if(theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
2029 for(context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
2030 it_end
= theory
->facts_end();
2033 Node assertion
= (*it
).d_assertion
;
2034 Node val
= getModel()->getValue(assertion
);
2037 std::stringstream ss
;
2038 ss
<< " " << theoryId
2039 << " has an asserted fact that the model doesn't satisfy." << endl
2040 << "The fact: " << assertion
<< endl
2041 << "Model value: " << val
<< endl
;
2046 // Always an error if it is false
2047 InternalError() << ss
.str();
2051 // Otherwise just a warning. Notice this case may happen for
2052 // assertions with unevaluable operators, e.g. transcendental
2053 // functions. It also may happen for separation logic, where
2054 // check-model support is limited.
2055 Warning() << ss
.str();
2064 std::pair
<bool, Node
> TheoryEngine::entailmentCheck(options::TheoryOfMode mode
,
2067 TNode atom
= (lit
.getKind() == kind::NOT
) ? lit
[0] : lit
;
2068 if( atom
.getKind()==kind::AND
|| atom
.getKind()==kind::OR
|| atom
.getKind()==kind::IMPLIES
){
2069 //Boolean connective, recurse
2070 std::vector
< Node
> children
;
2071 bool pol
= (lit
.getKind()!=kind::NOT
);
2072 bool is_conjunction
= pol
==(lit
.getKind()==kind::AND
);
2073 for( unsigned i
=0; i
<atom
.getNumChildren(); i
++ ){
2075 if( pol
==( lit
.getKind()==kind::IMPLIES
&& i
==0 ) ){
2076 ch
= atom
[i
].negate();
2078 std::pair
<bool, Node
> chres
= entailmentCheck(mode
, ch
);
2080 if( !is_conjunction
){
2083 children
.push_back( chres
.second
);
2085 }else if( !chres
.first
&& is_conjunction
){
2086 return std::pair
<bool, Node
>(false, Node::null());
2089 if( is_conjunction
){
2090 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, children
));
2092 return std::pair
<bool, Node
>(false, Node::null());
2094 }else if( atom
.getKind()==kind::ITE
|| ( atom
.getKind()==kind::EQUAL
&& atom
[0].getType().isBoolean() ) ){
2095 bool pol
= (lit
.getKind()!=kind::NOT
);
2096 for( unsigned r
=0; r
<2; r
++ ){
2101 std::pair
<bool, Node
> chres
= entailmentCheck(mode
, ch
);
2103 Node ch2
= atom
[ atom
.getKind()==kind::ITE
? r
+1 : 1 ];
2104 if( pol
==( atom
.getKind()==kind::ITE
? true : r
==1 ) ){
2107 std::pair
<bool, Node
> chres2
= entailmentCheck(mode
, ch2
);
2109 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, chres
.second
, chres2
.second
));
2115 return std::pair
<bool, Node
>(false, Node::null());
2117 //it is a theory atom
2118 theory::TheoryId tid
= theory::Theory::theoryOf(mode
, atom
);
2119 theory::Theory
* th
= theoryOf(tid
);
2122 Trace("theory-engine-entc") << "Entailment check : " << lit
<< std::endl
;
2124 std::pair
<bool, Node
> chres
= th
->entailmentCheck(lit
);
2129 void TheoryEngine::spendResource(ResourceManager::Resource r
)
2131 d_resourceManager
->spendResource(r
);
2134 }/* CVC4 namespace */