annotate src/elaborate.sml @ 866:03e7f111fe99

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