annotate src/expl_util.sml @ 190:3eb53c957d10

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