src/HOL/MicroJava/J/TypeRel.thy
author wenzelm
Tue Sep 03 01:12:40 2013 +0200 (2013-09-03)
changeset 53374 a14d2a854c02
parent 45970 b6d0cff57d96
child 55017 2df6ad1dbd66
permissions -rw-r--r--
tuned proofs -- clarified flow of facts wrt. calculation;
     1 (*  Title:      HOL/MicroJava/J/TypeRel.thy
     2     Author:     David von Oheimb, Technische Universitaet Muenchen
     3 *)
     4 
     5 header {* \isaheader{Relations between Java Types} *}
     6 
     7 theory TypeRel
     8 imports Decl "~~/src/HOL/Library/Wfrec"
     9 begin
    10 
    11 -- "direct subclass, cf. 8.1.3"
    12 
    13 inductive_set
    14   subcls1 :: "'c prog => (cname \<times> cname) set"
    15   and subcls1' :: "'c prog => cname \<Rightarrow> cname => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
    16   for G :: "'c prog"
    17 where
    18   "G \<turnstile> C \<prec>C1 D \<equiv> (C, D) \<in> subcls1 G"
    19   | subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G \<turnstile> C \<prec>C1 D"
    20 
    21 abbreviation
    22   subcls  :: "'c prog => cname \<Rightarrow> cname => bool" ("_ \<turnstile> _ \<preceq>C _"  [71,71,71] 70)
    23   where "G \<turnstile> C \<preceq>C D \<equiv> (C, D) \<in> (subcls1 G)^*"
    24 
    25 lemma subcls1D: 
    26   "G\<turnstile>C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>fs ms. class G C = Some (D,fs,ms))"
    27 apply (erule subcls1.cases)
    28 apply auto
    29 done
    30 
    31 lemma subcls1_def2:
    32   "subcls1 P =
    33      (SIGMA C:{C. is_class P C}. {D. C\<noteq>Object \<and> fst (the (class P C))=D})"
    34   by (auto simp add: is_class_def dest: subcls1D intro: subcls1I)
    35 
    36 lemma finite_subcls1: "finite (subcls1 G)"
    37 apply(simp add: subcls1_def2 del: mem_Sigma_iff)
    38 apply(rule finite_SigmaI [OF finite_is_class])
    39 apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
    40 apply  auto
    41 done
    42 
    43 lemma subcls_is_class: "(C, D) \<in> (subcls1 G)^+  ==> is_class G C"
    44 apply (unfold is_class_def)
    45 apply(erule trancl_trans_induct)
    46 apply (auto dest!: subcls1D)
    47 done
    48 
    49 lemma subcls_is_class2 [rule_format (no_asm)]: 
    50   "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
    51 apply (unfold is_class_def)
    52 apply (erule rtrancl_induct)
    53 apply  (drule_tac [2] subcls1D)
    54 apply  auto
    55 done
    56 
    57 definition class_rec :: "'c prog \<Rightarrow> cname \<Rightarrow> 'a \<Rightarrow>
    58     (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a" where
    59   "class_rec G == wfrec ((subcls1 G)^-1)
    60     (\<lambda>r C t f. case class G C of
    61          None \<Rightarrow> undefined
    62        | Some (D,fs,ms) \<Rightarrow> 
    63            f C fs ms (if C = Object then t else r D t f))"
    64 
    65 lemma class_rec_lemma:
    66   assumes wf: "wf ((subcls1 G)^-1)"
    67     and cls: "class G C = Some (D, fs, ms)"
    68   shows "class_rec G C t f = f C fs ms (if C=Object then t else class_rec G D t f)"
    69 proof -
    70   from wf have step: "\<And>H a. wfrec ((subcls1 G)\<inverse>) H a =
    71     H (cut (wfrec ((subcls1 G)\<inverse>) H) ((subcls1 G)\<inverse>) a) a"
    72     by (rule wfrec)
    73   have cut: "\<And>f. C \<noteq> Object \<Longrightarrow> cut f ((subcls1 G)\<inverse>) C D = f D"
    74     by (rule cut_apply [where r="(subcls1 G)^-1", simplified, OF subcls1I, OF cls])
    75   from cls show ?thesis by (simp add: step cut class_rec_def)
    76 qed
    77 
    78 definition
    79   "wf_class G = wf ((subcls1 G)^-1)"
    80 
    81 
    82 
    83 text {* Code generator setup *}
    84 
    85 code_pred 
    86   (modes: i \<Rightarrow> i \<Rightarrow> o \<Rightarrow> bool, i \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool)
    87   subcls1p 
    88   .
    89 
    90 declare subcls1_def [code_pred_def]
    91 
    92 code_pred 
    93   (modes: i \<Rightarrow> i \<times> o \<Rightarrow> bool, i \<Rightarrow> i \<times> i \<Rightarrow> bool)
    94   [inductify]
    95   subcls1 
    96   .
    97 
    98 definition subcls' where "subcls' G = (subcls1p G)^**"
    99 
   100 code_pred
   101   (modes: i \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool, i \<Rightarrow> i \<Rightarrow> o \<Rightarrow> bool)
   102   [inductify]
   103   subcls'
   104   .
   105 
   106 lemma subcls_conv_subcls' [code_unfold]:
   107   "(subcls1 G)^* = {(C, D). subcls' G C D}"
   108 by(simp add: subcls'_def subcls1_def rtrancl_def)
   109 
   110 lemma class_rec_code [code]:
   111   "class_rec G C t f = 
   112   (if wf_class G then 
   113     (case class G C of
   114        None \<Rightarrow> class_rec G C t f
   115      | Some (D, fs, ms) \<Rightarrow> 
   116        if C = Object then f Object fs ms t else f C fs ms (class_rec G D t f))
   117    else class_rec G C t f)"
   118 apply(cases "wf_class G")
   119  apply(unfold class_rec_def wf_class_def)
   120  apply(subst wfrec, assumption)
   121  apply(cases "class G C")
   122   apply(simp add: wfrec)
   123  apply clarsimp
   124  apply(rename_tac D fs ms)
   125  apply(rule_tac f="f C fs ms" in arg_cong)
   126  apply(clarsimp simp add: cut_def)
   127  apply(blast intro: subcls1I)
   128 apply simp
   129 done
   130 
   131 lemma wf_class_code [code]:
   132   "wf_class G \<longleftrightarrow> (\<forall>(C, rest) \<in> set G. C \<noteq> Object \<longrightarrow> \<not> G \<turnstile> fst (the (class G C)) \<preceq>C C)"
   133 proof
   134   assume "wf_class G"
   135   hence wf: "wf (((subcls1 G)^+)^-1)" unfolding wf_class_def by(rule wf_converse_trancl)
   136   hence acyc: "acyclic ((subcls1 G)^+)" by(auto dest: wf_acyclic)
   137   show "\<forall>(C, rest) \<in> set G. C \<noteq> Object \<longrightarrow> \<not> G \<turnstile> fst (the (class G C)) \<preceq>C C"
   138   proof(safe)
   139     fix C D fs ms
   140     assume "(C, D, fs, ms) \<in> set G"
   141       and "C \<noteq> Object"
   142       and subcls: "G \<turnstile> fst (the (class G C)) \<preceq>C C"
   143     from `(C, D, fs, ms) \<in> set G` obtain D' fs' ms'
   144       where "class": "class G C = Some (D', fs', ms')"
   145       unfolding class_def by(auto dest!: weak_map_of_SomeI)
   146     hence "G \<turnstile> C \<prec>C1 D'" using `C \<noteq> Object` ..
   147     hence *: "(C, D') \<in> (subcls1 G)^+" ..
   148     also from * acyc have "C \<noteq> D'" by(auto simp add: acyclic_def)
   149     with subcls "class" have "(D', C) \<in> (subcls1 G)^+" by(auto dest: rtranclD)
   150     finally show False using acyc by(auto simp add: acyclic_def)
   151   qed
   152 next
   153   assume rhs[rule_format]: "\<forall>(C, rest) \<in> set G. C \<noteq> Object \<longrightarrow> \<not> G \<turnstile> fst (the (class G C)) \<preceq>C C"
   154   have "acyclic (subcls1 G)"
   155   proof(intro acyclicI strip notI)
   156     fix C
   157     assume "(C, C) \<in> (subcls1 G)\<^sup>+"
   158     thus False
   159     proof(cases)
   160       case base
   161       then obtain rest where "class G C = Some (C, rest)"
   162         and "C \<noteq> Object" by cases
   163       from `class G C = Some (C, rest)` have "(C, C, rest) \<in> set G"
   164         unfolding class_def by(rule map_of_SomeD)
   165       with `C \<noteq> Object` `class G C = Some (C, rest)`
   166       have "\<not> G \<turnstile> C \<preceq>C C" by(auto dest: rhs)
   167       thus False by simp
   168     next
   169       case (step D)
   170       from `G \<turnstile> D \<prec>C1 C` obtain rest where "class G D = Some (C, rest)"
   171         and "D \<noteq> Object" by cases
   172       from `class G D = Some (C, rest)` have "(D, C, rest) \<in> set G"
   173         unfolding class_def by(rule map_of_SomeD)
   174       with `D \<noteq> Object` `class G D = Some (C, rest)`
   175       have "\<not> G \<turnstile> C \<preceq>C D" by(auto dest: rhs)
   176       moreover from `(C, D) \<in> (subcls1 G)\<^sup>+`
   177       have "G \<turnstile> C \<preceq>C D" by(rule trancl_into_rtrancl)
   178       ultimately show False by contradiction
   179     qed
   180   qed
   181   thus "wf_class G" unfolding wf_class_def
   182     by(rule finite_acyclic_wf_converse[OF finite_subcls1])
   183 qed
   184 
   185 consts
   186   method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
   187   field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
   188   fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
   189 
   190 -- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
   191 defs method_def [code]: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
   192                            ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
   193 
   194 lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
   195   method (G,C) = (if C = Object then empty else method (G,D)) ++  
   196   map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
   197 apply (unfold method_def)
   198 apply (simp split del: split_if)
   199 apply (erule (1) class_rec_lemma [THEN trans])
   200 apply auto
   201 done
   202 
   203 
   204 -- "list of fields of a class, including inherited and hidden ones"
   205 defs fields_def [code]: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
   206                            map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
   207 
   208 lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
   209  fields (G,C) = 
   210   map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
   211 apply (unfold fields_def)
   212 apply (simp split del: split_if)
   213 apply (erule (1) class_rec_lemma [THEN trans])
   214 apply auto
   215 done
   216 
   217 
   218 defs field_def [code]: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
   219 
   220 lemma field_fields: 
   221 "field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
   222 apply (unfold field_def)
   223 apply (rule table_of_remap_SomeD)
   224 apply simp
   225 done
   226 
   227 
   228 -- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
   229 inductive
   230   widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
   231   for G :: "'c prog"
   232 where
   233   refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
   234 | subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
   235 | null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
   236 
   237 code_pred widen .
   238 
   239 lemmas refl = HOL.refl
   240 
   241 -- "casting conversion, cf. 5.5 / 5.1.5"
   242 -- "left out casts on primitve types"
   243 inductive
   244   cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
   245   for G :: "'c prog"
   246 where
   247   widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
   248 | subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
   249 
   250 lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
   251 apply (rule iffI)
   252 apply (erule widen.cases)
   253 apply auto
   254 done
   255 
   256 lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
   257 apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
   258 apply auto
   259 done
   260 
   261 lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
   262 apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
   263 apply auto
   264 done
   265 
   266 lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
   267 apply (ind_cases "G\<turnstile>Class C\<preceq>T")
   268 apply auto
   269 done
   270 
   271 lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
   272 apply (rule iffI)
   273 apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
   274 apply auto
   275 done
   276 
   277 lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
   278 apply (rule iffI)
   279 apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
   280 apply (auto elim: widen.subcls)
   281 done
   282 
   283 lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
   284 by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
   285 
   286 lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
   287 apply (rule iffI)
   288 apply (erule cast.cases)
   289 apply auto
   290 done
   291 
   292 lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
   293 apply (erule cast.cases)
   294 apply simp apply (erule widen.cases) 
   295 apply auto
   296 done
   297 
   298 theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   299 proof -
   300   assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
   301   proof induct
   302     case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
   303   next
   304     case (subcls C D T)
   305     then obtain E where "T = Class E" by (blast dest: widen_Class)
   306     with subcls show "G\<turnstile>Class C\<preceq>T" by auto
   307   next
   308     case (null R RT)
   309     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   310     thus "G\<turnstile>NT\<preceq>RT" by auto
   311   qed
   312 qed
   313 
   314 end