Merge branch 'master' into bv-core
[cvc5.git] / src / theory / bv / bv_subtheory_core.cpp
1 /********************* */
2 /*! \file bv_subtheory_eq.cpp
3 ** \verbatim
4 ** Original author: dejan
5 ** Major contributors: none
6 ** Minor contributors (to current version): lianah
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009-2012 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 Algebraic solver.
13 **
14 ** Algebraic solver.
15 **/
16
17 #include "theory/bv/bv_subtheory_core.h"
18
19 #include "theory/bv/theory_bv.h"
20 #include "theory/bv/theory_bv_utils.h"
21 #include "theory/bv/slicer.h"
22 #include "theory/model.h"
23
24 using namespace std;
25 using namespace CVC4;
26 using namespace CVC4::context;
27 using namespace CVC4::theory;
28 using namespace CVC4::theory::bv;
29 using namespace CVC4::theory::bv::utils;
30
31 CoreSolver::CoreSolver(context::Context* c, TheoryBV* bv)
32 : SubtheorySolver(c, bv),
33 d_notify(*this),
34 d_equalityEngine(d_notify, c, "theory::bv::TheoryBV"),
35 d_slicer(new Slicer(c, this)),
36 d_isCoreTheory(c, true),
37 d_reasons(c)
38 {
39 if (d_useEqualityEngine) {
40
41 // The kinds we are treating as function application in congruence
42 d_equalityEngine.addFunctionKind(kind::BITVECTOR_CONCAT, true);
43 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_AND);
44 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_OR);
45 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_XOR);
46 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_NOT);
47 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_NAND);
48 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_NOR);
49 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_XNOR);
50 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_COMP);
51 d_equalityEngine.addFunctionKind(kind::BITVECTOR_MULT, true);
52 d_equalityEngine.addFunctionKind(kind::BITVECTOR_PLUS, true);
53 d_equalityEngine.addFunctionKind(kind::BITVECTOR_EXTRACT, true);
54 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SUB);
55 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_NEG);
56 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_UDIV);
57 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_UREM);
58 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SDIV);
59 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SREM);
60 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SMOD);
61 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SHL);
62 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_LSHR);
63 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_ASHR);
64 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_ULT);
65 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_ULE);
66 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_UGT);
67 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_UGE);
68 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SLT);
69 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SLE);
70 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SGT);
71 // d_equalityEngine.addFunctionKind(kind::BITVECTOR_SGE);
72 }
73 }
74
75 CoreSolver::~CoreSolver() {
76 delete d_slicer;
77 }
78 void CoreSolver::setMasterEqualityEngine(eq::EqualityEngine* eq) {
79 d_equalityEngine.setMasterEqualityEngine(eq);
80 }
81
82 void CoreSolver::preRegister(TNode node) {
83 if (!d_useEqualityEngine)
84 return;
85
86 if (node.getKind() == kind::EQUAL) {
87 d_equalityEngine.addTriggerEquality(node);
88 // d_slicer->processEquality(node);
89 } else {
90 d_equalityEngine.addTerm(node);
91 }
92 }
93
94
95 void CoreSolver::explain(TNode literal, std::vector<TNode>& assumptions) {
96 bool polarity = literal.getKind() != kind::NOT;
97 TNode atom = polarity ? literal : literal[0];
98 if (atom.getKind() == kind::EQUAL) {
99 d_equalityEngine.explainEquality(atom[0], atom[1], polarity, assumptions);
100 } else {
101 d_equalityEngine.explainPredicate(atom, polarity, assumptions);
102 }
103 }
104
105 Node CoreSolver::getBaseDecomposition(TNode a, std::vector<Node>& explanation) {
106 std::vector<Node> a_decomp;
107 d_slicer->getBaseDecomposition(a, a_decomp, explanation);
108 Node new_a = utils::mkConcat(a_decomp);
109 Debug("bv-slicer") << "CoreSolver::getBaseDecomposition " << a <<" => " << new_a << "\n";
110 return new_a;
111 }
112
113 bool CoreSolver::decomposeFact(TNode fact) {
114 Debug("bv-slicer") << "CoreSolver::decomposeFact fact=" << fact << endl;
115 // assert decompositions since the equality engine does not know the semantics of
116 // concat:
117 // a == a_1 concat ... concat a_k
118 // b == b_1 concat ... concat b_k
119
120 if (fact.getKind() == kind::EQUAL) {
121 TNode a = fact[0];
122 TNode b = fact[1];
123
124 d_slicer->processEquality(fact);
125 std::vector<Node> explanation;
126 Node new_a = getBaseDecomposition(a, explanation);
127 Node new_b = getBaseDecomposition(b, explanation);
128
129 explanation.push_back(fact);
130 Node reason = utils::mkAnd(explanation);
131 d_reasons.insert(reason);
132
133 Assert (utils::getSize(new_a) == utils::getSize(new_b) &&
134 utils::getSize(new_a) == utils::getSize(a));
135 // FIXME: do we still need to assert these?
136 NodeManager* nm = NodeManager::currentNM();
137 Node a_eq_new_a = nm->mkNode(kind::EQUAL, a, new_a);
138 Node b_eq_new_b = nm->mkNode(kind::EQUAL, b, new_b);
139
140 d_reasons.insert(a_eq_new_a);
141 d_reasons.insert(b_eq_new_b);
142
143 bool ok = true;
144 ok = assertFactToEqualityEngine(a_eq_new_a, utils::mkTrue());
145 if (!ok) return false;
146 ok = assertFactToEqualityEngine(b_eq_new_b, utils::mkTrue());
147 if (!ok) return false;
148 // assert the individual equalities as well
149 // a_i == b_i
150 if (new_a.getKind() == kind::BITVECTOR_CONCAT &&
151 new_b.getKind() == kind::BITVECTOR_CONCAT) {
152 Assert (new_a.getNumChildren() == new_b.getNumChildren());
153 for (unsigned i = 0; i < new_a.getNumChildren(); ++i) {
154 Node eq_i = nm->mkNode(kind::EQUAL, new_a[i], new_b[i]);
155 ok = assertFactToEqualityEngine(eq_i, reason);
156 d_reasons.insert(eq_i);
157 if (!ok) return false;
158 }
159 }
160 // merge the two terms in the slicer as well
161 d_slicer->assertEquality(fact);
162 } else {
163 // still need to register the terms
164 d_slicer->processEquality(fact[0]);
165 TNode a = fact[0][0];
166 TNode b = fact[0][1];
167 std::vector<Node> explanation_a;
168 Node new_a = getBaseDecomposition(a, explanation_a);
169 Node reason_a = explanation_a.empty()? mkTrue() : mkAnd(explanation_a);
170 assertFactToEqualityEngine(utils::mkNode(kind::EQUAL, a, new_a), reason_a);
171
172 std::vector<Node> explanation_b;
173 Node new_b = getBaseDecomposition(b, explanation_b);
174 Node reason_b = explanation_b.empty()? mkTrue() : mkAnd(explanation_b);
175 assertFactToEqualityEngine(utils::mkNode(kind::EQUAL, b, new_b), reason_b);
176 d_reasons.insert(reason_a);
177 d_reasons.insert(reason_b);
178 }
179 // finally assert the actual fact to the equality engine
180 return assertFactToEqualityEngine(fact, fact);
181 }
182
183 bool CoreSolver::check(Theory::Effort e) {
184 Trace("bitvector::core") << "CoreSolver::check \n";
185 Assert (!d_bv->inConflict());
186
187 bool ok = true;
188 std::vector<Node> core_eqs;
189 while (! done()) {
190 TNode fact = get();
191
192 // update whether we are in the core fragment
193 if (d_isCoreTheory && !d_slicer->isCoreTerm(fact)) {
194 d_isCoreTheory = false;
195 }
196
197 // only reason about equalities
198 if (fact.getKind() == kind::EQUAL || (fact.getKind() == kind::NOT && fact[0].getKind() == kind::EQUAL)) {
199 ok = decomposeFact(fact);
200 } else {
201 ok = assertFactToEqualityEngine(fact, fact);
202 }
203 if (!ok)
204 return false;
205 }
206
207 // make sure to assert the new splits
208 std::vector<Node> new_splits;
209 d_slicer->getNewSplits(new_splits);
210 for (unsigned i = 0; i < new_splits.size(); ++i) {
211 ok = assertFactToEqualityEngine(new_splits[i], utils::mkTrue());
212 if (!ok)
213 return false;
214 }
215 return true;
216 }
217
218 bool CoreSolver::assertFactToEqualityEngine(TNode fact, TNode reason) {
219 // Notify the equality engine
220 if (d_useEqualityEngine && !d_bv->inConflict() && !d_bv->propagatedBy(fact, SUB_CORE) ) {
221 Debug("bv-slicer-eq") << "CoreSolver::assertFactToEqualityEngine fact=" << fact << endl;
222 // Debug("bv-slicer-eq") << " reason=" << reason << endl;
223 bool negated = fact.getKind() == kind::NOT;
224 TNode predicate = negated ? fact[0] : fact;
225 if (predicate.getKind() == kind::EQUAL) {
226 if (negated) {
227 // dis-equality
228 d_equalityEngine.assertEquality(predicate, false, reason);
229 } else {
230 // equality
231 d_equalityEngine.assertEquality(predicate, true, reason);
232 }
233 } else {
234 // Adding predicate if the congruence over it is turned on
235 if (d_equalityEngine.isFunctionKind(predicate.getKind())) {
236 d_equalityEngine.assertPredicate(predicate, !negated, reason);
237 }
238 }
239 }
240
241 // checking for a conflict
242 if (d_bv->inConflict()) {
243 return false;
244 }
245 return true;
246 }
247
248 bool CoreSolver::NotifyClass::eqNotifyTriggerEquality(TNode equality, bool value) {
249 Debug("bitvector::core") << "NotifyClass::eqNotifyTriggerEquality(" << equality << ", " << (value ? "true" : "false" )<< ")" << std::endl;
250 if (value) {
251 return d_solver.storePropagation(equality);
252 } else {
253 return d_solver.storePropagation(equality.notNode());
254 }
255 }
256
257 bool CoreSolver::NotifyClass::eqNotifyTriggerPredicate(TNode predicate, bool value) {
258 Debug("bitvector::core") << "NotifyClass::eqNotifyTriggerPredicate(" << predicate << ", " << (value ? "true" : "false" ) << ")" << std::endl;
259 if (value) {
260 return d_solver.storePropagation(predicate);
261 } else {
262 return d_solver.storePropagation(predicate.notNode());
263 }
264 }
265
266 bool CoreSolver::NotifyClass::eqNotifyTriggerTermEquality(TheoryId tag, TNode t1, TNode t2, bool value) {
267 Debug("bitvector::core") << "NotifyClass::eqNotifyTriggerTermMerge(" << t1 << ", " << t2 << ")" << std::endl;
268 if (value) {
269 return d_solver.storePropagation(t1.eqNode(t2));
270 } else {
271 return d_solver.storePropagation(t1.eqNode(t2).notNode());
272 }
273 }
274
275 void CoreSolver::NotifyClass::eqNotifyConstantTermMerge(TNode t1, TNode t2) {
276 d_solver.conflict(t1, t2);
277 }
278
279 bool CoreSolver::storePropagation(TNode literal) {
280 return d_bv->storePropagation(literal, SUB_CORE);
281 }
282
283 void CoreSolver::conflict(TNode a, TNode b) {
284 std::vector<TNode> assumptions;
285 d_equalityEngine.explainEquality(a, b, true, assumptions);
286 d_bv->setConflict(mkAnd(assumptions));
287 }
288
289 void CoreSolver::collectModelInfo(TheoryModel* m) {
290 if (Debug.isOn("bitvector-model")) {
291 context::CDQueue<Node>::const_iterator it = d_assertionQueue.begin();
292 for (; it!= d_assertionQueue.end(); ++it) {
293 Debug("bitvector-model") << "CoreSolver::collectModelInfo (assert "
294 << *it << ")\n";
295 }
296 }
297 set<Node> termSet;
298 d_bv->computeRelevantTerms(termSet);
299 m->assertEqualityEngine(&d_equalityEngine, &termSet);
300 }