src/HOL/MicroJava/J/TypeRel.thy
author berghofe
Wed Jul 11 11:32:02 2007 +0200 (2007-07-11)
changeset 23757 087b0a241557
parent 22597 284b2183d070
child 28524 644b62cf678f
permissions -rw-r--r--
- Renamed inductive2 to inductive
- Renamed some theorems about transitive closure for predicates
nipkow@8011
     1
(*  Title:      HOL/MicroJava/J/TypeRel.thy
nipkow@8011
     2
    ID:         $Id$
nipkow@8011
     3
    Author:     David von Oheimb
nipkow@8011
     4
    Copyright   1999 Technische Universitaet Muenchen
oheimb@11070
     5
*)
nipkow@8011
     6
kleing@12911
     7
header {* \isaheader{Relations between Java Types} *}
nipkow@8011
     8
haftmann@16417
     9
theory TypeRel imports Decl begin
nipkow@8011
    10
berghofe@22271
    11
-- "direct subclass, cf. 8.1.3"
berghofe@23757
    12
inductive
oheimb@11026
    13
  subcls1 :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
berghofe@22271
    14
  for G :: "'c prog"
berghofe@22271
    15
where
berghofe@22271
    16
  subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G\<turnstile>C\<prec>C1D"
kleing@10061
    17
berghofe@22271
    18
abbreviation
berghofe@22271
    19
  subcls  :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>C _"  [71,71,71] 70)
berghofe@22271
    20
  where "G\<turnstile>C \<preceq>C  D \<equiv> (subcls1 G)^** C D"
nipkow@8011
    21
  
oheimb@11026
    22
lemma subcls1D: 
oheimb@11026
    23
  "G\<turnstile>C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>fs ms. class G C = Some (D,fs,ms))"
berghofe@22271
    24
apply (erule subcls1.cases)
oheimb@11026
    25
apply auto
oheimb@11026
    26
done
oheimb@11026
    27
oheimb@11026
    28
lemma subcls1_def2: 
berghofe@23757
    29
  "subcls1 G = (\<lambda>C D. (C, D) \<in>
berghofe@23757
    30
     (SIGMA C: {C. is_class G C} . {D. C\<noteq>Object \<and> fst (the (class G C))=D}))"
berghofe@22271
    31
  by (auto simp add: is_class_def expand_fun_eq dest: subcls1D intro: subcls1I)
oheimb@11026
    32
berghofe@23757
    33
lemma finite_subcls1: "finite {(C, D). subcls1 G C D}"
berghofe@23757
    34
apply(simp add: subcls1_def2 del: mem_Sigma_iff)
oheimb@11026
    35
apply(rule finite_SigmaI [OF finite_is_class])
oheimb@11026
    36
apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
oheimb@11026
    37
apply  auto
oheimb@11026
    38
done
oheimb@11026
    39
berghofe@22271
    40
lemma subcls_is_class: "(subcls1 G)^++ C D ==> is_class G C"
oheimb@11026
    41
apply (unfold is_class_def)
berghofe@23757
    42
apply(erule tranclp_trans_induct)
oheimb@11026
    43
apply (auto dest!: subcls1D)
oheimb@11026
    44
done
oheimb@11026
    45
oheimb@11266
    46
lemma subcls_is_class2 [rule_format (no_asm)]: 
oheimb@11266
    47
  "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
oheimb@11026
    48
apply (unfold is_class_def)
berghofe@23757
    49
apply (erule rtranclp_induct)
oheimb@11026
    50
apply  (drule_tac [2] subcls1D)
oheimb@11026
    51
apply  auto
oheimb@11026
    52
done
oheimb@11026
    53
berghofe@13090
    54
constdefs
berghofe@13090
    55
  class_rec :: "'c prog \<Rightarrow> cname \<Rightarrow> 'a \<Rightarrow>
berghofe@13090
    56
    (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
berghofe@23757
    57
  "class_rec G == wfrec {(C, D). (subcls1 G)^--1 C D}
berghofe@13090
    58
    (\<lambda>r C t f. case class G C of
berghofe@13090
    59
         None \<Rightarrow> arbitrary
berghofe@13090
    60
       | Some (D,fs,ms) \<Rightarrow> 
berghofe@13090
    61
           f C fs ms (if C = Object then t else r D t f))"
nipkow@11284
    62
berghofe@22271
    63
lemma class_rec_lemma: "wfP ((subcls1 G)^--1) \<Longrightarrow> class G C = Some (D,fs,ms) \<Longrightarrow>
berghofe@13090
    64
 class_rec G C t f = f C fs ms (if C=Object then t else class_rec G D t f)"
berghofe@23757
    65
  by (simp add: class_rec_def wfrec [to_pred, where r="(subcls1 G)^--1", simplified]
berghofe@23757
    66
    cut_apply [where r="{(C, D). subcls1 G D C}", simplified, OF subcls1I])
oheimb@11026
    67
haftmann@20970
    68
definition
berghofe@22271
    69
  "wf_class G = wfP ((subcls1 G)^--1)"
haftmann@20970
    70
haftmann@20970
    71
lemma class_rec_func [code func]:
haftmann@20970
    72
  "class_rec G C t f = (if wf_class G then
haftmann@20970
    73
    (case class G C
haftmann@20970
    74
      of None \<Rightarrow> arbitrary
haftmann@20970
    75
       | Some (D, fs, ms) \<Rightarrow> f C fs ms (if C = Object then t else class_rec G D t f))
haftmann@20970
    76
    else class_rec G C t f)"
haftmann@20970
    77
proof (cases "wf_class G")
haftmann@20970
    78
  case False then show ?thesis by auto
haftmann@20970
    79
next
haftmann@20970
    80
  case True
berghofe@22271
    81
  from `wf_class G` have wf: "wfP ((subcls1 G)^--1)"
haftmann@20970
    82
    unfolding wf_class_def .
haftmann@20970
    83
  show ?thesis
haftmann@20970
    84
  proof (cases "class G C")
haftmann@20970
    85
    case None
haftmann@20970
    86
    with wf show ?thesis
berghofe@23757
    87
      by (simp add: class_rec_def wfrec [to_pred, where r="(subcls1 G)^--1", simplified]
berghofe@23757
    88
        cut_apply [where r="{(C, D).subcls1 G D C}", simplified, OF subcls1I])
haftmann@20970
    89
  next
haftmann@20970
    90
    case (Some x) show ?thesis
haftmann@20970
    91
    proof (cases x)
haftmann@20970
    92
      case (fields D fs ms)
haftmann@20970
    93
      then have is_some: "class G C = Some (D, fs, ms)" using Some by simp
haftmann@20970
    94
      note class_rec = class_rec_lemma [OF wf is_some]
haftmann@20970
    95
      show ?thesis unfolding class_rec by (simp add: is_some)
haftmann@20970
    96
    qed
haftmann@20970
    97
  qed
haftmann@20970
    98
qed
haftmann@20970
    99
nipkow@8011
   100
consts
nipkow@8011
   101
nipkow@14134
   102
  method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
nipkow@14134
   103
  field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
oheimb@11026
   104
  fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
nipkow@8011
   105
kleing@12517
   106
-- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
berghofe@13090
   107
defs method_def: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
oheimb@11026
   108
                           ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
oheimb@11026
   109
berghofe@22271
   110
lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
oheimb@11026
   111
  method (G,C) = (if C = Object then empty else method (G,D)) ++  
oheimb@11026
   112
  map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
oheimb@11026
   113
apply (unfold method_def)
oheimb@11026
   114
apply (simp split del: split_if)
oheimb@11026
   115
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   116
apply auto
oheimb@11026
   117
done
oheimb@11026
   118
nipkow@8011
   119
kleing@12517
   120
-- "list of fields of a class, including inherited and hidden ones"
berghofe@13090
   121
defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
oheimb@11026
   122
                           map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
oheimb@11026
   123
berghofe@22271
   124
lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
oheimb@11026
   125
 fields (G,C) = 
oheimb@11026
   126
  map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
oheimb@11026
   127
apply (unfold fields_def)
oheimb@11026
   128
apply (simp split del: split_if)
oheimb@11026
   129
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   130
apply auto
oheimb@11026
   131
done
oheimb@11026
   132
oheimb@11026
   133
oheimb@11026
   134
defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
oheimb@11026
   135
oheimb@11026
   136
lemma field_fields: 
oheimb@11026
   137
"field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
oheimb@11026
   138
apply (unfold field_def)
oheimb@11026
   139
apply (rule table_of_remap_SomeD)
oheimb@11026
   140
apply simp
oheimb@11026
   141
done
oheimb@11026
   142
oheimb@11026
   143
kleing@12517
   144
-- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
berghofe@23757
   145
inductive
berghofe@22271
   146
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
berghofe@22271
   147
  for G :: "'c prog"
berghofe@22271
   148
where
kleing@12517
   149
  refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
berghofe@22271
   150
| subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
berghofe@22271
   151
| null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
nipkow@8011
   152
wenzelm@22597
   153
lemmas refl = HOL.refl
wenzelm@22597
   154
kleing@12517
   155
-- "casting conversion, cf. 5.5 / 5.1.5"
kleing@12517
   156
-- "left out casts on primitve types"
berghofe@23757
   157
inductive
berghofe@22271
   158
  cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
berghofe@22271
   159
  for G :: "'c prog"
berghofe@22271
   160
where
streckem@14045
   161
  widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
berghofe@22271
   162
| subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
oheimb@11026
   163
oheimb@11026
   164
lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
oheimb@11026
   165
apply (rule iffI)
berghofe@22271
   166
apply (erule widen.cases)
oheimb@11026
   167
apply auto
oheimb@11026
   168
done
oheimb@11026
   169
oheimb@11026
   170
lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
berghofe@23757
   171
apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
oheimb@11026
   172
apply auto
oheimb@11026
   173
done
oheimb@11026
   174
oheimb@11026
   175
lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
berghofe@23757
   176
apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
oheimb@11026
   177
apply auto
oheimb@11026
   178
done
oheimb@11026
   179
oheimb@11026
   180
lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
berghofe@23757
   181
apply (ind_cases "G\<turnstile>Class C\<preceq>T")
oheimb@11026
   182
apply auto
oheimb@11026
   183
done
oheimb@11026
   184
oheimb@11026
   185
lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
oheimb@11026
   186
apply (rule iffI)
berghofe@23757
   187
apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
oheimb@11026
   188
apply auto
oheimb@11026
   189
done
nipkow@8011
   190
oheimb@11026
   191
lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
oheimb@11026
   192
apply (rule iffI)
berghofe@23757
   193
apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
oheimb@11026
   194
apply (auto elim: widen.subcls)
oheimb@11026
   195
done
oheimb@11026
   196
streckem@14045
   197
lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
berghofe@23757
   198
by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
streckem@14045
   199
streckem@14045
   200
lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
streckem@14045
   201
apply (rule iffI)
berghofe@22271
   202
apply (erule cast.cases)
streckem@14045
   203
apply auto
streckem@14045
   204
done
streckem@14045
   205
streckem@14045
   206
lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
streckem@14045
   207
apply (erule cast.cases)
streckem@14045
   208
apply simp apply (erule widen.cases) 
streckem@14045
   209
apply auto
streckem@14045
   210
done
streckem@14045
   211
kleing@12517
   212
theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
oheimb@11026
   213
proof -
kleing@12517
   214
  assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
wenzelm@11987
   215
  proof induct
kleing@12517
   216
    case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
oheimb@11026
   217
  next
wenzelm@11987
   218
    case (subcls C D T)
oheimb@11026
   219
    then obtain E where "T = Class E" by (blast dest: widen_Class)
berghofe@22271
   220
    with subcls show "G\<turnstile>Class C\<preceq>T" by auto
oheimb@11026
   221
  next
wenzelm@11987
   222
    case (null R RT)
oheimb@11026
   223
    then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
oheimb@11026
   224
    thus "G\<turnstile>NT\<preceq>RT" by auto
oheimb@11026
   225
  qed
oheimb@11026
   226
qed
oheimb@11026
   227
nipkow@8011
   228
end