1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Gereon Kremer, Andres Noetzli |
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 |
|
* Implementation of solver for extended functions of theory of strings. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/strings/extf_solver.h" |
17 |
|
|
18 |
|
#include "options/strings_options.h" |
19 |
|
#include "theory/strings/sequences_rewriter.h" |
20 |
|
#include "theory/strings/theory_strings_preprocess.h" |
21 |
|
#include "theory/strings/theory_strings_utils.h" |
22 |
|
#include "util/statistics_registry.h" |
23 |
|
|
24 |
|
using namespace std; |
25 |
|
using namespace cvc5::context; |
26 |
|
using namespace cvc5::kind; |
27 |
|
|
28 |
|
namespace cvc5 { |
29 |
|
namespace theory { |
30 |
|
namespace strings { |
31 |
|
|
32 |
15272 |
ExtfSolver::ExtfSolver(Env& env, |
33 |
|
SolverState& s, |
34 |
|
InferenceManager& im, |
35 |
|
TermRegistry& tr, |
36 |
|
StringsRewriter& rewriter, |
37 |
|
BaseSolver& bs, |
38 |
|
CoreSolver& cs, |
39 |
|
ExtTheory& et, |
40 |
15272 |
SequencesStatistics& statistics) |
41 |
|
: EnvObj(env), |
42 |
|
d_state(s), |
43 |
|
d_im(im), |
44 |
|
d_termReg(tr), |
45 |
|
d_rewriter(rewriter), |
46 |
|
d_bsolver(bs), |
47 |
|
d_csolver(cs), |
48 |
|
d_extt(et), |
49 |
|
d_statistics(statistics), |
50 |
15272 |
d_preproc(d_termReg.getSkolemCache(), &statistics.d_reductions), |
51 |
|
d_hasExtf(context(), false), |
52 |
|
d_extfInferCache(context()), |
53 |
30544 |
d_reduced(userContext()) |
54 |
|
{ |
55 |
15272 |
d_extt.addFunctionKind(kind::STRING_SUBSTR); |
56 |
15272 |
d_extt.addFunctionKind(kind::STRING_UPDATE); |
57 |
15272 |
d_extt.addFunctionKind(kind::STRING_INDEXOF); |
58 |
15272 |
d_extt.addFunctionKind(kind::STRING_INDEXOF_RE); |
59 |
15272 |
d_extt.addFunctionKind(kind::STRING_ITOS); |
60 |
15272 |
d_extt.addFunctionKind(kind::STRING_STOI); |
61 |
15272 |
d_extt.addFunctionKind(kind::STRING_REPLACE); |
62 |
15272 |
d_extt.addFunctionKind(kind::STRING_REPLACE_ALL); |
63 |
15272 |
d_extt.addFunctionKind(kind::STRING_REPLACE_RE); |
64 |
15272 |
d_extt.addFunctionKind(kind::STRING_REPLACE_RE_ALL); |
65 |
15272 |
d_extt.addFunctionKind(kind::STRING_CONTAINS); |
66 |
15272 |
d_extt.addFunctionKind(kind::STRING_IN_REGEXP); |
67 |
15272 |
d_extt.addFunctionKind(kind::STRING_LEQ); |
68 |
15272 |
d_extt.addFunctionKind(kind::STRING_TO_CODE); |
69 |
15272 |
d_extt.addFunctionKind(kind::STRING_TOLOWER); |
70 |
15272 |
d_extt.addFunctionKind(kind::STRING_TOUPPER); |
71 |
15272 |
d_extt.addFunctionKind(kind::STRING_REV); |
72 |
15272 |
d_extt.addFunctionKind(kind::SEQ_UNIT); |
73 |
15272 |
d_extt.addFunctionKind(kind::SEQ_NTH); |
74 |
|
|
75 |
15272 |
d_true = NodeManager::currentNM()->mkConst(true); |
76 |
15272 |
d_false = NodeManager::currentNM()->mkConst(false); |
77 |
15272 |
} |
78 |
|
|
79 |
15267 |
ExtfSolver::~ExtfSolver() {} |
80 |
|
|
81 |
225193 |
bool ExtfSolver::doReduction(int effort, Node n) |
82 |
|
{ |
83 |
225193 |
Assert(d_extfInfoTmp.find(n) != d_extfInfoTmp.end()); |
84 |
225193 |
if (!d_extfInfoTmp[n].d_modelActive) |
85 |
|
{ |
86 |
|
// n is not active in the model, no need to reduce |
87 |
|
Trace("strings-extf-debug") << "...skip due to model active" << std::endl; |
88 |
|
return false; |
89 |
|
} |
90 |
225193 |
if (d_reduced.find(n)!=d_reduced.end()) |
91 |
|
{ |
92 |
|
// already sent a reduction lemma |
93 |
145648 |
Trace("strings-extf-debug") << "...skip due to reduced" << std::endl; |
94 |
145648 |
return false; |
95 |
|
} |
96 |
|
// determine the effort level to process the extf at |
97 |
|
// 0 - at assertion time, 1+ - after no other reduction is applicable |
98 |
79545 |
int r_effort = -1; |
99 |
|
// polarity : 1 true, -1 false, 0 neither |
100 |
79545 |
int pol = 0; |
101 |
79545 |
Kind k = n.getKind(); |
102 |
79545 |
if (n.getType().isBoolean() && !d_extfInfoTmp[n].d_const.isNull()) |
103 |
|
{ |
104 |
38849 |
pol = d_extfInfoTmp[n].d_const.getConst<bool>() ? 1 : -1; |
105 |
|
} |
106 |
79545 |
if (k == STRING_CONTAINS) |
107 |
|
{ |
108 |
12619 |
if (pol == 1) |
109 |
|
{ |
110 |
4076 |
r_effort = 1; |
111 |
|
} |
112 |
8543 |
else if (pol == -1) |
113 |
|
{ |
114 |
7053 |
if (effort == 2) |
115 |
|
{ |
116 |
216 |
Node x = n[0]; |
117 |
216 |
Node s = n[1]; |
118 |
216 |
std::vector<Node> lexp; |
119 |
216 |
Node lenx = d_state.getLength(x, lexp); |
120 |
216 |
Node lens = d_state.getLength(s, lexp); |
121 |
116 |
if (d_state.areEqual(lenx, lens)) |
122 |
|
{ |
123 |
32 |
Trace("strings-extf-debug") |
124 |
16 |
<< " resolve extf : " << n |
125 |
16 |
<< " based on equal lengths disequality." << std::endl; |
126 |
|
// We can reduce negative contains to a disequality when lengths are |
127 |
|
// equal. In other words, len( x ) = len( s ) implies |
128 |
|
// ~contains( x, s ) reduces to x != s. |
129 |
16 |
if (!d_state.areDisequal(x, s)) |
130 |
|
{ |
131 |
|
// len( x ) = len( s ) ^ ~contains( x, s ) => x != s |
132 |
8 |
lexp.push_back(lenx.eqNode(lens)); |
133 |
8 |
lexp.push_back(n.negate()); |
134 |
16 |
Node xneqs = x.eqNode(s).negate(); |
135 |
8 |
d_im.sendInference( |
136 |
|
lexp, xneqs, InferenceId::STRINGS_CTN_NEG_EQUAL, false, true); |
137 |
|
} |
138 |
|
// this depends on the current assertions, so this |
139 |
|
// inference is context-dependent |
140 |
16 |
d_extt.markReduced(n, ExtReducedId::STRINGS_NEG_CTN_DEQ, true); |
141 |
16 |
return true; |
142 |
|
} |
143 |
|
else |
144 |
|
{ |
145 |
100 |
r_effort = 2; |
146 |
|
} |
147 |
|
} |
148 |
|
} |
149 |
|
} |
150 |
66926 |
else if (k == STRING_SUBSTR) |
151 |
|
{ |
152 |
1466 |
r_effort = 1; |
153 |
|
} |
154 |
65460 |
else if (k == SEQ_UNIT) |
155 |
|
{ |
156 |
|
// never necessary to reduce seq.unit |
157 |
278 |
return false; |
158 |
|
} |
159 |
65182 |
else if (k != STRING_IN_REGEXP) |
160 |
|
{ |
161 |
36329 |
r_effort = 2; |
162 |
|
} |
163 |
79251 |
if (effort != r_effort) |
164 |
|
{ |
165 |
71943 |
Trace("strings-extf-debug") << "...skip due to effort" << std::endl; |
166 |
|
// not the right effort level to reduce |
167 |
71943 |
return false; |
168 |
|
} |
169 |
14616 |
Node c_n = pol == -1 ? n.negate() : n; |
170 |
14616 |
Trace("strings-process-debug") |
171 |
7308 |
<< "Process reduction for " << n << ", pol = " << pol << std::endl; |
172 |
7308 |
if (k == STRING_CONTAINS && pol == 1) |
173 |
|
{ |
174 |
8152 |
Node x = n[0]; |
175 |
8152 |
Node s = n[1]; |
176 |
|
// positive contains reduces to a equality |
177 |
4076 |
SkolemCache* skc = d_termReg.getSkolemCache(); |
178 |
8152 |
Node eq = d_termReg.eagerReduce(n, skc, d_termReg.getAlphabetCardinality()); |
179 |
4076 |
Assert(!eq.isNull()); |
180 |
4076 |
Assert(eq.getKind() == ITE && eq[0] == n); |
181 |
4076 |
eq = eq[1]; |
182 |
8152 |
std::vector<Node> expn; |
183 |
4076 |
expn.push_back(n); |
184 |
4076 |
d_im.sendInference(expn, expn, eq, InferenceId::STRINGS_CTN_POS, false, true); |
185 |
8152 |
Trace("strings-extf-debug") |
186 |
4076 |
<< " resolve extf : " << n << " based on positive contain reduction." |
187 |
4076 |
<< std::endl; |
188 |
8152 |
Trace("strings-red-lemma") << "Reduction (positive contains) lemma : " << n |
189 |
4076 |
<< " => " << eq << std::endl; |
190 |
|
// context-dependent because it depends on the polarity of n itself |
191 |
8152 |
d_extt.markReduced(n, ExtReducedId::STRINGS_POS_CTN, true); |
192 |
|
} |
193 |
3232 |
else if (k != kind::STRING_TO_CODE) |
194 |
|
{ |
195 |
2195 |
NodeManager* nm = NodeManager::currentNM(); |
196 |
2195 |
Assert(k == STRING_SUBSTR || k == STRING_UPDATE || k == STRING_CONTAINS |
197 |
|
|| k == STRING_INDEXOF || k == STRING_INDEXOF_RE || k == STRING_ITOS |
198 |
|
|| k == STRING_STOI || k == STRING_REPLACE || k == STRING_REPLACE_ALL |
199 |
|
|| k == SEQ_NTH || k == STRING_REPLACE_RE |
200 |
|
|| k == STRING_REPLACE_RE_ALL || k == STRING_LEQ |
201 |
|
|| k == STRING_TOLOWER || k == STRING_TOUPPER || k == STRING_REV); |
202 |
4390 |
std::vector<Node> new_nodes; |
203 |
4390 |
Node res = d_preproc.simplify(n, new_nodes); |
204 |
2195 |
Assert(res != n); |
205 |
2195 |
new_nodes.push_back(n.eqNode(res)); |
206 |
|
Node nnlem = |
207 |
4390 |
new_nodes.size() == 1 ? new_nodes[0] : nm->mkNode(AND, new_nodes); |
208 |
4390 |
Trace("strings-red-lemma") |
209 |
2195 |
<< "Reduction_" << effort << " lemma : " << nnlem << std::endl; |
210 |
2195 |
Trace("strings-red-lemma") << "...from " << n << std::endl; |
211 |
4390 |
Trace("strings-red-lemma") |
212 |
4390 |
<< "Reduction_" << effort << " rewritten : " << rewrite(nnlem) |
213 |
2195 |
<< std::endl; |
214 |
2195 |
d_im.sendInference(d_emptyVec, nnlem, InferenceId::STRINGS_REDUCTION, false, true); |
215 |
4390 |
Trace("strings-extf-debug") |
216 |
2195 |
<< " resolve extf : " << n << " based on reduction." << std::endl; |
217 |
|
// add as reduction lemma |
218 |
2195 |
d_reduced.insert(n); |
219 |
|
} |
220 |
7308 |
return true; |
221 |
|
} |
222 |
|
|
223 |
32531 |
void ExtfSolver::checkExtfReductions(int effort) |
224 |
|
{ |
225 |
|
// Notice we don't make a standard call to ExtTheory::doReductions here, |
226 |
|
// since certain optimizations like context-dependent reductions and |
227 |
|
// stratifying effort levels are done in doReduction below. |
228 |
58783 |
std::vector<Node> extf = d_extt.getActive(); |
229 |
65062 |
Trace("strings-process") << " checking " << extf.size() << " active extf" |
230 |
32531 |
<< std::endl; |
231 |
251445 |
for (const Node& n : extf) |
232 |
|
{ |
233 |
225193 |
Assert(!d_state.isInConflict()); |
234 |
450386 |
Trace("strings-extf-debug") |
235 |
225193 |
<< " check " << n |
236 |
225193 |
<< ", active in model=" << d_extfInfoTmp[n].d_modelActive << std::endl; |
237 |
225193 |
bool ret = doReduction(effort, n); |
238 |
225193 |
if (ret) |
239 |
|
{ |
240 |
|
// we do not mark as reduced, since we may want to evaluate |
241 |
7324 |
if (d_im.hasProcessed()) |
242 |
|
{ |
243 |
12558 |
return; |
244 |
|
} |
245 |
|
} |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
52743 |
void ExtfSolver::checkExtfEval(int effort) |
250 |
|
{ |
251 |
105486 |
Trace("strings-extf-list") |
252 |
52743 |
<< "Active extended functions, effort=" << effort << " : " << std::endl; |
253 |
52743 |
d_extfInfoTmp.clear(); |
254 |
52743 |
NodeManager* nm = NodeManager::currentNM(); |
255 |
52743 |
bool has_nreduce = false; |
256 |
104731 |
std::vector<Node> terms = d_extt.getActive(); |
257 |
|
// the set of terms we have done extf inferences for |
258 |
104731 |
std::unordered_set<Node> inferProcessed; |
259 |
601835 |
for (const Node& n : terms) |
260 |
|
{ |
261 |
|
// Setup information about n, including if it is equal to a constant. |
262 |
549847 |
ExtfInfoTmp& einfo = d_extfInfoTmp[n]; |
263 |
549847 |
Assert(einfo.d_exp.empty()); |
264 |
1098939 |
Node r = d_state.getRepresentative(n); |
265 |
549847 |
einfo.d_const = d_bsolver.getConstantEqc(r); |
266 |
|
// Get the current values of the children of n. |
267 |
|
// Notice that we look up the value of the direct children of n, and not |
268 |
|
// their free variables. In other words, given a term: |
269 |
|
// t = (str.replace "B" (str.replace x "A" "B") "C") |
270 |
|
// we may build the explanation that: |
271 |
|
// ((str.replace x "A" "B") = "B") => t = (str.replace "B" "B" "C") |
272 |
|
// instead of basing this on the free variable x: |
273 |
|
// (x = "A") => t = (str.replace "B" (str.replace "A" "A" "B") "C") |
274 |
|
// Although both allow us to infer t = "C", it is important to use the |
275 |
|
// first kind of inference since it ensures that its subterms have the |
276 |
|
// expected values. Otherwise, we may in rare cases fail to realize that |
277 |
|
// the subterm (str.replace x "A" "B") does not currently have the correct |
278 |
|
// value, say in this example that (str.replace x "A" "B") != "B". |
279 |
1098939 |
std::vector<Node> exp; |
280 |
1098939 |
std::vector<Node> schildren; |
281 |
549847 |
bool schanged = false; |
282 |
1805414 |
for (const Node& nc : n) |
283 |
|
{ |
284 |
2511134 |
Node sc = getCurrentSubstitutionFor(effort, nc, exp); |
285 |
1255567 |
schildren.push_back(sc); |
286 |
1255567 |
schanged = schanged || sc != nc; |
287 |
|
} |
288 |
|
// If there is information involving the children, attempt to do an |
289 |
|
// inference and/or mark n as reduced. |
290 |
549847 |
bool reduced = false; |
291 |
1098939 |
Node to_reduce = n; |
292 |
549847 |
if (schanged) |
293 |
|
{ |
294 |
527786 |
Node sn = nm->mkNode(n.getKind(), schildren); |
295 |
527786 |
Trace("strings-extf-debug") |
296 |
263893 |
<< "Check extf " << n << " == " << sn |
297 |
263893 |
<< ", constant = " << einfo.d_const << ", effort=" << effort |
298 |
263893 |
<< ", exp " << exp << std::endl; |
299 |
263893 |
einfo.d_exp.insert(einfo.d_exp.end(), exp.begin(), exp.end()); |
300 |
|
// inference is rewriting the substituted node |
301 |
527786 |
Node nrc = rewrite(sn); |
302 |
|
// if rewrites to a constant, then do the inference and mark as reduced |
303 |
263893 |
if (nrc.isConst()) |
304 |
|
{ |
305 |
145455 |
if (effort < 3) |
306 |
|
{ |
307 |
145455 |
d_extt.markReduced(n, ExtReducedId::STRINGS_SR_CONST); |
308 |
290910 |
Trace("strings-extf-debug") |
309 |
145455 |
<< " resolvable by evaluation..." << std::endl; |
310 |
290910 |
std::vector<Node> exps; |
311 |
|
// The following optimization gets the "symbolic definition" of |
312 |
|
// an extended term. The symbolic definition of a term t is a term |
313 |
|
// t' where constants are replaced by their corresponding proxy |
314 |
|
// variables. |
315 |
|
// For example, if lsym is a proxy variable for "", then |
316 |
|
// str.replace( lsym, lsym, lsym ) is the symbolic definition for |
317 |
|
// str.replace( "", "", "" ). It is generally better to use symbolic |
318 |
|
// definitions when doing cd-rewriting for the purpose of minimizing |
319 |
|
// clauses, e.g. we infer the unit equality: |
320 |
|
// str.replace( lsym, lsym, lsym ) == "" |
321 |
|
// instead of making this inference multiple times: |
322 |
|
// x = "" => str.replace( x, x, x ) == "" |
323 |
|
// y = "" => str.replace( y, y, y ) == "" |
324 |
290910 |
Trace("strings-extf-debug") |
325 |
145455 |
<< " get symbolic definition..." << std::endl; |
326 |
290910 |
Node nrs; |
327 |
|
// only use symbolic definitions if option is set |
328 |
145455 |
if (options::stringInferSym()) |
329 |
|
{ |
330 |
145455 |
nrs = d_termReg.getSymbolicDefinition(sn, exps); |
331 |
|
} |
332 |
145455 |
if (!nrs.isNull()) |
333 |
|
{ |
334 |
249376 |
Trace("strings-extf-debug") |
335 |
124688 |
<< " rewrite " << nrs << "..." << std::endl; |
336 |
249376 |
Node nrsr = rewrite(nrs); |
337 |
|
// ensure the symbolic form is not rewritable |
338 |
124688 |
if (nrsr != nrs) |
339 |
|
{ |
340 |
|
// we cannot use the symbolic definition if it rewrites |
341 |
3836 |
Trace("strings-extf-debug") |
342 |
1918 |
<< " symbolic definition is trivial..." << std::endl; |
343 |
1918 |
nrs = Node::null(); |
344 |
|
} |
345 |
|
} |
346 |
|
else |
347 |
|
{ |
348 |
41534 |
Trace("strings-extf-debug") |
349 |
20767 |
<< " could not infer symbolic definition." << std::endl; |
350 |
|
} |
351 |
290910 |
Node conc; |
352 |
145455 |
if (!nrs.isNull()) |
353 |
|
{ |
354 |
245540 |
Trace("strings-extf-debug") |
355 |
122770 |
<< " symbolic def : " << nrs << std::endl; |
356 |
122770 |
if (!d_state.areEqual(nrs, nrc)) |
357 |
|
{ |
358 |
|
// infer symbolic unit |
359 |
1731 |
if (n.getType().isBoolean()) |
360 |
|
{ |
361 |
1336 |
conc = nrc == d_true ? nrs : nrs.negate(); |
362 |
|
} |
363 |
|
else |
364 |
|
{ |
365 |
395 |
conc = nrs.eqNode(nrc); |
366 |
|
} |
367 |
1731 |
einfo.d_exp.clear(); |
368 |
|
} |
369 |
|
} |
370 |
|
else |
371 |
|
{ |
372 |
22685 |
if (!d_state.areEqual(n, nrc)) |
373 |
|
{ |
374 |
1462 |
if (n.getType().isBoolean()) |
375 |
|
{ |
376 |
720 |
if (d_state.areEqual(n, nrc == d_true ? d_false : d_true)) |
377 |
|
{ |
378 |
609 |
einfo.d_exp.push_back(nrc == d_true ? n.negate() : n); |
379 |
609 |
conc = d_false; |
380 |
|
} |
381 |
|
else |
382 |
|
{ |
383 |
111 |
conc = nrc == d_true ? n : n.negate(); |
384 |
|
} |
385 |
|
} |
386 |
|
else |
387 |
|
{ |
388 |
742 |
conc = n.eqNode(nrc); |
389 |
|
} |
390 |
|
} |
391 |
|
} |
392 |
145455 |
if (!conc.isNull()) |
393 |
|
{ |
394 |
6386 |
Trace("strings-extf") |
395 |
3193 |
<< " resolve extf : " << sn << " -> " << nrc << std::endl; |
396 |
3193 |
InferenceId inf = effort == 0 ? InferenceId::STRINGS_EXTF : InferenceId::STRINGS_EXTF_N; |
397 |
3193 |
d_im.sendInference(einfo.d_exp, conc, inf, false, true); |
398 |
3193 |
d_statistics.d_cdSimplifications << n.getKind(); |
399 |
|
} |
400 |
|
} |
401 |
|
else |
402 |
|
{ |
403 |
|
// check if it is already equal, if so, mark as reduced. Otherwise, do |
404 |
|
// nothing. |
405 |
|
if (d_state.areEqual(n, nrc)) |
406 |
|
{ |
407 |
|
Trace("strings-extf") |
408 |
|
<< " resolved extf, since satisfied by model: " << n |
409 |
|
<< std::endl; |
410 |
|
einfo.d_modelActive = false; |
411 |
|
} |
412 |
|
} |
413 |
145455 |
reduced = true; |
414 |
|
} |
415 |
|
else |
416 |
|
{ |
417 |
|
// if this was a predicate which changed after substitution + rewriting |
418 |
118438 |
if (!einfo.d_const.isNull() && nrc.getType().isBoolean() && nrc != n) |
419 |
|
{ |
420 |
25312 |
bool pol = einfo.d_const == d_true; |
421 |
50624 |
Node nrcAssert = pol ? nrc : nrc.negate(); |
422 |
50624 |
Node nAssert = pol ? n : n.negate(); |
423 |
25312 |
Assert(effort < 3); |
424 |
25312 |
einfo.d_exp.push_back(nAssert); |
425 |
25312 |
Trace("strings-extf-debug") << " decomposable..." << std::endl; |
426 |
50624 |
Trace("strings-extf") << " resolve extf : " << sn << " -> " << nrc |
427 |
25312 |
<< ", const = " << einfo.d_const << std::endl; |
428 |
|
// We send inferences internal here, which may help show unsat. |
429 |
|
// However, we do not make a determination whether n can be marked |
430 |
|
// reduced since this argument may be circular: we may infer than n |
431 |
|
// can be reduced to something else, but that thing may argue that it |
432 |
|
// can be reduced to n, in theory. |
433 |
25312 |
InferenceId infer = |
434 |
25312 |
effort == 0 ? InferenceId::STRINGS_EXTF_D : InferenceId::STRINGS_EXTF_D_N; |
435 |
25312 |
d_im.sendInternalInference(einfo.d_exp, nrcAssert, infer); |
436 |
|
} |
437 |
118438 |
to_reduce = nrc; |
438 |
|
} |
439 |
|
} |
440 |
|
// We must use the original n here to avoid circular justifications for |
441 |
|
// why extended functions are reduced. In particular, n should never be a |
442 |
|
// duplicate of another term considered in the block of code for |
443 |
|
// checkExtfInference below. |
444 |
|
// if not reduced and not processed |
445 |
1504086 |
if (!reduced && !n.isNull() |
446 |
954239 |
&& inferProcessed.find(n) == inferProcessed.end()) |
447 |
|
{ |
448 |
404392 |
inferProcessed.insert(n); |
449 |
404392 |
Assert(effort < 3); |
450 |
404392 |
if (effort == 1) |
451 |
|
{ |
452 |
83416 |
Trace("strings-extf") |
453 |
41708 |
<< " cannot rewrite extf : " << to_reduce << std::endl; |
454 |
|
} |
455 |
|
// we take to_reduce to be the (partially) reduced version of n, which |
456 |
|
// is justified by the explanation in einfo. |
457 |
404392 |
checkExtfInference(n, to_reduce, einfo, effort); |
458 |
404392 |
if (Trace.isOn("strings-extf-list")) |
459 |
|
{ |
460 |
|
Trace("strings-extf-list") << " * " << to_reduce; |
461 |
|
if (!einfo.d_const.isNull()) |
462 |
|
{ |
463 |
|
Trace("strings-extf-list") << ", const = " << einfo.d_const; |
464 |
|
} |
465 |
|
if (n != to_reduce) |
466 |
|
{ |
467 |
|
Trace("strings-extf-list") << ", from " << n; |
468 |
|
} |
469 |
|
Trace("strings-extf-list") << std::endl; |
470 |
|
} |
471 |
404392 |
if (d_extt.isActive(n) && einfo.d_modelActive) |
472 |
|
{ |
473 |
404392 |
has_nreduce = true; |
474 |
|
} |
475 |
|
} |
476 |
549847 |
if (d_state.isInConflict()) |
477 |
|
{ |
478 |
755 |
Trace("strings-extf-debug") << " conflict, return." << std::endl; |
479 |
755 |
return; |
480 |
|
} |
481 |
|
} |
482 |
51988 |
d_hasExtf = has_nreduce; |
483 |
|
} |
484 |
|
|
485 |
404392 |
void ExtfSolver::checkExtfInference(Node n, |
486 |
|
Node nr, |
487 |
|
ExtfInfoTmp& in, |
488 |
|
int effort) |
489 |
|
{ |
490 |
404392 |
if (in.d_const.isNull()) |
491 |
|
{ |
492 |
457371 |
return; |
493 |
|
} |
494 |
192006 |
NodeManager* nm = NodeManager::currentNM(); |
495 |
384012 |
Trace("strings-extf-infer") |
496 |
192006 |
<< "checkExtfInference: " << n << " : " << nr << " == " << in.d_const |
497 |
192006 |
<< " with exp " << in.d_exp << std::endl; |
498 |
|
|
499 |
|
// add original to explanation |
500 |
192006 |
if (n.getType().isBoolean()) |
501 |
|
{ |
502 |
|
// if Boolean, it's easy |
503 |
97759 |
in.d_exp.push_back(in.d_const.getConst<bool>() ? n : n.negate()); |
504 |
|
} |
505 |
|
else |
506 |
|
{ |
507 |
|
// otherwise, must explain via base node |
508 |
188494 |
Node r = d_state.getRepresentative(n); |
509 |
|
// explain using the base solver |
510 |
94247 |
d_bsolver.explainConstantEqc(n, r, in.d_exp); |
511 |
|
} |
512 |
|
|
513 |
|
// d_extfInferCache stores whether we have made the inferences associated |
514 |
|
// with a node n, |
515 |
|
// this may need to be generalized if multiple inferences apply |
516 |
|
|
517 |
192006 |
if (nr.getKind() == STRING_CONTAINS) |
518 |
|
{ |
519 |
32599 |
Assert(in.d_const.isConst()); |
520 |
32599 |
bool pol = in.d_const.getConst<bool>(); |
521 |
41562 |
if ((pol && nr[1].getKind() == STRING_CONCAT) |
522 |
65124 |
|| (!pol && nr[0].getKind() == STRING_CONCAT)) |
523 |
|
{ |
524 |
|
// If str.contains( x, str.++( y1, ..., yn ) ), |
525 |
|
// we may infer str.contains( x, y1 ), ..., str.contains( x, yn ) |
526 |
|
// The following recognizes two situations related to the above reasoning: |
527 |
|
// (1) If ~str.contains( x, yi ) holds for some i, we are in conflict, |
528 |
|
// (2) If str.contains( x, yj ) already holds for some j, then the term |
529 |
|
// str.contains( x, yj ) is irrelevant since it is satisfied by all models |
530 |
|
// for str.contains( x, str.++( y1, ..., yn ) ). |
531 |
|
|
532 |
|
// Notice that the dual of the above reasoning also holds, i.e. |
533 |
|
// If ~str.contains( str.++( x1, ..., xn ), y ), |
534 |
|
// we may infer ~str.contains( x1, y ), ..., ~str.contains( xn, y ) |
535 |
|
// This is also handled here. |
536 |
3886 |
if (d_extfInferCache.find(nr) == d_extfInferCache.end()) |
537 |
|
{ |
538 |
1378 |
d_extfInferCache.insert(nr); |
539 |
|
|
540 |
1378 |
int index = pol ? 1 : 0; |
541 |
2752 |
std::vector<Node> children; |
542 |
1378 |
children.push_back(nr[0]); |
543 |
1378 |
children.push_back(nr[1]); |
544 |
4574 |
for (const Node& nrc : nr[index]) |
545 |
|
{ |
546 |
3200 |
children[index] = nrc; |
547 |
6396 |
Node conc = nm->mkNode(STRING_CONTAINS, children); |
548 |
3200 |
conc = rewrite(pol ? conc : conc.negate()); |
549 |
|
// check if it already (does not) hold |
550 |
3200 |
if (d_state.hasTerm(conc)) |
551 |
|
{ |
552 |
147 |
if (d_state.areEqual(conc, d_false)) |
553 |
|
{ |
554 |
|
// we are in conflict |
555 |
4 |
d_im.addToExplanation(conc, d_false, in.d_exp); |
556 |
4 |
d_im.sendInference( |
557 |
|
in.d_exp, d_false, InferenceId::STRINGS_CTN_DECOMPOSE); |
558 |
4 |
Assert(d_state.isInConflict()); |
559 |
4 |
return; |
560 |
|
} |
561 |
143 |
else if (d_extt.hasFunctionKind(conc.getKind())) |
562 |
|
{ |
563 |
|
// can mark as reduced, since model for n implies model for conc |
564 |
|
d_extt.markReduced(conc, ExtReducedId::STRINGS_CTN_DECOMPOSE); |
565 |
|
} |
566 |
|
} |
567 |
|
} |
568 |
|
} |
569 |
|
} |
570 |
|
else |
571 |
|
{ |
572 |
114852 |
if (std::find(d_extfInfoTmp[nr[0]].d_ctn[pol].begin(), |
573 |
57426 |
d_extfInfoTmp[nr[0]].d_ctn[pol].end(), |
574 |
114852 |
nr[1]) |
575 |
86139 |
== d_extfInfoTmp[nr[0]].d_ctn[pol].end()) |
576 |
|
{ |
577 |
57326 |
Trace("strings-extf-debug") << " store contains info : " << nr[0] |
578 |
28663 |
<< " " << pol << " " << nr[1] << std::endl; |
579 |
|
// Store s (does not) contains t, since nr = (~)contains( s, t ) holds. |
580 |
28663 |
d_extfInfoTmp[nr[0]].d_ctn[pol].push_back(nr[1]); |
581 |
28663 |
d_extfInfoTmp[nr[0]].d_ctnFrom[pol].push_back(n); |
582 |
|
// Do transistive closure on contains, e.g. |
583 |
|
// if contains( s, t ) and ~contains( s, r ), then ~contains( t, r ). |
584 |
|
|
585 |
|
// The following infers new (negative) contains based on the above |
586 |
|
// reasoning, provided that ~contains( t, r ) does not |
587 |
|
// already hold in the current context. We test this by checking that |
588 |
|
// contains( t, r ) is not already asserted false in the current |
589 |
|
// context. We also handle the case where contains( t, r ) is equivalent |
590 |
|
// to t = r, in which case we check that t != r does not already hold |
591 |
|
// in the current context. |
592 |
|
|
593 |
|
// Notice that form of the above inference is enough to find |
594 |
|
// conflicts purely due to contains predicates. For example, if we |
595 |
|
// have only positive occurrences of contains, then no conflicts due to |
596 |
|
// contains predicates are possible and this schema does nothing. For |
597 |
|
// example, note that contains( s, t ) and contains( t, r ) implies |
598 |
|
// contains( s, r ), which we could but choose not to infer. Instead, |
599 |
|
// we prefer being lazy: only if ~contains( s, r ) appears later do we |
600 |
|
// infer ~contains( t, r ), which suffices to show a conflict. |
601 |
28663 |
bool opol = !pol; |
602 |
32231 |
for (unsigned i = 0, size = d_extfInfoTmp[nr[0]].d_ctn[opol].size(); |
603 |
32231 |
i < size; |
604 |
|
i++) |
605 |
|
{ |
606 |
7136 |
Node onr = d_extfInfoTmp[nr[0]].d_ctn[opol][i]; |
607 |
|
Node concOrig = |
608 |
7136 |
nm->mkNode(STRING_CONTAINS, pol ? nr[1] : onr, pol ? onr : nr[1]); |
609 |
7136 |
Node conc = rewrite(concOrig); |
610 |
|
// For termination concerns, we only do the inference if the contains |
611 |
|
// does not rewrite (and thus does not introduce new terms). |
612 |
3568 |
if (conc == concOrig) |
613 |
|
{ |
614 |
|
bool do_infer = false; |
615 |
|
conc = conc.negate(); |
616 |
|
bool pol2 = conc.getKind() != NOT; |
617 |
|
Node lit = pol2 ? conc : conc[0]; |
618 |
|
if (lit.getKind() == EQUAL) |
619 |
|
{ |
620 |
|
do_infer = pol2 ? !d_state.areEqual(lit[0], lit[1]) |
621 |
|
: !d_state.areDisequal(lit[0], lit[1]); |
622 |
|
} |
623 |
|
else |
624 |
|
{ |
625 |
|
do_infer = !d_state.areEqual(lit, pol2 ? d_true : d_false); |
626 |
|
} |
627 |
|
if (do_infer) |
628 |
|
{ |
629 |
|
std::vector<Node> exp_c; |
630 |
|
exp_c.insert(exp_c.end(), in.d_exp.begin(), in.d_exp.end()); |
631 |
|
Node ofrom = d_extfInfoTmp[nr[0]].d_ctnFrom[opol][i]; |
632 |
|
Assert(d_extfInfoTmp.find(ofrom) != d_extfInfoTmp.end()); |
633 |
|
exp_c.insert(exp_c.end(), |
634 |
|
d_extfInfoTmp[ofrom].d_exp.begin(), |
635 |
|
d_extfInfoTmp[ofrom].d_exp.end()); |
636 |
|
d_im.sendInference(exp_c, conc, InferenceId::STRINGS_CTN_TRANS); |
637 |
|
} |
638 |
|
} |
639 |
|
} |
640 |
|
} |
641 |
|
else |
642 |
|
{ |
643 |
|
// If we already know that s (does not) contain t, then n may be |
644 |
|
// redundant. However, we do not mark n as reduced here, since strings |
645 |
|
// reductions may require dependencies between extended functions. |
646 |
|
// Marking reduced here could lead to incorrect models if an |
647 |
|
// extended function is marked reduced based on an assignment to |
648 |
|
// something that depends on n. |
649 |
50 |
Trace("strings-extf-debug") << " redundant." << std::endl; |
650 |
|
} |
651 |
|
} |
652 |
32595 |
return; |
653 |
|
} |
654 |
|
|
655 |
|
// If it's not a predicate, see if we can solve the equality n = c, where c |
656 |
|
// is the constant that extended term n is equal to. |
657 |
318814 |
Node inferEq = nr.eqNode(in.d_const); |
658 |
318814 |
Node inferEqr = rewrite(inferEq); |
659 |
318814 |
Node inferEqrr = inferEqr; |
660 |
159407 |
if (inferEqr.getKind() == EQUAL) |
661 |
|
{ |
662 |
|
// try to use the extended rewriter for equalities |
663 |
95271 |
inferEqrr = d_rewriter.rewriteEqualityExt(inferEqr); |
664 |
|
} |
665 |
159407 |
if (inferEqrr != inferEqr) |
666 |
|
{ |
667 |
6137 |
inferEqrr = rewrite(inferEqrr); |
668 |
12274 |
Trace("strings-extf-infer") |
669 |
6137 |
<< "checkExtfInference: " << inferEq << " ...reduces to " << inferEqrr |
670 |
6137 |
<< " with explanation " << in.d_exp << std::endl; |
671 |
6137 |
d_im.sendInternalInference(in.d_exp, inferEqrr, InferenceId::STRINGS_EXTF_EQ_REW); |
672 |
|
} |
673 |
|
} |
674 |
|
|
675 |
1255567 |
Node ExtfSolver::getCurrentSubstitutionFor(int effort, |
676 |
|
Node n, |
677 |
|
std::vector<Node>& exp) |
678 |
|
{ |
679 |
1255567 |
if (effort >= 3) |
680 |
|
{ |
681 |
|
// model values |
682 |
|
Node mv = d_state.getModel()->getRepresentative(n); |
683 |
|
Trace("strings-subs") << " model val : " << mv << std::endl; |
684 |
|
return mv; |
685 |
|
} |
686 |
2511134 |
Node nr = d_state.getRepresentative(n); |
687 |
|
// if the normal form is available, use it |
688 |
1255567 |
if (effort >= 1 && n.getType().isStringLike()) |
689 |
|
{ |
690 |
53882 |
Assert(effort < 3); |
691 |
|
// normal forms |
692 |
53882 |
NormalForm& nfnr = d_csolver.getNormalForm(nr); |
693 |
107764 |
Node ns = d_csolver.getNormalString(nfnr.d_base, exp); |
694 |
107764 |
Trace("strings-subs") << " normal eqc : " << ns << " " << nfnr.d_base |
695 |
53882 |
<< " " << nr << std::endl; |
696 |
53882 |
if (!nfnr.d_base.isNull()) |
697 |
|
{ |
698 |
53882 |
d_im.addToExplanation(n, nfnr.d_base, exp); |
699 |
|
} |
700 |
53882 |
return ns; |
701 |
|
} |
702 |
|
// otherwise, we use the best content heuristic |
703 |
2403370 |
Node c = d_bsolver.explainBestContentEqc(n, nr, exp); |
704 |
1201685 |
if (!c.isNull()) |
705 |
|
{ |
706 |
630106 |
return c; |
707 |
|
} |
708 |
571579 |
return n; |
709 |
|
} |
710 |
|
|
711 |
10650 |
const std::map<Node, ExtfInfoTmp>& ExtfSolver::getInfo() const |
712 |
|
{ |
713 |
10650 |
return d_extfInfoTmp; |
714 |
|
} |
715 |
|
bool ExtfSolver::hasExtendedFunctions() const { return d_hasExtf.get(); } |
716 |
|
|
717 |
10650 |
std::vector<Node> ExtfSolver::getActive(Kind k) const |
718 |
|
{ |
719 |
10650 |
return d_extt.getActive(k); |
720 |
|
} |
721 |
|
|
722 |
|
bool StringsExtfCallback::getCurrentSubstitution( |
723 |
|
int effort, |
724 |
|
const std::vector<Node>& vars, |
725 |
|
std::vector<Node>& subs, |
726 |
|
std::map<Node, std::vector<Node> >& exp) |
727 |
|
{ |
728 |
|
Trace("strings-subs") << "getCurrentSubstitution, effort = " << effort |
729 |
|
<< std::endl; |
730 |
|
for (const Node& v : vars) |
731 |
|
{ |
732 |
|
Trace("strings-subs") << " get subs for " << v << "..." << std::endl; |
733 |
|
Node s = d_esolver->getCurrentSubstitutionFor(effort, v, exp[v]); |
734 |
|
subs.push_back(s); |
735 |
|
} |
736 |
|
return true; |
737 |
|
} |
738 |
|
|
739 |
|
std::string ExtfSolver::debugPrintModel() |
740 |
|
{ |
741 |
|
std::stringstream ss; |
742 |
|
std::vector<Node> extf; |
743 |
|
d_extt.getTerms(extf); |
744 |
|
// each extended function should have at least one annotation below |
745 |
|
for (const Node& n : extf) |
746 |
|
{ |
747 |
|
ss << "- " << n; |
748 |
|
ExtReducedId id; |
749 |
|
if (!d_extt.isActive(n, id)) |
750 |
|
{ |
751 |
|
ss << " :extt-inactive " << id; |
752 |
|
} |
753 |
|
if (!d_extfInfoTmp[n].d_modelActive) |
754 |
|
{ |
755 |
|
ss << " :model-inactive"; |
756 |
|
} |
757 |
|
if (d_reduced.find(n) != d_reduced.end()) |
758 |
|
{ |
759 |
|
ss << " :reduced"; |
760 |
|
} |
761 |
|
ss << std::endl; |
762 |
|
} |
763 |
|
return ss.str(); |
764 |
|
} |
765 |
|
|
766 |
|
} // namespace strings |
767 |
|
} // namespace theory |
768 |
31137 |
} // namespace cvc5 |