src/HOL/MicroJava/J/TypeRel.thy
author haftmann
Fri Jun 17 16:12:49 2005 +0200 (2005-06-17)
changeset 16417 9bc16273c2d4
parent 14952 47455995693d
child 18447 da548623916a
permissions -rw-r--r--
migrated theory headers to new format
     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 consts
    12   subcls1 :: "'c prog => (cname \<times> cname) set"  -- "subclass"
    13   widen   :: "'c prog => (ty    \<times> ty   ) set"  -- "widening"
    14   cast    :: "'c prog => (ty    \<times> ty   ) 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 => [ty   , ty   ] => 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 => [ty   , ty   ] => 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 -- "direct subclass, cf. 8.1.3"
    35 inductive "subcls1 G" intros
    36   subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G\<turnstile>C\<prec>C1D"
    37   
    38 lemma subcls1D: 
    39   "G\<turnstile>C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>fs ms. class G C = Some (D,fs,ms))"
    40 apply (erule subcls1.elims)
    41 apply auto
    42 done
    43 
    44 lemma subcls1_def2: 
    45   "subcls1 G = 
    46      (SIGMA C: {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 constdefs
    71   class_rec :: "'c prog \<Rightarrow> cname \<Rightarrow> 'a \<Rightarrow>
    72     (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
    73   "class_rec G == wfrec ((subcls1 G)^-1)
    74     (\<lambda>r C t f. case class G C of
    75          None \<Rightarrow> arbitrary
    76        | Some (D,fs,ms) \<Rightarrow> 
    77            f C fs ms (if C = Object then t else r D t f))"
    78 
    79 lemma class_rec_lemma: "wf ((subcls1 G)^-1) \<Longrightarrow> class G C = Some (D,fs,ms) \<Longrightarrow>
    80  class_rec G C t f = f C fs ms (if C=Object then t else class_rec G D t f)"
    81   by (simp add: class_rec_def wfrec cut_apply [OF converseI [OF subcls1I]])
    82 
    83 consts
    84 
    85   method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
    86   field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
    87   fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
    88 
    89 -- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
    90 defs method_def: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
    91                            ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
    92 
    93 lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
    94   method (G,C) = (if C = Object then empty else method (G,D)) ++  
    95   map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
    96 apply (unfold method_def)
    97 apply (simp split del: split_if)
    98 apply (erule (1) class_rec_lemma [THEN trans]);
    99 apply auto
   100 done
   101 
   102 
   103 -- "list of fields of a class, including inherited and hidden ones"
   104 defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
   105                            map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
   106 
   107 lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
   108  fields (G,C) = 
   109   map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
   110 apply (unfold fields_def)
   111 apply (simp split del: split_if)
   112 apply (erule (1) class_rec_lemma [THEN trans]);
   113 apply auto
   114 done
   115 
   116 
   117 defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
   118 
   119 lemma field_fields: 
   120 "field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
   121 apply (unfold field_def)
   122 apply (rule table_of_remap_SomeD)
   123 apply simp
   124 done
   125 
   126 
   127 -- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
   128 inductive "widen G" intros 
   129   refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
   130   subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
   131   null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
   132 
   133 -- "casting conversion, cf. 5.5 / 5.1.5"
   134 -- "left out casts on primitve types"
   135 inductive "cast G" intros
   136   widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
   137   subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
   138 
   139 lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
   140 apply (rule iffI)
   141 apply (erule widen.elims)
   142 apply auto
   143 done
   144 
   145 lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
   146 apply (ind_cases "G\<turnstile>S\<preceq>T")
   147 apply auto
   148 done
   149 
   150 lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
   151 apply (ind_cases "G\<turnstile>S\<preceq>T")
   152 apply auto
   153 done
   154 
   155 lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
   156 apply (ind_cases "G\<turnstile>S\<preceq>T")
   157 apply auto
   158 done
   159 
   160 lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
   161 apply (rule iffI)
   162 apply (ind_cases "G\<turnstile>S\<preceq>T")
   163 apply auto
   164 done
   165 
   166 lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
   167 apply (rule iffI)
   168 apply (ind_cases "G\<turnstile>S\<preceq>T")
   169 apply (auto elim: widen.subcls)
   170 done
   171 
   172 lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
   173 by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
   174 
   175 lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
   176 apply (rule iffI)
   177 apply (erule cast.elims)
   178 apply auto
   179 done
   180 
   181 lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
   182 apply (erule cast.cases)
   183 apply simp apply (erule widen.cases) 
   184 apply auto
   185 done
   186 
   187 theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   188 proof -
   189   assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
   190   proof induct
   191     case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
   192   next
   193     case (subcls C D T)
   194     then obtain E where "T = Class E" by (blast dest: widen_Class)
   195     with subcls show "G\<turnstile>Class C\<preceq>T" by (auto elim: rtrancl_trans)
   196   next
   197     case (null R RT)
   198     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   199     thus "G\<turnstile>NT\<preceq>RT" by auto
   200   qed
   201 qed
   202 
   203 end