1 (* Title: HOL/MicroJava/BV/JVM.thy
3 Author: Tobias Nipkow, Gerwin Klein
7 header {* \isaheader{LBV for the JVM}\label{sec:JVM} *}
10 imports LBVCorrect LBVComplete Typing_Framework_JVM
13 types prog_cert = "cname \<Rightarrow> sig \<Rightarrow> state list"
16 check_cert :: "jvm_prog \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> state list \<Rightarrow> bool"
17 "check_cert G mxs mxr n cert \<equiv> check_types G mxs mxr cert \<and> length cert = n+1 \<and>
18 (\<forall>i<n. cert!i \<noteq> Err) \<and> cert!n = OK None"
20 lbvjvm :: "jvm_prog \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> ty \<Rightarrow> exception_table \<Rightarrow>
21 state list \<Rightarrow> instr list \<Rightarrow> state \<Rightarrow> state"
22 "lbvjvm G maxs maxr rT et cert bs \<equiv>
23 wtl_inst_list bs cert (JVMType.sup G maxs maxr) (JVMType.le G maxs maxr) Err (OK None) (exec G maxs rT et bs) 0"
25 wt_lbv :: "jvm_prog \<Rightarrow> cname \<Rightarrow> ty list \<Rightarrow> ty \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow>
26 exception_table \<Rightarrow> state list \<Rightarrow> instr list \<Rightarrow> bool"
27 "wt_lbv G C pTs rT mxs mxl et cert ins \<equiv>
28 check_bounded ins et \<and>
29 check_cert G mxs (1+size pTs+mxl) (length ins) cert \<and>
31 (let start = Some ([],(OK (Class C))#((map OK pTs))@(replicate mxl Err));
32 result = lbvjvm G mxs (1+size pTs+mxl) rT et cert ins (OK start)
33 in result \<noteq> Err)"
35 wt_jvm_prog_lbv :: "jvm_prog \<Rightarrow> prog_cert \<Rightarrow> bool"
36 "wt_jvm_prog_lbv G cert \<equiv>
37 wf_prog (\<lambda>G C (sig,rT,(maxs,maxl,b,et)). wt_lbv G C (snd sig) rT maxs maxl et (cert C sig) b) G"
39 mk_cert :: "jvm_prog \<Rightarrow> nat \<Rightarrow> ty \<Rightarrow> exception_table \<Rightarrow> instr list
40 \<Rightarrow> method_type \<Rightarrow> state list"
41 "mk_cert G maxs rT et bs phi \<equiv> make_cert (exec G maxs rT et bs) (map OK phi) (OK None)"
43 prg_cert :: "jvm_prog \<Rightarrow> prog_type \<Rightarrow> prog_cert"
44 "prg_cert G phi C sig \<equiv> let (C,rT,(maxs,maxl,ins,et)) = the (method (G,C) sig) in
45 mk_cert G maxs rT et ins (phi C sig)"
49 fixes pTs and mxl and G and mxs and rT and et and bs and phi
50 defines [simp]: "mxr \<equiv> 1 + length pTs + mxl"
51 defines [simp]: "r \<equiv> sup_state_opt G"
52 defines [simp]: "app0 \<equiv> \<lambda>pc. app (bs!pc) G mxs rT pc et"
53 defines [simp]: "step0 \<equiv> \<lambda>pc. eff (bs!pc) G pc et"
56 "wt_method G C pTs rT mxs mxl bs et phi =
57 (bs \<noteq> [] \<and>
58 length phi = length bs \<and>
59 check_bounded bs et \<and>
60 check_types G mxs mxr (map OK phi) \<and>
61 wt_start G C pTs mxl phi \<and>
62 wt_app_eff r app0 step0 phi)"
63 by (auto simp add: wt_method_def wt_app_eff_def wt_instr_def lesub_def
64 dest: check_bounded_is_bounded boundedD)
68 "check_cert G mxs mxr n cert \<Longrightarrow> cert_ok cert n Err (OK None) (states G mxs mxr)"
69 apply (unfold cert_ok_def check_cert_def check_types_def)
70 apply (auto simp add: list_all_iff)
75 assumes wf: "wf_prog wf_mb G"
76 assumes lbv: "wt_lbv G C pTs rT mxs mxl et cert ins"
77 assumes C: "is_class G C"
78 assumes pTs: "set pTs \<subseteq> types G"
80 defines [simp]: "mxr \<equiv> 1+length pTs+mxl"
82 shows "\<exists>ts \<in> list (size ins) (states G mxs mxr).
83 wt_step (JVMType.le G mxs mxr) Err (exec G mxs rT et ins) ts
84 \<and> OK (Some ([],(OK (Class C))#((map OK pTs))@(replicate mxl Err))) <=_(JVMType.le G mxs mxr) ts!0"
86 let ?step = "exec G mxs rT et ins"
87 let ?r = "JVMType.le G mxs mxr"
88 let ?f = "JVMType.sup G mxs mxr"
89 let ?A = "states G mxs mxr"
91 have "semilat (JVMType.sl G mxs mxr)"
92 by (rule semilat_JVM_slI, rule wf_prog_ws_prog)
93 hence "semilat (?A, ?r, ?f)" by (unfold sl_triple_conv)
95 have "top ?r Err" by (simp add: JVM_le_unfold)
97 have "Err \<in> ?A" by (simp add: JVM_states_unfold)
99 have "bottom ?r (OK None)"
100 by (simp add: JVM_le_unfold bottom_def)
102 have "OK None \<in> ?A" by (simp add: JVM_states_unfold)
105 have "bounded ?step (length ins)"
106 by (clarsimp simp add: wt_lbv_def exec_def)
107 (intro bounded_lift check_bounded_is_bounded)
110 have "cert_ok cert (length ins) Err (OK None) ?A"
111 by (unfold wt_lbv_def) (auto dest: check_certD)
113 have "pres_type ?step (length ins) ?A" by (rule exec_pres_type)
115 let ?start = "OK (Some ([],(OK (Class C))#(map OK pTs)@(replicate mxl Err)))"
117 have "wtl_inst_list ins cert ?f ?r Err (OK None) ?step 0 ?start \<noteq> Err"
118 by (simp add: wt_lbv_def lbvjvm_def)
120 from C pTs have "?start \<in> ?A"
121 by (unfold JVM_states_unfold) (auto intro: list_appendI, force)
123 from lbv have "0 < length ins" by (simp add: wt_lbv_def)
125 show ?thesis by (rule lbvs.wtl_sound_strong)
128 lemma wt_lbv_wt_method:
129 assumes wf: "wf_prog wf_mb G"
130 assumes lbv: "wt_lbv G C pTs rT mxs mxl et cert ins"
131 assumes C: "is_class G C"
132 assumes pTs: "set pTs \<subseteq> types G"
134 shows "\<exists>phi. wt_method G C pTs rT mxs mxl ins et phi"
136 let ?mxr = "1 + length pTs + mxl"
137 let ?step = "exec G mxs rT et ins"
138 let ?r = "JVMType.le G mxs ?mxr"
139 let ?f = "JVMType.sup G mxs ?mxr"
140 let ?A = "states G mxs ?mxr"
141 let ?start = "OK (Some ([],(OK (Class C))#(map OK pTs)@(replicate mxl Err)))"
143 from lbv have l: "ins \<noteq> []" by (simp add: wt_lbv_def)
147 list: "phi \<in> list (length ins) ?A" and
148 step: "wt_step ?r Err ?step phi" and
149 start: "?start <=_?r phi!0"
150 by (blast dest: wt_lbv_wt_step)
151 from list have [simp]: "length phi = length ins" by simp
152 have "length (map ok_val phi) = length ins" by simp
154 from l have 0: "0 < length phi" by simp
155 with step obtain phi0 where "phi!0 = OK phi0"
156 by (unfold wt_step_def) blast
158 have "wt_start G C pTs mxl (map ok_val phi)"
159 by (simp add: wt_start_def JVM_le_Err_conv lesub_def)
161 from lbv have chk_bounded: "check_bounded ins et"
162 by (simp add: wt_lbv_def)
165 have "check_types G mxs ?mxr phi"
166 by (simp add: check_types_def)
168 have [symmetric]: "map OK (map ok_val phi) = phi"
169 by (auto intro!: map_id simp add: wt_step_def)
170 finally have "check_types G mxs ?mxr (map OK (map ok_val phi))" .
173 let ?app = "\<lambda>pc. app (ins!pc) G mxs rT pc et"
174 let ?eff = "\<lambda>pc. eff (ins!pc) G pc et"
177 have "bounded (err_step (length ins) ?app ?eff) (length ins)"
178 by (blast dest: check_bounded_is_bounded boundedD intro: bounded_err_stepI)
181 have "wt_err_step (sup_state_opt G) ?step phi"
182 by (simp add: wt_err_step_def JVM_le_Err_conv)
184 have "wt_app_eff (sup_state_opt G) ?app ?eff (map ok_val phi)"
185 by (auto intro: wt_err_imp_wt_app_eff simp add: exec_def)
188 have "wt_method G C pTs rT mxs mxl ins et (map ok_val phi)"
189 by - (rule wt_method_def2 [THEN iffD2], simp)
194 lemma wt_method_wt_lbv:
195 assumes wf: "wf_prog wf_mb G"
196 assumes wt: "wt_method G C pTs rT mxs mxl ins et phi"
197 assumes C: "is_class G C"
198 assumes pTs: "set pTs \<subseteq> types G"
200 defines [simp]: "cert \<equiv> mk_cert G mxs rT et ins phi"
202 shows "wt_lbv G C pTs rT mxs mxl et cert ins"
204 let ?mxr = "1 + length pTs + mxl"
205 let ?step = "exec G mxs rT et ins"
206 let ?app = "\<lambda>pc. app (ins!pc) G mxs rT pc et"
207 let ?eff = "\<lambda>pc. eff (ins!pc) G pc et"
208 let ?r = "JVMType.le G mxs ?mxr"
209 let ?f = "JVMType.sup G mxs ?mxr"
210 let ?A = "states G mxs ?mxr"
211 let ?phi = "map OK phi"
212 let ?cert = "make_cert ?step ?phi (OK None)"
215 0: "0 < length ins" and
216 length: "length ins = length ?phi" and
217 ck_bounded: "check_bounded ins et" and
218 ck_types: "check_types G mxs ?mxr ?phi" and
219 wt_start: "wt_start G C pTs mxl phi" and
220 app_eff: "wt_app_eff (sup_state_opt G) ?app ?eff phi"
221 by (simp (asm_lr) add: wt_method_def2)
223 have "semilat (JVMType.sl G mxs ?mxr)"
224 by (rule semilat_JVM_slI, rule wf_prog_ws_prog)
225 hence "semilat (?A, ?r, ?f)" by (unfold sl_triple_conv)
227 have "top ?r Err" by (simp add: JVM_le_unfold)
229 have "Err \<in> ?A" by (simp add: JVM_states_unfold)
231 have "bottom ?r (OK None)"
232 by (simp add: JVM_le_unfold bottom_def)
234 have "OK None \<in> ?A" by (simp add: JVM_states_unfold)
237 have bounded: "bounded ?step (length ins)"
238 by (clarsimp simp add: exec_def)
239 (intro bounded_lift check_bounded_is_bounded)
241 have "mono ?r ?step (length ins) ?A"
242 by (rule wf_prog_ws_prog [THEN exec_mono])
243 hence "mono ?r ?step (length ?phi) ?A" by (simp add: length)
245 have "pres_type ?step (length ins) ?A" by (rule exec_pres_type)
246 hence "pres_type ?step (length ?phi) ?A" by (simp add: length)
249 have "set ?phi \<subseteq> ?A" by (simp add: check_types_def)
250 hence "\<forall>pc. pc < length ?phi \<longrightarrow> ?phi!pc \<in> ?A \<and> ?phi!pc \<noteq> Err" by auto
253 have "bounded (exec G mxs rT et ins) (length ?phi)" by (simp add: length)
255 have "OK None \<noteq> Err" by simp
257 from bounded length app_eff
258 have "wt_err_step (sup_state_opt G) ?step ?phi"
259 by (auto intro: wt_app_eff_imp_wt_err simp add: exec_def)
260 hence "wt_step ?r Err ?step ?phi"
261 by (simp add: wt_err_step_def JVM_le_Err_conv)
263 let ?start = "OK (Some ([],(OK (Class C))#(map OK pTs)@(replicate mxl Err)))"
264 from 0 length have "0 < length phi" by auto
265 hence "?phi!0 = OK (phi!0)" by simp
266 with wt_start have "?start <=_?r ?phi!0"
267 by (clarsimp simp add: wt_start_def lesub_def JVM_le_Err_conv)
269 from C pTs have "?start \<in> ?A"
270 by (unfold JVM_states_unfold) (auto intro: list_appendI, force)
272 have "?start \<noteq> Err" by simp
276 have "wtl_inst_list ins ?cert ?f ?r Err (OK None) ?step 0 ?start \<noteq> Err"
277 by (rule lbvc.wtl_complete)
279 from 0 length have "phi \<noteq> []" by auto
282 have "check_types G mxs ?mxr ?cert"
283 by (auto simp add: make_cert_def check_types_def JVM_states_unfold)
285 note ck_bounded 0 length
288 by (simp add: wt_lbv_def lbvjvm_def mk_cert_def
289 check_cert_def make_cert_def nth_append)
294 theorem jvm_lbv_correct:
295 "wt_jvm_prog_lbv G Cert \<Longrightarrow> \<exists>Phi. wt_jvm_prog G Phi"
297 let ?Phi = "\<lambda>C sig. let (C,rT,(maxs,maxl,ins,et)) = the (method (G,C) sig) in
298 SOME phi. wt_method G C (snd sig) rT maxs maxl ins et phi"
300 assume "wt_jvm_prog_lbv G Cert"
301 hence "wt_jvm_prog G ?Phi"
302 apply (unfold wt_jvm_prog_def wt_jvm_prog_lbv_def)
303 apply (erule jvm_prog_lift)
304 apply (auto dest: wt_lbv_wt_method intro: someI)
306 thus ?thesis by blast
309 theorem jvm_lbv_complete:
310 "wt_jvm_prog G Phi \<Longrightarrow> wt_jvm_prog_lbv G (prg_cert G Phi)"
311 apply (unfold wt_jvm_prog_def wt_jvm_prog_lbv_def)
312 apply (erule jvm_prog_lift)
313 apply (auto simp add: prg_cert_def intro: wt_method_wt_lbv)