annotate src/elaborate.sml @ 1305:a6fc03d28170

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