src/HOL/MicroJava/J/TypeRel.thy
author oheimb
Tue Jun 12 14:11:00 2001 +0200 (2001-06-12)
changeset 11372 648795477bb5
parent 11284 981ea92a86dd
child 11987 bf31b35949ce
permissions -rw-r--r--
corrected xsymbol/HTML syntax
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
oheimb@11070
     7
header "Relations between Java Types"
nipkow@8011
     8
oheimb@11026
     9
theory TypeRel = Decl:
nipkow@8011
    10
nipkow@8011
    11
consts
oheimb@11026
    12
  subcls1 :: "'c prog => (cname \<times> cname) set"  (* subclass *)
oheimb@11026
    13
  widen   :: "'c prog => (ty    \<times> ty   ) set"  (* widening *)
oheimb@11026
    14
  cast    :: "'c prog => (cname \<times> cname) set"  (* casting *)
nipkow@8011
    15
oheimb@11372
    16
syntax (xsymbols)
oheimb@11026
    17
  subcls1 :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
oheimb@11372
    18
  subcls  :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>C _"  [71,71,71] 70)
oheimb@11372
    19
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
oheimb@11372
    20
  cast    :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
kleing@10061
    21
oheimb@11372
    22
syntax
kleing@10061
    23
  subcls1 :: "'c prog => [cname, cname] => bool" ("_ |- _ <=C1 _" [71,71,71] 70)
oheimb@11372
    24
  subcls  :: "'c prog => [cname, cname] => bool" ("_ |- _ <=C _"  [71,71,71] 70)
oheimb@11372
    25
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ |- _ <= _"   [71,71,71] 70)
oheimb@11372
    26
  cast    :: "'c prog => [cname, cname] => bool" ("_ |- _ <=? _"  [71,71,71] 70)
nipkow@8011
    27
nipkow@8011
    28
translations
oheimb@11026
    29
  "G\<turnstile>C \<prec>C1 D" == "(C,D) \<in> subcls1 G"
oheimb@11026
    30
  "G\<turnstile>C \<preceq>C  D" == "(C,D) \<in> (subcls1 G)^*"
oheimb@11026
    31
  "G\<turnstile>S \<preceq>   T" == "(S,T) \<in> widen   G"
oheimb@11026
    32
  "G\<turnstile>C \<preceq>?  D" == "(C,D) \<in> cast    G"
nipkow@8011
    33
nipkow@8011
    34
defs
nipkow@8011
    35
nipkow@8011
    36
  (* direct subclass, cf. 8.1.3 *)
oheimb@11026
    37
 subcls1_def: "subcls1 G \<equiv> {(C,D). C\<noteq>Object \<and> (\<exists>c. class G C=Some c \<and> fst c=D)}"
nipkow@8011
    38
  
oheimb@11026
    39
lemma subcls1D: 
oheimb@11026
    40
  "G\<turnstile>C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>fs ms. class G C = Some (D,fs,ms))"
oheimb@11026
    41
apply (unfold subcls1_def)
oheimb@11026
    42
apply auto
oheimb@11026
    43
done
oheimb@11026
    44
oheimb@11026
    45
lemma subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G\<turnstile>C\<prec>C1D"
oheimb@11026
    46
apply (unfold subcls1_def)
oheimb@11026
    47
apply auto
oheimb@11026
    48
done
oheimb@11026
    49
oheimb@11026
    50
lemma subcls1_def2: 
oheimb@11026
    51
"subcls1 G = (\<Sigma>C\<in>{C. is_class G C} . {D. C\<noteq>Object \<and> fst (the (class G C))=D})"
oheimb@11026
    52
apply (unfold subcls1_def is_class_def)
oheimb@11026
    53
apply auto
oheimb@11026
    54
done
oheimb@11026
    55
oheimb@11026
    56
lemma finite_subcls1: "finite (subcls1 G)"
oheimb@11026
    57
apply(subst subcls1_def2)
oheimb@11026
    58
apply(rule finite_SigmaI [OF finite_is_class])
oheimb@11026
    59
apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
oheimb@11026
    60
apply  auto
oheimb@11026
    61
done
oheimb@11026
    62
oheimb@11026
    63
lemma subcls_is_class: "(C,D) \<in> (subcls1 G)^+ ==> is_class G C"
oheimb@11026
    64
apply (unfold is_class_def)
oheimb@11026
    65
apply(erule trancl_trans_induct)
oheimb@11026
    66
apply (auto dest!: subcls1D)
oheimb@11026
    67
done
oheimb@11026
    68
oheimb@11266
    69
lemma subcls_is_class2 [rule_format (no_asm)]: 
oheimb@11266
    70
  "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
oheimb@11026
    71
apply (unfold is_class_def)
oheimb@11026
    72
apply (erule rtrancl_induct)
oheimb@11026
    73
apply  (drule_tac [2] subcls1D)
oheimb@11026
    74
apply  auto
oheimb@11026
    75
done
oheimb@11026
    76
oheimb@11026
    77
consts class_rec ::"'c prog \<times> cname \<Rightarrow> 
oheimb@11026
    78
        'a \<Rightarrow> (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
oheimb@11266
    79
oheimb@11026
    80
recdef class_rec "same_fst (\<lambda>G. wf ((subcls1 G)^-1)) (\<lambda>G. (subcls1 G)^-1)"
oheimb@11026
    81
      "class_rec (G,C) = (\<lambda>t f. case class G C of None \<Rightarrow> arbitrary 
oheimb@11026
    82
                         | Some (D,fs,ms) \<Rightarrow> if wf ((subcls1 G)^-1) then 
oheimb@11026
    83
      f C fs ms (if C = Object then t else class_rec (G,D) t f) else arbitrary)"
oheimb@11266
    84
(hints intro: subcls1I)
nipkow@11284
    85
oheimb@11266
    86
declare class_rec.simps [simp del]
oheimb@11026
    87
nipkow@11284
    88
oheimb@11026
    89
lemma class_rec_lemma: "\<lbrakk> wf ((subcls1 G)^-1); class G C = Some (D,fs,ms)\<rbrakk> \<Longrightarrow>
oheimb@11026
    90
 class_rec (G,C) t f = f C fs ms (if C=Object then t else class_rec (G,D) t f)";
oheimb@11266
    91
  apply (rule class_rec.simps [THEN trans [THEN fun_cong [THEN fun_cong]]])
oheimb@11266
    92
  apply simp
oheimb@11026
    93
  done
oheimb@11026
    94
nipkow@8011
    95
consts
nipkow@8011
    96
oheimb@11026
    97
  method :: "'c prog \<times> cname => ( sig   \<leadsto> cname \<times> ty \<times> 'c)" (* ###curry *)
oheimb@11026
    98
  field  :: "'c prog \<times> cname => ( vname \<leadsto> cname \<times> ty     )" (* ###curry *)
oheimb@11026
    99
  fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
nipkow@8011
   100
nipkow@8011
   101
(* methods of a class, with inheritance, overriding and hiding, cf. 8.4.6 *)
oheimb@11026
   102
defs method_def: "method \<equiv> \<lambda>(G,C). class_rec (G,C) empty (\<lambda>C fs ms ts.
oheimb@11026
   103
                           ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
oheimb@11026
   104
oheimb@11026
   105
lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   106
  method (G,C) = (if C = Object then empty else method (G,D)) ++  
oheimb@11026
   107
  map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
oheimb@11026
   108
apply (unfold method_def)
oheimb@11026
   109
apply (simp split del: split_if)
oheimb@11026
   110
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   111
apply auto
oheimb@11026
   112
done
oheimb@11026
   113
nipkow@8011
   114
nipkow@8011
   115
(* list of fields of a class, including inherited and hidden ones *)
oheimb@11026
   116
defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec (G,C) []    (\<lambda>C fs ms ts.
oheimb@11026
   117
                           map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
oheimb@11026
   118
oheimb@11026
   119
lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   120
 fields (G,C) = 
oheimb@11026
   121
  map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
oheimb@11026
   122
apply (unfold fields_def)
oheimb@11026
   123
apply (simp split del: split_if)
oheimb@11026
   124
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   125
apply auto
oheimb@11026
   126
done
oheimb@11026
   127
oheimb@11026
   128
oheimb@11026
   129
defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
oheimb@11026
   130
oheimb@11026
   131
lemma field_fields: 
oheimb@11026
   132
"field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
oheimb@11026
   133
apply (unfold field_def)
oheimb@11026
   134
apply (rule table_of_remap_SomeD)
oheimb@11026
   135
apply simp
oheimb@11026
   136
done
oheimb@11026
   137
oheimb@11026
   138
oheimb@11026
   139
inductive "widen G" intros (*widening, viz. method invocation conversion,cf. 5.3
oheimb@11026
   140
			     i.e. sort of syntactic subtyping *)
oheimb@11026
   141
  refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T" 	 (* identity conv., cf. 5.1.1 *)
oheimb@11026
   142
  subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
oheimb@11026
   143
  null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
nipkow@8011
   144
oheimb@11026
   145
inductive "cast G" intros (* casting conversion, cf. 5.5 / 5.1.5 *)
oheimb@11026
   146
                          (* left out casts on primitve types    *)
oheimb@11026
   147
  widen:  "G\<turnstile>C\<preceq>C D ==> G\<turnstile>C \<preceq>? D"
oheimb@11026
   148
  subcls: "G\<turnstile>D\<preceq>C C ==> G\<turnstile>C \<preceq>? D"
oheimb@11026
   149
oheimb@11026
   150
lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
oheimb@11026
   151
apply (rule iffI)
oheimb@11026
   152
apply (erule widen.elims)
oheimb@11026
   153
apply auto
oheimb@11026
   154
done
oheimb@11026
   155
oheimb@11026
   156
lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
oheimb@11026
   157
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   158
apply auto
oheimb@11026
   159
done
oheimb@11026
   160
oheimb@11026
   161
lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
oheimb@11026
   162
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   163
apply auto
oheimb@11026
   164
done
oheimb@11026
   165
oheimb@11026
   166
lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
oheimb@11026
   167
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   168
apply auto
oheimb@11026
   169
done
oheimb@11026
   170
oheimb@11026
   171
lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
oheimb@11026
   172
apply (rule iffI)
oheimb@11026
   173
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   174
apply auto
oheimb@11026
   175
done
nipkow@8011
   176
oheimb@11026
   177
lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
oheimb@11026
   178
apply (rule iffI)
oheimb@11026
   179
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   180
apply (auto elim: widen.subcls)
oheimb@11026
   181
done
oheimb@11026
   182
oheimb@11026
   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"
oheimb@11026
   184
apply (erule widen.induct)
oheimb@11026
   185
apply   safe
oheimb@11026
   186
apply  (frule widen_Class)
oheimb@11026
   187
apply  (frule_tac [2] widen_RefT)
oheimb@11026
   188
apply  safe
oheimb@11026
   189
apply(erule (1) rtrancl_trans)
oheimb@11026
   190
done
oheimb@11026
   191
nipkow@8011
   192
oheimb@11026
   193
(*####theorem widen_trans: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
oheimb@11026
   194
proof -
oheimb@11026
   195
  assume "G\<turnstile>S\<preceq>U"
oheimb@11026
   196
  thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T" (*(is "PROP ?P S U")*)
oheimb@11026
   197
  proof (induct (*cases*) (open) (*?P S U*) rule: widen.induct [consumes 1])
oheimb@11026
   198
    case refl
oheimb@11026
   199
    fix T' assume "G\<turnstile>T\<preceq>T'" thus "G\<turnstile>T\<preceq>T'".
oheimb@11026
   200
      (* fix T' show "PROP ?P T T".*)
oheimb@11026
   201
  next
oheimb@11026
   202
    case subcls
oheimb@11026
   203
    fix T assume "G\<turnstile>Class D\<preceq>T"
oheimb@11026
   204
    then obtain E where "T = Class E" by (blast dest: widen_Class)
oheimb@11026
   205
    from prems show "G\<turnstile>Class C\<preceq>T" proof (auto elim: rtrancl_trans) qed
oheimb@11026
   206
  next
oheimb@11026
   207
    case null
oheimb@11026
   208
    fix RT assume "G\<turnstile>RefT R\<preceq>RT"
oheimb@11026
   209
    then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
oheimb@11026
   210
    thus "G\<turnstile>NT\<preceq>RT" by auto
oheimb@11026
   211
  qed
oheimb@11026
   212
qed
oheimb@11026
   213
*)
oheimb@11026
   214
oheimb@11026
   215
theorem widen_trans: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
oheimb@11026
   216
proof -
oheimb@11026
   217
  assume "G\<turnstile>S\<preceq>U"
oheimb@11026
   218
  thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T" (*(is "PROP ?P S U")*)
oheimb@11026
   219
  proof (induct (*cases*) (open) (*?P S U*)) (* rule: widen.induct *)
oheimb@11026
   220
    case refl
oheimb@11026
   221
    fix T' assume "G\<turnstile>T\<preceq>T'" thus "G\<turnstile>T\<preceq>T'".
oheimb@11026
   222
      (* fix T' show "PROP ?P T T".*)
oheimb@11026
   223
  next
oheimb@11026
   224
    case subcls
oheimb@11026
   225
    fix T assume "G\<turnstile>Class D\<preceq>T"
oheimb@11026
   226
    then obtain E where "T = Class E" by (blast dest: widen_Class)
oheimb@11026
   227
    from prems show "G\<turnstile>Class C\<preceq>T" proof (auto elim: rtrancl_trans) qed
oheimb@11026
   228
  next
oheimb@11026
   229
    case null
oheimb@11026
   230
    fix RT assume "G\<turnstile>RefT R\<preceq>RT"
oheimb@11026
   231
    then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
oheimb@11026
   232
    thus "G\<turnstile>NT\<preceq>RT" by auto
oheimb@11026
   233
  qed
oheimb@11026
   234
qed
oheimb@11026
   235
oheimb@11026
   236
nipkow@8011
   237
nipkow@8011
   238
end