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