Merge branch 'master' of https://github.com/CVC4/CVC4.git
[cvc5.git] / src / theory / theory.h
1 /********************* */
2 /*! \file theory.h
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Dejan Jovanovic, Morgan Deters, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 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 Base of the theory interface.
13 **
14 ** Base of the theory interface.
15 **/
16
17 #include "cvc4_private.h"
18
19 #ifndef __CVC4__THEORY__THEORY_H
20 #define __CVC4__THEORY__THEORY_H
21
22 #include <ext/hash_set>
23 #include <iosfwd>
24 #include <string>
25
26 #include "context/cdlist.h"
27 #include "context/cdo.h"
28 #include "context/context.h"
29 #include "expr/node.h"
30 #include "lib/ffs.h"
31 #include "options/options.h"
32 #include "options/theory_options.h"
33 #include "options/theoryof_mode.h"
34 #include "smt/command.h"
35 #include "smt/dump.h"
36 #include "smt/logic_request.h"
37 #include "theory/logic_info.h"
38 #include "theory/output_channel.h"
39 #include "theory/valuation.h"
40 #include "util/statistics_registry.h"
41
42 namespace CVC4 {
43
44 class TheoryEngine;
45
46 namespace theory {
47
48 class QuantifiersEngine;
49 class TheoryModel;
50 class SubstitutionMap;
51
52 class EntailmentCheckParameters;
53 class EntailmentCheckSideEffects;
54
55 namespace rrinst {
56 class CandidateGenerator;
57 }/* CVC4::theory::rrinst namespace */
58
59 namespace eq {
60 class EqualityEngine;
61 }/* CVC4::theory::eq namespace */
62
63 /**
64 * Information about an assertion for the theories.
65 */
66 struct Assertion {
67
68 /** The assertion */
69 Node assertion;
70 /** Has this assertion been preregistered with this theory */
71 bool isPreregistered;
72
73 Assertion(TNode assertion, bool isPreregistered)
74 : assertion(assertion), isPreregistered(isPreregistered) {}
75
76 /**
77 * Convert the assertion to a TNode.
78 */
79 operator TNode () const {
80 return assertion;
81 }
82
83 /**
84 * Convert the assertion to a Node.
85 */
86 operator Node () const {
87 return assertion;
88 }
89
90 };/* struct Assertion */
91
92 /**
93 * A (oredered) pair of terms a theory cares about.
94 */
95 struct CarePair {
96
97 TNode a, b;
98 TheoryId theory;
99
100 public:
101
102 CarePair(TNode a, TNode b, TheoryId theory)
103 : a(a < b ? a : b), b(a < b ? b : a), theory(theory) {}
104
105 bool operator == (const CarePair& other) const {
106 return (theory == other.theory) && (a == other.a) && (b == other.b);
107 }
108
109 bool operator < (const CarePair& other) const {
110 if (theory < other.theory) return true;
111 if (theory > other.theory) return false;
112 if (a < other.a) return true;
113 if (a > other.a) return false;
114 return b < other.b;
115 }
116
117 };/* struct CarePair */
118
119 /**
120 * A set of care pairs.
121 */
122 typedef std::set<CarePair> CareGraph;
123
124 /**
125 * Base class for T-solvers. Abstract DPLL(T).
126 *
127 * This is essentially an interface class. The TheoryEngine has
128 * pointers to Theory. Note that only one specific Theory type (e.g.,
129 * TheoryUF) can exist per NodeManager, because of how the
130 * RegisteredAttr works. (If you need multiple instances of the same
131 * theory, you'll have to write a multiplexed theory that dispatches
132 * all calls to them.)
133 */
134 class Theory {
135
136 private:
137
138 friend class ::CVC4::TheoryEngine;
139
140 // Disallow default construction, copy, assignment.
141 Theory() CVC4_UNDEFINED;
142 Theory(const Theory&) CVC4_UNDEFINED;
143 Theory& operator=(const Theory&) CVC4_UNDEFINED;
144
145 /**
146 * An integer identifying the type of the theory
147 */
148 TheoryId d_id;
149
150 /** Name of this theory instance. Along with the TheoryId this should provide
151 * an unique string identifier for each instance of a Theory class. We need
152 * this to ensure unique statistics names over multiple theory instances. */
153 std::string d_instanceName;
154
155 /**
156 * The SAT search context for the Theory.
157 */
158 context::Context* d_satContext;
159
160 /**
161 * The user level assertion context for the Theory.
162 */
163 context::UserContext* d_userContext;
164
165 /**
166 * Information about the logic we're operating within.
167 */
168 const LogicInfo& d_logicInfo;
169
170 /**
171 * The assertFact() queue.
172 *
173 * These can not be TNodes as some atoms (such as equalities) are sent
174 * across theories without being stored in a global map.
175 */
176 context::CDList<Assertion> d_facts;
177
178 /** Index into the head of the facts list */
179 context::CDO<unsigned> d_factsHead;
180
181 /**
182 * Add shared term to the theory.
183 */
184 void addSharedTermInternal(TNode node);
185
186 /**
187 * Indices for splitting on the shared terms.
188 */
189 context::CDO<unsigned> d_sharedTermsIndex;
190
191 /**
192 * The care graph the theory will use during combination.
193 */
194 CareGraph* d_careGraph;
195
196 /**
197 * Reference to the quantifiers engine (or NULL, if quantifiers are
198 * not supported or not enabled).
199 */
200 QuantifiersEngine* d_quantEngine;
201
202 protected:
203
204 // === STATISTICS ===
205 /** time spent in check calls */
206 TimerStat d_checkTime;
207 /** time spent in theory combination */
208 TimerStat d_computeCareGraphTime;
209
210 /**
211 * The only method to add suff to the care graph.
212 */
213 void addCarePair(TNode t1, TNode t2) {
214 if (d_careGraph) {
215 d_careGraph->insert(CarePair(t1, t2, d_id));
216 }
217 }
218
219 /**
220 * The function should compute the care graph over the shared terms.
221 * The default function returns all the pairs among the shared variables.
222 */
223 virtual void computeCareGraph();
224
225 /**
226 * A list of shared terms that the theory has.
227 */
228 context::CDList<TNode> d_sharedTerms;
229
230 /**
231 * Helper function for computeRelevantTerms
232 */
233 void collectTerms(TNode n, std::set<Node>& termSet) const;
234 /**
235 * Scans the current set of assertions and shared terms top-down
236 * until a theory-leaf is reached, and adds all terms found to
237 * termSet. This is used by collectModelInfo to delimit the set of
238 * terms that should be used when constructing a model
239 */
240 void computeRelevantTerms(std::set<Node>& termSet, bool includeShared = true) const;
241
242 /**
243 * Construct a Theory.
244 *
245 * The pair <id, instance> is assumed to uniquely identify this Theory
246 * w.r.t. the SmtEngine.
247 */
248 Theory(TheoryId id, context::Context* satContext,
249 context::UserContext* userContext, OutputChannel& out,
250 Valuation valuation, const LogicInfo& logicInfo,
251 std::string instance = "") throw(); // taking : No default.
252
253 /**
254 * This is called at shutdown time by the TheoryEngine, just before
255 * destruction. It is important because there are destruction
256 * ordering issues between PropEngine and Theory (based on what
257 * hard-links to Nodes are outstanding). As the fact queue might be
258 * nonempty, we ensure here that it's clear. If you overload this,
259 * you must make an explicit call here to this->Theory::shutdown()
260 * too.
261 */
262 virtual void shutdown() { }
263
264 /**
265 * The output channel for the Theory.
266 */
267 OutputChannel* d_out;
268
269 /**
270 * The valuation proxy for the Theory to communicate back with the
271 * theory engine (and other theories).
272 */
273 Valuation d_valuation;
274
275 /**
276 * Whether proofs are enabled
277 *
278 */
279 bool d_proofsEnabled;
280
281 /**
282 * Returns the next assertion in the assertFact() queue.
283 *
284 * @return the next assertion in the assertFact() queue
285 */
286 inline Assertion get();
287
288 const LogicInfo& getLogicInfo() const {
289 return d_logicInfo;
290 }
291
292 /**
293 * The theory that owns the uninterpreted sort.
294 */
295 static TheoryId s_uninterpretedSortOwner;
296
297 void printFacts(std::ostream& os) const;
298 void debugPrintFacts() const;
299
300 /**
301 * Whether proofs are enabled
302 *
303 */
304 bool d_proofEnabled;
305
306 public:
307
308 /**
309 * Return the ID of the theory responsible for the given type.
310 */
311 static inline TheoryId theoryOf(TypeNode typeNode) {
312 Trace("theory::internal") << "theoryOf(" << typeNode << ")" << std::endl;
313 TheoryId id;
314 while (typeNode.isPredicateSubtype()) {
315 typeNode = typeNode.getSubtypeParentType();
316 }
317 if (typeNode.getKind() == kind::TYPE_CONSTANT) {
318 id = typeConstantToTheoryId(typeNode.getConst<TypeConstant>());
319 } else {
320 id = kindToTheoryId(typeNode.getKind());
321 }
322 if (id == THEORY_BUILTIN) {
323 Trace("theory::internal") << "theoryOf(" << typeNode << ") == " << s_uninterpretedSortOwner << std::endl;
324 return s_uninterpretedSortOwner;
325 }
326 return id;
327 }
328
329 /**
330 * Returns the ID of the theory responsible for the given node.
331 */
332 static TheoryId theoryOf(TheoryOfMode mode, TNode node);
333
334 /**
335 * Returns the ID of the theory responsible for the given node.
336 */
337 static inline TheoryId theoryOf(TNode node) {
338 return theoryOf(options::theoryOfMode(), node);
339 }
340
341 /**
342 * Set the owner of the uninterpreted sort.
343 */
344 static void setUninterpretedSortOwner(TheoryId theory) {
345 s_uninterpretedSortOwner = theory;
346 }
347
348 /**
349 * Get the owner of the uninterpreted sort.
350 */
351 static TheoryId getUninterpretedSortOwner() {
352 return s_uninterpretedSortOwner;
353 }
354
355 /**
356 * Checks if the node is a leaf node of this theory
357 */
358 inline bool isLeaf(TNode node) const {
359 return node.getNumChildren() == 0 || theoryOf(node) != d_id;
360 }
361
362 /**
363 * Checks if the node is a leaf node of a theory.
364 */
365 inline static bool isLeafOf(TNode node, TheoryId theoryId) {
366 return node.getNumChildren() == 0 || theoryOf(node) != theoryId;
367 }
368
369 /**
370 * Returns true if the assertFact queue is empty
371 */
372 bool done() const throw() {
373 return d_factsHead == d_facts.size();
374 }
375
376 /**
377 * Destructs a Theory.
378 */
379 virtual ~Theory();
380
381 /**
382 * Subclasses of Theory may add additional efforts. DO NOT CHECK
383 * equality with one of these values (e.g. if STANDARD xxx) but
384 * rather use range checks (or use the helper functions below).
385 * Normally we call QUICK_CHECK or STANDARD; at the leaves we call
386 * with FULL_EFFORT.
387 */
388 enum Effort {
389 /**
390 * Standard effort where theory need not do anything
391 */
392 EFFORT_STANDARD = 50,
393 /**
394 * Full effort requires the theory make sure its assertions are satisfiable or not
395 */
396 EFFORT_FULL = 100,
397 /**
398 * Combination effort means that the individual theories are already satisfied, and
399 * it is time to put some effort into propagation of shared term equalities
400 */
401 EFFORT_COMBINATION = 150,
402 /**
403 * Last call effort, reserved for quantifiers.
404 */
405 EFFORT_LAST_CALL = 200
406 };/* enum Effort */
407
408 static inline bool standardEffortOrMore(Effort e) CVC4_CONST_FUNCTION
409 { return e >= EFFORT_STANDARD; }
410 static inline bool standardEffortOnly(Effort e) CVC4_CONST_FUNCTION
411 { return e >= EFFORT_STANDARD && e < EFFORT_FULL; }
412 static inline bool fullEffort(Effort e) CVC4_CONST_FUNCTION
413 { return e == EFFORT_FULL; }
414 static inline bool combination(Effort e) CVC4_CONST_FUNCTION
415 { return e == EFFORT_COMBINATION; }
416
417 /**
418 * Get the id for this Theory.
419 */
420 TheoryId getId() const {
421 return d_id;
422 }
423
424 /**
425 * Returns a string that uniquely identifies this theory solver w.r.t. the
426 * SmtEngine.
427 */
428 std::string getFullInstanceName() const;
429
430
431 /**
432 * Get the SAT context associated to this Theory.
433 */
434 context::Context* getSatContext() const {
435 return d_satContext;
436 }
437
438 /**
439 * Get the context associated to this Theory.
440 */
441 context::UserContext* getUserContext() const {
442 return d_userContext;
443 }
444
445 /**
446 * Set the output channel associated to this theory.
447 */
448 void setOutputChannel(OutputChannel& out) {
449 d_out = &out;
450 }
451
452 /**
453 * Get the output channel associated to this theory.
454 */
455 OutputChannel& getOutputChannel() {
456 return *d_out;
457 }
458
459 /**
460 * Get the valuation associated to this theory.
461 */
462 Valuation& getValuation() {
463 return d_valuation;
464 }
465
466 /**
467 * Get the quantifiers engine associated to this theory.
468 */
469 QuantifiersEngine* getQuantifiersEngine() {
470 return d_quantEngine;
471 }
472
473 /**
474 * Get the quantifiers engine associated to this theory (const version).
475 */
476 const QuantifiersEngine* getQuantifiersEngine() const {
477 return d_quantEngine;
478 }
479
480 /**
481 * Finish theory initialization. At this point, options and the logic
482 * setting are final, and the master equality engine and quantifiers
483 * engine (if any) are initialized. This base class implementation
484 * does nothing.
485 */
486 virtual void finishInit() { }
487
488 /**
489 * Some theories have kinds that are effectively definitions and
490 * should be expanded before they are handled. Definitions allow
491 * a much wider range of actions than the normal forms given by the
492 * rewriter; they can enable other theories and create new terms.
493 * However no assumptions can be made about subterms having been
494 * expanded or rewritten. Where possible rewrite rules should be
495 * used, definitions should only be used when rewrites are not
496 * possible, for example in handling under-specified operations
497 * using partially defined functions.
498 */
499 virtual Node expandDefinition(LogicRequest &logicRequest, Node node) {
500 // by default, do nothing
501 return node;
502 }
503
504 /**
505 * Pre-register a term. Done one time for a Node per SAT context level.
506 */
507 virtual void preRegisterTerm(TNode) { }
508
509 /**
510 * Assert a fact in the current context.
511 */
512 void assertFact(TNode assertion, bool isPreregistered) {
513 Trace("theory") << "Theory<" << getId() << ">::assertFact[" << d_satContext->getLevel() << "](" << assertion << ", " << (isPreregistered ? "true" : "false") << ")" << std::endl;
514 d_facts.push_back(Assertion(assertion, isPreregistered));
515 }
516
517 /**
518 * This method is called to notify a theory that the node n should
519 * be considered a "shared term" by this theory
520 */
521 virtual void addSharedTerm(TNode n) { }
522
523 /**
524 * Called to set the master equality engine.
525 */
526 virtual void setMasterEqualityEngine(eq::EqualityEngine* eq) { }
527
528 /**
529 * Called to set the quantifiers engine.
530 */
531 virtual void setQuantifiersEngine(QuantifiersEngine* qe) {
532 d_quantEngine = qe;
533 }
534
535 /**
536 * Return the current theory care graph. Theories should overload computeCareGraph to do
537 * the actual computation, and use addCarePair to add pairs to the care graph.
538 */
539 void getCareGraph(CareGraph& careGraph) {
540 Trace("sharing") << "Theory<" << getId() << ">::getCareGraph()" << std::endl;
541 TimerStat::CodeTimer computeCareGraphTime(d_computeCareGraphTime);
542 d_careGraph = &careGraph;
543 computeCareGraph();
544 d_careGraph = NULL;
545 }
546
547 /**
548 * Return the status of two terms in the current context. Should be implemented in
549 * sub-theories to enable more efficient theory-combination.
550 */
551 virtual EqualityStatus getEqualityStatus(TNode a, TNode b) { return EQUALITY_UNKNOWN; }
552
553 /**
554 * Return the model value of the give shared term (or null if not available).
555 */
556 virtual Node getModelValue(TNode var) { return Node::null(); }
557
558 /**
559 * Check the current assignment's consistency.
560 *
561 * An implementation of check() is required to either:
562 * - return a conflict on the output channel,
563 * - be interrupted,
564 * - throw an exception
565 * - or call get() until done() is true.
566 */
567 virtual void check(Effort level = EFFORT_FULL) { }
568
569 /**
570 * T-propagate new literal assignments in the current context.
571 */
572 virtual void propagate(Effort level = EFFORT_FULL) { }
573
574 /**
575 * Return an explanation for the literal represented by parameter n
576 * (which was previously propagated by this theory).
577 */
578 virtual Node explain(TNode n) {
579 Unimplemented("Theory %s propagated a node but doesn't implement the "
580 "Theory::explain() interface!", identify().c_str());
581 }
582
583 /**
584 * Get all relevant information in this theory regarding the current
585 * model. This should be called after a call to check( FULL_EFFORT )
586 * for all theories with no conflicts and no lemmas added.
587 * If fullModel is true, then we must specify sufficient information for
588 * the model class to construct constant representatives for each equivalence
589 * class.
590 */
591 virtual void collectModelInfo( TheoryModel* m, bool fullModel ){ }
592
593 /**
594 * Return a decision request, if the theory has one, or the NULL node
595 * otherwise.
596 */
597 virtual Node getNextDecisionRequest() { return Node(); }
598
599 /**
600 * Statically learn from assertion "in," which has been asserted
601 * true at the top level. The theory should only add (via
602 * ::operator<< or ::append()) to the "learned" builder---it should
603 * *never* clear it. It is a conjunction to add to the formula at
604 * the top-level and may contain other theories' contributions.
605 */
606 virtual void ppStaticLearn(TNode in, NodeBuilder<>& learned) { }
607
608 enum PPAssertStatus {
609 /** Atom has been solved */
610 PP_ASSERT_STATUS_SOLVED,
611 /** Atom has not been solved */
612 PP_ASSERT_STATUS_UNSOLVED,
613 /** Atom is inconsistent */
614 PP_ASSERT_STATUS_CONFLICT
615 };
616
617 /**
618 * Given a literal, add the solved substitutions to the map, if any.
619 * The method should return true if the literal can be safely removed.
620 */
621 virtual PPAssertStatus ppAssert(TNode in, SubstitutionMap& outSubstitutions);
622
623 /**
624 * Given an atom of the theory coming from the input formula, this
625 * method can be overridden in a theory implementation to rewrite
626 * the atom into an equivalent form. This is only called just
627 * before an input atom to the engine.
628 */
629 virtual Node ppRewrite(TNode atom) { return atom; }
630
631 /**
632 * Don't preprocess subterm of this term
633 */
634 virtual bool ppDontRewriteSubterm(TNode atom) { return false; }
635
636 /**
637 * A Theory is called with presolve exactly one time per user
638 * check-sat. presolve() is called after preregistration,
639 * rewriting, and Boolean propagation, (other theories'
640 * propagation?), but the notified Theory has not yet had its
641 * check() or propagate() method called. A Theory may empty its
642 * assertFact() queue using get(). A Theory can raise conflicts,
643 * add lemmas, and propagate literals during presolve().
644 *
645 * NOTE: The presolve property must be added to the kinds file for
646 * the theory.
647 */
648 virtual void presolve() { }
649
650 /**
651 * A Theory is called with postsolve exactly one time per user
652 * check-sat. postsolve() is called after the query has completed
653 * (regardless of whether sat, unsat, or unknown), and after any
654 * model-querying related to the query has been performed.
655 * After this call, the theory will not get another check() or
656 * propagate() call until presolve() is called again. A Theory
657 * cannot raise conflicts, add lemmas, or propagate literals during
658 * postsolve().
659 */
660 virtual void postsolve() { }
661
662 /**
663 * Notification sent to the theory wheneven the search restarts.
664 * Serves as a good time to do some clean-up work, and you can
665 * assume you're at DL 0 for the purposes of Contexts. This function
666 * should not use the output channel.
667 */
668 virtual void notifyRestart() { }
669
670 /**
671 * Identify this theory (for debugging, dynamic configuration,
672 * etc..)
673 */
674 virtual std::string identify() const = 0;
675
676 /** Set user attribute
677 * This function is called when an attribute is set by a user. In SMT-LIBv2 this is done
678 * via the syntax (! n :attr)
679 */
680 virtual void setUserAttribute(const std::string& attr, Node n, std::vector<Node> node_values, std::string str_value) {
681 Unimplemented("Theory %s doesn't support Theory::setUserAttribute interface",
682 identify().c_str());
683 }
684
685 /** A set of theories */
686 typedef uint32_t Set;
687
688 /** A set of all theories */
689 static const Set AllTheories = (1 << theory::THEORY_LAST) - 1;
690
691 /** Pops a first theory off the set */
692 static inline TheoryId setPop(Set& set) {
693 uint32_t i = ffs(set); // Find First Set (bit)
694 if (i == 0) { return THEORY_LAST; }
695 TheoryId id = (TheoryId)(i-1);
696 set = setRemove(id, set);
697 return id;
698 }
699
700 /** Returns the size of a set of theories */
701 static inline size_t setSize(Set set) {
702 size_t count = 0;
703 while (setPop(set) != THEORY_LAST) {
704 ++ count;
705 }
706 return count;
707 }
708
709 /** Returns the index size of a set of theories */
710 static inline size_t setIndex(TheoryId id, Set set) {
711 Assert (setContains(id, set));
712 size_t count = 0;
713 while (setPop(set) != id) {
714 ++ count;
715 }
716 return count;
717 }
718
719 /** Add the theory to the set. If no set specified, just returns a singleton set */
720 static inline Set setInsert(TheoryId theory, Set set = 0) {
721 return set | (1 << theory);
722 }
723
724 /** Add the theory to the set. If no set specified, just returns a singleton set */
725 static inline Set setRemove(TheoryId theory, Set set = 0) {
726 return setDifference(set, setInsert(theory));
727 }
728
729 /** Check if the set contains the theory */
730 static inline bool setContains(TheoryId theory, Set set) {
731 return set & (1 << theory);
732 }
733
734 static inline Set setComplement(Set a) {
735 return (~a) & AllTheories;
736 }
737
738 static inline Set setIntersection(Set a, Set b) {
739 return a & b;
740 }
741
742 static inline Set setUnion(Set a, Set b) {
743 return a | b;
744 }
745
746 /** a - b */
747 static inline Set setDifference(Set a, Set b) {
748 return (~b) & a;
749 }
750
751 static inline std::string setToString(theory::Theory::Set theorySet) {
752 std::stringstream ss;
753 ss << "[";
754 for(unsigned theoryId = 0; theoryId < theory::THEORY_LAST; ++theoryId) {
755 if (theory::Theory::setContains((theory::TheoryId)theoryId, theorySet)) {
756 ss << (theory::TheoryId) theoryId << " ";
757 }
758 }
759 ss << "]";
760 return ss.str();
761 }
762
763 typedef context::CDList<Assertion>::const_iterator assertions_iterator;
764
765 /**
766 * Provides access to the facts queue, primarily intended for theory
767 * debugging purposes.
768 *
769 * @return the iterator to the beginning of the fact queue
770 */
771 assertions_iterator facts_begin() const {
772 return d_facts.begin();
773 }
774
775 /**
776 * Provides access to the facts queue, primarily intended for theory
777 * debugging purposes.
778 *
779 * @return the iterator to the end of the fact queue
780 */
781 assertions_iterator facts_end() const {
782 return d_facts.end();
783 }
784 /**
785 * Whether facts have been asserted to this theory.
786 *
787 * @return true iff facts have been asserted to this theory.
788 */
789 bool hasFacts() {
790 return !d_facts.empty();
791 }
792
793 typedef context::CDList<TNode>::const_iterator shared_terms_iterator;
794
795 /**
796 * Provides access to the shared terms, primarily intended for theory
797 * debugging purposes.
798 *
799 * @return the iterator to the beginning of the shared terms list
800 */
801 shared_terms_iterator shared_terms_begin() const {
802 return d_sharedTerms.begin();
803 }
804
805 /**
806 * Provides access to the facts queue, primarily intended for theory
807 * debugging purposes.
808 *
809 * @return the iterator to the end of the shared terms list
810 */
811 shared_terms_iterator shared_terms_end() const {
812 return d_sharedTerms.end();
813 }
814
815
816 /**
817 * This is a utility function for constructing a copy of the currently shared terms
818 * in a queriable form. As this is
819 */
820 std::hash_set<TNode, TNodeHashFunction> currentlySharedTerms() const;
821
822 /**
823 * This allows the theory to be queried for whether a literal, lit, is
824 * entailed by the theory. This returns a pair of a Boolean and a node E.
825 *
826 * If the Boolean is true, then E is a formula that entails lit and E is propositionally
827 * entailed by the assertions to the theory.
828 *
829 * If the Boolean is false, it is "unknown" if lit is entailed and E may be
830 * any node.
831 *
832 * The literal lit is either an atom a or (not a), which must belong to the theory:
833 * There is some TheoryOfMode m s.t. Theory::theoryOf(m, a) == this->getId().
834 *
835 * There are NO assumptions that a or the subterms of a have been
836 * preprocessed in any form. This includes ppRewrite, rewriting,
837 * preregistering, registering, definition expansion or ITE removal!
838 *
839 * Theories are free to limit the amount of effort they use and so may
840 * always opt to return "unknown". Both "unknown" and "not entailed",
841 * may return for E a non-boolean Node (e.g. Node::null()). (There is no explicit output
842 * for the negation of lit is entailed.)
843 *
844 * If lit is theory valid, the return result may be the Boolean constant
845 * true for E.
846 *
847 * If lit is entailed by multiple assertions on the theory's getFact()
848 * queue, a_1, a_2, ... and a_k, this may return E=(and a_1 a_2 ... a_k) or
849 * another theory entailed explanation E=(and (and a_1 a_2) (and a3 a_4) ... a_k)
850 *
851 * If lit is entailed by a single assertion on the theory's getFact()
852 * queue, say a, this may return E=a.
853 *
854 * The theory may always return false!
855 *
856 * The search is controlled by the parameter params. For default behavior,
857 * this may be left NULL.
858 *
859 * Theories that want parameters extend the virtual EntailmentCheckParameters
860 * class. Users ask the theory for an appropriate subclass from the theory
861 * and configure that. How this is implemented is on a per theory basis.
862 *
863 * The search may provide additional output to guide the user of
864 * this function. This output is stored in a EntailmentCheckSideEffects*
865 * output parameter. The implementation of this is theory specific. For
866 * no output, this is NULL.
867 *
868 * Theories may not touch their output stream during an entailment check.
869 *
870 * @param lit a literal belonging to the theory.
871 * @param params the control parameters for the entailment check.
872 * @param out a theory specific output object of the entailment search.
873 * @return a pair <b,E> s.t. if b is true, then a formula E such that
874 * E |= lit in the theory.
875 */
876 virtual std::pair<bool, Node> entailmentCheck(TNode lit, const EntailmentCheckParameters* params = NULL, EntailmentCheckSideEffects* out = NULL);
877
878 /**
879 * Turn on proof-production mode.
880 */
881 void produceProofs() { d_proofsEnabled = true; }
882
883 };/* class Theory */
884
885 std::ostream& operator<<(std::ostream& os, theory::Theory::Effort level);
886 inline std::ostream& operator<<(std::ostream& out, const theory::Assertion& a);
887
888 inline theory::Assertion Theory::get() {
889 Assert( !done(), "Theory::get() called with assertion queue empty!" );
890
891 // Get the assertion
892 Assertion fact = d_facts[d_factsHead];
893 d_factsHead = d_factsHead + 1;
894
895 Trace("theory") << "Theory::get() => " << fact << " (" << d_facts.size() - d_factsHead << " left)" << std::endl;
896
897 if(Dump.isOn("state")) {
898 Dump("state") << AssertCommand(fact.assertion.toExpr());
899 }
900
901 return fact;
902 }
903
904 inline std::ostream& operator<<(std::ostream& out, const theory::Assertion& a) {
905 return out << a.assertion;
906 }
907
908 inline std::ostream& operator<<(std::ostream& out,
909 const CVC4::theory::Theory& theory) {
910 return out << theory.identify();
911 }
912
913 inline std::ostream& operator << (std::ostream& out, theory::Theory::PPAssertStatus status) {
914 switch (status) {
915 case theory::Theory::PP_ASSERT_STATUS_SOLVED:
916 out << "SOLVE_STATUS_SOLVED"; break;
917 case theory::Theory::PP_ASSERT_STATUS_UNSOLVED:
918 out << "SOLVE_STATUS_UNSOLVED"; break;
919 case theory::Theory::PP_ASSERT_STATUS_CONFLICT:
920 out << "SOLVE_STATUS_CONFLICT"; break;
921 default:
922 Unhandled();
923 }
924 return out;
925 }
926
927 class EntailmentCheckParameters {
928 private:
929 TheoryId d_tid;
930 protected:
931 EntailmentCheckParameters(TheoryId tid);
932 public:
933 TheoryId getTheoryId() const;
934 virtual ~EntailmentCheckParameters();
935 };/* class EntailmentCheckParameters */
936
937 class EntailmentCheckSideEffects {
938 private:
939 TheoryId d_tid;
940 protected:
941 EntailmentCheckSideEffects(TheoryId tid);
942 public:
943 TheoryId getTheoryId() const;
944 virtual ~EntailmentCheckSideEffects();
945 };/* class EntailmentCheckSideEffects */
946
947 }/* CVC4::theory namespace */
948 }/* CVC4 namespace */
949
950 #endif /* __CVC4__THEORY__THEORY_H */