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