annotate src/reduce.sml @ 990:46803e668a89

Fix a de Bruijn index bug in map fusion
author Adam Chlipala <adamc@hcoop.net>
date Tue, 06 Oct 2009 10:15:26 -0400
parents 01a4d936395a
children 065ce3252090
rev   line source
adamc@20 1 (* Copyright (c) 2008, Adam Chlipala
adamc@20 2 * All rights reserved.
adamc@20 3 *
adamc@20 4 * Redistribution and use in source and binary forms, with or without
adamc@20 5 * modification, are permitted provided that the following conditions are met:
adamc@20 6 *
adamc@20 7 * - Redistributions of source code must retain the above copyright notice,
adamc@20 8 * this list of conditions and the following disclaimer.
adamc@20 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@20 10 * this list of conditions and the following disclaimer in the documentation
adamc@20 11 * and/or other materials provided with the distribution.
adamc@20 12 * - The names of contributors may not be used to endorse or promote products
adamc@20 13 * derived from this software without specific prior written permission.
adamc@20 14 *
adamc@20 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@20 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@20 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@20 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@20 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@20 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@20 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@20 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@20 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@20 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@20 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@20 26 *)
adamc@20 27
adamc@20 28 (* Simplify a Core program algebraically *)
adamc@20 29
adamc@20 30 structure Reduce :> REDUCE = struct
adamc@20 31
adamc@20 32 open Core
adamc@20 33
adamc@508 34 structure IM = IntBinaryMap
adamc@20 35
adamc@908 36 structure E = CoreEnv
adamc@908 37
adamc@908 38 fun multiLiftExpInExp n e =
adamc@908 39 if n = 0 then
adamc@908 40 e
adamc@908 41 else
adamc@908 42 multiLiftExpInExp (n - 1) (E.liftExpInExp 0 e)
adamc@908 43
adamc@909 44 val dangling =
adamc@909 45 CoreUtil.Exp.existsB {kind = fn _ => false,
adamc@909 46 con = fn _ => false,
adamc@909 47 exp = fn (n, e) =>
adamc@909 48 case e of
adamc@909 49 ERel n' => n' >= n
adamc@909 50 | _ => false,
adamc@909 51 bind = fn (n, b) =>
adamc@909 52 case b of
adamc@909 53 CoreUtil.Exp.RelE _ => n + 1
adamc@909 54 | _ => n}
adamc@909 55
adamc@508 56 datatype env_item =
adamc@626 57 UnknownK
adamc@626 58 | KnownK of kind
adamc@626 59
adamc@626 60 | UnknownC
adamc@508 61 | KnownC of con
adamc@21 62
adamc@508 63 | UnknownE
adamc@508 64 | KnownE of exp
adamc@20 65
adamc@626 66 | Lift of int * int * int
adamc@20 67
adamc@909 68 val edepth = foldl (fn (UnknownE, n) => n + 1
adamc@909 69 | (KnownE _, n) => n + 1
adamc@909 70 | (_, n) => n) 0
adamc@909 71
adamc@909 72 val edepth' = foldl (fn (UnknownE, n) => n + 1
adamc@909 73 | (KnownE _, n) => n + 1
adamc@909 74 | (Lift (_, _, n'), n) => n + n'
adamc@909 75 | (_, n) => n) 0
adamc@909 76
adamc@508 77 type env = env_item list
adamc@20 78
adamc@510 79 fun ei2s ei =
adamc@510 80 case ei of
adamc@626 81 UnknownK => "UK"
adamc@626 82 | KnownK _ => "KK"
adamc@626 83 | UnknownC => "UC"
adamc@510 84 | KnownC _ => "KC"
adamc@510 85 | UnknownE => "UE"
adamc@510 86 | KnownE _ => "KE"
adamc@626 87 | Lift (_, n1, n2) => "(" ^ Int.toString n1 ^ ", " ^ Int.toString n2 ^ ")"
adamc@510 88
adamc@510 89 fun e2s env = String.concatWith " " (map ei2s env)
adamc@510 90
adamc@909 91 (*val deKnown = List.filter (fn KnownC _ => false
adamc@510 92 | KnownE _ => false
adamc@626 93 | KnownK _ => false
adamc@909 94 | _ => true)*)
adamc@909 95
adamc@909 96 val deKnown = ListUtil.mapConcat (fn KnownC _ => []
adamc@909 97 | KnownE _ => []
adamc@909 98 | KnownK _ => []
adamc@909 99 | Lift (nk, nc, ne) => List.tabulate (nk, fn _ => UnknownK)
adamc@909 100 @ List.tabulate (nc, fn _ => UnknownC)
adamc@909 101 @ List.tabulate (ne, fn _ => UnknownE)
adamc@909 102 | x => [x])
adamc@510 103
adamc@930 104 datatype result = Yes of env | No | Maybe
adamc@930 105
adamc@930 106 fun match (env, p : pat, e : exp) =
adamc@942 107 let
adamc@942 108 val baseline = length env
adamc@930 109
adamc@942 110 fun match (env, p, e) =
adamc@942 111 case (#1 p, #1 e) of
adamc@942 112 (PWild, _) => Yes env
adamc@942 113 | (PVar (x, t), _) => Yes (KnownE (multiLiftExpInExp (length env - baseline) e) :: env)
adamc@930 114
adamc@942 115 | (PPrim p, EPrim p') =>
adamc@942 116 if Prim.equal (p, p') then
adamc@942 117 Yes env
adamc@942 118 else
adamc@942 119 No
adamc@930 120
adamc@942 121 | (PCon (_, PConVar n1, _, NONE), ECon (_, PConVar n2, _, NONE)) =>
adamc@942 122 if n1 = n2 then
adamc@942 123 Yes env
adamc@942 124 else
adamc@942 125 No
adamc@930 126
adamc@942 127 | (PCon (_, PConVar n1, _, SOME p), ECon (_, PConVar n2, _, SOME e)) =>
adamc@942 128 if n1 = n2 then
adamc@942 129 match (env, p, e)
adamc@942 130 else
adamc@942 131 No
adamc@930 132
adamc@942 133 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, _, NONE),
adamc@942 134 ECon (_, PConFfi {mod = m2, con = con2, ...}, _, NONE)) =>
adamc@942 135 if m1 = m2 andalso con1 = con2 then
adamc@942 136 Yes env
adamc@942 137 else
adamc@942 138 No
adamc@930 139
adamc@942 140 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, _, SOME ep),
adamc@942 141 ECon (_, PConFfi {mod = m2, con = con2, ...}, _, SOME e)) =>
adamc@942 142 if m1 = m2 andalso con1 = con2 then
adamc@942 143 match (env, p, e)
adamc@942 144 else
adamc@942 145 No
adamc@930 146
adamc@942 147 | (PRecord xps, ERecord xes) =>
adamc@942 148 if List.exists (fn ((CName _, _), _, _) => false
adamc@942 149 | _ => true) xes then
adamc@942 150 Maybe
adamc@942 151 else
adamc@942 152 let
adamc@942 153 fun consider (xps, env) =
adamc@942 154 case xps of
adamc@942 155 [] => Yes env
adamc@942 156 | (x, p, _) :: rest =>
adamc@942 157 case List.find (fn ((CName x', _), _, _) => x' = x
adamc@942 158 | _ => false) xes of
adamc@942 159 NONE => No
adamc@942 160 | SOME (_, e, _) =>
adamc@942 161 case match (env, p, e) of
adamc@942 162 No => No
adamc@942 163 | Maybe => Maybe
adamc@942 164 | Yes env => consider (rest, env)
adamc@942 165 in
adamc@942 166 consider (xps, env)
adamc@942 167 end
adamc@942 168
adamc@942 169 | _ => Maybe
adamc@942 170 in
adamc@942 171 match (env, p, e)
adamc@942 172 end
adamc@930 173
adamc@626 174 fun kindConAndExp (namedC, namedE) =
adamc@508 175 let
adamc@626 176 fun kind env (all as (k, loc)) =
adamc@626 177 case k of
adamc@626 178 KType => all
adamc@626 179 | KArrow (k1, k2) => (KArrow (kind env k1, kind env k2), loc)
adamc@626 180 | KName => all
adamc@626 181 | KRecord k => (KRecord (kind env k), loc)
adamc@626 182 | KUnit => all
adamc@626 183 | KTuple ks => (KTuple (map (kind env) ks), loc)
adamc@626 184
adamc@626 185 | KRel n =>
adamc@626 186 let
adamc@626 187 fun find (n', env, nudge, lift) =
adamc@626 188 case env of
adamc@626 189 [] => raise Fail "Reduce.kind: KRel"
adamc@626 190 | UnknownC :: rest => find (n', rest, nudge, lift)
adamc@626 191 | KnownC _ :: rest => find (n', rest, nudge, lift)
adamc@626 192 | UnknownE :: rest => find (n', rest, nudge, lift)
adamc@626 193 | KnownE _ :: rest => find (n', rest, nudge, lift)
adamc@626 194 | Lift (lift', _, _) :: rest => find (n', rest, nudge + lift', lift + lift')
adamc@626 195 | UnknownK :: rest =>
adamc@626 196 if n' = 0 then
adamc@626 197 (KRel (n + nudge), loc)
adamc@626 198 else
adamc@626 199 find (n' - 1, rest, nudge, lift + 1)
adamc@626 200 | KnownK k :: rest =>
adamc@626 201 if n' = 0 then
adamc@626 202 kind (Lift (lift, 0, 0) :: rest) k
adamc@626 203 else
adamc@626 204 find (n' - 1, rest, nudge - 1, lift)
adamc@626 205 in
adamc@626 206 find (n, env, 0, 0)
adamc@626 207 end
adamc@626 208 | KFun (x, k) => (KFun (x, kind (UnknownK :: env) k), loc)
adamc@626 209
adamc@508 210 fun con env (all as (c, loc)) =
adamc@510 211 ((*Print.prefaces "con" [("c", CorePrint.p_con CoreEnv.empty all)];*)
adamc@508 212 case c of
adamc@508 213 TFun (c1, c2) => (TFun (con env c1, con env c2), loc)
adamc@626 214 | TCFun (x, k, c2) => (TCFun (x, kind env k, con (UnknownC :: env) c2), loc)
adamc@626 215 | TKFun (x, c2) => (TKFun (x, con (UnknownK :: env) c2), loc)
adamc@508 216 | TRecord c => (TRecord (con env c), loc)
adamc@215 217
adamc@508 218 | CRel n =>
adamc@508 219 let
adamc@626 220 fun find (n', env, nudge, liftK, liftC) =
adamc@510 221 case env of
adamc@510 222 [] => raise Fail "Reduce.con: CRel"
adamc@626 223 | UnknownK :: rest => find (n', rest, nudge, liftK + 1, liftC)
adamc@626 224 | KnownK _ :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 225 | UnknownE :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 226 | KnownE _ :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 227 | Lift (liftK', liftC', _) :: rest => find (n', rest, nudge + liftC',
adamc@626 228 liftK + liftK', liftC + liftC')
adamc@510 229 | UnknownC :: rest =>
adamc@510 230 if n' = 0 then
adamc@510 231 (CRel (n + nudge), loc)
adamc@510 232 else
adamc@626 233 find (n' - 1, rest, nudge, liftK, liftC + 1)
adamc@510 234 | KnownC c :: rest =>
adamc@510 235 if n' = 0 then
adamc@626 236 con (Lift (liftK, liftC, 0) :: rest) c
adamc@510 237 else
adamc@626 238 find (n' - 1, rest, nudge - 1, liftK, liftC)
adamc@508 239 in
adamc@510 240 (*print (Int.toString n ^ ": " ^ e2s env ^ "\n");*)
adamc@626 241 find (n, env, 0, 0, 0)
adamc@508 242 end
adamc@508 243 | CNamed n =>
adamc@508 244 (case IM.find (namedC, n) of
adamc@508 245 NONE => all
adamc@508 246 | SOME c => c)
adamc@508 247 | CFfi _ => all
adamc@508 248 | CApp (c1, c2) =>
adamc@508 249 let
adamc@508 250 val c1 = con env c1
adamc@508 251 val c2 = con env c2
adamc@508 252 in
adamc@508 253 case #1 c1 of
adamc@508 254 CAbs (_, _, b) =>
adamc@510 255 con (KnownC c2 :: deKnown env) b
adamc@215 256
adamc@621 257 | CApp ((CMap (dom, ran), _), f) =>
adamc@508 258 (case #1 c2 of
adamc@626 259 CRecord (_, []) => (CRecord (kind env ran, []), loc)
adamc@621 260 | CRecord (_, (x, c) :: rest) =>
adamc@510 261 con (deKnown env)
adamc@621 262 (CConcat ((CRecord (ran, [(x, (CApp (f, c), loc))]), loc),
adamc@626 263 (CApp (c1, (CRecord (kind env dom, rest), loc)), loc)), loc)
adamc@508 264 | _ => (CApp (c1, c2), loc))
adamc@20 265
adamc@508 266 | _ => (CApp (c1, c2), loc)
adamc@508 267 end
adamc@626 268 | CAbs (x, k, b) => (CAbs (x, kind env k, con (UnknownC :: env) b), loc)
adamc@626 269
adamc@626 270 | CKApp (c1, k) =>
adamc@626 271 let
adamc@626 272 val c1 = con env c1
adamc@626 273 in
adamc@626 274 case #1 c1 of
adamc@626 275 CKAbs (_, b) =>
adamc@626 276 con (KnownK k :: deKnown env) b
adamc@626 277
adamc@626 278 | _ => (CKApp (c1, kind env k), loc)
adamc@626 279 end
adamc@626 280 | CKAbs (x, b) => (CKAbs (x, con (UnknownK :: env) b), loc)
adamc@20 281
adamc@508 282 | CName _ => all
adamc@21 283
adamc@626 284 | CRecord (k, xcs) => (CRecord (kind env k, map (fn (x, c) => (con env x, con env c)) xcs), loc)
adamc@508 285 | CConcat (c1, c2) =>
adamc@508 286 let
adamc@508 287 val c1 = con env c1
adamc@508 288 val c2 = con env c2
adamc@508 289 in
adamc@508 290 case (#1 c1, #1 c2) of
adamc@508 291 (CRecord (k, xcs1), CRecord (_, xcs2)) =>
adamc@626 292 (CRecord (kind env k, xcs1 @ xcs2), loc)
adamc@508 293 | _ => (CConcat (c1, c2), loc)
adamc@508 294 end
adamc@626 295 | CMap (dom, ran) => (CMap (kind env dom, kind env ran), loc)
adamc@74 296
adamc@508 297 | CUnit => all
adamc@21 298
adamc@508 299 | CTuple cs => (CTuple (map (con env) cs), loc)
adamc@508 300 | CProj (c, n) =>
adamc@508 301 let
adamc@508 302 val c = con env c
adamc@508 303 in
adamc@508 304 case #1 c of
adamc@508 305 CTuple cs => List.nth (cs, n - 1)
adamc@508 306 | _ => (CProj (c, n), loc)
adamc@510 307 end)
adamc@22 308
adamc@509 309 fun patCon pc =
adamc@509 310 case pc of
adamc@509 311 PConVar _ => pc
adamc@509 312 | PConFfi {mod = m, datatyp, params, con = c, arg, kind} =>
adamc@509 313 PConFfi {mod = m, datatyp = datatyp, params = params, con = c,
adamc@509 314 arg = Option.map (con (map (fn _ => UnknownC) params)) arg,
adamc@509 315 kind = kind}
adamc@509 316
adamc@509 317
adamc@509 318 val k = (KType, ErrorMsg.dummySpan)
adamc@509 319 fun doPart e (this as (x, t), rest) =
adamc@509 320 ((x, (EField (e, x, {field = t, rest = (CRecord (k, rest), #2 t)}), #2 t), t),
adamc@509 321 this :: rest)
adamc@509 322
adamc@509 323 fun exp env (all as (e, loc)) =
adamc@909 324 let
adamc@909 325 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 326 ("env", Print.PD.string (e2s env))]*)
adamc@910 327 val () = if dangling (edepth env) all then
adamc@909 328 (Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 329 ("env", Print.PD.string (e2s env))];
adamc@909 330 raise Fail "!")
adamc@909 331 else
adamc@910 332 ()
adamc@509 333
adamc@909 334 val r = case e of
adamc@909 335 EPrim _ => all
adamc@909 336 | ERel n =>
adamc@909 337 let
adamc@909 338 fun find (n', env, nudge, liftK, liftC, liftE) =
adamc@909 339 case env of
adamc@909 340 [] => raise Fail ("Reduce.exp: ERel (" ^ ErrorMsg.spanToString loc ^ ")")
adamc@909 341 | UnknownK :: rest => find (n', rest, nudge, liftK + 1, liftC, liftE)
adamc@909 342 | KnownK _ :: rest => find (n', rest, nudge, liftK, liftC, liftE)
adamc@909 343 | UnknownC :: rest => find (n', rest, nudge, liftK, liftC + 1, liftE)
adamc@909 344 | KnownC _ :: rest => find (n', rest, nudge, liftK, liftC, liftE)
adamc@909 345 | Lift (liftK', liftC', liftE') :: rest =>
adamc@909 346 find (n', rest, nudge + liftE',
adamc@909 347 liftK + liftK', liftC + liftC', liftE + liftE')
adamc@909 348 | UnknownE :: rest =>
adamc@909 349 if n' = 0 then
adamc@909 350 (ERel (n + nudge), loc)
adamc@909 351 else
adamc@909 352 find (n' - 1, rest, nudge, liftK, liftC, liftE + 1)
adamc@909 353 | KnownE e :: rest =>
adamc@909 354 if n' = 0 then
adamc@909 355 ((*print "SUBSTITUTING\n";*)
adamc@909 356 exp (Lift (liftK, liftC, liftE) :: rest) e)
adamc@909 357 else
adamc@909 358 find (n' - 1, rest, nudge - 1, liftK, liftC, liftE)
adamc@909 359 in
adamc@909 360 (*print (Int.toString n ^ ": " ^ e2s env ^ "\n");*)
adamc@909 361 find (n, env, 0, 0, 0, 0)
adamc@909 362 end
adamc@909 363 | ENamed n =>
adamc@909 364 (case IM.find (namedE, n) of
adamc@909 365 NONE => all
adamc@909 366 | SOME e => e)
adamc@909 367 | ECon (dk, pc, cs, eo) => (ECon (dk, patCon pc,
adamc@909 368 map (con env) cs, Option.map (exp env) eo), loc)
adamc@909 369 | EFfi _ => all
adamc@909 370 | EFfiApp (m, f, es) => (EFfiApp (m, f, map (exp env) es), loc)
adamc@908 371
adamc@909 372 (*| EApp (
adamc@909 373 (EApp
adamc@909 374 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _), t1), _), t2), _),
adamc@909 375 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@909 376 (ECase (ed, pes, {disc, ...}), _)), _),
adamc@909 377 trans2) =>
adamc@909 378 let
adamc@909 379 val e' = (EFfi ("Basis", "bind"), loc)
adamc@909 380 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@909 381 val e' = (ECApp (e', t1), loc)
adamc@909 382 val e' = (ECApp (e', t2), loc)
adamc@909 383 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@908 384
adamc@909 385 val pes = map (fn (p, e) =>
adamc@909 386 let
adamc@909 387 val e' = (EApp (e', e), loc)
adamc@909 388 val e' = (EApp (e',
adamc@909 389 multiLiftExpInExp (E.patBindsN p)
adamc@909 390 trans2), loc)
adamc@909 391 val e' = exp env e'
adamc@909 392 in
adamc@909 393 (p, e')
adamc@909 394 end) pes
adamc@909 395 in
adamc@909 396 (ECase (exp env ed,
adamc@909 397 pes,
adamc@909 398 {disc = con env disc,
adamc@909 399 result = (CApp ((CFfi ("Basis", "transaction"), loc), con env t2), loc)}),
adamc@909 400 loc)
adamc@909 401 end*)
adamc@908 402
adamc@909 403 | EApp (e1, e2) =>
adamc@909 404 let
adamc@910 405 val env' = deKnown env
adamc@910 406
adamc@910 407 fun reassoc e =
adamc@910 408 case #1 e of
adamc@910 409 EApp
adamc@910 410 ((EApp
adamc@910 411 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _),
adamc@910 412 t1),
adamc@910 413 _), t2), _),
adamc@910 414 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@910 415 (EServerCall (n, es, (EAbs (_, _, _, ke), _), dom, ran), _)), _),
adamc@910 416 trans3) =>
adamc@910 417 let
adamc@910 418 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 419 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@910 420 val e' = (ECApp (e', dom), loc)
adamc@910 421 val e' = (ECApp (e', t2), loc)
adamc@910 422 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@910 423 val e' = (EApp (e', ke), loc)
adamc@910 424 val e' = (EApp (e', E.liftExpInExp 0 trans3), loc)
adamc@910 425 val e' = reassoc e'
adamc@910 426 val e' = (EAbs ("x", dom, t2, e'), loc)
adamc@910 427 val e' = (EServerCall (n, es, e', dom, t2), loc)
adamc@910 428 in
adamc@910 429 e'
adamc@910 430 end
adamc@910 431
adamc@910 432 | EApp
adamc@910 433 ((EApp
adamc@910 434 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _),
adamc@910 435 t1),
adamc@910 436 _), t2), _),
adamc@910 437 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@910 438 (EServerCall (n, es, ke, dom, ran), _)), _),
adamc@910 439 trans3) =>
adamc@910 440 let
adamc@910 441 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 442 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@910 443 val e' = (ECApp (e', dom), loc)
adamc@910 444 val e' = (ECApp (e', t2), loc)
adamc@910 445 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@910 446 val e' = (EApp (e', exp (UnknownE :: env')
adamc@910 447 (EApp (E.liftExpInExp 0 ke, (ERel 0, loc)), loc)),
adamc@910 448 loc)
adamc@910 449 val e' = (EApp (e', E.liftExpInExp 0 trans3), loc)
adamc@910 450 val e' = reassoc e'
adamc@910 451 val e' = (EAbs ("x", dom, t2, e'), loc)
adamc@910 452 val e' = (EServerCall (n, es, e', dom, t2), loc)
adamc@910 453 in
adamc@910 454 e'
adamc@910 455 end
adamc@910 456
adamc@910 457 | EApp
adamc@910 458 ((EApp
adamc@955 459 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _),
adamc@955 460 t1),
adamc@955 461 _), t2), _),
adamc@955 462 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@955 463 (ETailCall (n, es, (EAbs (_, _, _, ke), _), dom, ran), _)), _),
adamc@955 464 trans3) =>
adamc@955 465 let
adamc@955 466 val e' = (EFfi ("Basis", "bind"), loc)
adamc@955 467 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@955 468 val e' = (ECApp (e', dom), loc)
adamc@955 469 val e' = (ECApp (e', t2), loc)
adamc@955 470 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@955 471 val e' = (EApp (e', ke), loc)
adamc@955 472 val e' = (EApp (e', E.liftExpInExp 0 trans3), loc)
adamc@955 473 val e' = reassoc e'
adamc@955 474 val e' = (EAbs ("x", dom, t2, e'), loc)
adamc@955 475 val e' = (ETailCall (n, es, e', dom, t2), loc)
adamc@955 476 in
adamc@955 477 e'
adamc@955 478 end
adamc@955 479
adamc@955 480 | EApp
adamc@955 481 ((EApp
adamc@955 482 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _),
adamc@955 483 t1),
adamc@955 484 _), t2), _),
adamc@955 485 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@955 486 (ETailCall (n, es, ke, dom, ran), _)), _),
adamc@955 487 trans3) =>
adamc@955 488 let
adamc@955 489 val e' = (EFfi ("Basis", "bind"), loc)
adamc@955 490 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@955 491 val e' = (ECApp (e', dom), loc)
adamc@955 492 val e' = (ECApp (e', t2), loc)
adamc@955 493 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@955 494 val e' = (EApp (e', exp (UnknownE :: env')
adamc@955 495 (EApp (E.liftExpInExp 0 ke, (ERel 0, loc)), loc)),
adamc@955 496 loc)
adamc@955 497 val e' = (EApp (e', E.liftExpInExp 0 trans3), loc)
adamc@955 498 val e' = reassoc e'
adamc@955 499 val e' = (EAbs ("x", dom, t2, e'), loc)
adamc@955 500 val e' = (ETailCall (n, es, e', dom, t2), loc)
adamc@955 501 in
adamc@955 502 e'
adamc@955 503 end
adamc@955 504
adamc@955 505 | EApp
adamc@955 506 ((EApp
adamc@910 507 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), mt),
adamc@910 508 _), _), _), t3), _),
adamc@910 509 me), _),
adamc@910 510 (EApp ((EApp
adamc@910 511 ((EApp ((ECApp ((ECApp ((ECApp (
adamc@910 512 (EFfi ("Basis", "bind"), _), _), _),
adamc@910 513 t1), _), t2), _),
adamc@910 514 _), _),
adamc@910 515 trans1), _), (EAbs (_, _, _, trans2), _)), _)), _),
adamc@910 516 trans3) =>
adamc@910 517 let
adamc@910 518 val e'' = (EFfi ("Basis", "bind"), loc)
adamc@910 519 val e'' = (ECApp (e'', mt), loc)
adamc@910 520 val e'' = (ECApp (e'', t2), loc)
adamc@910 521 val e'' = (ECApp (e'', t3), loc)
adamc@910 522 val e'' = (EApp (e'', me), loc)
adamc@910 523 val e'' = (EApp (e'', trans2), loc)
adamc@910 524 val e'' = (EApp (e'', E.liftExpInExp 0 trans3), loc)
adamc@910 525 val e'' = reassoc e''
adamc@910 526 val e'' = (EAbs ("xb", t1, (CApp (mt, t3), loc), e''), loc)
adamc@910 527
adamc@910 528 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 529 val e' = (ECApp (e', mt), loc)
adamc@910 530 val e' = (ECApp (e', t1), loc)
adamc@910 531 val e' = (ECApp (e', t3), loc)
adamc@910 532 val e' = (EApp (e', me), loc)
adamc@910 533 val e' = (EApp (e', trans1), loc)
adamc@910 534 val e' = (EApp (e', e''), loc)
adamc@910 535 in
adamc@910 536 e'
adamc@910 537 end
adamc@910 538
adamc@910 539 | EApp
adamc@910 540 ((EApp
adamc@910 541 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), mt),
adamc@910 542 _), _), _), t3), _),
adamc@910 543 me), _),
adamc@910 544 (EApp ((EApp
adamc@910 545 ((EApp ((ECApp ((ECApp ((ECApp (
adamc@910 546 (EFfi ("Basis", "bind"), _), _), _),
adamc@910 547 t1), _), t2), _),
adamc@910 548 _), _),
adamc@910 549 trans1), _), trans2), _)), _),
adamc@910 550 trans3) =>
adamc@910 551 let
adamc@910 552 val e'' = (EFfi ("Basis", "bind"), loc)
adamc@910 553 val e'' = (ECApp (e'', mt), loc)
adamc@910 554 val e'' = (ECApp (e'', t2), loc)
adamc@910 555 val e'' = (ECApp (e'', t3), loc)
adamc@910 556 val e'' = (EApp (e'', me), loc)
adamc@910 557 val () = print "In2\n"
adamc@910 558 val e'' = (EApp (e'', exp (UnknownE :: env')
adamc@910 559 (EApp (E.liftExpInExp 0 trans2, (ERel 0, loc)),
adamc@910 560 loc)),
adamc@910 561 loc)
adamc@910 562 val () = print "Out2\n"
adamc@910 563 val e'' = (EApp (e'', E.liftExpInExp 0 trans3), loc)
adamc@910 564 val e'' = reassoc e''
adamc@910 565 val e'' = (EAbs ("xb", t1, (CApp (mt, t3), loc), e''), loc)
adamc@910 566
adamc@910 567 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 568 val e' = (ECApp (e', mt), loc)
adamc@910 569 val e' = (ECApp (e', t1), loc)
adamc@910 570 val e' = (ECApp (e', t3), loc)
adamc@910 571 val e' = (EApp (e', me), loc)
adamc@910 572 val e' = (EApp (e', trans1), loc)
adamc@910 573 val e' = (EApp (e', e''), loc)
adamc@910 574 in
adamc@910 575 e'
adamc@910 576 end
adamc@910 577
adamc@931 578 | EApp
adamc@931 579 ((EApp
adamc@931 580 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _),
adamc@931 581 t1),
adamc@931 582 _), t2), _),
adamc@931 583 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@931 584 (ECase (e, pes, {disc, ...}), _)), _), trans) =>
adamc@931 585 let
adamc@931 586 val e' = (EFfi ("Basis", "bind"), loc)
adamc@931 587 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@931 588 val e' = (ECApp (e', t1), loc)
adamc@931 589 val e' = (ECApp (e', t2), loc)
adamc@931 590 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@931 591
adamc@931 592 fun doCase (p, e) =
adamc@931 593 let
adamc@931 594 val e' = (EApp (e', e), loc)
adamc@931 595 val e' = (EApp (e',
adamc@931 596 multiLiftExpInExp (E.patBindsN p)
adamc@931 597 trans), loc)
adamc@931 598 in
adamc@931 599 (p, reassoc e')
adamc@931 600 end
adamc@931 601 in
adamc@931 602 (ECase (e, map doCase pes,
adamc@931 603 {disc = disc,
adamc@931 604 result = (CApp ((CFfi ("Basis", "transaction"), loc),
adamc@931 605 t2), loc)}), loc)
adamc@931 606 end
adamc@931 607
adamc@910 608 | _ => e
adamc@910 609
adamc@909 610 val e1 = exp env e1
adamc@909 611 val e2 = exp env e2
adamc@910 612 val e12 = reassoc (EApp (e1, e2), loc)
adamc@909 613 in
adamc@910 614 case #1 e12 of
adamc@910 615 EApp ((EAbs (_, _, _, b), _), e2) =>
adamc@931 616 exp (KnownE e2 :: env') b
adamc@910 617 | _ => e12
adamc@909 618 end
adamc@509 619
adamc@909 620 | EAbs (x, dom, ran, e) => (EAbs (x, con env dom, con env ran, exp (UnknownE :: env) e), loc)
adamc@509 621
adamc@909 622 | ECApp (e, c) =>
adamc@909 623 let
adamc@909 624 val e = exp env e
adamc@909 625 val c = con env c
adamc@909 626 in
adamc@909 627 case #1 e of
adamc@909 628 ECAbs (_, _, b) => exp (KnownC c :: deKnown env) b
adamc@909 629 | _ => (ECApp (e, c), loc)
adamc@909 630 end
adamc@626 631
adamc@909 632 | ECAbs (x, k, e) => (ECAbs (x, kind env k, exp (UnknownC :: env) e), loc)
adamc@626 633
adamc@909 634 | EKApp (e, k) =>
adamc@909 635 let
adamc@909 636 val e = exp env e
adamc@909 637 in
adamc@909 638 case #1 e of
adamc@909 639 EKAbs (_, b) => exp (KnownK k :: deKnown env) b
adamc@909 640 | _ => (EKApp (e, kind env k), loc)
adamc@909 641 end
adamc@509 642
adamc@909 643 | EKAbs (x, e) => (EKAbs (x, exp (UnknownK :: env) e), loc)
adamc@509 644
adamc@909 645 | ERecord xcs => (ERecord (map (fn (x, e, t) => (con env x, exp env e, con env t)) xcs), loc)
adamc@909 646 | EField (e, c, {field, rest}) =>
adamc@909 647 let
adamc@909 648 val e = exp env e
adamc@909 649 val c = con env c
adamc@509 650
adamc@909 651 fun default () = (EField (e, c, {field = con env field, rest = con env rest}), loc)
adamc@909 652 in
adamc@909 653 case (#1 e, #1 c) of
adamc@909 654 (ERecord xcs, CName x) =>
adamc@909 655 (case List.find (fn ((CName x', _), _, _) => x' = x | _ => false) xcs of
adamc@909 656 NONE => default ()
adamc@909 657 | SOME (_, e, _) => e)
adamc@909 658 | _ => default ()
adamc@909 659 end
adamc@509 660
adamc@909 661 | EConcat (e1, c1, e2, c2) =>
adamc@909 662 let
adamc@909 663 val e1 = exp env e1
adamc@909 664 val e2 = exp env e2
adamc@909 665 in
adamc@909 666 case (#1 e1, #1 e2) of
adamc@909 667 (ERecord xes1, ERecord xes2) => (ERecord (xes1 @ xes2), loc)
adamc@909 668 | _ =>
adamc@909 669 let
adamc@909 670 val c1 = con env c1
adamc@909 671 val c2 = con env c2
adamc@909 672 in
adamc@909 673 case (#1 c1, #1 c2) of
adamc@909 674 (CRecord (k, xcs1), CRecord (_, xcs2)) =>
adamc@909 675 let
adamc@909 676 val (xes1, rest) = ListUtil.foldlMap (doPart e1) [] xcs1
adamc@909 677 val (xes2, _) = ListUtil.foldlMap (doPart e2) rest xcs2
adamc@909 678 in
adamc@909 679 exp (deKnown env) (ERecord (xes1 @ xes2), loc)
adamc@909 680 end
adamc@909 681 | _ => (EConcat (e1, c1, e2, c2), loc)
adamc@909 682 end
adamc@909 683 end
adamc@509 684
adamc@909 685 | ECut (e, c, {field, rest}) =>
adamc@909 686 let
adamc@909 687 val e = exp env e
adamc@909 688 val c = con env c
adamc@509 689
adamc@909 690 fun default () =
adamc@909 691 let
adamc@909 692 val rest = con env rest
adamc@909 693 in
adamc@909 694 case #1 rest of
adamc@909 695 CRecord (k, xcs) =>
adamc@909 696 let
adamc@909 697 val (xes, _) = ListUtil.foldlMap (doPart e) [] xcs
adamc@909 698 in
adamc@909 699 exp (deKnown env) (ERecord xes, loc)
adamc@909 700 end
adamc@909 701 | _ => (ECut (e, c, {field = con env field, rest = rest}), loc)
adamc@909 702 end
adamc@909 703 in
adamc@909 704 case (#1 e, #1 c) of
adamc@909 705 (ERecord xes, CName x) =>
adamc@909 706 if List.all (fn ((CName _, _), _, _) => true | _ => false) xes then
adamc@909 707 (ERecord (List.filter (fn ((CName x', _), _, _) => x' <> x
adamc@909 708 | _ => raise Fail "Reduce: ECut") xes), loc)
adamc@909 709 else
adamc@909 710 default ()
adamc@909 711 | _ => default ()
adamc@909 712 end
adamc@509 713
adamc@909 714 | ECutMulti (e, c, {rest}) =>
adamc@909 715 let
adamc@909 716 val e = exp env e
adamc@909 717 val c = con env c
adamc@509 718
adamc@909 719 fun default () =
adamc@909 720 let
adamc@909 721 val rest = con env rest
adamc@909 722 in
adamc@909 723 case #1 rest of
adamc@909 724 CRecord (k, xcs) =>
adamc@909 725 let
adamc@909 726 val (xes, _) = ListUtil.foldlMap (doPart e) [] xcs
adamc@909 727 in
adamc@909 728 exp (deKnown env) (ERecord xes, loc)
adamc@909 729 end
adamc@909 730 | _ => (ECutMulti (e, c, {rest = rest}), loc)
adamc@909 731 end
adamc@909 732 in
adamc@909 733 case (#1 e, #1 c) of
adamc@909 734 (ERecord xes, CRecord (_, xcs)) =>
adamc@909 735 if List.all (fn ((CName _, _), _, _) => true | _ => false) xes
adamc@909 736 andalso List.all (fn ((CName _, _), _) => true | _ => false) xcs then
adamc@909 737 (ERecord (List.filter (fn ((CName x', _), _, _) =>
adamc@909 738 List.all (fn ((CName x, _), _) => x' <> x
adamc@909 739 | _ => raise Fail "Reduce: ECutMulti [1]") xcs
adamc@909 740 | _ => raise Fail "Reduce: ECutMulti [2]") xes), loc)
adamc@909 741 else
adamc@909 742 default ()
adamc@909 743 | _ => default ()
adamc@909 744 end
adamc@823 745
adamc@909 746 | ECase (_, [((PRecord [], _), e)], _) => exp env e
adamc@909 747 | ECase (_, [((PWild, _), e)], _) => exp env e
adamc@509 748
adamc@909 749 | ECase (e, pes, {disc, result}) =>
adamc@909 750 let
adamc@909 751 fun patBinds (p, _) =
adamc@909 752 case p of
adamc@909 753 PWild => 0
adamc@909 754 | PVar _ => 1
adamc@909 755 | PPrim _ => 0
adamc@909 756 | PCon (_, _, _, NONE) => 0
adamc@909 757 | PCon (_, _, _, SOME p) => patBinds p
adamc@909 758 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
adamc@509 759
adamc@909 760 fun pat (all as (p, loc)) =
adamc@909 761 case p of
adamc@909 762 PWild => all
adamc@909 763 | PVar (x, t) => (PVar (x, con env t), loc)
adamc@909 764 | PPrim _ => all
adamc@909 765 | PCon (dk, pc, cs, po) =>
adamc@909 766 (PCon (dk, patCon pc, map (con env) cs, Option.map pat po), loc)
adamc@909 767 | PRecord xpts => (PRecord (map (fn (x, p, t) => (x, pat p, con env t)) xpts), loc)
adamc@930 768
adamc@930 769 fun push () =
adamc@930 770 (ECase (exp env e,
adamc@930 771 map (fn (p, e) => (pat p,
adamc@930 772 exp (List.tabulate (patBinds p,
adamc@930 773 fn _ => UnknownE) @ env) e))
adamc@930 774 pes, {disc = con env disc, result = con env result}), loc)
adamc@930 775
adamc@930 776 fun search pes =
adamc@930 777 case pes of
adamc@930 778 [] => push ()
adamc@930 779 | (p, body) :: pes =>
adamc@930 780 case match (env, p, e) of
adamc@930 781 No => search pes
adamc@930 782 | Maybe => push ()
adamc@930 783 | Yes env' => exp env' body
adamc@909 784 in
adamc@930 785 search pes
adamc@909 786 end
adamc@509 787
adamc@909 788 | EWrite e => (EWrite (exp env e), loc)
adamc@909 789 | EClosure (n, es) => (EClosure (n, map (exp env) es), loc)
adamc@607 790
adamc@910 791 | ELet (x, t, e1, e2) =>
adamc@910 792 (ELet (x, con env t, exp env e1, exp (UnknownE :: env) e2), loc)
adamc@909 793
adamc@909 794 | EServerCall (n, es, e, t1, t2) => (EServerCall (n, map (exp env) es, exp env e,
adamc@909 795 con env t1, con env t2), loc)
adamc@954 796 | ETailCall (n, es, e, t1, t2) => (ETailCall (n, map (exp env) es, exp env e,
adamc@954 797 con env t1, con env t2), loc)
adamc@909 798 in
adamc@909 799 (*if dangling (edepth' (deKnown env)) r then
adamc@909 800 (Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 801 ("r", CorePrint.p_exp CoreEnv.empty r)];
adamc@909 802 raise Fail "!!")
adamc@909 803 else
adamc@909 804 ();*)
adamc@909 805 r
adamc@909 806 end
adamc@417 807 in
adamc@626 808 {kind = kind, con = con, exp = exp}
adamc@417 809 end
adamc@21 810
adamc@626 811 fun kind namedC env k = #kind (kindConAndExp (namedC, IM.empty)) env k
adamc@626 812 fun con namedC env c = #con (kindConAndExp (namedC, IM.empty)) env c
adamc@626 813 fun exp (namedC, namedE) env e = #exp (kindConAndExp (namedC, namedE)) env e
adamc@20 814
adamc@508 815 fun reduce file =
adamc@508 816 let
adamc@508 817 fun doDecl (d as (_, loc), st as (namedC, namedE)) =
adamc@508 818 case #1 d of
adamc@508 819 DCon (x, n, k, c) =>
adamc@508 820 let
adamc@626 821 val k = kind namedC [] k
adamc@509 822 val c = con namedC [] c
adamc@508 823 in
adamc@508 824 ((DCon (x, n, k, c), loc),
adamc@508 825 (IM.insert (namedC, n, c), namedE))
adamc@508 826 end
adamc@807 827 | DDatatype dts =>
adamc@807 828 ((DDatatype (map (fn (x, n, ps, cs) =>
adamc@807 829 let
adamc@807 830 val env = map (fn _ => UnknownC) ps
adamc@807 831 in
adamc@807 832 (x, n, ps, map (fn (x, n, co) => (x, n, Option.map (con namedC env) co)) cs)
adamc@807 833 end) dts), loc),
adamc@807 834 st)
adamc@508 835 | DVal (x, n, t, e, s) =>
adamc@508 836 let
adamc@509 837 val t = con namedC [] t
adamc@509 838 val e = exp (namedC, namedE) [] e
adamc@508 839 in
adamc@508 840 ((DVal (x, n, t, e, s), loc),
adamc@508 841 (namedC, IM.insert (namedE, n, e)))
adamc@508 842 end
adamc@508 843 | DValRec vis =>
adamc@910 844 ((DValRec (map (fn (x, n, t, e, s) => (x, n, con namedC [] t,
adamc@910 845 exp (namedC, namedE) [] e, s)) vis), loc),
adamc@508 846 st)
adamc@508 847 | DExport _ => (d, st)
adamc@707 848 | DTable (s, n, c, s', pe, pc, ce, cc) => ((DTable (s, n, con namedC [] c, s',
adamc@707 849 exp (namedC, namedE) [] pe,
adamc@707 850 con namedC [] pc,
adamc@707 851 exp (namedC, namedE) [] ce,
adamc@707 852 con namedC [] cc), loc), st)
adamc@508 853 | DSequence _ => (d, st)
adamc@754 854 | DView (s, n, s', e, c) => ((DView (s, n, s', exp (namedC, namedE) [] e, con namedC [] c), loc), st)
adamc@508 855 | DDatabase _ => (d, st)
adamc@509 856 | DCookie (s, n, c, s') => ((DCookie (s, n, con namedC [] c, s'), loc), st)
adamc@720 857 | DStyle (s, n, s') => ((DStyle (s, n, s'), loc), st)
adamc@20 858
adamc@508 859 val (file, _) = ListUtil.foldlMap doDecl (IM.empty, IM.empty) file
adamc@508 860 in
adamc@508 861 file
adamc@508 862 end
adamc@20 863
adamc@20 864 end