forked from Z3Prover/z3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
1349 lines (1188 loc) · 40.1 KB
/
example.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) 2015 Microsoft Corporation
--*/
#include<vector>
#include"z3++.h"
using namespace z3;
/**
Demonstration of how Z3 can be used to prove validity of
De Morgan's Duality Law: {e not(x and y) <-> (not x) or ( not y) }
*/
void demorgan() {
std::cout << "de-Morgan example\n";
context c;
expr x = c.bool_const("x");
expr y = c.bool_const("y");
expr conjecture = (!(x && y)) == (!x || !y);
solver s(c);
// adding the negation of the conjecture as a constraint.
s.add(!conjecture);
std::cout << s << "\n";
std::cout << s.to_smt2() << "\n";
switch (s.check()) {
case unsat: std::cout << "de-Morgan is valid\n"; break;
case sat: std::cout << "de-Morgan is not valid\n"; break;
case unknown: std::cout << "unknown\n"; break;
}
}
/**
\brief Find a model for <tt>x >= 1 and y < x + 3</tt>.
*/
void find_model_example1() {
std::cout << "find_model_example1\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
solver s(c);
s.add(x >= 1);
s.add(y < x + 3);
std::cout << s.check() << "\n";
model m = s.get_model();
std::cout << m << "\n";
// traversing the model
for (unsigned i = 0; i < m.size(); i++) {
func_decl v = m[i];
// this problem contains only constants
assert(v.arity() == 0);
std::cout << v.name() << " = " << m.get_const_interp(v) << "\n";
}
// we can evaluate expressions in the model.
std::cout << "x + y + 1 = " << m.eval(x + y + 1) << "\n";
}
/**
\brief Prove <tt>x = y implies g(x) = g(y)</tt>, and
disprove <tt>x = y implies g(g(x)) = g(y)</tt>.
This function demonstrates how to create uninterpreted types and
functions.
*/
void prove_example1() {
std::cout << "prove_example1\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
sort I = c.int_sort();
func_decl g = function("g", I, I);
solver s(c);
expr conjecture1 = implies(x == y, g(x) == g(y));
std::cout << "conjecture 1\n" << conjecture1 << "\n";
s.add(!conjecture1);
if (s.check() == unsat)
std::cout << "proved" << "\n";
else
std::cout << "failed to prove" << "\n";
s.reset(); // remove all assertions from solver s
expr conjecture2 = implies(x == y, g(g(x)) == g(y));
std::cout << "conjecture 2\n" << conjecture2 << "\n";
s.add(!conjecture2);
if (s.check() == unsat) {
std::cout << "proved" << "\n";
}
else {
std::cout << "failed to prove" << "\n";
model m = s.get_model();
std::cout << "counterexample:\n" << m << "\n";
std::cout << "g(g(x)) = " << m.eval(g(g(x))) << "\n";
std::cout << "g(y) = " << m.eval(g(y)) << "\n";
}
}
/**
\brief Prove <tt>not(g(g(x) - g(y)) = g(z)), x + z <= y <= x implies z < 0 </tt>.
Then, show that <tt>z < -1</tt> is not implied.
This example demonstrates how to combine uninterpreted functions and arithmetic.
*/
void prove_example2() {
std::cout << "prove_example1\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
sort I = c.int_sort();
func_decl g = function("g", I, I);
expr conjecture1 = implies(g(g(x) - g(y)) != g(z) && x + z <= y && y <= x,
z < 0);
solver s(c);
s.add(!conjecture1);
std::cout << "conjecture 1:\n" << conjecture1 << "\n";
if (s.check() == unsat)
std::cout << "proved" << "\n";
else
std::cout << "failed to prove" << "\n";
expr conjecture2 = implies(g(g(x) - g(y)) != g(z) && x + z <= y && y <= x,
z < -1);
s.reset();
s.add(!conjecture2);
std::cout << "conjecture 2:\n" << conjecture2 << "\n";
if (s.check() == unsat) {
std::cout << "proved" << "\n";
}
else {
std::cout << "failed to prove" << "\n";
std::cout << "counterexample:\n" << s.get_model() << "\n";
}
}
/**
\brief Nonlinear arithmetic example 1
*/
void nonlinear_example1() {
std::cout << "nonlinear example 1\n";
config cfg;
cfg.set("auto_config", true);
context c(cfg);
expr x = c.real_const("x");
expr y = c.real_const("y");
expr z = c.real_const("z");
solver s(c);
s.add(x*x + y*y == 1); // x^2 + y^2 == 1
s.add(x*x*x + z*z*z < c.real_val("1/2")); // x^3 + z^3 < 1/2
s.add(z != 0);
std::cout << s.check() << "\n";
model m = s.get_model();
std::cout << m << "\n";
set_param("pp.decimal", true); // set decimal notation
std::cout << "model in decimal notation\n";
std::cout << m << "\n";
set_param("pp.decimal-precision", 50); // increase number of decimal places to 50.
std::cout << "model using 50 decimal places\n";
std::cout << m << "\n";
set_param("pp.decimal", false); // disable decimal notation
}
/**
\brief Simple function that tries to prove the given conjecture using the following steps:
1- create a solver
2- assert the negation of the conjecture
3- checks if the result is unsat.
*/
void prove(expr conjecture) {
context & c = conjecture.ctx();
solver s(c);
s.add(!conjecture);
std::cout << "conjecture:\n" << conjecture << "\n";
if (s.check() == unsat) {
std::cout << "proved" << "\n";
}
else {
std::cout << "failed to prove" << "\n";
std::cout << "counterexample:\n" << s.get_model() << "\n";
}
}
/**
\brief Simple bit-vector example. This example disproves that x - 10 <= 0 IFF x <= 10 for (32-bit) machine integers
*/
void bitvector_example1() {
std::cout << "bitvector example 1\n";
context c;
expr x = c.bv_const("x", 32);
// using signed <=
prove((x - 10 <= 0) == (x <= 10));
// using unsigned <=
prove(ule(x - 10, 0) == ule(x, 10));
expr y = c.bv_const("y", 32);
prove(implies(concat(x, y) == concat(y, x), x == y));
}
/**
\brief Find x and y such that: x ^ y - 103 == x * y
*/
void bitvector_example2() {
std::cout << "bitvector example 2\n";
context c;
expr x = c.bv_const("x", 32);
expr y = c.bv_const("y", 32);
solver s(c);
// In C++, the operator == has higher precedence than ^.
s.add((x ^ y) - 103 == x * y);
std::cout << s << "\n";
std::cout << s.check() << "\n";
std::cout << s.get_model() << "\n";
}
/**
\brief Mixing C and C++ APIs.
*/
void capi_example() {
std::cout << "capi example\n";
context c;
expr x = c.bv_const("x", 32);
expr y = c.bv_const("y", 32);
// Invoking a C API function, and wrapping the result using an expr object.
expr r = to_expr(c, Z3_mk_bvsrem(c, x, y));
std::cout << "r: " << r << "\n";
}
/**
\brief Demonstrate how to evaluate expressions in a model.
*/
void eval_example1() {
std::cout << "eval example 1\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
solver s(c);
/* assert x < y */
s.add(x < y);
/* assert x > 2 */
s.add(x > 2);
std::cout << s.check() << "\n";
model m = s.get_model();
std::cout << "Model:\n" << m << "\n";
std::cout << "x+y = " << m.eval(x+y) << "\n";
}
/**
\brief Several contexts can be used simultaneously.
*/
void two_contexts_example1() {
std::cout << "two contexts example 1\n";
context c1, c2;
expr x = c1.int_const("x");
expr n = x + 1;
// We cannot mix expressions from different contexts, but we can copy
// an expression from one context to another.
// The following statement copies the expression n from c1 to c2.
expr n1 = to_expr(c2, Z3_translate(c1, n, c2));
std::cout << n1 << "\n";
}
/**
\brief Demonstrates how to catch API usage errors.
*/
void error_example() {
std::cout << "error example\n";
context c;
expr x = c.bool_const("x");
// Error using the C API can be detected using Z3_get_error_code.
// The next call fails because x is a constant.
//Z3_ast arg = Z3_get_app_arg(c, x, 0);
if (Z3_get_error_code(c) != Z3_OK) {
std::cout << "last call failed.\n";
}
// The C++ layer converts API usage errors into exceptions.
try {
// The next call fails because x is a Boolean.
expr n = x + 1;
}
catch (exception ex) {
std::cout << "failed: " << ex << "\n";
}
// The functions to_expr, to_sort and to_func_decl also convert C API errors into exceptions.
try {
expr arg = to_expr(c, Z3_get_app_arg(c, x, 0));
}
catch (exception ex) {
std::cout << "failed: " << ex << "\n";
}
}
/**
\brief Demonstrate different ways of creating rational numbers: decimal and fractional representations.
*/
void numeral_example() {
std::cout << "numeral example\n";
context c;
expr n1 = c.real_val("1/2");
expr n2 = c.real_val("0.5");
expr n3 = c.real_val(1, 2);
std::cout << n1 << " " << n2 << " " << n3 << "\n";
prove(n1 == n2 && n1 == n3);
n1 = c.real_val("-1/3");
n2 = c.real_val("-0.3333333333333333333333333333333333");
std::cout << n1 << " " << n2 << "\n";
prove(n1 != n2);
}
/**
\brief Test ite-term (if-then-else terms).
*/
void ite_example() {
std::cout << "if-then-else example\n";
context c;
expr f = c.bool_val(false);
expr one = c.int_val(1);
expr zero = c.int_val(0);
expr ite = to_expr(c, Z3_mk_ite(c, f, one, zero));
std::cout << "term: " << ite << "\n";
}
void ite_example2() {
std::cout << "if-then-else example2\n";
context c;
expr b = c.bool_const("b");
expr x = c.int_const("x");
expr y = c.int_const("y");
std::cout << (ite(b, x, y) > 0) << "\n";
}
/**
\brief Small example using quantifiers.
*/
void quantifier_example() {
std::cout << "quantifier example\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
sort I = c.int_sort();
func_decl f = function("f", I, I, I);
solver s(c);
// making sure model based quantifier instantiation is enabled.
params p(c);
p.set("mbqi", true);
s.set(p);
s.add(forall(x, y, f(x, y) >= 0));
expr a = c.int_const("a");
s.add(f(a, a) < a);
std::cout << s << "\n";
std::cout << s.check() << "\n";
std::cout << s.get_model() << "\n";
s.add(a < 0);
std::cout << s.check() << "\n";
}
/**
\brief Unsat core example
*/
void unsat_core_example1() {
std::cout << "unsat core example1\n";
context c;
// We use answer literals to track assertions.
// An answer literal is essentially a fresh Boolean marker
// that is used to track an assertion.
// For example, if we want to track assertion F, we
// create a fresh Boolean variable p and assert (p => F)
// Then we provide p as an argument for the check method.
expr p1 = c.bool_const("p1");
expr p2 = c.bool_const("p2");
expr p3 = c.bool_const("p3");
expr x = c.int_const("x");
expr y = c.int_const("y");
solver s(c);
s.add(implies(p1, x > 10));
s.add(implies(p1, y > x));
s.add(implies(p2, y < 5));
s.add(implies(p3, y > 0));
expr assumptions[3] = { p1, p2, p3 };
std::cout << s.check(3, assumptions) << "\n";
expr_vector core = s.unsat_core();
std::cout << core << "\n";
std::cout << "size: " << core.size() << "\n";
for (unsigned i = 0; i < core.size(); i++) {
std::cout << core[i] << "\n";
}
// Trying again without p2
expr assumptions2[2] = { p1, p3 };
std::cout << s.check(2, assumptions2) << "\n";
}
/**
\brief Unsat core example 2
*/
void unsat_core_example2() {
std::cout << "unsat core example 2\n";
context c;
// The answer literal mechanism, described in the previous example,
// tracks assertions. An assertion can be a complicated
// formula containing containing the conjunction of many subformulas.
expr p1 = c.bool_const("p1");
expr x = c.int_const("x");
expr y = c.int_const("y");
solver s(c);
expr F = x > 10 && y > x && y < 5 && y > 0;
s.add(implies(p1, F));
expr assumptions[1] = { p1 };
std::cout << s.check(1, assumptions) << "\n";
expr_vector core = s.unsat_core();
std::cout << core << "\n";
std::cout << "size: " << core.size() << "\n";
for (unsigned i = 0; i < core.size(); i++) {
std::cout << core[i] << "\n";
}
// The core is not very informative, since p1 is tracking the formula F
// that is a conjunction of subformulas.
// Now, we use the following piece of code to break this conjunction
// into individual subformulas. First, we flat the conjunctions by
// using the method simplify.
std::vector<expr> qs; // auxiliary vector used to store new answer literals.
assert(F.is_app()); // I'm assuming F is an application.
if (F.decl().decl_kind() == Z3_OP_AND) {
// F is a conjunction
std::cout << "F num. args (before simplify): " << F.num_args() << "\n";
F = F.simplify();
std::cout << "F num. args (after simplify): " << F.num_args() << "\n";
for (unsigned i = 0; i < F.num_args(); i++) {
std::cout << "Creating answer literal q" << i << " for " << F.arg(i) << "\n";
std::stringstream qname; qname << "q" << i;
expr qi = c.bool_const(qname.str().c_str()); // create a new answer literal
s.add(implies(qi, F.arg(i)));
qs.push_back(qi);
}
}
// The solver s already contains p1 => F
// To disable F, we add (not p1) as an additional assumption
qs.push_back(!p1);
std::cout << s.check(static_cast<unsigned>(qs.size()), &qs[0]) << "\n";
expr_vector core2 = s.unsat_core();
std::cout << core2 << "\n";
std::cout << "size: " << core2.size() << "\n";
for (unsigned i = 0; i < core2.size(); i++) {
std::cout << core2[i] << "\n";
}
}
/**
\brief Unsat core example 3
*/
void unsat_core_example3() {
// Extract unsat core using tracked assertions
std::cout << "unsat core example 3\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
solver s(c);
// enabling unsat core tracking
params p(c);
p.set("unsat_core", true);
s.set(p);
// The following assertion will not be tracked.
s.add(x > 0);
// The following assertion will be tracked using Boolean variable p1.
// The C++ wrapper will automatically create the Boolean variable.
s.add(y > 0, "p1");
// Asserting other tracked assertions.
s.add(x < 10, "p2");
s.add(y < 0, "p3");
std::cout << s.check() << "\n";
std::cout << s.unsat_core() << "\n";
}
void tactic_example1() {
/*
Z3 implements a methodology for orchestrating reasoning engines where "big" symbolic
reasoning steps are represented as functions known as tactics, and tactics are composed
using combinators known as tacticals. Tactics process sets of formulas called Goals.
When a tactic is applied to some goal G, four different outcomes are possible. The tactic succeeds
in showing G to be satisfiable (i.e., feasible); succeeds in showing G to be unsatisfiable (i.e., infeasible);
produces a sequence of subgoals; or fails. When reducing a goal G to a sequence of subgoals G1, ..., Gn,
we face the problem of model conversion. A model converter construct a model for G using a model for some subgoal Gi.
In this example, we create a goal g consisting of three formulas, and a tactic t composed of two built-in tactics:
simplify and solve-eqs. The tactic simplify apply transformations equivalent to the ones found in the command simplify.
The tactic solver-eqs eliminate variables using Gaussian elimination. Actually, solve-eqs is not restricted
only to linear arithmetic. It can also eliminate arbitrary variables.
Then, sequential composition combinator & applies simplify to the input goal and solve-eqs to each subgoal produced by simplify.
In this example, only one subgoal is produced.
*/
std::cout << "tactic example 1\n";
context c;
expr x = c.real_const("x");
expr y = c.real_const("y");
goal g(c);
g.add(x > 0);
g.add(y > 0);
g.add(x == y + 2);
std::cout << g << "\n";
tactic t1(c, "simplify");
tactic t2(c, "solve-eqs");
tactic t = t1 & t2;
apply_result r = t(g);
std::cout << r << "\n";
}
void tactic_example2() {
/*
In Z3, we say a clause is any constraint of the form (f_1 || ... || f_n).
The tactic split-clause will select a clause in the input goal, and split it n subgoals.
One for each subformula f_i.
*/
std::cout << "tactic example 2\n";
context c;
expr x = c.real_const("x");
expr y = c.real_const("y");
goal g(c);
g.add(x < 0 || x > 0);
g.add(x == y + 1);
g.add(y < 0);
tactic t(c, "split-clause");
apply_result r = t(g);
for (unsigned i = 0; i < r.size(); i++) {
std::cout << "subgoal " << i << "\n" << r[i] << "\n";
}
}
void tactic_example3() {
/*
- The choice combinator t | s first applies t to the given goal, if it fails then returns the result of s applied to the given goal.
- repeat(t) Keep applying the given tactic until no subgoal is modified by it.
- repeat(t, n) Keep applying the given tactic until no subgoal is modified by it, or the number of iterations is greater than n.
- try_for(t, ms) Apply tactic t to the input goal, if it does not return in ms milliseconds, it fails.
- with(t, params) Apply the given tactic using the given parameters.
*/
std::cout << "tactic example 3\n";
context c;
expr x = c.real_const("x");
expr y = c.real_const("y");
expr z = c.real_const("z");
goal g(c);
g.add(x == 0 || x == 1);
g.add(y == 0 || y == 1);
g.add(z == 0 || z == 1);
g.add(x + y + z > 2);
// split all clauses
tactic split_all = repeat(tactic(c, "split-clause") | tactic(c, "skip"));
std::cout << split_all(g) << "\n";
tactic split_at_most_2 = repeat(tactic(c, "split-clause") | tactic(c, "skip"), 1);
std::cout << split_at_most_2(g) << "\n";
// In the tactic split_solver, the tactic solve-eqs discharges all but one goal.
// Note that, this tactic generates one goal: the empty goal which is trivially satisfiable (i.e., feasible)
tactic split_solve = split_all & tactic(c, "solve-eqs");
std::cout << split_solve(g) << "\n";
}
void tactic_example4() {
/*
A tactic can be converted into a solver object using the method mk_solver().
If the tactic produces the empty goal, then the associated solver returns sat.
If the tactic produces a single goal containing False, then the solver returns unsat.
Otherwise, it returns unknown.
In this example, the tactic t implements a basic bit-vector solver using equation solving,
bit-blasting, and a propositional SAT solver.
We use the combinator `with` to configure our little solver.
We also include the tactic `aig` which tries to compress Boolean formulas using And-Inverted Graphs.
*/
std::cout << "tactic example 4\n";
context c;
params p(c);
p.set("mul2concat", true);
tactic t =
with(tactic(c, "simplify"), p) &
tactic(c, "solve-eqs") &
tactic(c, "bit-blast") &
tactic(c, "aig") &
tactic(c, "sat");
solver s = t.mk_solver();
expr x = c.bv_const("x", 16);
expr y = c.bv_const("y", 16);
s.add(x*32 + y == 13);
// In C++, the operator < has higher precedence than &.
s.add((x & y) < 10);
s.add(y > -100);
std::cout << s.check() << "\n";
model m = s.get_model();
std::cout << m << "\n";
std::cout << "x*32 + y = " << m.eval(x*32 + y) << "\n";
std::cout << "x & y = " << m.eval(x & y) << "\n";
}
void tactic_example5() {
/*
The tactic smt wraps the main solver in Z3 as a tactic.
*/
std::cout << "tactic example 5\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
solver s = tactic(c, "smt").mk_solver();
s.add(x > y + 1);
std::cout << s.check() << "\n";
std::cout << s.get_model() << "\n";
}
void tactic_example6() {
/*
In this example, we show how to implement a solver for integer arithmetic using SAT.
The solver is complete only for problems where every variable has a lower and upper bound.
*/
std::cout << "tactic example 6\n";
context c;
params p(c);
p.set("arith_lhs", true);
p.set("som", true); // sum-of-monomials normal form
solver s =
(with(tactic(c, "simplify"), p) &
tactic(c, "normalize-bounds") &
tactic(c, "lia2pb") &
tactic(c, "pb2bv") &
tactic(c, "bit-blast") &
tactic(c, "sat")).mk_solver();
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
s.add(x > 0 && x < 10);
s.add(y > 0 && y < 10);
s.add(z > 0 && z < 10);
s.add(3*y + 2*x == z);
std::cout << s.check() << "\n";
std::cout << s.get_model() << "\n";
s.reset();
s.add(3*y + 2*x == z);
std::cout << s.check() << "\n";
}
void tactic_example7() {
/*
Tactics can be combined with solvers.
For example, we can apply a tactic to a goal, produced a set of subgoals,
then select one of the subgoals and solve it using a solver.
This example demonstrates how to do that, and
how to use model converters to convert a model for a subgoal into a model for the original goal.
*/
std::cout << "tactic example 7\n";
context c;
tactic t =
tactic(c, "simplify") &
tactic(c, "normalize-bounds") &
tactic(c, "solve-eqs");
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
goal g(c);
g.add(x > 10);
g.add(y == x + 3);
g.add(z > y);
apply_result r = t(g);
// r contains only one subgoal
std::cout << r << "\n";
solver s(c);
goal subgoal = r[0];
for (unsigned i = 0; i < subgoal.size(); i++) {
s.add(subgoal[i]);
}
std::cout << s.check() << "\n";
model m = s.get_model();
std::cout << "model for subgoal:\n" << m << "\n";
std::cout << "model for original goal:\n" << subgoal.convert_model(m) << "\n";
}
void tactic_example8() {
/*
Probes (aka formula measures) are evaluated over goals.
Boolean expressions over them can be built using relational operators and Boolean connectives.
The tactic fail_if(cond) fails if the given goal does not satisfy the condition cond.
Many numeric and Boolean measures are available in Z3.
In this example, we build a simple tactic using fail_if.
It also shows that a probe can be applied directly to a goal.
*/
std::cout << "tactic example 8\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
goal g(c);
g.add(x + y + z > 0);
probe p(c, "num-consts");
std::cout << "num-consts: " << p(g) << "\n";
tactic t = fail_if(p > 2);
try {
t(g);
}
catch (exception) {
std::cout << "tactic failed...\n";
}
std::cout << "trying again...\n";
g.reset();
g.add(x + y > 0);
std::cout << t(g) << "\n";
}
void tactic_example9() {
/*
The combinator (tactical) cond(p, t1, t2) is a shorthand for:
(fail_if(p) & t1) | t2
The combinator when(p, t) is a shorthand for:
cond(p, t, tactic(c, "skip"))
The tactic skip just returns the input goal.
This example demonstrates how to use the cond combinator.
*/
std::cout << "tactic example 9\n";
context c;
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
goal g(c);
g.add(x*x - y*y >= 0);
probe p(c, "num-consts");
tactic t = cond(p > 2, tactic(c, "simplify"), tactic(c, "factor"));
std::cout << t(g) << "\n";
g.reset();
g.add(x + x + y + z >= 0);
g.add(x*x - y*y >= 0);
std::cout << t(g) << "\n";
}
void tactic_qe() {
std::cout << "tactic example using quantifier elimination\n";
context c;
// Create a solver using "qe" and "smt" tactics
solver s =
(tactic(c, "qe") &
tactic(c, "smt")).mk_solver();
expr a = c.int_const("a");
expr b = c.int_const("b");
expr x = c.int_const("x");
expr f = implies(x <= a, x < b);
expr qf = forall(x, f);
std::cout << qf << "\n";
s.add(qf);
std::cout << s.check() << "\n";
std::cout << s.get_model() << "\n";
}
void visit(std::vector<bool>& visited, expr const & e) {
if (visited.size() <= e.id()) {
visited.resize(e.id()+1, false);
}
if (visited[e.id()]) {
return;
}
visited[e.id()] = true;
if (e.is_app()) {
unsigned num = e.num_args();
for (unsigned i = 0; i < num; i++) {
visit(visited, e.arg(i));
}
// do something
// Example: print the visited expression
func_decl f = e.decl();
std::cout << "application of " << f.name() << ": " << e << "\n";
}
else if (e.is_quantifier()) {
visit(visited, e.body());
// do something
}
else {
assert(e.is_var());
// do something
}
}
void tst_visit() {
std::cout << "visit example\n";
context c;
// only one of the occurrences of x*x is visited
// because they are the same subterms
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
expr f = x*x + x*x - y*y >= 0;
std::vector<bool> visited;
visit(visited, f);
}
void tst_numeral() {
std::cout << "numeral example\n";
context c;
expr x = c.real_val("1/3");
double d = 0;
if (!x.is_numeral(d)) {
std::cout << x << " is not recognized as a numeral\n";
return;
}
std::cout << x << " is " << d << "\n";
}
void incremental_example1() {
std::cout << "incremental example1\n";
context c;
expr x = c.int_const("x");
solver s(c);
s.add(x > 0);
std::cout << s.check() << "\n";
// We can add more formulas to the solver
s.add(x < 0);
// and, invoke s.check() again...
std::cout << s.check() << "\n";
}
void incremental_example2() {
// In this example, we show how push() and pop() can be used
// to remove formulas added to the solver.
std::cout << "incremental example2\n";
context c;
expr x = c.int_const("x");
solver s(c);
s.add(x > 0);
std::cout << s.check() << "\n";
// push() creates a backtracking point (aka a snapshot).
s.push();
// We can add more formulas to the solver
s.add(x < 0);
// and, invoke s.check() again...
std::cout << s.check() << "\n";
// pop() will remove all formulas added between this pop() and the matching push()
s.pop();
// The context is satisfiable again
std::cout << s.check() << "\n";
// and contains only x > 0
std::cout << s << "\n";
}
void incremental_example3() {
// In this example, we show how to use assumptions to "remove"
// formulas added to a solver. Actually, we disable them.
std::cout << "incremental example3\n";
context c;
expr x = c.int_const("x");
solver s(c);
s.add(x > 0);
std::cout << s.check() << "\n";
// Now, suppose we want to add x < 0 to the solver, but we also want
// to be able to disable it later.
// To do that, we create an auxiliary Boolean variable
expr b = c.bool_const("b");
// and, assert (b implies x < 0)
s.add(implies(b, x < 0));
// Now, we check whether s is satisfiable under the assumption "b" is true.
expr_vector a1(c);
a1.push_back(b);
std::cout << s.check(a1) << "\n";
// To "disable" (x > 0), we may just ask with the assumption "not b" or not provide any assumption.
std::cout << s.check() << "\n";
expr_vector a2(c);
a2.push_back(!b);
std::cout << s.check(a2) << "\n";
}
void enum_sort_example() {
std::cout << "enumeration sort example\n";
context ctx;
const char * enum_names[] = { "a", "b", "c" };
func_decl_vector enum_consts(ctx);
func_decl_vector enum_testers(ctx);
sort s = ctx.enumeration_sort("enumT", 3, enum_names, enum_consts, enum_testers);
// enum_consts[0] is a func_decl of arity 0.
// we convert it to an expression using the operator()
expr a = enum_consts[0]();
expr b = enum_consts[1]();
expr x = ctx.constant("x", s);
expr test = (x==a) && (x==b);
std::cout << "1: " << test << std::endl;
tactic qe(ctx, "ctx-solver-simplify");
goal g(ctx);
g.add(test);
expr res(ctx);
apply_result result_of_elimination = qe.apply(g);
goal result_goal = result_of_elimination[0];
std::cout << "2: " << result_goal.as_expr() << std::endl;
}
void tuple_example() {
std::cout << "tuple example\n";
context ctx;
const char * names[] = { "first", "second" };
sort sorts[2] = { ctx.int_sort(), ctx.bool_sort() };
func_decl_vector projs(ctx);
func_decl pair = ctx.tuple_sort("pair", 2, names, sorts, projs);
sorts[1] = pair.range();
func_decl pair2 = ctx.tuple_sort("pair2", 2, names, sorts, projs);
std::cout << pair2 << "\n";
}
void expr_vector_example() {
std::cout << "expr_vector example\n";
context c;
const unsigned N = 10;
expr_vector x(c);
for (unsigned i = 0; i < N; i++) {
std::stringstream x_name;
x_name << "x_" << i;
x.push_back(c.int_const(x_name.str().c_str()));
}
solver s(c);
for (unsigned i = 0; i < N; i++) {
s.add(x[i] >= 1);
}
std::cout << s << "\n" << "solving...\n" << s.check() << "\n";
model m = s.get_model();
std::cout << "solution\n" << m;
}
void exists_expr_vector_example() {
std::cout << "exists expr_vector example\n";
context c;
const unsigned N = 10;
expr_vector xs(c);
expr x(c);
expr b(c);
b = c.bool_val(true);
for (unsigned i = 0; i < N; i++) {
std::stringstream x_name;
x_name << "x_" << i;
x = c.int_const(x_name.str().c_str());
xs.push_back(x);
b = b && x >= 0;
}
expr ex(c);
ex = exists(xs, b);