annotate src/mono_reduce.sml @ 741:f7e2026dd5ae

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