src/HOL/MicroJava/J/TypeRel.thy
author blanchet
Thu Jan 16 16:20:17 2014 +0100 (2014-01-16)
changeset 55017 2df6ad1dbd66
parent 53374 a14d2a854c02
child 58184 db1381d811ab
permissions -rw-r--r--
adapted to move of Wfrec
nipkow@8011
     1
(*  Title:      HOL/MicroJava/J/TypeRel.thy
wenzelm@41589
     2
    Author:     David von Oheimb, Technische Universitaet Muenchen
oheimb@11070
     3
*)
nipkow@8011
     4
kleing@12911
     5
header {* \isaheader{Relations between Java Types} *}
nipkow@8011
     6
haftmann@45970
     7
theory TypeRel
blanchet@55017
     8
imports Decl
haftmann@45970
     9
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
Andreas@44035
    82
Andreas@44035
    83
text {* Code generator setup *}
Andreas@44035
    84
Andreas@44035
    85
code_pred 
Andreas@44035
    86
  (modes: i \<Rightarrow> i \<Rightarrow> o \<Rightarrow> bool, i \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool)
Andreas@44035
    87
  subcls1p 
Andreas@44035
    88
  .
haftmann@45970
    89
haftmann@45970
    90
declare subcls1_def [code_pred_def]
haftmann@45970
    91
Andreas@44035
    92
code_pred 
Andreas@44035
    93
  (modes: i \<Rightarrow> i \<times> o \<Rightarrow> bool, i \<Rightarrow> i \<times> i \<Rightarrow> bool)
Andreas@44035
    94
  [inductify]
Andreas@44035
    95
  subcls1 
Andreas@44035
    96
  .
Andreas@44035
    97
Andreas@44035
    98
definition subcls' where "subcls' G = (subcls1p G)^**"
haftmann@45970
    99
Andreas@44035
   100
code_pred
Andreas@44035
   101
  (modes: i \<Rightarrow> i \<Rightarrow> i \<Rightarrow> bool, i \<Rightarrow> i \<Rightarrow> o \<Rightarrow> bool)
Andreas@44035
   102
  [inductify]
Andreas@44035
   103
  subcls'
haftmann@45970
   104
  .
haftmann@45970
   105
bulwahn@45231
   106
lemma subcls_conv_subcls' [code_unfold]:
haftmann@45970
   107
  "(subcls1 G)^* = {(C, D). subcls' G C D}"
haftmann@45970
   108
by(simp add: subcls'_def subcls1_def rtrancl_def)
Andreas@44035
   109
Andreas@44035
   110
lemma class_rec_code [code]:
Andreas@44035
   111
  "class_rec G C t f = 
Andreas@44035
   112
  (if wf_class G then 
Andreas@44035
   113
    (case class G C of
Andreas@44035
   114
       None \<Rightarrow> class_rec G C t f
Andreas@44035
   115
     | Some (D, fs, ms) \<Rightarrow> 
Andreas@44035
   116
       if C = Object then f Object fs ms t else f C fs ms (class_rec G D t f))
Andreas@44035
   117
   else class_rec G C t f)"
Andreas@44035
   118
apply(cases "wf_class G")
Andreas@44035
   119
 apply(unfold class_rec_def wf_class_def)
Andreas@44035
   120
 apply(subst wfrec, assumption)
Andreas@44035
   121
 apply(cases "class G C")
Andreas@44035
   122
  apply(simp add: wfrec)
Andreas@44035
   123
 apply clarsimp
Andreas@44035
   124
 apply(rename_tac D fs ms)
Andreas@44035
   125
 apply(rule_tac f="f C fs ms" in arg_cong)
Andreas@44035
   126
 apply(clarsimp simp add: cut_def)
Andreas@44035
   127
 apply(blast intro: subcls1I)
Andreas@44035
   128
apply simp
Andreas@44035
   129
done
krauss@32461
   130
Andreas@44035
   131
lemma wf_class_code [code]:
Andreas@44035
   132
  "wf_class G \<longleftrightarrow> (\<forall>(C, rest) \<in> set G. C \<noteq> Object \<longrightarrow> \<not> G \<turnstile> fst (the (class G C)) \<preceq>C C)"
Andreas@44035
   133
proof
Andreas@44035
   134
  assume "wf_class G"
Andreas@44035
   135
  hence wf: "wf (((subcls1 G)^+)^-1)" unfolding wf_class_def by(rule wf_converse_trancl)
Andreas@44035
   136
  hence acyc: "acyclic ((subcls1 G)^+)" by(auto dest: wf_acyclic)
Andreas@44035
   137
  show "\<forall>(C, rest) \<in> set G. C \<noteq> Object \<longrightarrow> \<not> G \<turnstile> fst (the (class G C)) \<preceq>C C"
Andreas@44035
   138
  proof(safe)
Andreas@44035
   139
    fix C D fs ms
Andreas@44035
   140
    assume "(C, D, fs, ms) \<in> set G"
Andreas@44035
   141
      and "C \<noteq> Object"
Andreas@44035
   142
      and subcls: "G \<turnstile> fst (the (class G C)) \<preceq>C C"
Andreas@44035
   143
    from `(C, D, fs, ms) \<in> set G` obtain D' fs' ms'
Andreas@44035
   144
      where "class": "class G C = Some (D', fs', ms')"
Andreas@44035
   145
      unfolding class_def by(auto dest!: weak_map_of_SomeI)
Andreas@44035
   146
    hence "G \<turnstile> C \<prec>C1 D'" using `C \<noteq> Object` ..
wenzelm@53374
   147
    hence *: "(C, D') \<in> (subcls1 G)^+" ..
wenzelm@53374
   148
    also from * acyc have "C \<noteq> D'" by(auto simp add: acyclic_def)
Andreas@44035
   149
    with subcls "class" have "(D', C) \<in> (subcls1 G)^+" by(auto dest: rtranclD)
Andreas@44035
   150
    finally show False using acyc by(auto simp add: acyclic_def)
Andreas@44035
   151
  qed
Andreas@44035
   152
next
Andreas@44035
   153
  assume rhs[rule_format]: "\<forall>(C, rest) \<in> set G. C \<noteq> Object \<longrightarrow> \<not> G \<turnstile> fst (the (class G C)) \<preceq>C C"
Andreas@44035
   154
  have "acyclic (subcls1 G)"
Andreas@44035
   155
  proof(intro acyclicI strip notI)
Andreas@44035
   156
    fix C
Andreas@44035
   157
    assume "(C, C) \<in> (subcls1 G)\<^sup>+"
Andreas@44035
   158
    thus False
Andreas@44035
   159
    proof(cases)
Andreas@44035
   160
      case base
Andreas@44035
   161
      then obtain rest where "class G C = Some (C, rest)"
Andreas@44035
   162
        and "C \<noteq> Object" by cases
Andreas@44035
   163
      from `class G C = Some (C, rest)` have "(C, C, rest) \<in> set G"
Andreas@44035
   164
        unfolding class_def by(rule map_of_SomeD)
Andreas@44035
   165
      with `C \<noteq> Object` `class G C = Some (C, rest)`
Andreas@44035
   166
      have "\<not> G \<turnstile> C \<preceq>C C" by(auto dest: rhs)
Andreas@44035
   167
      thus False by simp
Andreas@44035
   168
    next
Andreas@44035
   169
      case (step D)
Andreas@44035
   170
      from `G \<turnstile> D \<prec>C1 C` obtain rest where "class G D = Some (C, rest)"
Andreas@44035
   171
        and "D \<noteq> Object" by cases
Andreas@44035
   172
      from `class G D = Some (C, rest)` have "(D, C, rest) \<in> set G"
Andreas@44035
   173
        unfolding class_def by(rule map_of_SomeD)
Andreas@44035
   174
      with `D \<noteq> Object` `class G D = Some (C, rest)`
Andreas@44035
   175
      have "\<not> G \<turnstile> C \<preceq>C D" by(auto dest: rhs)
Andreas@44035
   176
      moreover from `(C, D) \<in> (subcls1 G)\<^sup>+`
Andreas@44035
   177
      have "G \<turnstile> C \<preceq>C D" by(rule trancl_into_rtrancl)
Andreas@44035
   178
      ultimately show False by contradiction
Andreas@44035
   179
    qed
Andreas@44035
   180
  qed
Andreas@44035
   181
  thus "wf_class G" unfolding wf_class_def
Andreas@44035
   182
    by(rule finite_acyclic_wf_converse[OF finite_subcls1])
Andreas@44035
   183
qed
krauss@32461
   184
nipkow@8011
   185
consts
nipkow@14134
   186
  method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
nipkow@14134
   187
  field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
oheimb@11026
   188
  fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
nipkow@8011
   189
kleing@12517
   190
-- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
Andreas@44035
   191
defs method_def [code]: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
oheimb@11026
   192
                           ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
oheimb@11026
   193
haftmann@33954
   194
lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   195
  method (G,C) = (if C = Object then empty else method (G,D)) ++  
oheimb@11026
   196
  map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
oheimb@11026
   197
apply (unfold method_def)
oheimb@11026
   198
apply (simp split del: split_if)
haftmann@45970
   199
apply (erule (1) class_rec_lemma [THEN trans])
oheimb@11026
   200
apply auto
oheimb@11026
   201
done
oheimb@11026
   202
nipkow@8011
   203
kleing@12517
   204
-- "list of fields of a class, including inherited and hidden ones"
Andreas@44035
   205
defs fields_def [code]: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
oheimb@11026
   206
                           map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
oheimb@11026
   207
haftmann@33954
   208
lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   209
 fields (G,C) = 
oheimb@11026
   210
  map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
oheimb@11026
   211
apply (unfold fields_def)
oheimb@11026
   212
apply (simp split del: split_if)
haftmann@45970
   213
apply (erule (1) class_rec_lemma [THEN trans])
oheimb@11026
   214
apply auto
oheimb@11026
   215
done
oheimb@11026
   216
oheimb@11026
   217
Andreas@44035
   218
defs field_def [code]: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
oheimb@11026
   219
oheimb@11026
   220
lemma field_fields: 
oheimb@11026
   221
"field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
oheimb@11026
   222
apply (unfold field_def)
oheimb@11026
   223
apply (rule table_of_remap_SomeD)
oheimb@11026
   224
apply simp
oheimb@11026
   225
done
oheimb@11026
   226
oheimb@11026
   227
kleing@12517
   228
-- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
berghofe@23757
   229
inductive
berghofe@22271
   230
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
berghofe@22271
   231
  for G :: "'c prog"
berghofe@22271
   232
where
kleing@12517
   233
  refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
berghofe@22271
   234
| subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
berghofe@22271
   235
| null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
nipkow@8011
   236
Andreas@44035
   237
code_pred widen .
Andreas@44035
   238
wenzelm@22597
   239
lemmas refl = HOL.refl
wenzelm@22597
   240
kleing@12517
   241
-- "casting conversion, cf. 5.5 / 5.1.5"
kleing@12517
   242
-- "left out casts on primitve types"
berghofe@23757
   243
inductive
berghofe@22271
   244
  cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
berghofe@22271
   245
  for G :: "'c prog"
berghofe@22271
   246
where
streckem@14045
   247
  widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
berghofe@22271
   248
| subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
oheimb@11026
   249
oheimb@11026
   250
lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
oheimb@11026
   251
apply (rule iffI)
berghofe@22271
   252
apply (erule widen.cases)
oheimb@11026
   253
apply auto
oheimb@11026
   254
done
oheimb@11026
   255
oheimb@11026
   256
lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
berghofe@23757
   257
apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
oheimb@11026
   258
apply auto
oheimb@11026
   259
done
oheimb@11026
   260
oheimb@11026
   261
lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
berghofe@23757
   262
apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
oheimb@11026
   263
apply auto
oheimb@11026
   264
done
oheimb@11026
   265
oheimb@11026
   266
lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
berghofe@23757
   267
apply (ind_cases "G\<turnstile>Class C\<preceq>T")
oheimb@11026
   268
apply auto
oheimb@11026
   269
done
oheimb@11026
   270
oheimb@11026
   271
lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
oheimb@11026
   272
apply (rule iffI)
berghofe@23757
   273
apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
oheimb@11026
   274
apply auto
oheimb@11026
   275
done
nipkow@8011
   276
oheimb@11026
   277
lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
oheimb@11026
   278
apply (rule iffI)
berghofe@23757
   279
apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
oheimb@11026
   280
apply (auto elim: widen.subcls)
oheimb@11026
   281
done
oheimb@11026
   282
streckem@14045
   283
lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
berghofe@23757
   284
by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
streckem@14045
   285
streckem@14045
   286
lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
streckem@14045
   287
apply (rule iffI)
berghofe@22271
   288
apply (erule cast.cases)
streckem@14045
   289
apply auto
streckem@14045
   290
done
streckem@14045
   291
streckem@14045
   292
lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
streckem@14045
   293
apply (erule cast.cases)
streckem@14045
   294
apply simp apply (erule widen.cases) 
streckem@14045
   295
apply auto
streckem@14045
   296
done
streckem@14045
   297
kleing@12517
   298
theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
oheimb@11026
   299
proof -
kleing@12517
   300
  assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
wenzelm@11987
   301
  proof induct
kleing@12517
   302
    case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
oheimb@11026
   303
  next
wenzelm@11987
   304
    case (subcls C D T)
oheimb@11026
   305
    then obtain E where "T = Class E" by (blast dest: widen_Class)
berghofe@22271
   306
    with subcls show "G\<turnstile>Class C\<preceq>T" by auto
oheimb@11026
   307
  next
wenzelm@11987
   308
    case (null R RT)
oheimb@11026
   309
    then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
oheimb@11026
   310
    thus "G\<turnstile>NT\<preceq>RT" by auto
oheimb@11026
   311
  qed
oheimb@11026
   312
qed
oheimb@11026
   313
nipkow@8011
   314
end