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