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_builder.h"
28 #include "expr/node_visitor.h"
29 #include "expr/proof_ensure_closed.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 "printer/printer.h"
36 #include "smt/logic_exception.h"
37 #include "smt/term_formula_removal.h"
38 #include "theory/arith/arith_ite_utils.h"
39 #include "theory/bv/theory_bv_utils.h"
40 #include "theory/care_graph.h"
41 #include "theory/combination_care_graph.h"
42 #include "theory/decision_manager.h"
43 #include "theory/quantifiers/first_order_model.h"
44 #include "theory/quantifiers/fmf/model_engine.h"
45 #include "theory/quantifiers/theory_quantifiers.h"
46 #include "theory/quantifiers_engine.h"
47 #include "theory/relevance_manager.h"
48 #include "theory/rewriter.h"
49 #include "theory/theory.h"
50 #include "theory/theory_engine_proof_generator.h"
51 #include "theory/theory_id.h"
52 #include "theory/theory_model.h"
53 #include "theory/theory_traits.h"
54 #include "theory/uf/equality_engine.h"
55 #include "util/resource_manager.h"
59 using namespace CVC4::preprocessing
;
60 using namespace CVC4::theory
;
64 /* -------------------------------------------------------------------------- */
69 * IMPORTANT: The order of the theories is important. For example, strings
70 * depends on arith, quantifiers needs to come as the very last.
71 * Do not change this order.
74 #define CVC4_FOR_EACH_THEORY \
75 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BUILTIN) \
76 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BOOL) \
77 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_UF) \
78 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_ARITH) \
79 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BV) \
80 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_FP) \
81 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_ARRAYS) \
82 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_DATATYPES) \
83 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_SEP) \
84 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_SETS) \
85 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_BAGS) \
86 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_STRINGS) \
87 CVC4_FOR_EACH_THEORY_STATEMENT(CVC4::theory::THEORY_QUANTIFIERS)
91 /* -------------------------------------------------------------------------- */
93 inline void flattenAnd(Node n
, std::vector
<TNode
>& out
){
94 Assert(n
.getKind() == kind::AND
);
95 for(Node::iterator i
=n
.begin(), i_end
=n
.end(); i
!= i_end
; ++i
){
97 if(curr
.getKind() == kind::AND
){
98 flattenAnd(curr
, out
);
105 inline Node
flattenAnd(Node n
){
106 std::vector
<TNode
> out
;
108 return NodeManager::currentNM()->mkNode(kind::AND
, out
);
112 * Compute the string for a given theory id. In this module, we use
113 * THEORY_SAT_SOLVER as an id, which is not a normal id but maps to
114 * THEORY_LAST. Thus, we need our own string conversion here.
116 * @param id The theory id
117 * @return The string corresponding to the theory id
119 std::string
getTheoryString(theory::TheoryId id
)
121 if (id
== theory::THEORY_SAT_SOLVER
)
123 return "THEORY_SAT_SOLVER";
127 std::stringstream ss
;
133 void TheoryEngine::finishInit()
135 // NOTE: This seems to be required since
136 // theory::TheoryTraits<THEORY>::isParametric cannot be accessed without
137 // using the CVC4_FOR_EACH_THEORY_STATEMENT macro. -AJR
138 std::vector
<theory::Theory
*> paraTheories
;
139 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
140 #undef CVC4_FOR_EACH_THEORY_STATEMENT
142 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
143 if (theory::TheoryTraits<THEORY>::isParametric \
144 && d_logicInfo.isTheoryEnabled(THEORY)) \
146 paraTheories.push_back(theoryOf(THEORY)); \
148 // Collect the parametric theories, which are given to the theory combination
150 CVC4_FOR_EACH_THEORY
;
152 // Initialize the theory combination architecture
153 if (options::tcMode() == options::TcMode::CARE_GRAPH
)
155 d_tc
.reset(new CombinationCareGraph(*this, paraTheories
, d_pnm
));
159 Unimplemented() << "TheoryEngine::finishInit: theory combination mode "
160 << options::tcMode() << " not supported";
162 // create the relevance filter if any option requires it
163 if (options::relevanceFilter())
166 new RelevanceManager(d_userContext
, theory::Valuation(this)));
169 // initialize the quantifiers engine
170 if (d_logicInfo
.isQuantified())
172 // initialize the quantifiers engine
173 d_quantEngine
= new QuantifiersEngine(this, *d_decManager
.get(), d_pnm
);
175 // initialize the theory combination manager, which decides and allocates the
176 // equality engines to use for all theories.
178 // get pointer to the shared solver
179 d_sharedSolver
= d_tc
->getSharedSolver();
181 // set the core equality engine on quantifiers engine
182 if (d_logicInfo
.isQuantified())
184 d_quantEngine
->setMasterEqualityEngine(d_tc
->getCoreEqualityEngine());
187 // finish initializing the theories by linking them with the appropriate
188 // utilities and then calling their finishInit method.
189 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
190 Theory
* t
= d_theoryTable
[theoryId
];
195 // setup the pointers to the utilities
196 const EeTheoryInfo
* eeti
= d_tc
->getEeTheoryInfo(theoryId
);
197 Assert(eeti
!= nullptr);
198 // the theory's official equality engine is the one specified by the
199 // equality engine manager
200 t
->setEqualityEngine(eeti
->d_usedEe
);
201 // set the quantifiers engine
202 t
->setQuantifiersEngine(d_quantEngine
);
203 // set the decision manager for the theory
204 t
->setDecisionManager(d_decManager
.get());
205 // finish initializing the theory
209 // finish initializing the quantifiers engine
210 if (d_logicInfo
.isQuantified())
212 d_quantEngine
->finishInit();
216 ProofNodeManager
* TheoryEngine::getProofNodeManager() const { return d_pnm
; }
218 TheoryEngine::TheoryEngine(context::Context
* context
,
219 context::UserContext
* userContext
,
221 RemoveTermFormulas
& iteRemover
,
222 const LogicInfo
& logicInfo
,
223 OutputManager
& outMgr
,
224 ProofNodeManager
* pnm
)
225 : d_propEngine(nullptr),
227 d_userContext(userContext
),
228 d_logicInfo(logicInfo
),
234 d_pnm
, nullptr, d_userContext
, "TheoryEngine::LazyCDProof")
236 d_tepg(new TheoryEngineProofGenerator(d_pnm
, d_userContext
)),
238 d_sharedSolver(nullptr),
239 d_quantEngine(nullptr),
240 d_decManager(new DecisionManager(userContext
)),
241 d_relManager(nullptr),
242 d_preRegistrationVisitor(this, context
),
243 d_eager_model_building(false),
244 d_inConflict(context
, false),
246 d_hasShutDown(false),
247 d_incomplete(context
, false),
248 d_propagationMap(context
),
249 d_propagationMapTimestamp(context
, 0),
250 d_propagatedLiterals(context
),
251 d_propagatedLiteralsIndex(context
, 0),
252 d_atomRequests(context
),
253 d_tpp(*this, iteRemover
, userContext
, d_pnm
),
254 d_combineTheoriesTime("TheoryEngine::combineTheoriesTime"),
257 d_interrupted(false),
258 d_resourceManager(rm
),
259 d_inPreregister(false),
260 d_factsAsserted(context
, false),
262 d_arithSubstitutionsAdded("theory::arith::zzz::arith::substitutions", 0)
264 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
;
267 d_theoryTable
[theoryId
] = NULL
;
268 d_theoryOut
[theoryId
] = NULL
;
271 smtStatisticsRegistry()->registerStat(&d_combineTheoriesTime
);
272 d_true
= NodeManager::currentNM()->mkConst
<bool>(true);
273 d_false
= NodeManager::currentNM()->mkConst
<bool>(false);
275 smtStatisticsRegistry()->registerStat(&d_arithSubstitutionsAdded
);
278 TheoryEngine::~TheoryEngine() {
279 Assert(d_hasShutDown
);
281 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
!= theory::THEORY_LAST
; ++ theoryId
) {
282 if(d_theoryTable
[theoryId
] != NULL
) {
283 delete d_theoryTable
[theoryId
];
284 delete d_theoryOut
[theoryId
];
288 delete d_quantEngine
;
290 smtStatisticsRegistry()->unregisterStat(&d_combineTheoriesTime
);
291 smtStatisticsRegistry()->unregisterStat(&d_arithSubstitutionsAdded
);
294 void TheoryEngine::interrupt() { d_interrupted
= true; }
295 void TheoryEngine::preRegister(TNode preprocessed
) {
296 Debug("theory") << "TheoryEngine::preRegister( " << preprocessed
<< ")"
298 d_preregisterQueue
.push(preprocessed
);
300 if (!d_inPreregister
) {
301 // We're in pre-register
302 d_inPreregister
= true;
304 // Process the pre-registration queue
305 while (!d_preregisterQueue
.empty()) {
306 // Get the next atom to pre-register
307 preprocessed
= d_preregisterQueue
.front();
308 d_preregisterQueue
.pop();
310 // the atom should not have free variables
311 Debug("theory") << "TheoryEngine::preRegister: " << preprocessed
313 Assert(!expr::hasFreeVar(preprocessed
));
314 // should not have witness
315 Assert(!expr::hasSubtermKind(kind::WITNESS
, preprocessed
));
317 // Pre-register the terms in the atom
318 theory::TheoryIdSet theories
= NodeVisitor
<PreRegisterVisitor
>::run(
319 d_preRegistrationVisitor
, preprocessed
);
320 theories
= TheoryIdSetUtil::setRemove(THEORY_BOOL
, theories
);
321 // Remove the top theory, if any more that means multiple theories were
323 bool multipleTheories
=
324 TheoryIdSetUtil::setRemove(Theory::theoryOf(preprocessed
), theories
);
325 if (Configuration::isAssertionBuild())
328 // This should never throw an exception, since theories should be
329 // guaranteed to be initialized.
330 // These checks don't work with finite model finding, because it
331 // uses Rational constants to represent cardinality constraints,
332 // even though arithmetic isn't actually involved.
333 if (!options::finiteModelFind())
335 while ((i
= TheoryIdSetUtil::setPop(theories
)) != THEORY_LAST
)
337 if (!d_logicInfo
.isTheoryEnabled(i
))
339 LogicInfo newLogicInfo
= d_logicInfo
.getUnlockedCopy();
340 newLogicInfo
.enableTheory(i
);
342 std::stringstream ss
;
343 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
344 << ", which doesn't include " << i
345 << ", but found a term in that theory." << std::endl
346 << "You might want to extend your logic to "
347 << newLogicInfo
.getLogicString() << std::endl
;
348 throw LogicException(ss
.str());
354 // pre-register with the shared solver, which also handles
355 // calling prepregister on individual theories.
356 Assert(d_sharedSolver
!= nullptr);
357 d_sharedSolver
->preRegisterShared(preprocessed
, multipleTheories
);
360 // Leaving pre-register
361 d_inPreregister
= false;
365 void TheoryEngine::printAssertions(const char* tag
) {
366 if (Trace
.isOn(tag
)) {
368 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
369 Theory
* theory
= d_theoryTable
[theoryId
];
370 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
371 Trace(tag
) << "--------------------------------------------" << endl
;
372 Trace(tag
) << "Assertions of " << theory
->getId() << ": " << endl
;
374 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
377 for (unsigned i
= 0; it
!= it_end
; ++it
, ++i
)
379 if ((*it
).d_isPreregistered
)
381 Trace(tag
) << "[" << i
<< "]: ";
385 Trace(tag
) << "(" << i
<< "): ";
387 Trace(tag
) << (*it
).d_assertion
<< endl
;
391 if (d_logicInfo
.isSharingEnabled()) {
392 Trace(tag
) << "Shared terms of " << theory
->getId() << ": " << endl
;
393 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
394 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
395 Trace(tag
) << "[" << i
<< "]: " << (*it
) << endl
;
403 void TheoryEngine::dumpAssertions(const char* tag
) {
404 if (Dump
.isOn(tag
)) {
405 const Printer
& printer
= d_outMgr
.getPrinter();
406 std::ostream
& out
= d_outMgr
.getDumpOut();
407 printer
.toStreamCmdComment(out
, "Starting completeness check");
408 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
409 Theory
* theory
= d_theoryTable
[theoryId
];
410 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
411 printer
.toStreamCmdComment(out
, "Completeness check");
412 printer
.toStreamCmdPush(out
);
414 // Dump the shared terms
415 if (d_logicInfo
.isSharingEnabled()) {
416 printer
.toStreamCmdComment(out
, "Shared terms");
417 context::CDList
<TNode
>::const_iterator it
= theory
->shared_terms_begin(), it_end
= theory
->shared_terms_end();
418 for (unsigned i
= 0; it
!= it_end
; ++ it
, ++i
) {
421 printer
.toStreamCmdComment(out
, ss
.str());
425 // Dump the assertions
426 printer
.toStreamCmdComment(out
, "Assertions");
427 context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(), it_end
= theory
->facts_end();
428 for (; it
!= it_end
; ++ it
) {
430 Node assertionNode
= (*it
).d_assertion
;
431 // Purify all the terms
433 if ((*it
).d_isPreregistered
)
435 printer
.toStreamCmdComment(out
, "Preregistered");
439 printer
.toStreamCmdComment(out
, "Shared assertion");
441 printer
.toStreamCmdAssert(out
, assertionNode
);
443 printer
.toStreamCmdCheckSat(out
);
445 printer
.toStreamCmdPop(out
);
452 * Check all (currently-active) theories for conflicts.
453 * @param effort the effort level to use
455 void TheoryEngine::check(Theory::Effort effort
) {
458 // Reset the interrupt flag
459 d_interrupted
= false;
461 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
462 #undef CVC4_FOR_EACH_THEORY_STATEMENT
464 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
465 if (theory::TheoryTraits<THEORY>::hasCheck \
466 && d_logicInfo.isTheoryEnabled(THEORY)) \
468 theoryOf(THEORY)->check(effort); \
471 Debug("conflict") << THEORY << " in conflict. " << std::endl; \
479 // Mark the output channel unused (if this is FULL_EFFORT, and nothing
480 // is done by the theories, no additional check will be needed)
481 d_outputChannelUsed
= false;
483 // Mark the lemmas flag (no lemmas added)
484 d_lemmasAdded
= false;
486 Debug("theory") << "TheoryEngine::check(" << effort
<< "): d_factsAsserted = " << (d_factsAsserted
? "true" : "false") << endl
;
488 // If in full effort, we have a fake new assertion just to jumpstart the checking
489 if (Theory::fullEffort(effort
)) {
490 d_factsAsserted
= true;
491 // Reset round for the relevance manager, which notice only sets a flag
492 // to indicate that its information must be recomputed.
493 if (d_relManager
!= nullptr)
495 d_relManager
->resetRound();
501 while (d_factsAsserted
&& !d_inConflict
&& !d_lemmasAdded
) {
503 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running check" << endl
;
505 Trace("theory::assertions") << endl
;
506 if (Trace
.isOn("theory::assertions")) {
507 printAssertions("theory::assertions");
510 if(Theory::fullEffort(effort
)) {
511 Trace("theory::assertions::fulleffort") << endl
;
512 if (Trace
.isOn("theory::assertions::fulleffort")) {
513 printAssertions("theory::assertions::fulleffort");
517 // Note that we've discharged all the facts
518 d_factsAsserted
= false;
521 CVC4_FOR_EACH_THEORY
;
523 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running propagation after the initial check" << endl
;
525 // We are still satisfiable, propagate as much as possible
528 // We do combination if all has been processed and we are in fullcheck
529 if (Theory::fullEffort(effort
) && d_logicInfo
.isSharingEnabled() && !d_factsAsserted
&& !d_lemmasAdded
&& !d_inConflict
) {
530 // Do the combination
531 Debug("theory") << "TheoryEngine::check(" << effort
<< "): running combination" << endl
;
533 TimerStat::CodeTimer
combineTheoriesTimer(d_combineTheoriesTime
);
534 d_tc
->combineTheories();
536 if(d_logicInfo
.isQuantified()){
537 d_quantEngine
->notifyCombineTheories();
542 // Must consult quantifiers theory for last call to ensure sat, or otherwise add a lemma
543 if( Theory::fullEffort(effort
) && ! d_inConflict
&& ! needCheck() ) {
544 Trace("theory::assertions-model") << endl
;
545 if (Trace
.isOn("theory::assertions-model")) {
546 printAssertions("theory::assertions-model");
548 // reset the model in the combination engine
550 //checks for theories requiring the model go at last call
551 for (TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
552 if( theoryId
!=THEORY_QUANTIFIERS
){
553 Theory
* theory
= d_theoryTable
[theoryId
];
554 if (theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
555 if( theory
->needsCheckLastEffort() ){
556 if (!d_tc
->buildModel())
560 theory
->check(Theory::EFFORT_LAST_CALL
);
567 if(d_logicInfo
.isQuantified()) {
568 // quantifiers engine must check at last call effort
569 d_quantEngine
->check(Theory::EFFORT_LAST_CALL
);
572 if (!d_inConflict
&& !needCheck())
574 // If d_eager_model_building is false, then we only mark that we
575 // are in "SAT mode". We build the model later only if the user asks
576 // for it via getBuiltModel.
578 if (d_eager_model_building
)
585 Debug("theory") << "TheoryEngine::check(" << effort
<< "): done, we are " << (d_inConflict
? "unsat" : "sat") << (d_lemmasAdded
? " with new lemmas" : " with no new lemmas");
586 Debug("theory") << ", need check = " << (needCheck() ? "YES" : "NO") << endl
;
588 if( Theory::fullEffort(effort
) && !d_inConflict
&& !needCheck()) {
589 // Do post-processing of model from the theories (e.g. used for THEORY_SEP
590 // to construct heap model)
591 d_tc
->postProcessModel(d_incomplete
.get());
593 } catch(const theory::Interrupted
&) {
594 Trace("theory") << "TheoryEngine::check() => interrupted" << endl
;
596 // If fulleffort, check all theories
597 if(Dump
.isOn("theory::fullcheck") && Theory::fullEffort(effort
)) {
598 if (!d_inConflict
&& !needCheck()) {
599 dumpAssertions("theory::fullcheck");
604 void TheoryEngine::propagate(Theory::Effort effort
)
606 // Reset the interrupt flag
607 d_interrupted
= false;
609 // Definition of the statement that is to be run by every theory
610 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
611 #undef CVC4_FOR_EACH_THEORY_STATEMENT
613 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
614 if (theory::TheoryTraits<THEORY>::hasPropagate && d_logicInfo.isTheoryEnabled(THEORY)) { \
615 theoryOf(THEORY)->propagate(effort); \
618 // Reset the interrupt flag
619 d_interrupted
= false;
621 // Propagate for each theory using the statement above
622 CVC4_FOR_EACH_THEORY
;
625 Node
TheoryEngine::getNextDecisionRequest()
627 return d_decManager
->getNextDecisionRequest();
630 bool TheoryEngine::properConflict(TNode conflict
) const {
632 if (conflict
.getKind() == kind::AND
) {
633 for (unsigned i
= 0; i
< conflict
.getNumChildren(); ++ i
) {
634 if (! getPropEngine()->hasValue(conflict
[i
], value
)) {
635 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
636 << conflict
[i
] << endl
;
640 Debug("properConflict") << "Bad conflict is due to false atom: "
641 << conflict
[i
] << endl
;
644 if (conflict
[i
] != Rewriter::rewrite(conflict
[i
])) {
645 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
646 << conflict
[i
] << " vs " << Rewriter::rewrite(conflict
[i
]) << endl
;
651 if (! getPropEngine()->hasValue(conflict
, value
)) {
652 Debug("properConflict") << "Bad conflict is due to unassigned atom: "
657 Debug("properConflict") << "Bad conflict is due to false atom: "
661 if (conflict
!= Rewriter::rewrite(conflict
)) {
662 Debug("properConflict") << "Bad conflict is due to atom not in normal form: "
663 << conflict
<< " vs " << Rewriter::rewrite(conflict
) << endl
;
670 TheoryModel
* TheoryEngine::getModel()
672 Assert(d_tc
!= nullptr);
673 TheoryModel
* m
= d_tc
->getModel();
674 Assert(m
!= nullptr);
678 TheoryModel
* TheoryEngine::getBuiltModel()
680 Assert(d_tc
!= nullptr);
681 // If this method was called, we should be in SAT mode, and produceModels
683 AlwaysAssert(options::produceModels());
686 // not available, perhaps due to interuption.
689 // must build model at this point
690 if (!d_tc
->buildModel())
694 return d_tc
->getModel();
697 bool TheoryEngine::buildModel()
699 Assert(d_tc
!= nullptr);
700 return d_tc
->buildModel();
703 bool TheoryEngine::getSynthSolutions(
704 std::map
<Node
, std::map
<Node
, Node
>>& sol_map
)
708 return d_quantEngine
->getSynthSolutions(sol_map
);
710 // we are not in a quantified logic, there is no synthesis solution
714 bool TheoryEngine::presolve() {
715 // Reset the interrupt flag
716 d_interrupted
= false;
718 // Reset the decision manager. This clears its decision strategies that are
719 // no longer valid in this user context.
720 d_decManager
->presolve();
723 // Definition of the statement that is to be run by every theory
724 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
725 #undef CVC4_FOR_EACH_THEORY_STATEMENT
727 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
728 if (theory::TheoryTraits<THEORY>::hasPresolve) { \
729 theoryOf(THEORY)->presolve(); \
735 // Presolve for each theory using the statement above
736 CVC4_FOR_EACH_THEORY
;
737 } catch(const theory::Interrupted
&) {
738 Trace("theory") << "TheoryEngine::presolve() => interrupted" << endl
;
740 // return whether we have a conflict
742 }/* TheoryEngine::presolve() */
744 void TheoryEngine::postsolve() {
745 // no longer in SAT mode
747 // Reset the interrupt flag
748 d_interrupted
= false;
749 bool CVC4_UNUSED wasInConflict
= d_inConflict
;
752 // Definition of the statement that is to be run by every theory
753 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
754 #undef CVC4_FOR_EACH_THEORY_STATEMENT
756 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
757 if (theory::TheoryTraits<THEORY>::hasPostsolve) \
759 theoryOf(THEORY)->postsolve(); \
760 Assert(!d_inConflict || wasInConflict) \
761 << "conflict raised during postsolve()"; \
764 // Postsolve for each theory using the statement above
765 CVC4_FOR_EACH_THEORY
;
766 } catch(const theory::Interrupted
&) {
767 Trace("theory") << "TheoryEngine::postsolve() => interrupted" << endl
;
769 }/* TheoryEngine::postsolve() */
772 void TheoryEngine::notifyRestart() {
773 // Reset the interrupt flag
774 d_interrupted
= false;
776 // Definition of the statement that is to be run by every theory
777 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
778 #undef CVC4_FOR_EACH_THEORY_STATEMENT
780 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
781 if (theory::TheoryTraits<THEORY>::hasNotifyRestart && d_logicInfo.isTheoryEnabled(THEORY)) { \
782 theoryOf(THEORY)->notifyRestart(); \
785 // notify each theory using the statement above
786 CVC4_FOR_EACH_THEORY
;
789 void TheoryEngine::ppStaticLearn(TNode in
, NodeBuilder
<>& learned
) {
790 // Reset the interrupt flag
791 d_interrupted
= false;
793 // Definition of the statement that is to be run by every theory
794 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
795 #undef CVC4_FOR_EACH_THEORY_STATEMENT
797 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
798 if (theory::TheoryTraits<THEORY>::hasPpStaticLearn) { \
799 theoryOf(THEORY)->ppStaticLearn(in, learned); \
802 // static learning for each theory using the statement above
803 CVC4_FOR_EACH_THEORY
;
806 bool TheoryEngine::isRelevant(Node lit
) const
808 if (d_relManager
!= nullptr)
810 return d_relManager
->isRelevant(lit
);
812 // otherwise must assume its relevant
816 void TheoryEngine::shutdown() {
817 // Set this first; if a Theory shutdown() throws an exception,
818 // at least the destruction of the TheoryEngine won't confound
820 d_hasShutDown
= true;
822 // Shutdown all the theories
823 for(TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
; ++theoryId
) {
824 if(d_theoryTable
[theoryId
]) {
825 theoryOf(theoryId
)->shutdown();
830 theory::Theory::PPAssertStatus
TheoryEngine::solve(
831 TrustNode tliteral
, TrustSubstitutionMap
& substitutionOut
)
833 // Reset the interrupt flag
834 d_interrupted
= false;
836 TNode literal
= tliteral
.getNode();
837 TNode atom
= literal
.getKind() == kind::NOT
? literal
[0] : literal
;
838 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< "): solving with " << theoryOf(atom
)->getId() << endl
;
840 if(! d_logicInfo
.isTheoryEnabled(Theory::theoryOf(atom
)) &&
841 Theory::theoryOf(atom
) != THEORY_SAT_SOLVER
) {
843 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
844 << ", which doesn't include " << Theory::theoryOf(atom
)
845 << ", but got a preprocessing-time fact for that theory." << endl
846 << "The fact:" << endl
848 throw LogicException(ss
.str());
851 Theory::PPAssertStatus solveStatus
=
852 theoryOf(atom
)->ppAssert(tliteral
, substitutionOut
);
853 Trace("theory::solve") << "TheoryEngine::solve(" << literal
<< ") => " << solveStatus
<< endl
;
857 TrustNode
TheoryEngine::preprocess(TNode assertion
)
859 return d_tpp
.theoryPreprocess(assertion
);
862 void TheoryEngine::notifyPreprocessedAssertions(
863 const std::vector
<Node
>& assertions
) {
864 // call all the theories
865 for (TheoryId theoryId
= theory::THEORY_FIRST
; theoryId
< theory::THEORY_LAST
;
867 if (d_theoryTable
[theoryId
]) {
868 theoryOf(theoryId
)->ppNotifyAssertions(assertions
);
871 if (d_relManager
!= nullptr)
873 d_relManager
->notifyPreprocessedAssertions(assertions
);
877 bool TheoryEngine::markPropagation(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
879 // What and where we are asserting
880 NodeTheoryPair
toAssert(assertion
, toTheoryId
, d_propagationMapTimestamp
);
881 // What and where it came from
882 NodeTheoryPair
toExplain(originalAssertion
, fromTheoryId
, d_propagationMapTimestamp
);
884 // See if the theory already got this literal
885 PropagationMap::const_iterator find
= d_propagationMap
.find(toAssert
);
886 if (find
!= d_propagationMap
.end()) {
887 // The theory already knows this
888 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): already there" << endl
;
892 Trace("theory::assertToTheory") << "TheoryEngine::markPropagation(): marking [" << d_propagationMapTimestamp
<< "] " << assertion
<< ", " << toTheoryId
<< " from " << originalAssertion
<< ", " << fromTheoryId
<< endl
;
894 // Mark the propagation
895 d_propagationMap
[toAssert
] = toExplain
;
896 d_propagationMapTimestamp
= d_propagationMapTimestamp
+ 1;
902 void TheoryEngine::assertToTheory(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
) {
904 Trace("theory::assertToTheory") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << originalAssertion
<< "," << toTheoryId
<< ", " << fromTheoryId
<< ")" << endl
;
906 Assert(toTheoryId
!= fromTheoryId
);
907 if(toTheoryId
!= THEORY_SAT_SOLVER
&&
908 ! d_logicInfo
.isTheoryEnabled(toTheoryId
)) {
910 ss
<< "The logic was specified as " << d_logicInfo
.getLogicString()
911 << ", which doesn't include " << toTheoryId
912 << ", but got an asserted fact to that theory." << endl
913 << "The fact:" << endl
915 throw LogicException(ss
.str());
922 // If sharing is disabled, things are easy
923 if (!d_logicInfo
.isSharingEnabled()) {
924 Assert(assertion
== originalAssertion
);
925 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
926 // Send to the apropriate theory
927 theory::Theory
* toTheory
= theoryOf(toTheoryId
);
928 // We assert it, and we know it's preregistereed
929 toTheory
->assertFact(assertion
, true);
930 // Mark that we have more information
931 d_factsAsserted
= true;
933 Assert(toTheoryId
== THEORY_SAT_SOLVER
);
934 // Check for propositional conflict
936 if (d_propEngine
->hasValue(assertion
, value
)) {
938 Trace("theory::propagate") << "TheoryEngine::assertToTheory(" << assertion
<< ", " << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (no sharing)" << endl
;
939 Trace("dtview::conflict")
940 << ":THEORY-CONFLICT: " << assertion
<< std::endl
;
946 d_propagatedLiterals
.push_back(assertion
);
951 // If sending to the shared terms database, it's also simple
952 if (toTheoryId
== THEORY_BUILTIN
) {
953 Assert(assertion
.getKind() == kind::EQUAL
954 || (assertion
.getKind() == kind::NOT
955 && assertion
[0].getKind() == kind::EQUAL
))
956 << "atom should be an EQUALity, not `" << assertion
<< "'";
957 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
958 // assert to the shared solver
959 bool polarity
= assertion
.getKind() != kind::NOT
;
960 TNode atom
= polarity
? assertion
: assertion
[0];
961 d_sharedSolver
->assertSharedEquality(atom
, polarity
, assertion
);
966 // Things from the SAT solver are already normalized, so they go
967 // directly to the apropriate theory
968 if (fromTheoryId
== THEORY_SAT_SOLVER
) {
969 // We know that this is normalized, so just send it off to the theory
970 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
971 // Is it preregistered
972 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
974 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
975 // Mark that we have more information
976 d_factsAsserted
= true;
981 // Propagations to the SAT solver are just enqueued for pickup by
982 // the SAT solver later
983 if (toTheoryId
== THEORY_SAT_SOLVER
) {
984 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
985 // Enqueue for propagation to the SAT solver
986 d_propagatedLiterals
.push_back(assertion
);
987 // Check for propositional conflicts
989 if (d_propEngine
->hasValue(assertion
, value
) && !value
) {
990 Trace("theory::propagate")
991 << "TheoryEngine::assertToTheory(" << assertion
<< ", "
992 << toTheoryId
<< ", " << fromTheoryId
<< "): conflict (sharing)"
994 Trace("dtview::conflict")
995 << ":THEORY-CONFLICT: " << assertion
<< std::endl
;
1002 Assert(assertion
.getKind() == kind::EQUAL
1003 || (assertion
.getKind() == kind::NOT
1004 && assertion
[0].getKind() == kind::EQUAL
));
1007 Node normalizedLiteral
= Rewriter::rewrite(assertion
);
1009 // See if it rewrites false directly -> conflict
1010 if (normalizedLiteral
.isConst()) {
1011 if (!normalizedLiteral
.getConst
<bool>()) {
1012 // Mark the propagation for explanations
1013 if (markPropagation(normalizedLiteral
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1014 // special case, trust node has no proof generator
1015 TrustNode trnn
= TrustNode::mkTrustConflict(normalizedLiteral
);
1016 // Get the explanation (conflict will figure out where it came from)
1017 conflict(trnn
, toTheoryId
);
1025 // Try and assert (note that we assert the non-normalized one)
1026 if (markPropagation(assertion
, originalAssertion
, toTheoryId
, fromTheoryId
)) {
1027 // Check if has been pre-registered with the theory
1028 bool preregistered
= d_propEngine
->isSatLiteral(assertion
) && Theory::theoryOf(assertion
) == toTheoryId
;
1030 theoryOf(toTheoryId
)->assertFact(assertion
, preregistered
);
1031 d_factsAsserted
= true;
1037 void TheoryEngine::assertFact(TNode literal
)
1039 Trace("theory") << "TheoryEngine::assertFact(" << literal
<< ")" << endl
;
1043 // If we're in conflict, nothing to do
1049 bool polarity
= literal
.getKind() != kind::NOT
;
1050 TNode atom
= polarity
? literal
: literal
[0];
1052 if (d_logicInfo
.isSharingEnabled()) {
1053 // If any shared terms, it's time to do sharing work
1054 d_sharedSolver
->preNotifySharedFact(atom
);
1056 // If it's an equality, assert it to the shared term manager, even though the terms are not
1057 // yet shared. As the terms become shared later, the shared terms manager will then add them
1058 // to the assert the equality to the interested theories
1059 if (atom
.getKind() == kind::EQUAL
) {
1060 // Assert it to the the owning theory
1061 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1062 // Shared terms manager will assert to interested theories directly, as
1063 // the terms become shared
1064 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ THEORY_SAT_SOLVER
);
1066 // Now, let's check for any atom triggers from lemmas
1067 AtomRequests::atom_iterator it
= d_atomRequests
.getAtomIterator(atom
);
1068 while (!it
.done()) {
1069 const AtomRequests::Request
& request
= it
.get();
1071 polarity
? (Node
)request
.d_atom
: request
.d_atom
.notNode();
1072 Debug("theory::atoms") << "TheoryEngine::assertFact(" << literal
<< "): sending requested " << toAssert
<< endl
;
1074 toAssert
, literal
, request
.d_toTheory
, THEORY_SAT_SOLVER
);
1079 // Not an equality, just assert to the appropriate theory
1080 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1083 // Assert the fact to the appropriate theory directly
1084 assertToTheory(literal
, literal
, /* to */ Theory::theoryOf(atom
), /* from */ THEORY_SAT_SOLVER
);
1088 bool TheoryEngine::propagate(TNode literal
, theory::TheoryId theory
) {
1089 Debug("theory::propagate") << "TheoryEngine::propagate(" << literal
<< ", " << theory
<< ")" << endl
;
1091 Trace("dtview::prop") << std::string(d_context
->getLevel(), ' ')
1092 << ":THEORY-PROP: " << literal
<< endl
;
1097 bool polarity
= literal
.getKind() != kind::NOT
;
1098 TNode atom
= polarity
? literal
: literal
[0];
1100 if (d_logicInfo
.isSharingEnabled() && atom
.getKind() == kind::EQUAL
) {
1101 if (d_propEngine
->isSatLiteral(literal
)) {
1102 // We propagate SAT literals to SAT
1103 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1105 if (theory
!= THEORY_BUILTIN
) {
1106 // Assert to the shared terms database
1107 assertToTheory(literal
, literal
, /* to */ THEORY_BUILTIN
, /* from */ theory
);
1110 // Just send off to the SAT solver
1111 Assert(d_propEngine
->isSatLiteral(literal
));
1112 assertToTheory(literal
, literal
, /* to */ THEORY_SAT_SOLVER
, /* from */ theory
);
1115 return !d_inConflict
;
1118 const LogicInfo
& TheoryEngine::getLogicInfo() const { return d_logicInfo
; }
1120 bool TheoryEngine::getSepHeapTypes(TypeNode
& locType
, TypeNode
& dataType
) const
1122 if (d_sepLocType
.isNull())
1126 locType
= d_sepLocType
;
1127 dataType
= d_sepDataType
;
1131 void TheoryEngine::declareSepHeap(TypeNode locT
, TypeNode dataT
)
1133 Theory
* tsep
= theoryOf(THEORY_SEP
);
1134 if (tsep
== nullptr)
1136 Assert(false) << "TheoryEngine::declareSepHeap called without the "
1137 "separation logic theory enabled";
1141 // Definition of the statement that is to be run by every theory
1142 #ifdef CVC4_FOR_EACH_THEORY_STATEMENT
1143 #undef CVC4_FOR_EACH_THEORY_STATEMENT
1145 #define CVC4_FOR_EACH_THEORY_STATEMENT(THEORY) \
1146 theoryOf(THEORY)->declareSepHeap(locT, dataT);
1148 // notify each theory using the statement above
1149 CVC4_FOR_EACH_THEORY
;
1151 // remember the types we have set
1152 d_sepLocType
= locT
;
1153 d_sepDataType
= dataT
;
1156 theory::EqualityStatus
TheoryEngine::getEqualityStatus(TNode a
, TNode b
) {
1157 Assert(a
.getType().isComparableTo(b
.getType()));
1158 return d_sharedSolver
->getEqualityStatus(a
, b
);
1161 Node
TheoryEngine::getModelValue(TNode var
) {
1164 // the model value of a constant must be itself
1167 Assert(d_sharedSolver
->isShared(var
));
1168 return theoryOf(Theory::theoryOf(var
.getType()))->getModelValue(var
);
1172 Node
TheoryEngine::ensureLiteral(TNode n
) {
1173 Trace("ensureLiteral") << "ensureLiteral rewriting: " << n
<< std::endl
;
1174 Node rewritten
= Rewriter::rewrite(n
);
1175 Trace("ensureLiteral") << " got: " << rewritten
<< std::endl
;
1176 std::vector
<TrustNode
> newLemmas
;
1177 std::vector
<Node
> newSkolems
;
1178 TrustNode tpn
= d_tpp
.preprocess(n
, newLemmas
, newSkolems
, true);
1179 // send lemmas corresponding to the skolems introduced by preprocessing n
1180 for (const TrustNode
& tnl
: newLemmas
)
1182 Trace("ensureLiteral") << " lemma: " << tnl
.getNode() << std::endl
;
1183 lemma(tnl
, LemmaProperty::NONE
);
1185 Node preprocessed
= tpn
.isNull() ? rewritten
: tpn
.getNode();
1186 Trace("ensureLiteral") << "ensureLiteral preprocessed: " << preprocessed
1188 d_propEngine
->ensureLiteral(preprocessed
);
1189 return preprocessed
;
1193 void TheoryEngine::printInstantiations( std::ostream
& out
) {
1194 if( d_quantEngine
){
1195 d_quantEngine
->printInstantiations( out
);
1197 out
<< "Internal error : instantiations not available when quantifiers are not present." << std::endl
;
1202 void TheoryEngine::printSynthSolution( std::ostream
& out
) {
1203 if( d_quantEngine
){
1204 d_quantEngine
->printSynthSolution( out
);
1206 out
<< "Internal error : synth solution not available when quantifiers are not present." << std::endl
;
1211 void TheoryEngine::getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
) {
1212 if( d_quantEngine
){
1213 d_quantEngine
->getInstantiatedQuantifiedFormulas( qs
);
1219 void TheoryEngine::getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
) {
1220 if( d_quantEngine
){
1221 d_quantEngine
->getInstantiationTermVectors( q
, tvecs
);
1227 void TheoryEngine::getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
) {
1228 if( d_quantEngine
){
1229 d_quantEngine
->getInstantiationTermVectors( insts
);
1235 TrustNode
TheoryEngine::getExplanation(TNode node
)
1237 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
1238 << "): current propagation index = "
1239 << d_propagationMapTimestamp
<< endl
;
1240 bool polarity
= node
.getKind() != kind::NOT
;
1241 TNode atom
= polarity
? node
: node
[0];
1243 // If we're not in shared mode, explanations are simple
1244 if (!d_logicInfo
.isSharingEnabled())
1246 Debug("theory::explain")
1247 << "TheoryEngine::getExplanation: sharing is NOT enabled. "
1248 << " Responsible theory is: " << theoryOf(atom
)->getId() << std::endl
;
1250 TrustNode texplanation
= theoryOf(atom
)->explain(node
);
1251 Node explanation
= texplanation
.getNode();
1252 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
1253 << ") => " << explanation
<< endl
;
1254 if (isProofEnabled())
1256 texplanation
.debugCheckClosed(
1257 "te-proof-exp", "texplanation no share", false);
1258 // check if no generator, if so, add THEORY_LEMMA
1259 if (texplanation
.getGenerator() == nullptr)
1261 Node proven
= texplanation
.getProven();
1262 TheoryId tid
= theoryOf(atom
)->getId();
1263 Node tidn
= builtin::BuiltinProofRuleChecker::mkTheoryIdNode(tid
);
1264 d_lazyProof
->addStep(proven
, PfRule::THEORY_LEMMA
, {}, {proven
, tidn
});
1266 TrustNode::mkTrustPropExp(node
, explanation
, d_lazyProof
.get());
1269 return texplanation
;
1272 Debug("theory::explain") << "TheoryEngine::getExplanation: sharing IS enabled"
1275 // Initial thing to explain
1276 NodeTheoryPair
toExplain(node
, THEORY_SAT_SOLVER
, d_propagationMapTimestamp
);
1277 Assert(d_propagationMap
.find(toExplain
) != d_propagationMap
.end());
1279 NodeTheoryPair nodeExplainerPair
= d_propagationMap
[toExplain
];
1280 Debug("theory::explain")
1281 << "TheoryEngine::getExplanation: explainer for node "
1282 << nodeExplainerPair
.d_node
1283 << " is theory: " << nodeExplainerPair
.d_theory
<< std::endl
;
1285 // Create the workplace for explanations
1286 std::vector
<NodeTheoryPair
> vec
{d_propagationMap
[toExplain
]};
1287 // Process the explanation
1288 TrustNode texplanation
= getExplanation(vec
);
1289 Debug("theory::explain") << "TheoryEngine::getExplanation(" << node
<< ") => "
1290 << texplanation
.getNode() << endl
;
1291 return texplanation
;
1294 struct AtomsCollect
{
1296 std::vector
<TNode
> d_atoms
;
1297 std::unordered_set
<TNode
, TNodeHashFunction
> d_visited
;
1301 typedef void return_type
;
1303 bool alreadyVisited(TNode current
, TNode parent
) {
1304 // Check if already visited
1305 if (d_visited
.find(current
) != d_visited
.end()) return true;
1306 // Don't visit non-boolean
1307 if (!current
.getType().isBoolean()) return true;
1312 void visit(TNode current
, TNode parent
) {
1313 if (Theory::theoryOf(current
) != theory::THEORY_BOOL
) {
1314 d_atoms
.push_back(current
);
1316 d_visited
.insert(current
);
1319 void start(TNode node
) {}
1320 void done(TNode node
) {}
1322 std::vector
<TNode
> getAtoms() const {
1327 void TheoryEngine::ensureLemmaAtoms(const std::vector
<TNode
>& atoms
, theory::TheoryId atomsTo
) {
1328 for (unsigned i
= 0; i
< atoms
.size(); ++ i
) {
1330 // Non-equality atoms are either owned by theory or they don't make sense
1331 if (atoms
[i
].getKind() != kind::EQUAL
) {
1337 // Simple normalization to not repeat stuff
1338 if (eq
[0] > eq
[1]) {
1339 eq
= eq
[1].eqNode(eq
[0]);
1342 // Rewrite the equality
1343 Node eqNormalized
= Rewriter::rewrite(atoms
[i
]);
1345 Debug("theory::atoms") << "TheoryEngine::ensureLemmaAtoms(): " << eq
<< " with nf " << eqNormalized
<< endl
;
1347 // If the equality is a boolean constant, we send immediately
1348 if (eqNormalized
.isConst()) {
1349 if (eqNormalized
.getConst
<bool>()) {
1350 assertToTheory(eq
, eqNormalized
, /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1352 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), /** to */ atomsTo
, /** Sat solver */ theory::THEORY_SAT_SOLVER
);
1355 }else if( eqNormalized
.getKind() != kind::EQUAL
){
1356 Assert(eqNormalized
.getKind() == kind::BOOLEAN_TERM_VARIABLE
1357 || (eqNormalized
.getKind() == kind::NOT
1358 && eqNormalized
[0].getKind() == kind::BOOLEAN_TERM_VARIABLE
));
1359 // this happens for Boolean term equalities V = true that are rewritten to V, we should skip
1360 // TODO : revisit this
1364 // If the normalization did the just flips, keep the flip
1365 if (eqNormalized
[0] == eq
[1] && eqNormalized
[1] == eq
[0]) {
1369 // Check if the equality is already known by the sat solver
1370 if (d_propEngine
->isSatLiteral(eqNormalized
)) {
1372 if (d_propEngine
->hasValue(eqNormalized
, value
)) {
1374 assertToTheory(eq
, eqNormalized
, atomsTo
, theory::THEORY_SAT_SOLVER
);
1377 assertToTheory(eq
.notNode(), eqNormalized
.notNode(), atomsTo
, theory::THEORY_SAT_SOLVER
);
1383 // If the theory is asking about a different form, or the form is ok but if will go to a different theory
1384 // then we must figure it out
1385 if (eqNormalized
!= eq
|| Theory::theoryOf(eq
) != atomsTo
) {
1386 // If you get eqNormalized, send atoms[i] to atomsTo
1387 d_atomRequests
.add(eqNormalized
, eq
, atomsTo
);
1392 theory::LemmaStatus
TheoryEngine::lemma(theory::TrustNode tlemma
,
1393 theory::LemmaProperty p
,
1394 theory::TheoryId atomsTo
,
1395 theory::TheoryId from
)
1397 // For resource-limiting (also does a time check).
1399 Assert(tlemma
.getKind() == TrustNodeKind::LEMMA
1400 || tlemma
.getKind() == TrustNodeKind::CONFLICT
);
1402 Node node
= tlemma
.getNode();
1403 Node lemma
= tlemma
.getProven();
1404 Trace("te-lemma") << "Lemma, input: " << lemma
<< ", property = " << p
1407 Assert(!expr::hasFreeVar(lemma
));
1409 // when proofs are enabled, we ensure the trust node has a generator by
1410 // adding a trust step to the lazy proof maintained by this class
1411 if (isProofEnabled())
1413 // ensure proof: set THEORY_LEMMA if no generator is provided
1414 if (tlemma
.getGenerator() == nullptr)
1416 // internal lemmas should have generators
1417 Assert(from
!= THEORY_LAST
);
1418 // add theory lemma step to proof
1419 Node tidn
= builtin::BuiltinProofRuleChecker::mkTheoryIdNode(from
);
1420 d_lazyProof
->addStep(lemma
, PfRule::THEORY_LEMMA
, {}, {lemma
, tidn
});
1421 // update the trust node
1422 tlemma
= TrustNode::mkTrustLemma(lemma
, d_lazyProof
.get());
1425 tlemma
.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma_initial");
1428 // Do we need to check atoms
1429 if (atomsTo
!= theory::THEORY_LAST
) {
1430 Debug("theory::atoms") << "TheoryEngine::lemma(" << node
<< ", " << atomsTo
<< ")" << endl
;
1431 AtomsCollect collectAtoms
;
1432 NodeVisitor
<AtomsCollect
>::run(collectAtoms
, node
);
1433 ensureLemmaAtoms(collectAtoms
.getAtoms(), atomsTo
);
1436 if(Dump
.isOn("t-lemmas")) {
1437 // we dump the negation of the lemma, to show validity of the lemma
1438 Node n
= lemma
.negate();
1439 const Printer
& printer
= d_outMgr
.getPrinter();
1440 std::ostream
& out
= d_outMgr
.getDumpOut();
1441 printer
.toStreamCmdComment(out
, "theory lemma: expect valid");
1442 printer
.toStreamCmdCheckSat(out
, n
);
1444 bool removable
= isLemmaPropertyRemovable(p
);
1445 bool preprocess
= isLemmaPropertyPreprocess(p
);
1448 tlemma
.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma_initial");
1450 // call preprocessor
1451 std::vector
<TrustNode
> newLemmas
;
1452 std::vector
<Node
> newSkolems
;
1454 d_tpp
.preprocessLemma(tlemma
, newLemmas
, newSkolems
, preprocess
);
1456 Assert(newSkolems
.size() == newLemmas
.size());
1458 // If specified, we must add this lemma to the set of those that need to be
1459 // justified, where note we pass all auxiliary lemmas in lemmas, since these
1460 // by extension must be justified as well.
1461 if (d_relManager
!= nullptr && isLemmaPropertyNeedsJustify(p
))
1463 d_relManager
->notifyPreprocessedAssertion(tplemma
.getProven());
1464 for (const theory::TrustNode
& tnl
: newLemmas
)
1466 d_relManager
->notifyPreprocessedAssertion(tnl
.getProven());
1470 // do final checks on the lemmas we are about to send
1471 if (isProofEnabled())
1473 Assert(tplemma
.getGenerator() != nullptr);
1474 // ensure closed, make the proof node eagerly here to debug
1475 tplemma
.debugCheckClosed("te-proof-debug", "TheoryEngine::lemma");
1476 for (size_t i
= 0, lsize
= newLemmas
.size(); i
< lsize
; ++i
)
1478 Assert(newLemmas
[i
].getGenerator() != nullptr);
1479 newLemmas
[i
].debugCheckClosed("te-proof-debug",
1480 "TheoryEngine::lemma_new");
1484 if (Trace
.isOn("te-lemma"))
1486 Trace("te-lemma") << "Lemma, output: " << tplemma
.getProven() << std::endl
;
1487 for (size_t i
= 0, lsize
= newLemmas
.size(); i
< lsize
; ++i
)
1489 Trace("te-lemma") << "Lemma, new lemma: " << newLemmas
[i
].getProven()
1490 << " (skolem is " << newSkolems
[i
] << ")" << std::endl
;
1494 // now, send the lemmas to the prop engine
1495 d_propEngine
->assertLemmas(tplemma
, newLemmas
, newSkolems
, removable
);
1497 // Mark that we added some lemmas
1498 d_lemmasAdded
= true;
1500 // Lemma analysis isn't online yet; this lemma may only live for this
1502 Node retLemma
= tplemma
.getNode();
1503 if (!newLemmas
.empty())
1505 std::vector
<Node
> lemmas
{retLemma
};
1506 for (const theory::TrustNode
& tnl
: newLemmas
)
1508 lemmas
.push_back(tnl
.getProven());
1510 // the returned lemma is the conjunction of all additional lemmas.
1511 retLemma
= NodeManager::currentNM()->mkNode(kind::AND
, lemmas
);
1513 return theory::LemmaStatus(retLemma
, d_userContext
->getLevel());
1516 void TheoryEngine::conflict(theory::TrustNode tconflict
, TheoryId theoryId
)
1518 Assert(tconflict
.getKind() == TrustNodeKind::CONFLICT
);
1519 TNode conflict
= tconflict
.getNode();
1520 Trace("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", "
1521 << theoryId
<< ")" << endl
;
1522 Trace("te-proof-debug") << "Check closed conflict" << std::endl
;
1523 // doesn't require proof generator, yet, since THEORY_LEMMA is added below
1524 tconflict
.debugCheckClosed(
1525 "te-proof-debug", "TheoryEngine::conflict_initial", false);
1527 Trace("dtview::conflict") << ":THEORY-CONFLICT: " << conflict
<< std::endl
;
1529 // Mark that we are in conflict
1530 d_inConflict
= true;
1532 if(Dump
.isOn("t-conflicts")) {
1533 const Printer
& printer
= d_outMgr
.getPrinter();
1534 std::ostream
& out
= d_outMgr
.getDumpOut();
1535 printer
.toStreamCmdComment(out
, "theory conflict: expect unsat");
1536 printer
.toStreamCmdCheckSat(out
, conflict
);
1539 // In the multiple-theories case, we need to reconstruct the conflict
1540 if (d_logicInfo
.isSharingEnabled()) {
1541 // Create the workplace for explanations
1542 std::vector
<NodeTheoryPair
> vec
;
1544 NodeTheoryPair(conflict
, theoryId
, d_propagationMapTimestamp
));
1546 // Process the explanation
1547 TrustNode tncExp
= getExplanation(vec
);
1548 Trace("te-proof-debug")
1549 << "Check closed conflict explained with sharing" << std::endl
;
1550 tncExp
.debugCheckClosed("te-proof-debug",
1551 "TheoryEngine::conflict_explained_sharing");
1552 Node fullConflict
= tncExp
.getNode();
1554 if (isProofEnabled())
1556 Trace("te-proof-debug") << "Process conflict: " << conflict
<< std::endl
;
1557 Trace("te-proof-debug") << "Conflict " << tconflict
<< " from "
1558 << tconflict
.identifyGenerator() << std::endl
;
1559 Trace("te-proof-debug") << "Explanation " << tncExp
<< " from "
1560 << tncExp
.identifyGenerator() << std::endl
;
1561 Assert(d_lazyProof
!= nullptr);
1562 if (tconflict
.getGenerator() != nullptr)
1564 d_lazyProof
->addLazyStep(tconflict
.getProven(),
1565 tconflict
.getGenerator());
1569 // add theory lemma step
1570 Node tidn
= builtin::BuiltinProofRuleChecker::mkTheoryIdNode(theoryId
);
1571 Node conf
= tconflict
.getProven();
1572 d_lazyProof
->addStep(conf
, PfRule::THEORY_LEMMA
, {}, {conf
, tidn
});
1574 // store the explicit step, which should come from a different
1575 // generator, e.g. d_tepg.
1576 Node proven
= tncExp
.getProven();
1577 Assert(tncExp
.getGenerator() != d_lazyProof
.get());
1578 Trace("te-proof-debug") << "add lazy step " << tncExp
.identifyGenerator()
1579 << " for " << proven
<< std::endl
;
1580 d_lazyProof
->addLazyStep(proven
, tncExp
.getGenerator());
1581 pfgEnsureClosed(proven
,
1584 "TheoryEngine::conflict_during");
1585 Node fullConflictNeg
= fullConflict
.notNode();
1586 std::vector
<Node
> children
;
1587 children
.push_back(proven
);
1588 std::vector
<Node
> args
;
1589 args
.push_back(fullConflictNeg
);
1590 if (conflict
== d_false
)
1592 AlwaysAssert(proven
== fullConflictNeg
);
1596 if (fullConflict
!= conflict
)
1598 // ------------------------- explained ---------- from theory
1599 // fullConflict => conflict ~conflict
1600 // ------------------------------------------ MACRO_SR_PRED_TRANSFORM
1602 children
.push_back(conflict
.notNode());
1603 args
.push_back(mkMethodId(MethodId::SB_LITERAL
));
1604 d_lazyProof
->addStep(
1605 fullConflictNeg
, PfRule::MACRO_SR_PRED_TRANSFORM
, children
, args
);
1609 // pass the processed trust node
1611 TrustNode::mkTrustConflict(fullConflict
, d_lazyProof
.get());
1612 Debug("theory::conflict") << "TheoryEngine::conflict(" << conflict
<< ", " << theoryId
<< "): full = " << fullConflict
<< endl
;
1613 Assert(properConflict(fullConflict
));
1614 Trace("te-proof-debug")
1615 << "Check closed conflict with sharing" << std::endl
;
1616 tconf
.debugCheckClosed("te-proof-debug", "TheoryEngine::conflict:sharing");
1617 lemma(tconf
, LemmaProperty::REMOVABLE
);
1619 // When only one theory, the conflict should need no processing
1620 Assert(properConflict(conflict
));
1621 // pass the trust node that was sent from the theory
1622 lemma(tconflict
, LemmaProperty::REMOVABLE
, THEORY_LAST
, theoryId
);
1626 theory::TrustNode
TheoryEngine::getExplanation(
1627 std::vector
<NodeTheoryPair
>& explanationVector
)
1629 Assert(explanationVector
.size() == 1);
1630 Node conclusion
= explanationVector
[0].d_node
;
1631 std::shared_ptr
<LazyCDProof
> lcp
;
1632 if (isProofEnabled())
1634 Trace("te-proof-exp") << "=== TheoryEngine::getExplanation " << conclusion
1636 lcp
.reset(new LazyCDProof(
1637 d_pnm
, nullptr, nullptr, "TheoryEngine::LazyCDProof::getExplanation"));
1639 unsigned i
= 0; // Index of the current literal we are processing
1641 std::unique_ptr
<std::set
<Node
>> inputAssertions
= nullptr;
1642 // the overall explanation
1643 std::set
<TNode
> exp
;
1644 // vector of trust nodes to explain at the end
1645 std::vector
<std::pair
<TheoryId
, TrustNode
>> texplains
;
1646 // cache of nodes we have already explained by some theory
1647 std::unordered_map
<Node
, size_t, NodeHashFunction
> cache
;
1649 while (i
< explanationVector
.size()) {
1650 // Get the current literal to explain
1651 NodeTheoryPair toExplain
= explanationVector
[i
];
1653 Debug("theory::explain")
1654 << "[i=" << i
<< "] TheoryEngine::explain(): processing ["
1655 << toExplain
.d_timestamp
<< "] " << toExplain
.d_node
<< " sent from "
1656 << toExplain
.d_theory
<< endl
;
1658 if (cache
.find(toExplain
.d_node
) != cache
.end()
1659 && cache
[toExplain
.d_node
] < toExplain
.d_timestamp
)
1664 cache
[toExplain
.d_node
] = toExplain
.d_timestamp
;
1666 // If a true constant or a negation of a false constant we can ignore it
1667 if ((toExplain
.d_node
.isConst() && toExplain
.d_node
.getConst
<bool>())
1668 || (toExplain
.d_node
.getKind() == kind::NOT
1669 && toExplain
.d_node
[0].isConst()
1670 && !toExplain
.d_node
[0].getConst
<bool>()))
1673 // if we are building a proof
1676 Trace("te-proof-exp")
1677 << "- explain " << toExplain
.d_node
<< " trivially..." << std::endl
;
1678 // ------------------MACRO_SR_PRED_INTRO
1680 std::vector
<Node
> children
;
1681 std::vector
<Node
> args
;
1682 args
.push_back(toExplain
.d_node
);
1684 toExplain
.d_node
, PfRule::MACRO_SR_PRED_INTRO
, children
, args
);
1689 // If from the SAT solver, keep it
1690 if (toExplain
.d_theory
== THEORY_SAT_SOLVER
)
1692 Debug("theory::explain") << "\tLiteral came from THEORY_SAT_SOLVER. Kepping it." << endl
;
1693 exp
.insert(explanationVector
[i
++].d_node
);
1694 // it will be a free assumption in the proof
1695 Trace("te-proof-exp") << "- keep " << toExplain
.d_node
<< std::endl
;
1699 // If an and, expand it
1700 if (toExplain
.d_node
.getKind() == kind::AND
)
1702 Debug("theory::explain")
1703 << "TheoryEngine::explain(): expanding " << toExplain
.d_node
1704 << " got from " << toExplain
.d_theory
<< endl
;
1705 size_t nchild
= toExplain
.d_node
.getNumChildren();
1706 for (size_t k
= 0; k
< nchild
; ++k
)
1708 NodeTheoryPair
newExplain(
1709 toExplain
.d_node
[k
], toExplain
.d_theory
, toExplain
.d_timestamp
);
1710 explanationVector
.push_back(newExplain
);
1714 Trace("te-proof-exp")
1715 << "- AND expand " << toExplain
.d_node
<< std::endl
;
1716 // delay explanation, use a dummy trust node
1717 TrustNode tnAndExp
= TrustNode::mkTrustPropExp(
1718 toExplain
.d_node
, toExplain
.d_node
, nullptr);
1719 texplains
.push_back(
1720 std::pair
<TheoryId
, TrustNode
>(THEORY_LAST
, tnAndExp
));
1726 // See if it was sent to the theory by another theory
1727 PropagationMap::const_iterator find
= d_propagationMap
.find(toExplain
);
1728 if (find
!= d_propagationMap
.end()) {
1729 Debug("theory::explain")
1730 << "\tTerm was propagated by another theory (theory = "
1731 << getTheoryString((*find
).second
.d_theory
) << ")" << std::endl
;
1732 // There is some propagation, check if its a timely one
1733 if ((*find
).second
.d_timestamp
< toExplain
.d_timestamp
)
1735 Debug("theory::explain")
1736 << "\tRelevant timetsamp, pushing " << (*find
).second
.d_node
1737 << "to index = " << explanationVector
.size() << std::endl
;
1738 explanationVector
.push_back((*find
).second
);
1743 if (!CDProof::isSame(toExplain
.d_node
, (*find
).second
.d_node
))
1745 Trace("te-proof-exp")
1746 << "- t-explained cached: " << toExplain
.d_node
<< " by "
1747 << (*find
).second
.d_node
<< std::endl
;
1748 // delay explanation, use a dummy trust node that says that
1749 // (*find).second.d_node explains toExplain.d_node.
1750 TrustNode tnRewExp
= TrustNode::mkTrustPropExp(
1751 toExplain
.d_node
, (*find
).second
.d_node
, nullptr);
1752 texplains
.push_back(
1753 std::pair
<TheoryId
, TrustNode
>(THEORY_LAST
, tnRewExp
));
1759 // It was produced by the theory, so ask for an explanation
1760 TrustNode texplanation
=
1761 d_sharedSolver
->explain(toExplain
.d_node
, toExplain
.d_theory
);
1764 texplanation
.debugCheckClosed("te-proof-exp", "texplanation", false);
1765 Trace("te-proof-exp")
1766 << "- t-explained[" << toExplain
.d_theory
<< "]: " << toExplain
.d_node
1767 << " by " << texplanation
.getNode() << std::endl
;
1768 // if not a trivial explanation
1769 if (!CDProof::isSame(texplanation
.getNode(), toExplain
.d_node
))
1771 // We add it to the list of theory explanations, to be processed at
1772 // the end of this method. We wait to explain here because it may
1773 // be that a later explanation may preempt the need for proving this
1774 // step. For instance, if the conclusion lit is later added as an
1775 // assumption in the final explanation. This avoids cyclic proofs.
1776 texplains
.push_back(
1777 std::pair
<TheoryId
, TrustNode
>(toExplain
.d_theory
, texplanation
));
1780 Node explanation
= texplanation
.getNode();
1782 Debug("theory::explain")
1783 << "TheoryEngine::explain(): got explanation " << explanation
1784 << " got from " << toExplain
.d_theory
<< endl
;
1785 Assert(explanation
!= toExplain
.d_node
)
1786 << "wasn't sent to you, so why are you explaining it trivially";
1787 // Mark the explanation
1788 NodeTheoryPair
newExplain(
1789 explanation
, toExplain
.d_theory
, toExplain
.d_timestamp
);
1790 explanationVector
.push_back(newExplain
);
1795 // make the explanation node
1797 if (exp
.size() == 0)
1799 // Normalize to true
1800 expNode
= NodeManager::currentNM()->mkConst
<bool>(true);
1802 else if (exp
.size() == 1)
1804 // All the same, or just one
1805 expNode
= *exp
.begin();
1809 NodeBuilder
<> conjunction(kind::AND
);
1810 std::set
<TNode
>::const_iterator it
= exp
.begin();
1811 std::set
<TNode
>::const_iterator it_end
= exp
.end();
1812 while (it
!= it_end
)
1817 expNode
= conjunction
;
1819 // if we are building a proof, go back through the explanations and
1823 if (Trace
.isOn("te-proof-exp"))
1825 Trace("te-proof-exp") << "Explanation is:" << std::endl
;
1828 Trace("te-proof-exp") << " " << e
<< std::endl
;
1830 Trace("te-proof-exp") << "=== Replay explanations..." << std::endl
;
1832 // Now, go back and add the necessary steps of theory explanations, i.e.
1833 // add those that prove things that aren't in the final explanation. We
1834 // iterate in reverse order so that most recent steps take priority. This
1835 // avoids cyclic proofs in the lazy proof we are building (lcp).
1836 for (std::vector
<std::pair
<TheoryId
, TrustNode
>>::reverse_iterator
1837 it
= texplains
.rbegin(),
1838 itEnd
= texplains
.rend();
1842 TrustNode trn
= it
->second
;
1843 Assert(trn
.getKind() == TrustNodeKind::PROP_EXP
);
1844 Node proven
= trn
.getProven();
1845 Assert(proven
.getKind() == kind::IMPLIES
);
1846 Node tConc
= proven
[1];
1847 Trace("te-proof-exp") << "- Process " << trn
<< std::endl
;
1848 if (exp
.find(tConc
) != exp
.end())
1850 // already added to proof
1851 Trace("te-proof-exp") << "...already added" << std::endl
;
1854 Node symTConc
= CDProof::getSymmFact(tConc
);
1855 if (!symTConc
.isNull())
1857 if (exp
.find(symTConc
) != exp
.end())
1859 // symmetric direction
1860 Trace("te-proof-exp") << "...already added (SYMM)" << std::endl
;
1864 // remember that we've explained this formula, to avoid cycles in lcp
1866 TheoryId ttid
= it
->first
;
1867 Node tExp
= proven
[0];
1868 if (ttid
== THEORY_LAST
)
1872 // dummy trust node, do AND expansion
1873 Assert(tConc
.getKind() == kind::AND
);
1874 // tConc[0] ... tConc[n]
1875 // ---------------------- AND_INTRO
1877 std::vector
<Node
> pfChildren
;
1878 pfChildren
.insert(pfChildren
.end(), tConc
.begin(), tConc
.end());
1879 lcp
->addStep(tConc
, PfRule::AND_INTRO
, pfChildren
, {});
1880 Trace("te-proof-exp") << "...via AND_INTRO" << std::endl
;
1883 // otherwise should hold by rewriting
1884 Assert(Rewriter::rewrite(tConc
) == Rewriter::rewrite(tExp
));
1886 // ---- MACRO_SR_PRED_TRANSFORM
1888 lcp
->addStep(tConc
, PfRule::MACRO_SR_PRED_TRANSFORM
, {tExp
}, {tConc
});
1889 Trace("te-proof-exp") << "...via MACRO_SR_PRED_TRANSFORM" << std::endl
;
1895 Trace("te-proof-exp") << "...trivial" << std::endl
;
1898 // ------------- Via theory
1899 // tExp tExp => tConc
1900 // ---------------------------------MODUS_PONENS
1902 if (trn
.getGenerator() != nullptr)
1904 Trace("te-proof-exp") << "...via theory generator" << std::endl
;
1905 lcp
->addLazyStep(proven
, trn
.getGenerator());
1909 Trace("te-proof-exp") << "...via trust THEORY_LEMMA" << std::endl
;
1910 // otherwise, trusted theory lemma
1911 Node tidn
= builtin::BuiltinProofRuleChecker::mkTheoryIdNode(it
->first
);
1912 lcp
->addStep(proven
, PfRule::THEORY_LEMMA
, {}, {proven
, tidn
});
1914 std::vector
<Node
> pfChildren
;
1915 pfChildren
.push_back(trn
.getNode());
1916 pfChildren
.push_back(proven
);
1917 lcp
->addStep(tConc
, PfRule::MODUS_PONENS
, pfChildren
, {});
1919 // store in the proof generator
1920 TrustNode trn
= d_tepg
->mkTrustExplain(conclusion
, expNode
, lcp
);
1921 // return the trust node
1925 return theory::TrustNode::mkTrustLemma(expNode
, nullptr);
1928 bool TheoryEngine::isProofEnabled() const { return d_pnm
!= nullptr; }
1930 void TheoryEngine::setUserAttribute(const std::string
& attr
,
1932 const std::vector
<Node
>& node_values
,
1933 const std::string
& str_value
)
1935 Trace("te-attr") << "set user attribute " << attr
<< " " << n
<< endl
;
1936 if( d_attr_handle
.find( attr
)!=d_attr_handle
.end() ){
1937 for( size_t i
=0; i
<d_attr_handle
[attr
].size(); i
++ ){
1938 d_attr_handle
[attr
][i
]->setUserAttribute(attr
, n
, node_values
, str_value
);
1941 //unhandled exception?
1945 void TheoryEngine::handleUserAttribute(const char* attr
, Theory
* t
) {
1946 Trace("te-attr") << "Handle user attribute " << attr
<< " " << t
<< endl
;
1947 std::string
str( attr
);
1948 d_attr_handle
[ str
].push_back( t
);
1951 void TheoryEngine::checkTheoryAssertionsWithModel(bool hardFailure
) {
1952 for(TheoryId theoryId
= THEORY_FIRST
; theoryId
< THEORY_LAST
; ++theoryId
) {
1953 Theory
* theory
= d_theoryTable
[theoryId
];
1954 if(theory
&& d_logicInfo
.isTheoryEnabled(theoryId
)) {
1955 for(context::CDList
<Assertion
>::const_iterator it
= theory
->facts_begin(),
1956 it_end
= theory
->facts_end();
1959 Node assertion
= (*it
).d_assertion
;
1960 if (!isRelevant(assertion
))
1962 // not relevant, skip
1965 Node val
= d_tc
->getModel()->getValue(assertion
);
1968 std::stringstream ss
;
1969 ss
<< " " << theoryId
1970 << " has an asserted fact that the model doesn't satisfy." << endl
1971 << "The fact: " << assertion
<< endl
1972 << "Model value: " << val
<< endl
;
1977 // Always an error if it is false
1978 InternalError() << ss
.str();
1982 // Otherwise just a warning. Notice this case may happen for
1983 // assertions with unevaluable operators, e.g. transcendental
1984 // functions. It also may happen for separation logic, where
1985 // check-model support is limited.
1986 Warning() << ss
.str();
1995 std::pair
<bool, Node
> TheoryEngine::entailmentCheck(options::TheoryOfMode mode
,
1998 TNode atom
= (lit
.getKind() == kind::NOT
) ? lit
[0] : lit
;
1999 if( atom
.getKind()==kind::AND
|| atom
.getKind()==kind::OR
|| atom
.getKind()==kind::IMPLIES
){
2000 //Boolean connective, recurse
2001 std::vector
< Node
> children
;
2002 bool pol
= (lit
.getKind()!=kind::NOT
);
2003 bool is_conjunction
= pol
==(lit
.getKind()==kind::AND
);
2004 for( unsigned i
=0; i
<atom
.getNumChildren(); i
++ ){
2006 if( pol
==( lit
.getKind()==kind::IMPLIES
&& i
==0 ) ){
2007 ch
= atom
[i
].negate();
2009 std::pair
<bool, Node
> chres
= entailmentCheck(mode
, ch
);
2011 if( !is_conjunction
){
2014 children
.push_back( chres
.second
);
2016 }else if( !chres
.first
&& is_conjunction
){
2017 return std::pair
<bool, Node
>(false, Node::null());
2020 if( is_conjunction
){
2021 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, children
));
2023 return std::pair
<bool, Node
>(false, Node::null());
2025 }else if( atom
.getKind()==kind::ITE
|| ( atom
.getKind()==kind::EQUAL
&& atom
[0].getType().isBoolean() ) ){
2026 bool pol
= (lit
.getKind()!=kind::NOT
);
2027 for( unsigned r
=0; r
<2; r
++ ){
2032 std::pair
<bool, Node
> chres
= entailmentCheck(mode
, ch
);
2034 Node ch2
= atom
[ atom
.getKind()==kind::ITE
? r
+1 : 1 ];
2035 if( pol
==( atom
.getKind()==kind::ITE
? true : r
==1 ) ){
2038 std::pair
<bool, Node
> chres2
= entailmentCheck(mode
, ch2
);
2040 return std::pair
<bool, Node
>(true, NodeManager::currentNM()->mkNode(kind::AND
, chres
.second
, chres2
.second
));
2046 return std::pair
<bool, Node
>(false, Node::null());
2048 //it is a theory atom
2049 theory::TheoryId tid
= theory::Theory::theoryOf(mode
, atom
);
2050 theory::Theory
* th
= theoryOf(tid
);
2053 Trace("theory-engine-entc") << "Entailment check : " << lit
<< std::endl
;
2055 std::pair
<bool, Node
> chres
= th
->entailmentCheck(lit
);
2060 void TheoryEngine::spendResource(ResourceManager::Resource r
)
2062 d_resourceManager
->spendResource(r
);
2065 }/* CVC4 namespace */