Update copyrights.
[cvc5.git] / src / theory / output_channel.h
1 /********************* */
2 /*! \file output_channel.h
3 ** \verbatim
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): Andrew Reynolds, Dejan Jovanovic, Tim King
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** 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 "util/cvc4_assert.h"
23 #include "theory/interrupted.h"
24
25 namespace CVC4 {
26 namespace theory {
27
28 class Theory;
29
30 /**
31 * A LemmaStatus, returned from OutputChannel::lemma(), provides information
32 * about the lemma added. In particular, it contains the T-rewritten lemma
33 * for inspection and the user-level at which the lemma will reside.
34 */
35 class LemmaStatus {
36 Node d_rewrittenLemma;
37 unsigned d_level;
38
39 public:
40 LemmaStatus(TNode rewrittenLemma, unsigned level) :
41 d_rewrittenLemma(rewrittenLemma),
42 d_level(level) {
43 }
44
45 /** Get the T-rewritten form of the lemma. */
46 TNode getRewrittenLemma() const throw() { return d_rewrittenLemma; }
47
48 /**
49 * Get the user-level at which the lemma resides. After this user level
50 * is popped, the lemma is un-asserted from the SAT layer. This level
51 * will be 0 if the lemma didn't reach the SAT layer at all.
52 */
53 unsigned getLevel() const throw() { return d_level; }
54
55 };/* class LemmaStatus */
56
57 /**
58 * Generic "theory output channel" interface.
59 */
60 class OutputChannel {
61 /** Disallow copying: private constructor */
62 OutputChannel(const OutputChannel&) CVC4_UNDEFINED;
63
64 /** Disallow assignment: private operator=() */
65 OutputChannel& operator=(const OutputChannel&) CVC4_UNDEFINED;
66
67 public:
68
69 /**
70 * Construct an OutputChannel.
71 */
72 OutputChannel() {
73 }
74
75 /**
76 * Destructs an OutputChannel. This implementation does nothing,
77 * but we need a virtual destructor for safety in case subclasses
78 * have a destructor.
79 */
80 virtual ~OutputChannel() {
81 }
82
83 /**
84 * With safePoint(), the theory signals that it is at a safe point
85 * and can be interrupted.
86 */
87 virtual void safePoint() throw(Interrupted, AssertionException) {
88 }
89
90 /**
91 * Indicate a theory conflict has arisen.
92 *
93 * @param n - a conflict at the current decision level. This should
94 * be an AND-kinded node of literals that are TRUE in the current
95 * assignment and are in conflict (i.e., at least one must be
96 * assigned false), or else a literal by itself (in the case of a
97 * unit conflict) which is assigned TRUE (and T-conflicting) in the
98 * current assignment.
99 */
100 virtual void conflict(TNode n) throw(AssertionException) = 0;
101
102 /**
103 * Propagate a theory literal.
104 *
105 * @param n - a theory consequence at the current decision level
106 * @return false if an immediate conflict was encountered
107 */
108 virtual bool propagate(TNode n) throw(AssertionException) = 0;
109
110 /**
111 * Tell the core that a valid theory lemma at decision level 0 has
112 * been detected. (This requests a split.)
113 *
114 * @param n - a theory lemma valid at decision level 0
115 * @param removable - whether the lemma can be removed at any point
116 * @param preprocess - whether to apply more aggressive preprocessing
117 * @return the "status" of the lemma, including user level at which
118 * the lemma resides; the lemma will be removed when this user level pops
119 */
120 virtual LemmaStatus lemma(TNode n, bool removable = false,
121 bool preprocess = false)
122 throw(TypeCheckingExceptionPrivate, AssertionException) = 0;
123
124 /**
125 * Request a split on a new theory atom. This is equivalent to
126 * calling lemma({OR n (NOT n)}).
127 *
128 * @param n - a theory atom; must be of Boolean type
129 */
130 LemmaStatus split(TNode n)
131 throw(TypeCheckingExceptionPrivate, AssertionException) {
132 return splitLemma(n.orNode(n.notNode()));
133 }
134
135 virtual LemmaStatus splitLemma(TNode n, bool removable = false)
136 throw(TypeCheckingExceptionPrivate, AssertionException) = 0;
137
138 /**
139 * If a decision is made on n, it must be in the phase specified.
140 * Note that this is enforced *globally*, i.e., it is completely
141 * context-INdependent. If you ever requirePhase() on a literal,
142 * it is phase-locked forever and ever. If it is to ever have the
143 * other phase as its assignment, it will be because it has been
144 * propagated that way (or it's a unit, at decision level 0).
145 *
146 * @param n - a theory atom with a SAT literal assigned; must have
147 * been pre-registered
148 * @param phase - the phase to decide on n
149 */
150 virtual void requirePhase(TNode n, bool phase)
151 throw(Interrupted, TypeCheckingExceptionPrivate, AssertionException) = 0;
152
153 /**
154 * Flips the most recent unflipped decision to the other phase and
155 * returns true. If all decisions have been flipped, the root
156 * decision is re-flipped and flipDecision() returns false. If no
157 * decisions (flipped nor unflipped) are on the decision stack, the
158 * state is not affected and flipDecision() returns false.
159 *
160 * For example, if l1, l2, and l3 are all decision literals, and
161 * have been decided in positive phase, a series of flipDecision()
162 * calls has the following effects:
163 *
164 * l1 l2 l3 <br/>
165 * l1 l2 ~l3 <br/>
166 * l1 ~l2 <br/>
167 * ~l1 <br/>
168 * l1 (and flipDecision() returns false)
169 *
170 * Naturally, flipDecision() might be interleaved with search. For example:
171 *
172 * l1 l2 l3 <br/>
173 * flipDecision() <br/>
174 * l1 l2 ~l3 <br/>
175 * flipDecision() <br/>
176 * l1 ~l2 <br/>
177 * SAT decides l3 <br/>
178 * l1 ~l2 l3 <br/>
179 * flipDecision() <br/>
180 * l1 ~l2 ~l3 <br/>
181 * flipDecision() <br/>
182 * ~l1 <br/>
183 * SAT decides l2 <br/>
184 * ~l1 l2 <br/>
185 * flipDecision() <br/>
186 * ~l1 ~l2 <br/>
187 * flipDecision() returns FALSE<br/>
188 * l1
189 *
190 * @return true if a decision was flipped; false if no decision
191 * could be flipped, or if the root decision was re-flipped
192 */
193 virtual bool flipDecision()
194 throw(Interrupted, TypeCheckingExceptionPrivate, AssertionException) = 0;
195
196 /**
197 * Notification from a theory that it realizes it is incomplete at
198 * this context level. If SAT is later determined by the
199 * TheoryEngine, it should actually return an UNKNOWN result.
200 */
201 virtual void setIncomplete() throw(AssertionException) = 0;
202
203 /**
204 * "Spend" a "resource." The meaning is specific to the context in
205 * which the theory is operating, and may even be ignored. The
206 * intended meaning is that if the user has set a limit on the "units
207 * of resource" that can be expended in a search, and that limit is
208 * exceeded, then the search is terminated. Note that the check for
209 * termination occurs in the main search loop, so while theories
210 * should call OutputChannel::spendResource() during particularly
211 * long-running operations, they cannot rely on resource() to break
212 * out of infinite or intractable computations.
213 */
214 virtual void spendResource() throw() {}
215
216 /**
217 * Handle user attribute.
218 * Associates theory t with the attribute attr. Theory t will be
219 * notified whenever an attribute of name attr is set on a node.
220 * This can happen through, for example, the SMT-LIBv2 language.
221 */
222 virtual void handleUserAttribute(const char* attr, Theory* t) {}
223
224
225 /** Demands that the search restart from sat search level 0.
226 * Using this leads to non-termination issues.
227 * It is appropriate for prototyping for theories.
228 */
229 virtual void demandRestart() throw(TypeCheckingExceptionPrivate, AssertionException) {}
230
231 };/* class OutputChannel */
232
233 }/* CVC4::theory namespace */
234 }/* CVC4 namespace */
235
236 #endif /* __CVC4__THEORY__OUTPUT_CHANNEL_H */