annotate src/mono_util.sml @ 283:c0e4ac23522d

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