src/HOL/MicroJava/J/TypeRel.thy
author wenzelm
Mon Jan 11 21:21:02 2016 +0100 (2016-01-11)
changeset 62145 5b946c81dfbf
parent 62042 6c6ccf573479
child 62390 842917225d56
permissions -rw-r--r--
eliminated old defs;
     1 (*  Title:      HOL/MicroJava/J/TypeRel.thy
     2     Author:     David von Oheimb, Technische Universitaet Muenchen
     3 *)
     4 
     5 section \<open>Relations between Java Types\<close>
     6 
     7 theory TypeRel
     8 imports Decl
     9 begin
    10 
    11 \<comment> "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   by (subst wfrec_def_adm[OF class_rec_def])
    70      (auto simp: assms adm_wf_def fun_eq_iff subcls1I split: option.split)
    71 
    72 definition
    73   "wf_class G = wf ((subcls1 G)^-1)"
    74 
    75 
    76 
    77 text \<open>Code generator setup\<close>
    78 
    79 code_pred 
    80   (modes: i \<Rightarrow> i \<Rightarrow> o \<Rightarrow> bool, i \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool)
    81   subcls1p 
    82   .
    83 
    84 declare subcls1_def [code_pred_def]
    85 
    86 code_pred 
    87   (modes: i \<Rightarrow> i \<times> o \<Rightarrow> bool, i \<Rightarrow> i \<times> i \<Rightarrow> bool)
    88   [inductify]
    89   subcls1 
    90   .
    91 
    92 definition subcls' where "subcls' G = (subcls1p G)^**"
    93 
    94 code_pred
    95   (modes: i \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool, i \<Rightarrow> i \<Rightarrow> o \<Rightarrow> bool)
    96   [inductify]
    97   subcls'
    98   .
    99 
   100 lemma subcls_conv_subcls' [code_unfold]:
   101   "(subcls1 G)^* = {(C, D). subcls' G C D}"
   102 by(simp add: subcls'_def subcls1_def rtrancl_def)
   103 
   104 lemma class_rec_code [code]:
   105   "class_rec G C t f = 
   106   (if wf_class G then 
   107     (case class G C of
   108        None \<Rightarrow> class_rec G C t f
   109      | Some (D, fs, ms) \<Rightarrow> 
   110        if C = Object then f Object fs ms t else f C fs ms (class_rec G D t f))
   111    else class_rec G C t f)"
   112 apply(cases "wf_class G")
   113  apply(unfold class_rec_def wf_class_def)
   114  apply(subst wfrec, assumption)
   115  apply(cases "class G C")
   116   apply(simp add: wfrec)
   117  apply clarsimp
   118  apply(rename_tac D fs ms)
   119  apply(rule_tac f="f C fs ms" in arg_cong)
   120  apply(clarsimp simp add: cut_def)
   121  apply(blast intro: subcls1I)
   122 apply simp
   123 done
   124 
   125 lemma wf_class_code [code]:
   126   "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)"
   127 proof
   128   assume "wf_class G"
   129   hence wf: "wf (((subcls1 G)^+)^-1)" unfolding wf_class_def by(rule wf_converse_trancl)
   130   hence acyc: "acyclic ((subcls1 G)^+)" by(auto dest: wf_acyclic)
   131   show "\<forall>(C, rest) \<in> set G. C \<noteq> Object \<longrightarrow> \<not> G \<turnstile> fst (the (class G C)) \<preceq>C C"
   132   proof(safe)
   133     fix C D fs ms
   134     assume "(C, D, fs, ms) \<in> set G"
   135       and "C \<noteq> Object"
   136       and subcls: "G \<turnstile> fst (the (class G C)) \<preceq>C C"
   137     from \<open>(C, D, fs, ms) \<in> set G\<close> obtain D' fs' ms'
   138       where "class": "class G C = Some (D', fs', ms')"
   139       unfolding class_def by(auto dest!: weak_map_of_SomeI)
   140     hence "G \<turnstile> C \<prec>C1 D'" using \<open>C \<noteq> Object\<close> ..
   141     hence *: "(C, D') \<in> (subcls1 G)^+" ..
   142     also from * acyc have "C \<noteq> D'" by(auto simp add: acyclic_def)
   143     with subcls "class" have "(D', C) \<in> (subcls1 G)^+" by(auto dest: rtranclD)
   144     finally show False using acyc by(auto simp add: acyclic_def)
   145   qed
   146 next
   147   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"
   148   have "acyclic (subcls1 G)"
   149   proof(intro acyclicI strip notI)
   150     fix C
   151     assume "(C, C) \<in> (subcls1 G)\<^sup>+"
   152     thus False
   153     proof(cases)
   154       case base
   155       then obtain rest where "class G C = Some (C, rest)"
   156         and "C \<noteq> Object" by cases
   157       from \<open>class G C = Some (C, rest)\<close> have "(C, C, rest) \<in> set G"
   158         unfolding class_def by(rule map_of_SomeD)
   159       with \<open>C \<noteq> Object\<close> \<open>class G C = Some (C, rest)\<close>
   160       have "\<not> G \<turnstile> C \<preceq>C C" by(auto dest: rhs)
   161       thus False by simp
   162     next
   163       case (step D)
   164       from \<open>G \<turnstile> D \<prec>C1 C\<close> obtain rest where "class G D = Some (C, rest)"
   165         and "D \<noteq> Object" by cases
   166       from \<open>class G D = Some (C, rest)\<close> have "(D, C, rest) \<in> set G"
   167         unfolding class_def by(rule map_of_SomeD)
   168       with \<open>D \<noteq> Object\<close> \<open>class G D = Some (C, rest)\<close>
   169       have "\<not> G \<turnstile> C \<preceq>C D" by(auto dest: rhs)
   170       moreover from \<open>(C, D) \<in> (subcls1 G)\<^sup>+\<close>
   171       have "G \<turnstile> C \<preceq>C D" by(rule trancl_into_rtrancl)
   172       ultimately show False by contradiction
   173     qed
   174   qed
   175   thus "wf_class G" unfolding wf_class_def
   176     by(rule finite_acyclic_wf_converse[OF finite_subcls1])
   177 qed
   178 
   179 definition "method" :: "'c prog \<times> cname => (sig \<rightharpoonup> cname \<times> ty \<times> 'c)"
   180   \<comment> "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
   181   where [code]: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
   182                            ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
   183 
   184 definition fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list"
   185   \<comment> "list of fields of a class, including inherited and hidden ones"
   186   where [code]: "fields \<equiv> \<lambda>(G,C). class_rec G C [] (\<lambda>C fs ms ts.
   187                            map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
   188 
   189 definition field :: "'c prog \<times> cname => (vname \<rightharpoonup> cname \<times> ty)"
   190   where [code]: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
   191 
   192 lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
   193   method (G,C) = (if C = Object then empty else method (G,D)) ++  
   194   map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
   195 apply (unfold method_def)
   196 apply (simp split del: split_if)
   197 apply (erule (1) class_rec_lemma [THEN trans])
   198 apply auto
   199 done
   200 
   201 lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
   202  fields (G,C) = 
   203   map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
   204 apply (unfold fields_def)
   205 apply (simp split del: split_if)
   206 apply (erule (1) class_rec_lemma [THEN trans])
   207 apply auto
   208 done
   209 
   210 lemma field_fields: 
   211 "field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
   212 apply (unfold field_def)
   213 apply (rule table_of_remap_SomeD)
   214 apply simp
   215 done
   216 
   217 
   218 \<comment> "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
   219 inductive
   220   widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
   221   for G :: "'c prog"
   222 where
   223   refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   \<comment> "identity conv., cf. 5.1.1"
   224 | subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
   225 | null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
   226 
   227 code_pred widen .
   228 
   229 lemmas refl = HOL.refl
   230 
   231 \<comment> "casting conversion, cf. 5.5 / 5.1.5"
   232 \<comment> "left out casts on primitve types"
   233 inductive
   234   cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
   235   for G :: "'c prog"
   236 where
   237   widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
   238 | subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
   239 
   240 lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
   241 apply (rule iffI)
   242 apply (erule widen.cases)
   243 apply auto
   244 done
   245 
   246 lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
   247 apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
   248 apply auto
   249 done
   250 
   251 lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
   252 apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
   253 apply auto
   254 done
   255 
   256 lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
   257 apply (ind_cases "G\<turnstile>Class C\<preceq>T")
   258 apply auto
   259 done
   260 
   261 lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
   262 apply (rule iffI)
   263 apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
   264 apply auto
   265 done
   266 
   267 lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
   268 apply (rule iffI)
   269 apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
   270 apply (auto elim: widen.subcls)
   271 done
   272 
   273 lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
   274 by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
   275 
   276 lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
   277 apply (rule iffI)
   278 apply (erule cast.cases)
   279 apply auto
   280 done
   281 
   282 lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
   283 apply (erule cast.cases)
   284 apply simp apply (erule widen.cases) 
   285 apply auto
   286 done
   287 
   288 theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   289 proof -
   290   assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
   291   proof induct
   292     case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
   293   next
   294     case (subcls C D T)
   295     then obtain E where "T = Class E" by (blast dest: widen_Class)
   296     with subcls show "G\<turnstile>Class C\<preceq>T" by auto
   297   next
   298     case (null R RT)
   299     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   300     thus "G\<turnstile>NT\<preceq>RT" by auto
   301   qed
   302 qed
   303 
   304 end