1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Andres Noetzli, Tianyi Liang |
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 |
|
* Base solver for the theory of strings. This class implements term |
14 |
|
* indexing and constant inference for the theory of strings. |
15 |
|
*/ |
16 |
|
|
17 |
|
#include "theory/strings/base_solver.h" |
18 |
|
|
19 |
|
#include "expr/sequence.h" |
20 |
|
#include "options/strings_options.h" |
21 |
|
#include "theory/rewriter.h" |
22 |
|
#include "theory/strings/theory_strings_utils.h" |
23 |
|
#include "theory/strings/word.h" |
24 |
|
#include "util/rational.h" |
25 |
|
|
26 |
|
using namespace std; |
27 |
|
using namespace cvc5::context; |
28 |
|
using namespace cvc5::kind; |
29 |
|
|
30 |
|
namespace cvc5 { |
31 |
|
namespace theory { |
32 |
|
namespace strings { |
33 |
|
|
34 |
9917 |
BaseSolver::BaseSolver(SolverState& s, InferenceManager& im) |
35 |
9917 |
: d_state(s), d_im(im), d_congruent(s.getSatContext()) |
36 |
|
{ |
37 |
9917 |
d_false = NodeManager::currentNM()->mkConst(false); |
38 |
9917 |
d_cardSize = utils::getAlphabetCardinality(); |
39 |
9917 |
} |
40 |
|
|
41 |
9914 |
BaseSolver::~BaseSolver() {} |
42 |
|
|
43 |
41661 |
void BaseSolver::checkInit() |
44 |
|
{ |
45 |
|
// build term index |
46 |
41661 |
d_eqcInfo.clear(); |
47 |
41661 |
d_termIndex.clear(); |
48 |
41661 |
d_stringsEqc.clear(); |
49 |
|
|
50 |
41661 |
Trace("strings-base") << "BaseSolver::checkInit" << std::endl; |
51 |
|
// count of congruent, non-congruent per operator (independent of type), |
52 |
|
// for debugging. |
53 |
83322 |
std::map<Kind, std::pair<uint32_t, uint32_t>> congruentCount; |
54 |
41661 |
eq::EqualityEngine* ee = d_state.getEqualityEngine(); |
55 |
41661 |
eq::EqClassesIterator eqcs_i = eq::EqClassesIterator(ee); |
56 |
4474045 |
while (!eqcs_i.isFinished()) |
57 |
|
{ |
58 |
4432384 |
Node eqc = (*eqcs_i); |
59 |
4432384 |
TypeNode tn = eqc.getType(); |
60 |
2216192 |
if (!tn.isRegExp()) |
61 |
|
{ |
62 |
4043922 |
Node emps; |
63 |
|
// get the term index for type tn |
64 |
2021961 |
std::map<Kind, TermIndex>& tti = d_termIndex[tn]; |
65 |
2021961 |
if (tn.isStringLike()) |
66 |
|
{ |
67 |
773810 |
d_stringsEqc.push_back(eqc); |
68 |
773810 |
emps = Word::mkEmptyWord(tn); |
69 |
|
} |
70 |
4043922 |
Node var; |
71 |
2021961 |
eq::EqClassIterator eqc_i = eq::EqClassIterator(eqc, ee); |
72 |
27514729 |
while (!eqc_i.isFinished()) |
73 |
|
{ |
74 |
25492768 |
Node n = *eqc_i; |
75 |
12746384 |
Kind k = n.getKind(); |
76 |
12746384 |
Trace("strings-base") << "initialize term: " << n << std::endl; |
77 |
|
// process constant-like terms |
78 |
12746384 |
if (utils::isConstantLike(n)) |
79 |
|
{ |
80 |
1073298 |
Node prev = d_eqcInfo[eqc].d_bestContent; |
81 |
536649 |
if (!prev.isNull()) |
82 |
|
{ |
83 |
|
// we have either (seq.unit x) = C, or (seq.unit x) = (seq.unit y) |
84 |
|
// where C is a sequence constant. |
85 |
|
Node cval = |
86 |
258 |
prev.isConst() ? prev : (n.isConst() ? n : Node::null()); |
87 |
258 |
std::vector<Node> exp; |
88 |
129 |
exp.push_back(prev.eqNode(n)); |
89 |
258 |
Node s, t; |
90 |
129 |
if (cval.isNull()) |
91 |
|
{ |
92 |
|
// injectivity of seq.unit |
93 |
20 |
s = prev[0]; |
94 |
20 |
t = n[0]; |
95 |
|
} |
96 |
|
else |
97 |
|
{ |
98 |
|
// should not have two constants in the same equivalence class |
99 |
109 |
Assert(cval.getType().isSequence()); |
100 |
218 |
std::vector<Node> cchars = Word::getChars(cval); |
101 |
109 |
if (cchars.size() == 1) |
102 |
|
{ |
103 |
218 |
Node oval = prev.isConst() ? n : prev; |
104 |
109 |
Assert(oval.getKind() == SEQ_UNIT); |
105 |
109 |
s = oval[0]; |
106 |
109 |
t = cchars[0].getConst<Sequence>().getVec()[0]; |
107 |
|
// oval is congruent (ignored) in this context |
108 |
109 |
d_congruent.insert(oval); |
109 |
|
} |
110 |
|
else |
111 |
|
{ |
112 |
|
// (seq.unit x) = C => false if |C| != 1. |
113 |
|
d_im.sendInference( |
114 |
|
exp, d_false, InferenceId::STRINGS_UNIT_CONST_CONFLICT); |
115 |
|
return; |
116 |
|
} |
117 |
|
} |
118 |
129 |
if (!d_state.areEqual(s, t)) |
119 |
|
{ |
120 |
|
// (seq.unit x) = (seq.unit y) => x=y, or |
121 |
|
// (seq.unit x) = (seq.unit c) => x=c |
122 |
32 |
Assert(s.getType() == t.getType()); |
123 |
32 |
d_im.sendInference(exp, s.eqNode(t), InferenceId::STRINGS_UNIT_INJ); |
124 |
|
} |
125 |
|
} |
126 |
|
// update best content |
127 |
536649 |
if (prev.isNull() || n.isConst()) |
128 |
|
{ |
129 |
536520 |
d_eqcInfo[eqc].d_bestContent = n; |
130 |
536520 |
d_eqcInfo[eqc].d_bestScore = 0; |
131 |
536520 |
d_eqcInfo[eqc].d_base = n; |
132 |
536520 |
d_eqcInfo[eqc].d_exp = Node::null(); |
133 |
|
} |
134 |
|
} |
135 |
12746384 |
if (tn.isInteger()) |
136 |
|
{ |
137 |
|
// do nothing |
138 |
|
} |
139 |
|
// process indexing |
140 |
9715273 |
else if (n.getNumChildren() > 0) |
141 |
|
{ |
142 |
7193190 |
if (k != EQUAL) |
143 |
|
{ |
144 |
1630484 |
if (d_congruent.find(n) == d_congruent.end()) |
145 |
|
{ |
146 |
2531592 |
std::vector<Node> c; |
147 |
2531592 |
Node nc = tti[k].add(n, 0, d_state, emps, c); |
148 |
1265796 |
if (nc != n) |
149 |
|
{ |
150 |
233016 |
Trace("strings-base-debug") |
151 |
116508 |
<< "...found congruent term " << nc << std::endl; |
152 |
|
// check if we have inferred a new equality by removal of empty |
153 |
|
// components |
154 |
116508 |
if (k == STRING_CONCAT && !d_state.areEqual(nc, n)) |
155 |
|
{ |
156 |
14654 |
std::vector<Node> exp; |
157 |
|
// the number of empty components of n, nc |
158 |
7327 |
size_t count[2] = {0, 0}; |
159 |
45493 |
while (count[0] < nc.getNumChildren() |
160 |
26410 |
|| count[1] < n.getNumChildren()) |
161 |
|
{ |
162 |
|
// explain empty prefixes |
163 |
57249 |
for (unsigned t = 0; t < 2; t++) |
164 |
|
{ |
165 |
76332 |
Node nn = t == 0 ? nc : n; |
166 |
59762 |
while (count[t] < nn.getNumChildren() |
167 |
157690 |
&& (nn[count[t]] == emps |
168 |
87854 |
|| d_state.areEqual(nn[count[t]], emps))) |
169 |
|
{ |
170 |
10798 |
if (nn[count[t]] != emps) |
171 |
|
{ |
172 |
10798 |
exp.push_back(nn[count[t]].eqNode(emps)); |
173 |
|
} |
174 |
10798 |
count[t]++; |
175 |
|
} |
176 |
|
} |
177 |
38166 |
Trace("strings-base-debug") |
178 |
19083 |
<< " counts = " << count[0] << ", " << count[1] |
179 |
19083 |
<< std::endl; |
180 |
|
// explain equal components |
181 |
19083 |
if (count[0] < nc.getNumChildren()) |
182 |
|
{ |
183 |
14046 |
Assert(count[1] < n.getNumChildren()); |
184 |
14046 |
if (nc[count[0]] != n[count[1]]) |
185 |
|
{ |
186 |
271 |
exp.push_back(nc[count[0]].eqNode(n[count[1]])); |
187 |
|
} |
188 |
14046 |
count[0]++; |
189 |
14046 |
count[1]++; |
190 |
|
} |
191 |
|
} |
192 |
|
// infer the equality |
193 |
7327 |
d_im.sendInference(exp, n.eqNode(nc), InferenceId::STRINGS_I_NORM); |
194 |
|
} |
195 |
|
else |
196 |
|
{ |
197 |
|
// We cannot mark one of the terms as reduced here (via |
198 |
|
// ExtTheory::markCongruent) since extended function terms |
199 |
|
// rely on reductions to other extended function terms. We |
200 |
|
// may have a pair of extended function terms f(a)=f(b) where |
201 |
|
// the reduction of argument a depends on the term b. |
202 |
|
// Thus, marking f(b) as reduced by virtue of the fact we |
203 |
|
// have f(a) is incorrect, since then we are effectively |
204 |
|
// assuming that the reduction of f(a) depends on itself. |
205 |
|
} |
206 |
|
// this node is congruent to another one, we can ignore it |
207 |
233016 |
Trace("strings-base-debug") |
208 |
116508 |
<< " congruent term : " << n << " (via " << nc << ")" |
209 |
116508 |
<< std::endl; |
210 |
116508 |
d_congruent.insert(n); |
211 |
116508 |
congruentCount[k].first++; |
212 |
|
} |
213 |
1149288 |
else if (k == STRING_CONCAT && c.size() == 1) |
214 |
|
{ |
215 |
71394 |
Trace("strings-base-debug") |
216 |
35697 |
<< " congruent term by singular : " << n << " " << c[0] |
217 |
35697 |
<< std::endl; |
218 |
|
// singular case |
219 |
35697 |
if (!d_state.areEqual(c[0], n)) |
220 |
|
{ |
221 |
28744 |
Node ns; |
222 |
28744 |
std::vector<Node> exp; |
223 |
|
// explain empty components |
224 |
14372 |
bool foundNEmpty = false; |
225 |
45501 |
for (const Node& nnc : n) |
226 |
|
{ |
227 |
31129 |
if (d_state.areEqual(nnc, emps)) |
228 |
|
{ |
229 |
16757 |
if (nnc != emps) |
230 |
|
{ |
231 |
16757 |
exp.push_back(nnc.eqNode(emps)); |
232 |
|
} |
233 |
|
} |
234 |
|
else |
235 |
|
{ |
236 |
14372 |
Assert(!foundNEmpty); |
237 |
14372 |
ns = nnc; |
238 |
14372 |
foundNEmpty = true; |
239 |
|
} |
240 |
|
} |
241 |
14372 |
AlwaysAssert(foundNEmpty); |
242 |
|
// infer the equality |
243 |
14372 |
d_im.sendInference(exp, n.eqNode(ns), InferenceId::STRINGS_I_NORM_S); |
244 |
|
} |
245 |
35697 |
d_congruent.insert(n); |
246 |
35697 |
congruentCount[k].first++; |
247 |
|
} |
248 |
|
else |
249 |
|
{ |
250 |
1113591 |
congruentCount[k].second++; |
251 |
|
} |
252 |
|
} |
253 |
|
else |
254 |
|
{ |
255 |
364688 |
congruentCount[k].first++; |
256 |
|
} |
257 |
|
} |
258 |
|
} |
259 |
2522083 |
else if (!n.isConst()) |
260 |
|
{ |
261 |
2208272 |
if (d_congruent.find(n) == d_congruent.end()) |
262 |
|
{ |
263 |
|
// We mark all but the oldest variable in the equivalence class as |
264 |
|
// congruent. |
265 |
1159310 |
if (var.isNull()) |
266 |
|
{ |
267 |
763826 |
var = n; |
268 |
|
} |
269 |
395484 |
else if (var > n) |
270 |
|
{ |
271 |
422854 |
Trace("strings-base-debug") |
272 |
211427 |
<< " congruent variable : " << var << std::endl; |
273 |
211427 |
d_congruent.insert(var); |
274 |
211427 |
var = n; |
275 |
|
} |
276 |
|
else |
277 |
|
{ |
278 |
368114 |
Trace("strings-base-debug") |
279 |
184057 |
<< " congruent variable : " << n << std::endl; |
280 |
184057 |
d_congruent.insert(n); |
281 |
|
} |
282 |
|
} |
283 |
|
} |
284 |
12746384 |
++eqc_i; |
285 |
|
} |
286 |
|
} |
287 |
2216192 |
++eqcs_i; |
288 |
|
} |
289 |
41661 |
if (Trace.isOn("strings-base")) |
290 |
|
{ |
291 |
|
for (const std::pair<const Kind, std::pair<uint32_t, uint32_t>>& cc : |
292 |
|
congruentCount) |
293 |
|
{ |
294 |
|
Trace("strings-base") |
295 |
|
<< " Terms[" << cc.first << "] = " << cc.second.second << "/" |
296 |
|
<< (cc.second.first + cc.second.second) << std::endl; |
297 |
|
} |
298 |
|
} |
299 |
41661 |
Trace("strings-base") << "BaseSolver::checkInit finished" << std::endl; |
300 |
|
} |
301 |
|
|
302 |
34790 |
void BaseSolver::checkConstantEquivalenceClasses() |
303 |
|
{ |
304 |
|
// do fixed point |
305 |
34790 |
size_t prevSize = 0; |
306 |
69580 |
std::vector<Node> vecc; |
307 |
|
do |
308 |
|
{ |
309 |
34790 |
vecc.clear(); |
310 |
69580 |
Trace("strings-base-debug") |
311 |
34790 |
<< "Check constant equivalence classes..." << std::endl; |
312 |
34790 |
prevSize = d_eqcInfo.size(); |
313 |
92338 |
for (std::pair<const TypeNode, std::map<Kind, TermIndex>>& tindex : |
314 |
34790 |
d_termIndex) |
315 |
|
{ |
316 |
92338 |
checkConstantEquivalenceClasses( |
317 |
184676 |
&tindex.second[STRING_CONCAT], vecc, true); |
318 |
|
} |
319 |
34790 |
} while (!d_im.hasProcessed() && d_eqcInfo.size() > prevSize); |
320 |
|
|
321 |
34790 |
if (!d_im.hasProcessed()) |
322 |
|
{ |
323 |
|
// now, go back and set "most content" terms |
324 |
34790 |
vecc.clear(); |
325 |
92338 |
for (std::pair<const TypeNode, std::map<Kind, TermIndex>>& tindex : |
326 |
34790 |
d_termIndex) |
327 |
|
{ |
328 |
92338 |
checkConstantEquivalenceClasses( |
329 |
184676 |
&tindex.second[STRING_CONCAT], vecc, false); |
330 |
|
} |
331 |
|
} |
332 |
34790 |
} |
333 |
|
|
334 |
1018077 |
void BaseSolver::checkConstantEquivalenceClasses(TermIndex* ti, |
335 |
|
std::vector<Node>& vecc, |
336 |
|
bool ensureConst, |
337 |
|
bool isConst) |
338 |
|
{ |
339 |
2036154 |
Node n = ti->d_data; |
340 |
1018077 |
if (!n.isNull()) |
341 |
|
{ |
342 |
|
// construct the constant if applicable |
343 |
988650 |
Node c; |
344 |
494325 |
if (isConst) |
345 |
|
{ |
346 |
109780 |
c = utils::mkNConcat(vecc, n.getType()); |
347 |
|
} |
348 |
494325 |
if (!isConst || !d_state.areEqual(n, c)) |
349 |
|
{ |
350 |
384545 |
if (Trace.isOn("strings-debug")) |
351 |
|
{ |
352 |
|
Trace("strings-debug") |
353 |
|
<< "Constant eqc : " << c << " for " << n << std::endl; |
354 |
|
Trace("strings-debug") << " "; |
355 |
|
for (const Node& v : vecc) |
356 |
|
{ |
357 |
|
Trace("strings-debug") << v << " "; |
358 |
|
} |
359 |
|
Trace("strings-debug") << std::endl; |
360 |
|
} |
361 |
384545 |
size_t countc = 0; |
362 |
769090 |
std::vector<Node> exp; |
363 |
|
// non-constant vector |
364 |
769090 |
std::vector<Node> vecnc; |
365 |
384545 |
size_t contentSize = 0; |
366 |
1333707 |
for (size_t count = 0, nchild = n.getNumChildren(); count < nchild; |
367 |
|
++count) |
368 |
|
{ |
369 |
|
// Add explanations for the empty children |
370 |
1031266 |
Node emps; |
371 |
984207 |
if (d_state.isEqualEmptyWord(n[count], emps)) |
372 |
|
{ |
373 |
35045 |
d_im.addToExplanation(n[count], emps, exp); |
374 |
35045 |
continue; |
375 |
|
} |
376 |
1746130 |
else if (vecc[countc].isNull()) |
377 |
|
{ |
378 |
832013 |
Assert(!isConst); |
379 |
|
// no constant for this component, leave it as is |
380 |
832013 |
vecnc.push_back(n[count]); |
381 |
832013 |
continue; |
382 |
|
} |
383 |
|
// if we are not entirely a constant |
384 |
82104 |
if (!isConst) |
385 |
|
{ |
386 |
|
// use the constant component |
387 |
82104 |
vecnc.push_back(vecc[countc]); |
388 |
82104 |
Assert(vecc[countc].isConst()); |
389 |
82104 |
contentSize += Word::getLength(vecc[countc]); |
390 |
|
} |
391 |
164208 |
Trace("strings-debug") |
392 |
82104 |
<< "...explain " << n[count] << " " << vecc[countc] << std::endl; |
393 |
82104 |
if (!d_state.areEqual(n[count], vecc[countc])) |
394 |
|
{ |
395 |
|
Node nrr = d_state.getRepresentative(n[count]); |
396 |
|
Assert(!d_eqcInfo[nrr].d_bestContent.isNull() |
397 |
|
&& d_eqcInfo[nrr].d_bestContent.isConst()); |
398 |
|
// must flatten to avoid nested AND in explanations |
399 |
|
utils::flattenOp(AND, d_eqcInfo[nrr].d_exp, exp); |
400 |
|
// now explain equality to base |
401 |
|
d_im.addToExplanation(n[count], d_eqcInfo[nrr].d_base, exp); |
402 |
|
} |
403 |
|
else |
404 |
|
{ |
405 |
82104 |
d_im.addToExplanation(n[count], vecc[countc], exp); |
406 |
|
} |
407 |
82104 |
countc++; |
408 |
|
} |
409 |
|
// exp contains an explanation of n==c |
410 |
384545 |
Assert(!isConst || countc == vecc.size()); |
411 |
384545 |
if (!isConst) |
412 |
|
{ |
413 |
|
// no use storing something with no content |
414 |
384545 |
if (contentSize > 0) |
415 |
|
{ |
416 |
145528 |
Node nr = d_state.getRepresentative(n); |
417 |
72764 |
BaseEqcInfo& bei = d_eqcInfo[nr]; |
418 |
145528 |
if (!bei.d_bestContent.isConst() |
419 |
72764 |
&& (bei.d_bestContent.isNull() || contentSize > bei.d_bestScore)) |
420 |
|
{ |
421 |
|
// The equivalence class is not entailed to be equal to a constant |
422 |
|
// and we found a better concatenation |
423 |
121350 |
Node nct = utils::mkNConcat(vecnc, n.getType()); |
424 |
60675 |
Assert(!nct.isConst()); |
425 |
60675 |
bei.d_bestContent = nct; |
426 |
60675 |
bei.d_bestScore = contentSize; |
427 |
60675 |
bei.d_base = n; |
428 |
60675 |
if (!exp.empty()) |
429 |
|
{ |
430 |
26300 |
bei.d_exp = utils::mkAnd(exp); |
431 |
|
} |
432 |
121350 |
Trace("strings-debug") |
433 |
60675 |
<< "Set eqc best content " << n << " to " << nct |
434 |
60675 |
<< ", explanation = " << bei.d_exp << std::endl; |
435 |
|
} |
436 |
|
} |
437 |
|
} |
438 |
|
else if (d_state.hasTerm(c)) |
439 |
|
{ |
440 |
|
d_im.sendInference(exp, n.eqNode(c), InferenceId::STRINGS_I_CONST_MERGE); |
441 |
|
return; |
442 |
|
} |
443 |
|
else if (!d_im.hasProcessed()) |
444 |
|
{ |
445 |
|
Node nr = d_state.getRepresentative(n); |
446 |
|
BaseEqcInfo& bei = d_eqcInfo[nr]; |
447 |
|
if (!bei.d_bestContent.isConst()) |
448 |
|
{ |
449 |
|
bei.d_bestContent = c; |
450 |
|
bei.d_base = n; |
451 |
|
bei.d_exp = utils::mkAnd(exp); |
452 |
|
Trace("strings-debug") |
453 |
|
<< "Set eqc const " << n << " to " << c |
454 |
|
<< ", explanation = " << bei.d_exp << std::endl; |
455 |
|
} |
456 |
|
else if (c != bei.d_bestContent) |
457 |
|
{ |
458 |
|
// conflict |
459 |
|
Trace("strings-debug") |
460 |
|
<< "Conflict, other constant was " << bei.d_bestContent |
461 |
|
<< ", this constant was " << c << std::endl; |
462 |
|
if (bei.d_exp.isNull()) |
463 |
|
{ |
464 |
|
// n==c ^ n == c' => false |
465 |
|
d_im.addToExplanation(n, bei.d_bestContent, exp); |
466 |
|
} |
467 |
|
else |
468 |
|
{ |
469 |
|
// n==c ^ n == d_base == c' => false |
470 |
|
exp.push_back(bei.d_exp); |
471 |
|
d_im.addToExplanation(n, bei.d_base, exp); |
472 |
|
} |
473 |
|
d_im.sendInference(exp, d_false, InferenceId::STRINGS_I_CONST_CONFLICT); |
474 |
|
return; |
475 |
|
} |
476 |
|
else |
477 |
|
{ |
478 |
|
Trace("strings-debug") << "Duplicate constant." << std::endl; |
479 |
|
} |
480 |
|
} |
481 |
|
} |
482 |
|
} |
483 |
2078549 |
for (std::pair<const TNode, TermIndex>& p : ti->d_children) |
484 |
|
{ |
485 |
1060472 |
std::map<Node, BaseEqcInfo>::const_iterator it = d_eqcInfo.find(p.first); |
486 |
1060472 |
if (it != d_eqcInfo.end() && it->second.d_bestContent.isConst()) |
487 |
|
{ |
488 |
317173 |
vecc.push_back(it->second.d_bestContent); |
489 |
317173 |
checkConstantEquivalenceClasses(&p.second, vecc, ensureConst, isConst); |
490 |
317173 |
vecc.pop_back(); |
491 |
|
} |
492 |
743299 |
else if (!ensureConst) |
493 |
|
{ |
494 |
|
// can still proceed, with null |
495 |
516228 |
vecc.push_back(Node::null()); |
496 |
516228 |
checkConstantEquivalenceClasses(&p.second, vecc, ensureConst, false); |
497 |
516228 |
vecc.pop_back(); |
498 |
|
} |
499 |
1060472 |
if (d_im.hasProcessed()) |
500 |
|
{ |
501 |
|
break; |
502 |
|
} |
503 |
|
} |
504 |
|
} |
505 |
|
|
506 |
7736 |
void BaseSolver::checkCardinality() |
507 |
|
{ |
508 |
|
// This will create a partition of eqc, where each collection has length that |
509 |
|
// are pairwise propagated to be equal. We do not require disequalities |
510 |
|
// between the lengths of each collection, since we split on disequalities |
511 |
|
// between lengths of string terms that are disequal (DEQ-LENGTH-SP). |
512 |
15472 |
std::map<TypeNode, std::vector<std::vector<Node> > > cols; |
513 |
15472 |
std::map<TypeNode, std::vector<Node> > lts; |
514 |
7736 |
d_state.separateByLength(d_stringsEqc, cols, lts); |
515 |
9284 |
for (std::pair<const TypeNode, std::vector<std::vector<Node> > >& c : cols) |
516 |
|
{ |
517 |
1548 |
checkCardinalityType(c.first, c.second, lts[c.first]); |
518 |
|
} |
519 |
7736 |
} |
520 |
|
|
521 |
1548 |
void BaseSolver::checkCardinalityType(TypeNode tn, |
522 |
|
std::vector<std::vector<Node> >& cols, |
523 |
|
std::vector<Node>& lts) |
524 |
|
{ |
525 |
3096 |
Trace("strings-card") << "Check cardinality (type " << tn << ")..." |
526 |
1548 |
<< std::endl; |
527 |
1548 |
NodeManager* nm = NodeManager::currentNM(); |
528 |
|
uint32_t typeCardSize; |
529 |
1548 |
if (tn.isString()) // string-only |
530 |
|
{ |
531 |
1458 |
typeCardSize = d_cardSize; |
532 |
|
} |
533 |
|
else |
534 |
|
{ |
535 |
90 |
Assert(tn.isSequence()); |
536 |
180 |
TypeNode etn = tn.getSequenceElementType(); |
537 |
90 |
if (!d_state.isFiniteType(etn)) |
538 |
|
{ |
539 |
|
// infinite cardinality, we are fine |
540 |
84 |
return; |
541 |
|
} |
542 |
|
// TODO (cvc4-projects #23): how to handle sequence for finite types? |
543 |
6 |
return; |
544 |
|
} |
545 |
|
// for each collection |
546 |
10107 |
for (unsigned i = 0, csize = cols.size(); i < csize; ++i) |
547 |
|
{ |
548 |
8649 |
Node lr = lts[i]; |
549 |
17298 |
Trace("strings-card") << "Number of strings with length equal to " << lr |
550 |
8649 |
<< " is " << cols[i].size() << std::endl; |
551 |
8649 |
if (cols[i].size() <= 1) |
552 |
|
{ |
553 |
|
// no restriction on sets in the partition of size 1 |
554 |
6662 |
continue; |
555 |
|
} |
556 |
|
// size > c^k |
557 |
1987 |
unsigned card_need = 1; |
558 |
1987 |
double curr = static_cast<double>(cols[i].size()); |
559 |
1987 |
while (curr > typeCardSize) |
560 |
|
{ |
561 |
|
curr = curr / static_cast<double>(typeCardSize); |
562 |
|
card_need++; |
563 |
|
} |
564 |
3974 |
Trace("strings-card") |
565 |
1987 |
<< "Need length " << card_need |
566 |
1987 |
<< " for this number of strings (where alphabet size is " |
567 |
1987 |
<< typeCardSize << ") given type " << tn << "." << std::endl; |
568 |
|
// check if we need to split |
569 |
1987 |
bool needsSplit = true; |
570 |
1987 |
if (lr.isConst()) |
571 |
|
{ |
572 |
|
// if constant, compare |
573 |
3740 |
Node cmp = nm->mkNode(GEQ, lr, nm->mkConst(Rational(card_need))); |
574 |
1870 |
cmp = Rewriter::rewrite(cmp); |
575 |
1870 |
needsSplit = !cmp.getConst<bool>(); |
576 |
|
} |
577 |
|
else |
578 |
|
{ |
579 |
|
// find the minimimum constant that we are unknown to be disequal from, or |
580 |
|
// otherwise stop if we increment such that cardinality does not apply. |
581 |
|
// We always start with r=1 since by the invariants of our term registry, |
582 |
|
// a term is either equal to the empty string, or has length >= 1. |
583 |
117 |
unsigned r = 1; |
584 |
117 |
bool success = true; |
585 |
117 |
while (r < card_need && success) |
586 |
|
{ |
587 |
|
Node rr = nm->mkConst(Rational(r)); |
588 |
|
if (d_state.areDisequal(rr, lr)) |
589 |
|
{ |
590 |
|
r++; |
591 |
|
} |
592 |
|
else |
593 |
|
{ |
594 |
|
success = false; |
595 |
|
} |
596 |
|
} |
597 |
117 |
if (r > 0) |
598 |
|
{ |
599 |
234 |
Trace("strings-card") |
600 |
117 |
<< "Symbolic length " << lr << " must be at least " << r |
601 |
117 |
<< " due to constant disequalities." << std::endl; |
602 |
|
} |
603 |
117 |
needsSplit = r < card_need; |
604 |
|
} |
605 |
|
|
606 |
1987 |
if (!needsSplit) |
607 |
|
{ |
608 |
|
// don't need to split |
609 |
1987 |
continue; |
610 |
|
} |
611 |
|
// first, try to split to merge equivalence classes |
612 |
|
for (std::vector<Node>::iterator itr1 = cols[i].begin(); |
613 |
|
itr1 != cols[i].end(); |
614 |
|
++itr1) |
615 |
|
{ |
616 |
|
for (std::vector<Node>::iterator itr2 = itr1 + 1; itr2 != cols[i].end(); |
617 |
|
++itr2) |
618 |
|
{ |
619 |
|
if (!d_state.areDisequal(*itr1, *itr2)) |
620 |
|
{ |
621 |
|
// add split lemma |
622 |
|
if (d_im.sendSplit(*itr1, *itr2, InferenceId::STRINGS_CARD_SP)) |
623 |
|
{ |
624 |
|
return; |
625 |
|
} |
626 |
|
} |
627 |
|
} |
628 |
|
} |
629 |
|
// otherwise, we need a length constraint |
630 |
|
uint32_t int_k = static_cast<uint32_t>(card_need); |
631 |
|
EqcInfo* ei = d_state.getOrMakeEqcInfo(lr, true); |
632 |
|
Trace("strings-card") << "Previous cardinality used for " << lr << " is " |
633 |
|
<< ((int)ei->d_cardinalityLemK.get() - 1) |
634 |
|
<< std::endl; |
635 |
|
if (int_k + 1 > ei->d_cardinalityLemK.get()) |
636 |
|
{ |
637 |
|
Node k_node = nm->mkConst(Rational(int_k)); |
638 |
|
// add cardinality lemma |
639 |
|
Node dist = nm->mkNode(DISTINCT, cols[i]); |
640 |
|
std::vector<Node> expn; |
641 |
|
expn.push_back(dist); |
642 |
|
for (std::vector<Node>::iterator itr1 = cols[i].begin(); |
643 |
|
itr1 != cols[i].end(); |
644 |
|
++itr1) |
645 |
|
{ |
646 |
|
Node len = nm->mkNode(STRING_LENGTH, *itr1); |
647 |
|
if (len != lr) |
648 |
|
{ |
649 |
|
Node len_eq_lr = len.eqNode(lr); |
650 |
|
expn.push_back(len_eq_lr); |
651 |
|
} |
652 |
|
} |
653 |
|
Node len = nm->mkNode(STRING_LENGTH, cols[i][0]); |
654 |
|
Node cons = nm->mkNode(GEQ, len, k_node); |
655 |
|
cons = Rewriter::rewrite(cons); |
656 |
|
ei->d_cardinalityLemK.set(int_k + 1); |
657 |
|
if (!cons.isConst() || !cons.getConst<bool>()) |
658 |
|
{ |
659 |
|
d_im.sendInference( |
660 |
|
expn, expn, cons, InferenceId::STRINGS_CARDINALITY, false, true); |
661 |
|
return; |
662 |
|
} |
663 |
|
} |
664 |
|
} |
665 |
1458 |
Trace("strings-card") << "...end check cardinality" << std::endl; |
666 |
|
} |
667 |
|
|
668 |
3840895 |
bool BaseSolver::isCongruent(Node n) |
669 |
|
{ |
670 |
3840895 |
return d_congruent.find(n) != d_congruent.end(); |
671 |
|
} |
672 |
|
|
673 |
2262715 |
Node BaseSolver::getConstantEqc(Node eqc) |
674 |
|
{ |
675 |
2262715 |
std::map<Node, BaseEqcInfo>::const_iterator it = d_eqcInfo.find(eqc); |
676 |
2262715 |
if (it != d_eqcInfo.end() && it->second.d_bestContent.isConst()) |
677 |
|
{ |
678 |
672715 |
return it->second.d_bestContent; |
679 |
|
} |
680 |
1590000 |
return Node::null(); |
681 |
|
} |
682 |
|
|
683 |
88048 |
Node BaseSolver::explainConstantEqc(Node n, Node eqc, std::vector<Node>& exp) |
684 |
|
{ |
685 |
88048 |
std::map<Node, BaseEqcInfo>::const_iterator it = d_eqcInfo.find(eqc); |
686 |
88048 |
if (it != d_eqcInfo.end()) |
687 |
|
{ |
688 |
88048 |
BaseEqcInfo& bei = d_eqcInfo[eqc]; |
689 |
88048 |
if (!bei.d_bestContent.isConst()) |
690 |
|
{ |
691 |
|
return Node::null(); |
692 |
|
} |
693 |
88048 |
if (!bei.d_exp.isNull()) |
694 |
|
{ |
695 |
|
utils::flattenOp(AND, bei.d_exp, exp); |
696 |
|
} |
697 |
88048 |
if (!bei.d_base.isNull()) |
698 |
|
{ |
699 |
88048 |
d_im.addToExplanation(n, bei.d_base, exp); |
700 |
|
} |
701 |
88048 |
return bei.d_bestContent; |
702 |
|
} |
703 |
|
return Node::null(); |
704 |
|
} |
705 |
|
|
706 |
1160226 |
Node BaseSolver::explainBestContentEqc(Node n, Node eqc, std::vector<Node>& exp) |
707 |
|
{ |
708 |
1160226 |
std::map<Node, BaseEqcInfo>::const_iterator it = d_eqcInfo.find(eqc); |
709 |
1160226 |
if (it != d_eqcInfo.end()) |
710 |
|
{ |
711 |
611130 |
BaseEqcInfo& bei = d_eqcInfo[eqc]; |
712 |
611130 |
Assert(!bei.d_bestContent.isNull()); |
713 |
611130 |
if (!bei.d_exp.isNull()) |
714 |
|
{ |
715 |
60015 |
utils::flattenOp(AND, bei.d_exp, exp); |
716 |
|
} |
717 |
611130 |
if (!bei.d_base.isNull()) |
718 |
|
{ |
719 |
611130 |
d_im.addToExplanation(n, bei.d_base, exp); |
720 |
|
} |
721 |
611130 |
return bei.d_bestContent; |
722 |
|
} |
723 |
|
|
724 |
549096 |
return Node::null(); |
725 |
|
} |
726 |
|
|
727 |
34253 |
const std::vector<Node>& BaseSolver::getStringEqc() const |
728 |
|
{ |
729 |
34253 |
return d_stringsEqc; |
730 |
|
} |
731 |
|
|
732 |
4332055 |
Node BaseSolver::TermIndex::add(TNode n, |
733 |
|
unsigned index, |
734 |
|
const SolverState& s, |
735 |
|
Node er, |
736 |
|
std::vector<Node>& c) |
737 |
|
{ |
738 |
4332055 |
if (index == n.getNumChildren()) |
739 |
|
{ |
740 |
1265796 |
if (d_data.isNull()) |
741 |
|
{ |
742 |
1149288 |
d_data = n; |
743 |
|
} |
744 |
1265796 |
return d_data; |
745 |
|
} |
746 |
3066259 |
Assert(index < n.getNumChildren()); |
747 |
6132518 |
TNode nir = s.getRepresentative(n[index]); |
748 |
|
// if it is empty, and doing CONCAT, ignore |
749 |
3066259 |
if (nir == er && n.getKind() == STRING_CONCAT) |
750 |
|
{ |
751 |
257085 |
return add(n, index + 1, s, er, c); |
752 |
|
} |
753 |
2809174 |
c.push_back(nir); |
754 |
2809174 |
return d_children[nir].add(n, index + 1, s, er, c); |
755 |
|
} |
756 |
|
|
757 |
|
} // namespace strings |
758 |
|
} // namespace theory |
759 |
29514 |
} // namespace cvc5 |