annotate src/elaborate.sml @ 1744:6fcce0592178

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