annotate src/flat_util.sml @ 107:bed5cf0b6b75

Optimizing attrification of constants
author Adam Chlipala <adamc@hcoop.net>
date Thu, 10 Jul 2008 15:58:16 -0400
parents d101cb1efe55
children
rev   line source
adamc@26 1 (* Copyright (c) 2008, Adam Chlipala
adamc@26 2 * All rights reserved.
adamc@26 3 *
adamc@26 4 * Redistribution and use in source and binary forms, with or without
adamc@26 5 * modification, are permitted provided that the following conditions are met:
adamc@26 6 *
adamc@26 7 * - Redistributions of source code must retain the above copyright notice,
adamc@26 8 * this list of conditions and the following disclaimer.
adamc@26 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@26 10 * this list of conditions and the following disclaimer in the documentation
adamc@26 11 * and/or other materials provided with the distribution.
adamc@26 12 * - The names of contributors may not be used to endorse or promote products
adamc@26 13 * derived from this software without specific prior written permission.
adamc@26 14 *
adamc@26 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@26 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@26 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@26 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@26 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@26 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@26 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@26 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@26 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@26 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@26 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@26 26 *)
adamc@26 27
adamc@26 28 structure FlatUtil :> FLAT_UTIL = struct
adamc@26 29
adamc@26 30 open Flat
adamc@26 31
adamc@26 32 structure S = Search
adamc@26 33
adamc@26 34 structure Typ = struct
adamc@26 35
adamc@28 36 fun join (o1, o2) =
adamc@28 37 case o1 of
adamc@28 38 EQUAL => o2 ()
adamc@28 39 | v => v
adamc@28 40
adamc@28 41 fun joinL f (os1, os2) =
adamc@28 42 case (os1, os2) of
adamc@28 43 (nil, nil) => EQUAL
adamc@28 44 | (nil, _) => LESS
adamc@28 45 | (h1 :: t1, h2 :: t2) =>
adamc@28 46 join (f (h1, h2), fn () => joinL f (t1, t2))
adamc@28 47 | (_ :: _, nil) => GREATER
adamc@28 48
adamc@28 49 fun compare ((t1, _), (t2, _)) =
adamc@28 50 case (t1, t2) of
adamc@29 51 (TTop, TTop) => EQUAL
adamc@29 52 | (TFun (d1, r1), TFun (d2, r2)) =>
adamc@28 53 join (compare (d1, d2), fn () => compare (r1, r2))
adamc@28 54 | (TCode (d1, r1), TCode (d2, r2)) =>
adamc@28 55 join (compare (d1, d2), fn () => compare (r1, r2))
adamc@28 56 | (TRecord xts1, TRecord xts2) =>
adamc@28 57 let
adamc@99 58 val xts1 = sortFields xts1
adamc@28 59 val xts2 = sortFields xts2
adamc@28 60 in
adamc@28 61 joinL compareFields (xts1, xts2)
adamc@28 62 end
adamc@28 63 | (TNamed n1, TNamed n2) => Int.compare (n1, n2)
adamc@52 64 | (TFfi (m1, x1), TFfi (m2, x2)) => join (String.compare (m1, m2), fn () => String.compare (x1, x2))
adamc@28 65
adamc@29 66 | (TTop, _) => LESS
adamc@29 67 | (_, TTop) => GREATER
adamc@29 68
adamc@28 69 | (TFun _, _) => LESS
adamc@28 70 | (_, TFun _) => GREATER
adamc@28 71
adamc@28 72 | (TCode _, _) => LESS
adamc@28 73 | (_, TCode _) => GREATER
adamc@28 74
adamc@28 75 | (TRecord _, _) => LESS
adamc@28 76 | (_, TRecord _) => GREATER
adamc@28 77
adamc@52 78 | (TNamed _, _) => LESS
adamc@52 79 | (_, TNamed _) => GREATER
adamc@52 80
adamc@28 81 and compareFields ((x1, t1), (x2, t2)) =
adamc@28 82 join (String.compare (x1, x2),
adamc@28 83 fn () => compare (t1, t2))
adamc@28 84
adamc@28 85 and sortFields xts = ListMergeSort.sort (fn (x, y) => compareFields (x, y) = GREATER) xts
adamc@28 86
adamc@26 87 fun mapfold fc =
adamc@26 88 let
adamc@26 89 fun mft c acc =
adamc@26 90 S.bindP (mft' c acc, fc)
adamc@26 91
adamc@26 92 and mft' (cAll as (c, loc)) =
adamc@26 93 case c of
adamc@29 94 TTop => S.return2 cAll
adamc@29 95 | TFun (t1, t2) =>
adamc@26 96 S.bind2 (mft t1,
adamc@26 97 fn t1' =>
adamc@26 98 S.map2 (mft t2,
adamc@26 99 fn t2' =>
adamc@26 100 (TFun (t1', t2'), loc)))
adamc@26 101 | TCode (t1, t2) =>
adamc@26 102 S.bind2 (mft t1,
adamc@26 103 fn t1' =>
adamc@26 104 S.map2 (mft t2,
adamc@26 105 fn t2' =>
adamc@26 106 (TCode (t1', t2'), loc)))
adamc@26 107 | TRecord xts =>
adamc@26 108 S.map2 (ListUtil.mapfold (fn (x, t) =>
adamc@26 109 S.map2 (mft t,
adamc@26 110 fn t' =>
adamc@26 111 (x, t')))
adamc@26 112 xts,
adamc@26 113 fn xts' => (TRecord xts', loc))
adamc@26 114 | TNamed _ => S.return2 cAll
adamc@52 115 | TFfi _ => S.return2 cAll
adamc@26 116 in
adamc@26 117 mft
adamc@26 118 end
adamc@26 119
adamc@26 120 fun map typ c =
adamc@26 121 case mapfold (fn c => fn () => S.Continue (typ c, ())) c () of
adamc@26 122 S.Return () => raise Fail "Flat_util.Typ.map"
adamc@26 123 | S.Continue (c, ()) => c
adamc@26 124
adamc@26 125 fun fold typ s c =
adamc@26 126 case mapfold (fn c => fn s => S.Continue (c, typ (c, s))) c s of
adamc@26 127 S.Continue (_, s) => s
adamc@26 128 | S.Return _ => raise Fail "FlatUtil.Typ.fold: Impossible"
adamc@26 129
adamc@26 130 fun exists typ k =
adamc@26 131 case mapfold (fn c => fn () =>
adamc@26 132 if typ c then
adamc@26 133 S.Return ()
adamc@26 134 else
adamc@26 135 S.Continue (c, ())) k () of
adamc@26 136 S.Return _ => true
adamc@26 137 | S.Continue _ => false
adamc@26 138
adamc@26 139 end
adamc@26 140
adamc@26 141 structure Exp = struct
adamc@26 142
adamc@26 143 datatype binder =
adamc@26 144 NamedT of string * int * typ option
adamc@26 145 | RelE of string * typ
adamc@26 146 | NamedE of string * int * typ * exp option
adamc@26 147
adamc@26 148 fun mapfoldB {typ = fc, exp = fe, bind} =
adamc@26 149 let
adamc@26 150 val mft = Typ.mapfold fc
adamc@26 151
adamc@26 152 fun mfe ctx e acc =
adamc@26 153 S.bindP (mfe' ctx e acc, fe ctx)
adamc@26 154
adamc@26 155 and mfe' ctx (eAll as (e, loc)) =
adamc@26 156 case e of
adamc@26 157 EPrim _ => S.return2 eAll
adamc@26 158 | ERel _ => S.return2 eAll
adamc@26 159 | ENamed _ => S.return2 eAll
adamc@52 160 | EFfi _ => S.return2 eAll
adamc@52 161 | EFfiApp (m, x, es) =>
adamc@52 162 S.map2 (ListUtil.mapfold (fn e => mfe ctx e) es,
adamc@52 163 fn es' =>
adamc@52 164 (EFfiApp (m, x, es'), loc))
adamc@26 165 | ECode _ => S.return2 eAll
adamc@26 166 | EApp (e1, e2) =>
adamc@26 167 S.bind2 (mfe ctx e1,
adamc@26 168 fn e1' =>
adamc@26 169 S.map2 (mfe ctx e2,
adamc@26 170 fn e2' =>
adamc@26 171 (EApp (e1', e2'), loc)))
adamc@26 172
adamc@26 173 | ERecord xes =>
adamc@29 174 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@29 175 S.bind2 (mfe ctx e,
adamc@26 176 fn e' =>
adamc@29 177 S.map2 (mft t,
adamc@29 178 fn t' =>
adamc@29 179 (x, e', t'))))
adamc@26 180 xes,
adamc@26 181 fn xes' =>
adamc@26 182 (ERecord xes', loc))
adamc@26 183 | EField (e, x) =>
adamc@26 184 S.map2 (mfe ctx e,
adamc@26 185 fn e' =>
adamc@26 186 (EField (e', x), loc))
adamc@26 187
adamc@26 188 | ELet (xes, e) =>
adamc@29 189 S.bind2 (ListUtil.mapfold (fn (x, t, e) =>
adamc@29 190 S.bind2 (mft t,
adamc@29 191 fn t' =>
adamc@29 192 S.map2 (mfe ctx e,
adamc@29 193 fn e' =>
adamc@29 194 (x, t', e'))))
adamc@26 195 xes,
adamc@26 196 fn xes' =>
adamc@26 197 S.map2 (mfe ctx e,
adamc@26 198 fn e' =>
adamc@26 199 (ELet (xes', e'), loc)))
adamc@102 200
adamc@102 201 | EStrcat (e1, e2) =>
adamc@102 202 S.bind2 (mfe ctx e1,
adamc@102 203 fn e1' =>
adamc@102 204 S.map2 (mfe ctx e2,
adamc@102 205 fn e2' =>
adamc@102 206 (EStrcat (e1', e2'), loc)))
adamc@102 207
adamc@102 208 | EWrite e =>
adamc@102 209 S.map2 (mfe ctx e,
adamc@102 210 fn e' =>
adamc@102 211 (EWrite e', loc))
adamc@106 212
adamc@106 213 | ESeq (e1, e2) =>
adamc@106 214 S.bind2 (mfe ctx e1,
adamc@106 215 fn e1' =>
adamc@106 216 S.map2 (mfe ctx e2,
adamc@106 217 fn e2' =>
adamc@106 218 (ESeq (e1', e2'), loc)))
adamc@26 219 in
adamc@26 220 mfe
adamc@26 221 end
adamc@26 222
adamc@26 223 fun mapfold {typ = fc, exp = fe} =
adamc@26 224 mapfoldB {typ = fc,
adamc@26 225 exp = fn () => fe,
adamc@26 226 bind = fn ((), _) => ()} ()
adamc@26 227
adamc@26 228 fun mapB {typ, exp, bind} ctx e =
adamc@26 229 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 230 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@26 231 bind = bind} ctx e () of
adamc@26 232 S.Continue (e, ()) => e
adamc@26 233 | S.Return _ => raise Fail "FlatUtil.Exp.mapB: Impossible"
adamc@26 234
adamc@26 235 fun map {typ, exp} e =
adamc@26 236 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 237 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@26 238 S.Return () => raise Fail "Flat_util.Exp.map"
adamc@26 239 | S.Continue (e, ()) => e
adamc@26 240
adamc@26 241 fun fold {typ, exp} s e =
adamc@26 242 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 243 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@26 244 S.Continue (_, s) => s
adamc@26 245 | S.Return _ => raise Fail "FlatUtil.Exp.fold: Impossible"
adamc@26 246
adamc@26 247 fun exists {typ, exp} k =
adamc@26 248 case mapfold {typ = fn c => fn () =>
adamc@26 249 if typ c then
adamc@26 250 S.Return ()
adamc@26 251 else
adamc@26 252 S.Continue (c, ()),
adamc@26 253 exp = fn e => fn () =>
adamc@26 254 if exp e then
adamc@26 255 S.Return ()
adamc@26 256 else
adamc@26 257 S.Continue (e, ())} k () of
adamc@26 258 S.Return _ => true
adamc@26 259 | S.Continue _ => false
adamc@26 260
adamc@26 261 end
adamc@26 262
adamc@26 263 structure Decl = struct
adamc@26 264
adamc@26 265 datatype binder = datatype Exp.binder
adamc@26 266
adamc@26 267 fun mapfoldB {typ = fc, exp = fe, decl = fd, bind} =
adamc@26 268 let
adamc@26 269 val mft = Typ.mapfold fc
adamc@26 270
adamc@26 271 val mfe = Exp.mapfoldB {typ = fc, exp = fe, bind = bind}
adamc@26 272
adamc@26 273 fun mfd ctx d acc =
adamc@26 274 S.bindP (mfd' ctx d acc, fd ctx)
adamc@26 275
adamc@26 276 and mfd' ctx (dAll as (d, loc)) =
adamc@26 277 case d of
adamc@26 278 DVal (x, n, t, e) =>
adamc@26 279 S.bind2 (mft t,
adamc@26 280 fn t' =>
adamc@26 281 S.map2 (mfe ctx e,
adamc@26 282 fn e' =>
adamc@26 283 (DVal (x, n, t', e'), loc)))
adamc@26 284 | DFun (n, x, dom, ran, e) =>
adamc@26 285 S.bind2 (mft dom,
adamc@26 286 fn dom' =>
adamc@26 287 S.bind2 (mft ran,
adamc@26 288 fn ran' =>
adamc@26 289 S.map2 (mfe ctx e,
adamc@26 290 fn e' =>
adamc@26 291 (DFun (n, x, dom', ran', e'), loc))))
adamc@101 292 | DPage (xts, e) =>
adamc@101 293 S.bind2 (ListUtil.mapfold (fn (x, t) =>
adamc@101 294 S.map2 (mft t,
adamc@101 295 fn t' =>
adamc@101 296 (x, t'))) xts,
adamc@101 297 fn xts' =>
adamc@101 298 S.map2 (mfe ctx e,
adamc@101 299 fn e' =>
adamc@101 300 (DPage (xts', e'), loc)))
adamc@26 301 in
adamc@26 302 mfd
adamc@26 303 end
adamc@26 304
adamc@26 305 fun mapfold {typ = fc, exp = fe, decl = fd} =
adamc@26 306 mapfoldB {typ = fc,
adamc@26 307 exp = fn () => fe,
adamc@26 308 decl = fn () => fd,
adamc@26 309 bind = fn ((), _) => ()} ()
adamc@26 310
adamc@26 311 fun fold {typ, exp, decl} s d =
adamc@26 312 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 313 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@26 314 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@26 315 S.Continue (_, s) => s
adamc@26 316 | S.Return _ => raise Fail "FlatUtil.Decl.fold: Impossible"
adamc@26 317
adamc@26 318 end
adamc@26 319
adamc@26 320 structure File = struct
adamc@26 321
adamc@26 322 datatype binder =
adamc@26 323 NamedT of string * int * typ option
adamc@26 324 | RelE of string * typ
adamc@26 325 | NamedE of string * int * typ * exp option
adamc@26 326 | F of int * string * Flat.typ * Flat.typ * Flat.exp
adamc@26 327
adamc@26 328 fun mapfoldB (all as {bind, ...}) =
adamc@26 329 let
adamc@26 330 val mfd = Decl.mapfoldB all
adamc@26 331
adamc@26 332 fun mff ctx ds =
adamc@26 333 case ds of
adamc@26 334 nil => S.return2 nil
adamc@26 335 | d :: ds' =>
adamc@26 336 S.bind2 (mfd ctx d,
adamc@26 337 fn d' =>
adamc@26 338 let
adamc@101 339 val ctx' =
adamc@26 340 case #1 d' of
adamc@101 341 DVal (x, n, t, e) => bind (ctx, NamedE (x, n, t, SOME e))
adamc@101 342 | DFun v => bind (ctx, F v)
adamc@101 343 | DPage _ => ctx
adamc@26 344 in
adamc@26 345 S.map2 (mff ctx' ds',
adamc@26 346 fn ds' =>
adamc@26 347 d' :: ds')
adamc@26 348 end)
adamc@26 349 in
adamc@26 350 mff
adamc@26 351 end
adamc@26 352
adamc@26 353 fun mapfold {typ = fc, exp = fe, decl = fd} =
adamc@26 354 mapfoldB {typ = fc,
adamc@26 355 exp = fn () => fe,
adamc@26 356 decl = fn () => fd,
adamc@26 357 bind = fn ((), _) => ()} ()
adamc@26 358
adamc@26 359 fun mapB {typ, exp, decl, bind} ctx ds =
adamc@26 360 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 361 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@26 362 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@26 363 bind = bind} ctx ds () of
adamc@26 364 S.Continue (ds, ()) => ds
adamc@26 365 | S.Return _ => raise Fail "FlatUtil.File.mapB: Impossible"
adamc@26 366
adamc@26 367 fun fold {typ, exp, decl} s d =
adamc@26 368 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 369 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@26 370 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@26 371 S.Continue (_, s) => s
adamc@26 372 | S.Return _ => raise Fail "FlatUtil.File.fold: Impossible"
adamc@26 373
adamc@26 374 end
adamc@26 375
adamc@26 376 end