annotate src/core_util.sml @ 79:37847b504cc6

More hardcore unification
author Adam Chlipala <adamc@hcoop.net>
date Sun, 29 Jun 2008 11:20:34 -0400
parents 8b611ecc5f2d
children 275aaeb73f1f
rev   line source
adamc@16 1 (* Copyright (c) 2008, Adam Chlipala
adamc@16 2 * All rights reserved.
adamc@16 3 *
adamc@16 4 * Redistribution and use in source and binary forms, with or without
adamc@16 5 * modification, are permitted provided that the following conditions are met:
adamc@16 6 *
adamc@16 7 * - Redistributions of source code must retain the above copyright notice,
adamc@16 8 * this list of conditions and the following disclaimer.
adamc@16 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@16 10 * this list of conditions and the following disclaimer in the documentation
adamc@16 11 * and/or other materials provided with the distribution.
adamc@16 12 * - The names of contributors may not be used to endorse or promote products
adamc@16 13 * derived from this software without specific prior written permission.
adamc@16 14 *
adamc@16 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@16 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@16 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@16 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@16 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@16 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@16 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@16 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@16 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@16 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@16 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@16 26 *)
adamc@16 27
adamc@16 28 structure CoreUtil :> CORE_UTIL = struct
adamc@16 29
adamc@16 30 open Core
adamc@16 31
adamc@16 32 structure S = Search
adamc@16 33
adamc@16 34 structure Kind = struct
adamc@16 35
adamc@16 36 fun mapfold f =
adamc@16 37 let
adamc@16 38 fun mfk k acc =
adamc@16 39 S.bindP (mfk' k acc, f)
adamc@16 40
adamc@16 41 and mfk' (kAll as (k, loc)) =
adamc@16 42 case k of
adamc@16 43 KType => S.return2 kAll
adamc@16 44
adamc@16 45 | KArrow (k1, k2) =>
adamc@16 46 S.bind2 (mfk k1,
adamc@16 47 fn k1' =>
adamc@16 48 S.map2 (mfk k2,
adamc@16 49 fn k2' =>
adamc@16 50 (KArrow (k1', k2'), loc)))
adamc@16 51
adamc@16 52 | KName => S.return2 kAll
adamc@16 53
adamc@16 54 | KRecord k =>
adamc@16 55 S.map2 (mfk k,
adamc@16 56 fn k' =>
adamc@16 57 (KRecord k', loc))
adamc@16 58 in
adamc@16 59 mfk
adamc@16 60 end
adamc@16 61
adamc@16 62 fun map f k =
adamc@16 63 case mapfold (fn k => fn () => S.Continue (f k, ())) k () of
adamc@16 64 S.Return () => raise Fail "Core_util.Kind.map"
adamc@16 65 | S.Continue (k, ()) => k
adamc@16 66
adamc@16 67 fun exists f k =
adamc@16 68 case mapfold (fn k => fn () =>
adamc@16 69 if f k then
adamc@16 70 S.Return ()
adamc@16 71 else
adamc@16 72 S.Continue (k, ())) k () of
adamc@16 73 S.Return _ => true
adamc@16 74 | S.Continue _ => false
adamc@16 75
adamc@16 76 end
adamc@16 77
adamc@16 78 structure Con = struct
adamc@16 79
adamc@16 80 datatype binder =
adamc@16 81 Rel of string * kind
adamc@20 82 | Named of string * int * kind * con option
adamc@16 83
adamc@16 84 fun mapfoldB {kind = fk, con = fc, bind} =
adamc@16 85 let
adamc@16 86 val mfk = Kind.mapfold fk
adamc@16 87
adamc@16 88 fun mfc ctx c acc =
adamc@16 89 S.bindP (mfc' ctx c acc, fc ctx)
adamc@16 90
adamc@16 91 and mfc' ctx (cAll as (c, loc)) =
adamc@16 92 case c of
adamc@16 93 TFun (c1, c2) =>
adamc@16 94 S.bind2 (mfc ctx c1,
adamc@16 95 fn c1' =>
adamc@16 96 S.map2 (mfc ctx c2,
adamc@16 97 fn c2' =>
adamc@16 98 (TFun (c1', c2'), loc)))
adamc@16 99 | TCFun (x, k, c) =>
adamc@16 100 S.bind2 (mfk k,
adamc@16 101 fn k' =>
adamc@16 102 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
adamc@16 103 fn c' =>
adamc@16 104 (TCFun (x, k', c'), loc)))
adamc@16 105 | TRecord c =>
adamc@16 106 S.map2 (mfc ctx c,
adamc@16 107 fn c' =>
adamc@16 108 (TRecord c', loc))
adamc@16 109
adamc@16 110 | CRel _ => S.return2 cAll
adamc@16 111 | CNamed _ => S.return2 cAll
adamc@48 112 | CFfi _ => S.return2 cAll
adamc@16 113 | CApp (c1, c2) =>
adamc@16 114 S.bind2 (mfc ctx c1,
adamc@16 115 fn c1' =>
adamc@16 116 S.map2 (mfc ctx c2,
adamc@16 117 fn c2' =>
adamc@16 118 (CApp (c1', c2'), loc)))
adamc@16 119 | CAbs (x, k, c) =>
adamc@16 120 S.bind2 (mfk k,
adamc@16 121 fn k' =>
adamc@16 122 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
adamc@16 123 fn c' =>
adamc@16 124 (CAbs (x, k', c'), loc)))
adamc@16 125
adamc@16 126 | CName _ => S.return2 cAll
adamc@16 127
adamc@16 128 | CRecord (k, xcs) =>
adamc@16 129 S.bind2 (mfk k,
adamc@16 130 fn k' =>
adamc@16 131 S.map2 (ListUtil.mapfold (fn (x, c) =>
adamc@16 132 S.bind2 (mfc ctx x,
adamc@16 133 fn x' =>
adamc@16 134 S.map2 (mfc ctx c,
adamc@16 135 fn c' =>
adamc@16 136 (x', c'))))
adamc@16 137 xcs,
adamc@16 138 fn xcs' =>
adamc@16 139 (CRecord (k', xcs'), loc)))
adamc@16 140 | CConcat (c1, c2) =>
adamc@16 141 S.bind2 (mfc ctx c1,
adamc@16 142 fn c1' =>
adamc@16 143 S.map2 (mfc ctx c2,
adamc@16 144 fn c2' =>
adamc@16 145 (CConcat (c1', c2'), loc)))
adamc@69 146 | CFold (k1, k2) =>
adamc@69 147 S.bind2 (mfk k1,
adamc@69 148 fn k1' =>
adamc@69 149 S.map2 (mfk k2,
adamc@69 150 fn k2' =>
adamc@69 151 (CFold (k1', k2'), loc)))
adamc@16 152 in
adamc@16 153 mfc
adamc@16 154 end
adamc@16 155
adamc@16 156 fun mapfold {kind = fk, con = fc} =
adamc@16 157 mapfoldB {kind = fk,
adamc@16 158 con = fn () => fc,
adamc@16 159 bind = fn ((), _) => ()} ()
adamc@16 160
adamc@16 161 fun map {kind, con} c =
adamc@16 162 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 163 con = fn c => fn () => S.Continue (con c, ())} c () of
adamc@16 164 S.Return () => raise Fail "Core_util.Con.map"
adamc@16 165 | S.Continue (c, ()) => c
adamc@16 166
adamc@16 167 fun mapB {kind, con, bind} ctx c =
adamc@16 168 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 169 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@16 170 bind = bind} ctx c () of
adamc@16 171 S.Continue (c, ()) => c
adamc@20 172 | S.Return _ => raise Fail "CoreUtil.Con.mapB: Impossible"
adamc@16 173
adamc@23 174 fun fold {kind, con} s c =
adamc@23 175 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 176 con = fn c => fn s => S.Continue (c, con (c, s))} c s of
adamc@23 177 S.Continue (_, s) => s
adamc@23 178 | S.Return _ => raise Fail "CoreUtil.Con.fold: Impossible"
adamc@23 179
adamc@16 180 fun exists {kind, con} k =
adamc@16 181 case mapfold {kind = fn k => fn () =>
adamc@16 182 if kind k then
adamc@16 183 S.Return ()
adamc@16 184 else
adamc@16 185 S.Continue (k, ()),
adamc@16 186 con = fn c => fn () =>
adamc@16 187 if con c then
adamc@16 188 S.Return ()
adamc@16 189 else
adamc@16 190 S.Continue (c, ())} k () of
adamc@16 191 S.Return _ => true
adamc@16 192 | S.Continue _ => false
adamc@16 193
adamc@16 194 end
adamc@16 195
adamc@16 196 structure Exp = struct
adamc@16 197
adamc@16 198 datatype binder =
adamc@16 199 RelC of string * kind
adamc@20 200 | NamedC of string * int * kind * con option
adamc@16 201 | RelE of string * con
adamc@20 202 | NamedE of string * int * con * exp option
adamc@16 203
adamc@16 204 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
adamc@16 205 let
adamc@16 206 val mfk = Kind.mapfold fk
adamc@16 207
adamc@16 208 fun bind' (ctx, b) =
adamc@16 209 let
adamc@16 210 val b' = case b of
adamc@16 211 Con.Rel x => RelC x
adamc@16 212 | Con.Named x => NamedC x
adamc@16 213 in
adamc@16 214 bind (ctx, b')
adamc@16 215 end
adamc@16 216 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@16 217
adamc@16 218 fun mfe ctx e acc =
adamc@16 219 S.bindP (mfe' ctx e acc, fe ctx)
adamc@16 220
adamc@16 221 and mfe' ctx (eAll as (e, loc)) =
adamc@16 222 case e of
adamc@16 223 EPrim _ => S.return2 eAll
adamc@16 224 | ERel _ => S.return2 eAll
adamc@16 225 | ENamed _ => S.return2 eAll
adamc@48 226 | EFfi _ => S.return2 eAll
adamc@48 227 | EFfiApp (m, x, es) =>
adamc@48 228 S.map2 (ListUtil.mapfold (fn e => mfe ctx e) es,
adamc@48 229 fn es' =>
adamc@48 230 (EFfiApp (m, x, es'), loc))
adamc@16 231 | EApp (e1, e2) =>
adamc@16 232 S.bind2 (mfe ctx e1,
adamc@16 233 fn e1' =>
adamc@16 234 S.map2 (mfe ctx e2,
adamc@16 235 fn e2' =>
adamc@16 236 (EApp (e1', e2'), loc)))
adamc@26 237 | EAbs (x, dom, ran, e) =>
adamc@26 238 S.bind2 (mfc ctx dom,
adamc@26 239 fn dom' =>
adamc@26 240 S.bind2 (mfc ctx ran,
adamc@26 241 fn ran' =>
adamc@26 242 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 243 fn e' =>
adamc@26 244 (EAbs (x, dom', ran', e'), loc))))
adamc@16 245
adamc@16 246 | ECApp (e, c) =>
adamc@16 247 S.bind2 (mfe ctx e,
adamc@16 248 fn e' =>
adamc@16 249 S.map2 (mfc ctx c,
adamc@16 250 fn c' =>
adamc@16 251 (ECApp (e', c'), loc)))
adamc@16 252 | ECAbs (x, k, e) =>
adamc@16 253 S.bind2 (mfk k,
adamc@16 254 fn k' =>
adamc@16 255 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
adamc@16 256 fn e' =>
adamc@16 257 (ECAbs (x, k', e'), loc)))
adamc@16 258
adamc@16 259 | ERecord xes =>
adamc@29 260 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@16 261 S.bind2 (mfc ctx x,
adamc@16 262 fn x' =>
adamc@29 263 S.bind2 (mfe ctx e,
adamc@29 264 fn e' =>
adamc@29 265 S.map2 (mfc ctx t,
adamc@29 266 fn t' =>
adamc@29 267 (x', e', t')))))
adamc@16 268 xes,
adamc@16 269 fn xes' =>
adamc@16 270 (ERecord xes', loc))
adamc@16 271 | EField (e, c, {field, rest}) =>
adamc@16 272 S.bind2 (mfe ctx e,
adamc@16 273 fn e' =>
adamc@16 274 S.bind2 (mfc ctx c,
adamc@16 275 fn c' =>
adamc@16 276 S.bind2 (mfc ctx field,
adamc@16 277 fn field' =>
adamc@16 278 S.map2 (mfc ctx rest,
adamc@16 279 fn rest' =>
adamc@16 280 (EField (e', c', {field = field', rest = rest'}), loc)))))
adamc@73 281 | EFold k =>
adamc@73 282 S.map2 (mfk k,
adamc@73 283 fn k' =>
adamc@73 284 (EFold k', loc))
adamc@16 285 in
adamc@16 286 mfe
adamc@16 287 end
adamc@16 288
adamc@16 289 fun mapfold {kind = fk, con = fc, exp = fe} =
adamc@16 290 mapfoldB {kind = fk,
adamc@16 291 con = fn () => fc,
adamc@16 292 exp = fn () => fe,
adamc@16 293 bind = fn ((), _) => ()} ()
adamc@16 294
adamc@21 295 fun mapB {kind, con, exp, bind} ctx e =
adamc@21 296 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@21 297 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@21 298 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@21 299 bind = bind} ctx e () of
adamc@21 300 S.Continue (e, ()) => e
adamc@21 301 | S.Return _ => raise Fail "CoreUtil.Exp.mapB: Impossible"
adamc@21 302
adamc@16 303 fun map {kind, con, exp} e =
adamc@16 304 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@16 305 con = fn c => fn () => S.Continue (con c, ()),
adamc@16 306 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@16 307 S.Return () => raise Fail "Core_util.Exp.map"
adamc@16 308 | S.Continue (e, ()) => e
adamc@16 309
adamc@23 310 fun fold {kind, con, exp} s e =
adamc@23 311 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 312 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 313 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@23 314 S.Continue (_, s) => s
adamc@23 315 | S.Return _ => raise Fail "CoreUtil.Exp.fold: Impossible"
adamc@23 316
adamc@16 317 fun exists {kind, con, exp} k =
adamc@16 318 case mapfold {kind = fn k => fn () =>
adamc@16 319 if kind k then
adamc@16 320 S.Return ()
adamc@16 321 else
adamc@16 322 S.Continue (k, ()),
adamc@16 323 con = fn c => fn () =>
adamc@16 324 if con c then
adamc@16 325 S.Return ()
adamc@16 326 else
adamc@16 327 S.Continue (c, ()),
adamc@16 328 exp = fn e => fn () =>
adamc@16 329 if exp e then
adamc@16 330 S.Return ()
adamc@16 331 else
adamc@16 332 S.Continue (e, ())} k () of
adamc@16 333 S.Return _ => true
adamc@16 334 | S.Continue _ => false
adamc@16 335
adamc@16 336 end
adamc@16 337
adamc@20 338 structure Decl = struct
adamc@20 339
adamc@20 340 datatype binder = datatype Exp.binder
adamc@20 341
adamc@20 342 fun mapfoldB {kind = fk, con = fc, exp = fe, decl = fd, bind} =
adamc@20 343 let
adamc@20 344 val mfk = Kind.mapfold fk
adamc@20 345
adamc@20 346 fun bind' (ctx, b) =
adamc@20 347 let
adamc@20 348 val b' = case b of
adamc@20 349 Con.Rel x => RelC x
adamc@20 350 | Con.Named x => NamedC x
adamc@20 351 in
adamc@20 352 bind (ctx, b')
adamc@20 353 end
adamc@20 354 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
adamc@20 355
adamc@20 356 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind}
adamc@20 357
adamc@20 358 fun mfd ctx d acc =
adamc@20 359 S.bindP (mfd' ctx d acc, fd ctx)
adamc@20 360
adamc@20 361 and mfd' ctx (dAll as (d, loc)) =
adamc@20 362 case d of
adamc@20 363 DCon (x, n, k, c) =>
adamc@20 364 S.bind2 (mfk k,
adamc@20 365 fn k' =>
adamc@20 366 S.map2 (mfc ctx c,
adamc@20 367 fn c' =>
adamc@20 368 (DCon (x, n, k', c'), loc)))
adamc@20 369 | DVal (x, n, t, e) =>
adamc@20 370 S.bind2 (mfc ctx t,
adamc@20 371 fn t' =>
adamc@20 372 S.map2 (mfe ctx e,
adamc@20 373 fn e' =>
adamc@20 374 (DVal (x, n, t', e'), loc)))
adamc@20 375 in
adamc@20 376 mfd
adamc@20 377 end
adamc@20 378
adamc@23 379 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 380 mapfoldB {kind = fk,
adamc@23 381 con = fn () => fc,
adamc@23 382 exp = fn () => fe,
adamc@23 383 decl = fn () => fd,
adamc@23 384 bind = fn ((), _) => ()} ()
adamc@23 385
adamc@23 386 fun fold {kind, con, exp, decl} s d =
adamc@23 387 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 388 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 389 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 390 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 391 S.Continue (_, s) => s
adamc@23 392 | S.Return _ => raise Fail "CoreUtil.Decl.fold: Impossible"
adamc@23 393
adamc@16 394 end
adamc@20 395
adamc@20 396 structure File = struct
adamc@20 397
adamc@20 398 datatype binder = datatype Exp.binder
adamc@20 399
adamc@20 400 fun mapfoldB (all as {bind, ...}) =
adamc@20 401 let
adamc@20 402 val mfd = Decl.mapfoldB all
adamc@20 403
adamc@20 404 fun mff ctx ds =
adamc@20 405 case ds of
adamc@20 406 nil => S.return2 nil
adamc@20 407 | d :: ds' =>
adamc@20 408 S.bind2 (mfd ctx d,
adamc@20 409 fn d' =>
adamc@20 410 let
adamc@20 411 val b =
adamc@20 412 case #1 d' of
adamc@20 413 DCon (x, n, k, c) => NamedC (x, n, k, SOME c)
adamc@20 414 | DVal (x, n, t, e) => NamedE (x, n, t, SOME e)
adamc@20 415 val ctx' = bind (ctx, b)
adamc@20 416 in
adamc@20 417 S.map2 (mff ctx' ds',
adamc@20 418 fn ds' =>
adamc@20 419 d' :: ds')
adamc@20 420 end)
adamc@20 421 in
adamc@20 422 mff
adamc@20 423 end
adamc@20 424
adamc@23 425 fun mapfold {kind = fk, con = fc, exp = fe, decl = fd} =
adamc@23 426 mapfoldB {kind = fk,
adamc@23 427 con = fn () => fc,
adamc@23 428 exp = fn () => fe,
adamc@23 429 decl = fn () => fd,
adamc@23 430 bind = fn ((), _) => ()} ()
adamc@23 431
adamc@20 432 fun mapB {kind, con, exp, decl, bind} ctx ds =
adamc@20 433 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
adamc@20 434 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
adamc@20 435 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@20 436 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@20 437 bind = bind} ctx ds () of
adamc@20 438 S.Continue (ds, ()) => ds
adamc@20 439 | S.Return _ => raise Fail "CoreUtil.File.mapB: Impossible"
adamc@20 440
adamc@23 441 fun fold {kind, con, exp, decl} s d =
adamc@23 442 case mapfold {kind = fn k => fn s => S.Continue (k, kind (k, s)),
adamc@23 443 con = fn c => fn s => S.Continue (c, con (c, s)),
adamc@23 444 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@23 445 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@23 446 S.Continue (_, s) => s
adamc@23 447 | S.Return _ => raise Fail "CoreUtil.File.fold: Impossible"
adamc@23 448
adamc@20 449 end
adamc@20 450
adamc@20 451 end