|
| 1 | +======================================================================== |
| 2 | +Tactic: `simplify if` |
| 3 | +======================================================================== |
| 4 | + |
| 5 | +The `simplify if` performs an if-conversion on program statement, |
| 6 | +i.e it transform if statement into if expression. This transformation preserves the semantics. |
| 7 | +This allows to obtain a statement where the weakest precondition does not grow |
| 8 | +exponentially in the number of if. |
| 9 | + |
| 10 | +To illustrate the problem here is an example that show how grow the weakest pre-condition: |
| 11 | +.. ecproof:: |
| 12 | + :title: Weakest pre-condition grow exponentially. |
| 13 | + |
| 14 | + require import AllCore. |
| 15 | + |
| 16 | + module M = { |
| 17 | + proc f (j:int) : int * int = { |
| 18 | + var i, x, y; |
| 19 | + x <- 0; |
| 20 | + y <- 0; |
| 21 | + i <- 0; |
| 22 | + while (i < 6) { |
| 23 | + if (i = j) x <- i; |
| 24 | + else y <- y + i; |
| 25 | + i <- i + 1; |
| 26 | + } |
| 27 | + return (x, y); |
| 28 | + } |
| 29 | + }. |
| 30 | + |
| 31 | + hoare fP j_: M.f : j = j_ ==> res = if 0 <= j_ < 6 then (j_, 15 - j_) else (0,15). |
| 32 | + proof. |
| 33 | + proc. |
| 34 | + unroll for ^while. |
| 35 | + (*$*) time wp. (* The size of the condition grow exponentially in the value of the bound (here 6). *) |
| 36 | + skip. |
| 37 | + move => />. |
| 38 | + smt(). |
| 39 | + qed. |
| 40 | +
|
| 41 | +.. admonition:: Syntax |
| 42 | + |
| 43 | + Since the tactic preserves the semantic it applies to all program logics. |
| 44 | + |
| 45 | + `simplify if side? codepos?` |
| 46 | + |
| 47 | + The `side` argument is required when the goal is a `equiv` judgment, it allows to select |
| 48 | + on which side you want to apply the program transformation. The `codepos` argument allows |
| 49 | + to specify on which `if` instruction you want to apply the transformation. |
| 50 | + |
| 51 | + |
| 52 | +.. contents:: |
| 53 | + :local: |
| 54 | + |
| 55 | +------------------------------------------------------------------------ |
| 56 | +Variant: Transform at a given code possition |
| 57 | +------------------------------------------------------------------------ |
| 58 | +The tactic applies only if the branches of selected the `if` instruction are only composed of |
| 59 | +assignment. |
| 60 | + |
| 61 | +.. exproof:: |
| 62 | + : title: Hoare logic example selecting where to apply the transformation |
| 63 | + require import AllCore. |
| 64 | + |
| 65 | + module M = { |
| 66 | + proc f (j:int) : int * int = { |
| 67 | + var i, x, y; |
| 68 | + x <- 0; |
| 69 | + y <- 0; |
| 70 | + i <- 0; |
| 71 | + while (i < 6) { |
| 72 | + if (i = j) x <- i; |
| 73 | + else y <- y + i; |
| 74 | + i <- i + 1; |
| 75 | + } |
| 76 | + return (x, y); |
| 77 | + } |
| 78 | + }. |
| 79 | + |
| 80 | + hoare fP_simplify2 j_: M.f : j = j_ ==> res = if 0 <= j_ < 6 then (j_, 15 - j_) else (0,15). |
| 81 | + proof. |
| 82 | + proc. |
| 83 | + unroll for ^while. |
| 84 | + (* You can select a particular occurence of if using codepos *) |
| 85 | + (*$*) simplify if ^if. (* Apply the transformation on the first if *) |
| 86 | + simplify if ^if{2}. (* Apply the transformation on the second if *) |
| 87 | + simplify if ^if{-2}. (* Apply the trnasformation of the penultimate if *) |
| 88 | + time wp. |
| 89 | + skip. |
| 90 | + move => />. |
| 91 | + smt(). |
| 92 | + qed. |
| 93 | +
|
| 94 | +------------------------------------------------------------------------ |
| 95 | +Variant: Transform as much as possible |
| 96 | +------------------------------------------------------------------------ |
| 97 | + |
| 98 | +.. admonition:: Syntax |
| 99 | + |
| 100 | + `simplify if` |
| 101 | + |
| 102 | +This variant try to find a position where the transformation is possible and applies it. |
| 103 | +This is repeated until no position is found. |
| 104 | + |
| 105 | +.. ecproof:: |
| 106 | + :title: Hoare logic example |
| 107 | + |
| 108 | + require import AllCore. |
| 109 | + |
| 110 | + module M = { |
| 111 | + proc f (j:int) : int * int = { |
| 112 | + var i, x, y; |
| 113 | + x <- 0; |
| 114 | + y <- 0; |
| 115 | + i <- 0; |
| 116 | + while (i < 6) { |
| 117 | + if (i = j) x <- i; |
| 118 | + else y <- y + i; |
| 119 | + i <- i + 1; |
| 120 | + } |
| 121 | + return (x, y); |
| 122 | + } |
| 123 | + }. |
| 124 | + |
| 125 | + hoare fP_simplify j_: M.f : j = j_ ==> res = if 0 <= j_ < 6 then (j_, 15 - j_) else (0,15). |
| 126 | + proof. |
| 127 | + proc. |
| 128 | + unroll for ^while. |
| 129 | + (*$*)simplify if. (* if instruction are transformed into single assignment *) |
| 130 | + time wp. (* The size of the wp is now linear in the number of instruction *) |
| 131 | + skip. |
| 132 | + move => />. |
| 133 | + smt(). |
| 134 | + qed. |
0 commit comments