annotate src/mono_reduce.sml @ 916:b873feb3eb52

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