annotate src/elaborate.sml @ 1639:6c00d8af6239

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