src/HOL/MicroJava/J/TypeRel.thy
author wenzelm
Tue Oct 30 17:37:25 2001 +0100 (2001-10-30)
changeset 11987 bf31b35949ce
parent 11372 648795477bb5
child 12443 e56ab6134b41
permissions -rw-r--r--
tuned induct proofs;
     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 "Relations between Java Types"
     8 
     9 theory TypeRel = Decl:
    10 
    11 consts
    12   subcls1 :: "'c prog => (cname \<times> cname) set"  (* subclass *)
    13   widen   :: "'c prog => (ty    \<times> ty   ) set"  (* widening *)
    14   cast    :: "'c prog => (cname \<times> cname) set"  (* casting *)
    15 
    16 syntax (xsymbols)
    17   subcls1 :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
    18   subcls  :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>C _"  [71,71,71] 70)
    19   widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
    20   cast    :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
    21 
    22 syntax
    23   subcls1 :: "'c prog => [cname, cname] => bool" ("_ |- _ <=C1 _" [71,71,71] 70)
    24   subcls  :: "'c prog => [cname, cname] => bool" ("_ |- _ <=C _"  [71,71,71] 70)
    25   widen   :: "'c prog => [ty   , ty   ] => bool" ("_ |- _ <= _"   [71,71,71] 70)
    26   cast    :: "'c prog => [cname, cname] => bool" ("_ |- _ <=? _"  [71,71,71] 70)
    27 
    28 translations
    29   "G\<turnstile>C \<prec>C1 D" == "(C,D) \<in> subcls1 G"
    30   "G\<turnstile>C \<preceq>C  D" == "(C,D) \<in> (subcls1 G)^*"
    31   "G\<turnstile>S \<preceq>   T" == "(S,T) \<in> widen   G"
    32   "G\<turnstile>C \<preceq>?  D" == "(C,D) \<in> cast    G"
    33 
    34 defs
    35 
    36   (* direct subclass, cf. 8.1.3 *)
    37  subcls1_def: "subcls1 G \<equiv> {(C,D). C\<noteq>Object \<and> (\<exists>c. class G C=Some c \<and> fst c=D)}"
    38   
    39 lemma subcls1D: 
    40   "G\<turnstile>C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>fs ms. class G C = Some (D,fs,ms))"
    41 apply (unfold subcls1_def)
    42 apply auto
    43 done
    44 
    45 lemma subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G\<turnstile>C\<prec>C1D"
    46 apply (unfold subcls1_def)
    47 apply auto
    48 done
    49 
    50 lemma subcls1_def2: 
    51 "subcls1 G = (\<Sigma>C\<in>{C. is_class G C} . {D. C\<noteq>Object \<and> fst (the (class G C))=D})"
    52 apply (unfold subcls1_def is_class_def)
    53 apply auto
    54 done
    55 
    56 lemma finite_subcls1: "finite (subcls1 G)"
    57 apply(subst subcls1_def2)
    58 apply(rule finite_SigmaI [OF finite_is_class])
    59 apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
    60 apply  auto
    61 done
    62 
    63 lemma subcls_is_class: "(C,D) \<in> (subcls1 G)^+ ==> is_class G C"
    64 apply (unfold is_class_def)
    65 apply(erule trancl_trans_induct)
    66 apply (auto dest!: subcls1D)
    67 done
    68 
    69 lemma subcls_is_class2 [rule_format (no_asm)]: 
    70   "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
    71 apply (unfold is_class_def)
    72 apply (erule rtrancl_induct)
    73 apply  (drule_tac [2] subcls1D)
    74 apply  auto
    75 done
    76 
    77 consts class_rec ::"'c prog \<times> cname \<Rightarrow> 
    78         'a \<Rightarrow> (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
    79 
    80 recdef class_rec "same_fst (\<lambda>G. wf ((subcls1 G)^-1)) (\<lambda>G. (subcls1 G)^-1)"
    81       "class_rec (G,C) = (\<lambda>t f. case class G C of None \<Rightarrow> arbitrary 
    82                          | Some (D,fs,ms) \<Rightarrow> if wf ((subcls1 G)^-1) then 
    83       f C fs ms (if C = Object then t else class_rec (G,D) t f) else arbitrary)"
    84 (hints intro: subcls1I)
    85 
    86 declare class_rec.simps [simp del]
    87 
    88 
    89 lemma class_rec_lemma: "\<lbrakk> wf ((subcls1 G)^-1); class G C = Some (D,fs,ms)\<rbrakk> \<Longrightarrow>
    90  class_rec (G,C) t f = f C fs ms (if C=Object then t else class_rec (G,D) t f)";
    91   apply (rule class_rec.simps [THEN trans [THEN fun_cong [THEN fun_cong]]])
    92   apply simp
    93   done
    94 
    95 consts
    96 
    97   method :: "'c prog \<times> cname => ( sig   \<leadsto> cname \<times> ty \<times> 'c)" (* ###curry *)
    98   field  :: "'c prog \<times> cname => ( vname \<leadsto> cname \<times> ty     )" (* ###curry *)
    99   fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
   100 
   101 (* methods of a class, with inheritance, overriding and hiding, cf. 8.4.6 *)
   102 defs method_def: "method \<equiv> \<lambda>(G,C). class_rec (G,C) empty (\<lambda>C fs ms ts.
   103                            ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
   104 
   105 lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
   106   method (G,C) = (if C = Object then empty else method (G,D)) ++  
   107   map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
   108 apply (unfold method_def)
   109 apply (simp split del: split_if)
   110 apply (erule (1) class_rec_lemma [THEN trans]);
   111 apply auto
   112 done
   113 
   114 
   115 (* list of fields of a class, including inherited and hidden ones *)
   116 defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec (G,C) []    (\<lambda>C fs ms ts.
   117                            map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
   118 
   119 lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
   120  fields (G,C) = 
   121   map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
   122 apply (unfold fields_def)
   123 apply (simp split del: split_if)
   124 apply (erule (1) class_rec_lemma [THEN trans]);
   125 apply auto
   126 done
   127 
   128 
   129 defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
   130 
   131 lemma field_fields: 
   132 "field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
   133 apply (unfold field_def)
   134 apply (rule table_of_remap_SomeD)
   135 apply simp
   136 done
   137 
   138 
   139 inductive "widen G" intros (*widening, viz. method invocation conversion,cf. 5.3
   140 			     i.e. sort of syntactic subtyping *)
   141   refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T" 	 (* identity conv., cf. 5.1.1 *)
   142   subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
   143   null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
   144 
   145 inductive "cast G" intros (* casting conversion, cf. 5.5 / 5.1.5 *)
   146                           (* left out casts on primitve types    *)
   147   widen:  "G\<turnstile>C\<preceq>C D ==> G\<turnstile>C \<preceq>? D"
   148   subcls: "G\<turnstile>D\<preceq>C C ==> G\<turnstile>C \<preceq>? D"
   149 
   150 lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
   151 apply (rule iffI)
   152 apply (erule widen.elims)
   153 apply auto
   154 done
   155 
   156 lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
   157 apply (ind_cases "G\<turnstile>S\<preceq>T")
   158 apply auto
   159 done
   160 
   161 lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
   162 apply (ind_cases "G\<turnstile>S\<preceq>T")
   163 apply auto
   164 done
   165 
   166 lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
   167 apply (ind_cases "G\<turnstile>S\<preceq>T")
   168 apply auto
   169 done
   170 
   171 lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
   172 apply (rule iffI)
   173 apply (ind_cases "G\<turnstile>S\<preceq>T")
   174 apply auto
   175 done
   176 
   177 lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
   178 apply (rule iffI)
   179 apply (ind_cases "G\<turnstile>S\<preceq>T")
   180 apply (auto elim: widen.subcls)
   181 done
   182 
   183 lemma widen_trans [rule_format (no_asm)]: "G\<turnstile>S\<preceq>U ==> \<forall>T. G\<turnstile>U\<preceq>T --> G\<turnstile>S\<preceq>T"
   184 apply (erule widen.induct)
   185 apply   safe
   186 apply  (frule widen_Class)
   187 apply  (frule_tac [2] widen_RefT)
   188 apply  safe
   189 apply(erule (1) rtrancl_trans)
   190 done
   191 
   192 
   193 (*####theorem widen_trans: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   194 proof -
   195   assume "G\<turnstile>S\<preceq>U"
   196   thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T" ( *(is "PROP ?P S U")* )
   197   proof (induct ( *cases* ) (open) ( *?P S U* ) rule: widen.induct [consumes 1])
   198     case refl
   199     fix T' assume "G\<turnstile>T\<preceq>T'" thus "G\<turnstile>T\<preceq>T'".
   200       ( * fix T' show "PROP ?P T T".* )
   201   next
   202     case subcls
   203     fix T assume "G\<turnstile>Class D\<preceq>T"
   204     then obtain E where "T = Class E" by (blast dest: widen_Class)
   205     from prems show "G\<turnstile>Class C\<preceq>T" proof (auto elim: rtrancl_trans) qed
   206   next
   207     case null
   208     fix RT assume "G\<turnstile>RefT R\<preceq>RT"
   209     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   210     thus "G\<turnstile>NT\<preceq>RT" by auto
   211   qed
   212 qed
   213 *)
   214 
   215 theorem widen_trans: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   216 proof -
   217   assume "G\<turnstile>S\<preceq>U"
   218   thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
   219   proof induct
   220     case (refl T T')
   221     thus "G\<turnstile>T\<preceq>T'" .
   222   next
   223     case (subcls C D T)
   224     then obtain E where "T = Class E" by (blast dest: widen_Class)
   225     with subcls show "G\<turnstile>Class C\<preceq>T" by (auto elim: rtrancl_trans)
   226   next
   227     case (null R RT)
   228     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   229     thus "G\<turnstile>NT\<preceq>RT" by auto
   230   qed
   231 qed
   232 
   233 end