annotate src/elaborate.sml @ 622:d64533157f40

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