annotate src/reduce.sml @ 1179:c58453683bbb

Dead code elimination in Reduce code
author Adam Chlipala <adamc@hcoop.net>
date Thu, 04 Mar 2010 16:59:13 -0500
parents 51e596feec37
children 618f9f458da9
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@1016 34 structure IS = IntBinarySet
adamc@508 35 structure IM = IntBinaryMap
adamc@20 36
adamc@908 37 structure E = CoreEnv
adamc@908 38
adamc@908 39 fun multiLiftExpInExp n e =
adamc@908 40 if n = 0 then
adamc@908 41 e
adamc@908 42 else
adamc@908 43 multiLiftExpInExp (n - 1) (E.liftExpInExp 0 e)
adamc@908 44
adamc@1179 45 val count = CoreUtil.Exp.foldB {kind = fn (_, _, c) => c,
adamc@1179 46 con = fn (_, _, c) => c,
adamc@1179 47 exp = fn (x, e, c) =>
adamc@1179 48 case e of
adamc@1179 49 ERel x' => if x = x' then c + 1 else c
adamc@1179 50 | _ => c,
adamc@1179 51 bind = fn (x, b) =>
adamc@1179 52 case b of
adamc@1179 53 CoreUtil.Exp.RelE _ => x+1
adamc@1179 54 | _ => x} 0 0
adamc@1179 55
adamc@909 56 val dangling =
adamc@909 57 CoreUtil.Exp.existsB {kind = fn _ => false,
adamc@909 58 con = fn _ => false,
adamc@909 59 exp = fn (n, e) =>
adamc@909 60 case e of
adamc@909 61 ERel n' => n' >= n
adamc@909 62 | _ => false,
adamc@909 63 bind = fn (n, b) =>
adamc@909 64 case b of
adamc@909 65 CoreUtil.Exp.RelE _ => n + 1
adamc@909 66 | _ => n}
adamc@909 67
adamc@508 68 datatype env_item =
adamc@626 69 UnknownK
adamc@626 70 | KnownK of kind
adamc@626 71
adamc@626 72 | UnknownC
adamc@508 73 | KnownC of con
adamc@21 74
adamc@508 75 | UnknownE
adamc@508 76 | KnownE of exp
adamc@20 77
adamc@626 78 | Lift of int * int * int
adamc@20 79
adamc@909 80 val edepth = foldl (fn (UnknownE, n) => n + 1
adamc@909 81 | (KnownE _, n) => n + 1
adamc@909 82 | (_, n) => n) 0
adamc@909 83
adamc@909 84 val edepth' = foldl (fn (UnknownE, n) => n + 1
adamc@909 85 | (KnownE _, n) => n + 1
adamc@909 86 | (Lift (_, _, n'), n) => n + n'
adamc@909 87 | (_, n) => n) 0
adamc@909 88
adamc@508 89 type env = env_item list
adamc@20 90
adamc@510 91 fun ei2s ei =
adamc@510 92 case ei of
adamc@626 93 UnknownK => "UK"
adamc@626 94 | KnownK _ => "KK"
adamc@626 95 | UnknownC => "UC"
adamc@510 96 | KnownC _ => "KC"
adamc@510 97 | UnknownE => "UE"
adamc@510 98 | KnownE _ => "KE"
adamc@626 99 | Lift (_, n1, n2) => "(" ^ Int.toString n1 ^ ", " ^ Int.toString n2 ^ ")"
adamc@510 100
adamc@510 101 fun e2s env = String.concatWith " " (map ei2s env)
adamc@510 102
adamc@909 103 (*val deKnown = List.filter (fn KnownC _ => false
adamc@510 104 | KnownE _ => false
adamc@626 105 | KnownK _ => false
adamc@909 106 | _ => true)*)
adamc@909 107
adamc@909 108 val deKnown = ListUtil.mapConcat (fn KnownC _ => []
adamc@909 109 | KnownE _ => []
adamc@909 110 | KnownK _ => []
adamc@909 111 | Lift (nk, nc, ne) => List.tabulate (nk, fn _ => UnknownK)
adamc@909 112 @ List.tabulate (nc, fn _ => UnknownC)
adamc@909 113 @ List.tabulate (ne, fn _ => UnknownE)
adamc@909 114 | x => [x])
adamc@510 115
adamc@930 116 datatype result = Yes of env | No | Maybe
adamc@930 117
adamc@930 118 fun match (env, p : pat, e : exp) =
adamc@942 119 let
adamc@942 120 val baseline = length env
adamc@930 121
adamc@942 122 fun match (env, p, e) =
adamc@942 123 case (#1 p, #1 e) of
adamc@942 124 (PWild, _) => Yes env
adamc@942 125 | (PVar (x, t), _) => Yes (KnownE (multiLiftExpInExp (length env - baseline) e) :: env)
adamc@930 126
adamc@942 127 | (PPrim p, EPrim p') =>
adamc@942 128 if Prim.equal (p, p') then
adamc@942 129 Yes env
adamc@942 130 else
adamc@942 131 No
adamc@930 132
adamc@942 133 | (PCon (_, PConVar n1, _, NONE), ECon (_, PConVar n2, _, NONE)) =>
adamc@942 134 if n1 = n2 then
adamc@942 135 Yes env
adamc@942 136 else
adamc@942 137 No
adamc@930 138
adamc@942 139 | (PCon (_, PConVar n1, _, SOME p), ECon (_, PConVar n2, _, SOME e)) =>
adamc@942 140 if n1 = n2 then
adamc@942 141 match (env, p, e)
adamc@942 142 else
adamc@942 143 No
adamc@930 144
adamc@942 145 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, _, NONE),
adamc@942 146 ECon (_, PConFfi {mod = m2, con = con2, ...}, _, NONE)) =>
adamc@942 147 if m1 = m2 andalso con1 = con2 then
adamc@942 148 Yes env
adamc@942 149 else
adamc@942 150 No
adamc@930 151
adamc@942 152 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, _, SOME ep),
adamc@942 153 ECon (_, PConFfi {mod = m2, con = con2, ...}, _, SOME e)) =>
adamc@942 154 if m1 = m2 andalso con1 = con2 then
adamc@942 155 match (env, p, e)
adamc@942 156 else
adamc@942 157 No
adamc@930 158
adamc@942 159 | (PRecord xps, ERecord xes) =>
adamc@942 160 if List.exists (fn ((CName _, _), _, _) => false
adamc@942 161 | _ => true) xes then
adamc@942 162 Maybe
adamc@942 163 else
adamc@942 164 let
adamc@942 165 fun consider (xps, env) =
adamc@942 166 case xps of
adamc@942 167 [] => Yes env
adamc@942 168 | (x, p, _) :: rest =>
adamc@942 169 case List.find (fn ((CName x', _), _, _) => x' = x
adamc@942 170 | _ => false) xes of
adamc@942 171 NONE => No
adamc@942 172 | SOME (_, e, _) =>
adamc@942 173 case match (env, p, e) of
adamc@942 174 No => No
adamc@942 175 | Maybe => Maybe
adamc@942 176 | Yes env => consider (rest, env)
adamc@942 177 in
adamc@942 178 consider (xps, env)
adamc@942 179 end
adamc@942 180
adamc@942 181 | _ => Maybe
adamc@942 182 in
adamc@942 183 match (env, p, e)
adamc@942 184 end
adamc@930 185
adamc@626 186 fun kindConAndExp (namedC, namedE) =
adamc@508 187 let
adamc@626 188 fun kind env (all as (k, loc)) =
adamc@626 189 case k of
adamc@626 190 KType => all
adamc@626 191 | KArrow (k1, k2) => (KArrow (kind env k1, kind env k2), loc)
adamc@626 192 | KName => all
adamc@626 193 | KRecord k => (KRecord (kind env k), loc)
adamc@626 194 | KUnit => all
adamc@626 195 | KTuple ks => (KTuple (map (kind env) ks), loc)
adamc@626 196
adamc@626 197 | KRel n =>
adamc@626 198 let
adamc@626 199 fun find (n', env, nudge, lift) =
adamc@626 200 case env of
adamc@626 201 [] => raise Fail "Reduce.kind: KRel"
adamc@626 202 | UnknownC :: rest => find (n', rest, nudge, lift)
adamc@626 203 | KnownC _ :: rest => find (n', rest, nudge, lift)
adamc@626 204 | UnknownE :: rest => find (n', rest, nudge, lift)
adamc@626 205 | KnownE _ :: rest => find (n', rest, nudge, lift)
adamc@626 206 | Lift (lift', _, _) :: rest => find (n', rest, nudge + lift', lift + lift')
adamc@626 207 | UnknownK :: rest =>
adamc@626 208 if n' = 0 then
adamc@626 209 (KRel (n + nudge), loc)
adamc@626 210 else
adamc@626 211 find (n' - 1, rest, nudge, lift + 1)
adamc@626 212 | KnownK k :: rest =>
adamc@626 213 if n' = 0 then
adamc@626 214 kind (Lift (lift, 0, 0) :: rest) k
adamc@626 215 else
adamc@626 216 find (n' - 1, rest, nudge - 1, lift)
adamc@626 217 in
adamc@626 218 find (n, env, 0, 0)
adamc@626 219 end
adamc@626 220 | KFun (x, k) => (KFun (x, kind (UnknownK :: env) k), loc)
adamc@626 221
adamc@508 222 fun con env (all as (c, loc)) =
adamc@510 223 ((*Print.prefaces "con" [("c", CorePrint.p_con CoreEnv.empty all)];*)
adamc@508 224 case c of
adamc@508 225 TFun (c1, c2) => (TFun (con env c1, con env c2), loc)
adamc@626 226 | TCFun (x, k, c2) => (TCFun (x, kind env k, con (UnknownC :: env) c2), loc)
adamc@626 227 | TKFun (x, c2) => (TKFun (x, con (UnknownK :: env) c2), loc)
adamc@508 228 | TRecord c => (TRecord (con env c), loc)
adamc@215 229
adamc@508 230 | CRel n =>
adamc@508 231 let
adamc@626 232 fun find (n', env, nudge, liftK, liftC) =
adamc@510 233 case env of
adamc@510 234 [] => raise Fail "Reduce.con: CRel"
adamc@626 235 | UnknownK :: rest => find (n', rest, nudge, liftK + 1, liftC)
adamc@626 236 | KnownK _ :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 237 | UnknownE :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 238 | KnownE _ :: rest => find (n', rest, nudge, liftK, liftC)
adamc@626 239 | Lift (liftK', liftC', _) :: rest => find (n', rest, nudge + liftC',
adamc@626 240 liftK + liftK', liftC + liftC')
adamc@510 241 | UnknownC :: rest =>
adamc@510 242 if n' = 0 then
adamc@510 243 (CRel (n + nudge), loc)
adamc@510 244 else
adamc@626 245 find (n' - 1, rest, nudge, liftK, liftC + 1)
adamc@510 246 | KnownC c :: rest =>
adamc@510 247 if n' = 0 then
adamc@626 248 con (Lift (liftK, liftC, 0) :: rest) c
adamc@510 249 else
adamc@626 250 find (n' - 1, rest, nudge - 1, liftK, liftC)
adamc@508 251 in
adamc@510 252 (*print (Int.toString n ^ ": " ^ e2s env ^ "\n");*)
adamc@626 253 find (n, env, 0, 0, 0)
adamc@508 254 end
adamc@508 255 | CNamed n =>
adamc@508 256 (case IM.find (namedC, n) of
adamc@508 257 NONE => all
adamc@508 258 | SOME c => c)
adamc@508 259 | CFfi _ => all
adamc@508 260 | CApp (c1, c2) =>
adamc@508 261 let
adamc@508 262 val c1 = con env c1
adamc@508 263 val c2 = con env c2
adamc@508 264 in
adamc@508 265 case #1 c1 of
adamc@508 266 CAbs (_, _, b) =>
adamc@510 267 con (KnownC c2 :: deKnown env) b
adamc@215 268
adamc@621 269 | CApp ((CMap (dom, ran), _), f) =>
adamc@508 270 (case #1 c2 of
adamc@626 271 CRecord (_, []) => (CRecord (kind env ran, []), loc)
adamc@621 272 | CRecord (_, (x, c) :: rest) =>
adamc@510 273 con (deKnown env)
adamc@621 274 (CConcat ((CRecord (ran, [(x, (CApp (f, c), loc))]), loc),
adamc@626 275 (CApp (c1, (CRecord (kind env dom, rest), loc)), loc)), loc)
adamc@508 276 | _ => (CApp (c1, c2), loc))
adamc@20 277
adamc@508 278 | _ => (CApp (c1, c2), loc)
adamc@508 279 end
adamc@626 280 | CAbs (x, k, b) => (CAbs (x, kind env k, con (UnknownC :: env) b), loc)
adamc@626 281
adamc@626 282 | CKApp (c1, k) =>
adamc@626 283 let
adamc@626 284 val c1 = con env c1
adamc@626 285 in
adamc@626 286 case #1 c1 of
adamc@626 287 CKAbs (_, b) =>
adamc@626 288 con (KnownK k :: deKnown env) b
adamc@626 289
adamc@626 290 | _ => (CKApp (c1, kind env k), loc)
adamc@626 291 end
adamc@626 292 | CKAbs (x, b) => (CKAbs (x, con (UnknownK :: env) b), loc)
adamc@20 293
adamc@508 294 | CName _ => all
adamc@21 295
adamc@626 296 | CRecord (k, xcs) => (CRecord (kind env k, map (fn (x, c) => (con env x, con env c)) xcs), loc)
adamc@508 297 | CConcat (c1, c2) =>
adamc@508 298 let
adamc@508 299 val c1 = con env c1
adamc@508 300 val c2 = con env c2
adamc@508 301 in
adamc@508 302 case (#1 c1, #1 c2) of
adamc@508 303 (CRecord (k, xcs1), CRecord (_, xcs2)) =>
adamc@626 304 (CRecord (kind env k, xcs1 @ xcs2), loc)
adamc@1122 305 | (CRecord (_, []), _) => c2
adamc@1122 306 | (_, CRecord (_, [])) => c1
adamc@508 307 | _ => (CConcat (c1, c2), loc)
adamc@508 308 end
adamc@626 309 | CMap (dom, ran) => (CMap (kind env dom, kind env ran), loc)
adamc@74 310
adamc@508 311 | CUnit => all
adamc@21 312
adamc@508 313 | CTuple cs => (CTuple (map (con env) cs), loc)
adamc@508 314 | CProj (c, n) =>
adamc@508 315 let
adamc@508 316 val c = con env c
adamc@508 317 in
adamc@508 318 case #1 c of
adamc@508 319 CTuple cs => List.nth (cs, n - 1)
adamc@508 320 | _ => (CProj (c, n), loc)
adamc@510 321 end)
adamc@22 322
adamc@509 323 fun patCon pc =
adamc@509 324 case pc of
adamc@509 325 PConVar _ => pc
adamc@509 326 | PConFfi {mod = m, datatyp, params, con = c, arg, kind} =>
adamc@509 327 PConFfi {mod = m, datatyp = datatyp, params = params, con = c,
adamc@509 328 arg = Option.map (con (map (fn _ => UnknownC) params)) arg,
adamc@509 329 kind = kind}
adamc@509 330
adamc@509 331
adamc@509 332 val k = (KType, ErrorMsg.dummySpan)
adamc@509 333 fun doPart e (this as (x, t), rest) =
adamc@509 334 ((x, (EField (e, x, {field = t, rest = (CRecord (k, rest), #2 t)}), #2 t), t),
adamc@509 335 this :: rest)
adamc@509 336
adamc@509 337 fun exp env (all as (e, loc)) =
adamc@909 338 let
adamc@909 339 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 340 ("env", Print.PD.string (e2s env))]*)
adamc@1176 341 (*val () = if dangling (edepth env) all then
adamc@909 342 (Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 343 ("env", Print.PD.string (e2s env))];
adamc@909 344 raise Fail "!")
adamc@909 345 else
adamc@1176 346 ()*)
adamc@509 347
adamc@909 348 val r = case e of
adamc@909 349 EPrim _ => all
adamc@909 350 | ERel n =>
adamc@909 351 let
adamc@909 352 fun find (n', env, nudge, liftK, liftC, liftE) =
adamc@909 353 case env of
adamc@909 354 [] => raise Fail ("Reduce.exp: ERel (" ^ ErrorMsg.spanToString loc ^ ")")
adamc@909 355 | UnknownK :: rest => find (n', rest, nudge, liftK + 1, liftC, liftE)
adamc@909 356 | KnownK _ :: rest => find (n', rest, nudge, liftK, liftC, liftE)
adamc@909 357 | UnknownC :: rest => find (n', rest, nudge, liftK, liftC + 1, liftE)
adamc@909 358 | KnownC _ :: rest => find (n', rest, nudge, liftK, liftC, liftE)
adamc@909 359 | Lift (liftK', liftC', liftE') :: rest =>
adamc@909 360 find (n', rest, nudge + liftE',
adamc@909 361 liftK + liftK', liftC + liftC', liftE + liftE')
adamc@909 362 | UnknownE :: rest =>
adamc@909 363 if n' = 0 then
adamc@909 364 (ERel (n + nudge), loc)
adamc@909 365 else
adamc@909 366 find (n' - 1, rest, nudge, liftK, liftC, liftE + 1)
adamc@909 367 | KnownE e :: rest =>
adamc@909 368 if n' = 0 then
adamc@909 369 ((*print "SUBSTITUTING\n";*)
adamc@909 370 exp (Lift (liftK, liftC, liftE) :: rest) e)
adamc@909 371 else
adamc@909 372 find (n' - 1, rest, nudge - 1, liftK, liftC, liftE)
adamc@909 373 in
adamc@909 374 (*print (Int.toString n ^ ": " ^ e2s env ^ "\n");*)
adamc@909 375 find (n, env, 0, 0, 0, 0)
adamc@909 376 end
adamc@909 377 | ENamed n =>
adamc@909 378 (case IM.find (namedE, n) of
adamc@909 379 NONE => all
adamc@909 380 | SOME e => e)
adamc@909 381 | ECon (dk, pc, cs, eo) => (ECon (dk, patCon pc,
adamc@909 382 map (con env) cs, Option.map (exp env) eo), loc)
adamc@909 383 | EFfi _ => all
adamc@909 384 | EFfiApp (m, f, es) => (EFfiApp (m, f, map (exp env) es), loc)
adamc@908 385
adamc@909 386 (*| EApp (
adamc@909 387 (EApp
adamc@909 388 ((EApp ((ECApp ((ECApp ((ECApp ((EFfi ("Basis", "bind"), loc), _), _), t1), _), t2), _),
adamc@909 389 (EFfi ("Basis", "transaction_monad"), _)), _),
adamc@909 390 (ECase (ed, pes, {disc, ...}), _)), _),
adamc@909 391 trans2) =>
adamc@909 392 let
adamc@909 393 val e' = (EFfi ("Basis", "bind"), loc)
adamc@909 394 val e' = (ECApp (e', (CFfi ("Basis", "transaction"), loc)), loc)
adamc@909 395 val e' = (ECApp (e', t1), loc)
adamc@909 396 val e' = (ECApp (e', t2), loc)
adamc@909 397 val e' = (EApp (e', (EFfi ("Basis", "transaction_monad"), loc)), loc)
adamc@908 398
adamc@909 399 val pes = map (fn (p, e) =>
adamc@909 400 let
adamc@909 401 val e' = (EApp (e', e), loc)
adamc@909 402 val e' = (EApp (e',
adamc@909 403 multiLiftExpInExp (E.patBindsN p)
adamc@909 404 trans2), loc)
adamc@909 405 val e' = exp env e'
adamc@909 406 in
adamc@909 407 (p, e')
adamc@909 408 end) pes
adamc@909 409 in
adamc@909 410 (ECase (exp env ed,
adamc@909 411 pes,
adamc@909 412 {disc = con env disc,
adamc@909 413 result = (CApp ((CFfi ("Basis", "transaction"), loc), con env t2), loc)}),
adamc@909 414 loc)
adamc@909 415 end*)
adamc@908 416
adamc@909 417 | EApp (e1, e2) =>
adamc@909 418 let
adamc@910 419 val env' = deKnown env
adamc@910 420
adamc@909 421 val e1 = exp env e1
adamc@909 422 val e2 = exp env e2
adamc@909 423 in
adamc@1179 424 case #1 e1 of
adamc@1179 425 EAbs (_, _, _, b) =>
adamc@931 426 exp (KnownE e2 :: env') b
adamc@1179 427 | _ => (EApp (e1, e2), loc)
adamc@909 428 end
adamc@509 429
adamc@909 430 | EAbs (x, dom, ran, e) => (EAbs (x, con env dom, con env ran, exp (UnknownE :: env) e), loc)
adamc@509 431
adamc@909 432 | ECApp (e, c) =>
adamc@909 433 let
adamc@909 434 val e = exp env e
adamc@909 435 val c = con env c
adamc@909 436 in
adamc@909 437 case #1 e of
adamc@909 438 ECAbs (_, _, b) => exp (KnownC c :: deKnown env) b
adamc@909 439 | _ => (ECApp (e, c), loc)
adamc@909 440 end
adamc@626 441
adamc@909 442 | ECAbs (x, k, e) => (ECAbs (x, kind env k, exp (UnknownC :: env) e), loc)
adamc@626 443
adamc@909 444 | EKApp (e, k) =>
adamc@909 445 let
adamc@909 446 val e = exp env e
adamc@909 447 in
adamc@909 448 case #1 e of
adamc@909 449 EKAbs (_, b) => exp (KnownK k :: deKnown env) b
adamc@909 450 | _ => (EKApp (e, kind env k), loc)
adamc@909 451 end
adamc@509 452
adamc@909 453 | EKAbs (x, e) => (EKAbs (x, exp (UnknownK :: env) e), loc)
adamc@509 454
adamc@909 455 | ERecord xcs => (ERecord (map (fn (x, e, t) => (con env x, exp env e, con env t)) xcs), loc)
adamc@909 456 | EField (e, c, {field, rest}) =>
adamc@909 457 let
adamc@909 458 val e = exp env e
adamc@909 459 val c = con env c
adamc@509 460
adamc@909 461 fun default () = (EField (e, c, {field = con env field, rest = con env rest}), loc)
adamc@909 462 in
adamc@909 463 case (#1 e, #1 c) of
adamc@909 464 (ERecord xcs, CName x) =>
adamc@909 465 (case List.find (fn ((CName x', _), _, _) => x' = x | _ => false) xcs of
adamc@909 466 NONE => default ()
adamc@909 467 | SOME (_, e, _) => e)
adamc@909 468 | _ => default ()
adamc@909 469 end
adamc@509 470
adamc@909 471 | EConcat (e1, c1, e2, c2) =>
adamc@909 472 let
adamc@909 473 val e1 = exp env e1
adamc@909 474 val e2 = exp env e2
adamc@909 475 in
adamc@909 476 case (#1 e1, #1 e2) of
adamc@909 477 (ERecord xes1, ERecord xes2) => (ERecord (xes1 @ xes2), loc)
adamc@909 478 | _ =>
adamc@909 479 let
adamc@909 480 val c1 = con env c1
adamc@909 481 val c2 = con env c2
adamc@909 482 in
adamc@909 483 case (#1 c1, #1 c2) of
adamc@909 484 (CRecord (k, xcs1), CRecord (_, xcs2)) =>
adamc@909 485 let
adamc@909 486 val (xes1, rest) = ListUtil.foldlMap (doPart e1) [] xcs1
adamc@909 487 val (xes2, _) = ListUtil.foldlMap (doPart e2) rest xcs2
adamc@909 488 in
adamc@909 489 exp (deKnown env) (ERecord (xes1 @ xes2), loc)
adamc@909 490 end
adamc@909 491 | _ => (EConcat (e1, c1, e2, c2), loc)
adamc@909 492 end
adamc@909 493 end
adamc@509 494
adamc@909 495 | ECut (e, c, {field, rest}) =>
adamc@909 496 let
adamc@909 497 val e = exp env e
adamc@909 498 val c = con env c
adamc@509 499
adamc@909 500 fun default () =
adamc@909 501 let
adamc@909 502 val rest = con env rest
adamc@909 503 in
adamc@909 504 case #1 rest of
adamc@909 505 CRecord (k, xcs) =>
adamc@909 506 let
adamc@909 507 val (xes, _) = ListUtil.foldlMap (doPart e) [] xcs
adamc@909 508 in
adamc@909 509 exp (deKnown env) (ERecord xes, loc)
adamc@909 510 end
adamc@909 511 | _ => (ECut (e, c, {field = con env field, rest = rest}), loc)
adamc@909 512 end
adamc@909 513 in
adamc@909 514 case (#1 e, #1 c) of
adamc@909 515 (ERecord xes, CName x) =>
adamc@909 516 if List.all (fn ((CName _, _), _, _) => true | _ => false) xes then
adamc@909 517 (ERecord (List.filter (fn ((CName x', _), _, _) => x' <> x
adamc@909 518 | _ => raise Fail "Reduce: ECut") xes), loc)
adamc@909 519 else
adamc@909 520 default ()
adamc@909 521 | _ => default ()
adamc@909 522 end
adamc@509 523
adamc@909 524 | ECutMulti (e, c, {rest}) =>
adamc@909 525 let
adamc@909 526 val e = exp env e
adamc@909 527 val c = con env c
adamc@509 528
adamc@909 529 fun default () =
adamc@909 530 let
adamc@909 531 val rest = con env rest
adamc@909 532 in
adamc@909 533 case #1 rest of
adamc@909 534 CRecord (k, xcs) =>
adamc@909 535 let
adamc@909 536 val (xes, _) = ListUtil.foldlMap (doPart e) [] xcs
adamc@909 537 in
adamc@909 538 exp (deKnown env) (ERecord xes, loc)
adamc@909 539 end
adamc@909 540 | _ => (ECutMulti (e, c, {rest = rest}), loc)
adamc@909 541 end
adamc@909 542 in
adamc@909 543 case (#1 e, #1 c) of
adamc@909 544 (ERecord xes, CRecord (_, xcs)) =>
adamc@909 545 if List.all (fn ((CName _, _), _, _) => true | _ => false) xes
adamc@909 546 andalso List.all (fn ((CName _, _), _) => true | _ => false) xcs then
adamc@909 547 (ERecord (List.filter (fn ((CName x', _), _, _) =>
adamc@909 548 List.all (fn ((CName x, _), _) => x' <> x
adamc@909 549 | _ => raise Fail "Reduce: ECutMulti [1]") xcs
adamc@909 550 | _ => raise Fail "Reduce: ECutMulti [2]") xes), loc)
adamc@909 551 else
adamc@909 552 default ()
adamc@909 553 | _ => default ()
adamc@909 554 end
adamc@823 555
adamc@909 556 | ECase (_, [((PRecord [], _), e)], _) => exp env e
adamc@909 557 | ECase (_, [((PWild, _), e)], _) => exp env e
adamc@509 558
adamc@909 559 | ECase (e, pes, {disc, result}) =>
adamc@909 560 let
adamc@909 561 fun patBinds (p, _) =
adamc@909 562 case p of
adamc@909 563 PWild => 0
adamc@909 564 | PVar _ => 1
adamc@909 565 | PPrim _ => 0
adamc@909 566 | PCon (_, _, _, NONE) => 0
adamc@909 567 | PCon (_, _, _, SOME p) => patBinds p
adamc@909 568 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
adamc@509 569
adamc@909 570 fun pat (all as (p, loc)) =
adamc@909 571 case p of
adamc@909 572 PWild => all
adamc@909 573 | PVar (x, t) => (PVar (x, con env t), loc)
adamc@909 574 | PPrim _ => all
adamc@909 575 | PCon (dk, pc, cs, po) =>
adamc@909 576 (PCon (dk, patCon pc, map (con env) cs, Option.map pat po), loc)
adamc@909 577 | PRecord xpts => (PRecord (map (fn (x, p, t) => (x, pat p, con env t)) xpts), loc)
adamc@930 578
adamc@930 579 fun push () =
adamc@930 580 (ECase (exp env e,
adamc@930 581 map (fn (p, e) => (pat p,
adamc@930 582 exp (List.tabulate (patBinds p,
adamc@930 583 fn _ => UnknownE) @ env) e))
adamc@930 584 pes, {disc = con env disc, result = con env result}), loc)
adamc@930 585
adamc@930 586 fun search pes =
adamc@930 587 case pes of
adamc@930 588 [] => push ()
adamc@930 589 | (p, body) :: pes =>
adamc@930 590 case match (env, p, e) of
adamc@930 591 No => search pes
adamc@930 592 | Maybe => push ()
adamc@930 593 | Yes env' => exp env' body
adamc@909 594 in
adamc@930 595 search pes
adamc@909 596 end
adamc@509 597
adamc@909 598 | EWrite e => (EWrite (exp env e), loc)
adamc@909 599 | EClosure (n, es) => (EClosure (n, map (exp env) es), loc)
adamc@607 600
adamc@910 601 | ELet (x, t, e1, e2) =>
adamc@910 602 (ELet (x, con env t, exp env e1, exp (UnknownE :: env) e2), loc)
adamc@909 603
adamc@1020 604 | EServerCall (n, es, t) => (EServerCall (n, map (exp env) es, con env t), loc)
adamc@909 605 in
adamc@909 606 (*if dangling (edepth' (deKnown env)) r then
adamc@909 607 (Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 608 ("r", CorePrint.p_exp CoreEnv.empty r)];
adamc@909 609 raise Fail "!!")
adamc@909 610 else
adamc@909 611 ();*)
adamc@909 612 r
adamc@909 613 end
adamc@417 614 in
adamc@626 615 {kind = kind, con = con, exp = exp}
adamc@417 616 end
adamc@21 617
adamc@626 618 fun kind namedC env k = #kind (kindConAndExp (namedC, IM.empty)) env k
adamc@626 619 fun con namedC env c = #con (kindConAndExp (namedC, IM.empty)) env c
adamc@626 620 fun exp (namedC, namedE) env e = #exp (kindConAndExp (namedC, namedE)) env e
adamc@20 621
adamc@508 622 fun reduce file =
adamc@508 623 let
adamc@1016 624 val uses = CoreUtil.File.fold {kind = fn (_, m) => m,
adamc@1016 625 con = fn (_, m) => m,
adamc@1016 626 exp = fn (e, m) =>
adamc@1016 627 case e of
adamc@1016 628 ENamed n => IM.insert (m, n, 1 + Option.getOpt (IM.find (m, n), 0))
adamc@1016 629 | _ => m,
adamc@1016 630 decl = fn (_, m) => m}
adamc@1016 631 IM.empty file
adamc@1016 632
adamc@1016 633 fun isPoly names = CoreUtil.Con.exists {kind = fn _ => false,
adamc@1016 634 con = fn TCFun _ => true
adamc@1016 635 | TKFun _ => true
adamc@1016 636 | CNamed n => IS.member (names, n)
adamc@1016 637 | _ => false}
adamc@1016 638
adamc@1016 639 val size = CoreUtil.Exp.fold {kind = fn (_, n) => n,
adamc@1016 640 con = fn (_, n) => n,
adamc@1016 641 exp = fn (_, n) => n + 1} 0
adamc@1016 642
adamc@1016 643 fun mayInline (polyC, n, t, e) =
adamc@1016 644 case IM.find (uses, n) of
adamc@1016 645 NONE => false
adamc@1016 646 | SOME count => count <= 1
adamc@1019 647 orelse (case #1 e of
adamc@1019 648 ERecord _ => true
adamc@1019 649 | _ => false)
adamc@1016 650 orelse isPoly polyC t
adamc@1016 651 orelse size e <= Settings.getCoreInline ()
adamc@1016 652
adamc@1016 653 fun doDecl (d as (_, loc), st as (polyC, namedC, namedE)) =
adamc@508 654 case #1 d of
adamc@508 655 DCon (x, n, k, c) =>
adamc@508 656 let
adamc@626 657 val k = kind namedC [] k
adamc@509 658 val c = con namedC [] c
adamc@508 659 in
adamc@508 660 ((DCon (x, n, k, c), loc),
adamc@1016 661 (if isPoly polyC c then
adamc@1016 662 IS.add (polyC, n)
adamc@1016 663 else
adamc@1016 664 polyC,
adamc@1016 665 IM.insert (namedC, n, c),
adamc@1016 666 namedE))
adamc@508 667 end
adamc@807 668 | DDatatype dts =>
adamc@807 669 ((DDatatype (map (fn (x, n, ps, cs) =>
adamc@807 670 let
adamc@807 671 val env = map (fn _ => UnknownC) ps
adamc@807 672 in
adamc@807 673 (x, n, ps, map (fn (x, n, co) => (x, n, Option.map (con namedC env) co)) cs)
adamc@807 674 end) dts), loc),
adamc@1016 675 (if List.exists (fn (_, _, _, cs) => List.exists (fn (_, _, co) => case co of
adamc@1016 676 NONE => false
adamc@1016 677 | SOME c => isPoly polyC c) cs)
adamc@1016 678 dts then
adamc@1016 679 foldl (fn ((_, n, _, _), polyC) => IS.add (polyC, n)) polyC dts
adamc@1016 680 else
adamc@1016 681 polyC,
adamc@1016 682 namedC,
adamc@1016 683 namedE))
adamc@508 684 | DVal (x, n, t, e, s) =>
adamc@508 685 let
adamc@509 686 val t = con namedC [] t
adamc@509 687 val e = exp (namedC, namedE) [] e
adamc@508 688 in
adamc@508 689 ((DVal (x, n, t, e, s), loc),
adamc@1016 690 (polyC,
adamc@1016 691 namedC,
adamc@1016 692 if mayInline (polyC, n, t, e) then
adamc@1016 693 IM.insert (namedE, n, e)
adamc@1016 694 else
adamc@1016 695 namedE))
adamc@508 696 end
adamc@508 697 | DValRec vis =>
adamc@910 698 ((DValRec (map (fn (x, n, t, e, s) => (x, n, con namedC [] t,
adamc@910 699 exp (namedC, namedE) [] e, s)) vis), loc),
adamc@508 700 st)
adamc@508 701 | DExport _ => (d, st)
adamc@707 702 | DTable (s, n, c, s', pe, pc, ce, cc) => ((DTable (s, n, con namedC [] c, s',
adamc@707 703 exp (namedC, namedE) [] pe,
adamc@707 704 con namedC [] pc,
adamc@707 705 exp (namedC, namedE) [] ce,
adamc@707 706 con namedC [] cc), loc), st)
adamc@508 707 | DSequence _ => (d, st)
adamc@754 708 | DView (s, n, s', e, c) => ((DView (s, n, s', exp (namedC, namedE) [] e, con namedC [] c), loc), st)
adamc@508 709 | DDatabase _ => (d, st)
adamc@509 710 | DCookie (s, n, c, s') => ((DCookie (s, n, con namedC [] c, s'), loc), st)
adamc@720 711 | DStyle (s, n, s') => ((DStyle (s, n, s'), loc), st)
adamc@1075 712 | DTask (e1, e2) =>
adamc@1073 713 let
adamc@1075 714 val e1 = exp (namedC, namedE) [] e1
adamc@1075 715 val e2 = exp (namedC, namedE) [] e2
adamc@1073 716 in
adamc@1075 717 ((DTask (e1, e2), loc),
adamc@1073 718 (polyC,
adamc@1073 719 namedC,
adamc@1073 720 namedE))
adamc@1073 721 end
adamc@20 722
adamc@1016 723 val (file, _) = ListUtil.foldlMap doDecl (IS.empty, IM.empty, IM.empty) file
adamc@508 724 in
adamc@508 725 file
adamc@508 726 end
adamc@20 727
adamc@20 728 end