13673
|
1 |
(* Title: HOL/MicroJava/Comp/TranslCompTp.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Martin Strecker
|
|
4 |
*)
|
|
5 |
|
|
6 |
theory TranslCompTp = JVMType + Index:
|
|
7 |
|
|
8 |
|
|
9 |
(**********************************************************************)
|
|
10 |
|
|
11 |
|
|
12 |
constdefs
|
|
13 |
comb :: "['a \<Rightarrow> 'b list \<times> 'c, 'c \<Rightarrow> 'b list \<times> 'd, 'a] \<Rightarrow> 'b list \<times> 'd"
|
|
14 |
"comb == (\<lambda> f1 f2 x0. let (xs1, x1) = f1 x0;
|
|
15 |
(xs2, x2) = f2 x1
|
|
16 |
in (xs1 @ xs2, x2))"
|
|
17 |
comb_nil :: "'a \<Rightarrow> 'b list \<times> 'a"
|
|
18 |
"comb_nil a == ([], a)"
|
|
19 |
|
|
20 |
syntax (xsymbols)
|
|
21 |
"comb" :: "['a \<Rightarrow> 'b list \<times> 'c, 'c \<Rightarrow> 'b list \<times> 'd, 'a] \<Rightarrow> 'b list \<times> 'd"
|
|
22 |
(infixr "\<box>" 55)
|
|
23 |
|
|
24 |
lemma comb_nil_left [simp]: "comb_nil \<box> f = f"
|
|
25 |
by (simp add: comb_def comb_nil_def split_beta)
|
|
26 |
|
|
27 |
lemma comb_nil_right [simp]: "f \<box> comb_nil = f"
|
|
28 |
by (simp add: comb_def comb_nil_def split_beta)
|
|
29 |
|
|
30 |
lemma comb_assoc [simp]: "(fa \<box> fb) \<box> fc = fa \<box> (fb \<box> fc)"
|
|
31 |
by (simp add: comb_def split_beta)
|
|
32 |
|
|
33 |
lemma comb_inv: "(xs', x') = (f1 \<box> f2) x0 \<Longrightarrow>
|
|
34 |
\<exists> xs1 x1 xs2 x2. (xs1, x1) = (f1 x0) \<and> (xs2, x2) = f2 x1 \<and> xs'= xs1 @ xs2 \<and> x'=x2"
|
|
35 |
apply (case_tac "f1 x0")
|
|
36 |
apply (case_tac "f2 x1")
|
|
37 |
apply (simp add: comb_def split_beta)
|
|
38 |
done
|
|
39 |
|
|
40 |
(**********************************************************************)
|
|
41 |
|
|
42 |
syntax
|
|
43 |
mt_of :: "method_type \<times> state_type \<Rightarrow> method_type"
|
|
44 |
sttp_of :: "method_type \<times> state_type \<Rightarrow> state_type"
|
|
45 |
|
|
46 |
translations
|
|
47 |
"mt_of" => "fst"
|
|
48 |
"sttp_of" => "snd"
|
|
49 |
|
|
50 |
consts
|
|
51 |
compTpExpr :: "java_mb \<Rightarrow> java_mb prog \<Rightarrow> expr
|
|
52 |
\<Rightarrow> state_type \<Rightarrow> method_type \<times> state_type"
|
|
53 |
|
|
54 |
compTpExprs :: "java_mb \<Rightarrow> java_mb prog \<Rightarrow> expr list
|
|
55 |
\<Rightarrow> state_type \<Rightarrow> method_type \<times> state_type"
|
|
56 |
|
|
57 |
compTpStmt :: "java_mb \<Rightarrow> java_mb prog \<Rightarrow> stmt
|
|
58 |
\<Rightarrow> state_type \<Rightarrow> method_type \<times> state_type"
|
|
59 |
|
|
60 |
|
|
61 |
constdefs
|
|
62 |
nochangeST :: "state_type \<Rightarrow> method_type \<times> state_type"
|
|
63 |
"nochangeST sttp == ([Some sttp], sttp)"
|
|
64 |
pushST :: "[ty list, state_type] \<Rightarrow> method_type \<times> state_type"
|
|
65 |
"pushST tps == (\<lambda> (ST, LT). ([Some (ST, LT)], (tps @ ST, LT)))"
|
|
66 |
dupST :: "state_type \<Rightarrow> method_type \<times> state_type"
|
|
67 |
"dupST == (\<lambda> (ST, LT). ([Some (ST, LT)], (hd ST # ST, LT)))"
|
|
68 |
dup_x1ST :: "state_type \<Rightarrow> method_type \<times> state_type"
|
|
69 |
"dup_x1ST == (\<lambda> (ST, LT). ([Some (ST, LT)],
|
|
70 |
(hd ST # hd (tl ST) # hd ST # (tl (tl ST)), LT)))"
|
|
71 |
popST :: "[nat, state_type] \<Rightarrow> method_type \<times> state_type"
|
|
72 |
"popST n == (\<lambda> (ST, LT). ([Some (ST, LT)], (drop n ST, LT)))"
|
|
73 |
replST :: "[nat, ty, state_type] \<Rightarrow> method_type \<times> state_type"
|
|
74 |
"replST n tp == (\<lambda> (ST, LT). ([Some (ST, LT)], (tp # (drop n ST), LT)))"
|
|
75 |
|
|
76 |
storeST :: "[nat, ty, state_type] \<Rightarrow> method_type \<times> state_type"
|
|
77 |
"storeST i tp == (\<lambda> (ST, LT). ([Some (ST, LT)], (tl ST, LT [i:= OK tp])))"
|
|
78 |
|
|
79 |
|
|
80 |
(* Expressions *)
|
|
81 |
|
|
82 |
primrec
|
|
83 |
|
|
84 |
"compTpExpr jmb G (NewC c) = pushST [Class c]"
|
|
85 |
|
|
86 |
"compTpExpr jmb G (Cast c e) =
|
|
87 |
(compTpExpr jmb G e) \<box> (replST 1 (Class c))"
|
|
88 |
|
|
89 |
"compTpExpr jmb G (Lit val) = pushST [the (typeof (\<lambda>v. None) val)]"
|
|
90 |
|
|
91 |
"compTpExpr jmb G (BinOp bo e1 e2) =
|
|
92 |
(compTpExpr jmb G e1) \<box> (compTpExpr jmb G e2) \<box>
|
|
93 |
(case bo of
|
|
94 |
Eq => popST 2 \<box> pushST [PrimT Boolean] \<box> popST 1 \<box> pushST [PrimT Boolean]
|
|
95 |
| Add => replST 2 (PrimT Integer))"
|
|
96 |
|
|
97 |
"compTpExpr jmb G (LAcc vn) = (\<lambda> (ST, LT).
|
|
98 |
pushST [ok_val (LT ! (index jmb vn))] (ST, LT))"
|
|
99 |
|
|
100 |
"compTpExpr jmb G (vn::=e) =
|
|
101 |
(compTpExpr jmb G e) \<box> dupST \<box> (popST 1)"
|
|
102 |
|
|
103 |
|
|
104 |
"compTpExpr jmb G ( {cn}e..fn ) =
|
|
105 |
(compTpExpr jmb G e) \<box> replST 1 (snd (the (field (G,cn) fn)))"
|
|
106 |
|
|
107 |
"compTpExpr jmb G (FAss cn e1 fn e2 ) =
|
|
108 |
(compTpExpr jmb G e1) \<box> (compTpExpr jmb G e2) \<box> dup_x1ST \<box> (popST 2)"
|
|
109 |
|
|
110 |
|
|
111 |
"compTpExpr jmb G ({C}a..mn({fpTs}ps)) =
|
|
112 |
(compTpExpr jmb G a) \<box> (compTpExprs jmb G ps) \<box>
|
|
113 |
(replST ((length ps) + 1) (method_rT (the (method (G,C) (mn,fpTs)))))"
|
|
114 |
|
|
115 |
|
|
116 |
(* Expression lists *)
|
|
117 |
|
|
118 |
"compTpExprs jmb G [] = comb_nil"
|
|
119 |
|
|
120 |
"compTpExprs jmb G (e#es) = (compTpExpr jmb G e) \<box> (compTpExprs jmb G es)"
|
|
121 |
|
|
122 |
|
|
123 |
(* Statements *)
|
|
124 |
primrec
|
|
125 |
"compTpStmt jmb G Skip = comb_nil"
|
|
126 |
|
|
127 |
"compTpStmt jmb G (Expr e) = (compTpExpr jmb G e) \<box> popST 1"
|
|
128 |
|
|
129 |
"compTpStmt jmb G (c1;; c2) = (compTpStmt jmb G c1) \<box> (compTpStmt jmb G c2)"
|
|
130 |
|
|
131 |
"compTpStmt jmb G (If(e) c1 Else c2) =
|
|
132 |
(pushST [PrimT Boolean]) \<box> (compTpExpr jmb G e) \<box> popST 2 \<box>
|
|
133 |
(compTpStmt jmb G c1) \<box> nochangeST \<box> (compTpStmt jmb G c2)"
|
|
134 |
|
|
135 |
"compTpStmt jmb G (While(e) c) =
|
|
136 |
(pushST [PrimT Boolean]) \<box> (compTpExpr jmb G e) \<box> popST 2 \<box>
|
|
137 |
(compTpStmt jmb G c) \<box> nochangeST"
|
|
138 |
|
|
139 |
constdefs
|
|
140 |
compTpInit :: "java_mb \<Rightarrow> (vname * ty)
|
|
141 |
\<Rightarrow> state_type \<Rightarrow> method_type \<times> state_type"
|
|
142 |
"compTpInit jmb == (\<lambda> (vn,ty). (pushST [ty]) \<box> (storeST (index jmb vn) ty))"
|
|
143 |
|
|
144 |
consts
|
|
145 |
compTpInitLvars :: "[java_mb, (vname \<times> ty) list]
|
|
146 |
\<Rightarrow> state_type \<Rightarrow> method_type \<times> state_type"
|
|
147 |
|
|
148 |
primrec
|
|
149 |
"compTpInitLvars jmb [] = comb_nil"
|
|
150 |
"compTpInitLvars jmb (lv#lvars) = (compTpInit jmb lv) \<box> (compTpInitLvars jmb lvars)"
|
|
151 |
|
|
152 |
constdefs
|
|
153 |
start_ST :: "opstack_type"
|
|
154 |
"start_ST == []"
|
|
155 |
|
|
156 |
start_LT :: "cname \<Rightarrow> ty list \<Rightarrow> nat \<Rightarrow> locvars_type"
|
|
157 |
"start_LT C pTs n == (OK (Class C))#((map OK pTs))@(replicate n Err)"
|
|
158 |
|
|
159 |
compTpMethod :: "[java_mb prog, cname, java_mb mdecl] \<Rightarrow> method_type"
|
|
160 |
"compTpMethod G C == \<lambda> ((mn,pTs),rT, jmb).
|
|
161 |
let (pns,lvars,blk,res) = jmb
|
|
162 |
in (mt_of
|
|
163 |
((compTpInitLvars jmb lvars \<box>
|
|
164 |
compTpStmt jmb G blk \<box>
|
|
165 |
compTpExpr jmb G res \<box>
|
|
166 |
nochangeST)
|
|
167 |
(start_ST, start_LT C pTs (length lvars))))"
|
|
168 |
|
|
169 |
compTp :: "java_mb prog => prog_type"
|
|
170 |
"compTp G C sig == let (D, rT, jmb) = (the (method (G, C) sig))
|
|
171 |
in compTpMethod G C (sig, rT, jmb)"
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
(**********************************************************************)
|
|
176 |
(* Computing the maximum stack size from the method_type *)
|
|
177 |
|
|
178 |
constdefs
|
|
179 |
ssize_sto :: "(state_type option) \<Rightarrow> nat"
|
|
180 |
"ssize_sto sto == case sto of None \<Rightarrow> 0 | (Some (ST, LT)) \<Rightarrow> length ST"
|
|
181 |
|
|
182 |
max_of_list :: "nat list \<Rightarrow> nat"
|
|
183 |
"max_of_list xs == foldr max xs 0"
|
|
184 |
|
|
185 |
max_ssize :: "method_type \<Rightarrow> nat"
|
|
186 |
"max_ssize mt == max_of_list (map ssize_sto mt)"
|
|
187 |
|
|
188 |
|
|
189 |
end
|
|
190 |
|