| author | wenzelm | 
| Thu, 01 Jan 2009 22:37:34 +0100 | |
| changeset 29300 | e841a9de5445 | 
| parent 23373 | ead82c82da9e | 
| permissions | -rw-r--r-- | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 1 | (* Title: HOL/Isar_examples/ExprCompiler.thy | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 2 | ID: $Id$ | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 3 | Author: Markus Wenzel, TU Muenchen | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 4 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 5 | Correctness of a simple expression/stack-machine compiler. | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 6 | *) | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 7 | |
| 10007 | 8 | header {* Correctness of a simple expression compiler *}
 | 
| 7748 | 9 | |
| 16417 | 10 | theory ExprCompiler imports Main begin | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 11 | |
| 7869 | 12 | text {*
 | 
| 7874 | 13 | This is a (rather trivial) example of program verification. We model | 
| 14 | a compiler for translating expressions to stack machine instructions, | |
| 15 | and prove its correctness wrt.\ some evaluation semantics. | |
| 10007 | 16 | *} | 
| 7869 | 17 | |
| 18 | ||
| 10007 | 19 | subsection {* Binary operations *}
 | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 20 | |
| 6744 | 21 | text {*
 | 
| 7869 | 22 | Binary operations are just functions over some type of values. This | 
| 7982 | 23 | is both for abstract syntax and semantics, i.e.\ we use a ``shallow | 
| 7874 | 24 | embedding'' here. | 
| 10007 | 25 | *} | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 26 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 27 | types | 
| 10007 | 28 | 'val binop = "'val => 'val => 'val" | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 29 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 30 | |
| 10007 | 31 | subsection {* Expressions *}
 | 
| 7869 | 32 | |
| 33 | text {*
 | |
| 34 | The language of expressions is defined as an inductive type, | |
| 35 | consisting of variables, constants, and binary operations on | |
| 36 | expressions. | |
| 10007 | 37 | *} | 
| 7869 | 38 | |
| 39 | datatype ('adr, 'val) expr =
 | |
| 40 | Variable 'adr | | |
| 41 | Constant 'val | | |
| 10007 | 42 |   Binop "'val binop" "('adr, 'val) expr" "('adr, 'val) expr"
 | 
| 7869 | 43 | |
| 44 | text {*
 | |
| 7874 | 45 | Evaluation (wrt.\ some environment of variable assignments) is | 
| 46 | defined by primitive recursion over the structure of expressions. | |
| 10007 | 47 | *} | 
| 7869 | 48 | |
| 49 | consts | |
| 10007 | 50 |   eval :: "('adr, 'val) expr => ('adr => 'val) => 'val"
 | 
| 7869 | 51 | |
| 52 | primrec | |
| 53 | "eval (Variable x) env = env x" | |
| 54 | "eval (Constant c) env = c" | |
| 10007 | 55 | "eval (Binop f e1 e2) env = f (eval e1 env) (eval e2 env)" | 
| 7869 | 56 | |
| 57 | ||
| 10007 | 58 | subsection {* Machine *}
 | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 59 | |
| 6744 | 60 | text {*
 | 
| 6792 | 61 | Next we model a simple stack machine, with three instructions. | 
| 10007 | 62 | *} | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 63 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 64 | datatype ('adr, 'val) instr =
 | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 65 | Const 'val | | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 66 | Load 'adr | | 
| 10007 | 67 | Apply "'val binop" | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 68 | |
| 6744 | 69 | text {*
 | 
| 7869 | 70 | Execution of a list of stack machine instructions is easily defined | 
| 71 | as follows. | |
| 10007 | 72 | *} | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 73 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 74 | consts | 
| 7761 | 75 |   exec :: "(('adr, 'val) instr) list
 | 
| 10007 | 76 |     => 'val list => ('adr => 'val) => 'val list"
 | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 77 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 78 | primrec | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 79 | "exec [] stack env = stack" | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 80 | "exec (instr # instrs) stack env = | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 81 | (case instr of | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 82 | Const c => exec instrs (c # stack) env | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 83 | | Load x => exec instrs (env x # stack) env | 
| 7761 | 84 | | Apply f => exec instrs (f (hd stack) (hd (tl stack)) | 
| 10007 | 85 | # (tl (tl stack))) env)" | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 86 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 87 | constdefs | 
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 88 |   execute :: "(('adr, 'val) instr) list => ('adr => 'val) => 'val"
 | 
| 10007 | 89 | "execute instrs env == hd (exec instrs [] env)" | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 90 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 91 | |
| 10007 | 92 | subsection {* Compiler *}
 | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 93 | |
| 6744 | 94 | text {*
 | 
| 7869 | 95 | We are ready to define the compilation function of expressions to | 
| 6792 | 96 | lists of stack machine instructions. | 
| 10007 | 97 | *} | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 98 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 99 | consts | 
| 10007 | 100 |   compile :: "('adr, 'val) expr => (('adr, 'val) instr) list"
 | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 101 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 102 | primrec | 
| 8031 
826c6222cca2
renamed comp to compile (avoids clash with Relation.comp);
 wenzelm parents: 
7982diff
changeset | 103 | "compile (Variable x) = [Load x]" | 
| 
826c6222cca2
renamed comp to compile (avoids clash with Relation.comp);
 wenzelm parents: 
7982diff
changeset | 104 | "compile (Constant c) = [Const c]" | 
| 10007 | 105 | "compile (Binop f e1 e2) = compile e2 @ compile e1 @ [Apply f]" | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 106 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 107 | |
| 6744 | 108 | text {*
 | 
| 8051 | 109 | The main result of this development is the correctness theorem for | 
| 110 |  $\idt{compile}$.  We first establish a lemma about $\idt{exec}$ and
 | |
| 111 | list append. | |
| 10007 | 112 | *} | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 113 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 114 | lemma exec_append: | 
| 18153 | 115 | "exec (xs @ ys) stack env = | 
| 116 | exec ys (exec xs stack env) env" | |
| 20503 | 117 | proof (induct xs arbitrary: stack) | 
| 18153 | 118 | case Nil | 
| 119 | show ?case by simp | |
| 11809 | 120 | next | 
| 18153 | 121 | case (Cons x xs) | 
| 122 | show ?case | |
| 11809 | 123 | proof (induct x) | 
| 23373 | 124 | case Const | 
| 125 | from Cons show ?case by simp | |
| 18153 | 126 | next | 
| 23373 | 127 | case Load | 
| 128 | from Cons show ?case by simp | |
| 18153 | 129 | next | 
| 23373 | 130 | case Apply | 
| 131 | from Cons show ?case by simp | |
| 10007 | 132 | qed | 
| 133 | qed | |
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 134 | |
| 10007 | 135 | theorem correctness: "execute (compile e) env = eval e env" | 
| 136 | proof - | |
| 18193 | 137 | have "\<And>stack. exec (compile e) stack env = eval e env # stack" | 
| 11809 | 138 | proof (induct e) | 
| 18153 | 139 | case Variable show ?case by simp | 
| 140 | next | |
| 141 | case Constant show ?case by simp | |
| 142 | next | |
| 143 | case Binop then show ?case by (simp add: exec_append) | |
| 10007 | 144 | qed | 
| 23373 | 145 | then show ?thesis by (simp add: execute_def) | 
| 10007 | 146 | qed | 
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 147 | |
| 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 148 | |
| 8051 | 149 | text {*
 | 
| 150 |  \bigskip In the proofs above, the \name{simp} method does quite a lot
 | |
| 151 | of work behind the scenes (mostly ``functional program execution''). | |
| 152 | Subsequently, the same reasoning is elaborated in detail --- at most | |
| 153 | one recursive function definition is used at a time. Thus we get a | |
| 154 | better idea of what is actually going on. | |
| 10007 | 155 | *} | 
| 8051 | 156 | |
| 13524 | 157 | lemma exec_append': | 
| 18153 | 158 | "exec (xs @ ys) stack env = exec ys (exec xs stack env) env" | 
| 20503 | 159 | proof (induct xs arbitrary: stack) | 
| 18153 | 160 | case (Nil s) | 
| 161 | have "exec ([] @ ys) s env = exec ys s env" by simp | |
| 162 | also have "... = exec ys (exec [] s env) env" by simp | |
| 163 | finally show ?case . | |
| 164 | next | |
| 165 | case (Cons x xs s) | |
| 166 | show ?case | |
| 10007 | 167 | proof (induct x) | 
| 18153 | 168 | case (Const val) | 
| 169 | have "exec ((Const val # xs) @ ys) s env = exec (Const val # xs @ ys) s env" | |
| 170 | by simp | |
| 171 | also have "... = exec (xs @ ys) (val # s) env" by simp | |
| 172 | also from Cons have "... = exec ys (exec xs (val # s) env) env" . | |
| 173 | also have "... = exec ys (exec (Const val # xs) s env) env" by simp | |
| 174 | finally show ?case . | |
| 10007 | 175 | next | 
| 18153 | 176 | case (Load adr) | 
| 177 |     from Cons show ?case by simp -- {* same as above *}
 | |
| 178 | next | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 179 | case (Apply fn) | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 180 | have "exec ((Apply fn # xs) @ ys) s env = | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 181 | exec (Apply fn # xs @ ys) s env" by simp | 
| 18153 | 182 | also have "... = | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 183 | exec (xs @ ys) (fn (hd s) (hd (tl s)) # (tl (tl s))) env" by simp | 
| 18153 | 184 | also from Cons have "... = | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 185 | exec ys (exec xs (fn (hd s) (hd (tl s)) # tl (tl s)) env) env" . | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 186 | also have "... = exec ys (exec (Apply fn # xs) s env) env" by simp | 
| 18153 | 187 | finally show ?case . | 
| 10007 | 188 | qed | 
| 189 | qed | |
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 190 | |
| 13537 | 191 | theorem correctness': "execute (compile e) env = eval e env" | 
| 10007 | 192 | proof - | 
| 18193 | 193 | have exec_compile: "\<And>stack. exec (compile e) stack env = eval e env # stack" | 
| 10007 | 194 | proof (induct e) | 
| 18153 | 195 | case (Variable adr s) | 
| 196 | have "exec (compile (Variable adr)) s env = exec [Load adr] s env" | |
| 197 | by simp | |
| 198 | also have "... = env adr # s" by simp | |
| 199 | also have "env adr = eval (Variable adr) env" by simp | |
| 200 | finally show ?case . | |
| 10007 | 201 | next | 
| 18153 | 202 | case (Constant val s) | 
| 203 |     show ?case by simp -- {* same as above *}
 | |
| 10007 | 204 | next | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 205 | case (Binop fn e1 e2 s) | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 206 | have "exec (compile (Binop fn e1 e2)) s env = | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 207 | exec (compile e2 @ compile e1 @ [Apply fn]) s env" by simp | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 208 | also have "... = exec [Apply fn] | 
| 18153 | 209 | (exec (compile e1) (exec (compile e2) s env) env) env" | 
| 210 | by (simp only: exec_append) | |
| 211 | also have "exec (compile e2) s env = eval e2 env # s" by fact | |
| 212 | also have "exec (compile e1) ... env = eval e1 env # ..." by fact | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 213 | also have "exec [Apply fn] ... env = | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 214 | fn (hd ...) (hd (tl ...)) # (tl (tl ...))" by simp | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 215 | also have "... = fn (eval e1 env) (eval e2 env) # s" by simp | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 216 | also have "fn (eval e1 env) (eval e2 env) = | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20503diff
changeset | 217 | eval (Binop fn e1 e2) env" | 
| 18153 | 218 | by simp | 
| 219 | finally show ?case . | |
| 10007 | 220 | qed | 
| 8051 | 221 | |
| 10007 | 222 | have "execute (compile e) env = hd (exec (compile e) [] env)" | 
| 223 | by (simp add: execute_def) | |
| 224 | also from exec_compile | |
| 18153 | 225 | have "exec (compile e) [] env = [eval e env]" . | 
| 10007 | 226 | also have "hd ... = eval e env" by simp | 
| 227 | finally show ?thesis . | |
| 228 | qed | |
| 6444 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 wenzelm parents: diff
changeset | 229 | |
| 10007 | 230 | end |