annotate src/especialize.sml @ 1859:38399d67a914

Make that new error message text a bit more accurate
author Adam Chlipala <adam@chlipala.net>
date Wed, 17 Jul 2013 10:40:13 -0400
parents e15234fbb163
children 52043ad66ce7
rev   line source
adam@1848 1 (* Copyright (c) 2008-2013, Adam Chlipala
adamc@443 2 * All rights reserved.
adamc@443 3 *
adamc@443 4 * Redistribution and use in source and binary forms, with or without
adamc@443 5 * modification, are permitted provided that the following conditions are met:
adamc@443 6 *
adamc@443 7 * - Redistributions of source code must retain the above copyright notice,
adamc@443 8 * this list of conditions and the following disclaimer.
adamc@443 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@443 10 * this list of conditions and the following disclaimer in the documentation
adamc@443 11 * and/or other materials provided with the distribution.
adamc@443 12 * - The names of contributors may not be used to endorse or promote products
adamc@443 13 * derived from this software without specific prior written permission.
adamc@443 14 *
adamc@443 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@443 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@443 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@443 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@443 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@443 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@443 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@443 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@443 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@443 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@443 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@443 26 *)
adamc@443 27
adamc@443 28 structure ESpecialize :> ESPECIALIZE = struct
adamc@443 29
adamc@443 30 open Core
adamc@443 31
adamc@443 32 structure E = CoreEnv
adamc@443 33 structure U = CoreUtil
adamc@443 34
adamc@479 35 type skey = exp
adamc@453 36
adamc@453 37 structure K = struct
adam@1314 38 type ord_key = con list * exp list
adam@1314 39 fun compare ((cs1, es1), (cs2, es2)) = Order.join (Order.joinL U.Con.compare (cs1, cs2),
adam@1314 40 fn () => Order.joinL U.Exp.compare (es1, es2))
adamc@443 41 end
adamc@443 42
adamc@453 43 structure KM = BinaryMapFn(K)
adamc@443 44 structure IM = IntBinaryMap
adamc@482 45 structure IS = IntBinarySet
adamc@443 46
adamc@626 47 val freeVars = U.Exp.foldB {kind = fn (_, _, xs) => xs,
adamc@488 48 con = fn (_, _, xs) => xs,
adamc@488 49 exp = fn (bound, e, xs) =>
adamc@488 50 case e of
adamc@488 51 ERel x =>
adamc@488 52 if x >= bound then
adamc@488 53 IS.add (xs, x - bound)
adamc@488 54 else
adamc@488 55 xs
adamc@488 56 | _ => xs,
adamc@488 57 bind = fn (bound, b) =>
adamc@488 58 case b of
adamc@488 59 U.Exp.RelE _ => bound + 1
adamc@488 60 | _ => bound}
adamc@488 61 0 IS.empty
adamc@479 62
adamc@1120 63 fun isPolyT (t, _) =
adamc@1120 64 case t of
adamc@1120 65 TFun (_, ran) => isPolyT ran
adamc@1120 66 | TCFun _ => true
adamc@1120 67 | TKFun _ => true
adamc@1120 68 | _ => false
adamc@1120 69
adamc@1120 70 fun isPoly (d, _) =
adamc@1120 71 case d of
adamc@1120 72 DVal (_, _, t, _, _) => isPolyT t
adamc@1120 73 | DValRec vis => List.exists (isPolyT o #3) vis
adamc@1120 74 | _ => false
adamc@522 75
adamc@488 76 fun positionOf (v : int, ls) =
adamc@488 77 let
adamc@488 78 fun pof (pos, ls) =
adamc@488 79 case ls of
adamc@488 80 [] => raise Fail "Defunc.positionOf"
adamc@488 81 | v' :: ls' =>
adamc@488 82 if v = v' then
adamc@488 83 pos
adamc@488 84 else
adamc@488 85 pof (pos + 1, ls')
adamc@488 86 in
adamc@488 87 pof (0, ls)
adamc@488 88 end
adamc@488 89
adamc@1079 90 fun squish fvs =
adamc@626 91 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@488 92 con = fn _ => fn c => c,
adamc@488 93 exp = fn bound => fn e =>
adamc@479 94 case e of
adamc@488 95 ERel x =>
adamc@488 96 if x >= bound then
adamc@1079 97 ERel (positionOf (x - bound, fvs) + bound)
adamc@488 98 else
adamc@488 99 e
adamc@488 100 | _ => e,
adamc@488 101 bind = fn (bound, b) =>
adamc@488 102 case b of
adamc@488 103 U.Exp.RelE _ => bound + 1
adamc@488 104 | _ => bound}
adamc@488 105 0
adamc@453 106
adamc@443 107 type func = {
adamc@443 108 name : string,
adamc@453 109 args : int KM.map,
adamc@443 110 body : exp,
adamc@443 111 typ : con,
adam@1675 112 tag : string,
adam@1675 113 constArgs : int (* What length prefix of the arguments never vary across recursive calls? *)
adamc@443 114 }
adamc@443 115
adamc@443 116 type state = {
adamc@443 117 maxName : int,
adamc@443 118 funcs : func IM.map,
adamc@1079 119 decls : (string * int * con * exp * string) list,
adamc@1080 120 specialized : IS.set
adamc@443 121 }
adamc@443 122
adamc@488 123 fun default (_, x, st) = (x, st)
adamc@443 124
adam@1289 125 val functionInside = U.Con.exists {kind = fn _ => false,
adam@1289 126 con = fn TFun _ => true
adam@1817 127 | TCFun _ => true
adam@1289 128 | CFfi ("Basis", "transaction") => true
adam@1289 129 | CFfi ("Basis", "eq") => true
adam@1289 130 | CFfi ("Basis", "num") => true
adam@1289 131 | CFfi ("Basis", "ord") => true
adam@1289 132 | CFfi ("Basis", "show") => true
adam@1289 133 | CFfi ("Basis", "read") => true
adam@1289 134 | CFfi ("Basis", "sql_injectable_prim") => true
adam@1289 135 | CFfi ("Basis", "sql_injectable") => true
adam@1289 136 | _ => false}
adam@1289 137
adam@1675 138 fun getApp (e, _) =
adam@1675 139 case e of
adam@1675 140 ENamed f => SOME (f, [])
adam@1675 141 | EApp (e1, e2) =>
adam@1675 142 (case getApp e1 of
adam@1675 143 NONE => NONE
adam@1675 144 | SOME (f, xs) => SOME (f, xs @ [e2]))
adam@1675 145 | _ => NONE
adam@1675 146
adam@1675 147 val getApp = fn e => case getApp e of
adam@1675 148 v as SOME (_, _ :: _) => v
adam@1675 149 | _ => NONE
adam@1675 150
adam@1675 151 val maxInt = Option.getOpt (Int.maxInt, 9999)
adam@1675 152
adam@1766 153 fun calcConstArgs enclosingFunctions e =
adam@1675 154 let
adam@1675 155 fun ca depth e =
adam@1675 156 case #1 e of
adam@1675 157 EPrim _ => maxInt
adam@1675 158 | ERel _ => maxInt
adam@1766 159 | ENamed n => if IS.member (enclosingFunctions, n) then 0 else maxInt
adam@1675 160 | ECon (_, _, _, NONE) => maxInt
adam@1675 161 | ECon (_, _, _, SOME e) => ca depth e
adam@1675 162 | EFfi _ => maxInt
adam@1675 163 | EFfiApp (_, _, ecs) => foldl (fn ((e, _), d) => Int.min (ca depth e, d)) maxInt ecs
adam@1675 164 | EApp (e1, e2) =>
adam@1675 165 let
adam@1675 166 fun default () = Int.min (ca depth e1, ca depth e2)
adam@1675 167 in
adam@1675 168 case getApp e of
adam@1675 169 NONE => default ()
adam@1675 170 | SOME (f, args) =>
adam@1766 171 if not (IS.member (enclosingFunctions, f)) then
adam@1675 172 default ()
adam@1675 173 else
adam@1675 174 let
adam@1675 175 fun visitArgs (count, args) =
adam@1675 176 case args of
adam@1675 177 [] => count
adam@1675 178 | arg :: args' =>
adam@1675 179 let
adam@1675 180 fun default () = foldl (fn (e, d) => Int.min (ca depth e, d)) count args
adam@1675 181 in
adam@1675 182 case #1 arg of
adam@1675 183 ERel n =>
adam@1676 184 if n = depth - 1 - count then
adam@1675 185 visitArgs (count + 1, args')
adam@1675 186 else
adam@1675 187 default ()
adam@1675 188 | _ => default ()
adam@1675 189 end
adam@1675 190 in
adam@1675 191 visitArgs (0, args)
adam@1675 192 end
adam@1675 193 end
adam@1675 194 | EAbs (_, _, _, e1) => ca (depth + 1) e1
adam@1675 195 | ECApp (e1, _) => ca depth e1
adam@1675 196 | ECAbs (_, _, e1) => ca depth e1
adam@1675 197 | EKAbs (_, e1) => ca depth e1
adam@1675 198 | EKApp (e1, _) => ca depth e1
adam@1675 199 | ERecord xets => foldl (fn ((_, e, _), d) => Int.min (ca depth e, d)) maxInt xets
adam@1675 200 | EField (e1, _, _) => ca depth e1
adam@1675 201 | EConcat (e1, _, e2, _) => Int.min (ca depth e1, ca depth e2)
adam@1675 202 | ECut (e1, _, _) => ca depth e1
adam@1675 203 | ECutMulti (e1, _, _) => ca depth e1
adam@1675 204 | ECase (e1, pes, _) => foldl (fn ((p, e), d) => Int.min (ca (depth + E.patBindsN p) e, d)) (ca depth e1) pes
adam@1675 205 | EWrite e1 => ca depth e1
adam@1675 206 | EClosure (_, es) => foldl (fn (e, d) => Int.min (ca depth e, d)) maxInt es
adam@1675 207 | ELet (_, _, e1, e2) => Int.min (ca depth e1, ca (depth + 1) e2)
adam@1848 208 | EServerCall (_, es, _, _) => foldl (fn (e, d) => Int.min (ca depth e, d)) maxInt es
adam@1675 209
adam@1675 210 fun enterAbs depth e =
adam@1675 211 case #1 e of
adam@1675 212 EAbs (_, _, _, e1) => enterAbs (depth + 1) e1
adam@1675 213 | _ => ca depth e
adam@1675 214 in
adam@1677 215 enterAbs 0 e
adam@1675 216 end
adam@1675 217
adam@1675 218
adamc@1080 219 fun specialize' (funcs, specialized) file =
adamc@443 220 let
adamc@488 221 fun bind (env, b) =
adamc@488 222 case b of
adamc@521 223 U.Decl.RelE xt => xt :: env
adamc@521 224 | _ => env
adamc@488 225
adamc@1080 226 fun exp (env, e as (_, loc), st : state) =
adamc@482 227 let
adamc@721 228 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 229 (e, ErrorMsg.dummySpan))]*)
adamc@721 230
adamc@1080 231 fun default () =
adamc@1080 232 case #1 e of
adamc@1080 233 EPrim _ => (e, st)
adamc@1080 234 | ERel _ => (e, st)
adamc@1080 235 | ENamed _ => (e, st)
adamc@1080 236 | ECon (_, _, _, NONE) => (e, st)
adamc@1080 237 | ECon (dk, pc, cs, SOME e) =>
adamc@1080 238 let
adamc@1080 239 val (e, st) = exp (env, e, st)
adamc@1080 240 in
adamc@1080 241 ((ECon (dk, pc, cs, SOME e), loc), st)
adamc@1080 242 end
adamc@1080 243 | EFfi _ => (e, st)
adamc@1080 244 | EFfiApp (m, x, es) =>
adamc@1080 245 let
adam@1663 246 val (es, st) = ListUtil.foldlMap (fn ((e, t), st) =>
adam@1663 247 let
adam@1663 248 val (e, st) = exp (env, e, st)
adam@1663 249 in
adam@1663 250 ((e, t), st)
adam@1663 251 end) st es
adamc@1080 252 in
adamc@1080 253 ((EFfiApp (m, x, es), loc), st)
adamc@1080 254 end
adamc@1080 255 | EApp (e1, e2) =>
adamc@1080 256 let
adamc@1080 257 val (e1, st) = exp (env, e1, st)
adamc@1080 258 val (e2, st) = exp (env, e2, st)
adamc@1080 259 in
adamc@1080 260 ((EApp (e1, e2), loc), st)
adamc@1080 261 end
adamc@1080 262 | EAbs (x, d, r, e) =>
adamc@1080 263 let
adamc@1080 264 val (e, st) = exp ((x, d) :: env, e, st)
adamc@1080 265 in
adamc@1080 266 ((EAbs (x, d, r, e), loc), st)
adamc@1080 267 end
adamc@1080 268 | ECApp (e, c) =>
adamc@1080 269 let
adamc@1080 270 val (e, st) = exp (env, e, st)
adamc@1080 271 in
adamc@1080 272 ((ECApp (e, c), loc), st)
adamc@1080 273 end
adamc@1185 274 | ECAbs _ => (e, st)
adamc@1120 275 | EKAbs _ => (e, st)
adamc@1080 276 | EKApp (e, k) =>
adamc@1080 277 let
adamc@1080 278 val (e, st) = exp (env, e, st)
adamc@1080 279 in
adamc@1080 280 ((EKApp (e, k), loc), st)
adamc@1080 281 end
adamc@1080 282 | ERecord fs =>
adamc@1080 283 let
adamc@1080 284 val (fs, st) = ListUtil.foldlMap (fn ((c1, e, c2), st) =>
adamc@1080 285 let
adamc@1080 286 val (e, st) = exp (env, e, st)
adamc@1080 287 in
adamc@1080 288 ((c1, e, c2), st)
adamc@1080 289 end) st fs
adamc@1080 290 in
adamc@1080 291 ((ERecord fs, loc), st)
adamc@1080 292 end
adamc@1080 293 | EField (e, c, cs) =>
adamc@1080 294 let
adamc@1080 295 val (e, st) = exp (env, e, st)
adamc@1080 296 in
adamc@1080 297 ((EField (e, c, cs), loc), st)
adamc@1080 298 end
adamc@1080 299 | EConcat (e1, c1, e2, c2) =>
adamc@1080 300 let
adamc@1080 301 val (e1, st) = exp (env, e1, st)
adamc@1080 302 val (e2, st) = exp (env, e2, st)
adamc@1080 303 in
adamc@1080 304 ((EConcat (e1, c1, e2, c2), loc), st)
adamc@1080 305 end
adamc@1080 306 | ECut (e, c, cs) =>
adamc@1080 307 let
adamc@1080 308 val (e, st) = exp (env, e, st)
adamc@1080 309 in
adamc@1080 310 ((ECut (e, c, cs), loc), st)
adamc@1080 311 end
adamc@1080 312 | ECutMulti (e, c, cs) =>
adamc@1080 313 let
adamc@1080 314 val (e, st) = exp (env, e, st)
adamc@1080 315 in
adamc@1080 316 ((ECutMulti (e, c, cs), loc), st)
adamc@1080 317 end
adamc@1080 318
adamc@1080 319 | ECase (e, pes, cs) =>
adamc@1080 320 let
adamc@1080 321 val (e, st) = exp (env, e, st)
adamc@1080 322 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
adamc@1080 323 let
adamc@1080 324 val (e, st) = exp (E.patBindsL p @ env, e, st)
adamc@1080 325 in
adamc@1080 326 ((p, e), st)
adamc@1080 327 end) st pes
adamc@1080 328 in
adamc@1080 329 ((ECase (e, pes, cs), loc), st)
adamc@1080 330 end
adamc@1080 331
adamc@1080 332 | EWrite e =>
adamc@1080 333 let
adamc@1080 334 val (e, st) = exp (env, e, st)
adamc@1080 335 in
adamc@1080 336 ((EWrite e, loc), st)
adamc@1080 337 end
adamc@1080 338 | EClosure (n, es) =>
adamc@1080 339 let
adamc@1080 340 val (es, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st es
adamc@1080 341 in
adamc@1080 342 ((EClosure (n, es), loc), st)
adamc@1080 343 end
adamc@1080 344 | ELet (x, t, e1, e2) =>
adamc@1080 345 let
adamc@1080 346 val (e1, st) = exp (env, e1, st)
adamc@1080 347 val (e2, st) = exp ((x, t) :: env, e2, st)
adamc@1080 348 in
adamc@1080 349 ((ELet (x, t, e1, e2), loc), st)
adamc@1080 350 end
adam@1848 351 | EServerCall (n, es, t, fm) =>
adamc@1080 352 let
adamc@1080 353 val (es, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st es
adamc@1080 354 in
adam@1848 355 ((EServerCall (n, es, t, fm), loc), st)
adamc@1080 356 end
adamc@482 357 in
adamc@482 358 case getApp e of
adamc@1080 359 NONE => default ()
adamc@488 360 | SOME (f, xs) =>
adamc@485 361 case IM.find (#funcs st, f) of
adamc@1272 362 NONE => ((*print ("No find: " ^ Int.toString f ^ "\n");*) default ())
adam@1675 363 | SOME {name, args, body, typ, tag, constArgs} =>
adamc@488 364 let
adamc@1080 365 val (xs, st) = ListUtil.foldlMap (fn (e, st) => exp (env, e, st)) st xs
adamc@1080 366
adamc@721 367 (*val () = Print.prefaces "Consider" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 368 (e, ErrorMsg.dummySpan))]*)
adamc@721 369
adamc@488 370 val loc = ErrorMsg.dummySpan
adamc@488 371
adam@1677 372 val oldXs = xs
adam@1677 373
adam@1675 374 fun findSplit av (constArgs, xs, typ, fxs, fvs) =
adamc@488 375 case (#1 typ, xs) of
adamc@488 376 (TFun (dom, ran), e :: xs') =>
adam@1675 377 if constArgs > 0 then
adam@1677 378 if functionInside dom then
adam@1677 379 (rev (e :: fxs), xs', IS.union (fvs, freeVars e))
adam@1677 380 else
adam@1677 381 findSplit av (constArgs - 1,
adam@1677 382 xs',
adam@1677 383 ran,
adam@1677 384 e :: fxs,
adam@1677 385 IS.union (fvs, freeVars e))
adam@1675 386 else
adam@1677 387 ([], oldXs, IS.empty)
adam@1677 388 | _ => ([], oldXs, IS.empty)
adamc@488 389
adam@1675 390 val (fxs, xs, fvs) = findSplit true (constArgs, xs, typ, [], IS.empty)
adam@1355 391
adam@1314 392 val vts = map (fn n => #2 (List.nth (env, n))) (IS.listItems fvs)
adamc@1079 393 val fxs' = map (squish (IS.listItems fvs)) fxs
adam@1362 394
adam@1362 395 val p_bool = Print.PD.string o Bool.toString
adamc@488 396 in
adam@1355 397 (*Print.prefaces "Func" [("name", Print.PD.string name),
adam@1355 398 ("e", CorePrint.p_exp CoreEnv.empty e),
adam@1355 399 ("fxs'", Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')];*)
adam@1675 400 if List.all (fn (ERel _, _) => true
adam@1675 401 | _ => false) fxs' then
adam@1675 402 default ()
adamc@488 403 else
adam@1667 404 case KM.find (args, (vts, fxs')) of
adam@1667 405 SOME f' =>
adamc@485 406 let
adamc@488 407 val e = (ENamed f', loc)
adamc@488 408 val e = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 409 e fvs
adamc@1079 410 val e = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 411 e xs
adamc@488 412 in
adamc@488 413 (*Print.prefaces "Brand new (reuse)"
adamc@721 414 [("e'", CorePrint.p_exp CoreEnv.empty e)];*)
adamc@1080 415 (e, st)
adamc@488 416 end
adam@1667 417 | NONE =>
adamc@488 418 let
adamc@800 419 (*val () = Print.prefaces "New one"
adam@1667 420 [("name", Print.PD.string name),
adam@1667 421 ("f", Print.PD.string (Int.toString f)),
adam@1667 422 ("|fvs|", Print.PD.string (Int.toString (IS.numItems fvs))),
adam@1667 423 ("|fxs|", Print.PD.string (Int.toString (length fxs))),
adam@1766 424 ("fxs'", Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs'),
adam@1667 425 ("spec", Print.PD.string (Bool.toString (IS.member (#specialized st, f))))]*)
adamc@800 426
adamc@818 427 (*val () = Print.prefaces ("Yes(" ^ name ^ ")")
adamc@818 428 [("fxs'",
adamc@818 429 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')]*)
adamc@818 430
adam@1675 431 (*val () = Print.prefaces name
adam@1675 432 [("Available", Print.PD.string (Int.toString constArgs)),
adam@1675 433 ("Used", Print.PD.string (Int.toString (length fxs'))),
adam@1675 434 ("fxs'",
adam@1675 435 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')]*)
adam@1675 436
adamc@1079 437 fun subBody (body, typ, fxs') =
adamc@1079 438 case (#1 body, #1 typ, fxs') of
adamc@488 439 (_, _, []) => SOME (body, typ)
adamc@1079 440 | (EAbs (_, _, _, body'), TFun (_, typ'), x :: fxs'') =>
adamc@488 441 let
adamc@1079 442 val body'' = E.subExpInExp (0, x) body'
adamc@488 443 in
adamc@488 444 subBody (body'',
adamc@488 445 typ',
adamc@1079 446 fxs'')
adamc@488 447 end
adamc@488 448 | _ => NONE
adamc@488 449 in
adamc@1079 450 case subBody (body, typ, fxs') of
adamc@1080 451 NONE => default ()
adamc@488 452 | SOME (body', typ') =>
adamc@488 453 let
adamc@488 454 val f' = #maxName st
adam@1314 455 val args = KM.insert (args, (vts, fxs'), f')
adamc@488 456 val funcs = IM.insert (#funcs st, f, {name = name,
adamc@488 457 args = args,
adamc@488 458 body = body,
adamc@488 459 typ = typ,
adam@1675 460 tag = tag,
adam@1766 461 constArgs = calcConstArgs (IS.singleton f) body})
adamc@1079 462
adamc@488 463 val st = {
adamc@488 464 maxName = f' + 1,
adamc@488 465 funcs = funcs,
adamc@1079 466 decls = #decls st,
adamc@1080 467 specialized = IS.add (#specialized st, f')
adamc@488 468 }
adamc@487 469
adamc@488 470 (*val () = Print.prefaces "specExp"
adamc@488 471 [("f", CorePrint.p_exp env (ENamed f, loc)),
adamc@488 472 ("f'", CorePrint.p_exp env (ENamed f', loc)),
adamc@488 473 ("xs", Print.p_list (CorePrint.p_exp env) xs),
adamc@488 474 ("fxs'", Print.p_list
adamc@488 475 (CorePrint.p_exp E.empty) fxs'),
adamc@488 476 ("e", CorePrint.p_exp env (e, loc))]*)
adamc@488 477 val (body', typ') = IS.foldl (fn (n, (body', typ')) =>
adamc@488 478 let
adamc@521 479 val (x, xt) = List.nth (env, n)
adamc@488 480 in
adamc@488 481 ((EAbs (x, xt, typ', body'),
adamc@488 482 loc),
adamc@488 483 (TFun (xt, typ'), loc))
adamc@488 484 end)
adamc@488 485 (body', typ') fvs
adamc@1272 486 (*val () = print ("NEW: " ^ name ^ "__" ^ Int.toString f' ^ "\n");*)
adamc@1272 487 val body' = ReduceLocal.reduceExp body'
adamc@1080 488 (*val () = Print.preface ("PRE", CorePrint.p_exp CoreEnv.empty body')*)
adamc@1080 489 val (body', st) = exp (env, body', st)
adamc@482 490
adamc@488 491 val e' = (ENamed f', loc)
adamc@488 492 val e' = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 493 e' fvs
adamc@1079 494 val e' = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 495 e' xs
adam@1362 496
adamc@488 497 (*val () = Print.prefaces "Brand new"
adamc@721 498 [("e'", CorePrint.p_exp CoreEnv.empty e'),
adamc@1080 499 ("e", CorePrint.p_exp CoreEnv.empty e),
adamc@721 500 ("body'", CorePrint.p_exp CoreEnv.empty body')]*)
adamc@488 501 in
adamc@1080 502 (e',
adamc@488 503 {maxName = #maxName st,
adamc@488 504 funcs = #funcs st,
adamc@1079 505 decls = (name, f', typ', body', tag) :: #decls st,
adamc@1079 506 specialized = #specialized st})
adamc@488 507 end
adamc@485 508 end
adamc@488 509 end
adamc@485 510 end
adamc@482 511
adamc@521 512 fun doDecl (d, (st : state, changed)) =
adamc@488 513 let
adamc@521 514 (*val befor = Time.now ()*)
adamc@482 515
adamc@453 516 val funcs = #funcs st
adamc@453 517 val funcs =
adamc@453 518 case #1 d of
adamc@453 519 DValRec vis =>
adam@1766 520 let
adam@1766 521 val fs = foldl (fn ((_, n, _, _, _), fs) => IS.add (fs, n)) IS.empty vis
adam@1766 522 val constArgs = foldl (fn ((_, _, _, e, _), constArgs) =>
adam@1766 523 Int.min (constArgs, calcConstArgs fs e))
adam@1766 524 maxInt vis
adam@1766 525 in
adam@1766 526 foldl (fn ((x, n, c, e, tag), funcs) =>
adam@1766 527 IM.insert (funcs, n, {name = x,
adam@1766 528 args = KM.empty,
adam@1766 529 body = e,
adam@1766 530 typ = c,
adam@1766 531 tag = tag,
adam@1766 532 constArgs = constArgs}))
adam@1766 533 funcs vis
adam@1766 534 end
adamc@453 535 | _ => funcs
adamc@453 536
adamc@453 537 val st = {maxName = #maxName st,
adamc@453 538 funcs = funcs,
adamc@1079 539 decls = [],
adamc@1079 540 specialized = #specialized st}
adamc@453 541
adamc@482 542 (*val () = Print.prefaces "decl" [("d", CorePrint.p_decl CoreEnv.empty d)]*)
adamc@521 543
adamc@522 544 val (d', st) =
adamc@522 545 if isPoly d then
adamc@522 546 (d, st)
adamc@522 547 else
adamc@1080 548 case #1 d of
adamc@1080 549 DVal (x, n, t, e, s) =>
adamc@1080 550 let
adam@1362 551 (*val () = Print.preface ("Visiting", Print.box [Print.PD.string (x ^ "__" ^ Int.toString n),
adam@1362 552 Print.space,
adam@1362 553 Print.PD.string ":",
adam@1362 554 Print.space,
adam@1362 555 CorePrint.p_con CoreEnv.empty t])*)
adam@1362 556
adamc@1080 557 val (e, st) = exp ([], e, st)
adamc@1080 558 in
adamc@1080 559 ((DVal (x, n, t, e, s), #2 d), st)
adamc@1080 560 end
adamc@1080 561 | DValRec vis =>
adamc@1080 562 let
adamc@1120 563 (*val () = Print.preface ("Visiting", Print.p_list (fn vi =>
adam@1362 564 Print.box [Print.PD.string (#1 vi ^ "__"
adam@1362 565 ^ Int.toString
adam@1362 566 (#2 vi)),
adam@1362 567 Print.space,
adam@1362 568 Print.PD.string ":",
adam@1362 569 Print.space,
adam@1362 570 CorePrint.p_con CoreEnv.empty (#3 vi)])
adamc@1120 571 vis)*)
adamc@1120 572
adamc@1080 573 val (vis, st) = ListUtil.foldlMap (fn ((x, n, t, e, s), st) =>
adamc@1080 574 let
adamc@1080 575 val (e, st) = exp ([], e, st)
adamc@1080 576 in
adamc@1080 577 ((x, n, t, e, s), st)
adamc@1080 578 end) st vis
adamc@1080 579 in
adamc@1080 580 ((DValRec vis, #2 d), st)
adamc@1080 581 end
adamc@1080 582 | DTable (s, n, t, s1, e1, t1, e2, t2) =>
adamc@1080 583 let
adamc@1080 584 val (e1, st) = exp ([], e1, st)
adamc@1080 585 val (e2, st) = exp ([], e2, st)
adamc@1080 586 in
adamc@1080 587 ((DTable (s, n, t, s1, e1, t2, e2, t2), #2 d), st)
adamc@1080 588 end
adamc@1080 589 | DView (x, n, s, e, t) =>
adamc@1080 590 let
adamc@1080 591 val (e, st) = exp ([], e, st)
adamc@1080 592 in
adamc@1080 593 ((DView (x, n, s, e, t), #2 d), st)
adamc@1080 594 end
adamc@1080 595 | DTask (e1, e2) =>
adamc@1080 596 let
adamc@1080 597 val (e1, st) = exp ([], e1, st)
adamc@1080 598 val (e2, st) = exp ([], e2, st)
adamc@1080 599 in
adamc@1080 600 ((DTask (e1, e2), #2 d), st)
adamc@1080 601 end
adamc@1080 602 | _ => (d, st)
adamc@1080 603
adamc@482 604 (*val () = print "/decl\n"*)
adamc@443 605
adamc@443 606 val funcs = #funcs st
adamc@443 607 val funcs =
adamc@443 608 case #1 d of
adamc@443 609 DVal (x, n, c, e as (EAbs _, _), tag) =>
adamc@443 610 IM.insert (funcs, n, {name = x,
adamc@453 611 args = KM.empty,
adamc@443 612 body = e,
adamc@443 613 typ = c,
adam@1675 614 tag = tag,
adam@1766 615 constArgs = calcConstArgs (IS.singleton n) e})
adamc@469 616 | DVal (_, n, _, (ENamed n', _), _) =>
adamc@469 617 (case IM.find (funcs, n') of
adamc@469 618 NONE => funcs
adamc@469 619 | SOME v => IM.insert (funcs, n, v))
adamc@443 620 | _ => funcs
adamc@443 621
adamc@453 622 val (changed, ds) =
adamc@443 623 case #decls st of
adamc@453 624 [] => (changed, [d'])
adamc@453 625 | vis =>
adamc@453 626 (true, case d' of
adamc@453 627 (DValRec vis', _) => [(DValRec (vis @ vis'), ErrorMsg.dummySpan)]
adamc@453 628 | _ => [(DValRec vis, ErrorMsg.dummySpan), d'])
adamc@443 629 in
adamc@802 630 (*Print.prefaces "doDecl" [("d", CorePrint.p_decl E.empty d),
adamc@802 631 ("d'", CorePrint.p_decl E.empty d')];*)
adamc@521 632 (ds, ({maxName = #maxName st,
adamc@453 633 funcs = funcs,
adamc@1079 634 decls = [],
adamc@1079 635 specialized = #specialized st}, changed))
adamc@443 636 end
adamc@443 637
adamc@1120 638 (*val () = Print.preface ("RESET", CorePrint.p_file CoreEnv.empty file)*)
adamc@1079 639 val (ds, (st, changed)) = ListUtil.foldlMapConcat doDecl
adamc@521 640 ({maxName = U.File.maxName file + 1,
adamc@1080 641 funcs = funcs,
adamc@1079 642 decls = [],
adamc@1079 643 specialized = specialized},
adamc@488 644 false)
adamc@488 645 file
adamc@443 646 in
adamc@1120 647 (*print ("Changed = " ^ Bool.toString changed ^ "\n");*)
adamc@1080 648 (changed, ds, #funcs st, #specialized st)
adamc@443 649 end
adamc@443 650
adamc@1080 651 fun specializeL (funcs, specialized) file =
adamc@453 652 let
adamc@721 653 val file = ReduceLocal.reduce file
adamc@520 654 (*val file = ReduceLocal.reduce file*)
adamc@1080 655 val (changed, file, funcs, specialized) = specialize' (funcs, specialized) file
adamc@520 656 (*val file = ReduceLocal.reduce file
adamc@520 657 val file = CoreUntangle.untangle file
adamc@488 658 val file = Shake.shake file*)
adamc@453 659 in
adamc@488 660 (*print "Round over\n";*)
adamc@453 661 if changed then
adamc@520 662 let
adamc@721 663 (*val file = ReduceLocal.reduce file*)
adamc@802 664 (*val () = Print.prefaces "Pre-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 665 val file = CoreUntangle.untangle file
adamc@802 666 (*val () = Print.prefaces "Post-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 667 val file = Shake.shake file
adamc@520 668 in
adamc@520 669 (*print "Again!\n";*)
adamc@1080 670 (*Print.prefaces "Again" [("file", CorePrint.p_file CoreEnv.empty file)];*)
adamc@1080 671 specializeL (funcs, specialized) file
adamc@520 672 end
adamc@453 673 else
adamc@453 674 file
adamc@453 675 end
adamc@453 676
adamc@1080 677 val specialize = specializeL (IM.empty, IS.empty)
adamc@1079 678
adamc@443 679 end