annotate src/elaborate.sml @ 1303:c7b9a33c26c8

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