Simplify interface for computing relevant terms. (#4966)
[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-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
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/cdhashset.h"
29 #include "context/cdlist.h"
30 #include "context/cdo.h"
31 #include "context/context.h"
32 #include "expr/node.h"
33 #include "options/options.h"
34 #include "options/theory_options.h"
35 #include "smt/command.h"
36 #include "smt/dump.h"
37 #include "smt/logic_request.h"
38 #include "theory/assertion.h"
39 #include "theory/care_graph.h"
40 #include "theory/decision_manager.h"
41 #include "theory/ee_setup_info.h"
42 #include "theory/logic_info.h"
43 #include "theory/output_channel.h"
44 #include "theory/theory_id.h"
45 #include "theory/theory_inference_manager.h"
46 #include "theory/theory_rewriter.h"
47 #include "theory/theory_state.h"
48 #include "theory/trust_node.h"
49 #include "theory/valuation.h"
50 #include "util/statistics_registry.h"
51
52 namespace CVC4 {
53
54 class TheoryEngine;
55 class ProofNodeManager;
56
57 namespace theory {
58
59 class QuantifiersEngine;
60 class TheoryModel;
61 class SubstitutionMap;
62 class TheoryRewriter;
63
64 namespace rrinst {
65 class CandidateGenerator;
66 }/* CVC4::theory::rrinst namespace */
67
68 namespace eq {
69 class EqualityEngine;
70 }/* CVC4::theory::eq namespace */
71
72 /**
73 * Base class for T-solvers. Abstract DPLL(T).
74 *
75 * This is essentially an interface class. The TheoryEngine has
76 * pointers to Theory. Note that only one specific Theory type (e.g.,
77 * TheoryUF) can exist per NodeManager, because of how the
78 * RegisteredAttr works. (If you need multiple instances of the same
79 * theory, you'll have to write a multiplexed theory that dispatches
80 * all calls to them.)
81 *
82 * NOTE: A Theory has a special way of being initialized. The owner of a Theory
83 * is either:
84 *
85 * (A) Using Theory as a standalone object, not associated with a TheoryEngine.
86 * In this case, simply call the public initialization method
87 * (Theory::finishInitStandalone).
88 *
89 * (B) TheoryEngine, which determines how the Theory acts in accordance with
90 * its theory combination policy. We require the following steps in order:
91 * (B.1) Get information about whether the theory wishes to use an equality
92 * eninge, and more specifically which equality engine notifications the Theory
93 * would like to be notified of (Theory::needsEqualityEngine).
94 * (B.2) Set the equality engine of the theory (Theory::setEqualityEngine),
95 * which we refer to as the "official equality engine" of this Theory. The
96 * equality engine passed to the theory must respect the contract(s) specified
97 * by the equality engine setup information (EeSetupInfo) returned in the
98 * previous step.
99 * (B.3) Set the other required utilities including setQuantifiersEngine and
100 * setDecisionManager.
101 * (B.4) Call the private initialization method (Theory::finishInit).
102 *
103 * Initialization of the second form happens during TheoryEngine::finishInit,
104 * after the quantifiers engine and model objects have been set up.
105 */
106 class Theory {
107 friend class ::CVC4::TheoryEngine;
108
109 private:
110 // Disallow default construction, copy, assignment.
111 Theory() = delete;
112 Theory(const Theory&) = delete;
113 Theory& operator=(const Theory&) = delete;
114
115 /** An integer identifying the type of the theory. */
116 TheoryId d_id;
117
118 /** The SAT search context for the Theory. */
119 context::Context* d_satContext;
120
121 /** The user level assertion context for the Theory. */
122 context::UserContext* d_userContext;
123
124 /** Information about the logic we're operating within. */
125 const LogicInfo& d_logicInfo;
126
127 /** Pointer to proof node manager */
128 ProofNodeManager* d_pnm;
129
130 /**
131 * The assertFact() queue.
132 *
133 * These can not be TNodes as some atoms (such as equalities) are sent
134 * across theories without being stored in a global map.
135 */
136 context::CDList<Assertion> d_facts;
137
138 /** Index into the head of the facts list */
139 context::CDO<unsigned> d_factsHead;
140
141 /** Indices for splitting on the shared terms. */
142 context::CDO<unsigned> d_sharedTermsIndex;
143
144 /** The care graph the theory will use during combination. */
145 CareGraph* d_careGraph;
146
147 /**
148 * Pointer to the quantifiers engine (or NULL, if quantifiers are not
149 * supported or not enabled). Not owned by the theory.
150 */
151 QuantifiersEngine* d_quantEngine;
152
153 /** Pointer to the decision manager. */
154 DecisionManager* d_decManager;
155
156 protected:
157 /** Name of this theory instance. Along with the TheoryId this should provide
158 * an unique string identifier for each instance of a Theory class. We need
159 * this to ensure unique statistics names over multiple theory instances. */
160 std::string d_instanceName;
161
162 // === STATISTICS ===
163 /** time spent in check calls */
164 TimerStat d_checkTime;
165 /** time spent in theory combination */
166 TimerStat d_computeCareGraphTime;
167
168 /**
169 * The only method to add suff to the care graph.
170 */
171 void addCarePair(TNode t1, TNode t2);
172
173 /**
174 * The function should compute the care graph over the shared terms.
175 * The default function returns all the pairs among the shared variables.
176 */
177 virtual void computeCareGraph();
178
179 /**
180 * A list of shared terms that the theory has.
181 */
182 context::CDList<TNode> d_sharedTerms;
183
184 //---------------------------------- private collect model info
185 /**
186 * Helper function for computeRelevantTerms
187 */
188 void collectTerms(TNode n,
189 const std::set<Kind>& irrKinds,
190 std::set<Node>& termSet) const;
191 //---------------------------------- end private collect model info
192
193 /**
194 * Construct a Theory.
195 *
196 * The pair <id, instance> is assumed to uniquely identify this Theory
197 * w.r.t. the SmtEngine.
198 */
199 Theory(TheoryId id,
200 context::Context* satContext,
201 context::UserContext* userContext,
202 OutputChannel& out,
203 Valuation valuation,
204 const LogicInfo& logicInfo,
205 ProofNodeManager* pnm,
206 std::string instance = ""); // taking : No default.
207
208 /**
209 * This is called at shutdown time by the TheoryEngine, just before
210 * destruction. It is important because there are destruction
211 * ordering issues between PropEngine and Theory (based on what
212 * hard-links to Nodes are outstanding). As the fact queue might be
213 * nonempty, we ensure here that it's clear. If you overload this,
214 * you must make an explicit call here to this->Theory::shutdown()
215 * too.
216 */
217 virtual void shutdown() { }
218
219 /**
220 * The output channel for the Theory.
221 */
222 OutputChannel* d_out;
223
224 /**
225 * The valuation proxy for the Theory to communicate back with the
226 * theory engine (and other theories).
227 */
228 Valuation d_valuation;
229 /**
230 * Pointer to the official equality engine of this theory, which is owned by
231 * the equality engine manager of TheoryEngine.
232 */
233 eq::EqualityEngine* d_equalityEngine;
234 /**
235 * The official equality engine, if we allocated it.
236 */
237 std::unique_ptr<eq::EqualityEngine> d_allocEqualityEngine;
238 /**
239 * The theory state, which contains contexts, valuation, and equality engine.
240 * Notice the theory is responsible for memory management of this class.
241 */
242 TheoryState* d_theoryState;
243 /**
244 * The theory inference manager. This is a wrapper around the equality
245 * engine and the output channel. It ensures that the output channel and
246 * the equality engine are used properly.
247 */
248 TheoryInferenceManager* d_inferManager;
249 /**
250 * Whether proofs are enabled
251 *
252 */
253 bool d_proofsEnabled;
254
255 /**
256 * Returns the next assertion in the assertFact() queue.
257 *
258 * @return the next assertion in the assertFact() queue
259 */
260 inline Assertion get();
261
262 const LogicInfo& getLogicInfo() const {
263 return d_logicInfo;
264 }
265
266 /**
267 * The theory that owns the uninterpreted sort.
268 */
269 static TheoryId s_uninterpretedSortOwner;
270
271 void printFacts(std::ostream& os) const;
272 void debugPrintFacts() const;
273
274 /** is legal elimination
275 *
276 * Returns true if x -> val is a legal elimination of variable x. This is
277 * useful for ppAssert, when x = val is an entailed equality. This function
278 * determines whether indeed x can be eliminated from the problem via the
279 * substituion x -> val.
280 *
281 * The following criteria imply that x -> val is *not* a legal elimination:
282 * (1) If x is contained in val,
283 * (2) If the type of val is not a subtype of the type of x,
284 * (3) If val contains an operator that cannot be evaluated, and produceModels
285 * is true. For example, x -> sqrt(2) is not a legal elimination if we
286 * are producing models. This is because we care about the value of x, and
287 * its value must be computed (approximated) by the non-linear solver.
288 */
289 bool isLegalElimination(TNode x, TNode val);
290 //--------------------------------- private initialization
291 /**
292 * Called to set the official equality engine. This should be done by
293 * TheoryEngine only.
294 */
295 void setEqualityEngine(eq::EqualityEngine* ee);
296 /** Called to set the quantifiers engine. */
297 void setQuantifiersEngine(QuantifiersEngine* qe);
298 /** Called to set the decision manager. */
299 void setDecisionManager(DecisionManager* dm);
300 /**
301 * Finish theory initialization. At this point, options and the logic
302 * setting are final, the master equality engine and quantifiers
303 * engine (if any) are initialized, and the official equality engine of this
304 * theory has been assigned. This base class implementation
305 * does nothing. This should be called by TheoryEngine only.
306 */
307 virtual void finishInit() {}
308 //--------------------------------- end private initialization
309
310 /**
311 * This method is called to notify a theory that the node n should
312 * be considered a "shared term" by this theory. This does anything
313 * theory-specific concerning the fact that n is now marked as a shared
314 * term, which is done in addition to explicitly storing n as a shared
315 * term and adding it as a trigger term in the equality engine of this
316 * class (see addSharedTerm).
317 */
318 virtual void notifySharedTerm(TNode n);
319
320 public:
321 //--------------------------------- initialization
322 /**
323 * @return The theory rewriter associated with this theory.
324 */
325 virtual TheoryRewriter* getTheoryRewriter() = 0;
326 /**
327 * Returns true if this theory needs an equality engine for checking
328 * satisfiability.
329 *
330 * If this method returns true, then the equality engine manager will
331 * initialize its equality engine field via setEqualityEngine above during
332 * TheoryEngine::finishInit, prior to calling finishInit for this theory.
333 *
334 * Additionally, if this method returns true, then this method is required to
335 * update the argument esi with instructions for initializing and setting up
336 * notifications from its equality engine, which is commonly done with
337 * a notifications class (eq::EqualityEngineNotify).
338 */
339 virtual bool needsEqualityEngine(EeSetupInfo& esi);
340 /**
341 * Finish theory initialization, standalone version. This is used to
342 * initialize this class if it is not associated with a theory engine.
343 * This allocates the official equality engine of this Theory and then
344 * calls the finishInit method above.
345 */
346 void finishInitStandalone();
347 //--------------------------------- end initialization
348
349 /**
350 * Return the ID of the theory responsible for the given type.
351 */
352 static inline TheoryId theoryOf(TypeNode typeNode) {
353 Trace("theory::internal") << "theoryOf(" << typeNode << ")" << std::endl;
354 TheoryId id;
355 if (typeNode.getKind() == kind::TYPE_CONSTANT) {
356 id = typeConstantToTheoryId(typeNode.getConst<TypeConstant>());
357 } else {
358 id = kindToTheoryId(typeNode.getKind());
359 }
360 if (id == THEORY_BUILTIN) {
361 Trace("theory::internal") << "theoryOf(" << typeNode << ") == " << s_uninterpretedSortOwner << std::endl;
362 return s_uninterpretedSortOwner;
363 }
364 return id;
365 }
366
367 /**
368 * Returns the ID of the theory responsible for the given node.
369 */
370 static TheoryId theoryOf(options::TheoryOfMode mode, TNode node);
371
372 /**
373 * Returns the ID of the theory responsible for the given node.
374 */
375 static inline TheoryId theoryOf(TNode node) {
376 return theoryOf(options::theoryOfMode(), node);
377 }
378
379 /**
380 * Set the owner of the uninterpreted sort.
381 */
382 static void setUninterpretedSortOwner(TheoryId theory) {
383 s_uninterpretedSortOwner = theory;
384 }
385
386 /**
387 * Get the owner of the uninterpreted sort.
388 */
389 static TheoryId getUninterpretedSortOwner() {
390 return s_uninterpretedSortOwner;
391 }
392
393 /**
394 * Checks if the node is a leaf node of this theory
395 */
396 inline bool isLeaf(TNode node) const {
397 return node.getNumChildren() == 0 || theoryOf(node) != d_id;
398 }
399
400 /**
401 * Checks if the node is a leaf node of a theory.
402 */
403 inline static bool isLeafOf(TNode node, TheoryId theoryId) {
404 return node.getNumChildren() == 0 || theoryOf(node) != theoryId;
405 }
406
407 /** Returns true if the assertFact queue is empty*/
408 bool done() const { return d_factsHead == d_facts.size(); }
409 /**
410 * Destructs a Theory.
411 */
412 virtual ~Theory();
413
414 /**
415 * Subclasses of Theory may add additional efforts. DO NOT CHECK
416 * equality with one of these values (e.g. if STANDARD xxx) but
417 * rather use range checks (or use the helper functions below).
418 * Normally we call QUICK_CHECK or STANDARD; at the leaves we call
419 * with FULL_EFFORT.
420 */
421 enum Effort
422 {
423 /**
424 * Standard effort where theory need not do anything
425 */
426 EFFORT_STANDARD = 50,
427 /**
428 * Full effort requires the theory make sure its assertions are satisfiable
429 * or not
430 */
431 EFFORT_FULL = 100,
432 /**
433 * Last call effort, called after theory combination has completed with
434 * no lemmas and a model is available.
435 */
436 EFFORT_LAST_CALL = 200
437 }; /* enum Effort */
438
439 static inline bool standardEffortOrMore(Effort e) CVC4_CONST_FUNCTION
440 { return e >= EFFORT_STANDARD; }
441 static inline bool standardEffortOnly(Effort e) CVC4_CONST_FUNCTION
442 { return e >= EFFORT_STANDARD && e < EFFORT_FULL; }
443 static inline bool fullEffort(Effort e) CVC4_CONST_FUNCTION
444 { return e == EFFORT_FULL; }
445
446 /**
447 * Get the id for this Theory.
448 */
449 TheoryId getId() const {
450 return d_id;
451 }
452
453 /**
454 * Get the SAT context associated to this Theory.
455 */
456 context::Context* getSatContext() const {
457 return d_satContext;
458 }
459
460 /**
461 * Get the context associated to this Theory.
462 */
463 context::UserContext* getUserContext() const {
464 return d_userContext;
465 }
466
467 /**
468 * Set the output channel associated to this theory.
469 */
470 void setOutputChannel(OutputChannel& out) {
471 d_out = &out;
472 }
473
474 /**
475 * Get the output channel associated to this theory.
476 */
477 OutputChannel& getOutputChannel() {
478 return *d_out;
479 }
480
481 /**
482 * Get the valuation associated to this theory.
483 */
484 Valuation& getValuation() {
485 return d_valuation;
486 }
487
488 /** Get the equality engine being used by this theory. */
489 eq::EqualityEngine* getEqualityEngine();
490
491 /**
492 * Get the quantifiers engine associated to this theory.
493 */
494 QuantifiersEngine* getQuantifiersEngine() {
495 return d_quantEngine;
496 }
497
498 /** Get the decision manager associated to this theory. */
499 DecisionManager* getDecisionManager() { return d_decManager; }
500
501 /**
502 * Expand definitions in the term node. This returns a term that is
503 * equivalent to node. It wraps this term in a TrustNode of kind
504 * TrustNodeKind::REWRITE. If node is unchanged by this method, the
505 * null TrustNode may be returned. This is an optimization to avoid
506 * constructing the trivial equality (= node node) internally within
507 * TrustNode.
508 *
509 * The purpose of this method is typically to eliminate the operators in node
510 * that are syntax sugar that cannot otherwise be eliminated during rewriting.
511 * For example, division relies on the introduction of an uninterpreted
512 * function for the divide-by-zero case, which we do not introduce with
513 * the rewriter, since this function may be cached in a non-global fashion.
514 *
515 * Some theories have kinds that are effectively definitions and should be
516 * expanded before they are handled. Definitions allow a much wider range of
517 * actions than the normal forms given by the rewriter. However no
518 * assumptions can be made about subterms having been expanded or rewritten.
519 * Where possible rewrite rules should be used, definitions should only be
520 * used when rewrites are not possible, for example in handling
521 * under-specified operations using partially defined functions.
522 *
523 * Some theories like sets use expandDefinition as a "context
524 * independent preRegisterTerm". This is required for cases where
525 * a theory wants to be notified about a term before preprocessing
526 * and simplification but doesn't necessarily want to rewrite it.
527 */
528 virtual TrustNode expandDefinition(Node node)
529 {
530 // by default, do nothing
531 return TrustNode::null();
532 }
533
534 /**
535 * Pre-register a term. Done one time for a Node per SAT context level.
536 */
537 virtual void preRegisterTerm(TNode);
538
539 /**
540 * Assert a fact in the current context.
541 */
542 void assertFact(TNode assertion, bool isPreregistered) {
543 Trace("theory") << "Theory<" << getId() << ">::assertFact["
544 << d_satContext->getLevel() << "](" << assertion << ", "
545 << (isPreregistered ? "true" : "false") << ")" << std::endl;
546 d_facts.push_back(Assertion(assertion, isPreregistered));
547 }
548
549 /** Add shared term to the theory. */
550 void addSharedTerm(TNode node);
551
552 /**
553 * Return the current theory care graph. Theories should overload
554 * computeCareGraph to do the actual computation, and use addCarePair to add
555 * pairs to the care graph.
556 */
557 void getCareGraph(CareGraph* careGraph);
558
559 /**
560 * Return the status of two terms in the current context. Should be
561 * implemented in sub-theories to enable more efficient theory-combination.
562 */
563 virtual EqualityStatus getEqualityStatus(TNode a, TNode b);
564
565 /**
566 * Return the model value of the give shared term (or null if not available).
567 *
568 * TODO (project #39): this method is likely to become deprecated.
569 */
570 virtual Node getModelValue(TNode var) { return Node::null(); }
571
572 /** T-propagate new literal assignments in the current context. */
573 virtual void propagate(Effort level = EFFORT_FULL) {}
574
575 /**
576 * Return an explanation for the literal represented by parameter n
577 * (which was previously propagated by this theory).
578 */
579 virtual TrustNode explain(TNode n)
580 {
581 Unimplemented() << "Theory " << identify()
582 << " propagated a node but doesn't implement the "
583 "Theory::explain() interface!";
584 }
585
586 //--------------------------------- check
587 /**
588 * Does this theory wish to be called to check at last call effort? This is
589 * the case for any theory that wishes to run when a model is available.
590 */
591 virtual bool needsCheckLastEffort() { return false; }
592 /**
593 * Check the current assignment's consistency.
594 *
595 * An implementation of check() is required to either:
596 * - return a conflict on the output channel,
597 * - be interrupted,
598 * - throw an exception
599 * - or call get() until done() is true.
600 *
601 * The standard method for check consists of a loop that processes the entire
602 * fact queue when preCheck returns false. It makes four theory-specific
603 * callbacks, (preCheck, postCheck, preNotifyFact, notifyFact) as described
604 * below. It asserts each fact to the official equality engine when
605 * preNotifyFact returns false.
606 *
607 * Theories that use this check method must use an official theory
608 * state object (d_theoryState).
609 *
610 * TODO (project #39): this method should be non-virtual, once all theories
611 * conform to the new standard
612 */
613 virtual void check(Effort level = EFFORT_FULL);
614 /**
615 * Pre-check, called before the fact queue of the theory is processed.
616 * If this method returns false, then the theory will process its fact
617 * queue. If this method returns true, then the theory has indicated
618 * its check method should finish immediately.
619 */
620 virtual bool preCheck(Effort level = EFFORT_FULL);
621 /**
622 * Post-check, called after the fact queue of the theory is processed.
623 */
624 virtual void postCheck(Effort level = EFFORT_FULL);
625 /**
626 * Pre-notify fact, return true if the theory processed it. If this
627 * method returns false, then the atom will be added to the equality engine
628 * of the theory and notifyFact will be called with isInternal=false.
629 *
630 * Theories that implement check but do not use official equality
631 * engines should always return true for this method.
632 *
633 * @param atom The atom
634 * @param polarity Its polarity
635 * @param fact The original literal that was asserted
636 * @param isPrereg Whether the assertion is preregistered
637 * @param isInternal Whether the origin of the fact was internal. If this
638 * is false, the fact was asserted via the fact queue of the theory.
639 * @return true if the theory completely processed this fact, i.e. it does
640 * not need to assert the fact to its equality engine.
641 */
642 virtual bool preNotifyFact(
643 TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal);
644 /**
645 * Notify fact, called immediately after the fact was pushed into the
646 * equality engine.
647 *
648 * @param atom The atom
649 * @param polarity Its polarity
650 * @param fact The original literal that was asserted.
651 * @param isInternal Whether the origin of the fact was internal. If this
652 * is false, the fact was asserted via the fact queue of the theory.
653 */
654 virtual void notifyFact(TNode atom, bool pol, TNode fact, bool isInternal);
655 //--------------------------------- end check
656
657 //--------------------------------- collect model info
658 /**
659 * Get all relevant information in this theory regarding the current
660 * model. This should be called after a call to check( FULL_EFFORT )
661 * for all theories with no conflicts and no lemmas added.
662 *
663 * This method returns true if and only if the equality engine of m is
664 * consistent as a result of this call.
665 *
666 * The standard method for collectModelInfo computes the relevant terms,
667 * asserts the theory's equality engine to the model (if necessary) and
668 * then calls computeModelValues.
669 *
670 * TODO (project #39): this method should be non-virtual, once all theories
671 * conform to the new standard
672 */
673 virtual bool collectModelInfo(TheoryModel* m);
674 /**
675 * Scans the current set of assertions and shared terms top-down
676 * until a theory-leaf is reached, and adds all terms found to
677 * termSet. This is used by collectModelInfo to delimit the set of
678 * terms that should be used when constructing a model.
679 *
680 * @param irrKinds The kinds of terms that appear in assertions that should *not*
681 * be included in termSet. Note that the kinds EQUAL and NOT are always
682 * treated as irrelevant kinds.
683 *
684 * @param includeShared Whether to include shared terms in termSet. Notice that
685 * shared terms are not influenced by irrKinds.
686 *
687 * TODO (project #39): this method will be deleted. The version in
688 * model manager will be used.
689 */
690 void computeAssertedTerms(std::set<Node>& termSet,
691 const std::set<Kind>& irrKinds,
692 bool includeShared = true) const;
693 /**
694 * Compute terms that are not necessarily part of the assertions or
695 * shared terms that should be considered relevant, add them to termSet.
696 */
697 virtual void computeRelevantTerms(std::set<Node>& termSet);
698 /**
699 * Collect model values, after equality information is added to the model.
700 * The argument termSet is the set of relevant terms returned by
701 * computeRelevantTerms.
702 */
703 virtual bool collectModelValues(TheoryModel* m,
704 const std::set<Node>& termSet);
705 /** if theories want to do something with model after building, do it here */
706 virtual void postProcessModel( TheoryModel* m ){ }
707 //--------------------------------- end collect model info
708
709 //--------------------------------- preprocessing
710 /**
711 * Statically learn from assertion "in," which has been asserted
712 * true at the top level. The theory should only add (via
713 * ::operator<< or ::append()) to the "learned" builder---it should
714 * *never* clear it. It is a conjunction to add to the formula at
715 * the top-level and may contain other theories' contributions.
716 */
717 virtual void ppStaticLearn(TNode in, NodeBuilder<>& learned) { }
718
719 enum PPAssertStatus {
720 /** Atom has been solved */
721 PP_ASSERT_STATUS_SOLVED,
722 /** Atom has not been solved */
723 PP_ASSERT_STATUS_UNSOLVED,
724 /** Atom is inconsistent */
725 PP_ASSERT_STATUS_CONFLICT
726 };
727
728 /**
729 * Given a literal, add the solved substitutions to the map, if any.
730 * The method should return true if the literal can be safely removed.
731 */
732 virtual PPAssertStatus ppAssert(TNode in, SubstitutionMap& outSubstitutions);
733
734 /**
735 * Given an atom of the theory coming from the input formula, this
736 * method can be overridden in a theory implementation to rewrite
737 * the atom into an equivalent form. This is only called just
738 * before an input atom to the engine. This method returns a TrustNode of
739 * kind TrustNodeKind::REWRITE, which carries information about the proof
740 * generator for the rewrite. Similarly to expandDefinition, this method may
741 * return the null TrustNode if atom is unchanged.
742 */
743 virtual TrustNode ppRewrite(TNode atom) { return TrustNode::null(); }
744
745 /**
746 * Notify preprocessed assertions. Called on new assertions after
747 * preprocessing before they are asserted to theory engine.
748 */
749 virtual void ppNotifyAssertions(const std::vector<Node>& assertions) {}
750 //--------------------------------- end preprocessing
751
752 /**
753 * A Theory is called with presolve exactly one time per user
754 * check-sat. presolve() is called after preregistration,
755 * rewriting, and Boolean propagation, (other theories'
756 * propagation?), but the notified Theory has not yet had its
757 * check() or propagate() method called. A Theory may empty its
758 * assertFact() queue using get(). A Theory can raise conflicts,
759 * add lemmas, and propagate literals during presolve().
760 *
761 * NOTE: The presolve property must be added to the kinds file for
762 * the theory.
763 */
764 virtual void presolve() { }
765
766 /**
767 * A Theory is called with postsolve exactly one time per user
768 * check-sat. postsolve() is called after the query has completed
769 * (regardless of whether sat, unsat, or unknown), and after any
770 * model-querying related to the query has been performed.
771 * After this call, the theory will not get another check() or
772 * propagate() call until presolve() is called again. A Theory
773 * cannot raise conflicts, add lemmas, or propagate literals during
774 * postsolve().
775 */
776 virtual void postsolve() { }
777
778 /**
779 * Notification sent to the theory wheneven the search restarts.
780 * Serves as a good time to do some clean-up work, and you can
781 * assume you're at DL 0 for the purposes of Contexts. This function
782 * should not use the output channel.
783 */
784 virtual void notifyRestart() { }
785
786 /**
787 * Identify this theory (for debugging, dynamic configuration,
788 * etc..)
789 */
790 virtual std::string identify() const = 0;
791
792 /** Set user attribute
793 * This function is called when an attribute is set by a user. In SMT-LIBv2 this is done
794 * via the syntax (! n :attr)
795 */
796 virtual void setUserAttribute(const std::string& attr, Node n, std::vector<Node> node_values, std::string str_value) {
797 Unimplemented() << "Theory " << identify()
798 << " doesn't support Theory::setUserAttribute interface";
799 }
800
801 typedef context::CDList<Assertion>::const_iterator assertions_iterator;
802
803 /**
804 * Provides access to the facts queue, primarily intended for theory
805 * debugging purposes.
806 *
807 * @return the iterator to the beginning of the fact queue
808 */
809 assertions_iterator facts_begin() const {
810 return d_facts.begin();
811 }
812
813 /**
814 * Provides access to the facts queue, primarily intended for theory
815 * debugging purposes.
816 *
817 * @return the iterator to the end of the fact queue
818 */
819 assertions_iterator facts_end() const {
820 return d_facts.end();
821 }
822 /**
823 * Whether facts have been asserted to this theory.
824 *
825 * @return true iff facts have been asserted to this theory.
826 */
827 bool hasFacts() {
828 return !d_facts.empty();
829 }
830
831 /** Return total number of facts asserted to this theory */
832 size_t numAssertions() {
833 return d_facts.size();
834 }
835
836 typedef context::CDList<TNode>::const_iterator shared_terms_iterator;
837
838 /**
839 * Provides access to the shared terms, primarily intended for theory
840 * debugging purposes.
841 *
842 * @return the iterator to the beginning of the shared terms list
843 */
844 shared_terms_iterator shared_terms_begin() const {
845 return d_sharedTerms.begin();
846 }
847
848 /**
849 * Provides access to the facts queue, primarily intended for theory
850 * debugging purposes.
851 *
852 * @return the iterator to the end of the shared terms list
853 */
854 shared_terms_iterator shared_terms_end() const {
855 return d_sharedTerms.end();
856 }
857
858
859 /**
860 * This is a utility function for constructing a copy of the currently shared terms
861 * in a queriable form. As this is
862 */
863 std::unordered_set<TNode, TNodeHashFunction> currentlySharedTerms() const;
864
865 /**
866 * This allows the theory to be queried for whether a literal, lit, is
867 * entailed by the theory. This returns a pair of a Boolean and a node E.
868 *
869 * If the Boolean is true, then E is a formula that entails lit and E is propositionally
870 * entailed by the assertions to the theory.
871 *
872 * If the Boolean is false, it is "unknown" if lit is entailed and E may be
873 * any node.
874 *
875 * The literal lit is either an atom a or (not a), which must belong to the theory:
876 * There is some TheoryOfMode m s.t. Theory::theoryOf(m, a) == this->getId().
877 *
878 * There are NO assumptions that a or the subterms of a have been
879 * preprocessed in any form. This includes ppRewrite, rewriting,
880 * preregistering, registering, definition expansion or ITE removal!
881 *
882 * Theories are free to limit the amount of effort they use and so may
883 * always opt to return "unknown". Both "unknown" and "not entailed",
884 * may return for E a non-boolean Node (e.g. Node::null()). (There is no explicit output
885 * for the negation of lit is entailed.)
886 *
887 * If lit is theory valid, the return result may be the Boolean constant
888 * true for E.
889 *
890 * If lit is entailed by multiple assertions on the theory's getFact()
891 * queue, a_1, a_2, ... and a_k, this may return E=(and a_1 a_2 ... a_k) or
892 * another theory entailed explanation E=(and (and a_1 a_2) (and a3 a_4) ... a_k)
893 *
894 * If lit is entailed by a single assertion on the theory's getFact()
895 * queue, say a, this may return E=a.
896 *
897 * The theory may always return false!
898 *
899 * Theories may not touch their output stream during an entailment check.
900 *
901 * @param lit a literal belonging to the theory.
902 * @return a pair <b,E> s.t. if b is true, then a formula E such that
903 * E |= lit in the theory.
904 */
905 virtual std::pair<bool, Node> entailmentCheck(TNode lit);
906
907 /* get current substitution at an effort
908 * input : vars
909 * output : subs, exp
910 * where ( exp[vars[i]] => vars[i] = subs[i] ) holds for all i
911 */
912 virtual bool getCurrentSubstitution(int effort, std::vector<Node>& vars,
913 std::vector<Node>& subs,
914 std::map<Node, std::vector<Node> >& exp) {
915 return false;
916 }
917
918 /* is extended function reduced */
919 virtual bool isExtfReduced( int effort, Node n, Node on, std::vector< Node >& exp ) { return n.isConst(); }
920
921 /**
922 * Get reduction for node
923 * If return value is not 0, then n is reduced.
924 * If return value <0 then n is reduced SAT-context-independently (e.g. by a
925 * lemma that persists at this user-context level).
926 * If nr is non-null, then ( n = nr ) should be added as a lemma by caller,
927 * and return value should be <0.
928 */
929 virtual int getReduction( int effort, Node n, Node& nr ) { return 0; }
930
931 /** Turn on proof-production mode. */
932 void produceProofs() { d_proofsEnabled = true; }
933 };/* class Theory */
934
935 std::ostream& operator<<(std::ostream& os, theory::Theory::Effort level);
936
937
938 inline theory::Assertion Theory::get() {
939 Assert(!done()) << "Theory::get() called with assertion queue empty!";
940
941 // Get the assertion
942 Assertion fact = d_facts[d_factsHead];
943 d_factsHead = d_factsHead + 1;
944
945 Trace("theory") << "Theory::get() => " << fact << " (" << d_facts.size() - d_factsHead << " left)" << std::endl;
946
947 if(Dump.isOn("state")) {
948 Dump("state") << AssertCommand(fact.d_assertion.toExpr());
949 }
950
951 return fact;
952 }
953
954 inline std::ostream& operator<<(std::ostream& out,
955 const CVC4::theory::Theory& theory) {
956 return out << theory.identify();
957 }
958
959 inline std::ostream& operator << (std::ostream& out, theory::Theory::PPAssertStatus status) {
960 switch (status) {
961 case theory::Theory::PP_ASSERT_STATUS_SOLVED:
962 out << "SOLVE_STATUS_SOLVED"; break;
963 case theory::Theory::PP_ASSERT_STATUS_UNSOLVED:
964 out << "SOLVE_STATUS_UNSOLVED"; break;
965 case theory::Theory::PP_ASSERT_STATUS_CONFLICT:
966 out << "SOLVE_STATUS_CONFLICT"; break;
967 default:
968 Unhandled();
969 }
970 return out;
971 }
972
973 }/* CVC4::theory namespace */
974 }/* CVC4 namespace */
975
976 #endif /* CVC4__THEORY__THEORY_H */