-
Notifications
You must be signed in to change notification settings - Fork 105
/
lib.rs
1535 lines (1283 loc) · 46.3 KB
/
lib.rs
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
extern crate env_logger;
#[macro_use]
extern crate log;
extern crate z3;
use std::convert::TryInto;
use std::ops::Add;
use std::time::Duration;
use z3::ast::{Ast, Bool};
use z3::*;
#[cfg(feature = "arbitrary-size-numeral")]
extern crate num;
#[cfg(feature = "arbitrary-size-numeral")]
use num::bigint::BigInt;
#[cfg(feature = "arbitrary-size-numeral")]
use num::rational::BigRational;
#[cfg(feature = "arbitrary-size-numeral")]
use std::str::FromStr;
#[test]
fn test_config() {
let _ = env_logger::try_init();
let mut c = Config::new();
c.set_proof_generation(true);
}
#[test]
fn test_context() {
let _ = env_logger::try_init();
let mut cfg = Config::new();
cfg.set_proof_generation(true);
let _ = Context::new(&cfg);
}
#[test]
fn test_sorts_and_symbols() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let _ = ast::Int::new_const(&ctx, "x");
let _ = ast::Int::new_const(&ctx, "y");
}
#[test]
fn test_solving() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let x = ast::Int::new_const(&ctx, "x");
let y = ast::Int::new_const(&ctx, "y");
let solver = Solver::new(&ctx);
solver.assert(&x.gt(&y));
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_solving_for_model() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let x = ast::Int::new_const(&ctx, "x");
let y = ast::Int::new_const(&ctx, "y");
let zero = ast::Int::from_i64(&ctx, 0);
let two = ast::Int::from_i64(&ctx, 2);
let seven = ast::Int::from_i64(&ctx, 7);
let solver = Solver::new(&ctx);
solver.assert(&x.gt(&y));
solver.assert(&y.gt(&zero));
solver.assert(&y.rem(&seven)._eq(&two));
let x_plus_two = ast::Int::add(&ctx, &[&x, &two]);
solver.assert(&x_plus_two.gt(&seven));
assert_eq!(solver.check(), SatResult::Sat);
let model = solver.get_model().unwrap();
let xv = model.eval(&x, true).unwrap().as_i64().unwrap();
let yv = model.eval(&y, true).unwrap().as_i64().unwrap();
info!("x: {}", xv);
info!("y: {}", yv);
assert!(xv > yv);
assert!(yv % 7 == 2);
assert!(xv + 2 > 7);
}
#[test]
fn test_cloning_ast() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let x = ast::Int::new_const(&ctx, "x");
let y = x.clone();
let zero = ast::Int::from_i64(&ctx, 0);
let solver = Solver::new(&ctx);
solver.assert(&x._eq(&zero));
assert_eq!(solver.check(), SatResult::Sat);
let model = solver.get_model().unwrap();
let xv = model.eval(&x, true).unwrap().as_i64().unwrap();
let yv = model.eval(&y, true).unwrap().as_i64().unwrap();
assert_eq!(xv, 0);
assert_eq!(yv, 0);
}
#[test]
fn test_format() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let ast = ast::Int::new_const(&ctx, "x");
assert_eq!("x", format!("{}", ast));
let int = Sort::int(&ctx);
assert_eq!("Int", format!("{}", int));
}
#[test]
fn test_bitvectors() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let a = ast::BV::new_const(&ctx, "a", 64);
let b = ast::BV::new_const(&ctx, "b", 64);
let two = ast::BV::from_i64(&ctx, 2, 64);
let solver = Solver::new(&ctx);
solver.assert(&a.bvsgt(&b));
solver.assert(&b.bvsgt(&two));
let b_plus_two = b.bvadd(&two);
solver.assert(&b_plus_two.bvsgt(&a));
assert_eq!(solver.check(), SatResult::Sat);
let model = solver.get_model().unwrap();
let av = model.eval(&a, true).unwrap().as_i64().unwrap();
let bv = model.eval(&b, true).unwrap().as_i64().unwrap();
assert!(av > bv);
assert!(bv > 2);
assert!(bv + 2 > av);
}
#[test]
fn test_bitvector_from_str() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let a = ast::BV::new_const(&ctx, "a", 129);
// 2 ** 128
let b = ast::BV::from_str(&ctx, 129, "340282366920938463463374607431768211456").unwrap();
let solver = Solver::new(&ctx);
solver.assert(&a._eq(&b));
assert_eq!(solver.check(), SatResult::Sat);
let model = solver.get_model().unwrap();
let av = model.eval(&a, true).unwrap().to_string();
assert_eq!(av, "#b100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".to_string());
}
#[test]
fn test_floating_point_bits() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let float32 = ast::Float::new_const_float32(&ctx, "float32");
let float64 = ast::Float::new_const_double(&ctx, "float64");
let float128 = ast::Float::new_const(&ctx, "float128", 15, 113);
let i = ast::Int::new_const(&ctx, "int");
let exp32 = Sort::float_exponent_size(&float32.get_sort());
let sig32 = Sort::float_significand_size(&float32.get_sort());
let exp64 = Sort::float_exponent_size(&float64.get_sort());
let sig64 = Sort::float_significand_size(&float64.get_sort());
let exp128 = Sort::float_exponent_size(&float128.get_sort());
let sig128 = Sort::float_significand_size(&float128.get_sort());
let expi = Sort::float_exponent_size(&i.get_sort());
let sigi = Sort::float_significand_size(&i.get_sort());
assert!(exp32 == Some(8));
assert!(sig32 == Some(24));
assert!(exp64 == Some(11));
assert!(sig64 == Some(53));
assert!(exp128 == Some(15));
assert!(sig128 == Some(113));
assert!(expi == None);
assert!(sigi == None);
}
#[test]
fn test_ast_translate() {
let cfg = Config::new();
let source = Context::new(&cfg);
let a = ast::Int::new_const(&source, "a");
let destination = Context::new(&cfg);
let translated_a = a.translate(&destination);
let slv = Solver::new(&destination);
slv.assert(&translated_a._eq(&ast::Int::from_u64(&destination, 2)));
assert_eq!(slv.check(), SatResult::Sat);
slv.assert(&translated_a._eq(&ast::Int::from_u64(&destination, 3)));
assert_eq!(slv.check(), SatResult::Unsat);
}
#[test]
fn test_solver_translate() {
let cfg = Config::new();
let source = Context::new(&cfg);
let a = ast::Int::new_const(&source, "a");
let destination = Context::new(&cfg);
let translated_a = a.translate(&destination);
let slv = Solver::new(&destination);
slv.assert(&translated_a._eq(&ast::Int::from_u64(&destination, 2)));
assert_eq!(slv.check(), SatResult::Sat);
let translated_slv = slv.translate(&source);
// Add a new constraint, make the old one unsatisfiable, while the copy remains satisfiable.
slv.assert(&translated_a._eq(&ast::Int::from_u64(&destination, 3)));
assert_eq!(slv.check(), SatResult::Unsat);
assert_eq!(translated_slv.check(), SatResult::Sat);
}
#[test]
fn test_model_translate() {
let cfg = Config::new();
let source = Context::new(&cfg);
let a = ast::Int::new_const(&source, "a");
let destination = Context::new(&cfg);
let translated_a = a.translate(&destination);
let slv = Solver::new(&source);
slv.assert(&a._eq(&ast::Int::from_u64(&source, 2)));
assert_eq!(slv.check(), SatResult::Sat);
let model = slv.get_model().unwrap();
assert_eq!(2, model.eval(&a, true).unwrap().as_i64().unwrap());
let translated_model = model.translate(&destination);
assert_eq!(
2,
translated_model
.eval(&translated_a, true)
.unwrap()
.as_i64()
.unwrap()
);
}
#[test]
fn test_pb_ops_model() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let x = ast::Bool::new_const(&ctx, "x");
let y = ast::Bool::new_const(&ctx, "y");
let solver = Solver::new(&ctx);
solver.push();
solver.assert(&ast::Bool::pb_eq(&ctx, &[(&x, 1), (&y, 1)], 1));
assert_eq!(solver.check(), SatResult::Sat);
let model = solver.get_model().unwrap();
let xv = model.eval(&x, true).unwrap().as_bool().unwrap();
let yv = model.eval(&y, true).unwrap().as_bool().unwrap();
info!("x: {}", xv);
info!("y: {}", yv);
assert!((xv && !yv) || (!xv && yv));
solver.pop(1);
solver.push();
solver.assert(&ast::Bool::pb_ge(&ctx, &[(&x, 1), (&y, 1)], 2));
assert_eq!(solver.check(), SatResult::Sat);
let model = solver.get_model().unwrap();
let xv = model.eval(&x, true).unwrap().as_bool().unwrap();
let yv = model.eval(&y, true).unwrap().as_bool().unwrap();
info!("x: {}", xv);
info!("y: {}", yv);
assert!(xv && yv);
solver.pop(1);
solver.assert(&ast::Bool::pb_le(&ctx, &[(&x, 1), (&y, 1)], 0));
assert_eq!(solver.check(), SatResult::Sat);
let model = solver.get_model().unwrap();
let xv = model.eval(&x, true).unwrap().as_bool().unwrap();
let yv = model.eval(&y, true).unwrap().as_bool().unwrap();
info!("x: {}", xv);
info!("y: {}", yv);
assert!(!xv && !yv);
}
#[test]
fn function_ref_count() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let int_sort = Sort::int(&ctx);
let _f = FuncDecl::new(&ctx, "f", &[&int_sort], &int_sort);
let _g = FuncDecl::new(&ctx, "g", &[&int_sort], &int_sort);
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_params() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let x = ast::Int::new_const(&ctx, "x");
let y = ast::Int::new_const(&ctx, "y");
let mut params = Params::new(&ctx);
params.set_bool("smt.mbqi", false);
params.set_f64("smt.qi.eager_threshold", 5.0);
params.set_u32("smt.qi.max_instances", 999);
let solver = Solver::new(&ctx);
solver.set_params(¶ms);
solver.assert(&x.gt(&y));
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_substitution() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let x = ast::Real::new_const(&ctx, "x");
let y = ast::Real::new_const(&ctx, "y");
let z = ast::Real::new_const(&ctx, "z");
let x_plus_y = ast::Real::add(&ctx, &[&x, &y]);
let x_plus_z = ast::Real::add(&ctx, &[&x, &z]);
let substitutions = &[(&y, &z)];
assert!(x_plus_y.substitute(substitutions) == x_plus_z);
}
#[test]
fn test_real_cmp() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::Real::new_const(&ctx, "x");
let x_plus_1 = ast::Real::add(&ctx, &[&x, &ast::Real::from_real(&ctx, 1, 1)]);
// forall x, x < x + 1
let forall = ast::forall_const(&ctx, &[&x], &[], &x.lt(&x_plus_1));
solver.assert(&forall);
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_float_add() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::Float::new_const_float32(&ctx, "x");
let x_plus_one = ast::Float::round_towards_zero(&ctx).add(&x, &ast::Float::from_f32(&ctx, 1.0));
let y = ast::Float::from_f32(&ctx, 3.14);
solver.assert(&x_plus_one._eq(&y));
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_arbitrary_size_real() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::Real::from_real_str(&ctx, "99999999999999999999998", "99999999999999999999999")
.unwrap();
let y = ast::Real::from_real(&ctx, 1, 1);
solver.assert(&x.lt(&y));
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_arbitrary_size_int() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::Int::from_str(&ctx, "99999999999999999999998").unwrap();
let one = ast::Int::from_i64(&ctx, 1);
let y = ast::Int::from_str(&ctx, "99999999999999999999999").unwrap();
solver.assert(&ast::Int::add(&ctx, &[&x, &one])._eq(&y));
assert_eq!(solver.check(), SatResult::Sat);
}
#[cfg(feature = "arbitrary-size-numeral")]
#[test]
fn test_arbitrary_size_real_from_bigrational() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::Real::from_real_str(&ctx, "99999999999999999999998", "99999999999999999999999")
.unwrap();
let num = BigInt::from_str("99999999999999999999998").unwrap();
let den = BigInt::from_str("99999999999999999999999").unwrap();
let ratio = BigRational::new(num, den);
let y = ast::Real::from_big_rational(&ctx, &ratio);
solver.assert(&x._eq(&y));
assert_eq!(solver.check(), SatResult::Sat);
}
#[cfg(feature = "arbitrary-size-numeral")]
#[test]
fn test_arbitrary_size_int_from_bigint() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let num1 = BigInt::from_str("99999999999999999999998").unwrap();
let x = ast::Int::from_big_int(&ctx, &num1);
let y = ast::Int::from_i64(&ctx, 1);
let num2 = BigInt::from_str("99999999999999999999999").unwrap();
let z = ast::Int::from_big_int(&ctx, &num2);
solver.assert(&ast::Int::add(&ctx, &[&x, &y])._eq(&z));
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_string_eq() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::String::from_str(&ctx, "foo").unwrap();
let y = ast::String::from_str(&ctx, "foo").unwrap();
let z = ast::String::from_str(&ctx, "bar").unwrap();
let h = ast::String::new_const(&ctx, "h");
solver.assert(&x._eq(&y));
solver.assert(&x._eq(&z).not());
solver.assert(&h._eq(&x));
assert_eq!(solver.check(), SatResult::Sat);
solver.assert(&h._eq(&z));
assert_eq!(solver.check(), SatResult::Unsat)
}
#[test]
fn test_string_concat() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::String::from_str(&ctx, "foo").unwrap();
let y = ast::String::from_str(&ctx, "bar").unwrap();
let z = ast::String::from_str(&ctx, "foobar").unwrap();
solver.assert(&ast::String::concat(&ctx, &[&x, &y])._eq(&z));
assert_eq!(solver.check(), SatResult::Sat)
}
#[test]
fn test_string_prefix() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::String::from_str(&ctx, "foo").unwrap();
let y = ast::String::from_str(&ctx, "foobar").unwrap();
solver.assert(&x.prefix(&y));
assert_eq!(solver.check(), SatResult::Sat)
}
#[test]
fn test_string_suffix() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let x = ast::String::from_str(&ctx, "bar").unwrap();
let y = ast::String::from_str(&ctx, "foobar").unwrap();
solver.assert(&x.suffix(&y));
assert_eq!(solver.check(), SatResult::Sat)
}
fn assert_string_roundtrip(source: &str) {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let expr = ast::String::from_str(&ctx, source).unwrap();
assert_eq!(&expr.as_string().unwrap(), source);
}
#[test]
fn test_string_as_string() {
assert_string_roundtrip("x");
assert_string_roundtrip("'x'");
assert_string_roundtrip(r#""x""#);
assert_string_roundtrip(r#"\\"x\\""#);
}
#[test]
fn test_rec_func_def() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let fac = RecFuncDecl::new(&ctx, "fac", &[&Sort::int(&ctx)], &Sort::int(&ctx));
let n = ast::Int::new_const(&ctx, "n");
let n_minus_1 = ast::Int::sub(&ctx, &[&n, &ast::Int::from_i64(&ctx, 1)]);
let fac_of_n_minus_1 = fac.apply(&[&n_minus_1.into()]);
let cond: ast::Bool = n.le(&ast::Int::from_i64(&ctx, 0));
let body = cond.ite(
&ast::Int::from_i64(&ctx, 1),
&ast::Int::mul(&ctx, &[&n, &fac_of_n_minus_1.as_int().unwrap()]),
);
fac.add_def(&[&n.into()], &body);
let x = ast::Int::new_const(&ctx, "x");
let y = ast::Int::new_const(&ctx, "y");
let solver = Solver::new(&ctx);
solver.assert(
&x._eq(
&fac.apply(&[&ast::Int::from_i64(&ctx, 4).into()])
.as_int()
.unwrap(),
),
);
solver.assert(&y._eq(&ast::Int::mul(&ctx, &[&ast::Int::from_i64(&ctx, 5), &x])));
solver.assert(
&y._eq(
&fac.apply(&[&ast::Int::from_i64(&ctx, 5).into()])
.as_int()
.unwrap(),
),
);
solver.assert(&y._eq(&ast::Int::from_i64(&ctx, 120)));
assert_eq!(solver.check(), SatResult::Sat)
}
#[test]
fn test_rec_func_def_unsat() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let fac = RecFuncDecl::new(&ctx, "fac", &[&Sort::int(&ctx)], &Sort::int(&ctx));
let n = ast::Int::new_const(&ctx, "n");
let n_minus_1 = ast::Int::sub(&ctx, &[&n, &ast::Int::from_i64(&ctx, 1)]);
let fac_of_n_minus_1 = fac.apply(&[&n_minus_1.into()]);
let cond: ast::Bool = n.le(&ast::Int::from_i64(&ctx, 0));
let body = cond.ite(
&ast::Int::from_i64(&ctx, 1),
&ast::Int::mul(&ctx, &[&n, &fac_of_n_minus_1.as_int().unwrap()]),
);
fac.add_def(&[&n.into()], &body);
let x = ast::Int::new_const(&ctx, "x");
let y = ast::Int::new_const(&ctx, "y");
let solver = Solver::new(&ctx);
solver.assert(
&x._eq(
&fac.apply(&[&ast::Int::from_i64(&ctx, 4).into()])
.as_int()
.unwrap(),
),
);
solver.assert(&y._eq(&ast::Int::mul(&ctx, &[&ast::Int::from_i64(&ctx, 5), &x])));
solver.assert(
&y._eq(
&fac.apply(&[&ast::Int::from_i64(&ctx, 5).into()])
.as_int()
.unwrap(),
),
);
// If fac was an uninterpreted function, this assertion would work.
// To see this, comment out `fac.add_def(&[&n.into()], &body);`
solver.assert(&y._eq(&ast::Int::from_i64(&ctx, 25)));
assert_eq!(solver.check(), SatResult::Unsat)
}
#[test]
#[ignore = "See https://github.com/Z3Prover/z3/issues/5702"]
fn test_solver_unknown() {
let _ = env_logger::try_init();
let mut cfg = Config::new();
// Use a very short timeout to quickly return "unknown"
cfg.set_timeout_msec(1);
let ctx = Context::new(&cfg);
// An open problem: find a model for x^3 + y^3 + z^3 == 42
// See: https://en.wikipedia.org/wiki/Sums_of_three_cubes
let x = ast::Int::new_const(&ctx, "x");
let y = ast::Int::new_const(&ctx, "y");
let z = ast::Int::new_const(&ctx, "z");
let x_cube = ast::Int::mul(&ctx, &[&x, &x, &x]);
let y_cube = ast::Int::mul(&ctx, &[&y, &y, &y]);
let z_cube = ast::Int::mul(&ctx, &[&z, &z, &z]);
let sum_of_cubes = &ast::Int::add(&ctx, &[&x_cube, &y_cube, &z_cube]);
let sum_of_cubes_is_42 = sum_of_cubes._eq(&ast::Int::from_i64(&ctx, 42));
let solver = Solver::new(&ctx);
solver.assert(&sum_of_cubes_is_42);
assert_eq!(solver.check(), SatResult::Unknown);
assert!(solver.get_reason_unknown().is_some());
}
#[test]
fn test_optimize_unknown() {
let _ = env_logger::try_init();
let mut cfg = Config::new();
// Use a very short timeout to quickly return "unknown"
cfg.set_timeout_msec(1);
let ctx = Context::new(&cfg);
// An open problem: find a model for x^3 + y^3 + z^3 == 42
// See: https://en.wikipedia.org/wiki/Sums_of_three_cubes
let x = ast::Int::new_const(&ctx, "x");
let y = ast::Int::new_const(&ctx, "y");
let z = ast::Int::new_const(&ctx, "z");
let x_cube = ast::Int::mul(&ctx, &[&x, &x, &x]);
let y_cube = ast::Int::mul(&ctx, &[&y, &y, &y]);
let z_cube = ast::Int::mul(&ctx, &[&z, &z, &z]);
let sum_of_cubes = &ast::Int::add(&ctx, &[&x_cube, &y_cube, &z_cube]);
let sum_of_cubes_is_42 = sum_of_cubes._eq(&ast::Int::from_i64(&ctx, 42));
let optimize = Optimize::new(&ctx);
optimize.assert(&sum_of_cubes_is_42);
assert_eq!(optimize.check(&[]), SatResult::Unknown);
assert!(optimize.get_reason_unknown().is_some());
}
#[test]
fn test_get_unsat_core() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
assert!(
solver.get_unsat_core().is_empty(),
"no unsat core before assertions"
);
let x = ast::Int::new_const(&ctx, "x");
let x_is_three = ast::Bool::new_const(&ctx, "x-is-three");
solver.assert_and_track(&x._eq(&ast::Int::from_i64(&ctx, 3)), &x_is_three);
let x_is_five = ast::Bool::new_const(&ctx, "x-is-five");
solver.assert_and_track(&x._eq(&ast::Int::from_i64(&ctx, 5)), &x_is_five);
assert!(
solver.get_unsat_core().is_empty(),
"no unsat core before checking"
);
let result = solver.check();
assert_eq!(result, SatResult::Unsat);
let unsat_core = solver.get_unsat_core();
assert_eq!(unsat_core.len(), 2);
assert!(unsat_core.contains(&x_is_three));
assert!(unsat_core.contains(&x_is_five));
}
#[test]
fn test_datatype_builder() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let maybe_int = DatatypeBuilder::new(&ctx, "MaybeInt")
.variant("Nothing", vec![])
.variant(
"Just",
vec![("int", DatatypeAccessor::Sort(Sort::int(&ctx)))],
)
.finish();
let nothing = maybe_int.variants[0].constructor.apply(&[]);
let five = ast::Int::from_i64(&ctx, 5);
let just_five = maybe_int.variants[1].constructor.apply(&[&five]);
let nothing_is_nothing = maybe_int.variants[0]
.tester
.apply(&[¬hing])
.as_bool()
.unwrap();
solver.assert(¬hing_is_nothing);
let nothing_is_just = maybe_int.variants[1]
.tester
.apply(&[¬hing])
.as_bool()
.unwrap();
solver.assert(¬hing_is_just.not());
let just_five_is_nothing = maybe_int.variants[0]
.tester
.apply(&[&just_five])
.as_bool()
.unwrap();
solver.assert(&just_five_is_nothing.not());
let just_five_is_just = maybe_int.variants[1]
.tester
.apply(&[&just_five])
.as_bool()
.unwrap();
solver.assert(&just_five_is_just);
let five_two = maybe_int.variants[1].accessors[0]
.apply(&[&just_five])
.as_int()
.unwrap();
solver.assert(&five._eq(&five_two));
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_recursive_datatype() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let list_sort = DatatypeBuilder::new(&ctx, "List")
.variant("nil", vec![])
.variant(
"cons",
vec![
("car", DatatypeAccessor::Sort(Sort::int(&ctx))),
("cdr", DatatypeAccessor::Datatype("List".into())),
],
)
.finish();
assert_eq!(list_sort.variants.len(), 2);
let nil = list_sort.variants[0].constructor.apply(&[]);
let five = ast::Int::from_i64(&ctx, 5);
let cons_five_nil = list_sort.variants[1].constructor.apply(&[&five, &nil]);
let nil_is_nil = list_sort.variants[0]
.tester
.apply(&[&nil])
.as_bool()
.unwrap();
solver.assert(&nil_is_nil);
let nil_is_cons = list_sort.variants[1]
.tester
.apply(&[&nil])
.as_bool()
.unwrap();
solver.assert(&nil_is_cons.not());
let cons_five_nil_is_nil = list_sort.variants[0]
.tester
.apply(&[&cons_five_nil])
.as_bool()
.unwrap();
solver.assert(&cons_five_nil_is_nil.not());
let cons_five_nil_is_cons = list_sort.variants[1]
.tester
.apply(&[&cons_five_nil])
.as_bool()
.unwrap();
solver.assert(&cons_five_nil_is_cons);
let car_cons_five_is_five = list_sort.variants[1].accessors[0]
.apply(&[&cons_five_nil])
.as_int()
.unwrap();
solver.assert(&car_cons_five_is_five._eq(&five));
assert_eq!(solver.check(), SatResult::Sat);
let cdr_cons_five_is_nil = list_sort.variants[1].accessors[1]
.apply(&[&cons_five_nil])
.as_datatype()
.unwrap();
solver.assert(&cdr_cons_five_is_nil._eq(&nil.as_datatype().unwrap()));
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn test_mutually_recursive_datatype() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
let tree_builder = DatatypeBuilder::new(&ctx, "Tree")
.variant(
"leaf",
vec![("val", DatatypeAccessor::Sort(Sort::int(&ctx)))],
)
.variant(
"node",
vec![("children", DatatypeAccessor::Datatype("TreeList".into()))],
);
let tree_list_builder = DatatypeBuilder::new(&ctx, "TreeList")
.variant("nil", vec![])
.variant(
"cons",
vec![
("car", DatatypeAccessor::Datatype("Tree".into())),
("cdr", DatatypeAccessor::Datatype("TreeList".into())),
],
);
let sorts = z3::datatype_builder::create_datatypes(vec![tree_builder, tree_list_builder]);
assert_eq!(sorts.len(), 2);
let tree_sort = &sorts[0];
assert_eq!(tree_sort.variants.len(), 2);
assert_eq!(tree_sort.variants[0].accessors.len(), 1);
assert_eq!(tree_sort.variants[1].accessors.len(), 1);
let tree_list_sort = &sorts[1];
assert_eq!(tree_list_sort.variants.len(), 2);
assert_eq!(tree_list_sort.variants[0].accessors.len(), 0);
assert_eq!(tree_list_sort.variants[1].accessors.len(), 2);
let ten = ast::Int::from_i64(&ctx, 10);
let leaf_ten = tree_sort.variants[0].constructor.apply(&[&ten]);
let leaf_ten_val_is_ten = tree_sort.variants[0].accessors[0]
.apply(&[&leaf_ten])
.as_int()
.unwrap();
solver.assert(&leaf_ten_val_is_ten._eq(&ten.clone()));
assert_eq!(solver.check(), SatResult::Sat);
let nil = tree_list_sort.variants[0].constructor.apply(&[]);
let twenty = ast::Int::from_i64(&ctx, 20);
let leaf_twenty = tree_sort.variants[0].constructor.apply(&[&twenty]);
let cons_leaf_twenty_nil = tree_list_sort.variants[1]
.constructor
.apply(&[&leaf_twenty, &nil]);
let cons_leaf_ten_cons_leaf_twenty_nil = tree_list_sort.variants[1]
.constructor
.apply(&[&leaf_ten, &cons_leaf_twenty_nil]);
// n1 = Tree.node(TreeList.cons(Tree.leaf(10), TreeList.cons(Tree.leaf(20), TreeList.nil)))
let n1 = tree_sort.variants[1]
.constructor
.apply(&[&cons_leaf_ten_cons_leaf_twenty_nil]);
let n1_cons_nil = tree_list_sort.variants[1].constructor.apply(&[&n1, &nil]);
// n2 = Tree.node(TreeList.cons(n1, TreeList.nil))
let n2 = tree_sort.variants[1].constructor.apply(&[&n1_cons_nil]);
solver.assert(&n2._eq(&n1).not());
// assert(TreeList.car(Tree.children(n2)) == n1)
solver.assert(
&tree_list_sort.variants[1].accessors[0]
.apply(&[&tree_sort.variants[1].accessors[0].apply(&[&n2])])
._eq(&n1),
);
assert_eq!(solver.check(), SatResult::Sat);
}
#[test]
fn get_model_without_check_does_not_exit() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let solver = Solver::new(&ctx);
solver.get_model();
}
#[test]
fn check_application_of_tactic_to_goal() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let params = Params::new(&ctx);
let tactic = Tactic::new(&ctx, "simplify");
let repeat_tactic = Tactic::repeat(&ctx, &tactic, 100);
let goal = Goal::new(&ctx, false, false, false);
let x = ast::Bool::new_const(&ctx, "x");
goal.assert(&x);
let one = ast::Int::from_i64(&ctx, 1);
let two = ast::Int::from_i64(&ctx, 2);
let y = ast::Int::new_const(&ctx, "y");
let y_plus_one_greater_than_or_equal_to_two = y.clone().add(&one).ge(&two);
goal.assert(&y_plus_one_greater_than_or_equal_to_two);
let y_greater_than_or_equal_to_one = y.ge(&one);
goal.assert(&y_greater_than_or_equal_to_one);
assert_eq!(
format!("{}", goal),
"(goal\n x\n (>= (+ y 1) 2)\n (>= y 1))"
);
let apply_results = repeat_tactic.apply(&goal, Some(¶ms));
let goal_results = apply_results.unwrap().list_subgoals().collect::<Vec<Goal>>();
let goal_result = goal_results.first().unwrap();
assert_eq!(format!("{}", goal_result), "(goal\n x\n (>= y 1))");
}
#[test]
fn test_goal_depth() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let goal = Goal::new(&ctx, false, false, false);
let a = ast::Bool::new_const(&ctx, "a");
let b = ast::Bool::new_const(&ctx, "b");
goal.assert(&a);
goal.assert(&b);
assert_eq!(goal.get_depth(), 0);
}
#[test]
fn test_goal_size() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let goal = Goal::new(&ctx, false, false, false);
let a = ast::Bool::new_const(&ctx, "a");
let b = ast::Bool::new_const(&ctx, "b");
goal.assert(&a);
goal.assert(&b);
assert_eq!(goal.get_size(), 2);
}
#[test]
fn test_goal_num_expr() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let goal = Goal::new(&ctx, false, false, false);
let a = ast::Bool::new_const(&ctx, "a");
goal.assert(&a);
assert_eq!(goal.get_num_expr(), 1);
let goal = Goal::new(&ctx, false, false, false);
let a = ast::Bool::new_const(&ctx, "a");
let b = ast::Bool::new_const(&ctx, "b");
goal.assert(&a);
goal.assert(&b);
assert_eq!(goal.get_num_expr(), 2);
}
#[test]
fn test_goal_get_precision() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let false_bool = ast::Bool::from_bool(&ctx, false);
let goal = Goal::new(&ctx, false, false, false);
goal.assert(&false_bool);
assert_eq!(goal.get_precision(), z3::GoalPrec::Precise);
}
#[test]
fn test_goal_is_inconsistent() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let false_bool = ast::Bool::from_bool(&ctx, false);
let goal = Goal::new(&ctx, false, false, false);
goal.assert(&false_bool);
assert_eq!(goal.is_inconsistent(), true);
let true_bool = ast::Bool::from_bool(&ctx, true);
let goal = Goal::new(&ctx, false, false, false);
goal.assert(&true_bool);
assert_eq!(goal.is_inconsistent(), false);