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