1 /********************* */
2 /*! \file theory_engine.cpp
4 ** Top contributors (to current version):
5 ** Dejan Jovanovic, Morgan Deters, Andrew Reynolds
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 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 "options/bv_options.h"
29 #include "options/options.h"
30 #include "options/proof_options.h"
31 #include "options/quantifiers_options.h"
32 #include "preprocessing/assertion_pipeline.h"
33 #include "proof/cnf_proof.h"
34 #include "proof/lemma_proof.h"
35 #include "proof/proof_manager.h"
36 #include "proof/theory_proof.h"
37 #include "smt/logic_exception.h"
38 #include "smt/term_formula_removal.h"
39 #include "smt_util/lemma_output_channel.h"
40 #include "smt_util/node_visitor.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/quantifiers/first_order_model.h"
45 #include "theory/quantifiers/fmf/model_engine.h"
46 #include "theory/quantifiers/theory_quantifiers.h"
47 #include "theory/quantifiers_engine.h"
48 #include "theory/rewriter.h"
49 #include "theory/theory.h"
50 #include "theory/theory_model.h"
51 #include "theory/theory_traits.h"
52 #include "theory/uf/equality_engine.h"
53 #include "util/resource_manager.h"
57 using namespace CVC4::preprocessing
;
58 using namespace CVC4::theory
;
62 inline void flattenAnd(Node n
, std::vector
<TNode
>& out
){
63 Assert(n
.getKind() == kind::AND
);
64 for(Node::iterator i
=n
.begin(), i_end
=n
.end(); i
!= i_end
; ++i
){
66 if(curr
.getKind() == kind::AND
){
67 flattenAnd(curr
, out
);
74 inline Node
flattenAnd(Node n
){
75 std::vector
<TNode
> out
;
77 return NodeManager::currentNM()->mkNode(kind::AND
, out
);
80 theory::LemmaStatus
TheoryEngine::EngineOutputChannel::lemma(TNode lemma
,
85 Debug("theory::lemma") << "EngineOutputChannel<" << d_theory
<< ">::lemma("
87 << ", preprocess = " << preprocess
<< std::endl
;
88 ++d_statistics
.lemmas
;
89 d_engine
->d_outputChannelUsed
= true;
91 PROOF({ registerLemmaRecipe(lemma
, lemma
, preprocess
, d_theory
); });
93 theory::LemmaStatus result
=
94 d_engine
->lemma(lemma
, rule
, false, removable
, preprocess
,
95 sendAtoms
? d_theory
: theory::THEORY_LAST
);
99 void TheoryEngine::EngineOutputChannel::registerLemmaRecipe(Node lemma
, Node originalLemma
, bool preprocess
, theory::TheoryId theoryId
) {
100 // During CNF conversion, conjunctions will be broken down into
101 // multiple lemmas. In order for the recipes to match, we have to do
103 NodeManager
* nm
= NodeManager::currentNM();
106 lemma
= d_engine
->preprocess(lemma
);
108 bool negated
= (lemma
.getKind() == kind::NOT
);
109 Node nnLemma
= negated
? lemma
[0] : lemma
;
111 switch (nnLemma
.getKind()) {
115 for (unsigned i
= 0; i
< nnLemma
.getNumChildren(); ++i
)
116 registerLemmaRecipe(nnLemma
[i
], originalLemma
, false, theoryId
);
118 NodeBuilder
<> builder(kind::OR
);
119 for (unsigned i
= 0; i
< nnLemma
.getNumChildren(); ++i
)
120 builder
<< nnLemma
[i
].negate();
122 Node disjunction
= (builder
.getNumChildren() == 1) ? builder
[0] : builder
;
123 registerLemmaRecipe(disjunction
, originalLemma
, false, theoryId
);
128 if( nnLemma
[0].getType().isBoolean() ){
130 registerLemmaRecipe(nm
->mkNode(kind::OR
, nnLemma
[0], nnLemma
[1].negate()), originalLemma
, false, theoryId
);
131 registerLemmaRecipe(nm
->mkNode(kind::OR
, nnLemma
[0].negate(), nnLemma
[1]), originalLemma
, false, theoryId
);
133 registerLemmaRecipe(nm
->mkNode(kind::OR
, nnLemma
[0], nnLemma
[1]), originalLemma
, false, theoryId
);
134 registerLemmaRecipe(nm
->mkNode(kind::OR
, nnLemma
[0].negate(), nnLemma
[1].negate()), originalLemma
, false, theoryId
);
141 registerLemmaRecipe(nm
->mkNode(kind::OR
, nnLemma
[0].negate(), nnLemma
[1]), originalLemma
, false, theoryId
);
142 registerLemmaRecipe(nm
->mkNode(kind::OR
, nnLemma
[0], nnLemma
[2]), originalLemma
, false, theoryId
);
144 registerLemmaRecipe(nm
->mkNode(kind::OR
, nnLemma
[0].negate(), nnLemma
[1].negate()), originalLemma
, false, theoryId
);
145 registerLemmaRecipe(nm
->mkNode(kind::OR
, nnLemma
[0], nnLemma
[2].negate()), originalLemma
, false, theoryId
);
153 // Theory lemmas have one step that proves the empty clause
154 LemmaProofRecipe proofRecipe
;
156 LemmaProofRecipe::ProofStep
proofStep(theoryId
, emptyNode
);
158 // Remember the original lemma, so we can report this later when asked to
159 proofRecipe
.setOriginalLemma(originalLemma
);
161 // Record the assertions and rewrites
163 if (lemma
.getKind() == kind::OR
) {
164 for (unsigned i
= 0; i
< lemma
.getNumChildren(); ++i
) {
165 rewritten
= theory::Rewriter::rewrite(lemma
[i
]);
166 if (rewritten
!= lemma
[i
]) {
167 proofRecipe
.addRewriteRule(lemma
[i
].negate(), rewritten
.negate());
169 proofStep
.addAssertion(lemma
[i
]);
170 proofRecipe
.addBaseAssertion(rewritten
);
173 rewritten
= theory::Rewriter::rewrite(lemma
);
174 if (rewritten
!= lemma
) {
175 proofRecipe
.addRewriteRule(lemma
.negate(), rewritten
.negate());
177 proofStep
.addAssertion(lemma
);
178 proofRecipe
.addBaseAssertion(rewritten
);
180 proofRecipe
.addStep(proofStep
);
181 ProofManager::getCnfProof()->setProofRecipe(&proofRecipe
);
184 theory::LemmaStatus
TheoryEngine::EngineOutputChannel::splitLemma(
185 TNode lemma
, bool removable
) {
186 Debug("theory::lemma") << "EngineOutputChannel<" << d_theory
<< ">::lemma("
187 << lemma
<< ")" << std::endl
;
188 ++d_statistics
.lemmas
;
189 d_engine
->d_outputChannelUsed
= true;
191 Debug("pf::explain") << "TheoryEngine::EngineOutputChannel::splitLemma( "
192 << lemma
<< " )" << std::endl
;
193 theory::LemmaStatus result
=
194 d_engine
->lemma(lemma
, RULE_SPLIT
, false, removable
, false, d_theory
);
198 bool TheoryEngine::EngineOutputChannel::propagate(TNode literal
) {
199 Debug("theory::propagate") << "EngineOutputChannel<" << d_theory
200 << ">::propagate(" << literal
<< ")" << std::endl
;
201 ++d_statistics
.propagations
;
202 d_engine
->d_outputChannelUsed
= true;
203 return d_engine
->propagate(literal
, d_theory
);
206 void TheoryEngine::EngineOutputChannel::conflict(TNode conflictNode
,
207 std::unique_ptr
<Proof
> proof
)
209 Trace("theory::conflict")
210 << "EngineOutputChannel<" << d_theory
<< ">::conflict(" << conflictNode
212 Assert(!proof
); // Theory shouldn't be producing proofs yet
213 ++d_statistics
.conflicts
;
214 d_engine
->d_outputChannelUsed
= true;
215 d_engine
->conflict(conflictNode
, d_theory
);
218 void TheoryEngine::finishInit() {
220 //initialize the quantifiers engine, master equality engine, model, model builder
221 if( d_logicInfo
.isQuantified() ) {
222 // initialize the quantifiers engine
223 d_quantEngine
= new QuantifiersEngine(d_context
, d_userContext
, this);
224 Assert(d_masterEqualityEngine
== 0);
225 d_masterEqualityEngine
= new eq::EqualityEngine(d_masterEENotify
,getSatContext(), "theory::master", false);
227 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
228 if (d_theoryTable
[theoryId
]) {
229 d_theoryTable
[theoryId
]->setQuantifiersEngine(d_quantEngine
);
230 d_theoryTable
[theoryId
]->setMasterEqualityEngine(d_masterEqualityEngine
);
234 d_curr_model_builder
= d_quantEngine
->getModelBuilder();
235 d_curr_model
= d_quantEngine
->getModel();
237 d_curr_model
= new theory::TheoryModel(
238 d_userContext
, "DefaultModel", options::assignFunctionValues());
239 d_aloc_curr_model
= true;
241 //make the default builder, e.g. in the case that the quantifiers engine does not have a model builder
242 if( d_curr_model_builder
==NULL
){
243 d_curr_model_builder
= new theory::TheoryEngineModelBuilder(this);
244 d_aloc_curr_model_builder
= true;
247 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
248 if (d_theoryTable
[theoryId
]) {
249 // set the decision manager for the theory
250 d_theoryTable
[theoryId
]->setDecisionManager(d_decManager
.get());
251 // finish initializing the theory
252 d_theoryTable
[theoryId
]->finishInit();
257 void TheoryEngine::eqNotifyNewClass(TNode t
){
258 if (d_logicInfo
.isQuantified()) {
259 d_quantEngine
->eqNotifyNewClass( t
);
263 void TheoryEngine::eqNotifyPreMerge(TNode t1
, TNode t2
){
264 if (d_logicInfo
.isQuantified()) {
265 d_quantEngine
->eqNotifyPreMerge( t1
, t2
);
269 void TheoryEngine::eqNotifyPostMerge(TNode t1
, TNode t2
){
270 if (d_logicInfo
.isQuantified()) {
271 d_quantEngine
->eqNotifyPostMerge( t1
, t2
);
275 void TheoryEngine::eqNotifyDisequal(TNode t1
, TNode t2
, TNode reason
){
276 if (d_logicInfo
.isQuantified()) {
277 d_quantEngine
->eqNotifyDisequal( t1
, t2
, reason
);
281 TheoryEngine::TheoryEngine(context::Context
* context
,
282 context::UserContext
* userContext
,
283 RemoveTermFormulas
& iteRemover
,
284 const LogicInfo
& logicInfo
,
285 LemmaChannels
* channels
)
286 : d_propEngine(nullptr),
287 d_decisionEngine(nullptr),
289 d_userContext(userContext
),
290 d_logicInfo(logicInfo
),
291 d_sharedTerms(this, context
),
292 d_masterEqualityEngine(nullptr),
293 d_masterEENotify(*this),
294 d_quantEngine(nullptr),
295 d_decManager(new DecisionManager(context
)),
296 d_curr_model(nullptr),
297 d_aloc_curr_model(false),
298 d_curr_model_builder(nullptr),
299 d_aloc_curr_model_builder(false),
300 d_eager_model_building(false),
302 d_possiblePropagations(context
),
303 d_hasPropagated(context
),
304 d_inConflict(context
, false),
306 d_hasShutDown(false),
307 d_incomplete(context
, false),
308 d_propagationMap(context
),
309 d_propagationMapTimestamp(context
, 0),
310 d_propagatedLiterals(context
),
311 d_propagatedLiteralsIndex(context
, 0),
312 d_atomRequests(context
),
313 d_tform_remover(iteRemover
),
314 d_combineTheoriesTime("TheoryEngine::combineTheoriesTime"),
317 d_interrupted(false),
318 d_resourceManager(NodeManager::currentResourceManager()),
319 d_channels(channels
),
320 d_inPreregister(false),
321 d_factsAsserted(context
, false),
322 d_preRegistrationVisitor(this, context
),
323 d_sharedTermsVisitor(d_sharedTerms
),
324 d_theoryAlternatives(),
326 d_arithSubstitutionsAdded("theory::arith::zzz::arith::substitutions", 0)
328 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
;
331 d_theoryTable
[theoryId
] = NULL
;
332 d_theoryOut
[theoryId
] = NULL
;
335 smtStatisticsRegistry()->registerStat(&d_combineTheoriesTime
);
336 d_true
= NodeManager::currentNM()->mkConst
<bool>(true);
337 d_false
= NodeManager::currentNM()->mkConst
<bool>(false);
340 ProofManager::currentPM()->initTheoryProofEngine();
343 smtStatisticsRegistry()->registerStat(&d_arithSubstitutionsAdded
);
346 TheoryEngine::~TheoryEngine() {
347 Assert(d_hasShutDown
);
349 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
350 if(d_theoryTable
[theoryId
] != NULL
) {
351 delete d_theoryTable
[theoryId
];
352 delete d_theoryOut
[theoryId
];
356 if( d_aloc_curr_model_builder
){
357 delete d_curr_model_builder
;
359 if( d_aloc_curr_model
){
363 delete d_quantEngine
;
365 delete d_masterEqualityEngine
;
367 smtStatisticsRegistry()->unregisterStat(&d_combineTheoriesTime
);
368 smtStatisticsRegistry()->unregisterStat(&d_arithSubstitutionsAdded
);
371 void TheoryEngine::interrupt() { d_interrupted
= true; }
372 void TheoryEngine::preRegister(TNode preprocessed
) {
374 Debug("theory") << "TheoryEngine::preRegister( " << preprocessed
<< ")" << std::endl
;
375 if(Dump
.isOn("missed-t-propagations")) {
376 d_possiblePropagations
.push_back(preprocessed
);
378 d_preregisterQueue
.push(preprocessed
);
380 if (!d_inPreregister
) {
381 // We're in pre-register
382 d_inPreregister
= true;
384 // Process the pre-registration queue
385 while (!d_preregisterQueue
.empty()) {
386 // Get the next atom to pre-register
387 preprocessed
= d_preregisterQueue
.front();
388 d_preregisterQueue
.pop();
390 if (d_logicInfo
.isSharingEnabled() && preprocessed
.getKind() == kind::EQUAL
) {
391 // When sharing is enabled, we propagate from the shared terms manager also
392 d_sharedTerms
.addEqualityToPropagate(preprocessed
);
395 // the atom should not have free variables
396 Debug("theory") << "TheoryEngine::preRegister: " << preprocessed
398 Assert(!expr::hasFreeVar(preprocessed
));
399 // Pre-register the terms in the atom
400 Theory::Set theories
= NodeVisitor
<PreRegisterVisitor
>::run(d_preRegistrationVisitor
, preprocessed
);
401 theories
= Theory::setRemove(THEORY_BOOL
, theories
);
402 // Remove the top theory, if any more that means multiple theories were involved
403 bool multipleTheories
= Theory::setRemove(Theory::theoryOf(preprocessed
), theories
);
405 // These checks don't work with finite model finding, because it
406 // uses Rational constants to represent cardinality constraints,
407 // even though arithmetic isn't actually involved.
408 if(!options::finiteModelFind()) {
409 while((i
= Theory::setPop(theories
)) != THEORY_LAST
) {
410 if(!d_logicInfo
.isTheoryEnabled(i
)) {
411 LogicInfo newLogicInfo
= d_logicInfo
.getUnlockedCopy();
412 newLogicInfo
.enableTheory(i
);
415 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
416 << ", which doesn't include " << i
417 << ", but found a term in that theory." << endl
418 << "You might want to extend your logic to "
419 << newLogicInfo
.getLogicString() << endl
;
420 throw LogicException(ss
.str());
424 if (multipleTheories
) {
425 // Collect the shared terms if there are multiple theories
426 NodeVisitor
<SharedTermsVisitor
>::run(d_sharedTermsVisitor
, preprocessed
);
430 // Leaving pre-register
431 d_inPreregister
= false;
435 void TheoryEngine::printAssertions(const char* tag
) {
436 if (Trace
.isOn(tag
)) {
438 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
439 Theory
* theory
= d_theoryTable
[theoryId
];
440 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
441 Trace(tag
) << "--------------------------------------------" << endl
;
442 Trace(tag
) << "Assertions of " << theory
->getId() << ": " << endl
;
443 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(), it_end
= theory
->facts_end();
444 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
445 if ((*it
).isPreregistered
) {
446 Trace(tag
) << "[" << i
<< "]: ";
448 Trace(tag
) << "(" << i
<< "): ";
450 Trace(tag
) << (*it
).assertion
<< endl
;
453 if (d_logicInfo
.isSharingEnabled()) {
454 Trace(tag
) << "Shared terms of " << theory
->getId() << ": " << endl
;
455 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
456 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
457 Trace(tag
) << "[" << i
<< "]: " << (*it
) << endl
;
465 void TheoryEngine::dumpAssertions(const char* tag
) {
466 if (Dump
.isOn(tag
)) {
467 Dump(tag
) << CommentCommand("Starting completeness check");
468 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
469 Theory
* theory
= d_theoryTable
[theoryId
];
470 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
471 Dump(tag
) << CommentCommand("Completeness check");
472 Dump(tag
) << PushCommand();
474 // Dump the shared terms
475 if (d_logicInfo
.isSharingEnabled()) {
476 Dump(tag
) << CommentCommand("Shared terms");
477 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
478 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
481 Dump(tag
) << CommentCommand(ss
.str());
485 // Dump the assertions
486 Dump(tag
) << CommentCommand("Assertions");
487 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(), it_end
= theory
->facts_end();
488 for (; it
!= it_end
; ++ it
) {
490 Node assertionNode
= (*it
).assertion
;
491 // Purify all the terms
493 if ((*it
).isPreregistered
) {
494 Dump(tag
) << CommentCommand("Preregistered");
496 Dump(tag
) << CommentCommand("Shared assertion");
498 Dump(tag
) << AssertCommand(assertionNode
.toExpr());
500 Dump(tag
) << CheckSatCommand();
502 Dump(tag
) << PopCommand();
509 * Check all (currently-active) theories for conflicts.
510 * @param effort the effort level to use
512 void TheoryEngine::check(Theory::Effort effort
) {
515 // Reset the interrupt flag
516 d_interrupted
= false;
518 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
519 #undef CVC4_FOR_EACH_THEORY_STATEMENT
521 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
522 if (theory::TheoryTraits<THEORY>::hasCheck && d_logicInfo.isTheoryEnabled(THEORY)) { \
523 theoryOf(THEORY)->check(effort); \
524 if (d_inConflict) { \
525 Debug("conflict") << THEORY << " in conflict. " << std::endl; \
533 // Mark the output channel unused (if this is FULL_EFFORT, and nothing
534 // is done by the theories, no additional check will be needed)
535 d_outputChannelUsed
= false;
537 // Mark the lemmas flag (no lemmas added)
538 d_lemmasAdded
= false;
540 Debug("theory") << "TheoryEngine::check(" << effort
<< "): d_factsAsserted = " << (d_factsAsserted
? "true" : "false") << endl
;
542 // If in full effort, we have a fake new assertion just to jumpstart the checking
543 if (Theory::fullEffort(effort
)) {
544 d_factsAsserted
= true;
548 while (d_factsAsserted
&& !d_inConflict
&& !d_lemmasAdded
) {
550 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running check" << endl
;
552 Trace("theory::assertions") << endl
;
553 if (Trace
.isOn("theory::assertions")) {
554 printAssertions("theory::assertions");
557 if(Theory::fullEffort(effort
)) {
558 Trace("theory::assertions::fulleffort") << endl
;
559 if (Trace
.isOn("theory::assertions::fulleffort")) {
560 printAssertions("theory::assertions::fulleffort");
564 // Note that we've discharged all the facts
565 d_factsAsserted
= false;
568 CVC4_FOR_EACH_THEORY
;
570 if(Dump
.isOn("missed-t-conflicts")) {
571 Dump("missed-t-conflicts")
572 << CommentCommand("Completeness check for T-conflicts; expect sat")
573 << CheckSatCommand();
576 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running propagation after the initial check" << endl
;
578 // We are still satisfiable, propagate as much as possible
581 // We do combination if all has been processed and we are in fullcheck
582 if (Theory::fullEffort(effort
) && d_logicInfo
.isSharingEnabled() && !d_factsAsserted
&& !d_lemmasAdded
&& !d_inConflict
) {
583 // Do the combination
584 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running combination" << endl
;
586 if(d_logicInfo
.isQuantified()){
587 d_quantEngine
->notifyCombineTheories();
592 // Must consult quantifiers theory for last call to ensure sat, or otherwise add a lemma
593 if( Theory::fullEffort(effort
) && ! d_inConflict
&& ! needCheck() ) {
594 Trace("theory::assertions-model") << endl
;
595 if (Trace
.isOn("theory::assertions-model")) {
596 printAssertions("theory::assertions-model");
598 //checks for theories requiring the model go at last call
599 d_curr_model
->reset();
600 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
601 if( theoryId
!=THEORY_QUANTIFIERS
){
602 Theory
* theory
= d_theoryTable
[theoryId
];
603 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
604 if( theory
->needsCheckLastEffort() ){
605 if( !d_curr_model
->isBuilt() ){
606 if( !d_curr_model_builder
->buildModel(d_curr_model
) ){
610 theory
->check(Theory::EFFORT_LAST_CALL
);
617 if(d_logicInfo
.isQuantified()) {
618 // quantifiers engine must check at last call effort
619 d_quantEngine
->check(Theory::EFFORT_LAST_CALL
);
622 if (!d_inConflict
&& !needCheck())
624 // If d_eager_model_building is false, then we only mark that we
625 // are in "SAT mode". We build the model later only if the user asks
626 // for it via getBuiltModel.
628 if (d_eager_model_building
&& !d_curr_model
->isBuilt())
630 d_curr_model_builder
->buildModel(d_curr_model
);
635 Debug("theory") << "TheoryEngine::check(" << effort
<< "): done, we are " << (d_inConflict
? "unsat" : "sat") << (d_lemmasAdded
? " with new lemmas" : " with no new lemmas");
636 Debug("theory") << ", need check = " << (needCheck() ? "YES" : "NO") << endl
;
638 if( Theory::fullEffort(effort
) && !d_inConflict
&& !needCheck()) {
639 // case where we are about to answer SAT
640 if( d_masterEqualityEngine
!= NULL
){
641 AlwaysAssert(d_masterEqualityEngine
->consistent());
643 if (d_curr_model
->isBuilt())
645 // model construction should always succeed unless lemmas were added
646 AlwaysAssert(d_curr_model
->isBuiltSuccess());
647 if (options::produceModels())
649 // Do post-processing of model from the theories (used for THEORY_SEP
650 // to construct heap model)
651 postProcessModel(d_curr_model
);
652 // also call the model builder's post-process model
653 d_curr_model_builder
->postProcessModel(d_incomplete
.get(),
658 } catch(const theory::Interrupted
&) {
659 Trace("theory") << "TheoryEngine::check() => interrupted" << endl
;
661 // If fulleffort, check all theories
662 if(Dump
.isOn("theory::fullcheck") && Theory::fullEffort(effort
)) {
663 if (!d_inConflict
&& !needCheck()) {
664 dumpAssertions("theory::fullcheck");
669 void TheoryEngine::combineTheories() {
671 Trace("combineTheories") << "TheoryEngine::combineTheories()" << endl
;
673 TimerStat::CodeTimer
combineTheoriesTimer(d_combineTheoriesTime
);
675 // Care graph we'll be building
678 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
679 #undef CVC4_FOR_EACH_THEORY_STATEMENT
681 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
682 if (theory::TheoryTraits<THEORY>::isParametric && d_logicInfo.isTheoryEnabled(THEORY)) { \
683 theoryOf(THEORY)->getCareGraph(&careGraph); \
686 // Call on each parametric theory to give us its care graph
687 CVC4_FOR_EACH_THEORY
;
689 Trace("combineTheories") << "TheoryEngine::combineTheories(): care graph size = " << careGraph
.size() << endl
;
691 // Now add splitters for the ones we are interested in
692 CareGraph::const_iterator care_it
= careGraph
.begin();
693 CareGraph::const_iterator care_it_end
= careGraph
.end();
695 for (; care_it
!= care_it_end
; ++ care_it
) {
696 const CarePair
& carePair
= *care_it
;
698 Debug("combineTheories") << "TheoryEngine::combineTheories(): checking " << carePair
.a
<< " = " << carePair
.b
<< " from " << carePair
.theory
<< endl
;
700 Assert(d_sharedTerms
.isShared(carePair
.a
) || carePair
.a
.isConst());
701 Assert(d_sharedTerms
.isShared(carePair
.b
) || carePair
.b
.isConst());
703 // The equality in question (order for no repetition)
704 Node equality
= carePair
.a
.eqNode(carePair
.b
);
705 // EqualityStatus es = getEqualityStatus(carePair.a, carePair.b);
706 // Debug("combineTheories") << "TheoryEngine::combineTheories(): " <<
707 // (es == EQUALITY_TRUE_AND_PROPAGATED ? "EQUALITY_TRUE_AND_PROPAGATED" :
708 // es == EQUALITY_FALSE_AND_PROPAGATED ? "EQUALITY_FALSE_AND_PROPAGATED" :
709 // es == EQUALITY_TRUE ? "EQUALITY_TRUE" :
710 // es == EQUALITY_FALSE ? "EQUALITY_FALSE" :
711 // es == EQUALITY_TRUE_IN_MODEL ? "EQUALITY_TRUE_IN_MODEL" :
712 // es == EQUALITY_FALSE_IN_MODEL ? "EQUALITY_FALSE_IN_MODEL" :
713 // es == EQUALITY_UNKNOWN ? "EQUALITY_UNKNOWN" :
714 // "Unexpected case") << endl;
716 // We need to split on it
717 Debug("combineTheories") << "TheoryEngine::combineTheories(): requesting a split " << endl
;
719 lemma(equality
.orNode(equality
.notNode()), RULE_INVALID
, false, false, false, carePair
.theory
);
721 // This code is supposed to force preference to follow what the theory models already have
722 // but it doesn't seem to make a big difference - need to explore more -Clark
724 // if (es == EQUALITY_TRUE || es == EQUALITY_TRUE_IN_MODEL) {
725 Node e
= ensureLiteral(equality
);
726 d_propEngine
->requirePhase(e
, true);
728 // else if (es == EQUALITY_FALSE_IN_MODEL) {
729 // Node e = ensureLiteral(equality);
730 // d_propEngine->requirePhase(e, false);
736 void TheoryEngine::propagate(Theory::Effort effort
) {
737 // Reset the interrupt flag
738 d_interrupted
= false;
740 // Definition of the statement that is to be run by every theory
741 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
742 #undef CVC4_FOR_EACH_THEORY_STATEMENT
744 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
745 if (theory::TheoryTraits<THEORY>::hasPropagate && d_logicInfo.isTheoryEnabled(THEORY)) { \
746 theoryOf(THEORY)->propagate(effort); \
749 // Reset the interrupt flag
750 d_interrupted
= false;
752 // Propagate for each theory using the statement above
753 CVC4_FOR_EACH_THEORY
;
755 if(Dump
.isOn("missed-t-propagations")) {
756 for(unsigned i
= 0; i
< d_possiblePropagations
.size(); ++i
) {
757 Node atom
= d_possiblePropagations
[i
];
759 if(d_propEngine
->hasValue(atom
, value
)) {
762 // Doesn't have a value, check it (and the negation)
763 if(d_hasPropagated
.find(atom
) == d_hasPropagated
.end()) {
764 Dump("missed-t-propagations")
765 << CommentCommand("Completeness check for T-propagations; expect invalid")
766 << EchoCommand(atom
.toString())
767 << QueryCommand(atom
.toExpr())
768 << EchoCommand(atom
.notNode().toString())
769 << QueryCommand(atom
.notNode().toExpr());
775 Node
TheoryEngine::getNextDecisionRequest()
777 return d_decManager
->getNextDecisionRequest();
780 bool TheoryEngine::properConflict(TNode conflict
) const {
782 if (conflict
.getKind() == kind::AND
) {
783 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++ i
) {
784 if (! getPropEngine()->hasValue(conflict
[i
], value
)) {
785 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
786 << conflict
[i
] << endl
;
790 Debug("properConflict") << "Bad conflict is due to false atom: "
791 << conflict
[i
] << endl
;
794 if (conflict
[i
] != Rewriter::rewrite(conflict
[i
])) {
795 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
796 << conflict
[i
] << " vs " << Rewriter::rewrite(conflict
[i
]) << endl
;
801 if (! getPropEngine()->hasValue(conflict
, value
)) {
802 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
807 Debug("properConflict") << "Bad conflict is due to false atom: "
811 if (conflict
!= Rewriter::rewrite(conflict
)) {
812 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
813 << conflict
<< " vs " << Rewriter::rewrite(conflict
) << endl
;
820 bool TheoryEngine::properPropagation(TNode lit
) const {
821 if(!getPropEngine()->isSatLiteral(lit
)) {
825 return !getPropEngine()->hasValue(lit
, b
);
828 bool TheoryEngine::properExplanation(TNode node
, TNode expl
) const {
829 // Explanation must be either a conjunction of true literals that have true SAT values already
830 // or a singled literal that has a true SAT value already.
831 if (expl
.getKind() == kind::AND
) {
832 for (unsigned i
= 0; i
< expl
.getNumChildren(); ++ i
) {
834 if (!d_propEngine
->hasValue(expl
[i
], value
) || !value
) {
840 return d_propEngine
->hasValue(expl
, value
) && value
;
845 bool TheoryEngine::collectModelInfo(theory::TheoryModel
* m
)
847 //have shared term engine collectModelInfo
848 // d_sharedTerms.collectModelInfo( m );
849 // Consult each active theory to get all relevant information
850 // concerning the model.
851 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
852 if(d_logicInfo
.isTheoryEnabled(theoryId
)) {
853 Trace("model-builder") << " CollectModelInfo on theory: " << theoryId
<< endl
;
854 if (!d_theoryTable
[theoryId
]->collectModelInfo(m
))
860 Trace("model-builder") << " CollectModelInfo boolean variables" << std::endl
;
861 // Get the Boolean variables
862 vector
<TNode
> boolVars
;
863 d_propEngine
->getBooleanVariables(boolVars
);
864 vector
<TNode
>::iterator it
, iend
= boolVars
.end();
865 bool hasValue
, value
;
866 for (it
= boolVars
.begin(); it
!= iend
; ++it
) {
868 hasValue
= d_propEngine
->hasValue(var
, value
);
869 // TODO: Assert that hasValue is true?
871 Trace("model-builder-assertions")
872 << " has no value : " << var
<< std::endl
;
875 Trace("model-builder-assertions") << "(assert" << (value
? " " : " (not ") << var
<< (value
? ");" : "));") << endl
;
876 if (!m
->assertPredicate(var
, value
))
884 void TheoryEngine::postProcessModel( theory::TheoryModel
* m
){
885 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
886 if(d_logicInfo
.isTheoryEnabled(theoryId
)) {
887 Trace("model-builder-debug") << " PostProcessModel on theory: " << theoryId
<< endl
;
888 d_theoryTable
[theoryId
]->postProcessModel( m
);
893 TheoryModel
* TheoryEngine::getModel() {
897 TheoryModel
* TheoryEngine::getBuiltModel()
899 if (!d_curr_model
->isBuilt())
901 // If this method was called, we should be in SAT mode, and produceModels
903 AlwaysAssert(d_inSatMode
&& options::produceModels());
904 // must build model at this point
905 d_curr_model_builder
->buildModel(d_curr_model
);
910 void TheoryEngine::getSynthSolutions(std::map
<Node
, Node
>& sol_map
)
914 d_quantEngine
->getSynthSolutions(sol_map
);
922 bool TheoryEngine::presolve() {
923 // Reset the interrupt flag
924 d_interrupted
= false;
927 // Definition of the statement that is to be run by every theory
928 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
929 #undef CVC4_FOR_EACH_THEORY_STATEMENT
931 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
932 if (theory::TheoryTraits<THEORY>::hasPresolve) { \
933 theoryOf(THEORY)->presolve(); \
939 // Presolve for each theory using the statement above
940 CVC4_FOR_EACH_THEORY
;
941 } catch(const theory::Interrupted
&) {
942 Trace("theory") << "TheoryEngine::presolve() => interrupted" << endl
;
944 // return whether we have a conflict
946 }/* TheoryEngine::presolve() */
948 void TheoryEngine::postsolve() {
949 // Reset the decision manager. This clears its decision strategies, which are
950 // user-context-dependent.
951 d_decManager
->reset();
952 // no longer in SAT mode
954 // Reset the interrupt flag
955 d_interrupted
= false;
956 bool CVC4_UNUSED wasInConflict
= d_inConflict
;
959 // Definition of the statement that is to be run by every theory
960 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
961 #undef CVC4_FOR_EACH_THEORY_STATEMENT
963 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
964 if (theory::TheoryTraits<THEORY>::hasPostsolve) { \
965 theoryOf(THEORY)->postsolve(); \
966 Assert(! d_inConflict || wasInConflict, "conflict raised during postsolve()"); \
969 // Postsolve for each theory using the statement above
970 CVC4_FOR_EACH_THEORY
;
971 } catch(const theory::Interrupted
&) {
972 Trace("theory") << "TheoryEngine::postsolve() => interrupted" << endl
;
974 }/* TheoryEngine::postsolve() */
977 void TheoryEngine::notifyRestart() {
978 // Reset the interrupt flag
979 d_interrupted
= false;
981 // Definition of the statement that is to be run by every theory
982 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
983 #undef CVC4_FOR_EACH_THEORY_STATEMENT
985 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
986 if (theory::TheoryTraits<THEORY>::hasNotifyRestart && d_logicInfo.isTheoryEnabled(THEORY)) { \
987 theoryOf(THEORY)->notifyRestart(); \
990 // notify each theory using the statement above
991 CVC4_FOR_EACH_THEORY
;
994 void TheoryEngine::ppStaticLearn(TNode in
, NodeBuilder
<>& learned
) {
995 // Reset the interrupt flag
996 d_interrupted
= false;
998 // Definition of the statement that is to be run by every theory
999 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
1000 #undef CVC4_FOR_EACH_THEORY_STATEMENT
1002 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
1003 if (theory::TheoryTraits<THEORY>::hasPpStaticLearn) { \
1004 theoryOf(THEORY)->ppStaticLearn(in, learned); \
1007 // static learning for each theory using the statement above
1008 CVC4_FOR_EACH_THEORY
;
1011 void TheoryEngine::shutdown() {
1012 // Set this first; if a Theory shutdown() throws an exception,
1013 // at least the destruction of the TheoryEngine won't confound
1015 d_hasShutDown
= true;
1017 // Shutdown all the theories
1018 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
1019 if(d_theoryTable
[theoryId
]) {
1020 theoryOf(theoryId
)->shutdown();
1027 theory::Theory::PPAssertStatus
TheoryEngine::solve(TNode literal
, SubstitutionMap
& substitutionOut
) {
1028 // Reset the interrupt flag
1029 d_interrupted
= false;
1031 TNode atom
= literal
.getKind() == kind::NOT
? literal
[0] : literal
;
1032 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< "): solving with " << theoryOf(atom
)->getId() << endl
;
1034 if(! d_logicInfo
.isTheoryEnabled(Theory::theoryOf(atom
)) &&
1035 Theory::theoryOf(atom
) != THEORY_SAT_SOLVER
) {
1037 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
1038 << ", which doesn't include " << Theory::theoryOf(atom
)
1039 << ", but got a preprocessing-time fact for that theory." << endl
1040 << "The fact:" << endl
1042 throw LogicException(ss
.str());
1045 Theory::PPAssertStatus solveStatus
= theoryOf(atom
)->ppAssert(literal
, substitutionOut
);
1046 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< ") => " << solveStatus
<< endl
;
1050 // Recursively traverse a term and call the theory rewriter on its sub-terms
1051 Node
TheoryEngine::ppTheoryRewrite(TNode term
) {
1052 NodeMap::iterator find
= d_ppCache
.find(term
);
1053 if (find
!= d_ppCache
.end()) {
1054 return (*find
).second
;
1056 unsigned nc
= term
.getNumChildren();
1058 return theoryOf(term
)->ppRewrite(term
);
1060 Trace("theory-pp") << "ppTheoryRewrite { " << term
<< endl
;
1063 // do not rewrite inside quantifiers
1064 if (term
.getKind() == kind::FORALL
|| term
.getKind() == kind::EXISTS
1065 || term
.getKind() == kind::CHOICE
1066 || term
.getKind() == kind::LAMBDA
)
1068 newTerm
= Rewriter::rewrite(term
);
1072 NodeBuilder
<> newNode(term
.getKind());
1073 if (term
.getMetaKind() == kind::metakind::PARAMETERIZED
) {
1074 newNode
<< term
.getOperator();
1077 for (i
= 0; i
< nc
; ++i
) {
1078 newNode
<< ppTheoryRewrite(term
[i
]);
1080 newTerm
= Rewriter::rewrite(Node(newNode
));
1082 Node newTerm2
= theoryOf(newTerm
)->ppRewrite(newTerm
);
1083 if (newTerm
!= newTerm2
) {
1084 newTerm
= ppTheoryRewrite(Rewriter::rewrite(newTerm2
));
1086 d_ppCache
[term
] = newTerm
;
1087 Trace("theory-pp")<< "ppTheoryRewrite returning " << newTerm
<< "}" << endl
;
1092 void TheoryEngine::preprocessStart()
1098 struct preprocess_stack_element
{
1100 bool children_added
;
1101 preprocess_stack_element(TNode node
)
1102 : node(node
), children_added(false) {}
1103 };/* struct preprocess_stack_element */
1106 Node
TheoryEngine::preprocess(TNode assertion
) {
1108 Trace("theory::preprocess") << "TheoryEngine::preprocess(" << assertion
<< ")" << endl
;
1111 // Do a topological sort of the subexpressions and substitute them
1112 vector
<preprocess_stack_element
> toVisit
;
1113 toVisit
.push_back(assertion
);
1115 while (!toVisit
.empty())
1117 // The current node we are processing
1118 preprocess_stack_element
& stackHead
= toVisit
.back();
1119 TNode current
= stackHead
.node
;
1121 Debug("theory::internal") << "TheoryEngine::preprocess(" << assertion
<< "): processing " << current
<< endl
;
1123 // If node already in the cache we're done, pop from the stack
1124 NodeMap::iterator find
= d_ppCache
.find(current
);
1125 if (find
!= d_ppCache
.end()) {
1130 if(! d_logicInfo
.isTheoryEnabled(Theory::theoryOf(current
)) &&
1131 Theory::theoryOf(current
) != THEORY_SAT_SOLVER
) {
1133 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
1134 << ", which doesn't include " << Theory::theoryOf(current
)
1135 << ", but got a preprocessing-time fact for that theory." << endl
1136 << "The fact:" << endl
1138 throw LogicException(ss
.str());
1141 // If this is an atom, we preprocess its terms with the theory ppRewriter
1142 if (Theory::theoryOf(current
) != THEORY_BOOL
) {
1143 Node ppRewritten
= ppTheoryRewrite(current
);
1144 d_ppCache
[current
] = ppRewritten
;
1145 Assert(Rewriter::rewrite(d_ppCache
[current
]) == d_ppCache
[current
]);
1149 // Not yet substituted, so process
1150 if (stackHead
.children_added
) {
1151 // Children have been processed, so substitute
1152 NodeBuilder
<> builder(current
.getKind());
1153 if (current
.getMetaKind() == kind::metakind::PARAMETERIZED
) {
1154 builder
<< current
.getOperator();
1156 for (unsigned i
= 0; i
< current
.getNumChildren(); ++ i
) {
1157 Assert(d_ppCache
.find(current
[i
]) != d_ppCache
.end());
1158 builder
<< d_ppCache
[current
[i
]];
1160 // Mark the substitution and continue
1161 Node result
= builder
;
1162 if (result
!= current
) {
1163 result
= Rewriter::rewrite(result
);
1165 Debug("theory::internal") << "TheoryEngine::preprocess(" << assertion
<< "): setting " << current
<< " -> " << result
<< endl
;
1166 d_ppCache
[current
] = result
;
1169 // Mark that we have added the children if any
1170 if (current
.getNumChildren() > 0) {
1171 stackHead
.children_added
= true;
1172 // We need to add the children
1173 for(TNode::iterator child_it
= current
.begin(); child_it
!= current
.end(); ++ child_it
) {
1174 TNode childNode
= *child_it
;
1175 NodeMap::iterator childFind
= d_ppCache
.find(childNode
);
1176 if (childFind
== d_ppCache
.end()) {
1177 toVisit
.push_back(childNode
);
1181 // No children, so we're done
1182 Debug("substitution::internal") << "SubstitutionMap::internalSubstitute(" << assertion
<< "): setting " << current
<< " -> " << current
<< endl
;
1183 d_ppCache
[current
] = current
;
1189 // Return the substituted version
1190 return d_ppCache
[assertion
];
1193 void TheoryEngine::notifyPreprocessedAssertions(
1194 const std::vector
<Node
>& assertions
) {
1195 // call all the theories
1196 for (TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
;
1198 if (d_theoryTable
[theoryId
]) {
1199 theoryOf(theoryId
)->ppNotifyAssertions(assertions
);
1204 bool TheoryEngine::markPropagation(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
1206 // What and where we are asserting
1207 NodeTheoryPair
toAssert(assertion
, toTheoryId
, d_propagationMapTimestamp
);
1208 // What and where it came from
1209 NodeTheoryPair
toExplain(originalAssertion
, fromTheoryId
, d_propagationMapTimestamp
);
1211 // See if the theory already got this literal
1212 PropagationMap::const_iterator find
= d_propagationMap
.find(toAssert
);
1213 if (find
!= d_propagationMap
.end()) {
1214 // The theory already knows this
1215 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): already there" << endl
;
1219 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): marking [" << d_propagationMapTimestamp
<< "] " << assertion
<< ", " << toTheoryId
<< " from " << originalAssertion
<< ", " << fromTheoryId
<< endl
;
1221 // Mark the propagation
1222 d_propagationMap
[toAssert
] = toExplain
;
1223 d_propagationMapTimestamp
= d_propagationMapTimestamp
+ 1;
1229 void TheoryEngine::assertToTheory(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
1231 Trace("theory::assertToTheory") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << originalAssertion
<< "," << toTheoryId
<< ", " << fromTheoryId
<< ")" << endl
;
1233 Assert(toTheoryId
!= fromTheoryId
);
1234 if(toTheoryId
!= THEORY_SAT_SOLVER
&&
1235 ! d_logicInfo
.isTheoryEnabled(toTheoryId
)) {
1237 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
1238 << ", which doesn't include " << toTheoryId
1239 << ", but got an asserted fact to that theory." << endl
1240 << "The fact:" << endl
1242 throw LogicException(ss
.str());
1249 // If sharing is disabled, things are easy
1250 if (!d_logicInfo
.isSharingEnabled()) {
1251 Assert(assertion
== originalAssertion
);
1252 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
1253 // Send to the apropriate theory
1254 theory::Theory
* toTheory
= theoryOf(toTheoryId
);
1255 // We assert it, and we know it's preregistereed
1256 toTheory
->assertFact(assertion
, true);
1257 // Mark that we have more information
1258 d_factsAsserted
= true;
1260 Assert(toTheoryId
== THEORY_SAT_SOLVER
);
1261 // Check for propositional conflict
1263 if (d_propEngine
->hasValue(assertion
, value
)) {
1265 Trace("theory::propagate") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (no sharing)" << endl
;
1266 d_inConflict
= true;
1271 d_propagatedLiterals
.push_back(assertion
);
1276 // Polarity of the assertion
1277 bool polarity
= assertion
.getKind() != kind::NOT
;
1279 // Atom of the assertion
1280 TNode atom
= polarity
? assertion
: assertion
[0];
1282 // If sending to the shared terms database, it's also simple
1283 if (toTheoryId
== THEORY_BUILTIN
) {
1284 Assert(atom
.getKind() == kind::EQUAL
, "atom should be an EQUALity, not `%s'", atom
.toString().c_str());
1285 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1286 d_sharedTerms
.assertEquality(atom
, polarity
, assertion
);
1291 // Things from the SAT solver are already normalized, so they go
1292 // directly to the apropriate theory
1293 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
1294 // We know that this is normalized, so just send it off to the theory
1295 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1296 // Is it preregistered
1297 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
1299 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
1300 // Mark that we have more information
1301 d_factsAsserted
= true;
1306 // Propagations to the SAT solver are just enqueued for pickup by
1307 // the SAT solver later
1308 if (toTheoryId
== THEORY_SAT_SOLVER
) {
1309 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1310 // Enqueue for propagation to the SAT solver
1311 d_propagatedLiterals
.push_back(assertion
);
1312 // Check for propositional conflicts
1314 if (d_propEngine
->hasValue(assertion
, value
) && !value
) {
1315 Trace("theory::propagate") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (sharing)" << endl
;
1316 d_inConflict
= true;
1322 Assert(atom
.getKind() == kind::EQUAL
);
1325 Node normalizedLiteral
= Rewriter::rewrite(assertion
);
1327 // See if it rewrites false directly -> conflict
1328 if (normalizedLiteral
.isConst()) {
1329 if (!normalizedLiteral
.getConst
<bool>()) {
1330 // Mark the propagation for explanations
1331 if (markPropagation(normalizedLiteral
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1332 // Get the explanation (conflict will figure out where it came from)
1333 conflict(normalizedLiteral
, toTheoryId
);
1341 // Try and assert (note that we assert the non-normalized one)
1342 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1343 // Check if has been pre-registered with the theory
1344 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
1346 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
1347 d_factsAsserted
= true;
1353 void TheoryEngine::assertFact(TNode literal
)
1355 Trace("theory") << "TheoryEngine::assertFact(" << literal
<< ")" << endl
;
1359 // If we're in conflict, nothing to do
1365 bool polarity
= literal
.getKind() != kind::NOT
;
1366 TNode atom
= polarity
? literal
: literal
[0];
1368 if (d_logicInfo
.isSharingEnabled()) {
1370 // If any shared terms, it's time to do sharing work
1371 if (d_sharedTerms
.hasSharedTerms(atom
)) {
1372 // Notify the theories the shared terms
1373 SharedTermsDatabase::shared_terms_iterator it
= d_sharedTerms
.begin(atom
);
1374 SharedTermsDatabase::shared_terms_iterator it_end
= d_sharedTerms
.end(atom
);
1375 for (; it
!= it_end
; ++ it
) {
1377 Theory::Set theories
= d_sharedTerms
.getTheoriesToNotify(atom
, term
);
1378 for (TheoryId id
= THEORY_FIRST
; id
!= THEORY_LAST
; ++ id
) {
1379 if (Theory::setContains(id
, theories
)) {
1380 theoryOf(id
)->addSharedTermInternal(term
);
1383 d_sharedTerms
.markNotified(term
, theories
);
1387 // If it's an equality, assert it to the shared term manager, even though the terms are not
1388 // yet shared. As the terms become shared later, the shared terms manager will then add them
1389 // to the assert the equality to the interested theories
1390 if (atom
.getKind() == kind::EQUAL
) {
1391 // Assert it to the the owning theory
1392 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1393 // Shared terms manager will assert to interested theories directly, as the terms become shared
1394 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ THEORY_SAT_SOLVER
);
1396 // Now, let's check for any atom triggers from lemmas
1397 AtomRequests::atom_iterator it
= d_atomRequests
.getAtomIterator(atom
);
1398 while (!it
.done()) {
1399 const AtomRequests::Request
& request
= it
.get();
1400 Node toAssert
= polarity
? (Node
) request
.atom
: request
.atom
.notNode();
1401 Debug("theory::atoms") << "TheoryEngine::assertFact(" << literal
<< "): sending requested " << toAssert
<< endl
;
1402 assertToTheory(toAssert
, literal
, request
.toTheory
, THEORY_SAT_SOLVER
);
1407 // Not an equality, just assert to the appropriate theory
1408 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1411 // Assert the fact to the appropriate theory directly
1412 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1416 bool TheoryEngine::propagate(TNode literal
, theory::TheoryId theory
) {
1418 Debug("theory::propagate") << "TheoryEngine::propagate(" << literal
<< ", " << theory
<< ")" << endl
;
1422 if(Dump
.isOn("t-propagations")) {
1423 Dump("t-propagations") << CommentCommand("negation of theory propagation: expect valid")
1424 << QueryCommand(literal
.toExpr());
1426 if(Dump
.isOn("missed-t-propagations")) {
1427 d_hasPropagated
.insert(literal
);
1431 bool polarity
= literal
.getKind() != kind::NOT
;
1432 TNode atom
= polarity
? literal
: literal
[0];
1434 if (d_logicInfo
.isSharingEnabled() && atom
.getKind() == kind::EQUAL
) {
1435 if (d_propEngine
->isSatLiteral(literal
)) {
1436 // We propagate SAT literals to SAT
1437 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1439 if (theory
!= THEORY_BUILTIN
) {
1440 // Assert to the shared terms database
1441 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ theory
);
1444 // We could be propagating a unit-clause lemma. In this case, we need to provide a
1446 // TODO: Consider putting this someplace else? This is the only refence to the proof
1447 // manager in this class.
1450 LemmaProofRecipe proofRecipe
;
1451 proofRecipe
.addBaseAssertion(literal
);
1454 LemmaProofRecipe::ProofStep
proofStep(theory
, emptyNode
);
1455 proofStep
.addAssertion(literal
);
1456 proofRecipe
.addStep(proofStep
);
1458 ProofManager::getCnfProof()->setProofRecipe(&proofRecipe
);
1461 // Just send off to the SAT solver
1462 Assert(d_propEngine
->isSatLiteral(literal
));
1463 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1466 return !d_inConflict
;
1469 const LogicInfo
& TheoryEngine::getLogicInfo() const { return d_logicInfo
; }
1471 theory::EqualityStatus
TheoryEngine::getEqualityStatus(TNode a
, TNode b
) {
1472 Assert(a
.getType().isComparableTo(b
.getType()));
1473 if (d_sharedTerms
.isShared(a
) && d_sharedTerms
.isShared(b
)) {
1474 if (d_sharedTerms
.areEqual(a
,b
)) {
1475 return EQUALITY_TRUE_AND_PROPAGATED
;
1477 else if (d_sharedTerms
.areDisequal(a
,b
)) {
1478 return EQUALITY_FALSE_AND_PROPAGATED
;
1481 return theoryOf(Theory::theoryOf(a
.getType()))->getEqualityStatus(a
, b
);
1484 Node
TheoryEngine::getModelValue(TNode var
) {
1487 // the model value of a constant must be itself
1490 Assert(d_sharedTerms
.isShared(var
));
1491 return theoryOf(Theory::theoryOf(var
.getType()))->getModelValue(var
);
1495 Node
TheoryEngine::ensureLiteral(TNode n
) {
1496 Debug("ensureLiteral") << "rewriting: " << n
<< std::endl
;
1497 Node rewritten
= Rewriter::rewrite(n
);
1498 Debug("ensureLiteral") << " got: " << rewritten
<< std::endl
;
1499 Node preprocessed
= preprocess(rewritten
);
1500 Debug("ensureLiteral") << "preprocessed: " << preprocessed
<< std::endl
;
1501 d_propEngine
->ensureLiteral(preprocessed
);
1502 return preprocessed
;
1506 void TheoryEngine::printInstantiations( std::ostream
& out
) {
1507 if( d_quantEngine
){
1508 d_quantEngine
->printInstantiations( out
);
1510 out
<< "Internal error : instantiations not available when quantifiers are not present." << std::endl
;
1515 void TheoryEngine::printSynthSolution( std::ostream
& out
) {
1516 if( d_quantEngine
){
1517 d_quantEngine
->printSynthSolution( out
);
1519 out
<< "Internal error : synth solution not available when quantifiers are not present." << std::endl
;
1524 void TheoryEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1525 if( d_quantEngine
){
1526 d_quantEngine
->getInstantiatedQuantifiedFormulas( qs
);
1532 void TheoryEngine::getInstantiations( Node q
, std::vector
< Node
>& insts
) {
1533 if( d_quantEngine
){
1534 d_quantEngine
->getInstantiations( q
, insts
);
1540 void TheoryEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1541 if( d_quantEngine
){
1542 d_quantEngine
->getInstantiationTermVectors( q
, tvecs
);
1548 void TheoryEngine::getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
) {
1549 if( d_quantEngine
){
1550 d_quantEngine
->getInstantiations( insts
);
1556 void TheoryEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1557 if( d_quantEngine
){
1558 d_quantEngine
->getInstantiationTermVectors( insts
);
1564 Node
TheoryEngine::getInstantiatedConjunction( Node q
) {
1565 if( d_quantEngine
){
1566 return d_quantEngine
->getInstantiatedConjunction( q
);
1569 return Node::null();
1574 static Node
mkExplanation(const std::vector
<NodeTheoryPair
>& explanation
) {
1576 std::set
<TNode
> all
;
1577 for (unsigned i
= 0; i
< explanation
.size(); ++ i
) {
1578 Assert(explanation
[i
].theory
== THEORY_SAT_SOLVER
);
1579 all
.insert(explanation
[i
].node
);
1582 if (all
.size() == 0) {
1583 // Normalize to true
1584 return NodeManager::currentNM()->mkConst
<bool>(true);
1587 if (all
.size() == 1) {
1588 // All the same, or just one
1589 return explanation
[0].node
;
1592 NodeBuilder
<> conjunction(kind::AND
);
1593 std::set
<TNode
>::const_iterator it
= all
.begin();
1594 std::set
<TNode
>::const_iterator it_end
= all
.end();
1595 while (it
!= it_end
) {
1603 Node
TheoryEngine::getExplanationAndRecipe(TNode node
, LemmaProofRecipe
* proofRecipe
) {
1604 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< "): current propagation index = " << d_propagationMapTimestamp
<< endl
;
1606 bool polarity
= node
.getKind() != kind::NOT
;
1607 TNode atom
= polarity
? node
: node
[0];
1609 // If we're not in shared mode, explanations are simple
1610 if (!d_logicInfo
.isSharingEnabled()) {
1611 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing is NOT enabled. "
1612 << " Responsible theory is: "
1613 << theoryOf(atom
)->getId() << std::endl
;
1615 Node explanation
= theoryOf(atom
)->explain(node
);
1616 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< ") => " << explanation
<< endl
;
1620 LemmaProofRecipe::ProofStep
proofStep(theoryOf(atom
)->getId(), emptyNode
);
1621 proofStep
.addAssertion(node
);
1622 proofRecipe
->addBaseAssertion(node
);
1624 if (explanation
.getKind() == kind::AND
) {
1625 // If the explanation is a conjunction, the recipe for the corresponding lemma is
1626 // the negation of its conjuncts.
1627 Node flat
= flattenAnd(explanation
);
1628 for (unsigned i
= 0; i
< flat
.getNumChildren(); ++i
) {
1629 if (flat
[i
].isConst() && flat
[i
].getConst
<bool>()) {
1633 if (flat
[i
].getKind() == kind::NOT
&&
1634 flat
[i
][0].isConst() && !flat
[i
][0].getConst
<bool>()) {
1638 Debug("theory::explain") << "TheoryEngine::getExplanationAndRecipe: adding recipe assertion: "
1639 << flat
[i
].negate() << std::endl
;
1640 proofStep
.addAssertion(flat
[i
].negate());
1641 proofRecipe
->addBaseAssertion(flat
[i
].negate());
1644 // The recipe for proving it is by negating it. "True" is not an acceptable reason.
1645 if (!((explanation
.isConst() && explanation
.getConst
<bool>()) ||
1646 (explanation
.getKind() == kind::NOT
&&
1647 explanation
[0].isConst() && !explanation
[0].getConst
<bool>()))) {
1648 proofStep
.addAssertion(explanation
.negate());
1649 proofRecipe
->addBaseAssertion(explanation
.negate());
1653 proofRecipe
->addStep(proofStep
);
1660 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing IS enabled" << std::endl
;
1662 // Initial thing to explain
1663 NodeTheoryPair
toExplain(node
, THEORY_SAT_SOLVER
, d_propagationMapTimestamp
);
1664 Assert(d_propagationMap
.find(toExplain
) != d_propagationMap
.end());
1666 NodeTheoryPair nodeExplainerPair
= d_propagationMap
[toExplain
];
1667 Debug("theory::explain") << "TheoryEngine::getExplanation: explainer for node "
1668 << nodeExplainerPair
.node
1669 << " is theory: " << nodeExplainerPair
.theory
<< std::endl
;
1670 TheoryId explainer
= nodeExplainerPair
.theory
;
1672 // Create the workplace for explanations
1673 std::vector
<NodeTheoryPair
> explanationVector
;
1674 explanationVector
.push_back(d_propagationMap
[toExplain
]);
1675 // Process the explanation
1678 LemmaProofRecipe::ProofStep
proofStep(explainer
, emptyNode
);
1679 proofStep
.addAssertion(node
);
1680 proofRecipe
->addStep(proofStep
);
1681 proofRecipe
->addBaseAssertion(node
);
1684 getExplanation(explanationVector
, proofRecipe
);
1685 Node explanation
= mkExplanation(explanationVector
);
1687 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< ") => " << explanation
<< endl
;
1692 Node
TheoryEngine::getExplanation(TNode node
) {
1693 LemmaProofRecipe
*dontCareRecipe
= NULL
;
1694 return getExplanationAndRecipe(node
, dontCareRecipe
);
1697 struct AtomsCollect
{
1699 std::vector
<TNode
> d_atoms
;
1700 std::unordered_set
<TNode
, TNodeHashFunction
> d_visited
;
1704 typedef void return_type
;
1706 bool alreadyVisited(TNode current
, TNode parent
) {
1707 // Check if already visited
1708 if (d_visited
.find(current
) != d_visited
.end()) return true;
1709 // Don't visit non-boolean
1710 if (!current
.getType().isBoolean()) return true;
1715 void visit(TNode current
, TNode parent
) {
1716 if (Theory::theoryOf(current
) != theory::THEORY_BOOL
) {
1717 d_atoms
.push_back(current
);
1719 d_visited
.insert(current
);
1722 void start(TNode node
) {}
1723 void done(TNode node
) {}
1725 std::vector
<TNode
> getAtoms() const {
1730 void TheoryEngine::ensureLemmaAtoms(const std::vector
<TNode
>& atoms
, theory::TheoryId atomsTo
) {
1731 for (unsigned i
= 0; i
< atoms
.size(); ++ i
) {
1733 // Non-equality atoms are either owned by theory or they don't make sense
1734 if (atoms
[i
].getKind() != kind::EQUAL
) {
1740 // Simple normalization to not repeat stuff
1741 if (eq
[0] > eq
[1]) {
1742 eq
= eq
[1].eqNode(eq
[0]);
1745 // Rewrite the equality
1746 Node eqNormalized
= Rewriter::rewrite(atoms
[i
]);
1748 Debug("theory::atoms") << "TheoryEngine::ensureLemmaAtoms(): " << eq
<< " with nf " << eqNormalized
<< endl
;
1750 // If the equality is a boolean constant, we send immediately
1751 if (eqNormalized
.isConst()) {
1752 if (eqNormalized
.getConst
<bool>()) {
1753 assertToTheory(eq
, eqNormalized
, /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1755 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1758 }else if( eqNormalized
.getKind() != kind::EQUAL
){
1759 Assert( eqNormalized
.getKind()==kind::BOOLEAN_TERM_VARIABLE
||
1760 ( eqNormalized
.getKind()==kind::NOT
&& eqNormalized
[0].getKind()==kind::BOOLEAN_TERM_VARIABLE
) );
1761 // this happens for Boolean term equalities V = true that are rewritten to V, we should skip
1762 // TODO : revisit this
1766 // If the normalization did the just flips, keep the flip
1767 if (eqNormalized
[0] == eq
[1] && eqNormalized
[1] == eq
[0]) {
1771 // Check if the equality is already known by the sat solver
1772 if (d_propEngine
->isSatLiteral(eqNormalized
)) {
1774 if (d_propEngine
->hasValue(eqNormalized
, value
)) {
1776 assertToTheory(eq
, eqNormalized
, atomsTo
, theory::THEORY_SAT_SOLVER
);
1779 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), atomsTo
, theory::THEORY_SAT_SOLVER
);
1785 // If the theory is asking about a different form, or the form is ok but if will go to a different theory
1786 // then we must figure it out
1787 if (eqNormalized
!= eq
|| Theory::theoryOf(eq
) != atomsTo
) {
1788 // If you get eqNormalized, send atoms[i] to atomsTo
1789 d_atomRequests
.add(eqNormalized
, eq
, atomsTo
);
1794 theory::LemmaStatus
TheoryEngine::lemma(TNode node
,
1799 theory::TheoryId atomsTo
) {
1800 // For resource-limiting (also does a time check).
1803 // Do we need to check atoms
1804 if (atomsTo
!= theory::THEORY_LAST
) {
1805 Debug("theory::atoms") << "TheoryEngine::lemma(" << node
<< ", " << atomsTo
<< ")" << endl
;
1806 AtomsCollect collectAtoms
;
1807 NodeVisitor
<AtomsCollect
>::run(collectAtoms
, node
);
1808 ensureLemmaAtoms(collectAtoms
.getAtoms(), atomsTo
);
1811 if(Dump
.isOn("t-lemmas")) {
1816 Dump("t-lemmas") << CommentCommand("theory lemma: expect valid")
1817 << QueryCommand(n
.toExpr());
1820 // Share with other portfolio threads
1821 if(d_channels
->getLemmaOutputChannel() != NULL
) {
1822 d_channels
->getLemmaOutputChannel()->notifyNewLemma(node
.toExpr());
1825 AssertionPipeline additionalLemmas
;
1827 // Run theory preprocessing, maybe
1828 Node ppNode
= preprocess
? this->preprocess(node
) : Node(node
);
1831 Debug("ite") << "Remove ITE from " << ppNode
<< std::endl
;
1832 additionalLemmas
.push_back(ppNode
);
1833 additionalLemmas
.updateRealAssertionsEnd();
1834 d_tform_remover
.run(additionalLemmas
.ref(),
1835 additionalLemmas
.getIteSkolemMap());
1836 Debug("ite") << "..done " << additionalLemmas
[0] << std::endl
;
1837 additionalLemmas
.replace(0, theory::Rewriter::rewrite(additionalLemmas
[0]));
1839 if(Debug
.isOn("lemma-ites")) {
1840 Debug("lemma-ites") << "removed ITEs from lemma: " << ppNode
<< endl
;
1841 Debug("lemma-ites") << " + now have the following "
1842 << additionalLemmas
.size() << " lemma(s):" << endl
;
1843 for(std::vector
<Node
>::const_iterator i
= additionalLemmas
.begin();
1844 i
!= additionalLemmas
.end();
1846 Debug("lemma-ites") << " + " << *i
<< endl
;
1848 Debug("lemma-ites") << endl
;
1851 // assert to prop engine
1852 d_propEngine
->assertLemma(additionalLemmas
[0], negated
, removable
, rule
, node
);
1853 for (unsigned i
= 1; i
< additionalLemmas
.size(); ++ i
) {
1854 additionalLemmas
.replace(i
, theory::Rewriter::rewrite(additionalLemmas
[i
]));
1855 d_propEngine
->assertLemma(additionalLemmas
[i
], false, removable
, rule
, node
);
1858 // WARNING: Below this point don't assume additionalLemmas[0] to be not negated.
1860 additionalLemmas
.replace(0, additionalLemmas
[0].notNode());
1864 // assert to decision engine
1866 d_decisionEngine
->addAssertions(additionalLemmas
);
1869 // Mark that we added some lemmas
1870 d_lemmasAdded
= true;
1872 // Lemma analysis isn't online yet; this lemma may only live for this
1874 return theory::LemmaStatus(additionalLemmas
[0], d_userContext
->getLevel());
1877 void TheoryEngine::conflict(TNode conflict
, TheoryId theoryId
) {
1879 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", " << theoryId
<< ")" << endl
;
1881 // Mark that we are in conflict
1882 d_inConflict
= true;
1884 if(Dump
.isOn("t-conflicts")) {
1885 Dump("t-conflicts") << CommentCommand("theory conflict: expect unsat")
1886 << CheckSatCommand(conflict
.toExpr());
1889 LemmaProofRecipe
* proofRecipe
= NULL
;
1891 proofRecipe
= new LemmaProofRecipe
;
1893 LemmaProofRecipe::ProofStep
proofStep(theoryId
, emptyNode
);
1895 if (conflict
.getKind() == kind::AND
) {
1896 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++i
) {
1897 proofStep
.addAssertion(conflict
[i
].negate());
1900 proofStep
.addAssertion(conflict
.negate());
1903 proofRecipe
->addStep(proofStep
);
1906 // In the multiple-theories case, we need to reconstruct the conflict
1907 if (d_logicInfo
.isSharingEnabled()) {
1908 // Create the workplace for explanations
1909 std::vector
<NodeTheoryPair
> explanationVector
;
1910 explanationVector
.push_back(NodeTheoryPair(conflict
, theoryId
, d_propagationMapTimestamp
));
1912 // Process the explanation
1913 getExplanation(explanationVector
, proofRecipe
);
1914 PROOF(ProofManager::getCnfProof()->setProofRecipe(proofRecipe
));
1915 Node fullConflict
= mkExplanation(explanationVector
);
1916 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", " << theoryId
<< "): full = " << fullConflict
<< endl
;
1917 Assert(properConflict(fullConflict
));
1918 lemma(fullConflict
, RULE_CONFLICT
, true, true, false, THEORY_LAST
);
1921 // When only one theory, the conflict should need no processing
1922 Assert(properConflict(conflict
));
1924 if (conflict
.getKind() == kind::AND
) {
1925 // If the conflict is a conjunction, the corresponding lemma is derived by negating
1927 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++i
) {
1928 if (conflict
[i
].isConst() && conflict
[i
].getConst
<bool>()) {
1932 if (conflict
[i
].getKind() == kind::NOT
&&
1933 conflict
[i
][0].isConst() && !conflict
[i
][0].getConst
<bool>()) {
1937 proofRecipe
->getStep(0)->addAssertion(conflict
[i
].negate());
1938 proofRecipe
->addBaseAssertion(conflict
[i
].negate());
1941 proofRecipe
->getStep(0)->addAssertion(conflict
.negate());
1942 proofRecipe
->addBaseAssertion(conflict
.negate());
1945 ProofManager::getCnfProof()->setProofRecipe(proofRecipe
);
1948 lemma(conflict
, RULE_CONFLICT
, true, true, false, THEORY_LAST
);
1957 void TheoryEngine::staticInitializeBVOptions(
1958 const std::vector
<Node
>& assertions
)
1960 bool useSlicer
= true;
1961 if (options::bitvectorEqualitySlicer() == bv::BITVECTOR_SLICER_ON
)
1963 if (!d_logicInfo
.isPure(theory::THEORY_BV
) || d_logicInfo
.isQuantified())
1964 throw ModalException(
1965 "Slicer currently only supports pure QF_BV formulas. Use "
1966 "--bv-eq-slicer=off");
1967 if (options::incrementalSolving())
1968 throw ModalException(
1969 "Slicer does not currently support incremental mode. Use "
1970 "--bv-eq-slicer=off");
1971 if (options::produceModels())
1972 throw ModalException(
1973 "Slicer does not currently support model generation. Use "
1974 "--bv-eq-slicer=off");
1976 else if (options::bitvectorEqualitySlicer() == bv::BITVECTOR_SLICER_OFF
)
1980 else if (options::bitvectorEqualitySlicer() == bv::BITVECTOR_SLICER_AUTO
)
1982 if ((!d_logicInfo
.isPure(theory::THEORY_BV
) || d_logicInfo
.isQuantified())
1983 || options::incrementalSolving()
1984 || options::produceModels())
1987 bv::utils::TNodeBoolMap cache
;
1988 for (unsigned i
= 0; i
< assertions
.size(); ++i
)
1990 useSlicer
= useSlicer
&& bv::utils::isCoreTerm(assertions
[i
], cache
);
1996 bv::TheoryBV
* bv_theory
= (bv::TheoryBV
*)d_theoryTable
[THEORY_BV
];
1997 bv_theory
->enableCoreTheorySlicer();
2001 void TheoryEngine::getExplanation(std::vector
<NodeTheoryPair
>& explanationVector
, LemmaProofRecipe
* proofRecipe
) {
2002 Assert(explanationVector
.size() > 0);
2004 unsigned i
= 0; // Index of the current literal we are processing
2005 unsigned j
= 0; // Index of the last literal we are keeping
2007 std::unique_ptr
<std::set
<Node
>> inputAssertions
= nullptr;
2011 inputAssertions
.reset(
2012 new std::set
<Node
>(proofRecipe
->getStep(0)->getAssertions()));
2016 while (i
< explanationVector
.size()) {
2017 // Get the current literal to explain
2018 NodeTheoryPair toExplain
= explanationVector
[i
];
2020 Debug("theory::explain") << "[i=" << i
<< "] TheoryEngine::explain(): processing [" << toExplain
.timestamp
<< "] " << toExplain
.node
<< " sent from " << toExplain
.theory
<< endl
;
2023 // If a true constant or a negation of a false constant we can ignore it
2024 if (toExplain
.node
.isConst() && toExplain
.node
.getConst
<bool>()) {
2028 if (toExplain
.node
.getKind() == kind::NOT
&& toExplain
.node
[0].isConst() && !toExplain
.node
[0].getConst
<bool>()) {
2033 // If from the SAT solver, keep it
2034 if (toExplain
.theory
== THEORY_SAT_SOLVER
) {
2035 Debug("theory::explain") << "\tLiteral came from THEORY_SAT_SOLVER. Kepping it." << endl
;
2036 explanationVector
[j
++] = explanationVector
[i
++];
2040 // If an and, expand it
2041 if (toExplain
.node
.getKind() == kind::AND
) {
2042 Debug("theory::explain") << "TheoryEngine::explain(): expanding " << toExplain
.node
<< " got from " << toExplain
.theory
<< endl
;
2043 for (unsigned k
= 0; k
< toExplain
.node
.getNumChildren(); ++ k
) {
2044 NodeTheoryPair
newExplain(toExplain
.node
[k
], toExplain
.theory
, toExplain
.timestamp
);
2045 explanationVector
.push_back(newExplain
);
2051 // See if it was sent to the theory by another theory
2052 PropagationMap::const_iterator find
= d_propagationMap
.find(toExplain
);
2053 if (find
!= d_propagationMap
.end()) {
2054 Debug("theory::explain") << "\tTerm was propagated by another theory (theory = "
2055 << theoryOf((*find
).second
.theory
)->getId() << ")" << std::endl
;
2056 // There is some propagation, check if its a timely one
2057 if ((*find
).second
.timestamp
< toExplain
.timestamp
) {
2058 Debug("theory::explain") << "\tRelevant timetsamp, pushing "
2059 << (*find
).second
.node
<< "to index = " << explanationVector
.size() << std::endl
;
2060 explanationVector
.push_back((*find
).second
);
2064 if (toExplain
.node
!= (*find
).second
.node
) {
2065 Debug("pf::explain") << "TheoryEngine::getExplanation: Rewrite alert! toAssert = " << toExplain
.node
2066 << ", toExplain = " << (*find
).second
.node
<< std::endl
;
2069 proofRecipe
->addRewriteRule(toExplain
.node
, (*find
).second
.node
);
2078 // It was produced by the theory, so ask for an explanation
2080 if (toExplain
.theory
== THEORY_BUILTIN
) {
2081 explanation
= d_sharedTerms
.explain(toExplain
.node
);
2082 Debug("theory::explain") << "\tTerm was propagated by THEORY_BUILTIN. Explanation: " << explanation
<< std::endl
;
2084 explanation
= theoryOf(toExplain
.theory
)->explain(toExplain
.node
);
2085 Debug("theory::explain") << "\tTerm was propagated by owner theory: "
2086 << theoryOf(toExplain
.theory
)->getId()
2087 << ". Explanation: " << explanation
<< std::endl
;
2090 Debug("theory::explain") << "TheoryEngine::explain(): got explanation " << explanation
<< " got from " << toExplain
.theory
<< endl
;
2091 Assert( explanation
!= toExplain
.node
, "wasn't sent to you, so why are you explaining it trivially");
2092 // Mark the explanation
2093 NodeTheoryPair
newExplain(explanation
, toExplain
.theory
, toExplain
.timestamp
);
2094 explanationVector
.push_back(newExplain
);
2099 if (proofRecipe
&& inputAssertions
)
2101 // If we're expanding the target node of the explanation (this is the
2102 // first expansion...), we don't want to add it as a separate proof
2103 // step. It is already part of the assertions.
2104 if (!ContainsKey(*inputAssertions
, toExplain
.node
))
2106 LemmaProofRecipe::ProofStep
proofStep(toExplain
.theory
,
2108 if (explanation
.getKind() == kind::AND
)
2110 Node flat
= flattenAnd(explanation
);
2111 for (unsigned k
= 0; k
< flat
.getNumChildren(); ++k
)
2113 // If a true constant or a negation of a false constant we can
2115 if (!((flat
[k
].isConst() && flat
[k
].getConst
<bool>())
2116 || (flat
[k
].getKind() == kind::NOT
&& flat
[k
][0].isConst()
2117 && !flat
[k
][0].getConst
<bool>())))
2119 proofStep
.addAssertion(flat
[k
].negate());
2125 if (!((explanation
.isConst() && explanation
.getConst
<bool>())
2126 || (explanation
.getKind() == kind::NOT
2127 && explanation
[0].isConst()
2128 && !explanation
[0].getConst
<bool>())))
2130 proofStep
.addAssertion(explanation
.negate());
2133 proofRecipe
->addStep(proofStep
);
2139 // Keep only the relevant literals
2140 explanationVector
.resize(j
);
2144 // The remaining literals are the base of the proof
2145 for (unsigned k
= 0; k
< explanationVector
.size(); ++k
) {
2146 proofRecipe
->addBaseAssertion(explanationVector
[k
].node
.negate());
2152 void TheoryEngine::setUserAttribute(const std::string
& attr
,
2154 const std::vector
<Node
>& node_values
,
2155 const std::string
& str_value
)
2157 Trace("te-attr") << "set user attribute " << attr
<< " " << n
<< endl
;
2158 if( d_attr_handle
.find( attr
)!=d_attr_handle
.end() ){
2159 for( size_t i
=0; i
<d_attr_handle
[attr
].size(); i
++ ){
2160 d_attr_handle
[attr
][i
]->setUserAttribute(attr
, n
, node_values
, str_value
);
2163 //unhandled exception?
2167 void TheoryEngine::handleUserAttribute(const char* attr
, Theory
* t
) {
2168 Trace("te-attr") << "Handle user attribute " << attr
<< " " << t
<< endl
;
2169 std::string
str( attr
);
2170 d_attr_handle
[ str
].push_back( t
);
2173 void TheoryEngine::checkTheoryAssertionsWithModel(bool hardFailure
) {
2174 for(TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
2175 Theory
* theory
= d_theoryTable
[theoryId
];
2176 if(theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
2177 for(context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
2178 it_end
= theory
->facts_end();
2181 Node assertion
= (*it
).assertion
;
2182 Node val
= getModel()->getValue(assertion
);
2185 ss
<< theoryId
<< " has an asserted fact that the model doesn't satisfy." << endl
2186 << "The fact: " << assertion
<< endl
2187 << "Model value: " << val
<< endl
;
2189 InternalError(ss
.str());
2197 std::pair
<bool, Node
> TheoryEngine::entailmentCheck(theory::TheoryOfMode mode
, TNode lit
, const EntailmentCheckParameters
* params
, EntailmentCheckSideEffects
* seffects
) {
2198 TNode atom
= (lit
.getKind() == kind::NOT
) ? lit
[0] : lit
;
2199 if( atom
.getKind()==kind::AND
|| atom
.getKind()==kind::OR
|| atom
.getKind()==kind::IMPLIES
){
2200 //Boolean connective, recurse
2201 std::vector
< Node
> children
;
2202 bool pol
= (lit
.getKind()!=kind::NOT
);
2203 bool is_conjunction
= pol
==(lit
.getKind()==kind::AND
);
2204 for( unsigned i
=0; i
<atom
.getNumChildren(); i
++ ){
2206 if( pol
==( lit
.getKind()==kind::IMPLIES
&& i
==0 ) ){
2207 ch
= atom
[i
].negate();
2209 std::pair
<bool, Node
> chres
= entailmentCheck( mode
, ch
, params
, seffects
);
2211 if( !is_conjunction
){
2214 children
.push_back( chres
.second
);
2216 }else if( !chres
.first
&& is_conjunction
){
2217 return std::pair
<bool, Node
>(false, Node::null());
2220 if( is_conjunction
){
2221 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, children
));
2223 return std::pair
<bool, Node
>(false, Node::null());
2225 }else if( atom
.getKind()==kind::ITE
|| ( atom
.getKind()==kind::EQUAL
&& atom
[0].getType().isBoolean() ) ){
2226 bool pol
= (lit
.getKind()!=kind::NOT
);
2227 for( unsigned r
=0; r
<2; r
++ ){
2232 std::pair
<bool, Node
> chres
= entailmentCheck( mode
, ch
, params
, seffects
);
2234 Node ch2
= atom
[ atom
.getKind()==kind::ITE
? r
+1 : 1 ];
2235 if( pol
==( atom
.getKind()==kind::ITE
? true : r
==1 ) ){
2238 std::pair
<bool, Node
> chres2
= entailmentCheck( mode
, ch2
, params
, seffects
);
2240 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, chres
.second
, chres2
.second
));
2246 return std::pair
<bool, Node
>(false, Node::null());
2248 //it is a theory atom
2249 theory::TheoryId tid
= theory::Theory::theoryOf(mode
, atom
);
2250 theory::Theory
* th
= theoryOf(tid
);
2253 Assert(params
== NULL
|| tid
== params
->getTheoryId());
2254 Assert(seffects
== NULL
|| tid
== seffects
->getTheoryId());
2255 Trace("theory-engine-entc") << "Entailment check : " << lit
<< std::endl
;
2257 std::pair
<bool, Node
> chres
= th
->entailmentCheck(lit
, params
, seffects
);
2262 void TheoryEngine::spendResource(unsigned amount
)
2264 d_resourceManager
->spendResource(amount
);
2267 void TheoryEngine::enableTheoryAlternative(const std::string
& name
){
2268 Debug("TheoryEngine::enableTheoryAlternative")
2269 << "TheoryEngine::enableTheoryAlternative(" << name
<< ")" << std::endl
;
2271 d_theoryAlternatives
.insert(name
);
2274 bool TheoryEngine::useTheoryAlternative(const std::string
& name
) {
2275 return d_theoryAlternatives
.find(name
) != d_theoryAlternatives
.end();
2279 TheoryEngine::Statistics::Statistics(theory::TheoryId theory
):
2280 conflicts(getStatsPrefix(theory
) + "::conflicts", 0),
2281 propagations(getStatsPrefix(theory
) + "::propagations", 0),
2282 lemmas(getStatsPrefix(theory
) + "::lemmas", 0),
2283 requirePhase(getStatsPrefix(theory
) + "::requirePhase", 0),
2284 restartDemands(getStatsPrefix(theory
) + "::restartDemands", 0)
2286 smtStatisticsRegistry()->registerStat(&conflicts
);
2287 smtStatisticsRegistry()->registerStat(&propagations
);
2288 smtStatisticsRegistry()->registerStat(&lemmas
);
2289 smtStatisticsRegistry()->registerStat(&requirePhase
);
2290 smtStatisticsRegistry()->registerStat(&restartDemands
);
2293 TheoryEngine::Statistics::~Statistics() {
2294 smtStatisticsRegistry()->unregisterStat(&conflicts
);
2295 smtStatisticsRegistry()->unregisterStat(&propagations
);
2296 smtStatisticsRegistry()->unregisterStat(&lemmas
);
2297 smtStatisticsRegistry()->unregisterStat(&requirePhase
);
2298 smtStatisticsRegistry()->unregisterStat(&restartDemands
);
2301 }/* CVC4 namespace */