GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/strings/sequences_rewriter.h Lines: 1 1 100.0 %
Date: 2021-08-06 Branches: 0 0 0.0 %

Line Exec Source
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
10068
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
  Node returnRewrite(Node node, Node ret, Rewrite r);
121
122
 public:
123
  RewriteResponse postRewrite(TNode node) override;
124
  RewriteResponse preRewrite(TNode node) override;
125
  /** Expand definition */
126
  TrustNode expandDefinition(Node n) override;
127
128
  /** rewrite equality
129
   *
130
   * This method returns a formula that is equivalent to the equality between
131
   * two strings s = t, given by node. The result of rewrite is one of
132
   * { s = t, t = s, true, false }.
133
   */
134
  Node rewriteEquality(Node node);
135
  /** rewrite equality extended
136
   *
137
   * This method returns a formula that is equivalent to the equality between
138
   * two terms s = t, given by node, where s and t are terms in the signature
139
   * of the theory of strings. Notice that s and t may be of string type or
140
   * of Int type.
141
   *
142
   * Specifically, this function performs rewrites whose conclusion is not
143
   * necessarily one of { s = t, t = s, true, false }.
144
   */
145
  Node rewriteEqualityExt(Node node) override;
146
  /** rewrite string length
147
   * This is the entry point for post-rewriting terms node of the form
148
   *   str.len( t )
149
   * Returns the rewritten form of node.
150
   */
151
  Node rewriteLength(Node node);
152
  /** rewrite concat
153
   * This is the entry point for post-rewriting terms node of the form
154
   *   str.++( t1, .., tn )
155
   * Returns the rewritten form of node.
156
   */
157
  Node rewriteConcat(Node node);
158
  /** rewrite character at
159
   * This is the entry point for post-rewriting terms node of the form
160
   *   str.charat( s, i1 )
161
   * Returns the rewritten form of node.
162
   */
163
  Node rewriteCharAt(Node node);
164
  /** rewrite substr
165
   * This is the entry point for post-rewriting terms node of the form
166
   *   str.substr( s, i1, i2 )
167
   * Returns the rewritten form of node.
168
   */
169
  Node rewriteSubstr(Node node);
170
  /** rewrite update
171
   * This is the entry point for post-rewriting terms node of the form
172
   *   str.update( s, i1, i2 )
173
   * Returns the rewritten form of node.
174
   */
175
  Node rewriteUpdate(Node node);
176
  /** rewrite contains
177
   * This is the entry point for post-rewriting terms node of the form
178
   *   str.contains( t, s )
179
   * Returns the rewritten form of node.
180
   *
181
   * For details on some of the basic rewrites done in this function, see Figure
182
   * 7 of Reynolds et al "Scaling Up DPLL(T) String Solvers Using
183
   * Context-Dependent Rewriting", CAV 2017.
184
   */
185
  Node rewriteContains(Node node);
186
  /** rewrite indexof
187
   * This is the entry point for post-rewriting terms n of the form
188
   *   str.indexof( s, t, n )
189
   * Returns the rewritten form of node.
190
   */
191
  Node rewriteIndexof(Node node);
192
  /** rewrite indexof regular expression match
193
   * This is the entry point for post-rewriting terms n of the form
194
   *   str.indexof_re( s, r, n )
195
   * Returns the rewritten form of node.
196
   */
197
  Node rewriteIndexofRe(Node node);
198
  /** rewrite replace
199
   * This is the entry point for post-rewriting terms n of the form
200
   *   str.replace( s, t, r )
201
   * Returns the rewritten form of node.
202
   */
203
  Node rewriteReplace(Node node);
204
  /** rewrite replace all
205
   * This is the entry point for post-rewriting terms n of the form
206
   *   str.replaceall( s, t, r )
207
   * Returns the rewritten form of node.
208
   */
209
  Node rewriteReplaceAll(Node node);
210
  /** rewrite replace internal
211
   *
212
   * This method implements rewrite rules that apply to both str.replace and
213
   * str.replaceall. If it returns a non-null ret, then node rewrites to ret.
214
   */
215
  Node rewriteReplaceInternal(Node node);
216
  /** rewrite regular expression replace
217
   *
218
   * This method implements rewrite rules that apply to terms of the form
219
   * str.replace_re(s, r, t).
220
   *
221
   * @param node The node to rewrite
222
   * @return The rewritten node
223
   */
224
  Node rewriteReplaceRe(Node node);
225
  /** rewrite regular expression replace
226
   *
227
   * This method implements rewrite rules that apply to terms of the form
228
   * str.replace_re_all(s, r, t).
229
   *
230
   * @param node The node to rewrite
231
   * @return The rewritten node
232
   */
233
  Node rewriteReplaceReAll(Node node);
234
  /**
235
   * Returns the first, shortest sequence in n that matches r.
236
   *
237
   * @param n The constant string or sequence to search in.
238
   * @param r The regular expression to search for.
239
   * @return A pair holding the start position and the end position of the
240
   *         match or a pair of string::npos if r does not appear in n.
241
   */
242
  std::pair<size_t, size_t> firstMatch(Node n, Node r);
243
  /** rewrite string reverse
244
   *
245
   * This is the entry point for post-rewriting terms n of the form
246
   *   str.rev( s )
247
   * Returns the rewritten form of node.
248
   */
249
  Node rewriteStrReverse(Node node);
250
  /** rewrite prefix/suffix
251
   * This is the entry point for post-rewriting terms n of the form
252
   *   str.prefixof( s, t ) / str.suffixof( s, t )
253
   * Returns the rewritten form of node.
254
   */
255
  Node rewritePrefixSuffix(Node node);
256
257
  /** rewrite str.to_code
258
   * This is the entry point for post-rewriting terms n of the form
259
   *   str.to_code( t )
260
   * Returns the rewritten form of node.
261
   */
262
  Node rewriteStringToCode(Node node);
263
  /** rewrite seq.unit
264
   * This is the entry point for post-rewriting terms n of the form
265
   *   seq.unit( t )
266
   * Returns the rewritten form of node.
267
   */
268
  Node rewriteSeqUnit(Node node);
269
270
  /** rewrite seq.nth
271
   * This is the entry point for post-rewriting terms n of the form
272
   *   seq.nth(s, i)
273
   * Returns the rewritten form of node.
274
   */
275
  Node rewriteSeqNth(Node node);
276
277
  /** length preserving rewrite
278
   *
279
   * Given input n, this returns a string n' whose length is equivalent to n.
280
   * We apply certain normalizations to n', such as replacing all constants
281
   * that are not relevant to length by "A".
282
   */
283
  static Node lengthPreserveRewrite(Node n);
284
285
  /**
286
   * Given a symbolic length n, returns the canonical string (of type stype)
287
   * for that length. For example if n is constant, this function returns a
288
   * string consisting of "A" repeated n times. Returns the null node if no such
289
   * string exists.
290
   */
291
  static Node canonicalStrForSymbolicLength(Node n, TypeNode stype);
292
293
  /**
294
   * post-process rewrite
295
   *
296
   * If node is not an equality and ret is an equality,
297
   * this method applies an additional rewrite step (rewriteEqualityExt) that
298
   * performs additional rewrites on ret, after which we return the result of
299
   * this call. Otherwise, this method simply returns ret.
300
   */
301
  Node postProcessRewrite(Node node, Node ret);
302
  /** Reference to the rewriter statistics. */
303
  HistogramStat<Rewrite>* d_statistics;
304
305
  /** Instance of the entailment checker for strings. */
306
  StringsEntail d_stringsEntail;
307
}; /* class SequencesRewriter */
308
309
}  // namespace strings
310
}  // namespace theory
311
}  // namespace cvc5
312
313
#endif /* CVC5__THEORY__STRINGS__SEQUENCES_REWRITER_H */