annotate src/mono_reduce.sml @ 465:ddd363e856ff

Cookie prose; fix bugs that broke demo compilation
author Adam Chlipala <adamc@hcoop.net>
date Thu, 06 Nov 2008 12:46:45 -0500
parents bb27c7efcd90
children 7cb418e9714f
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@133 37
adamc@252 38 fun impure (e, _) =
adamc@252 39 case e of
adamc@252 40 EWrite _ => true
adamc@252 41 | EQuery _ => true
adamc@307 42 | EDml _ => true
adamc@338 43 | ENextval _ => true
adamc@463 44 | EUnurlify _ => true
adamc@252 45 | EAbs _ => false
adamc@252 46
adamc@252 47 | EPrim _ => false
adamc@252 48 | ERel _ => false
adamc@252 49 | ENamed _ => false
adamc@252 50 | ECon (_, _, eo) => (case eo of NONE => false | SOME e => impure e)
adamc@297 51 | ENone _ => false
adamc@290 52 | ESome (_, e) => impure e
adamc@252 53 | EFfi _ => false
adamc@462 54 | EFfiApp ("Basis", "set_cookie", _) => true
adamc@252 55 | EFfiApp _ => false
adamc@252 56 | EApp ((EFfi _, _), _) => false
adamc@252 57 | EApp _ => true
adamc@252 58
adamc@387 59 | EUnop (_, e) => impure e
adamc@387 60 | EBinop (_, e1, e2) => impure e1 orelse impure e2
adamc@387 61
adamc@252 62 | ERecord xes => List.exists (fn (_, e, _) => impure e) xes
adamc@252 63 | EField (e, _) => impure e
adamc@252 64
adamc@252 65 | ECase (e, pes, _) => impure e orelse List.exists (fn (_, e) => impure e) pes
adamc@252 66
adamc@283 67 | EError (e, _) => impure e
adamc@283 68
adamc@252 69 | EStrcat (e1, e2) => impure e1 orelse impure e2
adamc@252 70
adamc@252 71 | ESeq (e1, e2) => impure e1 orelse impure e2
adamc@252 72 | ELet (_, _, e1, e2) => impure e1 orelse impure e2
adamc@252 73
adamc@252 74 | EClosure (_, es) => List.exists impure es
adamc@252 75
adamc@252 76
adamc@252 77 val liftExpInExp = Monoize.liftExpInExp
adamc@252 78
adamc@252 79 val subExpInExp' =
adamc@133 80 U.Exp.mapB {typ = fn t => t,
adamc@133 81 exp = fn (xn, rep) => fn e =>
adamc@133 82 case e of
adamc@133 83 ERel xn' =>
adamc@133 84 (case Int.compare (xn', xn) of
adamc@133 85 EQUAL => #1 rep
adamc@133 86 | GREATER=> ERel (xn' - 1)
adamc@133 87 | LESS => e)
adamc@133 88 | _ => e,
adamc@133 89 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
adamc@133 90 | (ctx, _) => ctx}
adamc@133 91
adamc@252 92 fun subExpInExp (n, e1) e2 =
adamc@252 93 let
adamc@252 94 val r = subExpInExp' (n, e1) e2
adamc@252 95 in
adamc@252 96 (*Print.prefaces "subExpInExp" [("e1", MonoPrint.p_exp MonoEnv.empty e1),
adamc@252 97 ("e2", MonoPrint.p_exp MonoEnv.empty e2),
adamc@252 98 ("r", MonoPrint.p_exp MonoEnv.empty r)];*)
adamc@252 99 r
adamc@252 100 end
adamc@133 101
adamc@133 102 fun typ c = c
adamc@133 103
adamc@316 104 val swapExpVars =
adamc@316 105 U.Exp.mapB {typ = fn t => t,
adamc@316 106 exp = fn lower => fn e =>
adamc@316 107 case e of
adamc@316 108 ERel xn =>
adamc@316 109 if xn = lower then
adamc@316 110 ERel (lower + 1)
adamc@316 111 else if xn = lower + 1 then
adamc@316 112 ERel lower
adamc@316 113 else
adamc@316 114 e
adamc@316 115 | _ => e,
adamc@316 116 bind = fn (lower, U.Exp.RelE _) => lower+1
adamc@316 117 | (lower, _) => lower}
adamc@316 118
adamc@341 119 val swapExpVarsPat =
adamc@341 120 U.Exp.mapB {typ = fn t => t,
adamc@341 121 exp = fn (lower, len) => fn e =>
adamc@341 122 case e of
adamc@341 123 ERel xn =>
adamc@341 124 if xn = lower then
adamc@341 125 ERel (lower + 1)
adamc@341 126 else if xn >= lower + 1 andalso xn < lower + 1 + len then
adamc@341 127 ERel (xn - 1)
adamc@341 128 else
adamc@341 129 e
adamc@341 130 | _ => e,
adamc@341 131 bind = fn ((lower, len), U.Exp.RelE _) => (lower+1, len)
adamc@341 132 | (st, _) => st}
adamc@341 133
adamc@258 134 datatype result = Yes of E.env | No | Maybe
adamc@258 135
adamc@183 136 fun match (env, p : pat, e : exp) =
adamc@183 137 case (#1 p, #1 e) of
adamc@258 138 (PWild, _) => Yes env
adamc@258 139 | (PVar (x, t), _) => Yes (E.pushERel env x t (SOME e))
adamc@183 140
adamc@280 141 | (PPrim (Prim.String s), EStrcat ((EPrim (Prim.String s'), _), _)) =>
adamc@280 142 if String.isPrefix s' s then
adamc@280 143 Maybe
adamc@280 144 else
adamc@280 145 No
adamc@280 146
adamc@183 147 | (PPrim p, EPrim p') =>
adamc@183 148 if Prim.equal (p, p') then
adamc@258 149 Yes env
adamc@183 150 else
adamc@258 151 No
adamc@183 152
adamc@188 153 | (PCon (_, PConVar n1, NONE), ECon (_, PConVar n2, NONE)) =>
adamc@183 154 if n1 = n2 then
adamc@258 155 Yes env
adamc@183 156 else
adamc@258 157 No
adamc@183 158
adamc@188 159 | (PCon (_, PConVar n1, SOME p), ECon (_, PConVar n2, SOME e)) =>
adamc@183 160 if n1 = n2 then
adamc@183 161 match (env, p, e)
adamc@183 162 else
adamc@258 163 No
adamc@183 164
adamc@188 165 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, NONE), ECon (_, PConFfi {mod = m2, con = con2, ...}, NONE)) =>
adamc@185 166 if m1 = m2 andalso con1 = con2 then
adamc@258 167 Yes env
adamc@185 168 else
adamc@258 169 No
adamc@185 170
adamc@188 171 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, SOME ep), ECon (_, PConFfi {mod = m2, con = con2, ...}, SOME e)) =>
adamc@185 172 if m1 = m2 andalso con1 = con2 then
adamc@185 173 match (env, p, e)
adamc@185 174 else
adamc@258 175 No
adamc@185 176
adamc@183 177 | (PRecord xps, ERecord xes) =>
adamc@183 178 let
adamc@183 179 fun consider (xps, env) =
adamc@183 180 case xps of
adamc@258 181 [] => Yes env
adamc@183 182 | (x, p, _) :: rest =>
adamc@183 183 case List.find (fn (x', _, _) => x' = x) xes of
adamc@258 184 NONE => No
adamc@183 185 | SOME (_, e, _) =>
adamc@183 186 case match (env, p, e) of
adamc@258 187 No => No
adamc@258 188 | Maybe => Maybe
adamc@258 189 | Yes env => consider (rest, env)
adamc@183 190 in
adamc@183 191 consider (xps, env)
adamc@183 192 end
adamc@183 193
adamc@258 194 | _ => Maybe
adamc@183 195
adamc@318 196 datatype event =
adamc@318 197 WritePage
adamc@318 198 | ReadDb
adamc@318 199 | WriteDb
adamc@318 200 | UseRel of int
adamc@318 201 | Unsure
adamc@318 202
adamc@318 203 fun p_event e =
adamc@318 204 let
adamc@318 205 open Print.PD
adamc@318 206 in
adamc@318 207 case e of
adamc@318 208 WritePage => string "WritePage"
adamc@318 209 | ReadDb => string "ReadDb"
adamc@318 210 | WriteDb => string "WriteDb"
adamc@318 211 | UseRel n => string ("UseRel" ^ Int.toString n)
adamc@318 212 | Unsure => string "Unsure"
adamc@318 213 end
adamc@318 214
adamc@318 215 fun patBinds (p, _) =
adamc@318 216 case p of
adamc@318 217 PWild => 0
adamc@318 218 | PVar _ => 1
adamc@318 219 | PPrim _ => 0
adamc@318 220 | PCon (_, _, NONE) => 0
adamc@318 221 | PCon (_, _, SOME p) => patBinds p
adamc@318 222 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
adamc@318 223 | PNone _ => 0
adamc@318 224 | PSome (_, p) => patBinds p
adamc@318 225
adamc@318 226 fun summarize d (e, _) =
adamc@318 227 case e of
adamc@318 228 EPrim _ => []
adamc@318 229 | ERel n => if n >= d then [UseRel (n - d)] else []
adamc@318 230 | ENamed _ => []
adamc@318 231 | ECon (_, _, NONE) => []
adamc@318 232 | ECon (_, _, SOME e) => summarize d e
adamc@318 233 | ENone _ => []
adamc@318 234 | ESome (_, e) => summarize d e
adamc@318 235 | EFfi _ => []
adamc@462 236 | EFfiApp ("Basis", "set_cookie", _) => [Unsure]
adamc@318 237 | EFfiApp (_, _, es) => List.concat (map (summarize d) es)
adamc@319 238 | EApp ((EFfi _, _), e) => summarize d e
adamc@318 239 | EApp _ => [Unsure]
adamc@318 240 | EAbs _ => []
adamc@318 241
adamc@387 242 | EUnop (_, e) => summarize d e
adamc@387 243 | EBinop (_, e1, e2) => summarize d e1 @ summarize d e2
adamc@387 244
adamc@318 245 | ERecord xets => List.concat (map (summarize d o #2) xets)
adamc@318 246 | EField (e, _) => summarize d e
adamc@318 247
adamc@318 248 | ECase (e, pes, _) =>
adamc@318 249 let
adamc@318 250 val lss = map (fn (p, e) => summarize (d + patBinds p) e) pes
adamc@318 251 in
adamc@318 252 case lss of
adamc@318 253 [] => raise Fail "Empty pattern match"
adamc@318 254 | ls :: lss =>
adamc@318 255 if List.all (fn ls' => ls' = ls) lss then
adamc@318 256 summarize d e @ ls
adamc@318 257 else
adamc@318 258 [Unsure]
adamc@318 259 end
adamc@318 260 | EStrcat (e1, e2) => summarize d e1 @ summarize d e2
adamc@318 261
adamc@318 262 | EError (e, _) => summarize d e @ [Unsure]
adamc@318 263
adamc@318 264 | EWrite e => summarize d e @ [WritePage]
adamc@318 265
adamc@318 266 | ESeq (e1, e2) => summarize d e1 @ summarize d e2
adamc@318 267 | ELet (_, _, e1, e2) => summarize d e1 @ summarize (d + 1) e2
adamc@318 268
adamc@318 269 | EClosure (_, es) => List.concat (map (summarize d) es)
adamc@318 270
adamc@318 271 | EQuery {query, body, initial, ...} =>
adamc@318 272 List.concat [summarize d query,
adamc@319 273 summarize (d + 2) body,
adamc@318 274 summarize d initial,
adamc@318 275 [ReadDb]]
adamc@318 276
adamc@318 277 | EDml e => summarize d e @ [WriteDb]
adamc@338 278 | ENextval e => summarize d e @ [WriteDb]
adamc@463 279 | EUnurlify (e, _) => summarize d e
adamc@318 280
adamc@133 281 fun exp env e =
adamc@398 282 let
adamc@398 283 (*val () = Print.prefaces "exp" [("e", MonoPrint.p_exp env (e, ErrorMsg.dummySpan))]*)
adamc@133 284
adamc@398 285 val r =
adamc@398 286 case e of
adamc@398 287 ERel n =>
adamc@398 288 (case E.lookupERel env n of
adamc@398 289 (_, _, SOME e') => #1 e'
adamc@398 290 | _ => e)
adamc@398 291 | ENamed n =>
adamc@398 292 (case E.lookupENamed env n of
adamc@398 293 (_, _, SOME e', _) => ((*Print.prefaces "Switch" [("n", Print.PD.string (Int.toString n)),
adamc@398 294 ("e'", MonoPrint.p_exp env e')];*)
adamc@398 295 #1 e')
adamc@398 296 | _ => e)
adamc@133 297
adamc@398 298 | EApp ((EAbs (x, t, _, e1), loc), e2) =>
adamc@398 299 ((*Print.prefaces "Considering" [("e1", MonoPrint.p_exp (E.pushERel env x t NONE) e1),
adamc@398 300 ("e2", MonoPrint.p_exp env e2),
adamc@398 301 ("sub", MonoPrint.p_exp env (reduceExp env (subExpInExp (0, e2) e1)))];*)
adamc@398 302 if impure e2 then
adamc@398 303 #1 (reduceExp env (ELet (x, t, e2, e1), loc))
adamc@398 304 else
adamc@398 305 #1 (reduceExp env (subExpInExp (0, e2) e1)))
adamc@341 306
adamc@398 307 | ECase (e', pes, {disc, result}) =>
adamc@398 308 let
adamc@398 309 fun push () =
adamc@398 310 case result of
adamc@398 311 (TFun (dom, result), loc) =>
adamc@398 312 if List.all (fn (_, (EAbs _, _)) => true | _ => false) pes then
adamc@398 313 EAbs ("_", dom, result,
adamc@398 314 (ECase (liftExpInExp 0 e',
adamc@398 315 map (fn (p, (EAbs (_, _, _, e), _)) =>
adamc@398 316 (p, swapExpVarsPat (0, patBinds p) e)
adamc@398 317 | _ => raise Fail "MonoReduce ECase") pes,
adamc@398 318 {disc = disc, result = result}), loc))
adamc@398 319 else
adamc@398 320 e
adamc@398 321 | _ => e
adamc@183 322
adamc@398 323 fun search pes =
adamc@398 324 case pes of
adamc@398 325 [] => push ()
adamc@398 326 | (p, body) :: pes =>
adamc@398 327 case match (env, p, e') of
adamc@398 328 No => search pes
adamc@398 329 | Maybe => push ()
adamc@398 330 | Yes env => #1 (reduceExp env body)
adamc@398 331 in
adamc@398 332 search pes
adamc@398 333 end
adamc@252 334
adamc@398 335 | EField ((ERecord xes, _), x) =>
adamc@398 336 (case List.find (fn (x', _, _) => x' = x) xes of
adamc@398 337 SOME (_, e, _) => #1 e
adamc@398 338 | NONE => e)
adamc@316 339
adamc@398 340 | ELet (x1, t1, (ELet (x2, t2, e1, b1), loc), b2) =>
adamc@398 341 let
adamc@398 342 val e' = (ELet (x2, t2, e1,
adamc@398 343 (ELet (x1, t1, b1,
adamc@398 344 liftExpInExp 1 b2), loc)), loc)
adamc@398 345 in
adamc@398 346 (*Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)),
adamc@398 347 ("e'", MonoPrint.p_exp env e')];*)
adamc@398 348 #1 (reduceExp env e')
adamc@398 349 end
adamc@398 350 | EApp ((ELet (x, t, e, b), loc), e') =>
adamc@398 351 #1 (reduceExp env (ELet (x, t, e,
adamc@398 352 (EApp (b, liftExpInExp 0 e'), loc)), loc))
adamc@316 353
adamc@462 354 | ELet (x, t, e', (EAbs (x', t' as (TRecord [], _), ran, e''), loc)) =>
adamc@465 355 (*if impure e' then
adamc@462 356 e
adamc@465 357 else*)
adamc@465 358 (* Seems unsound in general without the check... should revisit later *)
adamc@462 359 EAbs (x', t', ran, (ELet (x, t, liftExpInExp 0 e', swapExpVars 0 e''), loc))
adamc@318 360
adamc@398 361 | ELet (x, t, e', b) =>
adamc@442 362 let
adamc@462 363 fun doSub () =
adamc@462 364 #1 (reduceExp env (subExpInExp (0, e') b))
adamc@456 365
adamc@442 366 fun trySub () =
adamc@456 367 case t of
adamc@456 368 (TFfi ("Basis", "string"), _) => doSub ()
adamc@456 369 | _ =>
adamc@456 370 case e' of
adamc@456 371 (ECase _, _) => e
adamc@456 372 | _ => doSub ()
adamc@442 373 in
adamc@442 374 if impure e' then
adamc@442 375 let
adamc@442 376 val effs_e' = summarize 0 e'
adamc@442 377 val effs_b = summarize 0 b
adamc@318 378
adamc@442 379 fun does eff = List.exists (fn eff' => eff' = eff) effs_e'
adamc@442 380 val writesPage = does WritePage
adamc@442 381 val readsDb = does ReadDb
adamc@442 382 val writesDb = does WriteDb
adamc@318 383
adamc@442 384 fun verifyUnused eff =
adamc@442 385 case eff of
adamc@442 386 UseRel r => r <> 0
adamc@442 387 | Unsure => false
adamc@442 388 | _ => true
adamc@398 389
adamc@442 390 fun verifyCompatible effs =
adamc@442 391 case effs of
adamc@442 392 [] => false
adamc@442 393 | eff :: effs =>
adamc@442 394 case eff of
adamc@442 395 Unsure => false
adamc@442 396 | UseRel r =>
adamc@442 397 if r = 0 then
adamc@442 398 List.all verifyUnused effs
adamc@442 399 else
adamc@442 400 verifyCompatible effs
adamc@442 401 | WritePage => not writesPage andalso verifyCompatible effs
adamc@442 402 | ReadDb => not writesDb andalso verifyCompatible effs
adamc@442 403 | WriteDb => not writesDb andalso not readsDb andalso verifyCompatible effs
adamc@442 404 in
adamc@442 405 (*Print.prefaces "verifyCompatible"
adamc@442 406 [("e'", MonoPrint.p_exp env e'),
adamc@442 407 ("b", MonoPrint.p_exp (E.pushERel env x t NONE) b),
adamc@442 408 ("effs_e'", Print.p_list p_event effs_e'),
adamc@442 409 ("effs_b", Print.p_list p_event effs_b)];*)
adamc@442 410 if verifyCompatible effs_b then
adamc@442 411 trySub ()
adamc@442 412 else
adamc@442 413 e
adamc@442 414 end
adamc@442 415 else
adamc@442 416 trySub ()
adamc@442 417 end
adamc@252 418
adamc@398 419 | EStrcat ((EPrim (Prim.String s1), _), (EPrim (Prim.String s2), _)) =>
adamc@398 420 EPrim (Prim.String (s1 ^ s2))
adamc@268 421
adamc@398 422 | _ => e
adamc@398 423 in
adamc@398 424 (*Print.prefaces "exp'" [("r", MonoPrint.p_exp env (r, ErrorMsg.dummySpan))];*)
adamc@398 425 r
adamc@398 426 end
adamc@133 427
adamc@252 428 and bind (env, b) =
adamc@252 429 case b of
adamc@252 430 U.Decl.Datatype (x, n, xncs) => E.pushDatatype env x n xncs
adamc@252 431 | U.Decl.RelE (x, t) => E.pushERel env x t NONE
adamc@252 432 | U.Decl.NamedE (x, n, t, eo, s) => E.pushENamed env x n t (Option.map (reduceExp env) eo) s
adamc@252 433
adamc@133 434 and reduceExp env = U.Exp.mapB {typ = typ, exp = exp, bind = bind} env
adamc@133 435
adamc@133 436 fun decl env d = d
adamc@133 437
adamc@133 438 val reduce = U.File.mapB {typ = typ, exp = exp, decl = decl, bind = bind} E.empty
adamc@133 439
adamc@133 440 end