src/HOL/NanoJava/TypeRel.thy
author blanchet
Thu Jan 16 16:20:17 2014 +0100 (2014-01-16)
changeset 55017 2df6ad1dbd66
parent 45605 a89b4bc311a5
child 58860 fee7cfa69c50
permissions -rw-r--r--
adapted to move of Wfrec
oheimb@11376
     1
(*  Title:      HOL/NanoJava/TypeRel.thy
wenzelm@41589
     2
    Author:     David von Oheimb, Technische Universitaet Muenchen
oheimb@11376
     3
*)
oheimb@11376
     4
oheimb@11376
     5
header "Type relations"
oheimb@11376
     6
blanchet@55017
     7
theory TypeRel
blanchet@55017
     8
imports Decl
blanchet@55017
     9
begin
oheimb@11376
    10
krauss@44375
    11
text{* Direct subclass relation *}
krauss@44375
    12
krauss@44375
    13
definition subcls1 :: "(cname \<times> cname) set"
krauss@44375
    14
where
krauss@44375
    15
  "subcls1 \<equiv> {(C,D). C\<noteq>Object \<and> (\<exists>c. class C = Some c \<and> super c=D)}"
oheimb@11376
    16
wenzelm@35102
    17
abbreviation
wenzelm@35102
    18
  subcls1_syntax :: "[cname, cname] => bool"  ("_ <=C1 _" [71,71] 70)
wenzelm@35102
    19
  where "C <=C1 D == (C,D) \<in> subcls1"
wenzelm@35102
    20
abbreviation
wenzelm@35102
    21
  subcls_syntax  :: "[cname, cname] => bool" ("_ <=C _"  [71,71] 70)
wenzelm@35102
    22
  where "C <=C D == (C,D) \<in> subcls1^*"
oheimb@11376
    23
wenzelm@35102
    24
notation (xsymbols)
wenzelm@35102
    25
  subcls1_syntax  ("_ \<prec>C1 _"  [71,71] 70) and
wenzelm@35102
    26
  subcls_syntax  ("_ \<preceq>C _"   [71,71] 70)
oheimb@11376
    27
oheimb@11376
    28
oheimb@11565
    29
subsection "Declarations and properties not used in the meta theory"
oheimb@11376
    30
oheimb@11565
    31
text{* Widening, viz. method invocation conversion *}
berghofe@23755
    32
inductive
berghofe@23755
    33
  widen :: "ty => ty => bool"  ("_ \<preceq> _" [71,71] 70)
berghofe@23755
    34
where
berghofe@23755
    35
  refl [intro!, simp]: "T \<preceq> T"
berghofe@23755
    36
| subcls: "C\<preceq>C D \<Longrightarrow> Class C \<preceq> Class D"
berghofe@23755
    37
| null [intro!]: "NT \<preceq> R"
oheimb@11376
    38
oheimb@11376
    39
lemma subcls1D: 
oheimb@11376
    40
  "C\<prec>C1D \<Longrightarrow> C \<noteq> Object \<and> (\<exists>c. class C = Some c \<and> super c=D)"
oheimb@11376
    41
apply (unfold subcls1_def)
oheimb@11376
    42
apply auto
oheimb@11376
    43
done
oheimb@11376
    44
oheimb@11376
    45
lemma subcls1I: "\<lbrakk>class C = Some m; super m = D; C \<noteq> Object\<rbrakk> \<Longrightarrow> C\<prec>C1D"
oheimb@11376
    46
apply (unfold subcls1_def)
oheimb@11376
    47
apply auto
oheimb@11376
    48
done
oheimb@11376
    49
oheimb@11376
    50
lemma subcls1_def2: 
paulson@14952
    51
  "subcls1 = 
paulson@14952
    52
    (SIGMA C: {C. is_class C} . {D. C\<noteq>Object \<and> super (the (class C)) = D})"
oheimb@11376
    53
apply (unfold subcls1_def is_class_def)
nipkow@31166
    54
apply (auto split:split_if_asm)
oheimb@11376
    55
done
oheimb@11376
    56
oheimb@11376
    57
lemma finite_subcls1: "finite subcls1"
oheimb@11376
    58
apply(subst subcls1_def2)
oheimb@11376
    59
apply(rule finite_SigmaI [OF finite_is_class])
oheimb@11376
    60
apply(rule_tac B = "{super (the (class C))}" in finite_subset)
oheimb@11376
    61
apply  auto
oheimb@11376
    62
done
oheimb@11376
    63
haftmann@35416
    64
definition ws_prog :: "bool" where
oheimb@11376
    65
 "ws_prog \<equiv> \<forall>(C,c)\<in>set Prog. C\<noteq>Object \<longrightarrow> 
oheimb@11376
    66
                              is_class (super c) \<and> (super c,C)\<notin>subcls1^+"
oheimb@11376
    67
oheimb@11376
    68
lemma ws_progD: "\<lbrakk>class C = Some c; C\<noteq>Object; ws_prog\<rbrakk> \<Longrightarrow>  
oheimb@11376
    69
  is_class (super c) \<and> (super c,C)\<notin>subcls1^+"
oheimb@11376
    70
apply (unfold ws_prog_def class_def)
oheimb@11376
    71
apply (drule_tac map_of_SomeD)
oheimb@11376
    72
apply auto
oheimb@11376
    73
done
oheimb@11376
    74
oheimb@11376
    75
lemma subcls1_irrefl_lemma1: "ws_prog \<Longrightarrow> subcls1^-1 \<inter> subcls1^+ = {}"
oheimb@11376
    76
by (fast dest: subcls1D ws_progD)
oheimb@11376
    77
nipkow@13867
    78
(* irrefl_tranclI in Transitive_Closure.thy is more general *)
oheimb@11376
    79
lemma irrefl_tranclI': "r^-1 Int r^+ = {} ==> !x. (x, x) ~: r^+"
nipkow@13867
    80
by(blast elim: tranclE dest: trancl_into_rtrancl)
nipkow@13867
    81
oheimb@11376
    82
oheimb@11376
    83
lemmas subcls1_irrefl_lemma2 = subcls1_irrefl_lemma1 [THEN irrefl_tranclI']
oheimb@11376
    84
oheimb@11376
    85
lemma subcls1_irrefl: "\<lbrakk>(x, y) \<in> subcls1; ws_prog\<rbrakk> \<Longrightarrow> x \<noteq> y"
oheimb@11376
    86
apply (rule irrefl_trancl_rD)
oheimb@11376
    87
apply (rule subcls1_irrefl_lemma2)
oheimb@11376
    88
apply auto
oheimb@11376
    89
done
oheimb@11376
    90
wenzelm@45605
    91
lemmas subcls1_acyclic = subcls1_irrefl_lemma2 [THEN acyclicI]
oheimb@11376
    92
oheimb@11376
    93
lemma wf_subcls1: "ws_prog \<Longrightarrow> wf (subcls1\<inverse>)"
oheimb@11376
    94
by (auto intro: finite_acyclic_wf_converse finite_subcls1 subcls1_acyclic)
oheimb@11376
    95
krauss@44146
    96
definition class_rec ::"cname \<Rightarrow> (class \<Rightarrow> ('a \<times> 'b) list) \<Rightarrow> ('a \<rightharpoonup> 'b)"
krauss@44146
    97
where
krauss@44146
    98
  "class_rec \<equiv> wfrec (subcls1\<inverse>) (\<lambda>rec C f.
krauss@44146
    99
     case class C of None \<Rightarrow> undefined
krauss@44146
   100
      | Some m \<Rightarrow> (if C = Object then empty else rec (super m) f) ++ map_of (f m))"
oheimb@11376
   101
oheimb@11376
   102
lemma class_rec: "\<lbrakk>class C = Some m;  ws_prog\<rbrakk> \<Longrightarrow>
oheimb@11376
   103
 class_rec C f = (if C = Object then empty else class_rec (super m) f) ++ 
krauss@44146
   104
                 map_of (f m)"
oheimb@11376
   105
apply (drule wf_subcls1)
krauss@44146
   106
apply (subst def_wfrec[OF class_rec_def], auto)
krauss@44146
   107
apply (subst cut_apply, auto intro: subcls1I)
oheimb@11376
   108
done
oheimb@11376
   109
oheimb@11565
   110
--{* Methods of a class, with inheritance and hiding *}
krauss@44375
   111
definition method :: "cname => (mname \<rightharpoonup> methd)" where
krauss@44375
   112
  "method C \<equiv> class_rec C methods"
oheimb@11376
   113
oheimb@11376
   114
lemma method_rec: "\<lbrakk>class C = Some m; ws_prog\<rbrakk> \<Longrightarrow>
oheimb@11376
   115
method C = (if C=Object then empty else method (super m)) ++ map_of (methods m)"
oheimb@11376
   116
apply (unfold method_def)
oheimb@11376
   117
apply (erule (1) class_rec [THEN trans]);
oheimb@11376
   118
apply simp
oheimb@11376
   119
done
oheimb@11376
   120
oheimb@11376
   121
oheimb@11565
   122
--{* Fields of a class, with inheritance and hiding *}
krauss@44375
   123
definition field  :: "cname => (fname \<rightharpoonup> ty)" where
krauss@44375
   124
  "field C \<equiv> class_rec C flds"
oheimb@11376
   125
wenzelm@12264
   126
lemma flds_rec: "\<lbrakk>class C = Some m; ws_prog\<rbrakk> \<Longrightarrow>
wenzelm@12264
   127
field C = (if C=Object then empty else field (super m)) ++ map_of (flds m)"
oheimb@11376
   128
apply (unfold field_def)
oheimb@11376
   129
apply (erule (1) class_rec [THEN trans]);
oheimb@11376
   130
apply simp
oheimb@11376
   131
done
oheimb@11376
   132
oheimb@11376
   133
end