src/HOL/MicroJava/J/TypeRel.thy
author haftmann
Fri Oct 10 06:45:53 2008 +0200 (2008-10-10)
changeset 28562 4e74209f113e
parent 28524 644b62cf678f
child 32461 eee4fa79398f
permissions -rw-r--r--
`code func` now just `code`
     1 (*  Title:      HOL/MicroJava/J/TypeRel.thy
     2     ID:         $Id$
     3     Author:     David von Oheimb
     4     Copyright   1999 Technische Universitaet Muenchen
     5 *)
     6 
     7 header {* \isaheader{Relations between Java Types} *}
     8 
     9 theory TypeRel imports Decl begin
    10 
    11 -- "direct subclass, cf. 8.1.3"
    12 inductive
    13   subcls1 :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
    14   for G :: "'c prog"
    15 where
    16   subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G\<turnstile>C\<prec>C1D"
    17 
    18 abbreviation
    19   subcls  :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>C _"  [71,71,71] 70)
    20   where "G\<turnstile>C \<preceq>C  D \<equiv> (subcls1 G)^** C D"
    21   
    22 lemma subcls1D: 
    23   "G\<turnstile>C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>fs ms. class G C = Some (D,fs,ms))"
    24 apply (erule subcls1.cases)
    25 apply auto
    26 done
    27 
    28 lemma subcls1_def2: 
    29   "subcls1 G = (\<lambda>C D. (C, D) \<in>
    30      (SIGMA C: {C. is_class G C} . {D. C\<noteq>Object \<and> fst (the (class G C))=D}))"
    31   by (auto simp add: is_class_def expand_fun_eq dest: subcls1D intro: subcls1I)
    32 
    33 lemma finite_subcls1: "finite {(C, D). subcls1 G C D}"
    34 apply(simp add: subcls1_def2 del: mem_Sigma_iff)
    35 apply(rule finite_SigmaI [OF finite_is_class])
    36 apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
    37 apply  auto
    38 done
    39 
    40 lemma subcls_is_class: "(subcls1 G)^++ C D ==> is_class G C"
    41 apply (unfold is_class_def)
    42 apply(erule tranclp_trans_induct)
    43 apply (auto dest!: subcls1D)
    44 done
    45 
    46 lemma subcls_is_class2 [rule_format (no_asm)]: 
    47   "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
    48 apply (unfold is_class_def)
    49 apply (erule rtranclp_induct)
    50 apply  (drule_tac [2] subcls1D)
    51 apply  auto
    52 done
    53 
    54 constdefs
    55   class_rec :: "'c prog \<Rightarrow> cname \<Rightarrow> 'a \<Rightarrow>
    56     (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
    57   "class_rec G == wfrec {(C, D). (subcls1 G)^--1 C D}
    58     (\<lambda>r C t f. case class G C of
    59          None \<Rightarrow> undefined
    60        | Some (D,fs,ms) \<Rightarrow> 
    61            f C fs ms (if C = Object then t else r D t f))"
    62 
    63 lemma class_rec_lemma: "wfP ((subcls1 G)^--1) \<Longrightarrow> class G C = Some (D,fs,ms) \<Longrightarrow>
    64  class_rec G C t f = f C fs ms (if C=Object then t else class_rec G D t f)"
    65   by (simp add: class_rec_def wfrec [to_pred, where r="(subcls1 G)^--1", simplified]
    66     cut_apply [where r="{(C, D). subcls1 G D C}", simplified, OF subcls1I])
    67 
    68 definition
    69   "wf_class G = wfP ((subcls1 G)^--1)"
    70 
    71 lemma class_rec_func (*[code]*):
    72   "class_rec G C t f = (if wf_class G then
    73     (case class G C
    74       of None \<Rightarrow> undefined
    75        | Some (D, fs, ms) \<Rightarrow> f C fs ms (if C = Object then t else class_rec G D t f))
    76     else class_rec G C t f)"
    77 proof (cases "wf_class G")
    78   case False then show ?thesis by auto
    79 next
    80   case True
    81   from `wf_class G` have wf: "wfP ((subcls1 G)^--1)"
    82     unfolding wf_class_def .
    83   show ?thesis
    84   proof (cases "class G C")
    85     case None
    86     with wf show ?thesis
    87       by (simp add: class_rec_def wfrec [to_pred, where r="(subcls1 G)^--1", simplified]
    88         cut_apply [where r="{(C, D).subcls1 G D C}", simplified, OF subcls1I])
    89   next
    90     case (Some x) show ?thesis
    91     proof (cases x)
    92       case (fields D fs ms)
    93       then have is_some: "class G C = Some (D, fs, ms)" using Some by simp
    94       note class_rec = class_rec_lemma [OF wf is_some]
    95       show ?thesis unfolding class_rec by (simp add: is_some)
    96     qed
    97   qed
    98 qed
    99 
   100 consts
   101 
   102   method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
   103   field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
   104   fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
   105 
   106 -- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
   107 defs method_def: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
   108                            ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
   109 
   110 lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
   111   method (G,C) = (if C = Object then empty else method (G,D)) ++  
   112   map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
   113 apply (unfold method_def)
   114 apply (simp split del: split_if)
   115 apply (erule (1) class_rec_lemma [THEN trans]);
   116 apply auto
   117 done
   118 
   119 
   120 -- "list of fields of a class, including inherited and hidden ones"
   121 defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
   122                            map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
   123 
   124 lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
   125  fields (G,C) = 
   126   map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
   127 apply (unfold fields_def)
   128 apply (simp split del: split_if)
   129 apply (erule (1) class_rec_lemma [THEN trans]);
   130 apply auto
   131 done
   132 
   133 
   134 defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
   135 
   136 lemma field_fields: 
   137 "field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
   138 apply (unfold field_def)
   139 apply (rule table_of_remap_SomeD)
   140 apply simp
   141 done
   142 
   143 
   144 -- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
   145 inductive
   146   widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
   147   for G :: "'c prog"
   148 where
   149   refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
   150 | subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
   151 | null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
   152 
   153 lemmas refl = HOL.refl
   154 
   155 -- "casting conversion, cf. 5.5 / 5.1.5"
   156 -- "left out casts on primitve types"
   157 inductive
   158   cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
   159   for G :: "'c prog"
   160 where
   161   widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
   162 | subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
   163 
   164 lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
   165 apply (rule iffI)
   166 apply (erule widen.cases)
   167 apply auto
   168 done
   169 
   170 lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
   171 apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
   172 apply auto
   173 done
   174 
   175 lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
   176 apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
   177 apply auto
   178 done
   179 
   180 lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
   181 apply (ind_cases "G\<turnstile>Class C\<preceq>T")
   182 apply auto
   183 done
   184 
   185 lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
   186 apply (rule iffI)
   187 apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
   188 apply auto
   189 done
   190 
   191 lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
   192 apply (rule iffI)
   193 apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
   194 apply (auto elim: widen.subcls)
   195 done
   196 
   197 lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
   198 by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
   199 
   200 lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
   201 apply (rule iffI)
   202 apply (erule cast.cases)
   203 apply auto
   204 done
   205 
   206 lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
   207 apply (erule cast.cases)
   208 apply simp apply (erule widen.cases) 
   209 apply auto
   210 done
   211 
   212 theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   213 proof -
   214   assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
   215   proof induct
   216     case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
   217   next
   218     case (subcls C D T)
   219     then obtain E where "T = Class E" by (blast dest: widen_Class)
   220     with subcls show "G\<turnstile>Class C\<preceq>T" by auto
   221   next
   222     case (null R RT)
   223     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   224     thus "G\<turnstile>NT\<preceq>RT" by auto
   225   qed
   226 qed
   227 
   228 end