9549
|
1 |
(* Title: HOL/MicroJava/BV/Step.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Gerwin Klein
|
|
4 |
Copyright 2000 Technische Universitaet Muenchen
|
|
5 |
*)
|
|
6 |
|
|
7 |
header {* Effect of instructions on the state type *}
|
|
8 |
|
|
9 |
|
|
10 |
theory Step = Convert :
|
|
11 |
|
|
12 |
|
9559
|
13 |
text "Effect of instruction on the state type"
|
9549
|
14 |
consts
|
9580
|
15 |
step :: "instr \<times> jvm_prog \<times> state_type \<Rightarrow> state_type option"
|
9549
|
16 |
|
|
17 |
recdef step "{}"
|
|
18 |
"step (Load idx, G, (ST, LT)) = Some (the (LT ! idx) # ST, LT)"
|
|
19 |
"step (Store idx, G, (ts#ST, LT)) = Some (ST, LT[idx:= Some ts])"
|
|
20 |
"step (Bipush i, G, (ST, LT)) = Some (PrimT Integer # ST, LT)"
|
|
21 |
"step (Aconst_null, G, (ST, LT)) = Some (NT#ST,LT)"
|
|
22 |
"step (Getfield F C, G, (oT#ST, LT)) = Some (snd (the (field (G,C) F)) # ST, LT)"
|
|
23 |
"step (Putfield F C, G, (vT#oT#ST, LT)) = Some (ST,LT)"
|
|
24 |
"step (New C, G, (ST,LT)) = Some (Class C # ST, LT)"
|
|
25 |
"step (Checkcast C, G, (RefT rt#ST,LT)) = Some (Class C # ST,LT)"
|
|
26 |
"step (Pop, G, (ts#ST,LT)) = Some (ST,LT)"
|
|
27 |
"step (Dup, G, (ts#ST,LT)) = Some (ts#ts#ST,LT)"
|
|
28 |
"step (Dup_x1, G, (ts1#ts2#ST,LT)) = Some (ts1#ts2#ts1#ST,LT)"
|
|
29 |
"step (Dup_x2, G, (ts1#ts2#ts3#ST,LT)) = Some (ts1#ts2#ts3#ts1#ST,LT)"
|
|
30 |
"step (Swap, G, (ts1#ts2#ST,LT)) = Some (ts2#ts1#ST,LT)"
|
|
31 |
"step (IAdd, G, (PrimT Integer#PrimT Integer#ST,LT))
|
|
32 |
= Some (PrimT Integer#ST,LT)"
|
|
33 |
"step (Ifcmpeq b, G, (ts1#ts2#ST,LT)) = Some (ST,LT)"
|
|
34 |
"step (Goto b, G, s) = Some s"
|
|
35 |
"step (Return, G, (T#ST,LT)) = None" (* Return has no successor instruction in the same method *)
|
|
36 |
"step (Invoke C mn fpTs, G, (ST,LT)) = (let ST' = drop (length fpTs) ST in
|
|
37 |
Some (fst (snd (the (method (G,C) (mn,fpTs))))#(tl ST'),LT))"
|
|
38 |
|
|
39 |
"step (i,G,s) = None"
|
|
40 |
|
|
41 |
|
9559
|
42 |
text "Conditions under which step is applicable"
|
9549
|
43 |
consts
|
9580
|
44 |
app :: "instr \<times> jvm_prog \<times> ty \<times> state_type \<Rightarrow> bool"
|
9549
|
45 |
|
|
46 |
recdef app "{}"
|
9580
|
47 |
"app (Load idx, G, rT, s) = (idx < length (snd s) \<and> (snd s) ! idx \<noteq> None)"
|
9549
|
48 |
"app (Store idx, G, rT, (ts#ST, LT)) = (idx < length LT)"
|
|
49 |
"app (Bipush i, G, rT, s) = True"
|
|
50 |
"app (Aconst_null, G, rT, s) = True"
|
9580
|
51 |
"app (Getfield F C, G, rT, (oT#ST, LT)) = (is_class G C \<and>
|
|
52 |
field (G,C) F \<noteq> None \<and>
|
|
53 |
fst (the (field (G,C) F)) = C \<and>
|
|
54 |
G \<turnstile> oT \<preceq> (Class C))"
|
|
55 |
"app (Putfield F C, G, rT, (vT#oT#ST, LT)) = (is_class G C \<and>
|
|
56 |
field (G,C) F \<noteq> None \<and>
|
|
57 |
fst (the (field (G,C) F)) = C \<and>
|
|
58 |
G \<turnstile> oT \<preceq> (Class C) \<and>
|
|
59 |
G \<turnstile> vT \<preceq> (snd (the (field (G,C) F))))"
|
9549
|
60 |
"app (New C, G, rT, s) = (is_class G C)"
|
|
61 |
"app (Checkcast C, G, rT, (RefT rt#ST,LT)) = (is_class G C)"
|
|
62 |
"app (Pop, G, rT, (ts#ST,LT)) = True"
|
|
63 |
"app (Dup, G, rT, (ts#ST,LT)) = True"
|
|
64 |
"app (Dup_x1, G, rT, (ts1#ts2#ST,LT)) = True"
|
|
65 |
"app (Dup_x2, G, rT, (ts1#ts2#ts3#ST,LT)) = True"
|
|
66 |
"app (Swap, G, rT, (ts1#ts2#ST,LT)) = True"
|
|
67 |
"app (IAdd, G, rT, (PrimT Integer#PrimT Integer#ST,LT))
|
|
68 |
= True"
|
9580
|
69 |
"app (Ifcmpeq b, G, rT, (ts1#ts2#ST,LT)) = ((\<exists> p. ts1 = PrimT p \<and> ts1 = PrimT p) \<or>
|
|
70 |
(\<exists>r r'. ts1 = RefT r \<and> ts2 = RefT r'))"
|
9549
|
71 |
"app (Goto b, G, rT, s) = True"
|
9580
|
72 |
"app (Return, G, rT, (T#ST,LT)) = (G \<turnstile> T \<preceq> rT)"
|
9549
|
73 |
app_invoke:
|
9580
|
74 |
"app (Invoke C mn fpTs, G, rT, s) = (length fpTs < length (fst s) \<and>
|
9549
|
75 |
(let
|
|
76 |
apTs = rev (take (length fpTs) (fst s));
|
|
77 |
X = hd (drop (length fpTs) (fst s))
|
|
78 |
in
|
9580
|
79 |
G \<turnstile> X \<preceq> Class C \<and>
|
|
80 |
(\<forall>(aT,fT)\<in>set(zip apTs fpTs). G \<turnstile> aT \<preceq> fT) \<and>
|
|
81 |
method (G,C) (mn,fpTs) \<noteq> None
|
9549
|
82 |
))"
|
|
83 |
|
|
84 |
"app (i,G,rT,s) = False"
|
|
85 |
|
|
86 |
|
9559
|
87 |
text {* \isa{p_count} of successor instructions *}
|
|
88 |
|
9549
|
89 |
consts
|
9580
|
90 |
succs :: "instr \<Rightarrow> p_count \<Rightarrow> p_count set"
|
9549
|
91 |
|
|
92 |
primrec
|
|
93 |
"succs (Load idx) pc = {pc+1}"
|
|
94 |
"succs (Store idx) pc = {pc+1}"
|
|
95 |
"succs (Bipush i) pc = {pc+1}"
|
|
96 |
"succs (Aconst_null) pc = {pc+1}"
|
|
97 |
"succs (Getfield F C) pc = {pc+1}"
|
|
98 |
"succs (Putfield F C) pc = {pc+1}"
|
|
99 |
"succs (New C) pc = {pc+1}"
|
|
100 |
"succs (Checkcast C) pc = {pc+1}"
|
|
101 |
"succs Pop pc = {pc+1}"
|
|
102 |
"succs Dup pc = {pc+1}"
|
|
103 |
"succs Dup_x1 pc = {pc+1}"
|
|
104 |
"succs Dup_x2 pc = {pc+1}"
|
|
105 |
"succs Swap pc = {pc+1}"
|
|
106 |
"succs IAdd pc = {pc+1}"
|
|
107 |
"succs (Ifcmpeq b) pc = {pc+1, nat (int pc + b)}"
|
|
108 |
"succs (Goto b) pc = {nat (int pc + b)}"
|
|
109 |
"succs Return pc = {}"
|
|
110 |
"succs (Invoke C mn fpTs) pc = {pc+1}"
|
|
111 |
|
|
112 |
|
9580
|
113 |
lemma 1: "2 < length a \<Longrightarrow> (\<exists>l l' l'' ls. a = l#l'#l''#ls)"
|
9549
|
114 |
proof (cases a)
|
|
115 |
fix x xs assume "a = x#xs" "2 < length a"
|
|
116 |
thus ?thesis by - (cases xs, simp, cases "tl xs", auto)
|
|
117 |
qed auto
|
|
118 |
|
9580
|
119 |
lemma 2: "\<not>(2 < length a) \<Longrightarrow> a = [] \<or> (\<exists> l. a = [l]) \<or> (\<exists> l l'. a = [l,l'])"
|
9549
|
120 |
proof -;
|
9580
|
121 |
assume "\<not>(2 < length a)"
|
9549
|
122 |
hence "length a < (Suc 2)" by simp
|
9580
|
123 |
hence * : "length a = 0 \<or> length a = 1 \<or> length a = 2" by (auto simp add: less_Suc_eq)
|
9549
|
124 |
|
|
125 |
{
|
|
126 |
fix x
|
|
127 |
assume "length x = 1"
|
9580
|
128 |
hence "\<exists> l. x = [l]" by - (cases x, auto)
|
9549
|
129 |
} note 0 = this
|
|
130 |
|
9580
|
131 |
have "length a = 2 \<Longrightarrow> \<exists>l l'. a = [l,l']" by (cases a, auto dest: 0)
|
9549
|
132 |
with * show ?thesis by (auto dest: 0)
|
|
133 |
qed
|
|
134 |
|
9559
|
135 |
text {*
|
|
136 |
\mdeskip
|
|
137 |
simp rules for \isa{app} without patterns, better suited for proofs
|
|
138 |
\medskip
|
|
139 |
*}
|
|
140 |
|
9549
|
141 |
lemma appStore[simp]:
|
9580
|
142 |
"app (Store idx, G, rT, s) = (\<exists> ts ST LT. s = (ts#ST,LT) \<and> idx < length LT)"
|
9549
|
143 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
144 |
|
|
145 |
|
|
146 |
lemma appGetField[simp]:
|
9580
|
147 |
"app (Getfield F C, G, rT, s) = (\<exists> oT ST LT. s = (oT#ST, LT) \<and> is_class G C \<and>
|
|
148 |
fst (the (field (G,C) F)) = C \<and>
|
|
149 |
field (G,C) F \<noteq> None \<and> G \<turnstile> oT \<preceq> (Class C))"
|
9549
|
150 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
151 |
|
|
152 |
|
|
153 |
lemma appPutField[simp]:
|
9580
|
154 |
"app (Putfield F C, G, rT, s) = (\<exists> vT oT ST LT. s = (vT#oT#ST, LT) \<and> is_class G C \<and>
|
|
155 |
field (G,C) F \<noteq> None \<and> fst (the (field (G,C) F)) = C \<and>
|
|
156 |
G \<turnstile> oT \<preceq> (Class C) \<and>
|
|
157 |
G \<turnstile> vT \<preceq> (snd (the (field (G,C) F))))"
|
9549
|
158 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
159 |
|
|
160 |
|
|
161 |
lemma appCheckcast[simp]:
|
9580
|
162 |
"app (Checkcast C, G, rT, s) = (\<exists>rT ST LT. s = (RefT rT#ST,LT) \<and> is_class G C)"
|
9549
|
163 |
by (cases s, cases "fst s", simp, cases "hd (fst s)", auto)
|
|
164 |
|
|
165 |
lemma appPop[simp]:
|
9580
|
166 |
"app (Pop, G, rT, s) = (\<exists>ts ST LT. s = (ts#ST,LT))"
|
9549
|
167 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
168 |
|
|
169 |
|
|
170 |
lemma appDup[simp]:
|
9580
|
171 |
"app (Dup, G, rT, s) = (\<exists>ts ST LT. s = (ts#ST,LT))"
|
9549
|
172 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
173 |
|
|
174 |
|
|
175 |
lemma appDup_x1[simp]:
|
9580
|
176 |
"app (Dup_x1, G, rT, s) = (\<exists>ts1 ts2 ST LT. s = (ts1#ts2#ST,LT))"
|
9549
|
177 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
178 |
|
|
179 |
|
|
180 |
lemma appDup_x2[simp]:
|
9580
|
181 |
"app (Dup_x2, G, rT, s) = (\<exists>ts1 ts2 ts3 ST LT. s = (ts1#ts2#ts3#ST,LT))"
|
9549
|
182 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
183 |
|
|
184 |
|
|
185 |
lemma appSwap[simp]:
|
9580
|
186 |
"app (Swap, G, rT, s) = (\<exists>ts1 ts2 ST LT. s = (ts1#ts2#ST,LT))"
|
9549
|
187 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
188 |
|
|
189 |
|
|
190 |
lemma appIAdd[simp]:
|
9580
|
191 |
"app (IAdd, G, rT, s) = (\<exists> ST LT. s = (PrimT Integer#PrimT Integer#ST,LT))" (is "?app s = ?P s")
|
9549
|
192 |
proof (cases s)
|
|
193 |
case Pair
|
|
194 |
have "?app (a,b) = ?P (a,b)"
|
|
195 |
proof (cases "a")
|
|
196 |
fix t ts assume a: "a = t#ts"
|
|
197 |
show ?thesis
|
|
198 |
proof (cases t)
|
|
199 |
fix p assume p: "t = PrimT p"
|
|
200 |
show ?thesis
|
|
201 |
proof (cases p)
|
|
202 |
assume ip: "p = Integer"
|
|
203 |
show ?thesis
|
|
204 |
proof (cases ts)
|
|
205 |
fix t' ts' assume t': "ts = t' # ts'"
|
|
206 |
show ?thesis
|
|
207 |
proof (cases t')
|
|
208 |
fix p' assume "t' = PrimT p'"
|
|
209 |
with t' ip p a
|
|
210 |
show ?thesis by - (cases p', auto)
|
|
211 |
qed (auto simp add: a p ip t')
|
|
212 |
qed (auto simp add: a p ip)
|
|
213 |
qed (auto simp add: a p)
|
|
214 |
qed (auto simp add: a)
|
|
215 |
qed auto
|
|
216 |
with Pair show ?thesis by simp
|
|
217 |
qed
|
|
218 |
|
|
219 |
|
|
220 |
lemma appIfcmpeq[simp]:
|
9580
|
221 |
"app (Ifcmpeq b, G, rT, s) = (\<exists>ts1 ts2 ST LT. s = (ts1#ts2#ST,LT) \<and>
|
|
222 |
((\<exists> p. ts1 = PrimT p \<and> ts1 = PrimT p) \<or>
|
|
223 |
(\<exists>r r'. ts1 = RefT r \<and> ts2 = RefT r')))"
|
9549
|
224 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
225 |
|
|
226 |
|
|
227 |
lemma appReturn[simp]:
|
9580
|
228 |
"app (Return, G, rT, s) = (\<exists>T ST LT. s = (T#ST,LT) \<and> (G \<turnstile> T \<preceq> rT))"
|
9549
|
229 |
by (cases s, cases "2 < length (fst s)", auto dest: 1 2)
|
|
230 |
|
|
231 |
|
|
232 |
lemma appInvoke[simp]:
|
9580
|
233 |
"app (Invoke C mn fpTs, G, rT, s) = (\<exists>apTs X ST LT.
|
|
234 |
s = ((rev apTs) @ (X # ST), LT) \<and>
|
|
235 |
length apTs = length fpTs \<and>
|
|
236 |
G \<turnstile> X \<preceq> Class C \<and>
|
|
237 |
(\<forall>(aT,fT)\<in>set(zip apTs fpTs). G \<turnstile> aT \<preceq> fT) \<and>
|
|
238 |
method (G,C) (mn,fpTs) \<noteq> None)" (is "?app s = ?P s")
|
9549
|
239 |
proof (cases s)
|
|
240 |
case Pair
|
9580
|
241 |
have "?app (a,b) \<Longrightarrow> ?P (a,b)"
|
9549
|
242 |
proof -
|
|
243 |
assume app: "?app (a,b)"
|
9580
|
244 |
hence "a = (rev (rev (take (length fpTs) a))) @ (drop (length fpTs) a) \<and> length fpTs < length a"
|
|
245 |
(is "?a \<and> ?l") by auto
|
|
246 |
hence "?a \<and> 0 < length (drop (length fpTs) a)" (is "?a \<and> ?l") by auto
|
|
247 |
hence "?a \<and> ?l \<and> length (rev (take (length fpTs) a)) = length fpTs" by (auto simp add: min_def)
|
|
248 |
hence "\<exists>apTs ST. a = rev apTs @ ST \<and> length apTs = length fpTs \<and> 0 < length ST" by blast
|
|
249 |
hence "\<exists>apTs ST. a = rev apTs @ ST \<and> length apTs = length fpTs \<and> ST \<noteq> []" by blast
|
|
250 |
hence "\<exists>apTs ST. a = rev apTs @ ST \<and> length apTs = length fpTs \<and> (\<exists>X ST'. ST = X#ST')" by (simp add: neq_Nil_conv)
|
|
251 |
hence "\<exists>apTs X ST. a = rev apTs @ X # ST \<and> length apTs = length fpTs" by blast
|
9549
|
252 |
with app
|
|
253 |
show ?thesis by auto blast
|
|
254 |
qed
|
9580
|
255 |
with Pair have "?app s \<Longrightarrow> ?P s" by simp
|
9549
|
256 |
thus ?thesis by auto
|
|
257 |
qed
|
|
258 |
|
|
259 |
lemmas [simp del] = app_invoke
|
|
260 |
|
9580
|
261 |
|
|
262 |
lemma app_step_some:
|
|
263 |
"\<lbrakk>app (i,G,rT,s); succs i pc \<noteq> {}\<rbrakk> \<Longrightarrow> step (i,G,s) \<noteq> None";
|
|
264 |
by (cases s, cases i, auto)
|
|
265 |
|
9549
|
266 |
end
|