annotate src/reduce.sml @ 1271:503d4ec93494

Allow complex attribute records in Tag
author Adam Chlipala <adamc@hcoop.net>
date Tue, 01 Jun 2010 15:46:24 -0400
parents beb67ff4c8a0
children 56bd4a4f6e66
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@1181 426 let
adamc@1181 427 val r = exp (KnownE e2 :: env') b
adamc@1181 428 in
adamc@1181 429 (*Print.prefaces "eapp" [("b", CorePrint.p_exp CoreEnv.empty b),
adamc@1181 430 ("env", Print.PD.string (e2s env')),
adamc@1181 431 ("e2", CorePrint.p_exp CoreEnv.empty e2),
adamc@1181 432 ("r", CorePrint.p_exp CoreEnv.empty r)];*)
adamc@1181 433 r
adamc@1181 434 end
adamc@1179 435 | _ => (EApp (e1, e2), loc)
adamc@909 436 end
adamc@509 437
adamc@909 438 | EAbs (x, dom, ran, e) => (EAbs (x, con env dom, con env ran, exp (UnknownE :: env) e), loc)
adamc@509 439
adamc@909 440 | ECApp (e, c) =>
adamc@909 441 let
adamc@909 442 val e = exp env e
adamc@909 443 val c = con env c
adamc@909 444 in
adamc@909 445 case #1 e of
adamc@1181 446 ECAbs (_, _, b) =>
adamc@1181 447 let
adamc@1181 448 val r = exp (KnownC c :: deKnown env) b
adamc@1181 449 in
adamc@1181 450 (*Print.prefaces "csub" [("l", Print.PD.string (ErrorMsg.spanToString loc)),
adamc@1181 451 ("env", Print.PD.string (e2s (deKnown env))),
adamc@1181 452 ("b", CorePrint.p_exp CoreEnv.empty b),
adamc@1181 453 ("c", CorePrint.p_con CoreEnv.empty c),
adamc@1181 454 ("r", CorePrint.p_exp CoreEnv.empty r)];*)
adamc@1181 455 r
adamc@1181 456 end
adamc@909 457 | _ => (ECApp (e, c), loc)
adamc@909 458 end
adamc@626 459
adamc@909 460 | ECAbs (x, k, e) => (ECAbs (x, kind env k, exp (UnknownC :: env) e), loc)
adamc@626 461
adamc@909 462 | EKApp (e, k) =>
adamc@909 463 let
adamc@909 464 val e = exp env e
adamc@909 465 in
adamc@909 466 case #1 e of
adamc@1181 467 EKAbs (_, b) =>
adamc@1181 468 let
adamc@1181 469 val r = exp (KnownK k :: deKnown env) b
adamc@1181 470 in
adamc@1181 471 (*Print.prefaces "ksub" [("l", Print.PD.string (ErrorMsg.spanToString loc)),
adamc@1181 472 ("b", CorePrint.p_exp CoreEnv.empty b),
adamc@1181 473 ("k", CorePrint.p_kind CoreEnv.empty k),
adamc@1181 474 ("r", CorePrint.p_exp CoreEnv.empty r)];*)
adamc@1181 475 r
adamc@1181 476 end
adamc@909 477 | _ => (EKApp (e, kind env k), loc)
adamc@909 478 end
adamc@509 479
adamc@909 480 | EKAbs (x, e) => (EKAbs (x, exp (UnknownK :: env) e), loc)
adamc@509 481
adamc@909 482 | ERecord xcs => (ERecord (map (fn (x, e, t) => (con env x, exp env e, con env t)) xcs), loc)
adamc@909 483 | EField (e, c, {field, rest}) =>
adamc@909 484 let
adamc@909 485 val e = exp env e
adamc@909 486 val c = con env c
adamc@509 487
adamc@909 488 fun default () = (EField (e, c, {field = con env field, rest = con env rest}), loc)
adamc@909 489 in
adamc@909 490 case (#1 e, #1 c) of
adamc@909 491 (ERecord xcs, CName x) =>
adamc@909 492 (case List.find (fn ((CName x', _), _, _) => x' = x | _ => false) xcs of
adamc@909 493 NONE => default ()
adamc@909 494 | SOME (_, e, _) => e)
adamc@909 495 | _ => default ()
adamc@909 496 end
adamc@509 497
adamc@909 498 | EConcat (e1, c1, e2, c2) =>
adamc@909 499 let
adamc@909 500 val e1 = exp env e1
adamc@909 501 val e2 = exp env e2
adamc@909 502 in
adamc@909 503 case (#1 e1, #1 e2) of
adamc@909 504 (ERecord xes1, ERecord xes2) => (ERecord (xes1 @ xes2), loc)
adamc@909 505 | _ =>
adamc@909 506 let
adamc@909 507 val c1 = con env c1
adamc@909 508 val c2 = con env c2
adamc@909 509 in
adamc@909 510 case (#1 c1, #1 c2) of
adamc@909 511 (CRecord (k, xcs1), CRecord (_, xcs2)) =>
adamc@909 512 let
adamc@909 513 val (xes1, rest) = ListUtil.foldlMap (doPart e1) [] xcs1
adamc@909 514 val (xes2, _) = ListUtil.foldlMap (doPart e2) rest xcs2
adamc@909 515 in
adamc@909 516 exp (deKnown env) (ERecord (xes1 @ xes2), loc)
adamc@909 517 end
adamc@909 518 | _ => (EConcat (e1, c1, e2, c2), loc)
adamc@909 519 end
adamc@909 520 end
adamc@509 521
adamc@909 522 | ECut (e, c, {field, rest}) =>
adamc@909 523 let
adamc@909 524 val e = exp env e
adamc@909 525 val c = con env c
adamc@509 526
adamc@909 527 fun default () =
adamc@909 528 let
adamc@909 529 val rest = con env rest
adamc@909 530 in
adamc@909 531 case #1 rest of
adamc@909 532 CRecord (k, xcs) =>
adamc@909 533 let
adamc@909 534 val (xes, _) = ListUtil.foldlMap (doPart e) [] xcs
adamc@909 535 in
adamc@909 536 exp (deKnown env) (ERecord xes, loc)
adamc@909 537 end
adamc@909 538 | _ => (ECut (e, c, {field = con env field, rest = rest}), loc)
adamc@909 539 end
adamc@909 540 in
adamc@909 541 case (#1 e, #1 c) of
adamc@909 542 (ERecord xes, CName x) =>
adamc@909 543 if List.all (fn ((CName _, _), _, _) => true | _ => false) xes then
adamc@909 544 (ERecord (List.filter (fn ((CName x', _), _, _) => x' <> x
adamc@909 545 | _ => raise Fail "Reduce: ECut") xes), loc)
adamc@909 546 else
adamc@909 547 default ()
adamc@909 548 | _ => default ()
adamc@909 549 end
adamc@509 550
adamc@909 551 | ECutMulti (e, c, {rest}) =>
adamc@909 552 let
adamc@909 553 val e = exp env e
adamc@909 554 val c = con env c
adamc@509 555
adamc@909 556 fun default () =
adamc@909 557 let
adamc@909 558 val rest = con env rest
adamc@909 559 in
adamc@909 560 case #1 rest of
adamc@909 561 CRecord (k, xcs) =>
adamc@909 562 let
adamc@909 563 val (xes, _) = ListUtil.foldlMap (doPart e) [] xcs
adamc@909 564 in
adamc@909 565 exp (deKnown env) (ERecord xes, loc)
adamc@909 566 end
adamc@909 567 | _ => (ECutMulti (e, c, {rest = rest}), loc)
adamc@909 568 end
adamc@909 569 in
adamc@909 570 case (#1 e, #1 c) of
adamc@909 571 (ERecord xes, CRecord (_, xcs)) =>
adamc@909 572 if List.all (fn ((CName _, _), _, _) => true | _ => false) xes
adamc@909 573 andalso List.all (fn ((CName _, _), _) => true | _ => false) xcs then
adamc@909 574 (ERecord (List.filter (fn ((CName x', _), _, _) =>
adamc@909 575 List.all (fn ((CName x, _), _) => x' <> x
adamc@909 576 | _ => raise Fail "Reduce: ECutMulti [1]") xcs
adamc@909 577 | _ => raise Fail "Reduce: ECutMulti [2]") xes), loc)
adamc@909 578 else
adamc@909 579 default ()
adamc@909 580 | _ => default ()
adamc@909 581 end
adamc@823 582
adamc@909 583 | ECase (_, [((PRecord [], _), e)], _) => exp env e
adamc@909 584 | ECase (_, [((PWild, _), e)], _) => exp env e
adamc@509 585
adamc@909 586 | ECase (e, pes, {disc, result}) =>
adamc@909 587 let
adamc@909 588 fun patBinds (p, _) =
adamc@909 589 case p of
adamc@909 590 PWild => 0
adamc@909 591 | PVar _ => 1
adamc@909 592 | PPrim _ => 0
adamc@909 593 | PCon (_, _, _, NONE) => 0
adamc@909 594 | PCon (_, _, _, SOME p) => patBinds p
adamc@909 595 | PRecord xpts => foldl (fn ((_, p, _), n) => n + patBinds p) 0 xpts
adamc@509 596
adamc@909 597 fun pat (all as (p, loc)) =
adamc@909 598 case p of
adamc@909 599 PWild => all
adamc@909 600 | PVar (x, t) => (PVar (x, con env t), loc)
adamc@909 601 | PPrim _ => all
adamc@909 602 | PCon (dk, pc, cs, po) =>
adamc@909 603 (PCon (dk, patCon pc, map (con env) cs, Option.map pat po), loc)
adamc@909 604 | PRecord xpts => (PRecord (map (fn (x, p, t) => (x, pat p, con env t)) xpts), loc)
adamc@930 605
adamc@930 606 fun push () =
adamc@930 607 (ECase (exp env e,
adamc@930 608 map (fn (p, e) => (pat p,
adamc@930 609 exp (List.tabulate (patBinds p,
adamc@930 610 fn _ => UnknownE) @ env) e))
adamc@930 611 pes, {disc = con env disc, result = con env result}), loc)
adamc@930 612
adamc@930 613 fun search pes =
adamc@930 614 case pes of
adamc@930 615 [] => push ()
adamc@930 616 | (p, body) :: pes =>
adamc@930 617 case match (env, p, e) of
adamc@930 618 No => search pes
adamc@930 619 | Maybe => push ()
adamc@930 620 | Yes env' => exp env' body
adamc@909 621 in
adamc@930 622 search pes
adamc@909 623 end
adamc@509 624
adamc@909 625 | EWrite e => (EWrite (exp env e), loc)
adamc@909 626 | EClosure (n, es) => (EClosure (n, map (exp env) es), loc)
adamc@607 627
adamc@910 628 | ELet (x, t, e1, e2) =>
adamc@910 629 (ELet (x, con env t, exp env e1, exp (UnknownE :: env) e2), loc)
adamc@909 630
adamc@1020 631 | EServerCall (n, es, t) => (EServerCall (n, map (exp env) es, con env t), loc)
adamc@909 632 in
adamc@909 633 (*if dangling (edepth' (deKnown env)) r then
adamc@909 634 (Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty all),
adamc@909 635 ("r", CorePrint.p_exp CoreEnv.empty r)];
adamc@909 636 raise Fail "!!")
adamc@909 637 else
adamc@909 638 ();*)
adamc@909 639 r
adamc@909 640 end
adamc@417 641 in
adamc@626 642 {kind = kind, con = con, exp = exp}
adamc@417 643 end
adamc@21 644
adamc@626 645 fun kind namedC env k = #kind (kindConAndExp (namedC, IM.empty)) env k
adamc@626 646 fun con namedC env c = #con (kindConAndExp (namedC, IM.empty)) env c
adamc@626 647 fun exp (namedC, namedE) env e = #exp (kindConAndExp (namedC, namedE)) env e
adamc@20 648
adamc@508 649 fun reduce file =
adamc@508 650 let
adamc@1016 651 val uses = CoreUtil.File.fold {kind = fn (_, m) => m,
adamc@1016 652 con = fn (_, m) => m,
adamc@1016 653 exp = fn (e, m) =>
adamc@1016 654 case e of
adamc@1016 655 ENamed n => IM.insert (m, n, 1 + Option.getOpt (IM.find (m, n), 0))
adamc@1016 656 | _ => m,
adamc@1016 657 decl = fn (_, m) => m}
adamc@1016 658 IM.empty file
adamc@1016 659
adamc@1016 660 fun isPoly names = CoreUtil.Con.exists {kind = fn _ => false,
adamc@1016 661 con = fn TCFun _ => true
adamc@1016 662 | TKFun _ => true
adamc@1016 663 | CNamed n => IS.member (names, n)
adamc@1016 664 | _ => false}
adamc@1016 665
adamc@1016 666 val size = CoreUtil.Exp.fold {kind = fn (_, n) => n,
adamc@1016 667 con = fn (_, n) => n,
adamc@1016 668 exp = fn (_, n) => n + 1} 0
adamc@1016 669
adamc@1016 670 fun mayInline (polyC, n, t, e) =
adamc@1240 671 let
adamc@1240 672 fun isPolicy t =
adamc@1240 673 case #1 t of
adamc@1240 674 CFfi ("Basis", "sql_policy") => true
adamc@1240 675 | TFun (_, t) => isPolicy t
adamc@1240 676 | _ => false
adamc@1240 677 in
adamc@1240 678 case IM.find (uses, n) of
adamc@1240 679 NONE => false
adamc@1240 680 | SOME count => count <= 1
adamc@1240 681 orelse (case #1 e of
adamc@1240 682 ERecord _ => true
adamc@1240 683 | _ => false)
adamc@1240 684 orelse isPolicy t
adamc@1240 685 orelse isPoly polyC t
adamc@1240 686 orelse size e <= Settings.getCoreInline ()
adamc@1240 687 end
adamc@1016 688
adamc@1016 689 fun doDecl (d as (_, loc), st as (polyC, namedC, namedE)) =
adamc@508 690 case #1 d of
adamc@508 691 DCon (x, n, k, c) =>
adamc@508 692 let
adamc@626 693 val k = kind namedC [] k
adamc@509 694 val c = con namedC [] c
adamc@508 695 in
adamc@508 696 ((DCon (x, n, k, c), loc),
adamc@1016 697 (if isPoly polyC c then
adamc@1016 698 IS.add (polyC, n)
adamc@1016 699 else
adamc@1016 700 polyC,
adamc@1016 701 IM.insert (namedC, n, c),
adamc@1016 702 namedE))
adamc@508 703 end
adamc@807 704 | DDatatype dts =>
adamc@807 705 ((DDatatype (map (fn (x, n, ps, cs) =>
adamc@807 706 let
adamc@807 707 val env = map (fn _ => UnknownC) ps
adamc@807 708 in
adamc@807 709 (x, n, ps, map (fn (x, n, co) => (x, n, Option.map (con namedC env) co)) cs)
adamc@807 710 end) dts), loc),
adamc@1016 711 (if List.exists (fn (_, _, _, cs) => List.exists (fn (_, _, co) => case co of
adamc@1016 712 NONE => false
adamc@1016 713 | SOME c => isPoly polyC c) cs)
adamc@1016 714 dts then
adamc@1016 715 foldl (fn ((_, n, _, _), polyC) => IS.add (polyC, n)) polyC dts
adamc@1016 716 else
adamc@1016 717 polyC,
adamc@1016 718 namedC,
adamc@1016 719 namedE))
adamc@508 720 | DVal (x, n, t, e, s) =>
adamc@508 721 let
adamc@509 722 val t = con namedC [] t
adamc@509 723 val e = exp (namedC, namedE) [] e
adamc@508 724 in
adamc@508 725 ((DVal (x, n, t, e, s), loc),
adamc@1016 726 (polyC,
adamc@1016 727 namedC,
adamc@1016 728 if mayInline (polyC, n, t, e) then
adamc@1016 729 IM.insert (namedE, n, e)
adamc@1016 730 else
adamc@1016 731 namedE))
adamc@508 732 end
adamc@508 733 | DValRec vis =>
adamc@910 734 ((DValRec (map (fn (x, n, t, e, s) => (x, n, con namedC [] t,
adamc@910 735 exp (namedC, namedE) [] e, s)) vis), loc),
adamc@508 736 st)
adamc@508 737 | DExport _ => (d, st)
adamc@707 738 | DTable (s, n, c, s', pe, pc, ce, cc) => ((DTable (s, n, con namedC [] c, s',
adamc@707 739 exp (namedC, namedE) [] pe,
adamc@707 740 con namedC [] pc,
adamc@707 741 exp (namedC, namedE) [] ce,
adamc@707 742 con namedC [] cc), loc), st)
adamc@508 743 | DSequence _ => (d, st)
adamc@754 744 | DView (s, n, s', e, c) => ((DView (s, n, s', exp (namedC, namedE) [] e, con namedC [] c), loc), st)
adamc@508 745 | DDatabase _ => (d, st)
adamc@509 746 | DCookie (s, n, c, s') => ((DCookie (s, n, con namedC [] c, s'), loc), st)
adamc@720 747 | DStyle (s, n, s') => ((DStyle (s, n, s'), loc), st)
adamc@1075 748 | DTask (e1, e2) =>
adamc@1073 749 let
adamc@1075 750 val e1 = exp (namedC, namedE) [] e1
adamc@1075 751 val e2 = exp (namedC, namedE) [] e2
adamc@1073 752 in
adamc@1075 753 ((DTask (e1, e2), loc),
adamc@1073 754 (polyC,
adamc@1073 755 namedC,
adamc@1073 756 namedE))
adamc@1073 757 end
adamc@1199 758 | DPolicy e1 =>
adamc@1199 759 let
adamc@1199 760 val e1 = exp (namedC, namedE) [] e1
adamc@1199 761 in
adamc@1199 762 ((DPolicy e1, loc),
adamc@1199 763 (polyC,
adamc@1199 764 namedC,
adamc@1199 765 namedE))
adamc@1199 766 end
adamc@20 767
adamc@1016 768 val (file, _) = ListUtil.foldlMap doDecl (IS.empty, IM.empty, IM.empty) file
adamc@508 769 in
adamc@508 770 file
adamc@508 771 end
adamc@20 772
adamc@20 773 end