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