-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathqe_arith_plugin.cpp
2611 lines (2313 loc) · 93.8 KB
/
qe_arith_plugin.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
/*++
Copyright (c) 2010 Microsoft Corporation
Module Name:
arith_plugin.cpp
Abstract:
Eliminate Arithmetical variable from formula
Author:
Nikolaj Bjorner (nbjorner) 2010-02-19
Revision History:
--*/
#include "qe/qe.h"
#include "ast/ast_pp.h"
#include "ast/rewriter/expr_safe_replace.h"
#include "ast/rewriter/bool_rewriter.h"
#include "ast/bv_decl_plugin.h"
#include "ast/arith_decl_plugin.h"
#include "smt/arith_eq_solver.h"
#include "ast/rewriter/arith_rewriter.h"
#include "ast/rewriter/th_rewriter.h"
#include "ast/rewriter/factor_rewriter.h"
#include "util/obj_pair_hashtable.h"
#include "qe/nlarith_util.h"
#include "model/model_evaluator.h"
#include "smt/smt_kernel.h"
#include "qe/mbp/mbp_arith.h"
namespace qe {
static bool is_divides(arith_util& a, expr* e1, expr* e2, rational& k, expr_ref& p) {
expr* t1, *t2;
if (a.is_mod(e2, t1, t2) &&
a.is_numeral(e1, k) &&
k.is_zero() &&
a.is_numeral(t2, k)) {
p = t1;
return true;
}
return false;
}
static bool is_divides(arith_util& a, expr* e, rational& k, expr_ref& t) {
expr* e1, *e2;
if (!a.get_manager().is_eq(e, e1, e2)) {
return false;
}
return is_divides(a, e1, e2, k, t) || is_divides(a, e2, e1, k, t);
}
class bound {
rational m_coeff;
expr_ref m_term;
bool m_is_strict;
public:
bound(ast_manager& m, rational const& n, expr* t, bool is_strict) : m_coeff(n), m_term(t, m), m_is_strict(is_strict) {
}
bool is_strict() const { return m_is_strict; }
expr* term() const { return m_term.get(); }
rational const& coeff() const { return m_coeff; }
void update(rational const& k, expr* t) {
m_coeff = k;
m_term = t;
}
void pp(std::ostream& out, app* x) {
ast_manager& m = m_term.get_manager();
out << "(<= (+ (* " << coeff() << " " << mk_pp(x, m)
<< ") " << mk_pp(term(), m) << ") 0)";
}
};
typedef rational numeral;
// m_k | (m_a * x + m_term)
class div_constraint {
numeral m_k;
numeral m_a;
expr* m_term;
public:
div_constraint(numeral const& k, numeral const& a, expr* t):
m_k(k), m_a(a), m_term(t) {}
numeral const& a() const { return m_a; }
numeral const& k() const { return m_k; }
expr* t() const { return m_term; }
numeral& a_ref() { return m_a; }
numeral& k_ref() { return m_k; }
expr*& t_ref() { return m_term; }
};
typedef vector<div_constraint> div_constraints;
class arith_qe_util {
ast_manager& m;
i_solver_context& m_ctx;
public:
arith_util m_arith; // initialize before m_zero_i, etc.
th_rewriter simplify;
app_ref_vector m_vars_added;
private:
arith_eq_solver m_arith_solver;
bv_util m_bv;
expr_ref m_zero_i;
expr_ref m_one_i;
expr_ref m_minus_one_i;
expr_ref m_zero_r;
expr_ref m_one_r;
expr_ref m_tmp;
public:
expr_safe_replace m_replace;
bool_rewriter m_bool_rewriter;
arith_rewriter m_arith_rewriter;
arith_qe_util(ast_manager& m, smt_params& p, i_solver_context& ctx) :
m(m),
m_ctx(ctx),
m_arith(m),
simplify(m),
m_vars_added(m),
m_arith_solver(m),
m_bv(m),
m_zero_i(m_arith.mk_numeral(numeral(0), true), m),
m_one_i(m_arith.mk_numeral(numeral(1), true), m),
m_minus_one_i(m_arith.mk_numeral(numeral(-1), true), m),
m_zero_r(m_arith.mk_numeral(numeral(0), false), m),
m_one_r(m_arith.mk_numeral(numeral(1), false), m),
m_tmp(m),
m_replace(m),
m_bool_rewriter(m),
m_arith_rewriter(m) {
}
ast_manager& get_manager() { return m; }
//
// match e := k*x + rest, where k != 0.
//
bool get_coeff(contains_app& contains_x, expr* p, rational& k, expr_ref& rest) {
app* x = contains_x.x();
ptr_vector<expr> restl, todo;
todo.push_back(p);
bool found = false;
expr* e1, *e2;
while (!todo.empty()) {
expr* e = todo.back();
todo.pop_back();
if (m_arith.is_add(e)) {
for (unsigned i = 0; i < to_app(e)->get_num_args(); ++i) {
todo.push_back(to_app(e)->get_arg(i));
}
}
else if (e == x) {
k = numeral(1);
found = true;
break;
}
else if (m_arith.is_mul(e, e1, e2) &&
e1 == x &&
m_arith.is_numeral(e2, k)) {
found = true;
break;
}
else if (m_arith.is_mul(e, e1, e2) &&
e2 == x &&
m_arith.is_numeral(e1, k)) {
found = true;
break;
}
else {
restl.push_back(e);
}
}
if (!found) {
TRACE("qe_verbose",
tout
<< "Did not find: "
<< mk_pp(x, m) << " in "
<< mk_pp(p, m) << "\n";
);
return false;
}
while (!todo.empty()) {
restl.push_back(todo.back());
todo.pop_back();
}
if (restl.empty()) {
rest = mk_zero(x);
}
else {
rest = m_arith.mk_add(restl.size(), restl.data());
}
if (contains_x(rest)) {
return false;
}
TRACE("qe_verbose",
tout
<< mk_pp(p, m) << " = "
<< "(+ (* " << k << " "
<< mk_pp(x, m) << ") "
<< mk_pp(rest, m) << ")\n";
);
return true;
}
//
// match p := k + rest
// where k is a numeral and rest does not contain numerals.
//
void get_const(expr* p, rational& k, expr_ref& rest) {
ptr_vector<expr> todo, restl;
todo.push_back(p);
k = numeral(0);
while(!todo.empty()) {
p = todo.back();
todo.pop_back();
if (m_arith.is_add(p)) {
for (unsigned i = 0; i < to_app(p)->get_num_args(); ++i) {
todo.push_back(to_app(p)->get_arg(i));
}
}
else if (m_arith.is_numeral(p, k)) {
break;
}
else {
restl.push_back(p);
}
}
while (!todo.empty()) {
restl.push_back(todo.back());
todo.pop_back();
}
if (restl.empty()) {
rest = mk_zero(p);
}
else {
rest = m_arith.mk_add(restl.size(), restl.data());
}
}
//
// match (not ne)
bool is_neg(app* e, expr_ref& ne) {
if (m.is_not(e)) {
ne = to_app(e)->get_arg(0);
return true;
}
return false;
}
bool is_le(app* e, expr_ref& p) {
return is_le_ge_core<1>(e, p);
}
bool is_ge(app* e, expr_ref& p) {
return is_le_ge_core<0>(e, p);
}
// match e = p < 0 or p > 0
bool is_lt(app* e, expr_ref& p) {
numeral k;
expr* a1, *a2;
if (m_arith.is_lt(e, a1, a2) || m_arith.is_gt(e, a2, a1)) {
p = a1;
if (m_arith.is_numeral(a2, k) && k.is_zero()) {
return true;
}
}
else {
return false;
}
p = mk_sub(p, a2);
simplify(p);
return true;
}
//
// match 0 == p mod k, p mod k == 0
//
bool is_divides(expr* e, numeral& k, expr_ref& p) {
return qe::is_divides(m_arith, e, k, p);
}
bool is_not_divides(app* e, app_ref& n, numeral& k, expr_ref& p) {
if (!m.is_not(e)) {
return false;
}
if (!is_app(to_app(e)->get_arg(0))) {
return false;
}
n = to_app(to_app(e)->get_arg(0));
return is_divides(n, k, p);
}
bool is_real(app* x) const { return m_arith.is_real(x); }
//
// b*t <= a*s
//
template<bool is_strict>
void mk_bound_aux(rational const& a, expr* t, rational const& b, expr* s, expr_ref& result) {
SASSERT(a.is_neg() == b.is_neg());
expr_ref tt(t, m), ss(s, m), e(m);
// hack to fix weird gcc compilation error
rational abs_a(a);
rational abs_b(b);
if (abs_a.is_neg()) abs_a.neg();
if (abs_b.is_neg()) abs_b.neg();
ss = mk_mul(abs_a, ss);
tt = mk_mul(abs_b, tt);
if(a.is_neg()) {
e = mk_sub(tt, ss);
if (is_strict) {
if (m_arith.is_int(e)) {
e = mk_add(e, m_one_i);
mk_le(e, result);
}
else {
mk_lt(e, result);
}
}
else {
mk_le(e, result);
}
}
else {
e = mk_sub(ss, tt);
if (is_strict) {
if (m_arith.is_int(e)) {
e = mk_add(e, m_one_i);
mk_le(e, result);
}
else {
mk_lt(e, result);
}
}
else {
mk_le(e, result);
}
}
}
void mk_bound(rational const& a, expr* t, rational const& b, expr* s, expr_ref& result) {
mk_bound_aux<false>(a, t, b, s, result);
}
void mk_strict_bound(rational const& a, expr* t, rational const& b, expr* s, expr_ref& result) {
mk_bound_aux<true>(a, t, b, s, result);
}
void mk_divides(numeral n, expr* e, expr_ref& result) {
SASSERT(n.is_int());
expr_ref tmp1(e, m), tmp2(m);
simplify(tmp1);
m_arith_rewriter.mk_mod(tmp1, mk_numeral(n), tmp2);
m_bool_rewriter.mk_eq(m_zero_i, tmp2, result);
}
void mk_div(expr* a, numeral const & k, expr_ref& result) {
result = m_arith.mk_div(a, m_arith.mk_numeral(k, false));
simplify(result);
}
expr* mk_numeral(numeral const& k, bool is_int = true) { return m_arith.mk_numeral(k, is_int); }
expr* mk_numeral(int k, bool is_int) { return mk_numeral(numeral(k),is_int); }
expr* mk_uminus(expr* e) {
return m_arith.mk_uminus(e);
}
expr* mk_abs(expr* e) {
rational val;
if (m_arith.is_numeral(e, val)) {
if (val.is_neg()) {
return m_arith.mk_uminus(e);
}
else {
return e;
}
}
else {
return m.mk_ite(m_arith.mk_le(mk_zero(e), e), e, m_arith.mk_uminus(e));
}
}
template<bool is_max>
expr_ref mk_min_max(unsigned num_args, expr* const* args) {
SASSERT(num_args > 0);
if (num_args == 1) {
return expr_ref(args[0], m);
}
else {
expr_ref e2 = mk_min_max<is_max>(num_args-1,args+1);
expr* e1 = args[0];
expr* cmp = is_max?m_arith.mk_le(e1, e2):m_arith.mk_le(e2, e1);
return expr_ref(m.mk_ite(cmp, e2, e1), m);
}
}
expr_ref mk_max(unsigned num_args, expr* const* args) {
return mk_min_max<true>(num_args, args);
}
expr_ref mk_min(unsigned num_args, expr* const* args) {
return mk_min_max<false>(num_args, args);
}
expr* mk_mul(expr* a, expr* b) { return m_arith.mk_mul(a,b); }
expr* mk_add(expr* a, expr* b) { return m_arith.mk_add(a,b); }
expr* mk_sub(expr* a, expr* b) { return m_arith.mk_sub(a,b); }
expr* mk_mul(numeral const& a, expr* b) {
if (a.is_one()) return b;
return m_arith.mk_mul(mk_numeral(a, m_arith.is_int(b)),b);
}
expr* mk_zero(sort* s) { return m_arith.is_int(s)?m_zero_i:m_zero_r; }
expr* mk_zero(expr* e) { return m_arith.is_int(e)?m_zero_i:m_zero_r; }
expr* mk_one(sort* s) { return m_arith.is_int(s)?m_one_i:m_one_r; }
expr* mk_one(expr* e) { return m_arith.is_int(e)?m_one_i:m_one_r; }
void mk_le(expr* e, expr_ref& result) {
expr_ref tmp(e, m);
simplify(tmp);
m_arith_rewriter.mk_le(tmp, mk_zero(e), result);
}
void mk_lt(expr* e, expr_ref& result) {
rational r;
if (m_arith.is_numeral(e, r)) {
if (r.is_neg()) {
result = m.mk_true();
}
else {
result = m.mk_false();
}
}
else if (m_arith.is_int(e)) {
result = m_arith.mk_le(e, m_minus_one_i);
}
else {
result = m.mk_not(m_arith.mk_le(mk_zero(e), e));
}
simplify(result);
TRACE("qe_verbose", tout << "mk_lt " << mk_pp(result, m) << "\n";);
}
// ax + t = 0
void mk_eq(rational const& a, app* x, expr* t, expr_ref& result) {
result = m_arith.mk_eq(mk_add(mk_mul(a, x), t), mk_zero(x));
}
void mk_and(unsigned sz, expr*const* args, expr_ref& result) {
m_bool_rewriter.mk_and(sz, args, result);
}
void mk_and(expr* e1, expr* e2, expr_ref& result) {
m_bool_rewriter.mk_and(e1, e2, result);
}
void add_and(expr* e, ptr_vector<expr>& conjs) {
if (m.is_and(e)) {
conjs.append(to_app(e)->get_num_args(), to_app(e)->get_args());
}
else {
conjs.push_back(e);
}
}
void mk_flat_and(expr* e1, expr* e2, expr_ref& result) {
ptr_vector<expr> conjs;
add_and(e1, conjs);
add_and(e2, conjs);
m_bool_rewriter.mk_and(conjs.size(), conjs.data(), result);
}
void mk_or(unsigned sz, expr*const* args, expr_ref& result) {
m_bool_rewriter.mk_or(sz, args, result);
}
void mk_or(expr* e1, expr* e2, expr_ref& result) {
m_bool_rewriter.mk_or(e1, e2, result);
}
//
// b*t <= a*s
//
void mk_resolve(app* x, bool is_strict, rational const& a, expr* t, rational const& b, expr* s, expr_ref& result) {
rational abs_a(abs(a)), abs_b(abs(b));
SASSERT(a.is_neg() == b.is_pos());
SASSERT(!is_strict || (abs_a.is_one() && abs_b.is_one()));
expr_ref bt(mk_mul(abs_b, t), m);
expr_ref as(mk_mul(abs_a, s), m);
expr_ref as_bt(mk_add(as, bt), m);
if(is_strict) {
mk_lt(as_bt, result);
}
else {
mk_le(as_bt, result);
}
if (!abs_a.is_one() && !abs_b.is_one()) {
// integer resolution case.
SASSERT(!is_strict);
SASSERT(abs_a > rational::one() && abs_b > rational::one());
expr_ref slack(mk_numeral((abs_a-numeral(1))*(abs_b-numeral(1)), true), m);
expr_ref result1(m), result2(m);
// a*s + b*t <= 0
expr_ref as_bt_le_0(result, m), tmp2(m), asz_bt_le_0(m), tmp3(m), tmp4(m);
expr_ref b_divides_sz(m);
// a*s + b*t + (a-1)(b-1) <= 0
tmp2 = m_arith.mk_add(as_bt, slack);
mk_le(tmp2, result1);
rational a1 = a, b1 = b;
if (abs_a < abs_b) {
std::swap(abs_a, abs_b);
std::swap(a1, b1);
std::swap(s, t);
std::swap(as, bt);
}
SASSERT(abs_a >= abs_b);
// create finite disjunction for |b|.
// exists x, z in [0 .. |b|-2] . b*x + s + z = 0 && ax + t <= 0 && bx + s <= 0
// <=>
// exists x, z in [0 .. |b|-2] . b*x = -z - s && ax + t <= 0 && bx + s <= 0
// <=>
// exists x, z in [0 .. |b|-2] . b*x = -z - s && a|b|x + |b|t <= 0 && bx + s <= 0
// <=>
// exists x, z in [0 .. |b|-2] . b*x = -z - s && a|b|x + |b|t <= 0 && -z - s + s <= 0
// <=>
// exists x, z in [0 .. |b|-2] . b*x = -z - s && a|b|x + |b|t <= 0 && -z <= 0
// <=>
// exists x, z in [0 .. |b|-2] . b*x = -z - s && a|b|x + |b|t <= 0
// <=>
// exists x, z in [0 .. |b|-2] . b*x = -z - s && a*n_sign(b)(s + z) + |b|t <= 0
// <=>
// exists z in [0 .. |b|-2] . |b| | (z + s) && a*n_sign(b)(s + z) + |b|t <= 0
//
expr_ref sz(mk_add(s, x), m);
if (b1.is_pos()) {
sz = m_arith.mk_uminus(sz);
}
tmp4 = mk_add(mk_mul(a1, sz), bt);
mk_le(tmp4, asz_bt_le_0);
if (to_app(asz_bt_le_0)->get_arg(0) == x &&
m_arith.is_zero(to_app(asz_bt_le_0)->get_arg(1))) {
// exists z in [0 .. |b|-2] . |b| | (z + s) && z <= 0
// <=>
// |b| | s
mk_divides(abs_b, s, tmp2);
}
else {
mk_divides(abs_b, sz, b_divides_sz);
mk_and(b_divides_sz, asz_bt_le_0, tmp4);
mk_big_or(abs_b - numeral(2), x, tmp4, tmp2);
TRACE("qe",
tout << "b | s + z: " << mk_pp(b_divides_sz, m) << "\n";
tout << "a(s+z) + bt <= 0: " << mk_pp(asz_bt_le_0, m) << "\n";
);
}
mk_flat_and(as_bt_le_0, tmp2, result2);
mk_or(result1, result2, result);
simplify(result);
// a*s + b*t + (a-1)(b-1) <= 0
// or exists z in [0 .. |b|-2] . |b| | (z + s) && a*n_sign(b)(s + z) + |b|t <= 0
}
TRACE("qe",
{
tout << (is_strict?"strict":"non-strict") << "\n";
bound(m, a, t, false).pp(tout, x);
tout << "\n";
bound(m, b, s, false).pp(tout, x);
tout << "\n";
tout << mk_pp(result, m) << "\n";
});
}
struct mul_lt {
arith_util& u;
mul_lt(arith_qe_util& u): u(u.m_arith) {}
bool operator()(expr* n1, expr* n2) const {
expr* x, *y;
if (u.is_mul(n1, x, y) && u.is_numeral(x)) {
n1 = y;
}
if (u.is_mul(n2, x, y) && u.is_numeral(x)) {
n2 = y;
}
return n1->get_id() < n2->get_id();
}
};
void normalize_sum(expr_ref& p) {
simplify(p);
if (!m_arith.is_add(p)) {
return;
}
unsigned sz = to_app(p)->get_num_args();
ptr_buffer<expr> args;
for (unsigned i = 0; i < sz; ++i) {
args.push_back(to_app(p)->get_arg(i));
}
std::sort(args.begin(), args.end(), mul_lt(*this));
p = m_arith.mk_add(args.size(), args.data());
}
void pp_div(std::ostream& out, app* x, div_constraint const& div) {
out << div.k() << " | (" << div.a() << "*" << mk_pp(x, m)
<< " + " << mk_pp(div.t(), m) << ") ";
}
void pp_divs(std::ostream& out, app* x, div_constraints const& divs) {
for (unsigned i = 0; i < divs.size(); ++i) {
pp_div(out, x, divs[i]);
out << " ";
}
}
bool mk_atom(expr* e, bool p, expr_ref& result) {
// retain equalities.
if (!is_app(e)) {
return false;
}
app* a = to_app(e);
expr_ref t1(m), t2(m);
expr_ref tmp1(m), tmp2(m);
rational k;
expr* a0, *a1;
if (p && is_divides(a, k, tmp1)) {
result = e;
}
else if (!p && is_divides(a, k, tmp1)) {
m_bool_rewriter.mk_not(e, result);
}
else if (p && m.is_eq(e, a0, a1) && is_arith(a0)) {
t1 = mk_sub(a0, a1);
simplify(t1);
t2 = mk_sub(a1, a0);
simplify(t2);
mk_le(t1, tmp1);
mk_le(t2, tmp2);
mk_and(tmp1, tmp2, result);
}
else if (!p && m.is_eq(e, a0, a1) && m_arith.is_int(a0)) {
tmp1 = mk_sub(a0, a1);
t1 = mk_add(mk_one(a0), tmp1);
simplify(t1);
t2 = mk_sub(mk_one(a0), tmp1);
simplify(t2);
mk_le(t1, tmp1); // a0 < a1 <=> 1 + a0 - a1 <= 0
mk_le(t2, tmp2); // a0 > a1 <=> 1 - a0 + a1 <= 0
mk_or(tmp1, tmp2, result);
}
else if (!p && m.is_eq(e, a0, a1) && m_arith.is_real(a0)) {
t1 = mk_sub(a0, a1);
simplify(t1);
t2 = mk_sub(a1, a0);
simplify(t2);
mk_lt(t1, tmp1);
mk_lt(t2, tmp2);
mk_or(tmp1, tmp2, result);
}
else if (!p && (m_arith.is_le(e, a0, a1) || m_arith.is_ge(e, a1, a0))) {
tmp1 = mk_sub(a1, a0);
mk_lt(tmp1, result);
}
else if (p && (m_arith.is_le(e) || m_arith.is_ge(e))) {
result = e;
}
else if (p && (m_arith.is_lt(e, a0, a1) || m_arith.is_gt(e, a1, a0))) {
tmp1 = mk_sub(a0, a1);
mk_lt(tmp1, result);
}
else if (!p && (m_arith.is_lt(e, a0, a1) || m_arith.is_gt(e, a1, a0))) {
tmp1 = mk_sub(a1, a0);
mk_le(tmp1, result);
}
else {
return false;
}
TRACE("qe_verbose", tout << "Atom: " << mk_pp(result, m) << "\n";);
return true;
}
void mk_bounded_var(rational const& n, app_ref& z_bv, app_ref& z) {
rational two(2), b(n);
unsigned sz = 0;
do {
++sz;
b = div(b, two);
}
while (b.is_pos());
sort* s = m_bv.mk_sort(sz);
z_bv = m.mk_fresh_const("z", s);
expr_ref tmp(m);
z = m_bv.mk_bv2int(z_bv);
}
bool solve(conj_enum& conjs, expr* fml) {
expr_ref_vector eqs(m);
extract_equalities(conjs, eqs);
return reduce_equations(eqs, fml);
}
// ----------------------------------------------------------------------
//
// Equation solving features.
//
// Extract equalities from current goal.
//
void extract_equalities(conj_enum& conjs, expr_ref_vector& eqs) {
obj_hashtable<expr> leqs;
expr_ref_vector trail(m);
expr_ref tmp1(m), tmp2(m);
expr *a0, *a1;
eqs.reset();
for (expr* e : conjs) {
bool is_leq = false;
if (m.is_eq(e, a0, a1) && is_arith(a0)) {
m_arith_rewriter.mk_sub(a0, a1, tmp1);
simplify(tmp1);
eqs.push_back(tmp1);
}
else if (m_arith.is_le(e, a0, a1) || m_arith.is_ge(e, a1, a0)) {
m_arith_rewriter.mk_sub(a0, a1, tmp1);
is_leq = true;
}
else {
// drop equality.
}
if (is_leq) {
normalize_sum(tmp1);
tmp2 = m_arith.mk_uminus(tmp1);
normalize_sum(tmp2);
if (leqs.contains(tmp2)) {
eqs.push_back(tmp1);
TRACE("qe", tout << "found: " << mk_pp(tmp1, m) << "\n";);
}
else {
trail.push_back(tmp1);
leqs.insert(tmp1);
TRACE("qe_verbose", tout << "insert: " << mk_pp(tmp1, m) << "\n";);
}
}
}
}
void add_var(app* v, bool track = true) {
m_ctx.add_var(v);
if (track) m_vars_added.push_back(v);
}
private:
//
// match p <= 0 or p >= 0
//
template<unsigned IS_LE>
bool is_le_ge_core(app* e, expr_ref& p) {
numeral k;
expr_ref tmp(m);
expr* a2;
if (m_arith.is_le(e)) {
p = e->get_arg(1-IS_LE);
a2 = e->get_arg(IS_LE);
if (m_arith.is_numeral(a2, k) && k.is_zero()) {
return true;
}
}
else if (m_arith.is_ge(e)) {
p = e->get_arg(IS_LE);
a2 = e->get_arg(1-IS_LE);
if (m_arith.is_numeral(a2, k) && k.is_zero()) {
return true;
}
}
else {
return false;
}
p = mk_sub(p, a2);
simplify(p);
return true;
}
bool is_arith(expr* e) {
return m_arith.is_int(e) || m_arith.is_real(e);
}
void mk_big_or(numeral up, app* x, expr* body, expr_ref& result) {
TRACE("qe", tout << mk_pp(x, m) << " " << mk_pp(body, m) << "\n";);
if (numeral(1) >= up) {
mk_big_or_blast(up, x, body, result);
}
else {
mk_big_or_symbolic_blast(up, x, body, result);
}
}
void mk_big_or_blast(numeral up, app* x, expr* body, expr_ref& result) {
expr_ref_vector ors(m);
numeral index(0);
while (index <= up) {
expr* n = mk_numeral(index);
result = body;
m_replace.apply_substitution(x, n, result);
ors.push_back(result);
++index;
}
mk_or(ors.size(), ors.data(), result);
TRACE("qe",
tout
<< "[0 " << up << "] "
<< mk_pp(x, m) << "\n"
<< mk_pp(body, m) << "\n"
<< mk_pp(result, m) << "\n";);
}
void mk_big_or_symbolic(numeral up, app* x, expr* body, expr_ref& result) {
app_ref z_bv(m);
mk_big_or_symbolic(up, x, body, z_bv, result);
add_var(z_bv);
}
void mk_big_or_symbolic_blast(numeral up, app* x, expr* body, expr_ref& result) {
app_ref z_bv(m);
mk_big_or_symbolic(up, x, body, z_bv, result);
m_ctx.blast_or(z_bv, result);
}
void mk_big_or_symbolic(numeral up, app* x, expr* body, app_ref& z_bv, expr_ref& result) {
expr* e1 = m_arith.mk_le(x, m_arith.mk_numeral(up, true));
mk_flat_and(e1, body, result);
app_ref z(m);
mk_bounded_var(up, z_bv, z);
m_replace.apply_substitution(x, z, result);
}
//
// Determine if 'x' can be isolated.
// Return the coefficient if found.
//
bool isolate_x(expr* p, app* x, contains_app& contains_x, numeral& coeff) {
numeral k;
while (m_arith.is_add(p)) {
bool found_x = false;
expr* next_p = nullptr;
for (unsigned i = 0; i < to_app(p)->get_num_args(); ++i) {
expr* arg = to_app(p)->get_arg(i);
if (contains_x(arg)) {
if (found_x) {
return false;
}
found_x = true;
next_p = arg;
}
}
if (!next_p) {
return false;
}
p = next_p;
}
expr *e1, *e2;
if (p == x) {
coeff = numeral(1);
return true;
}
else if (m_arith.is_mul(p, e1, e2) &&
m_arith.is_numeral(e1, k) &&
e2 == x) {
coeff = k;
return true;
}
else if (m_arith.is_mul(p, e1, e2) &&
m_arith.is_numeral(e2, k) &&
e1 == x) {
coeff = k;
return true;
}
return false;
}
//
// Reduce equations.
// Singular equations eliminate variables directly.
// Linear equations eliminate original variables and introduce auxiliary variables.
//
bool reduce_equations(expr_ref_vector const& eqs, expr* fml) {
for (expr* eq : eqs) {
if (reduce_equation(eq, fml)) {
return true;
}
}
return false;
}
bool solve_singular(unsigned var_idx, expr* p, expr* fml) {
rational k;
expr_ref e(m), tmp(m);
app* x = m_ctx.get_var(var_idx);
if (!isolate_x(p, x, m_ctx.contains(var_idx), k)) {
return false;
}
if (m_arith.is_int(x) && !(abs(k).is_one())) {
return false;
}
if (abs(k).is_one()) {
if (k.is_neg()) {
e = m_arith.mk_add(p, x);
}
else {
e = m_arith.mk_sub(x, p);
}
}
else {
SASSERT(!m_arith.is_int(x));
// p = p' + k*x = 0
// <=>
// -k*x = p' = p - k*x
// =>
// x = (p - k*x)/ -k
expr* ke = m_arith.mk_numeral(-k, false);
tmp = m_arith.mk_mul(ke, x);
tmp = m_arith.mk_add(p, tmp);
e = m_arith.mk_div(tmp, ke);
}
TRACE("qe",
tout << "is singular:\n"
<< mk_pp(p, m) << "\n"
<< mk_pp(fml, m) << "\n"
<< mk_pp(x, m) << " = "
<< mk_pp(e, m) << "\n";
);
expr_ref result(fml, m);
m_replace.apply_substitution(x, e, result);
simplify(result);
TRACE("qe",
tout << "singular solved:\n"
<< mk_pp(result, m) << "\n";
);
m_ctx.elim_var(var_idx, result, e);
return true;
}
bool solve_singular(expr* p, expr* fml) {
unsigned num_vars = m_ctx.get_num_vars();
for (unsigned i = 0; i < num_vars; ++i) {
if (solve_singular(i, p, fml)) {
return true;
}