src/HOL/MicroJava/J/Conform.thy
author nipkow
Wed Jan 04 19:22:53 2006 +0100 (2006-01-04)
changeset 18576 8d98b7711e47
parent 16417 9bc16273c2d4
child 24783 5a3e336a2e37
permissions -rw-r--r--
Reversed Larry's option/iff change.
     1 (*  Title:      HOL/MicroJava/J/Conform.thy
     2     ID:         $Id$
     3     Author:     David von Oheimb
     4     Copyright   1999 Technische Universitaet Muenchen
     5 *)
     6 
     7 header {* \isaheader{Conformity Relations for Type Soundness Proof} *}
     8 
     9 theory Conform imports State WellType Exceptions begin
    10 
    11 types 'c env_ = "'c prog \<times> (vname \<rightharpoonup> ty)"  -- "same as @{text env} of @{text WellType.thy}"
    12 
    13 constdefs
    14 
    15   hext :: "aheap => aheap => bool" ("_ <=| _" [51,51] 50)
    16  "h<=|h' == \<forall>a C fs. h a = Some(C,fs) --> (\<exists>fs'. h' a = Some(C,fs'))"
    17 
    18   conf :: "'c prog => aheap => val => ty => bool" 
    19                                    ("_,_ |- _ ::<= _"  [51,51,51,51] 50)
    20  "G,h|-v::<=T == \<exists>T'. typeof (option_map obj_ty o h) v = Some T' \<and> G\<turnstile>T'\<preceq>T"
    21 
    22   lconf :: "'c prog => aheap => ('a \<rightharpoonup> val) => ('a \<rightharpoonup> ty) => bool"
    23                                    ("_,_ |- _ [::<=] _" [51,51,51,51] 50)
    24  "G,h|-vs[::<=]Ts == \<forall>n T. Ts n = Some T --> (\<exists>v. vs n = Some v \<and> G,h|-v::<=T)"
    25 
    26   oconf :: "'c prog => aheap => obj => bool" ("_,_ |- _ [ok]" [51,51,51] 50)
    27  "G,h|-obj [ok] == G,h|-snd obj[::<=]map_of (fields (G,fst obj))"
    28 
    29   hconf :: "'c prog => aheap => bool" ("_ |-h _ [ok]" [51,51] 50)
    30  "G|-h h [ok]    == \<forall>a obj. h a = Some obj --> G,h|-obj [ok]"
    31  
    32   xconf :: "aheap \<Rightarrow> val option \<Rightarrow> bool"
    33   "xconf hp vo  == preallocated hp \<and> (\<forall> v. (vo = Some v) \<longrightarrow> (\<exists> xc. v = (Addr (XcptRef xc))))"
    34 
    35   conforms :: "xstate => java_mb env_ => bool" ("_ ::<= _" [51,51] 50)
    36  "s::<=E == prg E|-h heap (store s) [ok] \<and> 
    37             prg E,heap (store s)|-locals (store s)[::<=]localT E \<and> 
    38             xconf (heap (store s)) (abrupt s)"
    39 
    40 
    41 syntax (xsymbols)
    42   hext     :: "aheap => aheap => bool"
    43               ("_ \<le>| _" [51,51] 50)
    44 
    45   conf     :: "'c prog => aheap => val => ty => bool"
    46               ("_,_ \<turnstile> _ ::\<preceq> _" [51,51,51,51] 50)
    47 
    48   lconf    :: "'c prog => aheap => ('a \<rightharpoonup> val) => ('a \<rightharpoonup> ty) => bool"
    49               ("_,_ \<turnstile> _ [::\<preceq>] _" [51,51,51,51] 50)
    50 
    51   oconf    :: "'c prog => aheap => obj => bool"
    52               ("_,_ \<turnstile> _ \<surd>" [51,51,51] 50)
    53 
    54   hconf    :: "'c prog => aheap => bool"
    55               ("_ \<turnstile>h _ \<surd>" [51,51] 50)
    56 
    57   conforms :: "state => java_mb env_ => bool"
    58               ("_ ::\<preceq> _" [51,51] 50)
    59 
    60 
    61 section "hext"
    62 
    63 lemma hextI: 
    64 " \<forall>a C fs . h  a = Some (C,fs) -->   
    65       (\<exists>fs'. h' a = Some (C,fs')) ==> h\<le>|h'"
    66 apply (unfold hext_def)
    67 apply auto
    68 done
    69 
    70 lemma hext_objD: "[|h\<le>|h'; h a = Some (C,fs) |] ==> \<exists>fs'. h' a = Some (C,fs')"
    71 apply (unfold hext_def)
    72 apply (force)
    73 done
    74 
    75 lemma hext_refl [simp]: "h\<le>|h"
    76 apply (rule hextI)
    77 apply (fast)
    78 done
    79 
    80 lemma hext_new [simp]: "h a = None ==> h\<le>|h(a\<mapsto>x)"
    81 apply (rule hextI)
    82 apply auto
    83 done
    84 
    85 lemma hext_trans: "[|h\<le>|h'; h'\<le>|h''|] ==> h\<le>|h''"
    86 apply (rule hextI)
    87 apply (fast dest: hext_objD)
    88 done
    89 
    90 lemma hext_upd_obj: "h a = Some (C,fs) ==> h\<le>|h(a\<mapsto>(C,fs'))"
    91 apply (rule hextI)
    92 apply auto
    93 done
    94 
    95 
    96 section "conf"
    97 
    98 lemma conf_Null [simp]: "G,h\<turnstile>Null::\<preceq>T = G\<turnstile>RefT NullT\<preceq>T"
    99 apply (unfold conf_def)
   100 apply (simp (no_asm))
   101 done
   102 
   103 lemma conf_litval [rule_format (no_asm), simp]: 
   104   "typeof (\<lambda>v. None) v = Some T --> G,h\<turnstile>v::\<preceq>T"
   105 apply (unfold conf_def)
   106 apply (rule val.induct)
   107 apply auto
   108 done
   109 
   110 lemma conf_AddrI: "[|h a = Some obj; G\<turnstile>obj_ty obj\<preceq>T|] ==> G,h\<turnstile>Addr a::\<preceq>T"
   111 apply (unfold conf_def)
   112 apply (simp)
   113 done
   114 
   115 lemma conf_obj_AddrI: "[|h a = Some (C,fs); G\<turnstile>C\<preceq>C D|] ==> G,h\<turnstile>Addr a::\<preceq> Class D"
   116 apply (unfold conf_def)
   117 apply (simp)
   118 done
   119 
   120 lemma defval_conf [rule_format (no_asm)]: 
   121   "is_type G T --> G,h\<turnstile>default_val T::\<preceq>T"
   122 apply (unfold conf_def)
   123 apply (rule_tac y = "T" in ty.exhaust)
   124 apply  (erule ssubst)
   125 apply  (rule_tac y = "prim_ty" in prim_ty.exhaust)
   126 apply    (auto simp add: widen.null)
   127 done
   128 
   129 lemma conf_upd_obj: 
   130 "h a = Some (C,fs) ==> (G,h(a\<mapsto>(C,fs'))\<turnstile>x::\<preceq>T) = (G,h\<turnstile>x::\<preceq>T)"
   131 apply (unfold conf_def)
   132 apply (rule val.induct)
   133 apply auto
   134 done
   135 
   136 lemma conf_widen [rule_format (no_asm)]: 
   137   "wf_prog wf_mb G ==> G,h\<turnstile>x::\<preceq>T --> G\<turnstile>T\<preceq>T' --> G,h\<turnstile>x::\<preceq>T'"
   138 apply (unfold conf_def)
   139 apply (rule val.induct)
   140 apply (auto intro: widen_trans)
   141 done
   142 
   143 lemma conf_hext [rule_format (no_asm)]: "h\<le>|h' ==> G,h\<turnstile>v::\<preceq>T --> G,h'\<turnstile>v::\<preceq>T"
   144 apply (unfold conf_def)
   145 apply (rule val.induct)
   146 apply (auto dest: hext_objD)
   147 done
   148 
   149 lemma new_locD: "[|h a = None; G,h\<turnstile>Addr t::\<preceq>T|] ==> t\<noteq>a"
   150 apply (unfold conf_def)
   151 apply auto
   152 done
   153 
   154 lemma conf_RefTD [rule_format (no_asm)]: 
   155  "G,h\<turnstile>a'::\<preceq>RefT T --> a' = Null |   
   156   (\<exists>a obj T'. a' = Addr a \<and>  h a = Some obj \<and>  obj_ty obj = T' \<and>  G\<turnstile>T'\<preceq>RefT T)"
   157 apply (unfold conf_def)
   158 apply(induct_tac "a'")
   159 apply(auto)
   160 done
   161 
   162 lemma conf_NullTD: "G,h\<turnstile>a'::\<preceq>RefT NullT ==> a' = Null"
   163 apply (drule conf_RefTD)
   164 apply auto
   165 done
   166 
   167 lemma non_npD: "[|a' \<noteq> Null; G,h\<turnstile>a'::\<preceq>RefT t|] ==>  
   168   \<exists>a C fs. a' = Addr a \<and>  h a = Some (C,fs) \<and>  G\<turnstile>Class C\<preceq>RefT t"
   169 apply (drule conf_RefTD)
   170 apply auto
   171 done
   172 
   173 lemma non_np_objD: "!!G. [|a' \<noteq> Null; G,h\<turnstile>a'::\<preceq> Class C|] ==>  
   174   (\<exists>a C' fs. a' = Addr a \<and>  h a = Some (C',fs) \<and>  G\<turnstile>C'\<preceq>C C)"
   175 apply (fast dest: non_npD)
   176 done
   177 
   178 lemma non_np_objD' [rule_format (no_asm)]: 
   179   "a' \<noteq> Null ==> wf_prog wf_mb G ==> G,h\<turnstile>a'::\<preceq>RefT t --> 
   180   (\<exists>a C fs. a' = Addr a \<and>  h a = Some (C,fs) \<and>  G\<turnstile>Class C\<preceq>RefT t)"
   181 apply(rule_tac y = "t" in ref_ty.exhaust)
   182  apply (fast dest: conf_NullTD)
   183 apply (fast dest: non_np_objD)
   184 done
   185 
   186 lemma conf_list_gext_widen [rule_format (no_asm)]: 
   187   "wf_prog wf_mb G ==> \<forall>Ts Ts'. list_all2 (conf G h) vs Ts --> 
   188   list_all2 (\<lambda>T T'. G\<turnstile>T\<preceq>T') Ts Ts' -->  list_all2 (conf G h) vs Ts'"
   189 apply(induct_tac "vs")
   190  apply(clarsimp)
   191 apply(clarsimp)
   192 apply(frule list_all2_lengthD [THEN sym])
   193 apply(simp (no_asm_use) add: length_Suc_conv)
   194 apply(safe)
   195 apply(frule list_all2_lengthD [THEN sym])
   196 apply(simp (no_asm_use) add: length_Suc_conv)
   197 apply(clarify)
   198 apply(fast elim: conf_widen)
   199 done
   200 
   201 
   202 section "lconf"
   203 
   204 lemma lconfD: "[| G,h\<turnstile>vs[::\<preceq>]Ts; Ts n = Some T |] ==> G,h\<turnstile>(the (vs n))::\<preceq>T"
   205 apply (unfold lconf_def)
   206 apply (force)
   207 done
   208 
   209 lemma lconf_hext [elim]: "[| G,h\<turnstile>l[::\<preceq>]L; h\<le>|h' |] ==> G,h'\<turnstile>l[::\<preceq>]L"
   210 apply (unfold lconf_def)
   211 apply  (fast elim: conf_hext)
   212 done
   213 
   214 lemma lconf_upd: "!!X. [| G,h\<turnstile>l[::\<preceq>]lT;  
   215   G,h\<turnstile>v::\<preceq>T; lT va = Some T |] ==> G,h\<turnstile>l(va\<mapsto>v)[::\<preceq>]lT"
   216 apply (unfold lconf_def)
   217 apply auto
   218 done
   219 
   220 lemma lconf_init_vars_lemma [rule_format (no_asm)]: 
   221   "\<forall>x. P x --> R (dv x) x ==> (\<forall>x. map_of fs f = Some x --> P x) -->  
   222   (\<forall>T. map_of fs f = Some T -->  
   223   (\<exists>v. map_of (map (\<lambda>(f,ft). (f, dv ft)) fs) f = Some v \<and>  R v T))"
   224 apply( induct_tac "fs")
   225 apply auto
   226 done
   227 
   228 lemma lconf_init_vars [intro!]: 
   229 "\<forall>n. \<forall>T. map_of fs n = Some T --> is_type G T ==> G,h\<turnstile>init_vars fs[::\<preceq>]map_of fs"
   230 apply (unfold lconf_def init_vars_def)
   231 apply auto
   232 apply( rule lconf_init_vars_lemma)
   233 apply(   erule_tac [3] asm_rl)
   234 apply(  intro strip)
   235 apply(  erule defval_conf)
   236 apply auto
   237 done
   238 
   239 lemma lconf_ext: "[|G,s\<turnstile>l[::\<preceq>]L; G,s\<turnstile>v::\<preceq>T|] ==> G,s\<turnstile>l(vn\<mapsto>v)[::\<preceq>]L(vn\<mapsto>T)"
   240 apply (unfold lconf_def)
   241 apply auto
   242 done
   243 
   244 lemma lconf_ext_list [rule_format (no_asm)]: 
   245   "G,h\<turnstile>l[::\<preceq>]L ==> \<forall>vs Ts. distinct vns --> length Ts = length vns --> 
   246   list_all2 (\<lambda>v T. G,h\<turnstile>v::\<preceq>T) vs Ts --> G,h\<turnstile>l(vns[\<mapsto>]vs)[::\<preceq>]L(vns[\<mapsto>]Ts)"
   247 apply (unfold lconf_def)
   248 apply( induct_tac "vns")
   249 apply(  clarsimp)
   250 apply( clarsimp)
   251 apply( frule list_all2_lengthD)
   252 apply( auto simp add: length_Suc_conv)
   253 done
   254 
   255 lemma lconf_restr: "\<lbrakk>lT vn = None; G, h \<turnstile> l [::\<preceq>] lT(vn\<mapsto>T)\<rbrakk> \<Longrightarrow> G, h \<turnstile> l [::\<preceq>] lT"
   256 apply (unfold lconf_def)
   257 apply (intro strip)
   258 apply (case_tac "n = vn")
   259 apply auto
   260 done
   261 
   262 section "oconf"
   263 
   264 lemma oconf_hext: "G,h\<turnstile>obj\<surd> ==> h\<le>|h' ==> G,h'\<turnstile>obj\<surd>"
   265 apply (unfold oconf_def)
   266 apply (fast)
   267 done
   268 
   269 lemma oconf_obj: "G,h\<turnstile>(C,fs)\<surd> =  
   270   (\<forall>T f. map_of(fields (G,C)) f = Some T --> (\<exists>v. fs f = Some v \<and>  G,h\<turnstile>v::\<preceq>T))"
   271 apply (unfold oconf_def lconf_def)
   272 apply auto
   273 done
   274 
   275 lemmas oconf_objD = oconf_obj [THEN iffD1, THEN spec, THEN spec, THEN mp]
   276 
   277 
   278 section "hconf"
   279 
   280 lemma hconfD: "[|G\<turnstile>h h\<surd>; h a = Some obj|] ==> G,h\<turnstile>obj\<surd>"
   281 apply (unfold hconf_def)
   282 apply (fast)
   283 done
   284 
   285 lemma hconfI: "\<forall>a obj. h a=Some obj --> G,h\<turnstile>obj\<surd> ==> G\<turnstile>h h\<surd>"
   286 apply (unfold hconf_def)
   287 apply (fast)
   288 done
   289 
   290 
   291 section "xconf"
   292 
   293 lemma xconf_raise_if: "xconf h x \<Longrightarrow> xconf h (raise_if b xcn x)"
   294 by (simp add: xconf_def raise_if_def)
   295 
   296 
   297 
   298 section "conforms"
   299 
   300 lemma conforms_heapD: "(x, (h, l))::\<preceq>(G, lT) ==> G\<turnstile>h h\<surd>"
   301 apply (unfold conforms_def)
   302 apply (simp)
   303 done
   304 
   305 lemma conforms_localD: "(x, (h, l))::\<preceq>(G, lT) ==> G,h\<turnstile>l[::\<preceq>]lT"
   306 apply (unfold conforms_def)
   307 apply (simp)
   308 done
   309 
   310 lemma conforms_xcptD: "(x, (h, l))::\<preceq>(G, lT) ==> xconf h x"
   311 apply (unfold conforms_def)
   312 apply (simp)
   313 done
   314 
   315 lemma conformsI: "[|G\<turnstile>h h\<surd>; G,h\<turnstile>l[::\<preceq>]lT; xconf h x|] ==> (x, (h, l))::\<preceq>(G, lT)"
   316 apply (unfold conforms_def)
   317 apply auto
   318 done
   319 
   320 lemma conforms_restr: "\<lbrakk>lT vn = None; s ::\<preceq> (G, lT(vn\<mapsto>T)) \<rbrakk> \<Longrightarrow> s ::\<preceq> (G, lT)"
   321 by (simp add: conforms_def, fast intro: lconf_restr)
   322 
   323 lemma conforms_xcpt_change: "\<lbrakk> (x, (h,l))::\<preceq> (G, lT); xconf h x \<longrightarrow> xconf h x' \<rbrakk> \<Longrightarrow> (x', (h,l))::\<preceq> (G, lT)"
   324 by (simp add: conforms_def)
   325 
   326 
   327 lemma preallocated_hext: "\<lbrakk> preallocated h; h\<le>|h'\<rbrakk> \<Longrightarrow> preallocated h'"
   328 by (simp add: preallocated_def hext_def)
   329 
   330 lemma xconf_hext: "\<lbrakk> xconf h vo; h\<le>|h'\<rbrakk> \<Longrightarrow> xconf h' vo"
   331 by (simp add: xconf_def preallocated_def hext_def)
   332 
   333 lemma conforms_hext: "[|(x,(h,l))::\<preceq>(G,lT); h\<le>|h'; G\<turnstile>h h'\<surd> |] 
   334   ==> (x,(h',l))::\<preceq>(G,lT)"
   335 by( fast dest: conforms_localD conforms_xcptD elim!: conformsI xconf_hext)
   336 
   337 
   338 lemma conforms_upd_obj: 
   339   "[|(x,(h,l))::\<preceq>(G, lT); G,h(a\<mapsto>obj)\<turnstile>obj\<surd>; h\<le>|h(a\<mapsto>obj)|] 
   340   ==> (x,(h(a\<mapsto>obj),l))::\<preceq>(G, lT)"
   341 apply(rule conforms_hext)
   342 apply  auto
   343 apply(rule hconfI)
   344 apply(drule conforms_heapD)
   345 apply(tactic {* auto_tac (HOL_cs addEs [thm "oconf_hext"] 
   346                 addDs [thm "hconfD"], simpset() delsimps [split_paired_All]) *})
   347 done
   348 
   349 lemma conforms_upd_local: 
   350 "[|(x,(h, l))::\<preceq>(G, lT); G,h\<turnstile>v::\<preceq>T; lT va = Some T|] 
   351   ==> (x,(h, l(va\<mapsto>v)))::\<preceq>(G, lT)"
   352 apply (unfold conforms_def)
   353 apply( auto elim: lconf_upd)
   354 done
   355 
   356 end