Unify CVC4_CHECK/CVC4_DCHECK/AlwaysAssert/Assert. (#3366)
[cvc5.git] / src / theory / output_channel.h
1 /********************* */
2 /*! \file output_channel.h
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Morgan Deters, Tim King, Liana Hadarean
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief The theory output channel interface
13 **
14 ** The theory output channel interface.
15 **/
16
17 #include "cvc4_private.h"
18
19 #ifndef CVC4__THEORY__OUTPUT_CHANNEL_H
20 #define CVC4__THEORY__OUTPUT_CHANNEL_H
21
22 #include <memory>
23
24 #include "proof/proof_manager.h"
25 #include "smt/logic_exception.h"
26 #include "theory/interrupted.h"
27 #include "util/proof.h"
28 #include "util/resource_manager.h"
29
30 namespace CVC4 {
31 namespace theory {
32
33 class Theory;
34
35 /**
36 * A LemmaStatus, returned from OutputChannel::lemma(), provides information
37 * about the lemma added. In particular, it contains the T-rewritten lemma
38 * for inspection and the user-level at which the lemma will reside.
39 */
40 class LemmaStatus {
41 public:
42 LemmaStatus(TNode rewrittenLemma, unsigned level)
43 : d_rewrittenLemma(rewrittenLemma), d_level(level) {}
44
45 /** Get the T-rewritten form of the lemma. */
46 TNode getRewrittenLemma() const { return d_rewrittenLemma; }
47 /**
48 * Get the user-level at which the lemma resides. After this user level
49 * is popped, the lemma is un-asserted from the SAT layer. This level
50 * will be 0 if the lemma didn't reach the SAT layer at all.
51 */
52 unsigned getLevel() const { return d_level; }
53 private:
54 Node d_rewrittenLemma;
55 unsigned d_level;
56 }; /* class LemmaStatus */
57
58 /**
59 * Generic "theory output channel" interface.
60 *
61 * All methods can throw unrecoverable CVC4::Exception's unless otherwise
62 * documented.
63 */
64 class OutputChannel {
65 public:
66 /** Construct an OutputChannel. */
67 OutputChannel() {}
68
69 /**
70 * Destructs an OutputChannel. This implementation does nothing,
71 * but we need a virtual destructor for safety in case subclasses
72 * have a destructor.
73 */
74 virtual ~OutputChannel() {}
75
76 OutputChannel(const OutputChannel&) = delete;
77 OutputChannel& operator=(const OutputChannel&) = delete;
78
79 /**
80 * With safePoint(), the theory signals that it is at a safe point
81 * and can be interrupted.
82 *
83 * @throws Interrupted if the theory can be safely interrupted.
84 */
85 virtual void safePoint(uint64_t amount) {}
86
87 /**
88 * Indicate a theory conflict has arisen.
89 *
90 * @param n - a conflict at the current decision level. This should
91 * be an AND-kinded node of literals that are TRUE in the current
92 * assignment and are in conflict (i.e., at least one must be
93 * assigned false), or else a literal by itself (in the case of a
94 * unit conflict) which is assigned TRUE (and T-conflicting) in the
95 * current assignment.
96 * @param pf - a proof of the conflict. This is only non-null if proofs
97 * are enabled.
98 */
99 virtual void conflict(TNode n, std::unique_ptr<Proof> pf = nullptr) = 0;
100
101 /**
102 * Propagate a theory literal.
103 *
104 * @param n - a theory consequence at the current decision level
105 * @return false if an immediate conflict was encountered
106 */
107 virtual bool propagate(TNode n) = 0;
108
109 /**
110 * Tell the core that a valid theory lemma at decision level 0 has
111 * been detected. (This requests a split.)
112 *
113 * @param n - a theory lemma valid at decision level 0
114 * @param rule - the proof rule for this lemma
115 * @param removable - whether the lemma can be removed at any point
116 * @param preprocess - whether to apply more aggressive preprocessing
117 * @param sendAtoms - whether to ensure atoms are sent to the theory
118 * @return the "status" of the lemma, including user level at which
119 * the lemma resides; the lemma will be removed when this user level pops
120 */
121 virtual LemmaStatus lemma(TNode n, ProofRule rule, bool removable = false,
122 bool preprocess = false,
123 bool sendAtoms = false) = 0;
124
125 /**
126 * Variant of the lemma function that does not require providing a proof rule.
127 */
128 virtual LemmaStatus lemma(TNode n, bool removable = false,
129 bool preprocess = false, bool sendAtoms = false) {
130 return lemma(n, RULE_INVALID, removable, preprocess, sendAtoms);
131 }
132
133 /**
134 * Request a split on a new theory atom. This is equivalent to
135 * calling lemma({OR n (NOT n)}).
136 *
137 * @param n - a theory atom; must be of Boolean type
138 */
139 LemmaStatus split(TNode n) { return splitLemma(n.orNode(n.notNode())); }
140
141 virtual LemmaStatus splitLemma(TNode n, bool removable = false) = 0;
142
143 /**
144 * If a decision is made on n, it must be in the phase specified.
145 * Note that this is enforced *globally*, i.e., it is completely
146 * context-INdependent. If you ever requirePhase() on a literal,
147 * it is phase-locked forever and ever. If it is to ever have the
148 * other phase as its assignment, it will be because it has been
149 * propagated that way (or it's a unit, at decision level 0).
150 *
151 * @param n - a theory atom with a SAT literal assigned; must have
152 * been pre-registered
153 * @param phase - the phase to decide on n
154 */
155 virtual void requirePhase(TNode n, bool phase) = 0;
156
157 /**
158 * Notification from a theory that it realizes it is incomplete at
159 * this context level. If SAT is later determined by the
160 * TheoryEngine, it should actually return an UNKNOWN result.
161 */
162 virtual void setIncomplete() = 0;
163
164 /**
165 * "Spend" a "resource." The meaning is specific to the context in
166 * which the theory is operating, and may even be ignored. The
167 * intended meaning is that if the user has set a limit on the "units
168 * of resource" that can be expended in a search, and that limit is
169 * exceeded, then the search is terminated. Note that the check for
170 * termination occurs in the main search loop, so while theories
171 * should call OutputChannel::spendResource() during particularly
172 * long-running operations, they cannot rely on resource() to break
173 * out of infinite or intractable computations.
174 */
175 virtual void spendResource(unsigned amount) {}
176
177 /**
178 * Handle user attribute.
179 * Associates theory t with the attribute attr. Theory t will be
180 * notified whenever an attribute of name attr is set on a node.
181 * This can happen through, for example, the SMT-LIBv2 language.
182 */
183 virtual void handleUserAttribute(const char* attr, Theory* t) {}
184
185 /** Demands that the search restart from sat search level 0.
186 * Using this leads to non-termination issues.
187 * It is appropriate for prototyping for theories.
188 */
189 virtual void demandRestart() {}
190
191 }; /* class OutputChannel */
192
193 } // namespace theory
194 } // namespace CVC4
195
196 #endif /* CVC4__THEORY__OUTPUT_CHANNEL_H */