annotate src/mono_reduce.sml @ 817:4585f744574a

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