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