src/HOL/MicroJava/J/TypeRel.thy
author krauss
Mon Aug 31 20:32:00 2009 +0200 (2009-08-31)
changeset 32461 eee4fa79398f
parent 28562 4e74209f113e
child 33954 1bc3b688548c
permissions -rw-r--r--
no consts_code for wfrec, as it violates the "code generation = equational reasoning" principle
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
haftmann@28524
    59
         None \<Rightarrow> undefined
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@28562
    71
lemma class_rec_func (*[code]*):
haftmann@20970
    72
  "class_rec G C t f = (if wf_class G then
haftmann@20970
    73
    (case class G C
haftmann@28524
    74
      of None \<Rightarrow> undefined
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
krauss@32461
   100
text {* Code generator setup (FIXME!) *}
krauss@32461
   101
krauss@32461
   102
consts_code
krauss@32461
   103
  "wfrec"   ("\<module>wfrec?")
krauss@32461
   104
attach {*
krauss@32461
   105
fun wfrec f x = f (wfrec f) x;
krauss@32461
   106
*}
krauss@32461
   107
nipkow@8011
   108
consts
nipkow@8011
   109
nipkow@14134
   110
  method :: "'c prog \<times> cname => ( sig   \<rightharpoonup> cname \<times> ty \<times> 'c)" (* ###curry *)
nipkow@14134
   111
  field  :: "'c prog \<times> cname => ( vname \<rightharpoonup> cname \<times> ty     )" (* ###curry *)
oheimb@11026
   112
  fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
nipkow@8011
   113
kleing@12517
   114
-- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
berghofe@13090
   115
defs method_def: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
oheimb@11026
   116
                           ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
oheimb@11026
   117
berghofe@22271
   118
lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
oheimb@11026
   119
  method (G,C) = (if C = Object then empty else method (G,D)) ++  
oheimb@11026
   120
  map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
oheimb@11026
   121
apply (unfold method_def)
oheimb@11026
   122
apply (simp split del: split_if)
oheimb@11026
   123
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   124
apply auto
oheimb@11026
   125
done
oheimb@11026
   126
nipkow@8011
   127
kleing@12517
   128
-- "list of fields of a class, including inherited and hidden ones"
berghofe@13090
   129
defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
oheimb@11026
   130
                           map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
oheimb@11026
   131
berghofe@22271
   132
lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wfP ((subcls1 G)^--1)|] ==>
oheimb@11026
   133
 fields (G,C) = 
oheimb@11026
   134
  map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
oheimb@11026
   135
apply (unfold fields_def)
oheimb@11026
   136
apply (simp split del: split_if)
oheimb@11026
   137
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   138
apply auto
oheimb@11026
   139
done
oheimb@11026
   140
oheimb@11026
   141
oheimb@11026
   142
defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
oheimb@11026
   143
oheimb@11026
   144
lemma field_fields: 
oheimb@11026
   145
"field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
oheimb@11026
   146
apply (unfold field_def)
oheimb@11026
   147
apply (rule table_of_remap_SomeD)
oheimb@11026
   148
apply simp
oheimb@11026
   149
done
oheimb@11026
   150
oheimb@11026
   151
kleing@12517
   152
-- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
berghofe@23757
   153
inductive
berghofe@22271
   154
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
berghofe@22271
   155
  for G :: "'c prog"
berghofe@22271
   156
where
kleing@12517
   157
  refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
berghofe@22271
   158
| subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
berghofe@22271
   159
| null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
nipkow@8011
   160
wenzelm@22597
   161
lemmas refl = HOL.refl
wenzelm@22597
   162
kleing@12517
   163
-- "casting conversion, cf. 5.5 / 5.1.5"
kleing@12517
   164
-- "left out casts on primitve types"
berghofe@23757
   165
inductive
berghofe@22271
   166
  cast    :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
berghofe@22271
   167
  for G :: "'c prog"
berghofe@22271
   168
where
streckem@14045
   169
  widen:  "G\<turnstile> C\<preceq> D ==> G\<turnstile>C \<preceq>? D"
berghofe@22271
   170
| subcls: "G\<turnstile> D\<preceq>C C ==> G\<turnstile>Class C \<preceq>? Class D"
oheimb@11026
   171
oheimb@11026
   172
lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
oheimb@11026
   173
apply (rule iffI)
berghofe@22271
   174
apply (erule widen.cases)
oheimb@11026
   175
apply auto
oheimb@11026
   176
done
oheimb@11026
   177
oheimb@11026
   178
lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
berghofe@23757
   179
apply (ind_cases "G\<turnstile>RefT R\<preceq>T")
oheimb@11026
   180
apply auto
oheimb@11026
   181
done
oheimb@11026
   182
oheimb@11026
   183
lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
berghofe@23757
   184
apply (ind_cases "G\<turnstile>S\<preceq>RefT R")
oheimb@11026
   185
apply auto
oheimb@11026
   186
done
oheimb@11026
   187
oheimb@11026
   188
lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
berghofe@23757
   189
apply (ind_cases "G\<turnstile>Class C\<preceq>T")
oheimb@11026
   190
apply auto
oheimb@11026
   191
done
oheimb@11026
   192
oheimb@11026
   193
lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
oheimb@11026
   194
apply (rule iffI)
berghofe@23757
   195
apply (ind_cases "G\<turnstile>Class C\<preceq>NT")
oheimb@11026
   196
apply auto
oheimb@11026
   197
done
nipkow@8011
   198
oheimb@11026
   199
lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
oheimb@11026
   200
apply (rule iffI)
berghofe@23757
   201
apply (ind_cases "G\<turnstile>Class C \<preceq> Class D")
oheimb@11026
   202
apply (auto elim: widen.subcls)
oheimb@11026
   203
done
oheimb@11026
   204
streckem@14045
   205
lemma widen_NT_Class [simp]: "G \<turnstile> T \<preceq> NT \<Longrightarrow> G \<turnstile> T \<preceq> Class D"
berghofe@23757
   206
by (ind_cases "G \<turnstile> T \<preceq> NT",  auto)
streckem@14045
   207
streckem@14045
   208
lemma cast_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>? RefT rT) = False"
streckem@14045
   209
apply (rule iffI)
berghofe@22271
   210
apply (erule cast.cases)
streckem@14045
   211
apply auto
streckem@14045
   212
done
streckem@14045
   213
streckem@14045
   214
lemma cast_RefT: "G \<turnstile> C \<preceq>? Class D \<Longrightarrow> \<exists> rT. C = RefT rT"
streckem@14045
   215
apply (erule cast.cases)
streckem@14045
   216
apply simp apply (erule widen.cases) 
streckem@14045
   217
apply auto
streckem@14045
   218
done
streckem@14045
   219
kleing@12517
   220
theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
oheimb@11026
   221
proof -
kleing@12517
   222
  assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
wenzelm@11987
   223
  proof induct
kleing@12517
   224
    case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
oheimb@11026
   225
  next
wenzelm@11987
   226
    case (subcls C D T)
oheimb@11026
   227
    then obtain E where "T = Class E" by (blast dest: widen_Class)
berghofe@22271
   228
    with subcls show "G\<turnstile>Class C\<preceq>T" by auto
oheimb@11026
   229
  next
wenzelm@11987
   230
    case (null R 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
nipkow@8011
   236
end