annotate src/mono_reduce.sml @ 919:cc956020801b

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