forked from Z3Prover/z3
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paththeory_str_noodler.cpp
2296 lines (2034 loc) · 101 KB
/
theory_str_noodler.cpp
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
The skeleton of this code was obtained by Yu-Fang Chen from https://github.com/guluchen/z3.
Eternal glory to Yu-Fang.
*/
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cmath>
#include "ast/ast_pp.h"
#include "smt/smt_context.h"
#include "smt/theory_lra.h"
#include "smt/theory_arith.h"
#include "smt/smt_context.h"
#include "ast/seq_decl_plugin.h"
#include "ast/reg_decl_plugins.h"
#include "decision_procedure.h"
#include "theory_str_noodler.h"
#include "memb_heuristics_procedures.h"
namespace smt::noodler {
theory_str_noodler::theory_str_noodler(context& ctx, ast_manager & m, theory_str_noodler_params const & params):
theory(ctx, ctx.get_manager().mk_family_id("seq")),
m_params(params),
m_rewrite(m),
m_util_a(m),
m_util_s(m),
var_eqs(m_util_a),
m_length(m),
axiomatized_instances(),
sat_length_formula(m) {
}
void theory_str_noodler::display(std::ostream &os) const {
os << "theory_str display" << std::endl;
}
void theory_str_noodler::init() {
theory::init();
STRACE("str", tout << "init" << std::endl;);
}
enode *theory_str_noodler::ensure_enode(expr *e) {
if (!ctx.e_internalized(e)) {
ctx.internalize(e, false);
}
enode *n = ctx.get_enode(e);
ctx.mark_as_relevant(n);
return n;
}
theory_var theory_str_noodler::mk_var(enode *const n) {
if (!m_util_s.is_seq(n->get_expr()) &&
!m_util_s.is_re(n->get_expr())) {
return null_theory_var;
}
if (is_attached_to_var(n)) {
return n->get_th_var(get_id());
} else {
theory_var v = theory::mk_var(n);
get_context().attach_th_var(n, this, v);
get_context().mark_as_relevant(n);
return v;
}
}
bool theory_str_noodler::internalize_atom(app *const atom, const bool gate_ctx) {
(void) gate_ctx;
STRACE("str", tout << "internalize_atom: gate_ctx is " << gate_ctx << ", "
<< mk_pp(atom, get_manager()) << '\n';);
context &ctx = get_context();
if (ctx.b_internalized(atom)) {
STRACE("str", tout << "done before\n";);
return true;
}
return internalize_term(atom);
}
bool theory_str_noodler::internalize_term(app *const term) {
context &ctx = get_context();
if (m_util_s.str.is_in_re(term)) {
if (!ctx.e_internalized(term->get_arg(0))) {
ctx.internalize(term->get_arg(0), false);
enode* enode = ctx.get_enode(term->get_arg(0));
mk_var(enode);
}
}
if (ctx.e_internalized(term)) {
enode *e = ctx.get_enode(term);
mk_var(e);
return true;
}
for (auto arg : *term) {
mk_var(ensure_enode(arg));
}
if (m.is_bool(term)) {
bool_var bv = ctx.mk_bool_var(term);
ctx.set_var_theory(bv, get_id());
//We do not want to mark as relevant because it involves
// irrelevant RE solutions comming from the underlying SAT solver.
//ctx.mark_as_relevant(bv);
}
enode *e = nullptr;
if (ctx.e_internalized(term)) {
e = ctx.get_enode(term);
} else {
e = ctx.mk_enode(term, false, m.is_bool(term), true);
}
mk_var(e);
if (!ctx.relevancy()) {
relevant_eh(term);
}
return true;
}
void theory_str_noodler::apply_sort_cnstr(enode *n, sort *s) {
mk_var(n);
}
void theory_str_noodler::collect_statistics(::statistics & st) const {
STRACE("str", tout << "collecting statistics" << std::endl;);
st.update("noodler-final_checks", num_of_solving_final_checks);
for (const auto& [heur_name, heur_stats] : this->statistics) {
st.update(statistics_bullshit_names.at(heur_name)[0], heur_stats.num_start);
st.update(statistics_bullshit_names.at(heur_name)[1], heur_stats.num_finish);
st.update(statistics_bullshit_names.at(heur_name)[2], heur_stats.num_solved_preprocess);
}
}
void theory_str_noodler::init_search_eh() {
STRACE("str", tout << __LINE__ << " enter " << __FUNCTION__ << std::endl;);
context &ctx = get_context();
unsigned nFormulas = ctx.get_num_asserted_formulas();
for (unsigned i = 0; i < nFormulas; ++i) {
STRACE("str-init-formula", tout << "Initial asserted formula " << i << ": " << expr_ref(ctx.get_asserted_formula(i), m) << std::endl;);
obj_hashtable<app> lens;
util::get_len_exprs(ctx.get_asserted_formula(i), m_util_s, m, lens);
for (app* const a : lens) {
util::get_str_variables(a, this->m_util_s, m, this->len_vars, &this->predicate_replace);
}
expr *ex = ctx.get_asserted_formula(i);
ctx.mark_as_relevant(ex);
string_theory_propagation(ex, true, false);
}
add_conversion_num_axioms();
add_len_num_axioms();
STRACE("str", tout << __LINE__ << " leave " << __FUNCTION__ << std::endl;);
}
void theory_str_noodler::string_theory_propagation(expr *expr, bool init, bool neg, bool var_lengths) {
STRACE("str", tout << __LINE__ << " enter " << __FUNCTION__ << std::endl;);
STRACE("str", tout << mk_pp(expr, get_manager()) << std::endl;);
context &ctx = get_context();
if (!ctx.e_internalized(expr)) {
// expr might be in a logical context (e.g., and, or, not)
ctx.internalize(expr, true);
}
//We do not mark the expression as relevant since we do not want bias a
//fresh SAT solution by the newly added theory axioms.
// enode *n = ctx.get_enode(expr);
// ctx.mark_as_relevant(n);
if(m.is_not(expr)) {
neg = !neg;
}
// TODO weird, we have to do it because inequations are handled differently as equations, and they might not have been set as relevant
if(init && m.is_eq(expr) && neg) {
ctx.mark_as_relevant(m.mk_not(expr));
}
// we need to propagate all string predicates (including their negated forms) before the actual solve (in init_search), because we need to ensure these axioms are
// generated only once on the decision level 0 (if they are generated on a higher level, they can cause looping for some reason)
if(init && (
m_util_s.str.is_prefix(expr) ||
m_util_s.str.is_suffix(expr) ||
m_util_s.str.is_contains(expr) ||
m_util_s.str.is_is_digit(expr)
// we cannot do it for conversions (and for string inequalities which lead to to_code conversions) because otherwise all conversions become relevant and there is a degradation on benchmarks
// (this degradation should not happen for other predicates, because they are transformed into simpler atoms such as equation, regular membership... whose relevancy we can check when it is needed)
)) {
if(neg) ctx.mark_as_relevant(m.mk_not(expr));
else ctx.mark_as_relevant(expr);
}
// Check if we already axiomatized the expr
if (propagated_string_theory.contains(expr)) {
return;
}
propagated_string_theory.insert(expr);
sort *expr_sort = expr->get_sort();
sort *str_sort = m_util_s.str.mk_string_sort();
if (expr_sort == str_sort) {
enode *n = ctx.get_enode(expr);
propagate_basic_string_axioms(n, var_lengths);
if (is_app(expr) && m_util_s.str.is_concat(to_app(expr))) {
propagate_concat_axiom(n);
}
}
// if expr is an application, recursively inspect all arguments
if (is_app(expr) && !m_util_s.str.is_length(expr)) {
app *term = to_app(expr);
unsigned num_args = term->get_num_args();
for (unsigned i = 0; i < num_args; i++) {
string_theory_propagation(term->get_arg(i), init, neg, var_lengths);
}
}
STRACE("str", tout << __LINE__ << " leave " << __FUNCTION__ << std::endl;);
}
// for concatenation xy create axiom |xy| = |x| + |y| where x, y are some string expressions
void theory_str_noodler::propagate_concat_axiom(enode *cat) {
STRACE("str", tout << __LINE__ << " enter " << __FUNCTION__ << std::endl;);
app *a_cat = cat->get_expr();
SASSERT(m_util_s.str.is_concat(a_cat));
ast_manager &m = get_manager();
// build LHS
expr_ref len_xy(m);
len_xy = m_util_s.str.mk_length(a_cat);
SASSERT(len_xy);
// build RHS: start by extracting x and y from Concat(x, y)
SASSERT(a_cat->get_num_args() == 2);
app *a_x = to_app(a_cat->get_arg(0));
app *a_y = to_app(a_cat->get_arg(1));
expr_ref len_x(m);
len_x = m_util_s.str.mk_length(a_x);
SASSERT(len_x);
expr_ref len_y(m);
len_y = m_util_s.str.mk_length(a_y);
SASSERT(len_y);
// now build len_x + len_y
expr_ref len_x_plus_len_y(m);
len_x_plus_len_y = m_util_a.mk_add(len_x, len_y);
SASSERT(len_x_plus_len_y);
STRACE("str-concat",
tout << "[Concat Axiom] " << mk_pp(len_xy, m) << " = " << mk_pp(len_x, m) << " + " << mk_pp(len_y, m)
<< std::endl;
);
// finally assert equality between the two subexpressions
app_ref eq(m.mk_eq(len_xy, len_x_plus_len_y), m);
SASSERT(eq);
add_axiom(eq);
this->axiomatized_len_axioms.push_back(eq);
STRACE("str", tout << __LINE__ << " leave " << __FUNCTION__ << std::endl;);
}
void theory_str_noodler::propagate_basic_string_axioms(enode *str, bool var_lengths) {
context &ctx = get_context();
ast_manager &m = get_manager();
{
sort *a_sort = str->get_expr()->get_sort();
sort *str_sort = m_util_s.str.mk_string_sort();
if (a_sort != str_sort) {
STRACE("str",
tout << "WARNING: not setting up string axioms on non-string term " << mk_pp(str->get_expr(), m)
<< std::endl;);
return;
}
}
// TESTING: attempt to avoid a crash here when a variable goes out of scope
if (str->get_iscope_lvl() > ctx.get_scope_level()) {
STRACE("str", tout << "WARNING: skipping axiom setup on out-of-scope string term" << std::endl;);
return;
}
// generate a stronger axiom for constant strings
app_ref a_str(str->get_expr(), m);
// len(str) = |str| for explicit string str
if (m_util_s.str.is_string(a_str)) {
STRACE("str-axiom", tout << "[ConstStr Axiom] " << mk_pp(a_str, m) << std::endl);
expr_ref len_str(m_util_s.str.mk_length(a_str), m);
SASSERT(len_str);
zstring strconst;
m_util_s.str.is_string(str->get_expr(), strconst);
unsigned int l = strconst.length();
expr_ref len(m_util_a.mk_numeral(rational(l), true), m);
expr_ref eq(m.mk_eq(len_str, len), m);
add_axiom(eq);
return;
} else if(!m.is_ite(a_str)) {
// axiom |t| >= 0 where t is a string term
{
STRACE("str-axiom", tout << "[Non-Zero Axiom] " << mk_pp(a_str, m) << std::endl);
// build LHS
expr_ref len_str(m);
len_str = m_util_s.str.mk_length(a_str);
SASSERT(len_str);
// build RHS
expr_ref zero(m);
zero = m_util_a.mk_numeral(rational(0), true);
SASSERT(zero);
// build LHS >= RHS and assert
app_ref lhs_ge_rhs(m_util_a.mk_ge(len_str, zero), m);
ctx.internalize(lhs_ge_rhs, false);
SASSERT(lhs_ge_rhs);
STRACE("str", tout << "string axiom 1: " << mk_ismt2_pp(lhs_ge_rhs, m) << std::endl;);
add_axiom({mk_literal(lhs_ge_rhs)});
this->axiomatized_len_axioms.push_back(lhs_ge_rhs);
}
// axiom |t| <= 0 -> t = eps; if var_lengths is set add also t = eps -> |t| = 0
{
STRACE("str-axiom", tout << "[Zero iff Empty Axiom] " << mk_pp(a_str, m) << std::endl);
// build LHS of iff
expr_ref len_str(m);
len_str = m_util_s.str.mk_length(a_str);
SASSERT(len_str);
expr_ref zero(m);
zero = m_util_a.mk_numeral(rational(0), true);
SASSERT(zero);
expr_ref lhs(m);
lhs = m_util_a.mk_le(len_str, zero);
SASSERT(lhs);
// build RHS of iff
expr_ref empty_str(m);
empty_str = m_util_s.str.mk_empty(a_str->get_sort());
SASSERT(empty_str);
expr_ref rhs(m);
rhs = m.mk_eq(a_str, empty_str);
ctx.internalize(rhs, false);
ctx.internalize(lhs, false);
SASSERT(rhs);
// build LHS <=> RHS and assert
add_axiom(m.mk_or(m.mk_not(lhs), rhs));
// generate also the other implication
if(var_lengths) {
add_axiom(m.mk_or(m.mk_not(rhs), lhs));
}
}
} else {
// INFO do nothing if ite, because this function is called only in string_theory_propagation where ite is processed
}
}
void theory_str_noodler::add_length_axiom(expr *n) {
app_ref ln(m_util_a.mk_ge(n, m_util_a.mk_int(0)), m);
ctx.internalize(ln, false);
add_axiom(ln);
this->axiomatized_len_axioms.push_back(ln);
}
void theory_str_noodler::relevant_eh(app *const n) {
STRACE("str", tout << "relevant: " << mk_pp(n, get_manager()) << " with family id " << n->get_family_id() << ", sort " << n->get_sort()->get_name() << " and decl kind " << n->get_decl_kind() << std::endl;);
if (m_util_s.str.is_length(n)) { // str.len
add_length_axiom(n);
// FIXME what is this? is it important? can we delete this?
expr *arg;
if (m_util_s.str.is_length(n, arg) && !has_length(arg) && get_context().e_internalized(arg)) {
enforce_length(arg);
}
} else if(m_util_s.str.is_lt(n)) { // str.<
handle_lex_lt(n);
} else if(m_util_s.str.is_le(n)) { // str.<=
handle_lex_leq(n);
} else if (m_util_s.str.is_at(n)) { // str.at
handle_char_at(n);
} else if (m_util_s.str.is_extract(n)) { // str.substr
handle_substr(n);
} else if(m_util_s.str.is_prefix(n)) { // str.prefixof
handle_prefix(n);
handle_not_prefix(n);
} else if(m_util_s.str.is_suffix(n)) { // str.suffixof
handle_suffix(n);
handle_not_suffix(n);
} else if(m_util_s.str.is_contains(n)) { // str.contains
handle_contains(n);
handle_not_contains(n);
} else if (m_util_s.str.is_index(n)) { // str.indexof
handle_index_of(n);
} else if (m_util_s.str.is_replace(n)) { // str.replace
handle_replace(n);
} else if(m_util_s.str.is_replace_all(n)) { // str.replace_all
util::throw_error("str.replace_all is not supported");
} else if(m_util_s.str.is_replace_re(n)) { // str.replace_re
handle_replace_re(n);
} else if(m_util_s.str.is_replace_re_all(n)) { // str.replace_re_all
util::throw_error("str.replace_re_all is not supported");
} else if (m_util_s.str.is_is_digit(n)) { // str.is_digit
handle_is_digit(n);
} else if (
m_util_s.str.is_stoi(n) || // str.to_int
m_util_s.str.is_itos(n) || // str.from_int
m_util_s.str.is_to_code(n) || // str.to_code
m_util_s.str.is_from_code(n) // str.from_code
) {
handle_conversion(n);
} else if (
m_util_s.str.is_concat(n) || // str.++
m_util_s.re.is_to_re(n) || // str.to_re
m_util_s.str.is_in_re(n) || // str.in_re
m_util_s.is_re(n) || // one of re. command (re.none, re.all, re.comp, ...)
util::is_str_variable(n, m_util_s) || // string variable
// RegLan variables should never occur here, they are always eliminated by rewriter I think
m_util_s.str.is_string(n) // string literal
) {
// we do not need to handle these, concatenation is handled in the decision procedure (it is a basic term)
// and everything else will be handled during final_check_eh
} else {
// if we got here it means that we got some uninterpreted string function (i.e. probably user declared), we just replace it with a fresh string variable
expr* e = n;
if (!axiomatized_persist_terms.contains(e)) {
axiomatized_persist_terms.insert(e);
expr_ref fresh = mk_str_var_fresh("uninter");
predicate_replace.insert(e, fresh.get());
add_axiom({mk_eq(fresh, e, false)});
}
}
}
/*
ensure that all elements in equivalence class occur under an application of 'length'
*/
void theory_str_noodler::enforce_length(expr *e) {
enode *n = ensure_enode(e);
enode *n1 = n;
do {
expr *o = n->get_expr();
// TODO is this needed? what happens if we get ite, it does not do anything
if (!has_length(o) && !m.is_ite(o)) {
expr_ref len = expr_ref(m_util_s.str.mk_length(o), m);
add_length_axiom(len);
}
n = n->get_next();
} while (n1 != n);
}
void theory_str_noodler::assign_eh(bool_var v, const bool is_true) {
ast_manager &m = get_manager();
STRACE("str", tout << "assign: bool_var #" << v << " is " << is_true << ", "
<< mk_pp(get_context().bool_var2expr(v), m) << "@ scope level:" << m_scope_level << '\n';);
context &ctx = get_context();
expr *e = ctx.bool_var2expr(v);
expr *e1 = nullptr, *e2 = nullptr;
if (m_util_s.str.is_prefix(e, e1, e2)) {
// already handled in relevant_eh. It suffices to handle is_prefix only once as it is fully
// axiomatized using basic string constraints ((dis)equations, regexes, and lengths)
} else if (m_util_s.str.is_suffix(e, e1, e2)) {
// already handled in relevant_eh. It suffices to handle is_suffix only once as it is fully
// axiomatized using basic string constraints ((dis)equations, regexes, and lengths)
} else if (m_util_s.str.is_contains(e, e1, e2)) {
// notcontains cannot be fully axiomatized. We need to add it among string constraints solved later in final_check
// (it is not sufficient to add it only once at the beginning of the solver run as it max occurr in different SAT assignments).
if (!is_true) {
assign_not_contains(e);
}
} else if(m_util_s.str.is_le(e) || m_util_s.str.is_lt(e)) {
// handled in relevant_eh
} else if (m_util_s.str.is_in_re(e)) {
// regexes are not axiomatized. We store them to be solved later in final_check
handle_in_re(e, is_true);
} else if(m.is_bool(e)) {
ensure_enode(e);
TRACE("str", tout << "bool literal " << mk_pp(e, m) << " " << is_true << "\n" );
} else {
TRACE("str", tout << "unhandled literal " << mk_pp(e, m) << "\n";);
UNREACHABLE();
}
}
void theory_str_noodler::new_eq_eh(theory_var x, theory_var y) {
// get the expressions for left and right side of equation
expr_ref l{get_enode(x)->get_expr(), m};
expr_ref r{get_enode(y)->get_expr(), m};
STRACE("str", tout << "new_eq: " << l << " = " << r << std::endl;);
app* equation = m.mk_eq(l, r);
// TODO explain what is happening here
if(!ctx.e_internalized(equation)) {
ctx.mark_as_relevant(equation);
}
if(m_util_s.is_re(l) && m_util_s.is_re(r)) { // language equation
m_lang_eq_todo.push_back({l, r});
} else { // word equation
// mk_eq_atom can check if equation trivially holds (by having the
// same thing on both sides) or not (by having two distintict
// string literals)
app* equation_atom = ctx.mk_eq_atom(l, r);
if (m.is_false(equation_atom)) {
// if we have two distinct literals, we immediately stop by not allowing this equation
add_axiom({mk_literal(m.mk_not(equation))});
} else if (!m.is_true(equation_atom)) {
// if equation is not trivially true, we add it for later check
m_word_eq_todo.push_back({l, r});
// Optimization: If equation holds, then the lengths of both sides must be the same.
// We do this only if the equation (or its inverse) is already for sure relevant,
// otherwise adding the axiom might make the equation relevant (even though it is not).
// Used for quick check for arith solver, to immediately realise that sides cannot be
// ever equal based on lengths.
// This does NOT add the variables from the equation to len_vars.
if (ctx.is_relevant(equation) || ctx.is_relevant(m.mk_eq(r, l))) {
literal l_eq_r = mk_literal(equation); //mk_eq(l, r, false);
literal len_l_eq_len_r = mk_eq(m_util_s.str.mk_length(l), m_util_s.str.mk_length(r), false);
add_axiom({~l_eq_r, len_l_eq_len_r});
}
}
}
}
void theory_str_noodler::new_diseq_eh(theory_var x, theory_var y) {
// get the expressions for left and right side of disequation
const expr_ref l{get_enode(x)->get_expr(), m};
const expr_ref r{get_enode(y)->get_expr(), m};
app* equation = m.mk_eq(l, r);
app* disequation = m.mk_not(equation);
// This is to handle the case containing ite inside disequations
// TODO explain better
if(!ctx.e_internalized(equation)) {
STRACE("str", tout << "relevanting: " << mk_pp(disequation, m) << std::endl;);
ctx.mark_as_relevant(disequation);
}
ctx.internalize(disequation, false);
if(m_util_s.is_re(l) && m_util_s.is_re(r)) { // language disequation
m_lang_diseq_todo.push_back({l, r});
} else { // word disequation
// mk_eq_atom can check if equation trivially holds (by having the
// same thing on both sides) or not (by having two distintict
// string literals)
app* equation_atom = ctx.mk_eq_atom(l, r);
if (m.is_true(equation_atom)) {
// if equation trivially holds (i.e. this disequation does not),
// we immediately stop by always forcing it
add_axiom({mk_literal(equation)});
} else if (!m.is_false(equation_atom)) {
// if equation is not trivially false, we add it for later check
m_word_diseq_todo.push_back({l, r});
}
}
STRACE("str",
tout << ctx.find_assignment(equation) << " " << ctx.find_assignment(disequation) << std::endl
<< "new_diseq: " << l << " != " << r
<< " @" << m_scope_level<< " " << ctx.get_bool_var(equation) << " "
<< ctx.is_relevant(disequation) << ":" << ctx.is_relevant(equation) << std::endl;
);
}
bool theory_str_noodler::can_propagate() {
return false;
}
void theory_str_noodler::propagate() {
}
void theory_str_noodler::push_scope_eh() {
m_scope_level += 1;
m_word_eq_todo.push_scope();
m_lang_eq_todo.push_scope();
m_lang_diseq_todo.push_scope();
m_word_diseq_todo.push_scope();
m_membership_todo.push_scope();
m_not_contains_todo.push_scope();
m_conversion_todo.push_scope();
STRACE("str", tout << "push_scope: " << m_scope_level << '\n';);
}
void theory_str_noodler::pop_scope_eh(const unsigned num_scopes) {
// remove all axiomatized terms
axiomatized_terms.reset();
propagated_string_theory.reset();
m_scope_level -= num_scopes;
m_word_eq_todo.pop_scope(num_scopes);
m_lang_eq_todo.pop_scope(num_scopes);
m_lang_diseq_todo.pop_scope(num_scopes);
m_word_diseq_todo.pop_scope(num_scopes);
m_membership_todo.pop_scope(num_scopes);
m_not_contains_todo.pop_scope(num_scopes);
m_conversion_todo.pop_scope(num_scopes);
m_rewrite.reset();
// for incremental solving, we assume (TODO: should be done differently?) that if we added another assert, then pop must have been called and the satisfiability of the last run does not matter
if (m_scope_level < scope_with_last_run_was_sat) {
last_run_was_sat = false;
}
STRACE("str",
tout << "pop_scope: " << num_scopes << " (back to level " << m_scope_level << ")\n";);
}
void theory_str_noodler::restart_eh() {
STRACE("str", tout << "restart\n");
}
void theory_str_noodler::reset_eh() {
// FIXME should here be something?
STRACE("str", tout << "reset" << '\n';);
}
lbool theory_str_noodler::validate_unsat_core(expr_ref_vector &unsat_core) {
return l_undef;
}
expr_ref theory_str_noodler::mk_sub(expr *a, expr *b) {
ast_manager &m = get_manager();
expr_ref result(m_util_a.mk_sub(a, b), m);
m_rewrite(result);
return result;
}
literal theory_str_noodler::mk_literal(expr *const e) {
ast_manager &m = get_manager();
context &ctx = get_context();
expr_ref ex{e, m};
// simplify the expression. This was commented before and it caused
// problems at some point, I am not pretty sure of what kind.
m_rewrite(ex);
// since ex might be different to e, propagate basic string axioms
string_theory_propagation(ex, true);
if (!ctx.e_internalized(ex)) {
ctx.internalize(ex, false);
}
enode *const n = ctx.get_enode(ex);
ctx.mark_as_relevant(n);
return ctx.get_literal(ex);
}
bool_var theory_str_noodler::mk_bool_var(expr *const e) {
ast_manager &m = get_manager();
STRACE("str", tout << "mk_bool_var: " << mk_pp(e, m) << '\n';);
if (!m.is_bool(e)) {
return null_bool_var;
}
context &ctx = get_context();
SASSERT(!ctx.b_internalized(e));
const bool_var &bv = ctx.mk_bool_var(e);
ctx.set_var_theory(bv, get_id());
ctx.set_enode_flag(bv, true);
return bv;
}
void theory_str_noodler::add_axiom(expr *const e) {
STRACE("str_axiom", tout << __LINE__ << " " << __FUNCTION__ << mk_pp(e, get_manager()) << std::endl;);
if (!axiomatized_terms.contains(e)) {
axiomatized_terms.insert(e);
if (e == nullptr || get_manager().is_true(e)) return;
context &ctx = get_context();
if (!ctx.b_internalized(e)) {
ctx.internalize(e, false);
}
ctx.internalize(e, false);
literal l{ctx.get_literal(e)};
ctx.mark_as_relevant(l);
ctx.mk_th_axiom(get_id(), 1, &l);
STRACE("str", ctx.display_literal_verbose(tout << "[Assert_e]\n", l) << '\n';);
}
}
void theory_str_noodler::add_axiom(std::vector<literal> ls) {
STRACE("str", tout << __LINE__ << " enter " << __FUNCTION__ << std::endl;);
context &ctx = get_context();
literal_vector lv;
for (const auto &l : ls) {
if (l != null_literal && l != false_literal) {
ctx.mark_as_relevant(l);
lv.push_back(l);
}
}
ctx.mk_th_axiom(get_id(), lv, lv.size());
STRACE("str_axiom", ctx.display_literals_verbose(tout << "[Assert_c]\n", lv) << '\n';);
STRACE("str", tout << __LINE__ << " leave " << __FUNCTION__ << std::endl;);
}
/**
* @brief Handle str.at(s,i)
*
* Translates to the following theory axioms:
* 0 <= i < |s| -> s = xvy
* 0 <= i < |s| -> v in re.allchar
* 0 <= i < |s| -> |x| = i
* i < 0 -> v = eps
* i >= |s| -> v = eps
*
* We store
* str.at(s,i) = v
*
* @param e str.at(s, i)
*/
void theory_str_noodler::handle_char_at(expr *e) {
STRACE("str", tout << "handle-charat: " << mk_pp(e, m) << '\n';);
if (axiomatized_persist_terms.contains(e))
return;
axiomatized_persist_terms.insert(e);
ast_manager &m = get_manager();
expr *s = nullptr, *i = nullptr, *res = nullptr;
VERIFY(m_util_s.str.is_at(e, s, i));
expr_ref fresh = mk_str_var_fresh("at");
expr_ref re(m_util_s.re.mk_in_re(fresh, m_util_s.re.mk_full_char(nullptr)), m);
expr_ref zero(m_util_a.mk_int(0), m);
literal i_ge_0 = mk_literal(m_util_a.mk_ge(i, zero));
literal i_ge_len_s = mk_literal(m_util_a.mk_ge(mk_sub(i, m_util_s.str.mk_length(s)), zero));
expr_ref emp(m_util_s.str.mk_empty(e->get_sort()), m);
rational r;
zstring str;
// handle the case str.at "A" i
if(m_util_s.str.is_string(s, str) && str.length() == 1) {
add_axiom({~mk_literal(m.mk_eq(i, m_util_a.mk_int(0))), mk_eq(fresh, s, false)});
add_axiom({mk_literal(m.mk_eq(i, m_util_a.mk_int(0))), mk_eq(fresh, emp, false)});
add_axiom({mk_eq(fresh, e, false)});
predicate_replace.insert(e, fresh.get());
return;
}
if(m_util_a.is_numeral(i, r)) {
int val = r.get_int32();
expr_ref y = mk_str_var_fresh("at_right");
for(int j = val; j >= 0; j--) {
y = m_util_s.str.mk_concat(m_util_s.str.mk_at(s, m_util_a.mk_int(j)), y);
}
string_theory_propagation(y);
add_axiom({i_ge_len_s, mk_eq(s, y, false)});
add_axiom({i_ge_len_s, mk_literal(re)});
add_axiom({i_ge_len_s, mk_eq(m_util_a.mk_int(1), m_util_s.str.mk_length(fresh), false) });
add_axiom({mk_eq(fresh, e, false)});
add_axiom({i_ge_0, mk_eq(fresh, emp, false)});
add_axiom({~i_ge_len_s, mk_eq(fresh, emp, false)});
predicate_replace.insert(e, fresh.get());
return;
}
if(util::is_len_sub(i, s, m, m_util_s, m_util_a, res) && m_util_a.is_numeral(res, r)) {
int val = r.get_int32();
expr_ref y = mk_str_var_fresh("at_left");
for(int j = val; j < 0; j++) {
y = m_util_s.str.mk_concat(y, m_util_s.str.mk_at(s, m_util_a.mk_add(m_util_a.mk_int(j), m_util_s.str.mk_length(s))));
}
string_theory_propagation(y);
add_axiom({~i_ge_0, mk_eq(s, y, false)});
add_axiom({~i_ge_0, mk_eq(m_util_a.mk_int(1), m_util_s.str.mk_length(fresh), false) });
add_axiom({mk_eq(fresh, e, false)});
add_axiom({~i_ge_0, mk_literal(re)});
add_axiom({i_ge_0, mk_eq(fresh, emp, false)});
predicate_replace.insert(e, fresh.get());
return;
}
expr_ref one(m_util_a.mk_int(1), m);
expr_ref x = mk_str_var_fresh("at_left");
expr_ref y = mk_str_var_fresh("at_right");
expr_ref xey(m_util_s.str.mk_concat(x, m_util_s.str.mk_concat(fresh, y)), m);
string_theory_propagation(xey);
expr_ref len_x(m_util_s.str.mk_length(x), m);
add_axiom({~i_ge_0, i_ge_len_s, mk_eq(s, xey, false)});
add_axiom({~i_ge_0, i_ge_len_s, mk_eq(one, m_util_s.str.mk_length(fresh), false)});
add_axiom({~i_ge_0, i_ge_len_s, mk_literal(re)});
add_axiom({~i_ge_0, i_ge_len_s, mk_eq(i, len_x, false)});
add_axiom({i_ge_0, mk_eq(fresh, emp, false)});
add_axiom({~i_ge_len_s, mk_eq(fresh, emp, false)});
add_axiom({mk_eq(fresh, e, false)});
// add the replacement charat -> v
predicate_replace.insert(e, fresh.get());
// update length variables
util::get_str_variables(s, this->m_util_s, m, this->len_vars, &this->predicate_replace);
this->len_vars.insert(x);
}
void theory_str_noodler::handle_substr_int(expr *e) {
expr *s = nullptr, *i = nullptr, *l = nullptr;
VERIFY(m_util_s.str.is_extract(e, s, i, l));
rational r;
if(!m_util_a.is_numeral(i, r)) {
return;
}
expr_ref ls(m_util_s.str.mk_length(s), m);
expr_ref ls_minus_i_l(mk_sub(mk_sub(ls, i), l), m);
expr_ref zero(m_util_a.mk_int(0), m);
expr_ref eps(m_util_s.str.mk_string(""), m);
literal i_ge_0 = mk_literal(m_util_a.mk_ge(i, zero));
literal ls_le_i = mk_literal(m_util_a.mk_le(mk_sub(i, ls), zero));
literal li_ge_ls = mk_literal(m_util_a.mk_ge(ls_minus_i_l, zero));
literal l_ge_zero = mk_literal(m_util_a.mk_ge(l, zero));
literal ls_le_0 = mk_literal(m_util_a.mk_le(ls, zero));
expr* num_val, *ind_val;
rational num_val_rat;
if(r.is_zero() && expr_cases::is_indexof_add(l, s, m, m_util_s, m_util_a, num_val, ind_val) && m_util_a.is_numeral(num_val, num_val_rat) && num_val_rat.is_one()) {
literal l_gt_zero = mk_literal(m_util_a.mk_le(l, zero));
expr_ref v = mk_str_var_fresh("substr");
expr_ref sub(m_util_a.mk_add(l, m_util_a.mk_int(-1)), m);
m_rewrite(sub);
expr_ref substr(m_util_s.str.mk_substr(s, i, sub), m);
expr_ref conc = mk_concat(substr, ind_val);
string_theory_propagation(conc);
add_axiom({l_gt_zero, mk_eq(e, conc, false)});
add_axiom({mk_eq(v, e, false)});
// add the replacement substr -> v
this->predicate_replace.insert(e, v.get());
util::get_str_variables(s, this->m_util_s, m, this->len_vars);
return;
}
expr_ref x(m_util_s.str.mk_string(""), m);
expr_ref v = mk_str_var_fresh("substr");
int val = r.get_int32();
for(int v = 0; v < val; v++) {
expr_ref var = mk_str_var_fresh("pre_substr");
expr_ref re(m_util_s.re.mk_in_re(var, m_util_s.re.mk_full_char(nullptr)), m);
x = m_util_s.str.mk_concat(x, var);
add_axiom({~i_ge_0, ~ls_le_i, mk_literal(re)});
// strenghtening the axiom to equivalence
// for multiple substrs, the SAT solver keeps guessing re and ~re until all possibilities are covered.
// Now the choice of re is bound together with the substr axiom
add_axiom({~mk_literal(re), i_ge_0});
add_axiom({~mk_literal(re), ls_le_i});
add_axiom({~i_ge_0, ~ls_le_i, mk_eq(m_util_s.str.mk_length(var), m_util_a.mk_int(1), false)});
}
expr_ref le(m_util_s.str.mk_length(v), m);
expr_ref y = mk_str_var_fresh("post_substr");
rational rl;
expr * num_len;
expr_ref xe(m_util_s.str.mk_concat(x, v), m);
expr_ref xey(m_util_s.str.mk_concat(x, v, y), m);
if(m_util_a.is_numeral(l, rl)) {
int lval = rl.get_int32();
expr_ref substr_re(m);
for(int i = 0; i < lval; i++) {
if(substr_re == nullptr) {
substr_re = m_util_s.re.mk_full_char(nullptr);
} else {
substr_re = m_util_s.re.mk_concat(substr_re, m_util_s.re.mk_full_char(nullptr));
}
}
expr_ref substr_in(m_util_s.re.mk_in_re(v, substr_re), m);
string_theory_propagation(xey);
// 0 <= i <= |s| && 0 <= l <= |s| - i -> |v| = l
add_axiom({~i_ge_0, ~ls_le_i, ~l_ge_zero, ~li_ge_ls, mk_eq(le, l, false)});
// 0 <= i <= |s| && 0 <= l <= |s| - i -> |v| in substr_re
add_axiom({~i_ge_0, ~ls_le_i, ~l_ge_zero, ~li_ge_ls, mk_literal(substr_in)});
// 0 <= i <= |s| && |s| < l + i -> s = x.v
add_axiom({~i_ge_0, ~ls_le_i, li_ge_ls, mk_eq(y, eps, false)});
// 0 <= i <= |s| && l < 0 -> v = eps
add_axiom({~i_ge_0, ~ls_le_i, l_ge_zero, mk_eq(v, eps, false)});
// 0 <= i <= |s| -> xey = s (e = v in fact)
add_axiom({~i_ge_0, ~ls_le_i, mk_eq(xey, s, false)});
// i < 0 -> v = eps
add_axiom({i_ge_0, mk_eq(v, eps, false)});
// |s| < 0 -> v = eps
add_axiom({~ls_le_0, mk_eq(v, eps, false)});
// i > |s| -> v = eps
add_axiom({ls_le_i, mk_eq(v, eps, false)});
// substr(s, i, n) = v
add_axiom({mk_eq(v, e, false)});
// add the replacement substr -> v
this->predicate_replace.insert(e, v.get());
// update length variables
util::get_str_variables(s, this->m_util_s, m, this->len_vars);
// add length |v| = l. This is not true entirely, because there could be a case that v = eps.
// but this case is handled by epsilon propagation preprocessing (this variable will not in the system
// after that)
this->var_eqs.add(expr_ref(l, m), v);
return;
} else if(util::is_len_sub(l, s, m, m_util_s, m_util_a, num_len) && m_util_a.is_numeral(num_len, rl) && rl == r) {
xe = expr_ref(m_util_s.str.mk_concat(x, v), m);
xey = expr_ref(m_util_s.str.mk_concat(x, v), m);
} else if(m_util_a.is_zero(i) && util::is_len_sub(l, s, m, m_util_s, m_util_a, num_len) && m_util_a.is_numeral(num_len, rl) && rl.is_minus_one()) {
expr_ref substr_re(m_util_s.re.mk_full_char(nullptr), m);
expr_ref substr_in(m_util_s.re.mk_in_re(y, substr_re), m);
expr_ref ly(m_util_s.str.mk_length(y), m);
literal l_ge_zero = mk_literal(m_util_a.mk_ge(l, zero));
string_theory_propagation(xey);
add_axiom({~l_ge_zero, mk_literal(substr_in)});
add_axiom({~l_ge_zero, mk_eq(ly, m_util_a.mk_int(1), false)});
add_axiom({~l_ge_zero, mk_eq(xey, s, false)});
add_axiom({~l_ge_zero, mk_eq(le, l, false)});
add_axiom({l_ge_zero, mk_eq(v, eps, false)});
add_axiom({mk_eq(v, e, false)});
this->predicate_replace.insert(e, v.get());
// update length variables
util::get_str_variables(s, this->m_util_s, m, this->len_vars);
this->var_eqs.add(expr_ref(l, m), v);
return;
} else {
expr_ref post_bound(m_util_a.mk_ge(m_util_a.mk_add(i, l), m_util_s.str.mk_length(s)), m);
m_rewrite(post_bound); // simplify
// if i + l >= |s|, we can set post_substr to eps
if(m.is_true(post_bound)) {
y = expr_ref(m_util_s.str.mk_string(""), m);
}
// 0 <= i <= |s| && 0 <= l <= |s| - i -> |v| = l
add_axiom({~i_ge_0, ~ls_le_i, ~l_ge_zero, ~li_ge_ls, mk_eq(le, l, false)});
// 0 <= i <= |s| && |s| < l + i -> |v| = |s| - i
add_axiom({~i_ge_0, ~ls_le_i, li_ge_ls, mk_eq(le, mk_sub(ls, i), false)});
this->len_vars.insert(v);
}
string_theory_propagation(xe);
string_theory_propagation(xey);
// 0 <= i <= |s| -> xvy = s
add_axiom({~i_ge_0, ~ls_le_i, mk_eq(xey, s, false)});
// 0 <= i <= |s| && 0 <= l <= |s| - i -> |v| = l
add_axiom({~i_ge_0, ~ls_le_i, ~l_ge_zero, ~li_ge_ls, mk_eq(le, l, false)});
// 0 <= i <= |s| && l < 0 -> v = eps
add_axiom({~i_ge_0, ~ls_le_i, l_ge_zero, mk_eq(v, eps, false)});
// i < 0 -> v = eps
add_axiom({i_ge_0, mk_eq(v, eps, false)});
// not(0 <= l <= |s| - i) -> v = eps
add_axiom({ls_le_i, mk_eq(v, eps, false)});
// i > |s| -> v = eps
add_axiom({~ls_le_0, mk_eq(v, eps, false)});
// substr(s, i, n) = v
add_axiom({mk_eq(v, e, false)});
// add the replacement substr -> v
this->predicate_replace.insert(e, v.get());
// update length variables
util::get_str_variables(s, this->m_util_s, m, this->len_vars);
// add length |v| = l. This is not true entirely, because there could be a case that v = eps.
// but this case is handled by epsilon propagation preprocessing (this variable will not in the system
// after that)
this->var_eqs.add(expr_ref(l, m), v);
}
/**
* @brief Handle str.substr(s,i,l)
*
* Translates to the following theory axioms:
* 0 <= i <= |s| -> x.v.y = s
* 0 <= i <= |s| -> |x| = i
* 0 <= i <= |s| && 0 <= l <= |s| - i -> |v| = l
* 0 <= i <= |s| && |s| < l + i -> |v| = |s| - i
* 0 <= i <= |s| && l < 0 -> v = eps
* i < 0 -> v = eps
* not(0 <= l <= |s| - i) -> v = eps
* i > |s| -> v = eps
*
* We store
* substr(s, i, n) = v
*
* @param e str.substr(s, i, l)
*/
void theory_str_noodler::handle_substr(expr *e) {
STRACE("str", tout << "handle-substr: " << mk_pp(e, m) << '\n';);
if (axiomatized_persist_terms.contains(e))
return;
axiomatized_persist_terms.insert(e);
ast_manager &m = get_manager();
expr *s = nullptr, *i = nullptr, *l = nullptr;