1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Morgan Deters, Dejan Jovanovic |
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 |
|
* This is the interface to TheoryUF implementations |
14 |
|
* |
15 |
|
* All implementations of TheoryUF should inherit from this class. |
16 |
|
*/ |
17 |
|
|
18 |
|
#include "theory/uf/theory_uf.h" |
19 |
|
|
20 |
|
#include <memory> |
21 |
|
#include <sstream> |
22 |
|
|
23 |
|
#include "expr/node_algorithm.h" |
24 |
|
#include "options/quantifiers_options.h" |
25 |
|
#include "options/smt_options.h" |
26 |
|
#include "options/theory_options.h" |
27 |
|
#include "options/uf_options.h" |
28 |
|
#include "proof/proof_node_manager.h" |
29 |
|
#include "smt/logic_exception.h" |
30 |
|
#include "theory/theory_model.h" |
31 |
|
#include "theory/type_enumerator.h" |
32 |
|
#include "theory/uf/cardinality_extension.h" |
33 |
|
#include "theory/uf/ho_extension.h" |
34 |
|
#include "theory/uf/theory_uf_rewriter.h" |
35 |
|
|
36 |
|
using namespace std; |
37 |
|
|
38 |
|
namespace cvc5 { |
39 |
|
namespace theory { |
40 |
|
namespace uf { |
41 |
|
|
42 |
|
/** Constructs a new instance of TheoryUF w.r.t. the provided context.*/ |
43 |
15273 |
TheoryUF::TheoryUF(Env& env, |
44 |
|
OutputChannel& out, |
45 |
|
Valuation valuation, |
46 |
15273 |
std::string instanceName) |
47 |
|
: Theory(THEORY_UF, env, out, valuation, instanceName), |
48 |
|
d_thss(nullptr), |
49 |
|
d_ho(nullptr), |
50 |
|
d_functionsTerms(context()), |
51 |
|
d_symb(env, instanceName), |
52 |
15273 |
d_rewriter(logicInfo().isHigherOrder()), |
53 |
|
d_state(env, valuation), |
54 |
30546 |
d_im(env, *this, d_state, "theory::uf::" + instanceName, false), |
55 |
61092 |
d_notify(d_im, *this) |
56 |
|
{ |
57 |
15273 |
d_true = NodeManager::currentNM()->mkConst( true ); |
58 |
|
// indicate we are using the default theory state and inference managers |
59 |
15273 |
d_theoryState = &d_state; |
60 |
15273 |
d_inferManager = &d_im; |
61 |
15273 |
} |
62 |
|
|
63 |
30536 |
TheoryUF::~TheoryUF() { |
64 |
30536 |
} |
65 |
|
|
66 |
15273 |
TheoryRewriter* TheoryUF::getTheoryRewriter() { return &d_rewriter; } |
67 |
|
|
68 |
7989 |
ProofRuleChecker* TheoryUF::getProofChecker() { return &d_checker; } |
69 |
|
|
70 |
15273 |
bool TheoryUF::needsEqualityEngine(EeSetupInfo& esi) |
71 |
|
{ |
72 |
15273 |
esi.d_notify = &d_notify; |
73 |
15273 |
esi.d_name = d_instanceName + "theory::uf::ee"; |
74 |
30546 |
if (options::finiteModelFind() |
75 |
15273 |
&& options::ufssMode() != options::UfssMode::NONE) |
76 |
|
{ |
77 |
|
// need notifications about sorts |
78 |
296 |
esi.d_notifyNewClass = true; |
79 |
296 |
esi.d_notifyMerge = true; |
80 |
296 |
esi.d_notifyDisequal = true; |
81 |
|
} |
82 |
15273 |
return true; |
83 |
|
} |
84 |
|
|
85 |
15273 |
void TheoryUF::finishInit() { |
86 |
15273 |
Assert(d_equalityEngine != nullptr); |
87 |
|
// combined cardinality constraints are not evaluated in getModelValue |
88 |
15273 |
d_valuation.setUnevaluatedKind(kind::COMBINED_CARDINALITY_CONSTRAINT); |
89 |
|
// Initialize the cardinality constraints solver if the logic includes UF, |
90 |
|
// finite model finding is enabled, and it is not disabled by |
91 |
|
// options::ufssMode(). |
92 |
30546 |
if (options::finiteModelFind() |
93 |
15273 |
&& options::ufssMode() != options::UfssMode::NONE) |
94 |
|
{ |
95 |
296 |
d_thss.reset(new CardinalityExtension(d_env, d_state, d_im, this)); |
96 |
|
} |
97 |
|
// The kinds we are treating as function application in congruence |
98 |
15273 |
bool isHo = logicInfo().isHigherOrder(); |
99 |
15273 |
d_equalityEngine->addFunctionKind(kind::APPLY_UF, false, isHo); |
100 |
15273 |
if (isHo) |
101 |
|
{ |
102 |
238 |
d_equalityEngine->addFunctionKind(kind::HO_APPLY); |
103 |
238 |
d_ho.reset(new HoExtension(d_env, d_state, d_im)); |
104 |
|
} |
105 |
15273 |
} |
106 |
|
|
107 |
|
static Node mkAnd(const std::vector<TNode>& conjunctions) { |
108 |
|
Assert(conjunctions.size() > 0); |
109 |
|
|
110 |
|
std::set<TNode> all; |
111 |
|
all.insert(conjunctions.begin(), conjunctions.end()); |
112 |
|
|
113 |
|
if (all.size() == 1) { |
114 |
|
// All the same, or just one |
115 |
|
return conjunctions[0]; |
116 |
|
} |
117 |
|
|
118 |
|
NodeBuilder conjunction(kind::AND); |
119 |
|
std::set<TNode>::const_iterator it = all.begin(); |
120 |
|
std::set<TNode>::const_iterator it_end = all.end(); |
121 |
|
while (it != it_end) { |
122 |
|
conjunction << *it; |
123 |
|
++ it; |
124 |
|
} |
125 |
|
|
126 |
|
return conjunction; |
127 |
|
}/* mkAnd() */ |
128 |
|
|
129 |
|
//--------------------------------- standard check |
130 |
|
|
131 |
24131 |
bool TheoryUF::needsCheckLastEffort() |
132 |
|
{ |
133 |
|
// last call effort needed if using finite model finding |
134 |
24131 |
return d_thss != nullptr; |
135 |
|
} |
136 |
|
|
137 |
1308995 |
void TheoryUF::postCheck(Effort level) |
138 |
|
{ |
139 |
1308995 |
if (d_state.isInConflict()) |
140 |
|
{ |
141 |
49180 |
return; |
142 |
|
} |
143 |
|
// check with the cardinality constraints extension |
144 |
1259815 |
if (d_thss != nullptr) |
145 |
|
{ |
146 |
88215 |
d_thss->check(level); |
147 |
|
} |
148 |
|
// check with the higher-order extension at full effort |
149 |
1259815 |
if (!d_state.isInConflict() && fullEffort(level)) |
150 |
|
{ |
151 |
88907 |
if (logicInfo().isHigherOrder()) |
152 |
|
{ |
153 |
1071 |
d_ho->check(); |
154 |
|
} |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
3442268 |
void TheoryUF::notifyFact(TNode atom, bool pol, TNode fact, bool isInternal) |
159 |
|
{ |
160 |
3442268 |
if (d_state.isInConflict()) |
161 |
|
{ |
162 |
48219 |
return; |
163 |
|
} |
164 |
3394049 |
if (d_thss != nullptr) |
165 |
|
{ |
166 |
|
bool isDecision = |
167 |
212226 |
d_valuation.isSatLiteral(fact) && d_valuation.isDecision(fact); |
168 |
212226 |
d_thss->assertNode(fact, isDecision); |
169 |
|
} |
170 |
3394049 |
switch (atom.getKind()) |
171 |
|
{ |
172 |
3196153 |
case kind::EQUAL: |
173 |
|
{ |
174 |
3196153 |
if (logicInfo().isHigherOrder() && options().uf.ufHoExt) |
175 |
|
{ |
176 |
56335 |
if (!pol && !d_state.isInConflict() && atom[0].getType().isFunction()) |
177 |
|
{ |
178 |
|
// apply extensionality eagerly using the ho extension |
179 |
3629 |
d_ho->applyExtensionality(fact); |
180 |
|
} |
181 |
|
} |
182 |
|
} |
183 |
3196153 |
break; |
184 |
13742 |
case kind::CARDINALITY_CONSTRAINT: |
185 |
|
case kind::COMBINED_CARDINALITY_CONSTRAINT: |
186 |
|
{ |
187 |
13742 |
if (d_thss == nullptr) |
188 |
|
{ |
189 |
|
if (!logicInfo().hasCardinalityConstraints()) |
190 |
|
{ |
191 |
|
std::stringstream ss; |
192 |
|
ss << "Cardinality constraint " << atom |
193 |
|
<< " was asserted, but the logic does not allow it." << std::endl; |
194 |
|
ss << "Try using a logic containing \"UFC\"." << std::endl; |
195 |
|
throw Exception(ss.str()); |
196 |
|
} |
197 |
|
else |
198 |
|
{ |
199 |
|
// support for cardinality constraints is not enabled, set incomplete |
200 |
|
d_im.setIncomplete(IncompleteId::UF_CARD_DISABLED); |
201 |
|
} |
202 |
|
} |
203 |
|
} |
204 |
13742 |
break; |
205 |
184154 |
default: break; |
206 |
|
} |
207 |
|
} |
208 |
|
//--------------------------------- end standard check |
209 |
|
|
210 |
299138 |
TrustNode TheoryUF::ppRewrite(TNode node, std::vector<SkolemLemma>& lems) |
211 |
|
{ |
212 |
598276 |
Trace("uf-exp-def") << "TheoryUF::ppRewrite: expanding definition : " << node |
213 |
299138 |
<< std::endl; |
214 |
299138 |
Kind k = node.getKind(); |
215 |
299138 |
if (k == kind::HO_APPLY || (node.isVar() && node.getType().isFunction())) |
216 |
|
{ |
217 |
746 |
if (!logicInfo().isHigherOrder()) |
218 |
|
{ |
219 |
4 |
std::stringstream ss; |
220 |
2 |
ss << "Partial function applications are only supported with " |
221 |
|
"higher-order logic. Try adding the logic prefix HO_."; |
222 |
2 |
throw LogicException(ss.str()); |
223 |
|
} |
224 |
1369 |
Node ret = d_ho->ppRewrite(node); |
225 |
744 |
if (ret != node) |
226 |
|
{ |
227 |
238 |
Trace("uf-exp-def") << "TheoryUF::ppRewrite: higher-order: " << node |
228 |
119 |
<< " to " << ret << std::endl; |
229 |
119 |
return TrustNode::mkTrustRewrite(node, ret, nullptr); |
230 |
|
} |
231 |
|
} |
232 |
298392 |
else if (k == kind::APPLY_UF) |
233 |
|
{ |
234 |
|
// check for higher-order |
235 |
|
// logic exception if higher-order is not enabled |
236 |
298998 |
if (isHigherOrderType(node.getOperator().getType()) |
237 |
298998 |
&& !logicInfo().isHigherOrder()) |
238 |
|
{ |
239 |
|
std::stringstream ss; |
240 |
|
ss << "UF received an application whose operator has higher-order type " |
241 |
|
<< node |
242 |
|
<< ", which is only supported with higher-order logic. Try adding the " |
243 |
|
"logic prefix HO_."; |
244 |
|
throw LogicException(ss.str()); |
245 |
|
} |
246 |
|
} |
247 |
299017 |
return TrustNode::null(); |
248 |
|
} |
249 |
|
|
250 |
396554 |
void TheoryUF::preRegisterTerm(TNode node) |
251 |
|
{ |
252 |
396554 |
Debug("uf") << "TheoryUF::preRegisterTerm(" << node << ")" << std::endl; |
253 |
|
|
254 |
396554 |
if (d_thss != nullptr) |
255 |
|
{ |
256 |
43302 |
d_thss->preRegisterTerm(node); |
257 |
|
} |
258 |
|
|
259 |
|
// we always use APPLY_UF if not higher-order, HO_APPLY if higher-order |
260 |
396554 |
Assert(node.getKind() != kind::HO_APPLY || logicInfo().isHigherOrder()); |
261 |
|
|
262 |
396554 |
Kind k = node.getKind(); |
263 |
396554 |
switch (k) |
264 |
|
{ |
265 |
171300 |
case kind::EQUAL: |
266 |
|
// Add the trigger for equality |
267 |
171300 |
d_equalityEngine->addTriggerPredicate(node); |
268 |
171300 |
break; |
269 |
145137 |
case kind::APPLY_UF: |
270 |
|
case kind::HO_APPLY: |
271 |
|
{ |
272 |
|
// Maybe it's a predicate |
273 |
145137 |
if (node.getType().isBoolean()) |
274 |
|
{ |
275 |
|
// Get triggered for both equal and dis-equal |
276 |
30391 |
d_equalityEngine->addTriggerPredicate(node); |
277 |
|
} |
278 |
|
else |
279 |
|
{ |
280 |
|
// Function applications/predicates |
281 |
114746 |
d_equalityEngine->addTerm(node); |
282 |
|
} |
283 |
|
// Remember the function and predicate terms |
284 |
145137 |
d_functionsTerms.push_back(node); |
285 |
|
} |
286 |
145137 |
break; |
287 |
3789 |
case kind::CARDINALITY_CONSTRAINT: |
288 |
|
case kind::COMBINED_CARDINALITY_CONSTRAINT: |
289 |
|
//do nothing |
290 |
3789 |
break; |
291 |
|
case kind::UNINTERPRETED_CONSTANT: |
292 |
|
{ |
293 |
|
// Uninterpreted constants should only appear in models, and should |
294 |
|
// never appear in constraints. They are unallowed to ever appear in |
295 |
|
// constraints since the cardinality of an uninterpreted sort may have |
296 |
|
// an upper bound, e.g. if (forall ((x U) (y U)) (= x y)) holds, then |
297 |
|
// @uc_U_2 is a ill-formed term, as its existence cannot be assumed. |
298 |
|
// The parser prevents the user from ever constructing uninterpreted |
299 |
|
// constants. However, they may be exported via models to API users. |
300 |
|
// It is thus possible that these uninterpreted constants are asserted |
301 |
|
// back in constraints, hence this check is necessary. |
302 |
|
throw LogicException( |
303 |
|
"An uninterpreted constant was preregistered to the UF theory."); |
304 |
|
} |
305 |
|
break; |
306 |
76328 |
default: |
307 |
|
// Variables etc |
308 |
76328 |
d_equalityEngine->addTerm(node); |
309 |
76328 |
break; |
310 |
|
} |
311 |
396554 |
} |
312 |
|
|
313 |
|
void TheoryUF::explain(TNode literal, Node& exp) |
314 |
|
{ |
315 |
|
Debug("uf") << "TheoryUF::explain(" << literal << ")" << std::endl; |
316 |
|
std::vector<TNode> assumptions; |
317 |
|
// Do the work |
318 |
|
bool polarity = literal.getKind() != kind::NOT; |
319 |
|
TNode atom = polarity ? literal : literal[0]; |
320 |
|
if (atom.getKind() == kind::EQUAL) |
321 |
|
{ |
322 |
|
d_equalityEngine->explainEquality( |
323 |
|
atom[0], atom[1], polarity, assumptions, nullptr); |
324 |
|
} |
325 |
|
else |
326 |
|
{ |
327 |
|
d_equalityEngine->explainPredicate(atom, polarity, assumptions, nullptr); |
328 |
|
} |
329 |
|
exp = mkAnd(assumptions); |
330 |
|
} |
331 |
|
|
332 |
94653 |
TrustNode TheoryUF::explain(TNode literal) { return d_im.explainLit(literal); } |
333 |
|
|
334 |
21866 |
bool TheoryUF::collectModelValues(TheoryModel* m, const std::set<Node>& termSet) |
335 |
|
{ |
336 |
21866 |
if (logicInfo().isHigherOrder()) |
337 |
|
{ |
338 |
|
// must add extensionality disequalities for all pairs of (non-disequal) |
339 |
|
// function equivalence classes. |
340 |
365 |
if (!d_ho->collectModelInfoHo(m, termSet)) |
341 |
|
{ |
342 |
|
Trace("uf") << "Collect model info fail HO" << std::endl; |
343 |
|
return false; |
344 |
|
} |
345 |
|
} |
346 |
|
|
347 |
21866 |
Debug("uf") << "UF : finish collectModelInfo " << std::endl; |
348 |
21866 |
return true; |
349 |
|
} |
350 |
|
|
351 |
20560 |
void TheoryUF::presolve() { |
352 |
|
// TimerStat::CodeTimer codeTimer(d_presolveTimer); |
353 |
|
|
354 |
20560 |
Debug("uf") << "uf: begin presolve()" << endl; |
355 |
20560 |
if(options::ufSymmetryBreaker()) { |
356 |
292 |
vector<Node> newClauses; |
357 |
146 |
d_symb.apply(newClauses); |
358 |
166 |
for(vector<Node>::const_iterator i = newClauses.begin(); |
359 |
166 |
i != newClauses.end(); |
360 |
|
++i) { |
361 |
20 |
Debug("uf") << "uf: generating a lemma: " << *i << std::endl; |
362 |
|
// no proof generator provided |
363 |
20 |
d_im.lemma(*i, InferenceId::UF_BREAK_SYMMETRY); |
364 |
|
} |
365 |
|
} |
366 |
20560 |
if( d_thss ){ |
367 |
346 |
d_thss->presolve(); |
368 |
|
} |
369 |
20560 |
Debug("uf") << "uf: end presolve()" << endl; |
370 |
20560 |
} |
371 |
|
|
372 |
250978 |
void TheoryUF::ppStaticLearn(TNode n, NodeBuilder& learned) |
373 |
|
{ |
374 |
|
//TimerStat::CodeTimer codeTimer(d_staticLearningTimer); |
375 |
|
|
376 |
501956 |
vector<TNode> workList; |
377 |
250978 |
workList.push_back(n); |
378 |
501956 |
std::unordered_set<TNode> processed; |
379 |
|
|
380 |
9235402 |
while(!workList.empty()) { |
381 |
4492212 |
n = workList.back(); |
382 |
|
|
383 |
4515792 |
if (n.isClosure()) |
384 |
|
{ |
385 |
|
// unsafe to go under quantifiers; we might pull bound vars out of scope! |
386 |
23580 |
processed.insert(n); |
387 |
23580 |
workList.pop_back(); |
388 |
23580 |
continue; |
389 |
|
} |
390 |
|
|
391 |
4468632 |
bool unprocessedChildren = false; |
392 |
11605405 |
for(TNode::iterator i = n.begin(), iend = n.end(); i != iend; ++i) { |
393 |
7136773 |
if(processed.find(*i) == processed.end()) { |
394 |
|
// unprocessed child |
395 |
2606127 |
workList.push_back(*i); |
396 |
2606127 |
unprocessedChildren = true; |
397 |
|
} |
398 |
|
} |
399 |
|
|
400 |
4468632 |
if(unprocessedChildren) { |
401 |
1635107 |
continue; |
402 |
|
} |
403 |
|
|
404 |
2833525 |
workList.pop_back(); |
405 |
|
// has node n been processed in the meantime ? |
406 |
2833525 |
if(processed.find(n) != processed.end()) { |
407 |
43987 |
continue; |
408 |
|
} |
409 |
2789538 |
processed.insert(n); |
410 |
|
|
411 |
|
// == DIAMONDS == |
412 |
|
|
413 |
5579076 |
Debug("diamonds") << "===================== looking at" << endl |
414 |
2789538 |
<< n << endl; |
415 |
|
|
416 |
|
// binary OR of binary ANDs of EQUALities |
417 |
5891820 |
if(n.getKind() == kind::OR && n.getNumChildren() == 2 && |
418 |
2971266 |
n[0].getKind() == kind::AND && n[0].getNumChildren() == 2 && |
419 |
2810640 |
n[1].getKind() == kind::AND && n[1].getNumChildren() == 2 && |
420 |
2795948 |
(n[0][0].getKind() == kind::EQUAL) && |
421 |
2792304 |
(n[0][1].getKind() == kind::EQUAL) && |
422 |
8371222 |
(n[1][0].getKind() == kind::EQUAL) && |
423 |
2790842 |
(n[1][1].getKind() == kind::EQUAL)) { |
424 |
|
// now we have (a = b && c = d) || (e = f && g = h) |
425 |
|
|
426 |
640 |
Debug("diamonds") << "has form of a diamond!" << endl; |
427 |
|
|
428 |
|
TNode |
429 |
1256 |
a = n[0][0][0], b = n[0][0][1], |
430 |
1256 |
c = n[0][1][0], d = n[0][1][1], |
431 |
1256 |
e = n[1][0][0], f = n[1][0][1], |
432 |
1256 |
g = n[1][1][0], h = n[1][1][1]; |
433 |
|
|
434 |
|
// test that one of {a, b} = one of {c, d}, and make "b" the |
435 |
|
// shared node (i.e. put in the form (a = b && b = d)) |
436 |
|
// note we don't actually care about the shared ones, so the |
437 |
|
// "swaps" below are one-sided, ignoring b and c |
438 |
640 |
if(a == c) { |
439 |
16 |
a = b; |
440 |
624 |
} else if(a == d) { |
441 |
442 |
a = b; |
442 |
442 |
d = c; |
443 |
182 |
} else if(b == c) { |
444 |
|
// nothing to do |
445 |
64 |
} else if(b == d) { |
446 |
20 |
d = c; |
447 |
|
} else { |
448 |
|
// condition not satisfied |
449 |
22 |
Debug("diamonds") << "+ A fails" << endl; |
450 |
22 |
continue; |
451 |
|
} |
452 |
|
|
453 |
618 |
Debug("diamonds") << "+ A holds" << endl; |
454 |
|
|
455 |
|
// same: one of {e, f} = one of {g, h}, and make "f" the |
456 |
|
// shared node (i.e. put in the form (e = f && f = h)) |
457 |
618 |
if(e == g) { |
458 |
16 |
e = f; |
459 |
602 |
} else if(e == h) { |
460 |
454 |
e = f; |
461 |
454 |
h = g; |
462 |
148 |
} else if(f == g) { |
463 |
|
// nothing to do |
464 |
10 |
} else if(f == h) { |
465 |
6 |
h = g; |
466 |
|
} else { |
467 |
|
// condition not satisfied |
468 |
2 |
Debug("diamonds") << "+ B fails" << endl; |
469 |
2 |
continue; |
470 |
|
} |
471 |
|
|
472 |
616 |
Debug("diamonds") << "+ B holds" << endl; |
473 |
|
|
474 |
|
// now we have (a = b && b = d) || (e = f && f = h) |
475 |
|
// test that {a, d} == {e, h} |
476 |
1232 |
if( (a == e && d == h) || |
477 |
|
(a == h && d == e) ) { |
478 |
|
// learn: n implies a == d |
479 |
616 |
Debug("diamonds") << "+ C holds" << endl; |
480 |
1232 |
Node newEquality = a.eqNode(d); |
481 |
616 |
Debug("diamonds") << " ==> " << newEquality << endl; |
482 |
616 |
learned << n.impNode(newEquality); |
483 |
|
} else { |
484 |
|
Debug("diamonds") << "+ C fails" << endl; |
485 |
|
} |
486 |
|
} |
487 |
|
} |
488 |
|
|
489 |
250978 |
if(options::ufSymmetryBreaker()) { |
490 |
31222 |
d_symb.assertFormula(n); |
491 |
|
} |
492 |
250978 |
} /* TheoryUF::ppStaticLearn() */ |
493 |
|
|
494 |
2403 |
EqualityStatus TheoryUF::getEqualityStatus(TNode a, TNode b) { |
495 |
|
|
496 |
|
// Check for equality (simplest) |
497 |
2403 |
if (d_equalityEngine->areEqual(a, b)) |
498 |
|
{ |
499 |
|
// The terms are implied to be equal |
500 |
|
return EQUALITY_TRUE; |
501 |
|
} |
502 |
|
|
503 |
|
// Check for disequality |
504 |
2403 |
if (d_equalityEngine->areDisequal(a, b, false)) |
505 |
|
{ |
506 |
|
// The terms are implied to be dis-equal |
507 |
|
return EQUALITY_FALSE; |
508 |
|
} |
509 |
|
|
510 |
|
// All other terms we interpret as dis-equal in the model |
511 |
2403 |
return EQUALITY_FALSE_IN_MODEL; |
512 |
|
} |
513 |
|
|
514 |
1451123 |
bool TheoryUF::areCareDisequal(TNode x, TNode y) |
515 |
|
{ |
516 |
4353369 |
if (d_equalityEngine->isTriggerTerm(x, THEORY_UF) |
517 |
4353369 |
&& d_equalityEngine->isTriggerTerm(y, THEORY_UF)) |
518 |
|
{ |
519 |
|
TNode x_shared = |
520 |
1416615 |
d_equalityEngine->getTriggerTermRepresentative(x, THEORY_UF); |
521 |
|
TNode y_shared = |
522 |
1416615 |
d_equalityEngine->getTriggerTermRepresentative(y, THEORY_UF); |
523 |
1166594 |
EqualityStatus eqStatus = d_valuation.getEqualityStatus(x_shared, y_shared); |
524 |
1166594 |
if( eqStatus==EQUALITY_FALSE_AND_PROPAGATED || eqStatus==EQUALITY_FALSE || eqStatus==EQUALITY_FALSE_IN_MODEL ){ |
525 |
916573 |
return true; |
526 |
|
} |
527 |
|
} |
528 |
534550 |
return false; |
529 |
|
} |
530 |
|
|
531 |
438552 |
void TheoryUF::addCarePairs(const TNodeTrie* t1, |
532 |
|
const TNodeTrie* t2, |
533 |
|
unsigned arity, |
534 |
|
unsigned depth) |
535 |
|
{ |
536 |
|
// Note we use d_state instead of d_equalityEngine in this method in several |
537 |
|
// places to be robust to cases where the tries have terms that do not |
538 |
|
// exist in the equality engine, which can be the case if higher order. |
539 |
438552 |
if( depth==arity ){ |
540 |
131230 |
if( t2!=NULL ){ |
541 |
262460 |
Node f1 = t1->getData(); |
542 |
262460 |
Node f2 = t2->getData(); |
543 |
131230 |
if (!d_state.areEqual(f1, f2)) |
544 |
|
{ |
545 |
121356 |
Debug("uf::sharing") << "TheoryUf::computeCareGraph(): checking function " << f1 << " and " << f2 << std::endl; |
546 |
242712 |
vector< pair<TNode, TNode> > currentPairs; |
547 |
326152 |
for (size_t k = 0, nchildren = f1.getNumChildren(); k < nchildren; ++k) |
548 |
|
{ |
549 |
409592 |
TNode x = f1[k]; |
550 |
409592 |
TNode y = f2[k]; |
551 |
204796 |
Assert(!d_state.areDisequal(x, y)); |
552 |
204796 |
Assert(!areCareDisequal(x, y)); |
553 |
204796 |
if (!d_state.areEqual(x, y)) |
554 |
|
{ |
555 |
380913 |
if (d_equalityEngine->isTriggerTerm(x, THEORY_UF) |
556 |
380913 |
&& d_equalityEngine->isTriggerTerm(y, THEORY_UF)) |
557 |
|
{ |
558 |
|
TNode x_shared = |
559 |
87064 |
d_equalityEngine->getTriggerTermRepresentative(x, THEORY_UF); |
560 |
|
TNode y_shared = |
561 |
87064 |
d_equalityEngine->getTriggerTermRepresentative(y, THEORY_UF); |
562 |
43532 |
currentPairs.push_back(make_pair(x_shared, y_shared)); |
563 |
|
} |
564 |
|
} |
565 |
|
} |
566 |
164888 |
for (unsigned c = 0; c < currentPairs.size(); ++ c) { |
567 |
43532 |
Debug("uf::sharing") << "TheoryUf::computeCareGraph(): adding to care-graph" << std::endl; |
568 |
43532 |
addCarePair(currentPairs[c].first, currentPairs[c].second); |
569 |
|
} |
570 |
|
} |
571 |
|
} |
572 |
|
}else{ |
573 |
307322 |
if( t2==NULL ){ |
574 |
108798 |
if( depth<(arity-1) ){ |
575 |
|
//add care pairs internal to each child |
576 |
125962 |
for (const std::pair<const TNode, TNodeTrie>& tt : t1->d_data) |
577 |
|
{ |
578 |
72637 |
addCarePairs(&tt.second, NULL, arity, depth + 1); |
579 |
|
} |
580 |
|
} |
581 |
|
//add care pairs based on each pair of non-disequal arguments |
582 |
537197 |
for (std::map<TNode, TNodeTrie>::const_iterator it = t1->d_data.begin(); |
583 |
537197 |
it != t1->d_data.end(); |
584 |
|
++it) |
585 |
|
{ |
586 |
428399 |
std::map<TNode, TNodeTrie>::const_iterator it2 = it; |
587 |
428399 |
++it2; |
588 |
5630729 |
for( ; it2 != t1->d_data.end(); ++it2 ){ |
589 |
2601165 |
if (!d_state.areDisequal(it->first, it2->first)) |
590 |
|
{ |
591 |
939208 |
if( !areCareDisequal(it->first, it2->first) ){ |
592 |
242532 |
addCarePairs( &it->second, &it2->second, arity, depth+1 ); |
593 |
|
} |
594 |
|
} |
595 |
|
} |
596 |
|
} |
597 |
|
}else{ |
598 |
|
//add care pairs based on product of indices, non-disequal arguments |
599 |
496398 |
for (const std::pair<const TNode, TNodeTrie>& tt1 : t1->d_data) |
600 |
|
{ |
601 |
617641 |
for (const std::pair<const TNode, TNodeTrie>& tt2 : t2->d_data) |
602 |
|
{ |
603 |
319767 |
if (!d_state.areDisequal(tt1.first, tt2.first)) |
604 |
|
{ |
605 |
307119 |
if (!areCareDisequal(tt1.first, tt2.first)) |
606 |
|
{ |
607 |
87222 |
addCarePairs(&tt1.second, &tt2.second, arity, depth + 1); |
608 |
|
} |
609 |
|
} |
610 |
|
} |
611 |
|
} |
612 |
|
} |
613 |
|
} |
614 |
438552 |
} |
615 |
|
|
616 |
32872 |
void TheoryUF::computeCareGraph() { |
617 |
32872 |
if (d_sharedTerms.empty()) |
618 |
|
{ |
619 |
11613 |
return; |
620 |
|
} |
621 |
21259 |
NodeManager* nm = NodeManager::currentNM(); |
622 |
|
// Use term indexing. We build separate indices for APPLY_UF and HO_APPLY. |
623 |
|
// We maintain indices per operator for the former, and indices per |
624 |
|
// function type for the latter. |
625 |
42518 |
Debug("uf::sharing") << "TheoryUf::computeCareGraph(): Build term indices..." |
626 |
21259 |
<< std::endl; |
627 |
|
// temporary keep set for higher-order indexing below |
628 |
42518 |
std::vector<Node> keep; |
629 |
42518 |
std::map<Node, TNodeTrie> index; |
630 |
42518 |
std::map<TypeNode, TNodeTrie> hoIndex; |
631 |
42518 |
std::map<Node, size_t> arity; |
632 |
450030 |
for (TNode app : d_functionsTerms) |
633 |
|
{ |
634 |
857542 |
std::vector<TNode> reps; |
635 |
428771 |
bool has_trigger_arg = false; |
636 |
998959 |
for (const Node& j : app) |
637 |
|
{ |
638 |
570188 |
reps.push_back(d_equalityEngine->getRepresentative(j)); |
639 |
570188 |
if (d_equalityEngine->isTriggerTerm(j, THEORY_UF)) |
640 |
|
{ |
641 |
498560 |
has_trigger_arg = true; |
642 |
|
} |
643 |
|
} |
644 |
428771 |
if (has_trigger_arg) |
645 |
|
{ |
646 |
387841 |
if (app.getKind() == kind::APPLY_UF) |
647 |
|
{ |
648 |
775598 |
Node op = app.getOperator(); |
649 |
387799 |
index[op].addTerm(app, reps); |
650 |
387799 |
arity[op] = reps.size(); |
651 |
387799 |
if (logicInfo().isHigherOrder() && d_equalityEngine->hasTerm(op)) |
652 |
|
{ |
653 |
|
// Since we use a lazy app-completion scheme for equating fully |
654 |
|
// and partially applied versions of terms, we must add all |
655 |
|
// sub-chains to the HO index if the operator of this term occurs |
656 |
|
// in a higher-order context in the equality engine. In other words, |
657 |
|
// for (f a b c), this will add the terms: |
658 |
|
// (HO_APPLY f a), (HO_APPLY (HO_APPLY f a) b), |
659 |
|
// (HO_APPLY (HO_APPLY (HO_APPLY f a) b) c) to the higher-order |
660 |
|
// term index for consideration when computing care pairs. |
661 |
922 |
Node curr = op; |
662 |
1211 |
for (const Node& c : app) |
663 |
|
{ |
664 |
1500 |
Node happ = nm->mkNode(kind::HO_APPLY, curr, c); |
665 |
750 |
hoIndex[curr.getType()].addTerm(happ, {curr, c}); |
666 |
750 |
curr = happ; |
667 |
750 |
keep.push_back(happ); |
668 |
|
} |
669 |
|
} |
670 |
|
} |
671 |
|
else |
672 |
|
{ |
673 |
42 |
Assert(app.getKind() == kind::HO_APPLY); |
674 |
|
// add it to the hoIndex for the function type |
675 |
42 |
hoIndex[app[0].getType()].addTerm(app, reps); |
676 |
|
} |
677 |
|
} |
678 |
|
} |
679 |
|
// for each index |
680 |
57146 |
for (std::pair<const Node, TNodeTrie>& tt : index) |
681 |
|
{ |
682 |
71774 |
Debug("uf::sharing") << "TheoryUf::computeCareGraph(): Process index " |
683 |
35887 |
<< tt.first << "..." << std::endl; |
684 |
35887 |
Assert(arity.find(tt.first) != arity.end()); |
685 |
35887 |
addCarePairs(&tt.second, nullptr, arity[tt.first], 0); |
686 |
|
} |
687 |
21533 |
for (std::pair<const TypeNode, TNodeTrie>& tt : hoIndex) |
688 |
|
{ |
689 |
548 |
Debug("uf::sharing") << "TheoryUf::computeCareGraph(): Process ho index " |
690 |
274 |
<< tt.first << "..." << std::endl; |
691 |
|
// the arity of HO_APPLY is always two |
692 |
274 |
addCarePairs(&tt.second, nullptr, 2, 0); |
693 |
|
} |
694 |
42518 |
Debug("uf::sharing") << "TheoryUf::computeCareGraph(): finished." |
695 |
21259 |
<< std::endl; |
696 |
|
}/* TheoryUF::computeCareGraph() */ |
697 |
|
|
698 |
314409 |
void TheoryUF::eqNotifyNewClass(TNode t) { |
699 |
314409 |
if (d_thss != NULL) { |
700 |
51426 |
d_thss->newEqClass(t); |
701 |
|
} |
702 |
314409 |
} |
703 |
|
|
704 |
4365785 |
void TheoryUF::eqNotifyMerge(TNode t1, TNode t2) |
705 |
|
{ |
706 |
4365785 |
if (d_thss != NULL) { |
707 |
275059 |
d_thss->merge(t1, t2); |
708 |
|
} |
709 |
4365785 |
} |
710 |
|
|
711 |
922432 |
void TheoryUF::eqNotifyDisequal(TNode t1, TNode t2, TNode reason) { |
712 |
922432 |
if (d_thss != NULL) { |
713 |
20681 |
d_thss->assertDisequal(t1, t2, reason); |
714 |
|
} |
715 |
922432 |
} |
716 |
|
|
717 |
99666 |
bool TheoryUF::isHigherOrderType(TypeNode tn) |
718 |
|
{ |
719 |
99666 |
Assert(tn.isFunction()); |
720 |
99666 |
std::map<TypeNode, bool>::iterator it = d_isHoType.find(tn); |
721 |
99666 |
if (it != d_isHoType.end()) |
722 |
|
{ |
723 |
94435 |
return it->second; |
724 |
|
} |
725 |
5231 |
bool ret = false; |
726 |
10462 |
const std::vector<TypeNode>& argTypes = tn.getArgTypes(); |
727 |
14846 |
for (const TypeNode& tnc : argTypes) |
728 |
|
{ |
729 |
9689 |
if (tnc.isFunction()) |
730 |
|
{ |
731 |
74 |
ret = true; |
732 |
74 |
break; |
733 |
|
} |
734 |
|
} |
735 |
5231 |
d_isHoType[tn] = ret; |
736 |
5231 |
return ret; |
737 |
|
} |
738 |
|
|
739 |
|
} // namespace uf |
740 |
|
} // namespace theory |
741 |
31137 |
} // namespace cvc5 |