src/HOL/MicroJava/J/TypeRel.thy
author haftmann
Tue Nov 24 14:37:23 2009 +0100 (2009-11-24)
changeset 33954 1bc3b688548c
parent 32461 eee4fa79398f
child 35416 d8d7d1b785af
permissions -rwxr-xr-x
backported parts of abstract byte code verifier from AFP/Jinja
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
berghofe@13090
    57
constdefs
berghofe@13090
    58
  class_rec :: "'c prog \<Rightarrow> cname \<Rightarrow> 'a \<Rightarrow>
berghofe@13090
    59
    (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
haftmann@33954
    60
  "class_rec G == wfrec ((subcls1 G)^-1)
berghofe@13090
    61
    (\<lambda>r C t f. case class G C of
haftmann@28524
    62
         None \<Rightarrow> undefined
berghofe@13090
    63
       | Some (D,fs,ms) \<Rightarrow> 
berghofe@13090
    64
           f C fs ms (if C = Object then t else r D t f))"
nipkow@11284
    65
haftmann@33954
    66
lemma class_rec_lemma:
haftmann@33954
    67
  assumes wf: "wf ((subcls1 G)^-1)"
haftmann@33954
    68
    and cls: "class G C = Some (D, fs, ms)"
haftmann@33954
    69
  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
    70
proof -
haftmann@33954
    71
  from wf have step: "\<And>H a. wfrec ((subcls1 G)\<inverse>) H a =
haftmann@33954
    72
    H (cut (wfrec ((subcls1 G)\<inverse>) H) ((subcls1 G)\<inverse>) a) a"
haftmann@33954
    73
    by (rule wfrec)
haftmann@33954
    74
  have cut: "\<And>f. C \<noteq> Object \<Longrightarrow> cut f ((subcls1 G)\<inverse>) C D = f D"
haftmann@33954
    75
    by (rule cut_apply [where r="(subcls1 G)^-1", simplified, OF subcls1I, OF cls])
haftmann@33954
    76
  from cls show ?thesis by (simp add: step cut class_rec_def)
haftmann@33954
    77
qed
oheimb@11026
    78
haftmann@20970
    79
definition
haftmann@33954
    80
  "wf_class G = wf ((subcls1 G)^-1)"
haftmann@20970
    81
haftmann@20970
    82
krauss@32461
    83
text {* Code generator setup (FIXME!) *}
krauss@32461
    84
krauss@32461
    85
consts_code
krauss@32461
    86
  "wfrec"   ("\<module>wfrec?")
krauss@32461
    87
attach {*
krauss@32461
    88
fun wfrec f x = f (wfrec f) x;
krauss@32461
    89
*}
krauss@32461
    90
nipkow@8011
    91
consts
nipkow@8011
    92
nipkow@14134
    93
  method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
nipkow@14134
    94
  field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
oheimb@11026
    95
  fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
nipkow@8011
    96
kleing@12517
    97
-- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
berghofe@13090
    98
defs method_def: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
oheimb@11026
    99
                           ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
oheimb@11026
   100
haftmann@33954
   101
lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   102
  method (G,C) = (if C = Object then empty else method (G,D)) ++  
oheimb@11026
   103
  map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
oheimb@11026
   104
apply (unfold method_def)
oheimb@11026
   105
apply (simp split del: split_if)
oheimb@11026
   106
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   107
apply auto
oheimb@11026
   108
done
oheimb@11026
   109
nipkow@8011
   110
kleing@12517
   111
-- "list of fields of a class, including inherited and hidden ones"
berghofe@13090
   112
defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
oheimb@11026
   113
                           map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
oheimb@11026
   114
haftmann@33954
   115
lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   116
 fields (G,C) = 
oheimb@11026
   117
  map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
oheimb@11026
   118
apply (unfold fields_def)
oheimb@11026
   119
apply (simp split del: split_if)
oheimb@11026
   120
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   121
apply auto
oheimb@11026
   122
done
oheimb@11026
   123
oheimb@11026
   124
oheimb@11026
   125
defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
oheimb@11026
   126
oheimb@11026
   127
lemma field_fields: 
oheimb@11026
   128
"field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
oheimb@11026
   129
apply (unfold field_def)
oheimb@11026
   130
apply (rule table_of_remap_SomeD)
oheimb@11026
   131
apply simp
oheimb@11026
   132
done
oheimb@11026
   133
oheimb@11026
   134
kleing@12517
   135
-- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
berghofe@23757
   136
inductive
berghofe@22271
   137
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
berghofe@22271
   138
  for G :: "'c prog"
berghofe@22271
   139
where
kleing@12517
   140
  refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
berghofe@22271
   141
| subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
berghofe@22271
   142
| null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
nipkow@8011
   143
wenzelm@22597
   144
lemmas refl = HOL.refl
wenzelm@22597
   145
kleing@12517
   146
-- "casting conversion, cf. 5.5 / 5.1.5"
kleing@12517
   147
-- "left out casts on primitve types"
berghofe@23757
   148
inductive
berghofe@22271
   149
  cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
berghofe@22271
   150
  for G :: "'c prog"
berghofe@22271
   151
where
streckem@14045
   152
  widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
berghofe@22271
   153
| subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
oheimb@11026
   154
oheimb@11026
   155
lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
oheimb@11026
   156
apply (rule iffI)
berghofe@22271
   157
apply (erule widen.cases)
oheimb@11026
   158
apply auto
oheimb@11026
   159
done
oheimb@11026
   160
oheimb@11026
   161
lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
berghofe@23757
   162
apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
oheimb@11026
   163
apply auto
oheimb@11026
   164
done
oheimb@11026
   165
oheimb@11026
   166
lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
berghofe@23757
   167
apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
oheimb@11026
   168
apply auto
oheimb@11026
   169
done
oheimb@11026
   170
oheimb@11026
   171
lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
berghofe@23757
   172
apply (ind_cases "G\<turnstile>Class C\<preceq>T")
oheimb@11026
   173
apply auto
oheimb@11026
   174
done
oheimb@11026
   175
oheimb@11026
   176
lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
oheimb@11026
   177
apply (rule iffI)
berghofe@23757
   178
apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
oheimb@11026
   179
apply auto
oheimb@11026
   180
done
nipkow@8011
   181
oheimb@11026
   182
lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
oheimb@11026
   183
apply (rule iffI)
berghofe@23757
   184
apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
oheimb@11026
   185
apply (auto elim: widen.subcls)
oheimb@11026
   186
done
oheimb@11026
   187
streckem@14045
   188
lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
berghofe@23757
   189
by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
streckem@14045
   190
streckem@14045
   191
lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
streckem@14045
   192
apply (rule iffI)
berghofe@22271
   193
apply (erule cast.cases)
streckem@14045
   194
apply auto
streckem@14045
   195
done
streckem@14045
   196
streckem@14045
   197
lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
streckem@14045
   198
apply (erule cast.cases)
streckem@14045
   199
apply simp apply (erule widen.cases) 
streckem@14045
   200
apply auto
streckem@14045
   201
done
streckem@14045
   202
kleing@12517
   203
theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
oheimb@11026
   204
proof -
kleing@12517
   205
  assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
wenzelm@11987
   206
  proof induct
kleing@12517
   207
    case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
oheimb@11026
   208
  next
wenzelm@11987
   209
    case (subcls C D T)
oheimb@11026
   210
    then obtain E where "T = Class E" by (blast dest: widen_Class)
berghofe@22271
   211
    with subcls show "G\<turnstile>Class C\<preceq>T" by auto
oheimb@11026
   212
  next
wenzelm@11987
   213
    case (null R RT)
oheimb@11026
   214
    then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
oheimb@11026
   215
    thus "G\<turnstile>NT\<preceq>RT" by auto
oheimb@11026
   216
  qed
oheimb@11026
   217
qed
oheimb@11026
   218
nipkow@8011
   219
end