|
| 1 | + |
| 2 | +======================================================================== |
| 3 | +Tactic: `cfold` |
| 4 | +======================================================================== |
| 5 | + |
| 6 | +The `cfold` tactic is part of the family of tactics that operate by |
| 7 | +rewriting the program into a semantically equivalent form. |
| 8 | +More concretely, given an assignment of the form:: |
| 9 | + |
| 10 | + a <- b; |
| 11 | + |
| 12 | +the tactic propagates this (known) values of a through the program |
| 13 | +and inlining this (known) value whenever `a` would be used. |
| 14 | + |
| 15 | +For example, the following excerpt:: |
| 16 | + |
| 17 | + a <- 3; |
| 18 | + c <- a + 1; |
| 19 | + a <- a + 2; |
| 20 | + b <- a; |
| 21 | + |
| 22 | +would be converted into:: |
| 23 | + |
| 24 | + c <- 4; (* 3 + 1 *) |
| 25 | + b <- 5; (* a = 3 + 2 = 5 at this point *) |
| 26 | + a <- 5; (* we need to make sure a has the |
| 27 | + correct value at the end of execution *) |
| 28 | + |
| 29 | +.. contents:: |
| 30 | + :local: |
| 31 | + |
| 32 | +------------------------------------------------------------------------ |
| 33 | +Syntax |
| 34 | +------------------------------------------------------------------------ |
| 35 | + |
| 36 | +The general form of the tactic is: |
| 37 | + |
| 38 | +.. admonition:: Syntax |
| 39 | + |
| 40 | + `cfold {side}? {codepos} {n}?` |
| 41 | + |
| 42 | +Where: |
| 43 | + |
| 44 | +- `{side}` is optional and only applicable in relational goals |
| 45 | + to specify on which of the two programs the tactic is to be applied. |
| 46 | + |
| 47 | +- `{codepos}` specifies the instruction on which the tactic will begin. |
| 48 | + The tactic will proceed to propagate the assignment as far as possible, |
| 49 | + even through other assignments to the same variable as long as it can or |
| 50 | + until it reaches the line limit (if given). |
| 51 | + |
| 52 | +- `{n}` is an optional integer argument corresponding to the number of |
| 53 | + lines of the program to process in the folding. This serves to limit the |
| 54 | + scope of the tactic application and prevent it from acting in the whole |
| 55 | + program when this would not be desirable. |
| 56 | + |
| 57 | +.. ecproof:: |
| 58 | + :title: Cfold example |
| 59 | + |
| 60 | + require import AllCore. |
| 61 | + |
| 62 | + module M = { |
| 63 | + proc f(a : int) = { |
| 64 | + var x, y : int; |
| 65 | + |
| 66 | + x <- a + 1; |
| 67 | + y <- 2 * x; |
| 68 | + x <- 3 * y; |
| 69 | + |
| 70 | + return x; |
| 71 | + } |
| 72 | + }. |
| 73 | + |
| 74 | + lemma L : hoare[M.f : witness a ==> witness res]. |
| 75 | + proof. |
| 76 | + proc. |
| 77 | + (*$*) cfold 1. |
| 78 | + |
| 79 | + (* The goal is the same but the program is now rewritten *) |
| 80 | + admit. |
| 81 | + qed. |
| 82 | +
|
| 83 | + |
| 84 | +The propagated variable is then set at the end of the part of the program |
| 85 | +where the tactic was applied (in this case, the end of the program, since |
| 86 | +the tactic applied to its entirety), and it is set to the value which the |
| 87 | +tactic accumulated. |
| 88 | + |
| 89 | +Here is an example of using the parameter `{n}` for limiting tactic |
| 90 | +application: |
| 91 | + |
| 92 | +.. ecproof:: |
| 93 | + :title: Cfold line restriction |
| 94 | + |
| 95 | + require import AllCore. |
| 96 | + |
| 97 | + module M = { |
| 98 | + proc f() = { |
| 99 | + var x, y : int; |
| 100 | + var i : int; |
| 101 | + |
| 102 | + i <- 0; |
| 103 | + |
| 104 | + while (i < 10) { |
| 105 | + x <- i + 1; |
| 106 | + y <- 2 * x; |
| 107 | + x <- 3 * y; |
| 108 | + i <- i + 1; |
| 109 | + } |
| 110 | + |
| 111 | + return x; |
| 112 | + } |
| 113 | + }. |
| 114 | + |
| 115 | + lemma L : hoare[M.f : witness ==> witness res]. |
| 116 | + proof. |
| 117 | + proc. |
| 118 | + |
| 119 | + unroll for 2. |
| 120 | + (*$*) cfold 1 27. |
| 121 | + (* The goal is the same but the program is now rewritten *) |
| 122 | + admit. |
| 123 | + qed. |
0 commit comments