annotate src/especialize.sml @ 767:d27ed5ddeb52

Add 'library' directive
author Adam Chlipala <adamc@hcoop.net>
date Sat, 02 May 2009 12:50:52 -0400
parents 9864b64b1700
children dc3fc3f3b834
rev   line source
adamc@443 1 (* Copyright (c) 2008, 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
adamc@479 38 type ord_key = exp list
adamc@479 39 val compare = Order.joinL U.Exp.compare
adamc@443 40 end
adamc@443 41
adamc@453 42 structure KM = BinaryMapFn(K)
adamc@443 43 structure IM = IntBinaryMap
adamc@482 44 structure IS = IntBinarySet
adamc@443 45
adamc@626 46 val freeVars = U.Exp.foldB {kind = fn (_, _, xs) => xs,
adamc@488 47 con = fn (_, _, xs) => xs,
adamc@488 48 exp = fn (bound, e, xs) =>
adamc@488 49 case e of
adamc@488 50 ERel x =>
adamc@488 51 if x >= bound then
adamc@488 52 IS.add (xs, x - bound)
adamc@488 53 else
adamc@488 54 xs
adamc@488 55 | _ => xs,
adamc@488 56 bind = fn (bound, b) =>
adamc@488 57 case b of
adamc@488 58 U.Exp.RelE _ => bound + 1
adamc@488 59 | _ => bound}
adamc@488 60 0 IS.empty
adamc@479 61
adamc@522 62 val isPoly = U.Decl.exists {kind = fn _ => false,
adamc@522 63 con = fn _ => false,
adamc@522 64 exp = fn ECAbs _ => true
adamc@522 65 | _ => false,
adamc@522 66 decl = fn _ => false}
adamc@522 67
adamc@488 68 fun positionOf (v : int, ls) =
adamc@488 69 let
adamc@488 70 fun pof (pos, ls) =
adamc@488 71 case ls of
adamc@488 72 [] => raise Fail "Defunc.positionOf"
adamc@488 73 | v' :: ls' =>
adamc@488 74 if v = v' then
adamc@488 75 pos
adamc@488 76 else
adamc@488 77 pof (pos + 1, ls')
adamc@488 78 in
adamc@488 79 pof (0, ls)
adamc@488 80 end
adamc@488 81
adamc@488 82 fun squish fvs =
adamc@626 83 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@488 84 con = fn _ => fn c => c,
adamc@488 85 exp = fn bound => fn e =>
adamc@479 86 case e of
adamc@488 87 ERel x =>
adamc@488 88 if x >= bound then
adamc@488 89 ERel (positionOf (x - bound, fvs) + bound)
adamc@488 90 else
adamc@488 91 e
adamc@488 92 | _ => e,
adamc@488 93 bind = fn (bound, b) =>
adamc@488 94 case b of
adamc@488 95 U.Exp.RelE _ => bound + 1
adamc@488 96 | _ => bound}
adamc@488 97 0
adamc@453 98
adamc@443 99 type func = {
adamc@443 100 name : string,
adamc@453 101 args : int KM.map,
adamc@443 102 body : exp,
adamc@443 103 typ : con,
adamc@443 104 tag : string
adamc@443 105 }
adamc@443 106
adamc@443 107 type state = {
adamc@443 108 maxName : int,
adamc@443 109 funcs : func IM.map,
adamc@443 110 decls : (string * int * con * exp * string) list
adamc@443 111 }
adamc@443 112
adamc@488 113 fun default (_, x, st) = (x, st)
adamc@443 114
adamc@453 115 fun specialize' file =
adamc@443 116 let
adamc@488 117 fun bind (env, b) =
adamc@488 118 case b of
adamc@521 119 U.Decl.RelE xt => xt :: env
adamc@521 120 | _ => env
adamc@488 121
adamc@488 122 fun exp (env, e, st : state) =
adamc@482 123 let
adamc@721 124 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 125 (e, ErrorMsg.dummySpan))]*)
adamc@721 126
adamc@488 127 fun getApp e =
adamc@482 128 case e of
adamc@488 129 ENamed f => SOME (f, [])
adamc@482 130 | EApp (e1, e2) =>
adamc@488 131 (case getApp (#1 e1) of
adamc@482 132 NONE => NONE
adamc@488 133 | SOME (f, xs) => SOME (f, xs @ [e2]))
adamc@482 134 | _ => NONE
adamc@482 135 in
adamc@482 136 case getApp e of
adamc@721 137 NONE => ((*Print.prefaces "No" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 138 (e, ErrorMsg.dummySpan))];*)
adamc@721 139 (e, st))
adamc@488 140 | SOME (f, xs) =>
adamc@485 141 case IM.find (#funcs st, f) of
adamc@485 142 NONE => (e, st)
adamc@485 143 | SOME {name, args, body, typ, tag} =>
adamc@488 144 let
adamc@721 145 (*val () = Print.prefaces "Consider" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 146 (e, ErrorMsg.dummySpan))]*)
adamc@721 147
adamc@488 148 val functionInside = U.Con.exists {kind = fn _ => false,
adamc@488 149 con = fn TFun _ => true
adamc@488 150 | CFfi ("Basis", "transaction") => true
adamc@488 151 | _ => false}
adamc@488 152 val loc = ErrorMsg.dummySpan
adamc@488 153
adamc@488 154 fun findSplit (xs, typ, fxs, fvs) =
adamc@488 155 case (#1 typ, xs) of
adamc@488 156 (TFun (dom, ran), e :: xs') =>
adamc@488 157 if functionInside dom then
adamc@488 158 findSplit (xs',
adamc@488 159 ran,
adamc@488 160 e :: fxs,
adamc@488 161 IS.union (fvs, freeVars e))
adamc@488 162 else
adamc@488 163 (rev fxs, xs, fvs)
adamc@488 164 | _ => (rev fxs, xs, fvs)
adamc@488 165
adamc@488 166 val (fxs, xs, fvs) = findSplit (xs, typ, [], IS.empty)
adamc@488 167
adamc@488 168 val fxs' = map (squish (IS.listItems fvs)) fxs
adamc@488 169
adamc@488 170 fun firstRel () =
adamc@488 171 case fxs' of
adamc@488 172 (ERel _, _) :: _ => true
adamc@488 173 | _ => false
adamc@488 174 in
adamc@488 175 if firstRel ()
adamc@488 176 orelse List.all (fn (ERel _, _) => true
adamc@488 177 | _ => false) fxs' then
adamc@488 178 (e, st)
adamc@488 179 else
adamc@488 180 case KM.find (args, fxs') of
adamc@488 181 SOME f' =>
adamc@485 182 let
adamc@488 183 val e = (ENamed f', loc)
adamc@488 184 val e = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 185 e fvs
adamc@488 186 val e = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 187 e xs
adamc@488 188 in
adamc@488 189 (*Print.prefaces "Brand new (reuse)"
adamc@721 190 [("e'", CorePrint.p_exp CoreEnv.empty e)];*)
adamc@488 191 (#1 e, st)
adamc@488 192 end
adamc@488 193 | NONE =>
adamc@488 194 let
adamc@488 195 fun subBody (body, typ, fxs') =
adamc@488 196 case (#1 body, #1 typ, fxs') of
adamc@488 197 (_, _, []) => SOME (body, typ)
adamc@488 198 | (EAbs (_, _, _, body'), TFun (_, typ'), x :: fxs'') =>
adamc@488 199 let
adamc@488 200 val body'' = E.subExpInExp (0, x) body'
adamc@488 201 in
adamc@488 202 subBody (body'',
adamc@488 203 typ',
adamc@488 204 fxs'')
adamc@488 205 end
adamc@488 206 | _ => NONE
adamc@488 207 in
adamc@488 208 case subBody (body, typ, fxs') of
adamc@488 209 NONE => (e, st)
adamc@488 210 | SOME (body', typ') =>
adamc@488 211 let
adamc@488 212 val f' = #maxName st
adamc@488 213 val args = KM.insert (args, fxs', f')
adamc@488 214 val funcs = IM.insert (#funcs st, f, {name = name,
adamc@488 215 args = args,
adamc@488 216 body = body,
adamc@488 217 typ = typ,
adamc@488 218 tag = tag})
adamc@488 219 val st = {
adamc@488 220 maxName = f' + 1,
adamc@488 221 funcs = funcs,
adamc@488 222 decls = #decls st
adamc@488 223 }
adamc@487 224
adamc@488 225 (*val () = Print.prefaces "specExp"
adamc@488 226 [("f", CorePrint.p_exp env (ENamed f, loc)),
adamc@488 227 ("f'", CorePrint.p_exp env (ENamed f', loc)),
adamc@488 228 ("xs", Print.p_list (CorePrint.p_exp env) xs),
adamc@488 229 ("fxs'", Print.p_list
adamc@488 230 (CorePrint.p_exp E.empty) fxs'),
adamc@488 231 ("e", CorePrint.p_exp env (e, loc))]*)
adamc@488 232 val (body', typ') = IS.foldl (fn (n, (body', typ')) =>
adamc@488 233 let
adamc@521 234 val (x, xt) = List.nth (env, n)
adamc@488 235 in
adamc@488 236 ((EAbs (x, xt, typ', body'),
adamc@488 237 loc),
adamc@488 238 (TFun (xt, typ'), loc))
adamc@488 239 end)
adamc@488 240 (body', typ') fvs
adamc@488 241 val (body', st) = specExp env st body'
adamc@482 242
adamc@488 243 val e' = (ENamed f', loc)
adamc@488 244 val e' = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 245 e' fvs
adamc@488 246 val e' = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 247 e' xs
adamc@488 248 (*val () = Print.prefaces "Brand new"
adamc@721 249 [("e'", CorePrint.p_exp CoreEnv.empty e'),
adamc@721 250 ("e", CorePrint.p_exp CoreEnv.empty (e, loc)),
adamc@721 251 ("body'", CorePrint.p_exp CoreEnv.empty body')]*)
adamc@488 252 in
adamc@488 253 (#1 e',
adamc@488 254 {maxName = #maxName st,
adamc@488 255 funcs = #funcs st,
adamc@488 256 decls = (name, f', typ', body', tag) :: #decls st})
adamc@488 257 end
adamc@485 258 end
adamc@488 259 end
adamc@485 260 end
adamc@482 261
adamc@626 262 and specExp env = U.Exp.foldMapB {kind = default, con = default, exp = exp, bind = bind} env
adamc@482 263
adamc@626 264 val specDecl = U.Decl.foldMapB {kind = default, con = default, exp = exp, decl = default, bind = bind}
adamc@482 265
adamc@521 266 fun doDecl (d, (st : state, changed)) =
adamc@488 267 let
adamc@521 268 (*val befor = Time.now ()*)
adamc@482 269
adamc@453 270 val funcs = #funcs st
adamc@453 271 val funcs =
adamc@453 272 case #1 d of
adamc@453 273 DValRec vis =>
adamc@453 274 foldl (fn ((x, n, c, e, tag), funcs) =>
adamc@453 275 IM.insert (funcs, n, {name = x,
adamc@453 276 args = KM.empty,
adamc@453 277 body = e,
adamc@453 278 typ = c,
adamc@453 279 tag = tag}))
adamc@453 280 funcs vis
adamc@453 281 | _ => funcs
adamc@453 282
adamc@453 283 val st = {maxName = #maxName st,
adamc@453 284 funcs = funcs,
adamc@453 285 decls = []}
adamc@453 286
adamc@482 287 (*val () = Print.prefaces "decl" [("d", CorePrint.p_decl CoreEnv.empty d)]*)
adamc@521 288
adamc@522 289 val (d', st) =
adamc@522 290 if isPoly d then
adamc@522 291 (d, st)
adamc@522 292 else
adamc@522 293 specDecl [] st d
adamc@521 294
adamc@482 295 (*val () = print "/decl\n"*)
adamc@443 296
adamc@443 297 val funcs = #funcs st
adamc@443 298 val funcs =
adamc@443 299 case #1 d of
adamc@443 300 DVal (x, n, c, e as (EAbs _, _), tag) =>
adamc@443 301 IM.insert (funcs, n, {name = x,
adamc@453 302 args = KM.empty,
adamc@443 303 body = e,
adamc@443 304 typ = c,
adamc@443 305 tag = tag})
adamc@469 306 | DVal (_, n, _, (ENamed n', _), _) =>
adamc@469 307 (case IM.find (funcs, n') of
adamc@469 308 NONE => funcs
adamc@469 309 | SOME v => IM.insert (funcs, n, v))
adamc@443 310 | _ => funcs
adamc@443 311
adamc@453 312 val (changed, ds) =
adamc@443 313 case #decls st of
adamc@453 314 [] => (changed, [d'])
adamc@453 315 | vis =>
adamc@453 316 (true, case d' of
adamc@453 317 (DValRec vis', _) => [(DValRec (vis @ vis'), ErrorMsg.dummySpan)]
adamc@453 318 | _ => [(DValRec vis, ErrorMsg.dummySpan), d'])
adamc@443 319 in
adamc@521 320 (*Print.prefaces "doDecl" [("d", CorePrint.p_decl E.empty d),
adamc@521 321 ("t", Print.PD.string (Real.toString (Time.toReal
adamc@521 322 (Time.- (Time.now (), befor)))))];*)
adamc@521 323 (ds, ({maxName = #maxName st,
adamc@453 324 funcs = funcs,
adamc@453 325 decls = []}, changed))
adamc@443 326 end
adamc@443 327
adamc@521 328 val (ds, (_, changed)) = ListUtil.foldlMapConcat doDecl
adamc@521 329 ({maxName = U.File.maxName file + 1,
adamc@488 330 funcs = IM.empty,
adamc@488 331 decls = []},
adamc@488 332 false)
adamc@488 333 file
adamc@443 334 in
adamc@453 335 (changed, ds)
adamc@443 336 end
adamc@443 337
adamc@453 338 fun specialize file =
adamc@453 339 let
adamc@721 340 val file = ReduceLocal.reduce file
adamc@721 341 (*val () = Print.prefaces "Intermediate" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 342 (*val file = ReduceLocal.reduce file*)
adamc@453 343 val (changed, file) = specialize' file
adamc@520 344 (*val file = ReduceLocal.reduce file
adamc@520 345 val file = CoreUntangle.untangle file
adamc@488 346 val file = Shake.shake file*)
adamc@453 347 in
adamc@488 348 (*print "Round over\n";*)
adamc@453 349 if changed then
adamc@520 350 let
adamc@721 351 (*val file = ReduceLocal.reduce file*)
adamc@520 352 val file = CoreUntangle.untangle file
adamc@520 353 val file = Shake.shake file
adamc@520 354 in
adamc@520 355 (*print "Again!\n";*)
adamc@520 356 specialize file
adamc@520 357 end
adamc@453 358 else
adamc@453 359 file
adamc@453 360 end
adamc@453 361
adamc@443 362 end