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@916
|
38 structure IS = IntBinarySet
|
adamc@470
|
39
|
adamc@133
|
40
|
adamc@948
|
41 fun simpleTypeImpure tsyms =
|
adamc@919
|
42 U.Typ.exists (fn TFun _ => true
|
adamc@948
|
43 | TDatatype (n, _) => IS.member (tsyms, n)
|
adamc@919
|
44 | _ => false)
|
adamc@919
|
45
|
adamc@948
|
46 fun simpleImpure (tsyms, syms) =
|
adamc@919
|
47 U.Exp.existsB {typ = fn _ => false,
|
adamc@919
|
48 exp = fn (env, e) =>
|
adamc@919
|
49 case e of
|
adamc@919
|
50 EWrite _ => true
|
adamc@919
|
51 | EQuery _ => true
|
adamc@919
|
52 | EDml _ => true
|
adamc@919
|
53 | ENextval _ => true
|
adamc@919
|
54 | EFfiApp (m, x, _) => Settings.isEffectful (m, x)
|
adamc@919
|
55 | EServerCall _ => true
|
adamc@919
|
56 | ERecv _ => true
|
adamc@919
|
57 | ESleep _ => true
|
adamc@919
|
58 | ENamed n => IS.member (syms, n)
|
adamc@1069
|
59 | EError _ => true
|
adamc@919
|
60 | ERel n =>
|
adamc@919
|
61 let
|
adamc@919
|
62 val (_, t, _) = E.lookupERel env n
|
adamc@919
|
63 in
|
adamc@948
|
64 simpleTypeImpure tsyms t
|
adamc@919
|
65 end
|
adamc@919
|
66 | _ => false,
|
adamc@919
|
67 bind = fn (env, b) =>
|
adamc@919
|
68 case b of
|
adamc@919
|
69 U.Exp.RelE (x, t) => E.pushERel env x t NONE
|
adamc@919
|
70 | _ => env}
|
adamc@916
|
71
|
adamc@252
|
72 fun impure (e, _) =
|
adamc@252
|
73 case e of
|
adamc@252
|
74 EWrite _ => true
|
adamc@252
|
75 | EQuery _ => true
|
adamc@307
|
76 | EDml _ => true
|
adamc@338
|
77 | ENextval _ => true
|
adamc@463
|
78 | EUnurlify _ => true
|
adamc@252
|
79 | EAbs _ => false
|
adamc@252
|
80
|
adamc@252
|
81 | EPrim _ => false
|
adamc@252
|
82 | ERel _ => false
|
adamc@252
|
83 | ENamed _ => false
|
adamc@252
|
84 | ECon (_, _, eo) => (case eo of NONE => false | SOME e => impure e)
|
adamc@297
|
85 | ENone _ => false
|
adamc@290
|
86 | ESome (_, e) => impure e
|
adamc@252
|
87 | EFfi _ => false
|
adamc@765
|
88 | EFfiApp (m, x, _) => Settings.isEffectful (m, x)
|
adamc@252
|
89 | EApp ((EFfi _, _), _) => false
|
adamc@252
|
90 | EApp _ => true
|
adamc@252
|
91
|
adamc@387
|
92 | EUnop (_, e) => impure e
|
adamc@387
|
93 | EBinop (_, e1, e2) => impure e1 orelse impure e2
|
adamc@387
|
94
|
adamc@252
|
95 | ERecord xes => List.exists (fn (_, e, _) => impure e) xes
|
adamc@252
|
96 | EField (e, _) => impure e
|
adamc@252
|
97
|
adamc@252
|
98 | ECase (e, pes, _) => impure e orelse List.exists (fn (_, e) => impure e) pes
|
adamc@252
|
99
|
adamc@1069
|
100 | EError _ => true
|
adamc@741
|
101 | EReturnBlob {blob = e1, mimeType = e2, ...} => impure e1 orelse impure e2
|
adamc@1065
|
102 | ERedirect (e, _) => impure e
|
adamc@283
|
103
|
adamc@252
|
104 | EStrcat (e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
105
|
adamc@252
|
106 | ESeq (e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
107 | ELet (_, _, e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
108
|
adamc@252
|
109 | EClosure (_, es) => List.exists impure es
|
adamc@815
|
110 | EJavaScript (_, e) => impure e
|
adamc@568
|
111 | ESignalReturn e => impure e
|
adamc@572
|
112 | ESignalBind (e1, e2) => impure e1 orelse impure e2
|
adamc@574
|
113 | ESignalSource e => impure e
|
adamc@608
|
114 | EServerCall _ => true
|
adamc@670
|
115 | ERecv _ => true
|
adamc@695
|
116 | ESleep _ => true
|
adamc@1021
|
117 | ESpawn _ => true
|
adamc@252
|
118
|
adamc@252
|
119 val liftExpInExp = Monoize.liftExpInExp
|
adamc@252
|
120
|
adamc@829
|
121 fun multiLift n e =
|
adamc@829
|
122 case n of
|
adamc@829
|
123 0 => e
|
adamc@829
|
124 | _ => multiLift (n - 1) (liftExpInExp 0 e)
|
adamc@829
|
125
|
adamc@252
|
126 val subExpInExp' =
|
adamc@133
|
127 U.Exp.mapB {typ = fn t => t,
|
adamc@133
|
128 exp = fn (xn, rep) => fn e =>
|
adamc@133
|
129 case e of
|
adamc@133
|
130 ERel xn' =>
|
adamc@133
|
131 (case Int.compare (xn', xn) of
|
adamc@133
|
132 EQUAL => #1 rep
|
adamc@133
|
133 | GREATER=> ERel (xn' - 1)
|
adamc@133
|
134 | LESS => e)
|
adamc@133
|
135 | _ => e,
|
adamc@133
|
136 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
|
adamc@133
|
137 | (ctx, _) => ctx}
|
adamc@133
|
138
|
adamc@252
|
139 fun subExpInExp (n, e1) e2 =
|
adamc@252
|
140 let
|
adamc@252
|
141 val r = subExpInExp' (n, e1) e2
|
adamc@252
|
142 in
|
adamc@252
|
143 (*Print.prefaces "subExpInExp" [("e1", MonoPrint.p_exp MonoEnv.empty e1),
|
adamc@252
|
144 ("e2", MonoPrint.p_exp MonoEnv.empty e2),
|
adamc@252
|
145 ("r", MonoPrint.p_exp MonoEnv.empty r)];*)
|
adamc@252
|
146 r
|
adamc@252
|
147 end
|
adamc@133
|
148
|
adamc@133
|
149 fun typ c = c
|
adamc@133
|
150
|
adamc@316
|
151 val swapExpVars =
|
adamc@316
|
152 U.Exp.mapB {typ = fn t => t,
|
adamc@316
|
153 exp = fn lower => fn e =>
|
adamc@316
|
154 case e of
|
adamc@316
|
155 ERel xn =>
|
adamc@316
|
156 if xn = lower then
|
adamc@316
|
157 ERel (lower + 1)
|
adamc@316
|
158 else if xn = lower + 1 then
|
adamc@316
|
159 ERel lower
|
adamc@316
|
160 else
|
adamc@316
|
161 e
|
adamc@316
|
162 | _ => e,
|
adamc@316
|
163 bind = fn (lower, U.Exp.RelE _) => lower+1
|
adamc@316
|
164 | (lower, _) => lower}
|
adamc@316
|
165
|
adamc@341
|
166 val swapExpVarsPat =
|
adamc@341
|
167 U.Exp.mapB {typ = fn t => t,
|
adamc@341
|
168 exp = fn (lower, len) => fn e =>
|
adamc@341
|
169 case e of
|
adamc@341
|
170 ERel xn =>
|
adamc@341
|
171 if xn = lower then
|
adamc@827
|
172 ERel (lower + len)
|
adamc@341
|
173 else if xn >= lower + 1 andalso xn < lower + 1 + len then
|
adamc@341
|
174 ERel (xn - 1)
|
adamc@341
|
175 else
|
adamc@341
|
176 e
|
adamc@341
|
177 | _ => e,
|
adamc@341
|
178 bind = fn ((lower, len), U.Exp.RelE _) => (lower+1, len)
|
adamc@341
|
179 | (st, _) => st}
|
adamc@341
|
180
|
adamc@814
|
181 datatype result = Yes of exp list | No | Maybe
|
adamc@258
|
182
|
adamc@183
|
183 fun match (env, p : pat, e : exp) =
|
adamc@183
|
184 case (#1 p, #1 e) of
|
adamc@258
|
185 (PWild, _) => Yes env
|
adamc@814
|
186 | (PVar (x, t), _) => Yes (e :: env)
|
adamc@183
|
187
|
adamc@280
|
188 | (PPrim (Prim.String s), EStrcat ((EPrim (Prim.String s'), _), _)) =>
|
adamc@280
|
189 if String.isPrefix s' s then
|
adamc@280
|
190 Maybe
|
adamc@280
|
191 else
|
adamc@280
|
192 No
|
adamc@280
|
193
|
adamc@183
|
194 | (PPrim p, EPrim p') =>
|
adamc@183
|
195 if Prim.equal (p, p') then
|
adamc@258
|
196 Yes env
|
adamc@183
|
197 else
|
adamc@258
|
198 No
|
adamc@183
|
199
|
adamc@188
|
200 | (PCon (_, PConVar n1, NONE), ECon (_, PConVar n2, NONE)) =>
|
adamc@183
|
201 if n1 = n2 then
|
adamc@258
|
202 Yes env
|
adamc@183
|
203 else
|
adamc@258
|
204 No
|
adamc@183
|
205
|
adamc@188
|
206 | (PCon (_, PConVar n1, SOME p), ECon (_, PConVar n2, SOME e)) =>
|
adamc@183
|
207 if n1 = n2 then
|
adamc@183
|
208 match (env, p, e)
|
adamc@183
|
209 else
|
adamc@258
|
210 No
|
adamc@183
|
211
|
adamc@188
|
212 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, NONE), ECon (_, PConFfi {mod = m2, con = con2, ...}, NONE)) =>
|
adamc@185
|
213 if m1 = m2 andalso con1 = con2 then
|
adamc@258
|
214 Yes env
|
adamc@185
|
215 else
|
adamc@258
|
216 No
|
adamc@185
|
217
|
adamc@188
|
218 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, SOME ep), ECon (_, PConFfi {mod = m2, con = con2, ...}, SOME e)) =>
|
adamc@185
|
219 if m1 = m2 andalso con1 = con2 then
|
adamc@185
|
220 match (env, p, e)
|
adamc@185
|
221 else
|
adamc@258
|
222 No
|
adamc@185
|
223
|
adamc@183
|
224 | (PRecord xps, ERecord xes) =>
|
adamc@183
|
225 let
|
adamc@183
|
226 fun consider (xps, env) =
|
adamc@183
|
227 case xps of
|
adamc@258
|
228 [] => Yes env
|
adamc@183
|
229 | (x, p, _) :: rest =>
|
adamc@183
|
230 case List.find (fn (x', _, _) => x' = x) xes of
|
adamc@258
|
231 NONE => No
|
adamc@183
|
232 | SOME (_, e, _) =>
|
adamc@183
|
233 case match (env, p, e) of
|
adamc@258
|
234 No => No
|
adamc@258
|
235 | Maybe => Maybe
|
adamc@258
|
236 | Yes env => consider (rest, env)
|
adamc@183
|
237 in
|
adamc@183
|
238 consider (xps, env)
|
adamc@183
|
239 end
|
adamc@183
|
240
|
adamc@726
|
241 | (PNone _, ENone _) => Yes env
|
adamc@921
|
242 | (PNone _, ESome _) => No
|
adamc@726
|
243 | (PSome (_, p), ESome (_, e)) => match (env, p, e)
|
adamc@921
|
244 | (PSome _, ENone _) => No
|
adamc@726
|
245
|
adamc@258
|
246 | _ => Maybe
|
adamc@183
|
247
|
adamc@318
|
248 datatype event =
|
adamc@318
|
249 WritePage
|
adamc@318
|
250 | ReadDb
|
adamc@318
|
251 | WriteDb
|
adamc@481
|
252 | UseRel
|
adamc@318
|
253 | Unsure
|
adamc@318
|
254
|
adamc@318
|
255 fun p_event e =
|
adamc@318
|
256 let
|
adamc@318
|
257 open Print.PD
|
adamc@318
|
258 in
|
adamc@318
|
259 case e of
|
adamc@318
|
260 WritePage => string "WritePage"
|
adamc@318
|
261 | ReadDb => string "ReadDb"
|
adamc@318
|
262 | WriteDb => string "WriteDb"
|
adamc@481
|
263 | UseRel => string "UseRel"
|
adamc@318
|
264 | Unsure => string "Unsure"
|
adamc@318
|
265 end
|
adamc@318
|
266
|
adamc@470
|
267 val p_events = Print.p_list p_event
|
adamc@470
|
268
|
adamc@318
|
269 fun patBinds (p, _) =
|
adamc@318
|
270 case p of
|
adamc@318
|
271 PWild => 0
|
adamc@318
|
272 | PVar _ => 1
|
adamc@318
|
273 | PPrim _ => 0
|
adamc@318
|
274 | PCon (_, _, NONE) => 0
|
adamc@318
|
275 | PCon (_, _, SOME p) => patBinds p
|
adamc@318
|
276 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
|
adamc@318
|
277 | PNone _ => 0
|
adamc@318
|
278 | PSome (_, p) => patBinds p
|
adamc@318
|
279
|
adamc@932
|
280 val countFree = U.Exp.foldB {typ = fn (_, n) => n,
|
adamc@932
|
281 exp = fn (x, e, n) =>
|
adamc@932
|
282 case e of
|
adamc@932
|
283 ERel x' => if x = x' then n + 1 else n
|
adamc@932
|
284 | _ => n,
|
adamc@932
|
285 bind = fn (n, b) =>
|
adamc@932
|
286 case b of
|
adamc@932
|
287 U.Exp.RelE _ => n + 1
|
adamc@975
|
288 | _ => n}
|
adamc@975
|
289
|
adamc@975
|
290 val freeInAbs = U.Exp.existsB {typ = fn _ => false,
|
adamc@975
|
291 exp = fn (n, e) =>
|
adamc@975
|
292 case e of
|
adamc@975
|
293 EAbs (_, _, _, b) => countFree n 0 b > 0
|
adamc@975
|
294 | EJavaScript (_, b) => countFree n 0 b > 0
|
adamc@975
|
295 | _ => false,
|
adamc@975
|
296 bind = fn (n, b) =>
|
adamc@975
|
297 case b of
|
adamc@975
|
298 U.Exp.RelE _ => n + 1
|
adamc@975
|
299 | _ => n} 0
|
adamc@932
|
300
|
adamc@470
|
301 fun reduce file =
|
adamc@470
|
302 let
|
adamc@948
|
303 val (timpures, impures, absCounts) =
|
adamc@948
|
304 foldl (fn ((d, _), (timpures, impures, absCounts)) =>
|
adamc@916
|
305 let
|
adamc@916
|
306 fun countAbs (e, _) =
|
adamc@916
|
307 case e of
|
adamc@916
|
308 EAbs (_, _, _, e) => 1 + countAbs e
|
adamc@916
|
309 | _ => 0
|
adamc@916
|
310 in
|
adamc@916
|
311 case d of
|
adamc@948
|
312 DDatatype dts =>
|
adamc@948
|
313 (if List.exists (fn (_, _, cs) =>
|
adamc@948
|
314 List.exists (fn (_, _, NONE) => false
|
adamc@948
|
315 | (_, _, SOME t) => simpleTypeImpure timpures t) cs)
|
adamc@948
|
316 dts then
|
adamc@948
|
317 IS.addList (timpures, map #2 dts)
|
adamc@948
|
318 else
|
adamc@948
|
319 timpures,
|
adamc@948
|
320 impures,
|
adamc@948
|
321 absCounts)
|
adamc@948
|
322 | DVal (_, n, _, e, _) =>
|
adamc@948
|
323 (timpures,
|
adamc@948
|
324 if simpleImpure (timpures, impures) E.empty e then
|
adamc@916
|
325 IS.add (impures, n)
|
adamc@916
|
326 else
|
adamc@916
|
327 impures,
|
adamc@916
|
328 IM.insert (absCounts, n, countAbs e))
|
adamc@916
|
329 | DValRec vis =>
|
adamc@948
|
330 (timpures,
|
adamc@948
|
331 if List.exists (fn (_, _, _, e, _) => simpleImpure (timpures, impures) E.empty e) vis then
|
adamc@916
|
332 foldl (fn ((_, n, _, _, _), impures) =>
|
adamc@916
|
333 IS.add (impures, n)) impures vis
|
adamc@916
|
334 else
|
adamc@916
|
335 impures,
|
adamc@916
|
336 foldl (fn ((x, n, _, e, _), absCounts) =>
|
adamc@916
|
337 IM.insert (absCounts, n, countAbs e))
|
adamc@916
|
338 absCounts vis)
|
adamc@948
|
339 | _ => (timpures, impures, absCounts)
|
adamc@916
|
340 end)
|
adamc@948
|
341 (IS.empty, IS.empty, IM.empty) file
|
adamc@387
|
342
|
adamc@1017
|
343 val uses = U.File.fold {typ = fn (_, m) => m,
|
adamc@1017
|
344 exp = fn (e, m) =>
|
adamc@1017
|
345 case e of
|
adamc@1017
|
346 ENamed n => IM.insert (m, n, 1 + Option.getOpt (IM.find (m, n), 0))
|
adamc@1017
|
347 | _ => m,
|
adamc@1017
|
348 decl = fn (_, m) => m}
|
adamc@1017
|
349 IM.empty file
|
adamc@1017
|
350
|
adamc@1017
|
351 val size = U.Exp.fold {typ = fn (_, n) => n,
|
adamc@1017
|
352 exp = fn (_, n) => n + 1} 0
|
adamc@1017
|
353
|
adamc@1017
|
354 fun mayInline (n, e) =
|
adamc@1017
|
355 case IM.find (uses, n) of
|
adamc@1017
|
356 NONE => false
|
adamc@1017
|
357 | SOME count => count <= 1
|
adamc@1017
|
358 orelse size e <= Settings.getMonoInline ()
|
adamc@1017
|
359
|
adamc@470
|
360 fun summarize d (e, _) =
|
adamc@579
|
361 let
|
adamc@579
|
362 val s =
|
adamc@579
|
363 case e of
|
adamc@579
|
364 EPrim _ => []
|
adamc@579
|
365 | ERel n => if n = d then [UseRel] else []
|
adamc@579
|
366 | ENamed _ => []
|
adamc@579
|
367 | ECon (_, _, NONE) => []
|
adamc@579
|
368 | ECon (_, _, SOME e) => summarize d e
|
adamc@579
|
369 | ENone _ => []
|
adamc@579
|
370 | ESome (_, e) => summarize d e
|
adamc@579
|
371 | EFfi _ => []
|
adamc@765
|
372 | EFfiApp (m, x, es) =>
|
adamc@765
|
373 if Settings.isEffectful (m, x) then
|
adamc@765
|
374 List.concat (map (summarize d) es) @ [Unsure]
|
adamc@765
|
375 else
|
adamc@765
|
376 List.concat (map (summarize d) es)
|
adamc@579
|
377 | EApp ((EFfi _, _), e) => summarize d e
|
adamc@579
|
378 | EApp _ =>
|
adamc@579
|
379 let
|
adamc@694
|
380 fun unravel (e, passed, ls) =
|
adamc@579
|
381 case e of
|
adamc@579
|
382 ENamed n =>
|
adamc@579
|
383 let
|
adamc@579
|
384 val ls = rev ls
|
adamc@579
|
385 in
|
adamc@948
|
386 if IS.member (impures, n) then
|
adamc@948
|
387 case IM.find (absCounts, n) of
|
adamc@948
|
388 NONE => [Unsure]
|
adamc@948
|
389 | SOME len =>
|
adamc@948
|
390 if passed < len then
|
adamc@948
|
391 ls
|
adamc@948
|
392 else
|
adamc@948
|
393 ls @ [Unsure]
|
adamc@948
|
394 else
|
adamc@948
|
395 ls
|
adamc@579
|
396 end
|
adamc@579
|
397 | ERel n => List.revAppend (ls,
|
adamc@579
|
398 if n = d then
|
adamc@579
|
399 [UseRel, Unsure]
|
adamc@579
|
400 else
|
adamc@579
|
401 [Unsure])
|
adamc@579
|
402 | EApp (f, x) =>
|
adamc@995
|
403 unravel (#1 f, passed + 1, List.revAppend (summarize d x,
|
adamc@995
|
404 ls))
|
adamc@579
|
405 | _ => [Unsure]
|
adamc@579
|
406 in
|
adamc@694
|
407 unravel (e, 0, [])
|
adamc@579
|
408 end
|
adamc@252
|
409
|
adamc@941
|
410 | EAbs _ => []
|
adamc@316
|
411
|
adamc@579
|
412 | EUnop (_, e) => summarize d e
|
adamc@579
|
413 | EBinop (_, e1, e2) => summarize d e1 @ summarize d e2
|
adamc@470
|
414
|
adamc@579
|
415 | ERecord xets => List.concat (map (summarize d o #2) xets)
|
adamc@579
|
416 | EField (e, _) => summarize d e
|
adamc@470
|
417
|
adamc@941
|
418 | ECase (e, pes, _) => summarize d e @ [Unsure]
|
adamc@941
|
419 (*let
|
adamc@579
|
420 val lss = map (fn (p, e) => summarize (d + patBinds p) e) pes
|
adamc@579
|
421 in
|
adamc@579
|
422 case lss of
|
adamc@579
|
423 [] => raise Fail "Empty pattern match"
|
adamc@579
|
424 | ls :: lss =>
|
adamc@579
|
425 if List.all (fn ls' => ls' = ls) lss then
|
adamc@579
|
426 summarize d e @ ls
|
adamc@579
|
427 else
|
adamc@579
|
428 [Unsure]
|
adamc@941
|
429 end*)
|
adamc@579
|
430 | EStrcat (e1, e2) => summarize d e1 @ summarize d e2
|
adamc@316
|
431
|
adamc@579
|
432 | EError (e, _) => summarize d e @ [Unsure]
|
adamc@741
|
433 | EReturnBlob {blob = e1, mimeType = e2, ...} => summarize d e1 @ summarize d e2 @ [Unsure]
|
adamc@1065
|
434 | ERedirect (e, _) => summarize d e @ [Unsure]
|
adamc@318
|
435
|
adamc@579
|
436 | EWrite e => summarize d e @ [WritePage]
|
adamc@579
|
437
|
adamc@579
|
438 | ESeq (e1, e2) => summarize d e1 @ summarize d e2
|
adamc@579
|
439 | ELet (_, _, e1, e2) => summarize d e1 @ summarize (d + 1) e2
|
adamc@456
|
440
|
adamc@579
|
441 | EClosure (_, es) => List.concat (map (summarize d) es)
|
adamc@470
|
442
|
adamc@579
|
443 | EQuery {query, body, initial, ...} =>
|
adamc@579
|
444 List.concat [summarize d query,
|
adamc@579
|
445 summarize d initial,
|
adamc@941
|
446 [ReadDb],
|
adamc@941
|
447 summarize (d + 2) body]
|
adamc@470
|
448
|
adamc@579
|
449 | EDml e => summarize d e @ [WriteDb]
|
adamc@579
|
450 | ENextval e => summarize d e @ [WriteDb]
|
adamc@579
|
451 | EUnurlify (e, _) => summarize d e
|
adamc@815
|
452 | EJavaScript (_, e) => summarize d e
|
adamc@579
|
453 | ESignalReturn e => summarize d e
|
adamc@579
|
454 | ESignalBind (e1, e2) => summarize d e1 @ summarize d e2
|
adamc@579
|
455 | ESignalSource e => summarize d e
|
adamc@608
|
456
|
adamc@1020
|
457 | EServerCall (e, _, _) => summarize d e @ [Unsure]
|
adamc@1021
|
458 | ERecv (e, _) => summarize d e @ [Unsure]
|
adamc@1021
|
459 | ESleep e => summarize d e @ [Unsure]
|
adamc@1021
|
460 | ESpawn e => summarize d e @ [Unsure]
|
adamc@579
|
461 in
|
adamc@579
|
462 (*Print.prefaces "Summarize"
|
adamc@579
|
463 [("e", MonoPrint.p_exp MonoEnv.empty (e, ErrorMsg.dummySpan)),
|
adamc@579
|
464 ("d", Print.PD.string (Int.toString d)),
|
adamc@579
|
465 ("s", p_events s)];*)
|
adamc@579
|
466 s
|
adamc@579
|
467 end
|
adamc@470
|
468
|
adamc@919
|
469 val impure = fn env => fn e =>
|
adamc@948
|
470 simpleImpure (timpures, impures) env e andalso impure e
|
adamc@916
|
471 andalso not (List.null (summarize ~1 e))
|
adamc@916
|
472
|
adamc@470
|
473 fun exp env e =
|
adamc@470
|
474 let
|
adamc@470
|
475 (*val () = Print.prefaces "exp" [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan))]*)
|
adamc@470
|
476
|
adamc@1017
|
477 fun doLet (x, t, e', b) =
|
adamc@1017
|
478 let
|
adamc@1017
|
479 fun doSub () =
|
adamc@1017
|
480 let
|
adamc@1017
|
481 val r = subExpInExp (0, e') b
|
adamc@1017
|
482 in
|
adamc@1017
|
483 (*Print.prefaces "doSub" [("e'", MonoPrint.p_exp env e'),
|
adamc@1017
|
484 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
|
adamc@1017
|
485 ("r", MonoPrint.p_exp env r)];*)
|
adamc@1017
|
486 #1 (reduceExp env r)
|
adamc@1017
|
487 end
|
adamc@1017
|
488
|
adamc@1017
|
489 fun trySub () =
|
adamc@1017
|
490 ((*Print.prefaces "trySub"
|
adamc@1017
|
491 [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan))];*)
|
adamc@1017
|
492 case t of
|
adamc@1017
|
493 (TFfi ("Basis", "string"), _) => doSub ()
|
adamc@1017
|
494 | (TSignal _, _) => e
|
adamc@1017
|
495 | _ =>
|
adamc@1017
|
496 case e' of
|
adamc@1017
|
497 (ECase _, _) => e
|
adamc@1017
|
498 | _ => doSub ())
|
adamc@1017
|
499 in
|
adamc@1017
|
500 if impure env e' then
|
adamc@1017
|
501 let
|
adamc@1017
|
502 val effs_e' = summarize 0 e'
|
adamc@1017
|
503 val effs_e' = List.filter (fn x => x <> UseRel) effs_e'
|
adamc@1017
|
504 val effs_b = summarize 0 b
|
adamc@1017
|
505
|
adamc@1017
|
506 (*val () = Print.prefaces "Try"
|
adamc@1017
|
507 [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan)),
|
adamc@1017
|
508 ("e'", MonoPrint.p_exp env e'),
|
adamc@1017
|
509 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
|
adamc@1017
|
510 ("e'_eff", p_events effs_e'),
|
adamc@1017
|
511 ("b_eff", p_events effs_b)]*)
|
adamc@1017
|
512
|
adamc@1017
|
513 fun does eff = List.exists (fn eff' => eff' = eff) effs_e'
|
adamc@1017
|
514 val writesPage = does WritePage
|
adamc@1017
|
515 val readsDb = does ReadDb
|
adamc@1017
|
516 val writesDb = does WriteDb
|
adamc@1017
|
517
|
adamc@1017
|
518 fun verifyUnused eff =
|
adamc@1017
|
519 case eff of
|
adamc@1017
|
520 UseRel => false
|
adamc@1017
|
521 | _ => true
|
adamc@1017
|
522
|
adamc@1017
|
523 fun verifyCompatible effs =
|
adamc@1017
|
524 case effs of
|
adamc@1017
|
525 [] => false
|
adamc@1017
|
526 | eff :: effs =>
|
adamc@1017
|
527 case eff of
|
adamc@1017
|
528 Unsure => false
|
adamc@1017
|
529 | UseRel => List.all verifyUnused effs
|
adamc@1017
|
530 | WritePage => not writesPage andalso verifyCompatible effs
|
adamc@1017
|
531 | ReadDb => not writesDb andalso verifyCompatible effs
|
adamc@1017
|
532 | WriteDb => not writesDb andalso not readsDb andalso verifyCompatible effs
|
adamc@1017
|
533 in
|
adamc@1017
|
534 (*Print.prefaces "verifyCompatible"
|
adamc@1017
|
535 [("e'", MonoPrint.p_exp env e'),
|
adamc@1017
|
536 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
|
adamc@1017
|
537 ("effs_e'", Print.p_list p_event effs_e'),
|
adamc@1017
|
538 ("effs_b", Print.p_list p_event effs_b)];*)
|
adamc@1017
|
539 if (List.null effs_e'
|
adamc@1017
|
540 orelse (List.all (fn eff => eff <> Unsure) effs_e'
|
adamc@1017
|
541 andalso verifyCompatible effs_b)
|
adamc@1017
|
542 orelse (case effs_b of
|
adamc@1017
|
543 UseRel :: effs => List.all verifyUnused effs
|
adamc@1017
|
544 | _ => false))
|
adamc@1017
|
545 andalso countFree 0 0 b = 1
|
adamc@1017
|
546 andalso not (freeInAbs b) then
|
adamc@1017
|
547 trySub ()
|
adamc@1017
|
548 else
|
adamc@1017
|
549 e
|
adamc@1017
|
550 end
|
adamc@1017
|
551 else
|
adamc@1017
|
552 trySub ()
|
adamc@1017
|
553 end
|
adamc@1017
|
554
|
adamc@470
|
555 val r =
|
adamc@470
|
556 case e of
|
adamc@470
|
557 ERel n =>
|
adamc@470
|
558 (case E.lookupERel env n of
|
adamc@470
|
559 (_, _, SOME e') => #1 e'
|
adamc@470
|
560 | _ => e)
|
adamc@470
|
561 | ENamed n =>
|
adamc@470
|
562 (case E.lookupENamed env n of
|
adamc@470
|
563 (_, _, SOME e', _) => ((*Print.prefaces "Switch" [("n", Print.PD.string (Int.toString n)),
|
adamc@470
|
564 ("e'", MonoPrint.p_exp env e')];*)
|
adamc@470
|
565 #1 e')
|
adamc@470
|
566 | _ => e)
|
adamc@470
|
567
|
adamc@470
|
568 | EApp ((EAbs (x, t, _, e1), loc), e2) =>
|
adamc@470
|
569 ((*Print.prefaces "Considering" [("e1", MonoPrint.p_exp (E.pushERel env x t NONE) e1),
|
adamc@694
|
570 ("e2", MonoPrint.p_exp env e2),
|
adamc@694
|
571 ("sub", MonoPrint.p_exp env (reduceExp env (subExpInExp (0, e2) e1)))];*)
|
adamc@975
|
572 if impure env e2 orelse countFree 0 0 e1 > 1 then
|
adamc@470
|
573 #1 (reduceExp env (ELet (x, t, e2, e1), loc))
|
adamc@470
|
574 else
|
adamc@470
|
575 #1 (reduceExp env (subExpInExp (0, e2) e1)))
|
adamc@470
|
576
|
adamc@470
|
577 | ECase (e', pes, {disc, result}) =>
|
adamc@442
|
578 let
|
adamc@470
|
579 fun push () =
|
adamc@470
|
580 case result of
|
adamc@470
|
581 (TFun (dom, result), loc) =>
|
adamc@470
|
582 if List.all (fn (_, (EAbs _, _)) => true | _ => false) pes then
|
adamc@827
|
583 let
|
adamc@827
|
584 val r =
|
adamc@827
|
585 EAbs ("y", dom, result,
|
adamc@827
|
586 (ECase (liftExpInExp 0 e',
|
adamc@827
|
587 map (fn (p, (EAbs (_, _, _, e), _)) =>
|
adamc@827
|
588 (p, swapExpVarsPat (0, patBinds p) e)
|
adamc@827
|
589 | _ => raise Fail "MonoReduce ECase") pes,
|
adamc@827
|
590 {disc = disc, result = result}), loc))
|
adamc@827
|
591 in
|
adamc@827
|
592 (*Print.prefaces "Swapped"
|
adamc@827
|
593 [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan)),
|
adamc@827
|
594 ("r", MonoPrint.p_exp env (r, ErrorMsg.dummySpan))];*)
|
adamc@827
|
595 r
|
adamc@827
|
596 end
|
adamc@470
|
597 else
|
adamc@470
|
598 e
|
adamc@470
|
599 | _ => e
|
adamc@318
|
600
|
adamc@470
|
601 fun search pes =
|
adamc@470
|
602 case pes of
|
adamc@470
|
603 [] => push ()
|
adamc@470
|
604 | (p, body) :: pes =>
|
adamc@814
|
605 case match ([], p, e') of
|
adamc@470
|
606 No => search pes
|
adamc@470
|
607 | Maybe => push ()
|
adamc@814
|
608 | Yes subs =>
|
adamc@800
|
609 let
|
adamc@829
|
610 val (body, remaining) =
|
adamc@829
|
611 foldl (fn (e, (body, remaining)) =>
|
adamc@829
|
612 (subExpInExp (0, multiLift remaining e) body, remaining - 1))
|
adamc@829
|
613 (body, length subs - 1) subs
|
adamc@920
|
614 val r = reduceExp (E.patBinds env p) body
|
adamc@800
|
615 in
|
adamc@829
|
616 (*Print.preface ("subs", Print.p_list (MonoPrint.p_exp env) subs);*)
|
adamc@800
|
617 (*Print.prefaces "ECase"
|
adamc@829
|
618 [("old", MonoPrint.p_exp env body),
|
adamc@829
|
619 ("body", MonoPrint.p_exp env body),
|
adamc@800
|
620 ("r", MonoPrint.p_exp env r)];*)
|
adamc@800
|
621 #1 r
|
adamc@800
|
622 end
|
adamc@470
|
623 in
|
adamc@941
|
624 if impure env e' then
|
adamc@941
|
625 e
|
adamc@941
|
626 else
|
adamc@941
|
627 search pes
|
adamc@470
|
628 end
|
adamc@318
|
629
|
adamc@470
|
630 | EField ((ERecord xes, _), x) =>
|
adamc@470
|
631 (case List.find (fn (x', _, _) => x' = x) xes of
|
adamc@470
|
632 SOME (_, e, _) => #1 e
|
adamc@470
|
633 | NONE => e)
|
adamc@398
|
634
|
adamc@470
|
635 | ELet (x1, t1, (ELet (x2, t2, e1, b1), loc), b2) =>
|
adamc@470
|
636 let
|
adamc@470
|
637 val e' = (ELet (x2, t2, e1,
|
adamc@470
|
638 (ELet (x1, t1, b1,
|
adamc@470
|
639 liftExpInExp 1 b2), loc)), loc)
|
adamc@442
|
640 in
|
adamc@470
|
641 (*Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)),
|
adamc@470
|
642 ("e'", MonoPrint.p_exp env e')];*)
|
adamc@470
|
643 #1 (reduceExp env e')
|
adamc@470
|
644 end
|
adamc@470
|
645 | EApp ((ELet (x, t, e, b), loc), e') =>
|
adamc@470
|
646 #1 (reduceExp env (ELet (x, t, e,
|
adamc@470
|
647 (EApp (b, liftExpInExp 0 e'), loc)), loc))
|
adamc@470
|
648
|
adamc@1017
|
649 | ELet (x, t, e', b as (EAbs (x', t' as (TRecord [], _), ran, e''), loc)) =>
|
adamc@919
|
650 if impure env e' then
|
adamc@1017
|
651 doLet (x, t, e', b)
|
adamc@848
|
652 else
|
adamc@920
|
653 EAbs (x', t', ran, reduceExp (E.pushERel env x' t' NONE)
|
adamc@920
|
654 (ELet (x, t, liftExpInExp 0 e', swapExpVars 0 e''), loc))
|
adamc@470
|
655
|
adamc@1017
|
656 | ELet (x, t, e', b) => doLet (x, t, e', b)
|
adamc@252
|
657
|
adamc@470
|
658 | EStrcat ((EPrim (Prim.String s1), _), (EPrim (Prim.String s2), _)) =>
|
adamc@470
|
659 EPrim (Prim.String (s1 ^ s2))
|
adamc@268
|
660
|
adamc@572
|
661 | ESignalBind ((ESignalReturn e1, loc), e2) =>
|
adamc@572
|
662 #1 (reduceExp env (EApp (e2, e1), loc))
|
adamc@572
|
663
|
adamc@470
|
664 | _ => e
|
adamc@470
|
665 in
|
adamc@829
|
666 (*Print.prefaces "exp'" [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan)),
|
adamc@829
|
667 ("r", MonoPrint.p_exp env (r, ErrorMsg.dummySpan))];*)
|
adamc@470
|
668 r
|
adamc@470
|
669 end
|
adamc@470
|
670
|
adamc@470
|
671 and bind (env, b) =
|
adamc@470
|
672 case b of
|
adamc@470
|
673 U.Decl.Datatype (x, n, xncs) => E.pushDatatype env x n xncs
|
adamc@470
|
674 | U.Decl.RelE (x, t) => E.pushERel env x t NONE
|
adamc@1017
|
675 | U.Decl.NamedE (x, n, t, eo, s) =>
|
adamc@1017
|
676 let
|
adamc@1017
|
677 val eo = case eo of
|
adamc@1017
|
678 NONE => NONE
|
adamc@1017
|
679 | SOME e => if mayInline (n, e) then
|
adamc@1017
|
680 SOME e
|
adamc@1017
|
681 else
|
adamc@1017
|
682 NONE
|
adamc@1017
|
683 in
|
adamc@1017
|
684 E.pushENamed env x n t (Option.map (reduceExp env) eo) s
|
adamc@1017
|
685 end
|
adamc@470
|
686
|
adamc@470
|
687 and reduceExp env = U.Exp.mapB {typ = typ, exp = exp, bind = bind} env
|
adamc@470
|
688
|
adamc@801
|
689 fun decl env d = ((*Print.preface ("d", MonoPrint.p_decl env (d, ErrorMsg.dummySpan));*)
|
adamc@801
|
690 d)
|
adamc@398
|
691 in
|
adamc@470
|
692 U.File.mapB {typ = typ, exp = exp, decl = decl, bind = bind} E.empty file
|
adamc@398
|
693 end
|
adamc@133
|
694
|
adamc@133
|
695 end
|