annotate src/elaborate.sml @ 459:f542bc3133dc

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