| author | wenzelm | 
| Fri, 06 Apr 2012 12:02:24 +0200 | |
| changeset 47347 | af937661e4a1 | 
| parent 46582 | dcc312f22ee8 | 
| child 55640 | abc140f21caa | 
| 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  | 
|
| 41818 | 25  | 
type_synonym 'val binop = "'val => 'val => '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  | 
|
| 37671 | 42  | 
primrec eval :: "('adr, 'val) expr => ('adr => 'val) => 'val"
 | 
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  | 
|
| 46582 | 62  | 
primrec exec :: "(('adr, 'val) instr) list => 'val list => ('adr => 'val) => '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  | 
| 
 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 
wenzelm 
parents:  
diff
changeset
 | 
67  | 
Const c => exec instrs (c # stack) env  | 
| 
 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 
wenzelm 
parents:  
diff
changeset
 | 
68  | 
| Load x => exec instrs (env x # stack) env  | 
| 7761 | 69  | 
| Apply f => 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  | 
|
| 46582 | 72  | 
definition execute :: "(('adr, 'val) instr) list => ('adr => 'val) => '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  | 
|
| 46582 | 81  | 
primrec compile :: "('adr, 'val) expr => (('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)  | 
| 18153 | 117  | 
case Variable show ?case by simp  | 
118  | 
next  | 
|
119  | 
case Constant show ?case by simp  | 
|
120  | 
next  | 
|
121  | 
case Binop then show ?case by (simp add: exec_append)  | 
|
| 10007 | 122  | 
qed  | 
| 23373 | 123  | 
then show ?thesis by (simp add: execute_def)  | 
| 10007 | 124  | 
qed  | 
| 
6444
 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 
wenzelm 
parents:  
diff
changeset
 | 
125  | 
|
| 
 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 
wenzelm 
parents:  
diff
changeset
 | 
126  | 
|
| 37671 | 127  | 
text {* \bigskip In the proofs above, the @{text simp} method does
 | 
128  | 
quite a lot of work behind the scenes (mostly ``functional program  | 
|
129  | 
execution''). Subsequently, the same reasoning is elaborated in  | 
|
130  | 
detail --- at most one recursive function definition is used at a  | 
|
131  | 
time. Thus we get a better idea of what is actually going on. *}  | 
|
| 8051 | 132  | 
|
| 13524 | 133  | 
lemma exec_append':  | 
| 18153 | 134  | 
"exec (xs @ ys) stack env = exec ys (exec xs stack env) env"  | 
| 20503 | 135  | 
proof (induct xs arbitrary: stack)  | 
| 18153 | 136  | 
case (Nil s)  | 
137  | 
have "exec ([] @ ys) s env = exec ys s env" by simp  | 
|
138  | 
also have "... = exec ys (exec [] s env) env" by simp  | 
|
139  | 
finally show ?case .  | 
|
140  | 
next  | 
|
141  | 
case (Cons x xs s)  | 
|
142  | 
show ?case  | 
|
| 10007 | 143  | 
proof (induct x)  | 
| 18153 | 144  | 
case (Const val)  | 
145  | 
have "exec ((Const val # xs) @ ys) s env = exec (Const val # xs @ ys) s env"  | 
|
146  | 
by simp  | 
|
147  | 
also have "... = exec (xs @ ys) (val # s) env" by simp  | 
|
148  | 
also from Cons have "... = exec ys (exec xs (val # s) env) env" .  | 
|
149  | 
also have "... = exec ys (exec (Const val # xs) s env) env" by simp  | 
|
150  | 
finally show ?case .  | 
|
| 10007 | 151  | 
next  | 
| 18153 | 152  | 
case (Load adr)  | 
153  | 
    from Cons show ?case by simp -- {* same as above *}
 | 
|
154  | 
next  | 
|
| 
20523
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
155  | 
case (Apply fn)  | 
| 
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
156  | 
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
 | 
157  | 
exec (Apply fn # xs @ ys) s env" by simp  | 
| 18153 | 158  | 
also have "... =  | 
| 
20523
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
159  | 
exec (xs @ ys) (fn (hd s) (hd (tl s)) # (tl (tl s))) env" by simp  | 
| 18153 | 160  | 
also from Cons have "... =  | 
| 
20523
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
161  | 
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: 
20503 
diff
changeset
 | 
162  | 
also have "... = exec ys (exec (Apply fn # xs) s env) env" by simp  | 
| 18153 | 163  | 
finally show ?case .  | 
| 10007 | 164  | 
qed  | 
165  | 
qed  | 
|
| 
6444
 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 
wenzelm 
parents:  
diff
changeset
 | 
166  | 
|
| 13537 | 167  | 
theorem correctness': "execute (compile e) env = eval e env"  | 
| 10007 | 168  | 
proof -  | 
| 18193 | 169  | 
have exec_compile: "\<And>stack. exec (compile e) stack env = eval e env # stack"  | 
| 10007 | 170  | 
proof (induct e)  | 
| 18153 | 171  | 
case (Variable adr s)  | 
172  | 
have "exec (compile (Variable adr)) s env = exec [Load adr] s env"  | 
|
173  | 
by simp  | 
|
174  | 
also have "... = env adr # s" by simp  | 
|
175  | 
also have "env adr = eval (Variable adr) env" by simp  | 
|
176  | 
finally show ?case .  | 
|
| 10007 | 177  | 
next  | 
| 18153 | 178  | 
case (Constant val s)  | 
179  | 
    show ?case by simp -- {* same as above *}
 | 
|
| 10007 | 180  | 
next  | 
| 
20523
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
181  | 
case (Binop fn e1 e2 s)  | 
| 
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
182  | 
have "exec (compile (Binop fn e1 e2)) s env =  | 
| 
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
183  | 
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: 
20503 
diff
changeset
 | 
184  | 
also have "... = exec [Apply fn]  | 
| 18153 | 185  | 
(exec (compile e1) (exec (compile e2) s env) env) env"  | 
186  | 
by (simp only: exec_append)  | 
|
187  | 
also have "exec (compile e2) s env = eval e2 env # s" by fact  | 
|
188  | 
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: 
20503 
diff
changeset
 | 
189  | 
also have "exec [Apply fn] ... env =  | 
| 
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
190  | 
fn (hd ...) (hd (tl ...)) # (tl (tl ...))" by simp  | 
| 
 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 
krauss 
parents: 
20503 
diff
changeset
 | 
191  | 
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: 
20503 
diff
changeset
 | 
192  | 
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
 | 
193  | 
eval (Binop fn e1 e2) env"  | 
| 18153 | 194  | 
by simp  | 
195  | 
finally show ?case .  | 
|
| 10007 | 196  | 
qed  | 
| 8051 | 197  | 
|
| 10007 | 198  | 
have "execute (compile e) env = hd (exec (compile e) [] env)"  | 
199  | 
by (simp add: execute_def)  | 
|
| 37671 | 200  | 
also from exec_compile have "exec (compile e) [] env = [eval e env]" .  | 
| 10007 | 201  | 
also have "hd ... = eval e env" by simp  | 
202  | 
finally show ?thesis .  | 
|
203  | 
qed  | 
|
| 
6444
 
2ebe9e630cab
Miscellaneous Isabelle/Isar examples for Higher-Order Logic.
 
wenzelm 
parents:  
diff
changeset
 | 
204  | 
|
| 10007 | 205  | 
end  |