1 /********************* */
2 /*! \file theory_engine.cpp
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Dejan Jovanovic, 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/lazy_proof.h"
26 #include "expr/node.h"
27 #include "expr/node_algorithm.h"
28 #include "expr/node_builder.h"
29 #include "expr/node_visitor.h"
30 #include "options/bv_options.h"
31 #include "options/options.h"
32 #include "options/quantifiers_options.h"
33 #include "options/theory_options.h"
34 #include "preprocessing/assertion_pipeline.h"
35 #include "printer/printer.h"
37 #include "smt/logic_exception.h"
38 #include "smt/term_formula_removal.h"
39 #include "theory/arith/arith_ite_utils.h"
40 #include "theory/bv/theory_bv_utils.h"
41 #include "theory/care_graph.h"
42 #include "theory/combination_care_graph.h"
43 #include "theory/decision_manager.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/relevance_manager.h"
49 #include "theory/rewriter.h"
50 #include "theory/theory.h"
51 #include "theory/theory_engine_proof_generator.h"
52 #include "theory/theory_id.h"
53 #include "theory/theory_model.h"
54 #include "theory/theory_traits.h"
55 #include "theory/uf/equality_engine.h"
56 #include "util/resource_manager.h"
60 using namespace CVC4::preprocessing
;
61 using namespace CVC4::theory
;
65 /* -------------------------------------------------------------------------- */
70 * IMPORTANT: The order of the theories is important. For example, strings
71 * depends on arith, quantifiers needs to come as the very last.
72 * Do not change this order.
75 #define CVC4_FOR_EACH_THEORY \
76 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BUILTIN) \
77 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BOOL) \
78 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_UF) \
79 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_ARITH) \
80 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BV) \
81 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_FP) \
82 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_ARRAYS) \
83 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_DATATYPES) \
84 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_SEP) \
85 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_SETS) \
86 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BAGS) \
87 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_STRINGS) \
88 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_QUANTIFIERS)
92 /* -------------------------------------------------------------------------- */
94 inline void flattenAnd(Node n
, std::vector
<TNode
>& out
){
95 Assert(n
.getKind() == kind::AND
);
96 for(Node::iterator i
=n
.begin(), i_end
=n
.end(); i
!= i_end
; ++i
){
98 if(curr
.getKind() == kind::AND
){
99 flattenAnd(curr
, out
);
106 inline Node
flattenAnd(Node n
){
107 std::vector
<TNode
> out
;
109 return NodeManager::currentNM()->mkNode(kind::AND
, out
);
113 * Compute the string for a given theory id. In this module, we use
114 * THEORY_SAT_SOLVER as an id, which is not a normal id but maps to
115 * THEORY_LAST. Thus, we need our own string conversion here.
117 * @param id The theory id
118 * @return The string corresponding to the theory id
120 std::string
getTheoryString(theory::TheoryId id
)
122 if (id
== theory::THEORY_SAT_SOLVER
)
124 return "THEORY_SAT_SOLVER";
128 std::stringstream ss
;
134 void TheoryEngine::finishInit()
136 // NOTE: This seems to be required since
137 // theory::TheoryTraits<THEORY>::isParametric cannot be accessed without
138 // using the CVC4_FOR_EACH_THEORY_STATEMENT macro. -AJR
139 std::vector
<theory::Theory
*> paraTheories
;
140 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
141 #undef CVC4_FOR_EACH_THEORY_STATEMENT
143 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
144 if (theory::TheoryTraits<THEORY>::isParametric \
145 && d_logicInfo.isTheoryEnabled(THEORY)) \
147 paraTheories.push_back(theoryOf(THEORY)); \
149 // Collect the parametric theories, which are given to the theory combination
151 CVC4_FOR_EACH_THEORY
;
153 // Initialize the theory combination architecture
154 if (options::tcMode() == options::TcMode::CARE_GRAPH
)
156 d_tc
.reset(new CombinationCareGraph(*this, paraTheories
, d_pnm
));
160 Unimplemented() << "TheoryEngine::finishInit: theory combination mode "
161 << options::tcMode() << " not supported";
163 // create the relevance filter if any option requires it
164 if (options::relevanceFilter())
167 new RelevanceManager(d_userContext
, theory::Valuation(this)));
170 // initialize the quantifiers engine
171 if (d_logicInfo
.isQuantified())
173 // initialize the quantifiers engine
174 d_quantEngine
= new QuantifiersEngine(this, *d_decManager
.get(), d_pnm
);
176 // initialize the theory combination manager, which decides and allocates the
177 // equality engines to use for all theories.
179 // get pointer to the shared solver
180 d_sharedSolver
= d_tc
->getSharedSolver();
182 // set the core equality engine on quantifiers engine
183 if (d_logicInfo
.isQuantified())
185 d_quantEngine
->setMasterEqualityEngine(d_tc
->getCoreEqualityEngine());
188 // finish initializing the theories by linking them with the appropriate
189 // utilities and then calling their finishInit method.
190 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
191 Theory
* t
= d_theoryTable
[theoryId
];
196 // setup the pointers to the utilities
197 const EeTheoryInfo
* eeti
= d_tc
->getEeTheoryInfo(theoryId
);
198 Assert(eeti
!= nullptr);
199 // the theory's official equality engine is the one specified by the
200 // equality engine manager
201 t
->setEqualityEngine(eeti
->d_usedEe
);
202 // set the quantifiers engine
203 t
->setQuantifiersEngine(d_quantEngine
);
204 // set the decision manager for the theory
205 t
->setDecisionManager(d_decManager
.get());
206 // finish initializing the theory
210 // finish initializing the quantifiers engine
211 if (d_logicInfo
.isQuantified())
213 d_quantEngine
->finishInit();
217 ProofNodeManager
* TheoryEngine::getProofNodeManager() const { return d_pnm
; }
219 TheoryEngine::TheoryEngine(context::Context
* context
,
220 context::UserContext
* userContext
,
222 RemoveTermFormulas
& iteRemover
,
223 const LogicInfo
& logicInfo
,
224 OutputManager
& outMgr
,
225 ProofNodeManager
* pnm
)
226 : d_propEngine(nullptr),
228 d_userContext(userContext
),
229 d_logicInfo(logicInfo
),
235 d_pnm
, nullptr, d_userContext
, "TheoryEngine::LazyCDProof")
237 d_tepg(new TheoryEngineProofGenerator(d_pnm
, d_userContext
)),
239 d_sharedSolver(nullptr),
240 d_quantEngine(nullptr),
241 d_decManager(new DecisionManager(userContext
)),
242 d_relManager(nullptr),
243 d_preRegistrationVisitor(this, context
),
244 d_eager_model_building(false),
245 d_inConflict(context
, false),
247 d_hasShutDown(false),
248 d_incomplete(context
, false),
249 d_propagationMap(context
),
250 d_propagationMapTimestamp(context
, 0),
251 d_propagatedLiterals(context
),
252 d_propagatedLiteralsIndex(context
, 0),
253 d_atomRequests(context
),
254 d_tpp(*this, iteRemover
, userContext
, d_pnm
),
255 d_combineTheoriesTime("TheoryEngine::combineTheoriesTime"),
258 d_interrupted(false),
259 d_resourceManager(rm
),
260 d_inPreregister(false),
261 d_factsAsserted(context
, false),
263 d_arithSubstitutionsAdded("theory::arith::zzz::arith::substitutions", 0)
265 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
;
268 d_theoryTable
[theoryId
] = NULL
;
269 d_theoryOut
[theoryId
] = NULL
;
272 smtStatisticsRegistry()->registerStat(&d_combineTheoriesTime
);
273 d_true
= NodeManager::currentNM()->mkConst
<bool>(true);
274 d_false
= NodeManager::currentNM()->mkConst
<bool>(false);
276 smtStatisticsRegistry()->registerStat(&d_arithSubstitutionsAdded
);
279 TheoryEngine::~TheoryEngine() {
280 Assert(d_hasShutDown
);
282 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
283 if(d_theoryTable
[theoryId
] != NULL
) {
284 delete d_theoryTable
[theoryId
];
285 delete d_theoryOut
[theoryId
];
289 delete d_quantEngine
;
291 smtStatisticsRegistry()->unregisterStat(&d_combineTheoriesTime
);
292 smtStatisticsRegistry()->unregisterStat(&d_arithSubstitutionsAdded
);
295 void TheoryEngine::interrupt() { d_interrupted
= true; }
296 void TheoryEngine::preRegister(TNode preprocessed
) {
297 Debug("theory") << "TheoryEngine::preRegister( " << preprocessed
<< ")"
299 d_preregisterQueue
.push(preprocessed
);
301 if (!d_inPreregister
) {
302 // We're in pre-register
303 d_inPreregister
= true;
305 // Process the pre-registration queue
306 while (!d_preregisterQueue
.empty()) {
307 // Get the next atom to pre-register
308 preprocessed
= d_preregisterQueue
.front();
309 d_preregisterQueue
.pop();
311 // the atom should not have free variables
312 Debug("theory") << "TheoryEngine::preRegister: " << preprocessed
314 Assert(!expr::hasFreeVar(preprocessed
));
315 // should not have witness
316 Assert(!expr::hasSubtermKind(kind::WITNESS
, preprocessed
));
318 // Pre-register the terms in the atom
319 theory::TheoryIdSet theories
= NodeVisitor
<PreRegisterVisitor
>::run(
320 d_preRegistrationVisitor
, preprocessed
);
321 theories
= TheoryIdSetUtil::setRemove(THEORY_BOOL
, theories
);
322 // Remove the top theory, if any more that means multiple theories were
324 bool multipleTheories
=
325 TheoryIdSetUtil::setRemove(Theory::theoryOf(preprocessed
), theories
);
326 if (Configuration::isAssertionBuild())
329 // This should never throw an exception, since theories should be
330 // guaranteed to be initialized.
331 // These checks don't work with finite model finding, because it
332 // uses Rational constants to represent cardinality constraints,
333 // even though arithmetic isn't actually involved.
334 if (!options::finiteModelFind())
336 while ((i
= TheoryIdSetUtil::setPop(theories
)) != THEORY_LAST
)
338 if (!d_logicInfo
.isTheoryEnabled(i
))
340 LogicInfo newLogicInfo
= d_logicInfo
.getUnlockedCopy();
341 newLogicInfo
.enableTheory(i
);
343 std::stringstream ss
;
344 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
345 << ", which doesn't include " << i
346 << ", but found a term in that theory." << std::endl
347 << "You might want to extend your logic to "
348 << newLogicInfo
.getLogicString() << std::endl
;
349 throw LogicException(ss
.str());
355 // pre-register with the shared solver, which also handles
356 // calling prepregister on individual theories.
357 Assert(d_sharedSolver
!= nullptr);
358 d_sharedSolver
->preRegisterShared(preprocessed
, multipleTheories
);
361 // Leaving pre-register
362 d_inPreregister
= false;
366 void TheoryEngine::printAssertions(const char* tag
) {
367 if (Trace
.isOn(tag
)) {
369 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
370 Theory
* theory
= d_theoryTable
[theoryId
];
371 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
372 Trace(tag
) << "--------------------------------------------" << endl
;
373 Trace(tag
) << "Assertions of " << theory
->getId() << ": " << endl
;
375 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
378 for (unsigned i
= 0; it
!= it_end
; ++it
, ++i
)
380 if ((*it
).d_isPreregistered
)
382 Trace(tag
) << "[" << i
<< "]: ";
386 Trace(tag
) << "(" << i
<< "): ";
388 Trace(tag
) << (*it
).d_assertion
<< endl
;
392 if (d_logicInfo
.isSharingEnabled()) {
393 Trace(tag
) << "Shared terms of " << theory
->getId() << ": " << endl
;
394 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
395 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
396 Trace(tag
) << "[" << i
<< "]: " << (*it
) << endl
;
404 void TheoryEngine::dumpAssertions(const char* tag
) {
405 if (Dump
.isOn(tag
)) {
406 const Printer
& printer
= d_outMgr
.getPrinter();
407 std::ostream
& out
= d_outMgr
.getDumpOut();
408 printer
.toStreamCmdComment(out
, "Starting completeness check");
409 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
410 Theory
* theory
= d_theoryTable
[theoryId
];
411 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
412 printer
.toStreamCmdComment(out
, "Completeness check");
413 printer
.toStreamCmdPush(out
);
415 // Dump the shared terms
416 if (d_logicInfo
.isSharingEnabled()) {
417 printer
.toStreamCmdComment(out
, "Shared terms");
418 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
419 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
422 printer
.toStreamCmdComment(out
, ss
.str());
426 // Dump the assertions
427 printer
.toStreamCmdComment(out
, "Assertions");
428 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(), it_end
= theory
->facts_end();
429 for (; it
!= it_end
; ++ it
) {
431 Node assertionNode
= (*it
).d_assertion
;
432 // Purify all the terms
434 if ((*it
).d_isPreregistered
)
436 printer
.toStreamCmdComment(out
, "Preregistered");
440 printer
.toStreamCmdComment(out
, "Shared assertion");
442 printer
.toStreamCmdAssert(out
, assertionNode
);
444 printer
.toStreamCmdCheckSat(out
);
446 printer
.toStreamCmdPop(out
);
453 * Check all (currently-active) theories for conflicts.
454 * @param effort the effort level to use
456 void TheoryEngine::check(Theory::Effort effort
) {
459 // Reset the interrupt flag
460 d_interrupted
= false;
462 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
463 #undef CVC4_FOR_EACH_THEORY_STATEMENT
465 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
466 if (theory::TheoryTraits<THEORY>::hasCheck \
467 && d_logicInfo.isTheoryEnabled(THEORY)) \
469 theoryOf(THEORY)->check(effort); \
472 Debug("conflict") << THEORY << " in conflict. " << std::endl; \
480 // Mark the output channel unused (if this is FULL_EFFORT, and nothing
481 // is done by the theories, no additional check will be needed)
482 d_outputChannelUsed
= false;
484 // Mark the lemmas flag (no lemmas added)
485 d_lemmasAdded
= false;
487 Debug("theory") << "TheoryEngine::check(" << effort
<< "): d_factsAsserted = " << (d_factsAsserted
? "true" : "false") << endl
;
489 // If in full effort, we have a fake new assertion just to jumpstart the checking
490 if (Theory::fullEffort(effort
)) {
491 d_factsAsserted
= true;
492 // Reset round for the relevance manager, which notice only sets a flag
493 // to indicate that its information must be recomputed.
494 if (d_relManager
!= nullptr)
496 d_relManager
->resetRound();
502 while (d_factsAsserted
&& !d_inConflict
&& !d_lemmasAdded
) {
504 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running check" << endl
;
506 Trace("theory::assertions") << endl
;
507 if (Trace
.isOn("theory::assertions")) {
508 printAssertions("theory::assertions");
511 if(Theory::fullEffort(effort
)) {
512 Trace("theory::assertions::fulleffort") << endl
;
513 if (Trace
.isOn("theory::assertions::fulleffort")) {
514 printAssertions("theory::assertions::fulleffort");
518 // Note that we've discharged all the facts
519 d_factsAsserted
= false;
522 CVC4_FOR_EACH_THEORY
;
524 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running propagation after the initial check" << endl
;
526 // We are still satisfiable, propagate as much as possible
529 // We do combination if all has been processed and we are in fullcheck
530 if (Theory::fullEffort(effort
) && d_logicInfo
.isSharingEnabled() && !d_factsAsserted
&& !d_lemmasAdded
&& !d_inConflict
) {
531 // Do the combination
532 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running combination" << endl
;
534 TimerStat::CodeTimer
combineTheoriesTimer(d_combineTheoriesTime
);
535 d_tc
->combineTheories();
537 if(d_logicInfo
.isQuantified()){
538 d_quantEngine
->notifyCombineTheories();
543 // Must consult quantifiers theory for last call to ensure sat, or otherwise add a lemma
544 if( Theory::fullEffort(effort
) && ! d_inConflict
&& ! needCheck() ) {
545 Trace("theory::assertions-model") << endl
;
546 if (Trace
.isOn("theory::assertions-model")) {
547 printAssertions("theory::assertions-model");
549 // reset the model in the combination engine
551 //checks for theories requiring the model go at last call
552 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
553 if( theoryId
!=THEORY_QUANTIFIERS
){
554 Theory
* theory
= d_theoryTable
[theoryId
];
555 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
556 if( theory
->needsCheckLastEffort() ){
557 if (!d_tc
->buildModel())
561 theory
->check(Theory::EFFORT_LAST_CALL
);
568 if(d_logicInfo
.isQuantified()) {
569 // quantifiers engine must check at last call effort
570 d_quantEngine
->check(Theory::EFFORT_LAST_CALL
);
573 if (!d_inConflict
&& !needCheck())
575 // If d_eager_model_building is false, then we only mark that we
576 // are in "SAT mode". We build the model later only if the user asks
577 // for it via getBuiltModel.
579 if (d_eager_model_building
)
586 Debug("theory") << "TheoryEngine::check(" << effort
<< "): done, we are " << (d_inConflict
? "unsat" : "sat") << (d_lemmasAdded
? " with new lemmas" : " with no new lemmas");
587 Debug("theory") << ", need check = " << (needCheck() ? "YES" : "NO") << endl
;
589 if( Theory::fullEffort(effort
) && !d_inConflict
&& !needCheck()) {
590 // Do post-processing of model from the theories (e.g. used for THEORY_SEP
591 // to construct heap model)
592 d_tc
->postProcessModel(d_incomplete
.get());
594 } catch(const theory::Interrupted
&) {
595 Trace("theory") << "TheoryEngine::check() => interrupted" << endl
;
597 // If fulleffort, check all theories
598 if(Dump
.isOn("theory::fullcheck") && Theory::fullEffort(effort
)) {
599 if (!d_inConflict
&& !needCheck()) {
600 dumpAssertions("theory::fullcheck");
605 void TheoryEngine::propagate(Theory::Effort effort
)
607 // Reset the interrupt flag
608 d_interrupted
= false;
610 // Definition of the statement that is to be run by every theory
611 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
612 #undef CVC4_FOR_EACH_THEORY_STATEMENT
614 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
615 if (theory::TheoryTraits<THEORY>::hasPropagate && d_logicInfo.isTheoryEnabled(THEORY)) { \
616 theoryOf(THEORY)->propagate(effort); \
619 // Reset the interrupt flag
620 d_interrupted
= false;
622 // Propagate for each theory using the statement above
623 CVC4_FOR_EACH_THEORY
;
626 Node
TheoryEngine::getNextDecisionRequest()
628 return d_decManager
->getNextDecisionRequest();
631 bool TheoryEngine::properConflict(TNode conflict
) const {
633 if (conflict
.getKind() == kind::AND
) {
634 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++ i
) {
635 if (! getPropEngine()->hasValue(conflict
[i
], value
)) {
636 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
637 << conflict
[i
] << endl
;
641 Debug("properConflict") << "Bad conflict is due to false atom: "
642 << conflict
[i
] << endl
;
645 if (conflict
[i
] != Rewriter::rewrite(conflict
[i
])) {
646 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
647 << conflict
[i
] << " vs " << Rewriter::rewrite(conflict
[i
]) << endl
;
652 if (! getPropEngine()->hasValue(conflict
, value
)) {
653 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
658 Debug("properConflict") << "Bad conflict is due to false atom: "
662 if (conflict
!= Rewriter::rewrite(conflict
)) {
663 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
664 << conflict
<< " vs " << Rewriter::rewrite(conflict
) << endl
;
671 TheoryModel
* TheoryEngine::getModel()
673 Assert(d_tc
!= nullptr);
674 TheoryModel
* m
= d_tc
->getModel();
675 Assert(m
!= nullptr);
679 TheoryModel
* TheoryEngine::getBuiltModel()
681 Assert(d_tc
!= nullptr);
682 // If this method was called, we should be in SAT mode, and produceModels
684 AlwaysAssert(options::produceModels());
687 // not available, perhaps due to interuption.
690 // must build model at this point
691 if (!d_tc
->buildModel())
695 return d_tc
->getModel();
698 bool TheoryEngine::buildModel()
700 Assert(d_tc
!= nullptr);
701 return d_tc
->buildModel();
704 bool TheoryEngine::getSynthSolutions(
705 std::map
<Node
, std::map
<Node
, Node
>>& sol_map
)
709 return d_quantEngine
->getSynthSolutions(sol_map
);
711 // we are not in a quantified logic, there is no synthesis solution
715 bool TheoryEngine::presolve() {
716 // Reset the interrupt flag
717 d_interrupted
= false;
719 // Reset the decision manager. This clears its decision strategies that are
720 // no longer valid in this user context.
721 d_decManager
->presolve();
724 // Definition of the statement that is to be run by every theory
725 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
726 #undef CVC4_FOR_EACH_THEORY_STATEMENT
728 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
729 if (theory::TheoryTraits<THEORY>::hasPresolve) { \
730 theoryOf(THEORY)->presolve(); \
736 // Presolve for each theory using the statement above
737 CVC4_FOR_EACH_THEORY
;
738 } catch(const theory::Interrupted
&) {
739 Trace("theory") << "TheoryEngine::presolve() => interrupted" << endl
;
741 // return whether we have a conflict
743 }/* TheoryEngine::presolve() */
745 void TheoryEngine::postsolve() {
746 // no longer in SAT mode
748 // Reset the interrupt flag
749 d_interrupted
= false;
750 bool CVC4_UNUSED wasInConflict
= d_inConflict
;
753 // Definition of the statement that is to be run by every theory
754 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
755 #undef CVC4_FOR_EACH_THEORY_STATEMENT
757 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
758 if (theory::TheoryTraits<THEORY>::hasPostsolve) \
760 theoryOf(THEORY)->postsolve(); \
761 Assert(!d_inConflict || wasInConflict) \
762 << "conflict raised during postsolve()"; \
765 // Postsolve for each theory using the statement above
766 CVC4_FOR_EACH_THEORY
;
767 } catch(const theory::Interrupted
&) {
768 Trace("theory") << "TheoryEngine::postsolve() => interrupted" << endl
;
770 }/* TheoryEngine::postsolve() */
773 void TheoryEngine::notifyRestart() {
774 // Reset the interrupt flag
775 d_interrupted
= false;
777 // Definition of the statement that is to be run by every theory
778 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
779 #undef CVC4_FOR_EACH_THEORY_STATEMENT
781 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
782 if (theory::TheoryTraits<THEORY>::hasNotifyRestart && d_logicInfo.isTheoryEnabled(THEORY)) { \
783 theoryOf(THEORY)->notifyRestart(); \
786 // notify each theory using the statement above
787 CVC4_FOR_EACH_THEORY
;
790 void TheoryEngine::ppStaticLearn(TNode in
, NodeBuilder
<>& learned
) {
791 // Reset the interrupt flag
792 d_interrupted
= false;
794 // Definition of the statement that is to be run by every theory
795 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
796 #undef CVC4_FOR_EACH_THEORY_STATEMENT
798 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
799 if (theory::TheoryTraits<THEORY>::hasPpStaticLearn) { \
800 theoryOf(THEORY)->ppStaticLearn(in, learned); \
803 // static learning for each theory using the statement above
804 CVC4_FOR_EACH_THEORY
;
807 bool TheoryEngine::isRelevant(Node lit
) const
809 if (d_relManager
!= nullptr)
811 return d_relManager
->isRelevant(lit
);
813 // otherwise must assume its relevant
817 void TheoryEngine::shutdown() {
818 // Set this first; if a Theory shutdown() throws an exception,
819 // at least the destruction of the TheoryEngine won't confound
821 d_hasShutDown
= true;
823 // Shutdown all the theories
824 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
825 if(d_theoryTable
[theoryId
]) {
826 theoryOf(theoryId
)->shutdown();
831 theory::Theory::PPAssertStatus
TheoryEngine::solve(
832 TrustNode tliteral
, TrustSubstitutionMap
& substitutionOut
)
834 // Reset the interrupt flag
835 d_interrupted
= false;
837 TNode literal
= tliteral
.getNode();
838 TNode atom
= literal
.getKind() == kind::NOT
? literal
[0] : literal
;
839 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< "): solving with " << theoryOf(atom
)->getId() << endl
;
841 if(! d_logicInfo
.isTheoryEnabled(Theory::theoryOf(atom
)) &&
842 Theory::theoryOf(atom
) != THEORY_SAT_SOLVER
) {
844 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
845 << ", which doesn't include " << Theory::theoryOf(atom
)
846 << ", but got a preprocessing-time fact for that theory." << endl
847 << "The fact:" << endl
849 throw LogicException(ss
.str());
852 Theory::PPAssertStatus solveStatus
=
853 theoryOf(atom
)->ppAssert(tliteral
, substitutionOut
);
854 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< ") => " << solveStatus
<< endl
;
858 TrustNode
TheoryEngine::preprocess(TNode assertion
)
860 return d_tpp
.theoryPreprocess(assertion
);
863 void TheoryEngine::notifyPreprocessedAssertions(
864 const std::vector
<Node
>& assertions
) {
865 // call all the theories
866 for (TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
;
868 if (d_theoryTable
[theoryId
]) {
869 theoryOf(theoryId
)->ppNotifyAssertions(assertions
);
872 if (d_relManager
!= nullptr)
874 d_relManager
->notifyPreprocessedAssertions(assertions
);
878 bool TheoryEngine::markPropagation(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
880 // What and where we are asserting
881 NodeTheoryPair
toAssert(assertion
, toTheoryId
, d_propagationMapTimestamp
);
882 // What and where it came from
883 NodeTheoryPair
toExplain(originalAssertion
, fromTheoryId
, d_propagationMapTimestamp
);
885 // See if the theory already got this literal
886 PropagationMap::const_iterator find
= d_propagationMap
.find(toAssert
);
887 if (find
!= d_propagationMap
.end()) {
888 // The theory already knows this
889 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): already there" << endl
;
893 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): marking [" << d_propagationMapTimestamp
<< "] " << assertion
<< ", " << toTheoryId
<< " from " << originalAssertion
<< ", " << fromTheoryId
<< endl
;
895 // Mark the propagation
896 d_propagationMap
[toAssert
] = toExplain
;
897 d_propagationMapTimestamp
= d_propagationMapTimestamp
+ 1;
903 void TheoryEngine::assertToTheory(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
905 Trace("theory::assertToTheory") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << originalAssertion
<< "," << toTheoryId
<< ", " << fromTheoryId
<< ")" << endl
;
907 Assert(toTheoryId
!= fromTheoryId
);
908 if(toTheoryId
!= THEORY_SAT_SOLVER
&&
909 ! d_logicInfo
.isTheoryEnabled(toTheoryId
)) {
911 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
912 << ", which doesn't include " << toTheoryId
913 << ", but got an asserted fact to that theory." << endl
914 << "The fact:" << endl
916 throw LogicException(ss
.str());
923 // If sharing is disabled, things are easy
924 if (!d_logicInfo
.isSharingEnabled()) {
925 Assert(assertion
== originalAssertion
);
926 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
927 // Send to the apropriate theory
928 theory::Theory
* toTheory
= theoryOf(toTheoryId
);
929 // We assert it, and we know it's preregistereed
930 toTheory
->assertFact(assertion
, true);
931 // Mark that we have more information
932 d_factsAsserted
= true;
934 Assert(toTheoryId
== THEORY_SAT_SOLVER
);
935 // Check for propositional conflict
937 if (d_propEngine
->hasValue(assertion
, value
)) {
939 Trace("theory::propagate") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (no sharing)" << endl
;
940 Trace("dtview::conflict")
941 << ":THEORY-CONFLICT: " << assertion
<< std::endl
;
947 d_propagatedLiterals
.push_back(assertion
);
952 // If sending to the shared terms database, it's also simple
953 if (toTheoryId
== THEORY_BUILTIN
) {
954 Assert(assertion
.getKind() == kind::EQUAL
955 || (assertion
.getKind() == kind::NOT
956 && assertion
[0].getKind() == kind::EQUAL
))
957 << "atom should be an EQUALity, not `" << assertion
<< "'";
958 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
959 // assert to the shared solver
960 bool polarity
= assertion
.getKind() != kind::NOT
;
961 TNode atom
= polarity
? assertion
: assertion
[0];
962 d_sharedSolver
->assertSharedEquality(atom
, polarity
, assertion
);
967 // Things from the SAT solver are already normalized, so they go
968 // directly to the apropriate theory
969 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
970 // We know that this is normalized, so just send it off to the theory
971 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
972 // Is it preregistered
973 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
975 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
976 // Mark that we have more information
977 d_factsAsserted
= true;
982 // Propagations to the SAT solver are just enqueued for pickup by
983 // the SAT solver later
984 if (toTheoryId
== THEORY_SAT_SOLVER
) {
985 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
986 // Enqueue for propagation to the SAT solver
987 d_propagatedLiterals
.push_back(assertion
);
988 // Check for propositional conflicts
990 if (d_propEngine
->hasValue(assertion
, value
) && !value
) {
991 Trace("theory::propagate")
992 << "TheoryEngine::assertToTheory(" << assertion
<< ", "
993 << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (sharing)"
995 Trace("dtview::conflict")
996 << ":THEORY-CONFLICT: " << assertion
<< std::endl
;
1003 Assert(assertion
.getKind() == kind::EQUAL
1004 || (assertion
.getKind() == kind::NOT
1005 && assertion
[0].getKind() == kind::EQUAL
));
1008 Node normalizedLiteral
= Rewriter::rewrite(assertion
);
1010 // See if it rewrites false directly -> conflict
1011 if (normalizedLiteral
.isConst()) {
1012 if (!normalizedLiteral
.getConst
<bool>()) {
1013 // Mark the propagation for explanations
1014 if (markPropagation(normalizedLiteral
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1015 // special case, trust node has no proof generator
1016 TrustNode trnn
= TrustNode::mkTrustConflict(normalizedLiteral
);
1017 // Get the explanation (conflict will figure out where it came from)
1018 conflict(trnn
, toTheoryId
);
1026 // Try and assert (note that we assert the non-normalized one)
1027 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1028 // Check if has been pre-registered with the theory
1029 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
1031 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
1032 d_factsAsserted
= true;
1038 void TheoryEngine::assertFact(TNode literal
)
1040 Trace("theory") << "TheoryEngine::assertFact(" << literal
<< ")" << endl
;
1044 // If we're in conflict, nothing to do
1050 bool polarity
= literal
.getKind() != kind::NOT
;
1051 TNode atom
= polarity
? literal
: literal
[0];
1053 if (d_logicInfo
.isSharingEnabled()) {
1054 // If any shared terms, it's time to do sharing work
1055 d_sharedSolver
->preNotifySharedFact(atom
);
1057 // If it's an equality, assert it to the shared term manager, even though the terms are not
1058 // yet shared. As the terms become shared later, the shared terms manager will then add them
1059 // to the assert the equality to the interested theories
1060 if (atom
.getKind() == kind::EQUAL
) {
1061 // Assert it to the the owning theory
1062 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1063 // Shared terms manager will assert to interested theories directly, as
1064 // the terms become shared
1065 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ THEORY_SAT_SOLVER
);
1067 // Now, let's check for any atom triggers from lemmas
1068 AtomRequests::atom_iterator it
= d_atomRequests
.getAtomIterator(atom
);
1069 while (!it
.done()) {
1070 const AtomRequests::Request
& request
= it
.get();
1072 polarity
? (Node
)request
.d_atom
: request
.d_atom
.notNode();
1073 Debug("theory::atoms") << "TheoryEngine::assertFact(" << literal
<< "): sending requested " << toAssert
<< endl
;
1075 toAssert
, literal
, request
.d_toTheory
, THEORY_SAT_SOLVER
);
1080 // Not an equality, just assert to the appropriate theory
1081 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1084 // Assert the fact to the appropriate theory directly
1085 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1089 bool TheoryEngine::propagate(TNode literal
, theory::TheoryId theory
) {
1090 Debug("theory::propagate") << "TheoryEngine::propagate(" << literal
<< ", " << theory
<< ")" << endl
;
1092 Trace("dtview::prop") << std::string(d_context
->getLevel(), ' ')
1093 << ":THEORY-PROP: " << literal
<< endl
;
1098 bool polarity
= literal
.getKind() != kind::NOT
;
1099 TNode atom
= polarity
? literal
: literal
[0];
1101 if (d_logicInfo
.isSharingEnabled() && atom
.getKind() == kind::EQUAL
) {
1102 if (d_propEngine
->isSatLiteral(literal
)) {
1103 // We propagate SAT literals to SAT
1104 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1106 if (theory
!= THEORY_BUILTIN
) {
1107 // Assert to the shared terms database
1108 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ theory
);
1111 // Just send off to the SAT solver
1112 Assert(d_propEngine
->isSatLiteral(literal
));
1113 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1116 return !d_inConflict
;
1119 const LogicInfo
& TheoryEngine::getLogicInfo() const { return d_logicInfo
; }
1121 bool TheoryEngine::getSepHeapTypes(TypeNode
& locType
, TypeNode
& dataType
) const
1123 if (d_sepLocType
.isNull())
1127 locType
= d_sepLocType
;
1128 dataType
= d_sepDataType
;
1132 void TheoryEngine::declareSepHeap(TypeNode locT
, TypeNode dataT
)
1134 Theory
* tsep
= theoryOf(THEORY_SEP
);
1135 if (tsep
== nullptr)
1137 Assert(false) << "TheoryEngine::declareSepHeap called without the "
1138 "separation logic theory enabled";
1142 // Definition of the statement that is to be run by every theory
1143 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
1144 #undef CVC4_FOR_EACH_THEORY_STATEMENT
1146 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
1147 theoryOf(THEORY)->declareSepHeap(locT, dataT);
1149 // notify each theory using the statement above
1150 CVC4_FOR_EACH_THEORY
;
1152 // remember the types we have set
1153 d_sepLocType
= locT
;
1154 d_sepDataType
= dataT
;
1157 theory::EqualityStatus
TheoryEngine::getEqualityStatus(TNode a
, TNode b
) {
1158 Assert(a
.getType().isComparableTo(b
.getType()));
1159 return d_sharedSolver
->getEqualityStatus(a
, b
);
1162 Node
TheoryEngine::getModelValue(TNode var
) {
1165 // the model value of a constant must be itself
1168 Assert(d_sharedSolver
->isShared(var
));
1169 return theoryOf(Theory::theoryOf(var
.getType()))->getModelValue(var
);
1173 Node
TheoryEngine::ensureLiteral(TNode n
) {
1174 Debug("ensureLiteral") << "rewriting: " << n
<< std::endl
;
1175 Node rewritten
= Rewriter::rewrite(n
);
1176 Debug("ensureLiteral") << " got: " << rewritten
<< std::endl
;
1177 std::vector
<TrustNode
> newLemmas
;
1178 std::vector
<Node
> newSkolems
;
1179 TrustNode tpn
= d_tpp
.preprocess(n
, newLemmas
, newSkolems
, true);
1180 // send lemmas corresponding to the skolems introduced by preprocessing n
1181 for (const TrustNode
& tnl
: newLemmas
)
1183 lemma(tnl
, LemmaProperty::NONE
);
1185 Node preprocessed
= tpn
.isNull() ? rewritten
: tpn
.getNode();
1186 Debug("ensureLiteral") << "preprocessed: " << preprocessed
<< std::endl
;
1187 d_propEngine
->ensureLiteral(preprocessed
);
1188 return preprocessed
;
1192 void TheoryEngine::printInstantiations( std::ostream
& out
) {
1193 if( d_quantEngine
){
1194 d_quantEngine
->printInstantiations( out
);
1196 out
<< "Internal error : instantiations not available when quantifiers are not present." << std::endl
;
1201 void TheoryEngine::printSynthSolution( std::ostream
& out
) {
1202 if( d_quantEngine
){
1203 d_quantEngine
->printSynthSolution( out
);
1205 out
<< "Internal error : synth solution not available when quantifiers are not present." << std::endl
;
1210 void TheoryEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1211 if( d_quantEngine
){
1212 d_quantEngine
->getInstantiatedQuantifiedFormulas( qs
);
1218 void TheoryEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1219 if( d_quantEngine
){
1220 d_quantEngine
->getInstantiationTermVectors( q
, tvecs
);
1226 void TheoryEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1227 if( d_quantEngine
){
1228 d_quantEngine
->getInstantiationTermVectors( insts
);
1234 TrustNode
TheoryEngine::getExplanation(TNode node
)
1236 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
1237 << "): current propagation index = "
1238 << d_propagationMapTimestamp
<< endl
;
1239 bool polarity
= node
.getKind() != kind::NOT
;
1240 TNode atom
= polarity
? node
: node
[0];
1242 // If we're not in shared mode, explanations are simple
1243 if (!d_logicInfo
.isSharingEnabled())
1245 Debug("theory::explain")
1246 << "TheoryEngine::getExplanation: sharing is NOT enabled. "
1247 << " Responsible theory is: " << theoryOf(atom
)->getId() << std::endl
;
1249 TrustNode texplanation
= theoryOf(atom
)->explain(node
);
1250 Node explanation
= texplanation
.getNode();
1251 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
1252 << ") => " << explanation
<< endl
;
1253 if (isProofEnabled())
1255 texplanation
.debugCheckClosed(
1256 "te-proof-exp", "texplanation no share", false);
1257 // check if no generator, if so, add THEORY_LEMMA
1258 if (texplanation
.getGenerator() == nullptr)
1260 Node proven
= texplanation
.getProven();
1261 TheoryId tid
= theoryOf(atom
)->getId();
1262 Node tidn
= builtin::BuiltinProofRuleChecker::mkTheoryIdNode(tid
);
1263 d_lazyProof
->addStep(proven
, PfRule::THEORY_LEMMA
, {}, {proven
, tidn
});
1265 TrustNode::mkTrustPropExp(node
, explanation
, d_lazyProof
.get());
1268 return texplanation
;
1271 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing IS enabled"
1274 // Initial thing to explain
1275 NodeTheoryPair
toExplain(node
, THEORY_SAT_SOLVER
, d_propagationMapTimestamp
);
1276 Assert(d_propagationMap
.find(toExplain
) != d_propagationMap
.end());
1278 NodeTheoryPair nodeExplainerPair
= d_propagationMap
[toExplain
];
1279 Debug("theory::explain")
1280 << "TheoryEngine::getExplanation: explainer for node "
1281 << nodeExplainerPair
.d_node
1282 << " is theory: " << nodeExplainerPair
.d_theory
<< std::endl
;
1284 // Create the workplace for explanations
1285 std::vector
<NodeTheoryPair
> vec
{d_propagationMap
[toExplain
]};
1286 // Process the explanation
1287 TrustNode texplanation
= getExplanation(vec
);
1288 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< ") => "
1289 << texplanation
.getNode() << endl
;
1290 return texplanation
;
1293 struct AtomsCollect
{
1295 std::vector
<TNode
> d_atoms
;
1296 std::unordered_set
<TNode
, TNodeHashFunction
> d_visited
;
1300 typedef void return_type
;
1302 bool alreadyVisited(TNode current
, TNode parent
) {
1303 // Check if already visited
1304 if (d_visited
.find(current
) != d_visited
.end()) return true;
1305 // Don't visit non-boolean
1306 if (!current
.getType().isBoolean()) return true;
1311 void visit(TNode current
, TNode parent
) {
1312 if (Theory::theoryOf(current
) != theory::THEORY_BOOL
) {
1313 d_atoms
.push_back(current
);
1315 d_visited
.insert(current
);
1318 void start(TNode node
) {}
1319 void done(TNode node
) {}
1321 std::vector
<TNode
> getAtoms() const {
1326 void TheoryEngine::ensureLemmaAtoms(const std::vector
<TNode
>& atoms
, theory::TheoryId atomsTo
) {
1327 for (unsigned i
= 0; i
< atoms
.size(); ++ i
) {
1329 // Non-equality atoms are either owned by theory or they don't make sense
1330 if (atoms
[i
].getKind() != kind::EQUAL
) {
1336 // Simple normalization to not repeat stuff
1337 if (eq
[0] > eq
[1]) {
1338 eq
= eq
[1].eqNode(eq
[0]);
1341 // Rewrite the equality
1342 Node eqNormalized
= Rewriter::rewrite(atoms
[i
]);
1344 Debug("theory::atoms") << "TheoryEngine::ensureLemmaAtoms(): " << eq
<< " with nf " << eqNormalized
<< endl
;
1346 // If the equality is a boolean constant, we send immediately
1347 if (eqNormalized
.isConst()) {
1348 if (eqNormalized
.getConst
<bool>()) {
1349 assertToTheory(eq
, eqNormalized
, /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1351 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1354 }else if( eqNormalized
.getKind() != kind::EQUAL
){
1355 Assert(eqNormalized
.getKind() == kind::BOOLEAN_TERM_VARIABLE
1356 || (eqNormalized
.getKind() == kind::NOT
1357 && eqNormalized
[0].getKind() == kind::BOOLEAN_TERM_VARIABLE
));
1358 // this happens for Boolean term equalities V = true that are rewritten to V, we should skip
1359 // TODO : revisit this
1363 // If the normalization did the just flips, keep the flip
1364 if (eqNormalized
[0] == eq
[1] && eqNormalized
[1] == eq
[0]) {
1368 // Check if the equality is already known by the sat solver
1369 if (d_propEngine
->isSatLiteral(eqNormalized
)) {
1371 if (d_propEngine
->hasValue(eqNormalized
, value
)) {
1373 assertToTheory(eq
, eqNormalized
, atomsTo
, theory::THEORY_SAT_SOLVER
);
1376 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), atomsTo
, theory::THEORY_SAT_SOLVER
);
1382 // If the theory is asking about a different form, or the form is ok but if will go to a different theory
1383 // then we must figure it out
1384 if (eqNormalized
!= eq
|| Theory::theoryOf(eq
) != atomsTo
) {
1385 // If you get eqNormalized, send atoms[i] to atomsTo
1386 d_atomRequests
.add(eqNormalized
, eq
, atomsTo
);
1391 theory::LemmaStatus
TheoryEngine::lemma(theory::TrustNode tlemma
,
1392 theory::LemmaProperty p
,
1393 theory::TheoryId atomsTo
,
1394 theory::TheoryId from
)
1396 // For resource-limiting (also does a time check).
1398 Assert(tlemma
.getKind() == TrustNodeKind::LEMMA
1399 || tlemma
.getKind() == TrustNodeKind::CONFLICT
);
1401 Node node
= tlemma
.getNode();
1402 Node lemma
= tlemma
.getProven();
1404 Assert(!expr::hasFreeVar(lemma
));
1406 // when proofs are enabled, we ensure the trust node has a generator by
1407 // adding a trust step to the lazy proof maintained by this class
1408 if (isProofEnabled())
1410 // ensure proof: set THEORY_LEMMA if no generator is provided
1411 if (tlemma
.getGenerator() == nullptr)
1413 // internal lemmas should have generators
1414 Assert(from
!= THEORY_LAST
);
1415 // add theory lemma step to proof
1416 Node tidn
= builtin::BuiltinProofRuleChecker::mkTheoryIdNode(from
);
1417 d_lazyProof
->addStep(lemma
, PfRule::THEORY_LEMMA
, {}, {lemma
, tidn
});
1418 // update the trust node
1419 tlemma
= TrustNode::mkTrustLemma(lemma
, d_lazyProof
.get());
1422 tlemma
.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma_initial");
1425 // Do we need to check atoms
1426 if (atomsTo
!= theory::THEORY_LAST
) {
1427 Debug("theory::atoms") << "TheoryEngine::lemma(" << node
<< ", " << atomsTo
<< ")" << endl
;
1428 AtomsCollect collectAtoms
;
1429 NodeVisitor
<AtomsCollect
>::run(collectAtoms
, node
);
1430 ensureLemmaAtoms(collectAtoms
.getAtoms(), atomsTo
);
1433 if(Dump
.isOn("t-lemmas")) {
1434 // we dump the negation of the lemma, to show validity of the lemma
1435 Node n
= lemma
.negate();
1436 const Printer
& printer
= d_outMgr
.getPrinter();
1437 std::ostream
& out
= d_outMgr
.getDumpOut();
1438 printer
.toStreamCmdComment(out
, "theory lemma: expect valid");
1439 printer
.toStreamCmdCheckSat(out
, n
);
1441 bool removable
= isLemmaPropertyRemovable(p
);
1442 bool preprocess
= isLemmaPropertyPreprocess(p
);
1445 tlemma
.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma_initial");
1447 // call preprocessor
1448 std::vector
<TrustNode
> newLemmas
;
1449 std::vector
<Node
> newSkolems
;
1451 d_tpp
.preprocess(lemma
, newLemmas
, newSkolems
, preprocess
);
1452 // the preprocessed lemma
1454 if (tplemma
.isNull())
1460 Assert(tplemma
.getKind() == TrustNodeKind::REWRITE
);
1461 lemmap
= tplemma
.getNode();
1463 // must update the trust lemma
1464 if (lemmap
!= lemma
)
1466 // process the preprocessing
1467 if (isProofEnabled())
1469 Assert(d_lazyProof
!= nullptr);
1470 // add the original proof to the lazy proof
1471 d_lazyProof
->addLazyStep(tlemma
.getProven(), tlemma
.getGenerator());
1472 // only need to do anything if lemmap changed in a non-trivial way
1473 if (!CDProof::isSame(lemmap
, lemma
))
1475 d_lazyProof
->addLazyStep(tplemma
.getProven(),
1476 tplemma
.getGenerator(),
1477 PfRule::PREPROCESS_LEMMA
,
1479 "TheoryEngine::lemma_pp");
1480 // ---------- from d_lazyProof -------------- from theory preprocess
1481 // lemma lemma = lemmap
1482 // ------------------------------------------ EQ_RESOLVE
1484 std::vector
<Node
> pfChildren
;
1485 pfChildren
.push_back(lemma
);
1486 pfChildren
.push_back(tplemma
.getProven());
1487 d_lazyProof
->addStep(lemmap
, PfRule::EQ_RESOLVE
, pfChildren
, {});
1490 tlemma
= TrustNode::mkTrustLemma(lemmap
, d_lazyProof
.get());
1494 // must use an assertion pipeline due to decision engine below
1495 AssertionPipeline lemmas
;
1496 // make the assertion pipeline
1497 lemmas
.push_back(lemmap
);
1498 lemmas
.updateRealAssertionsEnd();
1499 Assert(newSkolems
.size() == newLemmas
.size());
1500 for (size_t i
= 0, nsize
= newLemmas
.size(); i
< nsize
; i
++)
1502 // store skolem mapping here
1503 IteSkolemMap
& imap
= lemmas
.getIteSkolemMap();
1504 imap
[newSkolems
[i
]] = lemmas
.size();
1505 lemmas
.push_back(newLemmas
[i
].getNode());
1508 // If specified, we must add this lemma to the set of those that need to be
1509 // justified, where note we pass all auxiliary lemmas in lemmas, since these
1510 // by extension must be justified as well.
1511 if (d_relManager
!= nullptr && isLemmaPropertyNeedsJustify(p
))
1513 d_relManager
->notifyPreprocessedAssertions(lemmas
.ref());
1516 // do final checks on the lemmas we are about to send
1517 if (isProofEnabled())
1519 Assert(tlemma
.getGenerator() != nullptr);
1520 // ensure closed, make the proof node eagerly here to debug
1521 tlemma
.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma");
1522 for (size_t i
= 0, lsize
= newLemmas
.size(); i
< lsize
; ++i
)
1524 Assert(newLemmas
[i
].getGenerator() != nullptr);
1525 newLemmas
[i
].debugCheckClosed("te-proof-debug",
1526 "TheoryEngine::lemma_new");
1530 // now, send the lemmas to the prop engine
1531 d_propEngine
->assertLemma(tlemma
.getProven(), false, removable
);
1532 for (size_t i
= 0, lsize
= newLemmas
.size(); i
< lsize
; ++i
)
1534 d_propEngine
->assertLemma(newLemmas
[i
].getProven(), false, removable
);
1537 // assert to decision engine
1540 d_propEngine
->addAssertionsToDecisionEngine(lemmas
);
1543 // Mark that we added some lemmas
1544 d_lemmasAdded
= true;
1546 // Lemma analysis isn't online yet; this lemma may only live for this
1548 Node retLemma
= lemmas
[0];
1549 if (lemmas
.size() > 1)
1551 // the returned lemma is the conjunction of all additional lemmas.
1552 retLemma
= NodeManager::currentNM()->mkNode(kind::AND
, lemmas
.ref());
1554 return theory::LemmaStatus(retLemma
, d_userContext
->getLevel());
1557 void TheoryEngine::conflict(theory::TrustNode tconflict
, TheoryId theoryId
)
1559 Assert(tconflict
.getKind() == TrustNodeKind::CONFLICT
);
1560 TNode conflict
= tconflict
.getNode();
1561 Trace("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", "
1562 << theoryId
<< ")" << endl
;
1563 Trace("te-proof-debug") << "Check closed conflict" << std::endl
;
1564 // doesn't require proof generator, yet, since THEORY_LEMMA is added below
1565 tconflict
.debugCheckClosed(
1566 "te-proof-debug", "TheoryEngine::conflict_initial", false);
1568 Trace("dtview::conflict") << ":THEORY-CONFLICT: " << conflict
<< std::endl
;
1570 // Mark that we are in conflict
1571 d_inConflict
= true;
1573 if(Dump
.isOn("t-conflicts")) {
1574 const Printer
& printer
= d_outMgr
.getPrinter();
1575 std::ostream
& out
= d_outMgr
.getDumpOut();
1576 printer
.toStreamCmdComment(out
, "theory conflict: expect unsat");
1577 printer
.toStreamCmdCheckSat(out
, conflict
);
1580 // In the multiple-theories case, we need to reconstruct the conflict
1581 if (d_logicInfo
.isSharingEnabled()) {
1582 // Create the workplace for explanations
1583 std::vector
<NodeTheoryPair
> vec
;
1585 NodeTheoryPair(conflict
, theoryId
, d_propagationMapTimestamp
));
1587 // Process the explanation
1588 TrustNode tncExp
= getExplanation(vec
);
1589 Trace("te-proof-debug")
1590 << "Check closed conflict explained with sharing" << std::endl
;
1591 tncExp
.debugCheckClosed("te-proof-debug",
1592 "TheoryEngine::conflict_explained_sharing");
1593 Node fullConflict
= tncExp
.getNode();
1595 if (isProofEnabled())
1597 Trace("te-proof-debug") << "Process conflict: " << conflict
<< std::endl
;
1598 Trace("te-proof-debug") << "Conflict " << tconflict
<< " from "
1599 << tconflict
.identifyGenerator() << std::endl
;
1600 Trace("te-proof-debug") << "Explanation " << tncExp
<< " from "
1601 << tncExp
.identifyGenerator() << std::endl
;
1602 Assert(d_lazyProof
!= nullptr);
1603 if (tconflict
.getGenerator() != nullptr)
1605 d_lazyProof
->addLazyStep(tconflict
.getProven(),
1606 tconflict
.getGenerator());
1610 // add theory lemma step
1611 Node tidn
= builtin::BuiltinProofRuleChecker::mkTheoryIdNode(theoryId
);
1612 Node conf
= tconflict
.getProven();
1613 d_lazyProof
->addStep(conf
, PfRule::THEORY_LEMMA
, {}, {conf
, tidn
});
1615 // store the explicit step, which should come from a different
1616 // generator, e.g. d_tepg.
1617 Node proven
= tncExp
.getProven();
1618 Assert(tncExp
.getGenerator() != d_lazyProof
.get());
1619 Trace("te-proof-debug") << "add lazy step " << tncExp
.identifyGenerator()
1620 << " for " << proven
<< std::endl
;
1621 d_lazyProof
->addLazyStep(proven
, tncExp
.getGenerator());
1622 pfgEnsureClosed(proven
,
1625 "TheoryEngine::conflict_during");
1626 Node fullConflictNeg
= fullConflict
.notNode();
1627 std::vector
<Node
> children
;
1628 children
.push_back(proven
);
1629 std::vector
<Node
> args
;
1630 args
.push_back(fullConflictNeg
);
1631 if (conflict
== d_false
)
1633 AlwaysAssert(proven
== fullConflictNeg
);
1637 if (fullConflict
!= conflict
)
1639 // ------------------------- explained ---------- from theory
1640 // fullConflict => conflict ~conflict
1641 // ------------------------------------------ MACRO_SR_PRED_TRANSFORM
1643 children
.push_back(conflict
.notNode());
1644 args
.push_back(mkMethodId(MethodId::SB_LITERAL
));
1645 d_lazyProof
->addStep(
1646 fullConflictNeg
, PfRule::MACRO_SR_PRED_TRANSFORM
, children
, args
);
1650 // pass the processed trust node
1652 TrustNode::mkTrustConflict(fullConflict
, d_lazyProof
.get());
1653 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", " << theoryId
<< "): full = " << fullConflict
<< endl
;
1654 Assert(properConflict(fullConflict
));
1655 Trace("te-proof-debug")
1656 << "Check closed conflict with sharing" << std::endl
;
1657 tconf
.debugCheckClosed("te-proof-debug", "TheoryEngine::conflict:sharing");
1658 lemma(tconf
, LemmaProperty::REMOVABLE
);
1660 // When only one theory, the conflict should need no processing
1661 Assert(properConflict(conflict
));
1662 // pass the trust node that was sent from the theory
1663 lemma(tconflict
, LemmaProperty::REMOVABLE
, THEORY_LAST
, theoryId
);
1667 theory::TrustNode
TheoryEngine::getExplanation(
1668 std::vector
<NodeTheoryPair
>& explanationVector
)
1670 Assert(explanationVector
.size() == 1);
1671 Node conclusion
= explanationVector
[0].d_node
;
1672 std::shared_ptr
<LazyCDProof
> lcp
;
1673 if (isProofEnabled())
1675 Trace("te-proof-exp") << "=== TheoryEngine::getExplanation " << conclusion
1677 lcp
.reset(new LazyCDProof(
1678 d_pnm
, nullptr, nullptr, "TheoryEngine::LazyCDProof::getExplanation"));
1680 unsigned i
= 0; // Index of the current literal we are processing
1682 std::unique_ptr
<std::set
<Node
>> inputAssertions
= nullptr;
1683 // the overall explanation
1684 std::set
<TNode
> exp
;
1685 // vector of trust nodes to explain at the end
1686 std::vector
<std::pair
<TheoryId
, TrustNode
>> texplains
;
1687 // cache of nodes we have already explained by some theory
1688 std::unordered_map
<Node
, size_t, NodeHashFunction
> cache
;
1690 while (i
< explanationVector
.size()) {
1691 // Get the current literal to explain
1692 NodeTheoryPair toExplain
= explanationVector
[i
];
1694 Debug("theory::explain")
1695 << "[i=" << i
<< "] TheoryEngine::explain(): processing ["
1696 << toExplain
.d_timestamp
<< "] " << toExplain
.d_node
<< " sent from "
1697 << toExplain
.d_theory
<< endl
;
1699 if (cache
.find(toExplain
.d_node
) != cache
.end()
1700 && cache
[toExplain
.d_node
] < toExplain
.d_timestamp
)
1705 cache
[toExplain
.d_node
] = toExplain
.d_timestamp
;
1707 // If a true constant or a negation of a false constant we can ignore it
1708 if ((toExplain
.d_node
.isConst() && toExplain
.d_node
.getConst
<bool>())
1709 || (toExplain
.d_node
.getKind() == kind::NOT
1710 && toExplain
.d_node
[0].isConst()
1711 && !toExplain
.d_node
[0].getConst
<bool>()))
1714 // if we are building a proof
1717 Trace("te-proof-exp")
1718 << "- explain " << toExplain
.d_node
<< " trivially..." << std::endl
;
1719 // ------------------MACRO_SR_PRED_INTRO
1721 std::vector
<Node
> children
;
1722 std::vector
<Node
> args
;
1723 args
.push_back(toExplain
.d_node
);
1725 toExplain
.d_node
, PfRule::MACRO_SR_PRED_INTRO
, children
, args
);
1730 // If from the SAT solver, keep it
1731 if (toExplain
.d_theory
== THEORY_SAT_SOLVER
)
1733 Debug("theory::explain") << "\tLiteral came from THEORY_SAT_SOLVER. Kepping it." << endl
;
1734 exp
.insert(explanationVector
[i
++].d_node
);
1735 // it will be a free assumption in the proof
1736 Trace("te-proof-exp") << "- keep " << toExplain
.d_node
<< std::endl
;
1740 // If an and, expand it
1741 if (toExplain
.d_node
.getKind() == kind::AND
)
1743 Debug("theory::explain")
1744 << "TheoryEngine::explain(): expanding " << toExplain
.d_node
1745 << " got from " << toExplain
.d_theory
<< endl
;
1746 size_t nchild
= toExplain
.d_node
.getNumChildren();
1747 for (size_t k
= 0; k
< nchild
; ++k
)
1749 NodeTheoryPair
newExplain(
1750 toExplain
.d_node
[k
], toExplain
.d_theory
, toExplain
.d_timestamp
);
1751 explanationVector
.push_back(newExplain
);
1755 Trace("te-proof-exp")
1756 << "- AND expand " << toExplain
.d_node
<< std::endl
;
1757 // delay explanation, use a dummy trust node
1758 TrustNode tnAndExp
= TrustNode::mkTrustPropExp(
1759 toExplain
.d_node
, toExplain
.d_node
, nullptr);
1760 texplains
.push_back(
1761 std::pair
<TheoryId
, TrustNode
>(THEORY_LAST
, tnAndExp
));
1767 // See if it was sent to the theory by another theory
1768 PropagationMap::const_iterator find
= d_propagationMap
.find(toExplain
);
1769 if (find
!= d_propagationMap
.end()) {
1770 Debug("theory::explain")
1771 << "\tTerm was propagated by another theory (theory = "
1772 << getTheoryString((*find
).second
.d_theory
) << ")" << std::endl
;
1773 // There is some propagation, check if its a timely one
1774 if ((*find
).second
.d_timestamp
< toExplain
.d_timestamp
)
1776 Debug("theory::explain")
1777 << "\tRelevant timetsamp, pushing " << (*find
).second
.d_node
1778 << "to index = " << explanationVector
.size() << std::endl
;
1779 explanationVector
.push_back((*find
).second
);
1784 if (!CDProof::isSame(toExplain
.d_node
, (*find
).second
.d_node
))
1786 Trace("te-proof-exp")
1787 << "- t-explained cached: " << toExplain
.d_node
<< " by "
1788 << (*find
).second
.d_node
<< std::endl
;
1789 // delay explanation, use a dummy trust node that says that
1790 // (*find).second.d_node explains toExplain.d_node.
1791 TrustNode tnRewExp
= TrustNode::mkTrustPropExp(
1792 toExplain
.d_node
, (*find
).second
.d_node
, nullptr);
1793 texplains
.push_back(
1794 std::pair
<TheoryId
, TrustNode
>(THEORY_LAST
, tnRewExp
));
1800 // It was produced by the theory, so ask for an explanation
1801 TrustNode texplanation
=
1802 d_sharedSolver
->explain(toExplain
.d_node
, toExplain
.d_theory
);
1805 texplanation
.debugCheckClosed("te-proof-exp", "texplanation", false);
1806 Trace("te-proof-exp")
1807 << "- t-explained[" << toExplain
.d_theory
<< "]: " << toExplain
.d_node
1808 << " by " << texplanation
.getNode() << std::endl
;
1809 // if not a trivial explanation
1810 if (!CDProof::isSame(texplanation
.getNode(), toExplain
.d_node
))
1812 // We add it to the list of theory explanations, to be processed at
1813 // the end of this method. We wait to explain here because it may
1814 // be that a later explanation may preempt the need for proving this
1815 // step. For instance, if the conclusion lit is later added as an
1816 // assumption in the final explanation. This avoids cyclic proofs.
1817 texplains
.push_back(
1818 std::pair
<TheoryId
, TrustNode
>(toExplain
.d_theory
, texplanation
));
1821 Node explanation
= texplanation
.getNode();
1823 Debug("theory::explain")
1824 << "TheoryEngine::explain(): got explanation " << explanation
1825 << " got from " << toExplain
.d_theory
<< endl
;
1826 Assert(explanation
!= toExplain
.d_node
)
1827 << "wasn't sent to you, so why are you explaining it trivially";
1828 // Mark the explanation
1829 NodeTheoryPair
newExplain(
1830 explanation
, toExplain
.d_theory
, toExplain
.d_timestamp
);
1831 explanationVector
.push_back(newExplain
);
1836 // make the explanation node
1838 if (exp
.size() == 0)
1840 // Normalize to true
1841 expNode
= NodeManager::currentNM()->mkConst
<bool>(true);
1843 else if (exp
.size() == 1)
1845 // All the same, or just one
1846 expNode
= *exp
.begin();
1850 NodeBuilder
<> conjunction(kind::AND
);
1851 std::set
<TNode
>::const_iterator it
= exp
.begin();
1852 std::set
<TNode
>::const_iterator it_end
= exp
.end();
1853 while (it
!= it_end
)
1858 expNode
= conjunction
;
1860 // if we are building a proof, go back through the explanations and
1864 if (Trace
.isOn("te-proof-exp"))
1866 Trace("te-proof-exp") << "Explanation is:" << std::endl
;
1867 for (const Node
& e
: exp
)
1869 Trace("te-proof-exp") << " " << e
<< std::endl
;
1871 Trace("te-proof-exp") << "=== Replay explanations..." << std::endl
;
1873 // Now, go back and add the necessary steps of theory explanations, i.e.
1874 // add those that prove things that aren't in the final explanation. We
1875 // iterate in reverse order so that most recent steps take priority. This
1876 // avoids cyclic proofs in the lazy proof we are building (lcp).
1877 for (std::vector
<std::pair
<TheoryId
, TrustNode
>>::reverse_iterator
1878 it
= texplains
.rbegin(),
1879 itEnd
= texplains
.rend();
1883 TrustNode trn
= it
->second
;
1884 Assert(trn
.getKind() == TrustNodeKind::PROP_EXP
);
1885 Node proven
= trn
.getProven();
1886 Assert(proven
.getKind() == kind::IMPLIES
);
1887 Node tConc
= proven
[1];
1888 Trace("te-proof-exp") << "- Process " << trn
<< std::endl
;
1889 if (exp
.find(tConc
) != exp
.end())
1891 // already added to proof
1892 Trace("te-proof-exp") << "...already added" << std::endl
;
1895 Node symTConc
= CDProof::getSymmFact(tConc
);
1896 if (!symTConc
.isNull())
1898 if (exp
.find(symTConc
) != exp
.end())
1900 // symmetric direction
1901 Trace("te-proof-exp") << "...already added (SYMM)" << std::endl
;
1905 // remember that we've explained this formula, to avoid cycles in lcp
1907 TheoryId ttid
= it
->first
;
1908 Node tExp
= proven
[0];
1909 if (ttid
== THEORY_LAST
)
1913 // dummy trust node, do AND expansion
1914 Assert(tConc
.getKind() == kind::AND
);
1915 // tConc[0] ... tConc[n]
1916 // ---------------------- AND_INTRO
1918 std::vector
<Node
> pfChildren
;
1919 pfChildren
.insert(pfChildren
.end(), tConc
.begin(), tConc
.end());
1920 lcp
->addStep(tConc
, PfRule::AND_INTRO
, pfChildren
, {});
1921 Trace("te-proof-exp") << "...via AND_INTRO" << std::endl
;
1924 // otherwise should hold by rewriting
1925 Assert(Rewriter::rewrite(tConc
) == Rewriter::rewrite(tExp
));
1927 // ---- MACRO_SR_PRED_TRANSFORM
1929 lcp
->addStep(tConc
, PfRule::MACRO_SR_PRED_TRANSFORM
, {tExp
}, {tConc
});
1930 Trace("te-proof-exp") << "...via MACRO_SR_PRED_TRANSFORM" << std::endl
;
1936 Trace("te-proof-exp") << "...trivial" << std::endl
;
1939 // ------------- Via theory
1940 // tExp tExp => tConc
1941 // ---------------------------------MODUS_PONENS
1943 if (trn
.getGenerator() != nullptr)
1945 Trace("te-proof-exp") << "...via theory generator" << std::endl
;
1946 lcp
->addLazyStep(proven
, trn
.getGenerator());
1950 Trace("te-proof-exp") << "...via trust THEORY_LEMMA" << std::endl
;
1951 // otherwise, trusted theory lemma
1952 Node tidn
= builtin::BuiltinProofRuleChecker::mkTheoryIdNode(it
->first
);
1953 lcp
->addStep(proven
, PfRule::THEORY_LEMMA
, {}, {proven
, tidn
});
1955 std::vector
<Node
> pfChildren
;
1956 pfChildren
.push_back(trn
.getNode());
1957 pfChildren
.push_back(proven
);
1958 lcp
->addStep(tConc
, PfRule::MODUS_PONENS
, pfChildren
, {});
1960 // store in the proof generator
1961 TrustNode trn
= d_tepg
->mkTrustExplain(conclusion
, expNode
, lcp
);
1962 // return the trust node
1966 return theory::TrustNode::mkTrustLemma(expNode
, nullptr);
1969 bool TheoryEngine::isProofEnabled() const { return d_pnm
!= nullptr; }
1971 void TheoryEngine::setUserAttribute(const std::string
& attr
,
1973 const std::vector
<Node
>& node_values
,
1974 const std::string
& str_value
)
1976 Trace("te-attr") << "set user attribute " << attr
<< " " << n
<< endl
;
1977 if( d_attr_handle
.find( attr
)!=d_attr_handle
.end() ){
1978 for( size_t i
=0; i
<d_attr_handle
[attr
].size(); i
++ ){
1979 d_attr_handle
[attr
][i
]->setUserAttribute(attr
, n
, node_values
, str_value
);
1982 //unhandled exception?
1986 void TheoryEngine::handleUserAttribute(const char* attr
, Theory
* t
) {
1987 Trace("te-attr") << "Handle user attribute " << attr
<< " " << t
<< endl
;
1988 std::string
str( attr
);
1989 d_attr_handle
[ str
].push_back( t
);
1992 void TheoryEngine::checkTheoryAssertionsWithModel(bool hardFailure
) {
1993 for(TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
1994 Theory
* theory
= d_theoryTable
[theoryId
];
1995 if(theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
1996 for(context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
1997 it_end
= theory
->facts_end();
2000 Node assertion
= (*it
).d_assertion
;
2001 if (!isRelevant(assertion
))
2003 // not relevant, skip
2006 Node val
= d_tc
->getModel()->getValue(assertion
);
2009 std::stringstream ss
;
2010 ss
<< " " << theoryId
2011 << " has an asserted fact that the model doesn't satisfy." << endl
2012 << "The fact: " << assertion
<< endl
2013 << "Model value: " << val
<< endl
;
2018 // Always an error if it is false
2019 InternalError() << ss
.str();
2023 // Otherwise just a warning. Notice this case may happen for
2024 // assertions with unevaluable operators, e.g. transcendental
2025 // functions. It also may happen for separation logic, where
2026 // check-model support is limited.
2027 Warning() << ss
.str();
2036 std::pair
<bool, Node
> TheoryEngine::entailmentCheck(options::TheoryOfMode mode
,
2039 TNode atom
= (lit
.getKind() == kind::NOT
) ? lit
[0] : lit
;
2040 if( atom
.getKind()==kind::AND
|| atom
.getKind()==kind::OR
|| atom
.getKind()==kind::IMPLIES
){
2041 //Boolean connective, recurse
2042 std::vector
< Node
> children
;
2043 bool pol
= (lit
.getKind()!=kind::NOT
);
2044 bool is_conjunction
= pol
==(lit
.getKind()==kind::AND
);
2045 for( unsigned i
=0; i
<atom
.getNumChildren(); i
++ ){
2047 if( pol
==( lit
.getKind()==kind::IMPLIES
&& i
==0 ) ){
2048 ch
= atom
[i
].negate();
2050 std::pair
<bool, Node
> chres
= entailmentCheck(mode
, ch
);
2052 if( !is_conjunction
){
2055 children
.push_back( chres
.second
);
2057 }else if( !chres
.first
&& is_conjunction
){
2058 return std::pair
<bool, Node
>(false, Node::null());
2061 if( is_conjunction
){
2062 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, children
));
2064 return std::pair
<bool, Node
>(false, Node::null());
2066 }else if( atom
.getKind()==kind::ITE
|| ( atom
.getKind()==kind::EQUAL
&& atom
[0].getType().isBoolean() ) ){
2067 bool pol
= (lit
.getKind()!=kind::NOT
);
2068 for( unsigned r
=0; r
<2; r
++ ){
2073 std::pair
<bool, Node
> chres
= entailmentCheck(mode
, ch
);
2075 Node ch2
= atom
[ atom
.getKind()==kind::ITE
? r
+1 : 1 ];
2076 if( pol
==( atom
.getKind()==kind::ITE
? true : r
==1 ) ){
2079 std::pair
<bool, Node
> chres2
= entailmentCheck(mode
, ch2
);
2081 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, chres
.second
, chres2
.second
));
2087 return std::pair
<bool, Node
>(false, Node::null());
2089 //it is a theory atom
2090 theory::TheoryId tid
= theory::Theory::theoryOf(mode
, atom
);
2091 theory::Theory
* th
= theoryOf(tid
);
2094 Trace("theory-engine-entc") << "Entailment check : " << lit
<< std::endl
;
2096 std::pair
<bool, Node
> chres
= th
->entailmentCheck(lit
);
2101 void TheoryEngine::spendResource(ResourceManager::Resource r
)
2103 d_resourceManager
->spendResource(r
);
2106 }/* CVC4 namespace */