1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Andres Noetzli, Yoni Zohar |
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 a cache of skolems for theory of strings. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/strings/skolem_cache.h" |
17 |
|
|
18 |
|
#include "expr/attribute.h" |
19 |
|
#include "expr/bound_var_manager.h" |
20 |
|
#include "expr/skolem_manager.h" |
21 |
|
#include "theory/rewriter.h" |
22 |
|
#include "theory/strings/arith_entail.h" |
23 |
|
#include "theory/strings/theory_strings_utils.h" |
24 |
|
#include "theory/strings/word.h" |
25 |
|
#include "util/rational.h" |
26 |
|
|
27 |
|
using namespace cvc5::kind; |
28 |
|
|
29 |
|
namespace cvc5 { |
30 |
|
namespace theory { |
31 |
|
namespace strings { |
32 |
|
|
33 |
|
/** |
34 |
|
* A bound variable corresponding to the universally quantified integer |
35 |
|
* variable used to range over the valid positions in a string, used |
36 |
|
* for axiomatizing the behavior of some term. |
37 |
|
*/ |
38 |
|
struct IndexVarAttributeId |
39 |
|
{ |
40 |
|
}; |
41 |
|
typedef expr::Attribute<IndexVarAttributeId, Node> IndexVarAttribute; |
42 |
|
|
43 |
|
/** |
44 |
|
* A bound variable corresponding to the universally quantified integer |
45 |
|
* variable used to range over the valid lengths of a string, used for |
46 |
|
* axiomatizing the behavior of some term. |
47 |
|
*/ |
48 |
|
struct LengthVarAttributeId |
49 |
|
{ |
50 |
|
}; |
51 |
|
typedef expr::Attribute<LengthVarAttributeId, Node> LengthVarAttribute; |
52 |
|
|
53 |
16425 |
SkolemCache::SkolemCache(Rewriter* rr) : d_rr(rr) |
54 |
|
{ |
55 |
16425 |
NodeManager* nm = NodeManager::currentNM(); |
56 |
16425 |
d_strType = nm->stringType(); |
57 |
16425 |
d_zero = nm->mkConst(Rational(0)); |
58 |
16425 |
} |
59 |
|
|
60 |
40961 |
Node SkolemCache::mkSkolemCached(Node a, Node b, SkolemId id, const char* c) |
61 |
|
{ |
62 |
40961 |
return mkTypedSkolemCached(d_strType, a, b, id, c); |
63 |
|
} |
64 |
|
|
65 |
18017 |
Node SkolemCache::mkSkolemCached(Node a, SkolemId id, const char* c) |
66 |
|
{ |
67 |
18017 |
return mkSkolemCached(a, Node::null(), id, c); |
68 |
|
} |
69 |
|
|
70 |
41304 |
Node SkolemCache::mkTypedSkolemCached( |
71 |
|
TypeNode tn, Node a, Node b, SkolemId id, const char* c) |
72 |
|
{ |
73 |
82608 |
Trace("skolem-cache") << "mkTypedSkolemCached start: (" << id << ", " << a |
74 |
41304 |
<< ", " << b << ")" << std::endl; |
75 |
41304 |
SkolemId idOrig = id; |
76 |
|
// do not rewrite beforehand if we are not using optimizations, this is so |
77 |
|
// that the proof checker does not depend on the rewriter. |
78 |
41304 |
if (d_rr != nullptr) |
79 |
|
{ |
80 |
39114 |
a = a.isNull() ? a : d_rr->rewrite(a); |
81 |
39114 |
b = b.isNull() ? b : d_rr->rewrite(b); |
82 |
|
} |
83 |
41304 |
std::tie(id, a, b) = normalizeStringSkolem(id, a, b); |
84 |
|
|
85 |
|
// optimization: if we aren't asking for the purification skolem for constant |
86 |
|
// a, and the skolem is equivalent to a, then we just return a. |
87 |
41304 |
if (d_rr != nullptr && idOrig != SK_PURIFY && id == SK_PURIFY && a.isConst()) |
88 |
|
{ |
89 |
1764 |
Trace("skolem-cache") << "...optimization: return constant " << a |
90 |
882 |
<< std::endl; |
91 |
882 |
return a; |
92 |
|
} |
93 |
|
|
94 |
40422 |
std::map<SkolemId, Node>::iterator it = d_skolemCache[a][b].find(id); |
95 |
40422 |
if (it != d_skolemCache[a][b].end()) |
96 |
|
{ |
97 |
21961 |
Trace("skolem-cache") << "...return existing " << it->second << std::endl; |
98 |
|
// already cached |
99 |
21961 |
return it->second; |
100 |
|
} |
101 |
|
|
102 |
18461 |
NodeManager* nm = NodeManager::currentNM(); |
103 |
18461 |
SkolemManager* sm = nm->getSkolemManager(); |
104 |
36922 |
Node sk; |
105 |
18461 |
switch (id) |
106 |
|
{ |
107 |
|
// exists k. k = a |
108 |
18461 |
case SK_PURIFY: |
109 |
|
{ |
110 |
|
// for sequences of Booleans, we may purify Boolean terms, in which case |
111 |
|
// they must be Boolean term variables. |
112 |
36922 |
int flags = a.getType().isBoolean() ? SkolemManager::SKOLEM_BOOL_TERM_VAR |
113 |
18461 |
: SkolemManager::SKOLEM_DEFAULT; |
114 |
18461 |
sk = sm->mkPurifySkolem(a, c, "string purify skolem", flags); |
115 |
|
} |
116 |
18461 |
break; |
117 |
|
// these are eliminated by normalizeStringSkolem |
118 |
|
case SK_ID_V_SPT: |
119 |
|
case SK_ID_V_SPT_REV: |
120 |
|
case SK_ID_VC_SPT: |
121 |
|
case SK_ID_VC_SPT_REV: |
122 |
|
case SK_ID_C_SPT: |
123 |
|
case SK_ID_C_SPT_REV: |
124 |
|
case SK_ID_DC_SPT: |
125 |
|
case SK_ID_DC_SPT_REM: |
126 |
|
case SK_ID_DEQ_X: |
127 |
|
case SK_ID_DEQ_Y: |
128 |
|
case SK_FIRST_CTN_PRE: |
129 |
|
case SK_FIRST_CTN_POST: |
130 |
|
case SK_PREFIX: |
131 |
|
case SK_SUFFIX_REM: |
132 |
|
Unhandled() << "Expected to eliminate Skolem ID " << id << std::endl; |
133 |
|
break; |
134 |
|
default: |
135 |
|
{ |
136 |
|
Notice() << "Don't know how to handle Skolem ID " << id << std::endl; |
137 |
|
Node v = nm->mkBoundVar(tn); |
138 |
|
Node cond = nm->mkConst(true); |
139 |
|
sk = sm->mkSkolem(v, cond, c, "string skolem"); |
140 |
|
} |
141 |
|
break; |
142 |
|
} |
143 |
18461 |
Trace("skolem-cache") << "...returned " << sk << std::endl; |
144 |
18461 |
d_allSkolems.insert(sk); |
145 |
18461 |
d_skolemCache[a][b][id] = sk; |
146 |
18461 |
return sk; |
147 |
|
} |
148 |
343 |
Node SkolemCache::mkTypedSkolemCached(TypeNode tn, |
149 |
|
Node a, |
150 |
|
SkolemId id, |
151 |
|
const char* c) |
152 |
|
{ |
153 |
343 |
return mkTypedSkolemCached(tn, a, Node::null(), id, c); |
154 |
|
} |
155 |
|
|
156 |
56 |
Node SkolemCache::mkSkolemSeqNth(TypeNode seqType, const char* c) |
157 |
|
{ |
158 |
|
// Note this method is static and does not rely on any local caching. |
159 |
|
// It is used by expand definitions and by (dynamic) reductions, thus |
160 |
|
// it is centrally located here. |
161 |
56 |
Assert(seqType.isSequence()); |
162 |
56 |
NodeManager* nm = NodeManager::currentNM(); |
163 |
56 |
SkolemManager* sm = nm->getSkolemManager(); |
164 |
112 |
std::vector<TypeNode> argTypes; |
165 |
56 |
argTypes.push_back(seqType); |
166 |
56 |
argTypes.push_back(nm->integerType()); |
167 |
112 |
TypeNode elemType = seqType.getSequenceElementType(); |
168 |
112 |
TypeNode ufType = nm->mkFunctionType(argTypes, elemType); |
169 |
112 |
return sm->mkSkolemFunction(SkolemFunId::SEQ_NTH_OOB, ufType); |
170 |
|
} |
171 |
|
|
172 |
1170 |
Node SkolemCache::mkSkolem(const char* c) |
173 |
|
{ |
174 |
|
// TODO: eliminate this |
175 |
1170 |
SkolemManager* sm = NodeManager::currentNM()->getSkolemManager(); |
176 |
1170 |
Node n = sm->mkDummySkolem(c, d_strType, "string skolem"); |
177 |
1170 |
d_allSkolems.insert(n); |
178 |
1170 |
return n; |
179 |
|
} |
180 |
|
|
181 |
4648 |
bool SkolemCache::isSkolem(Node n) const |
182 |
|
{ |
183 |
4648 |
return d_allSkolems.find(n) != d_allSkolems.end(); |
184 |
|
} |
185 |
|
|
186 |
|
std::tuple<SkolemCache::SkolemId, Node, Node> |
187 |
41304 |
SkolemCache::normalizeStringSkolem(SkolemId id, Node a, Node b) |
188 |
|
{ |
189 |
|
|
190 |
41304 |
NodeManager* nm = NodeManager::currentNM(); |
191 |
|
|
192 |
|
// eliminate in terms of prefix/suffix_rem |
193 |
41304 |
if (id == SK_FIRST_CTN_POST) |
194 |
|
{ |
195 |
|
// SK_FIRST_CTN_POST(x, y) ---> |
196 |
|
// SK_SUFFIX_REM(x, (+ (str.len SK_FIRST_CTN_PRE(x, y)) (str.len y))) |
197 |
4473 |
id = SK_SUFFIX_REM; |
198 |
8946 |
Node pre = mkSkolemCached(a, b, SK_FIRST_CTN_PRE, "pre"); |
199 |
13419 |
b = nm->mkNode( |
200 |
17892 |
PLUS, nm->mkNode(STRING_LENGTH, pre), nm->mkNode(STRING_LENGTH, b)); |
201 |
|
} |
202 |
36831 |
else if (id == SK_ID_V_SPT || id == SK_ID_C_SPT) |
203 |
|
{ |
204 |
|
// SK_ID_*_SPT(x, y) ---> SK_SUFFIX_REM(x, (str.len y)) |
205 |
492 |
id = SK_SUFFIX_REM; |
206 |
492 |
b = nm->mkNode(STRING_LENGTH, b); |
207 |
|
} |
208 |
36339 |
else if (id == SK_ID_V_SPT_REV || id == SK_ID_C_SPT_REV) |
209 |
|
{ |
210 |
|
// SK_ID_*_SPT_REV(x, y) ---> SK_PREFIX(x, (- (str.len x) (str.len y))) |
211 |
487 |
id = SK_PREFIX; |
212 |
1948 |
b = nm->mkNode( |
213 |
1948 |
MINUS, nm->mkNode(STRING_LENGTH, a), nm->mkNode(STRING_LENGTH, b)); |
214 |
|
} |
215 |
35852 |
else if (id == SK_ID_VC_SPT) |
216 |
|
{ |
217 |
|
// SK_ID_VC_SPT(x, y) ---> SK_SUFFIX_REM(x, 1) |
218 |
2677 |
id = SK_SUFFIX_REM; |
219 |
2677 |
b = nm->mkConst(Rational(1)); |
220 |
|
} |
221 |
33175 |
else if (id == SK_ID_VC_SPT_REV) |
222 |
|
{ |
223 |
|
// SK_ID_VC_SPT_REV(x, y) ---> SK_PREFIX(x, (- (str.len x) 1)) |
224 |
3216 |
id = SK_PREFIX; |
225 |
9648 |
b = nm->mkNode( |
226 |
12864 |
MINUS, nm->mkNode(STRING_LENGTH, a), nm->mkConst(Rational(1))); |
227 |
|
} |
228 |
29959 |
else if (id == SK_ID_DC_SPT) |
229 |
|
{ |
230 |
|
// SK_ID_DC_SPT(x, y) ---> SK_PREFIX(x, 1) |
231 |
|
id = SK_PREFIX; |
232 |
|
b = nm->mkConst(Rational(1)); |
233 |
|
} |
234 |
29959 |
else if (id == SK_ID_DC_SPT_REM) |
235 |
|
{ |
236 |
|
// SK_ID_DC_SPT_REM(x, y) ---> SK_SUFFIX_REM(x, 1) |
237 |
|
id = SK_SUFFIX_REM; |
238 |
|
b = nm->mkConst(Rational(1)); |
239 |
|
} |
240 |
29959 |
else if (id == SK_ID_DEQ_X) |
241 |
|
{ |
242 |
|
// SK_ID_DEQ_X(x, y) ---> SK_PREFIX(y, (str.len x)) |
243 |
|
id = SK_PREFIX; |
244 |
|
Node aOld = a; |
245 |
|
a = b; |
246 |
|
b = nm->mkNode(STRING_LENGTH, aOld); |
247 |
|
} |
248 |
29959 |
else if (id == SK_ID_DEQ_Y) |
249 |
|
{ |
250 |
|
// SK_ID_DEQ_Y(x, y) ---> SK_PREFIX(x, (str.len y)) |
251 |
|
id = SK_PREFIX; |
252 |
|
b = nm->mkNode(STRING_LENGTH, b); |
253 |
|
} |
254 |
29959 |
else if (id == SK_FIRST_CTN_PRE) |
255 |
|
{ |
256 |
|
// SK_FIRST_CTN_PRE(x,y) ---> SK_PREFIX(x, indexof(x,y,0)) |
257 |
8950 |
id = SK_PREFIX; |
258 |
8950 |
b = nm->mkNode(STRING_INDEXOF, a, b, d_zero); |
259 |
|
} |
260 |
|
|
261 |
41304 |
if (id == SK_ID_V_UNIFIED_SPT || id == SK_ID_V_UNIFIED_SPT_REV) |
262 |
|
{ |
263 |
4656 |
bool isRev = (id == SK_ID_V_UNIFIED_SPT_REV); |
264 |
9312 |
Node la = nm->mkNode(STRING_LENGTH, a); |
265 |
9312 |
Node lb = nm->mkNode(STRING_LENGTH, b); |
266 |
6659 |
Node ta = isRev ? utils::mkPrefix(a, nm->mkNode(MINUS, la, lb)) |
267 |
11315 |
: utils::mkSuffix(a, lb); |
268 |
6659 |
Node tb = isRev ? utils::mkPrefix(b, nm->mkNode(MINUS, lb, la)) |
269 |
11315 |
: utils::mkSuffix(b, la); |
270 |
4656 |
id = SK_PURIFY; |
271 |
|
// SK_ID_V_UNIFIED_SPT(x,y) ---> |
272 |
|
// ite(len(x) >= len(y), substr(x,0,str.len(y)), substr(y,0,str.len(x)) |
273 |
4656 |
a = nm->mkNode(ITE, nm->mkNode(GEQ, la, lb), ta, tb); |
274 |
4656 |
b = Node::null(); |
275 |
|
} |
276 |
|
|
277 |
|
// now, eliminate prefix/suffix_rem in terms of purify |
278 |
41304 |
if (id == SK_PREFIX) |
279 |
|
{ |
280 |
|
// SK_PREFIX(x,y) ---> SK_PURIFY(substr(x,0,y)) |
281 |
14596 |
id = SK_PURIFY; |
282 |
14596 |
a = utils::mkPrefix(a, b); |
283 |
14596 |
b = Node::null(); |
284 |
|
} |
285 |
26708 |
else if (id == SK_SUFFIX_REM) |
286 |
|
{ |
287 |
|
// SK_SUFFIX_REM(x,y) ---> SK_PURIFY(substr(x,y,str.len(x)-y)) |
288 |
9585 |
id = SK_PURIFY; |
289 |
9585 |
a = utils::mkSuffix(a, b); |
290 |
9585 |
b = Node::null(); |
291 |
|
} |
292 |
|
|
293 |
41304 |
if (d_rr != nullptr) |
294 |
|
{ |
295 |
39114 |
a = a.isNull() ? a : d_rr->rewrite(a); |
296 |
39114 |
b = b.isNull() ? b : d_rr->rewrite(b); |
297 |
|
} |
298 |
82608 |
Trace("skolem-cache") << "normalizeStringSkolem end: (" << id << ", " << a |
299 |
41304 |
<< ", " << b << ")" << std::endl; |
300 |
41304 |
return std::make_tuple(id, a, b); |
301 |
|
} |
302 |
|
|
303 |
455 |
Node SkolemCache::mkIndexVar(Node t) |
304 |
|
{ |
305 |
455 |
NodeManager* nm = NodeManager::currentNM(); |
306 |
910 |
TypeNode intType = nm->integerType(); |
307 |
455 |
BoundVarManager* bvm = nm->getBoundVarManager(); |
308 |
910 |
return bvm->mkBoundVar<IndexVarAttribute>(t, intType); |
309 |
|
} |
310 |
|
|
311 |
176 |
Node SkolemCache::mkLengthVar(Node t) |
312 |
|
{ |
313 |
176 |
NodeManager* nm = NodeManager::currentNM(); |
314 |
352 |
TypeNode intType = nm->integerType(); |
315 |
176 |
BoundVarManager* bvm = nm->getBoundVarManager(); |
316 |
352 |
return bvm->mkBoundVar<LengthVarAttribute>(t, intType); |
317 |
|
} |
318 |
|
|
319 |
446 |
Node SkolemCache::mkSkolemFun(SkolemFunId id, TypeNode tn, Node a, Node b) |
320 |
|
{ |
321 |
892 |
std::vector<Node> cacheVals; |
322 |
1338 |
for (size_t i = 0; i < 2; i++) |
323 |
|
{ |
324 |
1784 |
Node n = i == 0 ? a : b; |
325 |
892 |
if (!n.isNull()) |
326 |
|
{ |
327 |
706 |
n = d_rr != nullptr ? d_rr->rewrite(n) : n; |
328 |
706 |
cacheVals.push_back(n); |
329 |
|
} |
330 |
|
} |
331 |
446 |
NodeManager* nm = NodeManager::currentNM(); |
332 |
446 |
SkolemManager* sm = nm->getSkolemManager(); |
333 |
446 |
Node k = sm->mkSkolemFunction(id, tn, cacheVals); |
334 |
446 |
d_allSkolems.insert(k); |
335 |
892 |
return k; |
336 |
|
} |
337 |
|
|
338 |
|
} // namespace strings |
339 |
|
} // namespace theory |
340 |
31125 |
} // namespace cvc5 |