started work on the inequality bv subtheory
[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_assertions(c),
36 d_slicer(new Slicer(c, this)),
37 d_isCoreTheory(c, true)
38 {
39 if (d_useEqualityEngine) {
40
41 // The kinds we are treating as function application in congruence
42 d_equalityEngine.addFunctionKind(kind::BITVECTOR_CONCAT);
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);
52 d_equalityEngine.addFunctionKind(kind::BITVECTOR_PLUS);
53 d_equalityEngine.addFunctionKind(kind::BITVECTOR_EXTRACT);
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 void CoreSolver::setMasterEqualityEngine(eq::EqualityEngine* eq) {
76 d_equalityEngine.setMasterEqualityEngine(eq);
77 }
78
79 void CoreSolver::preRegister(TNode node) {
80 if (!d_useEqualityEngine)
81 return;
82
83 if (node.getKind() == kind::EQUAL) {
84 d_equalityEngine.addTriggerEquality(node);
85 // d_slicer->processEquality(node);
86 } else {
87 d_equalityEngine.addTerm(node);
88 }
89 }
90
91
92 void CoreSolver::explain(TNode literal, std::vector<TNode>& assumptions) {
93 bool polarity = literal.getKind() != kind::NOT;
94 TNode atom = polarity ? literal : literal[0];
95 if (atom.getKind() == kind::EQUAL) {
96 d_equalityEngine.explainEquality(atom[0], atom[1], polarity, assumptions);
97 } else {
98 d_equalityEngine.explainPredicate(atom, polarity, assumptions);
99 }
100 }
101
102 Node CoreSolver::getBaseDecomposition(TNode a, std::vector<TNode>& explanation) {
103 std::vector<Node> a_decomp;
104 d_slicer->getBaseDecomposition(a, a_decomp, explanation);
105 Node new_a = utils::mkConcat(a_decomp);
106 return new_a;
107 }
108
109 bool CoreSolver::decomposeFact(TNode fact) {
110 Debug("bv-slicer") << "CoreSolver::decomposeFact fact=" << fact << endl;
111 // assert decompositions since the equality engine does not know the semantics of
112 // concat:
113 // a == a_1 concat ... concat a_k
114 // b == b_1 concat ... concat b_k
115
116 if (fact.getKind() == kind::EQUAL) {
117 TNode a = fact[0];
118 TNode b = fact[1];
119
120 d_slicer->processEquality(fact);
121 std::vector<TNode> explanation;
122 Node new_a = getBaseDecomposition(a, explanation);
123 Node new_b = getBaseDecomposition(b, explanation);
124
125 explanation.push_back(fact);
126 TNode reason = utils::mkAnd(explanation);
127
128 Assert (utils::getSize(new_a) == utils::getSize(new_b) &&
129 utils::getSize(new_a) == utils::getSize(a));
130 // FIXME: do we still need to assert these?
131 NodeManager* nm = NodeManager::currentNM();
132 Node a_eq_new_a = nm->mkNode(kind::EQUAL, a, new_a);
133 Node b_eq_new_b = nm->mkNode(kind::EQUAL, b, new_b);
134
135 bool ok = true;
136 ok = assertFactToEqualityEngine(a_eq_new_a, utils::mkTrue());
137 if (!ok) return false;
138 ok = assertFactToEqualityEngine(b_eq_new_b, utils::mkTrue());
139 if (!ok) return false;
140 // assert the individual equalities as well
141 // a_i == b_i
142 if (new_a.getKind() == kind::BITVECTOR_CONCAT &&
143 new_b.getKind() == kind::BITVECTOR_CONCAT) {
144 Assert (new_a.getNumChildren() == new_b.getNumChildren());
145 for (unsigned i = 0; i < new_a.getNumChildren(); ++i) {
146 Node eq_i = nm->mkNode(kind::EQUAL, new_a[i], new_b[i]);
147 ok = assertFactToEqualityEngine(eq_i, reason);
148 if (!ok) return false;
149 }
150 }
151 // merge the two terms in the slicer as well
152 d_slicer->assertEquality(fact);
153 } else {
154 // still need to register the terms
155 TNode a = fact[0][0];
156 TNode b = fact[0][1];
157 d_slicer->registerTerm(a);
158 d_slicer->registerTerm(b);
159 }
160 // finally assert the actual fact to the equality engine
161 return assertFactToEqualityEngine(fact, fact);
162 }
163
164 bool CoreSolver::check(Theory::Effort e) {
165 Trace("bitvector::core") << "CoreSolver::check \n";
166 Assert (!d_bv->inConflict());
167
168 bool ok = true;
169 std::vector<Node> core_eqs;
170 while (! done()) {
171 TNode fact = get();
172
173 // update whether we are in the core fragment
174 if (d_isCoreTheory && !d_slicer->isCoreTerm(fact)) {
175 d_isCoreTheory = false;
176 }
177
178 // only reason about equalities
179 if (fact.getKind() == kind::EQUAL || (fact.getKind() == kind::NOT && fact[0].getKind() == kind::EQUAL)) {
180 ok = decomposeFact(fact);
181 } else {
182 ok = assertFactToEqualityEngine(fact, fact);
183 }
184 if (!ok)
185 return false;
186 }
187
188 // make sure to assert the new splits
189 std::vector<Node> new_splits;
190 d_slicer->getNewSplits(new_splits);
191 for (unsigned i = 0; i < new_splits.size(); ++i) {
192 ok = assertFactToEqualityEngine(new_splits[i], utils::mkTrue());
193 if (!ok)
194 return false;
195 }
196 return true;
197 }
198
199 bool CoreSolver::assertFactToEqualityEngine(TNode fact, TNode reason) {
200 Debug("bv-slicer") << "CoreSolver::assertFactToEqualityEngine fact=" << fact << endl;
201 Debug("bv-slicer") << " reason=" << reason << endl;
202 // Notify the equality engine
203 if (d_useEqualityEngine && !d_bv->inConflict() && !d_bv->propagatedBy(fact, SUB_CORE) ) {
204 Trace("bitvector::core") << " (assert " << fact << ")\n";
205 bool negated = fact.getKind() == kind::NOT;
206 TNode predicate = negated ? fact[0] : fact;
207 if (predicate.getKind() == kind::EQUAL) {
208 if (negated) {
209 // dis-equality
210 d_equalityEngine.assertEquality(predicate, false, reason);
211 } else {
212 // equality
213 d_equalityEngine.assertEquality(predicate, true, reason);
214 }
215 } else {
216 // Adding predicate if the congruence over it is turned on
217 if (d_equalityEngine.isFunctionKind(predicate.getKind())) {
218 d_equalityEngine.assertPredicate(predicate, !negated, reason);
219 }
220 }
221 }
222
223 // checking for a conflict
224 if (d_bv->inConflict()) {
225 return false;
226 }
227 return true;
228 }
229
230 bool CoreSolver::NotifyClass::eqNotifyTriggerEquality(TNode equality, bool value) {
231 Debug("bitvector::core") << "NotifyClass::eqNotifyTriggerEquality(" << equality << ", " << (value ? "true" : "false" )<< ")" << std::endl;
232 if (value) {
233 return d_solver.storePropagation(equality);
234 } else {
235 return d_solver.storePropagation(equality.notNode());
236 }
237 }
238
239 bool CoreSolver::NotifyClass::eqNotifyTriggerPredicate(TNode predicate, bool value) {
240 Debug("bitvector::core") << "NotifyClass::eqNotifyTriggerPredicate(" << predicate << ", " << (value ? "true" : "false" ) << ")" << std::endl;
241 if (value) {
242 return d_solver.storePropagation(predicate);
243 } else {
244 return d_solver.storePropagation(predicate.notNode());
245 }
246 }
247
248 bool CoreSolver::NotifyClass::eqNotifyTriggerTermEquality(TheoryId tag, TNode t1, TNode t2, bool value) {
249 Debug("bitvector::core") << "NotifyClass::eqNotifyTriggerTermMerge(" << t1 << ", " << t2 << ")" << std::endl;
250 if (value) {
251 return d_solver.storePropagation(t1.eqNode(t2));
252 } else {
253 return d_solver.storePropagation(t1.eqNode(t2).notNode());
254 }
255 }
256
257 void CoreSolver::NotifyClass::eqNotifyConstantTermMerge(TNode t1, TNode t2) {
258 d_solver.conflict(t1, t2);
259 }
260
261 bool CoreSolver::storePropagation(TNode literal) {
262 return d_bv->storePropagation(literal, SUB_CORE);
263 }
264
265 void CoreSolver::conflict(TNode a, TNode b) {
266 std::vector<TNode> assumptions;
267 d_equalityEngine.explainEquality(a, b, true, assumptions);
268 d_bv->setConflict(mkAnd(assumptions));
269 }
270
271 void CoreSolver::collectModelInfo(TheoryModel* m) {
272 if (Debug.isOn("bitvector-model")) {
273 context::CDList<TNode>::const_iterator it = d_assertions.begin();
274 for (; it!= d_assertions.end(); ++it) {
275 Debug("bitvector-model") << "CoreSolver::collectModelInfo (assert "
276 << *it << ")\n";
277 }
278 }
279 set<Node> termSet;
280 d_bv->computeRelevantTerms(termSet);
281 m->assertEqualityEngine(&d_equalityEngine, &termSet);
282 }