annotate src/elaborate.sml @ 813:7b380e2b9e68

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