annotate src/mono_util.sml @ 178:eb3f9913bf31

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