src/HOL/MicroJava/J/WellType.thy
author berghofe
Wed Jul 11 11:32:02 2007 +0200 (2007-07-11)
changeset 23757 087b0a241557
parent 22271 51a80e238b29
child 35102 cc7a0b9f938c
permissions -rw-r--r--
- Renamed inductive2 to inductive
- Renamed some theorems about transitive closure for predicates
     1 (*  Title:      HOL/MicroJava/J/WellType.thy
     2     ID:         $Id$
     3     Author:     David von Oheimb
     4     Copyright   1999 Technische Universitaet Muenchen
     5 *)
     6 
     7 header {* \isaheader{Well-typedness Constraints} *}
     8 
     9 theory WellType imports Term WellForm begin
    10 
    11 text {*
    12 the formulation of well-typedness of method calls given below (as well as
    13 the Java Specification 1.0) is a little too restrictive: Is does not allow
    14 methods of class Object to be called upon references of interface type.
    15 
    16 \begin{description}
    17 \item[simplifications:]\ \\
    18 \begin{itemize}
    19 \item the type rules include all static checks on expressions and statements, 
    20   e.g.\ definedness of names (of parameters, locals, fields, methods)
    21 \end{itemize}
    22 \end{description}
    23 *}
    24 
    25 text "local variables, including method parameters and This:"
    26 types 
    27   lenv   = "vname \<rightharpoonup> ty"
    28   'c env = "'c prog \<times> lenv"
    29 
    30 syntax
    31   prg    :: "'c env => 'c prog"
    32   localT :: "'c env => (vname \<rightharpoonup> ty)"
    33 
    34 translations  
    35   "prg"    => "fst"
    36   "localT" => "snd"
    37 
    38 consts
    39   more_spec :: "'c prog => (ty \<times> 'x) \<times> ty list =>
    40                 (ty \<times> 'x) \<times> ty list => bool"
    41   appl_methds :: "'c prog =>  cname => sig => ((ty \<times> ty) \<times> ty list) set"
    42   max_spec :: "'c prog =>  cname => sig => ((ty \<times> ty) \<times> ty list) set"
    43 
    44 defs
    45   more_spec_def: "more_spec G == \<lambda>((d,h),pTs). \<lambda>((d',h'),pTs'). G\<turnstile>d\<preceq>d' \<and>
    46                                 list_all2 (\<lambda>T T'. G\<turnstile>T\<preceq>T') pTs pTs'"
    47   
    48   -- "applicable methods, cf. 15.11.2.1"
    49   appl_methds_def: "appl_methds G C == \<lambda>(mn, pTs).
    50                      {((Class md,rT),pTs') |md rT mb pTs'.
    51                       method (G,C)  (mn, pTs') = Some (md,rT,mb) \<and>
    52                       list_all2 (\<lambda>T T'. G\<turnstile>T\<preceq>T') pTs pTs'}"
    53 
    54   -- "maximally specific methods, cf. 15.11.2.2"
    55   max_spec_def: "max_spec G C sig == {m. m \<in>appl_methds G C sig \<and> 
    56                                        (\<forall>m'\<in>appl_methds G C sig.
    57                                          more_spec G m' m --> m' = m)}"
    58 
    59 lemma max_spec2appl_meths: 
    60   "x \<in> max_spec G C sig ==> x \<in> appl_methds G C sig"
    61 apply (unfold max_spec_def)
    62 apply (fast)
    63 done
    64 
    65 lemma appl_methsD: 
    66 "((md,rT),pTs')\<in>appl_methds G C (mn, pTs) ==>  
    67   \<exists>D b. md = Class D \<and> method (G,C) (mn, pTs') = Some (D,rT,b)  
    68   \<and> list_all2 (\<lambda>T T'. G\<turnstile>T\<preceq>T') pTs pTs'"
    69 apply (unfold appl_methds_def)
    70 apply (fast)
    71 done
    72 
    73 lemmas max_spec2mheads = insertI1 [THEN [2] equalityD2 [THEN subsetD], 
    74                          THEN max_spec2appl_meths, THEN appl_methsD]
    75 
    76 
    77 consts
    78   typeof :: "(loc => ty option) => val => ty option"
    79 
    80 primrec
    81   "typeof dt  Unit    = Some (PrimT Void)"
    82   "typeof dt  Null    = Some NT"
    83   "typeof dt (Bool b) = Some (PrimT Boolean)"
    84   "typeof dt (Intg i) = Some (PrimT Integer)"
    85   "typeof dt (Addr a) = dt a"
    86 
    87 lemma is_type_typeof [rule_format (no_asm), simp]: 
    88   "(\<forall>a. v \<noteq> Addr a) --> (\<exists>T. typeof t v = Some T \<and> is_type G T)"
    89 apply (rule val.induct)
    90 apply     auto
    91 done
    92 
    93 lemma typeof_empty_is_type [rule_format (no_asm)]: 
    94   "typeof (\<lambda>a. None) v = Some T \<longrightarrow> is_type G T"
    95 apply (rule val.induct)
    96 apply     auto
    97 done
    98 
    99 lemma typeof_default_val: "\<exists>T. (typeof dt (default_val ty) = Some T) \<and> G\<turnstile> T \<preceq> ty"
   100 apply (case_tac ty)
   101 apply (case_tac prim_ty)
   102 apply auto
   103 done
   104 
   105 types
   106   java_mb = "vname list \<times> (vname \<times> ty) list \<times> stmt \<times> expr"
   107 -- "method body with parameter names, local variables, block, result expression."
   108 -- "local variables might include This, which is hidden anyway"
   109   
   110 inductive
   111   ty_expr :: "'c env => expr => ty => bool" ("_ \<turnstile> _ :: _" [51, 51, 51] 50)
   112   and ty_exprs :: "'c env => expr list => ty list => bool" ("_ \<turnstile> _ [::] _" [51, 51, 51] 50)
   113   and wt_stmt :: "'c env => stmt => bool" ("_ \<turnstile> _ \<surd>" [51, 51] 50)
   114 where
   115   
   116   NewC: "[| is_class (prg E) C |] ==>
   117          E\<turnstile>NewC C::Class C"  -- "cf. 15.8"
   118 
   119   -- "cf. 15.15"
   120 | Cast: "[| E\<turnstile>e::C; is_class (prg E) D;
   121             prg E\<turnstile>C\<preceq>? Class D |] ==>
   122          E\<turnstile>Cast D e:: Class D"
   123 
   124   -- "cf. 15.7.1"
   125 | Lit:    "[| typeof (\<lambda>v. None) x = Some T |] ==>
   126          E\<turnstile>Lit x::T"
   127 
   128   
   129   -- "cf. 15.13.1"
   130 | LAcc: "[| localT E v = Some T; is_type (prg E) T |] ==>
   131          E\<turnstile>LAcc v::T"
   132 
   133 | BinOp:"[| E\<turnstile>e1::T;
   134             E\<turnstile>e2::T;
   135             if bop = Eq then T' = PrimT Boolean
   136                         else T' = T \<and> T = PrimT Integer|] ==>
   137             E\<turnstile>BinOp bop e1 e2::T'"
   138 
   139   -- "cf. 15.25, 15.25.1"
   140 | LAss: "[| v ~= This;
   141             E\<turnstile>LAcc v::T;
   142             E\<turnstile>e::T';
   143             prg E\<turnstile>T'\<preceq>T |] ==>
   144          E\<turnstile>v::=e::T'"
   145 
   146   -- "cf. 15.10.1"
   147 | FAcc: "[| E\<turnstile>a::Class C; 
   148             field (prg E,C) fn = Some (fd,fT) |] ==>
   149             E\<turnstile>{fd}a..fn::fT"
   150 
   151   -- "cf. 15.25, 15.25.1"
   152 | FAss: "[| E\<turnstile>{fd}a..fn::T;
   153             E\<turnstile>v        ::T';
   154             prg E\<turnstile>T'\<preceq>T |] ==>
   155          E\<turnstile>{fd}a..fn:=v::T'"
   156 
   157 
   158   -- "cf. 15.11.1, 15.11.2, 15.11.3"
   159 | Call: "[| E\<turnstile>a::Class C;
   160             E\<turnstile>ps[::]pTs;
   161             max_spec (prg E) C (mn, pTs) = {((md,rT),pTs')} |] ==>
   162          E\<turnstile>{C}a..mn({pTs'}ps)::rT"
   163 
   164 -- "well-typed expression lists"
   165 
   166   -- "cf. 15.11.???"
   167 | Nil: "E\<turnstile>[][::][]"
   168 
   169   -- "cf. 15.11.???"
   170 | Cons:"[| E\<turnstile>e::T;
   171            E\<turnstile>es[::]Ts |] ==>
   172         E\<turnstile>e#es[::]T#Ts"
   173 
   174 -- "well-typed statements"
   175 
   176 | Skip:"E\<turnstile>Skip\<surd>"
   177 
   178 | Expr:"[| E\<turnstile>e::T |] ==>
   179         E\<turnstile>Expr e\<surd>"
   180 
   181 | Comp:"[| E\<turnstile>s1\<surd>; 
   182            E\<turnstile>s2\<surd> |] ==>
   183         E\<turnstile>s1;; s2\<surd>"
   184 
   185   -- "cf. 14.8"
   186 | Cond:"[| E\<turnstile>e::PrimT Boolean;
   187            E\<turnstile>s1\<surd>;
   188            E\<turnstile>s2\<surd> |] ==>
   189          E\<turnstile>If(e) s1 Else s2\<surd>"
   190 
   191   -- "cf. 14.10"
   192 | Loop:"[| E\<turnstile>e::PrimT Boolean;
   193            E\<turnstile>s\<surd> |] ==>
   194         E\<turnstile>While(e) s\<surd>"
   195 
   196 
   197 constdefs
   198 
   199  wf_java_mdecl :: "'c prog => cname => java_mb mdecl => bool"
   200 "wf_java_mdecl G C == \<lambda>((mn,pTs),rT,(pns,lvars,blk,res)).
   201   length pTs = length pns \<and>
   202   distinct pns \<and>
   203   unique lvars \<and>
   204         This \<notin> set pns \<and> This \<notin> set (map fst lvars) \<and> 
   205   (\<forall>pn\<in>set pns. map_of lvars pn = None) \<and>
   206   (\<forall>(vn,T)\<in>set lvars. is_type G T) &
   207   (let E = (G,map_of lvars(pns[\<mapsto>]pTs)(This\<mapsto>Class C)) in
   208    E\<turnstile>blk\<surd> \<and> (\<exists>T. E\<turnstile>res::T \<and> G\<turnstile>T\<preceq>rT))"
   209 
   210 syntax 
   211  wf_java_prog :: "'c prog => bool"
   212 translations
   213   "wf_java_prog" == "wf_prog wf_java_mdecl"
   214 
   215 lemma wf_java_prog_wf_java_mdecl: "\<lbrakk> 
   216   wf_java_prog G; (C, D, fds, mths) \<in> set G; jmdcl \<in> set mths \<rbrakk>
   217   \<Longrightarrow> wf_java_mdecl G C jmdcl"
   218 apply (simp only: wf_prog_def) 
   219 apply (erule conjE)+
   220 apply (drule bspec, assumption)
   221 apply (simp add: wf_cdecl_mdecl_def split_beta)
   222 done
   223 
   224 
   225 lemma wt_is_type: "(E\<turnstile>e::T \<longrightarrow> ws_prog (prg E) \<longrightarrow> is_type (prg E) T) \<and>  
   226        (E\<turnstile>es[::]Ts \<longrightarrow> ws_prog (prg E) \<longrightarrow> Ball (set Ts) (is_type (prg E))) \<and> 
   227        (E\<turnstile>c \<surd> \<longrightarrow> True)"
   228 apply (rule ty_expr_ty_exprs_wt_stmt.induct)
   229 apply auto
   230 apply (   erule typeof_empty_is_type)
   231 apply (  simp split add: split_if_asm)
   232 apply ( drule field_fields)
   233 apply ( drule (1) fields_is_type)
   234 apply (  simp (no_asm_simp))
   235 apply  (assumption)
   236 apply (auto dest!: max_spec2mheads method_wf_mhead is_type_rTI 
   237             simp add: wf_mdecl_def)
   238 done
   239 
   240 lemmas ty_expr_is_type = wt_is_type [THEN conjunct1,THEN mp, rule_format]
   241 
   242 lemma expr_class_is_class: "
   243   \<lbrakk>ws_prog (prg E); E \<turnstile> e :: Class C\<rbrakk> \<Longrightarrow> is_class (prg E) C"
   244   by (frule ty_expr_is_type, assumption, simp)
   245 
   246 
   247 end