src/HOL/MicroJava/J/JTypeSafe.thy
author wenzelm
Thu Feb 28 13:24:51 2013 +0100 (2013-02-28)
changeset 51304 0e71a248cacb
parent 45133 2214ba5bdfff
child 51686 532e0ac5a66d
permissions -rw-r--r--
marginalized historic strip_tac;
     1 (*  Title:      HOL/MicroJava/J/JTypeSafe.thy
     2     Author:     David von Oheimb, Technische Universitaet Muenchen
     3 *)
     4 
     5 header {* \isaheader{Type Safety Proof} *}
     6 
     7 theory JTypeSafe imports Eval Conform begin
     8 
     9 declare split_beta [simp]
    10 
    11 lemma NewC_conforms: 
    12 "[|h a = None; (x,(h, l))::\<preceq>(G, lT); wf_prog wf_mb G; is_class G C|] ==>  
    13   (x,(h(a\<mapsto>(C,(init_vars (fields (G,C))))), l))::\<preceq>(G, lT)"
    14 apply( erule conforms_upd_obj)
    15 apply(  unfold oconf_def)
    16 apply(  auto dest!: fields_is_type simp add: wf_prog_ws_prog)
    17 done
    18 
    19 
    20 lemma Cast_conf: 
    21  "[| wf_prog wf_mb G; G,h\<turnstile>v::\<preceq>CC; G\<turnstile>CC \<preceq>? Class D; cast_ok G D h v|]  
    22   ==> G,h\<turnstile>v::\<preceq>Class D"
    23 apply (case_tac "CC")
    24 apply simp
    25 apply (case_tac "ref_ty")
    26 apply (clarsimp simp add: conf_def)
    27 apply simp
    28 apply (ind_cases "G \<turnstile> Class cname \<preceq>? Class D" for cname, simp)
    29 apply (rule conf_widen, assumption+) apply (erule widen.subcls)
    30 
    31 apply (unfold cast_ok_def)
    32 apply( case_tac "v = Null")
    33 apply(  simp)
    34 apply(  clarify)
    35 apply( drule (1) non_npD)
    36 apply( auto intro!: conf_AddrI simp add: obj_ty_def)
    37 done
    38 
    39 
    40 lemma FAcc_type_sound: 
    41 "[| wf_prog wf_mb G; field (G,C) fn = Some (fd, ft); (x,(h,l))::\<preceq>(G,lT);  
    42   x' = None --> G,h\<turnstile>a'::\<preceq> Class C; np a' x' = None |] ==>  
    43   G,h\<turnstile>the (snd (the (h (the_Addr a'))) (fn, fd))::\<preceq>ft"
    44 apply( drule np_NoneD)
    45 apply( erule conjE)
    46 apply( erule (1) notE impE)
    47 apply( drule non_np_objD)
    48 apply   auto
    49 apply( drule conforms_heapD [THEN hconfD])
    50 apply(  assumption)
    51 apply (frule wf_prog_ws_prog)
    52 apply( drule (2) widen_cfs_fields)
    53 apply( drule (1) oconf_objD)
    54 apply auto
    55 done
    56 
    57 lemma FAss_type_sound: 
    58  "[| wf_prog wf_mb G; a = the_Addr a'; (c, fs) = the (h a);  
    59     (G, lT)\<turnstile>v::T'; G\<turnstile>T'\<preceq>ft;  
    60     (G, lT)\<turnstile>aa::Class C;  
    61     field (G,C) fn = Some (fd, ft); h''\<le>|h';  
    62     x' = None --> G,h'\<turnstile>a'::\<preceq> Class C; h'\<le>|h;  
    63     Norm (h, l)::\<preceq>(G, lT); G,h\<turnstile>x::\<preceq>T'; np a' x' = None|] ==>  
    64   h''\<le>|h(a\<mapsto>(c,(fs((fn,fd)\<mapsto>x)))) \<and>   
    65   Norm(h(a\<mapsto>(c,(fs((fn,fd)\<mapsto>x)))), l)::\<preceq>(G, lT) \<and>   
    66   G,h(a\<mapsto>(c,(fs((fn,fd)\<mapsto>x))))\<turnstile>x::\<preceq>T'"
    67 apply( drule np_NoneD)
    68 apply( erule conjE)
    69 apply( simp)
    70 apply( drule non_np_objD)
    71 apply(  assumption)
    72 apply( clarify)
    73 apply( simp (no_asm_use))
    74 apply( frule (1) hext_objD)
    75 apply( erule exE)
    76 apply( simp)
    77 apply( clarify)
    78 apply( rule conjI)
    79 apply(  fast elim: hext_trans hext_upd_obj)
    80 apply( rule conjI)
    81 prefer 2
    82 apply(  fast elim: conf_upd_obj [THEN iffD2])
    83 
    84 apply( rule conforms_upd_obj)
    85 apply   auto
    86 apply(  rule_tac [2] hextI)
    87 prefer 2
    88 apply(  force)
    89 apply( rule oconf_hext)
    90 apply(  erule_tac [2] hext_upd_obj)
    91 apply (frule wf_prog_ws_prog)
    92 apply( drule (2) widen_cfs_fields)
    93 apply( rule oconf_obj [THEN iffD2])
    94 apply( simp (no_asm))
    95 apply( intro strip)
    96 apply( case_tac "(aaa, b) = (fn, fd)")
    97 apply(  simp)
    98 apply(  fast intro: conf_widen)
    99 apply( fast dest: conforms_heapD [THEN hconfD] oconf_objD)
   100 done
   101 
   102 
   103 
   104 lemma Call_lemma2: "[| wf_prog wf_mb G; list_all2 (conf G h) pvs pTs;  
   105    list_all2 (\<lambda>T T'. G\<turnstile>T\<preceq>T') pTs pTs'; wf_mhead G (mn,pTs') rT;  
   106   length pTs' = length pns; distinct pns;  
   107   Ball (set lvars) (split (\<lambda>vn. is_type G))  
   108   |] ==> G,h\<turnstile>init_vars lvars(pns[\<mapsto>]pvs)[::\<preceq>]map_of lvars(pns[\<mapsto>]pTs')"
   109 apply (unfold wf_mhead_def)
   110 apply( clarsimp)
   111 apply( rule lconf_ext_list)
   112 apply(    rule Ball_set_table [THEN lconf_init_vars])
   113 apply(    force)
   114 apply(   assumption)
   115 apply(  assumption)
   116 apply( erule (2) conf_list_gext_widen)
   117 done
   118 
   119 lemma Call_type_sound: 
   120  "[| wf_java_prog G; a' \<noteq> Null; Norm (h, l)::\<preceq>(G, lT); class G C = Some y;  
   121      max_spec G C (mn,pTsa) = {((mda,rTa),pTs')}; xc\<le>|xh; xh\<le>|h;  
   122      list_all2 (conf G h) pvs pTsa; 
   123      (md, rT, pns, lvars, blk, res) =  
   124                the (method (G,fst (the (h (the_Addr a')))) (mn, pTs')); 
   125   \<forall>lT. (np a' None, h, init_vars lvars(pns[\<mapsto>]pvs)(This\<mapsto>a'))::\<preceq>(G, lT) -->  
   126   (G, lT)\<turnstile>blk\<surd> -->  h\<le>|xi \<and>  (xcptb, xi, xl)::\<preceq>(G, lT);  
   127   \<forall>lT. (xcptb,xi, xl)::\<preceq>(G, lT) --> (\<forall>T. (G, lT)\<turnstile>res::T -->  
   128           xi\<le>|h' \<and> (x',h', xj)::\<preceq>(G, lT) \<and> (x' = None --> G,h'\<turnstile>v::\<preceq>T));  
   129   G,xh\<turnstile>a'::\<preceq> Class C
   130   |] ==>  
   131   xc\<le>|h' \<and> (x',(h', l))::\<preceq>(G, lT) \<and>  (x' = None --> G,h'\<turnstile>v::\<preceq>rTa)"
   132 apply( drule max_spec2mheads)
   133 apply( clarify)
   134 apply( drule (2) non_np_objD')
   135 apply( clarsimp)
   136 apply( frule (1) hext_objD)
   137 apply( clarsimp)
   138 apply( drule (3) Call_lemma)
   139 apply( clarsimp simp add: wf_java_mdecl_def)
   140 apply( erule_tac V = "method ?sig ?x = ?y" in thin_rl)
   141 apply( drule spec, erule impE,  erule_tac [2] notE impE, tactic "assume_tac 2")
   142 apply(  rule conformsI)
   143 apply(   erule conforms_heapD)
   144 apply(  rule lconf_ext)
   145 apply(   force elim!: Call_lemma2)
   146 apply(  erule conf_hext, erule (1) conf_obj_AddrI)
   147 apply( erule_tac V = "?E\<turnstile>?blk\<surd>" in thin_rl) 
   148 apply (simp add: conforms_def)
   149 
   150 apply( erule conjE)
   151 apply( drule spec, erule (1) impE)
   152 apply( drule spec, erule (1) impE)
   153 apply( erule_tac V = "?E\<turnstile>res::?rT" in thin_rl)
   154 apply( clarify)
   155 apply( rule conjI)
   156 apply(  fast intro: hext_trans)
   157 apply( rule conjI)
   158 apply(  rule_tac [2] impI)
   159 apply(  erule_tac [2] notE impE, tactic "assume_tac 2")
   160 apply(  frule_tac [2] conf_widen)
   161 apply(    tactic "assume_tac 4")
   162 apply(   tactic "assume_tac 2")
   163 prefer 2
   164 apply(  fast elim!: widen_trans)
   165 apply (rule conforms_xcpt_change)
   166 apply( rule conforms_hext) apply assumption
   167 apply(  erule (1) hext_trans)
   168 apply( erule conforms_heapD)
   169 apply (simp add: conforms_def)
   170 done
   171 
   172 
   173 
   174 declare split_if [split del]
   175 declare fun_upd_apply [simp del]
   176 declare fun_upd_same [simp]
   177 declare wf_prog_ws_prog [simp]
   178 
   179 ML{*
   180 val forward_hyp_tac = ALLGOALS (TRY o (EVERY' [dtac spec, mp_tac,
   181   (mp_tac ORELSE' (dtac spec THEN' mp_tac)), REPEAT o (etac conjE)]))
   182 *}
   183 
   184 
   185 theorem eval_evals_exec_type_sound: 
   186 "wf_java_prog G ==>  
   187   (G\<turnstile>(x,(h,l)) -e  \<succ>v  -> (x', (h',l')) -->  
   188       (\<forall>lT.   (x,(h ,l ))::\<preceq>(G,lT) --> (\<forall>T . (G,lT)\<turnstile>e  :: T -->  
   189       h\<le>|h' \<and> (x',(h',l'))::\<preceq>(G,lT) \<and> (x'=None --> G,h'\<turnstile>v  ::\<preceq> T )))) \<and>  
   190   (G\<turnstile>(x,(h,l)) -es[\<succ>]vs-> (x', (h',l')) -->  
   191       (\<forall>lT.   (x,(h ,l ))::\<preceq>(G,lT) --> (\<forall>Ts. (G,lT)\<turnstile>es[::]Ts -->  
   192       h\<le>|h' \<and> (x',(h',l'))::\<preceq>(G,lT) \<and> (x'=None --> list_all2 (\<lambda>v T. G,h'\<turnstile>v::\<preceq>T) vs Ts)))) \<and>  
   193   (G\<turnstile>(x,(h,l)) -c       -> (x', (h',l')) -->  
   194       (\<forall>lT.   (x,(h ,l ))::\<preceq>(G,lT) -->       (G,lT)\<turnstile>c  \<surd> -->  
   195       h\<le>|h' \<and> (x',(h',l'))::\<preceq>(G,lT)))"
   196 apply( rule eval_evals_exec_induct)
   197 apply( unfold c_hupd_def)
   198 
   199 -- "several simplifications, XcptE, XcptEs, XcptS, Skip, Nil??"
   200 apply( simp_all)
   201 apply( tactic "ALLGOALS (REPEAT o resolve_tac [impI, allI])")
   202 apply( tactic {* ALLGOALS (eresolve_tac [@{thm ty_expr.cases}, @{thm ty_exprs.cases}, @{thm wt_stmt.cases}]
   203                  THEN_ALL_NEW (full_simp_tac (global_simpset_of @{theory Conform}))) *})
   204 apply(tactic "ALLGOALS (EVERY' [REPEAT o (etac conjE), REPEAT o hyp_subst_tac])")
   205 
   206 -- "Level 7"
   207 -- "15 NewC"
   208 apply (drule sym)
   209 apply( drule new_AddrD)
   210 apply( erule disjE)
   211 prefer 2
   212 apply(  simp (no_asm_simp))
   213 apply (rule conforms_xcpt_change, assumption) 
   214 apply (simp (no_asm_simp) add: xconf_def)
   215 apply( clarsimp)
   216 apply( rule conjI)
   217 apply(  force elim!: NewC_conforms)
   218 apply( rule conf_obj_AddrI)
   219 apply(  rule_tac [2] rtrancl.rtrancl_refl)
   220 apply( simp (no_asm))
   221 
   222 -- "for Cast"
   223 defer 1
   224 
   225 -- "14 Lit"
   226 apply( erule conf_litval)
   227 
   228 -- "13 BinOp"
   229 apply (tactic "forward_hyp_tac")
   230 apply (tactic "forward_hyp_tac")
   231 apply( rule conjI, erule (1) hext_trans)
   232 apply( erule conjI)
   233 apply( clarsimp)
   234 apply( drule eval_no_xcpt)
   235 apply( simp split add: binop.split)
   236 
   237 -- "12 LAcc"
   238 apply simp
   239 apply( fast elim: conforms_localD [THEN lconfD])
   240 
   241 -- "for FAss"
   242 apply( tactic {* EVERY'[eresolve_tac [@{thm ty_expr.cases}, @{thm ty_exprs.cases}, @{thm wt_stmt.cases}] 
   243        THEN_ALL_NEW (full_simp_tac @{simpset}), REPEAT o (etac conjE), hyp_subst_tac] 3*})
   244 
   245 -- "for if"
   246 apply( tactic {* (Induct_Tacs.case_tac @{context} "the_Bool v" THEN_ALL_NEW
   247   (asm_full_simp_tac @{simpset})) 7*})
   248 
   249 apply (tactic "forward_hyp_tac")
   250 
   251 -- "11+1 if"
   252 prefer 7
   253 apply(  fast intro: hext_trans)
   254 prefer 7
   255 apply(  fast intro: hext_trans)
   256 
   257 -- "10 Expr"
   258 prefer 6
   259 apply( fast)
   260 
   261 -- "9 ???"
   262 apply( simp_all)
   263 
   264 -- "8 Cast"
   265 prefer 8
   266 apply (rule conjI)
   267   apply (fast intro: conforms_xcpt_change xconf_raise_if)
   268 
   269   apply clarify
   270   apply (drule raise_if_NoneD)
   271   apply (clarsimp)
   272   apply (rule Cast_conf)
   273   apply assumption+
   274 
   275 
   276 -- "7 LAss"
   277 apply (fold fun_upd_def)
   278 apply( tactic {* (eresolve_tac [@{thm ty_expr.cases}, @{thm ty_exprs.cases}, @{thm wt_stmt.cases}] 
   279                  THEN_ALL_NEW (full_simp_tac @{simpset})) 1 *})
   280 apply (intro strip)
   281 apply (case_tac E)
   282 apply (simp)
   283 apply( blast intro: conforms_upd_local conf_widen)
   284 
   285 -- "6 FAcc"
   286 apply (rule conjI) 
   287   apply (simp add: np_def)
   288   apply (fast intro: conforms_xcpt_change xconf_raise_if)
   289 apply( fast elim!: FAcc_type_sound)
   290 
   291 -- "5 While"
   292 prefer 5
   293 apply(erule_tac V = "?a \<longrightarrow> ?b" in thin_rl)
   294 apply(drule (1) ty_expr_ty_exprs_wt_stmt.Loop)
   295 apply(force elim: hext_trans)
   296 
   297 apply (tactic "forward_hyp_tac")
   298 
   299 -- "4 Cond"
   300 prefer 4
   301 apply (case_tac "the_Bool v")
   302 apply simp
   303 apply( fast dest: evals_no_xcpt intro: conf_hext hext_trans)
   304 apply simp
   305 apply( fast dest: evals_no_xcpt intro: conf_hext hext_trans)
   306 
   307 -- "3 ;;"
   308 prefer 3
   309 apply( fast dest: evals_no_xcpt intro: conf_hext hext_trans)
   310 
   311 
   312 -- "2 FAss"
   313 apply (subgoal_tac "(np a' x1, aa, ba) ::\<preceq> (G, lT)")
   314   prefer 2
   315   apply (simp add: np_def)
   316   apply (fast intro: conforms_xcpt_change xconf_raise_if)
   317 apply( case_tac "x2")
   318   -- "x2 = None"
   319   apply (simp)
   320   apply (tactic forward_hyp_tac, clarify)
   321   apply( drule eval_no_xcpt)
   322   apply( erule FAss_type_sound, rule HOL.refl, assumption+)
   323   -- "x2 = Some a"
   324   apply (  simp (no_asm_simp))
   325   apply(  fast intro: hext_trans)
   326 
   327 
   328 apply( tactic prune_params_tac)
   329 -- "Level 52"
   330 
   331 -- "1 Call"
   332 apply( case_tac "x")
   333 prefer 2
   334 apply(  clarsimp)
   335 apply(  drule exec_xcpt)
   336 apply(  simp)
   337 apply(  drule_tac eval_xcpt)
   338 apply(  simp)
   339 apply(  fast elim: hext_trans)
   340 apply( clarify)
   341 apply( drule evals_no_xcpt)
   342 apply( simp)
   343 apply( case_tac "a' = Null")
   344 apply(  simp)
   345 apply(  drule exec_xcpt)
   346 apply(  simp)
   347 apply(  drule eval_xcpt)
   348 apply(  simp)
   349 apply (rule conjI)
   350   apply(  fast elim: hext_trans)
   351   apply (rule conforms_xcpt_change, assumption) 
   352   apply (simp (no_asm_simp) add: xconf_def)
   353 apply(clarsimp)
   354 
   355 apply( drule ty_expr_is_type, simp)
   356 apply(clarsimp)
   357 apply(unfold is_class_def)
   358 apply(clarsimp)
   359 
   360 apply(rule Call_type_sound);
   361 prefer 11
   362 apply blast
   363 apply (simp (no_asm_simp))+ 
   364 
   365 done
   366 
   367 
   368 lemma eval_type_sound: "!!E s s'.  
   369   [| wf_java_prog G; G\<turnstile>(x,s) -e\<succ>v -> (x',s'); (x,s)::\<preceq>E; E\<turnstile>e::T; G=prg E |]  
   370   ==> (x',s')::\<preceq>E \<and> (x'=None --> G,heap s'\<turnstile>v::\<preceq>T) \<and> heap s \<le>| heap s'"
   371 apply (simp (no_asm_simp) only: split_tupled_all)
   372 apply (drule eval_evals_exec_type_sound [THEN conjunct1, THEN mp, THEN spec, THEN mp])
   373 apply auto
   374 done
   375 
   376 
   377 lemma evals_type_sound: "!!E s s'.  
   378   [| wf_java_prog G; G\<turnstile>(x,s) -es[\<succ>]vs -> (x',s'); (x,s)::\<preceq>E; E\<turnstile>es[::]Ts; G=prg E |]  
   379   ==> (x',s')::\<preceq>E \<and> (x'=None --> (list_all2 (\<lambda>v T. G,heap s'\<turnstile>v::\<preceq>T) vs Ts)) \<and> heap s \<le>| heap s'"
   380 apply (simp (no_asm_simp) only: split_tupled_all)
   381 apply (drule eval_evals_exec_type_sound [THEN conjunct2, THEN conjunct1, THEN mp, THEN spec, THEN mp])
   382 apply auto
   383 done
   384 
   385 lemma exec_type_sound: "!!E s s'.  
   386   \<lbrakk> wf_java_prog G; G\<turnstile>(x,s) -s0-> (x',s'); (x,s)::\<preceq>E; E\<turnstile>s0\<surd>; G=prg E \<rbrakk>
   387   \<Longrightarrow> (x',s')::\<preceq>E \<and> heap s \<le>| heap s'"
   388 apply( simp (no_asm_simp) only: split_tupled_all)
   389 apply (drule eval_evals_exec_type_sound 
   390              [THEN conjunct2, THEN conjunct2, THEN mp, THEN spec, THEN mp])
   391 apply   auto
   392 done
   393 
   394 theorem all_methods_understood: 
   395 "[|G=prg E; wf_java_prog G; G\<turnstile>(x,s) -e\<succ>a'-> Norm s'; a' \<noteq> Null; 
   396           (x,s)::\<preceq>E; E\<turnstile>e::Class C; method (G,C) sig \<noteq> None|] ==>  
   397   method (G,fst (the (heap s' (the_Addr a')))) sig \<noteq> None"
   398 apply (frule eval_type_sound, assumption+)
   399 apply(clarsimp)
   400 apply( frule widen_methd)
   401 apply assumption
   402 prefer 2
   403 apply(  fast)
   404 apply( drule non_npD)
   405 apply auto
   406 done
   407 
   408 declare split_beta [simp del]
   409 declare fun_upd_apply [simp]
   410 declare wf_prog_ws_prog [simp del]
   411 
   412 end
   413