annotate src/mono_util.sml @ 576:813f1e78d9d0

Merge
author Adam Chlipala <adamc@hcoop.net>
date Tue, 30 Dec 2008 09:43:45 -0500
parents ac947e2f29ff
children 3d56940120b1
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@288 53 | (TOption t1, TOption t2) => compare (t1, t2)
adamc@568 54 | (TSignal t1, TSignal t2) => compare (t1, t2)
adamc@109 55
adamc@109 56 | (TFun _, _) => LESS
adamc@109 57 | (_, TFun _) => GREATER
adamc@109 58
adamc@109 59 | (TRecord _, _) => LESS
adamc@109 60 | (_, TRecord _) => GREATER
adamc@109 61
adamc@168 62 | (TDatatype _, _) => LESS
adamc@168 63 | (_, TDatatype _) => GREATER
adamc@109 64
adamc@288 65 | (TFfi _, _) => LESS
adamc@288 66 | (_, TFfi _) => GREATER
adamc@288 67
adamc@568 68 | (TOption _, _) => LESS
adamc@568 69 | (_, TOption _) => GREATER
adamc@568 70
adamc@109 71 and compareFields ((x1, t1), (x2, t2)) =
adamc@109 72 join (String.compare (x1, x2),
adamc@109 73 fn () => compare (t1, t2))
adamc@109 74
adamc@109 75 and sortFields xts = ListMergeSort.sort (fn (x, y) => compareFields (x, y) = GREATER) xts
adamc@109 76
adamc@26 77 fun mapfold fc =
adamc@26 78 let
adamc@26 79 fun mft c acc =
adamc@26 80 S.bindP (mft' c acc, fc)
adamc@26 81
adamc@26 82 and mft' (cAll as (c, loc)) =
adamc@26 83 case c of
adamc@26 84 TFun (t1, t2) =>
adamc@26 85 S.bind2 (mft t1,
adamc@26 86 fn t1' =>
adamc@26 87 S.map2 (mft t2,
adamc@26 88 fn t2' =>
adamc@26 89 (TFun (t1', t2'), loc)))
adamc@26 90 | TRecord xts =>
adamc@26 91 S.map2 (ListUtil.mapfold (fn (x, t) =>
adamc@26 92 S.map2 (mft t,
adamc@26 93 fn t' =>
adamc@26 94 (x, t')))
adamc@26 95 xts,
adamc@26 96 fn xts' => (TRecord xts', loc))
adamc@168 97 | TDatatype _ => S.return2 cAll
adamc@51 98 | TFfi _ => S.return2 cAll
adamc@288 99 | TOption t =>
adamc@288 100 S.map2 (mft t,
adamc@288 101 fn t' =>
adamc@288 102 (TOption t, loc))
adamc@568 103 | TSignal t =>
adamc@568 104 S.map2 (mft t,
adamc@568 105 fn t' =>
adamc@568 106 (TSignal t, loc))
adamc@26 107 in
adamc@26 108 mft
adamc@26 109 end
adamc@26 110
adamc@26 111 fun map typ c =
adamc@26 112 case mapfold (fn c => fn () => S.Continue (typ c, ())) c () of
adamc@26 113 S.Return () => raise Fail "Mono_util.Typ.map"
adamc@26 114 | S.Continue (c, ()) => c
adamc@26 115
adamc@26 116 fun fold typ s c =
adamc@26 117 case mapfold (fn c => fn s => S.Continue (c, typ (c, s))) c s of
adamc@26 118 S.Continue (_, s) => s
adamc@26 119 | S.Return _ => raise Fail "MonoUtil.Typ.fold: Impossible"
adamc@26 120
adamc@26 121 fun exists typ k =
adamc@26 122 case mapfold (fn c => fn () =>
adamc@26 123 if typ c then
adamc@26 124 S.Return ()
adamc@26 125 else
adamc@26 126 S.Continue (c, ())) k () of
adamc@26 127 S.Return _ => true
adamc@26 128 | S.Continue _ => false
adamc@26 129
adamc@26 130 end
adamc@26 131
adamc@26 132 structure Exp = struct
adamc@26 133
adamc@26 134 datatype binder =
adamc@168 135 Datatype of string * int * (string * int * typ option) list
adamc@26 136 | RelE of string * typ
adamc@109 137 | NamedE of string * int * typ * exp option * string
adamc@26 138
adamc@26 139 fun mapfoldB {typ = fc, exp = fe, bind} =
adamc@26 140 let
adamc@26 141 val mft = Typ.mapfold fc
adamc@26 142
adamc@26 143 fun mfe ctx e acc =
adamc@26 144 S.bindP (mfe' ctx e acc, fe ctx)
adamc@26 145
adamc@26 146 and mfe' ctx (eAll as (e, loc)) =
adamc@26 147 case e of
adamc@26 148 EPrim _ => S.return2 eAll
adamc@26 149 | ERel _ => S.return2 eAll
adamc@26 150 | ENamed _ => S.return2 eAll
adamc@188 151 | ECon (_, _, NONE) => S.return2 eAll
adamc@188 152 | ECon (dk, n, SOME e) =>
adamc@178 153 S.map2 (mfe ctx e,
adamc@178 154 fn e' =>
adamc@188 155 (ECon (dk, n, SOME e'), loc))
adamc@297 156 | ENone t =>
adamc@297 157 S.map2 (mft t,
adamc@297 158 fn t' =>
adamc@297 159 (ENone t', loc))
adamc@290 160 | ESome (t, e) =>
adamc@290 161 S.bind2 (mft t,
adamc@290 162 fn t' =>
adamc@290 163 S.map2 (mfe ctx e,
adamc@290 164 fn e' =>
adamc@290 165 (ESome (t', e'), loc)))
adamc@51 166 | EFfi _ => S.return2 eAll
adamc@51 167 | EFfiApp (m, x, es) =>
adamc@51 168 S.map2 (ListUtil.mapfold (fn e => mfe ctx e) es,
adamc@51 169 fn es' =>
adamc@51 170 (EFfiApp (m, x, es'), loc))
adamc@26 171 | EApp (e1, e2) =>
adamc@26 172 S.bind2 (mfe ctx e1,
adamc@26 173 fn e1' =>
adamc@26 174 S.map2 (mfe ctx e2,
adamc@26 175 fn e2' =>
adamc@26 176 (EApp (e1', e2'), loc)))
adamc@26 177 | EAbs (x, dom, ran, e) =>
adamc@26 178 S.bind2 (mft dom,
adamc@26 179 fn dom' =>
adamc@26 180 S.bind2 (mft ran,
adamc@26 181 fn ran' =>
adamc@26 182 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 183 fn e' =>
adamc@26 184 (EAbs (x, dom', ran', e'), loc))))
adamc@26 185
adamc@387 186 | EUnop (s, e) =>
adamc@387 187 S.map2 (mfe ctx e,
adamc@387 188 fn e' =>
adamc@387 189 (EUnop (s, e'), loc))
adamc@387 190 | EBinop (s, e1, e2) =>
adamc@387 191 S.bind2 (mfe ctx e1,
adamc@387 192 fn e1' =>
adamc@387 193 S.map2 (mfe ctx e2,
adamc@387 194 fn e2' =>
adamc@387 195 (EBinop (s, e1', e2'), loc)))
adamc@387 196
adamc@26 197 | ERecord xes =>
adamc@29 198 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@29 199 S.bind2 (mfe ctx e,
adamc@26 200 fn e' =>
adamc@29 201 S.map2 (mft t,
adamc@29 202 fn t' =>
adamc@29 203 (x, e', t'))))
adamc@26 204 xes,
adamc@26 205 fn xes' =>
adamc@26 206 (ERecord xes', loc))
adamc@26 207 | EField (e, x) =>
adamc@26 208 S.map2 (mfe ctx e,
adamc@26 209 fn e' =>
adamc@26 210 (EField (e', x), loc))
adamc@94 211
adamc@182 212 | ECase (e, pes, {disc, result}) =>
adamc@178 213 S.bind2 (mfe ctx e,
adamc@178 214 fn e' =>
adamc@178 215 S.bind2 (ListUtil.mapfold (fn (p, e) =>
adamc@179 216 let
adamc@179 217 fun pb ((p, _), ctx) =
adamc@179 218 case p of
adamc@179 219 PWild => ctx
adamc@182 220 | PVar (x, t) => bind (ctx, RelE (x, t))
adamc@179 221 | PPrim _ => ctx
adamc@188 222 | PCon (_, _, NONE) => ctx
adamc@188 223 | PCon (_, _, SOME p) => pb (p, ctx)
adamc@182 224 | PRecord xps => foldl (fn ((_, p, _), ctx) =>
adamc@179 225 pb (p, ctx)) ctx xps
adamc@288 226 | PNone _ => ctx
adamc@288 227 | PSome (_, p) => pb (p, ctx)
adamc@179 228 in
adamc@179 229 S.map2 (mfe (pb (p, ctx)) e,
adamc@179 230 fn e' => (p, e'))
adamc@179 231 end) pes,
adamc@178 232 fn pes' =>
adamc@182 233 S.bind2 (mft disc,
adamc@182 234 fn disc' =>
adamc@182 235 S.map2 (mft result,
adamc@182 236 fn result' =>
adamc@182 237 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
adamc@178 238
adamc@283 239 | EError (e, t) =>
adamc@283 240 S.bind2 (mfe ctx e,
adamc@283 241 fn e' =>
adamc@283 242 S.map2 (mft t,
adamc@283 243 fn t' =>
adamc@283 244 (EError (e', t'), loc)))
adamc@283 245
adamc@94 246 | EStrcat (e1, e2) =>
adamc@94 247 S.bind2 (mfe ctx e1,
adamc@94 248 fn e1' =>
adamc@94 249 S.map2 (mfe ctx e2,
adamc@94 250 fn e2' =>
adamc@94 251 (EStrcat (e1', e2'), loc)))
adamc@102 252
adamc@102 253 | EWrite e =>
adamc@102 254 S.map2 (mfe ctx e,
adamc@102 255 fn e' =>
adamc@102 256 (EWrite e', loc))
adamc@106 257
adamc@106 258 | ESeq (e1, e2) =>
adamc@106 259 S.bind2 (mfe ctx e1,
adamc@106 260 fn e1' =>
adamc@106 261 S.map2 (mfe ctx e2,
adamc@106 262 fn e2' =>
adamc@106 263 (ESeq (e1', e2'), loc)))
adamc@251 264 | ELet (x, t, e1, e2) =>
adamc@251 265 S.bind2 (mft t,
adamc@251 266 fn t' =>
adamc@251 267 S.bind2 (mfe ctx e1,
adamc@251 268 fn e1' =>
adamc@252 269 S.map2 (mfe (bind (ctx, RelE (x, t'))) e2,
adamc@251 270 fn e2' =>
adamc@251 271 (ELet (x, t', e1', e2'), loc))))
adamc@111 272
adamc@111 273 | EClosure (n, es) =>
adamc@111 274 S.map2 (ListUtil.mapfold (mfe ctx) es,
adamc@111 275 fn es' =>
adamc@111 276 (EClosure (n, es'), loc))
adamc@252 277
adamc@252 278 | EQuery {exps, tables, state, query, body, initial} =>
adamc@252 279 S.bind2 (ListUtil.mapfold (fn (x, t) =>
adamc@252 280 S.map2 (mft t,
adamc@252 281 fn t' => (x, t'))) exps,
adamc@252 282 fn exps' =>
adamc@252 283 S.bind2 (ListUtil.mapfold (fn (x, xts) =>
adamc@252 284 S.map2 (ListUtil.mapfold
adamc@252 285 (fn (x, t) =>
adamc@252 286 S.map2 (mft t,
adamc@252 287 fn t' => (x, t'))) xts,
adamc@252 288 fn xts' => (x, xts'))) tables,
adamc@252 289 fn tables' =>
adamc@252 290 S.bind2 (mft state,
adamc@252 291 fn state' =>
adamc@252 292 S.bind2 (mfe ctx query,
adamc@252 293 fn query' =>
adamc@267 294 S.bind2 (mfe (bind (bind (ctx, RelE ("r", dummyt)),
adamc@267 295 RelE ("acc", dummyt)))
adamc@267 296 body,
adamc@267 297 fn body' =>
adamc@267 298 S.map2 (mfe ctx initial,
adamc@267 299 fn initial' =>
adamc@267 300 (EQuery {exps = exps',
adamc@267 301 tables = tables',
adamc@267 302 state = state',
adamc@267 303 query = query',
adamc@267 304 body = body',
adamc@267 305 initial = initial'},
adamc@267 306 loc)))))))
adamc@307 307
adamc@307 308 | EDml e =>
adamc@307 309 S.map2 (mfe ctx e,
adamc@307 310 fn e' =>
adamc@307 311 (EDml e', loc))
adamc@338 312 | ENextval e =>
adamc@338 313 S.map2 (mfe ctx e,
adamc@338 314 fn e' =>
adamc@338 315 (ENextval e', loc))
adamc@463 316 | EUnurlify (e, t) =>
adamc@463 317 S.bind2 (mfe ctx e,
adamc@463 318 fn e' =>
adamc@463 319 S.map2 (mft t,
adamc@463 320 fn t' =>
adamc@463 321 (EUnurlify (e', t'), loc)))
adamc@568 322 | EJavaScript (m, e) =>
adamc@566 323 S.map2 (mfe ctx e,
adamc@566 324 fn e' =>
adamc@568 325 (EJavaScript (m, e'), loc))
adamc@569 326
adamc@568 327 | ESignalReturn e =>
adamc@568 328 S.map2 (mfe ctx e,
adamc@568 329 fn e' =>
adamc@568 330 (ESignalReturn e', loc))
adamc@572 331 | ESignalBind (e1, e2) =>
adamc@572 332 S.bind2 (mfe ctx e1,
adamc@572 333 fn e1' =>
adamc@572 334 S.map2 (mfe ctx e2,
adamc@572 335 fn e2' =>
adamc@572 336 (ESignalBind (e1', e2'), loc)))
adamc@574 337 | ESignalSource e =>
adamc@574 338 S.map2 (mfe ctx e,
adamc@574 339 fn e' =>
adamc@574 340 (ESignalSource e', loc))
adamc@26 341 in
adamc@26 342 mfe
adamc@26 343 end
adamc@26 344
adamc@26 345 fun mapfold {typ = fc, exp = fe} =
adamc@26 346 mapfoldB {typ = fc,
adamc@26 347 exp = fn () => fe,
adamc@26 348 bind = fn ((), _) => ()} ()
adamc@26 349
adamc@26 350 fun mapB {typ, exp, bind} ctx e =
adamc@26 351 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 352 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@26 353 bind = bind} ctx e () of
adamc@26 354 S.Continue (e, ()) => e
adamc@26 355 | S.Return _ => raise Fail "MonoUtil.Exp.mapB: Impossible"
adamc@26 356
adamc@26 357 fun map {typ, exp} e =
adamc@26 358 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 359 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@26 360 S.Return () => raise Fail "Mono_util.Exp.map"
adamc@26 361 | S.Continue (e, ()) => e
adamc@26 362
adamc@26 363 fun fold {typ, exp} s e =
adamc@26 364 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 365 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@26 366 S.Continue (_, s) => s
adamc@26 367 | S.Return _ => raise Fail "MonoUtil.Exp.fold: Impossible"
adamc@26 368
adamc@26 369 fun exists {typ, exp} k =
adamc@26 370 case mapfold {typ = fn c => fn () =>
adamc@26 371 if typ c then
adamc@26 372 S.Return ()
adamc@26 373 else
adamc@26 374 S.Continue (c, ()),
adamc@26 375 exp = fn e => fn () =>
adamc@26 376 if exp e then
adamc@26 377 S.Return ()
adamc@26 378 else
adamc@26 379 S.Continue (e, ())} k () of
adamc@26 380 S.Return _ => true
adamc@26 381 | S.Continue _ => false
adamc@26 382
adamc@567 383 fun foldB {typ, exp, bind} ctx s e =
adamc@567 384 case mapfoldB {typ = fn t => fn s => S.Continue (t, typ (t, s)),
adamc@567 385 exp = fn ctx => fn e => fn s => S.Continue (e, exp (ctx, e, s)),
adamc@567 386 bind = bind} ctx e s of
adamc@567 387 S.Continue (_, s) => s
adamc@567 388 | S.Return _ => raise Fail "MonoUtil.Exp.foldB: Impossible"
adamc@567 389
adamc@26 390 end
adamc@26 391
adamc@26 392 structure Decl = struct
adamc@26 393
adamc@26 394 datatype binder = datatype Exp.binder
adamc@26 395
adamc@26 396 fun mapfoldB {typ = fc, exp = fe, decl = fd, bind} =
adamc@26 397 let
adamc@26 398 val mft = Typ.mapfold fc
adamc@26 399
adamc@26 400 val mfe = Exp.mapfoldB {typ = fc, exp = fe, bind = bind}
adamc@26 401
adamc@26 402 fun mfd ctx d acc =
adamc@26 403 S.bindP (mfd' ctx d acc, fd ctx)
adamc@26 404
adamc@26 405 and mfd' ctx (dAll as (d, loc)) =
adamc@26 406 case d of
adamc@164 407 DDatatype (x, n, xncs) =>
adamc@164 408 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
adamc@164 409 case c of
adamc@164 410 NONE => S.return2 (x, n, c)
adamc@164 411 | SOME c =>
adamc@164 412 S.map2 (mft c,
adamc@164 413 fn c' => (x, n, SOME c'))) xncs,
adamc@164 414 fn xncs' =>
adamc@164 415 (DDatatype (x, n, xncs'), loc))
adamc@164 416 | DVal vi =>
adamc@126 417 S.map2 (mfvi ctx vi,
adamc@126 418 fn vi' =>
adamc@126 419 (DVal vi', loc))
adamc@126 420 | DValRec vis =>
adamc@196 421 let
adamc@196 422 val ctx' = foldl (fn ((x, n, t, _, s), ctx') => bind (ctx', NamedE (x, n, t, NONE, s))) ctx vis
adamc@196 423 in
adamc@196 424 S.map2 (ListUtil.mapfold (mfvi ctx') vis,
adamc@196 425 fn vis' =>
adamc@196 426 (DValRec vis', loc))
adamc@196 427 end
adamc@144 428 | DExport (ek, s, n, ts) =>
adamc@120 429 S.map2 (ListUtil.mapfold mft ts,
adamc@120 430 fn ts' =>
adamc@144 431 (DExport (ek, s, n, ts'), loc))
adamc@273 432 | DTable _ => S.return2 dAll
adamc@338 433 | DSequence _ => S.return2 dAll
adamc@271 434 | DDatabase _ => S.return2 dAll
adamc@569 435 | DJavaScript _ => S.return2 dAll
adamc@126 436
adamc@126 437 and mfvi ctx (x, n, t, e, s) =
adamc@126 438 S.bind2 (mft t,
adamc@126 439 fn t' =>
adamc@126 440 S.map2 (mfe ctx e,
adamc@126 441 fn e' =>
adamc@126 442 (x, n, t', e', s)))
adamc@26 443 in
adamc@26 444 mfd
adamc@26 445 end
adamc@26 446
adamc@26 447 fun mapfold {typ = fc, exp = fe, decl = fd} =
adamc@26 448 mapfoldB {typ = fc,
adamc@26 449 exp = fn () => fe,
adamc@26 450 decl = fn () => fd,
adamc@26 451 bind = fn ((), _) => ()} ()
adamc@26 452
adamc@26 453 fun fold {typ, exp, decl} s d =
adamc@26 454 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 455 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@26 456 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@26 457 S.Continue (_, s) => s
adamc@26 458 | S.Return _ => raise Fail "MonoUtil.Decl.fold: Impossible"
adamc@26 459
adamc@506 460 fun map {typ, exp, decl} e =
adamc@506 461 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@506 462 exp = fn e => fn () => S.Continue (exp e, ()),
adamc@506 463 decl = fn d => fn () => S.Continue (decl d, ())} e () of
adamc@506 464 S.Return () => raise Fail "MonoUtil.Decl.map: Impossible"
adamc@506 465 | S.Continue (e, ()) => e
adamc@506 466
adamc@567 467 fun foldMapB {typ, exp, decl, bind} ctx s d =
adamc@567 468 case mapfoldB {typ = fn c => fn s => S.Continue (typ (c, s)),
adamc@567 469 exp = fn ctx => fn e => fn s => S.Continue (exp (ctx, e, s)),
adamc@567 470 decl = fn ctx => fn d => fn s => S.Continue (decl (ctx, d, s)),
adamc@567 471 bind = bind} ctx d s of
adamc@567 472 S.Continue v => v
adamc@567 473 | S.Return _ => raise Fail "MonoUtil.Decl.foldMapB: Impossible"
adamc@567 474
adamc@26 475 end
adamc@26 476
adamc@26 477 structure File = struct
adamc@26 478
adamc@26 479 datatype binder = datatype Exp.binder
adamc@26 480
adamc@26 481 fun mapfoldB (all as {bind, ...}) =
adamc@26 482 let
adamc@26 483 val mfd = Decl.mapfoldB all
adamc@26 484
adamc@26 485 fun mff ctx ds =
adamc@26 486 case ds of
adamc@26 487 nil => S.return2 nil
adamc@26 488 | d :: ds' =>
adamc@26 489 S.bind2 (mfd ctx d,
adamc@26 490 fn d' =>
adamc@26 491 let
adamc@100 492 val ctx' =
adamc@26 493 case #1 d' of
adamc@164 494 DDatatype (x, n, xncs) =>
adamc@164 495 let
adamc@168 496 val ctx = bind (ctx, Datatype (x, n, xncs))
adamc@198 497 val t = (TDatatype (n, ref (ElabUtil.classifyDatatype xncs, xncs)), #2 d')
adamc@164 498 in
adamc@164 499 foldl (fn ((x, n, to), ctx) =>
adamc@164 500 let
adamc@164 501 val t = case to of
adamc@164 502 NONE => t
adamc@164 503 | SOME t' => (TFun (t', t), #2 d')
adamc@164 504 in
adamc@164 505 bind (ctx, NamedE (x, n, t, NONE, ""))
adamc@164 506 end)
adamc@164 507 ctx xncs
adamc@164 508 end
adamc@164 509 | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s))
adamc@126 510 | DValRec vis => foldl (fn ((x, n, t, e, s), ctx) =>
adamc@196 511 bind (ctx, NamedE (x, n, t, NONE, s))) ctx vis
adamc@109 512 | DExport _ => ctx
adamc@273 513 | DTable _ => ctx
adamc@338 514 | DSequence _ => ctx
adamc@271 515 | DDatabase _ => ctx
adamc@569 516 | DJavaScript _ => ctx
adamc@26 517 in
adamc@26 518 S.map2 (mff ctx' ds',
adamc@26 519 fn ds' =>
adamc@26 520 d' :: ds')
adamc@26 521 end)
adamc@26 522 in
adamc@26 523 mff
adamc@26 524 end
adamc@26 525
adamc@26 526 fun mapfold {typ = fc, exp = fe, decl = fd} =
adamc@26 527 mapfoldB {typ = fc,
adamc@26 528 exp = fn () => fe,
adamc@26 529 decl = fn () => fd,
adamc@26 530 bind = fn ((), _) => ()} ()
adamc@26 531
adamc@26 532 fun mapB {typ, exp, decl, bind} ctx ds =
adamc@26 533 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 534 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@26 535 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@26 536 bind = bind} ctx ds () of
adamc@26 537 S.Continue (ds, ()) => ds
adamc@26 538 | S.Return _ => raise Fail "MonoUtil.File.mapB: Impossible"
adamc@26 539
adamc@96 540 fun map {typ, exp, decl} e =
adamc@96 541 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@96 542 exp = fn e => fn () => S.Continue (exp e, ()),
adamc@96 543 decl = fn d => fn () => S.Continue (decl d, ())} e () of
adamc@506 544 S.Return () => raise Fail "MonoUtil.File.map: Impossible"
adamc@96 545 | S.Continue (e, ()) => e
adamc@96 546
adamc@26 547 fun fold {typ, exp, decl} s d =
adamc@26 548 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 549 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@26 550 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@26 551 S.Continue (_, s) => s
adamc@26 552 | S.Return _ => raise Fail "MonoUtil.File.fold: Impossible"
adamc@26 553
adamc@506 554 val maxName = foldl (fn ((d, _) : decl, count) =>
adamc@506 555 case d of
adamc@506 556 DDatatype (_, n, ns) =>
adamc@506 557 foldl (fn ((_, n', _), m) => Int.max (n', m))
adamc@506 558 (Int.max (n, count)) ns
adamc@506 559 | DVal (_, n, _, _, _) => Int.max (n, count)
adamc@506 560 | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis
adamc@506 561 | DExport _ => count
adamc@506 562 | DTable _ => count
adamc@506 563 | DSequence _ => count
adamc@569 564 | DDatabase _ => count
adamc@569 565 | DJavaScript _ => count) 0
adamc@506 566
adamc@26 567 end
adamc@26 568
adamc@26 569 end