annotate src/elaborate.sml @ 518:685d232bd1a5

Remove some isUnitCon cases
author Adam Chlipala <adamc@hcoop.net>
date Thu, 27 Nov 2008 11:17:56 -0500
parents 0fc08d1750e1
children 44958d74c43f
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@211 2062 | L.SgiClassAbs x =>
adamc@211 2063 let
adamc@211 2064 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@211 2065 val (env, n) = E.pushCNamed env x k NONE
adamc@211 2066 val env = E.pushClass env n
adamc@211 2067 in
adamc@211 2068 ([(L'.SgiClassAbs (x, n), loc)], (env, denv, []))
adamc@211 2069 end
adamc@211 2070
adamc@211 2071 | L.SgiClass (x, c) =>
adamc@211 2072 let
adamc@211 2073 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@211 2074 val (c', ck, gs) = elabCon (env, denv) c
adamc@211 2075 val (env, n) = E.pushCNamed env x k (SOME c')
adamc@211 2076 val env = E.pushClass env n
adamc@211 2077 in
adamc@211 2078 checkKind env c' ck k;
adamc@211 2079 ([(L'.SgiClass (x, n, c'), loc)], (env, denv, []))
adamc@211 2080 end
adamc@211 2081
adamc@83 2082 and elabSgn (env, denv) (sgn, loc) =
adamc@31 2083 case sgn of
adamc@31 2084 L.SgnConst sgis =>
adamc@31 2085 let
adamc@88 2086 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 2087
adamc@62 2088 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 2089 case sgi of
adamc@62 2090 L'.SgiConAbs (x, _, _) =>
adamc@62 2091 (if SS.member (cons, x) then
adamc@62 2092 sgnError env (DuplicateCon (loc, x))
adamc@62 2093 else
adamc@62 2094 ();
adamc@62 2095 (SS.add (cons, x), vals, sgns, strs))
adamc@62 2096 | L'.SgiCon (x, _, _, _) =>
adamc@62 2097 (if SS.member (cons, x) then
adamc@62 2098 sgnError env (DuplicateCon (loc, x))
adamc@62 2099 else
adamc@62 2100 ();
adamc@62 2101 (SS.add (cons, x), vals, sgns, strs))
adamc@191 2102 | L'.SgiDatatype (x, _, _, xncs) =>
adamc@156 2103 let
adamc@156 2104 val vals = foldl (fn ((x, _, _), vals) =>
adamc@156 2105 (if SS.member (vals, x) then
adamc@156 2106 sgnError env (DuplicateVal (loc, x))
adamc@156 2107 else
adamc@156 2108 ();
adamc@156 2109 SS.add (vals, x)))
adamc@156 2110 vals xncs
adamc@156 2111 in
adamc@156 2112 if SS.member (cons, x) then
adamc@156 2113 sgnError env (DuplicateCon (loc, x))
adamc@156 2114 else
adamc@156 2115 ();
adamc@156 2116 (SS.add (cons, x), vals, sgns, strs)
adamc@156 2117 end
adamc@191 2118 | L'.SgiDatatypeImp (x, _, _, _, _, _, _) =>
adamc@156 2119 (if SS.member (cons, x) then
adamc@156 2120 sgnError env (DuplicateCon (loc, x))
adamc@156 2121 else
adamc@156 2122 ();
adamc@156 2123 (SS.add (cons, x), vals, sgns, strs))
adamc@62 2124 | L'.SgiVal (x, _, _) =>
adamc@62 2125 (if SS.member (vals, x) then
adamc@62 2126 sgnError env (DuplicateVal (loc, x))
adamc@62 2127 else
adamc@62 2128 ();
adamc@62 2129 (cons, SS.add (vals, x), sgns, strs))
adamc@62 2130 | L'.SgiSgn (x, _, _) =>
adamc@62 2131 (if SS.member (sgns, x) then
adamc@62 2132 sgnError env (DuplicateSgn (loc, x))
adamc@62 2133 else
adamc@62 2134 ();
adamc@62 2135 (cons, vals, SS.add (sgns, x), strs))
adamc@62 2136 | L'.SgiStr (x, _, _) =>
adamc@62 2137 (if SS.member (strs, x) then
adamc@62 2138 sgnError env (DuplicateStr (loc, x))
adamc@62 2139 else
adamc@62 2140 ();
adamc@88 2141 (cons, vals, sgns, SS.add (strs, x)))
adamc@203 2142 | L'.SgiConstraint _ => (cons, vals, sgns, strs)
adamc@211 2143 | L'.SgiClassAbs (x, _) =>
adamc@211 2144 (if SS.member (cons, x) then
adamc@211 2145 sgnError env (DuplicateCon (loc, x))
adamc@211 2146 else
adamc@211 2147 ();
adamc@211 2148 (SS.add (cons, x), vals, sgns, strs))
adamc@211 2149 | L'.SgiClass (x, _, _) =>
adamc@211 2150 (if SS.member (cons, x) then
adamc@211 2151 sgnError env (DuplicateCon (loc, x))
adamc@211 2152 else
adamc@211 2153 ();
adamc@460 2154 (SS.add (cons, x), vals, sgns, strs)))
adamc@62 2155 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 2156 in
adamc@83 2157 ((L'.SgnConst sgis', loc), gs)
adamc@31 2158 end
adamc@31 2159 | L.SgnVar x =>
adamc@31 2160 (case E.lookupSgn env x of
adamc@31 2161 NONE =>
adamc@31 2162 (sgnError env (UnboundSgn (loc, x));
adamc@83 2163 ((L'.SgnError, loc), []))
adamc@83 2164 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 2165 | L.SgnFun (m, dom, ran) =>
adamc@41 2166 let
adamc@83 2167 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2168 val (env', n) = E.pushStrNamed env m dom'
adamc@469 2169 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = []}
adamc@469 2170 val (ran', gs2) = elabSgn (env', denv') ran
adamc@41 2171 in
adamc@83 2172 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 2173 end
adamc@42 2174 | L.SgnWhere (sgn, x, c) =>
adamc@42 2175 let
adamc@83 2176 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 2177 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 2178 in
adamc@42 2179 case #1 (hnormSgn env sgn') of
adamc@83 2180 L'.SgnError => (sgnerror, [])
adamc@42 2181 | L'.SgnConst sgis =>
adamc@75 2182 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 2183 x' = x andalso
adamc@75 2184 (unifyKinds k ck
adamc@75 2185 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 2186 true)
adamc@42 2187 | _ => false) sgis then
adamc@83 2188 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 2189 else
adamc@42 2190 (sgnError env (UnWhereable (sgn', x));
adamc@83 2191 (sgnerror, []))
adamc@42 2192 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 2193 (sgnerror, []))
adamc@42 2194 end
adamc@59 2195 | L.SgnProj (m, ms, x) =>
adamc@59 2196 (case E.lookupStr env m of
adamc@59 2197 NONE => (strError env (UnboundStr (loc, m));
adamc@83 2198 (sgnerror, []))
adamc@59 2199 | SOME (n, sgn) =>
adamc@59 2200 let
adamc@59 2201 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 2202 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 2203 NONE => (strError env (UnboundStr (loc, m));
adamc@59 2204 (strerror, sgnerror))
adamc@59 2205 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 2206 ((L'.StrVar n, loc), sgn) ms
adamc@59 2207 in
adamc@59 2208 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 2209 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 2210 (sgnerror, []))
adamc@83 2211 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 2212 end)
adamc@59 2213
adamc@31 2214
adamc@66 2215 fun selfify env {str, strs, sgn} =
adamc@66 2216 case #1 (hnormSgn env sgn) of
adamc@66 2217 L'.SgnError => sgn
adamc@66 2218 | L'.SgnVar _ => sgn
adamc@66 2219
adamc@66 2220 | L'.SgnConst sgis =>
adamc@66 2221 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 2222 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@191 2223 | (L'.SgiDatatype (x, n, xs, xncs), loc) =>
adamc@191 2224 (L'.SgiDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
adamc@218 2225 | (L'.SgiClassAbs (x, n), loc) =>
adamc@218 2226 (L'.SgiClass (x, n, (L'.CModProj (str, strs, x), loc)), loc)
adamc@66 2227 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 2228 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 2229 | x => x) sgis), #2 sgn)
adamc@66 2230 | L'.SgnFun _ => sgn
adamc@66 2231 | L'.SgnWhere _ => sgn
adamc@66 2232 | L'.SgnProj (m, ms, x) =>
adamc@66 2233 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2234 (L'.StrVar m, #2 sgn) ms,
adamc@66 2235 sgn = #2 (E.lookupStrNamed env m),
adamc@66 2236 field = x} of
adamc@66 2237 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 2238 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 2239
adamc@66 2240 fun selfifyAt env {str, sgn} =
adamc@66 2241 let
adamc@66 2242 fun self (str, _) =
adamc@66 2243 case str of
adamc@66 2244 L'.StrVar x => SOME (x, [])
adamc@66 2245 | L'.StrProj (str, x) =>
adamc@66 2246 (case self str of
adamc@66 2247 NONE => NONE
adamc@66 2248 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 2249 | _ => NONE
adamc@66 2250 in
adamc@66 2251 case self str of
adamc@66 2252 NONE => sgn
adamc@66 2253 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 2254 end
adamc@66 2255
adamc@88 2256 fun dopen (env, denv) {str, strs, sgn} =
adamc@66 2257 let
adamc@66 2258 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2259 (L'.StrVar str, #2 sgn) strs
adamc@66 2260 in
adamc@66 2261 case #1 (hnormSgn env sgn) of
adamc@66 2262 L'.SgnConst sgis =>
adamc@88 2263 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
adamc@162 2264 let
adamc@162 2265 val d =
adamc@162 2266 case sgi of
adamc@162 2267 L'.SgiConAbs (x, n, k) =>
adamc@162 2268 let
adamc@162 2269 val c = (L'.CModProj (str, strs, x), loc)
adamc@162 2270 in
adamc@162 2271 (L'.DCon (x, n, k, c), loc)
adamc@162 2272 end
adamc@162 2273 | L'.SgiCon (x, n, k, c) =>
adamc@162 2274 (L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@191 2275 | L'.SgiDatatype (x, n, xs, xncs) =>
adamc@191 2276 (L'.DDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
adamc@191 2277 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@191 2278 (L'.DDatatypeImp (x, n, m1, ms, x', xs, xncs), loc)
adamc@162 2279 | L'.SgiVal (x, n, t) =>
adamc@162 2280 (L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)
adamc@162 2281 | L'.SgiStr (x, n, sgn) =>
adamc@162 2282 (L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)
adamc@162 2283 | L'.SgiSgn (x, n, sgn) =>
adamc@162 2284 (L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)
adamc@162 2285 | L'.SgiConstraint (c1, c2) =>
adamc@162 2286 (L'.DConstraint (c1, c2), loc)
adamc@211 2287 | L'.SgiClassAbs (x, n) =>
adamc@211 2288 let
adamc@211 2289 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@211 2290 val c = (L'.CModProj (str, strs, x), loc)
adamc@211 2291 in
adamc@211 2292 (L'.DCon (x, n, k, c), loc)
adamc@211 2293 end
adamc@211 2294 | L'.SgiClass (x, n, _) =>
adamc@211 2295 let
adamc@211 2296 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@211 2297 val c = (L'.CModProj (str, strs, x), loc)
adamc@211 2298 in
adamc@211 2299 (L'.DCon (x, n, k, c), loc)
adamc@211 2300 end
adamc@162 2301 in
adamc@162 2302 (d, (E.declBinds env' d, denv'))
adamc@162 2303 end)
adamc@88 2304 (env, denv) sgis
adamc@66 2305 | _ => (strError env (UnOpenable sgn);
adamc@88 2306 ([], (env, denv)))
adamc@66 2307 end
adamc@66 2308
adamc@31 2309 fun sgiOfDecl (d, loc) =
adamc@31 2310 case d of
adamc@123 2311 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@156 2312 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
adamc@156 2313 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
adamc@123 2314 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 2315 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 2316 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 2317 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2318 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2319 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 2320 | L'.DExport _ => []
adamc@460 2321 | L'.DTable (tn, x, n, c) => [(L'.SgiVal (x, n, (L'.CApp (tableOf (), c), loc)), loc)]
adamc@460 2322 | L'.DSequence (tn, x, n) => [(L'.SgiVal (x, n, sequenceOf ()), loc)]
adamc@213 2323 | L'.DClass (x, n, c) => [(L'.SgiClass (x, n, c), loc)]
adamc@271 2324 | L'.DDatabase _ => []
adamc@460 2325 | L'.DCookie (tn, x, n, c) => [(L'.SgiVal (x, n, (L'.CApp (cookieOf (), c), loc)), loc)]
adamc@88 2326
adamc@88 2327 fun sgiBindsD (env, denv) (sgi, _) =
adamc@88 2328 case sgi of
adamc@90 2329 L'.SgiConstraint (c1, c2) =>
adamc@90 2330 (case D.assert env denv (c1, c2) of
adamc@90 2331 (denv, []) => denv
adamc@90 2332 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
adamc@88 2333 | _ => denv
adamc@31 2334
adamc@86 2335 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
adamc@469 2336 ((*prefaces "subSgn" [("sgn1", p_sgn env sgn1),
adamc@469 2337 ("sgn2", p_sgn env sgn2)];*)
adamc@35 2338 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 2339 (L'.SgnError, _) => ()
adamc@31 2340 | (_, L'.SgnError) => ()
adamc@31 2341
adamc@31 2342 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 2343 let
adamc@362 2344 (*val () = prefaces "subSgn" [("sgn1", p_sgn env sgn1),
adamc@362 2345 ("sgn2", p_sgn env sgn2),
adamc@362 2346 ("sgis1", p_sgn env (L'.SgnConst sgis1, loc2)),
adamc@362 2347 ("sgis2", p_sgn env (L'.SgnConst sgis2, loc2))]*)
adamc@362 2348
adamc@156 2349 fun folder (sgi2All as (sgi, loc), (env, denv)) =
adamc@31 2350 let
adamc@362 2351 (*val () = prefaces "folder" [("sgis1", p_sgn env (L'.SgnConst sgis1, loc2))]*)
adamc@362 2352
adamc@31 2353 fun seek p =
adamc@31 2354 let
adamc@88 2355 fun seek (env, denv) ls =
adamc@31 2356 case ls of
adamc@31 2357 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@88 2358 (env, denv))
adamc@31 2359 | h :: t =>
adamc@469 2360 case p (env, h) of
adamc@469 2361 NONE =>
adamc@469 2362 let
adamc@469 2363 val env = case #1 h of
adamc@469 2364 L'.SgiCon (x, n, k, c) =>
adamc@471 2365 if E.checkENamed env n then
adamc@471 2366 env
adamc@471 2367 else
adamc@471 2368 E.pushCNamedAs env x n k (SOME c)
adamc@469 2369 | L'.SgiConAbs (x, n, k) =>
adamc@471 2370 if E.checkENamed env n then
adamc@471 2371 env
adamc@471 2372 else
adamc@471 2373 E.pushCNamedAs env x n k NONE
adamc@469 2374 | _ => env
adamc@469 2375 in
adamc@469 2376 seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
adamc@469 2377 end
adamc@88 2378 | SOME envs => envs
adamc@31 2379 in
adamc@88 2380 seek (env, denv) sgis1
adamc@31 2381 end
adamc@31 2382 in
adamc@31 2383 case sgi of
adamc@31 2384 L'.SgiConAbs (x, n2, k2) =>
adamc@469 2385 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@31 2386 let
adamc@41 2387 fun found (x', n1, k1, co1) =
adamc@41 2388 if x = x' then
adamc@41 2389 let
adamc@41 2390 val () = unifyKinds k1 k2
adamc@41 2391 handle KUnify (k1, k2, err) =>
adamc@41 2392 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 2393 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 2394 in
adamc@41 2395 SOME (if n1 = n2 then
adamc@41 2396 env
adamc@41 2397 else
adamc@88 2398 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
adamc@88 2399 denv)
adamc@41 2400 end
adamc@41 2401 else
adamc@41 2402 NONE
adamc@31 2403 in
adamc@31 2404 case sgi1 of
adamc@41 2405 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 2406 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@191 2407 | L'.SgiDatatype (x', n1, xs, _) =>
adamc@191 2408 let
adamc@191 2409 val k = (L'.KType, loc)
adamc@191 2410 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2411 in
adamc@191 2412 found (x', n1, k', NONE)
adamc@191 2413 end
adamc@191 2414 | L'.SgiDatatypeImp (x', n1, m1, ms, s, xs, _) =>
adamc@191 2415 let
adamc@191 2416 val k = (L'.KType, loc)
adamc@191 2417 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2418 in
adamc@191 2419 found (x', n1, k', SOME (L'.CModProj (m1, ms, s), loc))
adamc@191 2420 end
adamc@211 2421 | L'.SgiClassAbs (x', n1) => found (x', n1,
adamc@218 2422 (L'.KArrow ((L'.KType, loc),
adamc@218 2423 (L'.KType, loc)), loc),
adamc@218 2424 NONE)
adamc@211 2425 | L'.SgiClass (x', n1, c) => found (x', n1,
adamc@211 2426 (L'.KArrow ((L'.KType, loc),
adamc@211 2427 (L'.KType, loc)), loc),
adamc@211 2428 SOME c)
adamc@31 2429 | _ => NONE
adamc@31 2430 end)
adamc@31 2431
adamc@31 2432 | L'.SgiCon (x, n2, k2, c2) =>
adamc@469 2433 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 2434 let
adamc@211 2435 fun found (x', n1, k1, c1) =
adamc@211 2436 if x = x' then
adamc@211 2437 let
adamc@327 2438 fun good () =
adamc@327 2439 let
adamc@327 2440 val env = E.pushCNamedAs env x n2 k2 (SOME c2)
adamc@327 2441 val env = if n1 = n2 then
adamc@327 2442 env
adamc@327 2443 else
adamc@327 2444 E.pushCNamedAs env x n1 k1 (SOME c1)
adamc@327 2445 in
adamc@327 2446 SOME (env, denv)
adamc@327 2447 end
adamc@211 2448 in
adamc@211 2449 (case unifyCons (env, denv) c1 c2 of
adamc@211 2450 [] => good ()
adamc@211 2451 | _ => NONE)
adamc@211 2452 handle CUnify (c1, c2, err) =>
adamc@211 2453 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@211 2454 good ())
adamc@211 2455 end
adamc@211 2456 else
adamc@211 2457 NONE
adamc@211 2458 in
adamc@211 2459 case sgi1 of
adamc@211 2460 L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@211 2461 | L'.SgiClass (x', n1, c1) =>
adamc@211 2462 found (x', n1, (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc), c1)
adamc@211 2463 | _ => NONE
adamc@211 2464 end)
adamc@31 2465
adamc@191 2466 | L'.SgiDatatype (x, n2, xs2, xncs2) =>
adamc@469 2467 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@160 2468 let
adamc@191 2469 fun found (n1, xs1, xncs1) =
adamc@160 2470 let
adamc@160 2471 fun mismatched ue =
adamc@160 2472 (sgnError env (SgiMismatchedDatatypes (sgi1All, sgi2All, ue));
adamc@160 2473 SOME (env, denv))
adamc@156 2474
adamc@191 2475 val k = (L'.KType, loc)
adamc@191 2476 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs1
adamc@191 2477
adamc@160 2478 fun good () =
adamc@160 2479 let
adamc@471 2480 val env = E.sgiBinds env sgi1All
adamc@160 2481 val env = if n1 = n2 then
adamc@160 2482 env
adamc@160 2483 else
adamc@471 2484 E.pushCNamedAs env x n2 k'
adamc@471 2485 (SOME (L'.CNamed n1, loc))
adamc@160 2486 in
adamc@160 2487 SOME (env, denv)
adamc@160 2488 end
adamc@156 2489
adamc@397 2490 val env = E.pushCNamedAs env x n1 k' NONE
adamc@397 2491 val env = if n1 = n2 then
adamc@397 2492 env
adamc@397 2493 else
adamc@397 2494 E.pushCNamedAs env x n2 k' (SOME (L'.CNamed n1, loc))
adamc@191 2495 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs1
adamc@160 2496 fun xncBad ((x1, _, t1), (x2, _, t2)) =
adamc@160 2497 String.compare (x1, x2) <> EQUAL
adamc@160 2498 orelse case (t1, t2) of
adamc@160 2499 (NONE, NONE) => false
adamc@160 2500 | (SOME t1, SOME t2) =>
adamc@160 2501 not (List.null (unifyCons (env, denv) t1 t2))
adamc@160 2502 | _ => true
adamc@160 2503 in
adamc@191 2504 (if xs1 <> xs2
adamc@191 2505 orelse length xncs1 <> length xncs2
adamc@160 2506 orelse ListPair.exists xncBad (xncs1, xncs2) then
adamc@160 2507 mismatched NONE
adamc@160 2508 else
adamc@160 2509 good ())
adamc@160 2510 handle CUnify ue => mismatched (SOME ue)
adamc@160 2511 end
adamc@160 2512 in
adamc@160 2513 case sgi1 of
adamc@191 2514 L'.SgiDatatype (x', n1, xs, xncs1) =>
adamc@160 2515 if x' = x then
adamc@191 2516 found (n1, xs, xncs1)
adamc@160 2517 else
adamc@160 2518 NONE
adamc@191 2519 | L'.SgiDatatypeImp (x', n1, _, _, _, xs, xncs1) =>
adamc@162 2520 if x' = x then
adamc@191 2521 found (n1, xs, xncs1)
adamc@162 2522 else
adamc@162 2523 NONE
adamc@160 2524 | _ => NONE
adamc@160 2525 end)
adamc@156 2526
adamc@191 2527 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, xs, _) =>
adamc@469 2528 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@158 2529 case sgi1 of
adamc@191 2530 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _, _) =>
adamc@158 2531 if x = x' then
adamc@158 2532 let
adamc@158 2533 val k = (L'.KType, loc)
adamc@191 2534 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@158 2535 val t1 = (L'.CModProj (m11, ms1, s1), loc)
adamc@158 2536 val t2 = (L'.CModProj (m12, ms2, s2), loc)
adamc@158 2537
adamc@158 2538 fun good () =
adamc@158 2539 let
adamc@191 2540 val env = E.pushCNamedAs env x n1 k' (SOME t1)
adamc@191 2541 val env = E.pushCNamedAs env x n2 k' (SOME t2)
adamc@158 2542 in
adamc@158 2543 SOME (env, denv)
adamc@158 2544 end
adamc@158 2545 in
adamc@158 2546 (case unifyCons (env, denv) t1 t2 of
adamc@158 2547 [] => good ()
adamc@158 2548 | _ => NONE)
adamc@158 2549 handle CUnify (c1, c2, err) =>
adamc@158 2550 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@158 2551 good ())
adamc@158 2552 end
adamc@158 2553 else
adamc@158 2554 NONE
adamc@158 2555
adamc@158 2556 | _ => NONE)
adamc@156 2557
adamc@32 2558 | L'.SgiVal (x, n2, c2) =>
adamc@469 2559 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@32 2560 case sgi1 of
adamc@41 2561 L'.SgiVal (x', n1, c1) =>
adamc@41 2562 if x = x' then
adamc@362 2563 ((*prefaces "Pre" [("c1", p_con env c1),
adamc@362 2564 ("c2", p_con env c2)];*)
adamc@362 2565 case unifyCons (env, denv) c1 c2 of
adamc@88 2566 [] => SOME (env, denv)
adamc@86 2567 | _ => NONE)
adamc@86 2568 handle CUnify (c1, c2, err) =>
adamc@86 2569 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@88 2570 SOME (env, denv))
adamc@41 2571 else
adamc@41 2572 NONE
adamc@32 2573 | _ => NONE)
adamc@32 2574
adamc@33 2575 | L'.SgiStr (x, n2, sgn2) =>
adamc@469 2576 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@33 2577 case sgi1 of
adamc@41 2578 L'.SgiStr (x', n1, sgn1) =>
adamc@41 2579 if x = x' then
adamc@66 2580 let
adamc@86 2581 val () = subSgn (env, denv) sgn1 sgn2
adamc@66 2582 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 2583 val env = if n1 = n2 then
adamc@66 2584 env
adamc@66 2585 else
adamc@66 2586 E.pushStrNamedAs env x n2
adamc@66 2587 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 2588 sgn = sgn2})
adamc@66 2589 in
adamc@88 2590 SOME (env, denv)
adamc@66 2591 end
adamc@41 2592 else
adamc@41 2593 NONE
adamc@33 2594 | _ => NONE)
adamc@59 2595
adamc@59 2596 | L'.SgiSgn (x, n2, sgn2) =>
adamc@469 2597 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@59 2598 case sgi1 of
adamc@59 2599 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 2600 if x = x' then
adamc@65 2601 let
adamc@86 2602 val () = subSgn (env, denv) sgn1 sgn2
adamc@86 2603 val () = subSgn (env, denv) sgn2 sgn1
adamc@65 2604
adamc@65 2605 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 2606 val env = if n1 = n2 then
adamc@65 2607 env
adamc@65 2608 else
adamc@65 2609 E.pushSgnNamedAs env x n1 sgn2
adamc@65 2610 in
adamc@88 2611 SOME (env, denv)
adamc@65 2612 end
adamc@59 2613 else
adamc@59 2614 NONE
adamc@59 2615 | _ => NONE)
adamc@88 2616
adamc@88 2617 | L'.SgiConstraint (c2, d2) =>
adamc@469 2618 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@88 2619 case sgi1 of
adamc@88 2620 L'.SgiConstraint (c1, d1) =>
adamc@88 2621 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
adamc@90 2622 let
adamc@90 2623 val (denv, gs) = D.assert env denv (c2, d2)
adamc@90 2624 in
adamc@90 2625 case gs of
adamc@90 2626 [] => ()
adamc@90 2627 | _ => raise Fail "subSgn: Sub-constraints remain";
adamc@90 2628
adamc@90 2629 SOME (env, denv)
adamc@90 2630 end
adamc@88 2631 else
adamc@88 2632 NONE
adamc@88 2633 | _ => NONE)
adamc@203 2634
adamc@211 2635 | L'.SgiClassAbs (x, n2) =>
adamc@469 2636 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 2637 let
adamc@211 2638 fun found (x', n1, co) =
adamc@211 2639 if x = x' then
adamc@211 2640 let
adamc@211 2641 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@211 2642 val env = E.pushCNamedAs env x n1 k co
adamc@211 2643 in
adamc@211 2644 SOME (if n1 = n2 then
adamc@211 2645 env
adamc@211 2646 else
adamc@211 2647 E.pushCNamedAs env x n2 k (SOME (L'.CNamed n1, loc2)),
adamc@211 2648 denv)
adamc@211 2649 end
adamc@211 2650 else
adamc@211 2651 NONE
adamc@211 2652 in
adamc@211 2653 case sgi1 of
adamc@211 2654 L'.SgiClassAbs (x', n1) => found (x', n1, NONE)
adamc@211 2655 | L'.SgiClass (x', n1, c) => found (x', n1, SOME c)
adamc@211 2656 | _ => NONE
adamc@211 2657 end)
adamc@211 2658 | L'.SgiClass (x, n2, c2) =>
adamc@469 2659 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@211 2660 let
adamc@211 2661 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@211 2662
adamc@211 2663 fun found (x', n1, c1) =
adamc@211 2664 if x = x' then
adamc@211 2665 let
adamc@218 2666 fun good () =
adamc@218 2667 let
adamc@218 2668 val env = E.pushCNamedAs env x n2 k (SOME c2)
adamc@218 2669 val env = if n1 = n2 then
adamc@218 2670 env
adamc@218 2671 else
adamc@218 2672 E.pushCNamedAs env x n1 k (SOME c1)
adamc@218 2673 in
adamc@218 2674 SOME (env, denv)
adamc@218 2675 end
adamc@211 2676 in
adamc@211 2677 (case unifyCons (env, denv) c1 c2 of
adamc@211 2678 [] => good ()
adamc@211 2679 | _ => NONE)
adamc@211 2680 handle CUnify (c1, c2, err) =>
adamc@211 2681 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@211 2682 good ())
adamc@211 2683 end
adamc@211 2684 else
adamc@211 2685 NONE
adamc@211 2686 in
adamc@211 2687 case sgi1 of
adamc@211 2688 L'.SgiClass (x', n1, c1) => found (x', n1, c1)
adamc@211 2689 | _ => NONE
adamc@211 2690 end)
adamc@31 2691 end
adamc@31 2692 in
adamc@88 2693 ignore (foldl folder (env, denv) sgis2)
adamc@31 2694 end
adamc@31 2695
adamc@41 2696 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 2697 let
adamc@480 2698 val ran2 =
adamc@43 2699 if n1 = n2 then
adamc@480 2700 ran2
adamc@43 2701 else
adamc@480 2702 subStrInSgn (n2, n1) ran2
adamc@43 2703 in
adamc@86 2704 subSgn (env, denv) dom2 dom1;
adamc@480 2705 subSgn (E.pushStrNamedAs env m1 n1 dom2, denv) ran1 ran2
adamc@43 2706 end
adamc@41 2707
adamc@469 2708 | _ => sgnError env (SgnWrongForm (sgn1, sgn2)))
adamc@41 2709
adamc@61 2710
adamc@312 2711 fun positive self =
adamc@312 2712 let
adamc@312 2713 open L
adamc@312 2714
adamc@312 2715 fun none (c, _) =
adamc@312 2716 case c of
adamc@312 2717 CAnnot (c, _) => none c
adamc@312 2718
adamc@312 2719 | TFun (c1, c2) => none c1 andalso none c2
adamc@312 2720 | TCFun (_, _, _, c) => none c
adamc@312 2721 | TRecord c => none c
adamc@312 2722
adamc@312 2723 | CVar ([], x) => x <> self
adamc@312 2724 | CVar _ => true
adamc@312 2725 | CApp (c1, c2) => none c1 andalso none c2
adamc@312 2726 | CAbs _ => false
adamc@312 2727 | CDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
adamc@312 2728
adamc@312 2729 | CName _ => true
adamc@312 2730
adamc@312 2731 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso none c2) xcs
adamc@312 2732 | CConcat (c1, c2) => none c1 andalso none c2
adamc@312 2733 | CFold => true
adamc@312 2734
adamc@312 2735 | CUnit => true
adamc@312 2736
adamc@312 2737 | CTuple cs => List.all none cs
adamc@312 2738 | CProj (c, _) => none c
adamc@312 2739
adamc@312 2740 | CWild _ => false
adamc@312 2741
adamc@312 2742 fun pos (c, _) =
adamc@312 2743 case c of
adamc@312 2744 CAnnot (c, _) => pos c
adamc@312 2745
adamc@312 2746 | TFun (c1, c2) => none c1 andalso pos c2
adamc@312 2747 | TCFun (_, _, _, c) => pos c
adamc@312 2748 | TRecord c => pos c
adamc@312 2749
adamc@312 2750 | CVar _ => true
adamc@312 2751 | CApp (c1, c2) => pos c1 andalso none c2
adamc@312 2752 | CAbs _ => false
adamc@312 2753 | CDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
adamc@312 2754
adamc@312 2755 | CName _ => true
adamc@312 2756
adamc@312 2757 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso pos c2) xcs
adamc@312 2758 | CConcat (c1, c2) => pos c1 andalso pos c2
adamc@312 2759 | CFold => true
adamc@312 2760
adamc@312 2761 | CUnit => true
adamc@312 2762
adamc@312 2763 | CTuple cs => List.all pos cs
adamc@312 2764 | CProj (c, _) => pos c
adamc@312 2765
adamc@312 2766 | CWild _ => false
adamc@312 2767 in
adamc@312 2768 pos
adamc@312 2769 end
adamc@312 2770
adamc@325 2771 fun wildifyStr env (str, sgn) =
adamc@325 2772 case #1 (hnormSgn env sgn) of
adamc@325 2773 L'.SgnConst sgis =>
adamc@325 2774 (case #1 str of
adamc@325 2775 L.StrConst ds =>
adamc@325 2776 let
adamc@493 2777 fun decompileKind (k, loc) =
adamc@493 2778 case k of
adamc@493 2779 L'.KType => SOME (L.KType, loc)
adamc@493 2780 | L'.KArrow (k1, k2) =>
adamc@493 2781 (case (decompileKind k1, decompileKind k2) of
adamc@493 2782 (SOME k1, SOME k2) => SOME (L.KArrow (k1, k2), loc)
adamc@493 2783 | _ => NONE)
adamc@493 2784 | L'.KName => SOME (L.KName, loc)
adamc@493 2785 | L'.KRecord k =>
adamc@493 2786 (case decompileKind k of
adamc@493 2787 SOME k => SOME (L.KRecord k, loc)
adamc@493 2788 | _ => NONE)
adamc@493 2789 | L'.KUnit => SOME (L.KUnit, loc)
adamc@493 2790 | L'.KTuple ks =>
adamc@493 2791 let
adamc@493 2792 val ks' = List.mapPartial decompileKind ks
adamc@493 2793 in
adamc@493 2794 if length ks' = length ks then
adamc@493 2795 SOME (L.KTuple ks', loc)
adamc@493 2796 else
adamc@493 2797 NONE
adamc@493 2798 end
adamc@493 2799
adamc@493 2800 | L'.KError => NONE
adamc@493 2801 | L'.KUnif (_, _, ref (SOME k)) => decompileKind k
adamc@493 2802 | L'.KUnif _ => NONE
adamc@493 2803
adamc@325 2804 fun decompileCon env (c, loc) =
adamc@325 2805 case c of
adamc@325 2806 L'.CRel i =>
adamc@325 2807 let
adamc@325 2808 val (s, _) = E.lookupCRel env i
adamc@325 2809 in
adamc@325 2810 SOME (L.CVar ([], s), loc)
adamc@325 2811 end
adamc@325 2812 | L'.CNamed i =>
adamc@325 2813 let
adamc@325 2814 val (s, _, _) = E.lookupCNamed env i
adamc@325 2815 in
adamc@325 2816 SOME (L.CVar ([], s), loc)
adamc@325 2817 end
adamc@325 2818 | L'.CModProj (m1, ms, x) =>
adamc@325 2819 let
adamc@325 2820 val (s, _) = E.lookupStrNamed env m1
adamc@325 2821 in
adamc@325 2822 SOME (L.CVar (s :: ms, x), loc)
adamc@325 2823 end
adamc@325 2824 | L'.CName s => SOME (L.CName s, loc)
adamc@325 2825 | L'.CRecord (_, xcs) =>
adamc@325 2826 let
adamc@325 2827 fun fields xcs =
adamc@325 2828 case xcs of
adamc@325 2829 [] => SOME []
adamc@325 2830 | (x, t) :: xcs =>
adamc@325 2831 case (decompileCon env x, decompileCon env t, fields xcs) of
adamc@325 2832 (SOME x, SOME t, SOME xcs) => SOME ((x, t) :: xcs)
adamc@325 2833 | _ => NONE
adamc@325 2834 in
adamc@325 2835 Option.map (fn xcs => (L.CRecord xcs, loc))
adamc@325 2836 (fields xcs)
adamc@325 2837 end
adamc@325 2838 | L'.CConcat (c1, c2) =>
adamc@325 2839 (case (decompileCon env c1, decompileCon env c2) of
adamc@325 2840 (SOME c1, SOME c2) => SOME (L.CConcat (c1, c2), loc)
adamc@325 2841 | _ => NONE)
adamc@325 2842 | L'.CUnit => SOME (L.CUnit, loc)
adamc@325 2843
adamc@325 2844 | _ => NONE
adamc@325 2845
adamc@469 2846 val (neededC, constraints, neededV, _) =
adamc@469 2847 foldl (fn ((sgi, loc), (neededC, constraints, neededV, env')) =>
adamc@325 2848 let
adamc@469 2849 val (needed, constraints, neededV) =
adamc@325 2850 case sgi of
adamc@493 2851 L'.SgiConAbs (x, _, k) => (SM.insert (neededC, x, k), constraints, neededV)
adamc@469 2852 | L'.SgiConstraint cs => (neededC, (env', cs, loc) :: constraints, neededV)
adamc@469 2853
adamc@469 2854 | L'.SgiVal (x, _, t) =>
adamc@469 2855 let
adamc@469 2856 fun default () = (neededC, constraints, neededV)
adamc@469 2857
adamc@469 2858 val t = normClassConstraint env' t
adamc@469 2859 in
adamc@469 2860 case #1 t of
adamc@469 2861 L'.CApp (f, _) =>
adamc@469 2862 if E.isClass env' f then
adamc@469 2863 (neededC, constraints, SS.add (neededV, x))
adamc@469 2864 else
adamc@469 2865 default ()
adamc@469 2866
adamc@469 2867 | _ => default ()
adamc@469 2868 end
adamc@469 2869
adamc@469 2870 | _ => (neededC, constraints, neededV)
adamc@325 2871 in
adamc@469 2872 (needed, constraints, neededV, E.sgiBinds env' (sgi, loc))
adamc@325 2873 end)
adamc@493 2874 (SM.empty, [], SS.empty, env) sgis
adamc@325 2875
adamc@469 2876 val (neededC, neededV) = foldl (fn ((d, _), needed as (neededC, neededV)) =>
adamc@469 2877 case d of
adamc@493 2878 L.DCon (x, _, _) => ((#1 (SM.remove (neededC, x)), neededV)
adamc@469 2879 handle NotFound =>
adamc@469 2880 needed)
adamc@493 2881 | L.DClass (x, _) => ((#1 (SM.remove (neededC, x)), neededV)
adamc@469 2882 handle NotFound => needed)
adamc@469 2883 | L.DVal (x, _, _) => ((neededC, SS.delete (neededV, x))
adamc@469 2884 handle NotFound => needed)
adamc@493 2885 | L.DOpen _ => (SM.empty, SS.empty)
adamc@469 2886 | _ => needed)
adamc@469 2887 (neededC, neededV) ds
adamc@469 2888
adamc@469 2889 val ds' = List.mapPartial (fn (env', (c1, c2), loc) =>
adamc@325 2890 case (decompileCon env' c1, decompileCon env' c2) of
adamc@325 2891 (SOME c1, SOME c2) =>
adamc@325 2892 SOME (L.DConstraint (c1, c2), loc)
adamc@325 2893 | _ => NONE) constraints
adamc@469 2894
adamc@469 2895 val ds' =
adamc@469 2896 case SS.listItems neededV of
adamc@469 2897 [] => ds'
adamc@469 2898 | xs =>
adamc@469 2899 let
adamc@469 2900 val ewild = (L.EWild, #2 str)
adamc@469 2901 val ds'' = map (fn x => (L.DVal (x, NONE, ewild), #2 str)) xs
adamc@469 2902 in
adamc@469 2903 ds'' @ ds'
adamc@469 2904 end
adamc@469 2905
adamc@469 2906 val ds' =
adamc@493 2907 case SM.listItemsi neededC of
adamc@469 2908 [] => ds'
adamc@469 2909 | xs =>
adamc@469 2910 let
adamc@493 2911 val ds'' = map (fn (x, k) =>
adamc@493 2912 let
adamc@493 2913 val k =
adamc@493 2914 case decompileKind k of
adamc@493 2915 NONE => (L.KWild, #2 str)
adamc@493 2916 | SOME k => k
adamc@493 2917 val cwild = (L.CWild k, #2 str)
adamc@493 2918 in
adamc@493 2919 (L.DCon (x, NONE, cwild), #2 str)
adamc@493 2920 end) xs
adamc@469 2921 in
adamc@469 2922 ds'' @ ds'
adamc@469 2923 end
adamc@325 2924 in
adamc@469 2925 (L.StrConst (ds @ ds'), #2 str)
adamc@325 2926 end
adamc@325 2927 | _ => str)
adamc@325 2928 | _ => str
adamc@325 2929
adamc@280 2930 fun elabDecl (dAll as (d, loc), (env, denv, gs : constraint list)) =
adamc@255 2931 let
adamc@255 2932 (*val () = preface ("elabDecl", SourcePrint.p_decl (d, loc))*)
adamc@514 2933 (*val befor = Time.now ()*)
adamc@255 2934
adamc@255 2935 val r =
adamc@255 2936 case d of
adamc@255 2937 L.DCon (x, ko, c) =>
adamc@255 2938 let
adamc@255 2939 val k' = case ko of
adamc@255 2940 NONE => kunif loc
adamc@255 2941 | SOME k => elabKind k
adamc@255 2942
adamc@255 2943 val (c', ck, gs') = elabCon (env, denv) c
adamc@255 2944 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@255 2945 in
adamc@255 2946 checkKind env c' ck k';
adamc@255 2947
adamc@255 2948 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, enD gs' @ gs))
adamc@255 2949 end
adamc@255 2950 | L.DDatatype (x, xs, xcs) =>
adamc@255 2951 let
adamc@312 2952 val positive = List.all (fn (_, to) =>
adamc@312 2953 case to of
adamc@312 2954 NONE => true
adamc@312 2955 | SOME t => positive x t) xcs
adamc@312 2956
adamc@255 2957 val k = (L'.KType, loc)
adamc@255 2958 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@255 2959 val (env, n) = E.pushCNamed env x k' NONE
adamc@255 2960 val t = (L'.CNamed n, loc)
adamc@255 2961 val nxs = length xs - 1
adamc@255 2962 val t = ListUtil.foldli (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@255 2963
adamc@255 2964 val (env', denv') = foldl (fn (x, (env', denv')) =>
adamc@255 2965 (E.pushCRel env' x k,
adamc@255 2966 D.enter denv')) (env, denv) xs
adamc@255 2967
adamc@255 2968 val (xcs, (used, env, gs')) =
adamc@255 2969 ListUtil.foldlMap
adamc@255 2970 (fn ((x, to), (used, env, gs)) =>
adamc@255 2971 let
adamc@255 2972 val (to, t, gs') = case to of
adamc@255 2973 NONE => (NONE, t, gs)
adamc@255 2974 | SOME t' =>
adamc@255 2975 let
adamc@255 2976 val (t', tk, gs') = elabCon (env', denv') t'
adamc@255 2977 in
adamc@255 2978 checkKind env' t' tk k;
adamc@255 2979 (SOME t', (L'.TFun (t', t), loc), enD gs' @ gs)
adamc@255 2980 end
adamc@255 2981 val t = foldr (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
adamc@255 2982
adamc@255 2983 val (env, n') = E.pushENamed env x t
adamc@255 2984 in
adamc@255 2985 if SS.member (used, x) then
adamc@255 2986 strError env (DuplicateConstructor (x, loc))
adamc@255 2987 else
adamc@255 2988 ();
adamc@255 2989 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@255 2990 end)
adamc@255 2991 (SS.empty, env, []) xcs
adamc@255 2992
adamc@255 2993 val env = E.pushDatatype env n xs xcs
adamc@312 2994 val d' = (L'.DDatatype (x, n, xs, xcs), loc)
adamc@255 2995 in
adamc@312 2996 if positive then
adamc@312 2997 ()
adamc@312 2998 else
adamc@312 2999 declError env (Nonpositive d');
adamc@312 3000
adamc@312 3001 ([d'], (env, denv, gs' @ gs))
adamc@255 3002 end
adamc@255 3003
adamc@255 3004 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
adamc@255 3005
adamc@255 3006 | L.DDatatypeImp (x, m1 :: ms, s) =>
adamc@255 3007 (case E.lookupStr env m1 of
adamc@255 3008 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@255 3009 ([], (env, denv, gs)))
adamc@255 3010 | SOME (n, sgn) =>
adamc@255 3011 let
adamc@255 3012 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@255 3013 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@255 3014 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@255 3015 (strerror, sgnerror))
adamc@255 3016 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@255 3017 ((L'.StrVar n, loc), sgn) ms
adamc@255 3018 in
adamc@255 3019 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@255 3020 ((L'.CModProj (n, ms, s), _), gs') =>
adamc@255 3021 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@255 3022 NONE => (conError env (UnboundDatatype (loc, s));
adamc@255 3023 ([], (env, denv, gs)))
adamc@255 3024 | SOME (xs, xncs) =>
adamc@255 3025 let
adamc@255 3026 val k = (L'.KType, loc)
adamc@255 3027 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@255 3028 val t = (L'.CModProj (n, ms, s), loc)
adamc@255 3029 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@255 3030 val env = E.pushDatatype env n' xs xncs
adamc@255 3031
adamc@255 3032 val t = (L'.CNamed n', loc)
adamc@255 3033 val env = foldl (fn ((x, n, to), env) =>
adamc@255 3034 let
adamc@255 3035 val t = case to of
adamc@255 3036 NONE => t
adamc@255 3037 | SOME t' => (L'.TFun (t', t), loc)
adamc@255 3038
adamc@255 3039 val t = foldr (fn (x, t) =>
adamc@255 3040 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@255 3041 t xs
adamc@255 3042 in
adamc@255 3043 E.pushENamedAs env x n t
adamc@255 3044 end) env xncs
adamc@255 3045 in
adamc@255 3046 ([(L'.DDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, enD gs' @ gs))
adamc@255 3047 end)
adamc@255 3048 | _ => (strError env (NotDatatype loc);
adamc@255 3049 ([], (env, denv, [])))
adamc@255 3050 end)
adamc@255 3051
adamc@255 3052 | L.DVal (x, co, e) =>
adamc@255 3053 let
adamc@255 3054 val (c', _, gs1) = case co of
adamc@255 3055 NONE => (cunif (loc, ktype), ktype, [])
adamc@255 3056 | SOME c => elabCon (env, denv) c
adamc@255 3057
adamc@255 3058 val (e', et, gs2) = elabExp (env, denv) e
adamc@255 3059 val gs3 = checkCon (env, denv) e' et c'
adamc@467 3060 val c = normClassConstraint env c'
adamc@255 3061 val (env', n) = E.pushENamed env x c'
adamc@334 3062 val c' = makeInstantiable c'
adamc@255 3063 in
adamc@255 3064 (*prefaces "DVal" [("x", Print.PD.string x),
adamc@362 3065 ("c'", p_con env c')];*)
adamc@467 3066 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, enD gs1 @ gs2 @ enD gs3 @ gs))
adamc@255 3067 end
adamc@255 3068 | L.DValRec vis =>
adamc@255 3069 let
adamc@255 3070 fun allowable (e, _) =
adamc@255 3071 case e of
adamc@255 3072 L.EAbs _ => true
adamc@255 3073 | L.ECAbs (_, _, _, e) => allowable e
adamc@255 3074 | L.EDisjoint (_, _, e) => allowable e
adamc@255 3075 | _ => false
adamc@255 3076
adamc@255 3077 val (vis, gs) = ListUtil.foldlMap
adamc@255 3078 (fn ((x, co, e), gs) =>
adamc@255 3079 let
adamc@255 3080 val (c', _, gs1) = case co of
adamc@255 3081 NONE => (cunif (loc, ktype), ktype, [])
adamc@255 3082 | SOME c => elabCon (env, denv) c
adamc@255 3083 in
adamc@255 3084 ((x, c', e), enD gs1 @ gs)
adamc@280 3085 end) gs vis
adamc@255 3086
adamc@255 3087 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@255 3088 let
adamc@255 3089 val (env, n) = E.pushENamed env x c'
adamc@255 3090 in
adamc@255 3091 ((x, n, c', e), env)
adamc@255 3092 end) env vis
adamc@255 3093
adamc@255 3094 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@255 3095 let
adamc@255 3096 val (e', et, gs1) = elabExp (env, denv) e
adamc@255 3097
adamc@255 3098 val gs2 = checkCon (env, denv) e' et c'
adamc@334 3099
adamc@334 3100 val c' = makeInstantiable c'
adamc@255 3101 in
adamc@255 3102 if allowable e then
adamc@255 3103 ()
adamc@255 3104 else
adamc@255 3105 expError env (IllegalRec (x, e'));
adamc@255 3106 ((x, n, c', e'), gs1 @ enD gs2 @ gs)
adamc@255 3107 end) gs vis
adamc@255 3108 in
adamc@255 3109 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@255 3110 end
adamc@255 3111
adamc@255 3112 | L.DSgn (x, sgn) =>
adamc@255 3113 let
adamc@255 3114 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@255 3115 val (env', n) = E.pushSgnNamed env x sgn'
adamc@255 3116 in
adamc@255 3117 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3118 end
adamc@255 3119
adamc@255 3120 | L.DStr (x, sgno, str) =>
adamc@255 3121 let
adamc@255 3122 val () = if x = "Basis" then
adamc@255 3123 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@255 3124 else
adamc@255 3125 ()
adamc@255 3126
adamc@255 3127 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@255 3128
adamc@255 3129 val (str', sgn', gs') =
adamc@255 3130 case formal of
adamc@255 3131 NONE =>
adamc@255 3132 let
adamc@255 3133 val (str', actual, gs') = elabStr (env, denv) str
adamc@255 3134 in
adamc@255 3135 (str', selfifyAt env {str = str', sgn = actual}, gs')
adamc@255 3136 end
adamc@255 3137 | SOME (formal, gs1) =>
adamc@255 3138 let
adamc@325 3139 val str = wildifyStr env (str, formal)
adamc@255 3140 val (str', actual, gs2) = elabStr (env, denv) str
adamc@255 3141 in
adamc@255 3142 subSgn (env, denv) (selfifyAt env {str = str', sgn = actual}) formal;
adamc@255 3143 (str', formal, enD gs1 @ gs2)
adamc@255 3144 end
adamc@255 3145
adamc@255 3146 val (env', n) = E.pushStrNamed env x sgn'
adamc@255 3147 in
adamc@255 3148 case #1 (hnormSgn env sgn') of
adamc@255 3149 L'.SgnFun _ =>
adamc@255 3150 (case #1 str' of
adamc@255 3151 L'.StrFun _ => ()
adamc@255 3152 | _ => strError env (FunctorRebind loc))
adamc@255 3153 | _ => ();
adamc@255 3154
adamc@255 3155 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
adamc@255 3156 end
adamc@255 3157
adamc@255 3158 | L.DFfiStr (x, sgn) =>
adamc@255 3159 let
adamc@255 3160 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@255 3161
adamc@255 3162 val (env', n) = E.pushStrNamed env x sgn'
adamc@255 3163 in
adamc@255 3164 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3165 end
adamc@255 3166
adamc@255 3167 | L.DOpen (m, ms) =>
adamc@255 3168 (case E.lookupStr env m of
adamc@255 3169 NONE => (strError env (UnboundStr (loc, m));
adamc@255 3170 ([], (env, denv, gs)))
adamc@255 3171 | SOME (n, sgn) =>
adamc@255 3172 let
adamc@255 3173 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@255 3174 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@255 3175 NONE => (strError env (UnboundStr (loc, m));
adamc@255 3176 (strerror, sgnerror))
adamc@255 3177 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@255 3178 ((L'.StrVar n, loc), sgn) ms
adamc@255 3179
adamc@255 3180 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
adamc@255 3181 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
adamc@255 3182 in
adamc@255 3183 (ds, (env', denv', gs))
adamc@255 3184 end)
adamc@255 3185
adamc@255 3186 | L.DConstraint (c1, c2) =>
adamc@255 3187 let
adamc@255 3188 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@255 3189 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@255 3190 val gs3 = D.prove env denv (c1', c2', loc)
adamc@255 3191
adamc@255 3192 val (denv', gs4) = D.assert env denv (c1', c2')
adamc@255 3193 in
adamc@255 3194 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@255 3195 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@255 3196
adamc@255 3197 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', enD gs1 @ enD gs2 @ enD gs3 @ enD gs4 @ gs))
adamc@255 3198 end
adamc@255 3199
adamc@255 3200 | L.DOpenConstraints (m, ms) =>
adamc@255 3201 let
adamc@255 3202 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@255 3203 in
adamc@255 3204 ([], (env, denv, gs))
adamc@255 3205 end
adamc@255 3206
adamc@255 3207 | L.DExport str =>
adamc@255 3208 let
adamc@255 3209 val (str', sgn, gs') = elabStr (env, denv) str
adamc@255 3210
adamc@255 3211 val sgn =
adamc@255 3212 case #1 (hnormSgn env sgn) of
adamc@255 3213 L'.SgnConst sgis =>
adamc@255 3214 let
adamc@255 3215 fun doOne (all as (sgi, _), env) =
adamc@255 3216 (case sgi of
adamc@255 3217 L'.SgiVal (x, n, t) =>
adamc@325 3218 let
adamc@325 3219 fun doPage (makeRes, ran) =
adamc@325 3220 case hnormCon (env, denv) ran of
adamc@325 3221 ((L'.CApp (tf, arg), _), []) =>
adamc@325 3222 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg) of
adamc@325 3223 (((L'.CModProj (basis, [], "transaction"), _), []),
adamc@325 3224 ((L'.CApp (tf, arg3), _), [])) =>
adamc@325 3225 (case (basis = !basis_r,
adamc@325 3226 hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
adamc@325 3227 (true,
adamc@325 3228 ((L'.CApp (tf, arg2), _), []),
adamc@325 3229 (((L'.CRecord (_, []), _), []))) =>
adamc@325 3230 (case (hnormCon (env, denv) tf) of
adamc@325 3231 ((L'.CApp (tf, arg1), _), []) =>
adamc@325 3232 (case (hnormCon (env, denv) tf,
adamc@325 3233 hnormCon (env, denv) arg1,
adamc@325 3234 hnormCon (env, denv) arg2) of
adamc@325 3235 ((tf, []), (arg1, []),
adamc@325 3236 ((L'.CRecord (_, []), _), [])) =>
adamc@325 3237 let
adamc@325 3238 val t = (L'.CApp (tf, arg1), loc)
adamc@325 3239 val t = (L'.CApp (t, arg2), loc)
adamc@325 3240 val t = (L'.CApp (t, arg3), loc)
adamc@325 3241 val t = (L'.CApp (
adamc@325 3242 (L'.CModProj
adamc@325 3243 (basis, [], "transaction"), loc),
adamc@325 3244 t), loc)
adamc@325 3245 in
adamc@325 3246 (L'.SgiVal (x, n, makeRes t), loc)
adamc@325 3247 end
adamc@325 3248 | _ => all)
adamc@325 3249 | _ => all)
adamc@325 3250 | _ => all)
adamc@325 3251 | _ => all)
adamc@325 3252 | _ => all
adamc@325 3253 in
adamc@325 3254 case hnormCon (env, denv) t of
adamc@325 3255 ((L'.TFun (dom, ran), _), []) =>
adamc@325 3256 (case hnormCon (env, denv) dom of
adamc@325 3257 ((L'.TRecord domR, _), []) =>
adamc@325 3258 doPage (fn t => (L'.TFun ((L'.TRecord domR,
adamc@325 3259 loc),
adamc@325 3260 t), loc), ran)
adamc@325 3261 | _ => all)
adamc@325 3262 | _ => doPage (fn t => t, t)
adamc@325 3263 end
adamc@255 3264 | _ => all,
adamc@255 3265 E.sgiBinds env all)
adamc@255 3266 in
adamc@255 3267 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@255 3268 end
adamc@255 3269 | _ => sgn
adamc@255 3270 in
adamc@255 3271 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs' @ gs))
adamc@255 3272 end
adamc@255 3273
adamc@255 3274 | L.DTable (x, c) =>
adamc@255 3275 let
adamc@255 3276 val (c', k, gs') = elabCon (env, denv) c
adamc@255 3277 val (env, n) = E.pushENamed env x (L'.CApp (tableOf (), c'), loc)
adamc@255 3278 in
adamc@255 3279 checkKind env c' k (L'.KRecord (L'.KType, loc), loc);
adamc@255 3280 ([(L'.DTable (!basis_r, x, n, c'), loc)], (env, denv, enD gs' @ gs))
adamc@255 3281 end
adamc@338 3282 | L.DSequence x =>
adamc@338 3283 let
adamc@338 3284 val (env, n) = E.pushENamed env x (sequenceOf ())
adamc@338 3285 in
adamc@338 3286 ([(L'.DSequence (!basis_r, x, n), loc)], (env, denv, gs))
adamc@338 3287 end
adamc@255 3288
adamc@255 3289 | L.DClass (x, c) =>
adamc@255 3290 let
adamc@255 3291 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@280 3292 val (c', ck, gs') = elabCon (env, denv) c
adamc@255 3293 val (env, n) = E.pushCNamed env x k (SOME c')
adamc@255 3294 val env = E.pushClass env n
adamc@255 3295 in
adamc@255 3296 checkKind env c' ck k;
adamc@280 3297 ([(L'.DClass (x, n, c'), loc)], (env, denv, enD gs' @ gs))
adamc@255 3298 end
adamc@271 3299
adamc@280 3300 | L.DDatabase s => ([(L'.DDatabase s, loc)], (env, denv, gs))
adamc@280 3301
adamc@459 3302 | L.DCookie (x, c) =>
adamc@459 3303 let
adamc@459 3304 val (c', k, gs') = elabCon (env, denv) c
adamc@459 3305 val (env, n) = E.pushENamed env x (L'.CApp (cookieOf (), c'), loc)
adamc@459 3306 in
adamc@459 3307 checkKind env c' k (L'.KType, loc);
adamc@459 3308 ([(L'.DCookie (!basis_r, x, n, c'), loc)], (env, denv, enD gs' @ gs))
adamc@459 3309 end
adamc@459 3310
adamc@280 3311 (*val tcs = List.filter (fn TypeClass _ => true | _ => false) (#3 (#2 r))*)
adamc@255 3312 in
adamc@280 3313 (*prefaces "elabDecl" [("e", SourcePrint.p_decl dAll),
adamc@514 3314 ("t", PD.string (LargeReal.toString (Time.toReal
adamc@514 3315 (Time.- (Time.now (), befor)))))];*)
adamc@280 3316
adamc@255 3317 r
adamc@255 3318 end
adamc@211 3319
adamc@83 3320 and elabStr (env, denv) (str, loc) =
adamc@31 3321 case str of
adamc@31 3322 L.StrConst ds =>
adamc@31 3323 let
adamc@88 3324 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 3325 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 3326
adamc@63 3327 val (sgis, _, _, _, _) =
adamc@64 3328 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 3329 case sgi of
adamc@64 3330 L'.SgiConAbs (x, n, k) =>
adamc@64 3331 let
adamc@64 3332 val (cons, x) =
adamc@64 3333 if SS.member (cons, x) then
adamc@64 3334 (cons, "?" ^ x)
adamc@64 3335 else
adamc@64 3336 (SS.add (cons, x), x)
adamc@64 3337 in
adamc@64 3338 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 3339 end
adamc@64 3340 | L'.SgiCon (x, n, k, c) =>
adamc@64 3341 let
adamc@64 3342 val (cons, x) =
adamc@64 3343 if SS.member (cons, x) then
adamc@64 3344 (cons, "?" ^ x)
adamc@64 3345 else
adamc@64 3346 (SS.add (cons, x), x)
adamc@64 3347 in
adamc@64 3348 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 3349 end
adamc@191 3350 | L'.SgiDatatype (x, n, xs, xncs) =>
adamc@156 3351 let
adamc@156 3352 val (cons, x) =
adamc@156 3353 if SS.member (cons, x) then
adamc@156 3354 (cons, "?" ^ x)
adamc@156 3355 else
adamc@156 3356 (SS.add (cons, x), x)
adamc@156 3357
adamc@156 3358 val (xncs, vals) =
adamc@156 3359 ListUtil.foldlMap
adamc@156 3360 (fn ((x, n, t), vals) =>
adamc@156 3361 if SS.member (vals, x) then
adamc@156 3362 (("?" ^ x, n, t), vals)
adamc@156 3363 else
adamc@156 3364 ((x, n, t), SS.add (vals, x)))
adamc@156 3365 vals xncs
adamc@156 3366 in
adamc@191 3367 ((L'.SgiDatatype (x, n, xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 3368 end
adamc@191 3369 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@156 3370 let
adamc@156 3371 val (cons, x) =
adamc@156 3372 if SS.member (cons, x) then
adamc@156 3373 (cons, "?" ^ x)
adamc@156 3374 else
adamc@156 3375 (SS.add (cons, x), x)
adamc@156 3376 in
adamc@191 3377 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 3378 end
adamc@64 3379 | L'.SgiVal (x, n, c) =>
adamc@64 3380 let
adamc@64 3381 val (vals, x) =
adamc@64 3382 if SS.member (vals, x) then
adamc@64 3383 (vals, "?" ^ x)
adamc@64 3384 else
adamc@64 3385 (SS.add (vals, x), x)
adamc@64 3386 in
adamc@64 3387 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 3388 end
adamc@64 3389 | L'.SgiSgn (x, n, sgn) =>
adamc@64 3390 let
adamc@64 3391 val (sgns, x) =
adamc@64 3392 if SS.member (sgns, x) then
adamc@64 3393 (sgns, "?" ^ x)
adamc@64 3394 else
adamc@64 3395 (SS.add (sgns, x), x)
adamc@64 3396 in
adamc@64 3397 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 3398 end
adamc@64 3399
adamc@64 3400 | L'.SgiStr (x, n, sgn) =>
adamc@64 3401 let
adamc@64 3402 val (strs, x) =
adamc@64 3403 if SS.member (strs, x) then
adamc@64 3404 (strs, "?" ^ x)
adamc@64 3405 else
adamc@64 3406 (SS.add (strs, x), x)
adamc@64 3407 in
adamc@64 3408 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 3409 end
adamc@203 3410 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs)
adamc@211 3411 | L'.SgiClassAbs (x, n) =>
adamc@211 3412 let
adamc@211 3413 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@211 3414
adamc@211 3415 val (cons, x) =
adamc@211 3416 if SS.member (cons, x) then
adamc@211 3417 (cons, "?" ^ x)
adamc@211 3418 else
adamc@211 3419 (SS.add (cons, x), x)
adamc@211 3420 in
adamc@211 3421 ((L'.SgiClassAbs (x, n), loc) :: sgis, cons, vals, sgns, strs)
adamc@211 3422 end
adamc@211 3423 | L'.SgiClass (x, n, c) =>
adamc@211 3424 let
adamc@211 3425 val k = (L'.KArrow ((L'.KType, loc), (L'.KType, loc)), loc)
adamc@211 3426
adamc@211 3427 val (cons, x) =
adamc@211 3428 if SS.member (cons, x) then
adamc@211 3429 (cons, "?" ^ x)
adamc@211 3430 else
adamc@211 3431 (SS.add (cons, x), x)
adamc@211 3432 in
adamc@211 3433 ((L'.SgiClass (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@203 3434 end)
adamc@64 3435
adamc@63 3436 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 3437 in
adamc@83 3438 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 3439 end
adamc@31 3440 | L.StrVar x =>
adamc@31 3441 (case E.lookupStr env x of
adamc@31 3442 NONE =>
adamc@31 3443 (strError env (UnboundStr (loc, x));
adamc@83 3444 (strerror, sgnerror, []))
adamc@83 3445 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 3446 | L.StrProj (str, x) =>
adamc@34 3447 let
adamc@83 3448 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 3449 in
adamc@34 3450 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 3451 NONE => (strError env (UnboundStr (loc, x));
adamc@83 3452 (strerror, sgnerror, []))
adamc@83 3453 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 3454 end
adamc@41 3455 | L.StrFun (m, dom, ranO, str) =>
adamc@41 3456 let
adamc@83 3457 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 3458 val (env', n) = E.pushStrNamed env m dom'
adamc@83 3459 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 3460
adamc@83 3461 val (formal, gs3) =
adamc@41 3462 case ranO of
adamc@83 3463 NONE => (actual, [])
adamc@41 3464 | SOME ran =>
adamc@41 3465 let
adamc@83 3466 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 3467 in
adamc@86 3468 subSgn (env', denv) actual ran';
adamc@83 3469 (ran', gs)
adamc@41 3470 end
adamc@41 3471 in
adamc@41 3472 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 3473 (L'.SgnFun (m, n, dom', formal), loc),
adamc@228 3474 enD gs1 @ gs2 @ enD gs3)
adamc@41 3475 end
adamc@44 3476 | L.StrApp (str1, str2) =>
adamc@44 3477 let
adamc@83 3478 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@325 3479 val str2 =
adamc@325 3480 case sgn1 of
adamc@325 3481 (L'.SgnFun (_, _, dom, _), _) =>
adamc@325 3482 wildifyStr env (str2, dom)
adamc@325 3483 | _ => str2
adamc@83 3484 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 3485 in
adamc@44 3486 case #1 (hnormSgn env sgn1) of
adamc@83 3487 L'.SgnError => (strerror, sgnerror, [])
adamc@44 3488 | L'.SgnFun (m, n, dom, ran) =>
adamc@86 3489 (subSgn (env, denv) sgn2 dom;
adamc@44 3490 case #1 (hnormSgn env ran) of
adamc@83 3491 L'.SgnError => (strerror, sgnerror, [])
adamc@44 3492 | L'.SgnConst sgis =>
adamc@44 3493 ((L'.StrApp (str1', str2'), loc),
adamc@83 3494 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 3495 gs1 @ gs2)
adamc@44 3496 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 3497 | _ => (strError env (NotFunctor sgn1);
adamc@83 3498 (strerror, sgnerror, []))
adamc@44 3499 end
adamc@31 3500
adamc@325 3501 fun elabFile basis topStr topSgn env file =
adamc@56 3502 let
adamc@83 3503 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 3504 val () = case gs of
adamc@83 3505 [] => ()
adamc@207 3506 | _ => (app (fn (_, env, _, c1, c2) =>
adamc@207 3507 prefaces "Unresolved"
adamc@207 3508 [("c1", p_con env c1),
adamc@207 3509 ("c2", p_con env c2)]) gs;
adamc@207 3510 raise Fail "Unresolved disjointness constraints in Basis")
adamc@83 3511
adamc@56 3512 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@210 3513 val () = basis_r := basis_n
adamc@56 3514
adamc@88 3515 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
adamc@56 3516
adamc@56 3517 fun discoverC r x =
adamc@56 3518 case E.lookupC env' x of
adamc@56 3519 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 3520 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 3521 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 3522
adamc@56 3523 val () = discoverC int "int"
adamc@56 3524 val () = discoverC float "float"
adamc@56 3525 val () = discoverC string "string"
adamc@203 3526 val () = discoverC table "sql_table"
adamc@56 3527
adamc@325 3528 val (topSgn, gs) = elabSgn (env', D.empty) (L.SgnConst topSgn, ErrorMsg.dummySpan)
adamc@325 3529 val () = case gs of
adamc@325 3530 [] => ()
adamc@325 3531 | _ => raise Fail "Unresolved disjointness constraints in top.urs"
adamc@325 3532 val (topStr, topSgn', gs) = elabStr (env', D.empty) (L.StrConst topStr, ErrorMsg.dummySpan)
adamc@325 3533 val () = case gs of
adamc@325 3534 [] => ()
adamc@329 3535 | _ => app (fn Disjoint (loc, env, denv, c1, c2) =>
adamc@329 3536 (case D.prove env denv (c1, c2, loc) of
adamc@329 3537 [] => ()
adamc@329 3538 | _ =>
adamc@329 3539 (prefaces "Unresolved constraint in top.ur"
adamc@329 3540 [("loc", PD.string (ErrorMsg.spanToString loc)),
adamc@329 3541 ("c1", p_con env c1),
adamc@329 3542 ("c2", p_con env c2)];
adamc@478 3543 raise Fail "Unresolved constraint in top.ur"))
adamc@329 3544 | TypeClass _ => raise Fail "Unresolved type class constraint in top.ur") gs
adamc@325 3545 val () = subSgn (env', D.empty) topSgn' topSgn
adamc@325 3546
adamc@325 3547 val (env', top_n) = E.pushStrNamed env' "Top" topSgn
adamc@325 3548
adamc@325 3549 val (ds', (env', _)) = dopen (env', D.empty) {str = top_n, strs = [], sgn = topSgn}
adamc@325 3550
adamc@83 3551 fun elabDecl' (d, (env, gs)) =
adamc@76 3552 let
adamc@76 3553 val () = resetKunif ()
adamc@76 3554 val () = resetCunif ()
adamc@88 3555 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 3556 in
adamc@76 3557 if ErrorMsg.anyErrors () then
adamc@76 3558 ()
adamc@76 3559 else (
adamc@76 3560 if List.exists kunifsInDecl ds then
adamc@91 3561 declError env (KunifsRemain ds)
adamc@76 3562 else
adamc@76 3563 ();
adamc@76 3564
adamc@76 3565 case ListUtil.search cunifsInDecl ds of
adamc@76 3566 NONE => ()
adamc@76 3567 | SOME loc =>
adamc@91 3568 declError env (CunifsRemain ds)
adamc@76 3569 );
adamc@76 3570
adamc@83 3571 (ds, (env, gs))
adamc@76 3572 end
adamc@76 3573
adamc@83 3574 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 3575 in
adamc@84 3576 if ErrorMsg.anyErrors () then
adamc@84 3577 ()
adamc@84 3578 else
adamc@228 3579 app (fn Disjoint (loc, env, denv, c1, c2) =>
adamc@228 3580 (case D.prove env denv (c1, c2, loc) of
adamc@228 3581 [] => ()
adamc@228 3582 | _ =>
adamc@228 3583 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adamc@228 3584 eprefaces' [("Con 1", p_con env c1),
adamc@228 3585 ("Con 2", p_con env c2),
adamc@228 3586 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
adamc@228 3587 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))]))
adamc@228 3588 | TypeClass (env, c, r, loc) =>
adamc@349 3589 let
adamc@467 3590 val c = normClassKey env c
adamc@349 3591 in
adamc@349 3592 case E.resolveClass env c of
adamc@349 3593 SOME e => r := SOME e
adamc@349 3594 | NONE => expError env (Unresolvable (loc, c))
adamc@349 3595 end) gs;
adamc@83 3596
adamc@325 3597 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan)
adamc@325 3598 :: ds
adamc@325 3599 @ (L'.DStr ("Top", top_n, topSgn, topStr), ErrorMsg.dummySpan)
adamc@325 3600 :: ds' @ file
adamc@56 3601 end
adamc@2 3602
adamc@2 3603 end