|
| 1 | +require import AllCore. |
| 2 | + |
| 3 | +exception assume. |
| 4 | +exception assert. |
| 5 | + |
| 6 | +op p1: int -> bool. |
| 7 | +op p2: int -> bool. |
| 8 | + |
| 9 | +module M' ={ |
| 10 | + proc truc (x:int) : int = { |
| 11 | + raise (! p1 x \/ ! p2 x) assume; |
| 12 | + if (!p1 x \/ !p2 x) { raise assert;} |
| 13 | + return x; |
| 14 | + } |
| 15 | +}. |
| 16 | +
|
| 17 | +print M'. |
| 18 | + |
| 19 | +lemma assume_assert (_x:int): |
| 20 | +hoare [M'.truc : _x = x ==> false | assume:p1 _x | assert: !(p1 _x /\ p2 _x)]. |
| 21 | + proof. |
| 22 | + proc. |
| 23 | + wp. |
| 24 | + auto => &hr <- />. smt. |
| 25 | + qed. |
| 26 | + |
| 27 | +lemma assert_assume (_x:int): |
| 28 | +hoare [M'.truc : _x = x ==> false | assume:p2 _x | assert: !(p2 _x /\ p1 _x) ]. |
| 29 | + proof. |
| 30 | + proc. |
| 31 | + wp. |
| 32 | + auto => &hr <- />. smt. |
| 33 | + qed. |
| 34 | + |
| 35 | +lemma assert_assume' ( _x:int) : |
| 36 | +hoare [M'.truc : _x = x ==> false | assume: p1 _x /\ p2 _x | assert: !(p2 _x /\ p1 _x) ]. |
| 37 | + proof. |
| 38 | + conseq (assume_assert _x) (assert_assume _x). |
| 39 | + + auto. |
| 40 | + + auto. |
| 41 | + qed. |
| 42 | + |
| 43 | +exception e1. |
| 44 | +exception e2. |
| 45 | +exception e3. |
| 46 | + |
| 47 | +module M ={ |
| 48 | + proc f1 (x:int) : int = { |
| 49 | + raise (x <> 3) e1; |
| 50 | + x <- 5; |
| 51 | + return x; |
| 52 | + } |
| 53 | + |
| 54 | + proc f2 (x:int) : int = { |
| 55 | + if (x = 3) { |
| 56 | + x <- x; |
| 57 | + x <@ f1(x); |
| 58 | + } else { |
| 59 | + x <@ f1(x); |
| 60 | + } |
| 61 | + return x; |
| 62 | + } |
| 63 | +}. |
| 64 | + |
| 65 | +op pe: bool. |
| 66 | +op pe1: bool. |
| 67 | +op pe2: bool. |
| 68 | +op pe3: bool. |
| 69 | +op pe4: bool. |
| 70 | +op pd1: bool. |
| 71 | +op pd2: bool. |
| 72 | +op pd3: bool. |
| 73 | + |
| 74 | +axiom a1: pd2 => pd1. |
| 75 | +axiom a2: pe => pd1. |
| 76 | +axiom a3: pd2 => pe3. |
| 77 | +axiom a4: pd2 => pe4. |
| 78 | +axiom a5: pd1 => pd2. |
| 79 | + |
| 80 | +lemma l_f1 (_x: int): |
| 81 | +hoare [M.f1 : _x = x ==> (res <= 5) | e1:_x <= 3 | pd1]. |
| 82 | + proof. |
| 83 | + proc. |
| 84 | + conseq (: _ ==> x = 5 | e1: _x = 3 | e2: pe | pd2). |
| 85 | + + move => &hr h x. smt. |
| 86 | + + wp. auto. |
| 87 | + qed. |
| 88 | + |
| 89 | +lemma l_f2 (_x: int): |
| 90 | +hoare [M.f2 : _x = x ==> res < 6 | e1: _x < 4 | e2:pe3 | e3: pe4 | pd2 ]. |
| 91 | + proof. |
| 92 | + proc. |
| 93 | + if. |
| 94 | + + call (: _x = x ==> res = 5 | e1 : 3 = _x | e2: pd2 | pd2). |
| 95 | + + proc. |
| 96 | + wp. auto. |
| 97 | + wp. auto. smt. |
| 98 | + call (l_f1 _x). |
| 99 | + auto. smt. |
| 100 | + qed. |
| 101 | + |
| 102 | +module M1 ={ |
| 103 | + var i:int |
| 104 | + |
| 105 | + proc f1 (x:int) : int = { |
| 106 | + i <- 0; |
| 107 | + raise e2; |
| 108 | + return x; |
| 109 | + } |
| 110 | + |
| 111 | + proc f2 (x:int) : int = { |
| 112 | + i <- 1; |
| 113 | + x <@ f1(x); |
| 114 | + return x; |
| 115 | + } |
| 116 | +}. |
| 117 | + |
| 118 | +lemma test (_x: int): |
| 119 | +hoare [M1.f2 : true ==> true |e2: M1.i = 0]. |
| 120 | + proof. |
| 121 | + proc. |
| 122 | + call (: true ==> true | e2 : M1.i = 0). |
| 123 | + + proc. wp. auto. |
| 124 | + auto. |
| 125 | +qed. |
| 126 | + |
| 127 | +lemma test2 (_x: int): |
| 128 | +hoare [M1.f1 : true ==> true |e2: M1.i = 0]. |
| 129 | + proof. |
| 130 | + proc. |
| 131 | + conseq (: _ ==> _ | e2: M1.i = 0). |
| 132 | + auto. |
| 133 | +qed. |
| 134 | + |
| 135 | +module M2 ={ |
| 136 | + |
| 137 | + proc f1 (x:int) : int = { |
| 138 | + |
| 139 | + return x; |
| 140 | + } |
| 141 | + |
| 142 | + proc f2 (x:int) : int = { |
| 143 | + x <- x + 1; |
| 144 | + x <@ f1(x); |
| 145 | + return x; |
| 146 | + } |
| 147 | +}. |
| 148 | + |
| 149 | +lemma test3 (_x: int): |
| 150 | +hoare [M2.f1 : _x = x ==> res = _x | e2 : _x = 0]. |
| 151 | +proof. |
| 152 | + proc. |
| 153 | + auto. |
| 154 | +qed. |
| 155 | + |
| 156 | +lemma test4 (_x: int): |
| 157 | +hoare [M2.f2 : _x = x ==> res = _x + 1 | e2 : _x + 1= 0 ]. |
| 158 | + proof. |
| 159 | + proc. |
| 160 | + ecall(test3 x). |
| 161 | + auto. |
| 162 | +qed. |
0 commit comments