annotate src/elaborate.sml @ 1387:dd99e1702432

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