annotate src/elaborate.sml @ 645:1b571a05874c

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