1 /********************* */
2 /*! \file theory_engine.h
4 ** Top contributors (to current version):
5 ** Dejan Jovanovic, Andrew Reynolds, Morgan Deters
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief The theory engine
17 #include "cvc4_private.h"
19 #ifndef CVC4__THEORY_ENGINE_H
20 #define CVC4__THEORY_ENGINE_H
25 #include <unordered_map>
29 #include "base/check.h"
30 #include "context/cdhashset.h"
31 #include "expr/node.h"
32 #include "options/options.h"
33 #include "options/smt_options.h"
34 #include "options/theory_options.h"
35 #include "prop/prop_engine.h"
36 #include "smt/command.h"
37 #include "theory/atom_requests.h"
38 #include "theory/engine_output_channel.h"
39 #include "theory/interrupted.h"
40 #include "theory/rewriter.h"
41 #include "theory/shared_terms_database.h"
42 #include "theory/sort_inference.h"
43 #include "theory/substitutions.h"
44 #include "theory/term_registration_visitor.h"
45 #include "theory/theory.h"
46 #include "theory/theory_preprocessor.h"
47 #include "theory/uf/equality_engine.h"
48 #include "theory/valuation.h"
49 #include "util/hash.h"
50 #include "util/resource_manager.h"
51 #include "util/statistics_registry.h"
52 #include "util/unsafe_interrupt_exception.h"
56 class ResourceManager
;
57 class LemmaProofRecipe
;
60 * A pair of a theory and a node. This is used to mark the flow of
61 * propagations between theories.
63 struct NodeTheoryPair
{
65 theory::TheoryId d_theory
;
67 NodeTheoryPair(TNode n
, theory::TheoryId t
, size_t ts
= 0)
68 : d_node(n
), d_theory(t
), d_timestamp(ts
)
71 NodeTheoryPair() : d_theory(theory::THEORY_LAST
), d_timestamp() {}
72 // Comparison doesn't take into account the timestamp
73 bool operator == (const NodeTheoryPair
& pair
) const {
74 return d_node
== pair
.d_node
&& d_theory
== pair
.d_theory
;
76 };/* struct NodeTheoryPair */
78 struct NodeTheoryPairHashFunction
{
79 NodeHashFunction hashFunction
;
80 // Hash doesn't take into account the timestamp
81 size_t operator()(const NodeTheoryPair
& pair
) const {
82 uint64_t hash
= fnv1a::fnv1a_64(NodeHashFunction()(pair
.d_node
));
83 return static_cast<size_t>(fnv1a::fnv1a_64(pair
.d_theory
, hash
));
85 };/* struct NodeTheoryPairHashFunction */
88 /* Forward declarations */
90 class CombinationEngine
;
92 class TheoryEngineModelBuilder
;
93 class EqEngineManagerDistributed
;
95 class DecisionManager
;
96 class RelevanceManager
;
102 namespace quantifiers
{
106 class EntailmentCheckParameters
;
107 class EntailmentCheckSideEffects
;
108 }/* CVC4::theory namespace */
110 class RemoveTermFormulas
;
113 * This is essentially an abstraction for a collection of theories. A
114 * TheoryEngine provides services to a PropEngine, making various
115 * T-solvers look like a single unit to the propositional part of
120 /** Shared terms database can use the internals notify the theories */
121 friend class SharedTermsDatabase
;
122 friend class theory::CombinationEngine
;
123 friend class theory::quantifiers::TermDb
;
124 friend class theory::EngineOutputChannel
;
126 /** Associated PropEngine engine */
127 prop::PropEngine
* d_propEngine
;
130 context::Context
* d_context
;
132 /** Our user context */
133 context::UserContext
* d_userContext
;
136 * A table of from theory IDs to theory pointers. Never use this table
137 * directly, use theoryOf() instead.
139 theory::Theory
* d_theoryTable
[theory::THEORY_LAST
];
142 * A collection of theories that are "active" for the current run.
143 * This set is provided by the user (as a logic string, say, in SMT-LIBv2
144 * format input), or else by default it's all-inclusive. This is important
145 * because we can optimize for single-theory runs (no sharing), can reduce
146 * the cost of walking the DAG on registration, etc.
148 const LogicInfo
& d_logicInfo
;
151 * The database of shared terms.
153 SharedTermsDatabase d_sharedTerms
;
156 * The distributed equality manager. This class is responsible for
157 * configuring the theories of this class for handling equalties
158 * in a "distributed" fashion, i.e. each theory maintains a unique
159 * instance of an equality engine. These equality engines are memory
160 * managed by this class.
162 std::unique_ptr
<theory::EqEngineManagerDistributed
> d_eeDistributed
;
165 * The quantifiers engine
167 theory::QuantifiersEngine
* d_quantEngine
;
169 * The decision manager
171 std::unique_ptr
<theory::DecisionManager
> d_decManager
;
172 /** The relevance manager */
173 std::unique_ptr
<theory::RelevanceManager
> d_relManager
;
176 * Default model object
178 theory::TheoryModel
* d_curr_model
;
179 bool d_aloc_curr_model
;
181 * Model builder object
183 theory::TheoryEngineModelBuilder
* d_curr_model_builder
;
184 bool d_aloc_curr_model_builder
;
185 /** are we in eager model building mode? (see setEagerModelBuilding). */
186 bool d_eager_model_building
;
188 typedef std::unordered_map
<Node
, Node
, NodeHashFunction
> NodeMap
;
189 typedef std::unordered_map
<TNode
, Node
, TNodeHashFunction
> TNodeMap
;
192 * Used for "missed-t-propagations" dumping mode only. A set of all
193 * theory-propagable literals.
195 context::CDList
<TNode
> d_possiblePropagations
;
198 * Used for "missed-t-propagations" dumping mode only. A
199 * context-dependent set of those theory-propagable literals that
200 * have been propagated.
202 context::CDHashSet
<Node
, NodeHashFunction
> d_hasPropagated
;
205 * Output channels for individual theories.
207 theory::EngineOutputChannel
* d_theoryOut
[theory::THEORY_LAST
];
210 * Are we in conflict.
212 context::CDO
<bool> d_inConflict
;
215 * Are we in "SAT mode"? In this state, the user can query for the model.
216 * This corresponds to the state in Figure 4.1, page 52 of the SMT-LIB
217 * standard, version 2.6.
222 * Called by the theories to notify of a conflict.
224 void conflict(TNode conflict
, theory::TheoryId theoryId
);
227 * Debugging flag to ensure that shutdown() is called before the
233 * True if a theory has notified us of incompleteness (at this
234 * context level or below).
236 context::CDO
<bool> d_incomplete
;
239 * Called by the theories to notify that the current branch is incomplete.
241 void setIncomplete(theory::TheoryId theory
) {
247 * Mapping of propagations from recievers to senders.
249 typedef context::CDHashMap
<NodeTheoryPair
, NodeTheoryPair
, NodeTheoryPairHashFunction
> PropagationMap
;
250 PropagationMap d_propagationMap
;
253 * Timestamp of propagations
255 context::CDO
<size_t> d_propagationMapTimestamp
;
258 * Literals that are propagated by the theory. Note that these are TNodes.
259 * The theory can only propagate nodes that have an assigned literal in the
260 * SAT solver and are hence referenced in the SAT solver.
262 context::CDList
<TNode
> d_propagatedLiterals
;
265 * The index of the next literal to be propagated by a theory.
267 context::CDO
<unsigned> d_propagatedLiteralsIndex
;
270 * Called by the output channel to propagate literals and facts
271 * @return false if immediate conflict
273 bool propagate(TNode literal
, theory::TheoryId theory
);
276 * Internal method to call the propagation routines and collect the
277 * propagated literals.
279 void propagate(theory::Theory::Effort effort
);
282 * A variable to mark if we added any lemmas.
287 * A variable to mark if the OutputChannel was "used" by any theory
288 * since the start of the last check. If it has been, we require
289 * a FULL_EFFORT check before exiting and reporting SAT.
291 * See the documentation for the needCheck() function, below.
293 bool d_outputChannelUsed
;
295 /** Atom requests from lemmas */
296 AtomRequests d_atomRequests
;
299 * Adds a new lemma, returning its status.
300 * @param node the lemma
301 * @param negated should the lemma be asserted negated
302 * @param p the properties of the lemma.
304 theory::LemmaStatus
lemma(TNode node
,
307 theory::LemmaProperty p
,
308 theory::TheoryId atomsTo
);
310 /** Enusre that the given atoms are send to the given theory */
311 void ensureLemmaAtoms(const std::vector
<TNode
>& atoms
, theory::TheoryId theory
);
313 /** sort inference module */
314 SortInference d_sortInfer
;
316 /** The theory preprocessor */
317 theory::TheoryPreprocessor d_tpp
;
319 /** Time spent in theory combination */
320 TimerStat d_combineTheoriesTime
;
325 /** Whether we were just interrupted (or not) */
327 ResourceManager
* d_resourceManager
;
330 /** Constructs a theory engine */
331 TheoryEngine(context::Context
* context
,
332 context::UserContext
* userContext
,
334 RemoveTermFormulas
& iteRemover
,
335 const LogicInfo
& logic
);
337 /** Destroys a theory engine */
342 /** "Spend" a resource during a search or preprocessing.*/
343 void spendResource(ResourceManager::Resource r
);
346 * Adds a theory. Only one theory per TheoryId can be present, so if
347 * there is another theory it will be deleted.
349 template <class TheoryClass
>
350 inline void addTheory(theory::TheoryId theoryId
)
352 Assert(d_theoryTable
[theoryId
] == NULL
&& d_theoryOut
[theoryId
] == NULL
);
353 d_theoryOut
[theoryId
] = new theory::EngineOutputChannel(this, theoryId
);
354 d_theoryTable
[theoryId
] = new TheoryClass(d_context
,
356 *d_theoryOut
[theoryId
],
357 theory::Valuation(this),
360 theory::Rewriter::registerTheoryRewriter(
361 theoryId
, d_theoryTable
[theoryId
]->getTheoryRewriter());
364 void setPropEngine(prop::PropEngine
* propEngine
)
366 d_propEngine
= propEngine
;
370 * Called when all initialization of options/logic is done, after theory
371 * objects have been created.
373 * This initializes the quantifiers engine, the "official" equality engines
374 * of each theory as required, and the model and model builder utilities.
379 * Get a pointer to the underlying propositional engine.
381 inline prop::PropEngine
* getPropEngine() const {
386 * Get a pointer to the underlying sat context.
388 inline context::Context
* getSatContext() const {
393 * Get a pointer to the underlying user context.
395 inline context::Context
* getUserContext() const {
396 return d_userContext
;
400 * Get a pointer to the underlying quantifiers engine.
402 theory::QuantifiersEngine
* getQuantifiersEngine() const {
403 return d_quantEngine
;
406 * Get a pointer to the underlying decision manager.
408 theory::DecisionManager
* getDecisionManager() const
410 return d_decManager
.get();
415 * Queue of nodes for pre-registration.
417 std::queue
<TNode
> d_preregisterQueue
;
420 * Boolean flag denoting we are in pre-registration.
422 bool d_inPreregister
;
425 * Did the theories get any new facts since the last time we called
428 context::CDO
<bool> d_factsAsserted
;
431 * Map from equality atoms to theories that would like to be notified about them.
436 * Assert the formula to the given theory.
437 * @param assertion the assertion to send (not necesserily normalized)
438 * @param original the assertion as it was sent in from the propagating theory
439 * @param toTheoryId the theory to assert to
440 * @param fromTheoryId the theory that sent it
442 void assertToTheory(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
);
445 * Marks a theory propagation from a theory to a theory where a
446 * theory could be the THEORY_SAT_SOLVER for literals coming from
447 * or being propagated to the SAT solver. If the receiving theory
448 * already recieved the literal, the method returns false, otherwise
451 * @param assertion the normalized assertion being sent
452 * @param originalAssertion the actual assertion that was sent
453 * @param toTheoryId the theory that is on the receiving end
454 * @param fromTheoryId the theory that sent the assertion
455 * @return true if a new assertion, false if theory already got it
457 bool markPropagation(TNode assertion
, TNode originalAssertions
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
);
460 * Computes the explanation by travarsing the propagation graph and
461 * asking relevant theories to explain the propagations. Initially
462 * the explanation vector should contain only the element (node, theory)
463 * where the node is the one to be explained, and the theory is the
464 * theory that sent the literal. The lemmaProofRecipe will contain a list
465 * of the explanation steps required to produce the original node.
467 void getExplanation(std::vector
<NodeTheoryPair
>& explanationVector
, LemmaProofRecipe
* lemmaProofRecipe
);
472 * Signal the start of a new round of assertion preprocessing
474 void preprocessStart();
477 * Runs theory specific preprocessing on the non-Boolean parts of
478 * the formula. This is only called on input assertions, after ITEs
481 Node
preprocess(TNode node
);
483 /** Notify (preprocessed) assertions. */
484 void notifyPreprocessedAssertions(const std::vector
<Node
>& assertions
);
486 /** Return whether or not we are incomplete (in the current context). */
487 inline bool isIncomplete() const { return d_incomplete
; }
490 * Returns true if we need another round of checking. If this
491 * returns true, check(FULL_EFFORT) _must_ be called by the
492 * propositional layer before reporting SAT.
494 * This is especially necessary for incomplete theories that lazily
495 * output some lemmas on FULL_EFFORT check (e.g. quantifier reasoning
496 * outputing quantifier instantiations). In such a case, a lemma can
497 * be asserted that is simplified away (perhaps it's already true).
498 * However, we must maintain the invariant that, if a theory uses the
499 * OutputChannel, it implicitly requests that another check(FULL_EFFORT)
500 * be performed before exit, even if no new facts are on its fact queue,
501 * as it might decide to further instantiate some lemmas, precluding
504 inline bool needCheck() const {
505 return d_outputChannelUsed
|| d_lemmasAdded
;
508 * Is the literal lit (possibly) critical for satisfying the input formula in
509 * the current context? This call is applicable only during collectModelInfo
510 * or during LAST_CALL effort.
512 bool isRelevant(Node lit
) const;
515 * This is called at shutdown time by the SmtEngine, just before
516 * destruction. It is important because there are destruction
517 * ordering issues between PropEngine and Theory.
522 * Solve the given literal with a theory that owns it.
524 theory::Theory::PPAssertStatus
solve(TNode literal
,
525 theory::SubstitutionMap
& substitutionOut
);
528 * Preregister a Theory atom with the responsible theory (or
531 void preRegister(TNode preprocessed
);
534 * Assert the formula to the appropriate theory.
535 * @param node the assertion
537 void assertFact(TNode node
);
540 * Check all (currently-active) theories for conflicts.
541 * @param effort the effort level to use
543 void check(theory::Theory::Effort effort
);
546 * Run the combination framework.
548 void combineTheories();
551 * Calls ppStaticLearn() on all theories, accumulating their
552 * combined contributions in the "learned" builder.
554 void ppStaticLearn(TNode in
, NodeBuilder
<>& learned
);
557 * Calls presolve() on all theories and returns true
558 * if one of the theories discovers a conflict.
563 * Calls postsolve() on all theories.
568 * Calls notifyRestart() on all active theories.
570 void notifyRestart();
572 void getPropagatedLiterals(std::vector
<TNode
>& literals
) {
573 for (; d_propagatedLiteralsIndex
< d_propagatedLiterals
.size(); d_propagatedLiteralsIndex
= d_propagatedLiteralsIndex
+ 1) {
574 Debug("getPropagatedLiterals") << "TheoryEngine::getPropagatedLiterals: propagating: " << d_propagatedLiterals
[d_propagatedLiteralsIndex
] << std::endl
;
575 literals
.push_back(d_propagatedLiterals
[d_propagatedLiteralsIndex
]);
580 * Returns the next decision request, or null if none exist. The next
581 * decision request is a literal that this theory engine prefers the SAT
582 * solver to make as its next decision. Decision requests are managed by
583 * the decision manager d_decManager.
585 Node
getNextDecisionRequest();
587 bool properConflict(TNode conflict
) const;
588 bool properPropagation(TNode lit
) const;
589 bool properExplanation(TNode node
, TNode expl
) const;
592 * Returns an explanation of the node propagated to the SAT solver.
594 Node
getExplanation(TNode node
);
597 * Returns an explanation of the node propagated to the SAT solver and the theory
598 * that propagated it.
600 Node
getExplanationAndRecipe(TNode node
, LemmaProofRecipe
* proofRecipe
);
605 bool collectModelInfo(theory::TheoryModel
* m
);
606 /** post process model */
607 void postProcessModel( theory::TheoryModel
* m
);
610 * Get the pointer to the model object used by this theory engine.
612 theory::TheoryModel
* getModel();
614 * Get the current model for the current set of assertions. This method
615 * should only be called immediately after a satisfiable or unknown
616 * response to a check-sat call, and only if produceModels is true.
618 * If the model is not already built, this will cause this theory engine
619 * to build the model.
621 * If the model is not available (for instance, if the last call to check-sat
622 * was interrupted), then this returns the null pointer.
624 theory::TheoryModel
* getBuiltModel();
625 /** set eager model building
627 * If this method is called, then this TheoryEngine will henceforth build
628 * its model immediately after every satisfiability check that results
629 * in a satisfiable or unknown result. The motivation for this mode is to
630 * accomodate API users that get the model object from the TheoryEngine,
631 * where we want to ensure that this model is always valid.
632 * TODO (#2648): revisit this.
634 void setEagerModelBuilding() { d_eager_model_building
= true; }
636 /** get synth solutions
638 * This method returns true if there is a synthesis solution available. This
639 * is the case if the last call to check satisfiability originated in a
640 * check-synth call, and the synthesis solver successfully found a solution
641 * for all active synthesis conjectures.
643 * This method adds entries to sol_map that map functions-to-synthesize with
644 * their solutions, for all active conjectures. This should be called
645 * immediately after the solver answers unsat for sygus input.
647 * For details on what is added to sol_map, see
648 * SynthConjecture::getSynthSolutions.
650 bool getSynthSolutions(std::map
<Node
, std::map
<Node
, Node
> >& sol_map
);
653 * Get the model builder
655 theory::TheoryEngineModelBuilder
* getModelBuilder() { return d_curr_model_builder
; }
658 * Get the theory associated to a given Node.
660 * @returns the theory, or NULL if the TNode is
663 inline theory::Theory
* theoryOf(TNode node
) const {
664 return d_theoryTable
[theory::Theory::theoryOf(node
)];
668 * Get the theory associated to a the given theory id.
670 * @returns the theory
672 inline theory::Theory
* theoryOf(theory::TheoryId theoryId
) const {
673 Assert(theoryId
< theory::THEORY_LAST
);
674 return d_theoryTable
[theoryId
];
677 inline bool isTheoryEnabled(theory::TheoryId theoryId
) const {
678 return d_logicInfo
.isTheoryEnabled(theoryId
);
680 /** get the logic info used by this theory engine */
681 const LogicInfo
& getLogicInfo() const;
683 * Returns the equality status of the two terms, from the theory
684 * that owns the domain type. The types of a and b must be the same.
686 theory::EqualityStatus
getEqualityStatus(TNode a
, TNode b
);
689 * Returns the value that a theory that owns the type of var currently
690 * has (or null if none);
692 Node
getModelValue(TNode var
);
695 * Takes a literal and returns an equivalent literal that is guaranteed to be a SAT literal
697 Node
ensureLiteral(TNode n
);
700 * Print all instantiations made by the quantifiers module.
702 void printInstantiations( std::ostream
& out
);
705 * Print solution for synthesis conjectures found by ce_guided_instantiation module
707 void printSynthSolution( std::ostream
& out
);
710 * Get list of quantified formulas that were instantiated
712 void getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
);
715 * Get instantiation methods
716 * first inputs forall x.q[x] and returns ( q[a], ..., q[z] )
717 * second inputs forall x.q[x] and returns ( a, ..., z )
718 * third and fourth return mappings e.g. forall x.q1[x] -> ( q1[a]...q1[z] ) , ... , forall x.qn[x] -> ( qn[a]...qn[z] )
720 void getInstantiations( Node q
, std::vector
< Node
>& insts
);
721 void getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
);
722 void getInstantiations( std::map
< Node
, std::vector
< Node
> >& insts
);
723 void getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
);
726 * Get instantiated conjunction, returns q[t1] ^ ... ^ q[tn] where t1...tn are current set of instantiations for q.
727 * Can be used for quantifier elimination when satisfiable and q[t1] ^ ... ^ q[tn] |= q
729 Node
getInstantiatedConjunction( Node q
);
732 * Forwards an entailment check according to the given theoryOfMode.
733 * See theory.h for documentation on entailmentCheck().
735 std::pair
<bool, Node
> entailmentCheck(options::TheoryOfMode mode
, TNode lit
);
738 /** Default visitor for pre-registration */
739 PreRegisterVisitor d_preRegistrationVisitor
;
741 /** Visitor for collecting shared terms */
742 SharedTermsVisitor d_sharedTermsVisitor
;
744 /** Dump the assertions to the dump */
745 void dumpAssertions(const char* tag
);
747 /** For preprocessing pass lifting bit-vectors of size 1 to booleans */
750 SharedTermsDatabase
* getSharedTermsDatabase();
752 SortInference
* getSortInference() { return &d_sortInfer
; }
754 /** Prints the assertions to the debug stream */
755 void printAssertions(const char* tag
);
759 std::map
< std::string
, std::vector
< theory::Theory
* > > d_attr_handle
;
762 /** Set user attribute.
764 * This function is called when an attribute is set by a user. In SMT-LIBv2
765 * this is done via the syntax (! n :attr)
767 void setUserAttribute(const std::string
& attr
,
769 const std::vector
<Node
>& node_values
,
770 const std::string
& str_value
);
772 /** Handle user attribute.
774 * Associates theory t with the attribute attr. Theory t will be
775 * notified whenever an attribute of name attr is set.
777 void handleUserAttribute(const char* attr
, theory::Theory
* t
);
780 * Check that the theory assertions are satisfied in the model.
781 * This function is called from the smt engine's checkModel routine.
783 void checkTheoryAssertionsWithModel(bool hardFailure
);
786 IntStat d_arithSubstitutionsAdded
;
788 };/* class TheoryEngine */
790 }/* CVC4 namespace */
792 #endif /* CVC4__THEORY_ENGINE_H */