annotate src/elaborate.sml @ 564:803b2f3bb86b

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