src/HOL/MicroJava/J/TypeRel.thy
author oheimb
Thu Feb 01 20:53:13 2001 +0100 (2001-02-01)
changeset 11026 a50365d21144
parent 10613 78b1d6c3ee9c
child 11070 cc421547e744
permissions -rw-r--r--
converted to Isar, simplifying recursion on class hierarchy
     1 (*  Title:      HOL/MicroJava/J/TypeRel.thy
     2     ID:         $Id$
     3     Author:     David von Oheimb
     4     Copyright   1999 Technische Universitaet Muenchen
     5 
     6 The relations between Java types
     7 *)
     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
    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 (HTML)
    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)]: "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
    70 apply (unfold is_class_def)
    71 apply (erule rtrancl_induct)
    72 apply  (drule_tac [2] subcls1D)
    73 apply  auto
    74 done
    75 
    76 consts class_rec ::"'c prog \<times> cname \<Rightarrow> 
    77         'a \<Rightarrow> (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
    78 recdef class_rec "same_fst (\<lambda>G. wf ((subcls1 G)^-1)) (\<lambda>G. (subcls1 G)^-1)"
    79       "class_rec (G,C) = (\<lambda>t f. case class G C of None \<Rightarrow> arbitrary 
    80                          | Some (D,fs,ms) \<Rightarrow> if wf ((subcls1 G)^-1) then 
    81       f C fs ms (if C = Object then t else class_rec (G,D) t f) else arbitrary)"
    82 recdef_tc class_rec_tc: class_rec
    83   apply (unfold same_fst_def)
    84   apply (auto intro: subcls1I)
    85   done
    86 
    87 lemma class_rec_lemma: "\<lbrakk> wf ((subcls1 G)^-1); class G C = Some (D,fs,ms)\<rbrakk> \<Longrightarrow>
    88  class_rec (G,C) t f = f C fs ms (if C=Object then t else class_rec (G,D) t f)";
    89   apply (rule class_rec_tc [THEN class_rec.simps 
    90               [THEN trans [THEN fun_cong [THEN fun_cong]]]])
    91   apply (rule ext, rule ext)
    92   apply auto
    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 ML {* InductAttrib.print_global_rules(the_context()) *}
   193 ML {* set show_tags *}
   194 
   195 (*####theorem widen_trans: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   196 proof -
   197   assume "G\<turnstile>S\<preceq>U"
   198   thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T" (*(is "PROP ?P S U")*)
   199   proof (induct (*cases*) (open) (*?P S U*) rule: widen.induct [consumes 1])
   200     case refl
   201     fix T' assume "G\<turnstile>T\<preceq>T'" thus "G\<turnstile>T\<preceq>T'".
   202       (* fix T' show "PROP ?P T T".*)
   203   next
   204     case subcls
   205     fix T assume "G\<turnstile>Class D\<preceq>T"
   206     then obtain E where "T = Class E" by (blast dest: widen_Class)
   207     from prems show "G\<turnstile>Class C\<preceq>T" proof (auto elim: rtrancl_trans) qed
   208   next
   209     case null
   210     fix RT assume "G\<turnstile>RefT R\<preceq>RT"
   211     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   212     thus "G\<turnstile>NT\<preceq>RT" by auto
   213   qed
   214 qed
   215 *)
   216 
   217 theorem widen_trans: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   218 proof -
   219   assume "G\<turnstile>S\<preceq>U"
   220   thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T" (*(is "PROP ?P S U")*)
   221   proof (induct (*cases*) (open) (*?P S U*)) (* rule: widen.induct *)
   222     case refl
   223     fix T' assume "G\<turnstile>T\<preceq>T'" thus "G\<turnstile>T\<preceq>T'".
   224       (* fix T' show "PROP ?P T T".*)
   225   next
   226     case subcls
   227     fix T assume "G\<turnstile>Class D\<preceq>T"
   228     then obtain E where "T = Class E" by (blast dest: widen_Class)
   229     from prems show "G\<turnstile>Class C\<preceq>T" proof (auto elim: rtrancl_trans) qed
   230   next
   231     case null
   232     fix RT assume "G\<turnstile>RefT R\<preceq>RT"
   233     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   234     thus "G\<turnstile>NT\<preceq>RT" by auto
   235   qed
   236 qed
   237 
   238 
   239 
   240 end