1 /********************* */
2 /*! \file theory_engine.h
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 "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 "theory/atom_requests.h"
37 #include "theory/engine_output_channel.h"
38 #include "theory/interrupted.h"
39 #include "theory/rewriter.h"
40 #include "theory/sort_inference.h"
41 #include "theory/term_registration_visitor.h"
42 #include "theory/theory.h"
43 #include "theory/theory_preprocessor.h"
44 #include "theory/trust_node.h"
45 #include "theory/trust_substitutions.h"
46 #include "theory/uf/equality_engine.h"
47 #include "theory/valuation.h"
48 #include "util/hash.h"
49 #include "util/resource_manager.h"
50 #include "util/statistics_registry.h"
51 #include "util/unsafe_interrupt_exception.h"
55 class ResourceManager
;
56 class TheoryEngineProofGenerator
;
59 * A pair of a theory and a node. This is used to mark the flow of
60 * propagations between theories.
62 struct NodeTheoryPair
{
64 theory::TheoryId d_theory
;
66 NodeTheoryPair(TNode n
, theory::TheoryId t
, size_t ts
= 0)
67 : d_node(n
), d_theory(t
), d_timestamp(ts
)
70 NodeTheoryPair() : d_theory(theory::THEORY_LAST
), d_timestamp() {}
71 // Comparison doesn't take into account the timestamp
72 bool operator == (const NodeTheoryPair
& pair
) const {
73 return d_node
== pair
.d_node
&& d_theory
== pair
.d_theory
;
75 };/* struct NodeTheoryPair */
77 struct NodeTheoryPairHashFunction
{
78 NodeHashFunction hashFunction
;
79 // Hash doesn't take into account the timestamp
80 size_t operator()(const NodeTheoryPair
& pair
) const {
81 uint64_t hash
= fnv1a::fnv1a_64(NodeHashFunction()(pair
.d_node
));
82 return static_cast<size_t>(fnv1a::fnv1a_64(pair
.d_theory
, hash
));
84 };/* struct NodeTheoryPairHashFunction */
87 /* Forward declarations */
90 class CombinationEngine
;
92 class DecisionManager
;
93 class RelevanceManager
;
98 }/* CVC4::theory namespace */
101 * This is essentially an abstraction for a collection of theories. A
102 * TheoryEngine provides services to a PropEngine, making various
103 * T-solvers look like a single unit to the propositional part of
108 /** Shared terms database can use the internals notify the theories */
109 friend class SharedTermsDatabase
;
110 friend class theory::EngineOutputChannel
;
111 friend class theory::CombinationEngine
;
112 friend class theory::SharedSolver
;
114 /** Associated PropEngine engine */
115 prop::PropEngine
* d_propEngine
;
118 context::Context
* d_context
;
120 /** Our user context */
121 context::UserContext
* d_userContext
;
124 * A table of from theory IDs to theory pointers. Never use this table
125 * directly, use theoryOf() instead.
127 theory::Theory
* d_theoryTable
[theory::THEORY_LAST
];
130 * A collection of theories that are "active" for the current run.
131 * This set is provided by the user (as a logic string, say, in SMT-LIBv2
132 * format input), or else by default it's all-inclusive. This is important
133 * because we can optimize for single-theory runs (no sharing), can reduce
134 * the cost of walking the DAG on registration, etc.
136 const LogicInfo
& d_logicInfo
;
137 /** The separation logic location and data types */
138 TypeNode d_sepLocType
;
139 TypeNode d_sepDataType
;
141 /** Reference to the output manager of the smt engine */
142 OutputManager
& d_outMgr
;
144 //--------------------------------- new proofs
145 /** Proof node manager used by this theory engine, if proofs are enabled */
146 ProofNodeManager
* d_pnm
;
147 /** The lazy proof object
149 * This stores instructions for how to construct proofs for all theory lemmas.
151 std::shared_ptr
<LazyCDProof
> d_lazyProof
;
152 /** The proof generator */
153 std::shared_ptr
<TheoryEngineProofGenerator
> d_tepg
;
154 //--------------------------------- end new proofs
155 /** The combination manager we are using */
156 std::unique_ptr
<theory::CombinationEngine
> d_tc
;
157 /** The shared solver of the above combination engine. */
158 theory::SharedSolver
* d_sharedSolver
;
160 * The quantifiers engine
162 theory::QuantifiersEngine
* d_quantEngine
;
164 * The decision manager
166 std::unique_ptr
<theory::DecisionManager
> d_decManager
;
167 /** The relevance manager */
168 std::unique_ptr
<theory::RelevanceManager
> d_relManager
;
170 /** Default visitor for pre-registration */
171 PreRegisterVisitor d_preRegistrationVisitor
;
173 /** are we in eager model building mode? (see setEagerModelBuilding). */
174 bool d_eager_model_building
;
177 * Output channels for individual theories.
179 theory::EngineOutputChannel
* d_theoryOut
[theory::THEORY_LAST
];
182 * Are we in conflict.
184 context::CDO
<bool> d_inConflict
;
187 * Are we in "SAT mode"? In this state, the user can query for the model.
188 * This corresponds to the state in Figure 4.1, page 52 of the SMT-LIB
189 * standard, version 2.6.
194 * Called by the theories to notify of a conflict.
196 * @param conflict The trust node containing the conflict and its proof
197 * generator (if it exists),
198 * @param theoryId The theory that sent the conflict
200 void conflict(theory::TrustNode conflict
, theory::TheoryId theoryId
);
203 * Debugging flag to ensure that shutdown() is called before the
209 * True if a theory has notified us of incompleteness (at this
210 * context level or below).
212 context::CDO
<bool> d_incomplete
;
215 * Called by the theories to notify that the current branch is incomplete.
217 void setIncomplete(theory::TheoryId theory
) {
222 * Mapping of propagations from recievers to senders.
224 typedef context::CDHashMap
<NodeTheoryPair
, NodeTheoryPair
, NodeTheoryPairHashFunction
> PropagationMap
;
225 PropagationMap d_propagationMap
;
228 * Timestamp of propagations
230 context::CDO
<size_t> d_propagationMapTimestamp
;
233 * Literals that are propagated by the theory. Note that these are TNodes.
234 * The theory can only propagate nodes that have an assigned literal in the
235 * SAT solver and are hence referenced in the SAT solver.
237 context::CDList
<TNode
> d_propagatedLiterals
;
240 * The index of the next literal to be propagated by a theory.
242 context::CDO
<unsigned> d_propagatedLiteralsIndex
;
245 * Called by the output channel to propagate literals and facts
246 * @return false if immediate conflict
248 bool propagate(TNode literal
, theory::TheoryId theory
);
251 * Internal method to call the propagation routines and collect the
252 * propagated literals.
254 void propagate(theory::Theory::Effort effort
);
257 * A variable to mark if we added any lemmas.
262 * A variable to mark if the OutputChannel was "used" by any theory
263 * since the start of the last check. If it has been, we require
264 * a FULL_EFFORT check before exiting and reporting SAT.
266 * See the documentation for the needCheck() function, below.
268 bool d_outputChannelUsed
;
270 /** Atom requests from lemmas */
271 AtomRequests d_atomRequests
;
274 * Adds a new lemma, returning its status.
275 * @param node the lemma
276 * @param p the properties of the lemma.
277 * @param atomsTo the theory that atoms of the lemma should be sent to
278 * @param from the theory that sent the lemma
279 * @return a lemma status, containing the lemma and context information
280 * about when it was sent.
282 theory::LemmaStatus
lemma(theory::TrustNode node
,
283 theory::LemmaProperty p
,
284 theory::TheoryId atomsTo
= theory::THEORY_LAST
,
285 theory::TheoryId from
= theory::THEORY_LAST
);
287 /** Enusre that the given atoms are send to the given theory */
288 void ensureLemmaAtoms(const std::vector
<TNode
>& atoms
, theory::TheoryId theory
);
290 /** sort inference module */
291 SortInference d_sortInfer
;
293 /** Time spent in theory combination */
294 TimerStat d_combineTheoriesTime
;
299 /** Whether we were just interrupted (or not) */
301 ResourceManager
* d_resourceManager
;
304 /** Constructs a theory engine */
305 TheoryEngine(context::Context
* context
,
306 context::UserContext
* userContext
,
308 const LogicInfo
& logic
,
309 OutputManager
& outMgr
,
310 ProofNodeManager
* pnm
);
312 /** Destroys a theory engine */
317 /** "Spend" a resource during a search or preprocessing.*/
318 void spendResource(ResourceManager::Resource r
);
321 * Adds a theory. Only one theory per TheoryId can be present, so if
322 * there is another theory it will be deleted.
324 template <class TheoryClass
>
325 inline void addTheory(theory::TheoryId theoryId
)
327 Assert(d_theoryTable
[theoryId
] == NULL
&& d_theoryOut
[theoryId
] == NULL
);
328 d_theoryOut
[theoryId
] = new theory::EngineOutputChannel(this, theoryId
);
329 d_theoryTable
[theoryId
] = new TheoryClass(d_context
,
331 *d_theoryOut
[theoryId
],
332 theory::Valuation(this),
335 theory::Rewriter::registerTheoryRewriter(
336 theoryId
, d_theoryTable
[theoryId
]->getTheoryRewriter());
339 void setPropEngine(prop::PropEngine
* propEngine
)
341 d_propEngine
= propEngine
;
345 * Called when all initialization of options/logic is done, after theory
346 * objects have been created.
348 * This initializes the quantifiers engine, the "official" equality engines
349 * of each theory as required, and the model and model builder utilities.
354 * Get a pointer to the underlying propositional engine.
356 inline prop::PropEngine
* getPropEngine() const {
360 /** Get the proof node manager */
361 ProofNodeManager
* getProofNodeManager() const;
364 * Get a pointer to the underlying sat context.
366 context::Context
* getSatContext() const { return d_context
; }
369 * Get a pointer to the underlying user context.
371 context::UserContext
* getUserContext() const { return d_userContext
; }
374 * Get a pointer to the underlying quantifiers engine.
376 theory::QuantifiersEngine
* getQuantifiersEngine() const {
377 return d_quantEngine
;
380 * Get a pointer to the underlying decision manager.
382 theory::DecisionManager
* getDecisionManager() const
384 return d_decManager
.get();
389 * Queue of nodes for pre-registration.
391 std::queue
<TNode
> d_preregisterQueue
;
394 * Boolean flag denoting we are in pre-registration.
396 bool d_inPreregister
;
399 * Did the theories get any new facts since the last time we called
402 context::CDO
<bool> d_factsAsserted
;
405 * Assert the formula to the given theory.
406 * @param assertion the assertion to send (not necesserily normalized)
407 * @param original the assertion as it was sent in from the propagating theory
408 * @param toTheoryId the theory to assert to
409 * @param fromTheoryId the theory that sent it
411 void assertToTheory(TNode assertion
, TNode originalAssertion
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
);
414 * Marks a theory propagation from a theory to a theory where a
415 * theory could be the THEORY_SAT_SOLVER for literals coming from
416 * or being propagated to the SAT solver. If the receiving theory
417 * already recieved the literal, the method returns false, otherwise
420 * @param assertion the normalized assertion being sent
421 * @param originalAssertion the actual assertion that was sent
422 * @param toTheoryId the theory that is on the receiving end
423 * @param fromTheoryId the theory that sent the assertion
424 * @return true if a new assertion, false if theory already got it
426 bool markPropagation(TNode assertion
, TNode originalAssertions
, theory::TheoryId toTheoryId
, theory::TheoryId fromTheoryId
);
429 * Computes the explanation by traversing the propagation graph and
430 * asking relevant theories to explain the propagations. Initially
431 * the explanation vector should contain only the element (node, theory)
432 * where the node is the one to be explained, and the theory is the
433 * theory that sent the literal.
435 theory::TrustNode
getExplanation(
436 std::vector
<NodeTheoryPair
>& explanationVector
);
438 /** Are proofs enabled? */
439 bool isProofEnabled() const;
443 * Preprocess rewrite equality, called by the preprocessor to rewrite
444 * equalities appearing in the input.
446 theory::TrustNode
ppRewriteEquality(TNode eq
);
447 /** Notify (preprocessed) assertions. */
448 void notifyPreprocessedAssertions(const std::vector
<Node
>& assertions
);
450 /** Return whether or not we are incomplete (in the current context). */
451 inline bool isIncomplete() const { return d_incomplete
; }
454 * Returns true if we need another round of checking. If this
455 * returns true, check(FULL_EFFORT) _must_ be called by the
456 * propositional layer before reporting SAT.
458 * This is especially necessary for incomplete theories that lazily
459 * output some lemmas on FULL_EFFORT check (e.g. quantifier reasoning
460 * outputing quantifier instantiations). In such a case, a lemma can
461 * be asserted that is simplified away (perhaps it's already true).
462 * However, we must maintain the invariant that, if a theory uses the
463 * OutputChannel, it implicitly requests that another check(FULL_EFFORT)
464 * be performed before exit, even if no new facts are on its fact queue,
465 * as it might decide to further instantiate some lemmas, precluding
468 inline bool needCheck() const {
469 return d_outputChannelUsed
|| d_lemmasAdded
;
472 * Is the literal lit (possibly) critical for satisfying the input formula in
473 * the current context? This call is applicable only during collectModelInfo
474 * or during LAST_CALL effort.
476 bool isRelevant(Node lit
) const;
478 * This is called at shutdown time by the SmtEngine, just before
479 * destruction. It is important because there are destruction
480 * ordering issues between PropEngine and Theory.
485 * Solve the given literal with a theory that owns it. The proof of tliteral
486 * is carried in the trust node. The proof added to substitutionOut should
487 * take this proof into account (when proofs are enabled).
489 theory::Theory::PPAssertStatus
solve(
490 theory::TrustNode tliteral
,
491 theory::TrustSubstitutionMap
& substitutionOut
);
494 * Preregister a Theory atom with the responsible theory (or
497 void preRegister(TNode preprocessed
);
500 * Assert the formula to the appropriate theory.
501 * @param node the assertion
503 void assertFact(TNode node
);
506 * Check all (currently-active) theories for conflicts.
507 * @param effort the effort level to use
509 void check(theory::Theory::Effort effort
);
512 * Calls ppStaticLearn() on all theories, accumulating their
513 * combined contributions in the "learned" builder.
515 void ppStaticLearn(TNode in
, NodeBuilder
<>& learned
);
518 * Calls presolve() on all theories and returns true
519 * if one of the theories discovers a conflict.
524 * Calls postsolve() on all theories.
529 * Calls notifyRestart() on all active theories.
531 void notifyRestart();
533 void getPropagatedLiterals(std::vector
<TNode
>& literals
) {
534 for (; d_propagatedLiteralsIndex
< d_propagatedLiterals
.size(); d_propagatedLiteralsIndex
= d_propagatedLiteralsIndex
+ 1) {
535 Debug("getPropagatedLiterals") << "TheoryEngine::getPropagatedLiterals: propagating: " << d_propagatedLiterals
[d_propagatedLiteralsIndex
] << std::endl
;
536 literals
.push_back(d_propagatedLiterals
[d_propagatedLiteralsIndex
]);
541 * Returns the next decision request, or null if none exist. The next
542 * decision request is a literal that this theory engine prefers the SAT
543 * solver to make as its next decision. Decision requests are managed by
544 * the decision manager d_decManager.
546 Node
getNextDecisionRequest();
548 bool properConflict(TNode conflict
) const;
551 * Returns an explanation of the node propagated to the SAT solver.
553 theory::TrustNode
getExplanation(TNode node
);
556 * Get the pointer to the model object used by this theory engine.
558 theory::TheoryModel
* getModel();
560 * Get the current model for the current set of assertions. This method
561 * should only be called immediately after a satisfiable or unknown
562 * response to a check-sat call, and only if produceModels is true.
564 * If the model is not already built, this will cause this theory engine
565 * to build the model.
567 * If the model is not available (for instance, if the last call to check-sat
568 * was interrupted), then this returns the null pointer.
570 theory::TheoryModel
* getBuiltModel();
572 * This forces the model maintained by the combination engine to be built
573 * if it has not been done so already. This should be called only during a
574 * last call effort check after theory combination is run.
576 * @return true if the model was successfully built (possibly prior to this
580 /** set eager model building
582 * If this method is called, then this TheoryEngine will henceforth build
583 * its model immediately after every satisfiability check that results
584 * in a satisfiable or unknown result. The motivation for this mode is to
585 * accomodate API users that get the model object from the TheoryEngine,
586 * where we want to ensure that this model is always valid.
587 * TODO (#2648): revisit this.
589 void setEagerModelBuilding() { d_eager_model_building
= true; }
591 /** get synth solutions
593 * This method returns true if there is a synthesis solution available. This
594 * is the case if the last call to check satisfiability originated in a
595 * check-synth call, and the synthesis solver successfully found a solution
596 * for all active synthesis conjectures.
598 * This method adds entries to sol_map that map functions-to-synthesize with
599 * their solutions, for all active conjectures. This should be called
600 * immediately after the solver answers unsat for sygus input.
602 * For details on what is added to sol_map, see
603 * SynthConjecture::getSynthSolutions.
605 bool getSynthSolutions(std::map
<Node
, std::map
<Node
, Node
> >& sol_map
);
608 * Get the theory associated to a given Node.
610 * @returns the theory, or NULL if the TNode is
613 inline theory::Theory
* theoryOf(TNode node
) const {
614 return d_theoryTable
[theory::Theory::theoryOf(node
)];
618 * Get the theory associated to a the given theory id.
620 * @returns the theory
622 inline theory::Theory
* theoryOf(theory::TheoryId theoryId
) const {
623 Assert(theoryId
< theory::THEORY_LAST
);
624 return d_theoryTable
[theoryId
];
627 inline bool isTheoryEnabled(theory::TheoryId theoryId
) const {
628 return d_logicInfo
.isTheoryEnabled(theoryId
);
630 /** get the logic info used by this theory engine */
631 const LogicInfo
& getLogicInfo() const;
632 /** get the separation logic heap types */
633 bool getSepHeapTypes(TypeNode
& locType
, TypeNode
& dataType
) const;
636 * Declare heap. This is used for separation logics to set the location
637 * and data types. It should be called only once, and before any separation
638 * logic constraints are asserted to this theory engine.
640 void declareSepHeap(TypeNode locT
, TypeNode dataT
);
643 * Returns the equality status of the two terms, from the theory
644 * that owns the domain type. The types of a and b must be the same.
646 theory::EqualityStatus
getEqualityStatus(TNode a
, TNode b
);
649 * Returns the value that a theory that owns the type of var currently
650 * has (or null if none);
652 Node
getModelValue(TNode var
);
655 * Takes a literal and returns an equivalent literal that is guaranteed to be
656 * a SAT literal. This rewrites and preprocesses n, which notice may involve
657 * sending lemmas if preprocessing n involves introducing new skolems.
659 Node
ensureLiteral(TNode n
);
662 * Print all instantiations made by the quantifiers module.
664 void printInstantiations( std::ostream
& out
);
667 * Print solution for synthesis conjectures found by ce_guided_instantiation module
669 void printSynthSolution( std::ostream
& out
);
672 * Get list of quantified formulas that were instantiated
674 void getInstantiatedQuantifiedFormulas( std::vector
< Node
>& qs
);
677 * Get instantiation methods
678 * the first given forall x.q[x] returns ( a, ..., z )
679 * the second returns mappings e.g. forall x.q1[x] -> ( q1[a]...q1[z] )
680 * , ... , forall x.qn[x] -> ( qn[a]...qn[z] )
682 void getInstantiationTermVectors( Node q
, std::vector
< std::vector
< Node
> >& tvecs
);
683 void getInstantiationTermVectors( std::map
< Node
, std::vector
< std::vector
< Node
> > >& insts
);
686 * Forwards an entailment check according to the given theoryOfMode.
687 * See theory.h for documentation on entailmentCheck().
689 std::pair
<bool, Node
> entailmentCheck(options::TheoryOfMode mode
, TNode lit
);
693 /** Dump the assertions to the dump */
694 void dumpAssertions(const char* tag
);
696 /** For preprocessing pass lifting bit-vectors of size 1 to booleans */
699 SortInference
* getSortInference() { return &d_sortInfer
; }
701 /** Prints the assertions to the debug stream */
702 void printAssertions(const char* tag
);
706 std::map
< std::string
, std::vector
< theory::Theory
* > > d_attr_handle
;
709 /** Set user attribute.
711 * This function is called when an attribute is set by a user. In SMT-LIBv2
712 * this is done via the syntax (! n :attr)
714 void setUserAttribute(const std::string
& attr
,
716 const std::vector
<Node
>& node_values
,
717 const std::string
& str_value
);
719 /** Handle user attribute.
721 * Associates theory t with the attribute attr. Theory t will be
722 * notified whenever an attribute of name attr is set.
724 void handleUserAttribute(const char* attr
, theory::Theory
* t
);
727 * Check that the theory assertions are satisfied in the model.
728 * This function is called from the smt engine's checkModel routine.
730 void checkTheoryAssertionsWithModel(bool hardFailure
);
732 IntStat d_arithSubstitutionsAdded
;
733 };/* class TheoryEngine */
735 }/* CVC4 namespace */
737 #endif /* CVC4__THEORY_ENGINE_H */