src/HOL/MicroJava/J/WellType.thy
author nipkow
Thu Feb 14 12:06:07 2002 +0100 (2002-02-14)
changeset 12888 f6c1e7306c40
parent 12517 360e3215f029
child 12911 704713ca07ea
permissions -rw-r--r--
nodups -> distinct
     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 "Well-typedness Constraints"
     8 
     9 theory WellType = Term + WellForm:
    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 \<leadsto> ty"
    28   'c env = "'c prog \<times> lenv"
    29 
    30 syntax
    31   prg    :: "'c env => 'c prog"
    32   localT :: "'c env => (vname \<leadsto> 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 types
   100   java_mb = "vname list \<times> (vname \<times> ty) list \<times> stmt \<times> expr"
   101 -- "method body with parameter names, local variables, block, result expression."
   102 -- "local variables might include This, which is hidden anyway"
   103 
   104 consts
   105   ty_expr :: "java_mb env => (expr      \<times> ty     ) set"
   106   ty_exprs:: "java_mb env => (expr list \<times> ty list) set"
   107   wt_stmt :: "java_mb env =>  stmt                 set"
   108 
   109 syntax (xsymbols)
   110   ty_expr :: "java_mb env => [expr     , ty     ] => bool" ("_ \<turnstile> _ :: _"   [51,51,51]50)
   111   ty_exprs:: "java_mb env => [expr list, ty list] => bool" ("_ \<turnstile> _ [::] _" [51,51,51]50)
   112   wt_stmt :: "java_mb env =>  stmt                => bool" ("_ \<turnstile> _ \<surd>"      [51,51   ]50)
   113 
   114 syntax
   115   ty_expr :: "java_mb env => [expr     , ty     ] => bool" ("_ |- _ :: _"   [51,51,51]50)
   116   ty_exprs:: "java_mb env => [expr list, ty list] => bool" ("_ |- _ [::] _" [51,51,51]50)
   117   wt_stmt :: "java_mb env =>  stmt                => bool" ("_ |- _ [ok]"   [51,51   ]50)
   118 
   119 
   120 translations
   121   "E\<turnstile>e :: T" == "(e,T) \<in> ty_expr  E"
   122   "E\<turnstile>e[::]T" == "(e,T) \<in> ty_exprs E"
   123   "E\<turnstile>c \<surd>"    == "c     \<in> wt_stmt  E"
   124   
   125 inductive "ty_expr E" "ty_exprs E" "wt_stmt E" intros
   126   
   127   NewC: "[| is_class (prg E) C |] ==>
   128          E\<turnstile>NewC C::Class C"  -- "cf. 15.8"
   129 
   130   -- "cf. 15.15"
   131   Cast: "[| E\<turnstile>e::Class C; is_class (prg E) D;
   132             prg E\<turnstile>C\<preceq>? D |] ==>
   133          E\<turnstile>Cast D e::Class D"
   134 
   135   -- "cf. 15.7.1"
   136   Lit:    "[| typeof (\<lambda>v. None) x = Some T |] ==>
   137          E\<turnstile>Lit x::T"
   138 
   139   
   140   -- "cf. 15.13.1"
   141   LAcc: "[| localT E v = Some T; is_type (prg E) T |] ==>
   142          E\<turnstile>LAcc v::T"
   143 
   144   BinOp:"[| E\<turnstile>e1::T;
   145             E\<turnstile>e2::T;
   146             if bop = Eq then T' = PrimT Boolean
   147                         else T' = T \<and> T = PrimT Integer|] ==>
   148             E\<turnstile>BinOp bop e1 e2::T'"
   149 
   150   -- "cf. 15.25, 15.25.1"
   151   LAss: "[| v ~= This;
   152             E\<turnstile>LAcc v::T;
   153       E\<turnstile>e::T';
   154             prg E\<turnstile>T'\<preceq>T |] ==>
   155          E\<turnstile>v::=e::T'"
   156 
   157   -- "cf. 15.10.1"
   158   FAcc: "[| E\<turnstile>a::Class C; 
   159             field (prg E,C) fn = Some (fd,fT) |] ==>
   160             E\<turnstile>{fd}a..fn::fT"
   161 
   162   -- "cf. 15.25, 15.25.1"
   163   FAss: "[| E\<turnstile>{fd}a..fn::T;
   164             E\<turnstile>v        ::T';
   165             prg E\<turnstile>T'\<preceq>T |] ==>
   166          E\<turnstile>{fd}a..fn:=v::T'"
   167 
   168 
   169   -- "cf. 15.11.1, 15.11.2, 15.11.3"
   170   Call: "[| E\<turnstile>a::Class C;
   171             E\<turnstile>ps[::]pTs;
   172             max_spec (prg E) C (mn, pTs) = {((md,rT),pTs')} |] ==>
   173          E\<turnstile>{C}a..mn({pTs'}ps)::rT"
   174 
   175 -- "well-typed expression lists"
   176 
   177   -- "cf. 15.11.???"
   178   Nil: "E\<turnstile>[][::][]"
   179 
   180   -- "cf. 15.11.???"
   181   Cons:"[| E\<turnstile>e::T;
   182            E\<turnstile>es[::]Ts |] ==>
   183         E\<turnstile>e#es[::]T#Ts"
   184 
   185 -- "well-typed statements"
   186 
   187   Skip:"E\<turnstile>Skip\<surd>"
   188 
   189   Expr:"[| E\<turnstile>e::T |] ==>
   190         E\<turnstile>Expr e\<surd>"
   191 
   192   Comp:"[| E\<turnstile>s1\<surd>; 
   193            E\<turnstile>s2\<surd> |] ==>
   194         E\<turnstile>s1;; s2\<surd>"
   195 
   196   -- "cf. 14.8"
   197   Cond:"[| E\<turnstile>e::PrimT Boolean;
   198            E\<turnstile>s1\<surd>;
   199            E\<turnstile>s2\<surd> |] ==>
   200          E\<turnstile>If(e) s1 Else s2\<surd>"
   201 
   202   -- "cf. 14.10"
   203   Loop:"[| E\<turnstile>e::PrimT Boolean;
   204            E\<turnstile>s\<surd> |] ==>
   205         E\<turnstile>While(e) s\<surd>"
   206 
   207 constdefs
   208 
   209  wf_java_mdecl :: "java_mb prog => cname => java_mb mdecl => bool"
   210 "wf_java_mdecl G C == \<lambda>((mn,pTs),rT,(pns,lvars,blk,res)).
   211   length pTs = length pns \<and>
   212   distinct pns \<and>
   213   unique lvars \<and>
   214         This \<notin> set pns \<and> This \<notin> set (map fst lvars) \<and> 
   215   (\<forall>pn\<in>set pns. map_of lvars pn = None) \<and>
   216   (\<forall>(vn,T)\<in>set lvars. is_type G T) &
   217   (let E = (G,map_of lvars(pns[\<mapsto>]pTs)(This\<mapsto>Class C)) in
   218    E\<turnstile>blk\<surd> \<and> (\<exists>T. E\<turnstile>res::T \<and> G\<turnstile>T\<preceq>rT))"
   219 
   220 syntax 
   221  wf_java_prog :: "java_mb prog => bool"
   222 translations
   223   "wf_java_prog" == "wf_prog wf_java_mdecl"
   224 
   225 
   226 lemma wt_is_type: "wf_prog wf_mb G \<Longrightarrow> ((G,L)\<turnstile>e::T \<longrightarrow> is_type G T) \<and>  
   227        ((G,L)\<turnstile>es[::]Ts \<longrightarrow> Ball (set Ts) (is_type G)) \<and> ((G,L)\<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_mdecl 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, COMP swap_prems_rl]
   241 
   242 end