src/HOL/MicroJava/J/TypeRel.thy
author krauss
Mon Aug 31 20:32:00 2009 +0200 (2009-08-31)
changeset 32461 eee4fa79398f
parent 28562 4e74209f113e
child 33954 1bc3b688548c
permissions -rw-r--r--
no consts_code for wfrec, as it violates the "code generation = equational reasoning" principle
     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 text {* Code generator setup (FIXME!) *}
   101 
   102 consts_code
   103   "wfrec"   ("\<module>wfrec?")
   104 attach {*
   105 fun wfrec f x = f (wfrec f) x;
   106 *}
   107 
   108 consts
   109 
   110   method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
   111   field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
   112   fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
   113 
   114 -- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
   115 defs method_def: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
   116                            ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
   117 
   118 lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
   119   method (G,C) = (if C = Object then empty else method (G,D)) ++  
   120   map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
   121 apply (unfold method_def)
   122 apply (simp split del: split_if)
   123 apply (erule (1) class_rec_lemma [THEN trans]);
   124 apply auto
   125 done
   126 
   127 
   128 -- "list of fields of a class, including inherited and hidden ones"
   129 defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
   130                            map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
   131 
   132 lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
   133  fields (G,C) = 
   134   map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
   135 apply (unfold fields_def)
   136 apply (simp split del: split_if)
   137 apply (erule (1) class_rec_lemma [THEN trans]);
   138 apply auto
   139 done
   140 
   141 
   142 defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
   143 
   144 lemma field_fields: 
   145 "field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
   146 apply (unfold field_def)
   147 apply (rule table_of_remap_SomeD)
   148 apply simp
   149 done
   150 
   151 
   152 -- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
   153 inductive
   154   widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
   155   for G :: "'c prog"
   156 where
   157   refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
   158 | subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
   159 | null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
   160 
   161 lemmas refl = HOL.refl
   162 
   163 -- "casting conversion, cf. 5.5 / 5.1.5"
   164 -- "left out casts on primitve types"
   165 inductive
   166   cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
   167   for G :: "'c prog"
   168 where
   169   widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
   170 | subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
   171 
   172 lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
   173 apply (rule iffI)
   174 apply (erule widen.cases)
   175 apply auto
   176 done
   177 
   178 lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
   179 apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
   180 apply auto
   181 done
   182 
   183 lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
   184 apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
   185 apply auto
   186 done
   187 
   188 lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
   189 apply (ind_cases "G\<turnstile>Class C\<preceq>T")
   190 apply auto
   191 done
   192 
   193 lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
   194 apply (rule iffI)
   195 apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
   196 apply auto
   197 done
   198 
   199 lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
   200 apply (rule iffI)
   201 apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
   202 apply (auto elim: widen.subcls)
   203 done
   204 
   205 lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
   206 by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
   207 
   208 lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
   209 apply (rule iffI)
   210 apply (erule cast.cases)
   211 apply auto
   212 done
   213 
   214 lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
   215 apply (erule cast.cases)
   216 apply simp apply (erule widen.cases) 
   217 apply auto
   218 done
   219 
   220 theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
   221 proof -
   222   assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
   223   proof induct
   224     case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
   225   next
   226     case (subcls C D T)
   227     then obtain E where "T = Class E" by (blast dest: widen_Class)
   228     with subcls show "G\<turnstile>Class C\<preceq>T" by auto
   229   next
   230     case (null R RT)
   231     then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
   232     thus "G\<turnstile>NT\<preceq>RT" by auto
   233   qed
   234 qed
   235 
   236 end