src/HOL/MicroJava/J/TypeRel.thy
author haftmann
Mon Mar 01 13:40:23 2010 +0100 (2010-03-01)
changeset 35416 d8d7d1b785af
parent 33954 1bc3b688548c
child 41589 bbd861837ebc
permissions -rw-r--r--
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
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"
haftmann@33954
    12
haftmann@33954
    13
inductive_set
haftmann@33954
    14
  subcls1 :: "'c prog => (cname \<times> cname) set"
haftmann@33954
    15
  and subcls1' :: "'c prog => cname \<Rightarrow> cname => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
berghofe@22271
    16
  for G :: "'c prog"
berghofe@22271
    17
where
haftmann@33954
    18
  "G \<turnstile> C \<prec>C1 D \<equiv> (C, D) \<in> subcls1 G"
haftmann@33954
    19
  | subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G \<turnstile> C \<prec>C1 D"
kleing@10061
    20
berghofe@22271
    21
abbreviation
haftmann@33954
    22
  subcls  :: "'c prog => cname \<Rightarrow> cname => bool" ("_ \<turnstile> _ \<preceq>C _"  [71,71,71] 70)
haftmann@33954
    23
  where "G \<turnstile> C \<preceq>C D \<equiv> (C, D) \<in> (subcls1 G)^*"
haftmann@33954
    24
oheimb@11026
    25
lemma subcls1D: 
oheimb@11026
    26
  "G\<turnstile>C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>fs ms. class G C = Some (D,fs,ms))"
berghofe@22271
    27
apply (erule subcls1.cases)
oheimb@11026
    28
apply auto
oheimb@11026
    29
done
oheimb@11026
    30
haftmann@33954
    31
lemma subcls1_def2:
haftmann@33954
    32
  "subcls1 P =
haftmann@33954
    33
     (SIGMA C:{C. is_class P C}. {D. C\<noteq>Object \<and> fst (the (class P C))=D})"
haftmann@33954
    34
  by (auto simp add: is_class_def dest: subcls1D intro: subcls1I)
oheimb@11026
    35
haftmann@33954
    36
lemma finite_subcls1: "finite (subcls1 G)"
berghofe@23757
    37
apply(simp add: subcls1_def2 del: mem_Sigma_iff)
oheimb@11026
    38
apply(rule finite_SigmaI [OF finite_is_class])
oheimb@11026
    39
apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
oheimb@11026
    40
apply  auto
oheimb@11026
    41
done
oheimb@11026
    42
haftmann@33954
    43
lemma subcls_is_class: "(C, D) \<in> (subcls1 G)^+  ==> is_class G C"
oheimb@11026
    44
apply (unfold is_class_def)
haftmann@33954
    45
apply(erule trancl_trans_induct)
oheimb@11026
    46
apply (auto dest!: subcls1D)
oheimb@11026
    47
done
oheimb@11026
    48
oheimb@11266
    49
lemma subcls_is_class2 [rule_format (no_asm)]: 
oheimb@11266
    50
  "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
oheimb@11026
    51
apply (unfold is_class_def)
haftmann@33954
    52
apply (erule rtrancl_induct)
oheimb@11026
    53
apply  (drule_tac [2] subcls1D)
oheimb@11026
    54
apply  auto
oheimb@11026
    55
done
oheimb@11026
    56
haftmann@35416
    57
definition class_rec :: "'c prog \<Rightarrow> cname \<Rightarrow> 'a \<Rightarrow>
haftmann@35416
    58
    (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a" where
haftmann@33954
    59
  "class_rec G == wfrec ((subcls1 G)^-1)
berghofe@13090
    60
    (\<lambda>r C t f. case class G C of
haftmann@28524
    61
         None \<Rightarrow> undefined
berghofe@13090
    62
       | Some (D,fs,ms) \<Rightarrow> 
berghofe@13090
    63
           f C fs ms (if C = Object then t else r D t f))"
nipkow@11284
    64
haftmann@33954
    65
lemma class_rec_lemma:
haftmann@33954
    66
  assumes wf: "wf ((subcls1 G)^-1)"
haftmann@33954
    67
    and cls: "class G C = Some (D, fs, ms)"
haftmann@33954
    68
  shows "class_rec G C t f = f C fs ms (if C=Object then t else class_rec G D t f)"
haftmann@33954
    69
proof -
haftmann@33954
    70
  from wf have step: "\<And>H a. wfrec ((subcls1 G)\<inverse>) H a =
haftmann@33954
    71
    H (cut (wfrec ((subcls1 G)\<inverse>) H) ((subcls1 G)\<inverse>) a) a"
haftmann@33954
    72
    by (rule wfrec)
haftmann@33954
    73
  have cut: "\<And>f. C \<noteq> Object \<Longrightarrow> cut f ((subcls1 G)\<inverse>) C D = f D"
haftmann@33954
    74
    by (rule cut_apply [where r="(subcls1 G)^-1", simplified, OF subcls1I, OF cls])
haftmann@33954
    75
  from cls show ?thesis by (simp add: step cut class_rec_def)
haftmann@33954
    76
qed
oheimb@11026
    77
haftmann@20970
    78
definition
haftmann@33954
    79
  "wf_class G = wf ((subcls1 G)^-1)"
haftmann@20970
    80
haftmann@20970
    81
krauss@32461
    82
text {* Code generator setup (FIXME!) *}
krauss@32461
    83
krauss@32461
    84
consts_code
krauss@32461
    85
  "wfrec"   ("\<module>wfrec?")
krauss@32461
    86
attach {*
krauss@32461
    87
fun wfrec f x = f (wfrec f) x;
krauss@32461
    88
*}
krauss@32461
    89
nipkow@8011
    90
consts
nipkow@8011
    91
nipkow@14134
    92
  method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
nipkow@14134
    93
  field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
oheimb@11026
    94
  fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
nipkow@8011
    95
kleing@12517
    96
-- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
berghofe@13090
    97
defs method_def: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
oheimb@11026
    98
                           ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
oheimb@11026
    99
haftmann@33954
   100
lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   101
  method (G,C) = (if C = Object then empty else method (G,D)) ++  
oheimb@11026
   102
  map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
oheimb@11026
   103
apply (unfold method_def)
oheimb@11026
   104
apply (simp split del: split_if)
oheimb@11026
   105
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   106
apply auto
oheimb@11026
   107
done
oheimb@11026
   108
nipkow@8011
   109
kleing@12517
   110
-- "list of fields of a class, including inherited and hidden ones"
berghofe@13090
   111
defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
oheimb@11026
   112
                           map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
oheimb@11026
   113
haftmann@33954
   114
lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   115
 fields (G,C) = 
oheimb@11026
   116
  map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
oheimb@11026
   117
apply (unfold fields_def)
oheimb@11026
   118
apply (simp split del: split_if)
oheimb@11026
   119
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   120
apply auto
oheimb@11026
   121
done
oheimb@11026
   122
oheimb@11026
   123
oheimb@11026
   124
defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
oheimb@11026
   125
oheimb@11026
   126
lemma field_fields: 
oheimb@11026
   127
"field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
oheimb@11026
   128
apply (unfold field_def)
oheimb@11026
   129
apply (rule table_of_remap_SomeD)
oheimb@11026
   130
apply simp
oheimb@11026
   131
done
oheimb@11026
   132
oheimb@11026
   133
kleing@12517
   134
-- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
berghofe@23757
   135
inductive
berghofe@22271
   136
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
berghofe@22271
   137
  for G :: "'c prog"
berghofe@22271
   138
where
kleing@12517
   139
  refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
berghofe@22271
   140
| subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
berghofe@22271
   141
| null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
nipkow@8011
   142
wenzelm@22597
   143
lemmas refl = HOL.refl
wenzelm@22597
   144
kleing@12517
   145
-- "casting conversion, cf. 5.5 / 5.1.5"
kleing@12517
   146
-- "left out casts on primitve types"
berghofe@23757
   147
inductive
berghofe@22271
   148
  cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
berghofe@22271
   149
  for G :: "'c prog"
berghofe@22271
   150
where
streckem@14045
   151
  widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
berghofe@22271
   152
| subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
oheimb@11026
   153
oheimb@11026
   154
lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
oheimb@11026
   155
apply (rule iffI)
berghofe@22271
   156
apply (erule widen.cases)
oheimb@11026
   157
apply auto
oheimb@11026
   158
done
oheimb@11026
   159
oheimb@11026
   160
lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
berghofe@23757
   161
apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
oheimb@11026
   162
apply auto
oheimb@11026
   163
done
oheimb@11026
   164
oheimb@11026
   165
lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
berghofe@23757
   166
apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
oheimb@11026
   167
apply auto
oheimb@11026
   168
done
oheimb@11026
   169
oheimb@11026
   170
lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
berghofe@23757
   171
apply (ind_cases "G\<turnstile>Class C\<preceq>T")
oheimb@11026
   172
apply auto
oheimb@11026
   173
done
oheimb@11026
   174
oheimb@11026
   175
lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
oheimb@11026
   176
apply (rule iffI)
berghofe@23757
   177
apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
oheimb@11026
   178
apply auto
oheimb@11026
   179
done
nipkow@8011
   180
oheimb@11026
   181
lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
oheimb@11026
   182
apply (rule iffI)
berghofe@23757
   183
apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
oheimb@11026
   184
apply (auto elim: widen.subcls)
oheimb@11026
   185
done
oheimb@11026
   186
streckem@14045
   187
lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
berghofe@23757
   188
by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
streckem@14045
   189
streckem@14045
   190
lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
streckem@14045
   191
apply (rule iffI)
berghofe@22271
   192
apply (erule cast.cases)
streckem@14045
   193
apply auto
streckem@14045
   194
done
streckem@14045
   195
streckem@14045
   196
lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
streckem@14045
   197
apply (erule cast.cases)
streckem@14045
   198
apply simp apply (erule widen.cases) 
streckem@14045
   199
apply auto
streckem@14045
   200
done
streckem@14045
   201
kleing@12517
   202
theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
oheimb@11026
   203
proof -
kleing@12517
   204
  assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
wenzelm@11987
   205
  proof induct
kleing@12517
   206
    case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
oheimb@11026
   207
  next
wenzelm@11987
   208
    case (subcls C D T)
oheimb@11026
   209
    then obtain E where "T = Class E" by (blast dest: widen_Class)
berghofe@22271
   210
    with subcls show "G\<turnstile>Class C\<preceq>T" by auto
oheimb@11026
   211
  next
wenzelm@11987
   212
    case (null R RT)
oheimb@11026
   213
    then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
oheimb@11026
   214
    thus "G\<turnstile>NT\<preceq>RT" by auto
oheimb@11026
   215
  qed
oheimb@11026
   216
qed
oheimb@11026
   217
nipkow@8011
   218
end