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 |
|
* Rewriter for the theory of strings and sequences. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "cvc5_private.h" |
17 |
|
|
18 |
|
#ifndef CVC5__THEORY__STRINGS__SEQUENCES_REWRITER_H |
19 |
|
#define CVC5__THEORY__STRINGS__SEQUENCES_REWRITER_H |
20 |
|
|
21 |
|
#include <vector> |
22 |
|
|
23 |
|
#include "expr/node.h" |
24 |
|
#include "theory/strings/rewrites.h" |
25 |
|
#include "theory/strings/sequences_stats.h" |
26 |
|
#include "theory/strings/strings_entail.h" |
27 |
|
#include "theory/theory_rewriter.h" |
28 |
|
|
29 |
|
namespace cvc5 { |
30 |
|
namespace theory { |
31 |
|
namespace strings { |
32 |
|
|
33 |
9649 |
class SequencesRewriter : public TheoryRewriter |
34 |
|
{ |
35 |
|
public: |
36 |
|
SequencesRewriter(HistogramStat<Rewrite>* statistics); |
37 |
|
|
38 |
|
protected: |
39 |
|
/** rewrite regular expression concatenation |
40 |
|
* |
41 |
|
* This is the entry point for post-rewriting applications of re.++. |
42 |
|
* Returns the rewritten form of node. |
43 |
|
*/ |
44 |
|
Node rewriteConcatRegExp(TNode node); |
45 |
|
/** rewrite regular expression star |
46 |
|
* |
47 |
|
* This is the entry point for post-rewriting applications of re.*. |
48 |
|
* Returns the rewritten form of node. |
49 |
|
*/ |
50 |
|
Node rewriteStarRegExp(TNode node); |
51 |
|
/** rewrite regular expression intersection/union |
52 |
|
* |
53 |
|
* This is the entry point for post-rewriting applications of re.inter and |
54 |
|
* re.union. Returns the rewritten form of node. |
55 |
|
*/ |
56 |
|
Node rewriteAndOrRegExp(TNode node); |
57 |
|
/** rewrite regular expression loop |
58 |
|
* |
59 |
|
* This is the entry point for post-rewriting applications of re.loop. |
60 |
|
* Returns the rewritten form of node. |
61 |
|
*/ |
62 |
|
Node rewriteLoopRegExp(TNode node); |
63 |
|
/** rewrite regular expression repeat |
64 |
|
* |
65 |
|
* This is the entry point for post-rewriting applications of re.repeat. |
66 |
|
* Returns the rewritten form of node. |
67 |
|
*/ |
68 |
|
Node rewriteRepeatRegExp(TNode node); |
69 |
|
/** rewrite regular expression option |
70 |
|
* |
71 |
|
* This is the entry point for post-rewriting applications of re.opt. |
72 |
|
* Returns the rewritten form of node. |
73 |
|
*/ |
74 |
|
Node rewriteOptionRegExp(TNode node); |
75 |
|
/** rewrite regular expression plus |
76 |
|
* |
77 |
|
* This is the entry point for post-rewriting applications of re.+. |
78 |
|
* Returns the rewritten form of node. |
79 |
|
*/ |
80 |
|
Node rewritePlusRegExp(TNode node); |
81 |
|
/** rewrite regular expression difference |
82 |
|
* |
83 |
|
* This is the entry point for post-rewriting applications of re.diff. |
84 |
|
* Returns the rewritten form of node. |
85 |
|
*/ |
86 |
|
Node rewriteDifferenceRegExp(TNode node); |
87 |
|
/** rewrite regular expression range |
88 |
|
* |
89 |
|
* This is the entry point for post-rewriting applications of re.range. |
90 |
|
* Returns the rewritten form of node. |
91 |
|
*/ |
92 |
|
Node rewriteRangeRegExp(TNode node); |
93 |
|
|
94 |
|
/** rewrite regular expression membership |
95 |
|
* |
96 |
|
* This is the entry point for post-rewriting applications of str.in.re |
97 |
|
* Returns the rewritten form of node. |
98 |
|
*/ |
99 |
|
Node rewriteMembership(TNode node); |
100 |
|
|
101 |
|
/** rewrite string equality extended |
102 |
|
* |
103 |
|
* This method returns a formula that is equivalent to the equality between |
104 |
|
* two strings s = t, given by node. It is called by rewriteEqualityExt. |
105 |
|
*/ |
106 |
|
Node rewriteStrEqualityExt(Node node); |
107 |
|
/** rewrite arithmetic equality extended |
108 |
|
* |
109 |
|
* This method returns a formula that is equivalent to the equality between |
110 |
|
* two arithmetic string terms s = t, given by node. t is called by |
111 |
|
* rewriteEqualityExt. |
112 |
|
*/ |
113 |
|
Node rewriteArithEqualityExt(Node node); |
114 |
|
/** |
115 |
|
* Called when node rewrites to ret. |
116 |
|
* |
117 |
|
* The rewrite r indicates the justification for the rewrite, which is printed |
118 |
|
* by this function for debugging. |
119 |
|
* |
120 |
|
* If node is not an equality and ret is an equality, this method applies |
121 |
|
* an additional rewrite step (rewriteEqualityExt) that performs |
122 |
|
* additional rewrites on ret, after which we return the result of this call. |
123 |
|
* Otherwise, this method simply returns ret. |
124 |
|
*/ |
125 |
|
Node returnRewrite(Node node, Node ret, Rewrite r); |
126 |
|
|
127 |
|
public: |
128 |
|
RewriteResponse postRewrite(TNode node) override; |
129 |
|
RewriteResponse preRewrite(TNode node) override; |
130 |
|
/** Expand definition */ |
131 |
|
TrustNode expandDefinition(Node n) override; |
132 |
|
|
133 |
|
/** rewrite equality |
134 |
|
* |
135 |
|
* This method returns a formula that is equivalent to the equality between |
136 |
|
* two strings s = t, given by node. The result of rewrite is one of |
137 |
|
* { s = t, t = s, true, false }. |
138 |
|
*/ |
139 |
|
Node rewriteEquality(Node node); |
140 |
|
/** rewrite equality extended |
141 |
|
* |
142 |
|
* This method returns a formula that is equivalent to the equality between |
143 |
|
* two terms s = t, given by node, where s and t are terms in the signature |
144 |
|
* of the theory of strings. Notice that s and t may be of string type or |
145 |
|
* of Int type. |
146 |
|
* |
147 |
|
* Specifically, this function performs rewrites whose conclusion is not |
148 |
|
* necessarily one of { s = t, t = s, true, false }. |
149 |
|
*/ |
150 |
|
Node rewriteEqualityExt(Node node) override; |
151 |
|
/** rewrite string length |
152 |
|
* This is the entry point for post-rewriting terms node of the form |
153 |
|
* str.len( t ) |
154 |
|
* Returns the rewritten form of node. |
155 |
|
*/ |
156 |
|
Node rewriteLength(Node node); |
157 |
|
/** rewrite concat |
158 |
|
* This is the entry point for post-rewriting terms node of the form |
159 |
|
* str.++( t1, .., tn ) |
160 |
|
* Returns the rewritten form of node. |
161 |
|
*/ |
162 |
|
Node rewriteConcat(Node node); |
163 |
|
/** rewrite character at |
164 |
|
* This is the entry point for post-rewriting terms node of the form |
165 |
|
* str.charat( s, i1 ) |
166 |
|
* Returns the rewritten form of node. |
167 |
|
*/ |
168 |
|
Node rewriteCharAt(Node node); |
169 |
|
/** rewrite substr |
170 |
|
* This is the entry point for post-rewriting terms node of the form |
171 |
|
* str.substr( s, i1, i2 ) |
172 |
|
* Returns the rewritten form of node. |
173 |
|
*/ |
174 |
|
Node rewriteSubstr(Node node); |
175 |
|
/** rewrite update |
176 |
|
* This is the entry point for post-rewriting terms node of the form |
177 |
|
* str.update( s, i1, i2 ) |
178 |
|
* Returns the rewritten form of node. |
179 |
|
*/ |
180 |
|
Node rewriteUpdate(Node node); |
181 |
|
/** rewrite contains |
182 |
|
* This is the entry point for post-rewriting terms node of the form |
183 |
|
* str.contains( t, s ) |
184 |
|
* Returns the rewritten form of node. |
185 |
|
* |
186 |
|
* For details on some of the basic rewrites done in this function, see Figure |
187 |
|
* 7 of Reynolds et al "Scaling Up DPLL(T) String Solvers Using |
188 |
|
* Context-Dependent Rewriting", CAV 2017. |
189 |
|
*/ |
190 |
|
Node rewriteContains(Node node); |
191 |
|
/** rewrite indexof |
192 |
|
* This is the entry point for post-rewriting terms n of the form |
193 |
|
* str.indexof( s, t, n ) |
194 |
|
* Returns the rewritten form of node. |
195 |
|
*/ |
196 |
|
Node rewriteIndexof(Node node); |
197 |
|
/** rewrite replace |
198 |
|
* This is the entry point for post-rewriting terms n of the form |
199 |
|
* str.replace( s, t, r ) |
200 |
|
* Returns the rewritten form of node. |
201 |
|
*/ |
202 |
|
Node rewriteReplace(Node node); |
203 |
|
/** rewrite replace all |
204 |
|
* This is the entry point for post-rewriting terms n of the form |
205 |
|
* str.replaceall( s, t, r ) |
206 |
|
* Returns the rewritten form of node. |
207 |
|
*/ |
208 |
|
Node rewriteReplaceAll(Node node); |
209 |
|
/** rewrite replace internal |
210 |
|
* |
211 |
|
* This method implements rewrite rules that apply to both str.replace and |
212 |
|
* str.replaceall. If it returns a non-null ret, then node rewrites to ret. |
213 |
|
*/ |
214 |
|
Node rewriteReplaceInternal(Node node); |
215 |
|
/** rewrite regular expression replace |
216 |
|
* |
217 |
|
* This method implements rewrite rules that apply to terms of the form |
218 |
|
* str.replace_re(s, r, t). |
219 |
|
* |
220 |
|
* @param node The node to rewrite |
221 |
|
* @return The rewritten node |
222 |
|
*/ |
223 |
|
Node rewriteReplaceRe(Node node); |
224 |
|
/** rewrite regular expression replace |
225 |
|
* |
226 |
|
* This method implements rewrite rules that apply to terms of the form |
227 |
|
* str.replace_re_all(s, r, t). |
228 |
|
* |
229 |
|
* @param node The node to rewrite |
230 |
|
* @return The rewritten node |
231 |
|
*/ |
232 |
|
Node rewriteReplaceReAll(Node node); |
233 |
|
/** |
234 |
|
* Returns the first, shortest sequence in n that matches r. |
235 |
|
* |
236 |
|
* @param n The constant string or sequence to search in. |
237 |
|
* @param r The regular expression to search for. |
238 |
|
* @return A pair holding the start position and the end position of the |
239 |
|
* match or a pair of string::npos if r does not appear in n. |
240 |
|
*/ |
241 |
|
std::pair<size_t, size_t> firstMatch(Node n, Node r); |
242 |
|
/** rewrite string reverse |
243 |
|
* |
244 |
|
* This is the entry point for post-rewriting terms n of the form |
245 |
|
* str.rev( s ) |
246 |
|
* Returns the rewritten form of node. |
247 |
|
*/ |
248 |
|
Node rewriteStrReverse(Node node); |
249 |
|
/** rewrite prefix/suffix |
250 |
|
* This is the entry point for post-rewriting terms n of the form |
251 |
|
* str.prefixof( s, t ) / str.suffixof( s, t ) |
252 |
|
* Returns the rewritten form of node. |
253 |
|
*/ |
254 |
|
Node rewritePrefixSuffix(Node node); |
255 |
|
|
256 |
|
/** rewrite str.to_code |
257 |
|
* This is the entry point for post-rewriting terms n of the form |
258 |
|
* str.to_code( t ) |
259 |
|
* Returns the rewritten form of node. |
260 |
|
*/ |
261 |
|
Node rewriteStringToCode(Node node); |
262 |
|
/** rewrite seq.unit |
263 |
|
* This is the entry point for post-rewriting terms n of the form |
264 |
|
* seq.unit( t ) |
265 |
|
* Returns the rewritten form of node. |
266 |
|
*/ |
267 |
|
Node rewriteSeqUnit(Node node); |
268 |
|
|
269 |
|
/** rewrite seq.nth |
270 |
|
* This is the entry point for post-rewriting terms n of the form |
271 |
|
* seq.nth(s, i) |
272 |
|
* Returns the rewritten form of node. |
273 |
|
*/ |
274 |
|
Node rewriteSeqNth(Node node); |
275 |
|
|
276 |
|
/** length preserving rewrite |
277 |
|
* |
278 |
|
* Given input n, this returns a string n' whose length is equivalent to n. |
279 |
|
* We apply certain normalizations to n', such as replacing all constants |
280 |
|
* that are not relevant to length by "A". |
281 |
|
*/ |
282 |
|
static Node lengthPreserveRewrite(Node n); |
283 |
|
|
284 |
|
/** |
285 |
|
* Given a symbolic length n, returns the canonical string (of type stype) |
286 |
|
* for that length. For example if n is constant, this function returns a |
287 |
|
* string consisting of "A" repeated n times. Returns the null node if no such |
288 |
|
* string exists. |
289 |
|
*/ |
290 |
|
static Node canonicalStrForSymbolicLength(Node n, TypeNode stype); |
291 |
|
|
292 |
|
/** Reference to the rewriter statistics. */ |
293 |
|
HistogramStat<Rewrite>* d_statistics; |
294 |
|
|
295 |
|
/** Instance of the entailment checker for strings. */ |
296 |
|
StringsEntail d_stringsEntail; |
297 |
|
}; /* class SequencesRewriter */ |
298 |
|
|
299 |
|
} // namespace strings |
300 |
|
} // namespace theory |
301 |
|
} // namespace cvc5 |
302 |
|
|
303 |
|
#endif /* CVC5__THEORY__STRINGS__SEQUENCES_REWRITER_H */ |