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