1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*++
Copyright (c) 2024 Microsoft Corporation
Regression tests for seq_rewriter smart constructors for regex ranges.
Tests:
1. Empty range (lo > hi) → re.none
2. Singleton range (lo == hi) → str.to_re lo
3. Range ∩ Range → reduced range or re.none
4. Range ∪ Range → merged range for overlapping/adjacent
5. Complement of range → one or two ranges
6. Downstream operators absorb empty ranges correctly
15. Symbolic-bound range membership rewrite (structural)
16. Symbolic-bound range membership: concrete element, symbolic bounds (structural)
17. Solver: (str.in_re x (re.range x x)) sat when len(x)=1
18. Solver: (str.in_re x (re.range x x)) unsat when len(x)=2
19. Solver: inverted symbolic bounds make membership unsatisfiable
20. Solver: contradictory constant lexical bounds are unsatisfiable
--*/
#include "ast/arith_decl_plugin.h"
#include "ast/ast_pp.h"
#include "ast/reg_decl_plugins.h"
#include "ast/rewriter/th_rewriter.h"
#include "ast/seq_decl_plugin.h"
#include "api/z3.h"
#include "smt/smt_context.h"
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
// Build a single-char string literal expression.
static expr_ref mk_str(ast_manager& m, seq_util& su, unsigned c) {
return expr_ref(su.str.mk_string(zstring(c)), m);
}
static void test_seq_foldl_nth_model_validation() {
Z3_context ctx = Z3_mk_context(nullptr);
char const* result =
Z3_eval_smtlib2_string(ctx,
"(set-option :model_validate true)\n"
"(declare-const initial Int)\n"
"(declare-const all (Seq Int))\n"
"(declare-const final Int)\n"
"(declare-const elements (Seq Int))\n"
"(define-fun all_sums ((prev_sums (Seq Int)) (elem Int)) (Seq Int)\n"
" (seq.++ (seq.unit (+ (seq.nth prev_sums 0) elem)) prev_sums))\n"
"(assert (= all (seq.foldl all_sums (seq.unit initial) elements)))\n"
"(assert (= final (seq.nth all 0)))\n"
"(assert (= initial 0))\n"
"(assert (= final 6))\n"
"(check-sat)\n"
"(get-model)\n");
ENSURE(std::strstr(result, "sat") != nullptr);
ENSURE(std::strstr(result, "invalid model") == nullptr);
Z3_del_context(ctx);
}
static void test_seq_foldl_foldli_scalar_model_validation() {
Z3_context ctx = Z3_mk_context(nullptr);
char const* result =
Z3_eval_smtlib2_string(ctx,
"(set-option :model_validate true)\n"
"(push)\n"
"(declare-fun f (Int Int) Int)\n"
"(declare-const il (Seq Int))\n"
"(assert (= (seq.foldl f 0 il) 5))\n"
"(check-sat)\n"
"(pop)\n"
"(push)\n"
"(declare-const il (Seq Int))\n"
"(declare-const F (Array Bool Int Bool))\n"
"(assert (= (seq.foldl F true il) true))\n"
"(assert (> (seq.len il) 0))\n"
"(assert (not (= F ((as const (Array Bool Int Bool)) true))))\n"
"(check-sat)\n"
"(pop)\n"
"(push)\n"
"(declare-fun f (Int Int Int) Int)\n"
"(declare-const il (Seq Int))\n"
"(assert (= (seq.foldli f 0 0 il) 5))\n"
"(check-sat)\n"
"(pop)\n"
"(push)\n"
"(declare-const il (Seq Int))\n"
"(declare-const F (Array Int Bool Int Bool))\n"
"(assert (= (seq.foldli F 5 true il) true))\n"
"(assert (> (seq.len il) 0))\n"
"(assert (not (= F ((as const (Array Int Bool Int Bool)) true))))\n"
"(check-sat)\n"
"(pop)\n");
ENSURE(std::strstr(result, "unknown") == nullptr);
ENSURE(std::strstr(result, "invalid model") == nullptr);
unsigned sat_count = 0;
std::istringstream in{std::string(result)};
for (std::string line; std::getline(in, line);)
if (line == "sat")
++sat_count;
ENSURE(sat_count == 4);
Z3_del_context(ctx);
}
void tst_seq_rewriter() {
ast_manager m;
reg_decl_plugins(m);
th_rewriter rw(m);
seq_util su(m);
sort* str_sort = su.str.mk_string_sort();
sort* re_sort = su.re.mk_re(str_sort);
auto range = [&](unsigned lo, unsigned hi) -> expr_ref {
return expr_ref(su.re.mk_range(mk_str(m, su, lo), mk_str(m, su, hi)), m);
};
// Arbitrary regex variable for downstream tests.
app_ref R(m.mk_fresh_const("R", re_sort), m);
// -----------------------------------------------------------------------
// 1. Empty range (lo > hi) → re.none
// -----------------------------------------------------------------------
{
expr_ref e = range('z', 'a');
rw(e);
std::cout << "empty range lo>hi: " << mk_pp(e, m) << "\n";
ENSURE(su.re.is_empty(e));
}
// -----------------------------------------------------------------------
// 2. Singleton range (lo == hi) → str.to_re lo
// -----------------------------------------------------------------------
{
expr_ref e = range('a', 'a');
rw(e);
std::cout << "singleton range: " << mk_pp(e, m) << "\n";
expr* inner = nullptr;
ENSURE(su.re.is_to_re(e, inner));
}
// -----------------------------------------------------------------------
// 3. Range intersection: overlapping → smaller range
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_inter(range('a', 'z'), range('f', 'k')), m);
rw(e);
std::cout << "range inter overlapping: " << mk_pp(e, m) << "\n";
unsigned lo = 0, hi = 0;
ENSURE(su.re.is_range(e, lo, hi) && lo == 'f' && hi == 'k');
}
// -----------------------------------------------------------------------
// 4. Range intersection: disjoint → re.none
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_inter(range('a', 'f'), range('k', 'z')), m);
rw(e);
std::cout << "range inter disjoint: " << mk_pp(e, m) << "\n";
ENSURE(su.re.is_empty(e));
}
// -----------------------------------------------------------------------
// 5. Range intersection: touching at boundary → singleton (str.to_re "f")
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_inter(range('a', 'f'), range('f', 'z')), m);
rw(e);
std::cout << "range inter touching: " << mk_pp(e, m) << "\n";
expr* inner = nullptr;
ENSURE(su.re.is_to_re(e, inner));
}
// -----------------------------------------------------------------------
// 6. Range union: overlapping → merged range
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_union(range('a', 'f'), range('e', 'k')), m);
rw(e);
std::cout << "range union overlapping: " << mk_pp(e, m) << "\n";
unsigned lo = 0, hi = 0;
ENSURE(su.re.is_range(e, lo, hi) && lo == 'a' && hi == 'k');
}
// -----------------------------------------------------------------------
// 7. Range union: adjacent → merged range
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_union(range('a', 'f'), range('g', 'k')), m);
rw(e);
std::cout << "range union adjacent: " << mk_pp(e, m) << "\n";
unsigned lo = 0, hi = 0;
ENSURE(su.re.is_range(e, lo, hi) && lo == 'a' && hi == 'k');
}
// -----------------------------------------------------------------------
// 8. Range union: disjoint → stays as union
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_union(range('a', 'c'), range('m', 'z')), m);
rw(e);
std::cout << "range union disjoint (stays as union): " << mk_pp(e, m) << "\n";
ENSURE(!su.re.is_range(e));
}
// -----------------------------------------------------------------------
// 11. Downstream: (re.* (re.range "z" "a")) → str.to_re ""
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_star(range('z', 'a')), m);
rw(e);
std::cout << "star of empty range: " << mk_pp(e, m) << "\n";
expr* inner = nullptr;
// star of empty → epsilon (str.to_re "")
ENSURE(su.re.is_to_re(e, inner) && su.str.is_empty(inner));
}
// -----------------------------------------------------------------------
// 12. Downstream: concat absorbs empty range → re.none
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_concat(R, su.re.mk_concat(range('z', 'a'), R)), m);
rw(e);
std::cout << "concat absorbs empty range: " << mk_pp(e, m) << "\n";
ENSURE(su.re.is_empty(e));
}
// -----------------------------------------------------------------------
// 13. Downstream: union absorbs empty range → R
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_union(R, range('z', 'a')), m);
rw(e);
std::cout << "union absorbs empty range: " << mk_pp(e, m) << "\n";
ENSURE(e.get() == R.get());
}
// -----------------------------------------------------------------------
// 14. Downstream: inter absorbs empty range → re.none
// -----------------------------------------------------------------------
{
expr_ref e(su.re.mk_inter(R, range('z', 'a')), m);
rw(e);
std::cout << "inter absorbs empty range: " << mk_pp(e, m) << "\n";
ENSURE(su.re.is_empty(e));
}
// -----------------------------------------------------------------------
// 15. Symbolic-bound range membership rewrite (structural).
// (str.in_re x (re.range x x)) with symbolic x should be unfolded
// by the rewriter into a conjunction of length and ordering
// constraints, not left stuck as an uninterpreted membership term.
// -----------------------------------------------------------------------
{
app_ref x(m.mk_fresh_const("x", str_sort), m);
expr_ref rng(su.re.mk_range(x, x), m);
expr_ref e(su.re.mk_in_re(x, rng), m);
rw(e);
std::cout << "symbolic range (x in [x,x]): " << mk_pp(e, m) << "\n";
ENSURE(m.is_and(e));
}
// -----------------------------------------------------------------------
// 16. Symbolic-bound range membership: concrete element, symbolic bounds.
// (str.in_re "b" (re.range lo hi)) should also be unfolded to a
// conjunction when lo/hi are free variables.
// -----------------------------------------------------------------------
{
app_ref lo(m.mk_fresh_const("lo", str_sort), m);
app_ref hi(m.mk_fresh_const("hi", str_sort), m);
expr_ref b_str(su.str.mk_string(zstring('b')), m);
expr_ref rng(su.re.mk_range(lo, hi), m);
expr_ref e(su.re.mk_in_re(b_str, rng), m);
rw(e);
std::cout << "symbolic range (\"b\" in [lo,hi]): " << mk_pp(e, m) << "\n";
ENSURE(m.is_and(e));
}
// -----------------------------------------------------------------------
// Solver-level tests: the unfolded conjunction must be decidable.
// -----------------------------------------------------------------------
{
arith_util a_util(m);
// 17. sat: (str.in_re x (re.range x x)) ∧ len(x)=1
{
smt_params sp;
smt::context ctx(m, sp);
app_ref x(m.mk_fresh_const("x", str_sort), m);
ctx.assert_expr(su.re.mk_in_re(x, su.re.mk_range(x, x)));
ctx.assert_expr(m.mk_eq(su.str.mk_length(x), a_util.mk_int(1)));
lbool res = ctx.check();
std::cout << "symbolic range solver sat (len=1): " << res << "\n";
ENSURE(res == l_true);
}
// 18. unsat: (str.in_re x (re.range x x)) ∧ len(x)=2
// The unfolded membership requires len(x)=1, which contradicts len(x)=2.
{
smt_params sp;
smt::context ctx(m, sp);
app_ref x(m.mk_fresh_const("x", str_sort), m);
ctx.assert_expr(su.re.mk_in_re(x, su.re.mk_range(x, x)));
ctx.assert_expr(m.mk_eq(su.str.mk_length(x), a_util.mk_int(2)));
lbool res = ctx.check();
std::cout << "symbolic range solver unsat (len=2): " << res << "\n";
ENSURE(res == l_false);
}
// 19. unsat: inverted symbolic bounds make membership false.
// (str.in_re "b" (re.range lo hi)) ∧ lo="z" ∧ hi="a"
// The unfolded conjunction requires lo <=_lex "b" <=_lex hi, but
// "z" > "b" > "a" so the ordering constraints are unsatisfiable.
{
smt_params sp;
smt::context ctx(m, sp);
app_ref lo(m.mk_fresh_const("lo", str_sort), m);
app_ref hi(m.mk_fresh_const("hi", str_sort), m);
expr_ref b_str(su.str.mk_string(zstring('b')), m);
ctx.assert_expr(su.re.mk_in_re(b_str, su.re.mk_range(lo, hi)));
ctx.assert_expr(m.mk_eq(lo, su.str.mk_string(zstring('z'))));
ctx.assert_expr(m.mk_eq(hi, su.str.mk_string(zstring('a'))));
lbool res = ctx.check();
std::cout << "symbolic range solver inverted bounds unsat: " << res << "\n";
ENSURE(res == l_false);
}
// 20. unsat: contradictory constant lexical bounds.
// "2024-01-01" < x < "2024-12-31" and x < "2023-01-01".
// Since "2023-01-01" < "2024-01-01", no such x exists.
if (false)
{
smt_params sp;
smt::context ctx(m, sp);
app_ref x(m.mk_fresh_const("x", str_sort), m);
expr_ref b1(su.str.mk_string("2024-01-01"), m);
expr_ref b2(su.str.mk_string("2024-12-31"), m);
expr_ref b3(su.str.mk_string("2023-01-01"), m);
ctx.assert_expr(su.str.mk_lex_lt(b1, x));
ctx.assert_expr(su.str.mk_lex_lt(x, b2));
ctx.assert_expr(su.str.mk_lex_lt(x, b3));
lbool res = ctx.check();
std::cout << "constant lexical bounds unsat: " << res << "\n";
ENSURE(res == l_false);
}
}
test_seq_foldl_nth_model_validation();
test_seq_foldl_foldli_scalar_model_validation();
std::cout << "tst_seq_rewriter: all tests passed\n";
}