annotate src/elaborate.sml @ 1302:d008c4c43a0a

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