annotate src/mono_util.sml @ 252:7e9bd70ad3ce

Monoized and optimized initial query test
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 Aug 2008 13:58:47 -0400
parents 326fb4686f60
children f31e8da68e90
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 MonoUtil :> MONO_UTIL = struct
adamc@26 29
adamc@26 30 open Mono
adamc@26 31
adamc@26 32 structure S = Search
adamc@26 33
adamc@26 34 structure Typ = struct
adamc@26 35
adamc@193 36 open Order
adamc@109 37
adamc@109 38 fun compare ((t1, _), (t2, _)) =
adamc@109 39 case (t1, t2) of
adamc@109 40 (TFun (d1, r1), TFun (d2, r2)) =>
adamc@109 41 join (compare (d1, d2), fn () => compare (r1, r2))
adamc@109 42 | (TRecord xts1, TRecord xts2) =>
adamc@109 43 let
adamc@109 44 val xts1 = sortFields xts1
adamc@109 45 val xts2 = sortFields xts2
adamc@109 46 in
adamc@109 47 joinL compareFields (xts1, xts2)
adamc@109 48 end
adamc@196 49 | (TDatatype (n1, _), TDatatype (n2, _)) => Int.compare (n1, n2)
adamc@109 50 | (TFfi (m1, x1), TFfi (m2, x2)) => join (String.compare (m1, m2), fn () => String.compare (x1, x2))
adamc@109 51
adamc@109 52 | (TFun _, _) => LESS
adamc@109 53 | (_, TFun _) => GREATER
adamc@109 54
adamc@109 55 | (TRecord _, _) => LESS
adamc@109 56 | (_, TRecord _) => GREATER
adamc@109 57
adamc@168 58 | (TDatatype _, _) => LESS
adamc@168 59 | (_, TDatatype _) => GREATER
adamc@109 60
adamc@109 61 and compareFields ((x1, t1), (x2, t2)) =
adamc@109 62 join (String.compare (x1, x2),
adamc@109 63 fn () => compare (t1, t2))
adamc@109 64
adamc@109 65 and sortFields xts = ListMergeSort.sort (fn (x, y) => compareFields (x, y) = GREATER) xts
adamc@109 66
adamc@26 67 fun mapfold fc =
adamc@26 68 let
adamc@26 69 fun mft c acc =
adamc@26 70 S.bindP (mft' c acc, fc)
adamc@26 71
adamc@26 72 and mft' (cAll as (c, loc)) =
adamc@26 73 case c of
adamc@26 74 TFun (t1, t2) =>
adamc@26 75 S.bind2 (mft t1,
adamc@26 76 fn t1' =>
adamc@26 77 S.map2 (mft t2,
adamc@26 78 fn t2' =>
adamc@26 79 (TFun (t1', t2'), loc)))
adamc@26 80 | TRecord xts =>
adamc@26 81 S.map2 (ListUtil.mapfold (fn (x, t) =>
adamc@26 82 S.map2 (mft t,
adamc@26 83 fn t' =>
adamc@26 84 (x, t')))
adamc@26 85 xts,
adamc@26 86 fn xts' => (TRecord xts', loc))
adamc@168 87 | TDatatype _ => S.return2 cAll
adamc@51 88 | TFfi _ => S.return2 cAll
adamc@26 89 in
adamc@26 90 mft
adamc@26 91 end
adamc@26 92
adamc@26 93 fun map typ c =
adamc@26 94 case mapfold (fn c => fn () => S.Continue (typ c, ())) c () of
adamc@26 95 S.Return () => raise Fail "Mono_util.Typ.map"
adamc@26 96 | S.Continue (c, ()) => c
adamc@26 97
adamc@26 98 fun fold typ s c =
adamc@26 99 case mapfold (fn c => fn s => S.Continue (c, typ (c, s))) c s of
adamc@26 100 S.Continue (_, s) => s
adamc@26 101 | S.Return _ => raise Fail "MonoUtil.Typ.fold: Impossible"
adamc@26 102
adamc@26 103 fun exists typ k =
adamc@26 104 case mapfold (fn c => fn () =>
adamc@26 105 if typ c then
adamc@26 106 S.Return ()
adamc@26 107 else
adamc@26 108 S.Continue (c, ())) k () of
adamc@26 109 S.Return _ => true
adamc@26 110 | S.Continue _ => false
adamc@26 111
adamc@26 112 end
adamc@26 113
adamc@26 114 structure Exp = struct
adamc@26 115
adamc@26 116 datatype binder =
adamc@168 117 Datatype of string * int * (string * int * typ option) list
adamc@26 118 | RelE of string * typ
adamc@109 119 | NamedE of string * int * typ * exp option * string
adamc@26 120
adamc@26 121 fun mapfoldB {typ = fc, exp = fe, bind} =
adamc@26 122 let
adamc@26 123 val mft = Typ.mapfold fc
adamc@26 124
adamc@26 125 fun mfe ctx e acc =
adamc@26 126 S.bindP (mfe' ctx e acc, fe ctx)
adamc@26 127
adamc@26 128 and mfe' ctx (eAll as (e, loc)) =
adamc@26 129 case e of
adamc@26 130 EPrim _ => S.return2 eAll
adamc@26 131 | ERel _ => S.return2 eAll
adamc@26 132 | ENamed _ => S.return2 eAll
adamc@188 133 | ECon (_, _, NONE) => S.return2 eAll
adamc@188 134 | ECon (dk, n, SOME e) =>
adamc@178 135 S.map2 (mfe ctx e,
adamc@178 136 fn e' =>
adamc@188 137 (ECon (dk, n, SOME e'), loc))
adamc@51 138 | EFfi _ => S.return2 eAll
adamc@51 139 | EFfiApp (m, x, es) =>
adamc@51 140 S.map2 (ListUtil.mapfold (fn e => mfe ctx e) es,
adamc@51 141 fn es' =>
adamc@51 142 (EFfiApp (m, x, es'), loc))
adamc@26 143 | EApp (e1, e2) =>
adamc@26 144 S.bind2 (mfe ctx e1,
adamc@26 145 fn e1' =>
adamc@26 146 S.map2 (mfe ctx e2,
adamc@26 147 fn e2' =>
adamc@26 148 (EApp (e1', e2'), loc)))
adamc@26 149 | EAbs (x, dom, ran, e) =>
adamc@26 150 S.bind2 (mft dom,
adamc@26 151 fn dom' =>
adamc@26 152 S.bind2 (mft ran,
adamc@26 153 fn ran' =>
adamc@26 154 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 155 fn e' =>
adamc@26 156 (EAbs (x, dom', ran', e'), loc))))
adamc@26 157
adamc@26 158 | ERecord xes =>
adamc@29 159 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@29 160 S.bind2 (mfe ctx e,
adamc@26 161 fn e' =>
adamc@29 162 S.map2 (mft t,
adamc@29 163 fn t' =>
adamc@29 164 (x, e', t'))))
adamc@26 165 xes,
adamc@26 166 fn xes' =>
adamc@26 167 (ERecord xes', loc))
adamc@26 168 | EField (e, x) =>
adamc@26 169 S.map2 (mfe ctx e,
adamc@26 170 fn e' =>
adamc@26 171 (EField (e', x), loc))
adamc@94 172
adamc@182 173 | ECase (e, pes, {disc, result}) =>
adamc@178 174 S.bind2 (mfe ctx e,
adamc@178 175 fn e' =>
adamc@178 176 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@179 177 let
adamc@179 178 fun pb ((p, _), ctx) =
adamc@179 179 case p of
adamc@179 180 PWild => ctx
adamc@182 181 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@179 182 | PPrim _ => ctx
adamc@188 183 | PCon (_, _, NONE) => ctx
adamc@188 184 | PCon (_, _, SOME p) => pb (p, ctx)
adamc@182 185 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@179 186 pb (p, ctx)) ctx xps
adamc@179 187 in
adamc@179 188 S.map2 (mfe (pb (p, ctx)) e,
adamc@179 189 fn e' => (p, e'))
adamc@179 190 end) pes,
adamc@178 191 fn pes' =>
adamc@182 192 S.bind2 (mft disc,
adamc@182 193 fn disc' =>
adamc@182 194 S.map2 (mft result,
adamc@182 195 fn result' =>
adamc@182 196 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@178 197
adamc@94 198 | EStrcat (e1, e2) =>
adamc@94 199 S.bind2 (mfe ctx e1,
adamc@94 200 fn e1' =>
adamc@94 201 S.map2 (mfe ctx e2,
adamc@94 202 fn e2' =>
adamc@94 203 (EStrcat (e1', e2'), loc)))
adamc@102 204
adamc@102 205 | EWrite e =>
adamc@102 206 S.map2 (mfe ctx e,
adamc@102 207 fn e' =>
adamc@102 208 (EWrite e', loc))
adamc@106 209
adamc@106 210 | ESeq (e1, e2) =>
adamc@106 211 S.bind2 (mfe ctx e1,
adamc@106 212 fn e1' =>
adamc@106 213 S.map2 (mfe ctx e2,
adamc@106 214 fn e2' =>
adamc@106 215 (ESeq (e1', e2'), loc)))
adamc@251 216 | ELet (x, t, e1, e2) =>
adamc@251 217 S.bind2 (mft t,
adamc@251 218 fn t' =>
adamc@251 219 S.bind2 (mfe ctx e1,
adamc@251 220 fn e1' =>
adamc@252 221 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
adamc@251 222 fn e2' =>
adamc@251 223 (ELet (x, t', e1', e2'), loc))))
adamc@111 224
adamc@111 225 | EClosure (n, es) =>
adamc@111 226 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@111 227 fn es' =>
adamc@111 228 (EClosure (n, es'), loc))
adamc@252 229
adamc@252 230 | EQuery {exps, tables, state, query, body, initial} =>
adamc@252 231 S.bind2 (ListUtil.mapfold (fn (x, t) =>
adamc@252 232 S.map2 (mft t,
adamc@252 233 fn t' => (x, t'))) exps,
adamc@252 234 fn exps' =>
adamc@252 235 S.bind2 (ListUtil.mapfold (fn (x, xts) =>
adamc@252 236 S.map2 (ListUtil.mapfold
adamc@252 237 (fn (x, t) =>
adamc@252 238 S.map2 (mft t,
adamc@252 239 fn t' => (x, t'))) xts,
adamc@252 240 fn xts' => (x, xts'))) tables,
adamc@252 241 fn tables' =>
adamc@252 242 S.bind2 (mft state,
adamc@252 243 fn state' =>
adamc@252 244 S.bind2 (mfe ctx query,
adamc@252 245 fn query' =>
adamc@252 246 S.bind2 (mfe ctx body,
adamc@252 247 fn body' =>
adamc@252 248 S.map2 (mfe ctx initial,
adamc@252 249 fn initial' =>
adamc@252 250 (EQuery {exps = exps',
adamc@252 251 tables = tables',
adamc@252 252 state = state',
adamc@252 253 query = query',
adamc@252 254 body = body',
adamc@252 255 initial = initial'},
adamc@252 256 loc)))))))
adamc@26 257 in
adamc@26 258 mfe
adamc@26 259 end
adamc@26 260
adamc@26 261 fun mapfold {typ = fc, exp = fe} =
adamc@26 262 mapfoldB {typ = fc,
adamc@26 263 exp = fn () => fe,
adamc@26 264 bind = fn ((), _) => ()} ()
adamc@26 265
adamc@26 266 fun mapB {typ, exp, bind} ctx e =
adamc@26 267 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 268 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@26 269 bind = bind} ctx e () of
adamc@26 270 S.Continue (e, ()) => e
adamc@26 271 | S.Return _ => raise Fail "MonoUtil.Exp.mapB: Impossible"
adamc@26 272
adamc@26 273 fun map {typ, exp} e =
adamc@26 274 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 275 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@26 276 S.Return () => raise Fail "Mono_util.Exp.map"
adamc@26 277 | S.Continue (e, ()) => e
adamc@26 278
adamc@26 279 fun fold {typ, exp} s e =
adamc@26 280 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 281 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@26 282 S.Continue (_, s) => s
adamc@26 283 | S.Return _ => raise Fail "MonoUtil.Exp.fold: Impossible"
adamc@26 284
adamc@26 285 fun exists {typ, exp} k =
adamc@26 286 case mapfold {typ = fn c => fn () =>
adamc@26 287 if typ c then
adamc@26 288 S.Return ()
adamc@26 289 else
adamc@26 290 S.Continue (c, ()),
adamc@26 291 exp = fn e => fn () =>
adamc@26 292 if exp e then
adamc@26 293 S.Return ()
adamc@26 294 else
adamc@26 295 S.Continue (e, ())} k () of
adamc@26 296 S.Return _ => true
adamc@26 297 | S.Continue _ => false
adamc@26 298
adamc@26 299 end
adamc@26 300
adamc@26 301 structure Decl = struct
adamc@26 302
adamc@26 303 datatype binder = datatype Exp.binder
adamc@26 304
adamc@26 305 fun mapfoldB {typ = fc, exp = fe, decl = fd, bind} =
adamc@26 306 let
adamc@26 307 val mft = Typ.mapfold fc
adamc@26 308
adamc@26 309 val mfe = Exp.mapfoldB {typ = fc, exp = fe, bind = bind}
adamc@26 310
adamc@26 311 fun mfd ctx d acc =
adamc@26 312 S.bindP (mfd' ctx d acc, fd ctx)
adamc@26 313
adamc@26 314 and mfd' ctx (dAll as (d, loc)) =
adamc@26 315 case d of
adamc@164 316 DDatatype (x, n, xncs) =>
adamc@164 317 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@164 318 case c of
adamc@164 319 NONE => S.return2 (x, n, c)
adamc@164 320 | SOME c =>
adamc@164 321 S.map2 (mft c,
adamc@164 322 fn c' => (x, n, SOME c'))) xncs,
adamc@164 323 fn xncs' =>
adamc@164 324 (DDatatype (x, n, xncs'), loc))
adamc@164 325 | DVal vi =>
adamc@126 326 S.map2 (mfvi ctx vi,
adamc@126 327 fn vi' =>
adamc@126 328 (DVal vi', loc))
adamc@126 329 | DValRec vis =>
adamc@196 330 let
adamc@196 331 val ctx' = foldl (fn ((x, n, t, _, s), ctx') => bind (ctx', NamedE (x, n, t, NONE, s))) ctx vis
adamc@196 332 in
adamc@196 333 S.map2 (ListUtil.mapfold (mfvi ctx') vis,
adamc@196 334 fn vis' =>
adamc@196 335 (DValRec vis', loc))
adamc@196 336 end
adamc@144 337 | DExport (ek, s, n, ts) =>
adamc@120 338 S.map2 (ListUtil.mapfold mft ts,
adamc@120 339 fn ts' =>
adamc@144 340 (DExport (ek, s, n, ts'), loc))
adamc@126 341
adamc@126 342 and mfvi ctx (x, n, t, e, s) =
adamc@126 343 S.bind2 (mft t,
adamc@126 344 fn t' =>
adamc@126 345 S.map2 (mfe ctx e,
adamc@126 346 fn e' =>
adamc@126 347 (x, n, t', e', s)))
adamc@26 348 in
adamc@26 349 mfd
adamc@26 350 end
adamc@26 351
adamc@26 352 fun mapfold {typ = fc, exp = fe, decl = fd} =
adamc@26 353 mapfoldB {typ = fc,
adamc@26 354 exp = fn () => fe,
adamc@26 355 decl = fn () => fd,
adamc@26 356 bind = fn ((), _) => ()} ()
adamc@26 357
adamc@26 358 fun fold {typ, exp, decl} s d =
adamc@26 359 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 360 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@26 361 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@26 362 S.Continue (_, s) => s
adamc@26 363 | S.Return _ => raise Fail "MonoUtil.Decl.fold: Impossible"
adamc@26 364
adamc@26 365 end
adamc@26 366
adamc@26 367 structure File = struct
adamc@26 368
adamc@26 369 datatype binder = datatype Exp.binder
adamc@26 370
adamc@26 371 fun mapfoldB (all as {bind, ...}) =
adamc@26 372 let
adamc@26 373 val mfd = Decl.mapfoldB all
adamc@26 374
adamc@26 375 fun mff ctx ds =
adamc@26 376 case ds of
adamc@26 377 nil => S.return2 nil
adamc@26 378 | d :: ds' =>
adamc@26 379 S.bind2 (mfd ctx d,
adamc@26 380 fn d' =>
adamc@26 381 let
adamc@100 382 val ctx' =
adamc@26 383 case #1 d' of
adamc@164 384 DDatatype (x, n, xncs) =>
adamc@164 385 let
adamc@168 386 val ctx = bind (ctx, Datatype (x, n, xncs))
adamc@198 387 val t = (TDatatype (n, ref (ElabUtil.classifyDatatype xncs, xncs)), #2 d')
adamc@164 388 in
adamc@164 389 foldl (fn ((x, n, to), ctx) =>
adamc@164 390 let
adamc@164 391 val t = case to of
adamc@164 392 NONE => t
adamc@164 393 | SOME t' => (TFun (t', t), #2 d')
adamc@164 394 in
adamc@164 395 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@164 396 end)
adamc@164 397 ctx xncs
adamc@164 398 end
adamc@164 399 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@126 400 | DValRec vis => foldl (fn ((x, n, t, e, s), ctx) =>
adamc@196 401 bind (ctx, NamedE (x, n, t, NONE, s))) ctx vis
adamc@109 402 | DExport _ => ctx
adamc@26 403 in
adamc@26 404 S.map2 (mff ctx' ds',
adamc@26 405 fn ds' =>
adamc@26 406 d' :: ds')
adamc@26 407 end)
adamc@26 408 in
adamc@26 409 mff
adamc@26 410 end
adamc@26 411
adamc@26 412 fun mapfold {typ = fc, exp = fe, decl = fd} =
adamc@26 413 mapfoldB {typ = fc,
adamc@26 414 exp = fn () => fe,
adamc@26 415 decl = fn () => fd,
adamc@26 416 bind = fn ((), _) => ()} ()
adamc@26 417
adamc@26 418 fun mapB {typ, exp, decl, bind} ctx ds =
adamc@26 419 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 420 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@26 421 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@26 422 bind = bind} ctx ds () of
adamc@26 423 S.Continue (ds, ()) => ds
adamc@26 424 | S.Return _ => raise Fail "MonoUtil.File.mapB: Impossible"
adamc@26 425
adamc@96 426 fun map {typ, exp, decl} e =
adamc@96 427 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@96 428 exp = fn e => fn () => S.Continue (exp e, ()),
adamc@96 429 decl = fn d => fn () => S.Continue (decl d, ())} e () of
adamc@96 430 S.Return () => raise Fail "Mono_util.File.map"
adamc@96 431 | S.Continue (e, ()) => e
adamc@96 432
adamc@26 433 fun fold {typ, exp, decl} s d =
adamc@26 434 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 435 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@26 436 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@26 437 S.Continue (_, s) => s
adamc@26 438 | S.Return _ => raise Fail "MonoUtil.File.fold: Impossible"
adamc@26 439
adamc@26 440 end
adamc@26 441
adamc@26 442 end