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