annotate src/elaborate.sml @ 711:7292bcb7c02d

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