src/HOL/MicroJava/J/TypeRel.thy
author berghofe
Fri Apr 19 14:43:16 2002 +0200 (2002-04-19)
changeset 13090 4fb7a2f2c1df
parent 12911 704713ca07ea
child 14045 a34d89ce6097
permissions -rw-r--r--
Improved definition of class_rec: no longer mixes algorithm and
termination check.
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
oheimb@11026
     9
theory TypeRel = Decl:
nipkow@8011
    10
nipkow@8011
    11
consts
kleing@12517
    12
  subcls1 :: "'c prog => (cname \<times> cname) set"  -- "subclass"
kleing@12517
    13
  widen   :: "'c prog => (ty    \<times> ty   ) set"  -- "widening"
kleing@12517
    14
  cast    :: "'c prog => (cname \<times> cname) set"  -- "casting"
nipkow@8011
    15
oheimb@11372
    16
syntax (xsymbols)
oheimb@11026
    17
  subcls1 :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<prec>C1 _" [71,71,71] 70)
oheimb@11372
    18
  subcls  :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>C _"  [71,71,71] 70)
oheimb@11372
    19
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ \<turnstile> _ \<preceq> _"   [71,71,71] 70)
oheimb@11372
    20
  cast    :: "'c prog => [cname, cname] => bool" ("_ \<turnstile> _ \<preceq>? _"  [71,71,71] 70)
kleing@10061
    21
oheimb@11372
    22
syntax
kleing@10061
    23
  subcls1 :: "'c prog => [cname, cname] => bool" ("_ |- _ <=C1 _" [71,71,71] 70)
oheimb@11372
    24
  subcls  :: "'c prog => [cname, cname] => bool" ("_ |- _ <=C _"  [71,71,71] 70)
oheimb@11372
    25
  widen   :: "'c prog => [ty   , ty   ] => bool" ("_ |- _ <= _"   [71,71,71] 70)
oheimb@11372
    26
  cast    :: "'c prog => [cname, cname] => bool" ("_ |- _ <=? _"  [71,71,71] 70)
nipkow@8011
    27
nipkow@8011
    28
translations
oheimb@11026
    29
  "G\<turnstile>C \<prec>C1 D" == "(C,D) \<in> subcls1 G"
oheimb@11026
    30
  "G\<turnstile>C \<preceq>C  D" == "(C,D) \<in> (subcls1 G)^*"
oheimb@11026
    31
  "G\<turnstile>S \<preceq>   T" == "(S,T) \<in> widen   G"
oheimb@11026
    32
  "G\<turnstile>C \<preceq>?  D" == "(C,D) \<in> cast    G"
nipkow@8011
    33
kleing@12517
    34
-- "direct subclass, cf. 8.1.3"
berghofe@12443
    35
inductive "subcls1 G" intros
berghofe@12443
    36
  subcls1I: "\<lbrakk>class G C = Some (D,rest); C \<noteq> Object\<rbrakk> \<Longrightarrow> G\<turnstile>C\<prec>C1D"
nipkow@8011
    37
  
oheimb@11026
    38
lemma subcls1D: 
oheimb@11026
    39
  "G\<turnstile>C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>fs ms. class G C = Some (D,fs,ms))"
berghofe@12443
    40
apply (erule subcls1.elims)
oheimb@11026
    41
apply auto
oheimb@11026
    42
done
oheimb@11026
    43
oheimb@11026
    44
lemma subcls1_def2: 
oheimb@11026
    45
"subcls1 G = (\<Sigma>C\<in>{C. is_class G C} . {D. C\<noteq>Object \<and> fst (the (class G C))=D})"
berghofe@12443
    46
  by (auto simp add: is_class_def dest: subcls1D intro: subcls1I)
oheimb@11026
    47
oheimb@11026
    48
lemma finite_subcls1: "finite (subcls1 G)"
oheimb@11026
    49
apply(subst subcls1_def2)
oheimb@11026
    50
apply(rule finite_SigmaI [OF finite_is_class])
oheimb@11026
    51
apply(rule_tac B = "{fst (the (class G C))}" in finite_subset)
oheimb@11026
    52
apply  auto
oheimb@11026
    53
done
oheimb@11026
    54
oheimb@11026
    55
lemma subcls_is_class: "(C,D) \<in> (subcls1 G)^+ ==> is_class G C"
oheimb@11026
    56
apply (unfold is_class_def)
oheimb@11026
    57
apply(erule trancl_trans_induct)
oheimb@11026
    58
apply (auto dest!: subcls1D)
oheimb@11026
    59
done
oheimb@11026
    60
oheimb@11266
    61
lemma subcls_is_class2 [rule_format (no_asm)]: 
oheimb@11266
    62
  "G\<turnstile>C\<preceq>C D \<Longrightarrow> is_class G D \<longrightarrow> is_class G C"
oheimb@11026
    63
apply (unfold is_class_def)
oheimb@11026
    64
apply (erule rtrancl_induct)
oheimb@11026
    65
apply  (drule_tac [2] subcls1D)
oheimb@11026
    66
apply  auto
oheimb@11026
    67
done
oheimb@11026
    68
berghofe@13090
    69
constdefs
berghofe@13090
    70
  class_rec :: "'c prog \<Rightarrow> cname \<Rightarrow> 'a \<Rightarrow>
berghofe@13090
    71
    (cname \<Rightarrow> fdecl list \<Rightarrow> 'c mdecl list \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'a"
berghofe@13090
    72
  "class_rec G == wfrec ((subcls1 G)^-1)
berghofe@13090
    73
    (\<lambda>r C t f. case class G C of
berghofe@13090
    74
         None \<Rightarrow> arbitrary
berghofe@13090
    75
       | Some (D,fs,ms) \<Rightarrow> 
berghofe@13090
    76
           f C fs ms (if C = Object then t else r D t f))"
nipkow@11284
    77
berghofe@13090
    78
lemma class_rec_lemma: "wf ((subcls1 G)^-1) \<Longrightarrow> class G C = Some (D,fs,ms) \<Longrightarrow>
berghofe@13090
    79
 class_rec G C t f = f C fs ms (if C=Object then t else class_rec G D t f)"
berghofe@13090
    80
  by (simp add: class_rec_def wfrec cut_apply [OF converseI [OF subcls1I]])
oheimb@11026
    81
nipkow@8011
    82
consts
nipkow@8011
    83
oheimb@11026
    84
  method :: "'c prog \<times> cname => ( sig   \<leadsto> cname \<times> ty \<times> 'c)" (* ###curry *)
oheimb@11026
    85
  field  :: "'c prog \<times> cname => ( vname \<leadsto> cname \<times> ty     )" (* ###curry *)
oheimb@11026
    86
  fields :: "'c prog \<times> cname => ((vname \<times> cname) \<times> ty) list" (* ###curry *)
nipkow@8011
    87
kleing@12517
    88
-- "methods of a class, with inheritance, overriding and hiding, cf. 8.4.6"
berghofe@13090
    89
defs method_def: "method \<equiv> \<lambda>(G,C). class_rec G C empty (\<lambda>C fs ms ts.
oheimb@11026
    90
                           ts ++ map_of (map (\<lambda>(s,m). (s,(C,m))) ms))"
oheimb@11026
    91
oheimb@11026
    92
lemma method_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
    93
  method (G,C) = (if C = Object then empty else method (G,D)) ++  
oheimb@11026
    94
  map_of (map (\<lambda>(s,m). (s,(C,m))) ms)"
oheimb@11026
    95
apply (unfold method_def)
oheimb@11026
    96
apply (simp split del: split_if)
oheimb@11026
    97
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
    98
apply auto
oheimb@11026
    99
done
oheimb@11026
   100
nipkow@8011
   101
kleing@12517
   102
-- "list of fields of a class, including inherited and hidden ones"
berghofe@13090
   103
defs fields_def: "fields \<equiv> \<lambda>(G,C). class_rec G C []    (\<lambda>C fs ms ts.
oheimb@11026
   104
                           map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ ts)"
oheimb@11026
   105
oheimb@11026
   106
lemma fields_rec_lemma: "[|class G C = Some (D,fs,ms); wf ((subcls1 G)^-1)|] ==>
oheimb@11026
   107
 fields (G,C) = 
oheimb@11026
   108
  map (\<lambda>(fn,ft). ((fn,C),ft)) fs @ (if C = Object then [] else fields (G,D))"
oheimb@11026
   109
apply (unfold fields_def)
oheimb@11026
   110
apply (simp split del: split_if)
oheimb@11026
   111
apply (erule (1) class_rec_lemma [THEN trans]);
oheimb@11026
   112
apply auto
oheimb@11026
   113
done
oheimb@11026
   114
oheimb@11026
   115
oheimb@11026
   116
defs field_def: "field == map_of o (map (\<lambda>((fn,fd),ft). (fn,(fd,ft)))) o fields"
oheimb@11026
   117
oheimb@11026
   118
lemma field_fields: 
oheimb@11026
   119
"field (G,C) fn = Some (fd, fT) \<Longrightarrow> map_of (fields (G,C)) (fn, fd) = Some fT"
oheimb@11026
   120
apply (unfold field_def)
oheimb@11026
   121
apply (rule table_of_remap_SomeD)
oheimb@11026
   122
apply simp
oheimb@11026
   123
done
oheimb@11026
   124
oheimb@11026
   125
kleing@12517
   126
-- "widening, viz. method invocation conversion,cf. 5.3 i.e. sort of syntactic subtyping"
kleing@12517
   127
inductive "widen G" intros 
kleing@12517
   128
  refl   [intro!, simp]:       "G\<turnstile>      T \<preceq> T"   -- "identity conv., cf. 5.1.1"
oheimb@11026
   129
  subcls         : "G\<turnstile>C\<preceq>C D ==> G\<turnstile>Class C \<preceq> Class D"
oheimb@11026
   130
  null   [intro!]:             "G\<turnstile>     NT \<preceq> RefT R"
nipkow@8011
   131
kleing@12517
   132
-- "casting conversion, cf. 5.5 / 5.1.5"
kleing@12517
   133
-- "left out casts on primitve types"
kleing@12517
   134
inductive "cast G" intros
oheimb@11026
   135
  widen:  "G\<turnstile>C\<preceq>C D ==> G\<turnstile>C \<preceq>? D"
oheimb@11026
   136
  subcls: "G\<turnstile>D\<preceq>C C ==> G\<turnstile>C \<preceq>? D"
oheimb@11026
   137
oheimb@11026
   138
lemma widen_PrimT_RefT [iff]: "(G\<turnstile>PrimT pT\<preceq>RefT rT) = False"
oheimb@11026
   139
apply (rule iffI)
oheimb@11026
   140
apply (erule widen.elims)
oheimb@11026
   141
apply auto
oheimb@11026
   142
done
oheimb@11026
   143
oheimb@11026
   144
lemma widen_RefT: "G\<turnstile>RefT R\<preceq>T ==> \<exists>t. T=RefT t"
oheimb@11026
   145
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   146
apply auto
oheimb@11026
   147
done
oheimb@11026
   148
oheimb@11026
   149
lemma widen_RefT2: "G\<turnstile>S\<preceq>RefT R ==> \<exists>t. S=RefT t"
oheimb@11026
   150
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   151
apply auto
oheimb@11026
   152
done
oheimb@11026
   153
oheimb@11026
   154
lemma widen_Class: "G\<turnstile>Class C\<preceq>T ==> \<exists>D. T=Class D"
oheimb@11026
   155
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   156
apply auto
oheimb@11026
   157
done
oheimb@11026
   158
oheimb@11026
   159
lemma widen_Class_NullT [iff]: "(G\<turnstile>Class C\<preceq>NT) = False"
oheimb@11026
   160
apply (rule iffI)
oheimb@11026
   161
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   162
apply auto
oheimb@11026
   163
done
nipkow@8011
   164
oheimb@11026
   165
lemma widen_Class_Class [iff]: "(G\<turnstile>Class C\<preceq> Class D) = (G\<turnstile>C\<preceq>C D)"
oheimb@11026
   166
apply (rule iffI)
oheimb@11026
   167
apply (ind_cases "G\<turnstile>S\<preceq>T")
oheimb@11026
   168
apply (auto elim: widen.subcls)
oheimb@11026
   169
done
oheimb@11026
   170
kleing@12517
   171
theorem widen_trans[trans]: "\<lbrakk>G\<turnstile>S\<preceq>U; G\<turnstile>U\<preceq>T\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
oheimb@11026
   172
proof -
kleing@12517
   173
  assume "G\<turnstile>S\<preceq>U" thus "\<And>T. G\<turnstile>U\<preceq>T \<Longrightarrow> G\<turnstile>S\<preceq>T"
wenzelm@11987
   174
  proof induct
kleing@12517
   175
    case (refl T T') thus "G\<turnstile>T\<preceq>T'" .
oheimb@11026
   176
  next
wenzelm@11987
   177
    case (subcls C D T)
oheimb@11026
   178
    then obtain E where "T = Class E" by (blast dest: widen_Class)
wenzelm@11987
   179
    with subcls show "G\<turnstile>Class C\<preceq>T" by (auto elim: rtrancl_trans)
oheimb@11026
   180
  next
wenzelm@11987
   181
    case (null R RT)
oheimb@11026
   182
    then obtain rt where "RT = RefT rt" by (blast dest: widen_RefT)
oheimb@11026
   183
    thus "G\<turnstile>NT\<preceq>RT" by auto
oheimb@11026
   184
  qed
oheimb@11026
   185
qed
oheimb@11026
   186
nipkow@8011
   187
end