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