forked from Z3Prover/z3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_capi.c
2995 lines (2556 loc) · 90.5 KB
/
test_capi.c
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<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#include<memory.h>
#include<setjmp.h>
#include<z3.h>
#define LOG_Z3_CALLS
#ifdef LOG_Z3_CALLS
#define LOG_MSG(msg) Z3_append_log(msg)
#else
#define LOG_MSG(msg) ((void)0)
#endif
/**
\defgroup capi_ex C API examples
*/
/*@{*/
/**
@name Auxiliary Functions
*/
/*@{*/
/**
\brief exit gracefully in case of error.
*/
void exitf(const char* message)
{
fprintf(stderr,"BUG: %s.\n", message);
exit(1);
}
/**
\brief exit if unreachable code was reached.
*/
void unreachable()
{
exitf("unreachable code was reached");
}
/**
\brief Simpler error handler.
*/
void error_handler(Z3_context c, Z3_error_code e)
{
printf("Error code: %d\n", e);
exitf("incorrect use of Z3");
}
static jmp_buf g_catch_buffer;
/**
\brief Low tech exceptions.
In high-level programming languages, an error handler can throw an exception.
*/
void throw_z3_error(Z3_context c, Z3_error_code e)
{
longjmp(g_catch_buffer, e);
}
/**
\brief Error handling that depends on checking an error code on the context.
*/
void nothrow_z3_error(Z3_context c, Z3_error_code e) {
// no-op
}
/**
\brief Create a logical context.
Enable model construction. Other configuration parameters can be passed in the cfg variable.
Also enable tracing to stderr and register custom error handler.
*/
Z3_context mk_context_custom(Z3_config cfg, Z3_error_handler err)
{
Z3_context ctx;
Z3_set_param_value(cfg, "model", "true");
ctx = Z3_mk_context(cfg);
Z3_set_error_handler(ctx, err);
return ctx;
}
Z3_solver mk_solver(Z3_context ctx)
{
Z3_solver s = Z3_mk_solver(ctx);
Z3_solver_inc_ref(ctx, s);
return s;
}
void del_solver(Z3_context ctx, Z3_solver s)
{
Z3_solver_dec_ref(ctx, s);
}
/**
\brief Create a logical context.
Enable model construction only.
Also enable tracing to stderr and register standard error handler.
*/
Z3_context mk_context()
{
Z3_config cfg;
Z3_context ctx;
cfg = Z3_mk_config();
ctx = mk_context_custom(cfg, error_handler);
Z3_del_config(cfg);
return ctx;
}
/**
\brief Create a logical context.
Enable fine-grained proof construction.
Enable model construction.
Also enable tracing to stderr and register standard error handler.
*/
Z3_context mk_proof_context() {
Z3_config cfg = Z3_mk_config();
Z3_context ctx;
Z3_set_param_value(cfg, "proof", "true");
ctx = mk_context_custom(cfg, throw_z3_error);
Z3_del_config(cfg);
return ctx;
}
/**
\brief Create a variable using the given name and type.
*/
Z3_ast mk_var(Z3_context ctx, const char * name, Z3_sort ty)
{
Z3_symbol s = Z3_mk_string_symbol(ctx, name);
return Z3_mk_const(ctx, s, ty);
}
/**
\brief Create a boolean variable using the given name.
*/
Z3_ast mk_bool_var(Z3_context ctx, const char * name)
{
Z3_sort ty = Z3_mk_bool_sort(ctx);
return mk_var(ctx, name, ty);
}
/**
\brief Create an integer variable using the given name.
*/
Z3_ast mk_int_var(Z3_context ctx, const char * name)
{
Z3_sort ty = Z3_mk_int_sort(ctx);
return mk_var(ctx, name, ty);
}
/**
\brief Create a Z3 integer node using a C int.
*/
Z3_ast mk_int(Z3_context ctx, int v)
{
Z3_sort ty = Z3_mk_int_sort(ctx);
return Z3_mk_int(ctx, v, ty);
}
/**
\brief Create a real variable using the given name.
*/
Z3_ast mk_real_var(Z3_context ctx, const char * name)
{
Z3_sort ty = Z3_mk_real_sort(ctx);
return mk_var(ctx, name, ty);
}
/**
\brief Create the unary function application: <tt>(f x)</tt>.
*/
Z3_ast mk_unary_app(Z3_context ctx, Z3_func_decl f, Z3_ast x)
{
Z3_ast args[1] = {x};
return Z3_mk_app(ctx, f, 1, args);
}
/**
\brief Create the binary function application: <tt>(f x y)</tt>.
*/
Z3_ast mk_binary_app(Z3_context ctx, Z3_func_decl f, Z3_ast x, Z3_ast y)
{
Z3_ast args[2] = {x, y};
return Z3_mk_app(ctx, f, 2, args);
}
/**
\brief Check whether the logical context is satisfiable, and compare the result with the expected result.
If the context is satisfiable, then display the model.
*/
void check(Z3_context ctx, Z3_solver s, Z3_lbool expected_result)
{
Z3_model m = 0;
Z3_lbool result = Z3_solver_check(ctx, s);
switch (result) {
case Z3_L_FALSE:
printf("unsat\n");
break;
case Z3_L_UNDEF:
printf("unknown\n");
m = Z3_solver_get_model(ctx, s);
if (m) Z3_model_inc_ref(ctx, m);
printf("potential model:\n%s\n", Z3_model_to_string(ctx, m));
break;
case Z3_L_TRUE:
m = Z3_solver_get_model(ctx, s);
if (m) Z3_model_inc_ref(ctx, m);
printf("sat\n%s\n", Z3_model_to_string(ctx, m));
break;
}
if (result != expected_result) {
exitf("unexpected result");
}
if (m) Z3_model_dec_ref(ctx, m);
}
/**
\brief Prove that the constraints already asserted into the logical
context implies the given formula. The result of the proof is
displayed.
Z3 is a satisfiability checker. So, one can prove \c f by showing
that <tt>(not f)</tt> is unsatisfiable.
The context \c ctx is not modified by this function.
*/
void prove(Z3_context ctx, Z3_solver s, Z3_ast f, bool is_valid)
{
Z3_model m = 0;
Z3_ast not_f;
/* save the current state of the context */
Z3_solver_push(ctx, s);
not_f = Z3_mk_not(ctx, f);
Z3_solver_assert(ctx, s, not_f);
switch (Z3_solver_check(ctx, s)) {
case Z3_L_FALSE:
/* proved */
printf("valid\n");
if (!is_valid) {
exitf("unexpected result");
}
break;
case Z3_L_UNDEF:
/* Z3 failed to prove/disprove f. */
printf("unknown\n");
m = Z3_solver_get_model(ctx, s);
if (m != 0) {
Z3_model_inc_ref(ctx, m);
/* m should be viewed as a potential counterexample. */
printf("potential counterexample:\n%s\n", Z3_model_to_string(ctx, m));
}
if (is_valid) {
exitf("unexpected result");
}
break;
case Z3_L_TRUE:
/* disproved */
printf("invalid\n");
m = Z3_solver_get_model(ctx, s);
if (m) {
Z3_model_inc_ref(ctx, m);
/* the model returned by Z3 is a counterexample */
printf("counterexample:\n%s\n", Z3_model_to_string(ctx, m));
}
if (is_valid) {
exitf("unexpected result");
}
break;
}
if (m) Z3_model_dec_ref(ctx, m);
/* restore scope */
Z3_solver_pop(ctx, s, 1);
}
/**
\brief Assert the axiom: function f is injective in the i-th argument.
The following axiom is asserted into the logical context:
\code
forall (x_0, ..., x_n) finv(f(x_0, ..., x_i, ..., x_{n-1})) = x_i
\endcode
Where, \c finv is a fresh function declaration.
*/
void assert_inj_axiom(Z3_context ctx, Z3_solver s, Z3_func_decl f, unsigned i)
{
unsigned sz, j;
Z3_sort finv_domain, finv_range;
Z3_func_decl finv;
Z3_sort * types; /* types of the quantified variables */
Z3_symbol * names; /* names of the quantified variables */
Z3_ast * xs; /* arguments for the application f(x_0, ..., x_i, ..., x_{n-1}) */
Z3_ast x_i, fxs, finv_fxs, eq;
Z3_pattern p;
Z3_ast q;
sz = Z3_get_domain_size(ctx, f);
if (i >= sz) {
exitf("failed to create inj axiom");
}
/* declare the i-th inverse of f: finv */
finv_domain = Z3_get_range(ctx, f);
finv_range = Z3_get_domain(ctx, f, i);
finv = Z3_mk_fresh_func_decl(ctx, "inv", 1, &finv_domain, finv_range);
/* allocate temporary arrays */
types = (Z3_sort *) malloc(sizeof(Z3_sort) * sz);
names = (Z3_symbol *) malloc(sizeof(Z3_symbol) * sz);
xs = (Z3_ast *) malloc(sizeof(Z3_ast) * sz);
/* fill types, names and xs */
for (j = 0; j < sz; j++) { types[j] = Z3_get_domain(ctx, f, j); };
for (j = 0; j < sz; j++) { names[j] = Z3_mk_int_symbol(ctx, j); };
for (j = 0; j < sz; j++) { xs[j] = Z3_mk_bound(ctx, j, types[j]); };
x_i = xs[i];
/* create f(x_0, ..., x_i, ..., x_{n-1}) */
fxs = Z3_mk_app(ctx, f, sz, xs);
/* create f_inv(f(x_0, ..., x_i, ..., x_{n-1})) */
finv_fxs = mk_unary_app(ctx, finv, fxs);
/* create finv(f(x_0, ..., x_i, ..., x_{n-1})) = x_i */
eq = Z3_mk_eq(ctx, finv_fxs, x_i);
/* use f(x_0, ..., x_i, ..., x_{n-1}) as the pattern for the quantifier */
p = Z3_mk_pattern(ctx, 1, &fxs);
printf("pattern: %s\n", Z3_pattern_to_string(ctx, p));
printf("\n");
/* create & assert quantifier */
q = Z3_mk_forall(ctx,
0, /* using default weight */
1, /* number of patterns */
&p, /* address of the "array" of patterns */
sz, /* number of quantified variables */
types,
names,
eq);
printf("assert axiom:\n%s\n", Z3_ast_to_string(ctx, q));
Z3_solver_assert(ctx, s, q);
/* free temporary arrays */
free(types);
free(names);
free(xs);
}
/**
\brief Assert the axiom: function f is commutative.
This example uses the SMT-LIB parser to simplify the axiom construction.
*/
void assert_comm_axiom(Z3_context ctx, Z3_solver s, Z3_func_decl f)
{
Z3_sort t;
Z3_symbol f_name, t_name;
Z3_ast_vector q;
unsigned i;
t = Z3_get_range(ctx, f);
if (Z3_get_domain_size(ctx, f) != 2 ||
Z3_get_domain(ctx, f, 0) != t ||
Z3_get_domain(ctx, f, 1) != t) {
exitf("function must be binary, and argument types must be equal to return type");
}
/* Inside the parser, function f will be referenced using the symbol 'f'. */
f_name = Z3_mk_string_symbol(ctx, "f");
/* Inside the parser, type t will be referenced using the symbol 'T'. */
t_name = Z3_mk_string_symbol(ctx, "T");
q = Z3_parse_smtlib2_string(ctx,
"(assert (forall ((x T) (y T)) (= (f x y) (f y x))))",
1, &t_name, &t,
1, &f_name, &f);
printf("assert axiom:\n%s\n", Z3_ast_vector_to_string(ctx, q));
for (i = 0; i < Z3_ast_vector_size(ctx, q); ++i) {
Z3_solver_assert(ctx, s, Z3_ast_vector_get(ctx, q, i));
}
}
/**
\brief Z3 does not support explicitly tuple updates. They can be easily implemented
as macros. The argument \c t must have tuple type.
A tuple update is a new tuple where field \c i has value \c new_val, and all
other fields have the value of the respective field of \c t.
<tt>update(t, i, new_val)</tt> is equivalent to
<tt>mk_tuple(proj_0(t), ..., new_val, ..., proj_n(t))</tt>
*/
Z3_ast mk_tuple_update(Z3_context c, Z3_ast t, unsigned i, Z3_ast new_val)
{
Z3_sort ty;
Z3_func_decl mk_tuple_decl;
unsigned num_fields, j;
Z3_ast * new_fields;
Z3_ast result;
ty = Z3_get_sort(c, t);
if (Z3_get_sort_kind(c, ty) != Z3_DATATYPE_SORT) {
exitf("argument must be a tuple");
}
num_fields = Z3_get_tuple_sort_num_fields(c, ty);
if (i >= num_fields) {
exitf("invalid tuple update, index is too big");
}
new_fields = (Z3_ast*) malloc(sizeof(Z3_ast) * num_fields);
for (j = 0; j < num_fields; j++) {
if (i == j) {
/* use new_val at position i */
new_fields[j] = new_val;
}
else {
/* use field j of t */
Z3_func_decl proj_decl = Z3_get_tuple_sort_field_decl(c, ty, j);
new_fields[j] = mk_unary_app(c, proj_decl, t);
}
}
mk_tuple_decl = Z3_get_tuple_sort_mk_decl(c, ty);
result = Z3_mk_app(c, mk_tuple_decl, num_fields, new_fields);
free(new_fields);
return result;
}
/**
\brief Display a symbol in the given output stream.
*/
void display_symbol(Z3_context c, FILE * out, Z3_symbol s)
{
switch (Z3_get_symbol_kind(c, s)) {
case Z3_INT_SYMBOL:
fprintf(out, "#%d", Z3_get_symbol_int(c, s));
break;
case Z3_STRING_SYMBOL:
fprintf(out, "%s", Z3_get_symbol_string(c, s));
break;
default:
unreachable();
}
}
/**
\brief Display the given type.
*/
void display_sort(Z3_context c, FILE * out, Z3_sort ty)
{
switch (Z3_get_sort_kind(c, ty)) {
case Z3_UNINTERPRETED_SORT:
display_symbol(c, out, Z3_get_sort_name(c, ty));
break;
case Z3_BOOL_SORT:
fprintf(out, "bool");
break;
case Z3_INT_SORT:
fprintf(out, "int");
break;
case Z3_REAL_SORT:
fprintf(out, "real");
break;
case Z3_BV_SORT:
fprintf(out, "bv%d", Z3_get_bv_sort_size(c, ty));
break;
case Z3_ARRAY_SORT:
fprintf(out, "[");
display_sort(c, out, Z3_get_array_sort_domain(c, ty));
fprintf(out, "->");
display_sort(c, out, Z3_get_array_sort_range(c, ty));
fprintf(out, "]");
break;
case Z3_DATATYPE_SORT:
if (Z3_get_datatype_sort_num_constructors(c, ty) != 1)
{
fprintf(out, "%s", Z3_sort_to_string(c,ty));
break;
}
{
unsigned num_fields = Z3_get_tuple_sort_num_fields(c, ty);
unsigned i;
fprintf(out, "(");
for (i = 0; i < num_fields; i++) {
Z3_func_decl field = Z3_get_tuple_sort_field_decl(c, ty, i);
if (i > 0) {
fprintf(out, ", ");
}
display_sort(c, out, Z3_get_range(c, field));
}
fprintf(out, ")");
break;
}
default:
fprintf(out, "unknown[");
display_symbol(c, out, Z3_get_sort_name(c, ty));
fprintf(out, "]");
break;
}
}
/**
\brief Custom ast pretty printer.
This function demonstrates how to use the API to navigate terms.
*/
void display_ast(Z3_context c, FILE * out, Z3_ast v)
{
switch (Z3_get_ast_kind(c, v)) {
case Z3_NUMERAL_AST: {
Z3_sort t;
fprintf(out, "%s", Z3_get_numeral_string(c, v));
t = Z3_get_sort(c, v);
fprintf(out, ":");
display_sort(c, out, t);
break;
}
case Z3_APP_AST: {
unsigned i;
Z3_app app = Z3_to_app(c, v);
unsigned num_fields = Z3_get_app_num_args(c, app);
Z3_func_decl d = Z3_get_app_decl(c, app);
fprintf(out, "%s", Z3_func_decl_to_string(c, d));
if (num_fields > 0) {
fprintf(out, "[");
for (i = 0; i < num_fields; i++) {
if (i > 0) {
fprintf(out, ", ");
}
display_ast(c, out, Z3_get_app_arg(c, app, i));
}
fprintf(out, "]");
}
break;
}
case Z3_QUANTIFIER_AST: {
fprintf(out, "quantifier");
;
}
default:
fprintf(out, "#unknown");
}
}
/**
\brief Custom function interpretations pretty printer.
*/
void display_function_interpretations(Z3_context c, FILE * out, Z3_model m)
{
unsigned num_functions, i;
fprintf(out, "function interpretations:\n");
num_functions = Z3_model_get_num_funcs(c, m);
for (i = 0; i < num_functions; i++) {
Z3_func_decl fdecl;
Z3_symbol name;
Z3_ast func_else;
unsigned num_entries = 0, j;
Z3_func_interp_opt finterp;
fdecl = Z3_model_get_func_decl(c, m, i);
finterp = Z3_model_get_func_interp(c, m, fdecl);
Z3_func_interp_inc_ref(c, finterp);
name = Z3_get_decl_name(c, fdecl);
display_symbol(c, out, name);
fprintf(out, " = {");
if (finterp)
num_entries = Z3_func_interp_get_num_entries(c, finterp);
for (j = 0; j < num_entries; j++) {
unsigned num_args, k;
Z3_func_entry fentry = Z3_func_interp_get_entry(c, finterp, j);
Z3_func_entry_inc_ref(c, fentry);
if (j > 0) {
fprintf(out, ", ");
}
num_args = Z3_func_entry_get_num_args(c, fentry);
fprintf(out, "(");
for (k = 0; k < num_args; k++) {
if (k > 0) {
fprintf(out, ", ");
}
display_ast(c, out, Z3_func_entry_get_arg(c, fentry, k));
}
fprintf(out, "|->");
display_ast(c, out, Z3_func_entry_get_value(c, fentry));
fprintf(out, ")");
Z3_func_entry_dec_ref(c, fentry);
}
if (num_entries > 0) {
fprintf(out, ", ");
}
fprintf(out, "(else|->");
func_else = Z3_func_interp_get_else(c, finterp);
display_ast(c, out, func_else);
fprintf(out, ")}\n");
Z3_func_interp_dec_ref(c, finterp);
}
}
/**
\brief Custom model pretty printer.
*/
void display_model(Z3_context c, FILE * out, Z3_model m)
{
unsigned num_constants;
unsigned i;
if (!m) return;
num_constants = Z3_model_get_num_consts(c, m);
for (i = 0; i < num_constants; i++) {
Z3_symbol name;
Z3_func_decl cnst = Z3_model_get_const_decl(c, m, i);
Z3_ast a, v;
bool ok;
name = Z3_get_decl_name(c, cnst);
display_symbol(c, out, name);
fprintf(out, " = ");
a = Z3_mk_app(c, cnst, 0, 0);
v = a;
ok = Z3_model_eval(c, m, a, 1, &v);
(void)ok;
display_ast(c, out, v);
fprintf(out, "\n");
}
display_function_interpretations(c, out, m);
}
/**
\brief Similar to #check, but uses #display_model instead of #Z3_model_to_string.
*/
void check2(Z3_context ctx, Z3_solver s, Z3_lbool expected_result)
{
Z3_model m = 0;
Z3_lbool result = Z3_solver_check(ctx, s);
switch (result) {
case Z3_L_FALSE:
printf("unsat\n");
break;
case Z3_L_UNDEF:
printf("unknown\n");
printf("potential model:\n");
m = Z3_solver_get_model(ctx, s);
if (m) Z3_model_inc_ref(ctx, m);
display_model(ctx, stdout, m);
break;
case Z3_L_TRUE:
printf("sat\n");
m = Z3_solver_get_model(ctx, s);
if (m) Z3_model_inc_ref(ctx, m);
display_model(ctx, stdout, m);
break;
}
if (result != expected_result) {
exitf("unexpected result");
}
if (m) Z3_model_dec_ref(ctx, m);
}
/**
\brief Display Z3 version in the standard output.
*/
void display_version()
{
unsigned major, minor, build, revision;
Z3_get_version(&major, &minor, &build, &revision);
printf("Z3 %d.%d.%d.%d\n", major, minor, build, revision);
}
/*@}*/
/**
@name Examples
*/
/*@{*/
/**
\brief "Hello world" example: create a Z3 logical context, and delete it.
*/
void simple_example()
{
Z3_context ctx;
LOG_MSG("simple_example");
printf("\nsimple_example\n");
ctx = mk_context();
/* delete logical context */
Z3_del_context(ctx);
}
/**
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()
{
Z3_config cfg;
Z3_context ctx;
Z3_solver s;
Z3_sort bool_sort;
Z3_symbol symbol_x, symbol_y;
Z3_ast x, y, not_x, not_y, x_and_y, ls, rs, conjecture, negated_conjecture;
Z3_ast args[2];
printf("\nDeMorgan\n");
LOG_MSG("DeMorgan");
cfg = Z3_mk_config();
ctx = Z3_mk_context(cfg);
Z3_del_config(cfg);
bool_sort = Z3_mk_bool_sort(ctx);
symbol_x = Z3_mk_int_symbol(ctx, 0);
symbol_y = Z3_mk_int_symbol(ctx, 1);
x = Z3_mk_const(ctx, symbol_x, bool_sort);
y = Z3_mk_const(ctx, symbol_y, bool_sort);
/* De Morgan - with a negation around */
/* !(!(x && y) <-> (!x || !y)) */
not_x = Z3_mk_not(ctx, x);
not_y = Z3_mk_not(ctx, y);
args[0] = x;
args[1] = y;
x_and_y = Z3_mk_and(ctx, 2, args);
ls = Z3_mk_not(ctx, x_and_y);
args[0] = not_x;
args[1] = not_y;
rs = Z3_mk_or(ctx, 2, args);
conjecture = Z3_mk_iff(ctx, ls, rs);
negated_conjecture = Z3_mk_not(ctx, conjecture);
s = mk_solver(ctx);
Z3_solver_assert(ctx, s, negated_conjecture);
switch (Z3_solver_check(ctx, s)) {
case Z3_L_FALSE:
/* The negated conjecture was unsatisfiable, hence the conjecture is valid */
printf("DeMorgan is valid\n");
break;
case Z3_L_UNDEF:
/* Check returned undef */
printf("Undef\n");
break;
case Z3_L_TRUE:
/* The negated conjecture was satisfiable, hence the conjecture is not valid */
printf("DeMorgan is not valid\n");
break;
}
del_solver(ctx, s);
Z3_del_context(ctx);
}
/**
\brief Find a model for <tt>x xor y</tt>.
*/
void find_model_example1()
{
Z3_context ctx;
Z3_ast x, y, x_xor_y;
Z3_solver s;
printf("\nfind_model_example1\n");
LOG_MSG("find_model_example1");
ctx = mk_context();
s = mk_solver(ctx);
x = mk_bool_var(ctx, "x");
y = mk_bool_var(ctx, "y");
x_xor_y = Z3_mk_xor(ctx, x, y);
Z3_solver_assert(ctx, s, x_xor_y);
printf("model for: x xor y\n");
check(ctx, s, Z3_L_TRUE);
del_solver(ctx, s);
Z3_del_context(ctx);
}
/**
\brief Find a model for <tt>x < y + 1, x > 2</tt>.
Then, assert <tt>not(x = y)</tt>, and find another model.
*/
void find_model_example2()
{
Z3_context ctx;
Z3_ast x, y, one, two, y_plus_one;
Z3_ast x_eq_y;
Z3_ast args[2];
Z3_ast c1, c2, c3;
Z3_solver s;
printf("\nfind_model_example2\n");
LOG_MSG("find_model_example2");
ctx = mk_context();
s = mk_solver(ctx);
x = mk_int_var(ctx, "x");
y = mk_int_var(ctx, "y");
one = mk_int(ctx, 1);
two = mk_int(ctx, 2);
args[0] = y;
args[1] = one;
y_plus_one = Z3_mk_add(ctx, 2, args);
c1 = Z3_mk_lt(ctx, x, y_plus_one);
c2 = Z3_mk_gt(ctx, x, two);
Z3_solver_assert(ctx, s, c1);
Z3_solver_assert(ctx, s, c2);
printf("model for: x < y + 1, x > 2\n");
check(ctx, s, Z3_L_TRUE);
/* assert not(x = y) */
x_eq_y = Z3_mk_eq(ctx, x, y);
c3 = Z3_mk_not(ctx, x_eq_y);
Z3_solver_assert(ctx, s,c3);
printf("model for: x < y + 1, x > 2, not(x = y)\n");
check(ctx, s, Z3_L_TRUE);
del_solver(ctx, s);
Z3_del_context(ctx);
}
/**
\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()
{
Z3_context ctx;
Z3_solver s;
Z3_symbol U_name, g_name, x_name, y_name;
Z3_sort U;
Z3_sort g_domain[1];
Z3_func_decl g;
Z3_ast x, y, gx, ggx, gy;
Z3_ast eq, f;
printf("\nprove_example1\n");
LOG_MSG("prove_example1");
ctx = mk_context();
s = mk_solver(ctx);
/* create uninterpreted type. */
U_name = Z3_mk_string_symbol(ctx, "U");
U = Z3_mk_uninterpreted_sort(ctx, U_name);
/* declare function g */
g_name = Z3_mk_string_symbol(ctx, "g");
g_domain[0] = U;
g = Z3_mk_func_decl(ctx, g_name, 1, g_domain, U);
/* create x and y */
x_name = Z3_mk_string_symbol(ctx, "x");
y_name = Z3_mk_string_symbol(ctx, "y");
x = Z3_mk_const(ctx, x_name, U);
y = Z3_mk_const(ctx, y_name, U);
/* create g(x), g(y) */
gx = mk_unary_app(ctx, g, x);
gy = mk_unary_app(ctx, g, y);
/* assert x = y */
eq = Z3_mk_eq(ctx, x, y);
Z3_solver_assert(ctx, s, eq);
/* prove g(x) = g(y) */
f = Z3_mk_eq(ctx, gx, gy);
printf("prove: x = y implies g(x) = g(y)\n");
prove(ctx, s, f, true);
/* create g(g(x)) */
ggx = mk_unary_app(ctx, g, gx);
/* disprove g(g(x)) = g(y) */
f = Z3_mk_eq(ctx, ggx, gy);
printf("disprove: x = y implies g(g(x)) = g(y)\n");
prove(ctx, s, f, false);
del_solver(ctx, s);
Z3_del_context(ctx);
}
/**
\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()
{
Z3_context ctx;
Z3_solver s;
Z3_sort int_sort;
Z3_symbol g_name;
Z3_sort g_domain[1];
Z3_func_decl g;
Z3_ast x, y, z, zero, minus_one, x_plus_z, gx, gy, gz, gx_gy, ggx_gy;
Z3_ast args[2];
Z3_ast eq, c1, c2, c3, f;
printf("\nprove_example2\n");
LOG_MSG("prove_example2");
ctx = mk_context();
s = mk_solver(ctx);
/* declare function g */
int_sort = Z3_mk_int_sort(ctx);
g_name = Z3_mk_string_symbol(ctx, "g");
g_domain[0] = int_sort;
g = Z3_mk_func_decl(ctx, g_name, 1, g_domain, int_sort);
/* create x, y, and z */
x = mk_int_var(ctx, "x");
y = mk_int_var(ctx, "y");
z = mk_int_var(ctx, "z");
/* create gx, gy, gz */
gx = mk_unary_app(ctx, g, x);
gy = mk_unary_app(ctx, g, y);
gz = mk_unary_app(ctx, g, z);
/* create zero */
zero = mk_int(ctx, 0);
/* assert not(g(g(x) - g(y)) = g(z)) */
args[0] = gx;
args[1] = gy;
gx_gy = Z3_mk_sub(ctx, 2, args);
ggx_gy = mk_unary_app(ctx, g, gx_gy);
eq = Z3_mk_eq(ctx, ggx_gy, gz);
c1 = Z3_mk_not(ctx, eq);
Z3_solver_assert(ctx, s, c1);
/* assert x + z <= y */
args[0] = x;
args[1] = z;
x_plus_z = Z3_mk_add(ctx, 2, args);
c2 = Z3_mk_le(ctx, x_plus_z, y);
Z3_solver_assert(ctx, s, c2);
/* assert y <= x */
c3 = Z3_mk_le(ctx, y, x);
Z3_solver_assert(ctx, s, c3);
/* prove z < 0 */
f = Z3_mk_lt(ctx, z, zero);
printf("prove: not(g(g(x) - g(y)) = g(z)), x + z <= y <= x implies z < 0\n");
prove(ctx, s, f, true);
/* disprove z < -1 */
minus_one = mk_int(ctx, -1);
f = Z3_mk_lt(ctx, z, minus_one);
printf("disprove: not(g(g(x) - g(y)) = g(z)), x + z <= y <= x implies z < -1\n");
prove(ctx, s, f, false);
del_solver(ctx, s);
Z3_del_context(ctx);
}
/**
\brief Show how push & pop can be used to create "backtracking"
points.
This example also demonstrates how big numbers can be created in Z3.
*/