-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
theory_str.cpp
8985 lines (7960 loc) · 388 KB
/
theory_str.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
/*++
Module Name:
theory_str.cpp
Abstract:
String Theory Plugin
Author:
Murphy Berzish and Yunhui Zheng
Revision History:
--*/
#include "ast/ast_smt2_pp.h"
#include "smt/smt_context.h"
#include "smt/theory_str.h"
#include "smt/smt_model_generator.h"
#include "ast/ast_pp.h"
#include "ast/ast_ll_pp.h"
#include<list>
#include<algorithm>
#include "smt/theory_seq_empty.h"
#include "smt/theory_arith.h"
#include "ast/ast_util.h"
#include "ast/rewriter/seq_rewriter.h"
#include "ast/rewriter/expr_replacer.h"
#include "ast/rewriter/var_subst.h"
#include "smt_kernel.h"
#include "model/model_smt2_pp.h"
namespace smt {
class seq_expr_solver : public expr_solver {
kernel m_kernel;
public:
seq_expr_solver(ast_manager& m, smt_params& fp):
m_kernel(m, fp) {}
lbool check_sat(expr* e) override {
m_kernel.push();
m_kernel.assert_expr(e);
lbool r = m_kernel.check();
m_kernel.pop(1);
return r;
}
};
theory_str::theory_str(context& ctx, ast_manager & m, theory_str_params const & params):
theory(ctx, m.mk_family_id("seq")),
m_params(params),
/* Options */
opt_EagerStringConstantLengthAssertions(true),
opt_VerifyFinalCheckProgress(false),
opt_LCMUnrollStep(2),
opt_NoQuickReturn_IntegerTheory(false),
opt_DisableIntegerTheoryIntegration(false),
opt_DeferEQCConsistencyCheck(false),
opt_CheckVariableScope(true),
opt_ConcatOverlapAvoid(true),
/* Internal setup */
search_started(false),
m_autil(m),
u(m),
sLevel(0),
finalCheckProgressIndicator(false),
m_trail(m),
m_factory(nullptr),
m_mk_aut(m),
m_unused_id(0),
m_delayed_axiom_setup_terms(m),
m_delayed_assertions_todo(m),
m_persisted_axioms(m),
m_persisted_axiom_todo(m),
tmpStringVarCount(0),
tmpXorVarCount(0),
avoidLoopCut(true),
loopDetected(false),
m_theoryStrOverlapAssumption_term(m.mk_true(), m),
contains_map(m),
string_int_conversion_terms(m),
totalCacheAccessCount(0),
cacheHitCount(0),
cacheMissCount(0),
m_fresh_id(0),
m_trail_stack(),
m_library_aware_trail_stack(),
m_find(*this),
fixed_length_subterm_trail(m),
fixed_length_assumptions(m)
{
}
theory_str::~theory_str() {
m_trail_stack.reset();
for (eautomaton * aut : regex_automata) {
dealloc(aut);
}
regex_automata.clear();
for (auto& kv: var_to_char_subterm_map) dealloc(kv.m_value);
for (auto& kv: uninterpreted_to_char_subterm_map) dealloc(kv.m_value);
}
void theory_str::init() {
m_mk_aut.set_solver(alloc(seq_expr_solver, get_manager(), ctx.get_fparams()));
}
void theory_str::reset_internal_data_structures() {
//m_trail.reset();
m_delayed_axiom_setup_terms.reset();
m_basicstr_axiom_todo.reset();
m_concat_axiom_todo.reset();
m_string_constant_length_todo.reset();
m_concat_eval_todo.reset();
m_delayed_assertions_todo.reset();
m_library_aware_axiom_todo.reset();
m_persisted_axioms.reset();
m_persisted_axiom_todo.reset();
axiomatized_terms.reset();
existing_toplevel_exprs.reset();
varForBreakConcat.clear();
loopDetected = false;
cut_var_map.reset();
m_cut_allocs.reset();
//variable_set.reset();
//internal_variable_set.reset();
//internal_variable_scope_levels.clear();
contains_map.reset();
contain_pair_bool_map.reset();
contain_pair_idx_map.reset();
m_automata.reset();
regex_automata.reset();
regex_terms.reset();
regex_terms_by_string.reset();
regex_automaton_assumptions.reset();
regex_terms_with_path_constraints.reset();
regex_terms_with_length_constraints.reset();
regex_term_to_length_constraint.reset();
regex_term_to_extra_length_vars.reset();
regex_last_lower_bound.reset();
regex_last_upper_bound.reset();
regex_length_attempt_count.reset();
regex_fail_count.reset();
regex_intersection_fail_count.reset();
string_chars.reset();
concat_astNode_map.reset();
string_int_conversion_terms.reset();
string_int_axioms.reset();
stringConstantCache.reset();
length_ast_map.reset();
//m_trail_stack.reset();
// m_find.reset();
fixed_length_subterm_trail.reset();
fixed_length_assumptions.reset();
fixed_length_used_len_terms.reset();
for (auto& kv: var_to_char_subterm_map) dealloc(kv.m_value);
var_to_char_subterm_map.reset();
for (auto& kv: uninterpreted_to_char_subterm_map) dealloc(kv.m_value);
uninterpreted_to_char_subterm_map.reset();
fixed_length_lesson.reset();
candidate_model.reset();
}
expr * theory_str::mk_string(zstring const& str) {
if (m_params.m_StringConstantCache) {
++totalCacheAccessCount;
expr * val;
if (stringConstantCache.find(str, val)) {
return val;
} else {
val = u.str.mk_string(str);
m_trail.push_back(val);
stringConstantCache.insert(str, val);
return val;
}
} else {
return u.str.mk_string(str);
}
}
expr * theory_str::mk_string(const char * str) {
return u.str.mk_string(str);
}
void theory_str::collect_statistics(::statistics & st) const {
st.update("str refine equation", m_stats.m_refine_eq);
st.update("str refine negated equation", m_stats.m_refine_neq);
st.update("str refine function", m_stats.m_refine_f);
st.update("str refine negated function", m_stats.m_refine_nf);
}
void theory_str::assert_axiom(expr * _e) {
if (_e == nullptr)
return;
if (opt_VerifyFinalCheckProgress) {
finalCheckProgressIndicator = true;
}
ast_manager& m = get_manager();
SASSERT(!m.is_true(_e));
if (m.is_true(_e)) return;
TRACE("str", tout << "asserting " << mk_ismt2_pp(_e, m) << std::endl;);
expr_ref e(_e, m);
if (!ctx.b_internalized(e)) {
ctx.internalize(e, false);
}
literal lit(ctx.get_literal(e));
ctx.mark_as_relevant(lit);
if (m.has_trace_stream()) log_axiom_instantiation(e);
ctx.mk_th_axiom(get_id(), 1, &lit);
if (m.has_trace_stream()) m.trace_stream() << "[end-of-instance]\n";
// crash/error avoidance: add all axioms to the trail
m_trail.push_back(e);
//TRACE("str", tout << "done asserting " << mk_ismt2_pp(e, get_manager()) << std::endl;);
}
void theory_str::assert_axiom_rw(expr * e) {
if (e == nullptr)
return;
ast_manager & m = get_manager();
expr_ref _e(e, m);
ctx.get_rewriter()(_e);
if (m.is_true(_e)) return;
assert_axiom(_e);
}
expr * theory_str::rewrite_implication(expr * premise, expr * conclusion) {
ast_manager & m = get_manager();
return m.mk_or(mk_not(m, premise), conclusion);
}
void theory_str::assert_implication(expr * premise, expr * conclusion) {
ast_manager & m = get_manager();
TRACE("str", tout << "asserting implication " << mk_ismt2_pp(premise, m) << " -> " << mk_ismt2_pp(conclusion, m) << std::endl;);
expr_ref axiom(m.mk_or(mk_not(m, premise), conclusion), m);
assert_axiom(axiom);
}
bool theory_str::internalize_atom(app * atom, bool gate_ctx) {
return internalize_term(atom);
}
bool theory_str::internalize_term(app * term) {
ast_manager & m = get_manager();
SASSERT(term->get_family_id() == get_family_id());
TRACE("str", tout << "internalizing term: " << mk_ismt2_pp(term, get_manager()) << std::endl;);
// emulation of user_smt_theory::internalize_term()
unsigned num_args = term->get_num_args();
for (unsigned i = 0; i < num_args; ++i) {
ctx.internalize(term->get_arg(i), false);
}
if (ctx.e_internalized(term)) {
enode * e = ctx.get_enode(term);
mk_var(e);
return true;
}
// m_parents.push_back(term);
enode * e = ctx.mk_enode(term, false, m.is_bool(term), true);
if (m.is_bool(term)) {
bool_var bv = ctx.mk_bool_var(term);
ctx.set_var_theory(bv, get_id());
ctx.set_enode_flag(bv, true);
}
// make sure every argument is attached to a theory variable
for (unsigned i = 0; i < num_args; ++i) {
enode * arg = e->get_arg(i);
theory_var v_arg = mk_var(arg);
TRACE("str", tout << "arg has theory var #" << v_arg << std::endl;); (void)v_arg;
}
theory_var v = mk_var(e);
TRACE("str", tout << "term has theory var #" << v << std::endl;); (void)v;
if (opt_EagerStringConstantLengthAssertions && u.str.is_string(term)) {
TRACE("str", tout << "eagerly asserting length of string term " << mk_pp(term, m) << std::endl;);
m_basicstr_axiom_todo.insert(e);
}
return true;
}
enode* theory_str::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;
}
void theory_str::refresh_theory_var(expr * e) {
enode * en = ensure_enode(e);
theory_var v = mk_var(en); (void)v;
TRACE("str", tout << "refresh " << mk_pp(e, get_manager()) << ": v#" << v << std::endl;);
if (e->get_sort() == u.str.mk_string_sort()) {
m_basicstr_axiom_todo.push_back(en);
}
}
theory_var theory_str::mk_var(enode* n) {
TRACE("str", tout << "mk_var for " << mk_pp(n->get_expr(), get_manager()) << std::endl;);
if (!(n->get_expr()->get_sort() == u.str.mk_string_sort())) {
return null_theory_var;
}
if (is_attached_to_var(n)) {
TRACE("str", tout << "already attached to theory var" << std::endl;);
return n->get_th_var(get_id());
} else {
theory_var v = theory::mk_var(n);
m_find.mk_var();
TRACE("str", tout << "new theory var v#" << v << " find " << m_find.find(v) << std::endl;);
ctx.attach_th_var(n, this, v);
ctx.mark_as_relevant(n);
return v;
}
}
static void cut_vars_map_copy(obj_map<expr, int> & dest, obj_map<expr, int> & src) {
for (auto const& kv : src) {
dest.insert(kv.m_key, 1);
}
}
bool theory_str::has_self_cut(expr * n1, expr * n2) {
if (!cut_var_map.contains(n1)) {
return false;
}
if (!cut_var_map.contains(n2)) {
return false;
}
if (cut_var_map[n1].empty() || cut_var_map[n2].empty()) {
return false;
}
for (auto const& kv : cut_var_map[n1].top()->vars) {
if (cut_var_map[n2].top()->vars.contains(kv.m_key)) {
return true;
}
}
return false;
}
void theory_str::add_cut_info_one_node(expr * baseNode, int slevel, expr * node) {
// crash avoidance?
m_trail.push_back(baseNode);
m_trail.push_back(node);
if (!cut_var_map.contains(baseNode)) {
T_cut * varInfo = alloc(T_cut);
m_cut_allocs.push_back(varInfo);
varInfo->level = slevel;
varInfo->vars.insert(node, 1);
cut_var_map.insert(baseNode, std::stack<T_cut*>());
cut_var_map[baseNode].push(varInfo);
TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;);
} else {
if (cut_var_map[baseNode].empty()) {
T_cut * varInfo = alloc(T_cut);
m_cut_allocs.push_back(varInfo);
varInfo->level = slevel;
varInfo->vars.insert(node, 1);
cut_var_map[baseNode].push(varInfo);
TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;);
} else {
if (cut_var_map[baseNode].top()->level < slevel) {
T_cut * varInfo = alloc(T_cut);
m_cut_allocs.push_back(varInfo);
varInfo->level = slevel;
cut_vars_map_copy(varInfo->vars, cut_var_map[baseNode].top()->vars);
varInfo->vars.insert(node, 1);
cut_var_map[baseNode].push(varInfo);
TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;);
} else if (cut_var_map[baseNode].top()->level == slevel) {
cut_var_map[baseNode].top()->vars.insert(node, 1);
TRACE("str", tout << "add var info for baseNode=" << mk_pp(baseNode, get_manager()) << ", node=" << mk_pp(node, get_manager()) << " [" << slevel << "]" << std::endl;);
} else {
get_manager().raise_exception("entered illegal state during add_cut_info_one_node()");
}
}
}
}
void theory_str::add_cut_info_merge(expr * destNode, int slevel, expr * srcNode) {
// crash avoidance?
m_trail.push_back(destNode);
m_trail.push_back(srcNode);
if (!cut_var_map.contains(srcNode)) {
get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map doesn't contain srcNode");
}
if (cut_var_map[srcNode].empty()) {
get_manager().raise_exception("illegal state in add_cut_info_merge(): cut_var_map[srcNode] is empty");
}
if (!cut_var_map.contains(destNode)) {
T_cut * varInfo = alloc(T_cut);
m_cut_allocs.push_back(varInfo);
varInfo->level = slevel;
cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars);
cut_var_map.insert(destNode, std::stack<T_cut*>());
cut_var_map[destNode].push(varInfo);
TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;);
} else {
if (cut_var_map[destNode].empty() || cut_var_map[destNode].top()->level < slevel) {
T_cut * varInfo = alloc(T_cut);
m_cut_allocs.push_back(varInfo);
varInfo->level = slevel;
cut_vars_map_copy(varInfo->vars, cut_var_map[destNode].top()->vars);
cut_vars_map_copy(varInfo->vars, cut_var_map[srcNode].top()->vars);
cut_var_map[destNode].push(varInfo);
TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;);
} else if (cut_var_map[destNode].top()->level == slevel) {
cut_vars_map_copy(cut_var_map[destNode].top()->vars, cut_var_map[srcNode].top()->vars);
TRACE("str", tout << "merge var info for destNode=" << mk_pp(destNode, get_manager()) << ", srcNode=" << mk_pp(srcNode, get_manager()) << " [" << slevel << "]" << std::endl;);
} else {
get_manager().raise_exception("illegal state in add_cut_info_merge(): inconsistent slevels");
}
}
}
void theory_str::check_and_init_cut_var(expr * node) {
if (cut_var_map.contains(node)) {
return;
} else if (!u.str.is_string(node)) {
add_cut_info_one_node(node, -1, node);
}
}
literal theory_str::mk_literal(expr* _e) {
ast_manager & m = get_manager();
expr_ref e(_e, m);
ensure_enode(e);
return ctx.get_literal(e);
}
app * theory_str::mk_int(int n) {
return m_autil.mk_numeral(rational(n), true);
}
app * theory_str::mk_int(rational & q) {
return m_autil.mk_numeral(q, true);
}
void theory_str::track_variable_scope(expr * var) {
if (internal_variable_scope_levels.find(sLevel) == internal_variable_scope_levels.end()) {
internal_variable_scope_levels[sLevel] = obj_hashtable<expr>();
}
internal_variable_scope_levels[sLevel].insert(var);
}
app * theory_str::mk_internal_xor_var() {
return mk_int_var("$$_xor");
}
app * theory_str::mk_fresh_const(char const* name, sort* s) {
string_buffer<64> buffer;
buffer << name;
buffer << "!tmp";
buffer << m_fresh_id;
m_fresh_id++;
return u.mk_skolem(symbol(buffer.c_str()), 0, nullptr, s);
}
app * theory_str::mk_int_var(std::string name) {
ast_manager & m = get_manager();
TRACE("str", tout << "creating integer variable " << name << " at scope level " << sLevel << std::endl;);
sort * int_sort = m.mk_sort(m_autil.get_family_id(), INT_SORT);
app * a = mk_fresh_const(name.c_str(), int_sort);
ctx.internalize(a, false);
SASSERT(ctx.get_enode(a) != nullptr);
SASSERT(ctx.e_internalized(a));
ctx.mark_as_relevant(a);
// I'm assuming that this combination will do the correct thing in the integer theory.
//mk_var(ctx.get_enode(a));
m_trail.push_back(a);
//variable_set.insert(a);
//internal_variable_set.insert(a);
//track_variable_scope(a);
return a;
}
app * theory_str::mk_str_var(std::string name) {
TRACE("str", tout << "creating string variable " << name << " at scope level " << sLevel << std::endl;);
sort * string_sort = u.str.mk_string_sort();
app * a = mk_fresh_const(name.c_str(), string_sort);
m_trail.push_back(a);
TRACE("str", tout << "a->get_family_id() = " << a->get_family_id() << std::endl
<< "this->get_family_id() = " << this->get_family_id() << std::endl;);
// I have a hunch that this may not get internalized for free...
ctx.internalize(a, false);
SASSERT(ctx.get_enode(a) != nullptr);
SASSERT(ctx.e_internalized(a));
// this might help??
mk_var(ctx.get_enode(a));
m_basicstr_axiom_todo.push_back(ctx.get_enode(a));
TRACE("str", tout << "add " << mk_pp(a, get_manager()) << " to m_basicstr_axiom_todo" << std::endl;);
variable_set.insert(a);
internal_variable_set.insert(a);
track_variable_scope(a);
return a;
}
void theory_str::add_nonempty_constraint(expr * s) {
ast_manager & m = get_manager();
expr_ref ax1(mk_not(m, ctx.mk_eq_atom(s, mk_string(""))), m);
assert_axiom(ax1);
{
// build LHS
expr_ref len_str(mk_strlen(s), m);
SASSERT(len_str);
// build RHS
expr_ref zero(m_autil.mk_numeral(rational(0), true), m);
SASSERT(zero);
// build LHS > RHS and assert
// we have to build !(LHS <= RHS) instead
expr_ref lhs_gt_rhs(mk_not(m, m_autil.mk_le(len_str, zero)), m);
SASSERT(lhs_gt_rhs);
assert_axiom(lhs_gt_rhs);
}
}
app_ref theory_str::mk_nonempty_str_var() {
ast_manager & m = get_manager();
std::stringstream ss;
ss << tmpStringVarCount;
tmpStringVarCount++;
std::string name = "$$_str" + ss.str();
TRACE("str", tout << "creating nonempty string variable " << name << " at scope level " << sLevel << std::endl;);
sort * string_sort = u.str.mk_string_sort();
app_ref a(mk_fresh_const(name.c_str(), string_sort), m);
ctx.internalize(a, false);
SASSERT(ctx.get_enode(a) != nullptr);
// this might help??
mk_var(ctx.get_enode(a));
// assert a variation of the basic string axioms that ensures this string is nonempty
{
// build LHS
expr_ref len_str(mk_strlen(a), m);
SASSERT(len_str);
// build RHS
expr_ref zero(m_autil.mk_numeral(rational(0), true), m);
SASSERT(zero);
// build LHS > RHS and assert
// we have to build !(LHS <= RHS) instead
expr_ref lhs_gt_rhs(mk_not(m, m_autil.mk_le(len_str, zero)), m);
SASSERT(lhs_gt_rhs);
assert_axiom(lhs_gt_rhs);
}
// add 'a' to variable sets, so we can keep track of it
m_trail.push_back(a);
variable_set.insert(a);
internal_variable_set.insert(a);
track_variable_scope(a);
return a;
}
app * theory_str::mk_contains(expr * haystack, expr * needle) {
app * contains = u.str.mk_contains(haystack, needle); // TODO double-check semantics/argument order
m_trail.push_back(contains);
// immediately force internalization so that axiom setup does not fail
ctx.internalize(contains, false);
set_up_axioms(contains);
return contains;
}
// note, this invokes "special-case" handling for the start index being 0
app * theory_str::mk_indexof(expr * haystack, expr * needle) {
app * indexof = u.str.mk_index(haystack, needle, mk_int(0));
m_trail.push_back(indexof);
// immediately force internalization so that axiom setup does not fail
ctx.internalize(indexof, false);
set_up_axioms(indexof);
return indexof;
}
app * theory_str::mk_strlen(expr * e) {
/*if (m_strutil.is_string(e)) {*/ if (false) {
zstring strval;
u.str.is_string(e, strval);
unsigned int len = strval.length();
return m_autil.mk_numeral(rational(len), true);
} else {
if (false) {
// use cache
app * lenTerm = nullptr;
if (!length_ast_map.find(e, lenTerm)) {
lenTerm = u.str.mk_length(e);
length_ast_map.insert(e, lenTerm);
m_trail.push_back(lenTerm);
}
return lenTerm;
} else {
// always regen
return u.str.mk_length(e);
}
}
}
/*
* Returns the simplified concatenation of two expressions,
* where either both expressions are constant strings
* or one expression is the empty string.
* If this precondition does not hold, the function returns nullptr.
* (note: this function was strTheory::Concat())
*/
expr * theory_str::mk_concat_const_str(expr * n1, expr * n2) {
bool n1HasEqcValue = false;
bool n2HasEqcValue = false;
expr * v1 = get_eqc_value(n1, n1HasEqcValue);
expr * v2 = get_eqc_value(n2, n2HasEqcValue);
if (u.str.is_string(v1)) {
n1HasEqcValue = true;
}
if (u.str.is_string(v2)) {
n2HasEqcValue = true;
}
if (n1HasEqcValue && n2HasEqcValue) {
zstring n1_str;
u.str.is_string(v1, n1_str);
zstring n2_str;
u.str.is_string(v2, n2_str);
zstring result = n1_str + n2_str;
return mk_string(result);
} else if (n1HasEqcValue && !n2HasEqcValue) {
zstring n1_str;
u.str.is_string(v1, n1_str);
if (n1_str.empty()) {
return n2;
}
} else if (!n1HasEqcValue && n2HasEqcValue) {
zstring n2_str;
u.str.is_string(v2, n2_str);
if (n2_str.empty()) {
return n1;
}
}
return nullptr;
}
expr * theory_str::mk_concat(expr * n1, expr * n2) {
ast_manager & m = get_manager();
ENSURE(n1 != nullptr);
ENSURE(n2 != nullptr);
bool n1HasEqcValue = false;
bool n2HasEqcValue = false;
n1 = get_eqc_value(n1, n1HasEqcValue);
n2 = get_eqc_value(n2, n2HasEqcValue);
if (n1HasEqcValue && n2HasEqcValue) {
return mk_concat_const_str(n1, n2);
} else if (n1HasEqcValue && !n2HasEqcValue) {
bool n2_isConcatFunc = u.str.is_concat(to_app(n2));
zstring n1_str;
u.str.is_string(n1, n1_str);
if (n1_str.empty()) {
return n2;
}
if (n2_isConcatFunc) {
expr * n2_arg0 = to_app(n2)->get_arg(0);
expr * n2_arg1 = to_app(n2)->get_arg(1);
if (u.str.is_string(n2_arg0)) {
n1 = mk_concat_const_str(n1, n2_arg0); // n1 will be a constant
n2 = n2_arg1;
}
}
} else if (!n1HasEqcValue && n2HasEqcValue) {
zstring n2_str;
u.str.is_string(n2, n2_str);
if (n2_str.empty()) {
return n1;
}
if (u.str.is_concat(to_app(n1))) {
expr * n1_arg0 = to_app(n1)->get_arg(0);
expr * n1_arg1 = to_app(n1)->get_arg(1);
if (u.str.is_string(n1_arg1)) {
n1 = n1_arg0;
n2 = mk_concat_const_str(n1_arg1, n2); // n2 will be a constant
}
}
} else {
if (u.str.is_concat(to_app(n1)) && u.str.is_concat(to_app(n2))) {
expr * n1_arg0 = to_app(n1)->get_arg(0);
expr * n1_arg1 = to_app(n1)->get_arg(1);
expr * n2_arg0 = to_app(n2)->get_arg(0);
expr * n2_arg1 = to_app(n2)->get_arg(1);
if (u.str.is_string(n1_arg1) && u.str.is_string(n2_arg0)) {
expr * tmpN1 = n1_arg0;
expr * tmpN2 = mk_concat_const_str(n1_arg1, n2_arg0);
n1 = mk_concat(tmpN1, tmpN2);
n2 = n2_arg1;
}
}
}
//------------------------------------------------------
// * expr * ast1 = mk_2_arg_app(ctx, td->Concat, n1, n2);
// * expr * ast2 = mk_2_arg_app(ctx, td->Concat, n1, n2);
// Z3 treats (ast1) and (ast2) as two different nodes.
//-------------------------------------------------------
expr * concatAst = nullptr;
if (!concat_astNode_map.find(n1, n2, concatAst)) {
concatAst = u.str.mk_concat(n1, n2);
m_trail.push_back(concatAst);
concat_astNode_map.insert(n1, n2, concatAst);
expr_ref concat_length(mk_strlen(concatAst), m);
ptr_vector<expr> childrenVector;
get_nodes_in_concat(concatAst, childrenVector);
expr_ref_vector items(m);
for (auto el : childrenVector) {
items.push_back(mk_strlen(el));
}
expr_ref lenAssert(ctx.mk_eq_atom(concat_length, m_autil.mk_add(items.size(), items.data())), m);
assert_axiom(lenAssert);
}
return concatAst;
}
bool theory_str::can_propagate() {
return !m_basicstr_axiom_todo.empty()
|| !m_concat_axiom_todo.empty() || !m_concat_eval_todo.empty()
|| !m_library_aware_axiom_todo.empty()
|| !m_delayed_axiom_setup_terms.empty()
|| !m_persisted_axiom_todo.empty()
|| (search_started && !m_delayed_assertions_todo.empty())
;
}
void theory_str::propagate() {
candidate_model.reset();
while (can_propagate()) {
TRACE("str", tout << "propagating..." << std::endl;);
while(true) {
// this can potentially recursively activate itself
unsigned start_count = m_basicstr_axiom_todo.size();
ptr_vector<enode> axioms_tmp(m_basicstr_axiom_todo);
for (auto const& el : axioms_tmp) {
instantiate_basic_string_axioms(el);
}
unsigned end_count = m_basicstr_axiom_todo.size();
if (end_count > start_count) {
TRACE("str", tout << "new basic string axiom terms added -- checking again" << std::endl;);
continue;
} else {
break;
}
}
m_basicstr_axiom_todo.reset();
TRACE("str", tout << "reset m_basicstr_axiom_todo" << std::endl;);
for (auto const& el : m_concat_axiom_todo) {
instantiate_concat_axiom(el);
}
m_concat_axiom_todo.reset();
for (auto const& el : m_concat_eval_todo) {
try_eval_concat(el);
}
m_concat_eval_todo.reset();
while(true) {
// Special handling: terms can recursively set up other terms
// (e.g. indexof can instantiate other indexof terms).
// - Copy the list so it can potentially be modified during setup.
// - Don't clear this list if new ones are added in the process;
// instead, set up all the new terms before proceeding.
// TODO see if any other propagate() worklists need this kind of handling
// TODO we really only need to check the new ones on each pass
unsigned start_count = m_library_aware_axiom_todo.size();
ptr_vector<enode> axioms_tmp(m_library_aware_axiom_todo);
for (auto const& e : axioms_tmp) {
app * a = e->get_expr();
if (u.str.is_stoi(a)) {
instantiate_axiom_str_to_int(e);
} else if (u.str.is_itos(a)) {
instantiate_axiom_int_to_str(e);
} else if (u.str.is_at(a)) {
instantiate_axiom_CharAt(e);
} else if (u.str.is_prefix(a)) {
instantiate_axiom_prefixof(e);
} else if (u.str.is_suffix(a)) {
instantiate_axiom_suffixof(e);
} else if (u.str.is_contains(a)) {
instantiate_axiom_Contains(e);
} else if (u.str.is_index(a)) {
instantiate_axiom_Indexof(e);
} else if (u.str.is_extract(a)) {
instantiate_axiom_Substr(e);
} else if (u.str.is_replace(a)) {
instantiate_axiom_Replace(e);
} else if (u.str.is_in_re(a)) {
instantiate_axiom_RegexIn(e);
} else if (u.str.is_is_digit(a)) {
instantiate_axiom_is_digit(e);
} else if (u.str.is_from_code(a)) {
instantiate_axiom_str_from_code(e);
} else if (u.str.is_to_code(a)) {
instantiate_axiom_str_to_code(e);
} else {
TRACE("str", tout << "BUG: unhandled library-aware term " << mk_pp(e->get_expr(), get_manager()) << std::endl;);
NOT_IMPLEMENTED_YET();
}
}
unsigned end_count = m_library_aware_axiom_todo.size();
if (end_count > start_count) {
TRACE("str", tout << "new library-aware terms added during axiom setup -- checking again" << std::endl;);
continue;
} else {
break;
}
}
//m_library_aware_axiom_todo.reset();
unsigned nScopes = m_library_aware_trail_stack.get_num_scopes();
m_library_aware_trail_stack.reset();
for (unsigned i = 0; i < nScopes; ++i) {
m_library_aware_trail_stack.push_scope();
}
for (auto el : m_delayed_axiom_setup_terms) {
// I think this is okay
ctx.internalize(el, false);
set_up_axioms(el);
}
m_delayed_axiom_setup_terms.reset();
for (expr * a : m_persisted_axiom_todo) {
assert_axiom(a);
}
m_persisted_axiom_todo.reset();
if (search_started) {
for (auto const& el : m_delayed_assertions_todo) {
assert_axiom(el);
}
m_delayed_assertions_todo.reset();
}
}
}
/*
* Attempt to evaluate a concat over constant strings,
* and if this is possible, assert equality between the
* flattened string and the original term.
*/
void theory_str::try_eval_concat(enode * cat) {
app * a_cat = cat->get_expr();
SASSERT(u.str.is_concat(a_cat));
ast_manager & m = get_manager();
TRACE("str", tout << "attempting to flatten " << mk_pp(a_cat, m) << std::endl;);
std::stack<app*> worklist;
zstring flattenedString("");
bool constOK = true;
{
app * arg0 = to_app(a_cat->get_arg(0));
app * arg1 = to_app(a_cat->get_arg(1));
worklist.push(arg1);
worklist.push(arg0);
}
while (constOK && !worklist.empty()) {
app * evalArg = worklist.top(); worklist.pop();
zstring nextStr;
if (u.str.is_string(evalArg, nextStr)) {
flattenedString = flattenedString + nextStr;
} else if (u.str.is_concat(evalArg)) {
app * arg0 = to_app(evalArg->get_arg(0));
app * arg1 = to_app(evalArg->get_arg(1));
worklist.push(arg1);
worklist.push(arg0);
} else {
TRACE("str", tout << "non-constant term in concat -- giving up." << std::endl;);
constOK = false;
break;
}
}
if (constOK) {
TRACE("str", tout << "flattened to \"" << flattenedString.encode() << '"' << std::endl;);
expr_ref constStr(mk_string(flattenedString), m);
expr_ref axiom(ctx.mk_eq_atom(a_cat, constStr), m);
assert_axiom(axiom);
}
}
/*
* Instantiate an axiom of the following form:
* Length(Concat(x, y)) = Length(x) + Length(y)
*/
void theory_str::instantiate_concat_axiom(enode * cat) {
ast_manager & m = get_manager();
app * a_cat = cat->get_expr();
TRACE("str", tout << "instantiating concat axiom for " << mk_ismt2_pp(a_cat, m) << std::endl;);
if (!u.str.is_concat(a_cat)) {
return;
}
// build LHS
expr_ref len_xy(m);
len_xy = mk_strlen(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 = mk_strlen(a_x);
SASSERT(len_x);
expr_ref len_y(m);
len_y = mk_strlen(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_autil.mk_add(len_x, len_y);
SASSERT(len_x_plus_len_y);
// finally assert equality between the two subexpressions
app * eq = m.mk_eq(len_xy, len_x_plus_len_y);
SASSERT(eq);
assert_axiom(eq);
}
/*
* Add axioms that are true for any string variable:
* 1. Length(x) >= 0
* 2. Length(x) == 0 <=> x == ""
* If the term is a string constant, we can assert something stronger:
* Length(x) == strlen(x)
*/
void theory_str::instantiate_basic_string_axioms(enode * str) {
ast_manager & m = get_manager();
TRACE("str", tout << "set up basic string axioms on " << mk_pp(str->get_expr(), m) << std::endl;);
{
sort * a_sort = str->get_expr()->get_sort();
sort * str_sort = u.str.mk_string_sort();
if (a_sort != str_sort) {
TRACE("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()) {
TRACE("str", tout << "WARNING: skipping axiom setup on out-of-scope string term" << std::endl;);
return;
}
// generate a stronger axiom for constant strings
app * a_str = str->get_expr();
if (u.str.is_string(a_str)) {