annotate src/mono_reduce.sml @ 1971:1e360ea06a09

Add basic year/month/day/hour/minute/second <-> time functions.
author Patrick Hurst <phurst@mit.edu>
date Thu, 05 Dec 2013 11:36:06 -0500
parents 0480b8f29a47
children 98895243b5b6
rev   line source
adam@1848 1 (* Copyright (c) 2008, 2013, 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
adam@1852 34 val fullMode = ref false
adam@1852 35
adamc@133 36 structure E = MonoEnv
adamc@133 37 structure U = MonoUtil
adamc@133 38
adamc@470 39 structure IM = IntBinaryMap
adamc@916 40 structure IS = IntBinarySet
adamc@470 41
adamc@133 42
adamc@948 43 fun simpleTypeImpure tsyms =
adamc@919 44 U.Typ.exists (fn TFun _ => true
adamc@948 45 | TDatatype (n, _) => IS.member (tsyms, n)
adamc@919 46 | _ => false)
adamc@919 47
adamc@948 48 fun simpleImpure (tsyms, syms) =
adamc@919 49 U.Exp.existsB {typ = fn _ => false,
adamc@919 50 exp = fn (env, e) =>
adamc@919 51 case e of
adamc@919 52 EWrite _ => true
adamc@919 53 | EQuery _ => true
adamc@919 54 | EDml _ => true
adamc@919 55 | ENextval _ => true
adamc@1073 56 | ESetval _ => true
adamc@1171 57 | EFfiApp (m, x, _) => Settings.isEffectful (m, x) orelse Settings.isBenignEffectful (m, x)
adamc@919 58 | EServerCall _ => true
adamc@919 59 | ERecv _ => true
adamc@919 60 | ESleep _ => true
adamc@919 61 | ENamed n => IS.member (syms, n)
adamc@919 62 | ERel n =>
adamc@919 63 let
adamc@919 64 val (_, t, _) = E.lookupERel env n
adamc@919 65 in
adamc@948 66 simpleTypeImpure tsyms t
adamc@919 67 end
adamc@919 68 | _ => false,
adamc@919 69 bind = fn (env, b) =>
adamc@919 70 case b of
adamc@919 71 U.Exp.RelE (x, t) => E.pushERel env x t NONE
adamc@919 72 | _ => env}
adamc@916 73
adamc@252 74 fun impure (e, _) =
adamc@252 75 case e of
adamc@252 76 EWrite _ => true
adamc@252 77 | EQuery _ => true
adamc@307 78 | EDml _ => true
adamc@338 79 | ENextval _ => true
adamc@1073 80 | ESetval _ => true
adam@1423 81 | EUnurlify (e, _, _) => impure e
adamc@252 82 | EAbs _ => false
adamc@252 83
adamc@252 84 | EPrim _ => false
adamc@252 85 | ERel _ => false
adamc@252 86 | ENamed _ => false
adamc@252 87 | ECon (_, _, eo) => (case eo of NONE => false | SOME e => impure e)
adamc@297 88 | ENone _ => false
adamc@290 89 | ESome (_, e) => impure e
adamc@252 90 | EFfi _ => false
adamc@1171 91 | EFfiApp (m, x, _) => Settings.isEffectful (m, x) orelse Settings.isBenignEffectful (m, x)
adamc@252 92 | EApp ((EFfi _, _), _) => false
adamc@252 93 | EApp _ => true
adamc@252 94
adamc@387 95 | EUnop (_, e) => impure e
adam@1360 96 | EBinop (_, _, e1, e2) => impure e1 orelse impure e2
adamc@387 97
adamc@252 98 | ERecord xes => List.exists (fn (_, e, _) => impure e) xes
adamc@252 99 | EField (e, _) => impure e
adamc@252 100
adamc@252 101 | ECase (e, pes, _) => impure e orelse List.exists (fn (_, e) => impure e) pes
adamc@252 102
adamc@1069 103 | EError _ => true
adamc@741 104 | EReturnBlob {blob = e1, mimeType = e2, ...} => impure e1 orelse impure e2
adamc@1065 105 | ERedirect (e, _) => impure e
adamc@283 106
adamc@252 107 | EStrcat (e1, e2) => impure e1 orelse impure e2
adamc@252 108
adamc@252 109 | ESeq (e1, e2) => impure e1 orelse impure e2
adamc@252 110 | ELet (_, _, e1, e2) => impure e1 orelse impure e2
adamc@252 111
adamc@252 112 | EClosure (_, es) => List.exists impure es
adamc@815 113 | EJavaScript (_, e) => impure e
adamc@568 114 | ESignalReturn e => impure e
adamc@572 115 | ESignalBind (e1, e2) => impure e1 orelse impure e2
adamc@574 116 | ESignalSource e => impure e
adamc@608 117 | EServerCall _ => true
adamc@670 118 | ERecv _ => true
adamc@695 119 | ESleep _ => true
adamc@1021 120 | ESpawn _ => true
adamc@252 121
adamc@252 122 val liftExpInExp = Monoize.liftExpInExp
adamc@252 123
adamc@829 124 fun multiLift n e =
adamc@829 125 case n of
adamc@829 126 0 => e
adamc@829 127 | _ => multiLift (n - 1) (liftExpInExp 0 e)
adamc@829 128
adamc@252 129 val subExpInExp' =
adamc@133 130 U.Exp.mapB {typ = fn t => t,
adamc@133 131 exp = fn (xn, rep) => fn e =>
adamc@133 132 case e of
adamc@133 133 ERel xn' =>
adamc@133 134 (case Int.compare (xn', xn) of
adamc@133 135 EQUAL => #1 rep
adamc@133 136 | GREATER=> ERel (xn' - 1)
adamc@133 137 | LESS => e)
adamc@133 138 | _ => e,
adamc@133 139 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
adamc@133 140 | (ctx, _) => ctx}
adamc@133 141
adamc@252 142 fun subExpInExp (n, e1) e2 =
adamc@252 143 let
adamc@252 144 val r = subExpInExp' (n, e1) e2
adamc@252 145 in
adamc@252 146 (*Print.prefaces "subExpInExp" [("e1", MonoPrint.p_exp MonoEnv.empty e1),
adamc@252 147 ("e2", MonoPrint.p_exp MonoEnv.empty e2),
adamc@252 148 ("r", MonoPrint.p_exp MonoEnv.empty r)];*)
adamc@252 149 r
adamc@252 150 end
adamc@133 151
adamc@133 152 fun typ c = c
adamc@133 153
adamc@316 154 val swapExpVars =
adamc@316 155 U.Exp.mapB {typ = fn t => t,
adamc@316 156 exp = fn lower => fn e =>
adamc@316 157 case e of
adamc@316 158 ERel xn =>
adamc@316 159 if xn = lower then
adamc@316 160 ERel (lower + 1)
adamc@316 161 else if xn = lower + 1 then
adamc@316 162 ERel lower
adamc@316 163 else
adamc@316 164 e
adamc@316 165 | _ => e,
adamc@316 166 bind = fn (lower, U.Exp.RelE _) => lower+1
adamc@316 167 | (lower, _) => lower}
adamc@316 168
adamc@341 169 val swapExpVarsPat =
adamc@341 170 U.Exp.mapB {typ = fn t => t,
adamc@341 171 exp = fn (lower, len) => fn e =>
adamc@341 172 case e of
adamc@341 173 ERel xn =>
adamc@341 174 if xn = lower then
adamc@827 175 ERel (lower + len)
adamc@341 176 else if xn >= lower + 1 andalso xn < lower + 1 + len then
adamc@341 177 ERel (xn - 1)
adamc@341 178 else
adamc@341 179 e
adamc@341 180 | _ => e,
adamc@341 181 bind = fn ((lower, len), U.Exp.RelE _) => (lower+1, len)
adamc@341 182 | (st, _) => st}
adamc@341 183
adam@1817 184 datatype result = Yes of (string * typ * exp) list | No | Maybe
adamc@258 185
adamc@183 186 fun match (env, p : pat, e : exp) =
adamc@183 187 case (#1 p, #1 e) of
adamc@258 188 (PWild, _) => Yes env
adam@1817 189 | (PVar (x, t), _) => Yes ((x, t, e) :: env)
adamc@183 190
adamc@280 191 | (PPrim (Prim.String s), EStrcat ((EPrim (Prim.String s'), _), _)) =>
adamc@280 192 if String.isPrefix s' s then
adamc@280 193 Maybe
adamc@280 194 else
adamc@280 195 No
adamc@280 196
adamc@1207 197 | (PPrim (Prim.String s), EStrcat (_, (EPrim (Prim.String s'), _))) =>
adamc@1207 198 if String.isSuffix s' s then
adamc@1207 199 Maybe
adamc@1207 200 else
adamc@1207 201 No
adamc@1207 202
adamc@183 203 | (PPrim p, EPrim p') =>
adamc@183 204 if Prim.equal (p, p') then
adamc@258 205 Yes env
adamc@183 206 else
adamc@258 207 No
adamc@183 208
kkallio@1489 209 | (PCon (_, PConVar n1, po), ECon (_, PConVar n2, eo)) =>
adamc@183 210 if n1 = n2 then
kkallio@1489 211 case (po, eo) of
kkallio@1489 212 (NONE, NONE) => Yes env
kkallio@1489 213 | (SOME p, SOME e) => match (env, p, e)
kkallio@1489 214 | _ => Maybe
adamc@183 215 else
adamc@258 216 No
adamc@183 217
adamc@188 218 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, NONE), ECon (_, PConFfi {mod = m2, con = con2, ...}, NONE)) =>
adamc@185 219 if m1 = m2 andalso con1 = con2 then
adamc@258 220 Yes env
adamc@185 221 else
adamc@258 222 No
adamc@185 223
adamc@188 224 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, SOME ep), ECon (_, PConFfi {mod = m2, con = con2, ...}, SOME e)) =>
adamc@185 225 if m1 = m2 andalso con1 = con2 then
adamc@185 226 match (env, p, e)
adamc@185 227 else
adamc@258 228 No
adamc@185 229
adamc@183 230 | (PRecord xps, ERecord xes) =>
adamc@183 231 let
adamc@183 232 fun consider (xps, env) =
adamc@183 233 case xps of
adamc@258 234 [] => Yes env
adamc@183 235 | (x, p, _) :: rest =>
adamc@183 236 case List.find (fn (x', _, _) => x' = x) xes of
adamc@258 237 NONE => No
adamc@183 238 | SOME (_, e, _) =>
adamc@183 239 case match (env, p, e) of
adamc@258 240 No => No
adamc@258 241 | Maybe => Maybe
adamc@258 242 | Yes env => consider (rest, env)
adamc@183 243 in
adamc@183 244 consider (xps, env)
adamc@183 245 end
adamc@183 246
adamc@726 247 | (PNone _, ENone _) => Yes env
adamc@921 248 | (PNone _, ESome _) => No
adamc@726 249 | (PSome (_, p), ESome (_, e)) => match (env, p, e)
adamc@921 250 | (PSome _, ENone _) => No
adamc@726 251
adamc@258 252 | _ => Maybe
adamc@183 253
adamc@318 254 datatype event =
adamc@318 255 WritePage
adamc@318 256 | ReadDb
adamc@318 257 | WriteDb
adamc@1225 258 | ReadCookie
adamc@1225 259 | WriteCookie
adamc@481 260 | UseRel
adamc@318 261 | Unsure
adamc@1225 262 | Abort
adamc@318 263
adamc@318 264 fun p_event e =
adamc@318 265 let
adamc@318 266 open Print.PD
adamc@318 267 in
adamc@318 268 case e of
adamc@318 269 WritePage => string "WritePage"
adamc@318 270 | ReadDb => string "ReadDb"
adamc@318 271 | WriteDb => string "WriteDb"
adamc@1225 272 | ReadCookie => string "ReadCookie"
adamc@1225 273 | WriteCookie => string "WriteCookie"
adamc@481 274 | UseRel => string "UseRel"
adamc@318 275 | Unsure => string "Unsure"
adamc@1225 276 | Abort => string "Abort"
adamc@318 277 end
adamc@318 278
adamc@470 279 val p_events = Print.p_list p_event
adamc@470 280
adamc@318 281 fun patBinds (p, _) =
adamc@318 282 case p of
adamc@318 283 PWild => 0
adamc@318 284 | PVar _ => 1
adamc@318 285 | PPrim _ => 0
adamc@318 286 | PCon (_, _, NONE) => 0
adamc@318 287 | PCon (_, _, SOME p) => patBinds p
adamc@318 288 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
adamc@318 289 | PNone _ => 0
adamc@318 290 | PSome (_, p) => patBinds p
adamc@318 291
adamc@932 292 val countFree = U.Exp.foldB {typ = fn (_, n) => n,
adamc@932 293 exp = fn (x, e, n) =>
adamc@932 294 case e of
adamc@932 295 ERel x' => if x = x' then n + 1 else n
adamc@932 296 | _ => n,
adamc@932 297 bind = fn (n, b) =>
adamc@932 298 case b of
adamc@932 299 U.Exp.RelE _ => n + 1
adamc@975 300 | _ => n}
adamc@975 301
adamc@975 302 val freeInAbs = U.Exp.existsB {typ = fn _ => false,
adamc@975 303 exp = fn (n, e) =>
adamc@975 304 case e of
adamc@975 305 EAbs (_, _, _, b) => countFree n 0 b > 0
adamc@975 306 | EJavaScript (_, b) => countFree n 0 b > 0
adamc@975 307 | _ => false,
adamc@975 308 bind = fn (n, b) =>
adamc@975 309 case b of
adamc@975 310 U.Exp.RelE _ => n + 1
adamc@975 311 | _ => n} 0
adamc@932 312
adam@1845 313 fun reduce (file : file) =
adamc@470 314 let
adamc@948 315 val (timpures, impures, absCounts) =
adamc@948 316 foldl (fn ((d, _), (timpures, impures, absCounts)) =>
adamc@916 317 let
adam@1820 318 fun countAbs env e =
adam@1820 319 case #1 e of
adam@1820 320 EAbs (x, t, _, e) => 1 + countAbs (E.pushERel env x t NONE) e
adam@1820 321 | _ =>
adam@1820 322 let
adam@1820 323 fun remaining e =
adam@1820 324 case #1 e of
adam@1820 325 ENamed n => IM.find (absCounts, n)
adam@1820 326 | EApp (e, arg) =>
adam@1820 327 if simpleImpure (timpures, impures) env arg then
adam@1820 328 NONE
adam@1820 329 else
adam@1820 330 (case remaining e of
adam@1820 331 NONE => NONE
adam@1820 332 | SOME n => if n > 0 then
adam@1820 333 SOME (n - 1)
adam@1820 334 else
adam@1820 335 NONE)
adam@1820 336 | _ => NONE
adam@1820 337 in
adam@1820 338 getOpt (remaining e, 0)
adam@1820 339 end
adamc@916 340 in
adamc@916 341 case d of
adamc@948 342 DDatatype dts =>
adamc@948 343 (if List.exists (fn (_, _, cs) =>
adamc@948 344 List.exists (fn (_, _, NONE) => false
adamc@948 345 | (_, _, SOME t) => simpleTypeImpure timpures t) cs)
adamc@948 346 dts then
adamc@948 347 IS.addList (timpures, map #2 dts)
adamc@948 348 else
adamc@948 349 timpures,
adamc@948 350 impures,
adamc@948 351 absCounts)
adamc@948 352 | DVal (_, n, _, e, _) =>
adamc@948 353 (timpures,
adamc@948 354 if simpleImpure (timpures, impures) E.empty e then
adamc@916 355 IS.add (impures, n)
adamc@916 356 else
adamc@916 357 impures,
adam@1820 358 IM.insert (absCounts, n, countAbs E.empty e))
adamc@916 359 | DValRec vis =>
adamc@948 360 (timpures,
adamc@948 361 if List.exists (fn (_, _, _, e, _) => simpleImpure (timpures, impures) E.empty e) vis then
adamc@916 362 foldl (fn ((_, n, _, _, _), impures) =>
adamc@916 363 IS.add (impures, n)) impures vis
adamc@916 364 else
adamc@916 365 impures,
adamc@916 366 foldl (fn ((x, n, _, e, _), absCounts) =>
adam@1820 367 IM.insert (absCounts, n, countAbs E.empty e))
adamc@916 368 absCounts vis)
adamc@948 369 | _ => (timpures, impures, absCounts)
adamc@916 370 end)
adam@1845 371 (IS.empty, IS.empty, IM.empty) (#1 file)
adamc@387 372
adamc@1017 373 val uses = U.File.fold {typ = fn (_, m) => m,
adamc@1017 374 exp = fn (e, m) =>
adamc@1017 375 case e of
adamc@1017 376 ENamed n => IM.insert (m, n, 1 + Option.getOpt (IM.find (m, n), 0))
adamc@1017 377 | _ => m,
adamc@1017 378 decl = fn (_, m) => m}
adamc@1017 379 IM.empty file
adamc@1017 380
adamc@1017 381 val size = U.Exp.fold {typ = fn (_, n) => n,
adamc@1017 382 exp = fn (_, n) => n + 1} 0
adamc@1017 383
adam@1360 384 val functionInside' = U.Typ.exists (fn c => case c of
adam@1360 385 TFun _ => true
adam@1360 386 | _ => false)
adam@1360 387
adam@1360 388 fun functionInside t =
adam@1360 389 case #1 t of
adam@1360 390 TFun (t1, t2) => functionInside' t1 orelse functionInside t2
adam@1360 391 | _ => functionInside' t
adam@1360 392
adam@1393 393 fun mayInline (n, e, t, s) =
adamc@1017 394 case IM.find (uses, n) of
adamc@1017 395 NONE => false
adamc@1017 396 | SOME count => count <= 1
adamc@1017 397 orelse size e <= Settings.getMonoInline ()
adam@1360 398 orelse functionInside t
adam@1393 399 orelse Settings.checkAlwaysInline s
adamc@1017 400
adamc@470 401 fun summarize d (e, _) =
adamc@579 402 let
adamc@579 403 val s =
adamc@579 404 case e of
adamc@579 405 EPrim _ => []
adamc@579 406 | ERel n => if n = d then [UseRel] else []
adamc@579 407 | ENamed _ => []
adamc@579 408 | ECon (_, _, NONE) => []
adamc@579 409 | ECon (_, _, SOME e) => summarize d e
adamc@579 410 | ENone _ => []
adamc@579 411 | ESome (_, e) => summarize d e
adamc@579 412 | EFfi _ => []
adam@1663 413 | EFfiApp ("Basis", "get_cookie", [(e, _)]) =>
adamc@1225 414 summarize d e @ [ReadCookie]
adam@1423 415 | EFfiApp ("Basis", "set_cookie", es) =>
adam@1663 416 List.concat (map (summarize d o #1) es) @ [WriteCookie]
adam@1423 417 | EFfiApp ("Basis", "clear_cookie", es) =>
adam@1663 418 List.concat (map (summarize d o #1) es) @ [WriteCookie]
adamc@765 419 | EFfiApp (m, x, es) =>
adamc@1171 420 if Settings.isEffectful (m, x) orelse Settings.isBenignEffectful (m, x) then
adam@1663 421 List.concat (map (summarize d o #1) es) @ [if m = "Basis" andalso String.isSuffix "_w" x then
adam@1663 422 WritePage
adam@1663 423 else
adam@1663 424 Unsure]
adamc@765 425 else
adam@1663 426 List.concat (map (summarize d o #1) es)
adamc@579 427 | EApp ((EFfi _, _), e) => summarize d e
adamc@579 428 | EApp _ =>
adamc@579 429 let
adamc@694 430 fun unravel (e, passed, ls) =
adamc@579 431 case e of
adamc@579 432 ENamed n =>
adamc@579 433 let
adamc@579 434 val ls = rev ls
adamc@579 435 in
adamc@948 436 if IS.member (impures, n) then
adamc@948 437 case IM.find (absCounts, n) of
adamc@948 438 NONE => [Unsure]
adamc@948 439 | SOME len =>
adamc@948 440 if passed < len then
adamc@948 441 ls
adamc@948 442 else
adamc@948 443 ls @ [Unsure]
adamc@948 444 else
adamc@948 445 ls
adamc@579 446 end
adamc@579 447 | ERel n => List.revAppend (ls,
adamc@579 448 if n = d then
adamc@579 449 [UseRel, Unsure]
adamc@579 450 else
adamc@579 451 [Unsure])
adamc@579 452 | EApp (f, x) =>
adamc@995 453 unravel (#1 f, passed + 1, List.revAppend (summarize d x,
adamc@995 454 ls))
adam@1394 455 | EError _ => [Abort]
adamc@579 456 | _ => [Unsure]
adamc@579 457 in
adamc@694 458 unravel (e, 0, [])
adamc@579 459 end
adamc@252 460
adamc@941 461 | EAbs _ => []
adamc@316 462
adamc@579 463 | EUnop (_, e) => summarize d e
adam@1360 464 | EBinop (_, _, e1, e2) => summarize d e1 @ summarize d e2
adamc@470 465
adamc@579 466 | ERecord xets => List.concat (map (summarize d o #2) xets)
adamc@579 467 | EField (e, _) => summarize d e
adamc@470 468
adamc@1214 469 | ECase (e, pes, _) =>
adamc@1214 470 let
adamc@579 471 val lss = map (fn (p, e) => summarize (d + patBinds p) e) pes
adam@1394 472
adam@1394 473 fun splitRel ls acc =
adam@1394 474 case ls of
adam@1394 475 [] => (acc, false, ls)
adam@1394 476 | UseRel :: ls => (acc, true, ls)
adam@1394 477 | v :: ls => splitRel ls (v :: acc)
adam@1394 478
adam@1394 479 val (pre, used, post) = foldl (fn (ls, (pre, used, post)) =>
adam@1394 480 let
adam@1394 481 val (pre', used', post') = splitRel ls []
adam@1394 482 in
adam@1394 483 (pre' @ pre, used' orelse used, post' @ post)
adam@1394 484 end)
adam@1394 485 ([], false, []) lss
adamc@579 486 in
adam@1394 487 summarize d e
adam@1394 488 @ pre
adam@1394 489 @ (if used then [UseRel] else [])
adam@1394 490 @ post
adamc@1214 491 end
adamc@579 492 | EStrcat (e1, e2) => summarize d e1 @ summarize d e2
adamc@316 493
adamc@1225 494 | EError (e, _) => summarize d e @ [Abort]
adamc@1225 495 | EReturnBlob {blob = e1, mimeType = e2, ...} => summarize d e1 @ summarize d e2 @ [Abort]
adamc@1225 496 | ERedirect (e, _) => summarize d e @ [Abort]
adamc@318 497
adamc@579 498 | EWrite e => summarize d e @ [WritePage]
adamc@579 499
adamc@579 500 | ESeq (e1, e2) => summarize d e1 @ summarize d e2
adamc@579 501 | ELet (_, _, e1, e2) => summarize d e1 @ summarize (d + 1) e2
adamc@456 502
adamc@579 503 | EClosure (_, es) => List.concat (map (summarize d) es)
adamc@470 504
adamc@579 505 | EQuery {query, body, initial, ...} =>
adamc@579 506 List.concat [summarize d query,
adamc@579 507 summarize d initial,
adamc@941 508 [ReadDb],
adamc@941 509 summarize (d + 2) body]
adamc@470 510
adam@1293 511 | EDml (e, _) => summarize d e @ [WriteDb]
adamc@579 512 | ENextval e => summarize d e @ [WriteDb]
adamc@1073 513 | ESetval (e1, e2) => summarize d e1 @ summarize d e2 @ [WriteDb]
adamc@1112 514 | EUnurlify (e, _, _) => summarize d e
adamc@815 515 | EJavaScript (_, e) => summarize d e
adamc@579 516 | ESignalReturn e => summarize d e
adamc@579 517 | ESignalBind (e1, e2) => summarize d e1 @ summarize d e2
adamc@579 518 | ESignalSource e => summarize d e
adamc@608 519
adam@1848 520 | EServerCall (e, _, _, _) => summarize d e @ [Unsure]
adamc@1021 521 | ERecv (e, _) => summarize d e @ [Unsure]
adamc@1021 522 | ESleep e => summarize d e @ [Unsure]
adamc@1021 523 | ESpawn e => summarize d e @ [Unsure]
adamc@579 524 in
adamc@579 525 (*Print.prefaces "Summarize"
adamc@579 526 [("e", MonoPrint.p_exp MonoEnv.empty (e, ErrorMsg.dummySpan)),
adamc@579 527 ("d", Print.PD.string (Int.toString d)),
adamc@579 528 ("s", p_events s)];*)
adamc@579 529 s
adamc@579 530 end
adamc@470 531
adamc@919 532 val impure = fn env => fn e =>
adamc@948 533 simpleImpure (timpures, impures) env e andalso impure e
adamc@916 534 andalso not (List.null (summarize ~1 e))
adamc@916 535
adam@1852 536 fun passive (e : exp) =
adam@1852 537 case #1 e of
adam@1852 538 EPrim _ => true
adam@1852 539 | ERel _ => true
adam@1852 540 | ENamed _ => true
adam@1852 541 | ECon (_, _, NONE) => true
adam@1852 542 | ECon (_, _, SOME e) => passive e
adam@1852 543 | ENone _ => true
adam@1852 544 | ESome (_, e) => passive e
adam@1852 545 | EFfi _ => true
adam@1852 546 | EAbs _ => true
adam@1852 547 | ERecord xets => List.all (passive o #2) xets
adam@1852 548 | EField (e, _) => passive e
adam@1852 549 | _ => false
adam@1852 550
adamc@470 551 fun exp env e =
adamc@470 552 let
adamc@470 553 (*val () = Print.prefaces "exp" [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan))]*)
adamc@470 554
adamc@1017 555 fun doLet (x, t, e', b) =
adamc@1017 556 let
adamc@1017 557 fun doSub () =
adamc@1017 558 let
adamc@1017 559 val r = subExpInExp (0, e') b
adamc@1017 560 in
adamc@1017 561 (*Print.prefaces "doSub" [("e'", MonoPrint.p_exp env e'),
adam@1423 562 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
adam@1423 563 ("r", MonoPrint.p_exp env r)];*)
adamc@1017 564 #1 (reduceExp env r)
adamc@1017 565 end
adamc@1017 566
adam@1855 567 fun trySub () =
adamc@1017 568 ((*Print.prefaces "trySub"
adamc@1017 569 [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan))];*)
adamc@1017 570 case t of
adamc@1017 571 (TFfi ("Basis", "string"), _) => doSub ()
adamc@1017 572 | (TSignal _, _) => e
adamc@1017 573 | _ =>
adam@1855 574 case e' of
adam@1855 575 (ECase _, _) => e
adam@1855 576 | _ => doSub ())
adamc@1017 577 in
adamc@1017 578 if impure env e' then
adamc@1017 579 let
adamc@1017 580 val effs_e' = summarize 0 e'
adamc@1017 581 val effs_e' = List.filter (fn x => x <> UseRel) effs_e'
adamc@1017 582 val effs_b = summarize 0 b
adamc@1017 583
adam@1394 584 (*val () = Print.fprefaces outf "Try"
adam@1394 585 [(*("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan)),*)
adamc@1017 586 ("e'", MonoPrint.p_exp env e'),
adamc@1017 587 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
adamc@1017 588 ("e'_eff", p_events effs_e'),
adamc@1017 589 ("b_eff", p_events effs_b)]*)
adamc@1017 590
adamc@1017 591 fun does eff = List.exists (fn eff' => eff' = eff) effs_e'
adamc@1017 592 val writesPage = does WritePage
adamc@1017 593 val readsDb = does ReadDb
adamc@1017 594 val writesDb = does WriteDb
adamc@1225 595 val readsCookie = does ReadCookie
adamc@1225 596 val writesCookie = does ReadCookie
adamc@1017 597
adamc@1017 598 fun verifyUnused eff =
adamc@1017 599 case eff of
adamc@1017 600 UseRel => false
adamc@1017 601 | _ => true
adamc@1017 602
adamc@1017 603 fun verifyCompatible effs =
adamc@1017 604 case effs of
adamc@1017 605 [] => false
adamc@1017 606 | eff :: effs =>
adamc@1017 607 case eff of
adamc@1017 608 Unsure => false
adamc@1017 609 | UseRel => List.all verifyUnused effs
adamc@1017 610 | WritePage => not writesPage andalso verifyCompatible effs
adamc@1017 611 | ReadDb => not writesDb andalso verifyCompatible effs
adamc@1017 612 | WriteDb => not writesDb andalso not readsDb andalso verifyCompatible effs
adamc@1225 613 | ReadCookie => not writesCookie andalso verifyCompatible effs
adamc@1225 614 | WriteCookie => not writesCookie andalso not readsCookie
adamc@1225 615 andalso verifyCompatible effs
adamc@1225 616 | Abort => true
adamc@1017 617 in
adamc@1017 618 (*Print.prefaces "verifyCompatible"
adamc@1017 619 [("e'", MonoPrint.p_exp env e'),
adamc@1017 620 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
adamc@1017 621 ("effs_e'", Print.p_list p_event effs_e'),
adamc@1017 622 ("effs_b", Print.p_list p_event effs_b)];*)
adamc@1017 623 if (List.null effs_e'
adamc@1017 624 orelse (List.all (fn eff => eff <> Unsure) effs_e'
adamc@1017 625 andalso verifyCompatible effs_b)
adamc@1017 626 orelse (case effs_b of
adamc@1017 627 UseRel :: effs => List.all verifyUnused effs
adamc@1017 628 | _ => false))
adamc@1017 629 andalso countFree 0 0 b = 1
adamc@1017 630 andalso not (freeInAbs b) then
adam@1855 631 trySub ()
adamc@1017 632 else
adamc@1017 633 e
adamc@1017 634 end
adam@1852 635 else if countFree 0 0 b > 1 andalso not (!fullMode) andalso not (passive e') then
adam@1817 636 e
adamc@1017 637 else
adam@1855 638 trySub ()
adamc@1017 639 end
adamc@1017 640
adamc@470 641 val r =
adamc@470 642 case e of
adamc@470 643 ERel n =>
adamc@470 644 (case E.lookupERel env n of
adamc@470 645 (_, _, SOME e') => #1 e'
adamc@470 646 | _ => e)
adamc@470 647 | ENamed n =>
adamc@470 648 (case E.lookupENamed env n of
adamc@470 649 (_, _, SOME e', _) => ((*Print.prefaces "Switch" [("n", Print.PD.string (Int.toString n)),
adamc@470 650 ("e'", MonoPrint.p_exp env e')];*)
adamc@470 651 #1 e')
adamc@470 652 | _ => e)
adamc@470 653
adamc@470 654 | EApp ((EAbs (x, t, _, e1), loc), e2) =>
adamc@470 655 ((*Print.prefaces "Considering" [("e1", MonoPrint.p_exp (E.pushERel env x t NONE) e1),
adamc@694 656 ("e2", MonoPrint.p_exp env e2),
adamc@694 657 ("sub", MonoPrint.p_exp env (reduceExp env (subExpInExp (0, e2) e1)))];*)
adam@1852 658 if impure env e2 orelse (not (!fullMode) andalso countFree 0 0 e1 > 1) then
adamc@470 659 #1 (reduceExp env (ELet (x, t, e2, e1), loc))
adamc@470 660 else
adamc@470 661 #1 (reduceExp env (subExpInExp (0, e2) e1)))
adamc@470 662
adamc@470 663 | ECase (e', pes, {disc, result}) =>
adamc@442 664 let
adamc@470 665 fun push () =
adamc@470 666 case result of
adamc@470 667 (TFun (dom, result), loc) =>
adamc@1107 668 let
adam@1855 669 fun safe e =
adam@1855 670 List.all (fn UseRel => true
adam@1855 671 | Abort => true
adam@1855 672 | _ => false) (summarize 0 e)
adam@1855 673
adam@1855 674 fun p_events' es = Print.box [Print.PD.string "{",
adam@1855 675 p_events es,
adam@1855 676 Print.PD.string "}"]
adamc@1107 677 in
adamc@1107 678 if List.all (safe o #2) pes then
adamc@1107 679 EAbs ("y", dom, result,
adamc@1107 680 (ECase (liftExpInExp 0 e',
adamc@1107 681 map (fn (p, (EAbs (_, _, _, e), _)) =>
adamc@1107 682 (p, swapExpVarsPat (0, patBinds p) e)
adam@1445 683 | (p, (EError (e, (TFun (_, t), _)), loc)) =>
adam@1445 684 (p, (EError (e, t), loc))
adam@1855 685 | (p, e) =>
adam@1855 686 (p, (EApp (liftExpInExp (patBinds p) e,
adam@1855 687 (ERel (patBinds p), loc)), loc)))
adam@1855 688 pes,
adamc@1107 689 {disc = disc, result = result}), loc))
adamc@1107 690 else
adamc@1107 691 e
adamc@1107 692 end
adamc@470 693 | _ => e
adamc@318 694
adamc@470 695 fun search pes =
adamc@470 696 case pes of
adamc@470 697 [] => push ()
adamc@470 698 | (p, body) :: pes =>
adamc@814 699 case match ([], p, e') of
adamc@470 700 No => search pes
adamc@470 701 | Maybe => push ()
adamc@814 702 | Yes subs =>
adamc@800 703 let
adamc@829 704 val (body, remaining) =
adam@1817 705 foldl (fn ((x, t, e), (body, remaining)) =>
adam@1817 706 (if countFree 0 0 body > 1 then
adam@1817 707 (ELet (x, t, multiLift remaining e, body), #2 e')
adam@1817 708 else
adam@1817 709 subExpInExp (0, multiLift remaining e) body, remaining - 1))
adamc@829 710 (body, length subs - 1) subs
adamc@920 711 val r = reduceExp (E.patBinds env p) body
adamc@800 712 in
adamc@829 713 (*Print.preface ("subs", Print.p_list (MonoPrint.p_exp env) subs);*)
adamc@800 714 (*Print.prefaces "ECase"
adamc@829 715 [("old", MonoPrint.p_exp env body),
adamc@829 716 ("body", MonoPrint.p_exp env body),
adamc@800 717 ("r", MonoPrint.p_exp env r)];*)
adamc@800 718 #1 r
adamc@800 719 end
adamc@470 720 in
adamc@941 721 if impure env e' then
adamc@941 722 e
adamc@941 723 else
adamc@941 724 search pes
adamc@470 725 end
adamc@318 726
adamc@470 727 | EField ((ERecord xes, _), x) =>
adamc@470 728 (case List.find (fn (x', _, _) => x' = x) xes of
adamc@470 729 SOME (_, e, _) => #1 e
adamc@470 730 | NONE => e)
adamc@398 731
adamc@470 732 | ELet (x1, t1, (ELet (x2, t2, e1, b1), loc), b2) =>
adamc@470 733 let
adamc@470 734 val e' = (ELet (x2, t2, e1,
adamc@470 735 (ELet (x1, t1, b1,
adamc@470 736 liftExpInExp 1 b2), loc)), loc)
adamc@442 737 in
adamc@470 738 (*Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)),
adamc@470 739 ("e'", MonoPrint.p_exp env e')];*)
adamc@470 740 #1 (reduceExp env e')
adamc@470 741 end
adamc@470 742 | EApp ((ELet (x, t, e, b), loc), e') =>
adamc@470 743 #1 (reduceExp env (ELet (x, t, e,
adamc@470 744 (EApp (b, liftExpInExp 0 e'), loc)), loc))
adamc@470 745
adamc@1017 746 | ELet (x, t, e', b as (EAbs (x', t' as (TRecord [], _), ran, e''), loc)) =>
adamc@919 747 if impure env e' then
adamc@1017 748 doLet (x, t, e', b)
adamc@848 749 else
adamc@920 750 EAbs (x', t', ran, reduceExp (E.pushERel env x' t' NONE)
adamc@920 751 (ELet (x, t, liftExpInExp 0 e', swapExpVars 0 e''), loc))
adamc@470 752
adamc@1017 753 | ELet (x, t, e', b) => doLet (x, t, e', b)
adamc@252 754
adamc@470 755 | EStrcat ((EPrim (Prim.String s1), _), (EPrim (Prim.String s2), _)) =>
adamc@470 756 EPrim (Prim.String (s1 ^ s2))
adamc@268 757
adamc@572 758 | ESignalBind ((ESignalReturn e1, loc), e2) =>
adamc@572 759 #1 (reduceExp env (EApp (e2, e1), loc))
adamc@572 760
adamc@470 761 | _ => e
adamc@470 762 in
adamc@829 763 (*Print.prefaces "exp'" [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan)),
adamc@829 764 ("r", MonoPrint.p_exp env (r, ErrorMsg.dummySpan))];*)
adamc@470 765 r
adamc@470 766 end
adamc@470 767
adamc@470 768 and bind (env, b) =
adamc@470 769 case b of
adamc@470 770 U.Decl.Datatype (x, n, xncs) => E.pushDatatype env x n xncs
adamc@470 771 | U.Decl.RelE (x, t) => E.pushERel env x t NONE
adamc@1017 772 | U.Decl.NamedE (x, n, t, eo, s) =>
adamc@1017 773 let
adamc@1017 774 val eo = case eo of
adamc@1017 775 NONE => NONE
adam@1393 776 | SOME e => if mayInline (n, e, t, s) then
adamc@1017 777 SOME e
adamc@1017 778 else
adamc@1017 779 NONE
adamc@1017 780 in
adamc@1017 781 E.pushENamed env x n t (Option.map (reduceExp env) eo) s
adamc@1017 782 end
adamc@470 783
adamc@470 784 and reduceExp env = U.Exp.mapB {typ = typ, exp = exp, bind = bind} env
adamc@470 785
adamc@801 786 fun decl env d = ((*Print.preface ("d", MonoPrint.p_decl env (d, ErrorMsg.dummySpan));*)
adamc@801 787 d)
adamc@398 788 in
adamc@470 789 U.File.mapB {typ = typ, exp = exp, decl = decl, bind = bind} E.empty file
adamc@398 790 end
adamc@133 791
adamc@133 792 end