1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Dejan Jovanovic, Morgan Deters |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 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. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* [[ Add lengthier description here ]] |
14 |
|
* \todo document this file |
15 |
|
*/ |
16 |
|
|
17 |
|
#include "theory/shared_terms_database.h" |
18 |
|
|
19 |
|
#include "smt/smt_statistics_registry.h" |
20 |
|
#include "theory/theory_engine.h" |
21 |
|
|
22 |
|
using namespace std; |
23 |
|
using namespace cvc5::theory; |
24 |
|
|
25 |
|
namespace cvc5 { |
26 |
|
|
27 |
8954 |
SharedTermsDatabase::SharedTermsDatabase(TheoryEngine* theoryEngine, |
28 |
|
context::Context* context, |
29 |
|
context::UserContext* userContext, |
30 |
8954 |
ProofNodeManager* pnm) |
31 |
|
: ContextNotifyObj(context), |
32 |
|
d_statSharedTerms( |
33 |
17908 |
smtStatisticsRegistry().registerInt("theory::shared_terms")), |
34 |
|
d_addedSharedTermsSize(context, 0), |
35 |
|
d_termsToTheories(context), |
36 |
|
d_alreadyNotifiedMap(context), |
37 |
|
d_registeredEqualities(context), |
38 |
|
d_EENotify(*this), |
39 |
|
d_theoryEngine(theoryEngine), |
40 |
|
d_inConflict(context, false), |
41 |
|
d_conflictPolarity(), |
42 |
|
d_satContext(context), |
43 |
|
d_userContext(userContext), |
44 |
|
d_equalityEngine(nullptr), |
45 |
|
d_pfee(nullptr), |
46 |
26862 |
d_pnm(pnm) |
47 |
|
{ |
48 |
8954 |
} |
49 |
|
|
50 |
8954 |
void SharedTermsDatabase::setEqualityEngine(eq::EqualityEngine* ee) |
51 |
|
{ |
52 |
8954 |
Assert(ee != nullptr); |
53 |
8954 |
d_equalityEngine = ee; |
54 |
|
// if proofs are enabled, make the proof equality engine |
55 |
8954 |
if (d_pnm != nullptr) |
56 |
|
{ |
57 |
2428 |
d_pfee.reset( |
58 |
1214 |
new eq::ProofEqEngine(d_satContext, d_userContext, *ee, d_pnm)); |
59 |
|
} |
60 |
8954 |
} |
61 |
|
|
62 |
8954 |
bool SharedTermsDatabase::needsEqualityEngine(EeSetupInfo& esi) |
63 |
|
{ |
64 |
8954 |
esi.d_notify = &d_EENotify; |
65 |
8954 |
esi.d_name = "SharedTermsDatabase"; |
66 |
8954 |
return true; |
67 |
|
} |
68 |
|
|
69 |
391944 |
void SharedTermsDatabase::addEqualityToPropagate(TNode equality) { |
70 |
391944 |
Assert(d_equalityEngine != nullptr); |
71 |
391944 |
d_registeredEqualities.insert(equality); |
72 |
391944 |
d_equalityEngine->addTriggerPredicate(equality); |
73 |
391944 |
checkForConflict(); |
74 |
391944 |
} |
75 |
|
|
76 |
992960 |
void SharedTermsDatabase::addSharedTerm(TNode atom, |
77 |
|
TNode term, |
78 |
|
TheoryIdSet theories) |
79 |
|
{ |
80 |
1985920 |
Debug("register") << "SharedTermsDatabase::addSharedTerm(" << atom << ", " |
81 |
1985920 |
<< term << ", " << TheoryIdSetUtil::setToString(theories) |
82 |
992960 |
<< ")" << std::endl; |
83 |
|
|
84 |
1985920 |
std::pair<TNode, TNode> search_pair(atom, term); |
85 |
992960 |
SharedTermsTheoriesMap::iterator find = d_termsToTheories.find(search_pair); |
86 |
992960 |
if (find == d_termsToTheories.end()) { |
87 |
|
// First time for this term and this atom |
88 |
991053 |
d_atomsToTerms[atom].push_back(term); |
89 |
991053 |
d_addedSharedTerms.push_back(atom); |
90 |
991053 |
d_addedSharedTermsSize = d_addedSharedTermsSize + 1; |
91 |
991053 |
d_termsToTheories[search_pair] = theories; |
92 |
|
} else { |
93 |
1907 |
Assert(theories != (*find).second); |
94 |
1907 |
d_termsToTheories[search_pair] = |
95 |
3814 |
TheoryIdSetUtil::setUnion(theories, (*find).second); |
96 |
|
} |
97 |
992960 |
} |
98 |
|
|
99 |
3921378 |
SharedTermsDatabase::shared_terms_iterator SharedTermsDatabase::begin(TNode atom) const { |
100 |
3921378 |
Assert(hasSharedTerms(atom)); |
101 |
3921378 |
return d_atomsToTerms.find(atom)->second.begin(); |
102 |
|
} |
103 |
|
|
104 |
3921378 |
SharedTermsDatabase::shared_terms_iterator SharedTermsDatabase::end(TNode atom) const { |
105 |
3921378 |
Assert(hasSharedTerms(atom)); |
106 |
3921378 |
return d_atomsToTerms.find(atom)->second.end(); |
107 |
|
} |
108 |
|
|
109 |
14153368 |
bool SharedTermsDatabase::hasSharedTerms(TNode atom) const { |
110 |
14153368 |
return d_atomsToTerms.find(atom) != d_atomsToTerms.end(); |
111 |
|
} |
112 |
|
|
113 |
2201736 |
void SharedTermsDatabase::backtrack() { |
114 |
3192789 |
for (int i = d_addedSharedTerms.size() - 1, i_end = (int)d_addedSharedTermsSize; i >= i_end; -- i) { |
115 |
1982106 |
TNode atom = d_addedSharedTerms[i]; |
116 |
991053 |
shared_terms_list& list = d_atomsToTerms[atom]; |
117 |
991053 |
list.pop_back(); |
118 |
991053 |
if (list.empty()) { |
119 |
375454 |
d_atomsToTerms.erase(atom); |
120 |
|
} |
121 |
|
} |
122 |
2201736 |
d_addedSharedTerms.resize(d_addedSharedTermsSize); |
123 |
2201736 |
} |
124 |
|
|
125 |
11115004 |
TheoryIdSet SharedTermsDatabase::getTheoriesToNotify(TNode atom, |
126 |
|
TNode term) const |
127 |
|
{ |
128 |
|
// Get the theories that share this term from this atom |
129 |
22230008 |
std::pair<TNode, TNode> search_pair(atom, term); |
130 |
11115004 |
SharedTermsTheoriesMap::iterator find = d_termsToTheories.find(search_pair); |
131 |
11115004 |
Assert(find != d_termsToTheories.end()); |
132 |
|
|
133 |
|
// Get the theories that were already notified |
134 |
11115004 |
TheoryIdSet alreadyNotified = 0; |
135 |
11115004 |
AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term); |
136 |
11115004 |
if (theoriesFind != d_alreadyNotifiedMap.end()) { |
137 |
10360318 |
alreadyNotified = (*theoriesFind).second; |
138 |
|
} |
139 |
|
|
140 |
|
// Return the ones that haven't been notified yet |
141 |
22230008 |
return TheoryIdSetUtil::setDifference((*find).second, alreadyNotified); |
142 |
|
} |
143 |
|
|
144 |
|
TheoryIdSet SharedTermsDatabase::getNotifiedTheories(TNode term) const |
145 |
|
{ |
146 |
|
// Get the theories that were already notified |
147 |
|
AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term); |
148 |
|
if (theoriesFind != d_alreadyNotifiedMap.end()) { |
149 |
|
return (*theoriesFind).second; |
150 |
|
} else { |
151 |
|
return 0; |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
2556989 |
bool SharedTermsDatabase::propagateSharedEquality(TheoryId theory, TNode a, TNode b, bool value) |
156 |
|
{ |
157 |
2556989 |
Debug("shared-terms-database") << "SharedTermsDatabase::newEquality(" << theory << "," << a << "," << b << ", " << (value ? "true" : "false") << ")" << endl; |
158 |
|
|
159 |
2556989 |
if (d_inConflict) { |
160 |
|
return false; |
161 |
|
} |
162 |
|
|
163 |
|
// Propagate away |
164 |
5113978 |
Node equality = a.eqNode(b); |
165 |
2556989 |
if (value) { |
166 |
1681407 |
d_theoryEngine->assertToTheory(equality, equality, theory, THEORY_BUILTIN); |
167 |
|
} else { |
168 |
875582 |
d_theoryEngine->assertToTheory(equality.notNode(), equality.notNode(), theory, THEORY_BUILTIN); |
169 |
|
} |
170 |
|
|
171 |
|
// As you were |
172 |
2556989 |
return true; |
173 |
|
} |
174 |
|
|
175 |
11115004 |
void SharedTermsDatabase::markNotified(TNode term, TheoryIdSet theories) |
176 |
|
{ |
177 |
|
// Find out if there are any new theories that were notified about this term |
178 |
11115004 |
TheoryIdSet alreadyNotified = 0; |
179 |
11115004 |
AlreadyNotifiedMap::iterator theoriesFind = d_alreadyNotifiedMap.find(term); |
180 |
11115004 |
if (theoriesFind != d_alreadyNotifiedMap.end()) { |
181 |
10360318 |
alreadyNotified = (*theoriesFind).second; |
182 |
|
} |
183 |
|
TheoryIdSet newlyNotified = |
184 |
11115004 |
TheoryIdSetUtil::setDifference(theories, alreadyNotified); |
185 |
|
|
186 |
|
// If no new theories were notified, we are done |
187 |
11115004 |
if (newlyNotified == 0) { |
188 |
20698974 |
return; |
189 |
|
} |
190 |
|
|
191 |
765517 |
Debug("shared-terms-database") << "SharedTermsDatabase::markNotified(" << term << ")" << endl; |
192 |
|
|
193 |
|
// First update the set of notified theories for this term |
194 |
765517 |
d_alreadyNotifiedMap[term] = |
195 |
1531034 |
TheoryIdSetUtil::setUnion(newlyNotified, alreadyNotified); |
196 |
|
|
197 |
765517 |
if (d_equalityEngine == nullptr) |
198 |
|
{ |
199 |
|
// if we are not assigned an equality engine, there is nothing to do |
200 |
|
return; |
201 |
|
} |
202 |
|
|
203 |
|
// Mark the shared terms in the equality engine |
204 |
|
theory::TheoryId currentTheory; |
205 |
1543213 |
while ((currentTheory = TheoryIdSetUtil::setPop(newlyNotified)) |
206 |
2308730 |
!= THEORY_LAST) |
207 |
|
{ |
208 |
1543213 |
d_equalityEngine->addTriggerTerm(term, currentTheory); |
209 |
|
} |
210 |
|
|
211 |
|
// Check for any conflits |
212 |
765517 |
checkForConflict(); |
213 |
|
} |
214 |
|
|
215 |
1100228 |
bool SharedTermsDatabase::areEqual(TNode a, TNode b) const { |
216 |
1100228 |
Assert(d_equalityEngine != nullptr); |
217 |
1100228 |
if (d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b)) |
218 |
|
{ |
219 |
1100228 |
return d_equalityEngine->areEqual(a, b); |
220 |
|
} else { |
221 |
|
Assert(d_equalityEngine->hasTerm(a) || a.isConst()); |
222 |
|
Assert(d_equalityEngine->hasTerm(b) || b.isConst()); |
223 |
|
// since one (or both) of them is a constant, and the other is in the equality engine, they are not same |
224 |
|
return false; |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
929063 |
bool SharedTermsDatabase::areDisequal(TNode a, TNode b) const { |
229 |
929063 |
Assert(d_equalityEngine != nullptr); |
230 |
929063 |
if (d_equalityEngine->hasTerm(a) && d_equalityEngine->hasTerm(b)) |
231 |
|
{ |
232 |
929063 |
return d_equalityEngine->areDisequal(a, b, false); |
233 |
|
} else { |
234 |
|
Assert(d_equalityEngine->hasTerm(a) || a.isConst()); |
235 |
|
Assert(d_equalityEngine->hasTerm(b) || b.isConst()); |
236 |
|
// one (or both) are in the equality engine |
237 |
|
return false; |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
|
theory::eq::EqualityEngine* SharedTermsDatabase::getEqualityEngine() |
242 |
|
{ |
243 |
|
return d_equalityEngine; |
244 |
|
} |
245 |
|
|
246 |
4620441 |
void SharedTermsDatabase::assertEquality(TNode equality, bool polarity, TNode reason) |
247 |
|
{ |
248 |
4620441 |
Assert(d_equalityEngine != nullptr); |
249 |
4620441 |
Debug("shared-terms-database::assert") << "SharedTermsDatabase::assertEquality(" << equality << ", " << (polarity ? "true" : "false") << ", " << reason << ")" << endl; |
250 |
|
// Add it to the equality engine |
251 |
4620441 |
d_equalityEngine->assertEquality(equality, polarity, reason); |
252 |
|
// Check for conflict |
253 |
4620441 |
checkForConflict(); |
254 |
4620441 |
} |
255 |
|
|
256 |
3594306 |
bool SharedTermsDatabase::propagateEquality(TNode equality, bool polarity) { |
257 |
3594306 |
if (polarity) { |
258 |
1443112 |
d_theoryEngine->propagate(equality, THEORY_BUILTIN); |
259 |
|
} else { |
260 |
2151194 |
d_theoryEngine->propagate(equality.notNode(), THEORY_BUILTIN); |
261 |
|
} |
262 |
3594306 |
return true; |
263 |
|
} |
264 |
|
|
265 |
5777902 |
void SharedTermsDatabase::checkForConflict() |
266 |
|
{ |
267 |
5777902 |
if (!d_inConflict) |
268 |
|
{ |
269 |
5765917 |
return; |
270 |
|
} |
271 |
11985 |
d_inConflict = false; |
272 |
23970 |
TrustNode trnc; |
273 |
11985 |
if (d_pfee != nullptr) |
274 |
|
{ |
275 |
3150 |
Node conflict = d_conflictLHS.eqNode(d_conflictRHS); |
276 |
1575 |
conflict = d_conflictPolarity ? conflict : conflict.notNode(); |
277 |
1575 |
trnc = d_pfee->assertConflict(conflict); |
278 |
|
} |
279 |
|
else |
280 |
|
{ |
281 |
|
// standard explain |
282 |
20820 |
std::vector<TNode> assumptions; |
283 |
20820 |
d_equalityEngine->explainEquality( |
284 |
10410 |
d_conflictLHS, d_conflictRHS, d_conflictPolarity, assumptions); |
285 |
20820 |
Node conflictNode = NodeManager::currentNM()->mkAnd(assumptions); |
286 |
10410 |
trnc = TrustNode::mkTrustConflict(conflictNode, nullptr); |
287 |
|
} |
288 |
11985 |
d_theoryEngine->conflict(trnc, THEORY_BUILTIN); |
289 |
11985 |
d_conflictLHS = d_conflictRHS = Node::null(); |
290 |
|
} |
291 |
|
|
292 |
|
bool SharedTermsDatabase::isKnown(TNode literal) const { |
293 |
|
Assert(d_equalityEngine != nullptr); |
294 |
|
bool polarity = literal.getKind() != kind::NOT; |
295 |
|
TNode equality = polarity ? literal : literal[0]; |
296 |
|
if (polarity) { |
297 |
|
return d_equalityEngine->areEqual(equality[0], equality[1]); |
298 |
|
} else { |
299 |
|
return d_equalityEngine->areDisequal(equality[0], equality[1], false); |
300 |
|
} |
301 |
|
} |
302 |
|
|
303 |
118306 |
theory::TrustNode SharedTermsDatabase::explain(TNode literal) const |
304 |
|
{ |
305 |
118306 |
if (d_pfee != nullptr) |
306 |
|
{ |
307 |
|
// use the proof equality engine if it exists |
308 |
22314 |
return d_pfee->explain(literal); |
309 |
|
} |
310 |
|
// otherwise, explain without proofs |
311 |
191984 |
Node exp = d_equalityEngine->mkExplainLit(literal); |
312 |
|
// no proof generator |
313 |
95992 |
return TrustNode::mkTrustPropExp(literal, exp, nullptr); |
314 |
|
} |
315 |
|
|
316 |
27735 |
} // namespace cvc5 |