annotate src/elaborate.sml @ 623:588b9d16b00a

Start of kind polymorphism, up to the point where demo/hello elaborates with updated Basis/Top
author Adam Chlipala <adamc@hcoop.net>
date Sun, 22 Feb 2009 16:10:25 -0500
parents d64533157f40
children 12b73f3c108e
rev   line source
adamc@412 1 (* Copyright (c) 2008, Adam Chlipala
adamc@445 2 * All rights reserved.
adamc@445 3 *
adamc@445 4 * Redistribution and use in source and binary forms, with or without
adamc@445 5 * modification, are permitted provided that the following conditions are met:
adamc@445 6 *
adamc@445 7 * - Redistributions of source code must retain the above copyright notice,
adamc@445 8 * this list of conditions and the following disclaimer.
adamc@445 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@445 10 * this list of conditions and the following disclaimer in the documentation
adamc@445 11 * and/or other materials provided with the distribution.
adamc@445 12 * - The names of contributors may not be used to endorse or promote products
adamc@445 13 * derived from this software without specific prior written permission.
adamc@445 14 *
adamc@445 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@445 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@445 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@445 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@445 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@445 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@445 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@445 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@445 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@445 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@445 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@445 26 *)
adamc@403 27
adamc@403 28 structure Elaborate :> ELABORATE = struct
adamc@403 29
adamc@403 30 structure P = Prim
adamc@403 31 structure L = Source
adamc@403 32 structure L' = Elab
adamc@403 33 structure E = ElabEnv
adamc@403 34 structure U = ElabUtil
adamc@403 35 structure D = Disjoint
adamc@403 36
adamc@403 37 open Print
adamc@403 38 open ElabPrint
adamc@403 39 open ElabErr
adamc@403 40
adamc@403 41 structure IM = IntBinaryMap
adamc@403 42
adamc@403 43 structure SK = struct
adamc@403 44 type ord_key = string
adamc@403 45 val compare = String.compare
adamc@403 46 end
adamc@403 47
adamc@403 48 structure SS = BinarySetFn(SK)
adamc@403 49 structure SM = BinaryMapFn(SK)
adamc@403 50
adamc@403 51 val basis_r = ref 0
adamc@403 52
adamc@403 53 fun elabExplicitness e =
adamc@403 54 case e of
adamc@403 55 L.Explicit => L'.Explicit
adamc@403 56 | L.Implicit => L'.Implicit
adamc@403 57
adamc@403 58 fun occursKind r =
adamc@403 59 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
adamc@403 60 | _ => false)
adamc@403 61
adamc@403 62 exception KUnify' of kunify_error
adamc@403 63
adamc@623 64 fun unifyKinds' env (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@403 65 let
adamc@403 66 fun err f = raise KUnify' (f (k1All, k2All))
adamc@403 67 in
adamc@403 68 case (k1, k2) of
adamc@403 69 (L'.KType, L'.KType) => ()
adamc@403 70 | (L'.KUnit, L'.KUnit) => ()
adamc@403 71
adamc@403 72 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@623 73 (unifyKinds' env d1 d2;
adamc@623 74 unifyKinds' env r1 r2)
adamc@403 75 | (L'.KName, L'.KName) => ()
adamc@623 76 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' env k1 k2
adamc@403 77 | (L'.KTuple ks1, L'.KTuple ks2) =>
adamc@623 78 ((ListPair.appEq (fn (k1, k2) => unifyKinds' env k1 k2) (ks1, ks2))
adamc@403 79 handle ListPair.UnequalLengths => err KIncompatible)
adamc@403 80
adamc@623 81 | (L'.KRel n1, L'.KRel n2) =>
adamc@623 82 if n1 = n2 then
adamc@623 83 ()
adamc@623 84 else
adamc@623 85 err KIncompatible
adamc@623 86 | (L'.KFun (x, k1), L'.KFun (_, k2)) =>
adamc@623 87 unifyKinds' (E.pushKRel env x) k1 k2
adamc@623 88
adamc@403 89 | (L'.KError, _) => ()
adamc@403 90 | (_, L'.KError) => ()
adamc@403 91
adamc@623 92 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' env k1All k2All
adamc@623 93 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' env k1All k2All
adamc@403 94
adamc@403 95 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
adamc@403 96 if r1 = r2 then
adamc@403 97 ()
adamc@403 98 else
adamc@403 99 r1 := SOME k2All
adamc@403 100
adamc@403 101 | (L'.KUnif (_, _, r), _) =>
adamc@403 102 if occursKind r k2All then
adamc@403 103 err KOccursCheckFailed
adamc@403 104 else
adamc@403 105 r := SOME k2All
adamc@403 106 | (_, L'.KUnif (_, _, r)) =>
adamc@403 107 if occursKind r k1All then
adamc@403 108 err KOccursCheckFailed
adamc@403 109 else
adamc@403 110 r := SOME k1All
adamc@403 111
adamc@403 112 | _ => err KIncompatible
adamc@403 113 end
adamc@403 114
adamc@403 115 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@403 116
adamc@623 117 fun unifyKinds env k1 k2 =
adamc@623 118 unifyKinds' env k1 k2
adamc@403 119 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@403 120
adamc@403 121 fun checkKind env c k1 k2 =
adamc@623 122 unifyKinds env k1 k2
adamc@403 123 handle KUnify (k1, k2, err) =>
adamc@403 124 conError env (WrongKind (c, k1, k2, err))
adamc@403 125
adamc@403 126 val dummy = ErrorMsg.dummySpan
adamc@403 127
adamc@403 128 val ktype = (L'.KType, dummy)
adamc@403 129 val kname = (L'.KName, dummy)
adamc@403 130 val ktype_record = (L'.KRecord ktype, dummy)
adamc@403 131
adamc@403 132 val cerror = (L'.CError, dummy)
adamc@403 133 val kerror = (L'.KError, dummy)
adamc@403 134 val eerror = (L'.EError, dummy)
adamc@403 135 val sgnerror = (L'.SgnError, dummy)
adamc@403 136 val strerror = (L'.StrError, dummy)
adamc@403 137
adamc@403 138 val int = ref cerror
adamc@403 139 val float = ref cerror
adamc@403 140 val string = ref cerror
adamc@403 141 val table = ref cerror
adamc@403 142
adamc@403 143 local
adamc@403 144 val count = ref 0
adamc@403 145 in
adamc@403 146
adamc@403 147 fun resetKunif () = count := 0
adamc@403 148
adamc@403 149 fun kunif loc =
adamc@403 150 let
adamc@403 151 val n = !count
adamc@403 152 val s = if n <= 26 then
adamc@403 153 str (chr (ord #"A" + n))
adamc@403 154 else
adamc@403 155 "U" ^ Int.toString (n - 26)
adamc@403 156 in
adamc@403 157 count := n + 1;
adamc@403 158 (L'.KUnif (loc, s, ref NONE), dummy)
adamc@403 159 end
adamc@403 160
adamc@403 161 end
adamc@403 162
adamc@403 163 local
adamc@403 164 val count = ref 0
adamc@403 165 in
adamc@403 166
adamc@403 167 fun resetCunif () = count := 0
adamc@403 168
adamc@403 169 fun cunif (loc, k) =
adamc@403 170 let
adamc@403 171 val n = !count
adamc@403 172 val s = if n <= 26 then
adamc@403 173 str (chr (ord #"A" + n))
adamc@403 174 else
adamc@403 175 "U" ^ Int.toString (n - 26)
adamc@403 176 in
adamc@403 177 count := n + 1;
adamc@403 178 (L'.CUnif (loc, k, s, ref NONE), dummy)
adamc@403 179 end
adamc@403 180
adamc@403 181 end
adamc@403 182
adamc@623 183 fun elabKind env (k, loc) =
adamc@403 184 case k of
adamc@403 185 L.KType => (L'.KType, loc)
adamc@623 186 | L.KArrow (k1, k2) => (L'.KArrow (elabKind env k1, elabKind env k2), loc)
adamc@403 187 | L.KName => (L'.KName, loc)
adamc@623 188 | L.KRecord k => (L'.KRecord (elabKind env k), loc)
adamc@403 189 | L.KUnit => (L'.KUnit, loc)
adamc@623 190 | L.KTuple ks => (L'.KTuple (map (elabKind env) ks), loc)
adamc@403 191 | L.KWild => kunif loc
adamc@403 192
adamc@623 193 | L.KVar s => (case E.lookupK env s of
adamc@623 194 NONE =>
adamc@623 195 (kindError env (UnboundKind (loc, s));
adamc@623 196 kerror)
adamc@623 197 | SOME n => (L'.KRel n, loc))
adamc@623 198 | L.KFun (x, k) => (L'.KFun (x, elabKind (E.pushKRel env x) k), loc)
adamc@623 199
adamc@621 200 fun mapKind (dom, ran, loc)=
adamc@621 201 (L'.KArrow ((L'.KArrow (dom, ran), loc),
adamc@621 202 (L'.KArrow ((L'.KRecord dom, loc),
adamc@621 203 (L'.KRecord ran, loc)), loc)), loc)
adamc@403 204
adamc@403 205 fun hnormKind (kAll as (k, _)) =
adamc@403 206 case k of
adamc@403 207 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@403 208 | _ => kAll
adamc@403 209
adamc@623 210 open ElabOps
adamc@623 211 val hnormCon = D.hnormCon
adamc@623 212
adamc@623 213 fun elabConHead (c as (_, loc)) k =
adamc@623 214 let
adamc@623 215 fun unravel (k, c) =
adamc@623 216 case hnormKind k of
adamc@623 217 (L'.KFun (x, k'), _) =>
adamc@623 218 let
adamc@623 219 val u = kunif loc
adamc@623 220
adamc@623 221 val k'' = subKindInKind (0, u) k'
adamc@623 222 in
adamc@623 223 unravel (k'', (L'.CKApp (c, u), loc))
adamc@623 224 end
adamc@623 225 | _ => (c, k)
adamc@623 226 in
adamc@623 227 unravel (k, c)
adamc@623 228 end
adamc@623 229
adamc@403 230 fun elabCon (env, denv) (c, loc) =
adamc@403 231 case c of
adamc@403 232 L.CAnnot (c, k) =>
adamc@403 233 let
adamc@623 234 val k' = elabKind env k
adamc@403 235 val (c', ck, gs) = elabCon (env, denv) c
adamc@403 236 in
adamc@403 237 checkKind env c' ck k';
adamc@403 238 (c', k', gs)
adamc@403 239 end
adamc@403 240
adamc@403 241 | L.TFun (t1, t2) =>
adamc@403 242 let
adamc@403 243 val (t1', k1, gs1) = elabCon (env, denv) t1
adamc@403 244 val (t2', k2, gs2) = elabCon (env, denv) t2
adamc@403 245 in
adamc@403 246 checkKind env t1' k1 ktype;
adamc@403 247 checkKind env t2' k2 ktype;
adamc@403 248 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
adamc@403 249 end
adamc@403 250 | L.TCFun (e, x, k, t) =>
adamc@403 251 let
adamc@403 252 val e' = elabExplicitness e
adamc@623 253 val k' = elabKind env k
adamc@403 254 val env' = E.pushCRel env x k'
adamc@403 255 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@403 256 in
adamc@403 257 checkKind env t' tk ktype;
adamc@403 258 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
adamc@403 259 end
adamc@623 260 | L.TKFun (x, t) =>
adamc@623 261 let
adamc@623 262 val env' = E.pushKRel env x
adamc@623 263 val (t', tk, gs) = elabCon (env', denv) t
adamc@623 264 in
adamc@623 265 checkKind env t' tk ktype;
adamc@623 266 ((L'.TKFun (x, t'), loc), ktype, gs)
adamc@623 267 end
adamc@403 268 | L.CDisjoint (c1, c2, c) =>
adamc@403 269 let
adamc@403 270 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 271 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 272
adamc@403 273 val ku1 = kunif loc
adamc@403 274 val ku2 = kunif loc
adamc@403 275
adamc@403 276 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@403 277 val (c', k, gs4) = elabCon (env, denv') c
adamc@403 278 in
adamc@403 279 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@403 280 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@403 281
adamc@403 282 ((L'.CDisjoint (L'.Instantiate, c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4)
adamc@403 283 end
adamc@403 284 | L.TRecord c =>
adamc@403 285 let
adamc@403 286 val (c', ck, gs) = elabCon (env, denv) c
adamc@403 287 val k = (L'.KRecord ktype, loc)
adamc@403 288 in
adamc@403 289 checkKind env c' ck k;
adamc@403 290 ((L'.TRecord c', loc), ktype, gs)
adamc@403 291 end
adamc@403 292
adamc@403 293 | L.CVar ([], s) =>
adamc@403 294 (case E.lookupC env s of
adamc@403 295 E.NotBound =>
adamc@403 296 (conError env (UnboundCon (loc, s));
adamc@403 297 (cerror, kerror, []))
adamc@403 298 | E.Rel (n, k) =>
adamc@623 299 let
adamc@623 300 val (c, k) = elabConHead (L'.CRel n, loc) k
adamc@623 301 in
adamc@623 302 (c, k, [])
adamc@623 303 end
adamc@403 304 | E.Named (n, k) =>
adamc@623 305 let
adamc@623 306 val (c, k) = elabConHead (L'.CNamed n, loc) k
adamc@623 307 in
adamc@623 308 (c, k, [])
adamc@623 309 end)
adamc@403 310 | L.CVar (m1 :: ms, s) =>
adamc@403 311 (case E.lookupStr env m1 of
adamc@403 312 NONE => (conError env (UnboundStrInCon (loc, m1));
adamc@403 313 (cerror, kerror, []))
adamc@403 314 | SOME (n, sgn) =>
adamc@403 315 let
adamc@403 316 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@403 317 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@403 318 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@403 319 (strerror, sgnerror))
adamc@403 320 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@403 321 ((L'.StrVar n, loc), sgn) ms
adamc@403 322
adamc@403 323 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
adamc@403 324 NONE => (conError env (UnboundCon (loc, s));
adamc@403 325 kerror)
adamc@403 326 | SOME (k, _) => k
adamc@403 327 in
adamc@403 328 ((L'.CModProj (n, ms, s), loc), k, [])
adamc@403 329 end)
adamc@403 330
adamc@403 331 | L.CApp (c1, c2) =>
adamc@403 332 let
adamc@403 333 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 334 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 335 val dom = kunif loc
adamc@403 336 val ran = kunif loc
adamc@403 337 in
adamc@403 338 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@403 339 checkKind env c2' k2 dom;
adamc@403 340 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
adamc@403 341 end
adamc@403 342 | L.CAbs (x, ko, t) =>
adamc@403 343 let
adamc@403 344 val k' = case ko of
adamc@403 345 NONE => kunif loc
adamc@623 346 | SOME k => elabKind env k
adamc@403 347 val env' = E.pushCRel env x k'
adamc@403 348 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@403 349 in
adamc@403 350 ((L'.CAbs (x, k', t'), loc),
adamc@403 351 (L'.KArrow (k', tk), loc),
adamc@403 352 gs)
adamc@403 353 end
adamc@623 354 | L.CKAbs (x, t) =>
adamc@623 355 let
adamc@623 356 val env' = E.pushKRel env x
adamc@623 357 val (t', tk, gs) = elabCon (env', denv) t
adamc@623 358 in
adamc@623 359 ((L'.CKAbs (x, t'), loc),
adamc@623 360 (L'.KFun (x, tk), loc),
adamc@623 361 gs)
adamc@623 362 end
adamc@403 363
adamc@403 364 | L.CName s =>
adamc@403 365 ((L'.CName s, loc), kname, [])
adamc@403 366
adamc@403 367 | L.CRecord xcs =>
adamc@403 368 let
adamc@403 369 val k = kunif loc
adamc@403 370
adamc@403 371 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
adamc@403 372 let
adamc@403 373 val (x', xk, gs1) = elabCon (env, denv) x
adamc@403 374 val (c', ck, gs2) = elabCon (env, denv) c
adamc@403 375 in
adamc@403 376 checkKind env x' xk kname;
adamc@403 377 checkKind env c' ck k;
adamc@403 378 ((x', c'), gs1 @ gs2 @ gs)
adamc@403 379 end) [] xcs
adamc@403 380
adamc@403 381 val rc = (L'.CRecord (k, xcs'), loc)
adamc@403 382 (* Add duplicate field checking later. *)
adamc@403 383
adamc@403 384 fun prove (xcs, ds) =
adamc@403 385 case xcs of
adamc@403 386 [] => ds
adamc@403 387 | xc :: rest =>
adamc@403 388 let
adamc@403 389 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@403 390 val ds = foldl (fn (xc', ds) =>
adamc@403 391 let
adamc@403 392 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@403 393 in
adamc@403 394 D.prove env denv (r1, r2, loc) @ ds
adamc@403 395 end)
adamc@403 396 ds rest
adamc@403 397 in
adamc@403 398 prove (rest, ds)
adamc@403 399 end
adamc@403 400 in
adamc@403 401 (rc, (L'.KRecord k, loc), prove (xcs', gs))
adamc@403 402 end
adamc@403 403 | L.CConcat (c1, c2) =>
adamc@403 404 let
adamc@403 405 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 406 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 407 val ku = kunif loc
adamc@403 408 val k = (L'.KRecord ku, loc)
adamc@403 409 in
adamc@403 410 checkKind env c1' k1 k;
adamc@403 411 checkKind env c2' k2 k;
adamc@403 412 ((L'.CConcat (c1', c2'), loc), k,
adamc@403 413 D.prove env denv (c1', c2', loc) @ gs1 @ gs2)
adamc@403 414 end
adamc@621 415 | L.CMap =>
adamc@403 416 let
adamc@403 417 val dom = kunif loc
adamc@403 418 val ran = kunif loc
adamc@403 419 in
adamc@621 420 ((L'.CMap (dom, ran), loc),
adamc@621 421 mapKind (dom, ran, loc),
adamc@403 422 [])
adamc@403 423 end
adamc@403 424
adamc@403 425 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
adamc@403 426
adamc@403 427 | L.CTuple cs =>
adamc@403 428 let
adamc@403 429 val (cs', ks, gs) = foldl (fn (c, (cs', ks, gs)) =>
adamc@403 430 let
adamc@403 431 val (c', k, gs') = elabCon (env, denv) c
adamc@403 432 in
adamc@403 433 (c' :: cs', k :: ks, gs' @ gs)
adamc@403 434 end) ([], [], []) cs
adamc@403 435 in
adamc@403 436 ((L'.CTuple (rev cs'), loc), (L'.KTuple (rev ks), loc), gs)
adamc@403 437 end
adamc@403 438 | L.CProj (c, n) =>
adamc@403 439 let
adamc@403 440 val (c', k, gs) = elabCon (env, denv) c
adamc@403 441 in
adamc@403 442 case hnormKind k of
adamc@403 443 (L'.KTuple ks, _) =>
adamc@403 444 if n <= 0 orelse n > length ks then
adamc@403 445 (conError env (ProjBounds (c', n));
adamc@83 446 (cerror, kerror, []))
adamc@403 447 else
adamc@403 448 ((L'.CProj (c', n), loc), List.nth (ks, n - 1), gs)
adamc@403 449 | k => (conError env (ProjMismatch (c', k));
adamc@403 450 (cerror, kerror, []))
adamc@403 451 end
adamc@403 452
adamc@403 453 | L.CWild k =>
adamc@403 454 let
adamc@623 455 val k' = elabKind env k
adamc@403 456 in
adamc@403 457 (cunif (loc, k'), k', [])
adamc@403 458 end
adamc@403 459
adamc@403 460 fun kunifsRemain k =
adamc@403 461 case k of
adamc@403 462 L'.KUnif (_, _, ref NONE) => true
adamc@403 463 | _ => false
adamc@403 464 fun cunifsRemain c =
adamc@403 465 case c of
adamc@403 466 L'.CUnif (loc, _, _, ref NONE) => SOME loc
adamc@403 467 | _ => NONE
adamc@403 468
adamc@403 469 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
adamc@403 470 con = fn _ => false,
adamc@403 471 exp = fn _ => false,
adamc@403 472 sgn_item = fn _ => false,
adamc@403 473 sgn = fn _ => false,
adamc@403 474 str = fn _ => false,
adamc@403 475 decl = fn _ => false}
adamc@403 476
adamc@403 477 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
adamc@403 478 con = cunifsRemain,
adamc@403 479 exp = fn _ => NONE,
adamc@403 480 sgn_item = fn _ => NONE,
adamc@403 481 sgn = fn _ => NONE,
adamc@403 482 str = fn _ => NONE,
adamc@403 483 decl = fn _ => NONE}
adamc@403 484
adamc@403 485 fun occursCon r =
adamc@403 486 U.Con.exists {kind = fn _ => false,
adamc@403 487 con = fn L'.CUnif (_, _, _, r') => r = r'
adamc@403 488 | _ => false}
adamc@403 489
adamc@403 490 exception CUnify' of cunify_error
adamc@403 491
adamc@403 492 exception SynUnif = E.SynUnif
adamc@403 493
adamc@403 494 type record_summary = {
adamc@403 495 fields : (L'.con * L'.con) list,
adamc@403 496 unifs : (L'.con * L'.con option ref) list,
adamc@403 497 others : L'.con list
adamc@403 498 }
adamc@403 499
adamc@403 500 fun summaryToCon {fields, unifs, others} =
adamc@403 501 let
adamc@403 502 val c = (L'.CRecord (ktype, []), dummy)
adamc@403 503 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@403 504 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@403 505 in
adamc@403 506 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@403 507 end
adamc@403 508
adamc@403 509 fun p_summary env s = p_con env (summaryToCon s)
adamc@403 510
adamc@403 511 exception CUnify of L'.con * L'.con * cunify_error
adamc@403 512
adamc@403 513 fun kindof env (c, loc) =
adamc@403 514 case c of
adamc@403 515 L'.TFun _ => ktype
adamc@403 516 | L'.TCFun _ => ktype
adamc@403 517 | L'.TRecord _ => ktype
adamc@403 518
adamc@403 519 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@403 520 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@403 521 | L'.CModProj (n, ms, x) =>
adamc@403 522 let
adamc@403 523 val (_, sgn) = E.lookupStrNamed env n
adamc@403 524 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@403 525 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@403 526 NONE => raise Fail "kindof: Unknown substructure"
adamc@403 527 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@403 528 ((L'.StrVar n, loc), sgn) ms
adamc@403 529 in
adamc@403 530 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@403 531 NONE => raise Fail "kindof: Unknown con in structure"
adamc@403 532 | SOME (k, _) => k
adamc@403 533 end
adamc@403 534
adamc@403 535 | L'.CApp (c, _) =>
adamc@403 536 (case hnormKind (kindof env c) of
adamc@403 537 (L'.KArrow (_, k), _) => k
adamc@403 538 | (L'.KError, _) => kerror
adamc@413 539 | k => raise CUnify' (CKindof (k, c, "arrow")))
adamc@403 540 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@403 541 | L'.CDisjoint (_, _, _, c) => kindof env c
adamc@403 542
adamc@403 543 | L'.CName _ => kname
adamc@403 544
adamc@403 545 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@403 546 | L'.CConcat (c, _) => kindof env c
adamc@621 547 | L'.CMap (dom, ran) => mapKind (dom, ran, loc)
adamc@403 548
adamc@403 549 | L'.CUnit => (L'.KUnit, loc)
adamc@403 550
adamc@403 551 | L'.CTuple cs => (L'.KTuple (map (kindof env) cs), loc)
adamc@403 552 | L'.CProj (c, n) =>
adamc@403 553 (case hnormKind (kindof env c) of
adamc@403 554 (L'.KTuple ks, _) => List.nth (ks, n - 1)
adamc@413 555 | k => raise CUnify' (CKindof (k, c, "tuple")))
adamc@403 556
adamc@403 557 | L'.CError => kerror
adamc@403 558 | L'.CUnif (_, k, _, _) => k
adamc@403 559
adamc@623 560 | L'.CKAbs (x, c) => (L'.KFun (x, kindof (E.pushKRel env x) c), loc)
adamc@623 561 | L'.CKApp (c, k) =>
adamc@623 562 (case hnormKind (kindof env c) of
adamc@623 563 (L'.KFun (_, k'), _) => subKindInKind (0, k) k'
adamc@623 564 | k => raise CUnify' (CKindof (k, c, "kapp")))
adamc@623 565 | L'.TKFun _ => ktype
adamc@403 566
adamc@621 567 fun deConstraintCon (env, denv) c =
adamc@403 568 let
adamc@403 569 val (c, gs) = hnormCon (env, denv) c
adamc@403 570 in
adamc@403 571 case #1 c of
adamc@621 572 L'.CDisjoint (_, _, _, c) =>
adamc@34 573 let
adamc@621 574 val (c', gs') = deConstraintCon (env, denv) c
adamc@34 575 in
adamc@621 576 (c', gs @ gs')
adamc@403 577 end
adamc@621 578 | _ => (c, gs)
adamc@403 579 end
adamc@403 580
adamc@621 581 exception GuessFailure
adamc@403 582
adamc@513 583 fun isUnitCon env (c, loc) =
adamc@513 584 case c of
adamc@513 585 L'.TFun _ => false
adamc@513 586 | L'.TCFun _ => false
adamc@513 587 | L'.TRecord _ => false
adamc@513 588
adamc@513 589 | L'.CRel xn => #1 (#2 (E.lookupCRel env xn)) = L'.KUnit
adamc@513 590 | L'.CNamed xn => #1 (#2 (E.lookupCNamed env xn)) = L'.KUnit
adamc@518 591 | L'.CModProj (n, ms, x) => false
adamc@518 592 (*let
adamc@513 593 val (_, sgn) = E.lookupStrNamed env n
adamc@513 594 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@513 595 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@513 596 NONE => raise Fail "kindof: Unknown substructure"
adamc@513 597 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@513 598 ((L'.StrVar n, loc), sgn) ms
adamc@513 599 in
adamc@513 600 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@513 601 NONE => raise Fail "kindof: Unknown con in structure"
adamc@513 602 | SOME ((k, _), _) => k = L'.KUnit
adamc@518 603 end*)
adamc@518 604
adamc@518 605 | L'.CApp (c, _) => false
adamc@518 606 (*(case hnormKind (kindof env c) of
adamc@513 607 (L'.KArrow (_, k), _) => #1 k = L'.KUnit
adamc@513 608 | (L'.KError, _) => false
adamc@518 609 | k => raise CUnify' (CKindof (k, c, "arrow")))*)
adamc@513 610 | L'.CAbs _ => false
adamc@518 611 | L'.CDisjoint (_, _, _, c) => false(*isUnitCon env c*)
adamc@513 612
adamc@513 613 | L'.CName _ => false
adamc@513 614
adamc@513 615 | L'.CRecord _ => false
adamc@513 616 | L'.CConcat _ => false
adamc@621 617 | L'.CMap _ => false
adamc@513 618
adamc@513 619 | L'.CUnit => true
adamc@513 620
adamc@513 621 | L'.CTuple _ => false
adamc@518 622 | L'.CProj (c, n) => false
adamc@518 623 (*(case hnormKind (kindof env c) of
adamc@513 624 (L'.KTuple ks, _) => #1 (List.nth (ks, n - 1)) = L'.KUnit
adamc@518 625 | k => raise CUnify' (CKindof (k, c, "tuple")))*)
adamc@513 626
adamc@513 627 | L'.CError => false
adamc@513 628 | L'.CUnif (_, k, _, _) => #1 k = L'.KUnit
adamc@513 629
adamc@623 630 | L'.CKAbs _ => false
adamc@623 631 | L'.CKApp _ => false
adamc@623 632 | L'.TKFun _ => false
adamc@623 633
adamc@403 634 fun unifyRecordCons (env, denv) (c1, c2) =
adamc@403 635 let
adamc@403 636 fun rkindof c =
adamc@403 637 case hnormKind (kindof env c) of
adamc@403 638 (L'.KRecord k, _) => k
adamc@403 639 | (L'.KError, _) => kerror
adamc@413 640 | k => raise CUnify' (CKindof (k, c, "record"))
adamc@403 641
adamc@403 642 val k1 = rkindof c1
adamc@403 643 val k2 = rkindof c2
adamc@403 644
adamc@403 645 val (r1, gs1) = recordSummary (env, denv) c1
adamc@403 646 val (r2, gs2) = recordSummary (env, denv) c2
adamc@403 647 in
adamc@623 648 unifyKinds env k1 k2;
adamc@403 649 unifySummaries (env, denv) (k1, r1, r2);
adamc@403 650 gs1 @ gs2
adamc@403 651 end
adamc@403 652
adamc@403 653 and recordSummary (env, denv) c =
adamc@403 654 let
adamc@403 655 val (c, gs) = hnormCon (env, denv) c
adamc@403 656
adamc@403 657 val (sum, gs') =
adamc@403 658 case c of
adamc@403 659 (L'.CRecord (_, xcs), _) => ({fields = xcs, unifs = [], others = []}, [])
adamc@403 660 | (L'.CConcat (c1, c2), _) =>
adamc@403 661 let
adamc@403 662 val (s1, gs1) = recordSummary (env, denv) c1
adamc@403 663 val (s2, gs2) = recordSummary (env, denv) c2
adamc@403 664 in
adamc@403 665 ({fields = #fields s1 @ #fields s2,
adamc@403 666 unifs = #unifs s1 @ #unifs s2,
adamc@403 667 others = #others s1 @ #others s2},
adamc@403 668 gs1 @ gs2)
adamc@403 669 end
adamc@403 670 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary (env, denv) c
adamc@403 671 | c' as (L'.CUnif (_, _, _, r), _) => ({fields = [], unifs = [(c', r)], others = []}, [])
adamc@403 672 | c' => ({fields = [], unifs = [], others = [c']}, [])
adamc@403 673 in
adamc@403 674 (sum, gs @ gs')
adamc@403 675 end
adamc@403 676
adamc@403 677 and consEq (env, denv) (c1, c2) =
adamc@403 678 let
adamc@403 679 val gs = unifyCons (env, denv) c1 c2
adamc@403 680 in
adamc@403 681 List.all (fn (loc, env, denv, c1, c2) =>
adamc@403 682 case D.prove env denv (c1, c2, loc) of
adamc@403 683 [] => true
adamc@403 684 | _ => false) gs
adamc@403 685 end
adamc@403 686 handle CUnify _ => false
adamc@403 687
adamc@403 688 and consNeq env (c1, c2) =
adamc@403 689 case (#1 (ElabOps.hnormCon env c1), #1 (ElabOps.hnormCon env c2)) of
adamc@403 690 (L'.CName x1, L'.CName x2) => x1 <> x2
adamc@403 691 | _ => false
adamc@403 692
adamc@403 693 and unifySummaries (env, denv) (k, s1 : record_summary, s2 : record_summary) =
adamc@403 694 let
adamc@403 695 val loc = #2 k
adamc@403 696 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@403 697 ("#2", p_summary env s2)]*)
adamc@403 698
adamc@403 699 fun eatMatching p (ls1, ls2) =
adamc@403 700 let
adamc@403 701 fun em (ls1, ls2, passed1) =
adamc@403 702 case ls1 of
adamc@403 703 [] => (rev passed1, ls2)
adamc@403 704 | h1 :: t1 =>
adamc@403 705 let
adamc@403 706 fun search (ls2', passed2) =
adamc@403 707 case ls2' of
adamc@403 708 [] => em (t1, ls2, h1 :: passed1)
adamc@403 709 | h2 :: t2 =>
adamc@403 710 if p (h1, h2) then
adamc@403 711 em (t1, List.revAppend (passed2, t2), passed1)
adamc@403 712 else
adamc@403 713 search (t2, h2 :: passed2)
adamc@403 714 in
adamc@403 715 search (ls2, [])
adamc@403 716 end
adamc@403 717 in
adamc@403 718 em (ls1, ls2, [])
adamc@403 719 end
adamc@403 720
adamc@403 721 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@403 722 not (consNeq env (x1, x2))
adamc@403 723 andalso consEq (env, denv) (c1, c2)
adamc@403 724 andalso consEq (env, denv) (x1, x2))
adamc@403 725 (#fields s1, #fields s2)
adamc@403 726 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@403 727 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@403 728 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@403 729 val (others1, others2) = eatMatching (consEq (env, denv)) (#others s1, #others s2)
adamc@403 730 (*val () = eprefaces "Summaries3" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@403 731 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 732
adamc@403 733 fun unifFields (fs, others, unifs) =
adamc@403 734 case (fs, others, unifs) of
adamc@403 735 ([], [], _) => ([], [], unifs)
adamc@403 736 | (_, _, []) => (fs, others, [])
adamc@403 737 | (_, _, (_, r) :: rest) =>
adamc@403 738 let
adamc@403 739 val r' = ref NONE
adamc@403 740 val kr = (L'.KRecord k, dummy)
adamc@403 741 val cr' = (L'.CUnif (dummy, kr, "recd", r'), dummy)
adamc@403 742
adamc@403 743 val prefix = case (fs, others) of
adamc@403 744 ([], other :: others) =>
adamc@403 745 List.foldl (fn (other, c) =>
adamc@403 746 (L'.CConcat (c, other), dummy))
adamc@403 747 other others
adamc@403 748 | (fs, []) =>
adamc@403 749 (L'.CRecord (k, fs), dummy)
adamc@403 750 | (fs, others) =>
adamc@403 751 List.foldl (fn (other, c) =>
adamc@403 752 (L'.CConcat (c, other), dummy))
adamc@403 753 (L'.CRecord (k, fs), dummy) others
adamc@403 754 in
adamc@403 755 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@403 756 ([], [], (cr', r') :: rest)
adamc@403 757 end
adamc@403 758
adamc@403 759 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@403 760 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@403 761
adamc@403 762 (*val () = eprefaces "Summaries4" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@403 763 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 764
adamc@403 765 fun isGuessable (other, fs) =
adamc@403 766 let
adamc@621 767 val gs = guessMap (env, denv) (other, (L'.CRecord (k, fs), loc), [], GuessFailure)
adamc@403 768 in
adamc@403 769 List.all (fn (loc, env, denv, c1, c2) =>
adamc@403 770 case D.prove env denv (c1, c2, loc) of
adamc@403 771 [] => true
adamc@403 772 | _ => false) gs
adamc@403 773 end
adamc@621 774 handle GuessFailure => false
adamc@403 775
adamc@403 776 val (fs1, fs2, others1, others2) =
adamc@403 777 case (fs1, fs2, others1, others2) of
adamc@403 778 ([], _, [other1], []) =>
adamc@403 779 if isGuessable (other1, fs2) then
adamc@403 780 ([], [], [], [])
adamc@403 781 else
adamc@403 782 (fs1, fs2, others1, others2)
adamc@403 783 | (_, [], [], [other2]) =>
adamc@403 784 if isGuessable (other2, fs1) then
adamc@403 785 ([], [], [], [])
adamc@403 786 else
adamc@403 787 (fs1, fs2, others1, others2)
adamc@403 788 | _ => (fs1, fs2, others1, others2)
adamc@403 789
adamc@403 790 (*val () = eprefaces "Summaries5" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@403 791 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 792
adamc@403 793 val clear = case (fs1, others1, fs2, others2) of
adamc@403 794 ([], [], [], []) => true
adamc@403 795 | _ => false
adamc@403 796 val empty = (L'.CRecord (k, []), dummy)
adamc@403 797
adamc@403 798 fun unsummarize {fields, unifs, others} =
adamc@403 799 let
adamc@403 800 val c = (L'.CRecord (k, fields), loc)
adamc@403 801
adamc@403 802 val c = foldl (fn ((c1, _), c2) => (L'.CConcat (c1, c2), loc))
adamc@403 803 c unifs
adamc@403 804 in
adamc@403 805 foldl (fn (c1, c2) => (L'.CConcat (c1, c2), loc))
adamc@403 806 c others
adamc@403 807 end
adamc@403 808
adamc@403 809 fun pairOffUnifs (unifs1, unifs2) =
adamc@403 810 case (unifs1, unifs2) of
adamc@403 811 ([], _) =>
adamc@403 812 if clear then
adamc@403 813 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@403 814 else
adamc@403 815 raise CUnify' (CRecordFailure (unsummarize s1, unsummarize s2))
adamc@403 816 | (_, []) =>
adamc@403 817 if clear then
adamc@403 818 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@403 819 else
adamc@403 820 raise CUnify' (CRecordFailure (unsummarize s1, unsummarize s2))
adamc@403 821 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@403 822 (r2 := SOME c1;
adamc@403 823 pairOffUnifs (rest1, rest2))
adamc@403 824 in
adamc@403 825 pairOffUnifs (unifs1, unifs2)
adamc@403 826 (*before eprefaces "Summaries'" [("#1", p_summary env s1),
adamc@403 827 ("#2", p_summary env s2)]*)
adamc@403 828 end
adamc@403 829
adamc@621 830 and guessMap (env, denv) (c1, c2, gs, ex) =
adamc@403 831 let
adamc@403 832 val loc = #2 c1
adamc@403 833
adamc@621 834 fun unfold (dom, ran, f, r, c) =
adamc@403 835 let
adamc@621 836 fun unfold (r, c) =
adamc@621 837 let
adamc@621 838 val (c', gs1) = deConstraintCon (env, denv) c
adamc@621 839 in
adamc@621 840 case #1 c' of
adamc@621 841 L'.CRecord (_, []) =>
adamc@621 842 let
adamc@621 843 val gs2 = unifyCons (env, denv) r (L'.CRecord (dom, []), loc)
adamc@621 844 in
adamc@621 845 gs1 @ gs2
adamc@621 846 end
adamc@621 847 | L'.CRecord (_, [(x, v)]) =>
adamc@621 848 let
adamc@621 849 val v' = case dom of
adamc@621 850 (L'.KUnit, _) => (L'.CUnit, loc)
adamc@621 851 | _ => cunif (loc, dom)
adamc@622 852 val gs2 = unifyCons (env, denv) v (L'.CApp (f, v'), loc)
adamc@621 853
adamc@621 854 val gs3 = unifyCons (env, denv) r (L'.CRecord (dom, [(x, v')]), loc)
adamc@621 855 in
adamc@621 856 gs1 @ gs2 @ gs3
adamc@621 857 end
adamc@621 858 | L'.CRecord (_, (x, v) :: rest) =>
adamc@621 859 let
adamc@621 860 val r1 = cunif (loc, (L'.KRecord dom, loc))
adamc@621 861 val r2 = cunif (loc, (L'.KRecord dom, loc))
adamc@622 862
adamc@622 863 val gs2 = unfold (r1, (L'.CRecord (ran, [(x, v)]), loc))
adamc@622 864 val gs3 = unfold (r2, (L'.CRecord (ran, rest), loc))
adamc@622 865 val gs4 = unifyCons (env, denv) r (L'.CConcat (r1, r2), loc)
adamc@621 866 in
adamc@621 867 gs1 @ gs2 @ gs3 @ gs4
adamc@621 868 end
adamc@621 869 | L'.CConcat (c1', c2') =>
adamc@621 870 let
adamc@621 871 val r1 = cunif (loc, (L'.KRecord dom, loc))
adamc@621 872 val r2 = cunif (loc, (L'.KRecord dom, loc))
adamc@622 873
adamc@622 874 val gs2 = unfold (r1, c1')
adamc@622 875 val gs3 = unfold (r2, c2')
adamc@622 876 val gs4 = unifyCons (env, denv) r (L'.CConcat (r1, r2), loc)
adamc@621 877 in
adamc@621 878 gs1 @ gs2 @ gs3 @ gs4
adamc@621 879 end
adamc@621 880 | _ => raise ex
adamc@621 881 end
adamc@403 882 in
adamc@621 883 unfold (r, c)
adamc@403 884 end
adamc@622 885 handle _ => (TextIO.print "Guess failure!\n"; raise ex)
adamc@403 886 in
adamc@403 887 case (#1 c1, #1 c2) of
adamc@621 888 (L'.CApp ((L'.CApp ((L'.CMap (dom, ran), _), f), _), r), _) =>
adamc@621 889 unfold (dom, ran, f, r, c2)
adamc@621 890 | (_, L'.CApp ((L'.CApp ((L'.CMap (dom, ran), _), f), _), r)) =>
adamc@621 891 unfold (dom, ran, f, r, c1)
adamc@403 892 | _ => raise ex
adamc@403 893 end
adamc@403 894
adamc@403 895 and unifyCons' (env, denv) c1 c2 =
adamc@403 896 case (#1 c1, #1 c2) of
adamc@403 897 (L'.CDisjoint (_, x1, y1, t1), L'.CDisjoint (_, x2, y2, t2)) =>
adamc@403 898 let
adamc@403 899 val gs1 = unifyCons' (env, denv) x1 x2
adamc@403 900 val gs2 = unifyCons' (env, denv) y1 y2
adamc@403 901 val (denv', gs3) = D.assert env denv (x1, y1)
adamc@403 902 val gs4 = unifyCons' (env, denv') t1 t2
adamc@403 903 in
adamc@403 904 gs1 @ gs2 @ gs3 @ gs4
adamc@403 905 end
adamc@403 906 | _ =>
adamc@513 907 if isUnitCon env c1 andalso isUnitCon env c2 then
adamc@513 908 []
adamc@513 909 else
adamc@403 910 let
adamc@514 911 (*val befor = Time.now ()
adamc@514 912 val old1 = c1
adamc@514 913 val old2 = c2*)
adamc@411 914 val (c1, gs1) = hnormCon (env, denv) c1
adamc@411 915 val (c2, gs2) = hnormCon (env, denv) c2
adamc@403 916 in
adamc@411 917 let
adamc@623 918 (*val () = prefaces "unifyCons'" [("old1", p_con env old1),
adamc@623 919 ("old2", p_con env old2),
adamc@623 920 ("c1", p_con env c1),
adamc@623 921 ("c2", p_con env c2)]*)
adamc@623 922
adamc@411 923 val gs3 = unifyCons'' (env, denv) c1 c2
adamc@411 924 in
adamc@411 925 gs1 @ gs2 @ gs3
adamc@411 926 end
adamc@621 927 handle ex => guessMap (env, denv) (c1, c2, gs1 @ gs2, ex)
adamc@403 928 end
adamc@403 929
adamc@403 930 and unifyCons'' (env, denv) (c1All as (c1, loc)) (c2All as (c2, _)) =
adamc@403 931 let
adamc@403 932 fun err f = raise CUnify' (f (c1All, c2All))
adamc@403 933
adamc@403 934 fun isRecord () = unifyRecordCons (env, denv) (c1All, c2All)
adamc@403 935 in
adamc@403 936 (*eprefaces "unifyCons''" [("c1All", p_con env c1All),
adamc@403 937 ("c2All", p_con env c2All)];*)
adamc@403 938
adamc@403 939 case (c1, c2) of
adamc@403 940 (L'.CUnit, L'.CUnit) => []
adamc@403 941
adamc@403 942 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@403 943 unifyCons' (env, denv) d1 d2
adamc@403 944 @ unifyCons' (env, denv) r1 r2
adamc@403 945 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@403 946 if expl1 <> expl2 then
adamc@403 947 err CExplicitness
adamc@403 948 else
adamc@623 949 (unifyKinds env d1 d2;
adamc@514 950 let
adamc@514 951 val denv' = D.enter denv
adamc@514 952 (*val befor = Time.now ()*)
adamc@514 953 val env' = E.pushCRel env x1 d1
adamc@514 954 in
adamc@514 955 (*TextIO.print ("E.pushCRel: "
adamc@514 956 ^ LargeReal.toString (Time.toReal (Time.- (Time.now (), befor)))
adamc@514 957 ^ "\n");*)
adamc@514 958 unifyCons' (env', denv') r1 r2
adamc@514 959 end)
adamc@403 960 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' (env, denv) r1 r2
adamc@403 961
adamc@403 962 | (L'.CRel n1, L'.CRel n2) =>
adamc@403 963 if n1 = n2 then
adamc@403 964 []
adamc@403 965 else
adamc@403 966 err CIncompatible
adamc@403 967 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@403 968 if n1 = n2 then
adamc@403 969 []
adamc@403 970 else
adamc@403 971 err CIncompatible
adamc@403 972
adamc@403 973 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@403 974 (unifyCons' (env, denv) d1 d2;
adamc@403 975 unifyCons' (env, denv) r1 r2)
adamc@403 976 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@623 977 (unifyKinds env k1 k2;
adamc@403 978 unifyCons' (E.pushCRel env x1 k1, D.enter denv) c1 c2)
adamc@403 979
adamc@403 980 | (L'.CName n1, L'.CName n2) =>
adamc@403 981 if n1 = n2 then
adamc@403 982 []
adamc@403 983 else
adamc@403 984 err CIncompatible
adamc@403 985
adamc@403 986 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@403 987 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@403 988 []
adamc@403 989 else
adamc@403 990 err CIncompatible
adamc@403 991
adamc@403 992 | (L'.CTuple cs1, L'.CTuple cs2) =>
adamc@403 993 ((ListPair.foldlEq (fn (c1, c2, gs) =>
adamc@403 994 let
adamc@403 995 val gs' = unifyCons' (env, denv) c1 c2
adamc@403 996 in
adamc@403 997 gs' @ gs
adamc@403 998 end) [] (cs1, cs2))
adamc@403 999 handle ListPair.UnequalLengths => err CIncompatible)
adamc@403 1000
adamc@403 1001 | (L'.CProj ((L'.CUnif (_, _, _, ref (SOME c1)), loc), n1), _) =>
adamc@403 1002 unifyCons' (env, denv) (L'.CProj (c1, n1), loc) c2All
adamc@403 1003 | (_, L'.CProj ((L'.CUnif (_, _, _, ref (SOME c2)), loc), n2)) =>
adamc@403 1004 unifyCons' (env, denv) c1All (L'.CProj (c2, n2), loc)
adamc@403 1005 | (L'.CProj ((L'.CUnif (_, (L'.KTuple ks, _), _, r), loc), n), _) =>
adamc@403 1006 let
adamc@403 1007 val us = map (fn k => cunif (loc, k)) ks
adamc@403 1008 in
adamc@403 1009 r := SOME (L'.CTuple us, loc);
adamc@403 1010 unifyCons' (env, denv) (List.nth (us, n - 1)) c2All
adamc@403 1011 end
adamc@403 1012 | (_, L'.CProj ((L'.CUnif (_, (L'.KTuple ks, _), _, r), loc), n)) =>
adamc@403 1013 let
adamc@403 1014 val us = map (fn k => cunif (loc, k)) ks
adamc@403 1015 in
adamc@403 1016 r := SOME (L'.CTuple us, loc);
adamc@403 1017 unifyCons' (env, denv) c1All (List.nth (us, n - 1))
adamc@403 1018 end
adamc@403 1019 | (L'.CProj (c1, n1), L'.CProj (c2, n2)) =>
adamc@403 1020 if n1 = n2 then
adamc@403 1021 unifyCons' (env, denv) c1 c2
adamc@403 1022 else
adamc@403 1023 err CIncompatible
adamc@403 1024
adamc@623 1025 | (L'.CMap (dom1, ran1), L'.CMap (dom2, ran2)) =>
adamc@623 1026 (unifyKinds env dom1 dom2;
adamc@623 1027 unifyKinds env ran1 ran2;
adamc@623 1028 [])
adamc@623 1029
adamc@623 1030 | (L'.CKAbs (x, c1), L'.CKAbs (_, c2)) =>
adamc@623 1031 unifyCons' (E.pushKRel env x, denv) c1 c2
adamc@623 1032 | (L'.CKApp (c1, k1), L'.CKApp (c2, k2)) =>
adamc@623 1033 (unifyKinds env k1 k2;
adamc@623 1034 unifyCons' (env, denv) c1 c2)
adamc@623 1035 | (L'.TKFun (x, c1), L'.TKFun (_, c2)) =>
adamc@623 1036 unifyCons' (E.pushKRel env x, denv) c1 c2
adamc@623 1037
adamc@403 1038 | (L'.CError, _) => []
adamc@403 1039 | (_, L'.CError) => []
adamc@403 1040
adamc@403 1041 | (L'.CRecord _, _) => isRecord ()
adamc@403 1042 | (_, L'.CRecord _) => isRecord ()
adamc@403 1043 | (L'.CConcat _, _) => isRecord ()
adamc@403 1044 | (_, L'.CConcat _) => isRecord ()
adamc@403 1045
adamc@403 1046 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
adamc@403 1047 if r1 = r2 then
adamc@403 1048 []
adamc@403 1049 else
adamc@623 1050 (unifyKinds env k1 k2;
adamc@403 1051 r1 := SOME c2All;
adamc@403 1052 [])
adamc@403 1053
adamc@403 1054 | (L'.CUnif (_, _, _, r), _) =>
adamc@403 1055 if occursCon r c2All then
adamc@403 1056 err COccursCheckFailed
adamc@403 1057 else
adamc@403 1058 (r := SOME c2All;
adamc@403 1059 [])
adamc@403 1060 | (_, L'.CUnif (_, _, _, r)) =>
adamc@403 1061 if occursCon r c1All then
adamc@403 1062 err COccursCheckFailed
adamc@403 1063 else
adamc@403 1064 (r := SOME c1All;
adamc@403 1065 [])
adamc@403 1066
adamc@403 1067 | _ => err CIncompatible
adamc@403 1068 end
adamc@403 1069
adamc@403 1070 and unifyCons (env, denv) c1 c2 =
adamc@403 1071 unifyCons' (env, denv) c1 c2
adamc@403 1072 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@403 1073 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@403 1074
adamc@403 1075 fun checkCon (env, denv) e c1 c2 =
adamc@403 1076 unifyCons (env, denv) c1 c2
adamc@403 1077 handle CUnify (c1, c2, err) =>
adamc@403 1078 (expError env (Unify (e, c1, c2, err));
adamc@83 1079 [])
adamc@403 1080
adamc@403 1081 fun checkPatCon (env, denv) p c1 c2 =
adamc@403 1082 unifyCons (env, denv) c1 c2
adamc@403 1083 handle CUnify (c1, c2, err) =>
adamc@403 1084 (expError env (PatUnify (p, c1, c2, err));
adamc@86 1085 [])
adamc@71 1086
adamc@403 1087 fun primType env p =
adamc@403 1088 case p of
adamc@403 1089 P.Int _ => !int
adamc@403 1090 | P.Float _ => !float
adamc@403 1091 | P.String _ => !string
adamc@623 1092
adamc@403 1093 datatype constraint =
adamc@403 1094 Disjoint of D.goal
adamc@403 1095 | TypeClass of E.env * L'.con * L'.exp option ref * ErrorMsg.span
adamc@403 1096
adamc@403 1097 val enD = map Disjoint
adamc@403 1098
adamc@403 1099 fun elabHead (env, denv) infer (e as (_, loc)) t =
adamc@403 1100 let
adamc@403 1101 fun unravel (t, e) =
adamc@403 1102 let
adamc@403 1103 val (t, gs) = hnormCon (env, denv) t
adamc@403 1104 in
adamc@403 1105 case t of
adamc@623 1106 (L'.TKFun (x, t'), _) =>
adamc@623 1107 let
adamc@623 1108 val u = kunif loc
adamc@623 1109
adamc@623 1110 val t'' = subKindInCon (0, u) t'
adamc@623 1111 val (e, t, gs') = unravel (t'', (L'.EKApp (e, u), loc))
adamc@623 1112 in
adamc@623 1113 (e, t, enD gs @ gs')
adamc@623 1114 end
adamc@623 1115 | (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@403 1116 let
adamc@403 1117 val u = cunif (loc, k)
adamc@403 1118
adamc@403 1119 val t'' = subConInCon (0, u) t'
adamc@403 1120 val (e, t, gs') = unravel (t'', (L'.ECApp (e, u), loc))
adamc@403 1121 in
adamc@403 1122 (*prefaces "Unravel" [("t'", p_con env t'),
adamc@403 1123 ("t''", p_con env t'')];*)
adamc@403 1124 (e, t, enD gs @ gs')
adamc@403 1125 end
adamc@403 1126 | (L'.TFun (dom as (L'.CApp (cl, _), _), ran), _) =>
adamc@403 1127 let
adamc@403 1128 val cl = #1 (hnormCon (env, denv) cl)
adamc@403 1129 in
adamc@403 1130 if infer <> L.TypesOnly andalso E.isClass env cl then
adamc@403 1131 let
adamc@403 1132 val r = ref NONE
adamc@403 1133 val (e, t, gs') = unravel (ran, (L'.EApp (e, (L'.EUnif r, loc)), loc))
adamc@403 1134 in
adamc@403 1135 (e, t, TypeClass (env, dom, r, loc) :: enD gs @ gs')
adamc@403 1136 end
adamc@403 1137 else
adamc@403 1138 (e, t, enD gs)
adamc@403 1139 end
adamc@228 1140 | _ => (e, t, enD gs)
adamc@86 1141 end
adamc@15 1142 in
adamc@403 1143 case infer of
adamc@403 1144 L.DontInfer => (e, t, [])
adamc@403 1145 | _ => unravel (t, e)
adamc@15 1146 end
adamc@15 1147
adamc@174 1148 fun elabPat (pAll as (p, loc), (env, denv, bound)) =
adamc@171 1149 let
adamc@171 1150 val perror = (L'.PWild, loc)
adamc@171 1151 val terror = (L'.CError, loc)
adamc@171 1152 val pterror = (perror, terror)
adamc@171 1153 val rerror = (pterror, (env, bound))
adamc@171 1154
adamc@191 1155 fun pcon (pc, po, xs, to, dn, dk) =
adamc@188 1156 case (po, to) of
adamc@188 1157 (NONE, SOME _) => (expError env (PatHasNoArg loc);
adamc@188 1158 rerror)
adamc@188 1159 | (SOME _, NONE) => (expError env (PatHasArg loc);
adamc@188 1160 rerror)
adamc@191 1161 | (NONE, NONE) =>
adamc@191 1162 let
adamc@191 1163 val k = (L'.KType, loc)
adamc@191 1164 val unifs = map (fn _ => cunif (loc, k)) xs
adamc@191 1165 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
adamc@191 1166 in
adamc@191 1167 (((L'.PCon (dk, pc, unifs, NONE), loc), dn),
adamc@191 1168 (env, bound))
adamc@191 1169 end
adamc@188 1170 | (SOME p, SOME t) =>
adamc@188 1171 let
adamc@188 1172 val ((p', pt), (env, bound)) = elabPat (p, (env, denv, bound))
adamc@191 1173
adamc@191 1174 val k = (L'.KType, loc)
adamc@191 1175 val unifs = map (fn _ => cunif (loc, k)) xs
adamc@194 1176 val nxs = length unifs - 1
adamc@194 1177 val t = ListUtil.foldli (fn (i, u, t) => subConInCon (nxs - i, u) t) t unifs
adamc@191 1178 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
adamc@188 1179 in
adamc@191 1180 ignore (checkPatCon (env, denv) p' pt t);
adamc@191 1181 (((L'.PCon (dk, pc, unifs, SOME p'), loc), dn),
adamc@188 1182 (env, bound))
adamc@188 1183 end
adamc@171 1184 in
adamc@171 1185 case p of
adamc@171 1186 L.PWild => (((L'.PWild, loc), cunif (loc, (L'.KType, loc))),
adamc@171 1187 (env, bound))
adamc@171 1188 | L.PVar x =>
adamc@171 1189 let
adamc@171 1190 val t = if SS.member (bound, x) then
adamc@171 1191 (expError env (DuplicatePatternVariable (loc, x));
adamc@171 1192 terror)
adamc@171 1193 else
adamc@171 1194 cunif (loc, (L'.KType, loc))
adamc@171 1195 in
adamc@182 1196 (((L'.PVar (x, t), loc), t),
adamc@171 1197 (E.pushERel env x t, SS.add (bound, x)))
adamc@171 1198 end
adamc@173 1199 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
adamc@173 1200 (env, bound))
adamc@171 1201 | L.PCon ([], x, po) =>
adamc@171 1202 (case E.lookupConstructor env x of
adamc@174 1203 NONE => (expError env (UnboundConstructor (loc, [], x));
adamc@171 1204 rerror)
adamc@191 1205 | SOME (dk, n, xs, to, dn) => pcon (L'.PConVar n, po, xs, to, (L'.CNamed dn, loc), dk))
adamc@174 1206 | L.PCon (m1 :: ms, x, po) =>
adamc@174 1207 (case E.lookupStr env m1 of
adamc@174 1208 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@174 1209 rerror)
adamc@174 1210 | SOME (n, sgn) =>
adamc@174 1211 let
adamc@174 1212 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@174 1213 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@182 1214 NONE => raise Fail "elabPat: Unknown substructure"
adamc@174 1215 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@174 1216 ((L'.StrVar n, loc), sgn) ms
adamc@174 1217 in
adamc@174 1218 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 1219 NONE => (expError env (UnboundConstructor (loc, m1 :: ms, x));
adamc@174 1220 rerror)
adamc@191 1221 | SOME (dk, _, xs, to, dn) => pcon (L'.PConProj (n, ms, x), po, xs, to, dn, dk)
adamc@174 1222 end)
adamc@174 1223
adamc@175 1224 | L.PRecord (xps, flex) =>
adamc@175 1225 let
adamc@175 1226 val (xpts, (env, bound, _)) =
adamc@175 1227 ListUtil.foldlMap (fn ((x, p), (env, bound, fbound)) =>
adamc@175 1228 let
adamc@175 1229 val ((p', t), (env, bound)) = elabPat (p, (env, denv, bound))
adamc@175 1230 in
adamc@175 1231 if SS.member (fbound, x) then
adamc@175 1232 expError env (DuplicatePatField (loc, x))
adamc@175 1233 else
adamc@175 1234 ();
adamc@175 1235 ((x, p', t), (env, bound, SS.add (fbound, x)))
adamc@175 1236 end)
adamc@175 1237 (env, bound, SS.empty) xps
adamc@175 1238
adamc@175 1239 val k = (L'.KType, loc)
adamc@175 1240 val c = (L'.CRecord (k, map (fn (x, _, t) => ((L'.CName x, loc), t)) xpts), loc)
adamc@176 1241 val c =
adamc@175 1242 if flex then
adamc@176 1243 (L'.CConcat (c, cunif (loc, (L'.KRecord k, loc))), loc)
adamc@175 1244 else
adamc@176 1245 c
adamc@175 1246 in
adamc@182 1247 (((L'.PRecord xpts, loc),
adamc@175 1248 (L'.TRecord c, loc)),
adamc@175 1249 (env, bound))
adamc@175 1250 end
adamc@175 1251
adamc@171 1252 end
adamc@171 1253
adamc@172 1254 datatype coverage =
adamc@172 1255 Wild
adamc@173 1256 | None
adamc@172 1257 | Datatype of coverage IM.map
adamc@175 1258 | Record of coverage SM.map list
adamc@172 1259
adamc@243 1260 fun c2s c =
adamc@243 1261 case c of
adamc@243 1262 Wild => "Wild"
adamc@243 1263 | None => "None"
adamc@243 1264 | Datatype _ => "Datatype"
adamc@243 1265 | Record _ => "Record"
adamc@243 1266
adamc@172 1267 fun exhaustive (env, denv, t, ps) =
adamc@172 1268 let
adamc@402 1269 fun depth (p, _) =
adamc@402 1270 case p of
adamc@402 1271 L'.PWild => 0
adamc@402 1272 | L'.PVar _ => 0
adamc@402 1273 | L'.PPrim _ => 0
adamc@402 1274 | L'.PCon (_, _, _, NONE) => 1
adamc@402 1275 | L'.PCon (_, _, _, SOME p) => 1 + depth p
adamc@402 1276 | L'.PRecord xps => foldl (fn ((_, p, _), n) => Int.max (depth p, n)) 0 xps
adamc@402 1277
adamc@402 1278 val depth = 1 + foldl (fn (p, n) => Int.max (depth p, n)) 0 ps
adamc@402 1279
adamc@172 1280 fun pcCoverage pc =
adamc@172 1281 case pc of
adamc@172 1282 L'.PConVar n => n
adamc@174 1283 | L'.PConProj (m1, ms, x) =>
adamc@174 1284 let
adamc@174 1285 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@174 1286 in
adamc@174 1287 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 1288 NONE => raise Fail "exhaustive: Can't project constructor"
adamc@191 1289 | SOME (_, n, _, _, _) => n
adamc@174 1290 end
adamc@172 1291
adamc@172 1292 fun coverage (p, _) =
adamc@172 1293 case p of
adamc@172 1294 L'.PWild => Wild
adamc@172 1295 | L'.PVar _ => Wild
adamc@173 1296 | L'.PPrim _ => None
adamc@191 1297 | L'.PCon (_, pc, _, NONE) => Datatype (IM.insert (IM.empty, pcCoverage pc, Wild))
adamc@191 1298 | L'.PCon (_, pc, _, SOME p) => Datatype (IM.insert (IM.empty, pcCoverage pc, coverage p))
adamc@182 1299 | L'.PRecord xps => Record [foldl (fn ((x, p, _), fmap) =>
adamc@182 1300 SM.insert (fmap, x, coverage p)) SM.empty xps]
adamc@176 1301
adamc@172 1302 fun merge (c1, c2) =
adamc@172 1303 case (c1, c2) of
adamc@173 1304 (None, _) => c2
adamc@173 1305 | (_, None) => c1
adamc@173 1306
adamc@173 1307 | (Wild, _) => Wild
adamc@172 1308 | (_, Wild) => Wild
adamc@172 1309
adamc@172 1310 | (Datatype cm1, Datatype cm2) => Datatype (IM.unionWith merge (cm1, cm2))
adamc@172 1311
adamc@175 1312 | (Record fm1, Record fm2) => Record (fm1 @ fm2)
adamc@175 1313
adamc@175 1314 | _ => None
adamc@175 1315
adamc@172 1316 fun combinedCoverage ps =
adamc@172 1317 case ps of
adamc@172 1318 [] => raise Fail "Empty pattern list for coverage checking"
adamc@172 1319 | [p] => coverage p
adamc@172 1320 | p :: ps => merge (coverage p, combinedCoverage ps)
adamc@172 1321
adamc@402 1322 fun enumerateCases depth t =
adamc@402 1323 if depth = 0 then
adamc@402 1324 [Wild]
adamc@402 1325 else
adamc@402 1326 let
adamc@402 1327 fun dtype cons =
adamc@402 1328 ListUtil.mapConcat (fn (_, n, to) =>
adamc@402 1329 case to of
adamc@402 1330 NONE => [Datatype (IM.insert (IM.empty, n, Wild))]
adamc@402 1331 | SOME t => map (fn c => Datatype (IM.insert (IM.empty, n, c)))
adamc@402 1332 (enumerateCases (depth-1) t)) cons
adamc@402 1333 in
adamc@402 1334 case #1 (#1 (hnormCon (env, denv) t)) of
adamc@402 1335 L'.CNamed n =>
adamc@402 1336 (let
adamc@402 1337 val dt = E.lookupDatatype env n
adamc@402 1338 val cons = E.constructors dt
adamc@175 1339 in
adamc@402 1340 dtype cons
adamc@402 1341 end handle E.UnboundNamed _ => [Wild])
adamc@402 1342 | L'.TRecord c =>
adamc@402 1343 (case #1 (#1 (hnormCon (env, denv) c)) of
adamc@402 1344 L'.CRecord (_, xts) =>
adamc@402 1345 let
adamc@402 1346 val xts = map (fn (x, t) => (#1 (hnormCon (env, denv) x), t)) xts
adamc@402 1347
adamc@402 1348 fun exponentiate fs =
adamc@402 1349 case fs of
adamc@402 1350 [] => [SM.empty]
adamc@402 1351 | ((L'.CName x, _), t) :: rest =>
adamc@402 1352 let
adamc@402 1353 val this = enumerateCases depth t
adamc@402 1354 val rest = exponentiate rest
adamc@402 1355 in
adamc@402 1356 ListUtil.mapConcat (fn fmap =>
adamc@402 1357 map (fn c => SM.insert (fmap, x, c)) this) rest
adamc@402 1358 end
adamc@402 1359 | _ => raise Fail "exponentiate: Not CName"
adamc@402 1360 in
adamc@402 1361 if List.exists (fn ((L'.CName _, _), _) => false
adamc@402 1362 | (c, _) => true) xts then
adamc@402 1363 [Wild]
adamc@402 1364 else
adamc@402 1365 map (fn ls => Record [ls]) (exponentiate xts)
adamc@402 1366 end
adamc@402 1367 | _ => [Wild])
adamc@402 1368 | _ => [Wild]
adamc@402 1369 end
adamc@175 1370
adamc@175 1371 fun coverageImp (c1, c2) =
adamc@243 1372 let
adamc@243 1373 val r =
adamc@243 1374 case (c1, c2) of
adamc@243 1375 (Wild, _) => true
adamc@243 1376
adamc@243 1377 | (Datatype cmap1, Datatype cmap2) =>
adamc@243 1378 List.all (fn (n, c2) =>
adamc@243 1379 case IM.find (cmap1, n) of
adamc@243 1380 NONE => false
adamc@243 1381 | SOME c1 => coverageImp (c1, c2)) (IM.listItemsi cmap2)
adamc@243 1382 | (Datatype cmap1, Wild) =>
adamc@243 1383 List.all (fn (n, c1) => coverageImp (c1, Wild)) (IM.listItemsi cmap1)
adamc@243 1384
adamc@243 1385 | (Record fmaps1, Record fmaps2) =>
adamc@243 1386 List.all (fn fmap2 =>
adamc@243 1387 List.exists (fn fmap1 =>
adamc@243 1388 List.all (fn (x, c2) =>
adamc@243 1389 case SM.find (fmap1, x) of
adamc@243 1390 NONE => true
adamc@243 1391 | SOME c1 => coverageImp (c1, c2))
adamc@243 1392 (SM.listItemsi fmap2))
adamc@243 1393 fmaps1) fmaps2
adamc@243 1394
adamc@243 1395 | (Record fmaps1, Wild) =>
adamc@243 1396 List.exists (fn fmap1 =>
adamc@243 1397 List.all (fn (x, c1) => coverageImp (c1, Wild))
adamc@243 1398 (SM.listItemsi fmap1)) fmaps1
adamc@243 1399
adamc@243 1400 | _ => false
adamc@243 1401 in
adamc@243 1402 (*TextIO.print ("coverageImp(" ^ c2s c1 ^ ", " ^ c2s c2 ^ ") = " ^ Bool.toString r ^ "\n");*)
adamc@243 1403 r
adamc@243 1404 end
adamc@175 1405
adamc@172 1406 fun isTotal (c, t) =
adamc@172 1407 case c of
adamc@173 1408 None => (false, [])
adamc@173 1409 | Wild => (true, [])
adamc@172 1410 | Datatype cm =>
adamc@172 1411 let
adamc@172 1412 val ((t, _), gs) = hnormCon (env, denv) t
adamc@174 1413
adamc@174 1414 fun dtype cons =
adamc@174 1415 foldl (fn ((_, n, to), (total, gs)) =>
adamc@174 1416 case IM.find (cm, n) of
adamc@174 1417 NONE => (false, gs)
adamc@174 1418 | SOME c' =>
adamc@174 1419 case to of
adamc@174 1420 NONE => (total, gs)
adamc@174 1421 | SOME t' =>
adamc@174 1422 let
adamc@174 1423 val (total, gs') = isTotal (c', t')
adamc@174 1424 in
adamc@174 1425 (total, gs' @ gs)
adamc@174 1426 end)
adamc@174 1427 (true, gs) cons
adamc@191 1428
adamc@191 1429 fun unapp t =
adamc@191 1430 case t of
adamc@191 1431 L'.CApp ((t, _), _) => unapp t
adamc@191 1432 | _ => t
adamc@172 1433 in
adamc@191 1434 case unapp t of
adamc@172 1435 L'.CNamed n =>
adamc@172 1436 let
adamc@172 1437 val dt = E.lookupDatatype env n
adamc@172 1438 val cons = E.constructors dt
adamc@172 1439 in
adamc@174 1440 dtype cons
adamc@174 1441 end
adamc@174 1442 | L'.CModProj (m1, ms, x) =>
adamc@174 1443 let
adamc@174 1444 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@174 1445 in
adamc@174 1446 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
adamc@174 1447 NONE => raise Fail "isTotal: Can't project datatype"
adamc@191 1448 | SOME (_, cons) => dtype cons
adamc@172 1449 end
adamc@172 1450 | L'.CError => (true, gs)
adamc@341 1451 | c =>
adamc@341 1452 (prefaces "Not a datatype" [("c", p_con env (c, ErrorMsg.dummySpan))];
adamc@341 1453 raise Fail "isTotal: Not a datatype")
adamc@172 1454 end
adamc@402 1455 | Record _ => (List.all (fn c2 => coverageImp (c, c2)) (enumerateCases depth t), [])
adamc@172 1456 in
adamc@172 1457 isTotal (combinedCoverage ps, t)
adamc@172 1458 end
adamc@172 1459
adamc@217 1460 fun unmodCon env (c, loc) =
adamc@217 1461 case c of
adamc@217 1462 L'.CNamed n =>
adamc@217 1463 (case E.lookupCNamed env n of
adamc@217 1464 (_, _, SOME (c as (L'.CModProj _, _))) => unmodCon env c
adamc@217 1465 | _ => (c, loc))
adamc@217 1466 | L'.CModProj (m1, ms, x) =>
adamc@217 1467 let
adamc@217 1468 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@217 1469 in
adamc@217 1470 case E.projectCon env {str = str, sgn = sgn, field = x} of
adamc@217 1471 NONE => raise Fail "unmodCon: Can't projectCon"
adamc@217 1472 | SOME (_, SOME (c as (L'.CModProj _, _))) => unmodCon env c
adamc@217 1473 | _ => (c, loc)
adamc@217 1474 end
adamc@217 1475 | _ => (c, loc)
adamc@217 1476
adamc@467 1477 fun normClassKey envs c =
adamc@467 1478 let
adamc@467 1479 val c = ElabOps.hnormCon envs c
adamc@467 1480 in
adamc@467 1481 case #1 c of
adamc@467 1482 L'.CApp (c1, c2) =>
adamc@467 1483 let
adamc@467 1484 val c1 = normClassKey envs c1
adamc@467 1485 val c2 = normClassKey envs c2
adamc@467 1486 in
adamc@467 1487 (L'.CApp (c1, c2), #2 c)
adamc@467 1488 end
adamc@467 1489 | _ => c
adamc@467 1490 end
adamc@467 1491
adamc@467 1492 fun normClassConstraint env (c, loc) =
adamc@217 1493 case c of
adamc@217 1494 L'.CApp (f, x) =>
adamc@217 1495 let
adamc@467 1496 val f = unmodCon env f
adamc@467 1497 val x = normClassKey env x
adamc@217 1498 in
adamc@467 1499 (L'.CApp (f, x), loc)
adamc@217 1500 end
adamc@467 1501 | L'.CUnif (_, _, _, ref (SOME c)) => normClassConstraint env c
adamc@467 1502 | _ => (c, loc)
adamc@216 1503
adamc@447 1504
adamc@447 1505 val makeInstantiable =
adamc@447 1506 let
adamc@447 1507 fun kind k = k
adamc@447 1508 fun con c =
adamc@447 1509 case c of
adamc@447 1510 L'.CDisjoint (L'.LeaveAlone, c1, c2, c) => L'.CDisjoint (L'.Instantiate, c1, c2, c)
adamc@447 1511 | _ => c
adamc@447 1512 in
adamc@447 1513 U.Con.map {kind = kind, con = con}
adamc@447 1514 end
adamc@447 1515
adamc@92 1516 fun elabExp (env, denv) (eAll as (e, loc)) =
adamc@91 1517 let
adamc@280 1518 (*val () = eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*)
adamc@514 1519 (*val befor = Time.now ()*)
adamc@280 1520
adamc@280 1521 val r = case e of
adamc@91 1522 L.EAnnot (e, t) =>
adamc@91 1523 let
adamc@91 1524 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1525 val (t', _, gs2) = elabCon (env, denv) t
adamc@91 1526 val gs3 = checkCon (env, denv) e' et t'
adamc@91 1527 in
adamc@228 1528 (e', t', gs1 @ enD gs2 @ enD gs3)
adamc@91 1529 end
adamc@34 1530
adamc@91 1531 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@403 1532 | L.EVar ([], s, infer) =>
adamc@91 1533 (case E.lookupE env s of
adamc@91 1534 E.NotBound =>
adamc@91 1535 (expError env (UnboundExp (loc, s));
adamc@91 1536 (eerror, cerror, []))
adamc@403 1537 | E.Rel (n, t) => elabHead (env, denv) infer (L'.ERel n, loc) t
adamc@403 1538 | E.Named (n, t) => elabHead (env, denv) infer (L'.ENamed n, loc) t)
adamc@403 1539 | L.EVar (m1 :: ms, s, infer) =>
adamc@91 1540 (case E.lookupStr env m1 of
adamc@91 1541 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@91 1542 (eerror, cerror, []))
adamc@91 1543 | SOME (n, sgn) =>
adamc@91 1544 let
adamc@91 1545 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@91 1546 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@91 1547 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@91 1548 (strerror, sgnerror))
adamc@91 1549 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@91 1550 ((L'.StrVar n, loc), sgn) ms
adamc@10 1551
adamc@91 1552 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@91 1553 NONE => (expError env (UnboundExp (loc, s));
adamc@91 1554 cerror)
adamc@91 1555 | SOME t => t
adamc@91 1556 in
adamc@403 1557 elabHead (env, denv) infer (L'.EModProj (n, ms, s), loc) t
adamc@91 1558 end)
adamc@86 1559
adamc@349 1560 | L.EWild =>
adamc@210 1561 let
adamc@349 1562 val r = ref NONE
adamc@349 1563 val c = cunif (loc, (L'.KType, loc))
adamc@210 1564 in
adamc@349 1565 ((L'.EUnif r, loc), c, [TypeClass (env, c, r, loc)])
adamc@210 1566 end
adamc@210 1567
adamc@91 1568 | L.EApp (e1, e2) =>
adamc@91 1569 let
adamc@91 1570 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@403 1571 val (e2', t2, gs2) = elabExp (env, denv) e2
adamc@71 1572
adamc@91 1573 val dom = cunif (loc, ktype)
adamc@91 1574 val ran = cunif (loc, ktype)
adamc@91 1575 val t = (L'.TFun (dom, ran), dummy)
adamc@91 1576
adamc@403 1577 val gs3 = checkCon (env, denv) e1' t1 t
adamc@403 1578 val gs4 = checkCon (env, denv) e2' t2 dom
adamc@403 1579
adamc@403 1580 val gs = gs1 @ gs2 @ enD gs3 @ enD gs4
adamc@91 1581 in
adamc@205 1582 ((L'.EApp (e1', e2'), loc), ran, gs)
adamc@91 1583 end
adamc@91 1584 | L.EAbs (x, to, e) =>
adamc@91 1585 let
adamc@91 1586 val (t', gs1) = case to of
adamc@91 1587 NONE => (cunif (loc, ktype), [])
adamc@91 1588 | SOME t =>
adamc@91 1589 let
adamc@91 1590 val (t', tk, gs) = elabCon (env, denv) t
adamc@91 1591 in
adamc@91 1592 checkKind env t' tk ktype;
adamc@91 1593 (t', gs)
adamc@91 1594 end
adamc@467 1595 val dom = normClassConstraint env t'
adamc@467 1596 val (e', et, gs2) = elabExp (E.pushERel env x dom, denv) e
adamc@91 1597 in
adamc@91 1598 ((L'.EAbs (x, t', et, e'), loc),
adamc@91 1599 (L'.TFun (t', et), loc),
adamc@467 1600 enD gs1 @ gs2)
adamc@91 1601 end
adamc@91 1602 | L.ECApp (e, c) =>
adamc@91 1603 let
adamc@91 1604 val (e', et, gs1) = elabExp (env, denv) e
adamc@328 1605 val oldEt = et
adamc@403 1606 val (c', ck, gs2) = elabCon (env, denv) c
adamc@403 1607 val ((et', _), gs3) = hnormCon (env, denv) et
adamc@91 1608 in
adamc@91 1609 case et' of
adamc@91 1610 L'.CError => (eerror, cerror, [])
adamc@328 1611 | L'.TCFun (_, x, k, eb) =>
adamc@91 1612 let
adamc@91 1613 val () = checkKind env c' ck k
adamc@91 1614 val eb' = subConInCon (0, c') eb
adamc@339 1615 handle SynUnif => (expError env (Unif ("substitution", loc, eb));
adamc@91 1616 cerror)
adamc@91 1617 in
adamc@328 1618 (*prefaces "Elab ECApp" [("e", SourcePrint.p_exp eAll),
adamc@328 1619 ("et", p_con env oldEt),
adamc@328 1620 ("x", PD.string x),
adamc@328 1621 ("eb", p_con (E.pushCRel env x k) eb),
adamc@328 1622 ("c", p_con env c'),
adamc@328 1623 ("eb'", p_con env eb')];*)
adamc@403 1624 ((L'.ECApp (e', c'), loc), eb', gs1 @ enD gs2 @ enD gs3)
adamc@91 1625 end
adamc@91 1626
adamc@91 1627 | _ =>
adamc@91 1628 (expError env (WrongForm ("constructor function", e', et));
adamc@91 1629 (eerror, cerror, []))
adamc@91 1630 end
adamc@91 1631 | L.ECAbs (expl, x, k, e) =>
adamc@91 1632 let
adamc@91 1633 val expl' = elabExplicitness expl
adamc@623 1634 val k' = elabKind env k
adamc@328 1635
adamc@328 1636 val env' = E.pushCRel env x k'
adamc@328 1637 val (e', et, gs) = elabExp (env', D.enter denv) e
adamc@91 1638 in
adamc@91 1639 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@91 1640 (L'.TCFun (expl', x, k', et), loc),
adamc@91 1641 gs)
adamc@91 1642 end
adamc@623 1643 | L.EKAbs (x, e) =>
adamc@623 1644 let
adamc@623 1645 val env' = E.pushKRel env x
adamc@623 1646 val (e', et, gs) = elabExp (env', denv) e
adamc@623 1647 in
adamc@623 1648 ((L'.EKAbs (x, e'), loc),
adamc@623 1649 (L'.TKFun (x, et), loc),
adamc@623 1650 gs)
adamc@623 1651 end
adamc@91 1652
adamc@91 1653 | L.EDisjoint (c1, c2, e) =>
adamc@91 1654 let
adamc@91 1655 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@91 1656 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@91 1657
adamc@91 1658 val ku1 = kunif loc
adamc@91 1659 val ku2 = kunif loc
adamc@91 1660
adamc@91 1661 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@91 1662 val (e', t, gs4) = elabExp (env, denv') e
adamc@91 1663 in
adamc@91 1664 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@91 1665 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@91 1666
adamc@345 1667 (e', (L'.CDisjoint (L'.LeaveAlone, c1', c2', t), loc), enD gs1 @ enD gs2 @ enD gs3 @ gs4)
adamc@91 1668 end
adamc@91 1669
adamc@91 1670 | L.ERecord xes =>
adamc@91 1671 let
adamc@91 1672 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@91 1673 let
adamc@91 1674 val (x', xk, gs1) = elabCon (env, denv) x
adamc@91 1675 val (e', et, gs2) = elabExp (env, denv) e
adamc@91 1676 in
adamc@91 1677 checkKind env x' xk kname;
adamc@228 1678 ((x', e', et), enD gs1 @ gs2 @ gs)
adamc@91 1679 end)
adamc@91 1680 [] xes
adamc@91 1681
adamc@91 1682 val k = (L'.KType, loc)
adamc@91 1683
adamc@91 1684 fun prove (xets, gs) =
adamc@91 1685 case xets of
adamc@91 1686 [] => gs
adamc@91 1687 | (x, _, t) :: rest =>
adamc@91 1688 let
adamc@91 1689 val xc = (x, t)
adamc@91 1690 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@91 1691 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@91 1692 let
adamc@91 1693 val xc' = (x', t')
adamc@91 1694 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@91 1695 in
adamc@91 1696 D.prove env denv (r1, r2, loc) @ gs
adamc@91 1697 end)
adamc@91 1698 gs rest
adamc@91 1699 in
adamc@91 1700 prove (rest, gs)
adamc@91 1701 end
adamc@228 1702
adamc@228 1703 val gsD = List.mapPartial (fn Disjoint d => SOME d | _ => NONE) gs
adamc@228 1704 val gsO = List.filter (fn Disjoint _ => false | _ => true) gs
adamc@91 1705 in
adamc@255 1706 (*TextIO.print ("|gsO| = " ^ Int.toString (length gsO) ^ "\n");*)
adamc@91 1707 ((L'.ERecord xes', loc),
adamc@91 1708 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@228 1709 enD (prove (xes', gsD)) @ gsO)
adamc@91 1710 end
adamc@91 1711
adamc@91 1712 | L.EField (e, c) =>
adamc@91 1713 let
adamc@91 1714 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1715 val (c', ck, gs2) = elabCon (env, denv) c
adamc@91 1716
adamc@91 1717 val ft = cunif (loc, ktype)
adamc@91 1718 val rest = cunif (loc, ktype_record)
adamc@91 1719 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@91 1720
adamc@91 1721 val gs3 =
adamc@91 1722 checkCon (env, denv) e' et
adamc@91 1723 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@91 1724 val gs4 = D.prove env denv (first, rest, loc)
adamc@91 1725 in
adamc@228 1726 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ enD gs2 @ enD gs3 @ enD gs4)
adamc@91 1727 end
adamc@91 1728
adamc@445 1729 | L.EConcat (e1, e2) =>
adamc@339 1730 let
adamc@339 1731 val (e1', e1t, gs1) = elabExp (env, denv) e1
adamc@445 1732 val (e2', e2t, gs2) = elabExp (env, denv) e2
adamc@445 1733
adamc@445 1734 val r1 = cunif (loc, ktype_record)
adamc@445 1735 val r2 = cunif (loc, ktype_record)
adamc@445 1736
adamc@445 1737 val gs3 = checkCon (env, denv) e1' e1t (L'.TRecord r1, loc)
adamc@445 1738 val gs4 = checkCon (env, denv) e2' e2t (L'.TRecord r2, loc)
adamc@445 1739 val gs5 = D.prove env denv (r1, r2, loc)
adamc@339 1740 in
adamc@445 1741 ((L'.EConcat (e1', r1, e2', r2), loc),
adamc@445 1742 (L'.TRecord ((L'.CConcat (r1, r2), loc)), loc),
adamc@445 1743 gs1 @ gs2 @ enD gs3 @ enD gs4 @ enD gs5)
adamc@339 1744 end
adamc@149 1745 | L.ECut (e, c) =>
adamc@149 1746 let
adamc@149 1747 val (e', et, gs1) = elabExp (env, denv) e
adamc@149 1748 val (c', ck, gs2) = elabCon (env, denv) c
adamc@149 1749
adamc@149 1750 val ft = cunif (loc, ktype)
adamc@149 1751 val rest = cunif (loc, ktype_record)
adamc@149 1752 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@149 1753
adamc@149 1754 val gs3 =
adamc@149 1755 checkCon (env, denv) e' et
adamc@149 1756 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@149 1757 val gs4 = D.prove env denv (first, rest, loc)
adamc@149 1758 in
adamc@228 1759 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc),
adamc@228 1760 gs1 @ enD gs2 @ enD gs3 @ enD gs4)
adamc@149 1761 end
adamc@493 1762 | L.ECutMulti (e, c) =>
adamc@493 1763 let
adamc@493 1764 val (e', et, gs1) = elabExp (env, denv) e
adamc@493 1765 val (c', ck, gs2) = elabCon (env, denv) c
adamc@493 1766
adamc@493 1767 val rest = cunif (loc, ktype_record)
adamc@493 1768
adamc@493 1769 val gs3 =
adamc@493 1770 checkCon (env, denv) e' et
adamc@493 1771 (L'.TRecord (L'.CConcat (c', rest), loc), loc)
adamc@493 1772 val gs4 = D.prove env denv (c', rest, loc)
adamc@493 1773 in
adamc@493 1774 ((L'.ECutMulti (e', c', {rest = rest}), loc), (L'.TRecord rest, loc),
adamc@493 1775 gs1 @ enD gs2 @ enD gs3 @ enD gs4)
adamc@493 1776 end
adamc@149 1777
adamc@171 1778 | L.ECase (e, pes) =>
adamc@171 1779 let
adamc@171 1780 val (e', et, gs1) = elabExp (env, denv) e
adamc@171 1781 val result = cunif (loc, (L'.KType, loc))
adamc@171 1782 val (pes', gs) = ListUtil.foldlMap
adamc@171 1783 (fn ((p, e), gs) =>
adamc@171 1784 let
adamc@174 1785 val ((p', pt), (env, _)) = elabPat (p, (env, denv, SS.empty))
adamc@171 1786
adamc@171 1787 val gs1 = checkPatCon (env, denv) p' pt et
adamc@171 1788 val (e', et, gs2) = elabExp (env, denv) e
adamc@171 1789 val gs3 = checkCon (env, denv) e' et result
adamc@171 1790 in
adamc@228 1791 ((p', e'), enD gs1 @ gs2 @ enD gs3 @ gs)
adamc@171 1792 end)
adamc@171 1793 gs1 pes
adamc@172 1794
adamc@172 1795 val (total, gs') = exhaustive (env, denv, et, map #1 pes')
adamc@171 1796 in
adamc@172 1797 if total then
adamc@172 1798 ()
adamc@172 1799 else
adamc@172 1800 expError env (Inexhaustive loc);
adamc@172 1801
adamc@228 1802 ((L'.ECase (e', pes', {disc = et, result = result}), loc), result, enD gs' @ gs)
adamc@171 1803 end
adamc@447 1804
adamc@447 1805 | L.ELet (eds, e) =>
adamc@447 1806 let
adamc@447 1807 val (eds, (env, gs1)) = ListUtil.foldlMap (elabEdecl denv) (env, []) eds
adamc@447 1808 val (e, t, gs2) = elabExp (env, denv) e
adamc@447 1809 in
adamc@447 1810 ((L'.ELet (eds, e), loc), t, gs1 @ gs2)
adamc@447 1811 end
adamc@280 1812 in
adamc@513 1813 (*prefaces "elabExp" [("e", SourcePrint.p_exp eAll),
adamc@514 1814 ("t", PD.string (LargeReal.toString (Time.toReal (Time.- (Time.now (), befor)))))];*)
adamc@280 1815 r
adamc@91 1816 end
adamc@31 1817
adamc@447 1818 and elabEdecl denv (dAll as (d, loc), (env, gs : constraint list)) =
adamc@447 1819 let
adamc@447 1820 val r =
adamc@447 1821 case d of
adamc@447 1822 L.EDVal (x, co, e) =>
adamc@447 1823 let
adamc@447 1824 val (c', _, gs1) = case co of
adamc@447 1825 NONE => (cunif (loc, ktype), ktype, [])
adamc@447 1826 | SOME c => elabCon (env, denv) c
adamc@447 1827
adamc@447 1828 val (e', et, gs2) = elabExp (env, denv) e
adamc@447 1829 val gs3 = checkCon (env, denv) e' et c'
adamc@467 1830 val c' = normClassConstraint env c'
adamc@447 1831 val env' = E.pushERel env x c'
adamc@447 1832 val c' = makeInstantiable c'
adamc@447 1833 in
adamc@467 1834 ((L'.EDVal (x, c', e'), loc), (env', enD gs1 @ gs2 @ enD gs3 @ gs))
adamc@447 1835 end
adamc@447 1836 | L.EDValRec vis =>
adamc@447 1837 let
adamc@447 1838 fun allowable (e, _) =
adamc@447 1839 case e of
adamc@447 1840 L.EAbs _ => true
adamc@447 1841 | L.ECAbs (_, _, _, e) => allowable e
adamc@623 1842 | L.EKAbs (_, e) => allowable e
adamc@447 1843 | L.EDisjoint (_, _, e) => allowable e
adamc@447 1844 | _ => false
adamc@447 1845
adamc@447 1846 val (vis, gs) = ListUtil.foldlMap
adamc@447 1847 (fn ((x, co, e), gs) =>
adamc@447 1848 let
adamc@447 1849 val (c', _, gs1) = case co of
adamc@447 1850 NONE => (cunif (loc, ktype), ktype, [])
adamc@447 1851 | SOME c => elabCon (env, denv) c
adamc@447 1852 in
adamc@447 1853 ((x, c', e), enD gs1 @ gs)
adamc@447 1854 end) gs vis
adamc@447 1855
adamc@447 1856 val env = foldl (fn ((x, c', _), env) => E.pushERel env x c') env vis
adamc@447 1857
adamc@447 1858 val (vis, gs) = ListUtil.foldlMap (fn ((x, c', e), gs) =>
adamc@447 1859 let
adamc@447 1860 val (e', et, gs1) = elabExp (env, denv) e
adamc@447 1861
adamc@447 1862 val gs2 = checkCon (env, denv) e' et c'
adamc@447 1863
adamc@447 1864 val c' = makeInstantiable c'
adamc@447 1865 in
adamc@447 1866 if allowable e then
adamc@447 1867 ()
adamc@447 1868 else
adamc@447 1869 expError env (IllegalRec (x, e'));
adamc@447 1870 ((x, c', e'), gs1 @ enD gs2 @ gs)
adamc@447 1871 end) gs vis
adamc@447 1872 in
adamc@447 1873 ((L'.EDValRec vis, loc), (env, gs))
adamc@447 1874 end
adamc@447 1875 in
adamc@447 1876 r
adamc@447 1877 end
adamc@447 1878
adamc@42 1879 val hnormSgn = E.hnormSgn
adamc@31 1880
adamc@210 1881 fun tableOf () = (L'.CModProj (!basis_r, [], "sql_table"), ErrorMsg.dummySpan)
adamc@338 1882 fun sequenceOf () = (L'.CModProj (!basis_r, [], "sql_sequence"), ErrorMsg.dummySpan)
adamc@459 1883 fun cookieOf () = (L'.CModProj (!basis_r, [], "http_cookie"), ErrorMsg.dummySpan)
adamc@205 1884
adamc@469 1885 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@469 1886 case E.lookupStr env str of
adamc@469 1887 NONE => (strError env (UnboundStr (loc, str));
adamc@469 1888 denv)
adamc@469 1889 | SOME (n, sgn) =>
adamc@469 1890 let
adamc@469 1891 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@469 1892 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@469 1893 NONE => (strError env (UnboundStr (loc, m));
adamc@469 1894 (strerror, sgnerror))
adamc@469 1895 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@469 1896 ((L'.StrVar n, loc), sgn) strs
adamc@469 1897
adamc@469 1898 val cso = E.projectConstraints env {sgn = sgn, str = st}
adamc@469 1899
adamc@469 1900 val denv = case cso of
adamc@469 1901 NONE => (strError env (UnboundStr (loc, str));
adamc@469 1902 denv)
adamc@469 1903 | SOME cs => foldl (fn ((c1, c2), denv) =>
adamc@469 1904 let
adamc@469 1905 val (denv, gs) = D.assert env denv (c1, c2)
adamc@469 1906 in
adamc@469 1907 case gs of
adamc@469 1908 [] => ()
adamc@469 1909 | _ => raise Fail "dopenConstraints: Sub-constraints remain";
adamc@469 1910
adamc@469 1911 denv
adamc@469 1912 end) denv cs
adamc@469 1913 in
adamc@469 1914 denv
adamc@469 1915 end
adamc@469 1916
adamc@88 1917 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@76 1918 case sgi of
adamc@76 1919 L.SgiConAbs (x, k) =>
adamc@76 1920 let
adamc@623 1921 val k' = elabKind env k
adamc@31 1922
adamc@76 1923 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1924 in
adamc@88 1925 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@76 1926 end
adamc@31 1927
adamc@76 1928 | L.SgiCon (x, ko, c) =>
adamc@76 1929 let
adamc@76 1930 val k' = case ko of
adamc@76 1931 NONE => kunif loc
adamc@623 1932 | SOME k => elabKind env k
adamc@31 1933
adamc@83 1934 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1935 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1936 in
adamc@76 1937 checkKind env c' ck k';
adamc@31 1938
adamc@88 1939 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1940 end
adamc@31 1941
adamc@191 1942 | L.SgiDatatype (x, xs, xcs) =>
adamc@157 1943 let
adamc@157 1944 val k = (L'.KType, loc)
adamc@191 1945 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 1946 val (env, n) = E.pushCNamed env x k' NONE
adamc@157 1947 val t = (L'.CNamed n, loc)
adamc@194 1948 val nxs = length xs - 1
adamc@194 1949 val t = ListUtil.foldli (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@157 1950
adamc@284 1951 val (env', denv') = foldl (fn (x, (env', denv')) =>
adamc@284 1952 (E.pushCRel env' x k,
adamc@284 1953 D.enter denv')) (env, denv) xs
adamc@284 1954
adamc@157 1955 val (xcs, (used, env, gs)) =
adamc@157 1956 ListUtil.foldlMap
adamc@157 1957 (fn ((x, to), (used, env, gs)) =>
adamc@157 1958 let
adamc@157 1959 val (to, t, gs') = case to of
adamc@157 1960 NONE => (NONE, t, gs)
adamc@157 1961 | SOME t' =>
adamc@157 1962 let
adamc@284 1963 val (t', tk, gs') = elabCon (env', denv') t'
adamc@157 1964 in
adamc@284 1965 checkKind env' t' tk k;
adamc@157 1966 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@157 1967 end
adamc@191 1968 val t = foldl (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
adamc@157 1969
adamc@157 1970 val (env, n') = E.pushENamed env x t
adamc@157 1971 in
adamc@157 1972 if SS.member (used, x) then
adamc@157 1973 strError env (DuplicateConstructor (x, loc))
adamc@157 1974 else
adamc@157 1975 ();
adamc@157 1976 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@157 1977 end)
adamc@157 1978 (SS.empty, env, []) xcs
adamc@191 1979
adamc@191 1980 val env = E.pushDatatype env n xs xcs
adamc@157 1981 in
adamc@191 1982 ([(L'.SgiDatatype (x, n, xs, xcs), loc)], (env, denv, gs))
adamc@157 1983 end
adamc@156 1984
adamc@158 1985 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
adamc@158 1986
adamc@158 1987 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
adamc@158 1988 (case E.lookupStr env m1 of
adamc@158 1989 NONE => (strError env (UnboundStr (loc, m1));
adamc@158 1990 ([], (env, denv, gs)))
adamc@158 1991 | SOME (n, sgn) =>
adamc@158 1992 let
adamc@158 1993 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@158 1994 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1995 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@158 1996 (strerror, sgnerror))
adamc@158 1997 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@158 1998 ((L'.StrVar n, loc), sgn) ms
adamc@158 1999 in
adamc@161 2000 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@161 2001 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@161 2002 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@161 2003 NONE => (conError env (UnboundDatatype (loc, s));
adamc@161 2004 ([], (env, denv, gs)))
adamc@191 2005 | SOME (xs, xncs) =>
adamc@161 2006 let
adamc@161 2007 val k = (L'.KType, loc)
adamc@191 2008 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2009
adamc@161 2010 val t = (L'.CModProj (n, ms, s), loc)
adamc@191 2011 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@191 2012 val env = E.pushDatatype env n' xs xncs
adamc@158 2013
adamc@161 2014 val t = (L'.CNamed n', loc)
adamc@161 2015 val env = foldl (fn ((x, n, to), env) =>
adamc@161 2016 let
adamc@161 2017 val t = case to of
adamc@161 2018 NONE => t
adamc@161 2019 | SOME t' => (L'.TFun (t', t), loc)
adamc@191 2020
adamc@191 2021 val t = foldr (fn (x, t) =>
adamc@191 2022 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@191 2023 t xs
adamc@161 2024 in
adamc@161 2025 E.pushENamedAs env x n t
adamc@161 2026 end) env xncs
adamc@161 2027 in
adamc@191 2028 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, gs))
adamc@161 2029 end)
adamc@161 2030 | _ => (strError env (NotDatatype loc);
adamc@161 2031 ([], (env, denv, [])))
adamc@158 2032 end)
adamc@156 2033
adamc@76 2034 | L.SgiVal (x, c) =>
adamc@76 2035 let
adamc@83 2036 val (c', ck, gs') = elabCon (env, denv) c
adamc@31 2037
adamc@76 2038 val (env', n) = E.pushENamed env x c'
adamc@467 2039 val c' = normClassConstraint env c'
adamc@76 2040 in
adamc@623 2041 (unifyKinds env ck ktype
adamc@76 2042 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 2043
adamc@467 2044 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@76 2045 end
adamc@31 2046
adamc@76 2047 | L.SgiStr (x, sgn) =>
adamc@76 2048 let
adamc@83 2049 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2050 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2051 in
adamc@88 2052 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2053 end
adamc@31 2054
adamc@76 2055 | L.SgiSgn (x, sgn) =>
adamc@76 2056 let
adamc@83 2057 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2058 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 2059 in
adamc@88 2060 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2061 end
adamc@31 2062
adamc@76 2063 | L.SgiInclude sgn =>
adamc@76 2064 let
adamc@83 2065 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2066 in
adamc@76 2067 case #1 (hnormSgn env sgn') of
adamc@76 2068 L'.SgnConst sgis =>
adamc@88 2069 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@76 2070 | _ => (sgnError env (NotIncludable sgn');
adamc@88 2071 ([], (env, denv, [])))
adamc@88 2072 end
adamc@88 2073
adamc@88 2074 | L.SgiConstraint (c1, c2) =>
adamc@88 2075 let
adamc@88 2076 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 2077 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@88 2078
adamc@90 2079 val (denv, gs3) = D.assert env denv (c1', c2')
adamc@88 2080 in
adamc@88 2081 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 2082 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 2083
adamc@90 2084 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3))
adamc@76 2085 end
adamc@31 2086
adamc@563 2087 | L.SgiClassAbs (x, k) =>
adamc@211 2088 let
adamc@623 2089 val k = elabKind env k
adamc@563 2090 val k' = (L'.KArrow (k, (L'.KType, loc)), loc)
adamc@563 2091 val (env, n) = E.pushCNamed env x k' NONE
adamc@211 2092 val env = E.pushClass env n
adamc@211 2093 in
adamc@563 2094 ([(L'.SgiClassAbs (x, n, k), loc)], (env, denv, []))
adamc@211 2095 end
adamc@211 2096
adamc@563 2097 | L.SgiClass (x, k, c) =>
adamc@211 2098 let
adamc@623 2099 val k = elabKind env k
adamc@563 2100 val k' = (L'.KArrow (k, (L'.KType, loc)), loc)
adamc@211 2101 val (c', ck, gs) = elabCon (env, denv) c
adamc@563 2102 val (env, n) = E.pushCNamed env x k' (SOME c')
adamc@211 2103 val env = E.pushClass env n
adamc@211 2104 in
adamc@563 2105 checkKind env c' ck k';
adamc@563 2106 ([(L'.SgiClass (x, n, k, c'), loc)], (env, denv, []))
adamc@211 2107 end
adamc@211 2108
adamc@83 2109 and elabSgn (env, denv) (sgn, loc) =
adamc@31 2110 case sgn of
adamc@31 2111 L.SgnConst sgis =>
adamc@31 2112 let
adamc@88 2113 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 2114
adamc@62 2115 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 2116 case sgi of
adamc@62 2117 L'.SgiConAbs (x, _, _) =>
adamc@62 2118 (if SS.member (cons, x) then
adamc@62 2119 sgnError env (DuplicateCon (loc, x))
adamc@62 2120 else
adamc@62 2121 ();
adamc@62 2122 (SS.add (cons, x), vals, sgns, strs))
adamc@62 2123 | L'.SgiCon (x, _, _, _) =>
adamc@62 2124 (if SS.member (cons, x) then
adamc@62 2125 sgnError env (DuplicateCon (loc, x))
adamc@62 2126 else
adamc@62 2127 ();
adamc@62 2128 (SS.add (cons, x), vals, sgns, strs))
adamc@191 2129 | L'.SgiDatatype (x, _, _, xncs) =>
adamc@156 2130 let
adamc@156 2131 val vals = foldl (fn ((x, _, _), vals) =>
adamc@156 2132 (if SS.member (vals, x) then
adamc@156 2133 sgnError env (DuplicateVal (loc, x))
adamc@156 2134 else
adamc@156 2135 ();
adamc@156 2136 SS.add (vals, x)))
adamc@156 2137 vals xncs
adamc@156 2138 in
adamc@156 2139 if SS.member (cons, x) then
adamc@156 2140 sgnError env (DuplicateCon (loc, x))
adamc@156 2141 else
adamc@156 2142 ();
adamc@156 2143 (SS.add (cons, x), vals, sgns, strs)
adamc@156 2144 end
adamc@191 2145 | L'.SgiDatatypeImp (x, _, _, _, _, _, _) =>
adamc@156 2146 (if SS.member (cons, x) then
adamc@156 2147 sgnError env (DuplicateCon (loc, x))
adamc@156 2148 else
adamc@156 2149 ();
adamc@156 2150 (SS.add (cons, x), vals, sgns, strs))
adamc@62 2151 | L'.SgiVal (x, _, _) =>
adamc@62 2152 (if SS.member (vals, x) then
adamc@62 2153 sgnError env (DuplicateVal (loc, x))
adamc@62 2154 else
adamc@62 2155 ();
adamc@62 2156 (cons, SS.add (vals, x), sgns, strs))
adamc@62 2157 | L'.SgiSgn (x, _, _) =>
adamc@62 2158 (if SS.member (sgns, x) then
adamc@62 2159 sgnError env (DuplicateSgn (loc, x))
adamc@62 2160 else
adamc@62 2161 ();
adamc@62 2162 (cons, vals, SS.add (sgns, x), strs))
adamc@62 2163 | L'.SgiStr (x, _, _) =>
adamc@62 2164 (if SS.member (strs, x) then
adamc@62 2165 sgnError env (DuplicateStr (loc, x))
adamc@62 2166 else
adamc@62 2167 ();
adamc@88 2168 (cons, vals, sgns, SS.add (strs, x)))
adamc@203 2169 | L'.SgiConstraint _ => (cons, vals, sgns, strs)
adamc@563 2170 | L'.SgiClassAbs (x, _, _) =>
adamc@211 2171 (if SS.member (cons, x) then
adamc@211 2172 sgnError env (DuplicateCon (loc, x))
adamc@211 2173 else
adamc@211 2174 ();
adamc@211 2175 (SS.add (cons, x), vals, sgns, strs))
adamc@563 2176 | L'.SgiClass (x, _, _, _) =>
adamc@211 2177 (if SS.member (cons, x) then
adamc@211 2178 sgnError env (DuplicateCon (loc, x))
adamc@211 2179 else
adamc@211 2180 ();
adamc@460 2181 (SS.add (cons, x), vals, sgns, strs)))
adamc@62 2182 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 2183 in
adamc@83 2184 ((L'.SgnConst sgis', loc), gs)
adamc@31 2185 end
adamc@31 2186 | L.SgnVar x =>
adamc@31 2187 (case E.lookupSgn env x of
adamc@31 2188 NONE =>
adamc@31 2189 (sgnError env (UnboundSgn (loc, x));
adamc@83 2190 ((L'.SgnError, loc), []))
adamc@83 2191 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 2192 | L.SgnFun (m, dom, ran) =>
adamc@41 2193 let
adamc@83 2194 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2195 val (env', n) = E.pushStrNamed env m dom'
adamc@469 2196 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = []}
adamc@469 2197 val (ran', gs2) = elabSgn (env', denv') ran
adamc@41 2198 in
adamc@83 2199 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 2200 end
adamc@42 2201 | L.SgnWhere (sgn, x, c) =>
adamc@42 2202 let
adamc@83 2203 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 2204 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 2205 in
adamc@42 2206 case #1 (hnormSgn env sgn') of
adamc@83 2207 L'.SgnError => (sgnerror, [])
adamc@42 2208 | L'.SgnConst sgis =>
adamc@75 2209 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 2210 x' = x andalso
adamc@623 2211 (unifyKinds env k ck
adamc@75 2212 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 2213 true)
adamc@42 2214 | _ => false) sgis then
adamc@83 2215 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 2216 else
adamc@42 2217 (sgnError env (UnWhereable (sgn', x));
adamc@83 2218 (sgnerror, []))
adamc@42 2219 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 2220 (sgnerror, []))
adamc@42 2221 end
adamc@59 2222 | L.SgnProj (m, ms, x) =>
adamc@59 2223 (case E.lookupStr env m of
adamc@59 2224 NONE => (strError env (UnboundStr (loc, m));
adamc@83 2225 (sgnerror, []))
adamc@59 2226 | SOME (n, sgn) =>
adamc@59 2227 let
adamc@59 2228 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 2229 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 2230 NONE => (strError env (UnboundStr (loc, m));
adamc@59 2231 (strerror, sgnerror))
adamc@59 2232 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 2233 ((L'.StrVar n, loc), sgn) ms
adamc@59 2234 in
adamc@59 2235 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 2236 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 2237 (sgnerror, []))
adamc@83 2238 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 2239 end)
adamc@59 2240
adamc@31 2241
adamc@66 2242 fun selfify env {str, strs, sgn} =
adamc@66 2243 case #1 (hnormSgn env sgn) of
adamc@66 2244 L'.SgnError => sgn
adamc@66 2245 | L'.SgnVar _ => sgn
adamc@66 2246
adamc@66 2247 | L'.SgnConst sgis =>
adamc@66 2248 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 2249 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@191 2250 | (L'.SgiDatatype (x, n, xs, xncs), loc) =>
adamc@191 2251 (L'.SgiDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
adamc@563 2252 | (L'.SgiClassAbs (x, n, k), loc) =>
adamc@563 2253 (L'.SgiClass (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@66 2254 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 2255 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 2256 | x => x) sgis), #2 sgn)
adamc@66 2257 | L'.SgnFun _ => sgn
adamc@66 2258 | L'.SgnWhere _ => sgn
adamc@66 2259 | L'.SgnProj (m, ms, x) =>
adamc@66 2260 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2261 (L'.StrVar m, #2 sgn) ms,
adamc@66 2262 sgn = #2 (E.lookupStrNamed env m),
adamc@66 2263 field = x} of
adamc@66 2264 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 2265 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 2266
adamc@66 2267 fun selfifyAt env {str, sgn} =
adamc@66 2268 let
adamc@66 2269 fun self (str, _) =
adamc@66 2270 case str of
adamc@66 2271 L'.StrVar x => SOME (x, [])
adamc@66 2272 | L'.StrProj (str, x) =>
adamc@66 2273 (case self str of
adamc@66 2274 NONE => NONE
adamc@66 2275 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 2276 | _ => NONE
adamc@66 2277 in
adamc@66 2278 case self str of
adamc@66 2279 NONE => sgn
adamc@66 2280 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 2281 end
adamc@66 2282
adamc@88 2283 fun dopen (env, denv) {str, strs, sgn} =
adamc@66 2284 let
adamc@66 2285 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2286 (L'.StrVar str, #2 sgn) strs
adamc@66 2287 in
adamc@66 2288 case #1 (hnormSgn env sgn) of
adamc@66 2289 L'.SgnConst sgis =>
adamc@88 2290 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
adamc@162 2291 let
adamc@162 2292 val d =
adamc@162 2293 case sgi of
adamc@162 2294 L'.SgiConAbs (x, n, k) =>
adamc@162 2295 let
adamc@162 2296 val c = (L'.CModProj (str, strs, x), loc)
adamc@162 2297 in
adamc@162 2298 (L'.DCon (x, n, k, c), loc)
adamc@162 2299 end
adamc@162 2300 | L'.SgiCon (x, n, k, c) =>
adamc@162 2301 (L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@191 2302 | L'.SgiDatatype (x, n, xs, xncs) =>
adamc@191 2303 (L'.DDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
adamc@191 2304 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@191 2305 (L'.DDatatypeImp (x, n, m1, ms, x', xs, xncs), loc)
adamc@162 2306 | L'.SgiVal (x, n, t) =>
adamc@162 2307 (L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)
adamc@162 2308 | L'.SgiStr (x, n, sgn) =>
adamc@162 2309 (L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)
adamc@162 2310 | L'.SgiSgn (x, n, sgn) =>
adamc@162 2311 (L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)
adamc@162 2312 | L'.SgiConstraint (c1, c2) =>
adamc@162 2313 (L'.DConstraint (c1, c2), loc)
adamc@563 2314 | L'.SgiClassAbs (x, n, k) =>
adamc@211 2315 let
adamc@563 2316 val k' = (L'.KArrow (k, (L'.KType, loc)), loc)
adamc@211 2317 val c = (L'.CModProj (str, strs, x), loc)
adamc@211 2318 in
adamc@563 2319 (L'.DCon (x, n, k', c), loc)
adamc@211 2320 end
adamc@563 2321 | L'.SgiClass (x, n, k, _) =>
adamc@211 2322 let
adamc@563 2323 val k' = (L'.KArrow (k, (L'.KType, loc)), loc)
adamc@211 2324 val c = (L'.CModProj (str, strs, x), loc)
adamc@211 2325 in
adamc@563 2326 (L'.DCon (x, n, k', c), loc)
adamc@211 2327 end
adamc@162 2328 in
adamc@162 2329 (d, (E.declBinds env' d, denv'))
adamc@162 2330 end)
adamc@88 2331 (env, denv) sgis
adamc@66 2332 | _ => (strError env (UnOpenable sgn);
adamc@88 2333 ([], (env, denv)))
adamc@66 2334 end
adamc@66 2335
adamc@31 2336 fun sgiOfDecl (d, loc) =
adamc@31 2337 case d of
adamc@123 2338 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@156 2339 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
adamc@156 2340 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
adamc@123 2341 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 2342 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 2343 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 2344 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2345 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2346 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 2347 | L'.DExport _ => []
adamc@460 2348 | L'.DTable (tn, x, n, c) => [(L'.SgiVal (x, n, (L'.CApp (tableOf (), c), loc)), loc)]
adamc@460 2349 | L'.DSequence (tn, x, n) => [(L'.SgiVal (x, n, sequenceOf ()), loc)]
adamc@563 2350 | L'.DClass (x, n, k, c) => [(L'.SgiClass (x, n, k, c), loc)]
adamc@271 2351 | L'.DDatabase _ => []
adamc@460 2352 | L'.DCookie (tn, x, n, c) => [(L'.SgiVal (x, n, (L'.CApp (cookieOf (), c), loc)), loc)]
adamc@88 2353
adamc@88 2354 fun sgiBindsD (env, denv) (sgi, _) =
adamc@88 2355 case sgi of
adamc@90 2356 L'.SgiConstraint (c1, c2) =>
adamc@90 2357 (case D.assert env denv (c1, c2) of
adamc@90 2358 (denv, []) => denv
adamc@90 2359 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
adamc@88 2360 | _ => denv
adamc@31 2361
adamc@86 2362 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
adamc@469 2363 ((*prefaces "subSgn" [("sgn1", p_sgn env sgn1),
adamc@469 2364 ("sgn2", p_sgn env sgn2)];*)
adamc@35 2365 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 2366 (L'.SgnError, _) => ()
adamc@31 2367 | (_, L'.SgnError) => ()
adamc@31 2368
adamc@31 2369 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 2370 let
adamc@362 2371 (*val () = prefaces "subSgn" [("sgn1", p_sgn env sgn1),
adamc@362 2372 ("sgn2", p_sgn env sgn2),
adamc@362 2373 ("sgis1", p_sgn env (L'.SgnConst sgis1, loc2)),
adamc@362 2374 ("sgis2", p_sgn env (L'.SgnConst sgis2, loc2))]*)
adamc@362 2375
adamc@156 2376 fun folder (sgi2All as (sgi, loc), (env, denv)) =
adamc@31 2377 let
adamc@362 2378 (*val () = prefaces "folder" [("sgis1", p_sgn env (L'.SgnConst sgis1, loc2))]*)
adamc@362 2379
adamc@31 2380 fun seek p =
adamc@31 2381 let
adamc@88 2382 fun seek (env, denv) ls =
adamc@31 2383 case ls of
adamc@31 2384 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@88 2385 (env, denv))
adamc@31 2386 | h :: t =>
adamc@469 2387 case p (env, h) of
adamc@469 2388 NONE =>
adamc@469 2389 let
adamc@469 2390 val env = case #1 h of
adamc@469 2391 L'.SgiCon (x, n, k, c) =>
adamc@471 2392 if E.checkENamed env n then
adamc@471 2393 env
adamc@471 2394 else
adamc@471 2395 E.pushCNamedAs env x n k (SOME c)
adamc@469 2396 | L'.SgiConAbs (x, n, k) =>
adamc@471 2397 if E.checkENamed env n then
adamc@471 2398 env
adamc@471 2399 else
adamc@471 2400 E.pushCNamedAs env x n k NONE
adamc@469 2401 | _ => env
adamc@469 2402 in
adamc@469 2403 seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
adamc@469 2404 end
adamc@88 2405 | SOME envs => envs
adamc@31 2406 in
adamc@88 2407 seek (env, denv) sgis1
adamc@31 2408 end
adamc@31 2409 in
adamc@31 2410 case sgi of
adamc@31 2411 L'.SgiConAbs (x, n2, k2) =>
adamc@469 2412 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@31 2413 let
adamc@41 2414 fun found (x', n1, k1, co1) =
adamc@41 2415 if x = x' then
adamc@41 2416 let
adamc@623 2417 val () = unifyKinds env k1 k2
adamc@41 2418 handle KUnify (k1, k2, err) =>
adamc@41 2419 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 2420 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 2421 in
adamc@41 2422 SOME (if n1 = n2 then
adamc@41 2423 env
adamc@41 2424 else
adamc@88 2425 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
adamc@88 2426 denv)
adamc@41 2427 end
adamc@41 2428 else
adamc@41 2429 NONE
adamc@31 2430 in
adamc@31 2431 case sgi1 of
adamc@41 2432 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 2433 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@191 2434 | L'.SgiDatatype (x', n1, xs, _) =>
adamc@191 2435 let
adamc@191 2436 val k = (L'.KType, loc)
adamc@191 2437 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2438 in
adamc@191 2439 found (x', n1, k', NONE)
adamc@191 2440 end
adamc@191 2441 | L'.SgiDatatypeImp (x', n1, m1, ms, s, xs, _) =>
adamc@191 2442 let
adamc@191 2443 val k = (L'.KType, loc)
adamc@191 2444 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2445 in
adamc@191 2446 found (x', n1, k', SOME (L'.CModProj (m1, ms, s), loc))
adamc@191 2447 end
adamc@563 2448 | L'.SgiClassAbs (x', n1, k) => found (x', n1,
adamc@563 2449 (L'.KArrow (k,
adamc@563 2450 (L'.KType, loc)), loc),
adamc@563 2451 NONE)
adamc@563 2452 | L'.SgiClass (x', n1, k, c) => found (x', n1,
adamc@563 2453 (L'.KArrow (k,
adamc@563 2454 (L'.KType, loc)), loc),
adamc@563 2455 SOME c)
adamc@31 2456 | _ => NONE
adamc@31 2457 end)
adamc@31 2458
adamc@31 2459 | L'.SgiCon (x, n2, k2, c2) =>
adamc@469 2460 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 2461 let
adamc@211 2462 fun found (x', n1, k1, c1) =
adamc@211 2463 if x = x' then
adamc@211 2464 let
adamc@327 2465 fun good () =
adamc@327 2466 let
adamc@327 2467 val env = E.pushCNamedAs env x n2 k2 (SOME c2)
adamc@327 2468 val env = if n1 = n2 then
adamc@327 2469 env
adamc@327 2470 else
adamc@327 2471 E.pushCNamedAs env x n1 k1 (SOME c1)
adamc@327 2472 in
adamc@327 2473 SOME (env, denv)
adamc@327 2474 end
adamc@211 2475 in
adamc@211 2476 (case unifyCons (env, denv) c1 c2 of
adamc@211 2477 [] => good ()
adamc@211 2478 | _ => NONE)
adamc@211 2479 handle CUnify (c1, c2, err) =>
adamc@211 2480 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@211 2481 good ())
adamc@211 2482 end
adamc@211 2483 else
adamc@211 2484 NONE
adamc@211 2485 in
adamc@211 2486 case sgi1 of
adamc@211 2487 L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@563 2488 | L'.SgiClass (x', n1, k1, c1) =>
adamc@563 2489 found (x', n1, (L'.KArrow (k1, (L'.KType, loc)), loc), c1)
adamc@211 2490 | _ => NONE
adamc@211 2491 end)
adamc@31 2492
adamc@191 2493 | L'.SgiDatatype (x, n2, xs2, xncs2) =>
adamc@469 2494 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@160 2495 let
adamc@191 2496 fun found (n1, xs1, xncs1) =
adamc@160 2497 let
adamc@160 2498 fun mismatched ue =
adamc@160 2499 (sgnError env (SgiMismatchedDatatypes (sgi1All, sgi2All, ue));
adamc@160 2500 SOME (env, denv))
adamc@156 2501
adamc@191 2502 val k = (L'.KType, loc)
adamc@191 2503 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs1
adamc@191 2504
adamc@160 2505 fun good () =
adamc@160 2506 let
adamc@471 2507 val env = E.sgiBinds env sgi1All
adamc@160 2508 val env = if n1 = n2 then
adamc@160 2509 env
adamc@160 2510 else
adamc@471 2511 E.pushCNamedAs env x n2 k'
adamc@471 2512 (SOME (L'.CNamed n1, loc))
adamc@160 2513 in
adamc@160 2514 SOME (env, denv)
adamc@160 2515 end
adamc@156 2516
adamc@397 2517 val env = E.pushCNamedAs env x n1 k' NONE
adamc@397 2518 val env = if n1 = n2 then
adamc@397 2519 env
adamc@397 2520 else
adamc@397 2521 E.pushCNamedAs env x n2 k' (SOME (L'.CNamed n1, loc))
adamc@191 2522 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs1
adamc@160 2523 fun xncBad ((x1, _, t1), (x2, _, t2)) =
adamc@160 2524 String.compare (x1, x2) <> EQUAL
adamc@160 2525 orelse case (t1, t2) of
adamc@160 2526 (NONE, NONE) => false
adamc@160 2527 | (SOME t1, SOME t2) =>
adamc@160 2528 not (List.null (unifyCons (env, denv) t1 t2))
adamc@160 2529 | _ => true
adamc@160 2530 in
adamc@191 2531 (if xs1 <> xs2
adamc@191 2532 orelse length xncs1 <> length xncs2
adamc@160 2533 orelse ListPair.exists xncBad (xncs1, xncs2) then
adamc@160 2534 mismatched NONE
adamc@160 2535 else
adamc@160 2536 good ())
adamc@160 2537 handle CUnify ue => mismatched (SOME ue)
adamc@160 2538 end
adamc@160 2539 in
adamc@160 2540 case sgi1 of
adamc@191 2541 L'.SgiDatatype (x', n1, xs, xncs1) =>
adamc@160 2542 if x' = x then
adamc@191 2543 found (n1, xs, xncs1)
adamc@160 2544 else
adamc@160 2545 NONE
adamc@191 2546 | L'.SgiDatatypeImp (x', n1, _, _, _, xs, xncs1) =>
adamc@162 2547 if x' = x then
adamc@191 2548 found (n1, xs, xncs1)
adamc@162 2549 else
adamc@162 2550 NONE
adamc@160 2551 | _ => NONE
adamc@160 2552 end)
adamc@156 2553
adamc@191 2554 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, xs, _) =>
adamc@469 2555 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@158 2556 case sgi1 of
adamc@191 2557 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _, _) =>
adamc@158 2558 if x = x' then
adamc@158 2559 let
adamc@158 2560 val k = (L'.KType, loc)
adamc@191 2561 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@158 2562 val t1 = (L'.CModProj (m11, ms1, s1), loc)
adamc@158 2563 val t2 = (L'.CModProj (m12, ms2, s2), loc)
adamc@158 2564
adamc@158 2565 fun good () =
adamc@158 2566 let
adamc@191 2567 val env = E.pushCNamedAs env x n1 k' (SOME t1)
adamc@191 2568 val env = E.pushCNamedAs env x n2 k' (SOME t2)
adamc@158 2569 in
adamc@158 2570 SOME (env, denv)
adamc@158 2571 end
adamc@158 2572 in
adamc@158 2573 (case unifyCons (env, denv) t1 t2 of
adamc@158 2574 [] => good ()
adamc@158 2575 | _ => NONE)
adamc@158 2576 handle CUnify (c1, c2, err) =>
adamc@158 2577 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@158 2578 good ())
adamc@158 2579 end
adamc@158 2580 else
adamc@158 2581 NONE
adamc@158 2582
adamc@158 2583 | _ => NONE)
adamc@156 2584
adamc@32 2585 | L'.SgiVal (x, n2, c2) =>
adamc@469 2586 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@32 2587 case sgi1 of
adamc@41 2588 L'.SgiVal (x', n1, c1) =>
adamc@41 2589 if x = x' then
adamc@362 2590 ((*prefaces "Pre" [("c1", p_con env c1),
adamc@362 2591 ("c2", p_con env c2)];*)
adamc@362 2592 case unifyCons (env, denv) c1 c2 of
adamc@88 2593 [] => SOME (env, denv)
adamc@86 2594 | _ => NONE)
adamc@86 2595 handle CUnify (c1, c2, err) =>
adamc@86 2596 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@88 2597 SOME (env, denv))
adamc@41 2598 else
adamc@41 2599 NONE
adamc@32 2600 | _ => NONE)
adamc@32 2601
adamc@33 2602 | L'.SgiStr (x, n2, sgn2) =>
adamc@469 2603 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@33 2604 case sgi1 of
adamc@41 2605 L'.SgiStr (x', n1, sgn1) =>
adamc@41 2606 if x = x' then
adamc@66 2607 let
adamc@86 2608 val () = subSgn (env, denv) sgn1 sgn2
adamc@66 2609 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 2610 val env = if n1 = n2 then
adamc@66 2611 env
adamc@66 2612 else
adamc@66 2613 E.pushStrNamedAs env x n2
adamc@66 2614 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 2615 sgn = sgn2})
adamc@66 2616 in
adamc@88 2617 SOME (env, denv)
adamc@66 2618 end
adamc@41 2619 else
adamc@41 2620 NONE
adamc@33 2621 | _ => NONE)
adamc@59 2622
adamc@59 2623 | L'.SgiSgn (x, n2, sgn2) =>
adamc@469 2624 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@59 2625 case sgi1 of
adamc@59 2626 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 2627 if x = x' then
adamc@65 2628 let
adamc@86 2629 val () = subSgn (env, denv) sgn1 sgn2
adamc@86 2630 val () = subSgn (env, denv) sgn2 sgn1
adamc@65 2631
adamc@65 2632 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 2633 val env = if n1 = n2 then
adamc@65 2634 env
adamc@65 2635 else
adamc@65 2636 E.pushSgnNamedAs env x n1 sgn2
adamc@65 2637 in
adamc@88 2638 SOME (env, denv)
adamc@65 2639 end
adamc@59 2640 else
adamc@59 2641 NONE
adamc@59 2642 | _ => NONE)
adamc@88 2643
adamc@88 2644 | L'.SgiConstraint (c2, d2) =>
adamc@469 2645 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@88 2646 case sgi1 of
adamc@88 2647 L'.SgiConstraint (c1, d1) =>
adamc@88 2648 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
adamc@90 2649 let
adamc@90 2650 val (denv, gs) = D.assert env denv (c2, d2)
adamc@90 2651 in
adamc@90 2652 case gs of
adamc@90 2653 [] => ()
adamc@90 2654 | _ => raise Fail "subSgn: Sub-constraints remain";
adamc@90 2655
adamc@90 2656 SOME (env, denv)
adamc@90 2657 end
adamc@88 2658 else
adamc@88 2659 NONE
adamc@88 2660 | _ => NONE)
adamc@203 2661
adamc@563 2662 | L'.SgiClassAbs (x, n2, k2) =>
adamc@469 2663 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 2664 let
adamc@563 2665 fun found (x', n1, k1, co) =
adamc@211 2666 if x = x' then
adamc@211 2667 let
adamc@623 2668 val () = unifyKinds env k1 k2
adamc@563 2669 handle KUnify (k1, k2, err) =>
adamc@563 2670 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@563 2671
adamc@563 2672 val k = (L'.KArrow (k1, (L'.KType, loc)), loc)
adamc@211 2673 val env = E.pushCNamedAs env x n1 k co
adamc@211 2674 in
adamc@211 2675 SOME (if n1 = n2 then
adamc@211 2676 env
adamc@211 2677 else
adamc@211 2678 E.pushCNamedAs env x n2 k (SOME (L'.CNamed n1, loc2)),
adamc@211 2679 denv)
adamc@211 2680 end
adamc@211 2681 else
adamc@211 2682 NONE
adamc@211 2683 in
adamc@211 2684 case sgi1 of
adamc@563 2685 L'.SgiClassAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@563 2686 | L'.SgiClass (x', n1, k1, c) => found (x', n1, k1, SOME c)
adamc@211 2687 | _ => NONE
adamc@211 2688 end)
adamc@563 2689 | L'.SgiClass (x, n2, k2, c2) =>
adamc@469 2690 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 2691 let
adamc@563 2692 val k = (L'.KArrow (k2, (L'.KType, loc)), loc)
adamc@563 2693
adamc@563 2694 fun found (x', n1, k1, c1) =
adamc@211 2695 if x = x' then
adamc@211 2696 let
adamc@623 2697 val () = unifyKinds env k1 k2
adamc@563 2698 handle KUnify (k1, k2, err) =>
adamc@563 2699 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@563 2700
adamc@218 2701 fun good () =
adamc@218 2702 let
adamc@218 2703 val env = E.pushCNamedAs env x n2 k (SOME c2)
adamc@218 2704 val env = if n1 = n2 then
adamc@218 2705 env
adamc@218 2706 else
adamc@218 2707 E.pushCNamedAs env x n1 k (SOME c1)
adamc@218 2708 in
adamc@218 2709 SOME (env, denv)
adamc@218 2710 end
adamc@211 2711 in
adamc@211 2712 (case unifyCons (env, denv) c1 c2 of
adamc@211 2713 [] => good ()
adamc@211 2714 | _ => NONE)
adamc@211 2715 handle CUnify (c1, c2, err) =>
adamc@211 2716 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@211 2717 good ())
adamc@211 2718 end
adamc@211 2719 else
adamc@211 2720 NONE
adamc@211 2721 in
adamc@211 2722 case sgi1 of
adamc@563 2723 L'.SgiClass (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@211 2724 | _ => NONE
adamc@211 2725 end)
adamc@31 2726 end
adamc@31 2727 in
adamc@88 2728 ignore (foldl folder (env, denv) sgis2)
adamc@31 2729 end
adamc@31 2730
adamc@41 2731 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 2732 let
adamc@480 2733 val ran2 =
adamc@43 2734 if n1 = n2 then
adamc@480 2735 ran2
adamc@43 2736 else
adamc@480 2737 subStrInSgn (n2, n1) ran2
adamc@43 2738 in
adamc@86 2739 subSgn (env, denv) dom2 dom1;
adamc@480 2740 subSgn (E.pushStrNamedAs env m1 n1 dom2, denv) ran1 ran2
adamc@43 2741 end
adamc@41 2742
adamc@469 2743 | _ => sgnError env (SgnWrongForm (sgn1, sgn2)))
adamc@41 2744
adamc@61 2745
adamc@312 2746 fun positive self =
adamc@312 2747 let
adamc@312 2748 open L
adamc@312 2749
adamc@312 2750 fun none (c, _) =
adamc@312 2751 case c of
adamc@312 2752 CAnnot (c, _) => none c
adamc@312 2753
adamc@312 2754 | TFun (c1, c2) => none c1 andalso none c2
adamc@312 2755 | TCFun (_, _, _, c) => none c
adamc@312 2756 | TRecord c => none c
adamc@312 2757
adamc@312 2758 | CVar ([], x) => x <> self
adamc@312 2759 | CVar _ => true
adamc@312 2760 | CApp (c1, c2) => none c1 andalso none c2
adamc@312 2761 | CAbs _ => false
adamc@312 2762 | CDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
adamc@312 2763
adamc@623 2764 | CKAbs _ => false
adamc@623 2765 | TKFun _ => false
adamc@623 2766
adamc@312 2767 | CName _ => true
adamc@312 2768
adamc@312 2769 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso none c2) xcs
adamc@312 2770 | CConcat (c1, c2) => none c1 andalso none c2
adamc@621 2771 | CMap => true
adamc@312 2772
adamc@312 2773 | CUnit => true
adamc@312 2774
adamc@312 2775 | CTuple cs => List.all none cs
adamc@312 2776 | CProj (c, _) => none c
adamc@312 2777
adamc@312 2778 | CWild _ => false
adamc@312 2779
adamc@312 2780 fun pos (c, _) =
adamc@312 2781 case c of
adamc@312 2782 CAnnot (c, _) => pos c
adamc@312 2783
adamc@312 2784 | TFun (c1, c2) => none c1 andalso pos c2
adamc@312 2785 | TCFun (_, _, _, c) => pos c
adamc@312 2786 | TRecord c => pos c
adamc@312 2787
adamc@312 2788 | CVar _ => true
adamc@312 2789 | CApp (c1, c2) => pos c1 andalso none c2
adamc@312 2790 | CAbs _ => false
adamc@312 2791 | CDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
adamc@312 2792
adamc@623 2793 | CKAbs _ => false
adamc@623 2794 | TKFun _ => false
adamc@623 2795
adamc@312 2796 | CName _ => true
adamc@312 2797
adamc@312 2798 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso pos c2) xcs
adamc@312 2799 | CConcat (c1, c2) => pos c1 andalso pos c2
adamc@621 2800 | CMap => true
adamc@312 2801
adamc@312 2802 | CUnit => true
adamc@312 2803
adamc@312 2804 | CTuple cs => List.all pos cs
adamc@312 2805 | CProj (c, _) => pos c
adamc@312 2806
adamc@312 2807 | CWild _ => false
adamc@312 2808 in
adamc@312 2809 pos
adamc@312 2810 end
adamc@312 2811
adamc@325 2812 fun wildifyStr env (str, sgn) =
adamc@325 2813 case #1 (hnormSgn env sgn) of
adamc@325 2814 L'.SgnConst sgis =>
adamc@325 2815 (case #1 str of
adamc@325 2816 L.StrConst ds =>
adamc@325 2817 let
adamc@493 2818 fun decompileKind (k, loc) =
adamc@493 2819 case k of
adamc@493 2820 L'.KType => SOME (L.KType, loc)
adamc@493 2821 | L'.KArrow (k1, k2) =>
adamc@493 2822 (case (decompileKind k1, decompileKind k2) of
adamc@493 2823 (SOME k1, SOME k2) => SOME (L.KArrow (k1, k2), loc)
adamc@493 2824 | _ => NONE)
adamc@493 2825 | L'.KName => SOME (L.KName, loc)
adamc@493 2826 | L'.KRecord k =>
adamc@493 2827 (case decompileKind k of
adamc@493 2828 SOME k => SOME (L.KRecord k, loc)
adamc@493 2829 | _ => NONE)
adamc@493 2830 | L'.KUnit => SOME (L.KUnit, loc)
adamc@493 2831 | L'.KTuple ks =>
adamc@493 2832 let
adamc@493 2833 val ks' = List.mapPartial decompileKind ks
adamc@493 2834 in
adamc@493 2835 if length ks' = length ks then
adamc@493 2836 SOME (L.KTuple ks', loc)
adamc@493 2837 else
adamc@493 2838 NONE
adamc@493 2839 end
adamc@493 2840
adamc@493 2841 | L'.KError => NONE
adamc@493 2842 | L'.KUnif (_, _, ref (SOME k)) => decompileKind k
adamc@493 2843 | L'.KUnif _ => NONE
adamc@493 2844
adamc@623 2845 | L'.KRel _ => NONE
adamc@623 2846 | L'.KFun _ => NONE
adamc@623 2847
adamc@325 2848 fun decompileCon env (c, loc) =
adamc@325 2849 case c of
adamc@325 2850 L'.CRel i =>
adamc@325 2851 let
adamc@325 2852 val (s, _) = E.lookupCRel env i
adamc@325 2853 in
adamc@325 2854 SOME (L.CVar ([], s), loc)
adamc@325 2855 end
adamc@325 2856 | L'.CNamed i =>
adamc@325 2857 let
adamc@325 2858 val (s, _, _) = E.lookupCNamed env i
adamc@325 2859 in
adamc@325 2860 SOME (L.CVar ([], s), loc)
adamc@325 2861 end
adamc@325 2862 | L'.CModProj (m1, ms, x) =>
adamc@325 2863 let
adamc@325 2864 val (s, _) = E.lookupStrNamed env m1
adamc@325 2865 in
adamc@325 2866 SOME (L.CVar (s :: ms, x), loc)
adamc@325 2867 end
adamc@325 2868 | L'.CName s => SOME (L.CName s, loc)
adamc@325 2869 | L'.CRecord (_, xcs) =>
adamc@325 2870 let
adamc@325 2871 fun fields xcs =
adamc@325 2872 case xcs of
adamc@325 2873 [] => SOME []
adamc@325 2874 | (x, t) :: xcs =>
adamc@325 2875 case (decompileCon env x, decompileCon env t, fields xcs) of
adamc@325 2876 (SOME x, SOME t, SOME xcs) => SOME ((x, t) :: xcs)
adamc@325 2877 | _ => NONE
adamc@325 2878 in
adamc@325 2879 Option.map (fn xcs => (L.CRecord xcs, loc))
adamc@325 2880 (fields xcs)
adamc@325 2881 end
adamc@325 2882 | L'.CConcat (c1, c2) =>
adamc@325 2883 (case (decompileCon env c1, decompileCon env c2) of
adamc@325 2884 (SOME c1, SOME c2) => SOME (L.CConcat (c1, c2), loc)
adamc@325 2885 | _ => NONE)
adamc@325 2886 | L'.CUnit => SOME (L.CUnit, loc)
adamc@325 2887
adamc@325 2888 | _ => NONE
adamc@325 2889
adamc@469 2890 val (neededC, constraints, neededV, _) =
adamc@469 2891 foldl (fn ((sgi, loc), (neededC, constraints, neededV, env')) =>
adamc@325 2892 let
adamc@469 2893 val (needed, constraints, neededV) =
adamc@325 2894 case sgi of
adamc@493 2895 L'.SgiConAbs (x, _, k) => (SM.insert (neededC, x, k), constraints, neededV)
adamc@469 2896 | L'.SgiConstraint cs => (neededC, (env', cs, loc) :: constraints, neededV)
adamc@469 2897
adamc@469 2898 | L'.SgiVal (x, _, t) =>
adamc@469 2899 let
adamc@469 2900 fun default () = (neededC, constraints, neededV)
adamc@469 2901
adamc@469 2902 val t = normClassConstraint env' t
adamc@469 2903 in
adamc@469 2904 case #1 t of
adamc@469 2905 L'.CApp (f, _) =>
adamc@469 2906 if E.isClass env' f then
adamc@469 2907 (neededC, constraints, SS.add (neededV, x))
adamc@469 2908 else
adamc@469 2909 default ()
adamc@469 2910
adamc@469 2911 | _ => default ()
adamc@469 2912 end
adamc@469 2913
adamc@469 2914 | _ => (neededC, constraints, neededV)
adamc@325 2915 in
adamc@469 2916 (needed, constraints, neededV, E.sgiBinds env' (sgi, loc))
adamc@325 2917 end)
adamc@493 2918 (SM.empty, [], SS.empty, env) sgis
adamc@325 2919
adamc@469 2920 val (neededC, neededV) = foldl (fn ((d, _), needed as (neededC, neededV)) =>
adamc@469 2921 case d of
adamc@493 2922 L.DCon (x, _, _) => ((#1 (SM.remove (neededC, x)), neededV)
adamc@469 2923 handle NotFound =>
adamc@469 2924 needed)
adamc@563 2925 | L.DClass (x, _, _) => ((#1 (SM.remove (neededC, x)), neededV)
adamc@563 2926 handle NotFound => needed)
adamc@469 2927 | L.DVal (x, _, _) => ((neededC, SS.delete (neededV, x))
adamc@469 2928 handle NotFound => needed)
adamc@493 2929 | L.DOpen _ => (SM.empty, SS.empty)
adamc@469 2930 | _ => needed)
adamc@469 2931 (neededC, neededV) ds
adamc@469 2932
adamc@469 2933 val ds' = List.mapPartial (fn (env', (c1, c2), loc) =>
adamc@325 2934 case (decompileCon env' c1, decompileCon env' c2) of
adamc@325 2935 (SOME c1, SOME c2) =>
adamc@325 2936 SOME (L.DConstraint (c1, c2), loc)
adamc@325 2937 | _ => NONE) constraints
adamc@469 2938
adamc@469 2939 val ds' =
adamc@469 2940 case SS.listItems neededV of
adamc@469 2941 [] => ds'
adamc@469 2942 | xs =>
adamc@469 2943 let
adamc@469 2944 val ewild = (L.EWild, #2 str)
adamc@469 2945 val ds'' = map (fn x => (L.DVal (x, NONE, ewild), #2 str)) xs
adamc@469 2946 in
adamc@469 2947 ds'' @ ds'
adamc@469 2948 end
adamc@469 2949
adamc@469 2950 val ds' =
adamc@493 2951 case SM.listItemsi neededC of
adamc@469 2952 [] => ds'
adamc@469 2953 | xs =>
adamc@469 2954 let
adamc@493 2955 val ds'' = map (fn (x, k) =>
adamc@493 2956 let
adamc@493 2957 val k =
adamc@493 2958 case decompileKind k of
adamc@493 2959 NONE => (L.KWild, #2 str)
adamc@493 2960 | SOME k => k
adamc@493 2961 val cwild = (L.CWild k, #2 str)
adamc@493 2962 in
adamc@493 2963 (L.DCon (x, NONE, cwild), #2 str)
adamc@493 2964 end) xs
adamc@469 2965 in
adamc@469 2966 ds'' @ ds'
adamc@469 2967 end
adamc@325 2968 in
adamc@469 2969 (L.StrConst (ds @ ds'), #2 str)
adamc@325 2970 end
adamc@325 2971 | _ => str)
adamc@325 2972 | _ => str
adamc@325 2973
adamc@280 2974 fun elabDecl (dAll as (d, loc), (env, denv, gs : constraint list)) =
adamc@255 2975 let
adamc@255 2976 (*val () = preface ("elabDecl", SourcePrint.p_decl (d, loc))*)
adamc@514 2977 (*val befor = Time.now ()*)
adamc@255 2978
adamc@255 2979 val r =
adamc@255 2980 case d of
adamc@255 2981 L.DCon (x, ko, c) =>
adamc@255 2982 let
adamc@255 2983 val k' = case ko of
adamc@255 2984 NONE => kunif loc
adamc@623 2985 | SOME k => elabKind env k
adamc@255 2986
adamc@255 2987 val (c', ck, gs') = elabCon (env, denv) c
adamc@255 2988 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@255 2989 in
adamc@255 2990 checkKind env c' ck k';
adamc@255 2991
adamc@255 2992 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, enD gs' @ gs))
adamc@255 2993 end
adamc@255 2994 | L.DDatatype (x, xs, xcs) =>
adamc@255 2995 let
adamc@312 2996 val positive = List.all (fn (_, to) =>
adamc@312 2997 case to of
adamc@312 2998 NONE => true
adamc@312 2999 | SOME t => positive x t) xcs
adamc@312 3000
adamc@255 3001 val k = (L'.KType, loc)
adamc@255 3002 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@255 3003 val (env, n) = E.pushCNamed env x k' NONE
adamc@255 3004 val t = (L'.CNamed n, loc)
adamc@255 3005 val nxs = length xs - 1
adamc@255 3006 val t = ListUtil.foldli (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@255 3007
adamc@255 3008 val (env', denv') = foldl (fn (x, (env', denv')) =>
adamc@255 3009 (E.pushCRel env' x k,
adamc@255 3010 D.enter denv')) (env, denv) xs
adamc@255 3011
adamc@255 3012 val (xcs, (used, env, gs')) =
adamc@255 3013 ListUtil.foldlMap
adamc@255 3014 (fn ((x, to), (used, env, gs)) =>
adamc@255 3015 let
adamc@255 3016 val (to, t, gs') = case to of
adamc@255 3017 NONE => (NONE, t, gs)
adamc@255 3018 | SOME t' =>
adamc@255 3019 let
adamc@255 3020 val (t', tk, gs') = elabCon (env', denv') t'
adamc@255 3021 in
adamc@255 3022 checkKind env' t' tk k;
adamc@255 3023 (SOME t', (L'.TFun (t', t), loc), enD gs' @ gs)
adamc@255 3024 end
adamc@255 3025 val t = foldr (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
adamc@255 3026
adamc@255 3027 val (env, n') = E.pushENamed env x t
adamc@255 3028 in
adamc@255 3029 if SS.member (used, x) then
adamc@255 3030 strError env (DuplicateConstructor (x, loc))
adamc@255 3031 else
adamc@255 3032 ();
adamc@255 3033 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@255 3034 end)
adamc@255 3035 (SS.empty, env, []) xcs
adamc@255 3036
adamc@255 3037 val env = E.pushDatatype env n xs xcs
adamc@312 3038 val d' = (L'.DDatatype (x, n, xs, xcs), loc)
adamc@255 3039 in
adamc@601 3040 (*if positive then
adamc@312 3041 ()
adamc@312 3042 else
adamc@601 3043 declError env (Nonpositive d');*)
adamc@312 3044
adamc@312 3045 ([d'], (env, denv, gs' @ gs))
adamc@255 3046 end
adamc@255 3047
adamc@255 3048 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
adamc@255 3049
adamc@255 3050 | L.DDatatypeImp (x, m1 :: ms, s) =>
adamc@255 3051 (case E.lookupStr env m1 of
adamc@255 3052 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@255 3053 ([], (env, denv, gs)))
adamc@255 3054 | SOME (n, sgn) =>
adamc@255 3055 let
adamc@255 3056 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@255 3057 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@255 3058 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@255 3059 (strerror, sgnerror))
adamc@255 3060 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@255 3061 ((L'.StrVar n, loc), sgn) ms
adamc@255 3062 in
adamc@255 3063 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@255 3064 ((L'.CModProj (n, ms, s), _), gs') =>
adamc@255 3065 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@255 3066 NONE => (conError env (UnboundDatatype (loc, s));
adamc@255 3067 ([], (env, denv, gs)))
adamc@255 3068 | SOME (xs, xncs) =>
adamc@255 3069 let
adamc@255 3070 val k = (L'.KType, loc)
adamc@255 3071 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@255 3072 val t = (L'.CModProj (n, ms, s), loc)
adamc@255 3073 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@255 3074 val env = E.pushDatatype env n' xs xncs
adamc@255 3075
adamc@255 3076 val t = (L'.CNamed n', loc)
adamc@255 3077 val env = foldl (fn ((x, n, to), env) =>
adamc@255 3078 let
adamc@255 3079 val t = case to of
adamc@255 3080 NONE => t
adamc@255 3081 | SOME t' => (L'.TFun (t', t), loc)
adamc@255 3082
adamc@255 3083 val t = foldr (fn (x, t) =>
adamc@255 3084 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@255 3085 t xs
adamc@255 3086 in
adamc@255 3087 E.pushENamedAs env x n t
adamc@255 3088 end) env xncs
adamc@255 3089 in
adamc@255 3090 ([(L'.DDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, enD gs' @ gs))
adamc@255 3091 end)
adamc@255 3092 | _ => (strError env (NotDatatype loc);
adamc@255 3093 ([], (env, denv, [])))
adamc@255 3094 end)
adamc@255 3095
adamc@255 3096 | L.DVal (x, co, e) =>
adamc@255 3097 let
adamc@255 3098 val (c', _, gs1) = case co of
adamc@255 3099 NONE => (cunif (loc, ktype), ktype, [])
adamc@255 3100 | SOME c => elabCon (env, denv) c
adamc@255 3101
adamc@255 3102 val (e', et, gs2) = elabExp (env, denv) e
adamc@255 3103 val gs3 = checkCon (env, denv) e' et c'
adamc@467 3104 val c = normClassConstraint env c'
adamc@255 3105 val (env', n) = E.pushENamed env x c'
adamc@334 3106 val c' = makeInstantiable c'
adamc@255 3107 in
adamc@255 3108 (*prefaces "DVal" [("x", Print.PD.string x),
adamc@362 3109 ("c'", p_con env c')];*)
adamc@467 3110 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, enD gs1 @ gs2 @ enD gs3 @ gs))
adamc@255 3111 end
adamc@255 3112 | L.DValRec vis =>
adamc@255 3113 let
adamc@255 3114 fun allowable (e, _) =
adamc@255 3115 case e of
adamc@255 3116 L.EAbs _ => true
adamc@255 3117 | L.ECAbs (_, _, _, e) => allowable e
adamc@623 3118 | L.EKAbs (_, e) => allowable e
adamc@255 3119 | L.EDisjoint (_, _, e) => allowable e
adamc@255 3120 | _ => false
adamc@255 3121
adamc@255 3122 val (vis, gs) = ListUtil.foldlMap
adamc@255 3123 (fn ((x, co, e), gs) =>
adamc@255 3124 let
adamc@255 3125 val (c', _, gs1) = case co of
adamc@255 3126 NONE => (cunif (loc, ktype), ktype, [])
adamc@255 3127 | SOME c => elabCon (env, denv) c
adamc@255 3128 in
adamc@255 3129 ((x, c', e), enD gs1 @ gs)
adamc@280 3130 end) gs vis
adamc@255 3131
adamc@255 3132 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@255 3133 let
adamc@255 3134 val (env, n) = E.pushENamed env x c'
adamc@255 3135 in
adamc@255 3136 ((x, n, c', e), env)
adamc@255 3137 end) env vis
adamc@255 3138
adamc@255 3139 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@255 3140 let
adamc@255 3141 val (e', et, gs1) = elabExp (env, denv) e
adamc@255 3142
adamc@255 3143 val gs2 = checkCon (env, denv) e' et c'
adamc@334 3144
adamc@334 3145 val c' = makeInstantiable c'
adamc@255 3146 in
adamc@255 3147 if allowable e then
adamc@255 3148 ()
adamc@255 3149 else
adamc@255 3150 expError env (IllegalRec (x, e'));
adamc@255 3151 ((x, n, c', e'), gs1 @ enD gs2 @ gs)
adamc@255 3152 end) gs vis
adamc@255 3153 in
adamc@255 3154 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@255 3155 end
adamc@255 3156
adamc@255 3157 | L.DSgn (x, sgn) =>
adamc@255 3158 let
adamc@255 3159 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@255 3160 val (env', n) = E.pushSgnNamed env x sgn'
adamc@255 3161 in
adamc@255 3162 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3163 end
adamc@255 3164
adamc@255 3165 | L.DStr (x, sgno, str) =>
adamc@255 3166 let
adamc@255 3167 val () = if x = "Basis" then
adamc@255 3168 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@255 3169 else
adamc@255 3170 ()
adamc@255 3171
adamc@255 3172 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@255 3173
adamc@255 3174 val (str', sgn', gs') =
adamc@255 3175 case formal of
adamc@255 3176 NONE =>
adamc@255 3177 let
adamc@255 3178 val (str', actual, gs') = elabStr (env, denv) str
adamc@255 3179 in
adamc@255 3180 (str', selfifyAt env {str = str', sgn = actual}, gs')
adamc@255 3181 end
adamc@255 3182 | SOME (formal, gs1) =>
adamc@255 3183 let
adamc@325 3184 val str = wildifyStr env (str, formal)
adamc@255 3185 val (str', actual, gs2) = elabStr (env, denv) str
adamc@255 3186 in
adamc@255 3187 subSgn (env, denv) (selfifyAt env {str = str', sgn = actual}) formal;
adamc@255 3188 (str', formal, enD gs1 @ gs2)
adamc@255 3189 end
adamc@255 3190
adamc@255 3191 val (env', n) = E.pushStrNamed env x sgn'
adamc@255 3192 in
adamc@255 3193 case #1 (hnormSgn env sgn') of
adamc@255 3194 L'.SgnFun _ =>
adamc@255 3195 (case #1 str' of
adamc@255 3196 L'.StrFun _ => ()
adamc@255 3197 | _ => strError env (FunctorRebind loc))
adamc@255 3198 | _ => ();
adamc@255 3199
adamc@255 3200 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
adamc@255 3201 end
adamc@255 3202
adamc@255 3203 | L.DFfiStr (x, sgn) =>
adamc@255 3204 let
adamc@255 3205 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@255 3206
adamc@255 3207 val (env', n) = E.pushStrNamed env x sgn'
adamc@255 3208 in
adamc@255 3209 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3210 end
adamc@255 3211
adamc@255 3212 | L.DOpen (m, ms) =>
adamc@255 3213 (case E.lookupStr env m of
adamc@255 3214 NONE => (strError env (UnboundStr (loc, m));
adamc@255 3215 ([], (env, denv, gs)))
adamc@255 3216 | SOME (n, sgn) =>
adamc@255 3217 let
adamc@255 3218 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@255 3219 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@255 3220 NONE => (strError env (UnboundStr (loc, m));
adamc@255 3221 (strerror, sgnerror))
adamc@255 3222 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@255 3223 ((L'.StrVar n, loc), sgn) ms
adamc@255 3224
adamc@255 3225 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
adamc@255 3226 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
adamc@255 3227 in
adamc@255 3228 (ds, (env', denv', gs))
adamc@255 3229 end)
adamc@255 3230
adamc@255 3231 | L.DConstraint (c1, c2) =>
adamc@255 3232 let
adamc@255 3233 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@255 3234 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@255 3235 val gs3 = D.prove env denv (c1', c2', loc)
adamc@255 3236
adamc@255 3237 val (denv', gs4) = D.assert env denv (c1', c2')
adamc@255 3238 in
adamc@255 3239 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@255 3240 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@255 3241
adamc@255 3242 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', enD gs1 @ enD gs2 @ enD gs3 @ enD gs4 @ gs))
adamc@255 3243 end
adamc@255 3244
adamc@255 3245 | L.DOpenConstraints (m, ms) =>
adamc@255 3246 let
adamc@255 3247 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@255 3248 in
adamc@255 3249 ([], (env, denv, gs))
adamc@255 3250 end
adamc@255 3251
adamc@255 3252 | L.DExport str =>
adamc@255 3253 let
adamc@255 3254 val (str', sgn, gs') = elabStr (env, denv) str
adamc@255 3255
adamc@255 3256 val sgn =
adamc@255 3257 case #1 (hnormSgn env sgn) of
adamc@255 3258 L'.SgnConst sgis =>
adamc@255 3259 let
adamc@255 3260 fun doOne (all as (sgi, _), env) =
adamc@255 3261 (case sgi of
adamc@255 3262 L'.SgiVal (x, n, t) =>
adamc@325 3263 let
adamc@325 3264 fun doPage (makeRes, ran) =
adamc@325 3265 case hnormCon (env, denv) ran of
adamc@325 3266 ((L'.CApp (tf, arg), _), []) =>
adamc@325 3267 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg) of
adamc@325 3268 (((L'.CModProj (basis, [], "transaction"), _), []),
adamc@325 3269 ((L'.CApp (tf, arg3), _), [])) =>
adamc@325 3270 (case (basis = !basis_r,
adamc@325 3271 hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
adamc@325 3272 (true,
adamc@325 3273 ((L'.CApp (tf, arg2), _), []),
adamc@325 3274 (((L'.CRecord (_, []), _), []))) =>
adamc@325 3275 (case (hnormCon (env, denv) tf) of
adamc@325 3276 ((L'.CApp (tf, arg1), _), []) =>
adamc@325 3277 (case (hnormCon (env, denv) tf,
adamc@325 3278 hnormCon (env, denv) arg1,
adamc@325 3279 hnormCon (env, denv) arg2) of
adamc@325 3280 ((tf, []), (arg1, []),
adamc@325 3281 ((L'.CRecord (_, []), _), [])) =>
adamc@325 3282 let
adamc@325 3283 val t = (L'.CApp (tf, arg1), loc)
adamc@325 3284 val t = (L'.CApp (t, arg2), loc)
adamc@325 3285 val t = (L'.CApp (t, arg3), loc)
adamc@325 3286 val t = (L'.CApp (
adamc@325 3287 (L'.CModProj
adamc@325 3288 (basis, [], "transaction"), loc),
adamc@325 3289 t), loc)
adamc@325 3290 in
adamc@325 3291 (L'.SgiVal (x, n, makeRes t), loc)
adamc@325 3292 end
adamc@325 3293 | _ => all)
adamc@325 3294 | _ => all)
adamc@325 3295 | _ => all)
adamc@325 3296 | _ => all)
adamc@325 3297 | _ => all
adamc@325 3298 in
adamc@325 3299 case hnormCon (env, denv) t of
adamc@325 3300 ((L'.TFun (dom, ran), _), []) =>
adamc@325 3301 (case hnormCon (env, denv) dom of
adamc@325 3302 ((L'.TRecord domR, _), []) =>
adamc@325 3303 doPage (fn t => (L'.TFun ((L'.TRecord domR,
adamc@325 3304 loc),
adamc@325 3305 t), loc), ran)
adamc@325 3306 | _ => all)
adamc@325 3307 | _ => doPage (fn t => t, t)
adamc@325 3308 end
adamc@255 3309 | _ => all,
adamc@255 3310 E.sgiBinds env all)
adamc@255 3311 in
adamc@255 3312 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@255 3313 end
adamc@255 3314 | _ => sgn
adamc@255 3315 in
adamc@255 3316 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs' @ gs))
adamc@255 3317 end
adamc@255 3318
adamc@255 3319 | L.DTable (x, c) =>
adamc@255 3320 let
adamc@255 3321 val (c', k, gs') = elabCon (env, denv) c
adamc@255 3322 val (env, n) = E.pushENamed env x (L'.CApp (tableOf (), c'), loc)
adamc@255 3323 in
adamc@255 3324 checkKind env c' k (L'.KRecord (L'.KType, loc), loc);
adamc@255 3325 ([(L'.DTable (!basis_r, x, n, c'), loc)], (env, denv, enD gs' @ gs))
adamc@255 3326 end
adamc@338 3327 | L.DSequence x =>
adamc@338 3328 let
adamc@338 3329 val (env, n) = E.pushENamed env x (sequenceOf ())
adamc@338 3330 in
adamc@338 3331 ([(L'.DSequence (!basis_r, x, n), loc)], (env, denv, gs))
adamc@338 3332 end
adamc@255 3333
adamc@563 3334 | L.DClass (x, k, c) =>
adamc@255 3335 let
adamc@623 3336 val k = elabKind env k
adamc@563 3337 val k' = (L'.KArrow (k, (L'.KType, loc)), loc)
adamc@280 3338 val (c', ck, gs') = elabCon (env, denv) c
adamc@563 3339 val (env, n) = E.pushCNamed env x k' (SOME c')
adamc@255 3340 val env = E.pushClass env n
adamc@255 3341 in
adamc@563 3342 checkKind env c' ck k';
adamc@563 3343 ([(L'.DClass (x, n, k, c'), loc)], (env, denv, enD gs' @ gs))
adamc@255 3344 end
adamc@271 3345
adamc@280 3346 | L.DDatabase s => ([(L'.DDatabase s, loc)], (env, denv, gs))
adamc@280 3347
adamc@459 3348 | L.DCookie (x, c) =>
adamc@459 3349 let
adamc@459 3350 val (c', k, gs') = elabCon (env, denv) c
adamc@459 3351 val (env, n) = E.pushENamed env x (L'.CApp (cookieOf (), c'), loc)
adamc@459 3352 in
adamc@459 3353 checkKind env c' k (L'.KType, loc);
adamc@459 3354 ([(L'.DCookie (!basis_r, x, n, c'), loc)], (env, denv, enD gs' @ gs))
adamc@459 3355 end
adamc@459 3356
adamc@280 3357 (*val tcs = List.filter (fn TypeClass _ => true | _ => false) (#3 (#2 r))*)
adamc@255 3358 in
adamc@280 3359 (*prefaces "elabDecl" [("e", SourcePrint.p_decl dAll),
adamc@514 3360 ("t", PD.string (LargeReal.toString (Time.toReal
adamc@514 3361 (Time.- (Time.now (), befor)))))];*)
adamc@280 3362
adamc@255 3363 r
adamc@255 3364 end
adamc@211 3365
adamc@83 3366 and elabStr (env, denv) (str, loc) =
adamc@31 3367 case str of
adamc@31 3368 L.StrConst ds =>
adamc@31 3369 let
adamc@88 3370 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 3371 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 3372
adamc@63 3373 val (sgis, _, _, _, _) =
adamc@64 3374 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 3375 case sgi of
adamc@64 3376 L'.SgiConAbs (x, n, k) =>
adamc@64 3377 let
adamc@64 3378 val (cons, x) =
adamc@64 3379 if SS.member (cons, x) then
adamc@64 3380 (cons, "?" ^ x)
adamc@64 3381 else
adamc@64 3382 (SS.add (cons, x), x)
adamc@64 3383 in
adamc@64 3384 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 3385 end
adamc@64 3386 | L'.SgiCon (x, n, k, c) =>
adamc@64 3387 let
adamc@64 3388 val (cons, x) =
adamc@64 3389 if SS.member (cons, x) then
adamc@64 3390 (cons, "?" ^ x)
adamc@64 3391 else
adamc@64 3392 (SS.add (cons, x), x)
adamc@64 3393 in
adamc@64 3394 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 3395 end
adamc@191 3396 | L'.SgiDatatype (x, n, xs, xncs) =>
adamc@156 3397 let
adamc@156 3398 val (cons, x) =
adamc@156 3399 if SS.member (cons, x) then
adamc@156 3400 (cons, "?" ^ x)
adamc@156 3401 else
adamc@156 3402 (SS.add (cons, x), x)
adamc@156 3403
adamc@156 3404 val (xncs, vals) =
adamc@156 3405 ListUtil.foldlMap
adamc@156 3406 (fn ((x, n, t), vals) =>
adamc@156 3407 if SS.member (vals, x) then
adamc@156 3408 (("?" ^ x, n, t), vals)
adamc@156 3409 else
adamc@156 3410 ((x, n, t), SS.add (vals, x)))
adamc@156 3411 vals xncs
adamc@156 3412 in
adamc@191 3413 ((L'.SgiDatatype (x, n, xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 3414 end
adamc@191 3415 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@156 3416 let
adamc@156 3417 val (cons, x) =
adamc@156 3418 if SS.member (cons, x) then
adamc@156 3419 (cons, "?" ^ x)
adamc@156 3420 else
adamc@156 3421 (SS.add (cons, x), x)
adamc@156 3422 in
adamc@191 3423 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 3424 end
adamc@64 3425 | L'.SgiVal (x, n, c) =>
adamc@64 3426 let
adamc@64 3427 val (vals, x) =
adamc@64 3428 if SS.member (vals, x) then
adamc@64 3429 (vals, "?" ^ x)
adamc@64 3430 else
adamc@64 3431 (SS.add (vals, x), x)
adamc@64 3432 in
adamc@64 3433 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 3434 end
adamc@64 3435 | L'.SgiSgn (x, n, sgn) =>
adamc@64 3436 let
adamc@64 3437 val (sgns, x) =
adamc@64 3438 if SS.member (sgns, x) then
adamc@64 3439 (sgns, "?" ^ x)
adamc@64 3440 else
adamc@64 3441 (SS.add (sgns, x), x)
adamc@64 3442 in
adamc@64 3443 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 3444 end
adamc@64 3445
adamc@64 3446 | L'.SgiStr (x, n, sgn) =>
adamc@64 3447 let
adamc@64 3448 val (strs, x) =
adamc@64 3449 if SS.member (strs, x) then
adamc@64 3450 (strs, "?" ^ x)
adamc@64 3451 else
adamc@64 3452 (SS.add (strs, x), x)
adamc@64 3453 in
adamc@64 3454 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 3455 end
adamc@203 3456 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs)
adamc@563 3457 | L'.SgiClassAbs (x, n, k) =>
adamc@211 3458 let
adamc@211 3459 val (cons, x) =
adamc@211 3460 if SS.member (cons, x) then
adamc@211 3461 (cons, "?" ^ x)
adamc@211 3462 else
adamc@211 3463 (SS.add (cons, x), x)
adamc@211 3464 in
adamc@563 3465 ((L'.SgiClassAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@211 3466 end
adamc@563 3467 | L'.SgiClass (x, n, k, c) =>
adamc@211 3468 let
adamc@211 3469 val (cons, x) =
adamc@211 3470 if SS.member (cons, x) then
adamc@211 3471 (cons, "?" ^ x)
adamc@211 3472 else
adamc@211 3473 (SS.add (cons, x), x)
adamc@211 3474 in
adamc@563 3475 ((L'.SgiClass (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@203 3476 end)
adamc@64 3477
adamc@63 3478 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 3479 in
adamc@83 3480 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 3481 end
adamc@31 3482 | L.StrVar x =>
adamc@31 3483 (case E.lookupStr env x of
adamc@31 3484 NONE =>
adamc@31 3485 (strError env (UnboundStr (loc, x));
adamc@83 3486 (strerror, sgnerror, []))
adamc@83 3487 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 3488 | L.StrProj (str, x) =>
adamc@34 3489 let
adamc@83 3490 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 3491 in
adamc@34 3492 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 3493 NONE => (strError env (UnboundStr (loc, x));
adamc@83 3494 (strerror, sgnerror, []))
adamc@83 3495 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 3496 end
adamc@41 3497 | L.StrFun (m, dom, ranO, str) =>
adamc@41 3498 let
adamc@83 3499 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 3500 val (env', n) = E.pushStrNamed env m dom'
adamc@83 3501 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 3502
adamc@83 3503 val (formal, gs3) =
adamc@41 3504 case ranO of
adamc@83 3505 NONE => (actual, [])
adamc@41 3506 | SOME ran =>
adamc@41 3507 let
adamc@83 3508 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 3509 in
adamc@86 3510 subSgn (env', denv) actual ran';
adamc@83 3511 (ran', gs)
adamc@41 3512 end
adamc@41 3513 in
adamc@41 3514 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 3515 (L'.SgnFun (m, n, dom', formal), loc),
adamc@228 3516 enD gs1 @ gs2 @ enD gs3)
adamc@41 3517 end
adamc@44 3518 | L.StrApp (str1, str2) =>
adamc@44 3519 let
adamc@83 3520 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@325 3521 val str2 =
adamc@325 3522 case sgn1 of
adamc@325 3523 (L'.SgnFun (_, _, dom, _), _) =>
adamc@325 3524 wildifyStr env (str2, dom)
adamc@325 3525 | _ => str2
adamc@83 3526 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 3527 in
adamc@44 3528 case #1 (hnormSgn env sgn1) of
adamc@83 3529 L'.SgnError => (strerror, sgnerror, [])
adamc@44 3530 | L'.SgnFun (m, n, dom, ran) =>
adamc@86 3531 (subSgn (env, denv) sgn2 dom;
adamc@44 3532 case #1 (hnormSgn env ran) of
adamc@83 3533 L'.SgnError => (strerror, sgnerror, [])
adamc@44 3534 | L'.SgnConst sgis =>
adamc@44 3535 ((L'.StrApp (str1', str2'), loc),
adamc@83 3536 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 3537 gs1 @ gs2)
adamc@44 3538 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 3539 | _ => (strError env (NotFunctor sgn1);
adamc@83 3540 (strerror, sgnerror, []))
adamc@44 3541 end
adamc@31 3542
adamc@325 3543 fun elabFile basis topStr topSgn env file =
adamc@56 3544 let
adamc@83 3545 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 3546 val () = case gs of
adamc@83 3547 [] => ()
adamc@207 3548 | _ => (app (fn (_, env, _, c1, c2) =>
adamc@207 3549 prefaces "Unresolved"
adamc@207 3550 [("c1", p_con env c1),
adamc@207 3551 ("c2", p_con env c2)]) gs;
adamc@207 3552 raise Fail "Unresolved disjointness constraints in Basis")
adamc@83 3553
adamc@56 3554 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@210 3555 val () = basis_r := basis_n
adamc@56 3556
adamc@88 3557 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
adamc@56 3558
adamc@56 3559 fun discoverC r x =
adamc@56 3560 case E.lookupC env' x of
adamc@56 3561 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 3562 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 3563 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 3564
adamc@56 3565 val () = discoverC int "int"
adamc@56 3566 val () = discoverC float "float"
adamc@56 3567 val () = discoverC string "string"
adamc@203 3568 val () = discoverC table "sql_table"
adamc@56 3569
adamc@325 3570 val (topSgn, gs) = elabSgn (env', D.empty) (L.SgnConst topSgn, ErrorMsg.dummySpan)
adamc@325 3571 val () = case gs of
adamc@325 3572 [] => ()
adamc@325 3573 | _ => raise Fail "Unresolved disjointness constraints in top.urs"
adamc@325 3574 val (topStr, topSgn', gs) = elabStr (env', D.empty) (L.StrConst topStr, ErrorMsg.dummySpan)
adamc@325 3575 val () = case gs of
adamc@325 3576 [] => ()
adamc@329 3577 | _ => app (fn Disjoint (loc, env, denv, c1, c2) =>
adamc@329 3578 (case D.prove env denv (c1, c2, loc) of
adamc@329 3579 [] => ()
adamc@329 3580 | _ =>
adamc@329 3581 (prefaces "Unresolved constraint in top.ur"
adamc@329 3582 [("loc", PD.string (ErrorMsg.spanToString loc)),
adamc@329 3583 ("c1", p_con env c1),
adamc@329 3584 ("c2", p_con env c2)];
adamc@478 3585 raise Fail "Unresolved constraint in top.ur"))
adamc@564 3586 | TypeClass (env, c, r, loc) =>
adamc@564 3587 let
adamc@564 3588 val c = normClassKey env c
adamc@564 3589 in
adamc@564 3590 case E.resolveClass env c of
adamc@564 3591 SOME e => r := SOME e
adamc@564 3592 | NONE => expError env (Unresolvable (loc, c))
adamc@564 3593 end) gs
adamc@564 3594
adamc@325 3595 val () = subSgn (env', D.empty) topSgn' topSgn
adamc@325 3596
adamc@325 3597 val (env', top_n) = E.pushStrNamed env' "Top" topSgn
adamc@325 3598
adamc@325 3599 val (ds', (env', _)) = dopen (env', D.empty) {str = top_n, strs = [], sgn = topSgn}
adamc@325 3600
adamc@83 3601 fun elabDecl' (d, (env, gs)) =
adamc@76 3602 let
adamc@76 3603 val () = resetKunif ()
adamc@76 3604 val () = resetCunif ()
adamc@88 3605 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 3606 in
adamc@76 3607 if ErrorMsg.anyErrors () then
adamc@76 3608 ()
adamc@76 3609 else (
adamc@76 3610 if List.exists kunifsInDecl ds then
adamc@91 3611 declError env (KunifsRemain ds)
adamc@76 3612 else
adamc@76 3613 ();
adamc@76 3614
adamc@76 3615 case ListUtil.search cunifsInDecl ds of
adamc@76 3616 NONE => ()
adamc@76 3617 | SOME loc =>
adamc@91 3618 declError env (CunifsRemain ds)
adamc@76 3619 );
adamc@76 3620
adamc@83 3621 (ds, (env, gs))
adamc@76 3622 end
adamc@76 3623
adamc@83 3624 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 3625 in
adamc@84 3626 if ErrorMsg.anyErrors () then
adamc@84 3627 ()
adamc@84 3628 else
adamc@228 3629 app (fn Disjoint (loc, env, denv, c1, c2) =>
adamc@228 3630 (case D.prove env denv (c1, c2, loc) of
adamc@228 3631 [] => ()
adamc@228 3632 | _ =>
adamc@228 3633 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adamc@228 3634 eprefaces' [("Con 1", p_con env c1),
adamc@228 3635 ("Con 2", p_con env c2),
adamc@228 3636 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
adamc@228 3637 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))]))
adamc@228 3638 | TypeClass (env, c, r, loc) =>
adamc@349 3639 let
adamc@467 3640 val c = normClassKey env c
adamc@349 3641 in
adamc@349 3642 case E.resolveClass env c of
adamc@349 3643 SOME e => r := SOME e
adamc@349 3644 | NONE => expError env (Unresolvable (loc, c))
adamc@349 3645 end) gs;
adamc@83 3646
adamc@325 3647 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan)
adamc@325 3648 :: ds
adamc@325 3649 @ (L'.DStr ("Top", top_n, topSgn, topStr), ErrorMsg.dummySpan)
adamc@325 3650 :: ds' @ file
adamc@56 3651 end
adamc@2 3652
adamc@2 3653 end