annotate src/mono_reduce.sml @ 989:0bdc4d538f1c

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