1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andres Noetzli, Andrew Reynolds |
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 |
|
* The Evaluator class. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/evaluator.h" |
17 |
|
|
18 |
|
#include "theory/bv/theory_bv_utils.h" |
19 |
|
#include "theory/rewriter.h" |
20 |
|
#include "theory/strings/theory_strings_utils.h" |
21 |
|
#include "theory/theory.h" |
22 |
|
#include "util/integer.h" |
23 |
|
|
24 |
|
namespace cvc5 { |
25 |
|
namespace theory { |
26 |
|
|
27 |
1213333 |
EvalResult::EvalResult(const EvalResult& other) |
28 |
|
{ |
29 |
1213333 |
d_tag = other.d_tag; |
30 |
1213333 |
switch (d_tag) |
31 |
|
{ |
32 |
174088 |
case BOOL: d_bool = other.d_bool; break; |
33 |
531082 |
case BITVECTOR: |
34 |
531082 |
new (&d_bv) BitVector; |
35 |
531082 |
d_bv = other.d_bv; |
36 |
531082 |
break; |
37 |
174535 |
case RATIONAL: |
38 |
174535 |
new (&d_rat) Rational; |
39 |
174535 |
d_rat = other.d_rat; |
40 |
174535 |
break; |
41 |
264907 |
case STRING: |
42 |
264907 |
new (&d_str) String; |
43 |
264907 |
d_str = other.d_str; |
44 |
264907 |
break; |
45 |
|
case UCONST: |
46 |
|
new (&d_uc) |
47 |
|
UninterpretedConstant(other.d_uc.getType(), other.d_uc.getIndex()); |
48 |
|
break; |
49 |
68721 |
case INVALID: break; |
50 |
|
} |
51 |
1213333 |
} |
52 |
|
|
53 |
3689730 |
EvalResult& EvalResult::operator=(const EvalResult& other) |
54 |
|
{ |
55 |
3689730 |
if (this != &other) |
56 |
|
{ |
57 |
3689730 |
d_tag = other.d_tag; |
58 |
3689730 |
switch (d_tag) |
59 |
|
{ |
60 |
867744 |
case BOOL: d_bool = other.d_bool; break; |
61 |
1622101 |
case BITVECTOR: |
62 |
1622101 |
new (&d_bv) BitVector; |
63 |
1622101 |
d_bv = other.d_bv; |
64 |
1622101 |
break; |
65 |
628226 |
case RATIONAL: |
66 |
628226 |
new (&d_rat) Rational; |
67 |
628226 |
d_rat = other.d_rat; |
68 |
628226 |
break; |
69 |
318442 |
case STRING: |
70 |
318442 |
new (&d_str) String; |
71 |
318442 |
d_str = other.d_str; |
72 |
318442 |
break; |
73 |
|
case UCONST: |
74 |
|
new (&d_uc) |
75 |
|
UninterpretedConstant(other.d_uc.getType(), other.d_uc.getIndex()); |
76 |
|
break; |
77 |
253217 |
case INVALID: break; |
78 |
|
} |
79 |
|
} |
80 |
3689730 |
return *this; |
81 |
|
} |
82 |
|
|
83 |
16668052 |
EvalResult::~EvalResult() |
84 |
|
{ |
85 |
8334026 |
switch (d_tag) |
86 |
|
{ |
87 |
3752946 |
case BITVECTOR: |
88 |
|
{ |
89 |
3752946 |
d_bv.~BitVector(); |
90 |
3752946 |
break; |
91 |
|
} |
92 |
1194842 |
case RATIONAL: |
93 |
|
{ |
94 |
1194842 |
d_rat.~Rational(); |
95 |
1194842 |
break; |
96 |
|
} |
97 |
901734 |
case STRING: |
98 |
|
{ |
99 |
901734 |
d_str.~String(); |
100 |
901734 |
break; |
101 |
|
} |
102 |
|
case UCONST: |
103 |
|
{ |
104 |
|
d_uc.~UninterpretedConstant(); |
105 |
|
break; |
106 |
|
} |
107 |
2484504 |
default: break; |
108 |
|
} |
109 |
8334026 |
} |
110 |
|
|
111 |
1332708 |
Node EvalResult::toNode() const |
112 |
|
{ |
113 |
1332708 |
NodeManager* nm = NodeManager::currentNM(); |
114 |
1332708 |
switch (d_tag) |
115 |
|
{ |
116 |
100944 |
case EvalResult::BOOL: return nm->mkConst(d_bool); |
117 |
795852 |
case EvalResult::BITVECTOR: return nm->mkConst(d_bv); |
118 |
99388 |
case EvalResult::RATIONAL: return nm->mkConst(d_rat); |
119 |
267978 |
case EvalResult::STRING: return nm->mkConst(d_str); |
120 |
|
case EvalResult::UCONST: return nm->mkConst(d_uc); |
121 |
68546 |
default: |
122 |
|
{ |
123 |
137092 |
Trace("evaluator") << "Missing conversion from " << d_tag << " to node" |
124 |
68546 |
<< std::endl; |
125 |
68546 |
return Node(); |
126 |
|
} |
127 |
|
} |
128 |
|
} |
129 |
|
|
130 |
898551 |
Node Evaluator::eval(TNode n, |
131 |
|
const std::vector<Node>& args, |
132 |
|
const std::vector<Node>& vals, |
133 |
|
bool useRewriter) const |
134 |
|
{ |
135 |
1797102 |
std::unordered_map<Node, Node> visited; |
136 |
1797102 |
return eval(n, args, vals, visited, useRewriter); |
137 |
|
} |
138 |
915169 |
Node Evaluator::eval(TNode n, |
139 |
|
const std::vector<Node>& args, |
140 |
|
const std::vector<Node>& vals, |
141 |
|
const std::unordered_map<Node, Node>& visited, |
142 |
|
bool useRewriter) const |
143 |
|
{ |
144 |
1830338 |
Trace("evaluator") << "Evaluating " << n << " under substitution " << args |
145 |
1830338 |
<< " " << vals << " with visited size = " << visited.size() |
146 |
915169 |
<< std::endl; |
147 |
1830338 |
std::unordered_map<TNode, Node> evalAsNode; |
148 |
1830338 |
std::unordered_map<TNode, EvalResult> results; |
149 |
|
// add visited to results |
150 |
936385 |
for (const std::pair<const Node, Node>& p : visited) |
151 |
|
{ |
152 |
21216 |
Trace("evaluator") << "Add " << p.first << " == " << p.second << std::endl; |
153 |
21216 |
results[p.first] = evalInternal(p.second, args, vals, evalAsNode, results, useRewriter); |
154 |
21216 |
if (results[p.first].d_tag == EvalResult::INVALID) |
155 |
|
{ |
156 |
|
// could not evaluate, use the evalAsNode map |
157 |
175 |
std::unordered_map<TNode, Node>::iterator itn = evalAsNode.find(p.second); |
158 |
175 |
Assert(itn != evalAsNode.end()); |
159 |
350 |
Node val = itn->second; |
160 |
175 |
if (useRewriter) |
161 |
|
{ |
162 |
175 |
val = Rewriter::rewrite(val); |
163 |
|
} |
164 |
175 |
evalAsNode[p.first] = val; |
165 |
|
} |
166 |
|
} |
167 |
915169 |
Trace("evaluator") << "Run eval internal..." << std::endl; |
168 |
915169 |
Node ret = evalInternal(n, args, vals, evalAsNode, results, useRewriter).toNode(); |
169 |
|
// if we failed to evaluate |
170 |
915169 |
if (ret.isNull() && useRewriter) |
171 |
|
{ |
172 |
|
// should be stored in the evaluation-as-node map |
173 |
68546 |
std::unordered_map<TNode, Node>::iterator itn = evalAsNode.find(n); |
174 |
68546 |
Assert(itn != evalAsNode.end()); |
175 |
68546 |
ret = Rewriter::rewrite(itn->second); |
176 |
|
} |
177 |
|
// should be the same as substitution + rewriting, or possibly null if |
178 |
|
// useRewriter is false |
179 |
915169 |
Assert((ret.isNull() && !useRewriter) |
180 |
|
|| ret |
181 |
|
== Rewriter::rewrite(n.substitute( |
182 |
|
args.begin(), args.end(), vals.begin(), vals.end()))); |
183 |
1830338 |
return ret; |
184 |
|
} |
185 |
|
|
186 |
936385 |
EvalResult Evaluator::evalInternal( |
187 |
|
TNode n, |
188 |
|
const std::vector<Node>& args, |
189 |
|
const std::vector<Node>& vals, |
190 |
|
std::unordered_map<TNode, Node>& evalAsNode, |
191 |
|
std::unordered_map<TNode, EvalResult>& results, |
192 |
|
bool useRewriter) const |
193 |
|
{ |
194 |
1872770 |
std::vector<TNode> queue; |
195 |
936385 |
queue.emplace_back(n); |
196 |
936385 |
std::unordered_map<TNode, EvalResult>::iterator itr; |
197 |
|
|
198 |
12209093 |
while (queue.size() != 0) |
199 |
|
{ |
200 |
10825162 |
TNode currNode = queue.back(); |
201 |
|
|
202 |
5974804 |
if (results.find(currNode) != results.end()) |
203 |
|
{ |
204 |
338450 |
queue.pop_back(); |
205 |
338450 |
continue; |
206 |
|
} |
207 |
|
|
208 |
5297904 |
bool doProcess = true; |
209 |
5297904 |
bool isVar = false; |
210 |
5297904 |
bool doEval = true; |
211 |
5297904 |
if (currNode.isVar()) |
212 |
|
{ |
213 |
|
// we do not evaluate if we are a variable, instead we look for the |
214 |
|
// variable in args below |
215 |
1110033 |
isVar = true; |
216 |
1110033 |
doEval = false; |
217 |
|
} |
218 |
4187871 |
else if (currNode.getMetaKind() == kind::metakind::PARAMETERIZED) |
219 |
|
{ |
220 |
437084 |
TNode op = currNode.getOperator(); |
221 |
|
// Certain nodes are parameterized with constant operators, including |
222 |
|
// bitvector extract. These operators do not need to be evaluated. |
223 |
218542 |
if (!op.isConst()) |
224 |
|
{ |
225 |
73496 |
itr = results.find(op); |
226 |
73496 |
if (itr == results.end()) |
227 |
|
{ |
228 |
32103 |
queue.emplace_back(op); |
229 |
32103 |
doProcess = false; |
230 |
|
} |
231 |
41393 |
else if (itr->second.d_tag == EvalResult::INVALID) |
232 |
|
{ |
233 |
41393 |
doEval = false; |
234 |
|
} |
235 |
|
} |
236 |
|
} |
237 |
13950076 |
for (const auto& currNodeChild : currNode) |
238 |
|
{ |
239 |
8652172 |
itr = results.find(currNodeChild); |
240 |
8652172 |
if (itr == results.end()) |
241 |
|
{ |
242 |
3038476 |
queue.emplace_back(currNodeChild); |
243 |
3038476 |
doProcess = false; |
244 |
|
} |
245 |
5613696 |
else if (itr->second.d_tag == EvalResult::INVALID) |
246 |
|
{ |
247 |
|
// we cannot evaluate since there was an invalid child |
248 |
213585 |
doEval = false; |
249 |
|
} |
250 |
|
} |
251 |
10595808 |
Trace("evaluator") << "Evaluator: visit " << currNode |
252 |
5297904 |
<< ", process = " << doProcess |
253 |
5297904 |
<< ", evaluate = " << doEval << std::endl; |
254 |
|
|
255 |
5297904 |
if (doProcess) |
256 |
|
{ |
257 |
3668514 |
queue.pop_back(); |
258 |
|
|
259 |
7227932 |
Node currNodeVal = currNode; |
260 |
|
// whether we need to reconstruct the current node in the case of failure |
261 |
3668514 |
bool needsReconstruct = true; |
262 |
|
|
263 |
|
// The code below should either: |
264 |
|
// (1) store a valid EvalResult into results[currNode], or |
265 |
|
// (2) store an invalid EvalResult into results[currNode] and |
266 |
|
// store the result of substitution + rewriting currNode { args -> vals } |
267 |
|
// into evalAsNode[currNode]. |
268 |
|
|
269 |
|
// If we did not successfully evaluate all children, or are a variable |
270 |
3668514 |
if (!doEval) |
271 |
|
{ |
272 |
1260171 |
if (isVar) |
273 |
|
{ |
274 |
1110033 |
const auto& it = std::find(args.begin(), args.end(), currNode); |
275 |
1147512 |
if (it == args.end()) |
276 |
|
{ |
277 |
|
// variable with no substitution is itself |
278 |
37479 |
evalAsNode[currNode] = currNode; |
279 |
37479 |
results[currNode] = EvalResult(); |
280 |
37479 |
continue; |
281 |
|
} |
282 |
1072554 |
ptrdiff_t pos = std::distance(args.begin(), it); |
283 |
1072554 |
currNodeVal = vals[pos]; |
284 |
|
// Don't need to rewrite since range of substitution should already |
285 |
|
// be normalized. |
286 |
|
} |
287 |
|
else |
288 |
|
{ |
289 |
|
// Reconstruct the node with a combination of the children that |
290 |
|
// successfully evaluated, and the children that did not. |
291 |
150138 |
Trace("evaluator") << "Evaluator: collect arguments" << std::endl; |
292 |
150138 |
currNodeVal = reconstruct(currNodeVal, results, evalAsNode); |
293 |
150138 |
if (useRewriter) |
294 |
|
{ |
295 |
|
// Rewrite the result now, if we use the rewriter. We will see below |
296 |
|
// if we are able to turn it into a valid EvalResult. |
297 |
150138 |
currNodeVal = Rewriter::rewrite(currNodeVal); |
298 |
|
} |
299 |
|
} |
300 |
1222692 |
needsReconstruct = false; |
301 |
2445384 |
Trace("evaluator") << "Evaluator: now after substitution + rewriting: " |
302 |
1222692 |
<< currNodeVal << std::endl; |
303 |
1294309 |
if (currNodeVal.getNumChildren() > 0) |
304 |
|
{ |
305 |
|
// We may continue with a valid EvalResult at this point only if |
306 |
|
// we have no children. We must otherwise fail here since some of |
307 |
|
// our children may not have successful evaluations. |
308 |
71617 |
results[currNode] = EvalResult(); |
309 |
71617 |
evalAsNode[currNode] = currNodeVal; |
310 |
71617 |
continue; |
311 |
|
} |
312 |
|
// Otherwise, we may be able to turn the overall result into an |
313 |
|
// valid EvalResult and continue. We fallthrough and continue with the |
314 |
|
// block of code below. |
315 |
|
} |
316 |
|
|
317 |
3559418 |
Trace("evaluator") << "Current node val : " << currNodeVal << std::endl; |
318 |
|
|
319 |
3559418 |
switch (currNodeVal.getKind()) |
320 |
|
{ |
321 |
|
// APPLY_UF is a special case where we look up the operator and apply |
322 |
|
// beta reduction if possible |
323 |
|
case kind::APPLY_UF: |
324 |
|
{ |
325 |
|
Trace("evaluator") << "Evaluate " << currNode << std::endl; |
326 |
|
TNode op = currNode.getOperator(); |
327 |
|
Assert(evalAsNode.find(op) != evalAsNode.end()); |
328 |
|
// no function can be a valid EvalResult |
329 |
|
op = evalAsNode[op]; |
330 |
|
Trace("evaluator") << "Operator evaluated to " << op << std::endl; |
331 |
|
if (op.getKind() != kind::LAMBDA) |
332 |
|
{ |
333 |
|
// this node is not evaluatable due to operator, must add to |
334 |
|
// evalAsNode |
335 |
|
results[currNode] = EvalResult(); |
336 |
|
evalAsNode[currNode] = reconstruct(currNode, results, evalAsNode); |
337 |
|
continue; |
338 |
|
} |
339 |
|
// Create a copy of the current substitutions |
340 |
|
std::vector<Node> lambdaArgs(args); |
341 |
|
std::vector<Node> lambdaVals(vals); |
342 |
|
|
343 |
|
// Add the values for the arguments of the lambda as substitutions at |
344 |
|
// the beginning of the vector to shadow variables from outer scopes |
345 |
|
// with the same name |
346 |
|
for (const auto& lambdaArg : op[0]) |
347 |
|
{ |
348 |
|
lambdaArgs.insert(lambdaArgs.begin(), lambdaArg); |
349 |
|
} |
350 |
|
|
351 |
|
for (const auto& lambdaVal : currNode) |
352 |
|
{ |
353 |
|
lambdaVals.insert(lambdaVals.begin(), results[lambdaVal].toNode()); |
354 |
|
} |
355 |
|
|
356 |
|
// Lambdas are evaluated in a recursive fashion because each |
357 |
|
// evaluation requires different substitutions. We use a fresh cache |
358 |
|
// since the evaluation of op[1] is under a new substitution and thus |
359 |
|
// should not be cached. We could alternatively copy evalAsNode to |
360 |
|
// evalAsNodeC but favor avoiding this copy for performance reasons. |
361 |
|
std::unordered_map<TNode, Node> evalAsNodeC; |
362 |
|
std::unordered_map<TNode, EvalResult> resultsC; |
363 |
|
results[currNode] = evalInternal(op[1], |
364 |
|
lambdaArgs, |
365 |
|
lambdaVals, |
366 |
|
evalAsNodeC, |
367 |
|
resultsC, |
368 |
|
useRewriter); |
369 |
|
Trace("evaluator") << "Evaluated via arguments to " |
370 |
|
<< results[currNode].d_tag << std::endl; |
371 |
|
if (results[currNode].d_tag == EvalResult::INVALID) |
372 |
|
{ |
373 |
|
// evaluation was invalid, we take the node of op[1] as the result |
374 |
|
evalAsNode[currNode] = evalAsNodeC[op[1]]; |
375 |
|
Trace("evaluator") |
376 |
|
<< "Take node evaluation: " << evalAsNodeC[op[1]] << std::endl; |
377 |
|
} |
378 |
|
} |
379 |
|
break; |
380 |
198644 |
case kind::CONST_BOOLEAN: |
381 |
198644 |
results[currNode] = EvalResult(currNodeVal.getConst<bool>()); |
382 |
198644 |
break; |
383 |
|
|
384 |
231821 |
case kind::NOT: |
385 |
|
{ |
386 |
231821 |
results[currNode] = EvalResult(!(results[currNode[0]].d_bool)); |
387 |
231821 |
break; |
388 |
|
} |
389 |
|
|
390 |
151235 |
case kind::AND: |
391 |
|
{ |
392 |
151235 |
bool res = results[currNode[0]].d_bool; |
393 |
1358657 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
394 |
|
{ |
395 |
1207422 |
res = res && results[currNode[i]].d_bool; |
396 |
|
} |
397 |
151235 |
results[currNode] = EvalResult(res); |
398 |
151235 |
break; |
399 |
|
} |
400 |
|
|
401 |
36828 |
case kind::OR: |
402 |
|
{ |
403 |
36828 |
bool res = results[currNode[0]].d_bool; |
404 |
297255 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
405 |
|
{ |
406 |
260427 |
res = res || results[currNode[i]].d_bool; |
407 |
|
} |
408 |
36828 |
results[currNode] = EvalResult(res); |
409 |
36828 |
break; |
410 |
|
} |
411 |
|
|
412 |
296773 |
case kind::CONST_RATIONAL: |
413 |
|
{ |
414 |
296773 |
const Rational& r = currNodeVal.getConst<Rational>(); |
415 |
296773 |
results[currNode] = EvalResult(r); |
416 |
296773 |
break; |
417 |
|
} |
418 |
|
case kind::UNINTERPRETED_CONSTANT: |
419 |
|
{ |
420 |
|
const UninterpretedConstant& uc = |
421 |
|
currNodeVal.getConst<UninterpretedConstant>(); |
422 |
|
results[currNode] = EvalResult(uc); |
423 |
|
break; |
424 |
|
} |
425 |
75009 |
case kind::PLUS: |
426 |
|
{ |
427 |
150018 |
Rational res = results[currNode[0]].d_rat; |
428 |
170356 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
429 |
|
{ |
430 |
95347 |
res = res + results[currNode[i]].d_rat; |
431 |
|
} |
432 |
75009 |
results[currNode] = EvalResult(res); |
433 |
75009 |
break; |
434 |
|
} |
435 |
|
|
436 |
2209 |
case kind::MINUS: |
437 |
|
{ |
438 |
2209 |
const Rational& x = results[currNode[0]].d_rat; |
439 |
2209 |
const Rational& y = results[currNode[1]].d_rat; |
440 |
2209 |
results[currNode] = EvalResult(x - y); |
441 |
2209 |
break; |
442 |
|
} |
443 |
|
|
444 |
|
case kind::UMINUS: |
445 |
|
{ |
446 |
|
const Rational& x = results[currNode[0]].d_rat; |
447 |
|
results[currNode] = EvalResult(-x); |
448 |
|
break; |
449 |
|
} |
450 |
17283 |
case kind::MULT: |
451 |
|
case kind::NONLINEAR_MULT: |
452 |
|
{ |
453 |
34566 |
Rational res = results[currNode[0]].d_rat; |
454 |
34578 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
455 |
|
{ |
456 |
17295 |
res = res * results[currNode[i]].d_rat; |
457 |
|
} |
458 |
17283 |
results[currNode] = EvalResult(res); |
459 |
17283 |
break; |
460 |
|
} |
461 |
|
|
462 |
67407 |
case kind::GEQ: |
463 |
|
{ |
464 |
67407 |
const Rational& x = results[currNode[0]].d_rat; |
465 |
67407 |
const Rational& y = results[currNode[1]].d_rat; |
466 |
67407 |
results[currNode] = EvalResult(x >= y); |
467 |
67407 |
break; |
468 |
|
} |
469 |
14659 |
case kind::LEQ: |
470 |
|
{ |
471 |
14659 |
const Rational& x = results[currNode[0]].d_rat; |
472 |
14659 |
const Rational& y = results[currNode[1]].d_rat; |
473 |
14659 |
results[currNode] = EvalResult(x <= y); |
474 |
14659 |
break; |
475 |
|
} |
476 |
13384 |
case kind::GT: |
477 |
|
{ |
478 |
13384 |
const Rational& x = results[currNode[0]].d_rat; |
479 |
13384 |
const Rational& y = results[currNode[1]].d_rat; |
480 |
13384 |
results[currNode] = EvalResult(x > y); |
481 |
13384 |
break; |
482 |
|
} |
483 |
14961 |
case kind::LT: |
484 |
|
{ |
485 |
14961 |
const Rational& x = results[currNode[0]].d_rat; |
486 |
14961 |
const Rational& y = results[currNode[1]].d_rat; |
487 |
14961 |
results[currNode] = EvalResult(x < y); |
488 |
14961 |
break; |
489 |
|
} |
490 |
|
case kind::ABS: |
491 |
|
{ |
492 |
|
const Rational& x = results[currNode[0]].d_rat; |
493 |
|
results[currNode] = EvalResult(x.abs()); |
494 |
|
break; |
495 |
|
} |
496 |
290369 |
case kind::CONST_STRING: |
497 |
290369 |
results[currNode] = EvalResult(currNodeVal.getConst<String>()); |
498 |
290369 |
break; |
499 |
|
|
500 |
24410 |
case kind::STRING_CONCAT: |
501 |
|
{ |
502 |
48820 |
String res = results[currNode[0]].d_str; |
503 |
48847 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
504 |
|
{ |
505 |
24437 |
res = res.concat(results[currNode[i]].d_str); |
506 |
|
} |
507 |
24410 |
results[currNode] = EvalResult(res); |
508 |
24410 |
break; |
509 |
|
} |
510 |
|
|
511 |
795 |
case kind::STRING_LENGTH: |
512 |
|
{ |
513 |
795 |
const String& s = results[currNode[0]].d_str; |
514 |
795 |
results[currNode] = EvalResult(Rational(s.size())); |
515 |
795 |
break; |
516 |
|
} |
517 |
|
|
518 |
3345 |
case kind::STRING_SUBSTR: |
519 |
|
{ |
520 |
3345 |
const String& s = results[currNode[0]].d_str; |
521 |
6690 |
Integer s_len(s.size()); |
522 |
6690 |
Integer i = results[currNode[1]].d_rat.getNumerator(); |
523 |
6690 |
Integer j = results[currNode[2]].d_rat.getNumerator(); |
524 |
|
|
525 |
3345 |
if (i.strictlyNegative() || j.strictlyNegative() || i >= s_len) |
526 |
|
{ |
527 |
489 |
results[currNode] = EvalResult(String("")); |
528 |
|
} |
529 |
2856 |
else if (i + j > s_len) |
530 |
|
{ |
531 |
225 |
results[currNode] = |
532 |
450 |
EvalResult(s.suffix((s_len - i).toUnsignedInt())); |
533 |
|
} |
534 |
|
else |
535 |
|
{ |
536 |
2631 |
results[currNode] = |
537 |
5262 |
EvalResult(s.substr(i.toUnsignedInt(), j.toUnsignedInt())); |
538 |
|
} |
539 |
3345 |
break; |
540 |
|
} |
541 |
|
|
542 |
|
case kind::STRING_UPDATE: |
543 |
|
{ |
544 |
|
const String& s = results[currNode[0]].d_str; |
545 |
|
Integer s_len(s.size()); |
546 |
|
Integer i = results[currNode[1]].d_rat.getNumerator(); |
547 |
|
const String& t = results[currNode[2]].d_str; |
548 |
|
|
549 |
|
if (i.strictlyNegative() || i >= s_len) |
550 |
|
{ |
551 |
|
results[currNode] = EvalResult(s); |
552 |
|
} |
553 |
|
else |
554 |
|
{ |
555 |
|
results[currNode] = EvalResult(s.update(i.toUnsignedInt(), t)); |
556 |
|
} |
557 |
|
break; |
558 |
|
} |
559 |
|
case kind::STRING_CHARAT: |
560 |
|
{ |
561 |
|
const String& s = results[currNode[0]].d_str; |
562 |
|
Integer s_len(s.size()); |
563 |
|
Integer i = results[currNode[1]].d_rat.getNumerator(); |
564 |
|
if (i.strictlyNegative() || i >= s_len) |
565 |
|
{ |
566 |
|
results[currNode] = EvalResult(String("")); |
567 |
|
} |
568 |
|
else |
569 |
|
{ |
570 |
|
results[currNode] = EvalResult(s.substr(i.toUnsignedInt(), 1)); |
571 |
|
} |
572 |
|
break; |
573 |
|
} |
574 |
|
|
575 |
279 |
case kind::STRING_CONTAINS: |
576 |
|
{ |
577 |
279 |
const String& s = results[currNode[0]].d_str; |
578 |
279 |
const String& t = results[currNode[1]].d_str; |
579 |
279 |
results[currNode] = EvalResult(s.find(t) != std::string::npos); |
580 |
279 |
break; |
581 |
|
} |
582 |
|
|
583 |
8 |
case kind::STRING_INDEXOF: |
584 |
|
{ |
585 |
8 |
const String& s = results[currNode[0]].d_str; |
586 |
16 |
Integer s_len(s.size()); |
587 |
8 |
const String& x = results[currNode[1]].d_str; |
588 |
16 |
Integer i = results[currNode[2]].d_rat.getNumerator(); |
589 |
|
|
590 |
8 |
if (i.strictlyNegative()) |
591 |
|
{ |
592 |
|
results[currNode] = EvalResult(Rational(-1)); |
593 |
|
} |
594 |
|
else |
595 |
|
{ |
596 |
8 |
size_t r = s.find(x, i.toUnsignedInt()); |
597 |
8 |
if (r == std::string::npos) |
598 |
|
{ |
599 |
6 |
results[currNode] = EvalResult(Rational(-1)); |
600 |
|
} |
601 |
|
else |
602 |
|
{ |
603 |
2 |
results[currNode] = EvalResult(Rational(r)); |
604 |
|
} |
605 |
|
} |
606 |
8 |
break; |
607 |
|
} |
608 |
|
|
609 |
258 |
case kind::STRING_REPLACE: |
610 |
|
{ |
611 |
258 |
const String& s = results[currNode[0]].d_str; |
612 |
258 |
const String& x = results[currNode[1]].d_str; |
613 |
258 |
const String& y = results[currNode[2]].d_str; |
614 |
258 |
results[currNode] = EvalResult(s.replace(x, y)); |
615 |
258 |
break; |
616 |
|
} |
617 |
|
|
618 |
|
case kind::STRING_PREFIX: |
619 |
|
{ |
620 |
|
const String& t = results[currNode[0]].d_str; |
621 |
|
const String& s = results[currNode[1]].d_str; |
622 |
|
if (s.size() < t.size()) |
623 |
|
{ |
624 |
|
results[currNode] = EvalResult(false); |
625 |
|
} |
626 |
|
else |
627 |
|
{ |
628 |
|
results[currNode] = EvalResult(s.prefix(t.size()) == t); |
629 |
|
} |
630 |
|
break; |
631 |
|
} |
632 |
|
|
633 |
|
case kind::STRING_SUFFIX: |
634 |
|
{ |
635 |
|
const String& t = results[currNode[0]].d_str; |
636 |
|
const String& s = results[currNode[1]].d_str; |
637 |
|
if (s.size() < t.size()) |
638 |
|
{ |
639 |
|
results[currNode] = EvalResult(false); |
640 |
|
} |
641 |
|
else |
642 |
|
{ |
643 |
|
results[currNode] = EvalResult(s.suffix(t.size()) == t); |
644 |
|
} |
645 |
|
break; |
646 |
|
} |
647 |
|
|
648 |
3 |
case kind::STRING_ITOS: |
649 |
|
{ |
650 |
6 |
Integer i = results[currNode[0]].d_rat.getNumerator(); |
651 |
3 |
if (i.strictlyNegative()) |
652 |
|
{ |
653 |
|
results[currNode] = EvalResult(String("")); |
654 |
|
} |
655 |
|
else |
656 |
|
{ |
657 |
3 |
results[currNode] = EvalResult(String(i.toString())); |
658 |
|
} |
659 |
3 |
break; |
660 |
|
} |
661 |
|
|
662 |
|
case kind::STRING_STOI: |
663 |
|
{ |
664 |
|
const String& s = results[currNode[0]].d_str; |
665 |
|
if (s.isNumber()) |
666 |
|
{ |
667 |
|
results[currNode] = EvalResult(Rational(s.toNumber())); |
668 |
|
} |
669 |
|
else |
670 |
|
{ |
671 |
|
results[currNode] = EvalResult(Rational(-1)); |
672 |
|
} |
673 |
|
break; |
674 |
|
} |
675 |
|
|
676 |
|
case kind::STRING_FROM_CODE: |
677 |
|
{ |
678 |
|
Integer i = results[currNode[0]].d_rat.getNumerator(); |
679 |
|
if (i >= 0 && i < strings::utils::getAlphabetCardinality()) |
680 |
|
{ |
681 |
|
std::vector<unsigned> svec = {i.toUnsignedInt()}; |
682 |
|
results[currNode] = EvalResult(String(svec)); |
683 |
|
} |
684 |
|
else |
685 |
|
{ |
686 |
|
results[currNode] = EvalResult(String("")); |
687 |
|
} |
688 |
|
break; |
689 |
|
} |
690 |
|
|
691 |
4 |
case kind::STRING_TO_CODE: |
692 |
|
{ |
693 |
4 |
const String& s = results[currNode[0]].d_str; |
694 |
4 |
if (s.size() == 1) |
695 |
|
{ |
696 |
2 |
results[currNode] = EvalResult(Rational(s.getVec()[0])); |
697 |
|
} |
698 |
|
else |
699 |
|
{ |
700 |
2 |
results[currNode] = EvalResult(Rational(-1)); |
701 |
|
} |
702 |
4 |
break; |
703 |
|
} |
704 |
|
|
705 |
984064 |
case kind::CONST_BITVECTOR: |
706 |
984064 |
results[currNode] = EvalResult(currNodeVal.getConst<BitVector>()); |
707 |
984064 |
break; |
708 |
|
|
709 |
70531 |
case kind::BITVECTOR_NOT: |
710 |
70531 |
results[currNode] = EvalResult(~results[currNode[0]].d_bv); |
711 |
70531 |
break; |
712 |
|
|
713 |
31336 |
case kind::BITVECTOR_NEG: |
714 |
31336 |
results[currNode] = EvalResult(-results[currNode[0]].d_bv); |
715 |
31336 |
break; |
716 |
|
|
717 |
80482 |
case kind::BITVECTOR_EXTRACT: |
718 |
|
{ |
719 |
80482 |
unsigned lo = bv::utils::getExtractLow(currNodeVal); |
720 |
80482 |
unsigned hi = bv::utils::getExtractHigh(currNodeVal); |
721 |
80482 |
results[currNode] = |
722 |
160964 |
EvalResult(results[currNode[0]].d_bv.extract(hi, lo)); |
723 |
80482 |
break; |
724 |
|
} |
725 |
|
|
726 |
63311 |
case kind::BITVECTOR_CONCAT: |
727 |
|
{ |
728 |
126622 |
BitVector res = results[currNode[0]].d_bv; |
729 |
130662 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
730 |
|
{ |
731 |
67351 |
res = res.concat(results[currNode[i]].d_bv); |
732 |
|
} |
733 |
63311 |
results[currNode] = EvalResult(res); |
734 |
63311 |
break; |
735 |
|
} |
736 |
|
|
737 |
52047 |
case kind::BITVECTOR_ADD: |
738 |
|
{ |
739 |
104094 |
BitVector res = results[currNode[0]].d_bv; |
740 |
104094 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
741 |
|
{ |
742 |
52047 |
res = res + results[currNode[i]].d_bv; |
743 |
|
} |
744 |
52047 |
results[currNode] = EvalResult(res); |
745 |
52047 |
break; |
746 |
|
} |
747 |
|
|
748 |
81729 |
case kind::BITVECTOR_MULT: |
749 |
|
{ |
750 |
163458 |
BitVector res = results[currNode[0]].d_bv; |
751 |
175027 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
752 |
|
{ |
753 |
93298 |
res = res * results[currNode[i]].d_bv; |
754 |
|
} |
755 |
81729 |
results[currNode] = EvalResult(res); |
756 |
81729 |
break; |
757 |
|
} |
758 |
67026 |
case kind::BITVECTOR_AND: |
759 |
|
{ |
760 |
134052 |
BitVector res = results[currNode[0]].d_bv; |
761 |
134241 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
762 |
|
{ |
763 |
67215 |
res = res & results[currNode[i]].d_bv; |
764 |
|
} |
765 |
67026 |
results[currNode] = EvalResult(res); |
766 |
67026 |
break; |
767 |
|
} |
768 |
|
|
769 |
117845 |
case kind::BITVECTOR_OR: |
770 |
|
{ |
771 |
235690 |
BitVector res = results[currNode[0]].d_bv; |
772 |
260264 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
773 |
|
{ |
774 |
142419 |
res = res | results[currNode[i]].d_bv; |
775 |
|
} |
776 |
117845 |
results[currNode] = EvalResult(res); |
777 |
117845 |
break; |
778 |
|
} |
779 |
|
|
780 |
126 |
case kind::BITVECTOR_XOR: |
781 |
|
{ |
782 |
252 |
BitVector res = results[currNode[0]].d_bv; |
783 |
252 |
for (size_t i = 1, end = currNode.getNumChildren(); i < end; i++) |
784 |
|
{ |
785 |
126 |
res = res ^ results[currNode[i]].d_bv; |
786 |
|
} |
787 |
126 |
results[currNode] = EvalResult(res); |
788 |
126 |
break; |
789 |
|
} |
790 |
24632 |
case kind::BITVECTOR_UDIV: |
791 |
|
{ |
792 |
49264 |
BitVector res = results[currNode[0]].d_bv; |
793 |
24632 |
res = res.unsignedDivTotal(results[currNode[1]].d_bv); |
794 |
24632 |
results[currNode] = EvalResult(res); |
795 |
24632 |
break; |
796 |
|
} |
797 |
26634 |
case kind::BITVECTOR_UREM: |
798 |
|
{ |
799 |
53268 |
BitVector res = results[currNode[0]].d_bv; |
800 |
26634 |
res = res.unsignedRemTotal(results[currNode[1]].d_bv); |
801 |
26634 |
results[currNode] = EvalResult(res); |
802 |
26634 |
break; |
803 |
|
} |
804 |
|
|
805 |
138474 |
case kind::EQUAL: |
806 |
|
{ |
807 |
276948 |
EvalResult lhs = results[currNode[0]]; |
808 |
276948 |
EvalResult rhs = results[currNode[1]]; |
809 |
|
|
810 |
138474 |
switch (lhs.d_tag) |
811 |
|
{ |
812 |
40663 |
case EvalResult::BOOL: |
813 |
|
{ |
814 |
40663 |
results[currNode] = EvalResult(lhs.d_bool == rhs.d_bool); |
815 |
40663 |
break; |
816 |
|
} |
817 |
|
|
818 |
22611 |
case EvalResult::BITVECTOR: |
819 |
|
{ |
820 |
22611 |
results[currNode] = EvalResult(lhs.d_bv == rhs.d_bv); |
821 |
22611 |
break; |
822 |
|
} |
823 |
|
|
824 |
74637 |
case EvalResult::RATIONAL: |
825 |
|
{ |
826 |
74637 |
results[currNode] = EvalResult(lhs.d_rat == rhs.d_rat); |
827 |
74637 |
break; |
828 |
|
} |
829 |
|
|
830 |
563 |
case EvalResult::STRING: |
831 |
|
{ |
832 |
563 |
results[currNode] = EvalResult(lhs.d_str == rhs.d_str); |
833 |
563 |
break; |
834 |
|
} |
835 |
|
case EvalResult::UCONST: |
836 |
|
{ |
837 |
|
results[currNode] = EvalResult(lhs.d_uc == rhs.d_uc); |
838 |
|
break; |
839 |
|
} |
840 |
|
|
841 |
|
default: |
842 |
|
{ |
843 |
|
Trace("evaluator") << "Theory " << Theory::theoryOf(currNode[0]) |
844 |
|
<< " not supported" << std::endl; |
845 |
|
results[currNode] = EvalResult(); |
846 |
|
evalAsNode[currNode] = |
847 |
|
needsReconstruct ? reconstruct(currNode, results, evalAsNode) |
848 |
|
: currNodeVal; |
849 |
|
break; |
850 |
|
} |
851 |
|
} |
852 |
|
|
853 |
138474 |
break; |
854 |
|
} |
855 |
|
|
856 |
237551 |
case kind::ITE: |
857 |
|
{ |
858 |
237551 |
if (results[currNode[0]].d_bool) |
859 |
|
{ |
860 |
85364 |
results[currNode] = results[currNode[1]]; |
861 |
|
} |
862 |
|
else |
863 |
|
{ |
864 |
152187 |
results[currNode] = results[currNode[2]]; |
865 |
|
} |
866 |
237551 |
break; |
867 |
|
} |
868 |
|
|
869 |
143946 |
default: |
870 |
|
{ |
871 |
287892 |
Trace("evaluator") << "Kind " << currNodeVal.getKind() |
872 |
143946 |
<< " not supported" << std::endl; |
873 |
143946 |
results[currNode] = EvalResult(); |
874 |
143946 |
evalAsNode[currNode] = |
875 |
287892 |
needsReconstruct ? reconstruct(currNode, results, evalAsNode) |
876 |
|
: currNodeVal; |
877 |
|
} |
878 |
|
} |
879 |
|
} |
880 |
|
} |
881 |
|
|
882 |
1872770 |
return results[n]; |
883 |
|
} |
884 |
|
|
885 |
284187 |
Node Evaluator::reconstruct(TNode n, |
886 |
|
std::unordered_map<TNode, EvalResult>& eresults, |
887 |
|
std::unordered_map<TNode, Node>& evalAsNode) const |
888 |
|
{ |
889 |
284187 |
if (n.getNumChildren() == 0) |
890 |
|
{ |
891 |
3071 |
return n; |
892 |
|
} |
893 |
281116 |
Trace("evaluator") << "Evaluator: reconstruct " << n << std::endl; |
894 |
281116 |
NodeManager* nm = NodeManager::currentNM(); |
895 |
281116 |
std::unordered_map<TNode, EvalResult>::iterator itr; |
896 |
281116 |
std::unordered_map<TNode, Node>::iterator itn; |
897 |
562232 |
std::vector<Node> echildren; |
898 |
281116 |
if (n.getMetaKind() == kind::metakind::PARAMETERIZED) |
899 |
|
{ |
900 |
70674 |
TNode op = n.getOperator(); |
901 |
35337 |
if (op.isConst()) |
902 |
|
{ |
903 |
155 |
echildren.push_back(op); |
904 |
|
} |
905 |
|
else |
906 |
|
{ |
907 |
35182 |
itr = eresults.find(op); |
908 |
35182 |
Assert(itr != eresults.end()); |
909 |
35182 |
if (itr->second.d_tag == EvalResult::INVALID) |
910 |
|
{ |
911 |
|
// could not evaluate the operator, look in the node cache |
912 |
35182 |
itn = evalAsNode.find(op); |
913 |
35182 |
Assert(itn != evalAsNode.end()); |
914 |
35182 |
echildren.push_back(itn->second); |
915 |
|
} |
916 |
|
else |
917 |
|
{ |
918 |
|
// otherwise, use the evaluation of the operator |
919 |
|
echildren.push_back(itr->second.toNode()); |
920 |
|
} |
921 |
|
} |
922 |
|
} |
923 |
885984 |
for (const auto& currNodeChild : n) |
924 |
|
{ |
925 |
604868 |
itr = eresults.find(currNodeChild); |
926 |
604868 |
Assert(itr != eresults.end()); |
927 |
604868 |
if (itr->second.d_tag == EvalResult::INVALID) |
928 |
|
{ |
929 |
|
// could not evaluate this child, look in the node cache |
930 |
187329 |
itn = evalAsNode.find(currNodeChild); |
931 |
187329 |
Assert(itn != evalAsNode.end()); |
932 |
187329 |
echildren.push_back(itn->second); |
933 |
|
} |
934 |
|
else |
935 |
|
{ |
936 |
|
// otherwise, use the evaluation |
937 |
417539 |
echildren.push_back(itr->second.toNode()); |
938 |
|
} |
939 |
|
} |
940 |
|
// The value is the result of our (partially) successful evaluation |
941 |
|
// of the children. |
942 |
562232 |
Node nn = nm->mkNode(n.getKind(), echildren); |
943 |
281116 |
Trace("evaluator") << "Evaluator: reconstructed " << nn << std::endl; |
944 |
|
// Return node, without rewriting. Notice we do not need to substitute here |
945 |
|
// since all substitutions should already have been applied recursively. |
946 |
281116 |
return nn; |
947 |
|
} |
948 |
|
|
949 |
|
} // namespace theory |
950 |
29574 |
} // namespace cvc5 |