src/HOL/MicroJava/J/TypeRel.thy
changeset 23757 087b0a241557
parent 22597 284b2183d070
child 28524 644b62cf678f
equal deleted inserted replaced
23756:14008ce7df96 23757:087b0a241557
     7 header {* \isaheader{Relations between Java Types} *}
     7 header {* \isaheader{Relations between Java Types} *}
     8 
     8 
     9 theory TypeRel imports Decl begin
     9 theory TypeRel imports Decl begin
    10 
    10 
    11 -- "direct subclass, cf. 8.1.3"
    11 -- "direct subclass, cf. 8.1.3"
    12 inductive2
    12 inductive
    13   subcls1 :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
    13   subcls1 :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
    14   for G :: "'c prog"
    14   for G :: "'c prog"
    15 where
    15 where
    16   subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G\<turnstile>C\<prec>C1D"
    16   subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G\<turnstile>C\<prec>C1D"
    17 
    17 
    24 apply (erule subcls1.cases)
    24 apply (erule subcls1.cases)
    25 apply auto
    25 apply auto
    26 done
    26 done
    27 
    27 
    28 lemma subcls1_def2: 
    28 lemma subcls1_def2: 
    29   "subcls1 G = member2
    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})"
    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)
    31   by (auto simp add: is_class_def expand_fun_eq dest: subcls1D intro: subcls1I)
    32 
    32 
    33 lemma finite_subcls1: "finite (Collect2 (subcls1 G))"
    33 lemma finite_subcls1: "finite {(C, D). subcls1 G C D}"
    34 apply(simp add: subcls1_def2)
    34 apply(simp add: subcls1_def2 del: mem_Sigma_iff)
    35 apply(rule finite_SigmaI [OF finite_is_class])
    35 apply(rule finite_SigmaI [OF finite_is_class])
    36 apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
    36 apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
    37 apply  auto
    37 apply  auto
    38 done
    38 done
    39 
    39 
    40 lemma subcls_is_class: "(subcls1 G)^++ C D ==> is_class G C"
    40 lemma subcls_is_class: "(subcls1 G)^++ C D ==> is_class G C"
    41 apply (unfold is_class_def)
    41 apply (unfold is_class_def)
    42 apply(erule trancl_trans_induct')
    42 apply(erule tranclp_trans_induct)
    43 apply (auto dest!: subcls1D)
    43 apply (auto dest!: subcls1D)
    44 done
    44 done
    45 
    45 
    46 lemma subcls_is_class2 [rule_format (no_asm)]: 
    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"
    47   "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
    48 apply (unfold is_class_def)
    48 apply (unfold is_class_def)
    49 apply (erule rtrancl_induct')
    49 apply (erule rtranclp_induct)
    50 apply  (drule_tac [2] subcls1D)
    50 apply  (drule_tac [2] subcls1D)
    51 apply  auto
    51 apply  auto
    52 done
    52 done
    53 
    53 
    54 constdefs
    54 constdefs
    55   class_rec :: "'c prog \<Rightarrow> cname \<Rightarrow> 'a \<Rightarrow>
    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"
    56     (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
    57   "class_rec G == wfrec (Collect2 ((subcls1 G)^--1))
    57   "class_rec G == wfrec {(C, D). (subcls1 G)^--1 C D}
    58     (\<lambda>r C t f. case class G C of
    58     (\<lambda>r C t f. case class G C of
    59          None \<Rightarrow> arbitrary
    59          None \<Rightarrow> arbitrary
    60        | Some (D,fs,ms) \<Rightarrow> 
    60        | Some (D,fs,ms) \<Rightarrow> 
    61            f C fs ms (if C = Object then t else r D t f))"
    61            f C fs ms (if C = Object then t else r D t f))"
    62 
    62 
    63 lemma class_rec_lemma: "wfP ((subcls1 G)^--1) \<Longrightarrow> class G C = Some (D,fs,ms) \<Longrightarrow>
    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)"
    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]
    65   by (simp add: class_rec_def wfrec [to_pred, where r="(subcls1 G)^--1", simplified]
    66     cut_apply [OF Collect2I [where P="(subcls1 G)^--1"], OF conversepI, OF subcls1I])
    66     cut_apply [where r="{(C, D). subcls1 G D C}", simplified, OF subcls1I])
    67 
    67 
    68 definition
    68 definition
    69   "wf_class G = wfP ((subcls1 G)^--1)"
    69   "wf_class G = wfP ((subcls1 G)^--1)"
    70 
    70 
    71 lemma class_rec_func [code func]:
    71 lemma class_rec_func [code func]:
    82     unfolding wf_class_def .
    82     unfolding wf_class_def .
    83   show ?thesis
    83   show ?thesis
    84   proof (cases "class G C")
    84   proof (cases "class G C")
    85     case None
    85     case None
    86     with wf show ?thesis
    86     with wf show ?thesis
    87       by (simp add: class_rec_def wfrec [to_pred]
    87       by (simp add: class_rec_def wfrec [to_pred, where r="(subcls1 G)^--1", simplified]
    88         cut_apply [OF Collect2I [where P="(subcls1 G)^--1"], OF conversepI, OF subcls1I])
    88         cut_apply [where r="{(C, D).subcls1 G D C}", simplified, OF subcls1I])
    89   next
    89   next
    90     case (Some x) show ?thesis
    90     case (Some x) show ?thesis
    91     proof (cases x)
    91     proof (cases x)
    92       case (fields D fs ms)
    92       case (fields D fs ms)
    93       then have is_some: "class G C = Some (D, fs, ms)" using Some by simp
    93       then have is_some: "class G C = Some (D, fs, ms)" using Some by simp
   140 apply simp
   140 apply simp
   141 done
   141 done
   142 
   142 
   143 
   143 
   144 -- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
   144 -- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
   145 inductive2
   145 inductive
   146   widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
   146   widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
   147   for G :: "'c prog"
   147   for G :: "'c prog"
   148 where
   148 where
   149   refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
   149   refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
   150 | subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
   150 | subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
   152 
   152 
   153 lemmas refl = HOL.refl
   153 lemmas refl = HOL.refl
   154 
   154 
   155 -- "casting conversion, cf. 5.5 / 5.1.5"
   155 -- "casting conversion, cf. 5.5 / 5.1.5"
   156 -- "left out casts on primitve types"
   156 -- "left out casts on primitve types"
   157 inductive2
   157 inductive
   158   cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
   158   cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
   159   for G :: "'c prog"
   159   for G :: "'c prog"
   160 where
   160 where
   161   widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
   161   widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
   162 | subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
   162 | subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
   166 apply (erule widen.cases)
   166 apply (erule widen.cases)
   167 apply auto
   167 apply auto
   168 done
   168 done
   169 
   169 
   170 lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
   170 lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
   171 apply (ind_cases2 "G\<turnstile>RefT R\<preceq>T")
   171 apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
   172 apply auto
   172 apply auto
   173 done
   173 done
   174 
   174 
   175 lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
   175 lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
   176 apply (ind_cases2 "G\<turnstile>S\<preceq>RefT R")
   176 apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
   177 apply auto
   177 apply auto
   178 done
   178 done
   179 
   179 
   180 lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
   180 lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
   181 apply (ind_cases2 "G\<turnstile>Class C\<preceq>T")
   181 apply (ind_cases "G\<turnstile>Class C\<preceq>T")
   182 apply auto
   182 apply auto
   183 done
   183 done
   184 
   184 
   185 lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
   185 lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
   186 apply (rule iffI)
   186 apply (rule iffI)
   187 apply (ind_cases2 "G\<turnstile>Class C\<preceq>NT")
   187 apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
   188 apply auto
   188 apply auto
   189 done
   189 done
   190 
   190 
   191 lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
   191 lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
   192 apply (rule iffI)
   192 apply (rule iffI)
   193 apply (ind_cases2 "G\<turnstile>Class C \<preceq> Class D")
   193 apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
   194 apply (auto elim: widen.subcls)
   194 apply (auto elim: widen.subcls)
   195 done
   195 done
   196 
   196 
   197 lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
   197 lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
   198 by (ind_cases2 "G \<turnstile> T \<preceq> NT",  auto)
   198 by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
   199 
   199 
   200 lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
   200 lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
   201 apply (rule iffI)
   201 apply (rule iffI)
   202 apply (erule cast.cases)
   202 apply (erule cast.cases)
   203 apply auto
   203 apply auto