annotate src/reduce.sml @ 920:7accd4546cf9

Fix memmem() signature; fix mono_reduce environment bookkeeping
author Adam Chlipala <adamc@hcoop.net>
date Tue, 08 Sep 2009 20:12:17 -0400
parents 8e540df3294d
children 51bc7681c47e
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@626 104 fun kindConAndExp (namedC, namedE) =
adamc@508 105 let
adamc@626 106 fun kind env (all as (k, loc)) =
adamc@626 107 case k of
adamc@626 108 KType => all
adamc@626 109 | KArrow (k1, k2) => (KArrow (kind env k1, kind env k2), loc)
adamc@626 110 | KName => all
adamc@626 111 | KRecord k => (KRecord (kind env k), loc)
adamc@626 112 | KUnit => all
adamc@626 113 | KTuple ks => (KTuple (map (kind env) ks), loc)
adamc@626 114
adamc@626 115 | KRel n =>
adamc@626 116 let
adamc@626 117 fun find (n', env, nudge, lift) =
adamc@626 118 case env of
adamc@626 119 [] => raise Fail "Reduce.kind: KRel"
adamc@626 120 | UnknownC :: rest => find (n', rest, nudge, lift)
adamc@626 121 | KnownC _ :: rest => find (n', rest, nudge, lift)
adamc@626 122 | UnknownE :: rest => find (n', rest, nudge, lift)
adamc@626 123 | KnownE _ :: rest => find (n', rest, nudge, lift)
adamc@626 124 | Lift (lift', _, _) :: rest => find (n', rest, nudge + lift', lift + lift')
adamc@626 125 | UnknownK :: rest =>
adamc@626 126 if n' = 0 then
adamc@626 127 (KRel (n + nudge), loc)
adamc@626 128 else
adamc@626 129 find (n' - 1, rest, nudge, lift + 1)
adamc@626 130 | KnownK k :: rest =>
adamc@626 131 if n' = 0 then
adamc@626 132 kind (Lift (lift, 0, 0) :: rest) k
adamc@626 133 else
adamc@626 134 find (n' - 1, rest, nudge - 1, lift)
adamc@626 135 in
adamc@626 136 find (n, env, 0, 0)
adamc@626 137 end
adamc@626 138 | KFun (x, k) => (KFun (x, kind (UnknownK :: env) k), loc)
adamc@626 139
adamc@508 140 fun con env (all as (c, loc)) =
adamc@510 141 ((*Print.prefaces "con" [("c", CorePrint.p_con CoreEnv.empty all)];*)
adamc@508 142 case c of
adamc@508 143 TFun (c1, c2) => (TFun (con env c1, con env c2), loc)
adamc@626 144 | TCFun (x, k, c2) => (TCFun (x, kind env k, con (UnknownC :: env) c2), loc)
adamc@626 145 | TKFun (x, c2) => (TKFun (x, con (UnknownK :: env) c2), loc)
adamc@508 146 | TRecord c => (TRecord (con env c), loc)
adamc@215 147
adamc@508 148 | CRel n =>
adamc@508 149 let
adamc@626 150 fun find (n', env, nudge, liftK, liftC) =
adamc@510 151 case env of
adamc@510 152 [] => raise Fail "Reduce.con: CRel"
adamc@626 153 | UnknownK :: rest => find (n', rest, nudge, liftK + 1, liftC)
adamc@626 154 | KnownK _ :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 155 | UnknownE :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 156 | KnownE _ :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 157 | Lift (liftK', liftC', _) :: rest => find (n', rest, nudge + liftC',
adamc@626 158 liftK + liftK', liftC + liftC')
adamc@510 159 | UnknownC :: rest =>
adamc@510 160 if n' = 0 then
adamc@510 161 (CRel (n + nudge), loc)
adamc@510 162 else
adamc@626 163 find (n' - 1, rest, nudge, liftK, liftC + 1)
adamc@510 164 | KnownC c :: rest =>
adamc@510 165 if n' = 0 then
adamc@626 166 con (Lift (liftK, liftC, 0) :: rest) c
adamc@510 167 else
adamc@626 168 find (n' - 1, rest, nudge - 1, liftK, liftC)
adamc@508 169 in
adamc@510 170 (*print (Int.toString n ^ ": " ^ e2s env ^ "\n");*)
adamc@626 171 find (n, env, 0, 0, 0)
adamc@508 172 end
adamc@508 173 | CNamed n =>
adamc@508 174 (case IM.find (namedC, n) of
adamc@508 175 NONE => all
adamc@508 176 | SOME c => c)
adamc@508 177 | CFfi _ => all
adamc@508 178 | CApp (c1, c2) =>
adamc@508 179 let
adamc@508 180 val c1 = con env c1
adamc@508 181 val c2 = con env c2
adamc@508 182 in
adamc@508 183 case #1 c1 of
adamc@508 184 CAbs (_, _, b) =>
adamc@510 185 con (KnownC c2 :: deKnown env) b
adamc@215 186
adamc@621 187 | CApp ((CMap (dom, ran), _), f) =>
adamc@508 188 (case #1 c2 of
adamc@626 189 CRecord (_, []) => (CRecord (kind env ran, []), loc)
adamc@621 190 | CRecord (_, (x, c) :: rest) =>
adamc@510 191 con (deKnown env)
adamc@621 192 (CConcat ((CRecord (ran, [(x, (CApp (f, c), loc))]), loc),
adamc@626 193 (CApp (c1, (CRecord (kind env dom, rest), loc)), loc)), loc)
adamc@508 194 | _ => (CApp (c1, c2), loc))
adamc@20 195
adamc@508 196 | _ => (CApp (c1, c2), loc)
adamc@508 197 end
adamc@626 198 | CAbs (x, k, b) => (CAbs (x, kind env k, con (UnknownC :: env) b), loc)
adamc@626 199
adamc@626 200 | CKApp (c1, k) =>
adamc@626 201 let
adamc@626 202 val c1 = con env c1
adamc@626 203 in
adamc@626 204 case #1 c1 of
adamc@626 205 CKAbs (_, b) =>
adamc@626 206 con (KnownK k :: deKnown env) b
adamc@626 207
adamc@626 208 | _ => (CKApp (c1, kind env k), loc)
adamc@626 209 end
adamc@626 210 | CKAbs (x, b) => (CKAbs (x, con (UnknownK :: env) b), loc)
adamc@20 211
adamc@508 212 | CName _ => all
adamc@21 213
adamc@626 214 | CRecord (k, xcs) => (CRecord (kind env k, map (fn (x, c) => (con env x, con env c)) xcs), loc)
adamc@508 215 | CConcat (c1, c2) =>
adamc@508 216 let
adamc@508 217 val c1 = con env c1
adamc@508 218 val c2 = con env c2
adamc@508 219 in
adamc@508 220 case (#1 c1, #1 c2) of
adamc@508 221 (CRecord (k, xcs1), CRecord (_, xcs2)) =>
adamc@626 222 (CRecord (kind env k, xcs1 @ xcs2), loc)
adamc@508 223 | _ => (CConcat (c1, c2), loc)
adamc@508 224 end
adamc@626 225 | CMap (dom, ran) => (CMap (kind env dom, kind env ran), loc)
adamc@74 226
adamc@508 227 | CUnit => all
adamc@21 228
adamc@508 229 | CTuple cs => (CTuple (map (con env) cs), loc)
adamc@508 230 | CProj (c, n) =>
adamc@508 231 let
adamc@508 232 val c = con env c
adamc@508 233 in
adamc@508 234 case #1 c of
adamc@508 235 CTuple cs => List.nth (cs, n - 1)
adamc@508 236 | _ => (CProj (c, n), loc)
adamc@510 237 end)
adamc@22 238
adamc@509 239 fun patCon pc =
adamc@509 240 case pc of
adamc@509 241 PConVar _ => pc
adamc@509 242 | PConFfi {mod = m, datatyp, params, con = c, arg, kind} =>
adamc@509 243 PConFfi {mod = m, datatyp = datatyp, params = params, con = c,
adamc@509 244 arg = Option.map (con (map (fn _ => UnknownC) params)) arg,
adamc@509 245 kind = kind}
adamc@509 246
adamc@509 247
adamc@509 248 val k = (KType, ErrorMsg.dummySpan)
adamc@509 249 fun doPart e (this as (x, t), rest) =
adamc@509 250 ((x, (EField (e, x, {field = t, rest = (CRecord (k, rest), #2 t)}), #2 t), t),
adamc@509 251 this :: rest)
adamc@509 252
adamc@509 253 fun exp env (all as (e, loc)) =
adamc@909 254 let
adamc@909 255 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 256 ("env", Print.PD.string (e2s env))]*)
adamc@910 257 val () = if dangling (edepth env) all then
adamc@909 258 (Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 259 ("env", Print.PD.string (e2s env))];
adamc@909 260 raise Fail "!")
adamc@909 261 else
adamc@910 262 ()
adamc@509 263
adamc@909 264 val r = case e of
adamc@909 265 EPrim _ => all
adamc@909 266 | ERel n =>
adamc@909 267 let
adamc@909 268 fun find (n', env, nudge, liftK, liftC, liftE) =
adamc@909 269 case env of
adamc@909 270 [] => raise Fail ("Reduce.exp: ERel (" ^ ErrorMsg.spanToString loc ^ ")")
adamc@909 271 | UnknownK :: rest => find (n', rest, nudge, liftK + 1, liftC, liftE)
adamc@909 272 | KnownK _ :: rest => find (n', rest, nudge, liftK, liftC, liftE)
adamc@909 273 | UnknownC :: rest => find (n', rest, nudge, liftK, liftC + 1, liftE)
adamc@909 274 | KnownC _ :: rest => find (n', rest, nudge, liftK, liftC, liftE)
adamc@909 275 | Lift (liftK', liftC', liftE') :: rest =>
adamc@909 276 find (n', rest, nudge + liftE',
adamc@909 277 liftK + liftK', liftC + liftC', liftE + liftE')
adamc@909 278 | UnknownE :: rest =>
adamc@909 279 if n' = 0 then
adamc@909 280 (ERel (n + nudge), loc)
adamc@909 281 else
adamc@909 282 find (n' - 1, rest, nudge, liftK, liftC, liftE + 1)
adamc@909 283 | KnownE e :: rest =>
adamc@909 284 if n' = 0 then
adamc@909 285 ((*print "SUBSTITUTING\n";*)
adamc@909 286 exp (Lift (liftK, liftC, liftE) :: rest) e)
adamc@909 287 else
adamc@909 288 find (n' - 1, rest, nudge - 1, liftK, liftC, liftE)
adamc@909 289 in
adamc@909 290 (*print (Int.toString n ^ ": " ^ e2s env ^ "\n");*)
adamc@909 291 find (n, env, 0, 0, 0, 0)
adamc@909 292 end
adamc@909 293 | ENamed n =>
adamc@909 294 (case IM.find (namedE, n) of
adamc@909 295 NONE => all
adamc@909 296 | SOME e => e)
adamc@909 297 | ECon (dk, pc, cs, eo) => (ECon (dk, patCon pc,
adamc@909 298 map (con env) cs, Option.map (exp env) eo), loc)
adamc@909 299 | EFfi _ => all
adamc@909 300 | EFfiApp (m, f, es) => (EFfiApp (m, f, map (exp env) es), loc)
adamc@908 301
adamc@909 302 (*| EApp (
adamc@909 303 (EApp
adamc@909 304 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _), t1), _), t2), _),
adamc@909 305 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@909 306 (ECase (ed, pes, {disc, ...}), _)), _),
adamc@909 307 trans2) =>
adamc@909 308 let
adamc@909 309 val e' = (EFfi ("Basis", "bind"), loc)
adamc@909 310 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@909 311 val e' = (ECApp (e', t1), loc)
adamc@909 312 val e' = (ECApp (e', t2), loc)
adamc@909 313 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@908 314
adamc@909 315 val pes = map (fn (p, e) =>
adamc@909 316 let
adamc@909 317 val e' = (EApp (e', e), loc)
adamc@909 318 val e' = (EApp (e',
adamc@909 319 multiLiftExpInExp (E.patBindsN p)
adamc@909 320 trans2), loc)
adamc@909 321 val e' = exp env e'
adamc@909 322 in
adamc@909 323 (p, e')
adamc@909 324 end) pes
adamc@909 325 in
adamc@909 326 (ECase (exp env ed,
adamc@909 327 pes,
adamc@909 328 {disc = con env disc,
adamc@909 329 result = (CApp ((CFfi ("Basis", "transaction"), loc), con env t2), loc)}),
adamc@909 330 loc)
adamc@909 331 end*)
adamc@908 332
adamc@909 333 | EApp (e1, e2) =>
adamc@909 334 let
adamc@910 335 val env' = deKnown env
adamc@910 336
adamc@910 337 fun reassoc e =
adamc@910 338 case #1 e of
adamc@910 339 EApp
adamc@910 340 ((EApp
adamc@910 341 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _),
adamc@910 342 t1),
adamc@910 343 _), t2), _),
adamc@910 344 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@910 345 (EServerCall (n, es, (EAbs (_, _, _, ke), _), dom, ran), _)), _),
adamc@910 346 trans3) =>
adamc@910 347 let
adamc@910 348 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 349 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@910 350 val e' = (ECApp (e', dom), loc)
adamc@910 351 val e' = (ECApp (e', t2), loc)
adamc@910 352 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@910 353 val e' = (EApp (e', ke), loc)
adamc@910 354 val e' = (EApp (e', E.liftExpInExp 0 trans3), loc)
adamc@910 355 val e' = reassoc e'
adamc@910 356 val e' = (EAbs ("x", dom, t2, e'), loc)
adamc@910 357 val e' = (EServerCall (n, es, e', dom, t2), loc)
adamc@910 358 in
adamc@910 359 e'
adamc@910 360 end
adamc@910 361
adamc@910 362 | EApp
adamc@910 363 ((EApp
adamc@910 364 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _),
adamc@910 365 t1),
adamc@910 366 _), t2), _),
adamc@910 367 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@910 368 (EServerCall (n, es, ke, dom, ran), _)), _),
adamc@910 369 trans3) =>
adamc@910 370 let
adamc@910 371 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 372 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@910 373 val e' = (ECApp (e', dom), loc)
adamc@910 374 val e' = (ECApp (e', t2), loc)
adamc@910 375 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@910 376 val e' = (EApp (e', exp (UnknownE :: env')
adamc@910 377 (EApp (E.liftExpInExp 0 ke, (ERel 0, loc)), loc)),
adamc@910 378 loc)
adamc@910 379 val e' = (EApp (e', E.liftExpInExp 0 trans3), loc)
adamc@910 380 val e' = reassoc e'
adamc@910 381 val e' = (EAbs ("x", dom, t2, e'), loc)
adamc@910 382 val e' = (EServerCall (n, es, e', dom, t2), loc)
adamc@910 383 in
adamc@910 384 e'
adamc@910 385 end
adamc@910 386
adamc@910 387 | EApp
adamc@910 388 ((EApp
adamc@910 389 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), mt),
adamc@910 390 _), _), _), t3), _),
adamc@910 391 me), _),
adamc@910 392 (EApp ((EApp
adamc@910 393 ((EApp ((ECApp ((ECApp ((ECApp (
adamc@910 394 (EFfi ("Basis", "bind"), _), _), _),
adamc@910 395 t1), _), t2), _),
adamc@910 396 _), _),
adamc@910 397 trans1), _), (EAbs (_, _, _, trans2), _)), _)), _),
adamc@910 398 trans3) =>
adamc@910 399 let
adamc@910 400 val e'' = (EFfi ("Basis", "bind"), loc)
adamc@910 401 val e'' = (ECApp (e'', mt), loc)
adamc@910 402 val e'' = (ECApp (e'', t2), loc)
adamc@910 403 val e'' = (ECApp (e'', t3), loc)
adamc@910 404 val e'' = (EApp (e'', me), loc)
adamc@910 405 val e'' = (EApp (e'', trans2), loc)
adamc@910 406 val e'' = (EApp (e'', E.liftExpInExp 0 trans3), loc)
adamc@910 407 val e'' = reassoc e''
adamc@910 408 val e'' = (EAbs ("xb", t1, (CApp (mt, t3), loc), e''), loc)
adamc@910 409
adamc@910 410 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 411 val e' = (ECApp (e', mt), loc)
adamc@910 412 val e' = (ECApp (e', t1), loc)
adamc@910 413 val e' = (ECApp (e', t3), loc)
adamc@910 414 val e' = (EApp (e', me), loc)
adamc@910 415 val e' = (EApp (e', trans1), loc)
adamc@910 416 val e' = (EApp (e', e''), loc)
adamc@910 417 in
adamc@910 418 e'
adamc@910 419 end
adamc@910 420
adamc@910 421 | EApp
adamc@910 422 ((EApp
adamc@910 423 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), mt),
adamc@910 424 _), _), _), t3), _),
adamc@910 425 me), _),
adamc@910 426 (EApp ((EApp
adamc@910 427 ((EApp ((ECApp ((ECApp ((ECApp (
adamc@910 428 (EFfi ("Basis", "bind"), _), _), _),
adamc@910 429 t1), _), t2), _),
adamc@910 430 _), _),
adamc@910 431 trans1), _), trans2), _)), _),
adamc@910 432 trans3) =>
adamc@910 433 let
adamc@910 434 val e'' = (EFfi ("Basis", "bind"), loc)
adamc@910 435 val e'' = (ECApp (e'', mt), loc)
adamc@910 436 val e'' = (ECApp (e'', t2), loc)
adamc@910 437 val e'' = (ECApp (e'', t3), loc)
adamc@910 438 val e'' = (EApp (e'', me), loc)
adamc@910 439 val () = print "In2\n"
adamc@910 440 val e'' = (EApp (e'', exp (UnknownE :: env')
adamc@910 441 (EApp (E.liftExpInExp 0 trans2, (ERel 0, loc)),
adamc@910 442 loc)),
adamc@910 443 loc)
adamc@910 444 val () = print "Out2\n"
adamc@910 445 val e'' = (EApp (e'', E.liftExpInExp 0 trans3), loc)
adamc@910 446 val e'' = reassoc e''
adamc@910 447 val e'' = (EAbs ("xb", t1, (CApp (mt, t3), loc), e''), loc)
adamc@910 448
adamc@910 449 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 450 val e' = (ECApp (e', mt), loc)
adamc@910 451 val e' = (ECApp (e', t1), loc)
adamc@910 452 val e' = (ECApp (e', t3), loc)
adamc@910 453 val e' = (EApp (e', me), loc)
adamc@910 454 val e' = (EApp (e', trans1), loc)
adamc@910 455 val e' = (EApp (e', e''), loc)
adamc@910 456 in
adamc@910 457 e'
adamc@910 458 end
adamc@910 459
adamc@910 460 | _ => e
adamc@910 461
adamc@909 462 val e1 = exp env e1
adamc@909 463 val e2 = exp env e2
adamc@910 464 val e12 = reassoc (EApp (e1, e2), loc)
adamc@909 465 in
adamc@910 466 case #1 e12 of
adamc@910 467 EApp ((EAbs (_, _, _, b), _), e2) =>
adamc@909 468 ((*Print.preface ("Body", CorePrint.p_exp CoreEnv.empty b);*)
adamc@910 469 exp (KnownE e2 :: env') b)
adamc@910 470 (*| EApp
adamc@910 471 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _), t1),
adamc@910 472 _), t2), _),
adamc@910 473 _), _),
adamc@910 474 (EApp (
adamc@910 475 (EApp (
adamc@910 476 (ECApp (
adamc@910 477 (ECApp ((EFfi ("Basis", "return"), _), _), _),
adamc@910 478 _), _),
adamc@910 479 _), _), v), _)) =>
adamc@910 480 (ELet ("rv", con env t1, v,
adamc@910 481 exp (deKnown env) (EApp (E.liftExpInExp 0 e2, (ERel 0, loc)), loc)), loc)*)
adamc@910 482 (*| EApp
adamc@910 483 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _), t1),
adamc@910 484 _), t2), _),
adamc@910 485 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@910 486 (EServerCall (n, es, ke, dom, ran), _)) =>
adamc@910 487 let
adamc@910 488 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 489 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@910 490 val e' = (ECApp (e', dom), loc)
adamc@910 491 val e' = (ECApp (e', t2), loc)
adamc@910 492 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@910 493 val e' = (EApp (e', (EApp (E.liftExpInExp 0 ke, (ERel 0, loc)), loc)), loc)
adamc@910 494 val e' = (EApp (e', E.liftExpInExp 0 (exp env e2)), loc)
adamc@910 495 val e' = (EAbs ("x", dom, t2, e'), loc)
adamc@910 496 val e' = (EServerCall (n, es, e', dom, t2), loc)
adamc@910 497 val e' = exp (deKnown env) e'
adamc@910 498 in
adamc@910 499 (*Print.prefaces "SC" [("Old", CorePrint.p_exp CoreEnv.empty all),
adamc@910 500 ("New", CorePrint.p_exp CoreEnv.empty e')]*)
adamc@910 501 e'
adamc@910 502 end
adamc@910 503 | EApp
adamc@910 504 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), mt),
adamc@910 505 _), _), _), t3), _),
adamc@910 506 me), _),
adamc@910 507 (EApp ((EApp
adamc@910 508 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), _), _), _),
adamc@910 509 t1), _), t2), _),
adamc@910 510 _), _),
adamc@910 511 trans1), _), trans2), _)) =>
adamc@910 512 let
adamc@910 513 val e'' = (EFfi ("Basis", "bind"), loc)
adamc@910 514 val e'' = (ECApp (e'', mt), loc)
adamc@910 515 val e'' = (ECApp (e'', t2), loc)
adamc@910 516 val e'' = (ECApp (e'', t3), loc)
adamc@910 517 val e'' = (EApp (e'', me), loc)
adamc@910 518 val e'' = (EApp (e'', (EApp (E.liftExpInExp 0 trans2, (ERel 0, loc)), loc)), loc)
adamc@910 519 val e'' = (EApp (e'', E.liftExpInExp 0 e2), loc)
adamc@910 520 val e'' = (EAbs ("xb", t1, (CApp (mt, t3), loc), e''), loc)
adamc@910 521
adamc@910 522 val e' = (EFfi ("Basis", "bind"), loc)
adamc@910 523 val e' = (ECApp (e', mt), loc)
adamc@910 524 val e' = (ECApp (e', t1), loc)
adamc@910 525 val e' = (ECApp (e', t3), loc)
adamc@910 526 val e' = (EApp (e', me), loc)
adamc@910 527 val e' = (EApp (e', trans1), loc)
adamc@910 528 val e' = (EApp (e', e''), loc)
adamc@910 529 (*val () = Print.prefaces "Going in" [("e", CorePrint.p_exp CoreEnv.empty (e, loc)),
adamc@910 530 ("e1", CorePrint.p_exp CoreEnv.empty e1),
adamc@910 531 ("e'", CorePrint.p_exp CoreEnv.empty e')]*)
adamc@910 532 val ee' = exp (deKnown env) e'
adamc@910 533 val () = Print.prefaces "Coming out" [("ee'", CorePrint.p_exp CoreEnv.empty ee')]
adamc@910 534 in
adamc@910 535 (*Print.prefaces "Commute" [("Pre", CorePrint.p_exp CoreEnv.empty (e, loc)),
adamc@910 536 ("Mid", CorePrint.p_exp CoreEnv.empty e'),
adamc@910 537 ("env", Print.PD.string (e2s env)),
adamc@910 538 ("Post", CorePrint.p_exp CoreEnv.empty ee')];*)
adamc@910 539 ee'
adamc@910 540 end
adamc@910 541 | _ => (EApp (e1, exp env e2), loc)*)
adamc@910 542 | _ => e12
adamc@909 543 end
adamc@509 544
adamc@909 545 | EAbs (x, dom, ran, e) => (EAbs (x, con env dom, con env ran, exp (UnknownE :: env) e), loc)
adamc@509 546
adamc@909 547 | ECApp (e, c) =>
adamc@909 548 let
adamc@909 549 val e = exp env e
adamc@909 550 val c = con env c
adamc@909 551 in
adamc@909 552 case #1 e of
adamc@909 553 ECAbs (_, _, b) => exp (KnownC c :: deKnown env) b
adamc@909 554 | _ => (ECApp (e, c), loc)
adamc@909 555 end
adamc@626 556
adamc@909 557 | ECAbs (x, k, e) => (ECAbs (x, kind env k, exp (UnknownC :: env) e), loc)
adamc@626 558
adamc@909 559 | EKApp (e, k) =>
adamc@909 560 let
adamc@909 561 val e = exp env e
adamc@909 562 in
adamc@909 563 case #1 e of
adamc@909 564 EKAbs (_, b) => exp (KnownK k :: deKnown env) b
adamc@909 565 | _ => (EKApp (e, kind env k), loc)
adamc@909 566 end
adamc@509 567
adamc@909 568 | EKAbs (x, e) => (EKAbs (x, exp (UnknownK :: env) e), loc)
adamc@509 569
adamc@909 570 | ERecord xcs => (ERecord (map (fn (x, e, t) => (con env x, exp env e, con env t)) xcs), loc)
adamc@909 571 | EField (e, c, {field, rest}) =>
adamc@909 572 let
adamc@909 573 val e = exp env e
adamc@909 574 val c = con env c
adamc@509 575
adamc@909 576 fun default () = (EField (e, c, {field = con env field, rest = con env rest}), loc)
adamc@909 577 in
adamc@909 578 case (#1 e, #1 c) of
adamc@909 579 (ERecord xcs, CName x) =>
adamc@909 580 (case List.find (fn ((CName x', _), _, _) => x' = x | _ => false) xcs of
adamc@909 581 NONE => default ()
adamc@909 582 | SOME (_, e, _) => e)
adamc@909 583 | _ => default ()
adamc@909 584 end
adamc@509 585
adamc@909 586 | EConcat (e1, c1, e2, c2) =>
adamc@909 587 let
adamc@909 588 val e1 = exp env e1
adamc@909 589 val e2 = exp env e2
adamc@909 590 in
adamc@909 591 case (#1 e1, #1 e2) of
adamc@909 592 (ERecord xes1, ERecord xes2) => (ERecord (xes1 @ xes2), loc)
adamc@909 593 | _ =>
adamc@909 594 let
adamc@909 595 val c1 = con env c1
adamc@909 596 val c2 = con env c2
adamc@909 597 in
adamc@909 598 case (#1 c1, #1 c2) of
adamc@909 599 (CRecord (k, xcs1), CRecord (_, xcs2)) =>
adamc@909 600 let
adamc@909 601 val (xes1, rest) = ListUtil.foldlMap (doPart e1) [] xcs1
adamc@909 602 val (xes2, _) = ListUtil.foldlMap (doPart e2) rest xcs2
adamc@909 603 in
adamc@909 604 exp (deKnown env) (ERecord (xes1 @ xes2), loc)
adamc@909 605 end
adamc@909 606 | _ => (EConcat (e1, c1, e2, c2), loc)
adamc@909 607 end
adamc@909 608 end
adamc@509 609
adamc@909 610 | ECut (e, c, {field, rest}) =>
adamc@909 611 let
adamc@909 612 val e = exp env e
adamc@909 613 val c = con env c
adamc@509 614
adamc@909 615 fun default () =
adamc@909 616 let
adamc@909 617 val rest = con env rest
adamc@909 618 in
adamc@909 619 case #1 rest of
adamc@909 620 CRecord (k, xcs) =>
adamc@909 621 let
adamc@909 622 val (xes, _) = ListUtil.foldlMap (doPart e) [] xcs
adamc@909 623 in
adamc@909 624 exp (deKnown env) (ERecord xes, loc)
adamc@909 625 end
adamc@909 626 | _ => (ECut (e, c, {field = con env field, rest = rest}), loc)
adamc@909 627 end
adamc@909 628 in
adamc@909 629 case (#1 e, #1 c) of
adamc@909 630 (ERecord xes, CName x) =>
adamc@909 631 if List.all (fn ((CName _, _), _, _) => true | _ => false) xes then
adamc@909 632 (ERecord (List.filter (fn ((CName x', _), _, _) => x' <> x
adamc@909 633 | _ => raise Fail "Reduce: ECut") xes), loc)
adamc@909 634 else
adamc@909 635 default ()
adamc@909 636 | _ => default ()
adamc@909 637 end
adamc@509 638
adamc@909 639 | ECutMulti (e, c, {rest}) =>
adamc@909 640 let
adamc@909 641 val e = exp env e
adamc@909 642 val c = con env c
adamc@509 643
adamc@909 644 fun default () =
adamc@909 645 let
adamc@909 646 val rest = con env rest
adamc@909 647 in
adamc@909 648 case #1 rest of
adamc@909 649 CRecord (k, xcs) =>
adamc@909 650 let
adamc@909 651 val (xes, _) = ListUtil.foldlMap (doPart e) [] xcs
adamc@909 652 in
adamc@909 653 exp (deKnown env) (ERecord xes, loc)
adamc@909 654 end
adamc@909 655 | _ => (ECutMulti (e, c, {rest = rest}), loc)
adamc@909 656 end
adamc@909 657 in
adamc@909 658 case (#1 e, #1 c) of
adamc@909 659 (ERecord xes, CRecord (_, xcs)) =>
adamc@909 660 if List.all (fn ((CName _, _), _, _) => true | _ => false) xes
adamc@909 661 andalso List.all (fn ((CName _, _), _) => true | _ => false) xcs then
adamc@909 662 (ERecord (List.filter (fn ((CName x', _), _, _) =>
adamc@909 663 List.all (fn ((CName x, _), _) => x' <> x
adamc@909 664 | _ => raise Fail "Reduce: ECutMulti [1]") xcs
adamc@909 665 | _ => raise Fail "Reduce: ECutMulti [2]") xes), loc)
adamc@909 666 else
adamc@909 667 default ()
adamc@909 668 | _ => default ()
adamc@909 669 end
adamc@823 670
adamc@909 671 | ECase (_, [((PRecord [], _), e)], _) => exp env e
adamc@909 672 | ECase (_, [((PWild, _), e)], _) => exp env e
adamc@509 673
adamc@909 674 | ECase (e, pes, {disc, result}) =>
adamc@909 675 let
adamc@909 676 fun patBinds (p, _) =
adamc@909 677 case p of
adamc@909 678 PWild => 0
adamc@909 679 | PVar _ => 1
adamc@909 680 | PPrim _ => 0
adamc@909 681 | PCon (_, _, _, NONE) => 0
adamc@909 682 | PCon (_, _, _, SOME p) => patBinds p
adamc@909 683 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
adamc@509 684
adamc@909 685 fun pat (all as (p, loc)) =
adamc@909 686 case p of
adamc@909 687 PWild => all
adamc@909 688 | PVar (x, t) => (PVar (x, con env t), loc)
adamc@909 689 | PPrim _ => all
adamc@909 690 | PCon (dk, pc, cs, po) =>
adamc@909 691 (PCon (dk, patCon pc, map (con env) cs, Option.map pat po), loc)
adamc@909 692 | PRecord xpts => (PRecord (map (fn (x, p, t) => (x, pat p, con env t)) xpts), loc)
adamc@909 693 in
adamc@909 694 (ECase (exp env e,
adamc@909 695 map (fn (p, e) => (pat p,
adamc@909 696 exp (List.tabulate (patBinds p, fn _ => UnknownE) @ env) e))
adamc@909 697 pes, {disc = con env disc, result = con env result}), loc)
adamc@909 698 end
adamc@509 699
adamc@909 700 | EWrite e => (EWrite (exp env e), loc)
adamc@909 701 | EClosure (n, es) => (EClosure (n, map (exp env) es), loc)
adamc@607 702
adamc@910 703 | ELet (x, t, e1, e2) =>
adamc@910 704 (ELet (x, con env t, exp env e1, exp (UnknownE :: env) e2), loc)
adamc@909 705
adamc@909 706 | EServerCall (n, es, e, t1, t2) => (EServerCall (n, map (exp env) es, exp env e,
adamc@909 707 con env t1, con env t2), loc)
adamc@909 708 in
adamc@909 709 (*if dangling (edepth' (deKnown env)) r then
adamc@909 710 (Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 711 ("r", CorePrint.p_exp CoreEnv.empty r)];
adamc@909 712 raise Fail "!!")
adamc@909 713 else
adamc@909 714 ();*)
adamc@909 715 r
adamc@909 716 end
adamc@417 717 in
adamc@626 718 {kind = kind, con = con, exp = exp}
adamc@417 719 end
adamc@21 720
adamc@626 721 fun kind namedC env k = #kind (kindConAndExp (namedC, IM.empty)) env k
adamc@626 722 fun con namedC env c = #con (kindConAndExp (namedC, IM.empty)) env c
adamc@626 723 fun exp (namedC, namedE) env e = #exp (kindConAndExp (namedC, namedE)) env e
adamc@20 724
adamc@508 725 fun reduce file =
adamc@508 726 let
adamc@508 727 fun doDecl (d as (_, loc), st as (namedC, namedE)) =
adamc@508 728 case #1 d of
adamc@508 729 DCon (x, n, k, c) =>
adamc@508 730 let
adamc@626 731 val k = kind namedC [] k
adamc@509 732 val c = con namedC [] c
adamc@508 733 in
adamc@508 734 ((DCon (x, n, k, c), loc),
adamc@508 735 (IM.insert (namedC, n, c), namedE))
adamc@508 736 end
adamc@807 737 | DDatatype dts =>
adamc@807 738 ((DDatatype (map (fn (x, n, ps, cs) =>
adamc@807 739 let
adamc@807 740 val env = map (fn _ => UnknownC) ps
adamc@807 741 in
adamc@807 742 (x, n, ps, map (fn (x, n, co) => (x, n, Option.map (con namedC env) co)) cs)
adamc@807 743 end) dts), loc),
adamc@807 744 st)
adamc@508 745 | DVal (x, n, t, e, s) =>
adamc@508 746 let
adamc@509 747 val t = con namedC [] t
adamc@509 748 val e = exp (namedC, namedE) [] e
adamc@508 749 in
adamc@508 750 ((DVal (x, n, t, e, s), loc),
adamc@508 751 (namedC, IM.insert (namedE, n, e)))
adamc@508 752 end
adamc@508 753 | DValRec vis =>
adamc@910 754 ((DValRec (map (fn (x, n, t, e, s) => (x, n, con namedC [] t,
adamc@910 755 exp (namedC, namedE) [] e, s)) vis), loc),
adamc@508 756 st)
adamc@508 757 | DExport _ => (d, st)
adamc@707 758 | DTable (s, n, c, s', pe, pc, ce, cc) => ((DTable (s, n, con namedC [] c, s',
adamc@707 759 exp (namedC, namedE) [] pe,
adamc@707 760 con namedC [] pc,
adamc@707 761 exp (namedC, namedE) [] ce,
adamc@707 762 con namedC [] cc), loc), st)
adamc@508 763 | DSequence _ => (d, st)
adamc@754 764 | DView (s, n, s', e, c) => ((DView (s, n, s', exp (namedC, namedE) [] e, con namedC [] c), loc), st)
adamc@508 765 | DDatabase _ => (d, st)
adamc@509 766 | DCookie (s, n, c, s') => ((DCookie (s, n, con namedC [] c, s'), loc), st)
adamc@720 767 | DStyle (s, n, s') => ((DStyle (s, n, s'), loc), st)
adamc@20 768
adamc@508 769 val (file, _) = ListUtil.foldlMap doDecl (IM.empty, IM.empty) file
adamc@508 770 in
adamc@508 771 file
adamc@508 772 end
adamc@20 773
adamc@20 774 end