Initial work towards -Wshadow (#3817)
[cvc5.git] / src / theory / theory_engine.h
1 /********************* */
2 /*! \file theory_engine.h
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Dejan Jovanovic, Morgan Deters, Andrew Reynolds
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief The theory engine
13 **
14 ** The theory engine.
15 **/
16
17 #include "cvc4_private.h"
18
19 #ifndef CVC4__THEORY_ENGINE_H
20 #define CVC4__THEORY_ENGINE_H
21
22 #include <deque>
23 #include <memory>
24 #include <set>
25 #include <unordered_map>
26 #include <utility>
27 #include <vector>
28
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/decision_manager.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/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"
52
53 namespace CVC4 {
54
55 class ResourceManager;
56 class LemmaProofRecipe;
57
58 /**
59 * A pair of a theory and a node. This is used to mark the flow of
60 * propagations between theories.
61 */
62 struct NodeTheoryPair {
63 Node d_node;
64 theory::TheoryId d_theory;
65 size_t d_timestamp;
66 NodeTheoryPair(TNode n, theory::TheoryId t, size_t ts = 0)
67 : d_node(n), d_theory(t), d_timestamp(ts)
68 {
69 }
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;
74 }
75 };/* struct NodeTheoryPair */
76
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));
83 }
84 };/* struct NodeTheoryPairHashFunction */
85
86
87 /* Forward declarations */
88 namespace theory {
89 class TheoryModel;
90 class TheoryEngineModelBuilder;
91
92 namespace eq {
93 class EqualityEngine;
94 }/* CVC4::theory::eq namespace */
95
96 namespace quantifiers {
97 class TermDb;
98 }
99
100 class EntailmentCheckParameters;
101 class EntailmentCheckSideEffects;
102 }/* CVC4::theory namespace */
103
104 class DecisionEngine;
105 class RemoveTermFormulas;
106
107 /**
108 * This is essentially an abstraction for a collection of theories. A
109 * TheoryEngine provides services to a PropEngine, making various
110 * T-solvers look like a single unit to the propositional part of
111 * CVC4.
112 */
113 class TheoryEngine {
114
115 /** Shared terms database can use the internals notify the theories */
116 friend class SharedTermsDatabase;
117 friend class theory::quantifiers::TermDb;
118
119 /** Associated PropEngine engine */
120 prop::PropEngine* d_propEngine;
121
122 /** Access to decision engine */
123 DecisionEngine* d_decisionEngine;
124
125 /** Our context */
126 context::Context* d_context;
127
128 /** Our user context */
129 context::UserContext* d_userContext;
130
131 /**
132 * A table of from theory IDs to theory pointers. Never use this table
133 * directly, use theoryOf() instead.
134 */
135 theory::Theory* d_theoryTable[theory::THEORY_LAST];
136
137 /**
138 * A collection of theories that are "active" for the current run.
139 * This set is provided by the user (as a logic string, say, in SMT-LIBv2
140 * format input), or else by default it's all-inclusive. This is important
141 * because we can optimize for single-theory runs (no sharing), can reduce
142 * the cost of walking the DAG on registration, etc.
143 */
144 const LogicInfo& d_logicInfo;
145
146 /**
147 * The database of shared terms.
148 */
149 SharedTermsDatabase d_sharedTerms;
150
151 /**
152 * Master equality engine, to share with theories.
153 */
154 theory::eq::EqualityEngine* d_masterEqualityEngine;
155
156 /** notify class for master equality engine */
157 class NotifyClass : public theory::eq::EqualityEngineNotify {
158 TheoryEngine& d_te;
159 public:
160 NotifyClass(TheoryEngine& te): d_te(te) {}
161 bool eqNotifyTriggerEquality(TNode equality, bool value) override
162 {
163 return true;
164 }
165 bool eqNotifyTriggerPredicate(TNode predicate, bool value) override
166 {
167 return true;
168 }
169 bool eqNotifyTriggerTermEquality(theory::TheoryId tag,
170 TNode t1,
171 TNode t2,
172 bool value) override
173 {
174 return true;
175 }
176 void eqNotifyConstantTermMerge(TNode t1, TNode t2) override {}
177 void eqNotifyNewClass(TNode t) override { d_te.eqNotifyNewClass(t); }
178 void eqNotifyPreMerge(TNode t1, TNode t2) override
179 {
180 }
181 void eqNotifyPostMerge(TNode t1, TNode t2) override
182 {
183 }
184 void eqNotifyDisequal(TNode t1, TNode t2, TNode reason) override
185 {
186 }
187 };/* class TheoryEngine::NotifyClass */
188 NotifyClass d_masterEENotify;
189
190 /**
191 * notification methods
192 */
193 void eqNotifyNewClass(TNode t);
194 void eqNotifyPreMerge(TNode t1, TNode t2);
195 void eqNotifyPostMerge(TNode t1, TNode t2);
196 void eqNotifyDisequal(TNode t1, TNode t2, TNode reason);
197
198 /**
199 * The quantifiers engine
200 */
201 theory::QuantifiersEngine* d_quantEngine;
202 /**
203 * The decision manager
204 */
205 std::unique_ptr<theory::DecisionManager> d_decManager;
206
207 /**
208 * Default model object
209 */
210 theory::TheoryModel* d_curr_model;
211 bool d_aloc_curr_model;
212 /**
213 * Model builder object
214 */
215 theory::TheoryEngineModelBuilder* d_curr_model_builder;
216 bool d_aloc_curr_model_builder;
217 /** are we in eager model building mode? (see setEagerModelBuilding). */
218 bool d_eager_model_building;
219
220 typedef std::unordered_map<Node, Node, NodeHashFunction> NodeMap;
221 typedef std::unordered_map<TNode, Node, TNodeHashFunction> TNodeMap;
222
223 /**
224 * Cache for theory-preprocessing of assertions
225 */
226 NodeMap d_ppCache;
227
228 /**
229 * Used for "missed-t-propagations" dumping mode only. A set of all
230 * theory-propagable literals.
231 */
232 context::CDList<TNode> d_possiblePropagations;
233
234 /**
235 * Used for "missed-t-propagations" dumping mode only. A
236 * context-dependent set of those theory-propagable literals that
237 * have been propagated.
238 */
239 context::CDHashSet<Node, NodeHashFunction> d_hasPropagated;
240
241
242 /**
243 * Statistics for a particular theory.
244 */
245 class Statistics {
246
247 static std::string mkName(std::string prefix,
248 theory::TheoryId theory,
249 std::string suffix) {
250 std::stringstream ss;
251 ss << prefix << theory << suffix;
252 return ss.str();
253 }
254
255 public:
256 IntStat conflicts, propagations, lemmas, requirePhase, restartDemands;
257
258 Statistics(theory::TheoryId theory);
259 ~Statistics();
260 };/* class TheoryEngine::Statistics */
261
262 /**
263 * An output channel for Theory that passes messages
264 * back to a TheoryEngine.
265 */
266 class EngineOutputChannel : public theory::OutputChannel {
267 friend class TheoryEngine;
268
269 /**
270 * The theory engine we're communicating with.
271 */
272 TheoryEngine* d_engine;
273
274 /**
275 * The statistics of the theory interractions.
276 */
277 Statistics d_statistics;
278
279 /** The theory owning this channel. */
280 theory::TheoryId d_theory;
281
282 public:
283 EngineOutputChannel(TheoryEngine* engine, theory::TheoryId theory)
284 : d_engine(engine), d_statistics(theory), d_theory(theory) {}
285
286 void safePoint(ResourceManager::Resource r) override
287 {
288 spendResource(r);
289 if (d_engine->d_interrupted) {
290 throw theory::Interrupted();
291 }
292 }
293
294 void conflict(TNode conflictNode,
295 std::unique_ptr<Proof> pf = nullptr) override;
296 bool propagate(TNode literal) override;
297
298 theory::LemmaStatus lemma(TNode lemma, ProofRule rule,
299 bool removable = false, bool preprocess = false,
300 bool sendAtoms = false) override;
301
302 theory::LemmaStatus splitLemma(TNode lemma,
303 bool removable = false) override;
304
305 void demandRestart() override {
306 NodeManager* curr = NodeManager::currentNM();
307 Node restartVar = curr->mkSkolem(
308 "restartVar", curr->booleanType(),
309 "A boolean variable asserted to be true to force a restart");
310 Trace("theory::restart")
311 << "EngineOutputChannel<" << d_theory << ">::restart(" << restartVar
312 << ")" << std::endl;
313 ++d_statistics.restartDemands;
314 lemma(restartVar, RULE_INVALID, true);
315 }
316
317 void requirePhase(TNode n, bool phase) override {
318 Debug("theory") << "EngineOutputChannel::requirePhase(" << n << ", "
319 << phase << ")" << std::endl;
320 ++d_statistics.requirePhase;
321 d_engine->d_propEngine->requirePhase(n, phase);
322 }
323
324 void setIncomplete() override {
325 Trace("theory") << "TheoryEngine::setIncomplete()" << std::endl;
326 d_engine->setIncomplete(d_theory);
327 }
328
329 void spendResource(ResourceManager::Resource r) override
330 {
331 d_engine->spendResource(r);
332 }
333
334 void handleUserAttribute(const char* attr, theory::Theory* t) override {
335 d_engine->handleUserAttribute(attr, t);
336 }
337
338 private:
339 /**
340 * A helper function for registering lemma recipes with the proof engine
341 */
342 void registerLemmaRecipe(Node lemma, Node originalLemma, bool preprocess,
343 theory::TheoryId theoryId);
344 }; /* class TheoryEngine::EngineOutputChannel */
345
346 /**
347 * Output channels for individual theories.
348 */
349 EngineOutputChannel* d_theoryOut[theory::THEORY_LAST];
350
351 /**
352 * Are we in conflict.
353 */
354 context::CDO<bool> d_inConflict;
355
356 /**
357 * Are we in "SAT mode"? In this state, the user can query for the model.
358 * This corresponds to the state in Figure 4.1, page 52 of the SMT-LIB
359 * standard, version 2.6.
360 */
361 bool d_inSatMode;
362
363 /**
364 * Called by the theories to notify of a conflict.
365 */
366 void conflict(TNode conflict, theory::TheoryId theoryId);
367
368 /**
369 * Debugging flag to ensure that shutdown() is called before the
370 * destructor.
371 */
372 bool d_hasShutDown;
373
374 /**
375 * True if a theory has notified us of incompleteness (at this
376 * context level or below).
377 */
378 context::CDO<bool> d_incomplete;
379
380 /**
381 * Called by the theories to notify that the current branch is incomplete.
382 */
383 void setIncomplete(theory::TheoryId theory) {
384 d_incomplete = true;
385 }
386
387
388 /**
389 * Mapping of propagations from recievers to senders.
390 */
391 typedef context::CDHashMap<NodeTheoryPair, NodeTheoryPair, NodeTheoryPairHashFunction> PropagationMap;
392 PropagationMap d_propagationMap;
393
394 /**
395 * Timestamp of propagations
396 */
397 context::CDO<size_t> d_propagationMapTimestamp;
398
399 /**
400 * Literals that are propagated by the theory. Note that these are TNodes.
401 * The theory can only propagate nodes that have an assigned literal in the
402 * SAT solver and are hence referenced in the SAT solver.
403 */
404 context::CDList<TNode> d_propagatedLiterals;
405
406 /**
407 * The index of the next literal to be propagated by a theory.
408 */
409 context::CDO<unsigned> d_propagatedLiteralsIndex;
410
411 /**
412 * Called by the output channel to propagate literals and facts
413 * @return false if immediate conflict
414 */
415 bool propagate(TNode literal, theory::TheoryId theory);
416
417 /**
418 * Internal method to call the propagation routines and collect the
419 * propagated literals.
420 */
421 void propagate(theory::Theory::Effort effort);
422
423 /**
424 * A variable to mark if we added any lemmas.
425 */
426 bool d_lemmasAdded;
427
428 /**
429 * A variable to mark if the OutputChannel was "used" by any theory
430 * since the start of the last check. If it has been, we require
431 * a FULL_EFFORT check before exiting and reporting SAT.
432 *
433 * See the documentation for the needCheck() function, below.
434 */
435 bool d_outputChannelUsed;
436
437 /** Atom requests from lemmas */
438 AtomRequests d_atomRequests;
439
440 /**
441 * Adds a new lemma, returning its status.
442 * @param node the lemma
443 * @param negated should the lemma be asserted negated
444 * @param removable can the lemma be remove (restrictions apply)
445 * @param needAtoms if not THEORY_LAST, then
446 */
447 theory::LemmaStatus lemma(TNode node,
448 ProofRule rule,
449 bool negated,
450 bool removable,
451 bool preprocess,
452 theory::TheoryId atomsTo);
453
454 /** Enusre that the given atoms are send to the given theory */
455 void ensureLemmaAtoms(const std::vector<TNode>& atoms, theory::TheoryId theory);
456
457 RemoveTermFormulas& d_tform_remover;
458
459 /** sort inference module */
460 SortInference d_sortInfer;
461
462 /** Time spent in theory combination */
463 TimerStat d_combineTheoriesTime;
464
465 Node d_true;
466 Node d_false;
467
468 /** Whether we were just interrupted (or not) */
469 bool d_interrupted;
470 ResourceManager* d_resourceManager;
471
472 public:
473 /** Constructs a theory engine */
474 TheoryEngine(context::Context* context,
475 context::UserContext* userContext,
476 RemoveTermFormulas& iteRemover,
477 const LogicInfo& logic);
478
479 /** Destroys a theory engine */
480 ~TheoryEngine();
481
482 void interrupt();
483
484 /** "Spend" a resource during a search or preprocessing.*/
485 void spendResource(ResourceManager::Resource r);
486
487 /**
488 * Adds a theory. Only one theory per TheoryId can be present, so if
489 * there is another theory it will be deleted.
490 */
491 template <class TheoryClass>
492 inline void addTheory(theory::TheoryId theoryId)
493 {
494 Assert(d_theoryTable[theoryId] == NULL && d_theoryOut[theoryId] == NULL);
495 d_theoryOut[theoryId] = new EngineOutputChannel(this, theoryId);
496 d_theoryTable[theoryId] = new TheoryClass(d_context,
497 d_userContext,
498 *d_theoryOut[theoryId],
499 theory::Valuation(this),
500 d_logicInfo);
501 }
502
503 inline void setPropEngine(prop::PropEngine* propEngine) {
504 Assert(d_propEngine == NULL);
505 d_propEngine = propEngine;
506 }
507
508 inline void setDecisionEngine(DecisionEngine* decisionEngine) {
509 Assert(d_decisionEngine == NULL);
510 d_decisionEngine = decisionEngine;
511 }
512
513 /** Called when all initialization of options/logic is done */
514 void finishInit();
515
516 /**
517 * Get a pointer to the underlying propositional engine.
518 */
519 inline prop::PropEngine* getPropEngine() const {
520 return d_propEngine;
521 }
522
523 /**
524 * Get a pointer to the underlying sat context.
525 */
526 inline context::Context* getSatContext() const {
527 return d_context;
528 }
529
530 /**
531 * Get a pointer to the underlying user context.
532 */
533 inline context::Context* getUserContext() const {
534 return d_userContext;
535 }
536
537 /**
538 * Get a pointer to the underlying quantifiers engine.
539 */
540 theory::QuantifiersEngine* getQuantifiersEngine() const {
541 return d_quantEngine;
542 }
543 /**
544 * Get a pointer to the underlying decision manager.
545 */
546 theory::DecisionManager* getDecisionManager() const
547 {
548 return d_decManager.get();
549 }
550
551 private:
552 /**
553 * Helper for preprocess
554 */
555 Node ppTheoryRewrite(TNode term);
556
557 /**
558 * Queue of nodes for pre-registration.
559 */
560 std::queue<TNode> d_preregisterQueue;
561
562 /**
563 * Boolean flag denoting we are in pre-registration.
564 */
565 bool d_inPreregister;
566
567 /**
568 * Did the theories get any new facts since the last time we called
569 * check()
570 */
571 context::CDO<bool> d_factsAsserted;
572
573 /**
574 * Map from equality atoms to theories that would like to be notified about them.
575 */
576
577
578 /**
579 * Assert the formula to the given theory.
580 * @param assertion the assertion to send (not necesserily normalized)
581 * @param original the assertion as it was sent in from the propagating theory
582 * @param toTheoryId the theory to assert to
583 * @param fromTheoryId the theory that sent it
584 */
585 void assertToTheory(TNode assertion, TNode originalAssertion, theory::TheoryId toTheoryId, theory::TheoryId fromTheoryId);
586
587 /**
588 * Marks a theory propagation from a theory to a theory where a
589 * theory could be the THEORY_SAT_SOLVER for literals coming from
590 * or being propagated to the SAT solver. If the receiving theory
591 * already recieved the literal, the method returns false, otherwise
592 * it returns true.
593 *
594 * @param assertion the normalized assertion being sent
595 * @param originalAssertion the actual assertion that was sent
596 * @param toTheoryId the theory that is on the receiving end
597 * @param fromTheoryId the theory that sent the assertino
598 * @return true if a new assertion, false if theory already got it
599 */
600 bool markPropagation(TNode assertion, TNode originalAssertions, theory::TheoryId toTheoryId, theory::TheoryId fromTheoryId);
601
602 /**
603 * Computes the explanation by travarsing the propagation graph and
604 * asking relevant theories to explain the propagations. Initially
605 * the explanation vector should contain only the element (node, theory)
606 * where the node is the one to be explained, and the theory is the
607 * theory that sent the literal. The lemmaProofRecipe will contain a list
608 * of the explanation steps required to produce the original node.
609 */
610 void getExplanation(std::vector<NodeTheoryPair>& explanationVector, LemmaProofRecipe* lemmaProofRecipe);
611
612 public:
613
614 /**
615 * Signal the start of a new round of assertion preprocessing
616 */
617 void preprocessStart();
618
619 /**
620 * Runs theory specific preprocessing on the non-Boolean parts of
621 * the formula. This is only called on input assertions, after ITEs
622 * have been removed.
623 */
624 Node preprocess(TNode node);
625
626 /** Notify (preprocessed) assertions. */
627 void notifyPreprocessedAssertions(const std::vector<Node>& assertions);
628
629 /** Return whether or not we are incomplete (in the current context). */
630 inline bool isIncomplete() const { return d_incomplete; }
631
632 /**
633 * Returns true if we need another round of checking. If this
634 * returns true, check(FULL_EFFORT) _must_ be called by the
635 * propositional layer before reporting SAT.
636 *
637 * This is especially necessary for incomplete theories that lazily
638 * output some lemmas on FULL_EFFORT check (e.g. quantifier reasoning
639 * outputing quantifier instantiations). In such a case, a lemma can
640 * be asserted that is simplified away (perhaps it's already true).
641 * However, we must maintain the invariant that, if a theory uses the
642 * OutputChannel, it implicitly requests that another check(FULL_EFFORT)
643 * be performed before exit, even if no new facts are on its fact queue,
644 * as it might decide to further instantiate some lemmas, precluding
645 * a SAT response.
646 */
647 inline bool needCheck() const {
648 return d_outputChannelUsed || d_lemmasAdded;
649 }
650
651 /**
652 * This is called at shutdown time by the SmtEngine, just before
653 * destruction. It is important because there are destruction
654 * ordering issues between PropEngine and Theory.
655 */
656 void shutdown();
657
658 /**
659 * Solve the given literal with a theory that owns it.
660 */
661 theory::Theory::PPAssertStatus solve(TNode literal,
662 theory::SubstitutionMap& substitutionOut);
663
664 /**
665 * Preregister a Theory atom with the responsible theory (or
666 * theories).
667 */
668 void preRegister(TNode preprocessed);
669
670 /**
671 * Assert the formula to the appropriate theory.
672 * @param node the assertion
673 */
674 void assertFact(TNode node);
675
676 /**
677 * Check all (currently-active) theories for conflicts.
678 * @param effort the effort level to use
679 */
680 void check(theory::Theory::Effort effort);
681
682 /**
683 * Run the combination framework.
684 */
685 void combineTheories();
686
687 /**
688 * Calls ppStaticLearn() on all theories, accumulating their
689 * combined contributions in the "learned" builder.
690 */
691 void ppStaticLearn(TNode in, NodeBuilder<>& learned);
692
693 /**
694 * Calls presolve() on all theories and returns true
695 * if one of the theories discovers a conflict.
696 */
697 bool presolve();
698
699 /**
700 * Calls postsolve() on all theories.
701 */
702 void postsolve();
703
704 /**
705 * Calls notifyRestart() on all active theories.
706 */
707 void notifyRestart();
708
709 void getPropagatedLiterals(std::vector<TNode>& literals) {
710 for (; d_propagatedLiteralsIndex < d_propagatedLiterals.size(); d_propagatedLiteralsIndex = d_propagatedLiteralsIndex + 1) {
711 Debug("getPropagatedLiterals") << "TheoryEngine::getPropagatedLiterals: propagating: " << d_propagatedLiterals[d_propagatedLiteralsIndex] << std::endl;
712 literals.push_back(d_propagatedLiterals[d_propagatedLiteralsIndex]);
713 }
714 }
715
716 /**
717 * Returns the next decision request, or null if none exist. The next
718 * decision request is a literal that this theory engine prefers the SAT
719 * solver to make as its next decision. Decision requests are managed by
720 * the decision manager d_decManager.
721 */
722 Node getNextDecisionRequest();
723
724 bool properConflict(TNode conflict) const;
725 bool properPropagation(TNode lit) const;
726 bool properExplanation(TNode node, TNode expl) const;
727
728 /**
729 * Returns an explanation of the node propagated to the SAT solver.
730 */
731 Node getExplanation(TNode node);
732
733 /**
734 * Returns an explanation of the node propagated to the SAT solver and the theory
735 * that propagated it.
736 */
737 Node getExplanationAndRecipe(TNode node, LemmaProofRecipe* proofRecipe);
738
739 /**
740 * collect model info
741 */
742 bool collectModelInfo(theory::TheoryModel* m);
743 /** post process model */
744 void postProcessModel( theory::TheoryModel* m );
745
746 /**
747 * Get the pointer to the model object used by this theory engine.
748 */
749 theory::TheoryModel* getModel();
750 /**
751 * Get the current model for the current set of assertions. This method
752 * should only be called immediately after a satisfiable or unknown
753 * response to a check-sat call, and only if produceModels is true.
754 *
755 * If the model is not already built, this will cause this theory engine
756 * to build the model.
757 *
758 * If the model is not available (for instance, if the last call to check-sat
759 * was interrupted), then this returns the null pointer.
760 */
761 theory::TheoryModel* getBuiltModel();
762 /** set eager model building
763 *
764 * If this method is called, then this TheoryEngine will henceforth build
765 * its model immediately after every satisfiability check that results
766 * in a satisfiable or unknown result. The motivation for this mode is to
767 * accomodate API users that get the model object from the TheoryEngine,
768 * where we want to ensure that this model is always valid.
769 * TODO (#2648): revisit this.
770 */
771 void setEagerModelBuilding() { d_eager_model_building = true; }
772
773 /** get synth solutions
774 *
775 * This method returns true if there is a synthesis solution available. This
776 * is the case if the last call to check satisfiability originated in a
777 * check-synth call, and the synthesis solver successfully found a solution
778 * for all active synthesis conjectures.
779 *
780 * This method adds entries to sol_map that map functions-to-synthesize with
781 * their solutions, for all active conjectures. This should be called
782 * immediately after the solver answers unsat for sygus input.
783 *
784 * For details on what is added to sol_map, see
785 * SynthConjecture::getSynthSolutions.
786 */
787 bool getSynthSolutions(std::map<Node, std::map<Node, Node> >& sol_map);
788
789 /**
790 * Get the model builder
791 */
792 theory::TheoryEngineModelBuilder* getModelBuilder() { return d_curr_model_builder; }
793
794 /**
795 * Get the theory associated to a given Node.
796 *
797 * @returns the theory, or NULL if the TNode is
798 * of built-in type.
799 */
800 inline theory::Theory* theoryOf(TNode node) const {
801 return d_theoryTable[theory::Theory::theoryOf(node)];
802 }
803
804 /**
805 * Get the theory associated to a the given theory id.
806 *
807 * @returns the theory
808 */
809 inline theory::Theory* theoryOf(theory::TheoryId theoryId) const {
810 Assert(theoryId < theory::THEORY_LAST);
811 return d_theoryTable[theoryId];
812 }
813
814 inline bool isTheoryEnabled(theory::TheoryId theoryId) const {
815 return d_logicInfo.isTheoryEnabled(theoryId);
816 }
817 /** get the logic info used by this theory engine */
818 const LogicInfo& getLogicInfo() const;
819 /**
820 * Returns the equality status of the two terms, from the theory
821 * that owns the domain type. The types of a and b must be the same.
822 */
823 theory::EqualityStatus getEqualityStatus(TNode a, TNode b);
824
825 /**
826 * Returns the value that a theory that owns the type of var currently
827 * has (or null if none);
828 */
829 Node getModelValue(TNode var);
830
831 /**
832 * Takes a literal and returns an equivalent literal that is guaranteed to be a SAT literal
833 */
834 Node ensureLiteral(TNode n);
835
836 /**
837 * Print all instantiations made by the quantifiers module.
838 */
839 void printInstantiations( std::ostream& out );
840
841 /**
842 * Print solution for synthesis conjectures found by ce_guided_instantiation module
843 */
844 void printSynthSolution( std::ostream& out );
845
846 /**
847 * Get list of quantified formulas that were instantiated
848 */
849 void getInstantiatedQuantifiedFormulas( std::vector< Node >& qs );
850
851 /**
852 * Get instantiation methods
853 * first inputs forall x.q[x] and returns ( q[a], ..., q[z] )
854 * second inputs forall x.q[x] and returns ( a, ..., z )
855 * third and fourth return mappings e.g. forall x.q1[x] -> ( q1[a]...q1[z] ) , ... , forall x.qn[x] -> ( qn[a]...qn[z] )
856 */
857 void getInstantiations( Node q, std::vector< Node >& insts );
858 void getInstantiationTermVectors( Node q, std::vector< std::vector< Node > >& tvecs );
859 void getInstantiations( std::map< Node, std::vector< Node > >& insts );
860 void getInstantiationTermVectors( std::map< Node, std::vector< std::vector< Node > > >& insts );
861
862 /**
863 * Get instantiated conjunction, returns q[t1] ^ ... ^ q[tn] where t1...tn are current set of instantiations for q.
864 * Can be used for quantifier elimination when satisfiable and q[t1] ^ ... ^ q[tn] |= q
865 */
866 Node getInstantiatedConjunction( Node q );
867
868 /**
869 * Forwards an entailment check according to the given theoryOfMode.
870 * See theory.h for documentation on entailmentCheck().
871 */
872 std::pair<bool, Node> entailmentCheck(
873 options::TheoryOfMode mode,
874 TNode lit,
875 const theory::EntailmentCheckParameters* params = NULL,
876 theory::EntailmentCheckSideEffects* out = NULL);
877
878 private:
879 /** Default visitor for pre-registration */
880 PreRegisterVisitor d_preRegistrationVisitor;
881
882 /** Visitor for collecting shared terms */
883 SharedTermsVisitor d_sharedTermsVisitor;
884
885 /** Dump the assertions to the dump */
886 void dumpAssertions(const char* tag);
887
888 /** For preprocessing pass lifting bit-vectors of size 1 to booleans */
889 public:
890 void staticInitializeBVOptions(const std::vector<Node>& assertions);
891
892 Node ppSimpITE(TNode assertion);
893 /** Returns false if an assertion simplified to false. */
894 bool donePPSimpITE(std::vector<Node>& assertions);
895
896 SharedTermsDatabase* getSharedTermsDatabase() { return &d_sharedTerms; }
897
898 theory::eq::EqualityEngine* getMasterEqualityEngine() { return d_masterEqualityEngine; }
899
900 RemoveTermFormulas* getTermFormulaRemover() { return &d_tform_remover; }
901
902 SortInference* getSortInference() { return &d_sortInfer; }
903
904 /** Prints the assertions to the debug stream */
905 void printAssertions(const char* tag);
906
907 /** Theory alternative is in use. */
908 bool useTheoryAlternative(const std::string& name);
909
910 /** Enables using a theory alternative by name. */
911 void enableTheoryAlternative(const std::string& name);
912
913 private:
914 std::set< std::string > d_theoryAlternatives;
915
916 std::map< std::string, std::vector< theory::Theory* > > d_attr_handle;
917
918 public:
919 /** Set user attribute.
920 *
921 * This function is called when an attribute is set by a user. In SMT-LIBv2
922 * this is done via the syntax (! n :attr)
923 */
924 void setUserAttribute(const std::string& attr,
925 Node n,
926 const std::vector<Node>& node_values,
927 const std::string& str_value);
928
929 /** Handle user attribute.
930 *
931 * Associates theory t with the attribute attr. Theory t will be
932 * notified whenever an attribute of name attr is set.
933 */
934 void handleUserAttribute(const char* attr, theory::Theory* t);
935
936 /**
937 * Check that the theory assertions are satisfied in the model.
938 * This function is called from the smt engine's checkModel routine.
939 */
940 void checkTheoryAssertionsWithModel(bool hardFailure);
941
942 private:
943 IntStat d_arithSubstitutionsAdded;
944
945 };/* class TheoryEngine */
946
947 }/* CVC4 namespace */
948
949 #endif /* CVC4__THEORY_ENGINE_H */