author | wenzelm |
Tue, 01 Feb 2000 12:26:47 +0100 | |
changeset 8178 | a6a4fb7b819b |
parent 8105 | 2dda3e88d23f |
child 9240 | f4d76cb26433 |
permissions | -rw-r--r-- |
8011 | 1 |
(* Title: HOL/MicroJava/J/WellType.thy |
2 |
ID: $Id$ |
|
3 |
Author: David von Oheimb |
|
4 |
Copyright 1999 Technische Universitaet Muenchen |
|
5 |
||
6 |
Well-typedness of Java programs |
|
7 |
||
8 |
the formulation of well-typedness of method calls given below (as well as |
|
9 |
the Java Specification 1.0) is a little too restrictive: Is does not allow |
|
10 |
methods of class Object to be called upon references of interface type. |
|
11 |
||
12 |
simplifications: |
|
13 |
* the type rules include all static checks on expressions and statements, e.g. |
|
14 |
definedness of names (of parameters, locals, fields, methods) |
|
15 |
||
16 |
*) |
|
17 |
||
18 |
WellType = Term + WellForm + |
|
19 |
||
20 |
types lenv (* local variables, including method parameters and This *) |
|
21 |
= "vname \\<leadsto> ty" |
|
22 |
'c env |
|
23 |
= "'c prog \\<times> lenv" |
|
24 |
||
25 |
syntax |
|
26 |
||
27 |
prg :: "'c env \\<Rightarrow> 'c prog" |
|
28 |
localT :: "'c env \\<Rightarrow> (vname \\<leadsto> ty)" |
|
29 |
||
30 |
translations |
|
31 |
||
32 |
"prg" => "fst" |
|
33 |
"localT" => "snd" |
|
34 |
||
35 |
consts |
|
36 |
||
37 |
more_spec :: "'c prog \\<Rightarrow> (ty \\<times> 'x) \\<times> ty list \\<Rightarrow> |
|
38 |
(ty \\<times> 'x) \\<times> ty list \\<Rightarrow> bool" |
|
8085 | 39 |
appl_methds :: "'c prog \\<Rightarrow> cname \\<Rightarrow> sig \\<Rightarrow> ((ty \\<times> ty) \\<times> ty list) set" |
40 |
max_spec :: "'c prog \\<Rightarrow> cname \\<Rightarrow> sig \\<Rightarrow> ((ty \\<times> ty) \\<times> ty list) set" |
|
8011 | 41 |
|
42 |
defs |
|
43 |
||
44 |
more_spec_def "more_spec G \\<equiv> \\<lambda>((d,h),pTs). \\<lambda>((d',h'),pTs'). G\\<turnstile>d\\<preceq>d' \\<and> |
|
45 |
list_all2 (\\<lambda>T T'. G\\<turnstile>T\\<preceq>T') pTs pTs'" |
|
46 |
||
47 |
(* applicable methods, cf. 15.11.2.1 *) |
|
8105
2dda3e88d23f
simplified definition of appl_methds, removing m_head
oheimb
parents:
8085
diff
changeset
|
48 |
appl_methds_def "appl_methds G C \\<equiv> \\<lambda>(mn, pTs). |
2dda3e88d23f
simplified definition of appl_methds, removing m_head
oheimb
parents:
8085
diff
changeset
|
49 |
{((Class md,rT),pTs') |md rT mb pTs'. |
2dda3e88d23f
simplified definition of appl_methds, removing m_head
oheimb
parents:
8085
diff
changeset
|
50 |
method (G,C) (mn, pTs') = Some (md,rT,mb) \\<and> |
8011 | 51 |
list_all2 (\\<lambda>T T'. G\\<turnstile>T\\<preceq>T') pTs pTs'}" |
52 |
||
53 |
(* maximally specific methods, cf. 15.11.2.2 *) |
|
8085 | 54 |
max_spec_def "max_spec G C sig \\<equiv> {m. m \\<in>appl_methds G C sig \\<and> |
55 |
(\\<forall>m'\\<in>appl_methds G C sig. |
|
8011 | 56 |
more_spec G m' m \\<longrightarrow> m' = m)}" |
57 |
consts |
|
58 |
||
59 |
typeof :: "(loc \\<Rightarrow> ty option) \\<Rightarrow> val \\<Rightarrow> ty option" |
|
60 |
||
61 |
primrec |
|
62 |
"typeof dt Unit = Some (PrimT Void)" |
|
63 |
"typeof dt Null = Some NT" |
|
64 |
"typeof dt (Bool b) = Some (PrimT Boolean)" |
|
65 |
"typeof dt (Intg i) = Some (PrimT Integer)" |
|
66 |
"typeof dt (Addr a) = dt a" |
|
67 |
||
68 |
types |
|
8082 | 69 |
java_mb = "vname list \\<times> (vname \\<times> ty) list \\<times> stmt \\<times> expr" |
8011 | 70 |
(* method body with parameter names, local variables, block, result expression *) |
71 |
||
72 |
consts |
|
73 |
||
8082 | 74 |
ty_expr :: "java_mb env \\<Rightarrow> (expr \\<times> ty ) set" |
75 |
ty_exprs:: "java_mb env \\<Rightarrow> (expr list \\<times> ty list) set" |
|
76 |
wt_stmt :: "java_mb env \\<Rightarrow> stmt set" |
|
8011 | 77 |
|
78 |
syntax |
|
79 |
||
8082 | 80 |
ty_expr :: "java_mb env \\<Rightarrow> [expr , ty ] \\<Rightarrow> bool" ("_\\<turnstile>_\\<Colon>_" [51,51,51]50) |
81 |
ty_exprs:: "java_mb env \\<Rightarrow> [expr list, ty list] \\<Rightarrow> bool" ("_\\<turnstile>_[\\<Colon>]_"[51,51,51]50) |
|
82 |
wt_stmt :: "java_mb env \\<Rightarrow> stmt \\<Rightarrow> bool" ("_\\<turnstile>_ \\<surd>" [51,51 ]50) |
|
8011 | 83 |
|
84 |
translations |
|
85 |
"E\\<turnstile>e \\<Colon> T" == "(e,T) \\<in> ty_expr E" |
|
86 |
"E\\<turnstile>e[\\<Colon>]T" == "(e,T) \\<in> ty_exprs E" |
|
87 |
"E\\<turnstile>c \\<surd>" == "c \\<in> wt_stmt E" |
|
88 |
||
89 |
inductive "ty_expr E" "ty_exprs E" "wt_stmt E" intrs |
|
90 |
||
91 |
(* well-typed expressions *) |
|
92 |
||
93 |
(* cf. 15.8 *) |
|
94 |
NewC "\\<lbrakk>is_class (prg E) C\\<rbrakk> \\<Longrightarrow> |
|
95 |
E\\<turnstile>NewC C\\<Colon>Class C" |
|
96 |
||
97 |
(* cf. 15.15 *) |
|
98 |
Cast "\\<lbrakk>E\\<turnstile>e\\<Colon>T; |
|
99 |
prg E\\<turnstile>T\\<Rightarrow>? T'\\<rbrakk> \\<Longrightarrow> |
|
100 |
E\\<turnstile>Cast T' e\\<Colon>T'" |
|
101 |
||
102 |
(* cf. 15.7.1 *) |
|
103 |
Lit "\\<lbrakk>typeof (\\<lambda>v. None) x = Some T\\<rbrakk> \\<Longrightarrow> |
|
104 |
E\\<turnstile>Lit x\\<Colon>T" |
|
105 |
||
106 |
(* cf. 15.13.1 *) |
|
107 |
LAcc "\\<lbrakk>localT E v = Some T; is_type (prg E) T\\<rbrakk> \\<Longrightarrow> |
|
108 |
E\\<turnstile>LAcc v\\<Colon>T" |
|
109 |
||
110 |
(* cf. 15.25, 15.25.1 *) |
|
111 |
LAss "\\<lbrakk>E\\<turnstile>LAcc v\\<Colon>T; |
|
112 |
E\\<turnstile>e\\<Colon>T'; |
|
113 |
prg E\\<turnstile>T'\\<preceq>T\\<rbrakk> \\<Longrightarrow> |
|
114 |
E\\<turnstile>v\\<Colon>=e\\<Colon>T'" |
|
115 |
||
116 |
(* cf. 15.10.1 *) |
|
117 |
FAcc "\\<lbrakk>E\\<turnstile>a\\<Colon>Class C; |
|
8034
6fc37b5c5e98
Various little changes like cmethd -> method and cfield -> field.
nipkow
parents:
8011
diff
changeset
|
118 |
field (prg E,C) fn = Some (fd,fT)\\<rbrakk> \\<Longrightarrow> |
8011 | 119 |
E\\<turnstile>{fd}a..fn\\<Colon>fT" |
120 |
||
121 |
(* cf. 15.25, 15.25.1 *) |
|
122 |
FAss "\\<lbrakk>E\\<turnstile>{fd}a..fn\\<Colon>T; |
|
123 |
E\\<turnstile>v \\<Colon>T'; |
|
124 |
prg E\\<turnstile>T'\\<preceq>T\\<rbrakk> \\<Longrightarrow> |
|
125 |
E\\<turnstile>{fd}a..fn\\<in>=v\\<Colon>T'" |
|
126 |
||
127 |
||
128 |
(* cf. 15.11.1, 15.11.2, 15.11.3 *) |
|
8085 | 129 |
Call "\\<lbrakk>E\\<turnstile>a\\<Colon>Class C; |
8011 | 130 |
E\\<turnstile>ps[\\<Colon>]pTs; |
8085 | 131 |
max_spec (prg E) C (mn, pTs) = {((md,rT),pTs')}\\<rbrakk> \\<Longrightarrow> |
8011 | 132 |
E\\<turnstile>a..mn({pTs'}ps)\\<Colon>rT" |
133 |
||
134 |
(* well-typed expression lists *) |
|
135 |
||
136 |
(* cf. 15.11.??? *) |
|
137 |
Nil "E\\<turnstile>[][\\<Colon>][]" |
|
138 |
||
139 |
(* cf. 15.11.??? *) |
|
140 |
Cons "\\<lbrakk>E\\<turnstile>e\\<Colon>T; |
|
141 |
E\\<turnstile>es[\\<Colon>]Ts\\<rbrakk> \\<Longrightarrow> |
|
142 |
E\\<turnstile>e#es[\\<Colon>]T#Ts" |
|
143 |
||
144 |
(* well-typed statements *) |
|
145 |
||
146 |
Skip "E\\<turnstile>Skip\\<surd>" |
|
147 |
||
148 |
Expr "\\<lbrakk>E\\<turnstile>e\\<Colon>T\\<rbrakk> \\<Longrightarrow> |
|
149 |
E\\<turnstile>Expr e\\<surd>" |
|
150 |
||
151 |
Comp "\\<lbrakk>E\\<turnstile>s1\\<surd>; |
|
152 |
E\\<turnstile>s2\\<surd>\\<rbrakk> \\<Longrightarrow> |
|
153 |
E\\<turnstile>s1;; s2\\<surd>" |
|
154 |
||
155 |
(* cf. 14.8 *) |
|
156 |
Cond "\\<lbrakk>E\\<turnstile>e\\<Colon>PrimT Boolean; |
|
157 |
E\\<turnstile>s1\\<surd>; |
|
158 |
E\\<turnstile>s2\\<surd>\\<rbrakk> \\<Longrightarrow> |
|
159 |
E\\<turnstile>If(e) s1 Else s2\\<surd>" |
|
160 |
||
161 |
(* cf. 14.10 *) |
|
162 |
Loop "\\<lbrakk>E\\<turnstile>e\\<Colon>PrimT Boolean; |
|
163 |
E\\<turnstile>s\\<surd>\\<rbrakk> \\<Longrightarrow> |
|
164 |
E\\<turnstile>While(e) s\\<surd>" |
|
165 |
||
166 |
constdefs |
|
167 |
||
8082 | 168 |
wf_java_mdecl :: java_mb prog => cname => java_mb mdecl => bool |
169 |
"wf_java_mdecl G C \\<equiv> \\<lambda>((mn,pTs),rT,(pns,lvars,blk,res)). |
|
8011 | 170 |
length pTs = length pns \\<and> |
171 |
nodups pns \\<and> |
|
172 |
unique lvars \\<and> |
|
173 |
(\\<forall>pn\\<in>set pns. map_of lvars pn = None) \\<and> |
|
8178 | 174 |
(\\<forall>(vn,T)\\<in>set lvars. is_type G T) & |
8011 | 175 |
(let E = (G,map_of lvars(pns[\\<mapsto>]pTs)(This\\<mapsto>Class C)) in |
176 |
E\\<turnstile>blk\\<surd> \\<and> (\\<exists>T. E\\<turnstile>res\\<Colon>T \\<and> G\\<turnstile>T\\<preceq>rT))" |
|
177 |
||
8082 | 178 |
wf_java_prog :: java_mb prog => bool |
179 |
"wf_java_prog G \\<equiv> wf_prog wf_java_mdecl G" |
|
8011 | 180 |
|
181 |
end |