annotate src/mono_util.sml @ 196:890a61991263

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