nipkow@43982
|
1 |
(* Author: Tobias Nipkow *)
|
nipkow@10343
|
2 |
|
nipkow@43982
|
3 |
header "A Compiler for IMP"
|
nipkow@10342
|
4 |
|
nipkow@43982
|
5 |
theory Compiler imports Big_Step
|
nipkow@43982
|
6 |
begin
|
kleing@12429
|
7 |
|
nipkow@43982
|
8 |
subsection "Instructions and Stack Machine"
|
nipkow@43982
|
9 |
|
nipkow@43982
|
10 |
datatype instr =
|
nipkow@43982
|
11 |
LOADI int | LOAD string | ADD |
|
nipkow@43982
|
12 |
STORE string |
|
nipkow@43982
|
13 |
JMPF nat |
|
nipkow@43982
|
14 |
JMPB nat |
|
nipkow@43982
|
15 |
JMPFLESS nat |
|
nipkow@43982
|
16 |
JMPFGE nat
|
nipkow@43982
|
17 |
|
nipkow@43982
|
18 |
type_synonym stack = "int list"
|
nipkow@43982
|
19 |
type_synonym config = "nat\<times>state\<times>stack"
|
nipkow@43982
|
20 |
|
nipkow@43982
|
21 |
abbreviation "hd2 xs == hd(tl xs)"
|
nipkow@43982
|
22 |
abbreviation "tl2 xs == tl(tl xs)"
|
nipkow@43982
|
23 |
|
nipkow@43982
|
24 |
inductive exec1 :: "instr list \<Rightarrow> config \<Rightarrow> config \<Rightarrow> bool"
|
nipkow@43982
|
25 |
("(_/ \<turnstile> (_ \<rightarrow>/ _))" [50,0,0] 50)
|
nipkow@43982
|
26 |
for P :: "instr list"
|
wenzelm@27362
|
27 |
where
|
nipkow@43982
|
28 |
"\<lbrakk> i < size P; P!i = LOADI n \<rbrakk> \<Longrightarrow>
|
nipkow@43982
|
29 |
P \<turnstile> (i,s,stk) \<rightarrow> (i+1,s, n#stk)" |
|
nipkow@43982
|
30 |
"\<lbrakk> i < size P; P!i = LOAD x \<rbrakk> \<Longrightarrow>
|
nipkow@43982
|
31 |
P \<turnstile> (i,s,stk) \<rightarrow> (i+1,s, s x # stk)" |
|
nipkow@43982
|
32 |
"\<lbrakk> i < size P; P!i = ADD \<rbrakk> \<Longrightarrow>
|
nipkow@43982
|
33 |
P \<turnstile> (i,s,stk) \<rightarrow> (i+1,s, (hd2 stk + hd stk) # tl2 stk)" |
|
nipkow@43982
|
34 |
"\<lbrakk> i < size P; P!i = STORE n \<rbrakk> \<Longrightarrow>
|
nipkow@43982
|
35 |
P \<turnstile> (i,s,stk) \<rightarrow> (i+1,s(n := hd stk),tl stk)" |
|
nipkow@43982
|
36 |
"\<lbrakk> i < size P; P!i = JMPF n \<rbrakk> \<Longrightarrow>
|
nipkow@43982
|
37 |
P \<turnstile> (i,s,stk) \<rightarrow> (i+1+n,s,stk)" |
|
nipkow@43982
|
38 |
"\<lbrakk> i < size P; P!i = JMPB n; n \<le> i+1 \<rbrakk> \<Longrightarrow>
|
nipkow@43982
|
39 |
P \<turnstile> (i,s,stk) \<rightarrow> (i+1-n,s,stk)" |
|
nipkow@43982
|
40 |
"\<lbrakk> i < size P; P!i = JMPFLESS n \<rbrakk> \<Longrightarrow>
|
nipkow@43982
|
41 |
P \<turnstile> (i,s,stk) \<rightarrow> (if hd2 stk < hd stk then i+1+n else i+1,s,tl2 stk)" |
|
nipkow@43982
|
42 |
"\<lbrakk> i < size P; P!i = JMPFGE n \<rbrakk> \<Longrightarrow>
|
nipkow@43982
|
43 |
P \<turnstile> (i,s,stk) \<rightarrow> (if hd2 stk >= hd stk then i+1+n else i+1,s,tl2 stk)"
|
nipkow@10342
|
44 |
|
nipkow@43982
|
45 |
code_pred exec1 .
|
nipkow@11275
|
46 |
|
nipkow@43982
|
47 |
declare exec1.intros[intro]
|
nipkow@43982
|
48 |
|
nipkow@43982
|
49 |
inductive exec :: "instr list \<Rightarrow> config \<Rightarrow> config \<Rightarrow> bool" ("_/ \<turnstile> (_ \<rightarrow>*/ _)" 50)
|
nipkow@43982
|
50 |
where
|
nipkow@43982
|
51 |
refl: "P \<turnstile> c \<rightarrow>* c" |
|
nipkow@43982
|
52 |
step: "P \<turnstile> c \<rightarrow> c' \<Longrightarrow> P \<turnstile> c' \<rightarrow>* c'' \<Longrightarrow> P \<turnstile> c \<rightarrow>* c''"
|
nipkow@43982
|
53 |
|
nipkow@43982
|
54 |
declare exec.intros[intro]
|
nipkow@43982
|
55 |
|
nipkow@43982
|
56 |
lemmas exec_induct = exec.induct[split_format(complete)]
|
nipkow@43982
|
57 |
|
nipkow@43982
|
58 |
code_pred exec .
|
nipkow@43982
|
59 |
|
nipkow@43982
|
60 |
values
|
nipkow@43982
|
61 |
"{(i,map t [''x'',''y''],stk) | i t stk.
|
nipkow@43982
|
62 |
[LOAD ''y'', STORE ''x''] \<turnstile>
|
nipkow@43982
|
63 |
(0,lookup[(''x'',3),(''y'',4)],[]) \<rightarrow>* (i,t,stk)}"
|
nipkow@43982
|
64 |
|
nipkow@43982
|
65 |
|
nipkow@43982
|
66 |
subsection{* Verification infrastructure *}
|
nipkow@43982
|
67 |
|
nipkow@43982
|
68 |
lemma exec_trans: "P \<turnstile> c \<rightarrow>* c' \<Longrightarrow> P \<turnstile> c' \<rightarrow>* c'' \<Longrightarrow> P \<turnstile> c \<rightarrow>* c''"
|
nipkow@43982
|
69 |
apply(induct rule: exec.induct)
|
nipkow@43982
|
70 |
apply blast
|
nipkow@43982
|
71 |
by (metis exec.step)
|
nipkow@43982
|
72 |
|
nipkow@43982
|
73 |
lemma exec1_subst: "P \<turnstile> c \<rightarrow> c' \<Longrightarrow> c' = c'' \<Longrightarrow> P \<turnstile> c \<rightarrow> c''"
|
nipkow@43982
|
74 |
by auto
|
nipkow@43982
|
75 |
|
nipkow@43982
|
76 |
lemmas exec1_simps = exec1.intros[THEN exec1_subst]
|
nipkow@43982
|
77 |
|
nipkow@43982
|
78 |
text{* Below we need to argue about the execution of code that is embedded in
|
nipkow@43982
|
79 |
larger programs. For this purpose we show that execution is preserved by
|
nipkow@43982
|
80 |
appending code to the left or right of a program. *}
|
nipkow@43982
|
81 |
|
nipkow@43982
|
82 |
lemma exec1_appendR: assumes "P \<turnstile> c \<rightarrow> c'" shows "P@P' \<turnstile> c \<rightarrow> c'"
|
nipkow@43982
|
83 |
proof-
|
nipkow@43982
|
84 |
from assms show ?thesis
|
nipkow@43982
|
85 |
by cases (simp_all add: exec1_simps nth_append)
|
nipkow@43982
|
86 |
-- "All cases proved with the final simp-all"
|
nipkow@12275
|
87 |
qed
|
nipkow@11275
|
88 |
|
nipkow@43982
|
89 |
lemma exec_appendR: "P \<turnstile> c \<rightarrow>* c' \<Longrightarrow> P@P' \<turnstile> c \<rightarrow>* c'"
|
nipkow@43982
|
90 |
apply(induct rule: exec.induct)
|
nipkow@43982
|
91 |
apply blast
|
nipkow@43982
|
92 |
by (metis exec1_appendR exec.step)
|
nipkow@13095
|
93 |
|
nipkow@43982
|
94 |
lemma exec1_appendL:
|
nipkow@43982
|
95 |
assumes "P \<turnstile> (i,s,stk) \<rightarrow> (i',s',stk')"
|
nipkow@43982
|
96 |
shows "P' @ P \<turnstile> (size(P')+i,s,stk) \<rightarrow> (size(P')+i',s',stk')"
|
nipkow@43982
|
97 |
proof-
|
nipkow@43982
|
98 |
from assms show ?thesis
|
nipkow@43982
|
99 |
by cases (simp_all add: exec1_simps)
|
nipkow@43982
|
100 |
qed
|
nipkow@13095
|
101 |
|
nipkow@43982
|
102 |
lemma exec_appendL:
|
nipkow@43982
|
103 |
"P \<turnstile> (i,s,stk) \<rightarrow>* (i',s',stk') \<Longrightarrow>
|
nipkow@43982
|
104 |
P' @ P \<turnstile> (size(P')+i,s,stk) \<rightarrow>* (size(P')+i',s',stk')"
|
nipkow@43982
|
105 |
apply(induct rule: exec_induct)
|
nipkow@43982
|
106 |
apply blast
|
nipkow@43982
|
107 |
by (blast intro: exec1_appendL exec.step)
|
nipkow@43982
|
108 |
|
nipkow@43982
|
109 |
text{* Now we specialise the above lemmas to enable automatic proofs of
|
nipkow@43982
|
110 |
@{prop "P \<turnstile> c \<rightarrow>* c'"} where @{text P} is a mixture of concrete instructions and
|
nipkow@43982
|
111 |
pieces of code that we already know how they execute (by induction), combined
|
nipkow@43982
|
112 |
by @{text "@"} and @{text "#"}. Backward jumps are not supported.
|
nipkow@43982
|
113 |
The details should be skipped on a first reading.
|
nipkow@43982
|
114 |
|
nipkow@43982
|
115 |
If the pc points beyond the first instruction or part of the program, drop it: *}
|
nipkow@43982
|
116 |
|
nipkow@43982
|
117 |
lemma exec_Cons_Suc[intro]:
|
nipkow@43982
|
118 |
"P \<turnstile> (i,s,stk) \<rightarrow>* (j,t,stk') \<Longrightarrow>
|
nipkow@43982
|
119 |
instr#P \<turnstile> (Suc i,s,stk) \<rightarrow>* (Suc j,t,stk')"
|
nipkow@43982
|
120 |
apply(drule exec_appendL[where P'="[instr]"])
|
nipkow@13095
|
121 |
apply simp
|
nipkow@10342
|
122 |
done
|
nipkow@10342
|
123 |
|
nipkow@43982
|
124 |
lemma exec_appendL_if[intro]:
|
nipkow@43982
|
125 |
"size P' <= i
|
nipkow@43982
|
126 |
\<Longrightarrow> P \<turnstile> (i - size P',s,stk) \<rightarrow>* (i',s',stk')
|
nipkow@43982
|
127 |
\<Longrightarrow> P' @ P \<turnstile> (i,s,stk) \<rightarrow>* (size P' + i',s',stk')"
|
nipkow@43982
|
128 |
apply(drule exec_appendL[where P'=P'])
|
nipkow@13095
|
129 |
apply simp
|
nipkow@13095
|
130 |
done
|
nipkow@13095
|
131 |
|
nipkow@43982
|
132 |
text{* Split the execution of a compound program up into the excution of its
|
nipkow@43982
|
133 |
parts: *}
|
nipkow@13095
|
134 |
|
nipkow@43982
|
135 |
lemma exec_append_trans[intro]:
|
nipkow@43982
|
136 |
"P \<turnstile> (0,s,stk) \<rightarrow>* (i',s',stk') \<Longrightarrow>
|
nipkow@43982
|
137 |
size P \<le> i' \<Longrightarrow>
|
nipkow@43982
|
138 |
P' \<turnstile> (i' - size P,s',stk') \<rightarrow>* (i'',s'',stk'') \<Longrightarrow>
|
nipkow@43982
|
139 |
j'' = size P + i''
|
nipkow@43982
|
140 |
\<Longrightarrow>
|
nipkow@43982
|
141 |
P @ P' \<turnstile> (0,s,stk) \<rightarrow>* (j'',s'',stk'')"
|
nipkow@43982
|
142 |
by(metis exec_trans[OF exec_appendR exec_appendL_if])
|
nipkow@13095
|
143 |
|
nipkow@43982
|
144 |
|
nipkow@43982
|
145 |
declare Let_def[simp] eval_nat_numeral[simp]
|
nipkow@43982
|
146 |
|
nipkow@43982
|
147 |
|
nipkow@43982
|
148 |
subsection "Compilation"
|
nipkow@43982
|
149 |
|
nipkow@43982
|
150 |
fun acomp :: "aexp \<Rightarrow> instr list" where
|
nipkow@43982
|
151 |
"acomp (N n) = [LOADI n]" |
|
nipkow@43982
|
152 |
"acomp (V x) = [LOAD x]" |
|
nipkow@43982
|
153 |
"acomp (Plus a1 a2) = acomp a1 @ acomp a2 @ [ADD]"
|
nipkow@43982
|
154 |
|
nipkow@43982
|
155 |
lemma acomp_correct[intro]:
|
nipkow@43982
|
156 |
"acomp a \<turnstile> (0,s,stk) \<rightarrow>* (size(acomp a),s,aval a s#stk)"
|
nipkow@43982
|
157 |
apply(induct a arbitrary: stk)
|
nipkow@43982
|
158 |
apply(fastsimp)+
|
nipkow@13095
|
159 |
done
|
nipkow@13095
|
160 |
|
nipkow@43982
|
161 |
fun bcomp :: "bexp \<Rightarrow> bool \<Rightarrow> nat \<Rightarrow> instr list" where
|
nipkow@43982
|
162 |
"bcomp (B bv) c n = (if bv=c then [JMPF n] else [])" |
|
nipkow@43982
|
163 |
"bcomp (Not b) c n = bcomp b (\<not>c) n" |
|
nipkow@43982
|
164 |
"bcomp (And b1 b2) c n =
|
nipkow@43982
|
165 |
(let cb2 = bcomp b2 c n;
|
nipkow@43982
|
166 |
m = (if c then size cb2 else size cb2+n);
|
nipkow@43982
|
167 |
cb1 = bcomp b1 False m
|
nipkow@43982
|
168 |
in cb1 @ cb2)" |
|
nipkow@43982
|
169 |
"bcomp (Less a1 a2) c n =
|
nipkow@43982
|
170 |
acomp a1 @ acomp a2 @ (if c then [JMPFLESS n] else [JMPFGE n])"
|
nipkow@43982
|
171 |
|
nipkow@43982
|
172 |
value
|
nipkow@43982
|
173 |
"bcomp (And (Less (V ''x'') (V ''y'')) (Not(Less (V ''u'') (V ''v''))))
|
nipkow@43982
|
174 |
False 3"
|
nipkow@43982
|
175 |
|
nipkow@43982
|
176 |
lemma bcomp_correct[intro]:
|
nipkow@43982
|
177 |
"bcomp b c n \<turnstile>
|
nipkow@43982
|
178 |
(0,s,stk) \<rightarrow>* (size(bcomp b c n) + (if c = bval b s then n else 0),s,stk)"
|
nipkow@43982
|
179 |
proof(induct b arbitrary: c n m)
|
nipkow@43982
|
180 |
case Not
|
nipkow@43982
|
181 |
from Not[of "~c"] show ?case by fastsimp
|
nipkow@13095
|
182 |
next
|
nipkow@43982
|
183 |
case (And b1 b2)
|
nipkow@43982
|
184 |
from And(1)[of "False"] And(2)[of "c"] show ?case by fastsimp
|
nipkow@43982
|
185 |
qed fastsimp+
|
nipkow@13095
|
186 |
|
nipkow@13095
|
187 |
|
nipkow@43982
|
188 |
fun ccomp :: "com \<Rightarrow> instr list" where
|
nipkow@43982
|
189 |
"ccomp SKIP = []" |
|
nipkow@43982
|
190 |
"ccomp (x ::= a) = acomp a @ [STORE x]" |
|
nipkow@43982
|
191 |
"ccomp (c\<^isub>1;c\<^isub>2) = ccomp c\<^isub>1 @ ccomp c\<^isub>2" |
|
nipkow@43982
|
192 |
"ccomp (IF b THEN c\<^isub>1 ELSE c\<^isub>2) =
|
nipkow@43982
|
193 |
(let cc\<^isub>1 = ccomp c\<^isub>1; cc\<^isub>2 = ccomp c\<^isub>2; cb = bcomp b False (size cc\<^isub>1 + 1)
|
nipkow@43982
|
194 |
in cb @ cc\<^isub>1 @ JMPF(size cc\<^isub>2) # cc\<^isub>2)" |
|
nipkow@43982
|
195 |
"ccomp (WHILE b DO c) =
|
nipkow@43982
|
196 |
(let cc = ccomp c; cb = bcomp b False (size cc + 1)
|
nipkow@43982
|
197 |
in cb @ cc @ [JMPB (size cb + size cc + 1)])"
|
nipkow@13095
|
198 |
|
nipkow@43982
|
199 |
value "ccomp
|
nipkow@43982
|
200 |
(IF Less (V ''u'') (N 1) THEN ''u'' ::= Plus (V ''u'') (N 1)
|
nipkow@43982
|
201 |
ELSE ''v'' ::= V ''u'')"
|
nipkow@13095
|
202 |
|
nipkow@43982
|
203 |
value "ccomp (WHILE Less (V ''u'') (N 1) DO (''u'' ::= Plus (V ''u'') (N 1)))"
|
nipkow@13095
|
204 |
|
nipkow@13095
|
205 |
|
nipkow@43982
|
206 |
subsection "Preservation of sematics"
|
nipkow@43982
|
207 |
|
nipkow@43982
|
208 |
lemma ccomp_correct:
|
nipkow@43982
|
209 |
"(c,s) \<Rightarrow> t \<Longrightarrow> ccomp c \<turnstile> (0,s,stk) \<rightarrow>* (size(ccomp c),t,stk)"
|
nipkow@43982
|
210 |
proof(induct arbitrary: stk rule: big_step_induct)
|
nipkow@43982
|
211 |
case (Assign x a s)
|
nipkow@43982
|
212 |
show ?case by (fastsimp simp:fun_upd_def)
|
nipkow@13095
|
213 |
next
|
nipkow@43982
|
214 |
case (Semi c1 s1 s2 c2 s3)
|
nipkow@43982
|
215 |
let ?cc1 = "ccomp c1" let ?cc2 = "ccomp c2"
|
nipkow@43982
|
216 |
have "?cc1 @ ?cc2 \<turnstile> (0,s1,stk) \<rightarrow>* (size ?cc1,s2,stk)"
|
nipkow@43982
|
217 |
using Semi.hyps(2) by (fastsimp)
|
nipkow@43982
|
218 |
moreover
|
nipkow@43982
|
219 |
have "?cc1 @ ?cc2 \<turnstile> (size ?cc1,s2,stk) \<rightarrow>* (size(?cc1 @ ?cc2),s3,stk)"
|
nipkow@43982
|
220 |
using Semi.hyps(4) by (fastsimp)
|
nipkow@43982
|
221 |
ultimately show ?case by simp (blast intro: exec_trans)
|
nipkow@13095
|
222 |
next
|
nipkow@43982
|
223 |
case (WhileTrue b s1 c s2 s3)
|
nipkow@43982
|
224 |
let ?cc = "ccomp c"
|
nipkow@43982
|
225 |
let ?cb = "bcomp b False (size ?cc + 1)"
|
nipkow@43982
|
226 |
let ?cw = "ccomp(WHILE b DO c)"
|
nipkow@43982
|
227 |
have "?cw \<turnstile> (0,s1,stk) \<rightarrow>* (size ?cb + size ?cc,s2,stk)"
|
nipkow@43982
|
228 |
using WhileTrue(1,3) by fastsimp
|
nipkow@43982
|
229 |
moreover
|
nipkow@43982
|
230 |
have "?cw \<turnstile> (size ?cb + size ?cc,s2,stk) \<rightarrow>* (0,s2,stk)"
|
nipkow@43982
|
231 |
by (fastsimp)
|
nipkow@43982
|
232 |
moreover
|
nipkow@43982
|
233 |
have "?cw \<turnstile> (0,s2,stk) \<rightarrow>* (size ?cw,s3,stk)" by(rule WhileTrue(5))
|
nipkow@43982
|
234 |
ultimately show ?case by(blast intro: exec_trans)
|
nipkow@43982
|
235 |
qed fastsimp+
|
nipkow@13095
|
236 |
|
webertj@20217
|
237 |
end
|