adamc@133
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@133
|
2 * All rights reserved.
|
adamc@133
|
3 *
|
adamc@133
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@133
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@133
|
6 *
|
adamc@133
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@133
|
8 * this list of conditions and the following disclaimer.
|
adamc@133
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@133
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@133
|
11 * and/or other materials provided with the distribution.
|
adamc@133
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@133
|
13 * derived from this software without specific prior written permission.
|
adamc@133
|
14 *
|
adamc@133
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@133
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@133
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@133
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@133
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@133
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@133
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@133
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@133
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@133
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@133
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@133
|
26 *)
|
adamc@133
|
27
|
adamc@133
|
28 (* Simplify a Mono program algebraically *)
|
adamc@133
|
29
|
adamc@133
|
30 structure MonoReduce :> MONO_REDUCE = struct
|
adamc@133
|
31
|
adamc@133
|
32 open Mono
|
adamc@133
|
33
|
adamc@133
|
34 structure E = MonoEnv
|
adamc@133
|
35 structure U = MonoUtil
|
adamc@133
|
36
|
adamc@133
|
37
|
adamc@252
|
38 fun impure (e, _) =
|
adamc@252
|
39 case e of
|
adamc@252
|
40 EWrite _ => true
|
adamc@252
|
41 | EQuery _ => true
|
adamc@307
|
42 | EDml _ => true
|
adamc@338
|
43 | ENextval _ => true
|
adamc@252
|
44 | EAbs _ => false
|
adamc@252
|
45
|
adamc@252
|
46 | EPrim _ => false
|
adamc@252
|
47 | ERel _ => false
|
adamc@252
|
48 | ENamed _ => false
|
adamc@252
|
49 | ECon (_, _, eo) => (case eo of NONE => false | SOME e => impure e)
|
adamc@297
|
50 | ENone _ => false
|
adamc@290
|
51 | ESome (_, e) => impure e
|
adamc@252
|
52 | EFfi _ => false
|
adamc@252
|
53 | EFfiApp _ => false
|
adamc@252
|
54 | EApp ((EFfi _, _), _) => false
|
adamc@252
|
55 | EApp _ => true
|
adamc@252
|
56
|
adamc@252
|
57 | ERecord xes => List.exists (fn (_, e, _) => impure e) xes
|
adamc@252
|
58 | EField (e, _) => impure e
|
adamc@252
|
59
|
adamc@252
|
60 | ECase (e, pes, _) => impure e orelse List.exists (fn (_, e) => impure e) pes
|
adamc@252
|
61
|
adamc@283
|
62 | EError (e, _) => impure e
|
adamc@283
|
63
|
adamc@252
|
64 | EStrcat (e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
65
|
adamc@252
|
66 | ESeq (e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
67 | ELet (_, _, e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
68
|
adamc@252
|
69 | EClosure (_, es) => List.exists impure es
|
adamc@252
|
70
|
adamc@252
|
71
|
adamc@252
|
72 val liftExpInExp = Monoize.liftExpInExp
|
adamc@252
|
73
|
adamc@252
|
74 val subExpInExp' =
|
adamc@133
|
75 U.Exp.mapB {typ = fn t => t,
|
adamc@133
|
76 exp = fn (xn, rep) => fn e =>
|
adamc@133
|
77 case e of
|
adamc@133
|
78 ERel xn' =>
|
adamc@133
|
79 (case Int.compare (xn', xn) of
|
adamc@133
|
80 EQUAL => #1 rep
|
adamc@133
|
81 | GREATER=> ERel (xn' - 1)
|
adamc@133
|
82 | LESS => e)
|
adamc@133
|
83 | _ => e,
|
adamc@133
|
84 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
|
adamc@133
|
85 | (ctx, _) => ctx}
|
adamc@133
|
86
|
adamc@252
|
87 fun subExpInExp (n, e1) e2 =
|
adamc@252
|
88 let
|
adamc@252
|
89 val r = subExpInExp' (n, e1) e2
|
adamc@252
|
90 in
|
adamc@252
|
91 (*Print.prefaces "subExpInExp" [("e1", MonoPrint.p_exp MonoEnv.empty e1),
|
adamc@252
|
92 ("e2", MonoPrint.p_exp MonoEnv.empty e2),
|
adamc@252
|
93 ("r", MonoPrint.p_exp MonoEnv.empty r)];*)
|
adamc@252
|
94 r
|
adamc@252
|
95 end
|
adamc@133
|
96
|
adamc@133
|
97 fun typ c = c
|
adamc@133
|
98
|
adamc@316
|
99 val swapExpVars =
|
adamc@316
|
100 U.Exp.mapB {typ = fn t => t,
|
adamc@316
|
101 exp = fn lower => fn e =>
|
adamc@316
|
102 case e of
|
adamc@316
|
103 ERel xn =>
|
adamc@316
|
104 if xn = lower then
|
adamc@316
|
105 ERel (lower + 1)
|
adamc@316
|
106 else if xn = lower + 1 then
|
adamc@316
|
107 ERel lower
|
adamc@316
|
108 else
|
adamc@316
|
109 e
|
adamc@316
|
110 | _ => e,
|
adamc@316
|
111 bind = fn (lower, U.Exp.RelE _) => lower+1
|
adamc@316
|
112 | (lower, _) => lower}
|
adamc@316
|
113
|
adamc@258
|
114 datatype result = Yes of E.env | No | Maybe
|
adamc@258
|
115
|
adamc@183
|
116 fun match (env, p : pat, e : exp) =
|
adamc@183
|
117 case (#1 p, #1 e) of
|
adamc@258
|
118 (PWild, _) => Yes env
|
adamc@258
|
119 | (PVar (x, t), _) => Yes (E.pushERel env x t (SOME e))
|
adamc@183
|
120
|
adamc@280
|
121 | (PPrim (Prim.String s), EStrcat ((EPrim (Prim.String s'), _), _)) =>
|
adamc@280
|
122 if String.isPrefix s' s then
|
adamc@280
|
123 Maybe
|
adamc@280
|
124 else
|
adamc@280
|
125 No
|
adamc@280
|
126
|
adamc@183
|
127 | (PPrim p, EPrim p') =>
|
adamc@183
|
128 if Prim.equal (p, p') then
|
adamc@258
|
129 Yes env
|
adamc@183
|
130 else
|
adamc@258
|
131 No
|
adamc@183
|
132
|
adamc@188
|
133 | (PCon (_, PConVar n1, NONE), ECon (_, PConVar n2, NONE)) =>
|
adamc@183
|
134 if n1 = n2 then
|
adamc@258
|
135 Yes env
|
adamc@183
|
136 else
|
adamc@258
|
137 No
|
adamc@183
|
138
|
adamc@188
|
139 | (PCon (_, PConVar n1, SOME p), ECon (_, PConVar n2, SOME e)) =>
|
adamc@183
|
140 if n1 = n2 then
|
adamc@183
|
141 match (env, p, e)
|
adamc@183
|
142 else
|
adamc@258
|
143 No
|
adamc@183
|
144
|
adamc@188
|
145 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, NONE), ECon (_, PConFfi {mod = m2, con = con2, ...}, NONE)) =>
|
adamc@185
|
146 if m1 = m2 andalso con1 = con2 then
|
adamc@258
|
147 Yes env
|
adamc@185
|
148 else
|
adamc@258
|
149 No
|
adamc@185
|
150
|
adamc@188
|
151 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, SOME ep), ECon (_, PConFfi {mod = m2, con = con2, ...}, SOME e)) =>
|
adamc@185
|
152 if m1 = m2 andalso con1 = con2 then
|
adamc@185
|
153 match (env, p, e)
|
adamc@185
|
154 else
|
adamc@258
|
155 No
|
adamc@185
|
156
|
adamc@183
|
157 | (PRecord xps, ERecord xes) =>
|
adamc@183
|
158 let
|
adamc@183
|
159 fun consider (xps, env) =
|
adamc@183
|
160 case xps of
|
adamc@258
|
161 [] => Yes env
|
adamc@183
|
162 | (x, p, _) :: rest =>
|
adamc@183
|
163 case List.find (fn (x', _, _) => x' = x) xes of
|
adamc@258
|
164 NONE => No
|
adamc@183
|
165 | SOME (_, e, _) =>
|
adamc@183
|
166 case match (env, p, e) of
|
adamc@258
|
167 No => No
|
adamc@258
|
168 | Maybe => Maybe
|
adamc@258
|
169 | Yes env => consider (rest, env)
|
adamc@183
|
170 in
|
adamc@183
|
171 consider (xps, env)
|
adamc@183
|
172 end
|
adamc@183
|
173
|
adamc@258
|
174 | _ => Maybe
|
adamc@183
|
175
|
adamc@318
|
176 datatype event =
|
adamc@318
|
177 WritePage
|
adamc@318
|
178 | ReadDb
|
adamc@318
|
179 | WriteDb
|
adamc@318
|
180 | UseRel of int
|
adamc@318
|
181 | Unsure
|
adamc@318
|
182
|
adamc@318
|
183 fun p_event e =
|
adamc@318
|
184 let
|
adamc@318
|
185 open Print.PD
|
adamc@318
|
186 in
|
adamc@318
|
187 case e of
|
adamc@318
|
188 WritePage => string "WritePage"
|
adamc@318
|
189 | ReadDb => string "ReadDb"
|
adamc@318
|
190 | WriteDb => string "WriteDb"
|
adamc@318
|
191 | UseRel n => string ("UseRel" ^ Int.toString n)
|
adamc@318
|
192 | Unsure => string "Unsure"
|
adamc@318
|
193 end
|
adamc@318
|
194
|
adamc@318
|
195 fun patBinds (p, _) =
|
adamc@318
|
196 case p of
|
adamc@318
|
197 PWild => 0
|
adamc@318
|
198 | PVar _ => 1
|
adamc@318
|
199 | PPrim _ => 0
|
adamc@318
|
200 | PCon (_, _, NONE) => 0
|
adamc@318
|
201 | PCon (_, _, SOME p) => patBinds p
|
adamc@318
|
202 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
|
adamc@318
|
203 | PNone _ => 0
|
adamc@318
|
204 | PSome (_, p) => patBinds p
|
adamc@318
|
205
|
adamc@318
|
206 fun summarize d (e, _) =
|
adamc@318
|
207 case e of
|
adamc@318
|
208 EPrim _ => []
|
adamc@318
|
209 | ERel n => if n >= d then [UseRel (n - d)] else []
|
adamc@318
|
210 | ENamed _ => []
|
adamc@318
|
211 | ECon (_, _, NONE) => []
|
adamc@318
|
212 | ECon (_, _, SOME e) => summarize d e
|
adamc@318
|
213 | ENone _ => []
|
adamc@318
|
214 | ESome (_, e) => summarize d e
|
adamc@318
|
215 | EFfi _ => []
|
adamc@318
|
216 | EFfiApp (_, _, es) => List.concat (map (summarize d) es)
|
adamc@319
|
217 | EApp ((EFfi _, _), e) => summarize d e
|
adamc@318
|
218 | EApp _ => [Unsure]
|
adamc@318
|
219 | EAbs _ => []
|
adamc@318
|
220
|
adamc@318
|
221 | ERecord xets => List.concat (map (summarize d o #2) xets)
|
adamc@318
|
222 | EField (e, _) => summarize d e
|
adamc@318
|
223
|
adamc@318
|
224 | ECase (e, pes, _) =>
|
adamc@318
|
225 let
|
adamc@318
|
226 val lss = map (fn (p, e) => summarize (d + patBinds p) e) pes
|
adamc@318
|
227 in
|
adamc@318
|
228 case lss of
|
adamc@318
|
229 [] => raise Fail "Empty pattern match"
|
adamc@318
|
230 | ls :: lss =>
|
adamc@318
|
231 if List.all (fn ls' => ls' = ls) lss then
|
adamc@318
|
232 summarize d e @ ls
|
adamc@318
|
233 else
|
adamc@318
|
234 [Unsure]
|
adamc@318
|
235 end
|
adamc@318
|
236 | EStrcat (e1, e2) => summarize d e1 @ summarize d e2
|
adamc@318
|
237
|
adamc@318
|
238 | EError (e, _) => summarize d e @ [Unsure]
|
adamc@318
|
239
|
adamc@318
|
240 | EWrite e => summarize d e @ [WritePage]
|
adamc@318
|
241
|
adamc@318
|
242 | ESeq (e1, e2) => summarize d e1 @ summarize d e2
|
adamc@318
|
243 | ELet (_, _, e1, e2) => summarize d e1 @ summarize (d + 1) e2
|
adamc@318
|
244
|
adamc@318
|
245 | EClosure (_, es) => List.concat (map (summarize d) es)
|
adamc@318
|
246
|
adamc@318
|
247 | EQuery {query, body, initial, ...} =>
|
adamc@318
|
248 List.concat [summarize d query,
|
adamc@319
|
249 summarize (d + 2) body,
|
adamc@318
|
250 summarize d initial,
|
adamc@318
|
251 [ReadDb]]
|
adamc@318
|
252
|
adamc@318
|
253 | EDml e => summarize d e @ [WriteDb]
|
adamc@338
|
254 | ENextval e => summarize d e @ [WriteDb]
|
adamc@318
|
255
|
adamc@133
|
256 fun exp env e =
|
adamc@133
|
257 case e of
|
adamc@183
|
258 ERel n =>
|
adamc@183
|
259 (case E.lookupERel env n of
|
adamc@183
|
260 (_, _, SOME e') => #1 e'
|
adamc@183
|
261 | _ => e)
|
adamc@183
|
262 | ENamed n =>
|
adamc@133
|
263 (case E.lookupENamed env n of
|
adamc@133
|
264 (_, _, SOME e', _) => #1 e'
|
adamc@133
|
265 | _ => e)
|
adamc@133
|
266
|
adamc@252
|
267 | EApp ((EAbs (x, t, _, e1), loc), e2) =>
|
adamc@252
|
268 ((*Print.prefaces "Considering" [("e1", MonoPrint.p_exp env e1),
|
adamc@252
|
269 ("e2", MonoPrint.p_exp env e2)];*)
|
adamc@252
|
270 if impure e2 then
|
adamc@252
|
271 #1 (reduceExp env (ELet (x, t, e2, e1), loc))
|
adamc@252
|
272 else
|
adamc@252
|
273 #1 (reduceExp env (subExpInExp (0, e2) e1)))
|
adamc@133
|
274
|
adamc@184
|
275 | ECase (disc, pes, _) =>
|
adamc@258
|
276 let
|
adamc@258
|
277 fun search pes =
|
adamc@258
|
278 case pes of
|
adamc@258
|
279 [] => e
|
adamc@258
|
280 | (p, body) :: pes =>
|
adamc@258
|
281 case match (env, p, disc) of
|
adamc@258
|
282 No => search pes
|
adamc@258
|
283 | Maybe => e
|
adamc@258
|
284 | Yes env => #1 (reduceExp env body)
|
adamc@258
|
285 in
|
adamc@258
|
286 search pes
|
adamc@258
|
287 end
|
adamc@183
|
288
|
adamc@252
|
289 | EField ((ERecord xes, _), x) =>
|
adamc@252
|
290 (case List.find (fn (x', _, _) => x' = x) xes of
|
adamc@252
|
291 SOME (_, e, _) => #1 e
|
adamc@252
|
292 | NONE => e)
|
adamc@252
|
293
|
adamc@252
|
294 | ELet (x1, t1, (ELet (x2, t2, e1, b1), loc), b2) =>
|
adamc@252
|
295 let
|
adamc@252
|
296 val e' = (ELet (x2, t2, e1,
|
adamc@252
|
297 (ELet (x1, t1, b1,
|
adamc@252
|
298 liftExpInExp 1 b2), loc)), loc)
|
adamc@252
|
299 in
|
adamc@253
|
300 (*Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)),
|
adamc@253
|
301 ("e'", MonoPrint.p_exp env e')];*)
|
adamc@252
|
302 #1 (reduceExp env e')
|
adamc@252
|
303 end
|
adamc@252
|
304 | EApp ((ELet (x, t, e, b), loc), e') =>
|
adamc@252
|
305 #1 (reduceExp env (ELet (x, t, e,
|
adamc@252
|
306 (EApp (b, liftExpInExp 0 e'), loc)), loc))
|
adamc@316
|
307
|
adamc@316
|
308 | ELet (x, t, e, (EAbs (x', t' as (TRecord [], _), ran, e'), loc)) =>
|
adamc@316
|
309 EAbs (x', t', ran, (ELet (x, t, liftExpInExp 0 e, swapExpVars 0 e'), loc))
|
adamc@316
|
310
|
adamc@252
|
311 | ELet (x, t, e', b) =>
|
adamc@252
|
312 if impure e' then
|
adamc@318
|
313 let
|
adamc@318
|
314 val effs_e' = summarize 0 e'
|
adamc@318
|
315 val effs_b = summarize 0 b
|
adamc@318
|
316
|
adamc@318
|
317 fun does eff = List.exists (fn eff' => eff' = eff) effs_e'
|
adamc@318
|
318 val writesPage = does WritePage
|
adamc@318
|
319 val readsDb = does ReadDb
|
adamc@318
|
320 val writesDb = does WriteDb
|
adamc@318
|
321
|
adamc@318
|
322 fun verifyUnused eff =
|
adamc@318
|
323 case eff of
|
adamc@318
|
324 UseRel r => r <> 0
|
adamc@318
|
325 | Unsure => false
|
adamc@318
|
326 | _ => true
|
adamc@318
|
327
|
adamc@318
|
328 fun verifyCompatible effs =
|
adamc@318
|
329 case effs of
|
adamc@318
|
330 [] => false
|
adamc@318
|
331 | eff :: effs =>
|
adamc@318
|
332 case eff of
|
adamc@318
|
333 Unsure => false
|
adamc@318
|
334 | UseRel r =>
|
adamc@318
|
335 if r = 0 then
|
adamc@318
|
336 List.all verifyUnused effs
|
adamc@318
|
337 else
|
adamc@318
|
338 verifyCompatible effs
|
adamc@318
|
339 | WritePage => not writesPage andalso verifyCompatible effs
|
adamc@318
|
340 | ReadDb => not writesDb andalso verifyCompatible effs
|
adamc@318
|
341 | WriteDb => not writesDb andalso not readsDb andalso verifyCompatible effs
|
adamc@318
|
342 in
|
adamc@318
|
343 (*Print.prefaces "verifyCompatible"
|
adamc@318
|
344 [("e'", MonoPrint.p_exp env e'),
|
adamc@318
|
345 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
|
adamc@318
|
346 ("effs_e'", Print.p_list p_event effs_e'),
|
adamc@318
|
347 ("effs_b", Print.p_list p_event effs_b)];*)
|
adamc@318
|
348 if verifyCompatible effs_b then
|
adamc@318
|
349 #1 (reduceExp env (subExpInExp (0, e') b))
|
adamc@318
|
350 else
|
adamc@318
|
351 e
|
adamc@318
|
352 end
|
adamc@252
|
353 else
|
adamc@252
|
354 #1 (reduceExp env (subExpInExp (0, e') b))
|
adamc@252
|
355
|
adamc@268
|
356 | EStrcat ((EPrim (Prim.String s1), _), (EPrim (Prim.String s2), _)) =>
|
adamc@268
|
357 EPrim (Prim.String (s1 ^ s2))
|
adamc@268
|
358
|
adamc@133
|
359 | _ => e
|
adamc@133
|
360
|
adamc@252
|
361 and bind (env, b) =
|
adamc@252
|
362 case b of
|
adamc@252
|
363 U.Decl.Datatype (x, n, xncs) => E.pushDatatype env x n xncs
|
adamc@252
|
364 | U.Decl.RelE (x, t) => E.pushERel env x t NONE
|
adamc@252
|
365 | U.Decl.NamedE (x, n, t, eo, s) => E.pushENamed env x n t (Option.map (reduceExp env) eo) s
|
adamc@252
|
366
|
adamc@133
|
367 and reduceExp env = U.Exp.mapB {typ = typ, exp = exp, bind = bind} env
|
adamc@133
|
368
|
adamc@133
|
369 fun decl env d = d
|
adamc@133
|
370
|
adamc@133
|
371 val reduce = U.File.mapB {typ = typ, exp = exp, decl = decl, bind = bind} E.empty
|
adamc@133
|
372
|
adamc@133
|
373 end
|