forked from Z3Prover/z3
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathformula_preprocess.cpp
1876 lines (1721 loc) · 80.5 KB
/
formula_preprocess.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
#include "formula_preprocess.h"
#include "util.h"
namespace smt::noodler {
FormulaVar::FormulaVar(const Formula& conj) : allpreds(), input_size(0) {
const std::vector<Predicate>& preds = conj.get_predicates();
for(size_t i = 0; i < preds.size(); i++) {
// we skip equations of the form X = X
if(preds[i].is_equation() && preds[i].get_left_side() == preds[i].get_right_side()) {
continue;
}
if(this->allpreds.find(preds[i]) == this->allpreds.end()) {
this->predicates[i] = preds[i];
this->allpreds.insert(preds[i]);
this->update_varmap(preds[i], i);
this->max_index = i;
this->input_size++;
}
}
}
/**
* @brief Update the structure collecting information about variable occurrence. The structure contains variables as
* [var] = { (var;eq_index;position) } s.t. position is negative if it is on the left side (numbering from 1).
*
* @param pred Equation whose variables should be stored
* @param index Index of the given equation @p pred.
*/
void FormulaVar::update_varmap(const Predicate& pred, size_t index) {
for(const VarNode& vr : get_var_positions(pred, index, true)) {
VarMap::iterator iter = this->varmap.find(vr.term);
if(iter != this->varmap.end()) {
iter->second.insert(vr);
} else {
this->varmap[vr.term] = std::set<VarNode>({vr});
}
}
}
/**
* @brief Get VarNode (structure representing variable position in the equation) for each
* variable in the equation @p pred.
*
* @param pred Equation
* @param index Index of the equation @p pred
* @param incl_lit Include positions of literals
* @return std::set<VarNode> Set if VarNodes for each occurrence of a variable.
*/
std::set<VarNode> FormulaVar::get_var_positions(const Predicate& pred, size_t index, bool incl_lit) const {
std::set<VarNode> ret;
int mult = -1;
for(const std::vector<BasicTerm>& side : pred.get_params()) {
update_var_positions_side(side, ret, index, incl_lit, mult);
mult *= -1;
}
return ret;
}
/**
* @brief Update BasicTerm positions in the concatenation
*
* @param side Concatenation of BasicTerms
* @param[out] res Set of positions (VarNodes)
* @param index Index of the equation the @p side belongs to
* @param incl_lit Include literals or just take variables?
* @param mult Multiplicative constant of each position (used to distinguish between left (negative positions) and right side of an equation).
*/
void FormulaVar::update_var_positions_side(const std::vector<BasicTerm>& side, std::set<VarNode>& res, size_t index, bool incl_lit, int mult) const {
for(size_t i = 0; i < side.size(); i++) {
if(incl_lit || side[i].is_variable()) {
VarNode new_item = { side[i], index, mult*int(i+1) };
res.insert(new_item);
}
}
}
/**
* @brief String representation of FormulaVar.
*
* @return String representation
*/
std::string FormulaVar::to_string() const {
std::string ret;
for(const auto& item : this->predicates) {
ret += std::to_string(item.first) + ": " + item.second.to_string() + "\n";
}
for(const auto& item : this->varmap) {
std::string st;
if(!item.second.empty()) {
st = item.second.begin()->to_string();
std::for_each(std::next(item.second.begin()), item.second.end(), [&st] (const VarNode& val) {
st.append(", ").append(val.to_string());
});
}
ret += item.first.to_string() + ": {" + st + "}\n";
}
return ret;
}
/**
* @brief Do all given variables occur at most once in the formula?
*
* @param items Set of variables to be checked.
* @return true All variables have unique occurrence.
*/
bool FormulaVar::single_occurr(const std::set<BasicTerm>& items) const {
for(const BasicTerm& t : items) {
if(t.get_type() != BasicTermType::Variable)
continue;
assert(t.get_type() == BasicTermType::Variable);
if(this->varmap.at(t).size() > 1) {
return false;
}
}
return true;
}
/**
* @brief Is the given predicate @p p regular? If so, set @p out to the form that
* a single variable is on the left side.
*
* @param p predicate to be checked (works for equalities only).
* @param[out] out Adjusted form of the predicate s.t. |L| = 1
* @return Is regular?
*/
bool FormulaVar::is_side_regular(const Predicate& p, Predicate& out) const {
std::vector<BasicTerm> side;
if(!p.is_equation()) {
return false;
}
if(p.get_left_side().size() == 1 && single_occurr(p.get_side_vars(Predicate::EquationSideType::Right))) {
out = p;
return true;
} else if (p.get_right_side().size() == 1 && single_occurr(p.get_side_vars(Predicate::EquationSideType::Left))) {
out = p.get_switched_sides_predicate();
return true;
}
return false;
}
/**
* @brief Get all regular predicates from the formula (regular predicates are defined in remove_regular) with
* an additional condition that all predicates are switched to the form X = X_1...X_n (the single variable is
* on the left).
*
* @param[out] out Vector of predicates with the corresponding index in the map predicates.
*/
void FormulaVar::get_side_regulars(std::vector<std::pair<size_t, Predicate>>& out) const {
for(const auto& item : this->predicates) {
Predicate regular;
if(is_side_regular(item.second, regular))
out.push_back({item.first, regular});
}
}
/**
* @brief Get simple equations. Simple equation is of the form X = Y where |X| = |Y| = 1.
*
* @param[out] out Vector of simple equations with the corresponding index in the map predicates
*/
void FormulaVar::get_simple_eqs(std::vector<std::pair<size_t, Predicate>>& out) const {
for(const auto& item : this->predicates) {
if(is_simple_eq(item.second))
out.push_back({item.first, item.second});
}
}
/**
* @brief Clean occurrences of variables that have empty set of occurrences from varmap.
* In other words remove items from varmap s.t. (x, {}).
*/
void FormulaVar::clean_varmap() {
remove_if(this->varmap, [](const auto& n) { return n.second.size() == 0; });
};
/**
* @brief Remove predicate from the formula. Updates the variable map (if the variable is no further present in
* the system, it is not removed, only the set of occurrences is set to {}).
*
* @param index Index of the predicate to be removed.
*/
void FormulaVar::remove_predicate(size_t index) {
std::set<BasicTerm> items = this->predicates[index].get_set();
const auto& filter = [&index](const VarNode& n) { return n.eq_index == index; };
for(const BasicTerm& v : items) {
std::set<VarNode>& occurr = this->varmap[v];
remove_if(occurr, filter);
}
this->allpreds.erase(this->predicates[index]);
this->predicates.erase(index);
}
/**
* @brief Add predicate to the formula (with updating the variable map).
*
* @param pred New predicate
* @param index Index of the new predicate (if set to -1, first higher index than top is chosen)
*/
int FormulaVar::add_predicate(const Predicate& pred, int index) {
auto it = this->allpreds.find(pred);
if(it != this->allpreds.end()) {
for(const auto& pr : this->predicates) { // WARNING: ineffective; needs to iterate over all predicates
if(pr.second == pred)
return pr.first;
}
assert(false);
}
if(index == -1) {
assert(this->predicates.find(index) == this->predicates.end()); // check if the position is free
this->max_index++;
index = int(this->max_index);
} else {
assert(index >= 0);
this->max_index = std::max(this->max_index, size_t(index));
}
this->predicates[index] = pred;
this->allpreds.insert(pred);
update_varmap(pred, size_t(index));
return index;
}
/**
* @brief Add a set of new predicates.
*
* @param preds Set of new predicates to be added
*/
void FormulaVar::add_predicates(const std::set<Predicate>& preds) {
for(const Predicate& p : preds) {
add_predicate(p);
}
}
/**
* @brief Replace @p find in the formula (in all predicates).
*
* @param find Find
* @param replace Replace
*/
void FormulaVar::replace(const Concat& find, const Concat& replace) {
std::vector<std::pair<size_t, Predicate>> replace_map;
for(const auto& pr : this->predicates) {
Predicate rpl;
if(pr.second.replace(find, replace, rpl)) { // changed, result is stored in rpl
assert(rpl != pr.second);
replace_map.push_back({pr.first, std::move(rpl)});
}
}
for(const auto& pr : replace_map) {
remove_predicate(pr.first);
add_predicate(pr.second, pr.first);
}
}
/**
* @brief Remove equations with both sides empty.
*/
void FormulaVar::clean_predicates() {
std::vector<size_t> rem_ids;
for(const auto& pr : this->predicates) {
if(!pr.second.is_equation())
continue;
if(pr.second.get_left_side().size() == 0 && pr.second.get_right_side().size() == 0)
rem_ids.push_back(pr.first);
}
for(size_t id : rem_ids) {
remove_predicate(id);
}
};
/**
* @brief Update automata assignment of @p var. If var exists in the aut assignment, we set
* L(var) = L(var) \cap L(upd). Otherwise we set L(var) = L(upd).
*
* @param var Basic term to be updated
* @param upd Concatenation of terms for updating @p var.
*/
void FormulaPreprocessor::update_reg_constr(const BasicTerm& var, const std::vector<BasicTerm>& upd) {
mata::nfa::Nfa concat = this->aut_ass.get_automaton_concat(upd);
auto iter = this->aut_ass.find(var);
if(iter != this->aut_ass.end()) {
mata::nfa::Nfa inters = mata::nfa::intersection(*(iter->second), concat);
inters.trim();
if(this->m_params.m_preprocess_red) {
this->aut_ass[var] = std::make_shared<mata::nfa::Nfa>(mata::nfa::reduce(inters));
} else {
this->aut_ass[var] = std::make_shared<mata::nfa::Nfa>(inters);
}
} else {
this->aut_ass[var] = std::make_shared<mata::nfa::Nfa>(concat);
}
}
/**
* @brief Iteratively remove regular predicates.
*
* A regular predicate is of the form X = X_1 X_2 ... X_n where X_1 ... X_n does not occurr elsewhere in the system.
* Formally, L = R is regular if |L| = 1 and each variable from Vars(R) has a single occurrence in the system only.
* Regular predicates can be removed from the system provided A(X) = A(X) \cap A(X_1).A(X_2)...A(X_n) where A(X)
* is the automaton assigned to variable X.
*/
void FormulaPreprocessor::remove_regular() {
STRACE("str-prep", tout << "Preprocessing step - remove_regular\n";);
std::vector<std::pair<size_t, Predicate>> regs;
this->formula.get_side_regulars(regs);
std::deque<std::pair<size_t, Predicate>> worklist(regs.begin(), regs.end());
// keeps the ids of removed equations
std::set<size_t> removed;
while(!worklist.empty()) {
std::pair<size_t, Predicate> pr = worklist.front();
worklist.pop_front();
if (removed.contains(pr.first)) continue; // if the equation was already removed, we do not remove it again
STRACE("str-prep-remove_regular", tout << "Remove regular:" << pr.second << std::endl;);
assert(pr.second.get_left_side().size() == 1);
// if right side contains multiple len vars we must do splitting => cannot remove (we can remove if we have only one length var with possibly literals)
bool is_right_side_len = !set_disjoint(this->len_variables, pr.second.get_side_vars(Predicate::EquationSideType::Right));
if(is_right_side_len && pr.second.get_side_vars(Predicate::EquationSideType::Right).size() > 1) {
continue;
}
// if right side contains conversion variable, then we can only handle the case X = Y, i.e., we have only one var on the right side AND there are NO literals (othewise we need to do proper splitting)
// note that if this is true, then Y is also len var and is_right_side_len is true
bool is_right_side_conv = !set_disjoint(this->conversion_vars, pr.second.get_side_vars(Predicate::EquationSideType::Right));
if(is_right_side_conv && pr.second.get_right_side().size() > 1) {
continue;
}
BasicTerm left_var = pr.second.get_left_side()[0];
update_reg_constr(left_var, pr.second.get_right_side());
if(is_right_side_len) {
// In the situation where we have X = Y (with possibly some literals around) and Y is length
// we propagate the lengthness of right side variable to the left side
this->len_variables.insert(left_var);
// and add len constraint |X| = |Y|
this->add_to_len_formula(pr.second.get_formula_eq());
}
if(is_right_side_conv) {
// if we also have that Y is conversion var, then there cannot be any literals around
// and we can add Y -> X to subst map (+ propagate that X is also conversion var)
this->conversion_vars.insert(left_var);
this->substitution_map[pr.second.get_right_side()[0]] = {left_var};
} else {
// otherwise we do not need to put anything in substitution map and we just need to remember the inclusion for model generation
removed_inclusions_for_model.push_back(pr.second);
}
this->formula.remove_predicate(pr.first);
removed.insert(pr.first);
STRACE("str-prep-remove_regular", tout << "removed" << std::endl;);
// check if by removing the regular equation, some other equations did not become regular
// we only need to check this for left_var, as the variables from the right side do not occur
// in the formula anymore (they occured only in pr)
std::set<VarNode> occurrs = this->formula.get_var_occurr(left_var);
if(occurrs.size() == 1) {
Predicate reg_pred;
if(this->formula.is_side_regular(this->formula.get_predicate(occurrs.begin()->eq_index), reg_pred)) {
worklist.emplace_back(occurrs.begin()->eq_index, reg_pred);
// update dependency
map_set_insert(this->dependency, occurrs.begin()->eq_index, pr.first);
}
}
}
STRACE("str-prep", tout << print_info(is_trace_enabled("str-nfa")));
}
/**
* @brief Propagate variables. Propagate all equations of the form X=Y
* (find all Y in the formula and replace with X).
*/
void FormulaPreprocessor::propagate_variables() {
STRACE("str-prep", tout << "Preprocessing step - propagate_variables\n";);
std::vector<std::pair<size_t, Predicate>> regs;
this->formula.get_simple_eqs(regs);
std::deque<size_t> worklist;
std::transform(regs.begin(), regs.end(), std::back_inserter(worklist),
[](auto const& pair){ return pair.first; });
while(!worklist.empty()) {
size_t index = worklist.front();
worklist.pop_front();
if(this->formula.get_predicates().find(index) == this->formula.get_predicates().end()) {
continue;
}
Predicate eq = this->formula.get_predicate(index);
if(eq.get_left_side() == eq.get_right_side()) {
this->formula.remove_predicate(index);
continue;
}
if(!eq.get_left_side()[0].is_variable()) {
eq = eq.get_switched_sides_predicate();
}
if(!eq.get_left_side()[0].is_variable()) {
continue;
}
/// if X = lit it is better to keep only lit
if(!eq.get_right_side()[0].is_variable()) {
BasicTerm v_left = eq.get_left_side()[0]; // X
update_reg_constr(v_left, eq.get_right_side());
this->formula.replace(eq.get_left_side(), eq.get_right_side());
this->formula.remove_predicate(index);
this->add_to_len_formula(eq.get_formula_eq());
substitution_map[v_left] = {eq.get_right_side()[0]};
continue;
}
assert(eq.get_left_side().size() == 1 && eq.get_right_side().size() == 1);
BasicTerm v_left = eq.get_left_side()[0]; // X
BasicTerm v_right = eq.get_right_side()[0]; // Y
update_reg_constr(v_left, eq.get_right_side()); // L(X) = L(X) cap L(Y)
this->add_to_len_formula(eq.get_formula_eq()); // add len constraint |X| = |Y|
// propagate len variables: if Y is in len_variables, include also X
if(this->len_variables.find(v_right) != this->len_variables.end()) {
this->len_variables.insert(v_left);
}
// propagate conv variables
if(this->conversion_vars.find(v_right) != this->conversion_vars.end()) {
this->conversion_vars.insert(v_left);
}
this->formula.replace(eq.get_right_side(), eq.get_left_side()); // find Y, replace for X
substitution_map[v_right] = {v_left}; // subst_map[Y] = X (the length constraint |X| = |Y| is already there)
this->formula.remove_predicate(index);
// update dependencies (overapproximation). Each remaining predicat depends on the removed one.
for(const auto& pr : this->formula.get_predicates()) {
map_set_insert(this->dependency, pr.first, index);
}
}
// Not true: you can have equation of two, literals which is not removed
//assert(!this->formula.contains_simple_eqs());
STRACE("str-prep", tout << print_info(is_trace_enabled("str-nfa")));
}
/**
* @brief Get symmetrical difference of occurrences of BasicTerms within two concatenations. For instance for X.Y.X and X.Y.W.Z
* it returns ({{X,3}}, {{W,3}, {Z,4}}). It includes both variables and literals.
*
* @param cat1 First concatenation
* @param cat2 Second concatenation
* @return Symmetrical difference
*/
VarNodeSymDiff FormulaPreprocessor::get_eq_sym_diff(const Concat& cat1, const Concat& cat2) const {
std::set<VarNode> p1, p2;
this->formula.update_var_positions_side(cat1, p1, 0, true); // include positions of literals, set equation index to 0
this->formula.update_var_positions_side(cat2, p2, 0, true);
return {set_difference(p1, p2), set_difference(p2, p1)};
}
/**
* @brief Propagate new equations having the regular shape. (for generate_identities).
* Creates new equations of the form X = Y1 Y2 Y3...
*
* @param diff1 First occurrence difference
* @param diff2 Second occurrence difference
* @param new_pred[out] Newly created equation
* @return True -> it is possible to create a new equation
*/
bool FormulaPreprocessor::propagate_regular_eqs(const std::set<VarNode>& diff1, const std::set<VarNode>& diff2, Predicate& new_pred) const {
VarNode val1 = *diff1.begin();
std::set<size_t> pos1, pos2;
std::map<size_t, BasicTerm> sorted_map;
for(size_t i = 0; i < diff2.size(); i++) pos1.insert(i + val1.position);
for(const auto& t : diff2) {
pos2.insert(t.position);
sorted_map.insert({t.position, t.term});
}
if(pos1 == pos2) {
Concat right;
for(const auto& k : sorted_map) { // in the order of their positions
right.push_back(k.second);
}
new_pred = Predicate(PredicateType::Equation,
std::vector<Concat>({Concat({val1.term}), right}));
return true;
}
return false;
}
/**
* @brief Check whether symmetric difference of term occurrences is suitable for generating identities. The
* symmetric difference contains different BasicTerms that are on the different positions in the contatenations. If so,
* set @p new_pred with the new predicate that was generated from the difference.
*
* @param diff Symmetric difference of two equivalent terms (concatenation)
* @param[out] new_pred Newly created identity
* @return Is it suitable for gen identity (was some identity created?)
*/
bool FormulaPreprocessor::generate_identities_suit(const VarNodeSymDiff& diff, Predicate& new_pred) const {
if(diff.first.size() == 1 && diff.second.size() == 1) {
VarNode val1 = *diff.first.begin();
VarNode val2 = *diff.second.begin();
if(val1.position == val2.position) {
new_pred = Predicate(PredicateType::Equation,
std::vector<Concat>({Concat({val1.term}),
std::vector<BasicTerm>({val2.term})}));
return true;
}
} else if(diff.first.size() == 1) {
return propagate_regular_eqs(diff.first, diff.second, new_pred);
} else if(diff.second.size() == 1) {
return propagate_regular_eqs(diff.second, diff.first, new_pred);
}
/// TODO: allow to generate X = eps
return false;
}
/**
* @brief Generate indentities. It covers two cases (a) X1 X X2 = X1 Y X2 => X = Y
* (b) X1 X X2 = Z and Z = X1 Y X2 => X = Y. Where each term can be both literal and variable.
*/
void FormulaPreprocessor::generate_identities() {
STRACE("str-prep", tout << "Preprocessing step - generate_identities\n";);
std::set<std::pair<size_t, Predicate>> new_preds;
std::set<size_t> rem_ids;
size_t index = this->formula.get_max_index() + 1;
for(const auto& pr1 : this->formula.get_predicates()) {
if(!pr1.second.is_equation())
continue;
for(const auto& pr2 : this->formula.get_predicates()) {
if(!pr2.second.is_equation())
continue;
if(pr1.first > pr2.first)
continue;
VarNodeSymDiff diff;
if(pr1.first == pr2.first) { // two equations are the same
diff = get_eq_sym_diff(pr1.second.get_left_side(), pr1.second.get_right_side());
// L1 = R1 and L2 = R2 and L1 = L2 => R1 = R2
} else if(pr1.second.get_left_side() == pr2.second.get_left_side()) {
diff = get_eq_sym_diff(pr1.second.get_right_side(), pr2.second.get_right_side());
// L1 = R1 and L2 = R2 and L1 = R2 => R1 = L2
} else if(pr1.second.get_left_side() == pr2.second.get_right_side()) {
diff = get_eq_sym_diff(pr1.second.get_right_side(), pr2.second.get_left_side());
// L1 = R1 and L2 = R2 and R1 = L2 => L2 = R1
} else if(pr1.second.get_right_side() == pr2.second.get_left_side()) {
diff = get_eq_sym_diff(pr1.second.get_left_side(), pr2.second.get_right_side());
// L1 = R1 and L2 = R2 and R1 = R2 => L1 = L2
} else if(pr1.second.get_right_side() == pr2.second.get_right_side()) {
diff = get_eq_sym_diff(pr1.second.get_left_side(), pr2.second.get_left_side());
}
Predicate new_pred;
if(generate_identities_suit(diff, new_pred) && new_pred != pr1.second && new_pred != pr2.second) {
new_preds.insert({index, new_pred});
/// It assumes L = A B X; L = A B Y1 Y2 ...
/// diff.first should contain X; diff.second Y1 Y2
if(diff.first.size() == 1) { // if a new predicate is of the form X = Y1 Y2 ...
rem_ids.insert(pr2.first); // remove L = A B Y1 Y2
} else {
rem_ids.insert(pr1.first);
}
index++;
}
}
}
for(const size_t & i : rem_ids) {
this->formula.remove_predicate(i);
}
for(const auto &pr : new_preds) {
this->formula.add_predicate(pr.second, pr.first);
}
STRACE("str-prep", tout << print_info(is_trace_enabled("str-nfa")));
}
/**
* @brief Create concatenation graph. Oriented graph where each term is node and two terms
* (variable/litaral) t1 and t2 are connected (t1 -> t2) if t1.t2 occurs in some equation.
* Moreover each edge is labelled by number of occurrences of such concatenation in the formula.
*
* @return ConcatGraph of the formula.
*/
ConcatGraph FormulaPreprocessor::get_concat_graph() const {
ConcatGraph graph;
for(const Predicate& pr : this->formula.get_predicates_set()) {
for(const std::vector<BasicTerm>& side : pr.get_params()) {
for(size_t i = 0; i <= side.size(); i++) {
// we use variable with empty name to denote that the variable varname is at the end of the side
BasicTerm from = graph.init();
BasicTerm to = graph.init();
if (i > 0) {
from = side[i-1];
}
if(i < side.size()) {
to = side[i];
}
graph.add_edge(from, to);
}
}
}
return graph;
}
/**
* @brief Get regular sublists, i.e., concatenations X1...Xk such that each Xi occurrs (if it is a variable) in the
* formula only in X1...Xk. In other words, if we replace X1...Xk by a fresh variable V, then
* X+ ... Xk do not occurr in the formula anymore (except in V).
*
* @param res Regular sublists with the number of their occurrences.
*/
void FormulaPreprocessor::get_regular_sublists(std::map<Concat, unsigned>& res) const {
ConcatGraph graph = get_concat_graph();
for(const BasicTerm& t : graph.get_init_vars()) {
Concat sub;
// Get all occurrences of t
std::set<VarNode> occurrs = this->formula.get_var_occurr(t);
// Get predicate of a first equation containing t; and side containing t
Predicate act_pred = this->formula.get_predicate(occurrs.begin()->eq_index);
Concat side = occurrs.begin()->position > 0 ? act_pred.get_right_side() : act_pred.get_left_side();
int start = std::abs(occurrs.begin()->position) - 1;
for(size_t i = start; i < side.size(); i++) {
std::set<VarNode> vns;
// Construct the set of supposed occurences of the symbol side[i]
for(const VarNode& vn : occurrs) {
vns.insert({
side[i],
vn.eq_index,
FormulaVar::increment_side_index(vn.position, i-start)
});
}
// Compare the supposed occurrences with real occurrences.
std::set<VarNode> occurs_act = this->formula.get_var_occurr(side[i]);
// do not include length variables
if(false && this->len_variables.find(side[i]) != this->len_variables.end()) {
break;
}
if(side[i].is_variable() && vns != occurs_act) {
break;
}
if(side[i].is_literal() && !std::includes(occurs_act.begin(),
occurs_act.end(), vns.begin(), vns.end())) {
break;
}
sub.push_back(side[i]);
}
if(sub.size() > 1) {
res[sub] = occurrs.size();
}
}
}
/**
* @brief Replace regular sequences with a fresh variables. The regular sequence is a concatenations X1...Xk
* such that each Xi occurrs (if it is a variable) in the
* formula only in X1...Xk. In other words, if we replace X1...Xk by a fresh variable V, then
* variables from X1 ... Xk do not occurr in the formula anymore (except in V).
*
* @param mn Minimum number of occurrences of a regular sequence to be replaced with a fresh variable.
*/
void FormulaPreprocessor::reduce_regular_sequence(unsigned mn) {
STRACE("str-prep", tout << "Preprocessing step - reduce_regular_sequence\n";);
std::map<Concat, unsigned> regs;
std::set<Predicate> new_eqs;
get_regular_sublists(regs);
for(const auto& pr : regs) {
if(pr.second >= mn) {
BasicTerm fresh_var = util::mk_noodler_var_fresh("regular_seq");
this->formula.replace(pr.first, Concat({fresh_var}));
update_reg_constr(fresh_var, pr.first);
this->add_to_len_formula(Predicate(PredicateType::Equation, std::vector<Concat>({Concat({fresh_var}), pr.first})).get_formula_eq());
new_eqs.insert(Predicate(PredicateType::Equation, std::vector<Concat>({Concat({fresh_var}), pr.first})));
}
}
for(const Predicate& eq : new_eqs) {
this->formula.add_predicate(eq);
// We do not add dependency
}
STRACE("str-prep", tout << print_info(is_trace_enabled("str-nfa")));
}
/**
* @brief Get all epsilon terms (variables with the language containing eps only and
* epsilon literals).
*
* @param res All terms with epsilon semantics.
*/
void FormulaPreprocessor::get_eps_terms(std::set<BasicTerm>& res) const {
for(const auto& pr : get_formula().get_varmap()) {
if(pr.first.is_variable() && is_var_eps(pr.first)) {
res.insert(pr.first);
}
if(pr.first.is_literal() && pr.first.get_name() == "") {
res.insert(pr.first);
}
if(pr.first.is_literal() && this->aut_ass.is_epsilon(pr.first) ) {
res.insert(pr.first);
}
}
}
/**
* @brief Transitively ropagate epsilon variables. The epsilon variables and the epsilon
* literal remove from the formula and set the corresponding languages appropriately.
*/
void FormulaPreprocessor::propagate_eps() {
STRACE("str-prep", tout << "Preprocessing step - propagate_eps\n";);
std::set<BasicTerm> eps_set;
get_eps_terms(eps_set);
std::deque<size_t> worklist;
std::set<size_t> eps_eq_id;
// get indices of equations containing at least one eps term
for(const BasicTerm& t : eps_set) {
std::set<VarNode> nds = get_formula().get_var_occurr(t);
std::transform(nds.begin(), nds.end(), std::back_inserter(worklist),
[](const VarNode& n){ return n.eq_index ; });
}
while(!worklist.empty()) {
size_t index = worklist.front();
worklist.pop_front();
// eps_eq_id contains indices of equations that were reduced to eps = eps (one side is eps)
if(eps_eq_id.find(index) != eps_eq_id.end())
continue;
std::set<BasicTerm> new_eps; // newly added epsilon terms
Predicate eq = this->formula.get_predicate(index);
if(!eq.is_equation())
continue;
std::set<BasicTerm> left = eq.get_left_set();
std::set<BasicTerm> right = eq.get_right_set();
if(is_subset(left, eps_set)) {
new_eps = set_difference(eq.get_side_vars(Predicate::EquationSideType::Right), eps_set);
eps_eq_id.insert(index);
} else if(is_subset(right, eps_set)) {
new_eps = set_difference(eq.get_side_vars(Predicate::EquationSideType::Left), eps_set);
eps_eq_id.insert(index);
}
for(const BasicTerm& t : new_eps) {
eps_set.insert(t);
std::set<VarNode> nds = get_formula().get_var_occurr(t);
std::transform(nds.begin(), nds.end(), std::back_inserter(worklist),
[](const VarNode& n){ return n.eq_index ; });
}
}
for(const BasicTerm& t : eps_set) {
if(t.is_variable()) {
update_reg_constr(t, Concat()); // L(t) = L(t) \cap {\eps}
}
this->formula.replace(Concat({t}), Concat());
// add len constraint |X| = 0
this->add_to_len_formula(Predicate(PredicateType::Equation, {Concat({t}), Concat()}).get_formula_eq());
}
this->formula.clean_predicates();
// update dependencies (overapproximation). Each remaining predicate depends on all epsilon equations.
for(const auto& pr : this->formula.get_predicates()) {
// might result to the case {0: {0,1,...}}
this->dependency[pr.first].insert(eps_eq_id.begin(), eps_eq_id.end());
}
STRACE("str-prep", tout << print_info(is_trace_enabled("str-nfa")));
}
/**
* @brief Gather information about a concatenation for equation separation.
*
* @param concat Concatenation
* @param res vector where i-th position contains a pair (M,n) where M is a map mapping variables to
* the number of their occurrences (multimap) preceeding position i in @p concat and n is a length of all
* literals preceeding @p concat.
*/
void FormulaPreprocessor::get_concat_gather(const Concat& concat, SepEqsGather& res) const {
std::pair<std::map<BasicTerm, unsigned>, unsigned> prev = { std::map<BasicTerm,unsigned>(), 0 };
for(const BasicTerm& t : concat) {
std::pair<std::map<BasicTerm, unsigned>, unsigned> new_val(prev);
if(t.is_variable()) {
new_val.first[t]++;
} else if(t.is_literal()) {
new_val.second += t.get_name().length();
} else {
assert(false);
}
res.push_back(new_val);
prev = new_val;
}
}
/**
* @brief Separate equations into a set of new equations.
*
* @param eq Equation
* @param gather_left Gathered informaiton about left side
* @param gather_right Gathered informaiton about right side
* @param res Set of new equations
*/
void FormulaPreprocessor::separate_eq(const Predicate& eq, const SepEqsGather& gather_left, SepEqsGather& gather_right, std::set<Predicate>& res) const {
Concat left = eq.get_left_side();
Concat right = eq.get_right_side();
auto it_left = left.begin();
auto it_right = right.begin();
assert(eq.is_equation());
for(size_t i = 0; i < gather_left.size(); i++) {
for(size_t j = 0; j < gather_right.size(); j++) {
if(gather_left[i] == gather_right[j]) {
res.insert(Predicate(PredicateType::Equation, std::vector<Concat>({
Concat(it_left, left.begin()+i+1),
Concat(it_right, right.begin()+j+1)
})));
it_left = left.begin()+i+1;
it_right = right.begin()+j+1;
}
}
}
Concat left_rest = Concat(it_left, left.end());
Concat right_rest = Concat(it_right, right.end());
if(left_rest.empty() && right_rest.empty()) // nothing to be added
return;
if(left_rest.empty())
left_rest.push_back(BasicTerm(BasicTermType::Literal, "")); // avoid empty side by putting there epsilon
if(right_rest.empty())
right_rest.push_back(BasicTerm(BasicTermType::Literal, "")); // avoid empty side by putting there epsilon
Predicate rest = Predicate(PredicateType::Equation, std::vector<Concat>({
left_rest,
right_rest
}));
res.insert(rest);
}
/**
* @brief Separate equations.
*/
void FormulaPreprocessor::separate_eqs() {
STRACE("str-prep", tout << "Preprocessing step - separate_eqs\n";);
std::set<Predicate> add_eqs;
std::set<size_t> rem_ids;
for(const auto& pr : this->formula.get_predicates()) {
if(!pr.second.is_equation())
continue;
SepEqsGather gather_left, gather_right;
std::set<Predicate> res;
get_concat_gather(pr.second.get_left_side(), gather_left);
get_concat_gather(pr.second.get_right_side(), gather_right);
separate_eq(pr.second, gather_left, gather_right, res);
if(res.size() > 1) {
add_eqs.insert(res.begin(), res.end());
rem_ids.insert(pr.first);
}
}
for(const Predicate& p : add_eqs) {
this->formula.add_predicate(p);
}
for(const size_t & i : rem_ids) {
this->formula.remove_predicate(i);
}
STRACE("str-prep", tout << print_info(is_trace_enabled("str-nfa")));
}
/**
* @brief Gather variables allowing left/right extension. A variable X is left extensible if
* L(X) = \Sigma^*.L(X) (right extensibility analogously). \Sigma is collected among all
* automata in the assignment.
*
* @param side Left/right extension
* @param res Extensible variables
*/
void FormulaPreprocessor::gather_extended_vars(Predicate::EquationSideType side, std::set<BasicTerm>& res) {
mata::nfa::Nfa sigma_star = this->aut_ass.sigma_star_automaton();
for(const auto& pr : this->formula.get_varmap()) {
if(pr.second.size() > 0) {
mata::nfa::Nfa concat;
if(side == Predicate::EquationSideType::Left)
concat = mata::nfa::concatenate(sigma_star, *(this->aut_ass.at(pr.first)));
else
concat = mata::nfa::concatenate(*(this->aut_ass.at(pr.first)), sigma_star);
if(mata::nfa::are_equivalent(*(this->aut_ass.at(pr.first)), concat)) {
res.insert(pr.first);
}
}
}
}
/**
* @brief Remove extensible variables from beginning/end of an equation:
* X = Y1 Y2 Y3 if Y1 occurrs always first (and X is the left side) and
* Y2 is left extensible. We can remove Y1 (similarly the right extensibility and removing from
* the end of the equation).
*/
void FormulaPreprocessor::remove_extension() {
STRACE("str-prep", tout << "Preprocessing step - remove_extension\n";);
std::set<BasicTerm> begin_star, end_star;
gather_extended_vars(Predicate::EquationSideType::Left, begin_star);
gather_extended_vars(Predicate::EquationSideType::Right, end_star);
using ExtVarMap = std::map<BasicTerm, std::vector<BasicTerm>>;
ExtVarMap b_map, e_map;
VarMap varmap = this->formula.get_varmap();
auto flt = [&varmap](ExtVarMap& mp) {
std::map<BasicTerm,BasicTerm> ret;
for(const auto& pr : mp) {
std::set<BasicTerm> sing(pr.second.begin(), pr.second.end());
if(varmap[pr.first].size() == pr.second.size() && sing.size() == 1) {
ret.insert({pr.first, *sing.begin()});
}
}
return ret;
};
for(const auto& pr : this->formula.get_predicates()) {
if(!pr.second.is_equation())
continue;
Concat left = pr.second.get_left_side();
Concat right = pr.second.get_right_side();
/**
* TODO: Extend to both sides (so far just the left side is considered)
*/
if(left.size() == 1 && right.size() > 1) {
if(right[0].is_variable() && left[0].is_variable()
&& right[1].is_variable() && begin_star.find(right[1]) != begin_star.end()
&& varmap[right[1]].size() == 1
&& this->len_variables.find(right[0]) == this->len_variables.end()
&& this->len_variables.find(right[1]) == this->len_variables.end() ) {
b_map.insert({right[0], {}});
b_map[right[0]].push_back(left[0]);
}
if(right[right.size()-1].is_variable() && left[0].is_variable()
&& right[right.size()-2].is_variable() && end_star.find(right[right.size()-2]) != end_star.end()
&& varmap[right[right.size()-2]].size() == 1
&& this->len_variables.find(right[right.size()-1]) == this->len_variables.end()
&& this->len_variables.find(right[right.size()-2]) == this->len_variables.end() ) {
e_map.insert({right[right.size()-1], {}});
e_map[right[right.size()-1]].push_back(left[0]);
}
}
}
std::map<BasicTerm,BasicTerm> b_rem = flt(b_map);
std::map<BasicTerm,BasicTerm> e_rem = flt(e_map);
std::map<size_t, Predicate> updates;
for(const auto& pr : this->formula.get_predicates()) {
Predicate pred = pr.second;
if(!pred.is_equation()) continue;
if(pred.get_left_side().size() > 1) continue;
if(pred.get_right_side().size() < 2) continue;
Concat right_side = pred.get_right_side();
bool modif = false;
for(const auto& beg : b_rem) {
if(pred.get_left_side()[0] == beg.second && right_side[0] == beg.first) {
// X = Y1 Y2 ...
Concat modif_side(right_side.begin()+1, right_side.end());
pred = Predicate(PredicateType::Equation, std::vector<Concat>{pred.get_left_side(), modif_side} );
modif = true;
break;
}
}
right_side = pred.get_right_side();
for(const auto& end : e_rem) {
if(pred.get_left_side()[0] == end.second && *(right_side.end()-1) == end.first) {
pred = Predicate(PredicateType::Equation, std::vector<Concat>{pred.get_left_side(), Concat(right_side.begin(), right_side.end()-1)} );
modif = true;
break;
}
}
if(modif) updates.insert({pr.first, pred});
}
// We do not need to update dependencies
for(const auto& pr : updates) {
this->update_predicate(pr.first, pr.second);
}
STRACE("str-prep", tout << print_info(is_trace_enabled("str-nfa")));
}
/**
* @brief Remove trivial equations of the form X = X
*/