annotate src/elaborate.sml @ 1586:03ad79980b55

Remove debug print
author Adam Chlipala <adam@chlipala.net>
date Wed, 02 Nov 2011 09:37:20 -0400
parents d5fb78321cca
children 65d6488c82d5
rev   line source
adam@1529 1 (* Copyright (c) 2008-2011, 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
adam@1531 41 val dumpTypes = ref false
adam@1531 42
adamc@819 43 structure IS = IntBinarySet
adamc@403 44 structure IM = IntBinaryMap
adamc@403 45
adamc@403 46 structure SK = struct
adamc@403 47 type ord_key = string
adamc@403 48 val compare = String.compare
adamc@403 49 end
adamc@403 50
adamc@403 51 structure SS = BinarySetFn(SK)
adamc@403 52 structure SM = BinaryMapFn(SK)
adamc@403 53
adamc@403 54 val basis_r = ref 0
adamc@633 55 val top_r = ref 0
adamc@403 56
adamc@403 57 fun elabExplicitness e =
adamc@403 58 case e of
adamc@403 59 L.Explicit => L'.Explicit
adamc@403 60 | L.Implicit => L'.Implicit
adamc@403 61
adamc@403 62 fun occursKind r =
adamc@403 63 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
adamc@403 64 | _ => false)
adamc@403 65
adamc@403 66 exception KUnify' of kunify_error
adamc@403 67
adamc@623 68 fun unifyKinds' env (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@403 69 let
adamc@403 70 fun err f = raise KUnify' (f (k1All, k2All))
adamc@403 71 in
adamc@403 72 case (k1, k2) of
adamc@403 73 (L'.KType, L'.KType) => ()
adamc@403 74 | (L'.KUnit, L'.KUnit) => ()
adamc@403 75
adamc@403 76 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@623 77 (unifyKinds' env d1 d2;
adamc@623 78 unifyKinds' env r1 r2)
adamc@403 79 | (L'.KName, L'.KName) => ()
adamc@623 80 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' env k1 k2
adamc@403 81 | (L'.KTuple ks1, L'.KTuple ks2) =>
adamc@623 82 ((ListPair.appEq (fn (k1, k2) => unifyKinds' env k1 k2) (ks1, ks2))
adamc@403 83 handle ListPair.UnequalLengths => err KIncompatible)
adamc@403 84
adamc@623 85 | (L'.KRel n1, L'.KRel n2) =>
adamc@623 86 if n1 = n2 then
adamc@623 87 ()
adamc@623 88 else
adamc@623 89 err KIncompatible
adamc@623 90 | (L'.KFun (x, k1), L'.KFun (_, k2)) =>
adamc@623 91 unifyKinds' (E.pushKRel env x) k1 k2
adamc@623 92
adamc@403 93 | (L'.KError, _) => ()
adamc@403 94 | (_, L'.KError) => ()
adamc@403 95
adamc@623 96 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' env k1All k2All
adamc@623 97 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' env k1All k2All
adamc@403 98
adam@1302 99 | (L'.KTupleUnif (_, _, ref (SOME k)), _) => unifyKinds' env k k2All
adam@1302 100 | (_, L'.KTupleUnif (_, _, ref (SOME k))) => unifyKinds' env k1All k
adam@1302 101
adamc@403 102 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
adamc@403 103 if r1 = r2 then
adamc@403 104 ()
adamc@403 105 else
adamc@403 106 r1 := SOME k2All
adamc@403 107
adamc@403 108 | (L'.KUnif (_, _, r), _) =>
adamc@403 109 if occursKind r k2All then
adamc@403 110 err KOccursCheckFailed
adamc@403 111 else
adamc@403 112 r := SOME k2All
adamc@403 113 | (_, L'.KUnif (_, _, r)) =>
adamc@403 114 if occursKind r k1All then
adamc@403 115 err KOccursCheckFailed
adamc@403 116 else
adamc@403 117 r := SOME k1All
adamc@403 118
adam@1302 119 | (L'.KTupleUnif (_, nks, r), L'.KTuple ks) =>
adam@1302 120 ((app (fn (n, k) => unifyKinds' env k (List.nth (ks, n-1))) nks;
adam@1302 121 r := SOME k2All)
adam@1302 122 handle Subscript => err KIncompatible)
adam@1302 123 | (L'.KTuple ks, L'.KTupleUnif (_, nks, r)) =>
adam@1302 124 ((app (fn (n, k) => unifyKinds' env (List.nth (ks, n-1)) k) nks;
adam@1302 125 r := SOME k1All)
adam@1302 126 handle Subscript => err KIncompatible)
adam@1302 127 | (L'.KTupleUnif (loc, nks1, r1), L'.KTupleUnif (_, nks2, r2)) =>
adam@1302 128 let
adam@1302 129 val nks = foldl (fn (p as (n, k1), nks) =>
adam@1302 130 case ListUtil.search (fn (n', k2) =>
adam@1302 131 if n' = n then
adam@1302 132 SOME k2
adam@1302 133 else
adam@1302 134 NONE) nks2 of
adam@1302 135 NONE => p :: nks
adam@1302 136 | SOME k2 => (unifyKinds' env k1 k2;
adam@1302 137 nks)) nks2 nks1
adam@1302 138
adam@1302 139 val k = (L'.KTupleUnif (loc, nks, ref NONE), loc)
adam@1302 140 in
adam@1302 141 r1 := SOME k;
adam@1302 142 r2 := SOME k
adam@1302 143 end
adam@1302 144
adamc@403 145 | _ => err KIncompatible
adamc@403 146 end
adamc@403 147
adamc@403 148 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@403 149
adamc@623 150 fun unifyKinds env k1 k2 =
adamc@623 151 unifyKinds' env k1 k2
adamc@403 152 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@403 153
adamc@403 154 fun checkKind env c k1 k2 =
adamc@623 155 unifyKinds env k1 k2
adamc@403 156 handle KUnify (k1, k2, err) =>
adamc@403 157 conError env (WrongKind (c, k1, k2, err))
adamc@403 158
adamc@403 159 val dummy = ErrorMsg.dummySpan
adamc@403 160
adamc@403 161 val ktype = (L'.KType, dummy)
adamc@403 162 val kname = (L'.KName, dummy)
adamc@403 163 val ktype_record = (L'.KRecord ktype, dummy)
adamc@403 164
adamc@403 165 val cerror = (L'.CError, dummy)
adamc@403 166 val kerror = (L'.KError, dummy)
adamc@403 167 val eerror = (L'.EError, dummy)
adamc@403 168 val sgnerror = (L'.SgnError, dummy)
adamc@403 169 val strerror = (L'.StrError, dummy)
adamc@403 170
adamc@403 171 val int = ref cerror
adamc@403 172 val float = ref cerror
adamc@403 173 val string = ref cerror
adamc@821 174 val char = ref cerror
adamc@403 175 val table = ref cerror
adamc@403 176
adamc@403 177 local
adamc@403 178 val count = ref 0
adamc@403 179 in
adamc@403 180
adamc@403 181 fun resetKunif () = count := 0
adamc@403 182
adamc@403 183 fun kunif loc =
adamc@403 184 let
adamc@403 185 val n = !count
adamc@403 186 val s = if n <= 26 then
adamc@403 187 str (chr (ord #"A" + n))
adamc@403 188 else
adamc@403 189 "U" ^ Int.toString (n - 26)
adamc@403 190 in
adamc@403 191 count := n + 1;
adamc@838 192 (L'.KUnif (loc, s, ref NONE), loc)
adamc@403 193 end
adamc@403 194
adamc@403 195 end
adamc@403 196
adamc@403 197 local
adamc@403 198 val count = ref 0
adamc@403 199 in
adamc@403 200
adamc@403 201 fun resetCunif () = count := 0
adamc@403 202
adamc@403 203 fun cunif (loc, k) =
adamc@403 204 let
adamc@403 205 val n = !count
adamc@712 206 val s = if n < 26 then
adamc@403 207 str (chr (ord #"A" + n))
adamc@403 208 else
adamc@403 209 "U" ^ Int.toString (n - 26)
adamc@403 210 in
adamc@403 211 count := n + 1;
adam@1303 212 (L'.CUnif (0, loc, k, s, ref NONE), loc)
adamc@403 213 end
adamc@403 214
adamc@403 215 end
adamc@403 216
adamc@623 217 fun elabKind env (k, loc) =
adamc@403 218 case k of
adamc@403 219 L.KType => (L'.KType, loc)
adamc@623 220 | L.KArrow (k1, k2) => (L'.KArrow (elabKind env k1, elabKind env k2), loc)
adamc@403 221 | L.KName => (L'.KName, loc)
adamc@623 222 | L.KRecord k => (L'.KRecord (elabKind env k), loc)
adamc@403 223 | L.KUnit => (L'.KUnit, loc)
adamc@623 224 | L.KTuple ks => (L'.KTuple (map (elabKind env) ks), loc)
adamc@403 225 | L.KWild => kunif loc
adamc@403 226
adamc@623 227 | L.KVar s => (case E.lookupK env s of
adamc@623 228 NONE =>
adamc@623 229 (kindError env (UnboundKind (loc, s));
adamc@623 230 kerror)
adamc@623 231 | SOME n => (L'.KRel n, loc))
adamc@623 232 | L.KFun (x, k) => (L'.KFun (x, elabKind (E.pushKRel env x) k), loc)
adamc@623 233
adamc@621 234 fun mapKind (dom, ran, loc)=
adamc@621 235 (L'.KArrow ((L'.KArrow (dom, ran), loc),
adamc@621 236 (L'.KArrow ((L'.KRecord dom, loc),
adamc@621 237 (L'.KRecord ran, loc)), loc)), loc)
adamc@403 238
adamc@403 239 fun hnormKind (kAll as (k, _)) =
adamc@403 240 case k of
adamc@403 241 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@403 242 | _ => kAll
adamc@403 243
adamc@623 244 open ElabOps
adamc@623 245
adamc@623 246 fun elabConHead (c as (_, loc)) k =
adamc@623 247 let
adamc@623 248 fun unravel (k, c) =
adamc@623 249 case hnormKind k of
adamc@623 250 (L'.KFun (x, k'), _) =>
adamc@623 251 let
adamc@623 252 val u = kunif loc
adamc@623 253
adamc@623 254 val k'' = subKindInKind (0, u) k'
adamc@623 255 in
adamc@623 256 unravel (k'', (L'.CKApp (c, u), loc))
adamc@623 257 end
adamc@623 258 | _ => (c, k)
adamc@623 259 in
adamc@623 260 unravel (k, c)
adamc@623 261 end
adamc@623 262
adamc@403 263 fun elabCon (env, denv) (c, loc) =
adamc@403 264 case c of
adamc@403 265 L.CAnnot (c, k) =>
adamc@403 266 let
adamc@623 267 val k' = elabKind env k
adamc@403 268 val (c', ck, gs) = elabCon (env, denv) c
adamc@403 269 in
adamc@403 270 checkKind env c' ck k';
adamc@403 271 (c', k', gs)
adamc@403 272 end
adamc@403 273
adamc@403 274 | L.TFun (t1, t2) =>
adamc@403 275 let
adamc@403 276 val (t1', k1, gs1) = elabCon (env, denv) t1
adamc@403 277 val (t2', k2, gs2) = elabCon (env, denv) t2
adamc@403 278 in
adamc@403 279 checkKind env t1' k1 ktype;
adamc@403 280 checkKind env t2' k2 ktype;
adamc@403 281 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
adamc@403 282 end
adamc@403 283 | L.TCFun (e, x, k, t) =>
adamc@403 284 let
adamc@403 285 val e' = elabExplicitness e
adamc@623 286 val k' = elabKind env k
adamc@403 287 val env' = E.pushCRel env x k'
adamc@403 288 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@403 289 in
adamc@403 290 checkKind env t' tk ktype;
adamc@403 291 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
adamc@403 292 end
adamc@623 293 | L.TKFun (x, t) =>
adamc@623 294 let
adamc@623 295 val env' = E.pushKRel env x
adamc@623 296 val (t', tk, gs) = elabCon (env', denv) t
adamc@623 297 in
adamc@623 298 checkKind env t' tk ktype;
adamc@623 299 ((L'.TKFun (x, t'), loc), ktype, gs)
adamc@623 300 end
adamc@628 301 | L.TDisjoint (c1, c2, c) =>
adamc@403 302 let
adamc@403 303 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 304 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 305
adamc@403 306 val ku1 = kunif loc
adamc@403 307 val ku2 = kunif loc
adamc@403 308
adamc@628 309 val denv' = D.assert env denv (c1', c2')
adamc@403 310 val (c', k, gs4) = elabCon (env, denv') c
adamc@403 311 in
adamc@403 312 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@403 313 checkKind env c2' k2 (L'.KRecord ku2, loc);
adam@1351 314 checkKind env c' k (L'.KType, loc);
adamc@403 315
adamc@628 316 ((L'.TDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs4)
adamc@403 317 end
adamc@403 318 | L.TRecord c =>
adamc@403 319 let
adamc@403 320 val (c', ck, gs) = elabCon (env, denv) c
adamc@403 321 val k = (L'.KRecord ktype, loc)
adamc@403 322 in
adamc@403 323 checkKind env c' ck k;
adamc@403 324 ((L'.TRecord c', loc), ktype, gs)
adamc@403 325 end
adamc@403 326
adamc@403 327 | L.CVar ([], s) =>
adamc@403 328 (case E.lookupC env s of
adamc@403 329 E.NotBound =>
adamc@403 330 (conError env (UnboundCon (loc, s));
adamc@403 331 (cerror, kerror, []))
adamc@403 332 | E.Rel (n, k) =>
adamc@623 333 let
adamc@623 334 val (c, k) = elabConHead (L'.CRel n, loc) k
adamc@623 335 in
adamc@623 336 (c, k, [])
adamc@623 337 end
adamc@403 338 | E.Named (n, k) =>
adamc@623 339 let
adamc@623 340 val (c, k) = elabConHead (L'.CNamed n, loc) k
adamc@623 341 in
adamc@623 342 (c, k, [])
adamc@623 343 end)
adamc@403 344 | L.CVar (m1 :: ms, s) =>
adamc@403 345 (case E.lookupStr env m1 of
adamc@403 346 NONE => (conError env (UnboundStrInCon (loc, m1));
adamc@403 347 (cerror, kerror, []))
adamc@403 348 | SOME (n, sgn) =>
adamc@403 349 let
adamc@403 350 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@403 351 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@403 352 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@403 353 (strerror, sgnerror))
adamc@403 354 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@403 355 ((L'.StrVar n, loc), sgn) ms
adamc@403 356
adamc@403 357 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
adamc@403 358 NONE => (conError env (UnboundCon (loc, s));
adamc@403 359 kerror)
adamc@403 360 | SOME (k, _) => k
adamc@987 361 val (c, k) = elabConHead (L'.CModProj (n, ms, s), loc) k
adamc@403 362 in
adamc@987 363 (c, k, [])
adamc@403 364 end)
adamc@403 365
adamc@403 366 | L.CApp (c1, c2) =>
adamc@403 367 let
adamc@403 368 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 369 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 370 val dom = kunif loc
adamc@403 371 val ran = kunif loc
adamc@403 372 in
adamc@403 373 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@403 374 checkKind env c2' k2 dom;
adamc@403 375 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
adamc@403 376 end
adamc@403 377 | L.CAbs (x, ko, t) =>
adamc@403 378 let
adamc@403 379 val k' = case ko of
adamc@403 380 NONE => kunif loc
adamc@623 381 | SOME k => elabKind env k
adamc@403 382 val env' = E.pushCRel env x k'
adamc@403 383 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@403 384 in
adamc@403 385 ((L'.CAbs (x, k', t'), loc),
adamc@403 386 (L'.KArrow (k', tk), loc),
adamc@403 387 gs)
adamc@403 388 end
adamc@623 389 | L.CKAbs (x, t) =>
adamc@623 390 let
adamc@623 391 val env' = E.pushKRel env x
adamc@623 392 val (t', tk, gs) = elabCon (env', denv) t
adamc@623 393 in
adamc@623 394 ((L'.CKAbs (x, t'), loc),
adamc@623 395 (L'.KFun (x, tk), loc),
adamc@623 396 gs)
adamc@623 397 end
adamc@403 398
adamc@403 399 | L.CName s =>
adamc@403 400 ((L'.CName s, loc), kname, [])
adamc@403 401
adamc@403 402 | L.CRecord xcs =>
adamc@403 403 let
adamc@403 404 val k = kunif loc
adamc@403 405
adamc@403 406 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
adamc@403 407 let
adamc@403 408 val (x', xk, gs1) = elabCon (env, denv) x
adamc@403 409 val (c', ck, gs2) = elabCon (env, denv) c
adamc@403 410 in
adamc@403 411 checkKind env x' xk kname;
adamc@403 412 checkKind env c' ck k;
adamc@403 413 ((x', c'), gs1 @ gs2 @ gs)
adamc@403 414 end) [] xcs
adamc@403 415
adamc@403 416 val rc = (L'.CRecord (k, xcs'), loc)
adamc@403 417 (* Add duplicate field checking later. *)
adamc@403 418
adamc@403 419 fun prove (xcs, ds) =
adamc@403 420 case xcs of
adamc@403 421 [] => ds
adamc@403 422 | xc :: rest =>
adamc@403 423 let
adamc@403 424 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@403 425 val ds = foldl (fn (xc', ds) =>
adamc@403 426 let
adamc@403 427 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@403 428 in
adamc@403 429 D.prove env denv (r1, r2, loc) @ ds
adamc@403 430 end)
adamc@403 431 ds rest
adamc@403 432 in
adamc@403 433 prove (rest, ds)
adamc@403 434 end
adamc@403 435 in
adamc@403 436 (rc, (L'.KRecord k, loc), prove (xcs', gs))
adamc@403 437 end
adamc@403 438 | L.CConcat (c1, c2) =>
adamc@403 439 let
adamc@403 440 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 441 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 442 val ku = kunif loc
adamc@403 443 val k = (L'.KRecord ku, loc)
adamc@403 444 in
adamc@403 445 checkKind env c1' k1 k;
adamc@403 446 checkKind env c2' k2 k;
adamc@403 447 ((L'.CConcat (c1', c2'), loc), k,
adamc@403 448 D.prove env denv (c1', c2', loc) @ gs1 @ gs2)
adamc@403 449 end
adamc@621 450 | L.CMap =>
adamc@403 451 let
adamc@403 452 val dom = kunif loc
adamc@403 453 val ran = kunif loc
adamc@403 454 in
adamc@621 455 ((L'.CMap (dom, ran), loc),
adamc@621 456 mapKind (dom, ran, loc),
adamc@403 457 [])
adamc@403 458 end
adamc@403 459
adamc@403 460 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
adamc@403 461
adamc@403 462 | L.CTuple cs =>
adamc@403 463 let
adamc@403 464 val (cs', ks, gs) = foldl (fn (c, (cs', ks, gs)) =>
adamc@403 465 let
adamc@403 466 val (c', k, gs') = elabCon (env, denv) c
adamc@403 467 in
adamc@403 468 (c' :: cs', k :: ks, gs' @ gs)
adamc@403 469 end) ([], [], []) cs
adamc@403 470 in
adamc@403 471 ((L'.CTuple (rev cs'), loc), (L'.KTuple (rev ks), loc), gs)
adamc@403 472 end
adamc@403 473 | L.CProj (c, n) =>
adamc@403 474 let
adamc@403 475 val (c', k, gs) = elabCon (env, denv) c
adam@1302 476
adam@1302 477 val k' = kunif loc
adamc@403 478 in
adam@1302 479 if n <= 0 then
adam@1302 480 (conError env (ProjBounds (c', n));
adam@1302 481 (cerror, kerror, []))
adam@1302 482 else
adam@1302 483 (checkKind env c' k (L'.KTupleUnif (loc, [(n, k')], ref NONE), loc);
adam@1302 484 ((L'.CProj (c', n), loc), k', gs))
adamc@403 485 end
adamc@403 486
adamc@403 487 | L.CWild k =>
adamc@403 488 let
adamc@623 489 val k' = elabKind env k
adamc@403 490 in
adamc@403 491 (cunif (loc, k'), k', [])
adamc@403 492 end
adamc@403 493
adamc@403 494 fun kunifsRemain k =
adamc@403 495 case k of
adamc@403 496 L'.KUnif (_, _, ref NONE) => true
adam@1302 497 | L'.KTupleUnif (_, _, ref NONE) => true
adamc@403 498 | _ => false
adamc@403 499 fun cunifsRemain c =
adamc@403 500 case c of
adam@1326 501 L'.CUnif (_, loc, k, _, r as ref NONE) =>
adam@1326 502 (case #1 (hnormKind k) of
adam@1584 503 L'.KUnit => (r := SOME (L'.CUnit, loc); false)
adam@1584 504 | _ => true)
adam@1584 505 | _ => false
adamc@403 506
adamc@403 507 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
adamc@403 508 con = fn _ => false,
adamc@403 509 exp = fn _ => false,
adamc@403 510 sgn_item = fn _ => false,
adamc@403 511 sgn = fn _ => false,
adamc@403 512 str = fn _ => false,
adamc@403 513 decl = fn _ => false}
adamc@403 514
adam@1584 515 val cunifsInDecl = U.Decl.exists {kind = fn _ => false,
adamc@403 516 con = cunifsRemain,
adam@1584 517 exp = fn _ => false,
adam@1584 518 sgn_item = fn _ => false,
adam@1584 519 sgn = fn _ => false,
adam@1584 520 str = fn _ => false,
adam@1584 521 decl = fn _ => false}
adamc@403 522
adamc@403 523 fun occursCon r =
adamc@403 524 U.Con.exists {kind = fn _ => false,
adam@1303 525 con = fn L'.CUnif (_, _, _, _, r') => r = r'
adamc@403 526 | _ => false}
adamc@403 527
adamc@403 528 exception CUnify' of cunify_error
adamc@403 529
adamc@403 530 type record_summary = {
adamc@403 531 fields : (L'.con * L'.con) list,
adamc@403 532 unifs : (L'.con * L'.con option ref) list,
adamc@403 533 others : L'.con list
adamc@403 534 }
adamc@403 535
adamc@403 536 fun summaryToCon {fields, unifs, others} =
adamc@403 537 let
adamc@403 538 val c = (L'.CRecord (ktype, []), dummy)
adamc@403 539 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@403 540 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@403 541 in
adamc@403 542 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@403 543 end
adamc@403 544
adamc@403 545 fun p_summary env s = p_con env (summaryToCon s)
adamc@403 546
adamc@403 547 exception CUnify of L'.con * L'.con * cunify_error
adamc@403 548
adamc@403 549 fun kindof env (c, loc) =
adamc@403 550 case c of
adamc@403 551 L'.TFun _ => ktype
adamc@403 552 | L'.TCFun _ => ktype
adamc@403 553 | L'.TRecord _ => ktype
adamc@628 554 | L'.TDisjoint _ => ktype
adamc@403 555
adamc@403 556 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@403 557 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@403 558 | L'.CModProj (n, ms, x) =>
adamc@403 559 let
adamc@403 560 val (_, sgn) = E.lookupStrNamed env n
adamc@403 561 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@403 562 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@403 563 NONE => raise Fail "kindof: Unknown substructure"
adamc@403 564 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@403 565 ((L'.StrVar n, loc), sgn) ms
adamc@403 566 in
adamc@403 567 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@403 568 NONE => raise Fail "kindof: Unknown con in structure"
adamc@403 569 | SOME (k, _) => k
adamc@403 570 end
adamc@403 571
adamc@403 572 | L'.CApp (c, _) =>
adamc@403 573 (case hnormKind (kindof env c) of
adamc@403 574 (L'.KArrow (_, k), _) => k
adamc@403 575 | (L'.KError, _) => kerror
adamc@413 576 | k => raise CUnify' (CKindof (k, c, "arrow")))
adamc@403 577 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@628 578
adamc@403 579
adamc@403 580 | L'.CName _ => kname
adamc@403 581
adamc@403 582 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@403 583 | L'.CConcat (c, _) => kindof env c
adamc@621 584 | L'.CMap (dom, ran) => mapKind (dom, ran, loc)
adamc@403 585
adamc@403 586 | L'.CUnit => (L'.KUnit, loc)
adamc@403 587
adamc@403 588 | L'.CTuple cs => (L'.KTuple (map (kindof env) cs), loc)
adamc@403 589 | L'.CProj (c, n) =>
adamc@403 590 (case hnormKind (kindof env c) of
adamc@403 591 (L'.KTuple ks, _) => List.nth (ks, n - 1)
adam@1311 592 | (L'.KUnif (_, _, r), _) =>
adam@1311 593 let
adam@1311 594 val ku = kunif loc
adam@1311 595 val k = (L'.KTupleUnif (loc, [(n, ku)], ref NONE), loc)
adam@1311 596 in
adam@1311 597 r := SOME k;
adam@1311 598 k
adam@1311 599 end
adam@1311 600 | (L'.KTupleUnif (_, nks, r), _) =>
adam@1311 601 (case ListUtil.search (fn (n', k) => if n' = n then SOME k else NONE) nks of
adam@1311 602 SOME k => k
adam@1311 603 | NONE =>
adam@1311 604 let
adam@1311 605 val ku = kunif loc
adam@1311 606 val k = (L'.KTupleUnif (loc, ((n, ku) :: nks), ref NONE), loc)
adam@1311 607 in
adam@1311 608 r := SOME k;
adam@1311 609 k
adam@1311 610 end)
adamc@413 611 | k => raise CUnify' (CKindof (k, c, "tuple")))
adamc@403 612
adamc@403 613 | L'.CError => kerror
adam@1303 614 | L'.CUnif (_, _, k, _, _) => k
adamc@403 615
adamc@623 616 | L'.CKAbs (x, c) => (L'.KFun (x, kindof (E.pushKRel env x) c), loc)
adamc@623 617 | L'.CKApp (c, k) =>
adamc@623 618 (case hnormKind (kindof env c) of
adamc@623 619 (L'.KFun (_, k'), _) => subKindInKind (0, k) k'
adamc@623 620 | k => raise CUnify' (CKindof (k, c, "kapp")))
adamc@623 621 | L'.TKFun _ => ktype
adamc@403 622
adamc@621 623 exception GuessFailure
adamc@403 624
adamc@513 625 fun isUnitCon env (c, loc) =
adamc@513 626 case c of
adamc@513 627 L'.TFun _ => false
adamc@513 628 | L'.TCFun _ => false
adamc@513 629 | L'.TRecord _ => false
adamc@628 630 | L'.TDisjoint _ => false
adamc@513 631
adam@1316 632 | L'.CRel xn => #1 (hnormKind (#2 (E.lookupCRel env xn))) = L'.KUnit
adam@1316 633 | L'.CNamed xn => #1 (hnormKind (#2 (E.lookupCNamed env xn))) = L'.KUnit
adamc@518 634 | L'.CModProj (n, ms, x) => false
adamc@518 635 (*let
adamc@513 636 val (_, sgn) = E.lookupStrNamed env n
adamc@513 637 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@513 638 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@513 639 NONE => raise Fail "kindof: Unknown substructure"
adamc@513 640 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@513 641 ((L'.StrVar n, loc), sgn) ms
adamc@513 642 in
adamc@513 643 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@513 644 NONE => raise Fail "kindof: Unknown con in structure"
adamc@513 645 | SOME ((k, _), _) => k = L'.KUnit
adamc@518 646 end*)
adamc@518 647
adamc@518 648 | L'.CApp (c, _) => false
adamc@518 649 (*(case hnormKind (kindof env c) of
adamc@513 650 (L'.KArrow (_, k), _) => #1 k = L'.KUnit
adamc@513 651 | (L'.KError, _) => false
adamc@518 652 | k => raise CUnify' (CKindof (k, c, "arrow")))*)
adamc@513 653 | L'.CAbs _ => false
adamc@513 654
adamc@513 655 | L'.CName _ => false
adamc@513 656
adamc@513 657 | L'.CRecord _ => false
adamc@513 658 | L'.CConcat _ => false
adamc@621 659 | L'.CMap _ => false
adamc@513 660
adamc@513 661 | L'.CUnit => true
adamc@513 662
adamc@513 663 | L'.CTuple _ => false
adamc@518 664 | L'.CProj (c, n) => false
adamc@518 665 (*(case hnormKind (kindof env c) of
adamc@513 666 (L'.KTuple ks, _) => #1 (List.nth (ks, n - 1)) = L'.KUnit
adamc@518 667 | k => raise CUnify' (CKindof (k, c, "tuple")))*)
adamc@513 668
adamc@513 669 | L'.CError => false
adam@1316 670 | L'.CUnif (_, _, k, _, _) => #1 (hnormKind k) = L'.KUnit
adamc@513 671
adamc@623 672 | L'.CKAbs _ => false
adamc@623 673 | L'.CKApp _ => false
adamc@623 674 | L'.TKFun _ => false
adamc@623 675
adamc@709 676 val recdCounter = ref 0
adamc@709 677
adamc@713 678 val mayDelay = ref false
adamc@777 679 val delayedUnifs = ref ([] : (ErrorMsg.span * E.env * L'.kind * record_summary * record_summary) list)
adamc@777 680
adamc@830 681 val delayedExhaustives = ref ([] : (E.env * L'.con * L'.pat list * ErrorMsg.span) list)
adamc@830 682
adam@1304 683 exception CantSquish
adam@1304 684
adam@1304 685 fun squish by =
adam@1304 686 U.Con.mapB {kind = fn _ => fn k => k,
adam@1304 687 con = fn bound => fn c =>
adam@1304 688 case c of
adam@1304 689 L'.CRel xn =>
adam@1304 690 if xn < bound then
adam@1304 691 c
adam@1304 692 else if bound <= xn andalso xn < bound + by then
adam@1304 693 raise CantSquish
adam@1304 694 else
adam@1304 695 L'.CRel (xn - by)
adam@1304 696 | L'.CUnif _ => raise CantSquish
adam@1304 697 | _ => c,
adam@1304 698 bind = fn (bound, U.Con.RelC _) => bound + 1
adam@1304 699 | (bound, _) => bound} 0
adam@1304 700
adam@1582 701 val reducedSummaries = ref (NONE : (Print.PD.pp_desc * Print.PD.pp_desc) option)
adam@1582 702
adamc@777 703 fun unifyRecordCons env (loc, c1, c2) =
adamc@403 704 let
adamc@403 705 fun rkindof c =
adamc@403 706 case hnormKind (kindof env c) of
adamc@403 707 (L'.KRecord k, _) => k
adamc@403 708 | (L'.KError, _) => kerror
adamc@753 709 | (L'.KUnif (_, _, r), _) =>
adamc@753 710 let
adamc@753 711 val k = kunif (#2 c)
adamc@753 712 in
adamc@753 713 r := SOME (L'.KRecord k, #2 c);
adamc@753 714 k
adamc@753 715 end
adamc@413 716 | k => raise CUnify' (CKindof (k, c, "record"))
adamc@403 717
adamc@403 718 val k1 = rkindof c1
adamc@403 719 val k2 = rkindof c2
adamc@403 720
adamc@628 721 val r1 = recordSummary env c1
adamc@628 722 val r2 = recordSummary env c2
adamc@403 723 in
adamc@623 724 unifyKinds env k1 k2;
adamc@777 725 unifySummaries env (loc, k1, r1, r2)
adamc@403 726 end
adamc@403 727
adamc@777 728 and normalizeRecordSummary env (r : record_summary) =
adamc@777 729 recordSummary env (summaryToCon r)
adamc@777 730
adamc@628 731 and recordSummary env c =
adamc@403 732 let
adamc@628 733 val c = hnormCon env c
adamc@628 734
adamc@628 735 val sum =
adamc@403 736 case c of
adamc@628 737 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
adamc@403 738 | (L'.CConcat (c1, c2), _) =>
adamc@403 739 let
adamc@628 740 val s1 = recordSummary env c1
adamc@628 741 val s2 = recordSummary env c2
adamc@403 742 in
adamc@628 743 {fields = #fields s1 @ #fields s2,
adamc@628 744 unifs = #unifs s1 @ #unifs s2,
adamc@628 745 others = #others s1 @ #others s2}
adamc@403 746 end
adam@1303 747 | (L'.CUnif (nl, _, _, _, ref (SOME c)), _) => recordSummary env (E.mliftConInCon nl c)
adam@1303 748 | c' as (L'.CUnif (0, _, _, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
adamc@628 749 | c' => {fields = [], unifs = [], others = [c']}
adamc@403 750 in
adamc@628 751 sum
adamc@403 752 end
adamc@403 753
adamc@987 754 and consEq env loc (c1, c2) =
adamc@777 755 let
adamc@777 756 val mayDelay' = !mayDelay
adamc@777 757 in
adamc@777 758 (mayDelay := false;
adamc@987 759 unifyCons env loc c1 c2;
adamc@777 760 mayDelay := mayDelay';
adamc@777 761 true)
adamc@777 762 handle CUnify _ => (mayDelay := mayDelay'; false)
adamc@777 763 end
adamc@403 764
adamc@403 765 and consNeq env (c1, c2) =
adamc@628 766 case (#1 (hnormCon env c1), #1 (hnormCon env c2)) of
adamc@403 767 (L'.CName x1, L'.CName x2) => x1 <> x2
adamc@1093 768 | (L'.CName _, L'.CRel _) => true
adamc@1093 769 | (L'.CRel _, L'.CName _) => true
adamc@1093 770 | (L'.CRel n1, L'.CRel n2) => n1 <> n2
adamc@1093 771 | (L'.CRel _, L'.CNamed _) => true
adamc@1093 772 | (L'.CNamed _, L'.CRel _) => true
adamc@1093 773 | (L'.CRel _, L'.CModProj _) => true
adamc@1093 774 | (L'.CModProj _, L'.CRel _) => true
adamc@1269 775 | (L'.CModProj (_, _, n1), L'.CModProj (_, _, n2)) => n1 <> n2
adamc@403 776 | _ => false
adamc@403 777
adamc@777 778 and unifySummaries env (loc, k, s1 : record_summary, s2 : record_summary) =
adamc@403 779 let
adam@1583 780 val () = reducedSummaries := NONE
adam@1583 781
adamc@848 782 (*val () = eprefaces "Summaries" [("loc", PD.string (ErrorMsg.spanToString loc)),
adam@1303 783 ("#1", p_summary env s1),
adam@1303 784 ("#2", p_summary env s2)]*)
adamc@403 785
adamc@403 786 fun eatMatching p (ls1, ls2) =
adamc@403 787 let
adamc@403 788 fun em (ls1, ls2, passed1) =
adamc@403 789 case ls1 of
adamc@403 790 [] => (rev passed1, ls2)
adamc@403 791 | h1 :: t1 =>
adamc@403 792 let
adamc@403 793 fun search (ls2', passed2) =
adamc@403 794 case ls2' of
adamc@403 795 [] => em (t1, ls2, h1 :: passed1)
adamc@403 796 | h2 :: t2 =>
adamc@403 797 if p (h1, h2) then
adamc@403 798 em (t1, List.revAppend (passed2, t2), passed1)
adamc@403 799 else
adamc@403 800 search (t2, h2 :: passed2)
adamc@403 801 in
adamc@403 802 search (ls2, [])
adamc@403 803 end
adamc@403 804 in
adamc@403 805 em (ls1, ls2, [])
adamc@403 806 end
adamc@403 807
adamc@403 808 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@403 809 not (consNeq env (x1, x2))
adamc@987 810 andalso consEq env loc (c1, c2)
adamc@987 811 andalso consEq env loc (x1, x2))
adamc@403 812 (#fields s1, #fields s2)
adamc@403 813 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@403 814 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@646 815
adamc@403 816 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@646 817
adamc@987 818 val (others1, others2) = eatMatching (consEq env loc) (#others s1, #others s2)
adamc@403 819 (*val () = eprefaces "Summaries3" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@403 820 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 821
adamc@709 822 fun unsummarize {fields, unifs, others} =
adamc@709 823 let
adamc@709 824 val c = (L'.CRecord (k, fields), loc)
adamc@709 825
adamc@709 826 val c = foldl (fn ((c1, _), c2) => (L'.CConcat (c1, c2), loc))
adamc@709 827 c unifs
adamc@709 828 in
adamc@709 829 foldl (fn (c1, c2) => (L'.CConcat (c1, c2), loc))
adamc@709 830 c others
adamc@709 831 end
adamc@709 832
adamc@710 833 val empties = ([], [], [], [], [], [])
adamc@710 834
adamc@709 835 val (unifs1, fs1, others1, unifs2, fs2, others2) =
adamc@709 836 case (unifs1, fs1, others1, unifs2, fs2, others2) of
adamc@709 837 orig as ([(_, r)], [], [], _, _, _) =>
adamc@709 838 let
adamc@709 839 val c = unsummarize {fields = fs2, others = others2, unifs = unifs2}
adamc@709 840 in
adamc@709 841 if occursCon r c then
adamc@709 842 orig
adamc@709 843 else
adamc@709 844 (r := SOME c;
adamc@710 845 empties)
adamc@709 846 end
adamc@709 847 | orig as (_, _, _, [(_, r)], [], []) =>
adamc@709 848 let
adamc@709 849 val c = unsummarize {fields = fs1, others = others1, unifs = unifs1}
adamc@709 850 in
adamc@709 851 if occursCon r c then
adamc@709 852 orig
adamc@709 853 else
adamc@709 854 (r := SOME c;
adamc@710 855 empties)
adamc@709 856 end
adamc@710 857 | orig as ([(_, r1 as ref NONE)], _, [], [(_, r2 as ref NONE)], _, []) =>
adamc@710 858 if List.all (fn (x1, _) => List.all (fn (x2, _) => consNeq env (x1, x2)) fs2) fs1 then
adamc@710 859 let
adamc@838 860 val kr = (L'.KRecord k, loc)
adamc@710 861 val u = cunif (loc, kr)
adamc@710 862 in
adamc@710 863 r1 := SOME (L'.CConcat ((L'.CRecord (k, fs2), loc), u), loc);
adamc@710 864 r2 := SOME (L'.CConcat ((L'.CRecord (k, fs1), loc), u), loc);
adamc@710 865 empties
adamc@710 866 end
adamc@710 867 else
adamc@710 868 orig
adamc@709 869 | orig => orig
adamc@709 870
adamc@403 871 (*val () = eprefaces "Summaries4" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@777 872 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 873
adamc@753 874 fun isGuessable (other, fs, unifs) =
adamc@753 875 let
adamc@753 876 val c = (L'.CRecord (k, fs), loc)
adamc@753 877 val c = foldl (fn ((c', _), c) => (L'.CConcat (c', c), loc)) c unifs
adamc@753 878 in
adamc@987 879 (guessMap env loc (other, c, GuessFailure);
adamc@753 880 true)
adamc@753 881 handle GuessFailure => false
adamc@753 882 end
adamc@403 883
adamc@754 884 val (fs1, fs2, others1, others2, unifs1, unifs2) =
adamc@753 885 case (fs1, fs2, others1, others2, unifs1, unifs2) of
adamc@753 886 ([], _, [other1], [], [], _) =>
adamc@753 887 if isGuessable (other1, fs2, unifs2) then
adamc@754 888 ([], [], [], [], [], [])
adamc@403 889 else
adamc@754 890 (fs1, fs2, others1, others2, unifs1, unifs2)
adamc@753 891 | (_, [], [], [other2], _, []) =>
adamc@777 892 if isGuessable (other2, fs1, unifs1) then
adamc@777 893 ([], [], [], [], [], [])
adamc@777 894 else
adamc@777 895 (fs1, fs2, others1, others2, unifs1, unifs2)
adamc@754 896 | _ => (fs1, fs2, others1, others2, unifs1, unifs2)
adamc@403 897
adam@1582 898 val () = if !mayDelay then
adam@1582 899 ()
adam@1582 900 else
adam@1582 901 reducedSummaries := SOME (p_summary env {fields = fs1, unifs = unifs1, others = others1},
adam@1582 902 p_summary env {fields = fs2, unifs = unifs2, others = others2})
adam@1582 903
adamc@403 904 (*val () = eprefaces "Summaries5" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@646 905 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 906
adamc@838 907 val empty = (L'.CRecord (k, []), loc)
adamc@1071 908 fun failure () =
adamc@1071 909 let
adamc@1071 910 val fs2 = #fields s2
adamc@1071 911
adamc@1071 912 fun findPointwise fs1 =
adamc@1071 913 case fs1 of
adamc@1071 914 [] => NONE
adamc@1071 915 | (nm1, c1) :: fs1 =>
adamc@1071 916 case List.find (fn (nm2, _) => consEq env loc (nm1, nm2)) fs2 of
adamc@1071 917 NONE => findPointwise fs1
adamc@1071 918 | SOME (_, c2) =>
adamc@1071 919 if consEq env loc (c1, c2) then
adamc@1071 920 findPointwise fs1
adamc@1071 921 else
adam@1359 922 SOME (nm1, c1, c2, (unifyCons env loc c1 c2; NONE) handle CUnify (_, _, err) => SOME err)
adamc@1071 923 in
adamc@1071 924 raise CUnify' (CRecordFailure (unsummarize s1, unsummarize s2, findPointwise (#fields s1)))
adamc@1071 925 end
adam@1341 926
adam@1341 927 fun default () = if !mayDelay then
adam@1341 928 delayedUnifs := (loc, env, k, s1, s2) :: !delayedUnifs
adam@1341 929 else
adam@1341 930 failure ()
adamc@709 931 in
adamc@777 932 (case (unifs1, fs1, others1, unifs2, fs2, others2) of
adamc@777 933 (_, [], [], [], [], []) =>
adamc@777 934 app (fn (_, r) => r := SOME empty) unifs1
adamc@777 935 | ([], [], [], _, [], []) =>
adamc@777 936 app (fn (_, r) => r := SOME empty) unifs2
adam@1341 937 | (_, _, _, [], [], [cr as (L'.CUnif (nl, _, _, _, r), _)]) =>
adam@1341 938 let
adam@1341 939 val c = summaryToCon {fields = fs1, unifs = unifs1, others = others1}
adam@1341 940 in
adam@1341 941 if occursCon r c then
adam@1341 942 raise CUnify' (COccursCheckFailed (cr, c))
adam@1341 943 else
adam@1341 944 (r := SOME (squish nl c))
adam@1341 945 handle CantSquish => default ()
adam@1341 946 end
adam@1341 947 | ([], [], [cr as (L'.CUnif (nl, _, _, _, r), _)], _, _, _) =>
adam@1341 948 let
adam@1341 949 val c = summaryToCon {fields = fs2, unifs = unifs2, others = others2}
adam@1341 950 in
adam@1341 951 if occursCon r c then
adam@1341 952 raise CUnify' (COccursCheckFailed (cr, c))
adam@1341 953 else
adam@1341 954 (r := SOME (squish nl c))
adam@1341 955 handle CantSquish => default ()
adam@1341 956 end
adam@1341 957 | _ => default ())
adam@1341 958
adamc@777 959 (*before eprefaces "Summaries'" [("#1", p_summary env (normalizeRecordSummary env s1)),
adamc@777 960 ("#2", p_summary env (normalizeRecordSummary env s2))]*)
adamc@403 961 end
adamc@403 962
adamc@987 963 and guessMap env loc (c1, c2, ex) =
adamc@403 964 let
adamc@621 965 fun unfold (dom, ran, f, r, c) =
adamc@403 966 let
adamc@621 967 fun unfold (r, c) =
adamc@754 968 case #1 (hnormCon env c) of
adamc@987 969 L'.CRecord (_, []) => unifyCons env loc r (L'.CRecord (dom, []), loc)
adamc@628 970 | L'.CRecord (_, [(x, v)]) =>
adamc@628 971 let
adamc@628 972 val v' = case dom of
adamc@628 973 (L'.KUnit, _) => (L'.CUnit, loc)
adamc@628 974 | _ => cunif (loc, dom)
adamc@628 975 in
adamc@987 976 unifyCons env loc v (L'.CApp (f, v'), loc);
adamc@987 977 unifyCons env loc r (L'.CRecord (dom, [(x, v')]), loc)
adamc@628 978 end
adamc@628 979 | L'.CRecord (_, (x, v) :: rest) =>
adamc@628 980 let
adamc@628 981 val r1 = cunif (loc, (L'.KRecord dom, loc))
adamc@628 982 val r2 = cunif (loc, (L'.KRecord dom, loc))
adamc@628 983 in
adamc@628 984 unfold (r1, (L'.CRecord (ran, [(x, v)]), loc));
adamc@628 985 unfold (r2, (L'.CRecord (ran, rest), loc));
adamc@987 986 unifyCons env loc r (L'.CConcat (r1, r2), loc)
adamc@628 987 end
adamc@628 988 | L'.CConcat (c1', c2') =>
adamc@628 989 let
adamc@628 990 val r1 = cunif (loc, (L'.KRecord dom, loc))
adamc@628 991 val r2 = cunif (loc, (L'.KRecord dom, loc))
adamc@628 992 in
adamc@628 993 unfold (r1, c1');
adamc@628 994 unfold (r2, c2');
adamc@987 995 unifyCons env loc r (L'.CConcat (r1, r2), loc)
adamc@628 996 end
adam@1303 997 | L'.CUnif (0, _, _, _, ur) =>
adamc@777 998 ur := SOME (L'.CApp ((L'.CApp ((L'.CMap (dom, ran), loc), f), loc), r), loc)
adamc@628 999 | _ => raise ex
adamc@403 1000 in
adamc@621 1001 unfold (r, c)
adamc@403 1002 end
adamc@632 1003 handle _ => raise ex
adamc@403 1004 in
adamc@403 1005 case (#1 c1, #1 c2) of
adamc@621 1006 (L'.CApp ((L'.CApp ((L'.CMap (dom, ran), _), f), _), r), _) =>
adamc@621 1007 unfold (dom, ran, f, r, c2)
adamc@621 1008 | (_, L'.CApp ((L'.CApp ((L'.CMap (dom, ran), _), f), _), r)) =>
adamc@621 1009 unfold (dom, ran, f, r, c1)
adamc@403 1010 | _ => raise ex
adamc@403 1011 end
adamc@403 1012
adamc@987 1013 and unifyCons' env loc c1 c2 =
adamc@628 1014 if isUnitCon env c1 andalso isUnitCon env c2 then
adamc@628 1015 ()
adamc@628 1016 else
adamc@403 1017 let
adamc@628 1018 (*val befor = Time.now ()
adamc@628 1019 val old1 = c1
adamc@628 1020 val old2 = c2*)
adamc@628 1021 val c1 = hnormCon env c1
adamc@628 1022 val c2 = hnormCon env c2
adamc@403 1023 in
adamc@987 1024 unifyCons'' env loc c1 c2
adamc@987 1025 handle ex => guessMap env loc (c1, c2, ex)
adamc@403 1026 end
adamc@628 1027
adamc@987 1028 and unifyCons'' env loc (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@403 1029 let
adamc@403 1030 fun err f = raise CUnify' (f (c1All, c2All))
adamc@403 1031
adamc@706 1032 fun projSpecial1 (c1, n1, onFail) =
adamc@403 1033 let
adamc@632 1034 fun trySnd () =
adamc@632 1035 case #1 (hnormCon env c2All) of
adamc@632 1036 L'.CProj (c2, n2) =>
adamc@632 1037 let
adamc@632 1038 fun tryNormal () =
adamc@632 1039 if n1 = n2 then
adamc@987 1040 unifyCons' env loc c1 c2
adamc@632 1041 else
adamc@706 1042 onFail ()
adamc@632 1043 in
adamc@632 1044 case #1 (hnormCon env c2) of
adam@1303 1045 L'.CUnif (0, _, k, _, r) =>
adamc@632 1046 (case #1 (hnormKind k) of
adamc@632 1047 L'.KTuple ks =>
adamc@632 1048 let
adamc@632 1049 val loc = #2 c2
adamc@632 1050 val us = map (fn k => cunif (loc, k)) ks
adamc@632 1051 in
adamc@632 1052 r := SOME (L'.CTuple us, loc);
adamc@987 1053 unifyCons' env loc c1All (List.nth (us, n2 - 1))
adamc@632 1054 end
adamc@632 1055 | _ => tryNormal ())
adamc@632 1056 | _ => tryNormal ()
adamc@632 1057 end
adamc@706 1058 | _ => onFail ()
adamc@403 1059 in
adamc@632 1060 case #1 (hnormCon env c1) of
adam@1303 1061 L'.CUnif (0, _, k, _, r) =>
adamc@632 1062 (case #1 (hnormKind k) of
adamc@632 1063 L'.KTuple ks =>
adamc@632 1064 let
adamc@632 1065 val loc = #2 c1
adamc@632 1066 val us = map (fn k => cunif (loc, k)) ks
adamc@632 1067 in
adamc@632 1068 r := SOME (L'.CTuple us, loc);
adamc@987 1069 unifyCons' env loc (List.nth (us, n1 - 1)) c2All
adamc@632 1070 end
adamc@632 1071 | _ => trySnd ())
adamc@632 1072 | _ => trySnd ()
adamc@403 1073 end
adamc@632 1074
adamc@706 1075 fun projSpecial2 (c2, n2, onFail) =
adamc@706 1076 case #1 (hnormCon env c2) of
adam@1303 1077 L'.CUnif (0, _, k, _, r) =>
adamc@706 1078 (case #1 (hnormKind k) of
adamc@706 1079 L'.KTuple ks =>
adamc@706 1080 let
adamc@706 1081 val loc = #2 c2
adamc@706 1082 val us = map (fn k => cunif (loc, k)) ks
adamc@706 1083 in
adamc@706 1084 r := SOME (L'.CTuple us, loc);
adamc@987 1085 unifyCons' env loc c1All (List.nth (us, n2 - 1))
adamc@706 1086 end
adamc@706 1087 | _ => onFail ())
adamc@706 1088 | _ => onFail ()
adamc@706 1089
adamc@777 1090 fun isRecord' () = unifyRecordCons env (loc, c1All, c2All)
adamc@706 1091
adamc@706 1092 fun isRecord () =
adamc@706 1093 case (c1, c2) of
adamc@706 1094 (L'.CProj (c1, n1), _) => projSpecial1 (c1, n1, isRecord')
adamc@706 1095 | (_, L'.CProj (c2, n2)) => projSpecial2 (c2, n2, isRecord')
adamc@706 1096 | _ => isRecord' ()
adamc@706 1097 in
adamc@706 1098 (*eprefaces "unifyCons''" [("c1All", p_con env c1All),
adamc@706 1099 ("c2All", p_con env c2All)];*)
adamc@706 1100
adamc@706 1101 case (c1, c2) of
adamc@706 1102 (L'.CError, _) => ()
adamc@706 1103 | (_, L'.CError) => ()
adamc@403 1104
adam@1303 1105 | (L'.CUnif (nl1, loc1, k1, _, r1), L'.CUnif (nl2, loc2, k2, _, r2)) =>
adam@1303 1106 if r1 = r2 andalso nl1 = nl2 then
adamc@705 1107 ()
adam@1303 1108 else if nl1 = 0 then
adamc@705 1109 (unifyKinds env k1 k2;
adamc@705 1110 r1 := SOME c2All)
adam@1303 1111 else if nl2 = 0 then
adam@1303 1112 (unifyKinds env k1 k2;
adam@1305 1113 r2 := SOME c1All)
adam@1303 1114 else
adam@1303 1115 err (fn _ => TooLifty (loc1, loc2))
adam@1303 1116
adam@1303 1117 | (L'.CUnif (0, _, _, _, r), _) =>
adamc@705 1118 if occursCon r c2All then
adamc@705 1119 err COccursCheckFailed
adamc@705 1120 else
adamc@705 1121 r := SOME c2All
adam@1303 1122 | (_, L'.CUnif (0, _, _, _, r)) =>
adamc@705 1123 if occursCon r c1All then
adamc@705 1124 err COccursCheckFailed
adamc@705 1125 else
adamc@705 1126 r := SOME c1All
adamc@705 1127
adam@1304 1128 | (L'.CUnif (nl, _, _, _, r), _) =>
adam@1304 1129 if occursCon r c2All then
adam@1304 1130 err COccursCheckFailed
adam@1304 1131 else
adam@1304 1132 (r := SOME (squish nl c2All)
adam@1306 1133 handle CantSquish => err (fn _ => TooDeep))
adam@1304 1134 | (_, L'.CUnif (nl, _, _, _, r)) =>
adam@1304 1135 if occursCon r c1All then
adam@1304 1136 err COccursCheckFailed
adam@1304 1137 else
adam@1304 1138 (r := SOME (squish nl c1All)
adam@1306 1139 handle CantSquish => err (fn _ => TooDeep))
adam@1304 1140
adam@1341 1141 | (L'.CRecord _, _) => isRecord ()
adam@1341 1142 | (_, L'.CRecord _) => isRecord ()
adam@1341 1143 | (L'.CConcat _, _) => isRecord ()
adam@1341 1144 | (_, L'.CConcat _) => isRecord ()
adam@1341 1145
adam@1341 1146
adamc@705 1147 | (L'.CUnit, L'.CUnit) => ()
adamc@705 1148
adamc@705 1149 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@987 1150 (unifyCons' env loc d1 d2;
adamc@987 1151 unifyCons' env loc r1 r2)
adamc@705 1152 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@705 1153 if expl1 <> expl2 then
adamc@705 1154 err CExplicitness
adamc@705 1155 else
adamc@705 1156 (unifyKinds env d1 d2;
adamc@705 1157 let
adamc@705 1158 (*val befor = Time.now ()*)
adamc@705 1159 val env' = E.pushCRel env x1 d1
adamc@705 1160 in
adamc@705 1161 (*TextIO.print ("E.pushCRel: "
adamc@705 1162 ^ LargeReal.toString (Time.toReal (Time.- (Time.now (), befor)))
adamc@705 1163 ^ "\n");*)
adamc@987 1164 unifyCons' env' loc r1 r2
adamc@705 1165 end)
adamc@987 1166 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env loc r1 r2
adamc@705 1167 | (L'.TDisjoint (c1, d1, e1), L'.TDisjoint (c2, d2, e2)) =>
adamc@987 1168 (unifyCons' env loc c1 c2;
adamc@987 1169 unifyCons' env loc d1 d2;
adamc@987 1170 unifyCons' env loc e1 e2)
adamc@705 1171
adamc@705 1172 | (L'.CRel n1, L'.CRel n2) =>
adamc@705 1173 if n1 = n2 then
adamc@705 1174 ()
adamc@705 1175 else
adamc@705 1176 err CIncompatible
adamc@705 1177 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@705 1178 if n1 = n2 then
adamc@705 1179 ()
adamc@705 1180 else
adamc@705 1181 err CIncompatible
adamc@705 1182
adamc@705 1183 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@987 1184 (unifyCons' env loc d1 d2;
adamc@987 1185 unifyCons' env loc r1 r2)
adamc@705 1186 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@705 1187 (unifyKinds env k1 k2;
adamc@987 1188 unifyCons' (E.pushCRel env x1 k1) loc c1 c2)
adamc@705 1189
adamc@705 1190 | (L'.CName n1, L'.CName n2) =>
adamc@705 1191 if n1 = n2 then
adamc@705 1192 ()
adamc@705 1193 else
adamc@705 1194 err CIncompatible
adamc@705 1195
adamc@705 1196 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@705 1197 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@705 1198 ()
adamc@705 1199 else
adamc@705 1200 err CIncompatible
adamc@705 1201
adamc@705 1202 | (L'.CTuple cs1, L'.CTuple cs2) =>
adamc@987 1203 ((ListPair.appEq (fn (c1, c2) => unifyCons' env loc c1 c2) (cs1, cs2))
adamc@705 1204 handle ListPair.UnequalLengths => err CIncompatible)
adamc@705 1205
adamc@706 1206 | (L'.CProj (c1, n1), _) => projSpecial1 (c1, n1, fn () => err CIncompatible)
adamc@706 1207 | (_, L'.CProj (c2, n2)) => projSpecial2 (c2, n2, fn () => err CIncompatible)
adamc@706 1208
adamc@623 1209 | (L'.CMap (dom1, ran1), L'.CMap (dom2, ran2)) =>
adamc@623 1210 (unifyKinds env dom1 dom2;
adamc@628 1211 unifyKinds env ran1 ran2)
adamc@623 1212
adamc@623 1213 | (L'.CKAbs (x, c1), L'.CKAbs (_, c2)) =>
adamc@987 1214 unifyCons' (E.pushKRel env x) loc c1 c2
adamc@623 1215 | (L'.CKApp (c1, k1), L'.CKApp (c2, k2)) =>
adamc@623 1216 (unifyKinds env k1 k2;
adamc@987 1217 unifyCons' env loc c1 c2)
adamc@623 1218 | (L'.TKFun (x, c1), L'.TKFun (_, c2)) =>
adamc@987 1219 unifyCons' (E.pushKRel env x) loc c1 c2
adamc@628 1220
adamc@403 1221 | _ => err CIncompatible
adamc@403 1222 end
adamc@403 1223
adamc@987 1224 and unifyCons env loc c1 c2 =
adamc@987 1225 unifyCons' env loc c1 c2
adamc@403 1226 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@403 1227 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@403 1228
adamc@628 1229 fun checkCon env e c1 c2 =
adamc@987 1230 unifyCons env (#2 e) c1 c2
adamc@403 1231 handle CUnify (c1, c2, err) =>
adamc@628 1232 expError env (Unify (e, c1, c2, err))
adamc@628 1233
adamc@628 1234 fun checkPatCon env p c1 c2 =
adamc@987 1235 unifyCons env (#2 p) c1 c2
adamc@403 1236 handle CUnify (c1, c2, err) =>
adamc@628 1237 expError env (PatUnify (p, c1, c2, err))
adamc@71 1238
adamc@403 1239 fun primType env p =
adamc@403 1240 case p of
adamc@403 1241 P.Int _ => !int
adamc@403 1242 | P.Float _ => !float
adamc@403 1243 | P.String _ => !string
adamc@821 1244 | P.Char _ => !char
adamc@623 1245
adamc@403 1246 datatype constraint =
adamc@403 1247 Disjoint of D.goal
adamc@403 1248 | TypeClass of E.env * L'.con * L'.exp option ref * ErrorMsg.span
adamc@403 1249
adamc@403 1250 val enD = map Disjoint
adamc@403 1251
adamc@634 1252 fun isClassOrFolder env cl =
adamc@634 1253 E.isClass env cl
adamc@634 1254 orelse case hnormCon env cl of
adamc@634 1255 (L'.CKApp (cl, _), _) =>
adamc@634 1256 (case hnormCon env cl of
adamc@634 1257 (L'.CModProj (top_n, [], "folder"), _) => top_n = !top_r
adamc@634 1258 | _ => false)
adamc@634 1259 | _ => false
adamc@634 1260
adam@1303 1261 fun subConInCon env x y =
adam@1303 1262 ElabOps.subConInCon x y
adam@1303 1263 handle SubUnif => (cunifyError env (TooUnify (#2 x, y));
adam@1303 1264 cerror)
adam@1303 1265
adamc@629 1266 fun elabHead (env, denv) infer (e as (_, loc)) t =
adamc@403 1267 let
adamc@898 1268 fun unravelKind (t, e) =
adamc@898 1269 case hnormCon env t of
adamc@898 1270 (L'.TKFun (x, t'), _) =>
adamc@898 1271 let
adamc@898 1272 val u = kunif loc
adamc@898 1273
adamc@898 1274 val t'' = subKindInCon (0, u) t'
adamc@898 1275 in
adamc@898 1276 unravelKind (t'', (L'.EKApp (e, u), loc))
adamc@898 1277 end
adamc@898 1278 | t => (e, t, [])
adamc@898 1279
adamc@403 1280 fun unravel (t, e) =
adamc@628 1281 case hnormCon env t of
adamc@628 1282 (L'.TKFun (x, t'), _) =>
adamc@628 1283 let
adamc@628 1284 val u = kunif loc
adamc@628 1285
adamc@628 1286 val t'' = subKindInCon (0, u) t'
adamc@628 1287 in
adamc@628 1288 unravel (t'', (L'.EKApp (e, u), loc))
adamc@628 1289 end
adamc@628 1290 | (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@628 1291 let
adamc@628 1292 val u = cunif (loc, k)
adamc@628 1293
adam@1303 1294 val t'' = subConInCon env (0, u) t'
adamc@628 1295 in
adamc@628 1296 unravel (t'', (L'.ECApp (e, u), loc))
adamc@628 1297 end
adamc@633 1298 | (L'.TFun (dom, ran), _) =>
adamc@628 1299 let
adamc@633 1300 fun default () = (e, t, [])
adamc@750 1301
adamc@750 1302 fun isInstance () =
adamc@750 1303 if infer <> L.TypesOnly then
adamc@750 1304 let
adamc@750 1305 val r = ref NONE
adamc@750 1306 val (e, t, gs) = unravel (ran, (L'.EApp (e, (L'.EUnif r, loc)), loc))
adamc@750 1307 in
adamc@750 1308 (e, t, TypeClass (env, dom, r, loc) :: gs)
adamc@750 1309 end
adamc@750 1310 else
adamc@750 1311 default ()
adamc@750 1312
adamc@750 1313 fun hasInstance c =
adamc@753 1314 case hnormCon env c of
adamc@753 1315 (L'.TRecord c, _) => U.Con.exists {kind = fn _ => false,
adamc@753 1316 con = fn c =>
adamc@753 1317 E.isClass env (hnormCon env (c, loc))} c
adamc@753 1318 | c =>
adamc@750 1319 let
adamc@753 1320 fun findHead c =
adamc@753 1321 case #1 c of
adamc@753 1322 L'.CApp (f, _) => findHead f
adamc@753 1323 | _ => c
adamc@753 1324
adamc@753 1325 val cl = hnormCon env (findHead c)
adamc@750 1326 in
adamc@750 1327 isClassOrFolder env cl
adamc@750 1328 end
adamc@628 1329 in
adamc@750 1330 if hasInstance dom then
adamc@750 1331 isInstance ()
adamc@750 1332 else
adamc@750 1333 default ()
adamc@628 1334 end
adamc@629 1335 | (L'.TDisjoint (r1, r2, t'), loc) =>
adamc@629 1336 if infer <> L.TypesOnly then
adamc@629 1337 let
adamc@792 1338 val gs = D.prove env denv (r1, r2, #2 e)
adamc@629 1339 val (e, t, gs') = unravel (t', e)
adamc@629 1340 in
adamc@629 1341 (e, t, enD gs @ gs')
adamc@629 1342 end
adamc@629 1343 else
adamc@629 1344 (e, t, [])
adamc@628 1345 | t => (e, t, [])
adamc@704 1346 in
adamc@704 1347 case infer of
adamc@898 1348 L.DontInfer => unravelKind (t, e)
adamc@704 1349 | _ => unravel (t, e)
adamc@704 1350 end
adamc@15 1351
adamc@628 1352 fun elabPat (pAll as (p, loc), (env, bound)) =
adamc@171 1353 let
adamc@171 1354 val perror = (L'.PWild, loc)
adamc@171 1355 val terror = (L'.CError, loc)
adamc@171 1356 val pterror = (perror, terror)
adamc@171 1357 val rerror = (pterror, (env, bound))
adamc@171 1358
adamc@191 1359 fun pcon (pc, po, xs, to, dn, dk) =
adamc@188 1360 case (po, to) of
adamc@188 1361 (NONE, SOME _) => (expError env (PatHasNoArg loc);
adamc@188 1362 rerror)
adamc@188 1363 | (SOME _, NONE) => (expError env (PatHasArg loc);
adamc@188 1364 rerror)
adamc@191 1365 | (NONE, NONE) =>
adamc@191 1366 let
adamc@191 1367 val k = (L'.KType, loc)
adamc@191 1368 val unifs = map (fn _ => cunif (loc, k)) xs
adamc@191 1369 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
adamc@191 1370 in
adamc@191 1371 (((L'.PCon (dk, pc, unifs, NONE), loc), dn),
adamc@191 1372 (env, bound))
adamc@191 1373 end
adamc@188 1374 | (SOME p, SOME t) =>
adamc@188 1375 let
adamc@628 1376 val ((p', pt), (env, bound)) = elabPat (p, (env, bound))
adamc@191 1377
adamc@191 1378 val k = (L'.KType, loc)
adamc@191 1379 val unifs = map (fn _ => cunif (loc, k)) xs
adamc@194 1380 val nxs = length unifs - 1
adam@1304 1381 val t = ListUtil.foldli (fn (i, u, t) => subConInCon env (nxs - i,
adam@1304 1382 E.mliftConInCon (nxs - i) u) t) t unifs
adamc@191 1383 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
adamc@188 1384 in
adamc@628 1385 ignore (checkPatCon env p' pt t);
adamc@191 1386 (((L'.PCon (dk, pc, unifs, SOME p'), loc), dn),
adamc@188 1387 (env, bound))
adamc@188 1388 end
adamc@171 1389 in
adamc@171 1390 case p of
adamc@171 1391 L.PWild => (((L'.PWild, loc), cunif (loc, (L'.KType, loc))),
adamc@171 1392 (env, bound))
adamc@171 1393 | L.PVar x =>
adamc@171 1394 let
adamc@171 1395 val t = if SS.member (bound, x) then
adamc@171 1396 (expError env (DuplicatePatternVariable (loc, x));
adamc@171 1397 terror)
adamc@171 1398 else
adamc@171 1399 cunif (loc, (L'.KType, loc))
adamc@171 1400 in
adamc@182 1401 (((L'.PVar (x, t), loc), t),
adamc@171 1402 (E.pushERel env x t, SS.add (bound, x)))
adamc@171 1403 end
adamc@173 1404 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
adamc@173 1405 (env, bound))
adamc@171 1406 | L.PCon ([], x, po) =>
adamc@171 1407 (case E.lookupConstructor env x of
adamc@174 1408 NONE => (expError env (UnboundConstructor (loc, [], x));
adamc@171 1409 rerror)
adamc@191 1410 | SOME (dk, n, xs, to, dn) => pcon (L'.PConVar n, po, xs, to, (L'.CNamed dn, loc), dk))
adamc@174 1411 | L.PCon (m1 :: ms, x, po) =>
adamc@174 1412 (case E.lookupStr env m1 of
adamc@174 1413 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@174 1414 rerror)
adamc@174 1415 | SOME (n, sgn) =>
adamc@174 1416 let
adamc@174 1417 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@174 1418 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@182 1419 NONE => raise Fail "elabPat: Unknown substructure"
adamc@174 1420 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@174 1421 ((L'.StrVar n, loc), sgn) ms
adamc@174 1422 in
adamc@174 1423 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 1424 NONE => (expError env (UnboundConstructor (loc, m1 :: ms, x));
adamc@174 1425 rerror)
adamc@191 1426 | SOME (dk, _, xs, to, dn) => pcon (L'.PConProj (n, ms, x), po, xs, to, dn, dk)
adamc@174 1427 end)
adamc@174 1428
adamc@175 1429 | L.PRecord (xps, flex) =>
adamc@175 1430 let
adamc@175 1431 val (xpts, (env, bound, _)) =
adamc@175 1432 ListUtil.foldlMap (fn ((x, p), (env, bound, fbound)) =>
adamc@175 1433 let
adamc@628 1434 val ((p', t), (env, bound)) = elabPat (p, (env, bound))
adamc@175 1435 in
adamc@175 1436 if SS.member (fbound, x) then
adamc@175 1437 expError env (DuplicatePatField (loc, x))
adamc@175 1438 else
adamc@175 1439 ();
adamc@175 1440 ((x, p', t), (env, bound, SS.add (fbound, x)))
adamc@175 1441 end)
adamc@175 1442 (env, bound, SS.empty) xps
adamc@175 1443
adamc@175 1444 val k = (L'.KType, loc)
adamc@175 1445 val c = (L'.CRecord (k, map (fn (x, _, t) => ((L'.CName x, loc), t)) xpts), loc)
adamc@176 1446 val c =
adamc@175 1447 if flex then
adamc@176 1448 (L'.CConcat (c, cunif (loc, (L'.KRecord k, loc))), loc)
adamc@175 1449 else
adamc@176 1450 c
adamc@175 1451 in
adamc@182 1452 (((L'.PRecord xpts, loc),
adamc@175 1453 (L'.TRecord c, loc)),
adamc@175 1454 (env, bound))
adamc@175 1455 end
adamc@822 1456
adamc@822 1457 | L.PAnnot (p, t) =>
adamc@822 1458 let
adamc@822 1459 val ((p', pt), (env, bound)) = elabPat (p, (env, bound))
adamc@822 1460 val (t', k, _) = elabCon (env, D.empty) t
adamc@822 1461 in
adamc@822 1462 checkPatCon env p' pt t';
adamc@822 1463 ((p', t'), (env, bound))
adamc@822 1464 end
adamc@171 1465 end
adamc@171 1466
adamc@819 1467 (* This exhaustiveness checking follows Luc Maranget's paper "Warnings for pattern matching." *)
adamc@819 1468 fun exhaustive (env, t, ps, loc) =
adamc@819 1469 let
adamc@819 1470 fun fail n = raise Fail ("Elaborate.exhaustive: Impossible " ^ Int.toString n)
adamc@819 1471
adamc@819 1472 fun patConNum pc =
adamc@819 1473 case pc of
adamc@819 1474 L'.PConVar n => n
adamc@819 1475 | L'.PConProj (m1, ms, x) =>
adamc@819 1476 let
adamc@819 1477 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@819 1478 in
adamc@819 1479 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@819 1480 NONE => raise Fail "exhaustive: Can't project datatype"
adamc@819 1481 | SOME (_, n, _, _, _) => n
adamc@819 1482 end
adamc@819 1483
adamc@819 1484 fun nameOfNum (t, n) =
adamc@819 1485 case t of
adamc@819 1486 L'.CModProj (m1, ms, x) =>
adamc@819 1487 let
adamc@819 1488 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@819 1489 in
adamc@819 1490 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
adamc@819 1491 NONE => raise Fail "exhaustive: Can't project datatype"
adamc@819 1492 | SOME (_, cons) =>
adamc@819 1493 case ListUtil.search (fn (name, n', _) =>
adamc@819 1494 if n' = n then
adamc@819 1495 SOME name
adamc@819 1496 else
adamc@819 1497 NONE) cons of
adamc@819 1498 NONE => fail 9
adamc@819 1499 | SOME name => L'.PConProj (m1, ms, name)
adamc@819 1500 end
adamc@819 1501 | _ => L'.PConVar n
adamc@819 1502
adamc@819 1503 fun S (args, c, P) =
adamc@819 1504 List.mapPartial
adamc@819 1505 (fn [] => fail 1
adamc@819 1506 | p1 :: ps =>
adamc@819 1507 let
adamc@819 1508 val loc = #2 p1
adamc@819 1509
adamc@819 1510 fun wild () =
adamc@819 1511 SOME (map (fn _ => (L'.PWild, loc)) args @ ps)
adamc@819 1512 in
adamc@819 1513 case #1 p1 of
adamc@819 1514 L'.PPrim _ => NONE
adamc@819 1515 | L'.PCon (_, c', _, NONE) =>
adamc@819 1516 if patConNum c' = c then
adamc@819 1517 SOME ps
adamc@819 1518 else
adamc@819 1519 NONE
adamc@819 1520 | L'.PCon (_, c', _, SOME p) =>
adamc@819 1521 if patConNum c' = c then
adamc@819 1522 SOME (p :: ps)
adamc@819 1523 else
adamc@819 1524 NONE
adamc@819 1525 | L'.PRecord xpts =>
adamc@819 1526 SOME (map (fn x =>
adamc@819 1527 case ListUtil.search (fn (x', p, _) =>
adamc@819 1528 if x = x' then
adamc@819 1529 SOME p
adamc@819 1530 else
adamc@819 1531 NONE) xpts of
adamc@819 1532 NONE => (L'.PWild, loc)
adamc@819 1533 | SOME p => p) args @ ps)
adamc@819 1534 | L'.PWild => wild ()
adamc@819 1535 | L'.PVar _ => wild ()
adamc@819 1536 end)
adamc@819 1537 P
adamc@819 1538
adamc@819 1539 fun D P =
adamc@819 1540 List.mapPartial
adamc@819 1541 (fn [] => fail 2
adamc@819 1542 | (p1, _) :: ps =>
adamc@819 1543 case p1 of
adamc@819 1544 L'.PWild => SOME ps
adamc@819 1545 | L'.PVar _ => SOME ps
adamc@819 1546 | L'.PPrim _ => NONE
adamc@819 1547 | L'.PCon _ => NONE
adamc@819 1548 | L'.PRecord _ => NONE)
adamc@819 1549 P
adamc@819 1550
adamc@819 1551 fun I (P, q) =
adamc@819 1552 (*(prefaces "I" [("P", p_list (fn P' => box [PD.string "[", p_list (p_pat env) P', PD.string "]"]) P),
adamc@819 1553 ("q", p_list (p_con env) q)];*)
adamc@819 1554 case q of
adamc@819 1555 [] => (case P of
adamc@819 1556 [] => SOME []
adamc@819 1557 | _ => NONE)
adamc@819 1558 | q1 :: qs =>
adamc@819 1559 let
adamc@819 1560 val loc = #2 q1
adamc@819 1561
adamc@819 1562 fun unapp (t, acc) =
adamc@830 1563 case #1 t of
adamc@830 1564 L'.CApp (t, arg) => unapp (t, arg :: acc)
adamc@819 1565 | _ => (t, rev acc)
adamc@819 1566
adamc@830 1567 val (t1, args) = unapp (hnormCon env q1, [])
adamc@830 1568 val t1 = hnormCon env t1
adam@1303 1569 fun doSub t = foldl (fn (arg, t) => subConInCon env (0, arg) t) t args
adamc@819 1570
adamc@819 1571 fun dtype (dtO, names) =
adamc@819 1572 let
adamc@819 1573 val nameSet = IS.addList (IS.empty, names)
adamc@819 1574 val nameSet = foldl (fn (ps, nameSet) =>
adamc@819 1575 case ps of
adamc@819 1576 [] => fail 4
adamc@819 1577 | (L'.PCon (_, pc, _, _), _) :: _ =>
adamc@819 1578 (IS.delete (nameSet, patConNum pc)
adamc@819 1579 handle NotFound => nameSet)
adamc@819 1580 | _ => nameSet)
adamc@819 1581 nameSet P
adamc@819 1582 in
adamc@819 1583 nameSet
adamc@819 1584 end
adamc@819 1585
adamc@819 1586 fun default () = (NONE, IS.singleton 0, [])
adamc@819 1587
adamc@819 1588 val (dtO, unused, cons) =
adamc@830 1589 case #1 t1 of
adamc@819 1590 L'.CNamed n =>
adamc@819 1591 let
adamc@819 1592 val dt = E.lookupDatatype env n
adamc@819 1593 val cons = E.constructors dt
adamc@819 1594 in
adamc@819 1595 (SOME dt,
adamc@819 1596 dtype (SOME dt, map #2 cons),
adamc@819 1597 map (fn (_, n, co) =>
adamc@819 1598 (n,
adamc@819 1599 case co of
adamc@819 1600 NONE => []
adamc@819 1601 | SOME t => [("", doSub t)])) cons)
adamc@819 1602 end
adamc@819 1603 | L'.CModProj (m1, ms, x) =>
adamc@819 1604 let
adamc@819 1605 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@819 1606 in
adamc@819 1607 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
adamc@819 1608 NONE => default ()
adamc@819 1609 | SOME (_, cons) =>
adamc@819 1610 (NONE,
adamc@819 1611 dtype (NONE, map #2 cons),
adamc@819 1612 map (fn (s, _, co) =>
adamc@819 1613 (patConNum (L'.PConProj (m1, ms, s)),
adamc@819 1614 case co of
adamc@819 1615 NONE => []
adamc@819 1616 | SOME t => [("", doSub t)])) cons)
adamc@819 1617 end
adamc@830 1618 | L'.TRecord t =>
adamc@830 1619 (case #1 (hnormCon env t) of
adamc@830 1620 L'.CRecord (_, xts) =>
adamc@830 1621 let
adamc@830 1622 val xts = map (fn ((L'.CName x, _), co) => SOME (x, co)
adamc@830 1623 | _ => NONE) xts
adamc@830 1624 in
adamc@830 1625 if List.all Option.isSome xts then
adamc@830 1626 let
adamc@830 1627 val xts = List.mapPartial (fn x => x) xts
adamc@830 1628 val xts = ListMergeSort.sort (fn ((x1, _), (x2, _)) =>
adamc@830 1629 String.compare (x1, x2) = GREATER) xts
adamc@830 1630 in
adamc@830 1631 (NONE, IS.empty, [(0, xts)])
adamc@830 1632 end
adamc@830 1633 else
adamc@830 1634 default ()
adamc@830 1635 end
adamc@830 1636 | _ => default ())
adamc@819 1637 | _ => default ()
adamc@819 1638 in
adamc@819 1639 if IS.isEmpty unused then
adamc@819 1640 let
adamc@819 1641 fun recurse cons =
adamc@819 1642 case cons of
adamc@819 1643 [] => NONE
adamc@819 1644 | (name, args) :: cons =>
adamc@819 1645 case I (S (map #1 args, name, P),
adamc@819 1646 map #2 args @ qs) of
adamc@819 1647 NONE => recurse cons
adamc@819 1648 | SOME ps =>
adamc@819 1649 let
adamc@819 1650 val nargs = length args
adamc@819 1651 val argPs = List.take (ps, nargs)
adamc@819 1652 val restPs = List.drop (ps, nargs)
adamc@819 1653
adamc@819 1654 val p = case name of
adamc@819 1655 0 => L'.PRecord (ListPair.map
adamc@819 1656 (fn ((name, t), p) => (name, p, t))
adamc@819 1657 (args, argPs))
adamc@830 1658 | _ => L'.PCon (L'.Default, nameOfNum (#1 t1, name), [],
adamc@819 1659 case argPs of
adamc@819 1660 [] => NONE
adamc@819 1661 | [p] => SOME p
adamc@819 1662 | _ => fail 3)
adamc@819 1663 in
adamc@819 1664 SOME ((p, loc) :: restPs)
adamc@819 1665 end
adamc@819 1666 in
adamc@819 1667 recurse cons
adamc@819 1668 end
adamc@819 1669 else
adamc@819 1670 case I (D P, qs) of
adamc@819 1671 NONE => NONE
adamc@819 1672 | SOME ps =>
adamc@819 1673 let
adamc@819 1674 val p = case cons of
adamc@819 1675 [] => L'.PWild
adamc@819 1676 | (0, _) :: _ => L'.PWild
adamc@819 1677 | _ =>
adamc@819 1678 case IS.find (fn _ => true) unused of
adamc@819 1679 NONE => fail 6
adamc@819 1680 | SOME name =>
adamc@819 1681 case ListUtil.search (fn (name', args) =>
adamc@819 1682 if name = name' then
adamc@819 1683 SOME (name', args)
adamc@819 1684 else
adamc@819 1685 NONE) cons of
adamc@819 1686 SOME (n, []) =>
adamc@830 1687 L'.PCon (L'.Default, nameOfNum (#1 t1, n), [], NONE)
adamc@819 1688 | SOME (n, [_]) =>
adamc@830 1689 L'.PCon (L'.Default, nameOfNum (#1 t1, n), [], SOME (L'.PWild, loc))
adamc@819 1690 | _ => fail 7
adamc@819 1691 in
adamc@819 1692 SOME ((p, loc) :: ps)
adamc@819 1693 end
adamc@819 1694 end
adamc@819 1695 in
adamc@819 1696 case I (map (fn x => [x]) ps, [t]) of
adamc@819 1697 NONE => NONE
adamc@819 1698 | SOME [p] => SOME p
adamc@819 1699 | _ => fail 7
adamc@819 1700 end
adamc@819 1701
adamc@217 1702 fun unmodCon env (c, loc) =
adamc@217 1703 case c of
adamc@217 1704 L'.CNamed n =>
adamc@217 1705 (case E.lookupCNamed env n of
adamc@217 1706 (_, _, SOME (c as (L'.CModProj _, _))) => unmodCon env c
adamc@217 1707 | _ => (c, loc))
adamc@217 1708 | L'.CModProj (m1, ms, x) =>
adamc@217 1709 let
adamc@217 1710 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@217 1711 in
adamc@217 1712 case E.projectCon env {str = str, sgn = sgn, field = x} of
adamc@217 1713 NONE => raise Fail "unmodCon: Can't projectCon"
adamc@217 1714 | SOME (_, SOME (c as (L'.CModProj _, _))) => unmodCon env c
adamc@217 1715 | _ => (c, loc)
adamc@217 1716 end
adamc@217 1717 | _ => (c, loc)
adamc@217 1718
adamc@721 1719 fun normClassKey env c =
adamc@467 1720 let
adamc@721 1721 val c = hnormCon env c
adamc@467 1722 in
adamc@467 1723 case #1 c of
adamc@467 1724 L'.CApp (c1, c2) =>
adamc@467 1725 let
adamc@721 1726 val c1 = normClassKey env c1
adamc@721 1727 val c2 = normClassKey env c2
adamc@467 1728 in
adamc@467 1729 (L'.CApp (c1, c2), #2 c)
adamc@467 1730 end
adamc@721 1731 | L'.CRecord (k, xcs) => (L'.CRecord (k, map (fn (x, c) => (normClassKey env x,
adamc@721 1732 normClassKey env c)) xcs), #2 c)
adamc@721 1733 | _ => unmodCon env c
adamc@467 1734 end
adamc@467 1735
adamc@467 1736 fun normClassConstraint env (c, loc) =
adamc@217 1737 case c of
adamc@217 1738 L'.CApp (f, x) =>
adamc@217 1739 let
adamc@721 1740 val f = normClassKey env f
adamc@467 1741 val x = normClassKey env x
adamc@217 1742 in
adamc@467 1743 (L'.CApp (f, x), loc)
adamc@217 1744 end
adamc@674 1745 | L'.TFun (c1, c2) =>
adamc@674 1746 let
adamc@674 1747 val c1 = normClassConstraint env c1
adamc@674 1748 val c2 = normClassConstraint env c2
adamc@674 1749 in
adamc@674 1750 (L'.TFun (c1, c2), loc)
adamc@674 1751 end
adamc@674 1752 | L'.TCFun (expl, x, k, c1) => (L'.TCFun (expl, x, k, normClassConstraint env c1), loc)
adam@1303 1753 | L'.CUnif (nl, _, _, _, ref (SOME c)) => normClassConstraint env (E.mliftConInCon nl c)
adamc@721 1754 | _ => unmodCon env (c, loc)
adamc@216 1755
adamc@1093 1756 fun findHead e e' =
adamc@1093 1757 let
adamc@1093 1758 fun findHead (e, _) =
adamc@1093 1759 case e of
adamc@1093 1760 L.EVar (_, _, infer) =>
adamc@1093 1761 let
adamc@1093 1762 fun findHead' (e, _) =
adamc@1093 1763 case e of
adamc@1093 1764 L'.ENamed _ => true
adamc@1093 1765 | L'.EModProj _ => true
adam@1472 1766 | L'.ERel _ => true
adamc@1093 1767 | L'.EApp (e, _) => findHead' e
adamc@1093 1768 | L'.ECApp (e, _) => findHead' e
adamc@1093 1769 | L'.EKApp (e, _) => findHead' e
adamc@1093 1770 | _ => false
adamc@1093 1771 in
adamc@1093 1772 if findHead' e' then
adamc@1093 1773 SOME infer
adamc@1093 1774 else
adamc@1093 1775 NONE
adamc@1093 1776 end
adamc@1093 1777 | L.EApp (e, _) => findHead e
adamc@1093 1778 | L.ECApp (e, _) => findHead e
adamc@1093 1779 | L.EDisjointApp e => findHead e
adamc@1093 1780 | _ => NONE
adamc@1093 1781 in
adamc@1093 1782 findHead e
adamc@1093 1783 end
adamc@1093 1784
adam@1570 1785 datatype needed = Needed of {Cons : L'.kind SM.map,
adamc@1264 1786 Constraints : (E.env * (L'.con * L'.con) * ErrorMsg.span) list,
adamc@1264 1787 Vals : SS.set,
adamc@1270 1788 Mods : (E.env * needed) SM.map}
adamc@1264 1789
adamc@1264 1790 fun ncons (Needed r) = #Cons r
adamc@1264 1791 fun nconstraints (Needed r) = #Constraints r
adamc@1264 1792 fun nvals (Needed r) = #Vals r
adamc@1264 1793 fun nmods (Needed r) = #Mods r
adamc@1264 1794
adamc@1264 1795 val nempty = Needed {Cons = SM.empty,
adamc@1264 1796 Constraints = nil,
adamc@1264 1797 Vals = SS.empty,
adamc@1264 1798 Mods = SM.empty}
adamc@1264 1799
adamc@1264 1800 fun naddCon (r : needed, k, v) =
adamc@1264 1801 let
adamc@1264 1802 val Needed r = r
adamc@1264 1803 in
adamc@1264 1804 Needed {Cons = SM.insert (#Cons r, k, v),
adamc@1264 1805 Constraints = #Constraints r,
adamc@1264 1806 Vals = #Vals r,
adamc@1264 1807 Mods = #Mods r}
adamc@1264 1808 end
adamc@1264 1809
adamc@1264 1810 fun naddConstraint (r : needed, v) =
adamc@1264 1811 let
adamc@1264 1812 val Needed r = r
adamc@1264 1813 in
adamc@1264 1814 Needed {Cons = #Cons r,
adamc@1264 1815 Constraints = v :: #Constraints r,
adamc@1264 1816 Vals = #Vals r,
adamc@1264 1817 Mods = #Mods r}
adamc@1264 1818 end
adamc@1264 1819
adamc@1264 1820 fun naddVal (r : needed, k) =
adamc@1264 1821 let
adamc@1264 1822 val Needed r = r
adamc@1264 1823 in
adamc@1264 1824 Needed {Cons = #Cons r,
adamc@1264 1825 Constraints = #Constraints r,
adamc@1264 1826 Vals = SS.add (#Vals r, k),
adamc@1264 1827 Mods = #Mods r}
adamc@1264 1828 end
adamc@1264 1829
adamc@1264 1830 fun naddMod (r : needed, k, v) =
adamc@1264 1831 let
adamc@1264 1832 val Needed r = r
adamc@1264 1833 in
adamc@1264 1834 Needed {Cons = #Cons r,
adamc@1264 1835 Constraints = #Constraints r,
adamc@1264 1836 Vals = #Vals r,
adamc@1264 1837 Mods = SM.insert (#Mods r, k, v)}
adamc@1264 1838 end
adamc@1264 1839
adamc@1264 1840 fun ndelCon (r : needed, k) =
adamc@1264 1841 let
adamc@1264 1842 val Needed r = r
adamc@1264 1843 in
adamc@1264 1844 Needed {Cons = #1 (SM.remove (#Cons r, k)) handle NotFound => #Cons r,
adamc@1264 1845 Constraints = #Constraints r,
adamc@1264 1846 Vals = #Vals r,
adamc@1264 1847 Mods = #Mods r}
adamc@1264 1848 end
adamc@1264 1849
adamc@1264 1850 fun ndelVal (r : needed, k) =
adamc@1264 1851 let
adamc@1264 1852 val Needed r = r
adamc@1264 1853 in
adamc@1264 1854 Needed {Cons = #Cons r,
adamc@1264 1855 Constraints = #Constraints r,
adamc@1264 1856 Vals = SS.delete (#Vals r, k) handle NotFound => #Vals r,
adamc@1264 1857 Mods = #Mods r}
adamc@1264 1858 end
adamc@1264 1859
adamc@92 1860 fun elabExp (env, denv) (eAll as (e, loc)) =
adamc@91 1861 let
adamc@848 1862 (*val () = eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)]*)
adamc@514 1863 (*val befor = Time.now ()*)
adamc@280 1864
adamc@280 1865 val r = case e of
adamc@91 1866 L.EAnnot (e, t) =>
adamc@91 1867 let
adamc@91 1868 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1869 val (t', _, gs2) = elabCon (env, denv) t
adamc@91 1870 in
adamc@628 1871 checkCon env e' et t';
adamc@628 1872 (e', t', gs1 @ enD gs2)
adamc@91 1873 end
adamc@34 1874
adamc@91 1875 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@403 1876 | L.EVar ([], s, infer) =>
adamc@91 1877 (case E.lookupE env s of
adamc@91 1878 E.NotBound =>
adamc@91 1879 (expError env (UnboundExp (loc, s));
adamc@91 1880 (eerror, cerror, []))
adamc@629 1881 | E.Rel (n, t) => elabHead (env, denv) infer (L'.ERel n, loc) t
adamc@629 1882 | E.Named (n, t) => elabHead (env, denv) infer (L'.ENamed n, loc) t)
adamc@403 1883 | L.EVar (m1 :: ms, s, infer) =>
adamc@91 1884 (case E.lookupStr env m1 of
adamc@91 1885 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@91 1886 (eerror, cerror, []))
adamc@91 1887 | SOME (n, sgn) =>
adamc@91 1888 let
adamc@91 1889 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@91 1890 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@91 1891 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@91 1892 (strerror, sgnerror))
adamc@91 1893 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@91 1894 ((L'.StrVar n, loc), sgn) ms
adamc@10 1895
adamc@91 1896 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@91 1897 NONE => (expError env (UnboundExp (loc, s));
adamc@91 1898 cerror)
adamc@91 1899 | SOME t => t
adamc@91 1900 in
adamc@629 1901 elabHead (env, denv) infer (L'.EModProj (n, ms, s), loc) t
adamc@91 1902 end)
adamc@86 1903
adamc@349 1904 | L.EWild =>
adamc@210 1905 let
adamc@349 1906 val r = ref NONE
adamc@349 1907 val c = cunif (loc, (L'.KType, loc))
adamc@210 1908 in
adamc@349 1909 ((L'.EUnif r, loc), c, [TypeClass (env, c, r, loc)])
adamc@210 1910 end
adamc@210 1911
adamc@91 1912 | L.EApp (e1, e2) =>
adamc@91 1913 let
adamc@91 1914 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@1093 1915
adamc@403 1916 val (e2', t2, gs2) = elabExp (env, denv) e2
adamc@71 1917
adamc@91 1918 val dom = cunif (loc, ktype)
adamc@91 1919 val ran = cunif (loc, ktype)
adamc@838 1920 val t = (L'.TFun (dom, ran), loc)
adamc@1093 1921
adamc@1093 1922 val () = checkCon env e1' t1 t
adamc@1093 1923 val () = checkCon env e2' t2 dom
adamc@1093 1924
adamc@1093 1925 val ef = (L'.EApp (e1', e2'), loc)
adamc@1093 1926 val (ef, et, gs3) =
adamc@1093 1927 case findHead e1 e1' of
adamc@1093 1928 NONE => (ef, ran, [])
adamc@1093 1929 | SOME infer => elabHead (env, denv) infer ef ran
adamc@91 1930 in
adamc@1093 1931 (ef, et, gs1 @ gs2 @ gs3)
adamc@91 1932 end
adamc@91 1933 | L.EAbs (x, to, e) =>
adamc@91 1934 let
adamc@91 1935 val (t', gs1) = case to of
adamc@91 1936 NONE => (cunif (loc, ktype), [])
adamc@91 1937 | SOME t =>
adamc@91 1938 let
adamc@91 1939 val (t', tk, gs) = elabCon (env, denv) t
adamc@91 1940 in
adamc@91 1941 checkKind env t' tk ktype;
adamc@91 1942 (t', gs)
adamc@91 1943 end
adamc@467 1944 val dom = normClassConstraint env t'
adamc@467 1945 val (e', et, gs2) = elabExp (E.pushERel env x dom, denv) e
adamc@91 1946 in
adamc@91 1947 ((L'.EAbs (x, t', et, e'), loc),
adamc@91 1948 (L'.TFun (t', et), loc),
adamc@467 1949 enD gs1 @ gs2)
adamc@91 1950 end
adamc@91 1951 | L.ECApp (e, c) =>
adamc@91 1952 let
adamc@91 1953 val (e', et, gs1) = elabExp (env, denv) e
adamc@1093 1954
adamc@328 1955 val oldEt = et
adamc@403 1956 val (c', ck, gs2) = elabCon (env, denv) c
adamc@628 1957 val (et', _) = hnormCon env et
adamc@91 1958 in
adamc@91 1959 case et' of
adamc@91 1960 L'.CError => (eerror, cerror, [])
adamc@328 1961 | L'.TCFun (_, x, k, eb) =>
adamc@91 1962 let
adamc@91 1963 val () = checkKind env c' ck k
adam@1303 1964 val eb' = subConInCon env (0, c') eb
adamc@1093 1965
adamc@1093 1966 val ef = (L'.ECApp (e', c'), loc)
adamc@1093 1967 val (ef, eb', gs3) =
adamc@1093 1968 case findHead e e' of
adamc@1093 1969 NONE => (ef, eb', [])
adamc@1093 1970 | SOME infer => elabHead (env, denv) infer ef eb'
adamc@91 1971 in
adamc@328 1972 (*prefaces "Elab ECApp" [("e", SourcePrint.p_exp eAll),
adamc@328 1973 ("et", p_con env oldEt),
adamc@328 1974 ("x", PD.string x),
adamc@328 1975 ("eb", p_con (E.pushCRel env x k) eb),
adamc@328 1976 ("c", p_con env c'),
adamc@328 1977 ("eb'", p_con env eb')];*)
adamc@1093 1978 (ef, eb', gs1 @ enD gs2 @ gs3)
adamc@91 1979 end
adamc@91 1980
adamc@91 1981 | _ =>
adamc@91 1982 (expError env (WrongForm ("constructor function", e', et));
adamc@91 1983 (eerror, cerror, []))
adamc@91 1984 end
adamc@91 1985 | L.ECAbs (expl, x, k, e) =>
adamc@91 1986 let
adamc@91 1987 val expl' = elabExplicitness expl
adamc@623 1988 val k' = elabKind env k
adamc@328 1989
adamc@328 1990 val env' = E.pushCRel env x k'
adamc@328 1991 val (e', et, gs) = elabExp (env', D.enter denv) e
adamc@91 1992 in
adamc@91 1993 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@91 1994 (L'.TCFun (expl', x, k', et), loc),
adamc@91 1995 gs)
adamc@91 1996 end
adamc@623 1997 | L.EKAbs (x, e) =>
adamc@623 1998 let
adamc@623 1999 val env' = E.pushKRel env x
adamc@623 2000 val (e', et, gs) = elabExp (env', denv) e
adamc@623 2001 in
adamc@623 2002 ((L'.EKAbs (x, e'), loc),
adamc@623 2003 (L'.TKFun (x, et), loc),
adamc@623 2004 gs)
adamc@623 2005 end
adamc@91 2006
adamc@91 2007 | L.EDisjoint (c1, c2, e) =>
adamc@91 2008 let
adamc@91 2009 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@91 2010 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@91 2011
adamc@91 2012 val ku1 = kunif loc
adamc@91 2013 val ku2 = kunif loc
adamc@91 2014
adamc@628 2015 val denv' = D.assert env denv (c1', c2')
adamc@628 2016 val (e', t, gs3) = elabExp (env, denv') e
adamc@91 2017 in
adamc@91 2018 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@91 2019 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@91 2020
adamc@628 2021 (e', (L'.TDisjoint (c1', c2', t), loc), enD gs1 @ enD gs2 @ gs3)
adamc@91 2022 end
adamc@629 2023 | L.EDisjointApp e =>
adamc@629 2024 let
adamc@629 2025 val (e', t, gs1) = elabExp (env, denv) e
adamc@629 2026
adamc@629 2027 val k1 = kunif loc
adamc@629 2028 val c1 = cunif (loc, (L'.KRecord k1, loc))
adamc@629 2029 val k2 = kunif loc
adamc@629 2030 val c2 = cunif (loc, (L'.KRecord k2, loc))
adamc@629 2031 val t' = cunif (loc, ktype)
adamc@629 2032 val () = checkCon env e' t (L'.TDisjoint (c1, c2, t'), loc)
adamc@629 2033 val gs2 = D.prove env denv (c1, c2, loc)
adamc@629 2034 in
adamc@629 2035 (e', t', enD gs2 @ gs1)
adamc@629 2036 end
adamc@91 2037
adamc@91 2038 | L.ERecord xes =>
adamc@91 2039 let
adamc@91 2040 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@91 2041 let
adamc@91 2042 val (x', xk, gs1) = elabCon (env, denv) x
adamc@91 2043 val (e', et, gs2) = elabExp (env, denv) e
adamc@91 2044 in
adamc@91 2045 checkKind env x' xk kname;
adamc@228 2046 ((x', e', et), enD gs1 @ gs2 @ gs)
adamc@91 2047 end)
adamc@91 2048 [] xes
adamc@91 2049
adamc@91 2050 val k = (L'.KType, loc)
adamc@91 2051
adamc@91 2052 fun prove (xets, gs) =
adamc@91 2053 case xets of
adamc@91 2054 [] => gs
adamc@91 2055 | (x, _, t) :: rest =>
adamc@91 2056 let
adamc@91 2057 val xc = (x, t)
adamc@91 2058 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@91 2059 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@91 2060 let
adamc@91 2061 val xc' = (x', t')
adamc@91 2062 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@91 2063 in
adamc@91 2064 D.prove env denv (r1, r2, loc) @ gs
adamc@91 2065 end)
adamc@91 2066 gs rest
adamc@91 2067 in
adamc@91 2068 prove (rest, gs)
adamc@91 2069 end
adamc@228 2070
adamc@228 2071 val gsD = List.mapPartial (fn Disjoint d => SOME d | _ => NONE) gs
adamc@228 2072 val gsO = List.filter (fn Disjoint _ => false | _ => true) gs
adamc@91 2073 in
adamc@255 2074 (*TextIO.print ("|gsO| = " ^ Int.toString (length gsO) ^ "\n");*)
adamc@91 2075 ((L'.ERecord xes', loc),
adamc@91 2076 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@228 2077 enD (prove (xes', gsD)) @ gsO)
adamc@91 2078 end
adamc@91 2079
adamc@91 2080 | L.EField (e, c) =>
adamc@91 2081 let
adamc@91 2082 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 2083 val (c', ck, gs2) = elabCon (env, denv) c
adamc@91 2084
adamc@91 2085 val ft = cunif (loc, ktype)
adamc@91 2086 val rest = cunif (loc, ktype_record)
adamc@91 2087 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@629 2088 val () = checkCon env e' et
adamc@629 2089 (L'.TRecord (L'.CConcat (first, rest), loc), loc);
adamc@628 2090 val gs3 = D.prove env denv (first, rest, loc)
adamc@91 2091 in
adamc@628 2092 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ enD gs2 @ enD gs3)
adamc@91 2093 end
adamc@91 2094
adamc@445 2095 | L.EConcat (e1, e2) =>
adamc@339 2096 let
adamc@339 2097 val (e1', e1t, gs1) = elabExp (env, denv) e1
adamc@445 2098 val (e2', e2t, gs2) = elabExp (env, denv) e2
adamc@445 2099
adamc@445 2100 val r1 = cunif (loc, ktype_record)
adamc@445 2101 val r2 = cunif (loc, ktype_record)
adamc@445 2102
adamc@629 2103 val () = checkCon env e1' e1t (L'.TRecord r1, loc)
adamc@629 2104 val () = checkCon env e2' e2t (L'.TRecord r2, loc)
adamc@629 2105
adamc@628 2106 val gs3 = D.prove env denv (r1, r2, loc)
adamc@339 2107 in
adamc@445 2108 ((L'.EConcat (e1', r1, e2', r2), loc),
adamc@445 2109 (L'.TRecord ((L'.CConcat (r1, r2), loc)), loc),
adamc@628 2110 gs1 @ gs2 @ enD gs3)
adamc@339 2111 end
adamc@149 2112 | L.ECut (e, c) =>
adamc@149 2113 let
adamc@149 2114 val (e', et, gs1) = elabExp (env, denv) e
adamc@149 2115 val (c', ck, gs2) = elabCon (env, denv) c
adamc@149 2116
adamc@149 2117 val ft = cunif (loc, ktype)
adamc@149 2118 val rest = cunif (loc, ktype_record)
adamc@149 2119 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@629 2120
adamc@629 2121 val () = checkCon env e' et
adamc@629 2122 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@149 2123
adamc@628 2124 val gs3 = D.prove env denv (first, rest, loc)
adamc@149 2125 in
adam@1299 2126 checkKind env c' ck kname;
adamc@228 2127 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc),
adamc@628 2128 gs1 @ enD gs2 @ enD gs3)
adamc@149 2129 end
adamc@493 2130 | L.ECutMulti (e, c) =>
adamc@493 2131 let
adamc@493 2132 val (e', et, gs1) = elabExp (env, denv) e
adamc@493 2133 val (c', ck, gs2) = elabCon (env, denv) c
adamc@493 2134
adamc@493 2135 val rest = cunif (loc, ktype_record)
adamc@629 2136
adamc@629 2137 val () = checkCon env e' et
adamc@629 2138 (L'.TRecord (L'.CConcat (c', rest), loc), loc)
adamc@493 2139
adamc@628 2140 val gs3 = D.prove env denv (c', rest, loc)
adamc@493 2141 in
adam@1299 2142 checkKind env c' ck (L'.KRecord ktype, loc);
adamc@493 2143 ((L'.ECutMulti (e', c', {rest = rest}), loc), (L'.TRecord rest, loc),
adamc@628 2144 gs1 @ enD gs2 @ enD gs3)
adamc@493 2145 end
adamc@149 2146
adamc@171 2147 | L.ECase (e, pes) =>
adamc@171 2148 let
adamc@171 2149 val (e', et, gs1) = elabExp (env, denv) e
adamc@171 2150 val result = cunif (loc, (L'.KType, loc))
adamc@171 2151 val (pes', gs) = ListUtil.foldlMap
adamc@171 2152 (fn ((p, e), gs) =>
adamc@171 2153 let
adamc@628 2154 val ((p', pt), (env, _)) = elabPat (p, (env, SS.empty))
adamc@628 2155
adamc@629 2156 val (e', et', gs1) = elabExp (env, denv) e
adamc@171 2157 in
adamc@628 2158 checkPatCon env p' pt et;
adamc@629 2159 checkCon env e' et' result;
adamc@628 2160 ((p', e'), gs1 @ gs)
adamc@171 2161 end)
adamc@171 2162 gs1 pes
adamc@171 2163 in
adamc@819 2164 case exhaustive (env, et, map #1 pes', loc) of
adamc@819 2165 NONE => ()
adamc@830 2166 | SOME p => if !mayDelay then
adamc@830 2167 delayedExhaustives := (env, et, map #1 pes', loc) :: !delayedExhaustives
adamc@830 2168 else
adamc@830 2169 expError env (Inexhaustive (loc, p));
adamc@172 2170
adamc@628 2171 ((L'.ECase (e', pes', {disc = et, result = result}), loc), result, gs)
adamc@171 2172 end
adamc@447 2173
adamc@447 2174 | L.ELet (eds, e) =>
adamc@447 2175 let
adamc@447 2176 val (eds, (env, gs1)) = ListUtil.foldlMap (elabEdecl denv) (env, []) eds
adamc@447 2177 val (e, t, gs2) = elabExp (env, denv) e
adamc@447 2178 in
adamc@825 2179 ((L'.ELet (eds, e, t), loc), t, gs1 @ gs2)
adamc@447 2180 end
adamc@280 2181 in
adamc@819 2182 (*prefaces "/elabExp" [("e", SourcePrint.p_exp eAll)];*)
adamc@280 2183 r
adamc@91 2184 end
adamc@31 2185
adamc@628 2186 and elabEdecl denv (dAll as (d, loc), (env, gs)) =
adamc@447 2187 let
adamc@447 2188 val r =
adamc@447 2189 case d of
adamc@825 2190 L.EDVal (p, e) =>
adamc@447 2191 let
adamc@825 2192 val ((p', pt), (env', _)) = elabPat (p, (env, SS.empty))
adamc@825 2193 val (e', et, gs1) = elabExp (env, denv) e
adamc@825 2194
adamc@825 2195 val () = checkCon env e' et pt
adamc@825 2196
adam@1554 2197 val env' = E.patBinds env p'
adam@1554 2198 (* Redo to get proper detection of type class witnesses *)
adam@1554 2199
adamc@825 2200 val pt = normClassConstraint env pt
adamc@447 2201 in
adamc@830 2202 case exhaustive (env, et, [p'], loc) of
adamc@830 2203 NONE => ()
adamc@830 2204 | SOME p => if !mayDelay then
adamc@830 2205 delayedExhaustives := (env, et, [p'], loc) :: !delayedExhaustives
adamc@830 2206 else
adamc@830 2207 expError env (Inexhaustive (loc, p));
adamc@830 2208
adamc@825 2209 ((L'.EDVal (p', pt, e'), loc), (env', gs1 @ gs))
adamc@447 2210 end
adamc@447 2211 | L.EDValRec vis =>
adamc@447 2212 let
adamc@447 2213 fun allowable (e, _) =
adamc@447 2214 case e of
adamc@447 2215 L.EAbs _ => true
adamc@447 2216 | L.ECAbs (_, _, _, e) => allowable e
adamc@623 2217 | L.EKAbs (_, e) => allowable e
adamc@447 2218 | L.EDisjoint (_, _, e) => allowable e
adamc@447 2219 | _ => false
adamc@447 2220
adamc@447 2221 val (vis, gs) = ListUtil.foldlMap
adamc@447 2222 (fn ((x, co, e), gs) =>
adamc@447 2223 let
adamc@447 2224 val (c', _, gs1) = case co of
adamc@447 2225 NONE => (cunif (loc, ktype), ktype, [])
adamc@447 2226 | SOME c => elabCon (env, denv) c
adamc@447 2227 in
adamc@447 2228 ((x, c', e), enD gs1 @ gs)
adamc@447 2229 end) gs vis
adamc@447 2230
adamc@447 2231 val env = foldl (fn ((x, c', _), env) => E.pushERel env x c') env vis
adamc@447 2232
adamc@447 2233 val (vis, gs) = ListUtil.foldlMap (fn ((x, c', e), gs) =>
adamc@447 2234 let
adamc@447 2235 val (e', et, gs1) = elabExp (env, denv) e
adamc@447 2236 in
adamc@628 2237 checkCon env e' et c';
adamc@447 2238 if allowable e then
adamc@447 2239 ()
adamc@447 2240 else
adamc@447 2241 expError env (IllegalRec (x, e'));
adamc@628 2242 ((x, c', e'), gs1 @ gs)
adamc@447 2243 end) gs vis
adamc@447 2244 in
adamc@447 2245 ((L'.EDValRec vis, loc), (env, gs))
adamc@447 2246 end
adamc@447 2247 in
adamc@447 2248 r
adamc@447 2249 end
adamc@447 2250
adamc@42 2251 val hnormSgn = E.hnormSgn
adamc@31 2252
adamc@210 2253 fun tableOf () = (L'.CModProj (!basis_r, [], "sql_table"), ErrorMsg.dummySpan)
adamc@338 2254 fun sequenceOf () = (L'.CModProj (!basis_r, [], "sql_sequence"), ErrorMsg.dummySpan)
adamc@754 2255 fun viewOf () = (L'.CModProj (!basis_r, [], "sql_view"), ErrorMsg.dummySpan)
adamc@754 2256 fun queryOf () = (L'.CModProj (!basis_r, [], "sql_query"), ErrorMsg.dummySpan)
adamc@459 2257 fun cookieOf () = (L'.CModProj (!basis_r, [], "http_cookie"), ErrorMsg.dummySpan)
adamc@718 2258 fun styleOf () = (L'.CModProj (!basis_r, [], "css_class"), ErrorMsg.dummySpan)
adamc@205 2259
adamc@469 2260 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@469 2261 case E.lookupStr env str of
adamc@469 2262 NONE => (strError env (UnboundStr (loc, str));
adamc@469 2263 denv)
adamc@469 2264 | SOME (n, sgn) =>
adamc@469 2265 let
adamc@469 2266 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@469 2267 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@469 2268 NONE => (strError env (UnboundStr (loc, m));
adamc@469 2269 (strerror, sgnerror))
adamc@469 2270 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@469 2271 ((L'.StrVar n, loc), sgn) strs
adamc@1030 2272
adamc@1030 2273 fun collect first (st, sgn) =
adamc@1030 2274 case E.projectConstraints env {sgn = sgn, str = st} of
adamc@1182 2275 NONE => []
adamc@1030 2276 | SOME cs =>
adamc@1030 2277 case #1 (hnormSgn env sgn) of
adamc@1030 2278 L'.SgnConst sgis =>
adamc@1030 2279 foldl (fn (sgi, cs) =>
adamc@1030 2280 case #1 sgi of
adamc@1030 2281 L'.SgiStr (x, _, _) =>
adamc@1030 2282 (case E.projectStr env {sgn = sgn, str = st, field = x} of
adamc@1030 2283 NONE => raise Fail "Elaborate: projectStr in collect"
adamc@1030 2284 | SOME sgn' =>
adamc@1030 2285 List.revAppend (collect false ((L'.StrProj (st, x), loc), sgn'),
adamc@1030 2286 cs))
adamc@1030 2287 | _ => cs) cs sgis
adamc@1030 2288 | _ => cs
adamc@469 2289 in
adamc@1030 2290 foldl (fn ((c1, c2), denv) =>
adamc@1030 2291 D.assert env denv (c1, c2)) denv (collect true (st, sgn))
adamc@469 2292 end
adamc@469 2293
adamc@88 2294 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@1270 2295 ((*Print.preface ("elabSgi", SourcePrint.p_sgn_item (sgi, loc));*)
adamc@1270 2296 case sgi of
adamc@1270 2297 L.SgiConAbs (x, k) =>
adamc@1270 2298 let
adamc@1270 2299 val k' = elabKind env k
adamc@1270 2300
adamc@1270 2301 val (env', n) = E.pushCNamed env x k' NONE
adamc@1270 2302 in
adamc@1270 2303 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@1270 2304 end
adamc@1270 2305
adamc@1270 2306 | L.SgiCon (x, ko, c) =>
adamc@1270 2307 let
adamc@1270 2308 val k' = case ko of
adamc@1270 2309 NONE => kunif loc
adamc@1270 2310 | SOME k => elabKind env k
adamc@1270 2311
adamc@1270 2312 val (c', ck, gs') = elabCon (env, denv) c
adamc@1270 2313 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@1270 2314 in
adamc@1270 2315 checkKind env c' ck k';
adamc@1270 2316
adamc@1270 2317 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@1270 2318 end
adamc@1270 2319
adamc@1270 2320 | L.SgiDatatype dts =>
adamc@1270 2321 let
adamc@1270 2322 val k = (L'.KType, loc)
adamc@1270 2323
adamc@1270 2324 val (dts, env) = ListUtil.foldlMap (fn ((x, xs, xcs), env) =>
adamc@1270 2325 let
adamc@1270 2326 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@1270 2327 val (env, n) = E.pushCNamed env x k' NONE
adamc@1270 2328 in
adamc@1270 2329 ((x, n, xs, xcs), env)
adamc@1270 2330 end)
adamc@1270 2331 env dts
adamc@1270 2332
adamc@1270 2333 val (dts, env) = ListUtil.foldlMap
adamc@1270 2334 (fn ((x, n, xs, xcs), env) =>
adamc@1270 2335 let
adamc@1270 2336 val t = (L'.CNamed n, loc)
adamc@1270 2337 val nxs = length xs - 1
adamc@1270 2338 val t = ListUtil.foldli (fn (i, _, t) =>
adamc@1270 2339 (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@1270 2340
adamc@1270 2341 val (env', denv') = foldl (fn (x, (env', denv')) =>
adamc@1270 2342 (E.pushCRel env' x k,
adamc@1270 2343 D.enter denv')) (env, denv) xs
adamc@1270 2344
adamc@1270 2345 val (xcs, (used, env, gs)) =
adamc@1270 2346 ListUtil.foldlMap
adamc@1270 2347 (fn ((x, to), (used, env, gs)) =>
adamc@1270 2348 let
adamc@1270 2349 val (to, t, gs') = case to of
adamc@1270 2350 NONE => (NONE, t, gs)
adamc@1270 2351 | SOME t' =>
adamc@1270 2352 let
adamc@1270 2353 val (t', tk, gs') =
adamc@1270 2354 elabCon (env', denv') t'
adamc@1270 2355 in
adamc@1270 2356 checkKind env' t' tk k;
adamc@1270 2357 (SOME t',
adamc@1270 2358 (L'.TFun (t', t), loc),
adamc@1270 2359 gs' @ gs)
adamc@1270 2360 end
adamc@1270 2361 val t = foldl (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@1270 2362 t xs
adamc@1270 2363
adamc@1270 2364 val (env, n') = E.pushENamed env x t
adamc@1270 2365 in
adamc@1270 2366 if SS.member (used, x) then
adamc@1270 2367 strError env (DuplicateConstructor (x, loc))
adamc@1270 2368 else
adamc@1270 2369 ();
adamc@1270 2370 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@1270 2371 end)
adamc@1270 2372 (SS.empty, env, []) xcs
adamc@1270 2373 in
adamc@1270 2374 ((x, n, xs, xcs), E.pushDatatype env n xs xcs)
adamc@1270 2375 end)
adamc@1270 2376 env dts
adamc@1270 2377 in
adamc@1270 2378 ([(L'.SgiDatatype dts, loc)], (env, denv, gs))
adamc@1270 2379 end
adamc@1270 2380
adamc@1270 2381 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
adamc@1270 2382
adamc@1270 2383 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
adamc@1270 2384 (case E.lookupStr env m1 of
adamc@1270 2385 NONE => (strError env (UnboundStr (loc, m1));
adamc@1270 2386 ([], (env, denv, gs)))
adamc@1270 2387 | SOME (n, sgn) =>
adamc@1270 2388 let
adamc@1270 2389 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@1270 2390 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@1270 2391 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@1270 2392 (strerror, sgnerror))
adamc@1270 2393 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@1270 2394 ((L'.StrVar n, loc), sgn) ms
adamc@1270 2395 in
adamc@1270 2396 case hnormCon env (L'.CModProj (n, ms, s), loc) of
adamc@1270 2397 (L'.CModProj (n, ms, s), _) =>
adamc@1270 2398 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@1270 2399 NONE => (conError env (UnboundDatatype (loc, s));
adamc@1270 2400 ([], (env, denv, [])))
adamc@1270 2401 | SOME (xs, xncs) =>
adamc@1270 2402 let
adamc@1270 2403 val k = (L'.KType, loc)
adamc@1270 2404 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@1270 2405
adamc@1270 2406 val t = (L'.CModProj (n, ms, s), loc)
adamc@1270 2407 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@1270 2408 val env = E.pushDatatype env n' xs xncs
adamc@1270 2409
adamc@1270 2410 val t = (L'.CNamed n', loc)
adamc@1270 2411 val env = foldl (fn ((x, n, to), env) =>
adamc@805 2412 let
adamc@1270 2413 val t = case to of
adamc@1270 2414 NONE => t
adamc@1270 2415 | SOME t' => (L'.TFun (t', t), loc)
adamc@1270 2416
adamc@1270 2417 val t = foldr (fn (x, t) =>
adamc@1270 2418 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@1270 2419 t xs
adamc@805 2420 in
adamc@1270 2421 E.pushENamedAs env x n t
adamc@1270 2422 end) env xncs
adamc@1270 2423 in
adamc@1270 2424 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, []))
adamc@1270 2425 end)
adamc@1270 2426 | _ => (strError env (NotDatatype loc);
adamc@1270 2427 ([], (env, denv, [])))
adamc@1270 2428 end)
adamc@1270 2429
adamc@1270 2430 | L.SgiVal (x, c) =>
adamc@1270 2431 let
adamc@1270 2432 val (c', ck, gs') = elabCon (env, denv) c
adamc@1270 2433
adamc@1270 2434 val old = c'
adamc@1270 2435 val c' = normClassConstraint env c'
adamc@1270 2436 val (env', n) = E.pushENamed env x c'
adamc@1270 2437 in
adamc@1270 2438 (unifyKinds env ck ktype
adamc@1270 2439 handle KUnify ue => strError env (NotType (loc, ck, ue)));
adamc@1270 2440
adamc@1270 2441 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@1270 2442 end
adamc@1270 2443
adamc@1270 2444 | L.SgiTable (x, c, pe, ce) =>
adamc@1270 2445 let
adamc@1270 2446 val cstK = (L'.KRecord (L'.KRecord (L'.KUnit, loc), loc), loc)
adamc@1270 2447
adamc@1270 2448 val (c', ck, gs') = elabCon (env, denv) c
adamc@1270 2449 val pkey = cunif (loc, cstK)
adamc@1270 2450 val visible = cunif (loc, cstK)
adamc@1270 2451 val (env', ds, uniques) =
adamc@1270 2452 case (#1 pe, #1 ce) of
adamc@1270 2453 (L.EVar (["Basis"], "no_primary_key", _), L.EVar (["Basis"], "no_constraint", _)) =>
adamc@1270 2454 let
adamc@1270 2455 val x' = x ^ "_hidden_constraints"
adamc@1270 2456 val (env', hidden_n) = E.pushCNamed env x' cstK NONE
adamc@1270 2457 val hidden = (L'.CNamed hidden_n, loc)
adamc@1270 2458 in
adamc@1270 2459 (env', [(L'.SgiConAbs (x', hidden_n, cstK), loc)], (L'.CConcat (visible, hidden), loc))
adamc@1270 2460 end
adamc@1270 2461 | _ => (env, [], visible)
adamc@1270 2462
adamc@1270 2463 val ct = tableOf ()
adamc@1270 2464 val ct = (L'.CApp (ct, c'), loc)
adamc@1270 2465 val ct = (L'.CApp (ct, (L'.CConcat (pkey, uniques), loc)), loc)
adamc@1270 2466
adamc@1270 2467 val (pe', pet, gs'') = elabExp (env', denv) pe
adamc@1270 2468 val gs'' = List.mapPartial (fn Disjoint x => SOME x
adamc@707 2469 | _ => NONE) gs''
adamc@707 2470
adamc@1270 2471 val pst = (L'.CModProj (!basis_r, [], "primary_key"), loc)
adamc@1270 2472 val pst = (L'.CApp (pst, c'), loc)
adamc@1270 2473 val pst = (L'.CApp (pst, pkey), loc)
adamc@1270 2474
adamc@1270 2475 val (env', n) = E.pushENamed env' x ct
adamc@1270 2476
adamc@1270 2477 val (ce', cet, gs''') = elabExp (env', denv) ce
adamc@1270 2478 val gs''' = List.mapPartial (fn Disjoint x => SOME x
adamc@1270 2479 | _ => NONE) gs'''
adamc@1270 2480
adamc@1270 2481 val cst = (L'.CModProj (!basis_r, [], "sql_constraints"), loc)
adamc@1270 2482 val cst = (L'.CApp (cst, c'), loc)
adamc@1270 2483 val cst = (L'.CApp (cst, visible), loc)
adamc@1270 2484 in
adamc@1270 2485 checkKind env c' ck (L'.KRecord (L'.KType, loc), loc);
adamc@1270 2486 checkCon env' pe' pet pst;
adamc@1270 2487 checkCon env' ce' cet cst;
adamc@1270 2488
adamc@1270 2489 (ds @ [(L'.SgiVal (x, n, ct), loc)], (env', denv, gs''' @ gs'' @ gs' @ gs))
adamc@1270 2490 end
adamc@1270 2491
adamc@1270 2492 | L.SgiStr (x, sgn) =>
adamc@1270 2493 let
adamc@1270 2494 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@1270 2495 val (env', n) = E.pushStrNamed env x sgn'
adamc@1270 2496 in
adamc@1270 2497 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@1270 2498 end
adamc@1270 2499
adamc@1270 2500 | L.SgiSgn (x, sgn) =>
adamc@1270 2501 let
adamc@1270 2502 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@1270 2503 val (env', n) = E.pushSgnNamed env x sgn'
adamc@1270 2504 in
adamc@1270 2505 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@1270 2506 end
adamc@1270 2507
adamc@1270 2508 | L.SgiInclude sgn =>
adamc@1270 2509 let
adamc@1270 2510 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@1270 2511 in
adamc@1270 2512 case #1 (hnormSgn env sgn') of
adamc@1270 2513 L'.SgnConst sgis =>
adamc@1270 2514 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@1270 2515 | _ => (sgnError env (NotIncludable sgn');
adamc@1270 2516 ([], (env, denv, [])))
adamc@1270 2517 end
adamc@1270 2518
adamc@1270 2519 | L.SgiConstraint (c1, c2) =>
adamc@1270 2520 let
adamc@1270 2521 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@1270 2522 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@1270 2523
adamc@1270 2524 val denv = D.assert env denv (c1', c2')
adamc@1270 2525 in
adamc@1270 2526 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@1270 2527 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@1270 2528
adamc@1270 2529 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2))
adamc@1270 2530 end
adamc@1270 2531
adamc@1270 2532 | L.SgiClassAbs (x, k) =>
adamc@1270 2533 let
adamc@1270 2534 val k = elabKind env k
adamc@1270 2535 val (env, n) = E.pushCNamed env x k NONE
adamc@1270 2536 val env = E.pushClass env n
adamc@1270 2537 in
adamc@1270 2538 ([(L'.SgiClassAbs (x, n, k), loc)], (env, denv, []))
adamc@1270 2539 end
adamc@1270 2540
adamc@1270 2541 | L.SgiClass (x, k, c) =>
adamc@1270 2542 let
adamc@1270 2543 val k = elabKind env k
adamc@1270 2544 val (c', ck, gs) = elabCon (env, denv) c
adamc@1270 2545 val (env, n) = E.pushCNamed env x k (SOME c')
adamc@1270 2546 val env = E.pushClass env n
adamc@1270 2547 in
adamc@1270 2548 checkKind env c' ck k;
adamc@1270 2549 ([(L'.SgiClass (x, n, k, c'), loc)], (env, denv, []))
adamc@1270 2550 end)
adamc@211 2551
adamc@83 2552 and elabSgn (env, denv) (sgn, loc) =
adamc@31 2553 case sgn of
adamc@31 2554 L.SgnConst sgis =>
adamc@31 2555 let
adamc@88 2556 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 2557
adamc@62 2558 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 2559 case sgi of
adamc@62 2560 L'.SgiConAbs (x, _, _) =>
adamc@62 2561 (if SS.member (cons, x) then
adamc@62 2562 sgnError env (DuplicateCon (loc, x))
adamc@62 2563 else
adamc@62 2564 ();
adamc@62 2565 (SS.add (cons, x), vals, sgns, strs))
adamc@62 2566 | L'.SgiCon (x, _, _, _) =>
adamc@62 2567 (if SS.member (cons, x) then
adamc@62 2568 sgnError env (DuplicateCon (loc, x))
adamc@62 2569 else
adamc@62 2570 ();
adamc@62 2571 (SS.add (cons, x), vals, sgns, strs))
adamc@805 2572 | L'.SgiDatatype dts =>
adamc@156 2573 let
adamc@805 2574 val (cons, vals) =
adamc@805 2575 let
adamc@805 2576 fun doOne ((x, _, _, xncs), (cons, vals)) =
adamc@805 2577 let
adamc@805 2578 val vals = foldl (fn ((x, _, _), vals) =>
adamc@805 2579 (if SS.member (vals, x) then
adamc@805 2580 sgnError env (DuplicateVal (loc, x))
adamc@805 2581 else
adamc@805 2582 ();
adamc@805 2583 SS.add (vals, x)))
adamc@805 2584 vals xncs
adamc@805 2585 in
adamc@805 2586 if SS.member (cons, x) then
adamc@805 2587 sgnError env (DuplicateCon (loc, x))
adamc@805 2588 else
adamc@805 2589 ();
adamc@805 2590 (SS.add (cons, x), vals)
adamc@805 2591 end
adamc@805 2592 in
adamc@805 2593 foldl doOne (cons, vals) dts
adamc@805 2594 end
adamc@156 2595 in
adamc@805 2596 (cons, vals, sgns, strs)
adamc@156 2597 end
adamc@191 2598 | L'.SgiDatatypeImp (x, _, _, _, _, _, _) =>
adamc@156 2599 (if SS.member (cons, x) then
adamc@156 2600 sgnError env (DuplicateCon (loc, x))
adamc@156 2601 else
adamc@156 2602 ();
adamc@156 2603 (SS.add (cons, x), vals, sgns, strs))
adamc@62 2604 | L'.SgiVal (x, _, _) =>
adamc@62 2605 (if SS.member (vals, x) then
adamc@62 2606 sgnError env (DuplicateVal (loc, x))
adamc@62 2607 else
adamc@62 2608 ();
adamc@62 2609 (cons, SS.add (vals, x), sgns, strs))
adamc@62 2610 | L'.SgiSgn (x, _, _) =>
adamc@62 2611 (if SS.member (sgns, x) then
adamc@62 2612 sgnError env (DuplicateSgn (loc, x))
adamc@62 2613 else
adamc@62 2614 ();
adamc@62 2615 (cons, vals, SS.add (sgns, x), strs))
adamc@62 2616 | L'.SgiStr (x, _, _) =>
adamc@62 2617 (if SS.member (strs, x) then
adamc@62 2618 sgnError env (DuplicateStr (loc, x))
adamc@62 2619 else
adamc@62 2620 ();
adamc@88 2621 (cons, vals, sgns, SS.add (strs, x)))
adamc@203 2622 | L'.SgiConstraint _ => (cons, vals, sgns, strs)
adamc@563 2623 | L'.SgiClassAbs (x, _, _) =>
adamc@211 2624 (if SS.member (cons, x) then
adamc@211 2625 sgnError env (DuplicateCon (loc, x))
adamc@211 2626 else
adamc@211 2627 ();
adamc@211 2628 (SS.add (cons, x), vals, sgns, strs))
adamc@563 2629 | L'.SgiClass (x, _, _, _) =>
adamc@211 2630 (if SS.member (cons, x) then
adamc@211 2631 sgnError env (DuplicateCon (loc, x))
adamc@211 2632 else
adamc@211 2633 ();
adamc@460 2634 (SS.add (cons, x), vals, sgns, strs)))
adamc@62 2635 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 2636 in
adamc@83 2637 ((L'.SgnConst sgis', loc), gs)
adamc@31 2638 end
adamc@31 2639 | L.SgnVar x =>
adamc@31 2640 (case E.lookupSgn env x of
adamc@31 2641 NONE =>
adamc@31 2642 (sgnError env (UnboundSgn (loc, x));
adamc@83 2643 ((L'.SgnError, loc), []))
adamc@83 2644 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 2645 | L.SgnFun (m, dom, ran) =>
adamc@41 2646 let
adamc@83 2647 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2648 val (env', n) = E.pushStrNamed env m dom'
adamc@469 2649 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = []}
adamc@469 2650 val (ran', gs2) = elabSgn (env', denv') ran
adamc@41 2651 in
adamc@83 2652 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 2653 end
adamc@42 2654 | L.SgnWhere (sgn, x, c) =>
adamc@42 2655 let
adamc@83 2656 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 2657 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 2658 in
adamc@42 2659 case #1 (hnormSgn env sgn') of
adamc@83 2660 L'.SgnError => (sgnerror, [])
adamc@42 2661 | L'.SgnConst sgis =>
adamc@75 2662 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 2663 x' = x andalso
adamc@623 2664 (unifyKinds env k ck
adamc@75 2665 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 2666 true)
adamc@42 2667 | _ => false) sgis then
adamc@83 2668 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 2669 else
adamc@42 2670 (sgnError env (UnWhereable (sgn', x));
adamc@83 2671 (sgnerror, []))
adamc@42 2672 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 2673 (sgnerror, []))
adamc@42 2674 end
adamc@59 2675 | L.SgnProj (m, ms, x) =>
adamc@59 2676 (case E.lookupStr env m of
adamc@59 2677 NONE => (strError env (UnboundStr (loc, m));
adamc@83 2678 (sgnerror, []))
adamc@59 2679 | SOME (n, sgn) =>
adamc@59 2680 let
adamc@59 2681 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 2682 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 2683 NONE => (strError env (UnboundStr (loc, m));
adamc@59 2684 (strerror, sgnerror))
adamc@59 2685 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 2686 ((L'.StrVar n, loc), sgn) ms
adamc@59 2687 in
adamc@59 2688 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 2689 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 2690 (sgnerror, []))
adamc@83 2691 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 2692 end)
adamc@59 2693
adamc@31 2694
adamc@706 2695 and selfify env {str, strs, sgn} =
adamc@66 2696 case #1 (hnormSgn env sgn) of
adamc@66 2697 L'.SgnError => sgn
adamc@66 2698 | L'.SgnVar _ => sgn
adamc@66 2699
adamc@66 2700 | L'.SgnConst sgis =>
adam@1387 2701 (L'.SgnConst (#1 (ListUtil.foldlMapConcat
adam@1387 2702 (fn (sgi, env) =>
adam@1387 2703 (case sgi of (L'.SgiConAbs (x, n, k), loc) =>
adam@1387 2704 [(L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)]
adam@1387 2705 | (L'.SgiDatatype dts, loc) =>
adam@1387 2706 map (fn (x, n, xs, xncs) => (L'.SgiDatatypeImp (x, n, str, strs, x, xs, xncs), loc)) dts
adam@1387 2707 | (L'.SgiClassAbs (x, n, k), loc) =>
adam@1387 2708 [(L'.SgiClass (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)]
adam@1387 2709 | (L'.SgiStr (x, n, sgn), loc) =>
adam@1387 2710 [(L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)]
adam@1387 2711 | x => [x],
adam@1387 2712 E.sgiBinds env sgi)) env sgis)), #2 sgn)
adamc@66 2713 | L'.SgnFun _ => sgn
adamc@66 2714 | L'.SgnWhere _ => sgn
adamc@66 2715 | L'.SgnProj (m, ms, x) =>
adamc@66 2716 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2717 (L'.StrVar m, #2 sgn) ms,
adamc@66 2718 sgn = #2 (E.lookupStrNamed env m),
adamc@66 2719 field = x} of
adamc@66 2720 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 2721 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 2722
adamc@706 2723 and selfifyAt env {str, sgn} =
adamc@66 2724 let
adamc@66 2725 fun self (str, _) =
adamc@66 2726 case str of
adamc@66 2727 L'.StrVar x => SOME (x, [])
adamc@66 2728 | L'.StrProj (str, x) =>
adamc@66 2729 (case self str of
adamc@66 2730 NONE => NONE
adamc@66 2731 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 2732 | _ => NONE
adamc@66 2733 in
adamc@66 2734 case self str of
adamc@66 2735 NONE => sgn
adamc@66 2736 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 2737 end
adamc@66 2738
adamc@706 2739 and dopen env {str, strs, sgn} =
adamc@66 2740 let
adam@1527 2741 fun isVisible x = x <> "" andalso String.sub (x, 0) <> #"?"
adam@1527 2742
adamc@66 2743 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2744 (L'.StrVar str, #2 sgn) strs
adamc@66 2745 in
adamc@66 2746 case #1 (hnormSgn env sgn) of
adamc@66 2747 L'.SgnConst sgis =>
adamc@805 2748 ListUtil.foldlMapConcat
adamc@805 2749 (fn ((sgi, loc), env') =>
adamc@805 2750 let
adamc@805 2751 val d =
adamc@805 2752 case sgi of
adamc@805 2753 L'.SgiConAbs (x, n, k) =>
adam@1527 2754 if isVisible x then
adam@1527 2755 let
adam@1527 2756 val c = (L'.CModProj (str, strs, x), loc)
adam@1527 2757 in
adam@1527 2758 [(L'.DCon (x, n, k, c), loc)]
adam@1527 2759 end
adam@1527 2760 else
adam@1527 2761 []
adamc@805 2762 | L'.SgiCon (x, n, k, c) =>
adam@1527 2763 if isVisible x then
adam@1527 2764 [(L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)]
adam@1527 2765 else
adam@1527 2766 []
adamc@805 2767 | L'.SgiDatatype dts =>
adam@1527 2768 List.mapPartial (fn (x, n, xs, xncs) => if isVisible x then
adam@1527 2769 SOME (L'.DDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
adam@1527 2770 else
adam@1527 2771 NONE) dts
adamc@805 2772 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adam@1527 2773 if isVisible x then
adam@1527 2774 [(L'.DDatatypeImp (x, n, m1, ms, x', xs, xncs), loc)]
adam@1527 2775 else
adam@1527 2776 []
adamc@805 2777 | L'.SgiVal (x, n, t) =>
adam@1527 2778 if isVisible x then
adam@1527 2779 [(L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)]
adam@1527 2780 else
adam@1527 2781 []
adamc@805 2782 | L'.SgiStr (x, n, sgn) =>
adam@1527 2783 if isVisible x then
adam@1527 2784 [(L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)]
adam@1527 2785 else
adam@1527 2786 []
adamc@805 2787 | L'.SgiSgn (x, n, sgn) =>
adam@1527 2788 if isVisible x then
adam@1527 2789 [(L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)]
adam@1527 2790 else
adam@1527 2791 []
adamc@805 2792 | L'.SgiConstraint (c1, c2) =>
adamc@805 2793 [(L'.DConstraint (c1, c2), loc)]
adamc@805 2794 | L'.SgiClassAbs (x, n, k) =>
adam@1527 2795 if isVisible x then
adam@1527 2796 let
adam@1527 2797 val c = (L'.CModProj (str, strs, x), loc)
adam@1527 2798 in
adam@1527 2799 [(L'.DCon (x, n, k, c), loc)]
adam@1527 2800 end
adam@1527 2801 else
adam@1527 2802 []
adamc@805 2803 | L'.SgiClass (x, n, k, _) =>
adam@1527 2804 if isVisible x then
adam@1527 2805 let
adam@1527 2806 val c = (L'.CModProj (str, strs, x), loc)
adam@1527 2807 in
adam@1527 2808 [(L'.DCon (x, n, k, c), loc)]
adam@1527 2809 end
adam@1527 2810 else
adam@1527 2811 []
adamc@805 2812 in
adamc@805 2813 (d, foldl (fn (d, env') => E.declBinds env' d) env' d)
adamc@805 2814 end)
adamc@805 2815 env sgis
adamc@66 2816 | _ => (strError env (UnOpenable sgn);
adamc@628 2817 ([], env))
adamc@66 2818 end
adamc@66 2819
adamc@706 2820 and sgiOfDecl (d, loc) =
adamc@31 2821 case d of
adamc@123 2822 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@156 2823 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
adamc@156 2824 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
adamc@123 2825 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 2826 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 2827 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 2828 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2829 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2830 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 2831 | L'.DExport _ => []
adamc@707 2832 | L'.DTable (tn, x, n, c, _, pc, _, cc) =>
adamc@707 2833 [(L'.SgiVal (x, n, (L'.CApp ((L'.CApp (tableOf (), c), loc),
adamc@707 2834 (L'.CConcat (pc, cc), loc)), loc)), loc)]
adamc@460 2835 | L'.DSequence (tn, x, n) => [(L'.SgiVal (x, n, sequenceOf ()), loc)]
adamc@754 2836 | L'.DView (tn, x, n, _, c) =>
adamc@754 2837 [(L'.SgiVal (x, n, (L'.CApp (viewOf (), c), loc)), loc)]
adamc@563 2838 | L'.DClass (x, n, k, c) => [(L'.SgiClass (x, n, k, c), loc)]
adamc@271 2839 | L'.DDatabase _ => []
adamc@460 2840 | L'.DCookie (tn, x, n, c) => [(L'.SgiVal (x, n, (L'.CApp (cookieOf (), c), loc)), loc)]
adamc@720 2841 | L'.DStyle (tn, x, n) => [(L'.SgiVal (x, n, styleOf ()), loc)]
adamc@1075 2842 | L'.DTask _ => []
adamc@1199 2843 | L'.DPolicy _ => []
adam@1294 2844 | L'.DOnError _ => []
adamc@88 2845
adamc@1000 2846 and subSgn' counterparts env strLoc sgn1 (sgn2 as (_, loc2)) =
adamc@469 2847 ((*prefaces "subSgn" [("sgn1", p_sgn env sgn1),
adamc@469 2848 ("sgn2", p_sgn env sgn2)];*)
adamc@35 2849 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 2850 (L'.SgnError, _) => ()
adamc@31 2851 | (_, L'.SgnError) => ()
adamc@31 2852
adamc@31 2853 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 2854 let
adamc@362 2855 (*val () = prefaces "subSgn" [("sgn1", p_sgn env sgn1),
adamc@362 2856 ("sgn2", p_sgn env sgn2),
adamc@362 2857 ("sgis1", p_sgn env (L'.SgnConst sgis1, loc2)),
adamc@362 2858 ("sgis2", p_sgn env (L'.SgnConst sgis2, loc2))]*)
adamc@362 2859
adamc@838 2860 fun cpart n = IM.find (!counterparts, n)
adamc@838 2861 fun cparts (n2, n1) = counterparts := IM.insert (!counterparts, n2, n1)
adamc@838 2862
adamc@838 2863 val sub2 = U.Con.map {kind = fn k => k,
adamc@838 2864 con = fn c =>
adamc@838 2865 case c of
adamc@838 2866 L'.CNamed n2 =>
adamc@838 2867 (case cpart n2 of
adamc@838 2868 NONE => c
adamc@838 2869 | SOME n1 => L'.CNamed n1)
adamc@838 2870 | _ => c}
adamc@838 2871
adamc@628 2872 fun folder (sgi2All as (sgi, loc), env) =
adamc@31 2873 let
adamc@362 2874 (*val () = prefaces "folder" [("sgis1", p_sgn env (L'.SgnConst sgis1, loc2))]*)
adamc@362 2875
adamc@805 2876 fun seek' f p =
adamc@31 2877 let
adamc@628 2878 fun seek env ls =
adamc@31 2879 case ls of
adamc@805 2880 [] => f env
adamc@31 2881 | h :: t =>
adamc@469 2882 case p (env, h) of
adamc@469 2883 NONE =>
adamc@469 2884 let
adamc@469 2885 val env = case #1 h of
adamc@469 2886 L'.SgiCon (x, n, k, c) =>
adamc@471 2887 if E.checkENamed env n then
adamc@471 2888 env
adamc@471 2889 else
adamc@471 2890 E.pushCNamedAs env x n k (SOME c)
adamc@469 2891 | L'.SgiConAbs (x, n, k) =>
adamc@471 2892 if E.checkENamed env n then
adamc@471 2893 env
adamc@471 2894 else
adamc@471 2895 E.pushCNamedAs env x n k NONE
adamc@469 2896 | _ => env
adamc@469 2897 in
adamc@628 2898 seek (E.sgiBinds env h) t
adamc@469 2899 end
adamc@88 2900 | SOME envs => envs
adamc@31 2901 in
adamc@628 2902 seek env sgis1
adamc@31 2903 end
adamc@805 2904
adamc@1000 2905 val seek = seek' (fn env => (sgnError env (UnmatchedSgi (strLoc, sgi2All));
adamc@805 2906 env))
adamc@31 2907 in
adamc@31 2908 case sgi of
adamc@31 2909 L'.SgiConAbs (x, n2, k2) =>
adamc@469 2910 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@31 2911 let
adamc@41 2912 fun found (x', n1, k1, co1) =
adamc@41 2913 if x = x' then
adamc@41 2914 let
adamc@623 2915 val () = unifyKinds env k1 k2
adamc@41 2916 handle KUnify (k1, k2, err) =>
adamc@1000 2917 sgnError env (SgiWrongKind (strLoc, sgi1All, k1,
adamc@1000 2918 sgi2All, k2, err))
adamc@41 2919 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 2920 in
adamc@41 2921 SOME (if n1 = n2 then
adamc@41 2922 env
adamc@41 2923 else
adamc@838 2924 (cparts (n2, n1);
adamc@838 2925 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2))))
adamc@41 2926 end
adamc@41 2927 else
adamc@41 2928 NONE
adamc@31 2929 in
adamc@31 2930 case sgi1 of
adamc@41 2931 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 2932 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@805 2933 | L'.SgiDatatype dts =>
adamc@191 2934 let
adamc@191 2935 val k = (L'.KType, loc)
adamc@805 2936
adamc@805 2937 fun search dts =
adamc@805 2938 case dts of
adamc@805 2939 [] => NONE
adamc@805 2940 | (x', n1, xs, _) :: dts =>
adamc@805 2941 let
adamc@805 2942 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@805 2943 in
adamc@805 2944 case found (x', n1, k', NONE) of
adamc@805 2945 NONE => search dts
adamc@805 2946 | x => x
adamc@805 2947 end
adamc@191 2948 in
adamc@805 2949 search dts
adamc@191 2950 end
adamc@191 2951 | L'.SgiDatatypeImp (x', n1, m1, ms, s, xs, _) =>
adamc@191 2952 let
adamc@191 2953 val k = (L'.KType, loc)
adamc@191 2954 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2955 in
adamc@191 2956 found (x', n1, k', SOME (L'.CModProj (m1, ms, s), loc))
adamc@191 2957 end
adamc@711 2958 | L'.SgiClassAbs (x', n1, k) => found (x', n1, k, NONE)
adamc@711 2959 | L'.SgiClass (x', n1, k, c) => found (x', n1, k, SOME c)
adamc@31 2960 | _ => NONE
adamc@31 2961 end)
adamc@31 2962
adamc@31 2963 | L'.SgiCon (x, n2, k2, c2) =>
adamc@469 2964 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 2965 let
adamc@211 2966 fun found (x', n1, k1, c1) =
adamc@211 2967 if x = x' then
adamc@211 2968 let
adamc@838 2969 val c2 = sub2 c2
adamc@838 2970
adamc@327 2971 fun good () =
adamc@327 2972 let
adamc@327 2973 val env = E.pushCNamedAs env x n2 k2 (SOME c2)
adamc@327 2974 val env = if n1 = n2 then
adamc@327 2975 env
adamc@327 2976 else
adamc@839 2977 (cparts (n2, n1);
adamc@839 2978 E.pushCNamedAs env x n1 k1 (SOME c1))
adamc@327 2979 in
adamc@628 2980 SOME env
adamc@327 2981 end
adamc@211 2982 in
adamc@987 2983 (unifyCons env loc c1 c2;
adamc@628 2984 good ())
adamc@211 2985 handle CUnify (c1, c2, err) =>
adamc@1000 2986 (sgnError env (SgiWrongCon (strLoc, sgi1All, c1,
adamc@1000 2987 sgi2All, c2, err));
adamc@211 2988 good ())
adamc@211 2989 end
adamc@211 2990 else
adamc@211 2991 NONE
adamc@211 2992 in
adamc@211 2993 case sgi1 of
adamc@211 2994 L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@711 2995 | L'.SgiClass (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@211 2996 | _ => NONE
adamc@211 2997 end)
adamc@31 2998
adamc@805 2999 | L'.SgiDatatype dts2 =>
adamc@805 3000 let
adamc@805 3001 fun found' (sgi1All, (x1, n1, xs1, xncs1), (x2, n2, xs2, xncs2), env) =
adamc@805 3002 if x1 <> x2 then
adamc@805 3003 NONE
adamc@805 3004 else
adamc@805 3005 let
adamc@805 3006 fun mismatched ue =
adamc@1000 3007 (sgnError env (SgiMismatchedDatatypes (strLoc, sgi1All, sgi2All, ue));
adamc@805 3008 SOME env)
adamc@805 3009
adamc@805 3010 val k = (L'.KType, loc)
adamc@805 3011 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs1
adamc@805 3012
adamc@805 3013 fun good () =
adamc@805 3014 let
adamc@805 3015 val env = E.sgiBinds env sgi1All
adamc@805 3016 val env = if n1 = n2 then
adamc@805 3017 env
adamc@805 3018 else
adamc@838 3019 (cparts (n2, n1);
adamc@838 3020 E.pushCNamedAs env x1 n2 k'
adamc@838 3021 (SOME (L'.CNamed n1, loc)))
adamc@805 3022 in
adamc@805 3023 SOME env
adamc@805 3024 end
adamc@805 3025
adamc@805 3026 val env = E.pushCNamedAs env x1 n1 k' NONE
adamc@805 3027 val env = if n1 = n2 then
adamc@805 3028 env
adamc@805 3029 else
adamc@838 3030 (cparts (n2, n1);
adamc@838 3031 E.pushCNamedAs env x1 n2 k' (SOME (L'.CNamed n1, loc)))
adamc@805 3032 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs1
adamc@805 3033 fun xncBad ((x1, _, t1), (x2, _, t2)) =
adamc@805 3034 String.compare (x1, x2) <> EQUAL
adamc@805 3035 orelse case (t1, t2) of
adamc@805 3036 (NONE, NONE) => false
adamc@805 3037 | (SOME t1, SOME t2) =>
adamc@987 3038 (unifyCons env loc t1 (sub2 t2); false)
adamc@805 3039 | _ => true
adamc@805 3040 in
adamc@805 3041 (if xs1 <> xs2
adamc@805 3042 orelse length xncs1 <> length xncs2
adamc@805 3043 orelse ListPair.exists xncBad (xncs1, xncs2) then
adamc@805 3044 mismatched NONE
adamc@160 3045 else
adamc@805 3046 good ())
adamc@805 3047 handle CUnify ue => mismatched (SOME ue)
adamc@805 3048 end
adamc@805 3049 in
adamc@805 3050 seek'
adamc@805 3051 (fn _ =>
adamc@805 3052 let
adamc@805 3053 fun seekOne (dt2, env) =
adamc@805 3054 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@805 3055 case sgi1 of
adamc@805 3056 L'.SgiDatatypeImp (x', n1, _, _, _, xs, xncs1) =>
adamc@805 3057 found' (sgi1All, (x', n1, xs, xncs1), dt2, env)
adamc@805 3058 | _ => NONE)
adamc@805 3059
adamc@805 3060 fun seekAll (dts, env) =
adamc@805 3061 case dts of
adamc@805 3062 [] => env
adamc@805 3063 | dt :: dts => seekAll (dts, seekOne (dt, env))
adamc@805 3064 in
adamc@805 3065 seekAll (dts2, env)
adamc@805 3066 end)
adamc@805 3067 (fn (env, sgi1All as (sgi1, _)) =>
adamc@805 3068 let
adamc@805 3069 fun found dts1 =
adamc@805 3070 let
adamc@805 3071 fun iter (dts1, dts2, env) =
adamc@805 3072 case (dts1, dts2) of
adamc@805 3073 ([], []) => SOME env
adamc@805 3074 | (dt1 :: dts1, dt2 :: dts2) =>
adamc@805 3075 (case found' (sgi1All, dt1, dt2, env) of
adamc@805 3076 NONE => NONE
adamc@805 3077 | SOME env => iter (dts1, dts2, env))
adamc@805 3078 | _ => NONE
adamc@805 3079 in
adamc@805 3080 iter (dts1, dts2, env)
adamc@805 3081 end
adamc@805 3082 in
adamc@805 3083 case sgi1 of
adamc@805 3084 L'.SgiDatatype dts1 => found dts1
adamc@805 3085 | _ => NONE
adamc@805 3086 end)
adamc@805 3087 end
adamc@156 3088
adamc@191 3089 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, xs, _) =>
adamc@469 3090 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@158 3091 case sgi1 of
adamc@191 3092 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _, _) =>
adamc@158 3093 if x = x' then
adamc@158 3094 let
adamc@158 3095 val k = (L'.KType, loc)
adamc@191 3096 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@158 3097 val t1 = (L'.CModProj (m11, ms1, s1), loc)
adamc@158 3098 val t2 = (L'.CModProj (m12, ms2, s2), loc)
adamc@158 3099
adamc@158 3100 fun good () =
adamc@158 3101 let
adamc@191 3102 val env = E.pushCNamedAs env x n1 k' (SOME t1)
adamc@191 3103 val env = E.pushCNamedAs env x n2 k' (SOME t2)
adamc@158 3104 in
adamc@838 3105 cparts (n2, n1);
adamc@628 3106 SOME env
adamc@158 3107 end
adamc@158 3108 in
adamc@987 3109 (unifyCons env loc t1 t2;
adamc@628 3110 good ())
adamc@158 3111 handle CUnify (c1, c2, err) =>
adamc@1000 3112 (sgnError env (SgiWrongCon (strLoc, sgi1All, c1, sgi2All, c2, err));
adamc@158 3113 good ())
adamc@158 3114 end
adamc@158 3115 else
adamc@158 3116 NONE
adamc@158 3117
adamc@158 3118 | _ => NONE)
adamc@156 3119
adamc@32 3120 | L'.SgiVal (x, n2, c2) =>
adamc@469 3121 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@32 3122 case sgi1 of
adamc@41 3123 L'.SgiVal (x', n1, c1) =>
adamc@41 3124 if x = x' then
adamc@838 3125 ((*prefaces "val" [("x", PD.string x),
adamc@838 3126 ("n1", PD.string (Int.toString n1)),
adamc@838 3127 ("c1", p_con env c1),
adamc@838 3128 ("c2", p_con env c2),
adamc@838 3129 ("c2'", p_con env (sub2 c2))];*)
adamc@987 3130 unifyCons env loc c1 (sub2 c2);
adamc@628 3131 SOME env)
adamc@86 3132 handle CUnify (c1, c2, err) =>
adamc@1000 3133 (sgnError env (SgiWrongCon (strLoc, sgi1All, c1, sgi2All, c2, err));
adamc@628 3134 SOME env)
adamc@41 3135 else
adamc@41 3136 NONE
adamc@32 3137 | _ => NONE)
adamc@32 3138
adamc@33 3139 | L'.SgiStr (x, n2, sgn2) =>
adamc@469 3140 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@33 3141 case sgi1 of
adamc@41 3142 L'.SgiStr (x', n1, sgn1) =>
adamc@41 3143 if x = x' then
adamc@66 3144 let
adamc@1000 3145 val () = subSgn' counterparts env strLoc sgn1 sgn2
adamc@66 3146 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 3147 val env = if n1 = n2 then
adamc@66 3148 env
adamc@66 3149 else
adamc@66 3150 E.pushStrNamedAs env x n2
adamc@66 3151 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 3152 sgn = sgn2})
adamc@66 3153 in
adamc@628 3154 SOME env
adamc@66 3155 end
adamc@41 3156 else
adamc@41 3157 NONE
adamc@33 3158 | _ => NONE)
adamc@59 3159
adamc@59 3160 | L'.SgiSgn (x, n2, sgn2) =>
adamc@469 3161 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@59 3162 case sgi1 of
adamc@59 3163 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 3164 if x = x' then
adamc@65 3165 let
adamc@1000 3166 val () = subSgn' counterparts env strLoc sgn1 sgn2
adamc@1000 3167 val () = subSgn' counterparts env strLoc sgn2 sgn1
adamc@65 3168
adamc@65 3169 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 3170 val env = if n1 = n2 then
adamc@65 3171 env
adamc@65 3172 else
adamc@838 3173 (cparts (n2, n1);
adamc@838 3174 E.pushSgnNamedAs env x n1 sgn2)
adamc@65 3175 in
adamc@628 3176 SOME env
adamc@65 3177 end
adamc@59 3178 else
adamc@59 3179 NONE
adamc@59 3180 | _ => NONE)
adamc@88 3181
adamc@88 3182 | L'.SgiConstraint (c2, d2) =>
adamc@469 3183 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@88 3184 case sgi1 of
adamc@88 3185 L'.SgiConstraint (c1, d1) =>
adamc@987 3186 if consEq env loc (c1, c2)
adamc@987 3187 andalso consEq env loc (d1, d2) then
adamc@628 3188 SOME env
adamc@88 3189 else
adamc@88 3190 NONE
adamc@88 3191 | _ => NONE)
adamc@203 3192
adamc@563 3193 | L'.SgiClassAbs (x, n2, k2) =>
adamc@469 3194 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 3195 let
adamc@563 3196 fun found (x', n1, k1, co) =
adamc@211 3197 if x = x' then
adamc@211 3198 let
adamc@623 3199 val () = unifyKinds env k1 k2
adamc@563 3200 handle KUnify (k1, k2, err) =>
adamc@1000 3201 sgnError env (SgiWrongKind (strLoc, sgi1All, k1,
adamc@1000 3202 sgi2All, k2, err))
adamc@563 3203
adamc@711 3204 val env = E.pushCNamedAs env x n1 k1 co
adamc@211 3205 in
adamc@211 3206 SOME (if n1 = n2 then
adamc@211 3207 env
adamc@211 3208 else
adamc@838 3209 (cparts (n2, n1);
adamc@838 3210 E.pushCNamedAs env x n2 k1 (SOME (L'.CNamed n1, loc2))))
adamc@211 3211 end
adamc@211 3212 else
adamc@211 3213 NONE
adamc@211 3214 in
adamc@211 3215 case sgi1 of
adamc@563 3216 L'.SgiClassAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@563 3217 | L'.SgiClass (x', n1, k1, c) => found (x', n1, k1, SOME c)
adamc@211 3218 | _ => NONE
adamc@211 3219 end)
adamc@563 3220 | L'.SgiClass (x, n2, k2, c2) =>
adamc@469 3221 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 3222 let
adamc@563 3223 fun found (x', n1, k1, c1) =
adamc@211 3224 if x = x' then
adamc@211 3225 let
adamc@623 3226 val () = unifyKinds env k1 k2
adamc@563 3227 handle KUnify (k1, k2, err) =>
adamc@1000 3228 sgnError env (SgiWrongKind (strLoc, sgi1All, k1,
adamc@1000 3229 sgi2All, k2, err))
adamc@563 3230
adamc@838 3231 val c2 = sub2 c2
adamc@838 3232
adamc@218 3233 fun good () =
adamc@218 3234 let
adamc@711 3235 val env = E.pushCNamedAs env x n2 k2 (SOME c2)
adamc@218 3236 val env = if n1 = n2 then
adamc@218 3237 env
adamc@218 3238 else
adamc@839 3239 (cparts (n2, n1);
adamc@839 3240 E.pushCNamedAs env x n1 k2 (SOME c1))
adamc@218 3241 in
adamc@628 3242 SOME env
adamc@218 3243 end
adamc@211 3244 in
adamc@987 3245 (unifyCons env loc c1 c2;
adamc@628 3246 good ())
adamc@211 3247 handle CUnify (c1, c2, err) =>
adamc@1000 3248 (sgnError env (SgiWrongCon (strLoc, sgi1All, c1,
adamc@1000 3249 sgi2All, c2, err));
adamc@211 3250 good ())
adamc@211 3251 end
adamc@211 3252 else
adamc@211 3253 NONE
adamc@211 3254 in
adamc@211 3255 case sgi1 of
adamc@563 3256 L'.SgiClass (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@211 3257 | _ => NONE
adamc@211 3258 end)
adamc@31 3259 end
adamc@31 3260 in
adamc@628 3261 ignore (foldl folder env sgis2)
adamc@31 3262 end
adamc@31 3263
adamc@41 3264 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 3265 let
adamc@480 3266 val ran2 =
adamc@43 3267 if n1 = n2 then
adamc@480 3268 ran2
adamc@43 3269 else
adamc@480 3270 subStrInSgn (n2, n1) ran2
adamc@43 3271 in
adamc@1000 3272 subSgn' counterparts env strLoc dom2 dom1;
adamc@1000 3273 subSgn' counterparts (E.pushStrNamedAs env m1 n1 dom2) strLoc ran1 ran2
adamc@43 3274 end
adamc@41 3275
adamc@1000 3276 | _ => sgnError env (SgnWrongForm (strLoc, sgn1, sgn2)))
adamc@41 3277
adam@1580 3278 and subSgn env x y z = subSgn' (ref IM.empty) env x y z
adam@1580 3279 handle e as E.UnboundNamed _ => if ErrorMsg.anyErrors () then () else raise e
adamc@61 3280
adamc@706 3281 and positive self =
adamc@312 3282 let
adamc@312 3283 open L
adamc@312 3284
adamc@312 3285 fun none (c, _) =
adamc@312 3286 case c of
adamc@312 3287 CAnnot (c, _) => none c
adamc@312 3288
adamc@312 3289 | TFun (c1, c2) => none c1 andalso none c2
adamc@312 3290 | TCFun (_, _, _, c) => none c
adamc@312 3291 | TRecord c => none c
adamc@312 3292
adamc@312 3293 | CVar ([], x) => x <> self
adamc@312 3294 | CVar _ => true
adamc@312 3295 | CApp (c1, c2) => none c1 andalso none c2
adamc@312 3296 | CAbs _ => false
adamc@628 3297 | TDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
adamc@312 3298
adamc@623 3299 | CKAbs _ => false
adamc@623 3300 | TKFun _ => false
adamc@623 3301
adamc@312 3302 | CName _ => true
adamc@312 3303
adamc@312 3304 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso none c2) xcs
adamc@312 3305 | CConcat (c1, c2) => none c1 andalso none c2
adamc@621 3306 | CMap => true
adamc@312 3307
adamc@312 3308 | CUnit => true
adamc@312 3309
adamc@312 3310 | CTuple cs => List.all none cs
adamc@312 3311 | CProj (c, _) => none c
adamc@312 3312
adamc@312 3313 | CWild _ => false
adamc@312 3314
adamc@312 3315 fun pos (c, _) =
adamc@312 3316 case c of
adamc@312 3317 CAnnot (c, _) => pos c
adamc@312 3318
adamc@312 3319 | TFun (c1, c2) => none c1 andalso pos c2
adamc@312 3320 | TCFun (_, _, _, c) => pos c
adamc@312 3321 | TRecord c => pos c
adamc@312 3322
adamc@312 3323 | CVar _ => true
adamc@312 3324 | CApp (c1, c2) => pos c1 andalso none c2
adamc@312 3325 | CAbs _ => false
adamc@628 3326 | TDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
adamc@312 3327
adamc@623 3328 | CKAbs _ => false
adamc@623 3329 | TKFun _ => false
adamc@623 3330
adamc@312 3331 | CName _ => true
adamc@312 3332
adamc@312 3333 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso pos c2) xcs
adamc@312 3334 | CConcat (c1, c2) => pos c1 andalso pos c2
adamc@621 3335 | CMap => true
adamc@312 3336
adamc@312 3337 | CUnit => true
adamc@312 3338
adamc@312 3339 | CTuple cs => List.all pos cs
adamc@312 3340 | CProj (c, _) => pos c
adamc@312 3341
adamc@312 3342 | CWild _ => false
adamc@312 3343 in
adamc@312 3344 pos
adamc@312 3345 end
adamc@312 3346
adamc@706 3347 and wildifyStr env (str, sgn) =
adamc@325 3348 case #1 (hnormSgn env sgn) of
adamc@325 3349 L'.SgnConst sgis =>
adamc@325 3350 (case #1 str of
adamc@325 3351 L.StrConst ds =>
adamc@325 3352 let
adamc@1270 3353 fun cname d =
adamc@1270 3354 case d of
adamc@1270 3355 L'.SgiCon (x, _, _, _) => SOME x
adamc@1270 3356 | L'.SgiConAbs (x, _, _) => SOME x
adamc@1270 3357 | L'.SgiClass (x, _, _, _) => SOME x
adamc@1270 3358 | L'.SgiClassAbs (x, _, _) => SOME x
adamc@1270 3359 | _ => NONE
adamc@1270 3360
adamc@1270 3361 fun dname (d, _) =
adamc@1270 3362 case d of
adamc@1270 3363 L.DCon (x, _, _) => SOME x
adamc@1270 3364 | L.DClass (x, _, _) => SOME x
adamc@1270 3365 | _ => NONE
adamc@1270 3366
adamc@493 3367 fun decompileKind (k, loc) =
adamc@493 3368 case k of
adamc@493 3369 L'.KType => SOME (L.KType, loc)
adamc@493 3370 | L'.KArrow (k1, k2) =>
adamc@493 3371 (case (decompileKind k1, decompileKind k2) of
adamc@493 3372 (SOME k1, SOME k2) => SOME (L.KArrow (k1, k2), loc)
adamc@493 3373 | _ => NONE)
adamc@493 3374 | L'.KName => SOME (L.KName, loc)
adamc@493 3375 | L'.KRecord k =>
adamc@493 3376 (case decompileKind k of
adamc@493 3377 SOME k => SOME (L.KRecord k, loc)
adamc@493 3378 | _ => NONE)
adamc@493 3379 | L'.KUnit => SOME (L.KUnit, loc)
adamc@493 3380 | L'.KTuple ks =>
adamc@493 3381 let
adamc@493 3382 val ks' = List.mapPartial decompileKind ks
adamc@493 3383 in
adamc@493 3384 if length ks' = length ks then
adamc@493 3385 SOME (L.KTuple ks', loc)
adamc@493 3386 else
adamc@493 3387 NONE
adamc@493 3388 end
adamc@493 3389
adamc@493 3390 | L'.KError => NONE
adamc@493 3391 | L'.KUnif (_, _, ref (SOME k)) => decompileKind k
adamc@493 3392 | L'.KUnif _ => NONE
adam@1302 3393 | L'.KTupleUnif (_, _, ref (SOME k)) => decompileKind k
adam@1302 3394 | L'.KTupleUnif _ => NONE
adamc@493 3395
adamc@623 3396 | L'.KRel _ => NONE
adamc@623 3397 | L'.KFun _ => NONE
adamc@623 3398
adam@1529 3399 fun maybeHnorm env c =
adam@1529 3400 hnormCon env c
adam@1529 3401 handle E.UnboundNamed _ => c
adam@1529 3402
adam@1529 3403 fun decompileCon env (c as (_, loc)) =
adam@1529 3404 case #1 (maybeHnorm env c) of
adamc@325 3405 L'.CRel i =>
adamc@325 3406 let
adamc@325 3407 val (s, _) = E.lookupCRel env i
adamc@325 3408 in
adamc@325 3409 SOME (L.CVar ([], s), loc)
adamc@325 3410 end
adamc@325 3411 | L'.CNamed i =>
adamc@325 3412 let
adamc@325 3413 val (s, _, _) = E.lookupCNamed env i
adamc@325 3414 in
adamc@325 3415 SOME (L.CVar ([], s), loc)
adamc@325 3416 end
adamc@325 3417 | L'.CModProj (m1, ms, x) =>
adamc@325 3418 let
adamc@325 3419 val (s, _) = E.lookupStrNamed env m1
adamc@325 3420 in
adamc@325 3421 SOME (L.CVar (s :: ms, x), loc)
adamc@325 3422 end
adamc@325 3423 | L'.CName s => SOME (L.CName s, loc)
adamc@325 3424 | L'.CRecord (_, xcs) =>
adamc@325 3425 let
adamc@325 3426 fun fields xcs =
adamc@325 3427 case xcs of
adamc@325 3428 [] => SOME []
adamc@325 3429 | (x, t) :: xcs =>
adamc@325 3430 case (decompileCon env x, decompileCon env t, fields xcs) of
adamc@325 3431 (SOME x, SOME t, SOME xcs) => SOME ((x, t) :: xcs)
adamc@325 3432 | _ => NONE
adamc@325 3433 in
adamc@325 3434 Option.map (fn xcs => (L.CRecord xcs, loc))
adamc@325 3435 (fields xcs)
adamc@325 3436 end
adamc@325 3437 | L'.CConcat (c1, c2) =>
adamc@325 3438 (case (decompileCon env c1, decompileCon env c2) of
adamc@325 3439 (SOME c1, SOME c2) => SOME (L.CConcat (c1, c2), loc)
adamc@325 3440 | _ => NONE)
adamc@325 3441 | L'.CUnit => SOME (L.CUnit, loc)
adam@1303 3442 | L'.CUnif (nl, _, _, _, ref (SOME c)) => decompileCon env (E.mliftConInCon nl c)
adamc@325 3443
adam@1585 3444 | L'.CApp (f, x) =>
adam@1585 3445 (case (decompileCon env f, decompileCon env x) of
adam@1585 3446 (SOME f, SOME x) => SOME (L.CApp (f, x), loc)
adam@1585 3447 | _ => NONE)
adam@1585 3448
adam@1585 3449 | c => (Print.preface ("WTF?", p_con env (c, loc)); NONE)
adamc@325 3450
adamc@1264 3451 fun buildNeeded env sgis =
adamc@1275 3452 #1 (foldl (fn ((sgi, loc), (nd, env')) =>
adamc@1264 3453 (case sgi of
adam@1570 3454 L'.SgiCon (x, _, k, _) => naddCon (nd, x, k)
adam@1570 3455 | L'.SgiConAbs (x, _, k) => naddCon (nd, x, k)
adamc@1264 3456 | L'.SgiConstraint cs => naddConstraint (nd, (env', cs, loc))
adamc@1264 3457 | L'.SgiVal (x, _, t) =>
adamc@1264 3458 let
adamc@1277 3459 fun should t =
adamc@1277 3460 let
adamc@1277 3461 val t = normClassConstraint env' t
adamc@1277 3462 in
adamc@1277 3463 case #1 t of
adamc@1277 3464 L'.CApp (f, _) => isClassOrFolder env' f
adamc@1277 3465 | L'.TRecord t =>
adamc@1277 3466 (case hnormCon env' t of
adamc@1277 3467 (L'.CApp (f, _), _) =>
adamc@1277 3468 (case hnormCon env' f of
adamc@1277 3469 (L'.CApp (f, cl), loc) =>
adamc@1277 3470 (case hnormCon env' f of
adamc@1277 3471 (L'.CMap _, _) => isClassOrFolder env' cl
adamc@1277 3472 | _ => false)
adamc@1277 3473 | _ => false)
adamc@1277 3474 | _ => false)
adamc@1277 3475 | _ => false
adamc@1277 3476 end
adamc@1270 3477 in
adamc@1277 3478 if should t then
adamc@1277 3479 naddVal (nd, x)
adamc@1277 3480 else
adamc@1277 3481 nd
adamc@1264 3482 end
adamc@1264 3483 | L'.SgiStr (x, _, s) =>
adamc@1270 3484 (case #1 (hnormSgn env' s) of
adamc@1270 3485 L'.SgnConst sgis' => naddMod (nd, x, (env', buildNeeded env' sgis'))
adamc@1264 3486 | _ => nd)
adamc@1264 3487 | _ => nd,
adamc@1275 3488 E.sgiBinds env' (sgi, loc)))
adamc@1275 3489 (nempty, env) sgis)
adamc@1264 3490
adamc@1264 3491 val nd = buildNeeded env sgis
adamc@1264 3492
adamc@1264 3493 fun removeUsed (nd, ds) =
adamc@1264 3494 foldl (fn ((d, _), nd) =>
adamc@634 3495 case d of
adamc@1264 3496 L.DCon (x, _, _) => ndelCon (nd, x)
adamc@1264 3497 | L.DClass (x, _, _) => ndelCon (nd, x)
adamc@1264 3498 | L.DVal (x, _, _) => ndelVal (nd, x)
adamc@1264 3499 | L.DOpen _ => nempty
adamc@1264 3500 | L.DStr (x, _, (L.StrConst ds', _)) =>
adamc@1264 3501 (case SM.find (nmods nd, x) of
adamc@1264 3502 NONE => nd
adamc@1270 3503 | SOME (env, nd') => naddMod (nd, x, (env, removeUsed (nd', ds'))))
adamc@1264 3504 | _ => nd)
adamc@1264 3505 nd ds
adamc@1264 3506
adamc@1264 3507 val nd = removeUsed (nd, ds)
adamc@1264 3508
adamc@1270 3509 fun extend (env, nd, ds) =
adamc@1264 3510 let
adamc@1264 3511 val ds' = List.mapPartial (fn (env', (c1, c2), loc) =>
adamc@1264 3512 case (decompileCon env' c1, decompileCon env' c2) of
adamc@1264 3513 (SOME c1, SOME c2) =>
adamc@1264 3514 SOME (L.DConstraint (c1, c2), loc)
adam@1586 3515 | _ => NONE) (nconstraints nd)
adamc@1264 3516
adamc@1264 3517 val ds' =
adamc@1264 3518 case SS.listItems (nvals nd) of
adamc@1264 3519 [] => ds'
adamc@1264 3520 | xs =>
adamc@1264 3521 let
adamc@1264 3522 val ewild = (L.EWild, #2 str)
adamc@1264 3523 val ds'' = map (fn x => (L.DVal (x, NONE, ewild), #2 str)) xs
adamc@1264 3524 in
adamc@1264 3525 ds'' @ ds'
adamc@1264 3526 end
adamc@1264 3527
adamc@1275 3528 val ds' =
adamc@1264 3529 case SM.listItemsi (ncons nd) of
adamc@1275 3530 [] => ds'
adamc@1264 3531 | xs =>
adam@1570 3532 map (fn (x, k) =>
adamc@1270 3533 let
adamc@1270 3534 val k =
adamc@1270 3535 case decompileKind k of
adamc@1270 3536 NONE => (L.KWild, #2 str)
adamc@1270 3537 | SOME k => k
adamc@1275 3538
adamc@1270 3539 val cwild = (L.CWild k, #2 str)
adamc@1270 3540 in
adam@1570 3541 (L.DCon (x, NONE, cwild), #2 str)
adamc@1275 3542 end) xs @ ds'
adamc@1275 3543
adamc@1275 3544 val ds = ds @ ds'
adamc@1264 3545 in
adamc@1270 3546 map (fn d as (L.DStr (x, s, (L.StrConst ds', loc')), loc) =>
adamc@1270 3547 (case SM.find (nmods nd, x) of
adamc@1270 3548 NONE => d
adamc@1270 3549 | SOME (env, nd') =>
adamc@1270 3550 (L.DStr (x, s, (L.StrConst (extend (env, nd', ds')), loc')), loc))
adamc@1270 3551 | d => d) ds
adamc@1264 3552 end
adamc@325 3553 in
adamc@1270 3554 (L.StrConst (extend (env, nd, ds)), #2 str)
adamc@325 3555 end
adamc@325 3556 | _ => str)
adamc@325 3557 | _ => str
adamc@325 3558
adamc@706 3559 and elabDecl (dAll as (d, loc), (env, denv, gs)) =
adamc@255 3560 let
adamc@904 3561 (*val () = preface ("elabDecl", SourcePrint.p_decl dAll)*)
adamc@514 3562 (*val befor = Time.now ()*)
adamc@255 3563
adamc@255 3564 val r =
adamc@255 3565 case d of
adamc@255 3566 L.DCon (x, ko, c) =>
adamc@255 3567 let
adamc@255 3568 val k' = case ko of
adamc@255 3569 NONE => kunif loc
adamc@623 3570 | SOME k => elabKind env k
adamc@255 3571
adamc@255 3572 val (c', ck, gs') = elabCon (env, denv) c
adamc@255 3573 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@255 3574 in
adamc@255 3575 checkKind env c' ck k';
adamc@255 3576
adamc@255 3577 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3578 end
adamc@805 3579 | L.DDatatype dts =>
adamc@255 3580 let
adamc@255 3581 val k = (L'.KType, loc)
adamc@805 3582
adamc@805 3583 val (dts, env) = ListUtil.foldlMap
adamc@805 3584 (fn ((x, xs, xcs), env) =>
adamc@805 3585 let
adamc@805 3586 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@805 3587 val (env, n) = E.pushCNamed env x k' NONE
adamc@805 3588 in
adamc@805 3589 ((x, n, xs, xcs), env)
adamc@805 3590 end)
adamc@805 3591 env dts
adamc@805 3592
adamc@805 3593 val (dts, (env, gs')) = ListUtil.foldlMap
adamc@805 3594 (fn ((x, n, xs, xcs), (env, gs')) =>
adamc@805 3595 let
adamc@805 3596 val t = (L'.CNamed n, loc)
adamc@805 3597 val nxs = length xs - 1
adamc@805 3598 val t = ListUtil.foldli
adamc@805 3599 (fn (i, _, t) =>
adamc@805 3600 (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@805 3601
adamc@805 3602 val (env', denv') = foldl (fn (x, (env', denv')) =>
adamc@805 3603 (E.pushCRel env' x k,
adamc@805 3604 D.enter denv')) (env, denv) xs
adamc@805 3605
adamc@805 3606 val (xcs, (used, env, gs')) =
adamc@805 3607 ListUtil.foldlMap
adamc@805 3608 (fn ((x, to), (used, env, gs)) =>
adamc@805 3609 let
adamc@805 3610 val (to, t, gs') = case to of
adamc@805 3611 NONE => (NONE, t, gs)
adamc@805 3612 | SOME t' =>
adamc@805 3613 let
adamc@805 3614 val (t', tk, gs') = elabCon (env', denv') t'
adamc@805 3615 in
adamc@805 3616 checkKind env' t' tk k;
adamc@805 3617 (SOME t', (L'.TFun (t', t), loc), enD gs' @ gs)
adamc@805 3618 end
adamc@805 3619 val t = foldr (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
adamc@805 3620
adamc@805 3621 val (env, n') = E.pushENamed env x t
adamc@805 3622 in
adamc@805 3623 if SS.member (used, x) then
adamc@805 3624 strError env (DuplicateConstructor (x, loc))
adamc@805 3625 else
adamc@805 3626 ();
adamc@805 3627 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@805 3628 end)
adamc@805 3629 (SS.empty, env, gs') xcs
adamc@805 3630 in
adamc@805 3631 ((x, n, xs, xcs), (E.pushDatatype env n xs xcs, gs'))
adamc@805 3632 end)
adamc@805 3633 (env, []) dts
adamc@255 3634 in
adamc@805 3635 ([(L'.DDatatype dts, loc)], (env, denv, gs' @ gs))
adamc@255 3636 end
adamc@255 3637
adamc@255 3638 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
adamc@255 3639
adamc@255 3640 | L.DDatatypeImp (x, m1 :: ms, s) =>
adamc@255 3641 (case E.lookupStr env m1 of
adamc@255 3642 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@255 3643 ([], (env, denv, gs)))
adamc@255 3644 | SOME (n, sgn) =>
adamc@255 3645 let
adamc@255 3646 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@255 3647 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@255 3648 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@255 3649 (strerror, sgnerror))
adamc@255 3650 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@255 3651 ((L'.StrVar n, loc), sgn) ms
adamc@255 3652 in
adamc@628 3653 case hnormCon env (L'.CModProj (n, ms, s), loc) of
adamc@628 3654 (L'.CModProj (n, ms, s), _) =>
adamc@255 3655 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@255 3656 NONE => (conError env (UnboundDatatype (loc, s));
adamc@255 3657 ([], (env, denv, gs)))
adamc@255 3658 | SOME (xs, xncs) =>
adamc@255 3659 let
adamc@255 3660 val k = (L'.KType, loc)
adamc@255 3661 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@255 3662 val t = (L'.CModProj (n, ms, s), loc)
adamc@255 3663 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@255 3664 val env = E.pushDatatype env n' xs xncs
adamc@255 3665
adamc@255 3666 val t = (L'.CNamed n', loc)
adamc@844 3667 val nxs = length xs
adamc@844 3668 val t = ListUtil.foldli (fn (i, _, t) =>
adamc@844 3669 (L'.CApp (t, (L'.CRel (nxs - 1 - i), loc)), loc))
adamc@844 3670 t xs
adamc@255 3671 val env = foldl (fn ((x, n, to), env) =>
adamc@255 3672 let
adamc@255 3673 val t = case to of
adamc@255 3674 NONE => t
adamc@255 3675 | SOME t' => (L'.TFun (t', t), loc)
adamc@255 3676
adamc@255 3677 val t = foldr (fn (x, t) =>
adamc@255 3678 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@255 3679 t xs
adamc@255 3680 in
adamc@255 3681 E.pushENamedAs env x n t
adamc@255 3682 end) env xncs
adamc@255 3683 in
adamc@628 3684 ([(L'.DDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, gs))
adamc@255 3685 end)
adamc@255 3686 | _ => (strError env (NotDatatype loc);
adamc@255 3687 ([], (env, denv, [])))
adamc@255 3688 end)
adamc@255 3689
adamc@255 3690 | L.DVal (x, co, e) =>
adamc@255 3691 let
adamc@255 3692 val (c', _, gs1) = case co of
adamc@255 3693 NONE => (cunif (loc, ktype), ktype, [])
adamc@255 3694 | SOME c => elabCon (env, denv) c
adamc@255 3695
adamc@255 3696 val (e', et, gs2) = elabExp (env, denv) e
adamc@629 3697
adamc@629 3698 val () = checkCon env e' et c'
adamc@629 3699
adamc@674 3700 val c' = normClassConstraint env c'
adamc@255 3701 val (env', n) = E.pushENamed env x c'
adamc@255 3702 in
adamc@255 3703 (*prefaces "DVal" [("x", Print.PD.string x),
adamc@362 3704 ("c'", p_con env c')];*)
adamc@628 3705 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, enD gs1 @ gs2 @ gs))
adamc@255 3706 end
adamc@255 3707 | L.DValRec vis =>
adamc@255 3708 let
adamc@255 3709 fun allowable (e, _) =
adamc@255 3710 case e of
adamc@255 3711 L.EAbs _ => true
adamc@255 3712 | L.ECAbs (_, _, _, e) => allowable e
adamc@623 3713 | L.EKAbs (_, e) => allowable e
adamc@255 3714 | L.EDisjoint (_, _, e) => allowable e
adamc@255 3715 | _ => false
adamc@255 3716
adamc@255 3717 val (vis, gs) = ListUtil.foldlMap
adamc@255 3718 (fn ((x, co, e), gs) =>
adamc@255 3719 let
adamc@255 3720 val (c', _, gs1) = case co of
adamc@255 3721 NONE => (cunif (loc, ktype), ktype, [])
adamc@255 3722 | SOME c => elabCon (env, denv) c
adamc@674 3723 val c' = normClassConstraint env c'
adamc@255 3724 in
adamc@255 3725 ((x, c', e), enD gs1 @ gs)
adamc@280 3726 end) gs vis
adamc@255 3727
adamc@255 3728 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@255 3729 let
adamc@255 3730 val (env, n) = E.pushENamed env x c'
adamc@255 3731 in
adamc@255 3732 ((x, n, c', e), env)
adamc@255 3733 end) env vis
adamc@255 3734
adamc@255 3735 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@255 3736 let
adamc@255 3737 val (e', et, gs1) = elabExp (env, denv) e
adamc@255 3738 in
adamc@628 3739 checkCon env e' et c';
adamc@255 3740 if allowable e then
adamc@255 3741 ()
adamc@255 3742 else
adamc@255 3743 expError env (IllegalRec (x, e'));
adamc@628 3744 ((x, n, c', e'), gs1 @ gs)
adamc@255 3745 end) gs vis
adamc@255 3746 in
adamc@255 3747 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@255 3748 end
adamc@255 3749
adamc@255 3750 | L.DSgn (x, sgn) =>
adamc@255 3751 let
adamc@255 3752 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@255 3753 val (env', n) = E.pushSgnNamed env x sgn'
adamc@255 3754 in
adamc@255 3755 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3756 end
adamc@255 3757
adamc@255 3758 | L.DStr (x, sgno, str) =>
adamc@255 3759 let
adamc@255 3760 val () = if x = "Basis" then
adamc@255 3761 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@255 3762 else
adamc@255 3763 ()
adamc@255 3764
adamc@255 3765 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@255 3766
adamc@255 3767 val (str', sgn', gs') =
adamc@255 3768 case formal of
adamc@255 3769 NONE =>
adamc@255 3770 let
adamc@255 3771 val (str', actual, gs') = elabStr (env, denv) str
adamc@255 3772 in
adamc@255 3773 (str', selfifyAt env {str = str', sgn = actual}, gs')
adamc@255 3774 end
adamc@255 3775 | SOME (formal, gs1) =>
adamc@255 3776 let
adamc@325 3777 val str = wildifyStr env (str, formal)
adamc@255 3778 val (str', actual, gs2) = elabStr (env, denv) str
adamc@255 3779 in
adamc@1000 3780 subSgn env loc (selfifyAt env {str = str', sgn = actual}) formal;
adamc@255 3781 (str', formal, enD gs1 @ gs2)
adamc@255 3782 end
adamc@255 3783
adamc@255 3784 val (env', n) = E.pushStrNamed env x sgn'
adamc@834 3785 val denv' =
adamc@834 3786 case #1 str' of
adamc@834 3787 L'.StrConst _ => dopenConstraints (loc, env', denv) {str = x, strs = []}
adamc@1022 3788 | L'.StrApp _ => dopenConstraints (loc, env', denv) {str = x, strs = []}
adamc@834 3789 | _ => denv
adamc@255 3790 in
adamc@255 3791 case #1 (hnormSgn env sgn') of
adamc@255 3792 L'.SgnFun _ =>
adamc@255 3793 (case #1 str' of
adamc@255 3794 L'.StrFun _ => ()
adamc@255 3795 | _ => strError env (FunctorRebind loc))
adamc@255 3796 | _ => ();
adamc@834 3797 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv', gs' @ gs))
adamc@255 3798 end
adamc@255 3799
adamc@255 3800 | L.DFfiStr (x, sgn) =>
adamc@255 3801 let
adamc@255 3802 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@255 3803
adamc@255 3804 val (env', n) = E.pushStrNamed env x sgn'
adamc@255 3805 in
adamc@255 3806 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3807 end
adamc@255 3808
adamc@255 3809 | L.DOpen (m, ms) =>
adamc@255 3810 (case E.lookupStr env m of
adamc@255 3811 NONE => (strError env (UnboundStr (loc, m));
adamc@255 3812 ([], (env, denv, gs)))
adamc@255 3813 | SOME (n, sgn) =>
adamc@255 3814 let
adamc@1030 3815 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@1030 3816 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@1030 3817 NONE => (strError env (UnboundStr (loc, m));
adamc@1030 3818 (strerror, sgnerror))
adamc@1030 3819 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@1030 3820 ((L'.StrVar n, loc), sgn) ms
adamc@1030 3821
adamc@1030 3822 val sgn = selfifyAt env {str = str, sgn = sgn}
adamc@255 3823
adamc@628 3824 val (ds, env') = dopen env {str = n, strs = ms, sgn = sgn}
adamc@628 3825 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = ms}
adamc@255 3826 in
adamc@255 3827 (ds, (env', denv', gs))
adamc@255 3828 end)
adamc@255 3829
adamc@255 3830 | L.DConstraint (c1, c2) =>
adamc@255 3831 let
adamc@255 3832 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@255 3833 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@255 3834 val gs3 = D.prove env denv (c1', c2', loc)
adamc@255 3835
adamc@628 3836 val denv' = D.assert env denv (c1', c2')
adamc@255 3837 in
adamc@255 3838 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@255 3839 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@255 3840
adamc@628 3841 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', enD gs1 @ enD gs2 @ enD gs3 @ gs))
adamc@255 3842 end
adamc@255 3843
adamc@255 3844 | L.DOpenConstraints (m, ms) =>
adamc@255 3845 let
adamc@255 3846 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@255 3847 in
adamc@255 3848 ([], (env, denv, gs))
adamc@255 3849 end
adamc@255 3850
adamc@255 3851 | L.DExport str =>
adamc@255 3852 let
adamc@255 3853 val (str', sgn, gs') = elabStr (env, denv) str
adamc@255 3854
adamc@255 3855 val sgn =
adamc@255 3856 case #1 (hnormSgn env sgn) of
adamc@255 3857 L'.SgnConst sgis =>
adamc@255 3858 let
adamc@255 3859 fun doOne (all as (sgi, _), env) =
adamc@255 3860 (case sgi of
adamc@255 3861 L'.SgiVal (x, n, t) =>
adamc@325 3862 let
adamc@325 3863 fun doPage (makeRes, ran) =
adamc@628 3864 case hnormCon env ran of
adamc@628 3865 (L'.CApp (tf, arg), _) =>
adamc@628 3866 (case (hnormCon env tf, hnormCon env arg) of
adamc@628 3867 ((L'.CModProj (basis, [], "transaction"), _),
adamc@720 3868 (L'.CApp (tf, arg3), _)) =>
adamc@325 3869 (case (basis = !basis_r,
adamc@720 3870 hnormCon env tf, hnormCon env arg3) of
adamc@325 3871 (true,
adamc@720 3872 (L'.CApp (tf, arg2), _),
adamc@628 3873 ((L'.CRecord (_, []), _))) =>
adamc@720 3874 (case (hnormCon env tf) of
adamc@720 3875 (L'.CApp (tf, arg1), _) =>
adamc@720 3876 (case (hnormCon env tf,
adamc@720 3877 hnormCon env arg1,
adamc@720 3878 hnormCon env arg2) of
adamc@720 3879 (tf, arg1,
adamc@720 3880 (L'.CRecord (_, []), _)) =>
adamc@720 3881 let
adamc@720 3882 val t = (L'.CApp (tf, arg1), loc)
adamc@720 3883 val t = (L'.CApp (t, arg2), loc)
adamc@720 3884 val t = (L'.CApp (t, arg3), loc)
adamc@720 3885 val t = (L'.CApp (
adamc@720 3886 (L'.CModProj
adamc@720 3887 (basis, [], "transaction"), loc),
adamc@325 3888 t), loc)
adam@1347 3889
adam@1347 3890 fun normArgs t =
adam@1347 3891 case hnormCon env t of
adam@1347 3892 (L'.TFun (dom, ran), loc) =>
adam@1347 3893 (L'.TFun (hnormCon env dom, normArgs ran), loc)
adam@1347 3894 | t' => t'
adamc@720 3895 in
adam@1347 3896 (L'.SgiVal (x, n, normArgs (makeRes t)), loc)
adamc@720 3897 end
adamc@325 3898 | _ => all)
adamc@325 3899 | _ => all)
adamc@325 3900 | _ => all)
adamc@325 3901 | _ => all)
adamc@325 3902 | _ => all
adamc@325 3903 in
adamc@628 3904 case hnormCon env t of
adamc@628 3905 (L'.TFun (dom, ran), _) =>
adamc@628 3906 (case hnormCon env dom of
adamc@628 3907 (L'.TRecord domR, _) =>
adamc@325 3908 doPage (fn t => (L'.TFun ((L'.TRecord domR,
adamc@325 3909 loc),
adamc@325 3910 t), loc), ran)
adamc@325 3911 | _ => all)
adamc@325 3912 | _ => doPage (fn t => t, t)
adamc@325 3913 end
adamc@255 3914 | _ => all,
adamc@255 3915 E.sgiBinds env all)
adamc@255 3916 in
adamc@255 3917 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@255 3918 end
adamc@255 3919 | _ => sgn
adamc@255 3920 in
adamc@255 3921 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs' @ gs))
adamc@255 3922 end
adamc@255 3923
adamc@707 3924 | L.DTable (x, c, pe, ce) =>
adamc@255 3925 let
adamc@707 3926 val cstK = (L'.KRecord (L'.KRecord (L'.KUnit, loc), loc), loc)
adamc@707 3927
adamc@255 3928 val (c', k, gs') = elabCon (env, denv) c
adamc@707 3929 val pkey = cunif (loc, cstK)
adamc@707 3930 val uniques = cunif (loc, cstK)
adamc@705 3931
adamc@705 3932 val ct = tableOf ()
adamc@705 3933 val ct = (L'.CApp (ct, c'), loc)
adamc@707 3934 val ct = (L'.CApp (ct, (L'.CConcat (pkey, uniques), loc)), loc)
adamc@705 3935
adamc@705 3936 val (env, n) = E.pushENamed env x ct
adamc@707 3937 val (pe', pet, gs'') = elabExp (env, denv) pe
adamc@707 3938 val (ce', cet, gs''') = elabExp (env, denv) ce
adamc@707 3939
adamc@707 3940 val pst = (L'.CModProj (!basis_r, [], "primary_key"), loc)
adamc@707 3941 val pst = (L'.CApp (pst, c'), loc)
adamc@707 3942 val pst = (L'.CApp (pst, pkey), loc)
adamc@704 3943
adamc@704 3944 val cst = (L'.CModProj (!basis_r, [], "sql_constraints"), loc)
adamc@704 3945 val cst = (L'.CApp (cst, c'), loc)
adamc@705 3946 val cst = (L'.CApp (cst, uniques), loc)
adamc@255 3947 in
adamc@255 3948 checkKind env c' k (L'.KRecord (L'.KType, loc), loc);
adamc@707 3949 checkCon env pe' pet pst;
adamc@707 3950 checkCon env ce' cet cst;
adamc@707 3951 ([(L'.DTable (!basis_r, x, n, c', pe', pkey, ce', uniques), loc)],
adamc@707 3952 (env, denv, gs''' @ gs'' @ enD gs' @ gs))
adamc@255 3953 end
adamc@338 3954 | L.DSequence x =>
adamc@338 3955 let
adamc@338 3956 val (env, n) = E.pushENamed env x (sequenceOf ())
adamc@338 3957 in
adamc@338 3958 ([(L'.DSequence (!basis_r, x, n), loc)], (env, denv, gs))
adamc@338 3959 end
adamc@754 3960 | L.DView (x, e) =>
adamc@754 3961 let
adamc@754 3962 val (e', t, gs') = elabExp (env, denv) e
adamc@754 3963
adamc@754 3964 val k = (L'.KRecord (L'.KType, loc), loc)
adamc@754 3965 val fs = cunif (loc, k)
adamc@754 3966 val ts = cunif (loc, (L'.KRecord k, loc))
adamc@754 3967 val tf = (L'.CApp ((L'.CMap (k, k), loc),
adamc@754 3968 (L'.CAbs ("_", k, (L'.CRecord ((L'.KType, loc), []), loc)), loc)), loc)
adamc@754 3969 val ts = (L'.CApp (tf, ts), loc)
adamc@754 3970
adamc@754 3971 val cv = viewOf ()
adamc@754 3972 val cv = (L'.CApp (cv, fs), loc)
adamc@754 3973 val (env', n) = E.pushENamed env x cv
adamc@754 3974
adamc@754 3975 val ct = queryOf ()
adamc@1191 3976 val ct = (L'.CApp (ct, (L'.CRecord ((L'.KRecord (L'.KType, loc), loc), []), loc)), loc)
adam@1395 3977 val ct = (L'.CApp (ct, (L'.CRecord ((L'.KRecord (L'.KType, loc), loc), []), loc)), loc)
adamc@754 3978 val ct = (L'.CApp (ct, ts), loc)
adamc@754 3979 val ct = (L'.CApp (ct, fs), loc)
adamc@754 3980 in
adamc@754 3981 checkCon env e' t ct;
adamc@754 3982 ([(L'.DView (!basis_r, x, n, e', fs), loc)],
adamc@754 3983 (env', denv, gs' @ gs))
adamc@754 3984 end
adamc@255 3985
adamc@563 3986 | L.DClass (x, k, c) =>
adamc@255 3987 let
adamc@623 3988 val k = elabKind env k
adamc@280 3989 val (c', ck, gs') = elabCon (env, denv) c
adamc@711 3990 val (env, n) = E.pushCNamed env x k (SOME c')
adamc@255 3991 val env = E.pushClass env n
adamc@255 3992 in
adamc@711 3993 checkKind env c' ck k;
adamc@563 3994 ([(L'.DClass (x, n, k, c'), loc)], (env, denv, enD gs' @ gs))
adamc@255 3995 end
adamc@271 3996
adamc@280 3997 | L.DDatabase s => ([(L'.DDatabase s, loc)], (env, denv, gs))
adamc@280 3998
adamc@459 3999 | L.DCookie (x, c) =>
adamc@459 4000 let
adamc@459 4001 val (c', k, gs') = elabCon (env, denv) c
adamc@459 4002 val (env, n) = E.pushENamed env x (L'.CApp (cookieOf (), c'), loc)
adamc@459 4003 in
adamc@459 4004 checkKind env c' k (L'.KType, loc);
adamc@459 4005 ([(L'.DCookie (!basis_r, x, n, c'), loc)], (env, denv, enD gs' @ gs))
adamc@459 4006 end
adamc@720 4007 | L.DStyle x =>
adamc@718 4008 let
adamc@720 4009 val (env, n) = E.pushENamed env x (styleOf ())
adamc@718 4010 in
adamc@720 4011 ([(L'.DStyle (!basis_r, x, n), loc)], (env, denv, gs))
adamc@718 4012 end
adamc@1075 4013 | L.DTask (e1, e2) =>
adamc@1073 4014 let
adamc@1075 4015 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@1075 4016 val (e2', t2, gs2) = elabExp (env, denv) e2
adamc@1075 4017
adam@1348 4018 val targ = cunif (loc, (L'.KType, loc))
adam@1348 4019
adamc@1075 4020 val t1' = (L'.CModProj (!basis_r, [], "task_kind"), loc)
adam@1348 4021 val t1' = (L'.CApp (t1', targ), loc)
adam@1348 4022
adamc@1075 4023 val t2' = (L'.CApp ((L'.CModProj (!basis_r, [], "transaction"), loc),
adamc@1075 4024 (L'.TRecord (L'.CRecord ((L'.KType, loc), []), loc), loc)), loc)
adam@1348 4025 val t2' = (L'.TFun (targ, t2'), loc)
adamc@1073 4026 in
adamc@1075 4027 checkCon env e1' t1 t1';
adamc@1075 4028 checkCon env e2' t2 t2';
adamc@1075 4029 ([(L'.DTask (e1', e2'), loc)], (env, denv, gs2 @ gs1 @ gs))
adamc@1073 4030 end
adamc@1199 4031 | L.DPolicy e1 =>
adamc@1199 4032 let
adamc@1199 4033 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@1199 4034
adamc@1199 4035 val t1' = (L'.CModProj (!basis_r, [], "sql_policy"), loc)
adamc@1199 4036 in
adamc@1199 4037 checkCon env e1' t1 t1';
adamc@1199 4038 ([(L'.DPolicy e1', loc)], (env, denv, gs1 @ gs))
adamc@1199 4039 end
adamc@459 4040
adam@1294 4041 | L.DOnError (m1, ms, s) =>
adam@1294 4042 (case E.lookupStr env m1 of
adam@1294 4043 NONE => (expError env (UnboundStrInExp (loc, m1));
adam@1294 4044 ([], (env, denv, [])))
adam@1294 4045 | SOME (n, sgn) =>
adam@1294 4046 let
adam@1294 4047 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adam@1294 4048 case E.projectStr env {sgn = sgn, str = str, field = m} of
adam@1294 4049 NONE => (conError env (UnboundStrInCon (loc, m));
adam@1294 4050 (strerror, sgnerror))
adam@1294 4051 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adam@1294 4052 ((L'.StrVar n, loc), sgn) ms
adam@1294 4053
adam@1294 4054 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adam@1294 4055 NONE => (expError env (UnboundExp (loc, s));
adam@1294 4056 cerror)
adam@1294 4057 | SOME t => t
adam@1294 4058
adam@1294 4059 val page = (L'.CModProj (!basis_r, [], "page"), loc)
adam@1294 4060 val xpage = (L'.CApp ((L'.CModProj (!basis_r, [], "transaction"), loc), page), loc)
adam@1294 4061 val func = (L'.TFun ((L'.CModProj (!basis_r, [], "xbody"), loc), xpage), loc)
adam@1294 4062 in
adam@1407 4063 (unifyCons env loc t func
adam@1407 4064 handle CUnify _ => ErrorMsg.error "onError handler has wrong type.");
adam@1294 4065 ([(L'.DOnError (n, ms, s), loc)], (env, denv, gs))
adam@1294 4066 end)
adam@1294 4067
adamc@280 4068 (*val tcs = List.filter (fn TypeClass _ => true | _ => false) (#3 (#2 r))*)
adamc@255 4069 in
adamc@904 4070 (*prefaces "/elabDecl" [("d", SourcePrint.p_decl dAll)];*)
adamc@255 4071 r
adamc@255 4072 end
adamc@211 4073
adamc@83 4074 and elabStr (env, denv) (str, loc) =
adamc@31 4075 case str of
adamc@31 4076 L.StrConst ds =>
adamc@31 4077 let
adamc@88 4078 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 4079 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 4080
adamc@63 4081 val (sgis, _, _, _, _) =
adamc@64 4082 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 4083 case sgi of
adamc@64 4084 L'.SgiConAbs (x, n, k) =>
adamc@64 4085 let
adamc@64 4086 val (cons, x) =
adamc@64 4087 if SS.member (cons, x) then
adamc@64 4088 (cons, "?" ^ x)
adamc@64 4089 else
adamc@64 4090 (SS.add (cons, x), x)
adamc@64 4091 in
adamc@64 4092 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 4093 end
adamc@64 4094 | L'.SgiCon (x, n, k, c) =>
adamc@64 4095 let
adamc@64 4096 val (cons, x) =
adamc@64 4097 if SS.member (cons, x) then
adamc@64 4098 (cons, "?" ^ x)
adamc@64 4099 else
adamc@64 4100 (SS.add (cons, x), x)
adamc@64 4101 in
adamc@64 4102 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 4103 end
adamc@805 4104 | L'.SgiDatatype dts =>
adamc@156 4105 let
adamc@805 4106 fun doOne ((x, n, xs, xncs), (cons, vals)) =
adamc@805 4107 let
adamc@805 4108 val (cons, x) =
adamc@805 4109 if SS.member (cons, x) then
adamc@805 4110 (cons, "?" ^ x)
adamc@156 4111 else
adamc@805 4112 (SS.add (cons, x), x)
adamc@805 4113
adamc@805 4114 val (xncs, vals) =
adamc@805 4115 ListUtil.foldlMap
adamc@805 4116 (fn ((x, n, t), vals) =>
adamc@805 4117 if SS.member (vals, x) then
adamc@805 4118 (("?" ^ x, n, t), vals)
adamc@805 4119 else
adamc@805 4120 ((x, n, t), SS.add (vals, x)))
adamc@805 4121 vals xncs
adamc@805 4122 in
adamc@805 4123 ((x, n, xs, xncs), (cons, vals))
adamc@805 4124 end
adamc@805 4125
adamc@805 4126 val (dts, (cons, vals)) = ListUtil.foldlMap doOne (cons, vals) dts
adamc@156 4127 in
adamc@805 4128 ((L'.SgiDatatype dts, loc) :: sgis, cons, vals, sgns, strs)
adamc@156 4129 end
adamc@191 4130 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@156 4131 let
adamc@156 4132 val (cons, x) =
adamc@156 4133 if SS.member (cons, x) then
adamc@156 4134 (cons, "?" ^ x)
adamc@156 4135 else
adamc@156 4136 (SS.add (cons, x), x)
adamc@156 4137 in
adamc@191 4138 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 4139 end
adamc@64 4140 | L'.SgiVal (x, n, c) =>
adamc@64 4141 let
adamc@64 4142 val (vals, x) =
adamc@64 4143 if SS.member (vals, x) then
adamc@64 4144 (vals, "?" ^ x)
adamc@64 4145 else
adamc@64 4146 (SS.add (vals, x), x)
adamc@64 4147 in
adamc@64 4148 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 4149 end
adamc@64 4150 | L'.SgiSgn (x, n, sgn) =>
adamc@64 4151 let
adamc@64 4152 val (sgns, x) =
adamc@64 4153 if SS.member (sgns, x) then
adamc@64 4154 (sgns, "?" ^ x)
adamc@64 4155 else
adamc@64 4156 (SS.add (sgns, x), x)
adamc@64 4157 in
adamc@64 4158 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 4159 end
adamc@64 4160
adamc@64 4161 | L'.SgiStr (x, n, sgn) =>
adamc@64 4162 let
adamc@64 4163 val (strs, x) =
adamc@64 4164 if SS.member (strs, x) then
adamc@64 4165 (strs, "?" ^ x)
adamc@64 4166 else
adamc@64 4167 (SS.add (strs, x), x)
adamc@64 4168 in
adamc@64 4169 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 4170 end
adamc@203 4171 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs)
adamc@563 4172 | L'.SgiClassAbs (x, n, k) =>
adamc@211 4173 let
adamc@211 4174 val (cons, x) =
adamc@211 4175 if SS.member (cons, x) then
adamc@211 4176 (cons, "?" ^ x)
adamc@211 4177 else
adamc@211 4178 (SS.add (cons, x), x)
adamc@211 4179 in
adamc@563 4180 ((L'.SgiClassAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@211 4181 end
adamc@563 4182 | L'.SgiClass (x, n, k, c) =>
adamc@211 4183 let
adamc@211 4184 val (cons, x) =
adamc@211 4185 if SS.member (cons, x) then
adamc@211 4186 (cons, "?" ^ x)
adamc@211 4187 else
adamc@211 4188 (SS.add (cons, x), x)
adamc@211 4189 in
adamc@563 4190 ((L'.SgiClass (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@203 4191 end)
adamc@64 4192
adamc@63 4193 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 4194 in
adamc@83 4195 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 4196 end
adamc@31 4197 | L.StrVar x =>
adamc@31 4198 (case E.lookupStr env x of
adamc@31 4199 NONE =>
adamc@31 4200 (strError env (UnboundStr (loc, x));
adamc@83 4201 (strerror, sgnerror, []))
adamc@83 4202 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 4203 | L.StrProj (str, x) =>
adamc@34 4204 let
adamc@83 4205 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 4206 in
adamc@34 4207 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 4208 NONE => (strError env (UnboundStr (loc, x));
adamc@83 4209 (strerror, sgnerror, []))
adamc@83 4210 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 4211 end
adamc@41 4212 | L.StrFun (m, dom, ranO, str) =>
adamc@41 4213 let
adamc@83 4214 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 4215 val (env', n) = E.pushStrNamed env m dom'
adamc@834 4216 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = []}
adamc@834 4217 val (str', actual, gs2) = elabStr (env', denv') str
adamc@41 4218
adamc@83 4219 val (formal, gs3) =
adamc@41 4220 case ranO of
adamc@83 4221 NONE => (actual, [])
adamc@41 4222 | SOME ran =>
adamc@41 4223 let
adamc@834 4224 val (ran', gs) = elabSgn (env', denv') ran
adamc@41 4225 in
adamc@1000 4226 subSgn env' loc actual ran';
adamc@83 4227 (ran', gs)
adamc@41 4228 end
adamc@41 4229 in
adamc@41 4230 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 4231 (L'.SgnFun (m, n, dom', formal), loc),
adamc@228 4232 enD gs1 @ gs2 @ enD gs3)
adamc@41 4233 end
adamc@44 4234 | L.StrApp (str1, str2) =>
adamc@44 4235 let
adamc@83 4236 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@325 4237 val str2 =
adamc@325 4238 case sgn1 of
adamc@325 4239 (L'.SgnFun (_, _, dom, _), _) =>
adamc@1275 4240 let
adamc@1275 4241 val s = wildifyStr env (str2, dom)
adamc@1275 4242 in
adamc@1275 4243 (*Print.preface ("Wild", SourcePrint.p_str s);*)
adamc@1275 4244 s
adamc@1275 4245 end
adamc@325 4246 | _ => str2
adamc@83 4247 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@1270 4248 in
adamc@44 4249 case #1 (hnormSgn env sgn1) of
adamc@83 4250 L'.SgnError => (strerror, sgnerror, [])
adamc@44 4251 | L'.SgnFun (m, n, dom, ran) =>
adamc@1000 4252 (subSgn env loc sgn2 dom;
adamc@44 4253 case #1 (hnormSgn env ran) of
adamc@83 4254 L'.SgnError => (strerror, sgnerror, [])
adamc@44 4255 | L'.SgnConst sgis =>
adamc@44 4256 ((L'.StrApp (str1', str2'), loc),
adamc@83 4257 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 4258 gs1 @ gs2)
adamc@44 4259 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 4260 | _ => (strError env (NotFunctor sgn1);
adamc@83 4261 (strerror, sgnerror, []))
adamc@44 4262 end
adamc@31 4263
adamc@987 4264 fun resolveClass env = E.resolveClass (hnormCon env) (consEq env dummy) env
adamc@833 4265
adamc@325 4266 fun elabFile basis topStr topSgn env file =
adamc@56 4267 let
adamc@713 4268 val () = mayDelay := true
adamc@713 4269 val () = delayedUnifs := []
adamc@830 4270 val () = delayedExhaustives := []
adamc@713 4271
adamc@83 4272 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 4273 val () = case gs of
adamc@83 4274 [] => ()
adamc@207 4275 | _ => (app (fn (_, env, _, c1, c2) =>
adamc@207 4276 prefaces "Unresolved"
adamc@207 4277 [("c1", p_con env c1),
adamc@207 4278 ("c2", p_con env c2)]) gs;
adamc@207 4279 raise Fail "Unresolved disjointness constraints in Basis")
adamc@83 4280
adamc@56 4281 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@210 4282 val () = basis_r := basis_n
adamc@56 4283
adamc@628 4284 val (ds, env') = dopen env' {str = basis_n, strs = [], sgn = sgn}
adamc@56 4285
adamc@56 4286 fun discoverC r x =
adamc@56 4287 case E.lookupC env' x of
adamc@56 4288 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 4289 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 4290 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 4291
adamc@56 4292 val () = discoverC int "int"
adamc@56 4293 val () = discoverC float "float"
adamc@56 4294 val () = discoverC string "string"
adamc@821 4295 val () = discoverC char "char"
adamc@203 4296 val () = discoverC table "sql_table"
adamc@56 4297
adamc@325 4298 val (topSgn, gs) = elabSgn (env', D.empty) (L.SgnConst topSgn, ErrorMsg.dummySpan)
adamc@325 4299 val () = case gs of
adamc@325 4300 [] => ()
adamc@325 4301 | _ => raise Fail "Unresolved disjointness constraints in top.urs"
adamc@325 4302 val (topStr, topSgn', gs) = elabStr (env', D.empty) (L.StrConst topStr, ErrorMsg.dummySpan)
adamc@325 4303 val () = case gs of
adamc@325 4304 [] => ()
adamc@329 4305 | _ => app (fn Disjoint (loc, env, denv, c1, c2) =>
adamc@329 4306 (case D.prove env denv (c1, c2, loc) of
adamc@329 4307 [] => ()
adamc@329 4308 | _ =>
adamc@329 4309 (prefaces "Unresolved constraint in top.ur"
adamc@329 4310 [("loc", PD.string (ErrorMsg.spanToString loc)),
adamc@329 4311 ("c1", p_con env c1),
adamc@720 4312 ("c2", p_con env c2)];
adamc@478 4313 raise Fail "Unresolved constraint in top.ur"))
adamc@564 4314 | TypeClass (env, c, r, loc) =>
adamc@564 4315 let
adamc@564 4316 val c = normClassKey env c
adamc@564 4317 in
adamc@833 4318 case resolveClass env c of
adamc@564 4319 SOME e => r := SOME e
adamc@564 4320 | NONE => expError env (Unresolvable (loc, c))
adamc@634 4321 end) gs
adamc@564 4322
adamc@1000 4323 val () = subSgn env' ErrorMsg.dummySpan topSgn' topSgn
adamc@720 4324
adamc@325 4325 val (env', top_n) = E.pushStrNamed env' "Top" topSgn
adamc@633 4326 val () = top_r := top_n
adamc@325 4327
adamc@628 4328 val (ds', env') = dopen env' {str = top_n, strs = [], sgn = topSgn}
adamc@325 4329
adamc@83 4330 fun elabDecl' (d, (env, gs)) =
adamc@76 4331 let
adamc@76 4332 val () = resetKunif ()
adamc@76 4333 val () = resetCunif ()
adam@1345 4334 val (ds, (env', _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 4335 in
adam@1345 4336 (**)
adam@1345 4337 (ds, (env', gs))
adamc@76 4338 end
adamc@76 4339
adam@1345 4340 val (file, (env'', gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@753 4341
adamc@777 4342 fun oneSummaryRound () =
adamc@777 4343 if ErrorMsg.anyErrors () then
adamc@777 4344 ()
adamc@777 4345 else
adamc@777 4346 let
adamc@777 4347 val delayed = !delayedUnifs
adamc@777 4348 in
adamc@777 4349 delayedUnifs := [];
adamc@777 4350 app (fn (loc, env, k, s1, s2) =>
adamc@777 4351 unifySummaries env (loc, k, normalizeRecordSummary env s1, normalizeRecordSummary env s2))
adamc@777 4352 delayed
adamc@777 4353 end
adam@1576 4354
adam@1576 4355 val checkConstraintErrors = ref (fn () => ())
adamc@56 4356 in
adamc@777 4357 oneSummaryRound ();
adamc@753 4358
adamc@753 4359 if ErrorMsg.anyErrors () then
adamc@753 4360 ()
adamc@753 4361 else
adamc@753 4362 let
adamc@753 4363 fun solver gs =
adamc@753 4364 let
adamc@753 4365 val (gs, solved) =
adamc@753 4366 ListUtil.foldlMapPartial
adamc@753 4367 (fn (g, solved) =>
adamc@753 4368 case g of
adamc@753 4369 Disjoint (loc, env, denv, c1, c2) =>
adamc@753 4370 (case D.prove env denv (c1, c2, loc) of
adamc@753 4371 [] => (NONE, true)
adamc@753 4372 | _ => (SOME g, solved))
adamc@753 4373 | TypeClass (env, c, r, loc) =>
adamc@753 4374 let
adamc@753 4375 fun default () = (SOME g, solved)
adamc@753 4376
adamc@753 4377 val c = normClassKey env c
adamc@753 4378 in
adamc@833 4379 case resolveClass env c of
adamc@753 4380 SOME e => (r := SOME e;
adamc@753 4381 (NONE, true))
adamc@753 4382 | NONE =>
adamc@753 4383 case #1 (hnormCon env c) of
adamc@753 4384 L'.CApp (f, x) =>
adamc@753 4385 (case (#1 (hnormCon env f), #1 (hnormCon env x)) of
adamc@753 4386 (L'.CKApp (f, _), L'.CRecord (k, xcs)) =>
adamc@753 4387 (case #1 (hnormCon env f) of
adamc@753 4388 L'.CModProj (top_n', [], "folder") =>
adamc@753 4389 if top_n' = top_n then
adamc@753 4390 let
adamc@753 4391 val e = (L'.EModProj (top_n, ["Folder"], "nil"), loc)
adamc@753 4392 val e = (L'.EKApp (e, k), loc)
adamc@753 4393
adamc@753 4394 val (folder, _) = foldr (fn ((x, c), (folder, xcs)) =>
adamc@753 4395 let
adamc@753 4396 val e = (L'.EModProj (top_n, ["Folder"],
adamc@753 4397 "cons"), loc)
adamc@753 4398 val e = (L'.EKApp (e, k), loc)
adamc@753 4399 val e = (L'.ECApp (e,
adamc@753 4400 (L'.CRecord (k, xcs),
adamc@753 4401 loc)), loc)
adamc@753 4402 val e = (L'.ECApp (e, x), loc)
adamc@753 4403 val e = (L'.ECApp (e, c), loc)
adamc@753 4404 val e = (L'.EApp (e, folder), loc)
adamc@753 4405 in
adamc@753 4406 (e, (x, c) :: xcs)
adamc@753 4407 end)
adamc@753 4408 (e, []) xcs
adamc@753 4409 in
adamc@753 4410 (r := SOME folder;
adamc@753 4411 (NONE, true))
adamc@753 4412 end
adamc@753 4413 else
adamc@753 4414 default ()
adamc@753 4415 | _ => default ())
adamc@753 4416 | _ => default ())
adamc@753 4417 | _ => default ()
adamc@753 4418 end)
adamc@753 4419 false gs
adamc@753 4420 in
adamc@753 4421 case (gs, solved) of
adamc@753 4422 ([], _) => ()
adamc@777 4423 | (_, true) => (oneSummaryRound (); solver gs)
adamc@753 4424 | _ =>
adam@1576 4425 checkConstraintErrors :=
adam@1576 4426 (fn () => app (fn Disjoint (loc, env, denv, c1, c2) =>
adam@1576 4427 let
adam@1576 4428 val c1' = ElabOps.hnormCon env c1
adam@1576 4429 val c2' = ElabOps.hnormCon env c2
adam@1576 4430
adam@1576 4431 fun isUnif (c, _) =
adam@1576 4432 case c of
adam@1576 4433 L'.CUnif _ => true
adam@1576 4434 | _ => false
adam@1576 4435
adam@1576 4436 fun maybeAttr (c, _) =
adam@1576 4437 case c of
adam@1576 4438 L'.CRecord ((L'.KType, _), xts) => true
adam@1576 4439 | _ => false
adam@1576 4440 in
adam@1576 4441 ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adam@1576 4442 eprefaces' [("Con 1", p_con env c1),
adam@1576 4443 ("Con 2", p_con env c2),
adam@1576 4444 ("Hnormed 1", p_con env c1'),
adam@1576 4445 ("Hnormed 2", p_con env c2')]
adam@1576 4446
adam@1576 4447 (*app (fn (loc, env, k, s1, s2) =>
adam@1576 4448 eprefaces' [("s1", p_summary env (normalizeRecordSummary env s1)),
adam@1576 4449 ("s2", p_summary env (normalizeRecordSummary env s2))])
adam@1576 4450 (!delayedUnifs);*)
adam@1576 4451 end
adam@1576 4452 | TypeClass (env, c, r, loc) =>
adam@1576 4453 expError env (Unresolvable (loc, c)))
adam@1576 4454 gs)
adamc@753 4455 end
adamc@753 4456 in
adamc@753 4457 solver gs
adamc@753 4458 end;
adamc@753 4459
adamc@713 4460 mayDelay := false;
adamc@713 4461
adamc@777 4462 if ErrorMsg.anyErrors () then
adamc@777 4463 ()
adamc@777 4464 else
adamc@777 4465 (app (fn (loc, env, k, s1, s2) =>
adamc@777 4466 unifySummaries env (loc, k, normalizeRecordSummary env s1, normalizeRecordSummary env s2)
adamc@777 4467 handle CUnify' err => (ErrorMsg.errorAt loc "Error in final record unification";
adam@1582 4468 cunifyError env err;
adam@1582 4469 case !reducedSummaries of
adam@1582 4470 NONE => ()
adam@1582 4471 | SOME (s1, s2) =>
adam@1582 4472 (ErrorMsg.errorAt loc "Stuck unifying these records after canceling matching pieces:";
adam@1582 4473 eprefaces' [("Have", s1),
adam@1582 4474 ("Need", s2)])))
adamc@777 4475 (!delayedUnifs);
adamc@777 4476 delayedUnifs := []);
adamc@713 4477
adam@1346 4478 if ErrorMsg.anyErrors () then
adam@1346 4479 ()
adam@1346 4480 else
adam@1584 4481 if List.exists kunifsInDecl file then
adam@1584 4482 case U.File.findDecl kunifsInDecl file of
adam@1584 4483 NONE => ()
adam@1584 4484 | SOME d => declError env'' (KunifsRemain [d])
adam@1584 4485 else
adam@1584 4486 ();
adam@1345 4487
adam@1346 4488 if ErrorMsg.anyErrors () then
adam@1346 4489 ()
adam@1346 4490 else
adam@1584 4491 if List.exists cunifsInDecl file then
adam@1584 4492 case U.File.findDecl cunifsInDecl file of
adam@1584 4493 NONE => ()
adam@1584 4494 | SOME d => declError env'' (CunifsRemain [d])
adam@1584 4495 else
adam@1584 4496 ();
adamc@750 4497
adamc@830 4498 if ErrorMsg.anyErrors () then
adamc@830 4499 ()
adamc@830 4500 else
adamc@837 4501 app (fn all as (env, _, _, loc) =>
adamc@830 4502 case exhaustive all of
adamc@830 4503 NONE => ()
adamc@830 4504 | SOME p => expError env (Inexhaustive (loc, p)))
adamc@830 4505 (!delayedExhaustives);
adamc@830 4506
adam@1576 4507 if ErrorMsg.anyErrors () then
adam@1576 4508 ()
adam@1576 4509 else
adam@1576 4510 !checkConstraintErrors ();
adam@1576 4511
adamc@799 4512 (*preface ("file", p_file env' file);*)
adam@1531 4513
adam@1531 4514 if !dumpTypes then
adam@1531 4515 let
adam@1531 4516 open L'
adam@1531 4517 open Print.PD
adam@1531 4518 open Print
adam@1531 4519
adam@1531 4520 fun dumpDecl (d, env) =
adam@1531 4521 case #1 d of
adam@1531 4522 DCon (x, _, k, _) => (print (box [string x,
adam@1531 4523 space,
adam@1531 4524 string "::",
adam@1531 4525 space,
adam@1531 4526 p_kind env k,
adam@1531 4527 newline,
adam@1531 4528 newline]);
adam@1531 4529 E.declBinds env d)
adam@1531 4530 | DVal (x, _, t, _) => (print (box [string x,
adam@1531 4531 space,
adam@1531 4532 string ":",
adam@1531 4533 space,
adam@1531 4534 p_con env t,
adam@1531 4535 newline,
adam@1531 4536 newline]);
adam@1531 4537 E.declBinds env d)
adam@1531 4538 | DValRec vis => (app (fn (x, _, t, _) => print (box [string x,
adam@1531 4539 space,
adam@1531 4540 string ":",
adam@1531 4541 space,
adam@1531 4542 p_con env t,
adam@1531 4543 newline,
adam@1531 4544 newline])) vis;
adam@1531 4545 E.declBinds env d)
adam@1531 4546 | DStr (x, _, _, str) => (print (box [string ("<" ^ x ^ ">"),
adam@1531 4547 newline,
adam@1531 4548 newline]);
adam@1531 4549 dumpStr (str, env);
adam@1531 4550 print (box [string ("</" ^ x ^ ">"),
adam@1531 4551 newline,
adam@1531 4552 newline]);
adam@1531 4553 E.declBinds env d)
adam@1531 4554 | _ => E.declBinds env d
adam@1531 4555
adam@1531 4556 and dumpStr (str, env) =
adam@1531 4557 case #1 str of
adam@1531 4558 StrConst ds => ignore (foldl dumpDecl env ds)
adam@1531 4559 | _ => ()
adam@1531 4560 in
adam@1531 4561 ignore (foldl dumpDecl env' file)
adam@1531 4562 end
adam@1531 4563 else
adam@1531 4564 ();
adam@1345 4565
adamc@325 4566 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan)
adamc@325 4567 :: ds
adamc@325 4568 @ (L'.DStr ("Top", top_n, topSgn, topStr), ErrorMsg.dummySpan)
adamc@325 4569 :: ds' @ file
adamc@56 4570 end
adamc@2 4571
adamc@2 4572 end