annotate src/elab_util.sml @ 59:abb2b32c19fb

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