annotate src/elaborate.sml @ 409:81d9f42bb641

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