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