|
| 1 | +# This script illustrates uses of proof replay and proof logs over the Python interface. |
| 2 | + |
| 3 | +from z3 import * |
| 4 | + |
| 5 | +example1 = """ |
| 6 | +(declare-sort T) |
| 7 | +
|
| 8 | +(declare-fun subtype (T T) Bool) |
| 9 | +
|
| 10 | +;; subtype is reflexive |
| 11 | +(assert (forall ((x T)) (subtype x x))) |
| 12 | +
|
| 13 | +;; subtype is antisymmetric |
| 14 | +(assert (forall ((x T) (y T)) (=> (and (subtype x y) |
| 15 | + (subtype y x)) |
| 16 | + (= x y)))) |
| 17 | +;; subtype is transitive |
| 18 | +(assert (forall ((x T) (y T) (z T)) (=> (and (subtype x y) |
| 19 | + (subtype y z)) |
| 20 | + (subtype x z)))) |
| 21 | +;; subtype has the tree-property |
| 22 | +(assert (forall ((x T) (y T) (z T)) (=> (and (subtype x z) |
| 23 | + (subtype y z)) |
| 24 | + (or (subtype x y) |
| 25 | + (subtype y x))))) |
| 26 | +
|
| 27 | +;; now we define a simple example using the axiomatization above. |
| 28 | +(declare-const obj-type T) |
| 29 | +(declare-const int-type T) |
| 30 | +(declare-const real-type T) |
| 31 | +(declare-const complex-type T) |
| 32 | +(declare-const string-type T) |
| 33 | +
|
| 34 | +;; we have an additional axiom: every type is a subtype of obj-type |
| 35 | +(assert (forall ((x T)) (subtype x obj-type))) |
| 36 | +
|
| 37 | +(assert (subtype int-type real-type)) |
| 38 | +(assert (subtype real-type complex-type)) |
| 39 | +(assert (not (subtype string-type real-type))) |
| 40 | +(declare-const root-type T) |
| 41 | +(assert (subtype obj-type root-type)) |
| 42 | +""" |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + print("Solve and log inferences") |
| 46 | + print("--------------------------------------------------------") |
| 47 | + |
| 48 | + # inference logging, replay, and checking is supported for |
| 49 | + # the core enabled by setting sat.euf = true. |
| 50 | + # setting the default tactic to 'sat' bypasses other tactics that could |
| 51 | + # end up using different solvers. |
| 52 | + set_param("sat.euf", True) |
| 53 | + set_param("tactic.default_tactic", "sat") |
| 54 | + |
| 55 | + # Set a log file to trace inferences |
| 56 | + set_param("sat.smt.proof", "proof_log.smt2") |
| 57 | + s = Solver() |
| 58 | + s.from_string(example1) |
| 59 | + print(s.check()) |
| 60 | + print(s.statistics()) |
| 61 | + print("Parse the logged inferences and replay them") |
| 62 | + print("--------------------------------------------------------") |
| 63 | + |
| 64 | + # Reset the log file to an invalid (empty) file name. |
| 65 | + set_param("sat.smt.proof", "") |
| 66 | + |
| 67 | + # Turn off proof checking. It is on by default when parsing proof logs. |
| 68 | + set_param("solver.proof.check", False) |
| 69 | + s = Solver() |
| 70 | + onc = OnClause(s, lambda pr, clause : print(pr, clause)) |
| 71 | + s.from_file("proof_log.smt2") |
| 72 | + |
| 73 | + |
| 74 | + print("Parse the logged inferences and check them") |
| 75 | + print("--------------------------------------------------------") |
| 76 | + s = Solver() |
| 77 | + |
| 78 | + # Now turn on proof checking. It invokes the self-validator. |
| 79 | + # The self-validator produces log lines of the form: |
| 80 | + # (proofs +tseitin 60 +alldiff 8 +euf 3 +rup 5 +inst 6 -quant 3 -inst 2) |
| 81 | + # (verified-smt |
| 82 | + # (inst (forall (vars (x T) (y T) (z T)) (or (subtype (:var 2) (:var 1)) ... |
| 83 | + # The 'proofs' line summarizes inferences that were self-validated. |
| 84 | + # The pair +tseitin 60 indicates that 60 inferences were validated as Tseitin |
| 85 | + # encodings. |
| 86 | + # The pair -inst 2 indicates that two quantifier instantiations were not self-validated |
| 87 | + # They were instead validated using a call to SMT solving. A log for an smt invocation |
| 88 | + # is exemplified in the next line. |
| 89 | + # Note that the pair +inst 6 indicates that 6 quantifier instantations were validated |
| 90 | + # using a syntactic (cheap) check. Some quantifier instantiations based on quantifier elimination |
| 91 | + # are not simple substitutions and therefore a simple syntactic check does not suffice. |
| 92 | + set_param("solver.proof.check", True) |
| 93 | + s.from_file("proof_log.smt2") |
| 94 | + |
| 95 | + print("Verify and self-validate on the fly") |
| 96 | + print("--------------------------------------------------------") |
| 97 | + set_param("sat.smt.proof.check", True) |
| 98 | + s = Solver() |
| 99 | + s.from_string(example1) |
| 100 | + print(s.check()) |
| 101 | + print(s.statistics()) |
| 102 | + |
| 103 | + print("Verify and self-validate on the fly, but don't check rup") |
| 104 | + print("--------------------------------------------------------") |
| 105 | + set_param("sat.smt.proof.check", True) |
| 106 | + set_param("sat.smt.proof.check_rup", False) |
| 107 | + s = Solver() |
| 108 | + s.from_string(example1) |
| 109 | + print(s.check()) |
| 110 | + print(s.statistics()) |
| 111 | + |
| 112 | + |
| 113 | + |
0 commit comments