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