equal
deleted
inserted
replaced
5 Correctness of a simple expression/stack-machine compiler. |
5 Correctness of a simple expression/stack-machine compiler. |
6 *) |
6 *) |
7 |
7 |
8 theory ExprCompiler = Main:; |
8 theory ExprCompiler = Main:; |
9 |
9 |
10 title |
10 title {* Correctness of a simple expression/stack-machine compiler *}; |
11 "Correctness of a simple expression/stack-machine compiler --- an Isabelle/Isar demonstration" |
|
12 "Markus Wenzel"; |
|
13 |
11 |
14 |
12 |
15 section "Basics"; |
13 section {* Basics *}; |
16 |
14 |
17 text {* |
15 text {* |
18 First we define a type abbreviation for binary operations over some |
16 First we define a type abbreviation for binary operations over some |
19 type @{type "'val"} of values. |
17 type @{type "'val"} of values. |
20 *}; |
18 *}; |
21 |
19 |
22 types |
20 types |
23 'val binop = "'val => 'val => 'val"; |
21 'val binop = "'val => 'val => 'val"; |
24 |
22 |
25 |
23 |
26 section "Machine"; |
24 section {* Machine *}; |
27 |
25 |
28 text {* |
26 text {* |
29 Next we model a simple stack machine, with three instructions. |
27 Next we model a simple stack machine, with three instructions. |
30 *}; |
28 *}; |
31 |
29 |
53 constdefs |
51 constdefs |
54 execute :: "(('adr, 'val) instr) list => ('adr => 'val) => 'val" |
52 execute :: "(('adr, 'val) instr) list => ('adr => 'val) => 'val" |
55 "execute instrs env == hd (exec instrs [] env)"; |
53 "execute instrs env == hd (exec instrs [] env)"; |
56 |
54 |
57 |
55 |
58 section "Expressions"; |
56 section {* Expressions *}; |
59 |
57 |
60 text {* |
58 text {* |
61 We introduce a simple language of expressions: variables --- |
59 We introduce a simple language of expressions: variables --- |
62 constants --- binary operations. |
60 constants --- binary operations. |
63 *}; |
61 *}; |
78 "eval (Variable x) env = env x" |
76 "eval (Variable x) env = env x" |
79 "eval (Constant c) env = c" |
77 "eval (Constant c) env = c" |
80 "eval (Binop f e1 e2) env = f (eval e1 env) (eval e2 env)"; |
78 "eval (Binop f e1 e2) env = f (eval e1 env) (eval e2 env)"; |
81 |
79 |
82 |
80 |
83 section "Compiler"; |
81 section {* Compiler *}; |
84 |
82 |
85 text {* |
83 text {* |
86 So we are ready to define the compilation function of expressions to |
84 So we are ready to define the compilation function of expressions to |
87 lists of stack machine instructions. |
85 lists of stack machine instructions. |
88 *}; |
86 *}; |
99 text {* |
97 text {* |
100 The main result of this developement is the correctness theorem for |
98 The main result of this developement is the correctness theorem for |
101 @{term "comp"}. We first establish some lemmas. |
99 @{term "comp"}. We first establish some lemmas. |
102 *}; |
100 *}; |
103 |
101 |
104 |
|
105 lemma exec_append: |
102 lemma exec_append: |
106 "ALL stack. exec (xs @ ys) stack env = exec ys (exec xs stack env) env" (is "??P xs"); |
103 "ALL stack. exec (xs @ ys) stack env = exec ys (exec xs stack env) env" (is "??P xs"); |
107 proof (induct ??P xs type: list); |
104 proof (induct ??P xs type: list); |
108 show "??P []"; by simp; |
105 show "??P []"; by simp; |
109 |
106 |
117 next; |
114 next; |
118 fix fun; show "??Q (Apply fun)"; by brute_force; |
115 fix fun; show "??Q (Apply fun)"; by brute_force; |
119 qed; |
116 qed; |
120 qed; |
117 qed; |
121 |
118 |
122 |
|
123 lemma exec_comp: |
119 lemma exec_comp: |
124 "ALL stack. exec (comp e) stack env = eval e env # stack" (is "??P e"); |
120 "ALL stack. exec (comp e) stack env = eval e env # stack" (is "??P e"); |
125 proof (induct ??P e type: expr); |
121 proof (induct ??P e type: expr); |
126 |
122 |
127 fix adr; show "??P (Variable adr)"; by brute_force; |
123 fix adr; show "??P (Variable adr)"; by brute_force; |
131 fix fun e1 e2; assume "??P e1" "??P e2"; show "??P (Binop fun e1 e2)"; |
127 fix fun e1 e2; assume "??P e1" "??P e2"; show "??P (Binop fun e1 e2)"; |
132 by (brute_force simp add: exec_append); |
128 by (brute_force simp add: exec_append); |
133 qed; |
129 qed; |
134 |
130 |
135 |
131 |
136 text "Main theorem ahead."; |
132 text {* Main theorem ahead. *}; |
137 |
133 |
138 theorem correctness: "execute (comp e) env = eval e env"; |
134 theorem correctness: "execute (comp e) env = eval e env"; |
139 by (simp add: execute_def exec_comp); |
135 by (simp add: execute_def exec_comp); |
140 |
136 |
141 |
137 |