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@470
|
37 structure IM = IntBinaryMap
|
adamc@470
|
38
|
adamc@133
|
39
|
adamc@252
|
40 fun impure (e, _) =
|
adamc@252
|
41 case e of
|
adamc@252
|
42 EWrite _ => true
|
adamc@252
|
43 | EQuery _ => true
|
adamc@307
|
44 | EDml _ => true
|
adamc@338
|
45 | ENextval _ => true
|
adamc@463
|
46 | EUnurlify _ => true
|
adamc@252
|
47 | EAbs _ => false
|
adamc@252
|
48
|
adamc@252
|
49 | EPrim _ => false
|
adamc@252
|
50 | ERel _ => false
|
adamc@252
|
51 | ENamed _ => false
|
adamc@252
|
52 | ECon (_, _, eo) => (case eo of NONE => false | SOME e => impure e)
|
adamc@297
|
53 | ENone _ => false
|
adamc@290
|
54 | ESome (_, e) => impure e
|
adamc@252
|
55 | EFfi _ => false
|
adamc@462
|
56 | EFfiApp ("Basis", "set_cookie", _) => true
|
adamc@565
|
57 | EFfiApp ("Basis", "new_client_source", _) => true
|
adamc@577
|
58 | EFfiApp ("Basis", "set_client_source", _) => true
|
adamc@579
|
59 | EFfiApp ("Basis", "alert", _) => true
|
adamc@668
|
60 | EFfiApp ("Basis", "new_channel", _) => true
|
adamc@668
|
61 | EFfiApp ("Basis", "subscribe", _) => true
|
adamc@668
|
62 | EFfiApp ("Basis", "send", _) => true
|
adamc@252
|
63 | EFfiApp _ => false
|
adamc@252
|
64 | EApp ((EFfi _, _), _) => false
|
adamc@252
|
65 | EApp _ => true
|
adamc@252
|
66
|
adamc@387
|
67 | EUnop (_, e) => impure e
|
adamc@387
|
68 | EBinop (_, e1, e2) => impure e1 orelse impure e2
|
adamc@387
|
69
|
adamc@252
|
70 | ERecord xes => List.exists (fn (_, e, _) => impure e) xes
|
adamc@252
|
71 | EField (e, _) => impure e
|
adamc@252
|
72
|
adamc@252
|
73 | ECase (e, pes, _) => impure e orelse List.exists (fn (_, e) => impure e) pes
|
adamc@252
|
74
|
adamc@283
|
75 | EError (e, _) => impure e
|
adamc@283
|
76
|
adamc@252
|
77 | EStrcat (e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
78
|
adamc@252
|
79 | ESeq (e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
80 | ELet (_, _, e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
81
|
adamc@252
|
82 | EClosure (_, es) => List.exists impure es
|
adamc@578
|
83 | EJavaScript (_, e, _) => impure e
|
adamc@568
|
84 | ESignalReturn e => impure e
|
adamc@572
|
85 | ESignalBind (e1, e2) => impure e1 orelse impure e2
|
adamc@574
|
86 | ESignalSource e => impure e
|
adamc@608
|
87 | EServerCall _ => true
|
adamc@252
|
88
|
adamc@252
|
89
|
adamc@252
|
90 val liftExpInExp = Monoize.liftExpInExp
|
adamc@252
|
91
|
adamc@252
|
92 val subExpInExp' =
|
adamc@133
|
93 U.Exp.mapB {typ = fn t => t,
|
adamc@133
|
94 exp = fn (xn, rep) => fn e =>
|
adamc@133
|
95 case e of
|
adamc@133
|
96 ERel xn' =>
|
adamc@133
|
97 (case Int.compare (xn', xn) of
|
adamc@133
|
98 EQUAL => #1 rep
|
adamc@133
|
99 | GREATER=> ERel (xn' - 1)
|
adamc@133
|
100 | LESS => e)
|
adamc@133
|
101 | _ => e,
|
adamc@133
|
102 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
|
adamc@133
|
103 | (ctx, _) => ctx}
|
adamc@133
|
104
|
adamc@252
|
105 fun subExpInExp (n, e1) e2 =
|
adamc@252
|
106 let
|
adamc@252
|
107 val r = subExpInExp' (n, e1) e2
|
adamc@252
|
108 in
|
adamc@252
|
109 (*Print.prefaces "subExpInExp" [("e1", MonoPrint.p_exp MonoEnv.empty e1),
|
adamc@252
|
110 ("e2", MonoPrint.p_exp MonoEnv.empty e2),
|
adamc@252
|
111 ("r", MonoPrint.p_exp MonoEnv.empty r)];*)
|
adamc@252
|
112 r
|
adamc@252
|
113 end
|
adamc@133
|
114
|
adamc@133
|
115 fun typ c = c
|
adamc@133
|
116
|
adamc@316
|
117 val swapExpVars =
|
adamc@316
|
118 U.Exp.mapB {typ = fn t => t,
|
adamc@316
|
119 exp = fn lower => fn e =>
|
adamc@316
|
120 case e of
|
adamc@316
|
121 ERel xn =>
|
adamc@316
|
122 if xn = lower then
|
adamc@316
|
123 ERel (lower + 1)
|
adamc@316
|
124 else if xn = lower + 1 then
|
adamc@316
|
125 ERel lower
|
adamc@316
|
126 else
|
adamc@316
|
127 e
|
adamc@316
|
128 | _ => e,
|
adamc@316
|
129 bind = fn (lower, U.Exp.RelE _) => lower+1
|
adamc@316
|
130 | (lower, _) => lower}
|
adamc@316
|
131
|
adamc@341
|
132 val swapExpVarsPat =
|
adamc@341
|
133 U.Exp.mapB {typ = fn t => t,
|
adamc@341
|
134 exp = fn (lower, len) => fn e =>
|
adamc@341
|
135 case e of
|
adamc@341
|
136 ERel xn =>
|
adamc@341
|
137 if xn = lower then
|
adamc@341
|
138 ERel (lower + 1)
|
adamc@341
|
139 else if xn >= lower + 1 andalso xn < lower + 1 + len then
|
adamc@341
|
140 ERel (xn - 1)
|
adamc@341
|
141 else
|
adamc@341
|
142 e
|
adamc@341
|
143 | _ => e,
|
adamc@341
|
144 bind = fn ((lower, len), U.Exp.RelE _) => (lower+1, len)
|
adamc@341
|
145 | (st, _) => st}
|
adamc@341
|
146
|
adamc@258
|
147 datatype result = Yes of E.env | No | Maybe
|
adamc@258
|
148
|
adamc@183
|
149 fun match (env, p : pat, e : exp) =
|
adamc@183
|
150 case (#1 p, #1 e) of
|
adamc@258
|
151 (PWild, _) => Yes env
|
adamc@258
|
152 | (PVar (x, t), _) => Yes (E.pushERel env x t (SOME e))
|
adamc@183
|
153
|
adamc@280
|
154 | (PPrim (Prim.String s), EStrcat ((EPrim (Prim.String s'), _), _)) =>
|
adamc@280
|
155 if String.isPrefix s' s then
|
adamc@280
|
156 Maybe
|
adamc@280
|
157 else
|
adamc@280
|
158 No
|
adamc@280
|
159
|
adamc@183
|
160 | (PPrim p, EPrim p') =>
|
adamc@183
|
161 if Prim.equal (p, p') then
|
adamc@258
|
162 Yes env
|
adamc@183
|
163 else
|
adamc@258
|
164 No
|
adamc@183
|
165
|
adamc@188
|
166 | (PCon (_, PConVar n1, NONE), ECon (_, PConVar n2, NONE)) =>
|
adamc@183
|
167 if n1 = n2 then
|
adamc@258
|
168 Yes env
|
adamc@183
|
169 else
|
adamc@258
|
170 No
|
adamc@183
|
171
|
adamc@188
|
172 | (PCon (_, PConVar n1, SOME p), ECon (_, PConVar n2, SOME e)) =>
|
adamc@183
|
173 if n1 = n2 then
|
adamc@183
|
174 match (env, p, e)
|
adamc@183
|
175 else
|
adamc@258
|
176 No
|
adamc@183
|
177
|
adamc@188
|
178 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, NONE), ECon (_, PConFfi {mod = m2, con = con2, ...}, NONE)) =>
|
adamc@185
|
179 if m1 = m2 andalso con1 = con2 then
|
adamc@258
|
180 Yes env
|
adamc@185
|
181 else
|
adamc@258
|
182 No
|
adamc@185
|
183
|
adamc@188
|
184 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, SOME ep), ECon (_, PConFfi {mod = m2, con = con2, ...}, SOME e)) =>
|
adamc@185
|
185 if m1 = m2 andalso con1 = con2 then
|
adamc@185
|
186 match (env, p, e)
|
adamc@185
|
187 else
|
adamc@258
|
188 No
|
adamc@185
|
189
|
adamc@183
|
190 | (PRecord xps, ERecord xes) =>
|
adamc@183
|
191 let
|
adamc@183
|
192 fun consider (xps, env) =
|
adamc@183
|
193 case xps of
|
adamc@258
|
194 [] => Yes env
|
adamc@183
|
195 | (x, p, _) :: rest =>
|
adamc@183
|
196 case List.find (fn (x', _, _) => x' = x) xes of
|
adamc@258
|
197 NONE => No
|
adamc@183
|
198 | SOME (_, e, _) =>
|
adamc@183
|
199 case match (env, p, e) of
|
adamc@258
|
200 No => No
|
adamc@258
|
201 | Maybe => Maybe
|
adamc@258
|
202 | Yes env => consider (rest, env)
|
adamc@183
|
203 in
|
adamc@183
|
204 consider (xps, env)
|
adamc@183
|
205 end
|
adamc@183
|
206
|
adamc@258
|
207 | _ => Maybe
|
adamc@183
|
208
|
adamc@318
|
209 datatype event =
|
adamc@318
|
210 WritePage
|
adamc@318
|
211 | ReadDb
|
adamc@318
|
212 | WriteDb
|
adamc@481
|
213 | UseRel
|
adamc@318
|
214 | Unsure
|
adamc@318
|
215
|
adamc@318
|
216 fun p_event e =
|
adamc@318
|
217 let
|
adamc@318
|
218 open Print.PD
|
adamc@318
|
219 in
|
adamc@318
|
220 case e of
|
adamc@318
|
221 WritePage => string "WritePage"
|
adamc@318
|
222 | ReadDb => string "ReadDb"
|
adamc@318
|
223 | WriteDb => string "WriteDb"
|
adamc@481
|
224 | UseRel => string "UseRel"
|
adamc@318
|
225 | Unsure => string "Unsure"
|
adamc@318
|
226 end
|
adamc@318
|
227
|
adamc@470
|
228 val p_events = Print.p_list p_event
|
adamc@470
|
229
|
adamc@318
|
230 fun patBinds (p, _) =
|
adamc@318
|
231 case p of
|
adamc@318
|
232 PWild => 0
|
adamc@318
|
233 | PVar _ => 1
|
adamc@318
|
234 | PPrim _ => 0
|
adamc@318
|
235 | PCon (_, _, NONE) => 0
|
adamc@318
|
236 | PCon (_, _, SOME p) => patBinds p
|
adamc@318
|
237 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
|
adamc@318
|
238 | PNone _ => 0
|
adamc@318
|
239 | PSome (_, p) => patBinds p
|
adamc@318
|
240
|
adamc@470
|
241 fun reduce file =
|
adamc@470
|
242 let
|
adamc@470
|
243 fun countAbs (e, _) =
|
adamc@470
|
244 case e of
|
adamc@470
|
245 EAbs (_, _, _, e) => 1 + countAbs e
|
adamc@470
|
246 | _ => 0
|
adamc@318
|
247
|
adamc@470
|
248 val absCounts =
|
adamc@470
|
249 foldl (fn ((d, _), absCounts) =>
|
adamc@470
|
250 case d of
|
adamc@470
|
251 DVal (_, n, _, e, _) =>
|
adamc@470
|
252 IM.insert (absCounts, n, countAbs e)
|
adamc@470
|
253 | DValRec vis =>
|
adamc@470
|
254 foldl (fn ((_, n, _, e, _), absCounts) =>
|
adamc@470
|
255 IM.insert (absCounts, n, countAbs e))
|
adamc@470
|
256 absCounts vis
|
adamc@470
|
257 | _ => absCounts)
|
adamc@470
|
258 IM.empty file
|
adamc@387
|
259
|
adamc@470
|
260 fun summarize d (e, _) =
|
adamc@579
|
261 let
|
adamc@668
|
262 fun ffi es = List.concat (map (summarize d) es) @ [Unsure]
|
adamc@668
|
263
|
adamc@579
|
264 val s =
|
adamc@579
|
265 case e of
|
adamc@579
|
266 EPrim _ => []
|
adamc@579
|
267 | ERel n => if n = d then [UseRel] else []
|
adamc@579
|
268 | ENamed _ => []
|
adamc@579
|
269 | ECon (_, _, NONE) => []
|
adamc@579
|
270 | ECon (_, _, SOME e) => summarize d e
|
adamc@579
|
271 | ENone _ => []
|
adamc@579
|
272 | ESome (_, e) => summarize d e
|
adamc@579
|
273 | EFfi _ => []
|
adamc@668
|
274 | EFfiApp ("Basis", "set_cookie", es) => ffi es
|
adamc@668
|
275 | EFfiApp ("Basis", "new_client_source", es) => ffi es
|
adamc@668
|
276 | EFfiApp ("Basis", "set_client_source", es) => ffi es
|
adamc@668
|
277 | EFfiApp ("Basis", "alert", es) => ffi es
|
adamc@668
|
278 | EFfiApp ("Basis", "new_channel", es) => ffi es
|
adamc@668
|
279 | EFfiApp ("Basis", "subscribe", es) => ffi es
|
adamc@668
|
280 | EFfiApp ("Basis", "send", es) => ffi es
|
adamc@579
|
281 | EFfiApp (_, _, es) => List.concat (map (summarize d) es)
|
adamc@579
|
282 | EApp ((EFfi _, _), e) => summarize d e
|
adamc@579
|
283 | EApp _ =>
|
adamc@579
|
284 let
|
adamc@579
|
285 fun unravel (e, ls) =
|
adamc@579
|
286 case e of
|
adamc@579
|
287 ENamed n =>
|
adamc@579
|
288 let
|
adamc@579
|
289 val ls = rev ls
|
adamc@579
|
290 in
|
adamc@579
|
291 case IM.find (absCounts, n) of
|
adamc@579
|
292 NONE => [Unsure]
|
adamc@579
|
293 | SOME len =>
|
adamc@579
|
294 if length ls < len then
|
adamc@579
|
295 ls
|
adamc@579
|
296 else
|
adamc@579
|
297 [Unsure]
|
adamc@579
|
298 end
|
adamc@579
|
299 | ERel n => List.revAppend (ls,
|
adamc@579
|
300 if n = d then
|
adamc@579
|
301 [UseRel, Unsure]
|
adamc@579
|
302 else
|
adamc@579
|
303 [Unsure])
|
adamc@579
|
304 | EApp (f, x) =>
|
adamc@579
|
305 unravel (#1 f, summarize d x @ ls)
|
adamc@579
|
306 | _ => [Unsure]
|
adamc@579
|
307 in
|
adamc@579
|
308 unravel (e, [])
|
adamc@579
|
309 end
|
adamc@252
|
310
|
adamc@579
|
311 | EAbs (_, _, _, e) => List.filter (fn UseRel => true
|
adamc@579
|
312 | _ => false) (summarize (d + 1) e)
|
adamc@316
|
313
|
adamc@579
|
314 | EUnop (_, e) => summarize d e
|
adamc@579
|
315 | EBinop (_, e1, e2) => summarize d e1 @ summarize d e2
|
adamc@470
|
316
|
adamc@579
|
317 | ERecord xets => List.concat (map (summarize d o #2) xets)
|
adamc@579
|
318 | EField (e, _) => summarize d e
|
adamc@470
|
319
|
adamc@579
|
320 | ECase (e, pes, _) =>
|
adamc@579
|
321 let
|
adamc@579
|
322 val lss = map (fn (p, e) => summarize (d + patBinds p) e) pes
|
adamc@579
|
323 in
|
adamc@579
|
324 case lss of
|
adamc@579
|
325 [] => raise Fail "Empty pattern match"
|
adamc@579
|
326 | ls :: lss =>
|
adamc@579
|
327 if List.all (fn ls' => ls' = ls) lss then
|
adamc@579
|
328 summarize d e @ ls
|
adamc@579
|
329 else
|
adamc@579
|
330 [Unsure]
|
adamc@579
|
331 end
|
adamc@579
|
332 | EStrcat (e1, e2) => summarize d e1 @ summarize d e2
|
adamc@316
|
333
|
adamc@579
|
334 | EError (e, _) => summarize d e @ [Unsure]
|
adamc@318
|
335
|
adamc@579
|
336 | EWrite e => summarize d e @ [WritePage]
|
adamc@579
|
337
|
adamc@579
|
338 | ESeq (e1, e2) => summarize d e1 @ summarize d e2
|
adamc@579
|
339 | ELet (_, _, e1, e2) => summarize d e1 @ summarize (d + 1) e2
|
adamc@456
|
340
|
adamc@579
|
341 | EClosure (_, es) => List.concat (map (summarize d) es)
|
adamc@470
|
342
|
adamc@579
|
343 | EQuery {query, body, initial, ...} =>
|
adamc@579
|
344 List.concat [summarize d query,
|
adamc@579
|
345 summarize (d + 2) body,
|
adamc@579
|
346 summarize d initial,
|
adamc@579
|
347 [ReadDb]]
|
adamc@470
|
348
|
adamc@579
|
349 | EDml e => summarize d e @ [WriteDb]
|
adamc@579
|
350 | ENextval e => summarize d e @ [WriteDb]
|
adamc@579
|
351 | EUnurlify (e, _) => summarize d e
|
adamc@579
|
352 | EJavaScript (_, e, _) => summarize d e
|
adamc@579
|
353 | ESignalReturn e => summarize d e
|
adamc@579
|
354 | ESignalBind (e1, e2) => summarize d e1 @ summarize d e2
|
adamc@579
|
355 | ESignalSource e => summarize d e
|
adamc@608
|
356
|
adamc@614
|
357 | EServerCall (e, ek, _) => summarize d e @ summarize d ek @ [Unsure]
|
adamc@579
|
358 in
|
adamc@579
|
359 (*Print.prefaces "Summarize"
|
adamc@579
|
360 [("e", MonoPrint.p_exp MonoEnv.empty (e, ErrorMsg.dummySpan)),
|
adamc@579
|
361 ("d", Print.PD.string (Int.toString d)),
|
adamc@579
|
362 ("s", p_events s)];*)
|
adamc@579
|
363 s
|
adamc@579
|
364 end
|
adamc@470
|
365
|
adamc@470
|
366 fun exp env e =
|
adamc@470
|
367 let
|
adamc@470
|
368 (*val () = Print.prefaces "exp" [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan))]*)
|
adamc@470
|
369
|
adamc@470
|
370 val r =
|
adamc@470
|
371 case e of
|
adamc@470
|
372 ERel n =>
|
adamc@470
|
373 (case E.lookupERel env n of
|
adamc@470
|
374 (_, _, SOME e') => #1 e'
|
adamc@470
|
375 | _ => e)
|
adamc@470
|
376 | ENamed n =>
|
adamc@470
|
377 (case E.lookupENamed env n of
|
adamc@470
|
378 (_, _, SOME e', _) => ((*Print.prefaces "Switch" [("n", Print.PD.string (Int.toString n)),
|
adamc@470
|
379 ("e'", MonoPrint.p_exp env e')];*)
|
adamc@470
|
380 #1 e')
|
adamc@470
|
381 | _ => e)
|
adamc@470
|
382
|
adamc@470
|
383 | EApp ((EAbs (x, t, _, e1), loc), e2) =>
|
adamc@470
|
384 ((*Print.prefaces "Considering" [("e1", MonoPrint.p_exp (E.pushERel env x t NONE) e1),
|
adamc@470
|
385 ("e2", MonoPrint.p_exp env e2),
|
adamc@470
|
386 ("sub", MonoPrint.p_exp env (reduceExp env (subExpInExp (0, e2) e1)))];*)
|
adamc@470
|
387 if impure e2 then
|
adamc@470
|
388 #1 (reduceExp env (ELet (x, t, e2, e1), loc))
|
adamc@470
|
389 else
|
adamc@470
|
390 #1 (reduceExp env (subExpInExp (0, e2) e1)))
|
adamc@470
|
391
|
adamc@470
|
392 | ECase (e', pes, {disc, result}) =>
|
adamc@442
|
393 let
|
adamc@470
|
394 fun push () =
|
adamc@470
|
395 case result of
|
adamc@470
|
396 (TFun (dom, result), loc) =>
|
adamc@470
|
397 if List.all (fn (_, (EAbs _, _)) => true | _ => false) pes then
|
adamc@470
|
398 EAbs ("_", dom, result,
|
adamc@470
|
399 (ECase (liftExpInExp 0 e',
|
adamc@470
|
400 map (fn (p, (EAbs (_, _, _, e), _)) =>
|
adamc@470
|
401 (p, swapExpVarsPat (0, patBinds p) e)
|
adamc@470
|
402 | _ => raise Fail "MonoReduce ECase") pes,
|
adamc@470
|
403 {disc = disc, result = result}), loc))
|
adamc@470
|
404 else
|
adamc@470
|
405 e
|
adamc@470
|
406 | _ => e
|
adamc@318
|
407
|
adamc@470
|
408 fun search pes =
|
adamc@470
|
409 case pes of
|
adamc@470
|
410 [] => push ()
|
adamc@470
|
411 | (p, body) :: pes =>
|
adamc@470
|
412 case match (env, p, e') of
|
adamc@470
|
413 No => search pes
|
adamc@470
|
414 | Maybe => push ()
|
adamc@470
|
415 | Yes env => #1 (reduceExp env body)
|
adamc@470
|
416 in
|
adamc@470
|
417 search pes
|
adamc@470
|
418 end
|
adamc@318
|
419
|
adamc@470
|
420 | EField ((ERecord xes, _), x) =>
|
adamc@470
|
421 (case List.find (fn (x', _, _) => x' = x) xes of
|
adamc@470
|
422 SOME (_, e, _) => #1 e
|
adamc@470
|
423 | NONE => e)
|
adamc@398
|
424
|
adamc@470
|
425 | ELet (x1, t1, (ELet (x2, t2, e1, b1), loc), b2) =>
|
adamc@470
|
426 let
|
adamc@470
|
427 val e' = (ELet (x2, t2, e1,
|
adamc@470
|
428 (ELet (x1, t1, b1,
|
adamc@470
|
429 liftExpInExp 1 b2), loc)), loc)
|
adamc@442
|
430 in
|
adamc@470
|
431 (*Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)),
|
adamc@470
|
432 ("e'", MonoPrint.p_exp env e')];*)
|
adamc@470
|
433 #1 (reduceExp env e')
|
adamc@470
|
434 end
|
adamc@470
|
435 | EApp ((ELet (x, t, e, b), loc), e') =>
|
adamc@470
|
436 #1 (reduceExp env (ELet (x, t, e,
|
adamc@470
|
437 (EApp (b, liftExpInExp 0 e'), loc)), loc))
|
adamc@470
|
438
|
adamc@470
|
439 | ELet (x, t, e', (EAbs (x', t' as (TRecord [], _), ran, e''), loc)) =>
|
adamc@470
|
440 (*if impure e' then
|
adamc@470
|
441 e
|
adamc@470
|
442 else*)
|
adamc@470
|
443 (* Seems unsound in general without the check... should revisit later *)
|
adamc@470
|
444 EAbs (x', t', ran, (ELet (x, t, liftExpInExp 0 e', swapExpVars 0 e''), loc))
|
adamc@470
|
445
|
adamc@470
|
446 | ELet (x, t, e', b) =>
|
adamc@470
|
447 let
|
adamc@470
|
448 fun doSub () =
|
adamc@470
|
449 #1 (reduceExp env (subExpInExp (0, e') b))
|
adamc@470
|
450
|
adamc@470
|
451 fun trySub () =
|
adamc@470
|
452 case t of
|
adamc@470
|
453 (TFfi ("Basis", "string"), _) => doSub ()
|
adamc@568
|
454 | (TSignal _, _) => e
|
adamc@470
|
455 | _ =>
|
adamc@470
|
456 case e' of
|
adamc@470
|
457 (ECase _, _) => e
|
adamc@470
|
458 | _ => doSub ()
|
adamc@470
|
459 in
|
adamc@470
|
460 if impure e' then
|
adamc@470
|
461 let
|
adamc@470
|
462 val effs_e' = summarize 0 e'
|
adamc@489
|
463 val effs_e' = List.filter (fn x => x <> UseRel) effs_e'
|
adamc@470
|
464 val effs_b = summarize 0 b
|
adamc@470
|
465
|
adamc@470
|
466 (*val () = Print.prefaces "Try"
|
adamc@489
|
467 [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan)),
|
adamc@489
|
468 ("e'", p_events effs_e'),
|
adamc@489
|
469 ("b", p_events effs_b)]*)
|
adamc@470
|
470
|
adamc@470
|
471 fun does eff = List.exists (fn eff' => eff' = eff) effs_e'
|
adamc@470
|
472 val writesPage = does WritePage
|
adamc@470
|
473 val readsDb = does ReadDb
|
adamc@470
|
474 val writesDb = does WriteDb
|
adamc@470
|
475
|
adamc@470
|
476 fun verifyUnused eff =
|
adamc@470
|
477 case eff of
|
adamc@481
|
478 UseRel => false
|
adamc@470
|
479 | _ => true
|
adamc@470
|
480
|
adamc@470
|
481 fun verifyCompatible effs =
|
adamc@470
|
482 case effs of
|
adamc@470
|
483 [] => false
|
adamc@470
|
484 | eff :: effs =>
|
adamc@470
|
485 case eff of
|
adamc@470
|
486 Unsure => false
|
adamc@481
|
487 | UseRel => List.all verifyUnused effs
|
adamc@470
|
488 | WritePage => not writesPage andalso verifyCompatible effs
|
adamc@470
|
489 | ReadDb => not writesDb andalso verifyCompatible effs
|
adamc@470
|
490 | WriteDb => not writesDb andalso not readsDb andalso verifyCompatible effs
|
adamc@470
|
491 in
|
adamc@470
|
492 (*Print.prefaces "verifyCompatible"
|
adamc@601
|
493 [("e'", MonoPrint.p_exp env e'),
|
adamc@601
|
494 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
|
adamc@601
|
495 ("effs_e'", Print.p_list p_event effs_e'),
|
adamc@601
|
496 ("effs_b", Print.p_list p_event effs_b)];*)
|
adamc@601
|
497 if List.null effs_e' orelse (List.all (fn eff => eff <> Unsure) effs_e'
|
adamc@601
|
498 andalso verifyCompatible effs_b) then
|
adamc@470
|
499 trySub ()
|
adamc@470
|
500 else
|
adamc@470
|
501 e
|
adamc@470
|
502 end
|
adamc@470
|
503 else
|
adamc@442
|
504 trySub ()
|
adamc@442
|
505 end
|
adamc@252
|
506
|
adamc@470
|
507 | EStrcat ((EPrim (Prim.String s1), _), (EPrim (Prim.String s2), _)) =>
|
adamc@470
|
508 EPrim (Prim.String (s1 ^ s2))
|
adamc@268
|
509
|
adamc@572
|
510 | ESignalBind ((ESignalReturn e1, loc), e2) =>
|
adamc@572
|
511 #1 (reduceExp env (EApp (e2, e1), loc))
|
adamc@572
|
512
|
adamc@470
|
513 | _ => e
|
adamc@470
|
514 in
|
adamc@470
|
515 (*Print.prefaces "exp'" [("r", MonoPrint.p_exp env (r, ErrorMsg.dummySpan))];*)
|
adamc@470
|
516 r
|
adamc@470
|
517 end
|
adamc@470
|
518
|
adamc@470
|
519 and bind (env, b) =
|
adamc@470
|
520 case b of
|
adamc@470
|
521 U.Decl.Datatype (x, n, xncs) => E.pushDatatype env x n xncs
|
adamc@470
|
522 | U.Decl.RelE (x, t) => E.pushERel env x t NONE
|
adamc@470
|
523 | U.Decl.NamedE (x, n, t, eo, s) => E.pushENamed env x n t (Option.map (reduceExp env) eo) s
|
adamc@470
|
524
|
adamc@470
|
525 and reduceExp env = U.Exp.mapB {typ = typ, exp = exp, bind = bind} env
|
adamc@470
|
526
|
adamc@470
|
527 fun decl env d = d
|
adamc@398
|
528 in
|
adamc@470
|
529 U.File.mapB {typ = typ, exp = exp, decl = decl, bind = bind} E.empty file
|
adamc@398
|
530 end
|
adamc@133
|
531
|
adamc@133
|
532 end
|