annotate src/elaborate.sml @ 629:e68de2a5506b

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