annotate src/elaborate.sml @ 719:5c099b1308ae

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