annotate src/elaborate.sml @ 1628:3621f486ce72

Don't crash on invalid URL head terms during Tag
author Adam Chlipala <adam@chlipala.net>
date Sat, 03 Dec 2011 17:25:51 -0500
parents 07eed8386f07
children 6c00d8af6239
rev   line source
adam@1529 1 (* Copyright (c) 2008-2011, Adam Chlipala
adamc@445 2 * All rights reserved.
adamc@445 3 *
adamc@445 4 * Redistribution and use in source and binary forms, with or without
adamc@445 5 * modification, are permitted provided that the following conditions are met:
adamc@445 6 *
adamc@445 7 * - Redistributions of source code must retain the above copyright notice,
adamc@445 8 * this list of conditions and the following disclaimer.
adamc@445 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@445 10 * this list of conditions and the following disclaimer in the documentation
adamc@445 11 * and/or other materials provided with the distribution.
adamc@445 12 * - The names of contributors may not be used to endorse or promote products
adamc@445 13 * derived from this software without specific prior written permission.
adamc@445 14 *
adamc@445 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@445 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@445 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@445 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@445 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@445 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@445 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@445 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@445 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@445 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@445 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@445 26 *)
adamc@403 27
adamc@403 28 structure Elaborate :> ELABORATE = struct
adamc@403 29
adamc@403 30 structure P = Prim
adamc@403 31 structure L = Source
adamc@403 32 structure L' = Elab
adamc@403 33 structure E = ElabEnv
adamc@403 34 structure U = ElabUtil
adamc@403 35 structure D = Disjoint
adamc@403 36
adamc@403 37 open Print
adamc@403 38 open ElabPrint
adamc@403 39 open ElabErr
adamc@403 40
adam@1531 41 val dumpTypes = ref false
adam@1531 42
adamc@819 43 structure IS = IntBinarySet
adamc@403 44 structure IM = IntBinaryMap
adamc@403 45
adamc@403 46 structure SK = struct
adamc@403 47 type ord_key = string
adamc@403 48 val compare = String.compare
adamc@403 49 end
adamc@403 50
adamc@403 51 structure SS = BinarySetFn(SK)
adamc@403 52 structure SM = BinaryMapFn(SK)
adamc@403 53
adamc@403 54 val basis_r = ref 0
adamc@633 55 val top_r = ref 0
adamc@403 56
adamc@403 57 fun elabExplicitness e =
adamc@403 58 case e of
adamc@403 59 L.Explicit => L'.Explicit
adamc@403 60 | L.Implicit => L'.Implicit
adamc@403 61
adamc@403 62 fun occursKind r =
adamc@403 63 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
adamc@403 64 | _ => false)
adamc@403 65
adamc@403 66 exception KUnify' of kunify_error
adamc@403 67
adamc@623 68 fun unifyKinds' env (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@403 69 let
adamc@403 70 fun err f = raise KUnify' (f (k1All, k2All))
adamc@403 71 in
adamc@403 72 case (k1, k2) of
adamc@403 73 (L'.KType, L'.KType) => ()
adamc@403 74 | (L'.KUnit, L'.KUnit) => ()
adamc@403 75
adamc@403 76 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@623 77 (unifyKinds' env d1 d2;
adamc@623 78 unifyKinds' env r1 r2)
adamc@403 79 | (L'.KName, L'.KName) => ()
adamc@623 80 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' env k1 k2
adamc@403 81 | (L'.KTuple ks1, L'.KTuple ks2) =>
adamc@623 82 ((ListPair.appEq (fn (k1, k2) => unifyKinds' env k1 k2) (ks1, ks2))
adamc@403 83 handle ListPair.UnequalLengths => err KIncompatible)
adamc@403 84
adamc@623 85 | (L'.KRel n1, L'.KRel n2) =>
adamc@623 86 if n1 = n2 then
adamc@623 87 ()
adamc@623 88 else
adamc@623 89 err KIncompatible
adamc@623 90 | (L'.KFun (x, k1), L'.KFun (_, k2)) =>
adamc@623 91 unifyKinds' (E.pushKRel env x) k1 k2
adamc@623 92
adamc@403 93 | (L'.KError, _) => ()
adamc@403 94 | (_, L'.KError) => ()
adamc@403 95
adamc@623 96 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' env k1All k2All
adamc@623 97 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' env k1All k2All
adamc@403 98
adam@1302 99 | (L'.KTupleUnif (_, _, ref (SOME k)), _) => unifyKinds' env k k2All
adam@1302 100 | (_, L'.KTupleUnif (_, _, ref (SOME k))) => unifyKinds' env k1All k
adam@1302 101
adamc@403 102 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
adamc@403 103 if r1 = r2 then
adamc@403 104 ()
adamc@403 105 else
adamc@403 106 r1 := SOME k2All
adamc@403 107
adamc@403 108 | (L'.KUnif (_, _, r), _) =>
adamc@403 109 if occursKind r k2All then
adamc@403 110 err KOccursCheckFailed
adamc@403 111 else
adamc@403 112 r := SOME k2All
adamc@403 113 | (_, L'.KUnif (_, _, r)) =>
adamc@403 114 if occursKind r k1All then
adamc@403 115 err KOccursCheckFailed
adamc@403 116 else
adamc@403 117 r := SOME k1All
adamc@403 118
adam@1302 119 | (L'.KTupleUnif (_, nks, r), L'.KTuple ks) =>
adam@1302 120 ((app (fn (n, k) => unifyKinds' env k (List.nth (ks, n-1))) nks;
adam@1302 121 r := SOME k2All)
adam@1302 122 handle Subscript => err KIncompatible)
adam@1302 123 | (L'.KTuple ks, L'.KTupleUnif (_, nks, r)) =>
adam@1302 124 ((app (fn (n, k) => unifyKinds' env (List.nth (ks, n-1)) k) nks;
adam@1302 125 r := SOME k1All)
adam@1302 126 handle Subscript => err KIncompatible)
adam@1302 127 | (L'.KTupleUnif (loc, nks1, r1), L'.KTupleUnif (_, nks2, r2)) =>
adam@1302 128 let
adam@1302 129 val nks = foldl (fn (p as (n, k1), nks) =>
adam@1302 130 case ListUtil.search (fn (n', k2) =>
adam@1302 131 if n' = n then
adam@1302 132 SOME k2
adam@1302 133 else
adam@1302 134 NONE) nks2 of
adam@1302 135 NONE => p :: nks
adam@1302 136 | SOME k2 => (unifyKinds' env k1 k2;
adam@1302 137 nks)) nks2 nks1
adam@1302 138
adam@1302 139 val k = (L'.KTupleUnif (loc, nks, ref NONE), loc)
adam@1302 140 in
adam@1302 141 r1 := SOME k;
adam@1302 142 r2 := SOME k
adam@1302 143 end
adam@1302 144
adamc@403 145 | _ => err KIncompatible
adamc@403 146 end
adamc@403 147
adamc@403 148 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@403 149
adamc@623 150 fun unifyKinds env k1 k2 =
adamc@623 151 unifyKinds' env k1 k2
adamc@403 152 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@403 153
adamc@403 154 fun checkKind env c k1 k2 =
adamc@623 155 unifyKinds env k1 k2
adamc@403 156 handle KUnify (k1, k2, err) =>
adamc@403 157 conError env (WrongKind (c, k1, k2, err))
adamc@403 158
adamc@403 159 val dummy = ErrorMsg.dummySpan
adamc@403 160
adamc@403 161 val ktype = (L'.KType, dummy)
adamc@403 162 val kname = (L'.KName, dummy)
adamc@403 163 val ktype_record = (L'.KRecord ktype, dummy)
adamc@403 164
adamc@403 165 val cerror = (L'.CError, dummy)
adamc@403 166 val kerror = (L'.KError, dummy)
adamc@403 167 val eerror = (L'.EError, dummy)
adamc@403 168 val sgnerror = (L'.SgnError, dummy)
adamc@403 169 val strerror = (L'.StrError, dummy)
adamc@403 170
adamc@403 171 val int = ref cerror
adamc@403 172 val float = ref cerror
adamc@403 173 val string = ref cerror
adamc@821 174 val char = ref cerror
adamc@403 175 val table = ref cerror
adamc@403 176
adamc@403 177 local
adamc@403 178 val count = ref 0
adamc@403 179 in
adamc@403 180
adamc@403 181 fun resetKunif () = count := 0
adamc@403 182
adamc@403 183 fun kunif loc =
adamc@403 184 let
adamc@403 185 val n = !count
adamc@403 186 val s = if n <= 26 then
adamc@403 187 str (chr (ord #"A" + n))
adamc@403 188 else
adamc@403 189 "U" ^ Int.toString (n - 26)
adamc@403 190 in
adamc@403 191 count := n + 1;
adamc@838 192 (L'.KUnif (loc, s, ref NONE), loc)
adamc@403 193 end
adamc@403 194
adamc@403 195 end
adamc@403 196
adamc@403 197 local
adamc@403 198 val count = ref 0
adamc@403 199 in
adamc@403 200
adamc@403 201 fun resetCunif () = count := 0
adamc@403 202
adamc@403 203 fun cunif (loc, k) =
adamc@403 204 let
adamc@403 205 val n = !count
adamc@712 206 val s = if n < 26 then
adamc@403 207 str (chr (ord #"A" + n))
adamc@403 208 else
adamc@403 209 "U" ^ Int.toString (n - 26)
adamc@403 210 in
adamc@403 211 count := n + 1;
adam@1303 212 (L'.CUnif (0, loc, k, s, ref NONE), loc)
adamc@403 213 end
adamc@403 214
adamc@403 215 end
adamc@403 216
adamc@623 217 fun elabKind env (k, loc) =
adamc@403 218 case k of
adamc@403 219 L.KType => (L'.KType, loc)
adamc@623 220 | L.KArrow (k1, k2) => (L'.KArrow (elabKind env k1, elabKind env k2), loc)
adamc@403 221 | L.KName => (L'.KName, loc)
adamc@623 222 | L.KRecord k => (L'.KRecord (elabKind env k), loc)
adamc@403 223 | L.KUnit => (L'.KUnit, loc)
adamc@623 224 | L.KTuple ks => (L'.KTuple (map (elabKind env) ks), loc)
adamc@403 225 | L.KWild => kunif loc
adamc@403 226
adamc@623 227 | L.KVar s => (case E.lookupK env s of
adamc@623 228 NONE =>
adamc@623 229 (kindError env (UnboundKind (loc, s));
adamc@623 230 kerror)
adamc@623 231 | SOME n => (L'.KRel n, loc))
adamc@623 232 | L.KFun (x, k) => (L'.KFun (x, elabKind (E.pushKRel env x) k), loc)
adamc@623 233
adamc@621 234 fun mapKind (dom, ran, loc)=
adamc@621 235 (L'.KArrow ((L'.KArrow (dom, ran), loc),
adamc@621 236 (L'.KArrow ((L'.KRecord dom, loc),
adamc@621 237 (L'.KRecord ran, loc)), loc)), loc)
adamc@403 238
adamc@403 239 fun hnormKind (kAll as (k, _)) =
adamc@403 240 case k of
adamc@403 241 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@403 242 | _ => kAll
adamc@403 243
adamc@623 244 open ElabOps
adamc@623 245
adamc@623 246 fun elabConHead (c as (_, loc)) k =
adamc@623 247 let
adamc@623 248 fun unravel (k, c) =
adamc@623 249 case hnormKind k of
adamc@623 250 (L'.KFun (x, k'), _) =>
adamc@623 251 let
adamc@623 252 val u = kunif loc
adamc@623 253
adamc@623 254 val k'' = subKindInKind (0, u) k'
adamc@623 255 in
adamc@623 256 unravel (k'', (L'.CKApp (c, u), loc))
adamc@623 257 end
adamc@623 258 | _ => (c, k)
adamc@623 259 in
adamc@623 260 unravel (k, c)
adamc@623 261 end
adamc@623 262
adamc@403 263 fun elabCon (env, denv) (c, loc) =
adamc@403 264 case c of
adamc@403 265 L.CAnnot (c, k) =>
adamc@403 266 let
adamc@623 267 val k' = elabKind env k
adamc@403 268 val (c', ck, gs) = elabCon (env, denv) c
adamc@403 269 in
adamc@403 270 checkKind env c' ck k';
adamc@403 271 (c', k', gs)
adamc@403 272 end
adamc@403 273
adamc@403 274 | L.TFun (t1, t2) =>
adamc@403 275 let
adamc@403 276 val (t1', k1, gs1) = elabCon (env, denv) t1
adamc@403 277 val (t2', k2, gs2) = elabCon (env, denv) t2
adamc@403 278 in
adamc@403 279 checkKind env t1' k1 ktype;
adamc@403 280 checkKind env t2' k2 ktype;
adamc@403 281 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
adamc@403 282 end
adamc@403 283 | L.TCFun (e, x, k, t) =>
adamc@403 284 let
adamc@403 285 val e' = elabExplicitness e
adamc@623 286 val k' = elabKind env k
adamc@403 287 val env' = E.pushCRel env x k'
adamc@403 288 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@403 289 in
adamc@403 290 checkKind env t' tk ktype;
adamc@403 291 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
adamc@403 292 end
adamc@623 293 | L.TKFun (x, t) =>
adamc@623 294 let
adamc@623 295 val env' = E.pushKRel env x
adamc@623 296 val (t', tk, gs) = elabCon (env', denv) t
adamc@623 297 in
adamc@623 298 checkKind env t' tk ktype;
adamc@623 299 ((L'.TKFun (x, t'), loc), ktype, gs)
adamc@623 300 end
adamc@628 301 | L.TDisjoint (c1, c2, c) =>
adamc@403 302 let
adamc@403 303 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 304 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 305
adamc@403 306 val ku1 = kunif loc
adamc@403 307 val ku2 = kunif loc
adamc@403 308
adamc@628 309 val denv' = D.assert env denv (c1', c2')
adamc@403 310 val (c', k, gs4) = elabCon (env, denv') c
adamc@403 311 in
adamc@403 312 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@403 313 checkKind env c2' k2 (L'.KRecord ku2, loc);
adam@1351 314 checkKind env c' k (L'.KType, loc);
adamc@403 315
adamc@628 316 ((L'.TDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs4)
adamc@403 317 end
adamc@403 318 | L.TRecord c =>
adamc@403 319 let
adamc@403 320 val (c', ck, gs) = elabCon (env, denv) c
adamc@403 321 val k = (L'.KRecord ktype, loc)
adamc@403 322 in
adamc@403 323 checkKind env c' ck k;
adamc@403 324 ((L'.TRecord c', loc), ktype, gs)
adamc@403 325 end
adamc@403 326
adamc@403 327 | L.CVar ([], s) =>
adamc@403 328 (case E.lookupC env s of
adamc@403 329 E.NotBound =>
adamc@403 330 (conError env (UnboundCon (loc, s));
adamc@403 331 (cerror, kerror, []))
adamc@403 332 | E.Rel (n, k) =>
adamc@623 333 let
adamc@623 334 val (c, k) = elabConHead (L'.CRel n, loc) k
adamc@623 335 in
adamc@623 336 (c, k, [])
adamc@623 337 end
adamc@403 338 | E.Named (n, k) =>
adamc@623 339 let
adamc@623 340 val (c, k) = elabConHead (L'.CNamed n, loc) k
adamc@623 341 in
adamc@623 342 (c, k, [])
adamc@623 343 end)
adamc@403 344 | L.CVar (m1 :: ms, s) =>
adamc@403 345 (case E.lookupStr env m1 of
adamc@403 346 NONE => (conError env (UnboundStrInCon (loc, m1));
adamc@403 347 (cerror, kerror, []))
adamc@403 348 | SOME (n, sgn) =>
adamc@403 349 let
adamc@403 350 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@403 351 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@403 352 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@403 353 (strerror, sgnerror))
adamc@403 354 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@403 355 ((L'.StrVar n, loc), sgn) ms
adamc@403 356
adamc@403 357 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
adamc@403 358 NONE => (conError env (UnboundCon (loc, s));
adamc@403 359 kerror)
adamc@403 360 | SOME (k, _) => k
adamc@987 361 val (c, k) = elabConHead (L'.CModProj (n, ms, s), loc) k
adamc@403 362 in
adamc@987 363 (c, k, [])
adamc@403 364 end)
adamc@403 365
adamc@403 366 | L.CApp (c1, c2) =>
adamc@403 367 let
adamc@403 368 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 369 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 370 val dom = kunif loc
adamc@403 371 val ran = kunif loc
adamc@403 372 in
adamc@403 373 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@403 374 checkKind env c2' k2 dom;
adamc@403 375 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
adamc@403 376 end
adamc@403 377 | L.CAbs (x, ko, t) =>
adamc@403 378 let
adamc@403 379 val k' = case ko of
adamc@403 380 NONE => kunif loc
adamc@623 381 | SOME k => elabKind env k
adamc@403 382 val env' = E.pushCRel env x k'
adamc@403 383 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@403 384 in
adamc@403 385 ((L'.CAbs (x, k', t'), loc),
adamc@403 386 (L'.KArrow (k', tk), loc),
adamc@403 387 gs)
adamc@403 388 end
adamc@623 389 | L.CKAbs (x, t) =>
adamc@623 390 let
adamc@623 391 val env' = E.pushKRel env x
adamc@623 392 val (t', tk, gs) = elabCon (env', denv) t
adamc@623 393 in
adamc@623 394 ((L'.CKAbs (x, t'), loc),
adamc@623 395 (L'.KFun (x, tk), loc),
adamc@623 396 gs)
adamc@623 397 end
adamc@403 398
adamc@403 399 | L.CName s =>
adamc@403 400 ((L'.CName s, loc), kname, [])
adamc@403 401
adamc@403 402 | L.CRecord xcs =>
adamc@403 403 let
adamc@403 404 val k = kunif loc
adamc@403 405
adamc@403 406 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
adamc@403 407 let
adamc@403 408 val (x', xk, gs1) = elabCon (env, denv) x
adamc@403 409 val (c', ck, gs2) = elabCon (env, denv) c
adamc@403 410 in
adamc@403 411 checkKind env x' xk kname;
adamc@403 412 checkKind env c' ck k;
adamc@403 413 ((x', c'), gs1 @ gs2 @ gs)
adamc@403 414 end) [] xcs
adamc@403 415
adamc@403 416 val rc = (L'.CRecord (k, xcs'), loc)
adamc@403 417 (* Add duplicate field checking later. *)
adamc@403 418
adamc@403 419 fun prove (xcs, ds) =
adamc@403 420 case xcs of
adamc@403 421 [] => ds
adamc@403 422 | xc :: rest =>
adamc@403 423 let
adamc@403 424 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@403 425 val ds = foldl (fn (xc', ds) =>
adamc@403 426 let
adamc@403 427 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@403 428 in
adamc@403 429 D.prove env denv (r1, r2, loc) @ ds
adamc@403 430 end)
adamc@403 431 ds rest
adamc@403 432 in
adamc@403 433 prove (rest, ds)
adamc@403 434 end
adamc@403 435 in
adamc@403 436 (rc, (L'.KRecord k, loc), prove (xcs', gs))
adamc@403 437 end
adamc@403 438 | L.CConcat (c1, c2) =>
adamc@403 439 let
adamc@403 440 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@403 441 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@403 442 val ku = kunif loc
adamc@403 443 val k = (L'.KRecord ku, loc)
adamc@403 444 in
adamc@403 445 checkKind env c1' k1 k;
adamc@403 446 checkKind env c2' k2 k;
adamc@403 447 ((L'.CConcat (c1', c2'), loc), k,
adamc@403 448 D.prove env denv (c1', c2', loc) @ gs1 @ gs2)
adamc@403 449 end
adamc@621 450 | L.CMap =>
adamc@403 451 let
adamc@403 452 val dom = kunif loc
adamc@403 453 val ran = kunif loc
adamc@403 454 in
adamc@621 455 ((L'.CMap (dom, ran), loc),
adamc@621 456 mapKind (dom, ran, loc),
adamc@403 457 [])
adamc@403 458 end
adamc@403 459
adamc@403 460 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
adamc@403 461
adamc@403 462 | L.CTuple cs =>
adamc@403 463 let
adamc@403 464 val (cs', ks, gs) = foldl (fn (c, (cs', ks, gs)) =>
adamc@403 465 let
adamc@403 466 val (c', k, gs') = elabCon (env, denv) c
adamc@403 467 in
adamc@403 468 (c' :: cs', k :: ks, gs' @ gs)
adamc@403 469 end) ([], [], []) cs
adamc@403 470 in
adamc@403 471 ((L'.CTuple (rev cs'), loc), (L'.KTuple (rev ks), loc), gs)
adamc@403 472 end
adamc@403 473 | L.CProj (c, n) =>
adamc@403 474 let
adamc@403 475 val (c', k, gs) = elabCon (env, denv) c
adam@1302 476
adam@1302 477 val k' = kunif loc
adamc@403 478 in
adam@1302 479 if n <= 0 then
adam@1302 480 (conError env (ProjBounds (c', n));
adam@1302 481 (cerror, kerror, []))
adam@1302 482 else
adam@1302 483 (checkKind env c' k (L'.KTupleUnif (loc, [(n, k')], ref NONE), loc);
adam@1302 484 ((L'.CProj (c', n), loc), k', gs))
adamc@403 485 end
adamc@403 486
adamc@403 487 | L.CWild k =>
adamc@403 488 let
adamc@623 489 val k' = elabKind env k
adamc@403 490 in
adamc@403 491 (cunif (loc, k'), k', [])
adamc@403 492 end
adamc@403 493
adamc@403 494 fun kunifsRemain k =
adamc@403 495 case k of
adamc@403 496 L'.KUnif (_, _, ref NONE) => true
adam@1302 497 | L'.KTupleUnif (_, _, ref NONE) => true
adamc@403 498 | _ => false
adamc@403 499 fun cunifsRemain c =
adamc@403 500 case c of
adam@1326 501 L'.CUnif (_, loc, k, _, r as ref NONE) =>
adam@1326 502 (case #1 (hnormKind k) of
adam@1584 503 L'.KUnit => (r := SOME (L'.CUnit, loc); false)
adam@1584 504 | _ => true)
adam@1584 505 | _ => false
adamc@403 506
adamc@403 507 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
adamc@403 508 con = fn _ => false,
adamc@403 509 exp = fn _ => false,
adamc@403 510 sgn_item = fn _ => false,
adamc@403 511 sgn = fn _ => false,
adamc@403 512 str = fn _ => false,
adamc@403 513 decl = fn _ => false}
adamc@403 514
adam@1584 515 val cunifsInDecl = U.Decl.exists {kind = fn _ => false,
adamc@403 516 con = cunifsRemain,
adam@1584 517 exp = fn _ => false,
adam@1584 518 sgn_item = fn _ => false,
adam@1584 519 sgn = fn _ => false,
adam@1584 520 str = fn _ => false,
adam@1584 521 decl = fn _ => false}
adamc@403 522
adamc@403 523 fun occursCon r =
adamc@403 524 U.Con.exists {kind = fn _ => false,
adam@1303 525 con = fn L'.CUnif (_, _, _, _, r') => r = r'
adamc@403 526 | _ => false}
adamc@403 527
adamc@403 528 exception CUnify' of cunify_error
adamc@403 529
adamc@403 530 type record_summary = {
adamc@403 531 fields : (L'.con * L'.con) list,
adamc@403 532 unifs : (L'.con * L'.con option ref) list,
adamc@403 533 others : L'.con list
adamc@403 534 }
adamc@403 535
adamc@403 536 fun summaryToCon {fields, unifs, others} =
adamc@403 537 let
adam@1626 538 fun concat (c1, c2) =
adam@1626 539 case #1 c1 of
adam@1626 540 L'.CRecord (_, []) => c2
adam@1626 541 | _ => case #1 c2 of
adam@1626 542 L'.CRecord (_, []) => c1
adam@1626 543 | _ => (L'.CConcat (c1, c2), dummy)
adam@1626 544
adamc@403 545 val c = (L'.CRecord (ktype, []), dummy)
adam@1626 546 val c = List.foldr concat c others
adam@1626 547 val c = List.foldr (fn ((c', _), c) => concat (c', c)) c unifs
adamc@403 548 in
adam@1626 549 concat ((L'.CRecord (ktype, fields), dummy), c)
adamc@403 550 end
adamc@403 551
adamc@403 552 fun p_summary env s = p_con env (summaryToCon s)
adamc@403 553
adamc@403 554 exception CUnify of L'.con * L'.con * cunify_error
adamc@403 555
adamc@403 556 fun kindof env (c, loc) =
adamc@403 557 case c of
adamc@403 558 L'.TFun _ => ktype
adamc@403 559 | L'.TCFun _ => ktype
adamc@403 560 | L'.TRecord _ => ktype
adamc@628 561 | L'.TDisjoint _ => ktype
adamc@403 562
adamc@403 563 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@403 564 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@403 565 | L'.CModProj (n, ms, x) =>
adamc@403 566 let
adamc@403 567 val (_, sgn) = E.lookupStrNamed env n
adamc@403 568 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@403 569 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@403 570 NONE => raise Fail "kindof: Unknown substructure"
adamc@403 571 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@403 572 ((L'.StrVar n, loc), sgn) ms
adamc@403 573 in
adamc@403 574 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@403 575 NONE => raise Fail "kindof: Unknown con in structure"
adamc@403 576 | SOME (k, _) => k
adamc@403 577 end
adamc@403 578
adamc@403 579 | L'.CApp (c, _) =>
adamc@403 580 (case hnormKind (kindof env c) of
adamc@403 581 (L'.KArrow (_, k), _) => k
adamc@403 582 | (L'.KError, _) => kerror
adamc@413 583 | k => raise CUnify' (CKindof (k, c, "arrow")))
adamc@403 584 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@628 585
adamc@403 586
adamc@403 587 | L'.CName _ => kname
adamc@403 588
adamc@403 589 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@403 590 | L'.CConcat (c, _) => kindof env c
adamc@621 591 | L'.CMap (dom, ran) => mapKind (dom, ran, loc)
adamc@403 592
adamc@403 593 | L'.CUnit => (L'.KUnit, loc)
adamc@403 594
adamc@403 595 | L'.CTuple cs => (L'.KTuple (map (kindof env) cs), loc)
adamc@403 596 | L'.CProj (c, n) =>
adamc@403 597 (case hnormKind (kindof env c) of
adamc@403 598 (L'.KTuple ks, _) => List.nth (ks, n - 1)
adam@1311 599 | (L'.KUnif (_, _, r), _) =>
adam@1311 600 let
adam@1311 601 val ku = kunif loc
adam@1311 602 val k = (L'.KTupleUnif (loc, [(n, ku)], ref NONE), loc)
adam@1311 603 in
adam@1311 604 r := SOME k;
adam@1311 605 k
adam@1311 606 end
adam@1311 607 | (L'.KTupleUnif (_, nks, r), _) =>
adam@1311 608 (case ListUtil.search (fn (n', k) => if n' = n then SOME k else NONE) nks of
adam@1311 609 SOME k => k
adam@1311 610 | NONE =>
adam@1311 611 let
adam@1311 612 val ku = kunif loc
adam@1311 613 val k = (L'.KTupleUnif (loc, ((n, ku) :: nks), ref NONE), loc)
adam@1311 614 in
adam@1311 615 r := SOME k;
adam@1311 616 k
adam@1311 617 end)
adamc@413 618 | k => raise CUnify' (CKindof (k, c, "tuple")))
adamc@403 619
adamc@403 620 | L'.CError => kerror
adam@1303 621 | L'.CUnif (_, _, k, _, _) => k
adamc@403 622
adamc@623 623 | L'.CKAbs (x, c) => (L'.KFun (x, kindof (E.pushKRel env x) c), loc)
adamc@623 624 | L'.CKApp (c, k) =>
adamc@623 625 (case hnormKind (kindof env c) of
adamc@623 626 (L'.KFun (_, k'), _) => subKindInKind (0, k) k'
adamc@623 627 | k => raise CUnify' (CKindof (k, c, "kapp")))
adamc@623 628 | L'.TKFun _ => ktype
adamc@403 629
adamc@621 630 exception GuessFailure
adamc@403 631
adamc@513 632 fun isUnitCon env (c, loc) =
adamc@513 633 case c of
adamc@513 634 L'.TFun _ => false
adamc@513 635 | L'.TCFun _ => false
adamc@513 636 | L'.TRecord _ => false
adamc@628 637 | L'.TDisjoint _ => false
adamc@513 638
adam@1316 639 | L'.CRel xn => #1 (hnormKind (#2 (E.lookupCRel env xn))) = L'.KUnit
adam@1316 640 | L'.CNamed xn => #1 (hnormKind (#2 (E.lookupCNamed env xn))) = L'.KUnit
adamc@518 641 | L'.CModProj (n, ms, x) => false
adamc@518 642 (*let
adamc@513 643 val (_, sgn) = E.lookupStrNamed env n
adamc@513 644 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@513 645 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@513 646 NONE => raise Fail "kindof: Unknown substructure"
adamc@513 647 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@513 648 ((L'.StrVar n, loc), sgn) ms
adamc@513 649 in
adamc@513 650 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@513 651 NONE => raise Fail "kindof: Unknown con in structure"
adamc@513 652 | SOME ((k, _), _) => k = L'.KUnit
adamc@518 653 end*)
adamc@518 654
adamc@518 655 | L'.CApp (c, _) => false
adamc@518 656 (*(case hnormKind (kindof env c) of
adamc@513 657 (L'.KArrow (_, k), _) => #1 k = L'.KUnit
adamc@513 658 | (L'.KError, _) => false
adamc@518 659 | k => raise CUnify' (CKindof (k, c, "arrow")))*)
adamc@513 660 | L'.CAbs _ => false
adamc@513 661
adamc@513 662 | L'.CName _ => false
adamc@513 663
adamc@513 664 | L'.CRecord _ => false
adamc@513 665 | L'.CConcat _ => false
adamc@621 666 | L'.CMap _ => false
adamc@513 667
adamc@513 668 | L'.CUnit => true
adamc@513 669
adamc@513 670 | L'.CTuple _ => false
adamc@518 671 | L'.CProj (c, n) => false
adamc@518 672 (*(case hnormKind (kindof env c) of
adamc@513 673 (L'.KTuple ks, _) => #1 (List.nth (ks, n - 1)) = L'.KUnit
adamc@518 674 | k => raise CUnify' (CKindof (k, c, "tuple")))*)
adamc@513 675
adamc@513 676 | L'.CError => false
adam@1316 677 | L'.CUnif (_, _, k, _, _) => #1 (hnormKind k) = L'.KUnit
adamc@513 678
adamc@623 679 | L'.CKAbs _ => false
adamc@623 680 | L'.CKApp _ => false
adamc@623 681 | L'.TKFun _ => false
adamc@623 682
adamc@709 683 val recdCounter = ref 0
adamc@709 684
adamc@713 685 val mayDelay = ref false
adamc@777 686 val delayedUnifs = ref ([] : (ErrorMsg.span * E.env * L'.kind * record_summary * record_summary) list)
adamc@777 687
adamc@830 688 val delayedExhaustives = ref ([] : (E.env * L'.con * L'.pat list * ErrorMsg.span) list)
adamc@830 689
adam@1304 690 exception CantSquish
adam@1304 691
adam@1304 692 fun squish by =
adam@1304 693 U.Con.mapB {kind = fn _ => fn k => k,
adam@1304 694 con = fn bound => fn c =>
adam@1304 695 case c of
adam@1304 696 L'.CRel xn =>
adam@1304 697 if xn < bound then
adam@1304 698 c
adam@1304 699 else if bound <= xn andalso xn < bound + by then
adam@1304 700 raise CantSquish
adam@1304 701 else
adam@1304 702 L'.CRel (xn - by)
adam@1304 703 | L'.CUnif _ => raise CantSquish
adam@1304 704 | _ => c,
adam@1304 705 bind = fn (bound, U.Con.RelC _) => bound + 1
adam@1304 706 | (bound, _) => bound} 0
adam@1304 707
adam@1582 708 val reducedSummaries = ref (NONE : (Print.PD.pp_desc * Print.PD.pp_desc) option)
adam@1582 709
adamc@777 710 fun unifyRecordCons env (loc, c1, c2) =
adamc@403 711 let
adamc@403 712 fun rkindof c =
adamc@403 713 case hnormKind (kindof env c) of
adamc@403 714 (L'.KRecord k, _) => k
adamc@403 715 | (L'.KError, _) => kerror
adamc@753 716 | (L'.KUnif (_, _, r), _) =>
adamc@753 717 let
adamc@753 718 val k = kunif (#2 c)
adamc@753 719 in
adamc@753 720 r := SOME (L'.KRecord k, #2 c);
adamc@753 721 k
adamc@753 722 end
adamc@413 723 | k => raise CUnify' (CKindof (k, c, "record"))
adamc@403 724
adamc@403 725 val k1 = rkindof c1
adamc@403 726 val k2 = rkindof c2
adamc@403 727
adamc@628 728 val r1 = recordSummary env c1
adamc@628 729 val r2 = recordSummary env c2
adamc@403 730 in
adamc@623 731 unifyKinds env k1 k2;
adamc@777 732 unifySummaries env (loc, k1, r1, r2)
adamc@403 733 end
adamc@403 734
adamc@777 735 and normalizeRecordSummary env (r : record_summary) =
adamc@777 736 recordSummary env (summaryToCon r)
adamc@777 737
adamc@628 738 and recordSummary env c =
adamc@403 739 let
adamc@628 740 val c = hnormCon env c
adamc@628 741
adamc@628 742 val sum =
adamc@403 743 case c of
adamc@628 744 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
adamc@403 745 | (L'.CConcat (c1, c2), _) =>
adamc@403 746 let
adamc@628 747 val s1 = recordSummary env c1
adamc@628 748 val s2 = recordSummary env c2
adamc@403 749 in
adamc@628 750 {fields = #fields s1 @ #fields s2,
adamc@628 751 unifs = #unifs s1 @ #unifs s2,
adamc@628 752 others = #others s1 @ #others s2}
adamc@403 753 end
adam@1303 754 | (L'.CUnif (nl, _, _, _, ref (SOME c)), _) => recordSummary env (E.mliftConInCon nl c)
adam@1303 755 | c' as (L'.CUnif (0, _, _, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
adamc@628 756 | c' => {fields = [], unifs = [], others = [c']}
adamc@403 757 in
adamc@628 758 sum
adamc@403 759 end
adamc@403 760
adamc@987 761 and consEq env loc (c1, c2) =
adamc@777 762 let
adamc@777 763 val mayDelay' = !mayDelay
adamc@777 764 in
adamc@777 765 (mayDelay := false;
adamc@987 766 unifyCons env loc c1 c2;
adamc@777 767 mayDelay := mayDelay';
adamc@777 768 true)
adamc@777 769 handle CUnify _ => (mayDelay := mayDelay'; false)
adamc@777 770 end
adamc@403 771
adamc@403 772 and consNeq env (c1, c2) =
adamc@628 773 case (#1 (hnormCon env c1), #1 (hnormCon env c2)) of
adamc@403 774 (L'.CName x1, L'.CName x2) => x1 <> x2
adamc@1093 775 | (L'.CName _, L'.CRel _) => true
adamc@1093 776 | (L'.CRel _, L'.CName _) => true
adamc@1093 777 | (L'.CRel n1, L'.CRel n2) => n1 <> n2
adamc@1093 778 | (L'.CRel _, L'.CNamed _) => true
adamc@1093 779 | (L'.CNamed _, L'.CRel _) => true
adamc@1093 780 | (L'.CRel _, L'.CModProj _) => true
adamc@1093 781 | (L'.CModProj _, L'.CRel _) => true
adamc@1269 782 | (L'.CModProj (_, _, n1), L'.CModProj (_, _, n2)) => n1 <> n2
adam@1589 783 | (L'.CModProj _, L'.CName _) => true
adam@1589 784 | (L'.CName _, L'.CModProj _) => true
adam@1589 785 | (L'.CNamed _, L'.CName _) => true
adam@1589 786 | (L'.CName _, L'.CNamed _) => true
adamc@403 787 | _ => false
adamc@403 788
adamc@777 789 and unifySummaries env (loc, k, s1 : record_summary, s2 : record_summary) =
adamc@403 790 let
adam@1583 791 val () = reducedSummaries := NONE
adam@1583 792
adamc@848 793 (*val () = eprefaces "Summaries" [("loc", PD.string (ErrorMsg.spanToString loc)),
adam@1303 794 ("#1", p_summary env s1),
adam@1303 795 ("#2", p_summary env s2)]*)
adamc@403 796
adamc@403 797 fun eatMatching p (ls1, ls2) =
adamc@403 798 let
adamc@403 799 fun em (ls1, ls2, passed1) =
adamc@403 800 case ls1 of
adamc@403 801 [] => (rev passed1, ls2)
adamc@403 802 | h1 :: t1 =>
adamc@403 803 let
adamc@403 804 fun search (ls2', passed2) =
adamc@403 805 case ls2' of
adamc@403 806 [] => em (t1, ls2, h1 :: passed1)
adamc@403 807 | h2 :: t2 =>
adamc@403 808 if p (h1, h2) then
adamc@403 809 em (t1, List.revAppend (passed2, t2), passed1)
adamc@403 810 else
adamc@403 811 search (t2, h2 :: passed2)
adamc@403 812 in
adamc@403 813 search (ls2, [])
adamc@403 814 end
adamc@403 815 in
adamc@403 816 em (ls1, ls2, [])
adamc@403 817 end
adamc@403 818
adamc@403 819 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@403 820 not (consNeq env (x1, x2))
adamc@987 821 andalso consEq env loc (c1, c2)
adamc@987 822 andalso consEq env loc (x1, x2))
adamc@403 823 (#fields s1, #fields s2)
adamc@403 824 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@403 825 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@646 826
adamc@403 827 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@646 828
adamc@987 829 val (others1, others2) = eatMatching (consEq env loc) (#others s1, #others s2)
adamc@403 830 (*val () = eprefaces "Summaries3" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@403 831 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 832
adamc@709 833 fun unsummarize {fields, unifs, others} =
adamc@709 834 let
adamc@709 835 val c = (L'.CRecord (k, fields), loc)
adamc@709 836
adamc@709 837 val c = foldl (fn ((c1, _), c2) => (L'.CConcat (c1, c2), loc))
adamc@709 838 c unifs
adamc@709 839 in
adamc@709 840 foldl (fn (c1, c2) => (L'.CConcat (c1, c2), loc))
adamc@709 841 c others
adamc@709 842 end
adamc@709 843
adamc@710 844 val empties = ([], [], [], [], [], [])
adamc@710 845
adamc@709 846 val (unifs1, fs1, others1, unifs2, fs2, others2) =
adamc@709 847 case (unifs1, fs1, others1, unifs2, fs2, others2) of
adamc@709 848 orig as ([(_, r)], [], [], _, _, _) =>
adamc@709 849 let
adamc@709 850 val c = unsummarize {fields = fs2, others = others2, unifs = unifs2}
adamc@709 851 in
adamc@709 852 if occursCon r c then
adamc@709 853 orig
adamc@709 854 else
adamc@709 855 (r := SOME c;
adamc@710 856 empties)
adamc@709 857 end
adamc@709 858 | orig as (_, _, _, [(_, r)], [], []) =>
adamc@709 859 let
adamc@709 860 val c = unsummarize {fields = fs1, others = others1, unifs = unifs1}
adamc@709 861 in
adamc@709 862 if occursCon r c then
adamc@709 863 orig
adamc@709 864 else
adamc@709 865 (r := SOME c;
adamc@710 866 empties)
adamc@709 867 end
adamc@710 868 | orig as ([(_, r1 as ref NONE)], _, [], [(_, r2 as ref NONE)], _, []) =>
adamc@710 869 if List.all (fn (x1, _) => List.all (fn (x2, _) => consNeq env (x1, x2)) fs2) fs1 then
adamc@710 870 let
adamc@838 871 val kr = (L'.KRecord k, loc)
adamc@710 872 val u = cunif (loc, kr)
adamc@710 873 in
adamc@710 874 r1 := SOME (L'.CConcat ((L'.CRecord (k, fs2), loc), u), loc);
adamc@710 875 r2 := SOME (L'.CConcat ((L'.CRecord (k, fs1), loc), u), loc);
adamc@710 876 empties
adamc@710 877 end
adamc@710 878 else
adamc@710 879 orig
adamc@709 880 | orig => orig
adamc@709 881
adamc@403 882 (*val () = eprefaces "Summaries4" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@777 883 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 884
adamc@753 885 fun isGuessable (other, fs, unifs) =
adamc@753 886 let
adamc@753 887 val c = (L'.CRecord (k, fs), loc)
adamc@753 888 val c = foldl (fn ((c', _), c) => (L'.CConcat (c', c), loc)) c unifs
adamc@753 889 in
adamc@987 890 (guessMap env loc (other, c, GuessFailure);
adamc@753 891 true)
adamc@753 892 handle GuessFailure => false
adamc@753 893 end
adamc@403 894
adamc@754 895 val (fs1, fs2, others1, others2, unifs1, unifs2) =
adamc@753 896 case (fs1, fs2, others1, others2, unifs1, unifs2) of
adamc@753 897 ([], _, [other1], [], [], _) =>
adamc@753 898 if isGuessable (other1, fs2, unifs2) then
adamc@754 899 ([], [], [], [], [], [])
adamc@403 900 else
adamc@754 901 (fs1, fs2, others1, others2, unifs1, unifs2)
adamc@753 902 | (_, [], [], [other2], _, []) =>
adamc@777 903 if isGuessable (other2, fs1, unifs1) then
adamc@777 904 ([], [], [], [], [], [])
adamc@777 905 else
adamc@777 906 (fs1, fs2, others1, others2, unifs1, unifs2)
adamc@754 907 | _ => (fs1, fs2, others1, others2, unifs1, unifs2)
adamc@403 908
adam@1582 909 val () = if !mayDelay then
adam@1582 910 ()
adam@1582 911 else
adam@1626 912 let
adam@1626 913 val c1 = summaryToCon {fields = fs1, unifs = unifs1, others = others1}
adam@1626 914 val c2 = summaryToCon {fields = fs2, unifs = unifs2, others = others2}
adam@1626 915 in
adam@1626 916 case (c1, c2) of
adam@1626 917 ((L'.CRecord (_, []), _), (L'.CRecord (_, []), _)) => reducedSummaries := NONE
adam@1626 918 | _ => reducedSummaries := SOME (p_con env c1, p_con env c2)
adam@1626 919 end
adam@1582 920
adamc@403 921 (*val () = eprefaces "Summaries5" [("#1", p_summary env {fields = fs1, unifs = unifs1, others = others1}),
adamc@646 922 ("#2", p_summary env {fields = fs2, unifs = unifs2, others = others2})]*)
adamc@403 923
adamc@838 924 val empty = (L'.CRecord (k, []), loc)
adamc@1071 925 fun failure () =
adamc@1071 926 let
adamc@1071 927 val fs2 = #fields s2
adamc@1071 928
adamc@1071 929 fun findPointwise fs1 =
adamc@1071 930 case fs1 of
adamc@1071 931 [] => NONE
adamc@1071 932 | (nm1, c1) :: fs1 =>
adamc@1071 933 case List.find (fn (nm2, _) => consEq env loc (nm1, nm2)) fs2 of
adamc@1071 934 NONE => findPointwise fs1
adamc@1071 935 | SOME (_, c2) =>
adamc@1071 936 if consEq env loc (c1, c2) then
adamc@1071 937 findPointwise fs1
adamc@1071 938 else
adam@1588 939 SOME (nm1, c1, c2, (unifyCons env loc c1 c2; NONE)
adam@1588 940 handle CUnify (_, _, err) => (reducedSummaries := NONE;
adam@1588 941 SOME err))
adamc@1071 942 in
adamc@1071 943 raise CUnify' (CRecordFailure (unsummarize s1, unsummarize s2, findPointwise (#fields s1)))
adamc@1071 944 end
adam@1341 945
adam@1341 946 fun default () = if !mayDelay then
adam@1341 947 delayedUnifs := (loc, env, k, s1, s2) :: !delayedUnifs
adam@1341 948 else
adam@1341 949 failure ()
adamc@709 950 in
adamc@777 951 (case (unifs1, fs1, others1, unifs2, fs2, others2) of
adamc@777 952 (_, [], [], [], [], []) =>
adamc@777 953 app (fn (_, r) => r := SOME empty) unifs1
adamc@777 954 | ([], [], [], _, [], []) =>
adamc@777 955 app (fn (_, r) => r := SOME empty) unifs2
adam@1341 956 | (_, _, _, [], [], [cr as (L'.CUnif (nl, _, _, _, r), _)]) =>
adam@1341 957 let
adam@1341 958 val c = summaryToCon {fields = fs1, unifs = unifs1, others = others1}
adam@1341 959 in
adam@1341 960 if occursCon r c then
adam@1588 961 (reducedSummaries := NONE;
adam@1588 962 raise CUnify' (COccursCheckFailed (cr, c)))
adam@1341 963 else
adam@1341 964 (r := SOME (squish nl c))
adam@1341 965 handle CantSquish => default ()
adam@1341 966 end
adam@1341 967 | ([], [], [cr as (L'.CUnif (nl, _, _, _, r), _)], _, _, _) =>
adam@1341 968 let
adam@1341 969 val c = summaryToCon {fields = fs2, unifs = unifs2, others = others2}
adam@1341 970 in
adam@1341 971 if occursCon r c then
adam@1588 972 (reducedSummaries := NONE;
adam@1588 973 raise CUnify' (COccursCheckFailed (cr, c)))
adam@1341 974 else
adam@1341 975 (r := SOME (squish nl c))
adam@1341 976 handle CantSquish => default ()
adam@1341 977 end
adam@1341 978 | _ => default ())
adam@1341 979
adamc@777 980 (*before eprefaces "Summaries'" [("#1", p_summary env (normalizeRecordSummary env s1)),
adamc@777 981 ("#2", p_summary env (normalizeRecordSummary env s2))]*)
adamc@403 982 end
adamc@403 983
adamc@987 984 and guessMap env loc (c1, c2, ex) =
adamc@403 985 let
adamc@621 986 fun unfold (dom, ran, f, r, c) =
adamc@403 987 let
adamc@621 988 fun unfold (r, c) =
adamc@754 989 case #1 (hnormCon env c) of
adamc@987 990 L'.CRecord (_, []) => unifyCons env loc r (L'.CRecord (dom, []), loc)
adamc@628 991 | L'.CRecord (_, [(x, v)]) =>
adamc@628 992 let
adamc@628 993 val v' = case dom of
adamc@628 994 (L'.KUnit, _) => (L'.CUnit, loc)
adamc@628 995 | _ => cunif (loc, dom)
adamc@628 996 in
adamc@987 997 unifyCons env loc v (L'.CApp (f, v'), loc);
adamc@987 998 unifyCons env loc r (L'.CRecord (dom, [(x, v')]), loc)
adamc@628 999 end
adamc@628 1000 | L'.CRecord (_, (x, v) :: rest) =>
adamc@628 1001 let
adamc@628 1002 val r1 = cunif (loc, (L'.KRecord dom, loc))
adamc@628 1003 val r2 = cunif (loc, (L'.KRecord dom, loc))
adamc@628 1004 in
adamc@628 1005 unfold (r1, (L'.CRecord (ran, [(x, v)]), loc));
adamc@628 1006 unfold (r2, (L'.CRecord (ran, rest), loc));
adamc@987 1007 unifyCons env loc r (L'.CConcat (r1, r2), loc)
adamc@628 1008 end
adamc@628 1009 | L'.CConcat (c1', c2') =>
adamc@628 1010 let
adamc@628 1011 val r1 = cunif (loc, (L'.KRecord dom, loc))
adamc@628 1012 val r2 = cunif (loc, (L'.KRecord dom, loc))
adamc@628 1013 in
adamc@628 1014 unfold (r1, c1');
adamc@628 1015 unfold (r2, c2');
adamc@987 1016 unifyCons env loc r (L'.CConcat (r1, r2), loc)
adamc@628 1017 end
adam@1303 1018 | L'.CUnif (0, _, _, _, ur) =>
adamc@777 1019 ur := SOME (L'.CApp ((L'.CApp ((L'.CMap (dom, ran), loc), f), loc), r), loc)
adamc@628 1020 | _ => raise ex
adamc@403 1021 in
adamc@621 1022 unfold (r, c)
adamc@403 1023 end
adamc@632 1024 handle _ => raise ex
adamc@403 1025 in
adamc@403 1026 case (#1 c1, #1 c2) of
adamc@621 1027 (L'.CApp ((L'.CApp ((L'.CMap (dom, ran), _), f), _), r), _) =>
adamc@621 1028 unfold (dom, ran, f, r, c2)
adamc@621 1029 | (_, L'.CApp ((L'.CApp ((L'.CMap (dom, ran), _), f), _), r)) =>
adamc@621 1030 unfold (dom, ran, f, r, c1)
adamc@403 1031 | _ => raise ex
adamc@403 1032 end
adamc@403 1033
adamc@987 1034 and unifyCons' env loc c1 c2 =
adamc@628 1035 if isUnitCon env c1 andalso isUnitCon env c2 then
adamc@628 1036 ()
adamc@628 1037 else
adamc@403 1038 let
adamc@628 1039 (*val befor = Time.now ()
adamc@628 1040 val old1 = c1
adamc@628 1041 val old2 = c2*)
adamc@628 1042 val c1 = hnormCon env c1
adamc@628 1043 val c2 = hnormCon env c2
adamc@403 1044 in
adamc@987 1045 unifyCons'' env loc c1 c2
adamc@987 1046 handle ex => guessMap env loc (c1, c2, ex)
adamc@403 1047 end
adamc@628 1048
adamc@987 1049 and unifyCons'' env loc (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@403 1050 let
adamc@403 1051 fun err f = raise CUnify' (f (c1All, c2All))
adamc@403 1052
adamc@706 1053 fun projSpecial1 (c1, n1, onFail) =
adamc@403 1054 let
adamc@632 1055 fun trySnd () =
adamc@632 1056 case #1 (hnormCon env c2All) of
adamc@632 1057 L'.CProj (c2, n2) =>
adamc@632 1058 let
adamc@632 1059 fun tryNormal () =
adamc@632 1060 if n1 = n2 then
adamc@987 1061 unifyCons' env loc c1 c2
adamc@632 1062 else
adamc@706 1063 onFail ()
adamc@632 1064 in
adamc@632 1065 case #1 (hnormCon env c2) of
adam@1303 1066 L'.CUnif (0, _, k, _, r) =>
adamc@632 1067 (case #1 (hnormKind k) of
adamc@632 1068 L'.KTuple ks =>
adamc@632 1069 let
adamc@632 1070 val loc = #2 c2
adamc@632 1071 val us = map (fn k => cunif (loc, k)) ks
adamc@632 1072 in
adamc@632 1073 r := SOME (L'.CTuple us, loc);
adamc@987 1074 unifyCons' env loc c1All (List.nth (us, n2 - 1))
adamc@632 1075 end
adamc@632 1076 | _ => tryNormal ())
adamc@632 1077 | _ => tryNormal ()
adamc@632 1078 end
adamc@706 1079 | _ => onFail ()
adamc@403 1080 in
adamc@632 1081 case #1 (hnormCon env c1) of
adam@1303 1082 L'.CUnif (0, _, k, _, r) =>
adamc@632 1083 (case #1 (hnormKind k) of
adamc@632 1084 L'.KTuple ks =>
adamc@632 1085 let
adamc@632 1086 val loc = #2 c1
adamc@632 1087 val us = map (fn k => cunif (loc, k)) ks
adamc@632 1088 in
adamc@632 1089 r := SOME (L'.CTuple us, loc);
adamc@987 1090 unifyCons' env loc (List.nth (us, n1 - 1)) c2All
adamc@632 1091 end
adamc@632 1092 | _ => trySnd ())
adamc@632 1093 | _ => trySnd ()
adamc@403 1094 end
adamc@632 1095
adamc@706 1096 fun projSpecial2 (c2, n2, onFail) =
adamc@706 1097 case #1 (hnormCon env c2) of
adam@1303 1098 L'.CUnif (0, _, k, _, r) =>
adamc@706 1099 (case #1 (hnormKind k) of
adamc@706 1100 L'.KTuple ks =>
adamc@706 1101 let
adamc@706 1102 val loc = #2 c2
adamc@706 1103 val us = map (fn k => cunif (loc, k)) ks
adamc@706 1104 in
adamc@706 1105 r := SOME (L'.CTuple us, loc);
adamc@987 1106 unifyCons' env loc c1All (List.nth (us, n2 - 1))
adamc@706 1107 end
adamc@706 1108 | _ => onFail ())
adamc@706 1109 | _ => onFail ()
adamc@706 1110
adamc@777 1111 fun isRecord' () = unifyRecordCons env (loc, c1All, c2All)
adamc@706 1112
adamc@706 1113 fun isRecord () =
adamc@706 1114 case (c1, c2) of
adamc@706 1115 (L'.CProj (c1, n1), _) => projSpecial1 (c1, n1, isRecord')
adamc@706 1116 | (_, L'.CProj (c2, n2)) => projSpecial2 (c2, n2, isRecord')
adamc@706 1117 | _ => isRecord' ()
adamc@706 1118 in
adamc@706 1119 (*eprefaces "unifyCons''" [("c1All", p_con env c1All),
adamc@706 1120 ("c2All", p_con env c2All)];*)
adamc@706 1121
adamc@706 1122 case (c1, c2) of
adamc@706 1123 (L'.CError, _) => ()
adamc@706 1124 | (_, L'.CError) => ()
adamc@403 1125
adam@1303 1126 | (L'.CUnif (nl1, loc1, k1, _, r1), L'.CUnif (nl2, loc2, k2, _, r2)) =>
adam@1303 1127 if r1 = r2 andalso nl1 = nl2 then
adamc@705 1128 ()
adam@1303 1129 else if nl1 = 0 then
adamc@705 1130 (unifyKinds env k1 k2;
adamc@705 1131 r1 := SOME c2All)
adam@1303 1132 else if nl2 = 0 then
adam@1303 1133 (unifyKinds env k1 k2;
adam@1305 1134 r2 := SOME c1All)
adam@1303 1135 else
adam@1303 1136 err (fn _ => TooLifty (loc1, loc2))
adam@1303 1137
adam@1303 1138 | (L'.CUnif (0, _, _, _, r), _) =>
adamc@705 1139 if occursCon r c2All then
adamc@705 1140 err COccursCheckFailed
adamc@705 1141 else
adamc@705 1142 r := SOME c2All
adam@1303 1143 | (_, L'.CUnif (0, _, _, _, r)) =>
adamc@705 1144 if occursCon r c1All then
adamc@705 1145 err COccursCheckFailed
adamc@705 1146 else
adamc@705 1147 r := SOME c1All
adamc@705 1148
adam@1304 1149 | (L'.CUnif (nl, _, _, _, r), _) =>
adam@1304 1150 if occursCon r c2All then
adam@1304 1151 err COccursCheckFailed
adam@1304 1152 else
adam@1304 1153 (r := SOME (squish nl c2All)
adam@1306 1154 handle CantSquish => err (fn _ => TooDeep))
adam@1304 1155 | (_, L'.CUnif (nl, _, _, _, r)) =>
adam@1304 1156 if occursCon r c1All then
adam@1304 1157 err COccursCheckFailed
adam@1304 1158 else
adam@1304 1159 (r := SOME (squish nl c1All)
adam@1306 1160 handle CantSquish => err (fn _ => TooDeep))
adam@1304 1161
adam@1341 1162 | (L'.CRecord _, _) => isRecord ()
adam@1341 1163 | (_, L'.CRecord _) => isRecord ()
adam@1341 1164 | (L'.CConcat _, _) => isRecord ()
adam@1341 1165 | (_, L'.CConcat _) => isRecord ()
adam@1341 1166
adam@1341 1167
adamc@705 1168 | (L'.CUnit, L'.CUnit) => ()
adamc@705 1169
adamc@705 1170 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@987 1171 (unifyCons' env loc d1 d2;
adamc@987 1172 unifyCons' env loc r1 r2)
adamc@705 1173 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@705 1174 if expl1 <> expl2 then
adamc@705 1175 err CExplicitness
adamc@705 1176 else
adamc@705 1177 (unifyKinds env d1 d2;
adamc@705 1178 let
adamc@705 1179 (*val befor = Time.now ()*)
adamc@705 1180 val env' = E.pushCRel env x1 d1
adamc@705 1181 in
adamc@705 1182 (*TextIO.print ("E.pushCRel: "
adamc@705 1183 ^ LargeReal.toString (Time.toReal (Time.- (Time.now (), befor)))
adamc@705 1184 ^ "\n");*)
adamc@987 1185 unifyCons' env' loc r1 r2
adamc@705 1186 end)
adamc@987 1187 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env loc r1 r2
adamc@705 1188 | (L'.TDisjoint (c1, d1, e1), L'.TDisjoint (c2, d2, e2)) =>
adamc@987 1189 (unifyCons' env loc c1 c2;
adamc@987 1190 unifyCons' env loc d1 d2;
adamc@987 1191 unifyCons' env loc e1 e2)
adamc@705 1192
adamc@705 1193 | (L'.CRel n1, L'.CRel n2) =>
adamc@705 1194 if n1 = n2 then
adamc@705 1195 ()
adamc@705 1196 else
adamc@705 1197 err CIncompatible
adamc@705 1198 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@705 1199 if n1 = n2 then
adamc@705 1200 ()
adamc@705 1201 else
adamc@705 1202 err CIncompatible
adamc@705 1203
adamc@705 1204 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@987 1205 (unifyCons' env loc d1 d2;
adamc@987 1206 unifyCons' env loc r1 r2)
adamc@705 1207 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@705 1208 (unifyKinds env k1 k2;
adamc@987 1209 unifyCons' (E.pushCRel env x1 k1) loc c1 c2)
adamc@705 1210
adamc@705 1211 | (L'.CName n1, L'.CName n2) =>
adamc@705 1212 if n1 = n2 then
adamc@705 1213 ()
adamc@705 1214 else
adamc@705 1215 err CIncompatible
adamc@705 1216
adamc@705 1217 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@705 1218 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@705 1219 ()
adamc@705 1220 else
adamc@705 1221 err CIncompatible
adamc@705 1222
adamc@705 1223 | (L'.CTuple cs1, L'.CTuple cs2) =>
adamc@987 1224 ((ListPair.appEq (fn (c1, c2) => unifyCons' env loc c1 c2) (cs1, cs2))
adamc@705 1225 handle ListPair.UnequalLengths => err CIncompatible)
adamc@705 1226
adamc@706 1227 | (L'.CProj (c1, n1), _) => projSpecial1 (c1, n1, fn () => err CIncompatible)
adamc@706 1228 | (_, L'.CProj (c2, n2)) => projSpecial2 (c2, n2, fn () => err CIncompatible)
adamc@706 1229
adamc@623 1230 | (L'.CMap (dom1, ran1), L'.CMap (dom2, ran2)) =>
adamc@623 1231 (unifyKinds env dom1 dom2;
adamc@628 1232 unifyKinds env ran1 ran2)
adamc@623 1233
adamc@623 1234 | (L'.CKAbs (x, c1), L'.CKAbs (_, c2)) =>
adamc@987 1235 unifyCons' (E.pushKRel env x) loc c1 c2
adamc@623 1236 | (L'.CKApp (c1, k1), L'.CKApp (c2, k2)) =>
adamc@623 1237 (unifyKinds env k1 k2;
adamc@987 1238 unifyCons' env loc c1 c2)
adamc@623 1239 | (L'.TKFun (x, c1), L'.TKFun (_, c2)) =>
adamc@987 1240 unifyCons' (E.pushKRel env x) loc c1 c2
adamc@628 1241
adamc@403 1242 | _ => err CIncompatible
adamc@403 1243 end
adamc@403 1244
adamc@987 1245 and unifyCons env loc c1 c2 =
adamc@987 1246 unifyCons' env loc c1 c2
adamc@403 1247 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@403 1248 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@403 1249
adamc@628 1250 fun checkCon env e c1 c2 =
adamc@987 1251 unifyCons env (#2 e) c1 c2
adamc@403 1252 handle CUnify (c1, c2, err) =>
adamc@628 1253 expError env (Unify (e, c1, c2, err))
adamc@628 1254
adamc@628 1255 fun checkPatCon env p c1 c2 =
adamc@987 1256 unifyCons env (#2 p) c1 c2
adamc@403 1257 handle CUnify (c1, c2, err) =>
adamc@628 1258 expError env (PatUnify (p, c1, c2, err))
adamc@71 1259
adamc@403 1260 fun primType env p =
adamc@403 1261 case p of
adamc@403 1262 P.Int _ => !int
adamc@403 1263 | P.Float _ => !float
adamc@403 1264 | P.String _ => !string
adamc@821 1265 | P.Char _ => !char
adamc@623 1266
adamc@403 1267 datatype constraint =
adamc@403 1268 Disjoint of D.goal
adamc@403 1269 | TypeClass of E.env * L'.con * L'.exp option ref * ErrorMsg.span
adamc@403 1270
adam@1591 1271 fun relocConstraint loc c =
adam@1591 1272 case c of
adam@1591 1273 Disjoint (_, a, b, c, d) => Disjoint (loc, a, b, c, d)
adam@1591 1274 | TypeClass (a, b, c, _) => TypeClass (a, b, c, loc)
adam@1591 1275
adamc@403 1276 val enD = map Disjoint
adamc@403 1277
adamc@634 1278 fun isClassOrFolder env cl =
adamc@634 1279 E.isClass env cl
adamc@634 1280 orelse case hnormCon env cl of
adamc@634 1281 (L'.CKApp (cl, _), _) =>
adamc@634 1282 (case hnormCon env cl of
adamc@634 1283 (L'.CModProj (top_n, [], "folder"), _) => top_n = !top_r
adamc@634 1284 | _ => false)
adamc@634 1285 | _ => false
adamc@634 1286
adam@1303 1287 fun subConInCon env x y =
adam@1303 1288 ElabOps.subConInCon x y
adam@1303 1289 handle SubUnif => (cunifyError env (TooUnify (#2 x, y));
adam@1303 1290 cerror)
adam@1303 1291
adamc@629 1292 fun elabHead (env, denv) infer (e as (_, loc)) t =
adamc@403 1293 let
adamc@898 1294 fun unravelKind (t, e) =
adamc@898 1295 case hnormCon env t of
adamc@898 1296 (L'.TKFun (x, t'), _) =>
adamc@898 1297 let
adamc@898 1298 val u = kunif loc
adamc@898 1299
adamc@898 1300 val t'' = subKindInCon (0, u) t'
adamc@898 1301 in
adamc@898 1302 unravelKind (t'', (L'.EKApp (e, u), loc))
adamc@898 1303 end
adamc@898 1304 | t => (e, t, [])
adamc@898 1305
adamc@403 1306 fun unravel (t, e) =
adamc@628 1307 case hnormCon env t of
adamc@628 1308 (L'.TKFun (x, t'), _) =>
adamc@628 1309 let
adamc@628 1310 val u = kunif loc
adamc@628 1311
adamc@628 1312 val t'' = subKindInCon (0, u) t'
adamc@628 1313 in
adamc@628 1314 unravel (t'', (L'.EKApp (e, u), loc))
adamc@628 1315 end
adamc@628 1316 | (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@628 1317 let
adamc@628 1318 val u = cunif (loc, k)
adamc@628 1319
adam@1303 1320 val t'' = subConInCon env (0, u) t'
adamc@628 1321 in
adamc@628 1322 unravel (t'', (L'.ECApp (e, u), loc))
adamc@628 1323 end
adamc@633 1324 | (L'.TFun (dom, ran), _) =>
adamc@628 1325 let
adamc@633 1326 fun default () = (e, t, [])
adamc@750 1327
adamc@750 1328 fun isInstance () =
adamc@750 1329 if infer <> L.TypesOnly then
adamc@750 1330 let
adamc@750 1331 val r = ref NONE
adamc@750 1332 val (e, t, gs) = unravel (ran, (L'.EApp (e, (L'.EUnif r, loc)), loc))
adamc@750 1333 in
adamc@750 1334 (e, t, TypeClass (env, dom, r, loc) :: gs)
adamc@750 1335 end
adamc@750 1336 else
adamc@750 1337 default ()
adamc@750 1338
adamc@750 1339 fun hasInstance c =
adamc@753 1340 case hnormCon env c of
adamc@753 1341 (L'.TRecord c, _) => U.Con.exists {kind = fn _ => false,
adamc@753 1342 con = fn c =>
adamc@753 1343 E.isClass env (hnormCon env (c, loc))} c
adamc@753 1344 | c =>
adamc@750 1345 let
adamc@753 1346 fun findHead c =
adamc@753 1347 case #1 c of
adamc@753 1348 L'.CApp (f, _) => findHead f
adamc@753 1349 | _ => c
adamc@753 1350
adamc@753 1351 val cl = hnormCon env (findHead c)
adamc@750 1352 in
adamc@750 1353 isClassOrFolder env cl
adamc@750 1354 end
adamc@628 1355 in
adamc@750 1356 if hasInstance dom then
adamc@750 1357 isInstance ()
adamc@750 1358 else
adamc@750 1359 default ()
adamc@628 1360 end
adamc@629 1361 | (L'.TDisjoint (r1, r2, t'), loc) =>
adamc@629 1362 if infer <> L.TypesOnly then
adamc@629 1363 let
adamc@792 1364 val gs = D.prove env denv (r1, r2, #2 e)
adamc@629 1365 val (e, t, gs') = unravel (t', e)
adamc@629 1366 in
adamc@629 1367 (e, t, enD gs @ gs')
adamc@629 1368 end
adamc@629 1369 else
adamc@629 1370 (e, t, [])
adamc@628 1371 | t => (e, t, [])
adam@1591 1372
adam@1591 1373 val (e, t, gs) = case infer of
adam@1591 1374 L.DontInfer => unravelKind (t, e)
adam@1591 1375 | _ => unravel (t, e)
adamc@704 1376 in
adam@1591 1377 ((#1 e, loc), (#1 t, loc), map (relocConstraint loc) gs)
adamc@704 1378 end
adamc@15 1379
adamc@628 1380 fun elabPat (pAll as (p, loc), (env, bound)) =
adamc@171 1381 let
adamc@171 1382 val perror = (L'.PWild, loc)
adamc@171 1383 val terror = (L'.CError, loc)
adamc@171 1384 val pterror = (perror, terror)
adamc@171 1385 val rerror = (pterror, (env, bound))
adamc@171 1386
adamc@191 1387 fun pcon (pc, po, xs, to, dn, dk) =
adamc@188 1388 case (po, to) of
adamc@188 1389 (NONE, SOME _) => (expError env (PatHasNoArg loc);
adamc@188 1390 rerror)
adamc@188 1391 | (SOME _, NONE) => (expError env (PatHasArg loc);
adamc@188 1392 rerror)
adamc@191 1393 | (NONE, NONE) =>
adamc@191 1394 let
adamc@191 1395 val k = (L'.KType, loc)
adamc@191 1396 val unifs = map (fn _ => cunif (loc, k)) xs
adamc@191 1397 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
adamc@191 1398 in
adamc@191 1399 (((L'.PCon (dk, pc, unifs, NONE), loc), dn),
adamc@191 1400 (env, bound))
adamc@191 1401 end
adamc@188 1402 | (SOME p, SOME t) =>
adamc@188 1403 let
adamc@628 1404 val ((p', pt), (env, bound)) = elabPat (p, (env, bound))
adamc@191 1405
adamc@191 1406 val k = (L'.KType, loc)
adamc@191 1407 val unifs = map (fn _ => cunif (loc, k)) xs
adamc@194 1408 val nxs = length unifs - 1
adam@1304 1409 val t = ListUtil.foldli (fn (i, u, t) => subConInCon env (nxs - i,
adam@1304 1410 E.mliftConInCon (nxs - i) u) t) t unifs
adamc@191 1411 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
adamc@188 1412 in
adamc@628 1413 ignore (checkPatCon env p' pt t);
adamc@191 1414 (((L'.PCon (dk, pc, unifs, SOME p'), loc), dn),
adamc@188 1415 (env, bound))
adamc@188 1416 end
adamc@171 1417 in
adamc@171 1418 case p of
adamc@171 1419 L.PWild => (((L'.PWild, loc), cunif (loc, (L'.KType, loc))),
adamc@171 1420 (env, bound))
adamc@171 1421 | L.PVar x =>
adamc@171 1422 let
adamc@171 1423 val t = if SS.member (bound, x) then
adamc@171 1424 (expError env (DuplicatePatternVariable (loc, x));
adamc@171 1425 terror)
adamc@171 1426 else
adamc@171 1427 cunif (loc, (L'.KType, loc))
adamc@171 1428 in
adamc@182 1429 (((L'.PVar (x, t), loc), t),
adamc@171 1430 (E.pushERel env x t, SS.add (bound, x)))
adamc@171 1431 end
adamc@173 1432 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
adamc@173 1433 (env, bound))
adamc@171 1434 | L.PCon ([], x, po) =>
adamc@171 1435 (case E.lookupConstructor env x of
adamc@174 1436 NONE => (expError env (UnboundConstructor (loc, [], x));
adamc@171 1437 rerror)
adamc@191 1438 | SOME (dk, n, xs, to, dn) => pcon (L'.PConVar n, po, xs, to, (L'.CNamed dn, loc), dk))
adamc@174 1439 | L.PCon (m1 :: ms, x, po) =>
adamc@174 1440 (case E.lookupStr env m1 of
adamc@174 1441 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@174 1442 rerror)
adamc@174 1443 | SOME (n, sgn) =>
adamc@174 1444 let
adamc@174 1445 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@174 1446 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@182 1447 NONE => raise Fail "elabPat: Unknown substructure"
adamc@174 1448 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@174 1449 ((L'.StrVar n, loc), sgn) ms
adamc@174 1450 in
adamc@174 1451 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 1452 NONE => (expError env (UnboundConstructor (loc, m1 :: ms, x));
adamc@174 1453 rerror)
adamc@191 1454 | SOME (dk, _, xs, to, dn) => pcon (L'.PConProj (n, ms, x), po, xs, to, dn, dk)
adamc@174 1455 end)
adamc@174 1456
adamc@175 1457 | L.PRecord (xps, flex) =>
adamc@175 1458 let
adamc@175 1459 val (xpts, (env, bound, _)) =
adamc@175 1460 ListUtil.foldlMap (fn ((x, p), (env, bound, fbound)) =>
adamc@175 1461 let
adamc@628 1462 val ((p', t), (env, bound)) = elabPat (p, (env, bound))
adamc@175 1463 in
adamc@175 1464 if SS.member (fbound, x) then
adamc@175 1465 expError env (DuplicatePatField (loc, x))
adamc@175 1466 else
adamc@175 1467 ();
adamc@175 1468 ((x, p', t), (env, bound, SS.add (fbound, x)))
adamc@175 1469 end)
adamc@175 1470 (env, bound, SS.empty) xps
adamc@175 1471
adamc@175 1472 val k = (L'.KType, loc)
adamc@175 1473 val c = (L'.CRecord (k, map (fn (x, _, t) => ((L'.CName x, loc), t)) xpts), loc)
adamc@176 1474 val c =
adamc@175 1475 if flex then
adamc@176 1476 (L'.CConcat (c, cunif (loc, (L'.KRecord k, loc))), loc)
adamc@175 1477 else
adamc@176 1478 c
adamc@175 1479 in
adamc@182 1480 (((L'.PRecord xpts, loc),
adamc@175 1481 (L'.TRecord c, loc)),
adamc@175 1482 (env, bound))
adamc@175 1483 end
adamc@822 1484
adamc@822 1485 | L.PAnnot (p, t) =>
adamc@822 1486 let
adamc@822 1487 val ((p', pt), (env, bound)) = elabPat (p, (env, bound))
adamc@822 1488 val (t', k, _) = elabCon (env, D.empty) t
adamc@822 1489 in
adamc@822 1490 checkPatCon env p' pt t';
adamc@822 1491 ((p', t'), (env, bound))
adamc@822 1492 end
adamc@171 1493 end
adamc@171 1494
adamc@819 1495 (* This exhaustiveness checking follows Luc Maranget's paper "Warnings for pattern matching." *)
adamc@819 1496 fun exhaustive (env, t, ps, loc) =
adamc@819 1497 let
adamc@819 1498 fun fail n = raise Fail ("Elaborate.exhaustive: Impossible " ^ Int.toString n)
adamc@819 1499
adamc@819 1500 fun patConNum pc =
adamc@819 1501 case pc of
adamc@819 1502 L'.PConVar n => n
adamc@819 1503 | L'.PConProj (m1, ms, x) =>
adamc@819 1504 let
adamc@819 1505 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@819 1506 in
adamc@819 1507 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@819 1508 NONE => raise Fail "exhaustive: Can't project datatype"
adamc@819 1509 | SOME (_, n, _, _, _) => n
adamc@819 1510 end
adamc@819 1511
adamc@819 1512 fun nameOfNum (t, n) =
adamc@819 1513 case t of
adamc@819 1514 L'.CModProj (m1, ms, x) =>
adamc@819 1515 let
adamc@819 1516 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@819 1517 in
adamc@819 1518 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
adamc@819 1519 NONE => raise Fail "exhaustive: Can't project datatype"
adamc@819 1520 | SOME (_, cons) =>
adamc@819 1521 case ListUtil.search (fn (name, n', _) =>
adamc@819 1522 if n' = n then
adamc@819 1523 SOME name
adamc@819 1524 else
adamc@819 1525 NONE) cons of
adamc@819 1526 NONE => fail 9
adamc@819 1527 | SOME name => L'.PConProj (m1, ms, name)
adamc@819 1528 end
adamc@819 1529 | _ => L'.PConVar n
adamc@819 1530
adamc@819 1531 fun S (args, c, P) =
adamc@819 1532 List.mapPartial
adamc@819 1533 (fn [] => fail 1
adamc@819 1534 | p1 :: ps =>
adamc@819 1535 let
adamc@819 1536 val loc = #2 p1
adamc@819 1537
adamc@819 1538 fun wild () =
adamc@819 1539 SOME (map (fn _ => (L'.PWild, loc)) args @ ps)
adamc@819 1540 in
adamc@819 1541 case #1 p1 of
adamc@819 1542 L'.PPrim _ => NONE
adamc@819 1543 | L'.PCon (_, c', _, NONE) =>
adamc@819 1544 if patConNum c' = c then
adamc@819 1545 SOME ps
adamc@819 1546 else
adamc@819 1547 NONE
adamc@819 1548 | L'.PCon (_, c', _, SOME p) =>
adamc@819 1549 if patConNum c' = c then
adamc@819 1550 SOME (p :: ps)
adamc@819 1551 else
adamc@819 1552 NONE
adamc@819 1553 | L'.PRecord xpts =>
adamc@819 1554 SOME (map (fn x =>
adamc@819 1555 case ListUtil.search (fn (x', p, _) =>
adamc@819 1556 if x = x' then
adamc@819 1557 SOME p
adamc@819 1558 else
adamc@819 1559 NONE) xpts of
adamc@819 1560 NONE => (L'.PWild, loc)
adamc@819 1561 | SOME p => p) args @ ps)
adamc@819 1562 | L'.PWild => wild ()
adamc@819 1563 | L'.PVar _ => wild ()
adamc@819 1564 end)
adamc@819 1565 P
adamc@819 1566
adamc@819 1567 fun D P =
adamc@819 1568 List.mapPartial
adamc@819 1569 (fn [] => fail 2
adamc@819 1570 | (p1, _) :: ps =>
adamc@819 1571 case p1 of
adamc@819 1572 L'.PWild => SOME ps
adamc@819 1573 | L'.PVar _ => SOME ps
adamc@819 1574 | L'.PPrim _ => NONE
adamc@819 1575 | L'.PCon _ => NONE
adamc@819 1576 | L'.PRecord _ => NONE)
adamc@819 1577 P
adamc@819 1578
adamc@819 1579 fun I (P, q) =
adamc@819 1580 (*(prefaces "I" [("P", p_list (fn P' => box [PD.string "[", p_list (p_pat env) P', PD.string "]"]) P),
adamc@819 1581 ("q", p_list (p_con env) q)];*)
adamc@819 1582 case q of
adamc@819 1583 [] => (case P of
adamc@819 1584 [] => SOME []
adamc@819 1585 | _ => NONE)
adamc@819 1586 | q1 :: qs =>
adamc@819 1587 let
adamc@819 1588 val loc = #2 q1
adamc@819 1589
adamc@819 1590 fun unapp (t, acc) =
adamc@830 1591 case #1 t of
adamc@830 1592 L'.CApp (t, arg) => unapp (t, arg :: acc)
adamc@819 1593 | _ => (t, rev acc)
adamc@819 1594
adamc@830 1595 val (t1, args) = unapp (hnormCon env q1, [])
adamc@830 1596 val t1 = hnormCon env t1
adam@1303 1597 fun doSub t = foldl (fn (arg, t) => subConInCon env (0, arg) t) t args
adamc@819 1598
adamc@819 1599 fun dtype (dtO, names) =
adamc@819 1600 let
adamc@819 1601 val nameSet = IS.addList (IS.empty, names)
adamc@819 1602 val nameSet = foldl (fn (ps, nameSet) =>
adamc@819 1603 case ps of
adamc@819 1604 [] => fail 4
adamc@819 1605 | (L'.PCon (_, pc, _, _), _) :: _ =>
adamc@819 1606 (IS.delete (nameSet, patConNum pc)
adamc@819 1607 handle NotFound => nameSet)
adamc@819 1608 | _ => nameSet)
adamc@819 1609 nameSet P
adamc@819 1610 in
adamc@819 1611 nameSet
adamc@819 1612 end
adamc@819 1613
adamc@819 1614 fun default () = (NONE, IS.singleton 0, [])
adamc@819 1615
adamc@819 1616 val (dtO, unused, cons) =
adamc@830 1617 case #1 t1 of
adamc@819 1618 L'.CNamed n =>
adamc@819 1619 let
adamc@819 1620 val dt = E.lookupDatatype env n
adamc@819 1621 val cons = E.constructors dt
adamc@819 1622 in
adamc@819 1623 (SOME dt,
adamc@819 1624 dtype (SOME dt, map #2 cons),
adamc@819 1625 map (fn (_, n, co) =>
adamc@819 1626 (n,
adamc@819 1627 case co of
adamc@819 1628 NONE => []
adamc@819 1629 | SOME t => [("", doSub t)])) cons)
adamc@819 1630 end
adamc@819 1631 | L'.CModProj (m1, ms, x) =>
adamc@819 1632 let
adamc@819 1633 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@819 1634 in
adamc@819 1635 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
adamc@819 1636 NONE => default ()
adamc@819 1637 | SOME (_, cons) =>
adamc@819 1638 (NONE,
adamc@819 1639 dtype (NONE, map #2 cons),
adamc@819 1640 map (fn (s, _, co) =>
adamc@819 1641 (patConNum (L'.PConProj (m1, ms, s)),
adamc@819 1642 case co of
adamc@819 1643 NONE => []
adamc@819 1644 | SOME t => [("", doSub t)])) cons)
adamc@819 1645 end
adamc@830 1646 | L'.TRecord t =>
adamc@830 1647 (case #1 (hnormCon env t) of
adamc@830 1648 L'.CRecord (_, xts) =>
adamc@830 1649 let
adamc@830 1650 val xts = map (fn ((L'.CName x, _), co) => SOME (x, co)
adamc@830 1651 | _ => NONE) xts
adamc@830 1652 in
adamc@830 1653 if List.all Option.isSome xts then
adamc@830 1654 let
adamc@830 1655 val xts = List.mapPartial (fn x => x) xts
adamc@830 1656 val xts = ListMergeSort.sort (fn ((x1, _), (x2, _)) =>
adamc@830 1657 String.compare (x1, x2) = GREATER) xts
adamc@830 1658 in
adamc@830 1659 (NONE, IS.empty, [(0, xts)])
adamc@830 1660 end
adamc@830 1661 else
adamc@830 1662 default ()
adamc@830 1663 end
adamc@830 1664 | _ => default ())
adamc@819 1665 | _ => default ()
adamc@819 1666 in
adamc@819 1667 if IS.isEmpty unused then
adamc@819 1668 let
adamc@819 1669 fun recurse cons =
adamc@819 1670 case cons of
adamc@819 1671 [] => NONE
adamc@819 1672 | (name, args) :: cons =>
adamc@819 1673 case I (S (map #1 args, name, P),
adamc@819 1674 map #2 args @ qs) of
adamc@819 1675 NONE => recurse cons
adamc@819 1676 | SOME ps =>
adamc@819 1677 let
adamc@819 1678 val nargs = length args
adamc@819 1679 val argPs = List.take (ps, nargs)
adamc@819 1680 val restPs = List.drop (ps, nargs)
adamc@819 1681
adamc@819 1682 val p = case name of
adamc@819 1683 0 => L'.PRecord (ListPair.map
adamc@819 1684 (fn ((name, t), p) => (name, p, t))
adamc@819 1685 (args, argPs))
adamc@830 1686 | _ => L'.PCon (L'.Default, nameOfNum (#1 t1, name), [],
adamc@819 1687 case argPs of
adamc@819 1688 [] => NONE
adamc@819 1689 | [p] => SOME p
adamc@819 1690 | _ => fail 3)
adamc@819 1691 in
adamc@819 1692 SOME ((p, loc) :: restPs)
adamc@819 1693 end
adamc@819 1694 in
adamc@819 1695 recurse cons
adamc@819 1696 end
adamc@819 1697 else
adamc@819 1698 case I (D P, qs) of
adamc@819 1699 NONE => NONE
adamc@819 1700 | SOME ps =>
adamc@819 1701 let
adamc@819 1702 val p = case cons of
adamc@819 1703 [] => L'.PWild
adamc@819 1704 | (0, _) :: _ => L'.PWild
adamc@819 1705 | _ =>
adamc@819 1706 case IS.find (fn _ => true) unused of
adamc@819 1707 NONE => fail 6
adamc@819 1708 | SOME name =>
adamc@819 1709 case ListUtil.search (fn (name', args) =>
adamc@819 1710 if name = name' then
adamc@819 1711 SOME (name', args)
adamc@819 1712 else
adamc@819 1713 NONE) cons of
adamc@819 1714 SOME (n, []) =>
adamc@830 1715 L'.PCon (L'.Default, nameOfNum (#1 t1, n), [], NONE)
adamc@819 1716 | SOME (n, [_]) =>
adamc@830 1717 L'.PCon (L'.Default, nameOfNum (#1 t1, n), [], SOME (L'.PWild, loc))
adamc@819 1718 | _ => fail 7
adamc@819 1719 in
adamc@819 1720 SOME ((p, loc) :: ps)
adamc@819 1721 end
adamc@819 1722 end
adamc@819 1723 in
adamc@819 1724 case I (map (fn x => [x]) ps, [t]) of
adamc@819 1725 NONE => NONE
adamc@819 1726 | SOME [p] => SOME p
adamc@819 1727 | _ => fail 7
adamc@819 1728 end
adamc@819 1729
adamc@217 1730 fun unmodCon env (c, loc) =
adamc@217 1731 case c of
adamc@217 1732 L'.CNamed n =>
adamc@217 1733 (case E.lookupCNamed env n of
adamc@217 1734 (_, _, SOME (c as (L'.CModProj _, _))) => unmodCon env c
adamc@217 1735 | _ => (c, loc))
adamc@217 1736 | L'.CModProj (m1, ms, x) =>
adamc@217 1737 let
adamc@217 1738 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@217 1739 in
adamc@217 1740 case E.projectCon env {str = str, sgn = sgn, field = x} of
adamc@217 1741 NONE => raise Fail "unmodCon: Can't projectCon"
adamc@217 1742 | SOME (_, SOME (c as (L'.CModProj _, _))) => unmodCon env c
adamc@217 1743 | _ => (c, loc)
adamc@217 1744 end
adamc@217 1745 | _ => (c, loc)
adamc@217 1746
adamc@721 1747 fun normClassKey env c =
adamc@467 1748 let
adamc@721 1749 val c = hnormCon env c
adamc@467 1750 in
adamc@467 1751 case #1 c of
adamc@467 1752 L'.CApp (c1, c2) =>
adamc@467 1753 let
adamc@721 1754 val c1 = normClassKey env c1
adamc@721 1755 val c2 = normClassKey env c2
adamc@467 1756 in
adamc@467 1757 (L'.CApp (c1, c2), #2 c)
adamc@467 1758 end
adamc@721 1759 | L'.CRecord (k, xcs) => (L'.CRecord (k, map (fn (x, c) => (normClassKey env x,
adamc@721 1760 normClassKey env c)) xcs), #2 c)
adamc@721 1761 | _ => unmodCon env c
adamc@467 1762 end
adamc@467 1763
adamc@467 1764 fun normClassConstraint env (c, loc) =
adamc@217 1765 case c of
adamc@217 1766 L'.CApp (f, x) =>
adamc@217 1767 let
adamc@721 1768 val f = normClassKey env f
adamc@467 1769 val x = normClassKey env x
adamc@217 1770 in
adamc@467 1771 (L'.CApp (f, x), loc)
adamc@217 1772 end
adamc@674 1773 | L'.TFun (c1, c2) =>
adamc@674 1774 let
adamc@674 1775 val c1 = normClassConstraint env c1
adamc@674 1776 val c2 = normClassConstraint env c2
adamc@674 1777 in
adamc@674 1778 (L'.TFun (c1, c2), loc)
adamc@674 1779 end
adamc@674 1780 | L'.TCFun (expl, x, k, c1) => (L'.TCFun (expl, x, k, normClassConstraint env c1), loc)
adam@1303 1781 | L'.CUnif (nl, _, _, _, ref (SOME c)) => normClassConstraint env (E.mliftConInCon nl c)
adamc@721 1782 | _ => unmodCon env (c, loc)
adamc@216 1783
adamc@1093 1784 fun findHead e e' =
adamc@1093 1785 let
adamc@1093 1786 fun findHead (e, _) =
adamc@1093 1787 case e of
adamc@1093 1788 L.EVar (_, _, infer) =>
adamc@1093 1789 let
adamc@1093 1790 fun findHead' (e, _) =
adamc@1093 1791 case e of
adamc@1093 1792 L'.ENamed _ => true
adamc@1093 1793 | L'.EModProj _ => true
adam@1472 1794 | L'.ERel _ => true
adamc@1093 1795 | L'.EApp (e, _) => findHead' e
adamc@1093 1796 | L'.ECApp (e, _) => findHead' e
adamc@1093 1797 | L'.EKApp (e, _) => findHead' e
adamc@1093 1798 | _ => false
adamc@1093 1799 in
adamc@1093 1800 if findHead' e' then
adamc@1093 1801 SOME infer
adamc@1093 1802 else
adamc@1093 1803 NONE
adamc@1093 1804 end
adamc@1093 1805 | L.EApp (e, _) => findHead e
adamc@1093 1806 | L.ECApp (e, _) => findHead e
adamc@1093 1807 | L.EDisjointApp e => findHead e
adamc@1093 1808 | _ => NONE
adamc@1093 1809 in
adamc@1093 1810 findHead e
adamc@1093 1811 end
adamc@1093 1812
adam@1570 1813 datatype needed = Needed of {Cons : L'.kind SM.map,
adamc@1264 1814 Constraints : (E.env * (L'.con * L'.con) * ErrorMsg.span) list,
adamc@1264 1815 Vals : SS.set,
adamc@1270 1816 Mods : (E.env * needed) SM.map}
adamc@1264 1817
adamc@1264 1818 fun ncons (Needed r) = #Cons r
adamc@1264 1819 fun nconstraints (Needed r) = #Constraints r
adamc@1264 1820 fun nvals (Needed r) = #Vals r
adamc@1264 1821 fun nmods (Needed r) = #Mods r
adamc@1264 1822
adamc@1264 1823 val nempty = Needed {Cons = SM.empty,
adamc@1264 1824 Constraints = nil,
adamc@1264 1825 Vals = SS.empty,
adamc@1264 1826 Mods = SM.empty}
adamc@1264 1827
adamc@1264 1828 fun naddCon (r : needed, k, v) =
adamc@1264 1829 let
adamc@1264 1830 val Needed r = r
adamc@1264 1831 in
adamc@1264 1832 Needed {Cons = SM.insert (#Cons r, k, v),
adamc@1264 1833 Constraints = #Constraints r,
adamc@1264 1834 Vals = #Vals r,
adamc@1264 1835 Mods = #Mods r}
adamc@1264 1836 end
adamc@1264 1837
adamc@1264 1838 fun naddConstraint (r : needed, v) =
adamc@1264 1839 let
adamc@1264 1840 val Needed r = r
adamc@1264 1841 in
adamc@1264 1842 Needed {Cons = #Cons r,
adamc@1264 1843 Constraints = v :: #Constraints r,
adamc@1264 1844 Vals = #Vals r,
adamc@1264 1845 Mods = #Mods r}
adamc@1264 1846 end
adamc@1264 1847
adamc@1264 1848 fun naddVal (r : needed, k) =
adamc@1264 1849 let
adamc@1264 1850 val Needed r = r
adamc@1264 1851 in
adamc@1264 1852 Needed {Cons = #Cons r,
adamc@1264 1853 Constraints = #Constraints r,
adamc@1264 1854 Vals = SS.add (#Vals r, k),
adamc@1264 1855 Mods = #Mods r}
adamc@1264 1856 end
adamc@1264 1857
adamc@1264 1858 fun naddMod (r : needed, k, v) =
adamc@1264 1859 let
adamc@1264 1860 val Needed r = r
adamc@1264 1861 in
adamc@1264 1862 Needed {Cons = #Cons r,
adamc@1264 1863 Constraints = #Constraints r,
adamc@1264 1864 Vals = #Vals r,
adamc@1264 1865 Mods = SM.insert (#Mods r, k, v)}
adamc@1264 1866 end
adamc@1264 1867
adamc@1264 1868 fun ndelCon (r : needed, k) =
adamc@1264 1869 let
adamc@1264 1870 val Needed r = r
adamc@1264 1871 in
adamc@1264 1872 Needed {Cons = #1 (SM.remove (#Cons r, k)) handle NotFound => #Cons r,
adamc@1264 1873 Constraints = #Constraints r,
adamc@1264 1874 Vals = #Vals r,
adamc@1264 1875 Mods = #Mods r}
adamc@1264 1876 end
adamc@1264 1877
adamc@1264 1878 fun ndelVal (r : needed, k) =
adamc@1264 1879 let
adamc@1264 1880 val Needed r = r
adamc@1264 1881 in
adamc@1264 1882 Needed {Cons = #Cons r,
adamc@1264 1883 Constraints = #Constraints r,
adamc@1264 1884 Vals = SS.delete (#Vals r, k) handle NotFound => #Vals r,
adamc@1264 1885 Mods = #Mods r}
adamc@1264 1886 end
adamc@1264 1887
adam@1591 1888 fun chaseUnifs c =
adam@1591 1889 case #1 c of
adam@1591 1890 L'.CUnif (_, _, _, _, ref (SOME c)) => chaseUnifs c
adam@1591 1891 | _ => c
adam@1591 1892
adamc@92 1893 fun elabExp (env, denv) (eAll as (e, loc)) =
adamc@91 1894 let
adamc@848 1895 (*val () = eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)]*)
adamc@514 1896 (*val befor = Time.now ()*)
adamc@280 1897
adamc@280 1898 val r = case e of
adamc@91 1899 L.EAnnot (e, t) =>
adamc@91 1900 let
adamc@91 1901 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1902 val (t', _, gs2) = elabCon (env, denv) t
adamc@91 1903 in
adamc@628 1904 checkCon env e' et t';
adamc@628 1905 (e', t', gs1 @ enD gs2)
adamc@91 1906 end
adamc@34 1907
adamc@91 1908 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@403 1909 | L.EVar ([], s, infer) =>
adamc@91 1910 (case E.lookupE env s of
adamc@91 1911 E.NotBound =>
adamc@91 1912 (expError env (UnboundExp (loc, s));
adamc@91 1913 (eerror, cerror, []))
adamc@629 1914 | E.Rel (n, t) => elabHead (env, denv) infer (L'.ERel n, loc) t
adamc@629 1915 | E.Named (n, t) => elabHead (env, denv) infer (L'.ENamed n, loc) t)
adamc@403 1916 | L.EVar (m1 :: ms, s, infer) =>
adamc@91 1917 (case E.lookupStr env m1 of
adamc@91 1918 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@91 1919 (eerror, cerror, []))
adamc@91 1920 | SOME (n, sgn) =>
adamc@91 1921 let
adamc@91 1922 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@91 1923 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@91 1924 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@91 1925 (strerror, sgnerror))
adamc@91 1926 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@91 1927 ((L'.StrVar n, loc), sgn) ms
adamc@10 1928
adamc@91 1929 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@91 1930 NONE => (expError env (UnboundExp (loc, s));
adamc@91 1931 cerror)
adamc@91 1932 | SOME t => t
adamc@91 1933 in
adamc@629 1934 elabHead (env, denv) infer (L'.EModProj (n, ms, s), loc) t
adamc@91 1935 end)
adamc@86 1936
adamc@349 1937 | L.EWild =>
adamc@210 1938 let
adamc@349 1939 val r = ref NONE
adamc@349 1940 val c = cunif (loc, (L'.KType, loc))
adamc@210 1941 in
adamc@349 1942 ((L'.EUnif r, loc), c, [TypeClass (env, c, r, loc)])
adamc@210 1943 end
adamc@210 1944
adamc@91 1945 | L.EApp (e1, e2) =>
adamc@91 1946 let
adamc@91 1947 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@1093 1948
adamc@403 1949 val (e2', t2, gs2) = elabExp (env, denv) e2
adamc@71 1950
adamc@91 1951 val dom = cunif (loc, ktype)
adamc@91 1952 val ran = cunif (loc, ktype)
adamc@838 1953 val t = (L'.TFun (dom, ran), loc)
adamc@1093 1954
adamc@1093 1955 val () = checkCon env e1' t1 t
adamc@1093 1956 val () = checkCon env e2' t2 dom
adamc@1093 1957
adamc@1093 1958 val ef = (L'.EApp (e1', e2'), loc)
adamc@1093 1959 val (ef, et, gs3) =
adamc@1093 1960 case findHead e1 e1' of
adam@1591 1961 NONE => (ef, (#1 (chaseUnifs ran), loc), [])
adamc@1093 1962 | SOME infer => elabHead (env, denv) infer ef ran
adamc@91 1963 in
adamc@1093 1964 (ef, et, gs1 @ gs2 @ gs3)
adamc@91 1965 end
adamc@91 1966 | L.EAbs (x, to, e) =>
adamc@91 1967 let
adamc@91 1968 val (t', gs1) = case to of
adamc@91 1969 NONE => (cunif (loc, ktype), [])
adamc@91 1970 | SOME t =>
adamc@91 1971 let
adamc@91 1972 val (t', tk, gs) = elabCon (env, denv) t
adamc@91 1973 in
adamc@91 1974 checkKind env t' tk ktype;
adamc@91 1975 (t', gs)
adamc@91 1976 end
adamc@467 1977 val dom = normClassConstraint env t'
adamc@467 1978 val (e', et, gs2) = elabExp (E.pushERel env x dom, denv) e
adamc@91 1979 in
adamc@91 1980 ((L'.EAbs (x, t', et, e'), loc),
adamc@91 1981 (L'.TFun (t', et), loc),
adamc@467 1982 enD gs1 @ gs2)
adamc@91 1983 end
adamc@91 1984 | L.ECApp (e, c) =>
adamc@91 1985 let
adamc@91 1986 val (e', et, gs1) = elabExp (env, denv) e
adamc@1093 1987
adamc@328 1988 val oldEt = et
adamc@403 1989 val (c', ck, gs2) = elabCon (env, denv) c
adamc@628 1990 val (et', _) = hnormCon env et
adamc@91 1991 in
adamc@91 1992 case et' of
adamc@91 1993 L'.CError => (eerror, cerror, [])
adamc@328 1994 | L'.TCFun (_, x, k, eb) =>
adamc@91 1995 let
adamc@91 1996 val () = checkKind env c' ck k
adam@1303 1997 val eb' = subConInCon env (0, c') eb
adamc@1093 1998
adamc@1093 1999 val ef = (L'.ECApp (e', c'), loc)
adamc@1093 2000 val (ef, eb', gs3) =
adamc@1093 2001 case findHead e e' of
adamc@1093 2002 NONE => (ef, eb', [])
adamc@1093 2003 | SOME infer => elabHead (env, denv) infer ef eb'
adamc@91 2004 in
adamc@328 2005 (*prefaces "Elab ECApp" [("e", SourcePrint.p_exp eAll),
adamc@328 2006 ("et", p_con env oldEt),
adamc@328 2007 ("x", PD.string x),
adamc@328 2008 ("eb", p_con (E.pushCRel env x k) eb),
adamc@328 2009 ("c", p_con env c'),
adamc@328 2010 ("eb'", p_con env eb')];*)
adam@1591 2011 (ef, (#1 eb', loc), gs1 @ enD gs2 @ gs3)
adamc@91 2012 end
adamc@91 2013
adamc@91 2014 | _ =>
adamc@91 2015 (expError env (WrongForm ("constructor function", e', et));
adamc@91 2016 (eerror, cerror, []))
adamc@91 2017 end
adamc@91 2018 | L.ECAbs (expl, x, k, e) =>
adamc@91 2019 let
adamc@91 2020 val expl' = elabExplicitness expl
adamc@623 2021 val k' = elabKind env k
adamc@328 2022
adamc@328 2023 val env' = E.pushCRel env x k'
adamc@328 2024 val (e', et, gs) = elabExp (env', D.enter denv) e
adamc@91 2025 in
adamc@91 2026 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@91 2027 (L'.TCFun (expl', x, k', et), loc),
adamc@91 2028 gs)
adamc@91 2029 end
adamc@623 2030 | L.EKAbs (x, e) =>
adamc@623 2031 let
adamc@623 2032 val env' = E.pushKRel env x
adamc@623 2033 val (e', et, gs) = elabExp (env', denv) e
adamc@623 2034 in
adamc@623 2035 ((L'.EKAbs (x, e'), loc),
adamc@623 2036 (L'.TKFun (x, et), loc),
adamc@623 2037 gs)
adamc@623 2038 end
adamc@91 2039
adamc@91 2040 | L.EDisjoint (c1, c2, e) =>
adamc@91 2041 let
adamc@91 2042 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@91 2043 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@91 2044
adamc@91 2045 val ku1 = kunif loc
adamc@91 2046 val ku2 = kunif loc
adamc@91 2047
adamc@628 2048 val denv' = D.assert env denv (c1', c2')
adamc@628 2049 val (e', t, gs3) = elabExp (env, denv') e
adamc@91 2050 in
adamc@91 2051 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@91 2052 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@91 2053
adamc@628 2054 (e', (L'.TDisjoint (c1', c2', t), loc), enD gs1 @ enD gs2 @ gs3)
adamc@91 2055 end
adamc@629 2056 | L.EDisjointApp e =>
adamc@629 2057 let
adamc@629 2058 val (e', t, gs1) = elabExp (env, denv) e
adamc@629 2059
adamc@629 2060 val k1 = kunif loc
adamc@629 2061 val c1 = cunif (loc, (L'.KRecord k1, loc))
adamc@629 2062 val k2 = kunif loc
adamc@629 2063 val c2 = cunif (loc, (L'.KRecord k2, loc))
adamc@629 2064 val t' = cunif (loc, ktype)
adamc@629 2065 val () = checkCon env e' t (L'.TDisjoint (c1, c2, t'), loc)
adamc@629 2066 val gs2 = D.prove env denv (c1, c2, loc)
adamc@629 2067 in
adam@1591 2068 (e', (#1 (chaseUnifs t'), loc), enD gs2 @ gs1)
adamc@629 2069 end
adamc@91 2070
adamc@91 2071 | L.ERecord xes =>
adamc@91 2072 let
adamc@91 2073 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@91 2074 let
adamc@91 2075 val (x', xk, gs1) = elabCon (env, denv) x
adamc@91 2076 val (e', et, gs2) = elabExp (env, denv) e
adamc@91 2077 in
adamc@91 2078 checkKind env x' xk kname;
adamc@228 2079 ((x', e', et), enD gs1 @ gs2 @ gs)
adamc@91 2080 end)
adamc@91 2081 [] xes
adamc@91 2082
adamc@91 2083 val k = (L'.KType, loc)
adamc@91 2084
adamc@91 2085 fun prove (xets, gs) =
adamc@91 2086 case xets of
adamc@91 2087 [] => gs
adamc@91 2088 | (x, _, t) :: rest =>
adamc@91 2089 let
adamc@91 2090 val xc = (x, t)
adamc@91 2091 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@91 2092 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@91 2093 let
adamc@91 2094 val xc' = (x', t')
adamc@91 2095 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@91 2096 in
adamc@91 2097 D.prove env denv (r1, r2, loc) @ gs
adamc@91 2098 end)
adamc@91 2099 gs rest
adamc@91 2100 in
adamc@91 2101 prove (rest, gs)
adamc@91 2102 end
adamc@228 2103
adamc@228 2104 val gsD = List.mapPartial (fn Disjoint d => SOME d | _ => NONE) gs
adamc@228 2105 val gsO = List.filter (fn Disjoint _ => false | _ => true) gs
adamc@91 2106 in
adamc@255 2107 (*TextIO.print ("|gsO| = " ^ Int.toString (length gsO) ^ "\n");*)
adamc@91 2108 ((L'.ERecord xes', loc),
adamc@91 2109 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@228 2110 enD (prove (xes', gsD)) @ gsO)
adamc@91 2111 end
adamc@91 2112
adamc@91 2113 | L.EField (e, c) =>
adamc@91 2114 let
adamc@91 2115 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 2116 val (c', ck, gs2) = elabCon (env, denv) c
adamc@91 2117
adamc@91 2118 val ft = cunif (loc, ktype)
adamc@91 2119 val rest = cunif (loc, ktype_record)
adamc@91 2120 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@629 2121 val () = checkCon env e' et
adamc@629 2122 (L'.TRecord (L'.CConcat (first, rest), loc), loc);
adamc@628 2123 val gs3 = D.prove env denv (first, rest, loc)
adamc@91 2124 in
adamc@628 2125 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ enD gs2 @ enD gs3)
adamc@91 2126 end
adamc@91 2127
adamc@445 2128 | L.EConcat (e1, e2) =>
adamc@339 2129 let
adamc@339 2130 val (e1', e1t, gs1) = elabExp (env, denv) e1
adamc@445 2131 val (e2', e2t, gs2) = elabExp (env, denv) e2
adamc@445 2132
adamc@445 2133 val r1 = cunif (loc, ktype_record)
adamc@445 2134 val r2 = cunif (loc, ktype_record)
adamc@445 2135
adamc@629 2136 val () = checkCon env e1' e1t (L'.TRecord r1, loc)
adamc@629 2137 val () = checkCon env e2' e2t (L'.TRecord r2, loc)
adamc@629 2138
adamc@628 2139 val gs3 = D.prove env denv (r1, r2, loc)
adamc@339 2140 in
adamc@445 2141 ((L'.EConcat (e1', r1, e2', r2), loc),
adamc@445 2142 (L'.TRecord ((L'.CConcat (r1, r2), loc)), loc),
adamc@628 2143 gs1 @ gs2 @ enD gs3)
adamc@339 2144 end
adamc@149 2145 | L.ECut (e, c) =>
adamc@149 2146 let
adamc@149 2147 val (e', et, gs1) = elabExp (env, denv) e
adamc@149 2148 val (c', ck, gs2) = elabCon (env, denv) c
adamc@149 2149
adamc@149 2150 val ft = cunif (loc, ktype)
adamc@149 2151 val rest = cunif (loc, ktype_record)
adamc@149 2152 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@629 2153
adamc@629 2154 val () = checkCon env e' et
adamc@629 2155 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@149 2156
adamc@628 2157 val gs3 = D.prove env denv (first, rest, loc)
adamc@149 2158 in
adam@1299 2159 checkKind env c' ck kname;
adamc@228 2160 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc),
adamc@628 2161 gs1 @ enD gs2 @ enD gs3)
adamc@149 2162 end
adamc@493 2163 | L.ECutMulti (e, c) =>
adamc@493 2164 let
adamc@493 2165 val (e', et, gs1) = elabExp (env, denv) e
adamc@493 2166 val (c', ck, gs2) = elabCon (env, denv) c
adamc@493 2167
adamc@493 2168 val rest = cunif (loc, ktype_record)
adamc@629 2169
adamc@629 2170 val () = checkCon env e' et
adamc@629 2171 (L'.TRecord (L'.CConcat (c', rest), loc), loc)
adamc@493 2172
adamc@628 2173 val gs3 = D.prove env denv (c', rest, loc)
adamc@493 2174 in
adam@1299 2175 checkKind env c' ck (L'.KRecord ktype, loc);
adamc@493 2176 ((L'.ECutMulti (e', c', {rest = rest}), loc), (L'.TRecord rest, loc),
adamc@628 2177 gs1 @ enD gs2 @ enD gs3)
adamc@493 2178 end
adamc@149 2179
adamc@171 2180 | L.ECase (e, pes) =>
adamc@171 2181 let
adamc@171 2182 val (e', et, gs1) = elabExp (env, denv) e
adamc@171 2183 val result = cunif (loc, (L'.KType, loc))
adamc@171 2184 val (pes', gs) = ListUtil.foldlMap
adamc@171 2185 (fn ((p, e), gs) =>
adamc@171 2186 let
adamc@628 2187 val ((p', pt), (env, _)) = elabPat (p, (env, SS.empty))
adamc@628 2188
adamc@629 2189 val (e', et', gs1) = elabExp (env, denv) e
adamc@171 2190 in
adamc@628 2191 checkPatCon env p' pt et;
adamc@629 2192 checkCon env e' et' result;
adamc@628 2193 ((p', e'), gs1 @ gs)
adamc@171 2194 end)
adamc@171 2195 gs1 pes
adamc@171 2196 in
adamc@819 2197 case exhaustive (env, et, map #1 pes', loc) of
adamc@819 2198 NONE => ()
adamc@830 2199 | SOME p => if !mayDelay then
adamc@830 2200 delayedExhaustives := (env, et, map #1 pes', loc) :: !delayedExhaustives
adamc@830 2201 else
adamc@830 2202 expError env (Inexhaustive (loc, p));
adamc@172 2203
adamc@628 2204 ((L'.ECase (e', pes', {disc = et, result = result}), loc), result, gs)
adamc@171 2205 end
adamc@447 2206
adamc@447 2207 | L.ELet (eds, e) =>
adamc@447 2208 let
adamc@447 2209 val (eds, (env, gs1)) = ListUtil.foldlMap (elabEdecl denv) (env, []) eds
adamc@447 2210 val (e, t, gs2) = elabExp (env, denv) e
adamc@447 2211 in
adamc@825 2212 ((L'.ELet (eds, e, t), loc), t, gs1 @ gs2)
adamc@447 2213 end
adamc@280 2214 in
adamc@819 2215 (*prefaces "/elabExp" [("e", SourcePrint.p_exp eAll)];*)
adamc@280 2216 r
adamc@91 2217 end
adamc@31 2218
adamc@628 2219 and elabEdecl denv (dAll as (d, loc), (env, gs)) =
adamc@447 2220 let
adamc@447 2221 val r =
adamc@447 2222 case d of
adamc@825 2223 L.EDVal (p, e) =>
adamc@447 2224 let
adamc@825 2225 val ((p', pt), (env', _)) = elabPat (p, (env, SS.empty))
adamc@825 2226 val (e', et, gs1) = elabExp (env, denv) e
adamc@825 2227
adamc@825 2228 val () = checkCon env e' et pt
adamc@825 2229
adam@1554 2230 val env' = E.patBinds env p'
adam@1554 2231 (* Redo to get proper detection of type class witnesses *)
adam@1554 2232
adamc@825 2233 val pt = normClassConstraint env pt
adamc@447 2234 in
adamc@830 2235 case exhaustive (env, et, [p'], loc) of
adamc@830 2236 NONE => ()
adamc@830 2237 | SOME p => if !mayDelay then
adamc@830 2238 delayedExhaustives := (env, et, [p'], loc) :: !delayedExhaustives
adamc@830 2239 else
adamc@830 2240 expError env (Inexhaustive (loc, p));
adamc@830 2241
adamc@825 2242 ((L'.EDVal (p', pt, e'), loc), (env', gs1 @ gs))
adamc@447 2243 end
adamc@447 2244 | L.EDValRec vis =>
adamc@447 2245 let
adamc@447 2246 fun allowable (e, _) =
adamc@447 2247 case e of
adamc@447 2248 L.EAbs _ => true
adamc@447 2249 | L.ECAbs (_, _, _, e) => allowable e
adamc@623 2250 | L.EKAbs (_, e) => allowable e
adamc@447 2251 | L.EDisjoint (_, _, e) => allowable e
adamc@447 2252 | _ => false
adamc@447 2253
adamc@447 2254 val (vis, gs) = ListUtil.foldlMap
adamc@447 2255 (fn ((x, co, e), gs) =>
adamc@447 2256 let
adamc@447 2257 val (c', _, gs1) = case co of
adamc@447 2258 NONE => (cunif (loc, ktype), ktype, [])
adamc@447 2259 | SOME c => elabCon (env, denv) c
adamc@447 2260 in
adamc@447 2261 ((x, c', e), enD gs1 @ gs)
adamc@447 2262 end) gs vis
adamc@447 2263
adamc@447 2264 val env = foldl (fn ((x, c', _), env) => E.pushERel env x c') env vis
adamc@447 2265
adamc@447 2266 val (vis, gs) = ListUtil.foldlMap (fn ((x, c', e), gs) =>
adamc@447 2267 let
adamc@447 2268 val (e', et, gs1) = elabExp (env, denv) e
adamc@447 2269 in
adamc@628 2270 checkCon env e' et c';
adamc@447 2271 if allowable e then
adamc@447 2272 ()
adamc@447 2273 else
adamc@447 2274 expError env (IllegalRec (x, e'));
adamc@628 2275 ((x, c', e'), gs1 @ gs)
adamc@447 2276 end) gs vis
adamc@447 2277 in
adamc@447 2278 ((L'.EDValRec vis, loc), (env, gs))
adamc@447 2279 end
adamc@447 2280 in
adamc@447 2281 r
adamc@447 2282 end
adamc@447 2283
adamc@42 2284 val hnormSgn = E.hnormSgn
adamc@31 2285
adamc@210 2286 fun tableOf () = (L'.CModProj (!basis_r, [], "sql_table"), ErrorMsg.dummySpan)
adamc@338 2287 fun sequenceOf () = (L'.CModProj (!basis_r, [], "sql_sequence"), ErrorMsg.dummySpan)
adamc@754 2288 fun viewOf () = (L'.CModProj (!basis_r, [], "sql_view"), ErrorMsg.dummySpan)
adamc@754 2289 fun queryOf () = (L'.CModProj (!basis_r, [], "sql_query"), ErrorMsg.dummySpan)
adamc@459 2290 fun cookieOf () = (L'.CModProj (!basis_r, [], "http_cookie"), ErrorMsg.dummySpan)
adamc@718 2291 fun styleOf () = (L'.CModProj (!basis_r, [], "css_class"), ErrorMsg.dummySpan)
adamc@205 2292
adamc@469 2293 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@469 2294 case E.lookupStr env str of
adamc@469 2295 NONE => (strError env (UnboundStr (loc, str));
adamc@469 2296 denv)
adamc@469 2297 | SOME (n, sgn) =>
adamc@469 2298 let
adamc@469 2299 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@469 2300 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@469 2301 NONE => (strError env (UnboundStr (loc, m));
adamc@469 2302 (strerror, sgnerror))
adamc@469 2303 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@469 2304 ((L'.StrVar n, loc), sgn) strs
adamc@1030 2305
adamc@1030 2306 fun collect first (st, sgn) =
adamc@1030 2307 case E.projectConstraints env {sgn = sgn, str = st} of
adamc@1182 2308 NONE => []
adamc@1030 2309 | SOME cs =>
adamc@1030 2310 case #1 (hnormSgn env sgn) of
adamc@1030 2311 L'.SgnConst sgis =>
adamc@1030 2312 foldl (fn (sgi, cs) =>
adamc@1030 2313 case #1 sgi of
adamc@1030 2314 L'.SgiStr (x, _, _) =>
adamc@1030 2315 (case E.projectStr env {sgn = sgn, str = st, field = x} of
adamc@1030 2316 NONE => raise Fail "Elaborate: projectStr in collect"
adamc@1030 2317 | SOME sgn' =>
adamc@1030 2318 List.revAppend (collect false ((L'.StrProj (st, x), loc), sgn'),
adamc@1030 2319 cs))
adamc@1030 2320 | _ => cs) cs sgis
adamc@1030 2321 | _ => cs
adamc@469 2322 in
adamc@1030 2323 foldl (fn ((c1, c2), denv) =>
adamc@1030 2324 D.assert env denv (c1, c2)) denv (collect true (st, sgn))
adamc@469 2325 end
adamc@469 2326
adamc@88 2327 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@1270 2328 ((*Print.preface ("elabSgi", SourcePrint.p_sgn_item (sgi, loc));*)
adamc@1270 2329 case sgi of
adamc@1270 2330 L.SgiConAbs (x, k) =>
adamc@1270 2331 let
adamc@1270 2332 val k' = elabKind env k
adamc@1270 2333
adamc@1270 2334 val (env', n) = E.pushCNamed env x k' NONE
adamc@1270 2335 in
adamc@1270 2336 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@1270 2337 end
adamc@1270 2338
adamc@1270 2339 | L.SgiCon (x, ko, c) =>
adamc@1270 2340 let
adamc@1270 2341 val k' = case ko of
adamc@1270 2342 NONE => kunif loc
adamc@1270 2343 | SOME k => elabKind env k
adamc@1270 2344
adamc@1270 2345 val (c', ck, gs') = elabCon (env, denv) c
adamc@1270 2346 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@1270 2347 in
adamc@1270 2348 checkKind env c' ck k';
adamc@1270 2349
adamc@1270 2350 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@1270 2351 end
adamc@1270 2352
adamc@1270 2353 | L.SgiDatatype dts =>
adamc@1270 2354 let
adamc@1270 2355 val k = (L'.KType, loc)
adamc@1270 2356
adamc@1270 2357 val (dts, env) = ListUtil.foldlMap (fn ((x, xs, xcs), env) =>
adamc@1270 2358 let
adamc@1270 2359 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@1270 2360 val (env, n) = E.pushCNamed env x k' NONE
adamc@1270 2361 in
adamc@1270 2362 ((x, n, xs, xcs), env)
adamc@1270 2363 end)
adamc@1270 2364 env dts
adamc@1270 2365
adamc@1270 2366 val (dts, env) = ListUtil.foldlMap
adamc@1270 2367 (fn ((x, n, xs, xcs), env) =>
adamc@1270 2368 let
adamc@1270 2369 val t = (L'.CNamed n, loc)
adamc@1270 2370 val nxs = length xs - 1
adamc@1270 2371 val t = ListUtil.foldli (fn (i, _, t) =>
adamc@1270 2372 (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@1270 2373
adamc@1270 2374 val (env', denv') = foldl (fn (x, (env', denv')) =>
adamc@1270 2375 (E.pushCRel env' x k,
adamc@1270 2376 D.enter denv')) (env, denv) xs
adamc@1270 2377
adamc@1270 2378 val (xcs, (used, env, gs)) =
adamc@1270 2379 ListUtil.foldlMap
adamc@1270 2380 (fn ((x, to), (used, env, gs)) =>
adamc@1270 2381 let
adamc@1270 2382 val (to, t, gs') = case to of
adamc@1270 2383 NONE => (NONE, t, gs)
adamc@1270 2384 | SOME t' =>
adamc@1270 2385 let
adamc@1270 2386 val (t', tk, gs') =
adamc@1270 2387 elabCon (env', denv') t'
adamc@1270 2388 in
adamc@1270 2389 checkKind env' t' tk k;
adamc@1270 2390 (SOME t',
adamc@1270 2391 (L'.TFun (t', t), loc),
adamc@1270 2392 gs' @ gs)
adamc@1270 2393 end
adamc@1270 2394 val t = foldl (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@1270 2395 t xs
adamc@1270 2396
adamc@1270 2397 val (env, n') = E.pushENamed env x t
adamc@1270 2398 in
adamc@1270 2399 if SS.member (used, x) then
adamc@1270 2400 strError env (DuplicateConstructor (x, loc))
adamc@1270 2401 else
adamc@1270 2402 ();
adamc@1270 2403 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@1270 2404 end)
adamc@1270 2405 (SS.empty, env, []) xcs
adamc@1270 2406 in
adamc@1270 2407 ((x, n, xs, xcs), E.pushDatatype env n xs xcs)
adamc@1270 2408 end)
adamc@1270 2409 env dts
adamc@1270 2410 in
adamc@1270 2411 ([(L'.SgiDatatype dts, loc)], (env, denv, gs))
adamc@1270 2412 end
adamc@1270 2413
adamc@1270 2414 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
adamc@1270 2415
adamc@1270 2416 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
adamc@1270 2417 (case E.lookupStr env m1 of
adamc@1270 2418 NONE => (strError env (UnboundStr (loc, m1));
adamc@1270 2419 ([], (env, denv, gs)))
adamc@1270 2420 | SOME (n, sgn) =>
adamc@1270 2421 let
adamc@1270 2422 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@1270 2423 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@1270 2424 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@1270 2425 (strerror, sgnerror))
adamc@1270 2426 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@1270 2427 ((L'.StrVar n, loc), sgn) ms
adamc@1270 2428 in
adamc@1270 2429 case hnormCon env (L'.CModProj (n, ms, s), loc) of
adamc@1270 2430 (L'.CModProj (n, ms, s), _) =>
adamc@1270 2431 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@1270 2432 NONE => (conError env (UnboundDatatype (loc, s));
adamc@1270 2433 ([], (env, denv, [])))
adamc@1270 2434 | SOME (xs, xncs) =>
adamc@1270 2435 let
adamc@1270 2436 val k = (L'.KType, loc)
adamc@1270 2437 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@1270 2438
adamc@1270 2439 val t = (L'.CModProj (n, ms, s), loc)
adamc@1270 2440 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@1270 2441 val env = E.pushDatatype env n' xs xncs
adamc@1270 2442
adamc@1270 2443 val t = (L'.CNamed n', loc)
adamc@1270 2444 val env = foldl (fn ((x, n, to), env) =>
adamc@805 2445 let
adamc@1270 2446 val t = case to of
adamc@1270 2447 NONE => t
adamc@1270 2448 | SOME t' => (L'.TFun (t', t), loc)
adamc@1270 2449
adamc@1270 2450 val t = foldr (fn (x, t) =>
adamc@1270 2451 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@1270 2452 t xs
adamc@805 2453 in
adamc@1270 2454 E.pushENamedAs env x n t
adamc@1270 2455 end) env xncs
adamc@1270 2456 in
adamc@1270 2457 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, []))
adamc@1270 2458 end)
adamc@1270 2459 | _ => (strError env (NotDatatype loc);
adamc@1270 2460 ([], (env, denv, [])))
adamc@1270 2461 end)
adamc@1270 2462
adamc@1270 2463 | L.SgiVal (x, c) =>
adamc@1270 2464 let
adamc@1270 2465 val (c', ck, gs') = elabCon (env, denv) c
adamc@1270 2466
adamc@1270 2467 val old = c'
adamc@1270 2468 val c' = normClassConstraint env c'
adamc@1270 2469 val (env', n) = E.pushENamed env x c'
adamc@1270 2470 in
adamc@1270 2471 (unifyKinds env ck ktype
adamc@1270 2472 handle KUnify ue => strError env (NotType (loc, ck, ue)));
adamc@1270 2473
adamc@1270 2474 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@1270 2475 end
adamc@1270 2476
adamc@1270 2477 | L.SgiTable (x, c, pe, ce) =>
adamc@1270 2478 let
adamc@1270 2479 val cstK = (L'.KRecord (L'.KRecord (L'.KUnit, loc), loc), loc)
adamc@1270 2480
adamc@1270 2481 val (c', ck, gs') = elabCon (env, denv) c
adamc@1270 2482 val pkey = cunif (loc, cstK)
adamc@1270 2483 val visible = cunif (loc, cstK)
adamc@1270 2484 val (env', ds, uniques) =
adamc@1270 2485 case (#1 pe, #1 ce) of
adamc@1270 2486 (L.EVar (["Basis"], "no_primary_key", _), L.EVar (["Basis"], "no_constraint", _)) =>
adamc@1270 2487 let
adamc@1270 2488 val x' = x ^ "_hidden_constraints"
adamc@1270 2489 val (env', hidden_n) = E.pushCNamed env x' cstK NONE
adamc@1270 2490 val hidden = (L'.CNamed hidden_n, loc)
adamc@1270 2491 in
adamc@1270 2492 (env', [(L'.SgiConAbs (x', hidden_n, cstK), loc)], (L'.CConcat (visible, hidden), loc))
adamc@1270 2493 end
adamc@1270 2494 | _ => (env, [], visible)
adamc@1270 2495
adamc@1270 2496 val ct = tableOf ()
adamc@1270 2497 val ct = (L'.CApp (ct, c'), loc)
adamc@1270 2498 val ct = (L'.CApp (ct, (L'.CConcat (pkey, uniques), loc)), loc)
adamc@1270 2499
adamc@1270 2500 val (pe', pet, gs'') = elabExp (env', denv) pe
adamc@1270 2501 val gs'' = List.mapPartial (fn Disjoint x => SOME x
adamc@707 2502 | _ => NONE) gs''
adamc@707 2503
adamc@1270 2504 val pst = (L'.CModProj (!basis_r, [], "primary_key"), loc)
adamc@1270 2505 val pst = (L'.CApp (pst, c'), loc)
adamc@1270 2506 val pst = (L'.CApp (pst, pkey), loc)
adamc@1270 2507
adamc@1270 2508 val (env', n) = E.pushENamed env' x ct
adamc@1270 2509
adamc@1270 2510 val (ce', cet, gs''') = elabExp (env', denv) ce
adamc@1270 2511 val gs''' = List.mapPartial (fn Disjoint x => SOME x
adamc@1270 2512 | _ => NONE) gs'''
adamc@1270 2513
adamc@1270 2514 val cst = (L'.CModProj (!basis_r, [], "sql_constraints"), loc)
adamc@1270 2515 val cst = (L'.CApp (cst, c'), loc)
adamc@1270 2516 val cst = (L'.CApp (cst, visible), loc)
adamc@1270 2517 in
adamc@1270 2518 checkKind env c' ck (L'.KRecord (L'.KType, loc), loc);
adamc@1270 2519 checkCon env' pe' pet pst;
adamc@1270 2520 checkCon env' ce' cet cst;
adamc@1270 2521
adamc@1270 2522 (ds @ [(L'.SgiVal (x, n, ct), loc)], (env', denv, gs''' @ gs'' @ gs' @ gs))
adamc@1270 2523 end
adamc@1270 2524
adamc@1270 2525 | L.SgiStr (x, sgn) =>
adamc@1270 2526 let
adamc@1270 2527 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@1270 2528 val (env', n) = E.pushStrNamed env x sgn'
adamc@1270 2529 in
adamc@1270 2530 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@1270 2531 end
adamc@1270 2532
adamc@1270 2533 | L.SgiSgn (x, sgn) =>
adamc@1270 2534 let
adamc@1270 2535 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@1270 2536 val (env', n) = E.pushSgnNamed env x sgn'
adamc@1270 2537 in
adamc@1270 2538 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@1270 2539 end
adamc@1270 2540
adamc@1270 2541 | L.SgiInclude sgn =>
adamc@1270 2542 let
adamc@1270 2543 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@1270 2544 in
adamc@1270 2545 case #1 (hnormSgn env sgn') of
adamc@1270 2546 L'.SgnConst sgis =>
adamc@1270 2547 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@1270 2548 | _ => (sgnError env (NotIncludable sgn');
adamc@1270 2549 ([], (env, denv, [])))
adamc@1270 2550 end
adamc@1270 2551
adamc@1270 2552 | L.SgiConstraint (c1, c2) =>
adamc@1270 2553 let
adamc@1270 2554 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@1270 2555 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@1270 2556
adamc@1270 2557 val denv = D.assert env denv (c1', c2')
adamc@1270 2558 in
adamc@1270 2559 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@1270 2560 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@1270 2561
adamc@1270 2562 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2))
adamc@1270 2563 end
adamc@1270 2564
adamc@1270 2565 | L.SgiClassAbs (x, k) =>
adamc@1270 2566 let
adamc@1270 2567 val k = elabKind env k
adamc@1270 2568 val (env, n) = E.pushCNamed env x k NONE
adamc@1270 2569 val env = E.pushClass env n
adamc@1270 2570 in
adamc@1270 2571 ([(L'.SgiClassAbs (x, n, k), loc)], (env, denv, []))
adamc@1270 2572 end
adamc@1270 2573
adamc@1270 2574 | L.SgiClass (x, k, c) =>
adamc@1270 2575 let
adamc@1270 2576 val k = elabKind env k
adamc@1270 2577 val (c', ck, gs) = elabCon (env, denv) c
adamc@1270 2578 val (env, n) = E.pushCNamed env x k (SOME c')
adamc@1270 2579 val env = E.pushClass env n
adamc@1270 2580 in
adamc@1270 2581 checkKind env c' ck k;
adamc@1270 2582 ([(L'.SgiClass (x, n, k, c'), loc)], (env, denv, []))
adamc@1270 2583 end)
adamc@211 2584
adamc@83 2585 and elabSgn (env, denv) (sgn, loc) =
adamc@31 2586 case sgn of
adamc@31 2587 L.SgnConst sgis =>
adamc@31 2588 let
adamc@88 2589 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 2590
adamc@62 2591 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 2592 case sgi of
adamc@62 2593 L'.SgiConAbs (x, _, _) =>
adamc@62 2594 (if SS.member (cons, x) then
adamc@62 2595 sgnError env (DuplicateCon (loc, x))
adamc@62 2596 else
adamc@62 2597 ();
adamc@62 2598 (SS.add (cons, x), vals, sgns, strs))
adamc@62 2599 | L'.SgiCon (x, _, _, _) =>
adamc@62 2600 (if SS.member (cons, x) then
adamc@62 2601 sgnError env (DuplicateCon (loc, x))
adamc@62 2602 else
adamc@62 2603 ();
adamc@62 2604 (SS.add (cons, x), vals, sgns, strs))
adamc@805 2605 | L'.SgiDatatype dts =>
adamc@156 2606 let
adamc@805 2607 val (cons, vals) =
adamc@805 2608 let
adamc@805 2609 fun doOne ((x, _, _, xncs), (cons, vals)) =
adamc@805 2610 let
adamc@805 2611 val vals = foldl (fn ((x, _, _), vals) =>
adamc@805 2612 (if SS.member (vals, x) then
adamc@805 2613 sgnError env (DuplicateVal (loc, x))
adamc@805 2614 else
adamc@805 2615 ();
adamc@805 2616 SS.add (vals, x)))
adamc@805 2617 vals xncs
adamc@805 2618 in
adamc@805 2619 if SS.member (cons, x) then
adamc@805 2620 sgnError env (DuplicateCon (loc, x))
adamc@805 2621 else
adamc@805 2622 ();
adamc@805 2623 (SS.add (cons, x), vals)
adamc@805 2624 end
adamc@805 2625 in
adamc@805 2626 foldl doOne (cons, vals) dts
adamc@805 2627 end
adamc@156 2628 in
adamc@805 2629 (cons, vals, sgns, strs)
adamc@156 2630 end
adamc@191 2631 | L'.SgiDatatypeImp (x, _, _, _, _, _, _) =>
adamc@156 2632 (if SS.member (cons, x) then
adamc@156 2633 sgnError env (DuplicateCon (loc, x))
adamc@156 2634 else
adamc@156 2635 ();
adamc@156 2636 (SS.add (cons, x), vals, sgns, strs))
adamc@62 2637 | L'.SgiVal (x, _, _) =>
adamc@62 2638 (if SS.member (vals, x) then
adamc@62 2639 sgnError env (DuplicateVal (loc, x))
adamc@62 2640 else
adamc@62 2641 ();
adamc@62 2642 (cons, SS.add (vals, x), sgns, strs))
adamc@62 2643 | L'.SgiSgn (x, _, _) =>
adamc@62 2644 (if SS.member (sgns, x) then
adamc@62 2645 sgnError env (DuplicateSgn (loc, x))
adamc@62 2646 else
adamc@62 2647 ();
adamc@62 2648 (cons, vals, SS.add (sgns, x), strs))
adamc@62 2649 | L'.SgiStr (x, _, _) =>
adamc@62 2650 (if SS.member (strs, x) then
adamc@62 2651 sgnError env (DuplicateStr (loc, x))
adamc@62 2652 else
adamc@62 2653 ();
adamc@88 2654 (cons, vals, sgns, SS.add (strs, x)))
adamc@203 2655 | L'.SgiConstraint _ => (cons, vals, sgns, strs)
adamc@563 2656 | L'.SgiClassAbs (x, _, _) =>
adamc@211 2657 (if SS.member (cons, x) then
adamc@211 2658 sgnError env (DuplicateCon (loc, x))
adamc@211 2659 else
adamc@211 2660 ();
adamc@211 2661 (SS.add (cons, x), vals, sgns, strs))
adamc@563 2662 | L'.SgiClass (x, _, _, _) =>
adamc@211 2663 (if SS.member (cons, x) then
adamc@211 2664 sgnError env (DuplicateCon (loc, x))
adamc@211 2665 else
adamc@211 2666 ();
adamc@460 2667 (SS.add (cons, x), vals, sgns, strs)))
adamc@62 2668 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 2669 in
adamc@83 2670 ((L'.SgnConst sgis', loc), gs)
adamc@31 2671 end
adamc@31 2672 | L.SgnVar x =>
adamc@31 2673 (case E.lookupSgn env x of
adamc@31 2674 NONE =>
adamc@31 2675 (sgnError env (UnboundSgn (loc, x));
adamc@83 2676 ((L'.SgnError, loc), []))
adamc@83 2677 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 2678 | L.SgnFun (m, dom, ran) =>
adamc@41 2679 let
adamc@83 2680 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2681 val (env', n) = E.pushStrNamed env m dom'
adamc@469 2682 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = []}
adamc@469 2683 val (ran', gs2) = elabSgn (env', denv') ran
adamc@41 2684 in
adamc@83 2685 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 2686 end
adamc@42 2687 | L.SgnWhere (sgn, x, c) =>
adamc@42 2688 let
adamc@83 2689 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 2690 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 2691 in
adamc@42 2692 case #1 (hnormSgn env sgn') of
adamc@83 2693 L'.SgnError => (sgnerror, [])
adamc@42 2694 | L'.SgnConst sgis =>
adamc@75 2695 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 2696 x' = x andalso
adamc@623 2697 (unifyKinds env k ck
adamc@75 2698 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 2699 true)
adamc@42 2700 | _ => false) sgis then
adamc@83 2701 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 2702 else
adamc@42 2703 (sgnError env (UnWhereable (sgn', x));
adamc@83 2704 (sgnerror, []))
adamc@42 2705 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 2706 (sgnerror, []))
adamc@42 2707 end
adamc@59 2708 | L.SgnProj (m, ms, x) =>
adamc@59 2709 (case E.lookupStr env m of
adamc@59 2710 NONE => (strError env (UnboundStr (loc, m));
adamc@83 2711 (sgnerror, []))
adamc@59 2712 | SOME (n, sgn) =>
adamc@59 2713 let
adamc@59 2714 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 2715 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 2716 NONE => (strError env (UnboundStr (loc, m));
adamc@59 2717 (strerror, sgnerror))
adamc@59 2718 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 2719 ((L'.StrVar n, loc), sgn) ms
adamc@59 2720 in
adamc@59 2721 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 2722 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 2723 (sgnerror, []))
adamc@83 2724 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 2725 end)
adamc@59 2726
adamc@31 2727
adamc@706 2728 and selfify env {str, strs, sgn} =
adamc@66 2729 case #1 (hnormSgn env sgn) of
adamc@66 2730 L'.SgnError => sgn
adamc@66 2731 | L'.SgnVar _ => sgn
adamc@66 2732
adamc@66 2733 | L'.SgnConst sgis =>
adam@1387 2734 (L'.SgnConst (#1 (ListUtil.foldlMapConcat
adam@1387 2735 (fn (sgi, env) =>
adam@1387 2736 (case sgi of (L'.SgiConAbs (x, n, k), loc) =>
adam@1387 2737 [(L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)]
adam@1387 2738 | (L'.SgiDatatype dts, loc) =>
adam@1387 2739 map (fn (x, n, xs, xncs) => (L'.SgiDatatypeImp (x, n, str, strs, x, xs, xncs), loc)) dts
adam@1387 2740 | (L'.SgiClassAbs (x, n, k), loc) =>
adam@1387 2741 [(L'.SgiClass (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)]
adam@1387 2742 | (L'.SgiStr (x, n, sgn), loc) =>
adam@1387 2743 [(L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)]
adam@1387 2744 | x => [x],
adam@1387 2745 E.sgiBinds env sgi)) env sgis)), #2 sgn)
adamc@66 2746 | L'.SgnFun _ => sgn
adamc@66 2747 | L'.SgnWhere _ => sgn
adamc@66 2748 | L'.SgnProj (m, ms, x) =>
adamc@66 2749 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2750 (L'.StrVar m, #2 sgn) ms,
adamc@66 2751 sgn = #2 (E.lookupStrNamed env m),
adamc@66 2752 field = x} of
adamc@66 2753 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 2754 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 2755
adamc@706 2756 and selfifyAt env {str, sgn} =
adamc@66 2757 let
adamc@66 2758 fun self (str, _) =
adamc@66 2759 case str of
adamc@66 2760 L'.StrVar x => SOME (x, [])
adamc@66 2761 | L'.StrProj (str, x) =>
adamc@66 2762 (case self str of
adamc@66 2763 NONE => NONE
adamc@66 2764 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 2765 | _ => NONE
adamc@66 2766 in
adamc@66 2767 case self str of
adamc@66 2768 NONE => sgn
adamc@66 2769 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 2770 end
adamc@66 2771
adamc@706 2772 and dopen env {str, strs, sgn} =
adamc@66 2773 let
adam@1527 2774 fun isVisible x = x <> "" andalso String.sub (x, 0) <> #"?"
adam@1527 2775
adamc@66 2776 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2777 (L'.StrVar str, #2 sgn) strs
adamc@66 2778 in
adamc@66 2779 case #1 (hnormSgn env sgn) of
adamc@66 2780 L'.SgnConst sgis =>
adamc@805 2781 ListUtil.foldlMapConcat
adamc@805 2782 (fn ((sgi, loc), env') =>
adamc@805 2783 let
adamc@805 2784 val d =
adamc@805 2785 case sgi of
adamc@805 2786 L'.SgiConAbs (x, n, k) =>
adam@1527 2787 if isVisible x then
adam@1527 2788 let
adam@1527 2789 val c = (L'.CModProj (str, strs, x), loc)
adam@1527 2790 in
adam@1527 2791 [(L'.DCon (x, n, k, c), loc)]
adam@1527 2792 end
adam@1527 2793 else
adam@1527 2794 []
adamc@805 2795 | L'.SgiCon (x, n, k, c) =>
adam@1527 2796 if isVisible x then
adam@1527 2797 [(L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)]
adam@1527 2798 else
adam@1527 2799 []
adamc@805 2800 | L'.SgiDatatype dts =>
adam@1527 2801 List.mapPartial (fn (x, n, xs, xncs) => if isVisible x then
adam@1527 2802 SOME (L'.DDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
adam@1527 2803 else
adam@1527 2804 NONE) dts
adamc@805 2805 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adam@1527 2806 if isVisible x then
adam@1527 2807 [(L'.DDatatypeImp (x, n, m1, ms, x', xs, xncs), loc)]
adam@1527 2808 else
adam@1527 2809 []
adamc@805 2810 | L'.SgiVal (x, n, t) =>
adam@1527 2811 if isVisible x then
adam@1527 2812 [(L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)]
adam@1527 2813 else
adam@1527 2814 []
adamc@805 2815 | L'.SgiStr (x, n, sgn) =>
adam@1527 2816 if isVisible x then
adam@1527 2817 [(L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)]
adam@1527 2818 else
adam@1527 2819 []
adamc@805 2820 | L'.SgiSgn (x, n, sgn) =>
adam@1527 2821 if isVisible x then
adam@1527 2822 [(L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)]
adam@1527 2823 else
adam@1527 2824 []
adamc@805 2825 | L'.SgiConstraint (c1, c2) =>
adamc@805 2826 [(L'.DConstraint (c1, c2), loc)]
adamc@805 2827 | L'.SgiClassAbs (x, n, k) =>
adam@1527 2828 if isVisible x then
adam@1527 2829 let
adam@1527 2830 val c = (L'.CModProj (str, strs, x), loc)
adam@1527 2831 in
adam@1527 2832 [(L'.DCon (x, n, k, c), loc)]
adam@1527 2833 end
adam@1527 2834 else
adam@1527 2835 []
adamc@805 2836 | L'.SgiClass (x, n, k, _) =>
adam@1527 2837 if isVisible x then
adam@1527 2838 let
adam@1527 2839 val c = (L'.CModProj (str, strs, x), loc)
adam@1527 2840 in
adam@1527 2841 [(L'.DCon (x, n, k, c), loc)]
adam@1527 2842 end
adam@1527 2843 else
adam@1527 2844 []
adamc@805 2845 in
adamc@805 2846 (d, foldl (fn (d, env') => E.declBinds env' d) env' d)
adamc@805 2847 end)
adamc@805 2848 env sgis
adamc@66 2849 | _ => (strError env (UnOpenable sgn);
adamc@628 2850 ([], env))
adamc@66 2851 end
adamc@66 2852
adamc@706 2853 and sgiOfDecl (d, loc) =
adamc@31 2854 case d of
adamc@123 2855 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@156 2856 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
adamc@156 2857 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
adamc@123 2858 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 2859 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 2860 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 2861 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2862 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2863 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 2864 | L'.DExport _ => []
adamc@707 2865 | L'.DTable (tn, x, n, c, _, pc, _, cc) =>
adamc@707 2866 [(L'.SgiVal (x, n, (L'.CApp ((L'.CApp (tableOf (), c), loc),
adamc@707 2867 (L'.CConcat (pc, cc), loc)), loc)), loc)]
adamc@460 2868 | L'.DSequence (tn, x, n) => [(L'.SgiVal (x, n, sequenceOf ()), loc)]
adamc@754 2869 | L'.DView (tn, x, n, _, c) =>
adamc@754 2870 [(L'.SgiVal (x, n, (L'.CApp (viewOf (), c), loc)), loc)]
adamc@563 2871 | L'.DClass (x, n, k, c) => [(L'.SgiClass (x, n, k, c), loc)]
adamc@271 2872 | L'.DDatabase _ => []
adamc@460 2873 | L'.DCookie (tn, x, n, c) => [(L'.SgiVal (x, n, (L'.CApp (cookieOf (), c), loc)), loc)]
adamc@720 2874 | L'.DStyle (tn, x, n) => [(L'.SgiVal (x, n, styleOf ()), loc)]
adamc@1075 2875 | L'.DTask _ => []
adamc@1199 2876 | L'.DPolicy _ => []
adam@1294 2877 | L'.DOnError _ => []
adamc@88 2878
adamc@1000 2879 and subSgn' counterparts env strLoc sgn1 (sgn2 as (_, loc2)) =
adamc@469 2880 ((*prefaces "subSgn" [("sgn1", p_sgn env sgn1),
adamc@469 2881 ("sgn2", p_sgn env sgn2)];*)
adamc@35 2882 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 2883 (L'.SgnError, _) => ()
adamc@31 2884 | (_, L'.SgnError) => ()
adamc@31 2885
adamc@31 2886 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 2887 let
adamc@362 2888 (*val () = prefaces "subSgn" [("sgn1", p_sgn env sgn1),
adamc@362 2889 ("sgn2", p_sgn env sgn2),
adamc@362 2890 ("sgis1", p_sgn env (L'.SgnConst sgis1, loc2)),
adamc@362 2891 ("sgis2", p_sgn env (L'.SgnConst sgis2, loc2))]*)
adamc@362 2892
adamc@838 2893 fun cpart n = IM.find (!counterparts, n)
adamc@838 2894 fun cparts (n2, n1) = counterparts := IM.insert (!counterparts, n2, n1)
adamc@838 2895
adamc@838 2896 val sub2 = U.Con.map {kind = fn k => k,
adamc@838 2897 con = fn c =>
adamc@838 2898 case c of
adamc@838 2899 L'.CNamed n2 =>
adamc@838 2900 (case cpart n2 of
adamc@838 2901 NONE => c
adamc@838 2902 | SOME n1 => L'.CNamed n1)
adamc@838 2903 | _ => c}
adamc@838 2904
adamc@628 2905 fun folder (sgi2All as (sgi, loc), env) =
adamc@31 2906 let
adamc@362 2907 (*val () = prefaces "folder" [("sgis1", p_sgn env (L'.SgnConst sgis1, loc2))]*)
adamc@362 2908
adamc@805 2909 fun seek' f p =
adamc@31 2910 let
adamc@628 2911 fun seek env ls =
adamc@31 2912 case ls of
adamc@805 2913 [] => f env
adamc@31 2914 | h :: t =>
adamc@469 2915 case p (env, h) of
adamc@469 2916 NONE =>
adamc@469 2917 let
adamc@469 2918 val env = case #1 h of
adamc@469 2919 L'.SgiCon (x, n, k, c) =>
adamc@471 2920 if E.checkENamed env n then
adamc@471 2921 env
adamc@471 2922 else
adamc@471 2923 E.pushCNamedAs env x n k (SOME c)
adamc@469 2924 | L'.SgiConAbs (x, n, k) =>
adamc@471 2925 if E.checkENamed env n then
adamc@471 2926 env
adamc@471 2927 else
adamc@471 2928 E.pushCNamedAs env x n k NONE
adamc@469 2929 | _ => env
adamc@469 2930 in
adamc@628 2931 seek (E.sgiBinds env h) t
adamc@469 2932 end
adamc@88 2933 | SOME envs => envs
adamc@31 2934 in
adamc@628 2935 seek env sgis1
adamc@31 2936 end
adamc@805 2937
adamc@1000 2938 val seek = seek' (fn env => (sgnError env (UnmatchedSgi (strLoc, sgi2All));
adamc@805 2939 env))
adamc@31 2940 in
adamc@31 2941 case sgi of
adamc@31 2942 L'.SgiConAbs (x, n2, k2) =>
adam@1591 2943 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@31 2944 let
adamc@41 2945 fun found (x', n1, k1, co1) =
adamc@41 2946 if x = x' then
adamc@41 2947 let
adamc@623 2948 val () = unifyKinds env k1 k2
adamc@41 2949 handle KUnify (k1, k2, err) =>
adam@1591 2950 sgnError env (SgiWrongKind (loc, sgi1All, k1,
adamc@1000 2951 sgi2All, k2, err))
adamc@41 2952 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 2953 in
adamc@41 2954 SOME (if n1 = n2 then
adamc@41 2955 env
adamc@41 2956 else
adamc@838 2957 (cparts (n2, n1);
adamc@838 2958 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2))))
adamc@41 2959 end
adamc@41 2960 else
adamc@41 2961 NONE
adamc@31 2962 in
adamc@31 2963 case sgi1 of
adamc@41 2964 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 2965 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@805 2966 | L'.SgiDatatype dts =>
adamc@191 2967 let
adamc@191 2968 val k = (L'.KType, loc)
adamc@805 2969
adamc@805 2970 fun search dts =
adamc@805 2971 case dts of
adamc@805 2972 [] => NONE
adamc@805 2973 | (x', n1, xs, _) :: dts =>
adamc@805 2974 let
adamc@805 2975 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@805 2976 in
adamc@805 2977 case found (x', n1, k', NONE) of
adamc@805 2978 NONE => search dts
adamc@805 2979 | x => x
adamc@805 2980 end
adamc@191 2981 in
adamc@805 2982 search dts
adamc@191 2983 end
adamc@191 2984 | L'.SgiDatatypeImp (x', n1, m1, ms, s, xs, _) =>
adamc@191 2985 let
adamc@191 2986 val k = (L'.KType, loc)
adamc@191 2987 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2988 in
adamc@191 2989 found (x', n1, k', SOME (L'.CModProj (m1, ms, s), loc))
adamc@191 2990 end
adamc@711 2991 | L'.SgiClassAbs (x', n1, k) => found (x', n1, k, NONE)
adamc@711 2992 | L'.SgiClass (x', n1, k, c) => found (x', n1, k, SOME c)
adamc@31 2993 | _ => NONE
adamc@31 2994 end)
adamc@31 2995
adamc@31 2996 | L'.SgiCon (x, n2, k2, c2) =>
adam@1591 2997 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@211 2998 let
adamc@211 2999 fun found (x', n1, k1, c1) =
adamc@211 3000 if x = x' then
adamc@211 3001 let
adamc@838 3002 val c2 = sub2 c2
adamc@838 3003
adamc@327 3004 fun good () =
adamc@327 3005 let
adamc@327 3006 val env = E.pushCNamedAs env x n2 k2 (SOME c2)
adamc@327 3007 val env = if n1 = n2 then
adamc@327 3008 env
adamc@327 3009 else
adamc@839 3010 (cparts (n2, n1);
adamc@839 3011 E.pushCNamedAs env x n1 k1 (SOME c1))
adamc@327 3012 in
adamc@628 3013 SOME env
adamc@327 3014 end
adamc@211 3015 in
adamc@987 3016 (unifyCons env loc c1 c2;
adamc@628 3017 good ())
adamc@211 3018 handle CUnify (c1, c2, err) =>
adam@1591 3019 (sgnError env (SgiWrongCon (loc, sgi1All, c1,
adamc@1000 3020 sgi2All, c2, err));
adamc@211 3021 good ())
adamc@211 3022 end
adamc@211 3023 else
adamc@211 3024 NONE
adamc@211 3025 in
adamc@211 3026 case sgi1 of
adamc@211 3027 L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@711 3028 | L'.SgiClass (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@211 3029 | _ => NONE
adamc@211 3030 end)
adamc@31 3031
adamc@805 3032 | L'.SgiDatatype dts2 =>
adamc@805 3033 let
adam@1591 3034 fun found' (sgi1All as (_, loc), (x1, n1, xs1, xncs1), (x2, n2, xs2, xncs2), env) =
adamc@805 3035 if x1 <> x2 then
adamc@805 3036 NONE
adamc@805 3037 else
adamc@805 3038 let
adamc@805 3039 fun mismatched ue =
adam@1591 3040 (sgnError env (SgiMismatchedDatatypes (loc, sgi1All, sgi2All, ue));
adamc@805 3041 SOME env)
adamc@805 3042
adamc@805 3043 val k = (L'.KType, loc)
adamc@805 3044 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs1
adamc@805 3045
adamc@805 3046 fun good () =
adamc@805 3047 let
adamc@805 3048 val env = E.sgiBinds env sgi1All
adamc@805 3049 val env = if n1 = n2 then
adamc@805 3050 env
adamc@805 3051 else
adamc@838 3052 (cparts (n2, n1);
adamc@838 3053 E.pushCNamedAs env x1 n2 k'
adamc@838 3054 (SOME (L'.CNamed n1, loc)))
adamc@805 3055 in
adamc@805 3056 SOME env
adamc@805 3057 end
adamc@805 3058
adamc@805 3059 val env = E.pushCNamedAs env x1 n1 k' NONE
adamc@805 3060 val env = if n1 = n2 then
adamc@805 3061 env
adamc@805 3062 else
adamc@838 3063 (cparts (n2, n1);
adamc@838 3064 E.pushCNamedAs env x1 n2 k' (SOME (L'.CNamed n1, loc)))
adamc@805 3065 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs1
adamc@805 3066 fun xncBad ((x1, _, t1), (x2, _, t2)) =
adamc@805 3067 String.compare (x1, x2) <> EQUAL
adamc@805 3068 orelse case (t1, t2) of
adamc@805 3069 (NONE, NONE) => false
adamc@805 3070 | (SOME t1, SOME t2) =>
adamc@987 3071 (unifyCons env loc t1 (sub2 t2); false)
adamc@805 3072 | _ => true
adamc@805 3073 in
adamc@805 3074 (if xs1 <> xs2
adamc@805 3075 orelse length xncs1 <> length xncs2
adamc@805 3076 orelse ListPair.exists xncBad (xncs1, xncs2) then
adamc@805 3077 mismatched NONE
adamc@160 3078 else
adamc@805 3079 good ())
adamc@805 3080 handle CUnify ue => mismatched (SOME ue)
adamc@805 3081 end
adamc@805 3082 in
adamc@805 3083 seek'
adamc@805 3084 (fn _ =>
adamc@805 3085 let
adamc@805 3086 fun seekOne (dt2, env) =
adamc@805 3087 seek (fn (env, sgi1All as (sgi1, _)) =>
adamc@805 3088 case sgi1 of
adamc@805 3089 L'.SgiDatatypeImp (x', n1, _, _, _, xs, xncs1) =>
adamc@805 3090 found' (sgi1All, (x', n1, xs, xncs1), dt2, env)
adamc@805 3091 | _ => NONE)
adamc@805 3092
adamc@805 3093 fun seekAll (dts, env) =
adamc@805 3094 case dts of
adamc@805 3095 [] => env
adamc@805 3096 | dt :: dts => seekAll (dts, seekOne (dt, env))
adamc@805 3097 in
adamc@805 3098 seekAll (dts2, env)
adamc@805 3099 end)
adamc@805 3100 (fn (env, sgi1All as (sgi1, _)) =>
adamc@805 3101 let
adamc@805 3102 fun found dts1 =
adamc@805 3103 let
adamc@805 3104 fun iter (dts1, dts2, env) =
adamc@805 3105 case (dts1, dts2) of
adamc@805 3106 ([], []) => SOME env
adamc@805 3107 | (dt1 :: dts1, dt2 :: dts2) =>
adamc@805 3108 (case found' (sgi1All, dt1, dt2, env) of
adamc@805 3109 NONE => NONE
adamc@805 3110 | SOME env => iter (dts1, dts2, env))
adamc@805 3111 | _ => NONE
adamc@805 3112 in
adamc@805 3113 iter (dts1, dts2, env)
adamc@805 3114 end
adamc@805 3115 in
adamc@805 3116 case sgi1 of
adamc@805 3117 L'.SgiDatatype dts1 => found dts1
adamc@805 3118 | _ => NONE
adamc@805 3119 end)
adamc@805 3120 end
adamc@156 3121
adamc@191 3122 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, xs, _) =>
adam@1591 3123 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@158 3124 case sgi1 of
adamc@191 3125 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _, _) =>
adamc@158 3126 if x = x' then
adamc@158 3127 let
adamc@158 3128 val k = (L'.KType, loc)
adamc@191 3129 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@158 3130 val t1 = (L'.CModProj (m11, ms1, s1), loc)
adamc@158 3131 val t2 = (L'.CModProj (m12, ms2, s2), loc)
adamc@158 3132
adamc@158 3133 fun good () =
adamc@158 3134 let
adamc@191 3135 val env = E.pushCNamedAs env x n1 k' (SOME t1)
adamc@191 3136 val env = E.pushCNamedAs env x n2 k' (SOME t2)
adamc@158 3137 in
adamc@838 3138 cparts (n2, n1);
adamc@628 3139 SOME env
adamc@158 3140 end
adamc@158 3141 in
adamc@987 3142 (unifyCons env loc t1 t2;
adamc@628 3143 good ())
adamc@158 3144 handle CUnify (c1, c2, err) =>
adam@1591 3145 (sgnError env (SgiWrongCon (loc, sgi1All, c1, sgi2All, c2, err));
adamc@158 3146 good ())
adamc@158 3147 end
adamc@158 3148 else
adamc@158 3149 NONE
adamc@158 3150
adamc@158 3151 | _ => NONE)
adamc@156 3152
adamc@32 3153 | L'.SgiVal (x, n2, c2) =>
adam@1591 3154 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@32 3155 case sgi1 of
adamc@41 3156 L'.SgiVal (x', n1, c1) =>
adamc@41 3157 if x = x' then
adamc@838 3158 ((*prefaces "val" [("x", PD.string x),
adamc@838 3159 ("n1", PD.string (Int.toString n1)),
adamc@838 3160 ("c1", p_con env c1),
adamc@838 3161 ("c2", p_con env c2),
adamc@838 3162 ("c2'", p_con env (sub2 c2))];*)
adamc@987 3163 unifyCons env loc c1 (sub2 c2);
adamc@628 3164 SOME env)
adamc@86 3165 handle CUnify (c1, c2, err) =>
adam@1591 3166 (sgnError env (SgiWrongCon (loc, sgi1All, c1, sgi2All, c2, err));
adamc@628 3167 SOME env)
adamc@41 3168 else
adamc@41 3169 NONE
adamc@32 3170 | _ => NONE)
adamc@32 3171
adamc@33 3172 | L'.SgiStr (x, n2, sgn2) =>
adam@1591 3173 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@33 3174 case sgi1 of
adamc@41 3175 L'.SgiStr (x', n1, sgn1) =>
adamc@41 3176 if x = x' then
adamc@66 3177 let
adam@1591 3178 val () = subSgn' counterparts env loc sgn1 sgn2
adamc@66 3179 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 3180 val env = if n1 = n2 then
adamc@66 3181 env
adamc@66 3182 else
adamc@66 3183 E.pushStrNamedAs env x n2
adamc@66 3184 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 3185 sgn = sgn2})
adamc@66 3186 in
adamc@628 3187 SOME env
adamc@66 3188 end
adamc@41 3189 else
adamc@41 3190 NONE
adamc@33 3191 | _ => NONE)
adamc@59 3192
adamc@59 3193 | L'.SgiSgn (x, n2, sgn2) =>
adam@1591 3194 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@59 3195 case sgi1 of
adamc@59 3196 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 3197 if x = x' then
adamc@65 3198 let
adam@1591 3199 val () = subSgn' counterparts env loc sgn1 sgn2
adam@1591 3200 val () = subSgn' counterparts env loc sgn2 sgn1
adamc@65 3201
adamc@65 3202 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 3203 val env = if n1 = n2 then
adamc@65 3204 env
adamc@65 3205 else
adamc@838 3206 (cparts (n2, n1);
adamc@838 3207 E.pushSgnNamedAs env x n1 sgn2)
adamc@65 3208 in
adamc@628 3209 SOME env
adamc@65 3210 end
adamc@59 3211 else
adamc@59 3212 NONE
adamc@59 3213 | _ => NONE)
adamc@88 3214
adamc@88 3215 | L'.SgiConstraint (c2, d2) =>
adam@1591 3216 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@88 3217 case sgi1 of
adamc@88 3218 L'.SgiConstraint (c1, d1) =>
adamc@987 3219 if consEq env loc (c1, c2)
adamc@987 3220 andalso consEq env loc (d1, d2) then
adamc@628 3221 SOME env
adamc@88 3222 else
adamc@88 3223 NONE
adamc@88 3224 | _ => NONE)
adamc@203 3225
adamc@563 3226 | L'.SgiClassAbs (x, n2, k2) =>
adam@1591 3227 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@211 3228 let
adamc@563 3229 fun found (x', n1, k1, co) =
adamc@211 3230 if x = x' then
adamc@211 3231 let
adamc@623 3232 val () = unifyKinds env k1 k2
adamc@563 3233 handle KUnify (k1, k2, err) =>
adam@1591 3234 sgnError env (SgiWrongKind (loc, sgi1All, k1,
adamc@1000 3235 sgi2All, k2, err))
adamc@563 3236
adamc@711 3237 val env = E.pushCNamedAs env x n1 k1 co
adamc@211 3238 in
adamc@211 3239 SOME (if n1 = n2 then
adamc@211 3240 env
adamc@211 3241 else
adamc@838 3242 (cparts (n2, n1);
adamc@838 3243 E.pushCNamedAs env x n2 k1 (SOME (L'.CNamed n1, loc2))))
adamc@211 3244 end
adamc@211 3245 else
adamc@211 3246 NONE
adamc@211 3247 in
adamc@211 3248 case sgi1 of
adamc@563 3249 L'.SgiClassAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@563 3250 | L'.SgiClass (x', n1, k1, c) => found (x', n1, k1, SOME c)
adamc@211 3251 | _ => NONE
adamc@211 3252 end)
adamc@563 3253 | L'.SgiClass (x, n2, k2, c2) =>
adam@1591 3254 seek (fn (env, sgi1All as (sgi1, loc)) =>
adamc@211 3255 let
adamc@563 3256 fun found (x', n1, k1, c1) =
adamc@211 3257 if x = x' then
adamc@211 3258 let
adamc@623 3259 val () = unifyKinds env k1 k2
adamc@563 3260 handle KUnify (k1, k2, err) =>
adam@1591 3261 sgnError env (SgiWrongKind (loc, sgi1All, k1,
adamc@1000 3262 sgi2All, k2, err))
adamc@563 3263
adamc@838 3264 val c2 = sub2 c2
adamc@838 3265
adamc@218 3266 fun good () =
adamc@218 3267 let
adamc@711 3268 val env = E.pushCNamedAs env x n2 k2 (SOME c2)
adamc@218 3269 val env = if n1 = n2 then
adamc@218 3270 env
adamc@218 3271 else
adamc@839 3272 (cparts (n2, n1);
adamc@839 3273 E.pushCNamedAs env x n1 k2 (SOME c1))
adamc@218 3274 in
adamc@628 3275 SOME env
adamc@218 3276 end
adamc@211 3277 in
adamc@987 3278 (unifyCons env loc c1 c2;
adamc@628 3279 good ())
adamc@211 3280 handle CUnify (c1, c2, err) =>
adam@1591 3281 (sgnError env (SgiWrongCon (loc, sgi1All, c1,
adamc@1000 3282 sgi2All, c2, err));
adamc@211 3283 good ())
adamc@211 3284 end
adamc@211 3285 else
adamc@211 3286 NONE
adamc@211 3287 in
adamc@211 3288 case sgi1 of
adamc@563 3289 L'.SgiClass (x', n1, k1, c1) => found (x', n1, k1, c1)
adamc@211 3290 | _ => NONE
adamc@211 3291 end)
adamc@31 3292 end
adamc@31 3293 in
adamc@628 3294 ignore (foldl folder env sgis2)
adamc@31 3295 end
adamc@31 3296
adamc@41 3297 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 3298 let
adamc@480 3299 val ran2 =
adamc@43 3300 if n1 = n2 then
adamc@480 3301 ran2
adamc@43 3302 else
adamc@480 3303 subStrInSgn (n2, n1) ran2
adamc@43 3304 in
adamc@1000 3305 subSgn' counterparts env strLoc dom2 dom1;
adamc@1000 3306 subSgn' counterparts (E.pushStrNamedAs env m1 n1 dom2) strLoc ran1 ran2
adamc@43 3307 end
adamc@41 3308
adamc@1000 3309 | _ => sgnError env (SgnWrongForm (strLoc, sgn1, sgn2)))
adamc@41 3310
adam@1580 3311 and subSgn env x y z = subSgn' (ref IM.empty) env x y z
adam@1580 3312 handle e as E.UnboundNamed _ => if ErrorMsg.anyErrors () then () else raise e
adamc@61 3313
adamc@706 3314 and positive self =
adamc@312 3315 let
adamc@312 3316 open L
adamc@312 3317
adamc@312 3318 fun none (c, _) =
adamc@312 3319 case c of
adamc@312 3320 CAnnot (c, _) => none c
adamc@312 3321
adamc@312 3322 | TFun (c1, c2) => none c1 andalso none c2
adamc@312 3323 | TCFun (_, _, _, c) => none c
adamc@312 3324 | TRecord c => none c
adamc@312 3325
adamc@312 3326 | CVar ([], x) => x <> self
adamc@312 3327 | CVar _ => true
adamc@312 3328 | CApp (c1, c2) => none c1 andalso none c2
adamc@312 3329 | CAbs _ => false
adamc@628 3330 | TDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
adamc@312 3331
adamc@623 3332 | CKAbs _ => false
adamc@623 3333 | TKFun _ => false
adamc@623 3334
adamc@312 3335 | CName _ => true
adamc@312 3336
adamc@312 3337 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso none c2) xcs
adamc@312 3338 | CConcat (c1, c2) => none c1 andalso none c2
adamc@621 3339 | CMap => true
adamc@312 3340
adamc@312 3341 | CUnit => true
adamc@312 3342
adamc@312 3343 | CTuple cs => List.all none cs
adamc@312 3344 | CProj (c, _) => none c
adamc@312 3345
adamc@312 3346 | CWild _ => false
adamc@312 3347
adamc@312 3348 fun pos (c, _) =
adamc@312 3349 case c of
adamc@312 3350 CAnnot (c, _) => pos c
adamc@312 3351
adamc@312 3352 | TFun (c1, c2) => none c1 andalso pos c2
adamc@312 3353 | TCFun (_, _, _, c) => pos c
adamc@312 3354 | TRecord c => pos c
adamc@312 3355
adamc@312 3356 | CVar _ => true
adamc@312 3357 | CApp (c1, c2) => pos c1 andalso none c2
adamc@312 3358 | CAbs _ => false
adamc@628 3359 | TDisjoint (c1, c2, c3) => none c1 andalso none c2 andalso none c3
adamc@312 3360
adamc@623 3361 | CKAbs _ => false
adamc@623 3362 | TKFun _ => false
adamc@623 3363
adamc@312 3364 | CName _ => true
adamc@312 3365
adamc@312 3366 | CRecord xcs => List.all (fn (c1, c2) => none c1 andalso pos c2) xcs
adamc@312 3367 | CConcat (c1, c2) => pos c1 andalso pos c2
adamc@621 3368 | CMap => true
adamc@312 3369
adamc@312 3370 | CUnit => true
adamc@312 3371
adamc@312 3372 | CTuple cs => List.all pos cs
adamc@312 3373 | CProj (c, _) => pos c
adamc@312 3374
adamc@312 3375 | CWild _ => false
adamc@312 3376 in
adamc@312 3377 pos
adamc@312 3378 end
adamc@312 3379
adamc@706 3380 and wildifyStr env (str, sgn) =
adamc@325 3381 case #1 (hnormSgn env sgn) of
adamc@325 3382 L'.SgnConst sgis =>
adamc@325 3383 (case #1 str of
adamc@325 3384 L.StrConst ds =>
adamc@325 3385 let
adamc@1270 3386 fun cname d =
adamc@1270 3387 case d of
adamc@1270 3388 L'.SgiCon (x, _, _, _) => SOME x
adamc@1270 3389 | L'.SgiConAbs (x, _, _) => SOME x
adamc@1270 3390 | L'.SgiClass (x, _, _, _) => SOME x
adamc@1270 3391 | L'.SgiClassAbs (x, _, _) => SOME x
adamc@1270 3392 | _ => NONE
adamc@1270 3393
adamc@1270 3394 fun dname (d, _) =
adamc@1270 3395 case d of
adamc@1270 3396 L.DCon (x, _, _) => SOME x
adamc@1270 3397 | L.DClass (x, _, _) => SOME x
adamc@1270 3398 | _ => NONE
adamc@1270 3399
adamc@493 3400 fun decompileKind (k, loc) =
adamc@493 3401 case k of
adamc@493 3402 L'.KType => SOME (L.KType, loc)
adamc@493 3403 | L'.KArrow (k1, k2) =>
adamc@493 3404 (case (decompileKind k1, decompileKind k2) of
adamc@493 3405 (SOME k1, SOME k2) => SOME (L.KArrow (k1, k2), loc)
adamc@493 3406 | _ => NONE)
adamc@493 3407 | L'.KName => SOME (L.KName, loc)
adamc@493 3408 | L'.KRecord k =>
adamc@493 3409 (case decompileKind k of
adamc@493 3410 SOME k => SOME (L.KRecord k, loc)
adamc@493 3411 | _ => NONE)
adamc@493 3412 | L'.KUnit => SOME (L.KUnit, loc)
adamc@493 3413 | L'.KTuple ks =>
adamc@493 3414 let
adamc@493 3415 val ks' = List.mapPartial decompileKind ks
adamc@493 3416 in
adamc@493 3417 if length ks' = length ks then
adamc@493 3418 SOME (L.KTuple ks', loc)
adamc@493 3419 else
adamc@493 3420 NONE
adamc@493 3421 end
adamc@493 3422
adamc@493 3423 | L'.KError => NONE
adamc@493 3424 | L'.KUnif (_, _, ref (SOME k)) => decompileKind k
adamc@493 3425 | L'.KUnif _ => NONE
adam@1302 3426 | L'.KTupleUnif (_, _, ref (SOME k)) => decompileKind k
adam@1302 3427 | L'.KTupleUnif _ => NONE
adamc@493 3428
adamc@623 3429 | L'.KRel _ => NONE
adamc@623 3430 | L'.KFun _ => NONE
adamc@623 3431
adam@1529 3432 fun maybeHnorm env c =
adam@1529 3433 hnormCon env c
adam@1529 3434 handle E.UnboundNamed _ => c
adam@1529 3435
adam@1529 3436 fun decompileCon env (c as (_, loc)) =
adam@1529 3437 case #1 (maybeHnorm env c) of
adamc@325 3438 L'.CRel i =>
adamc@325 3439 let
adamc@325 3440 val (s, _) = E.lookupCRel env i
adamc@325 3441 in
adamc@325 3442 SOME (L.CVar ([], s), loc)
adamc@325 3443 end
adamc@325 3444 | L'.CNamed i =>
adamc@325 3445 let
adamc@325 3446 val (s, _, _) = E.lookupCNamed env i
adamc@325 3447 in
adamc@325 3448 SOME (L.CVar ([], s), loc)
adamc@325 3449 end
adamc@325 3450 | L'.CModProj (m1, ms, x) =>
adamc@325 3451 let
adamc@325 3452 val (s, _) = E.lookupStrNamed env m1
adamc@325 3453 in
adamc@325 3454 SOME (L.CVar (s :: ms, x), loc)
adamc@325 3455 end
adamc@325 3456 | L'.CName s => SOME (L.CName s, loc)
adamc@325 3457 | L'.CRecord (_, xcs) =>
adamc@325 3458 let
adamc@325 3459 fun fields xcs =
adamc@325 3460 case xcs of
adamc@325 3461 [] => SOME []
adamc@325 3462 | (x, t) :: xcs =>
adamc@325 3463 case (decompileCon env x, decompileCon env t, fields xcs) of
adamc@325 3464 (SOME x, SOME t, SOME xcs) => SOME ((x, t) :: xcs)
adamc@325 3465 | _ => NONE
adamc@325 3466 in
adamc@325 3467 Option.map (fn xcs => (L.CRecord xcs, loc))
adamc@325 3468 (fields xcs)
adamc@325 3469 end
adamc@325 3470 | L'.CConcat (c1, c2) =>
adamc@325 3471 (case (decompileCon env c1, decompileCon env c2) of
adamc@325 3472 (SOME c1, SOME c2) => SOME (L.CConcat (c1, c2), loc)
adamc@325 3473 | _ => NONE)
adamc@325 3474 | L'.CUnit => SOME (L.CUnit, loc)
adam@1303 3475 | L'.CUnif (nl, _, _, _, ref (SOME c)) => decompileCon env (E.mliftConInCon nl c)
adamc@325 3476
adam@1585 3477 | L'.CApp (f, x) =>
adam@1585 3478 (case (decompileCon env f, decompileCon env x) of
adam@1585 3479 (SOME f, SOME x) => SOME (L.CApp (f, x), loc)
adam@1585 3480 | _ => NONE)
adam@1585 3481
adam@1585 3482 | c => (Print.preface ("WTF?", p_con env (c, loc)); NONE)
adamc@325 3483
adamc@1264 3484 fun buildNeeded env sgis =
adamc@1275 3485 #1 (foldl (fn ((sgi, loc), (nd, env')) =>
adamc@1264 3486 (case sgi of
adam@1570 3487 L'.SgiCon (x, _, k, _) => naddCon (nd, x, k)
adam@1570 3488 | L'.SgiConAbs (x, _, k) => naddCon (nd, x, k)
adamc@1264 3489 | L'.SgiConstraint cs => naddConstraint (nd, (env', cs, loc))
adamc@1264 3490 | L'.SgiVal (x, _, t) =>
adamc@1264 3491 let
adamc@1277 3492 fun should t =
adamc@1277 3493 let
adamc@1277 3494 val t = normClassConstraint env' t
adamc@1277 3495 in
adamc@1277 3496 case #1 t of
adamc@1277 3497 L'.CApp (f, _) => isClassOrFolder env' f
adamc@1277 3498 | L'.TRecord t =>
adamc@1277 3499 (case hnormCon env' t of
adamc@1277 3500 (L'.CApp (f, _), _) =>
adamc@1277 3501 (case hnormCon env' f of
adamc@1277 3502 (L'.CApp (f, cl), loc) =>
adamc@1277 3503 (case hnormCon env' f of
adamc@1277 3504 (L'.CMap _, _) => isClassOrFolder env' cl
adamc@1277 3505 | _ => false)
adamc@1277 3506 | _ => false)
adamc@1277 3507 | _ => false)
adamc@1277 3508 | _ => false
adamc@1277 3509 end
adamc@1270 3510 in
adamc@1277 3511 if should t then
adamc@1277 3512 naddVal (nd, x)
adamc@1277 3513 else
adamc@1277 3514 nd
adamc@1264 3515 end
adamc@1264 3516 | L'.SgiStr (x, _, s) =>
adamc@1270 3517 (case #1 (hnormSgn env' s) of
adamc@1270 3518 L'.SgnConst sgis' => naddMod (nd, x, (env', buildNeeded env' sgis'))
adamc@1264 3519 | _ => nd)
adamc@1264 3520 | _ => nd,
adamc@1275 3521 E.sgiBinds env' (sgi, loc)))
adamc@1275 3522 (nempty, env) sgis)
adamc@1264 3523
adamc@1264 3524 val nd = buildNeeded env sgis
adamc@1264 3525
adamc@1264 3526 fun removeUsed (nd, ds) =
adamc@1264 3527 foldl (fn ((d, _), nd) =>
adamc@634 3528 case d of
adamc@1264 3529 L.DCon (x, _, _) => ndelCon (nd, x)
adamc@1264 3530 | L.DClass (x, _, _) => ndelCon (nd, x)
adamc@1264 3531 | L.DVal (x, _, _) => ndelVal (nd, x)
adamc@1264 3532 | L.DOpen _ => nempty
adamc@1264 3533 | L.DStr (x, _, (L.StrConst ds', _)) =>
adamc@1264 3534 (case SM.find (nmods nd, x) of
adamc@1264 3535 NONE => nd
adamc@1270 3536 | SOME (env, nd') => naddMod (nd, x, (env, removeUsed (nd', ds'))))
adamc@1264 3537 | _ => nd)
adamc@1264 3538 nd ds
adamc@1264 3539
adamc@1264 3540 val nd = removeUsed (nd, ds)
adamc@1264 3541
adamc@1270 3542 fun extend (env, nd, ds) =
adamc@1264 3543 let
adamc@1264 3544 val ds' = List.mapPartial (fn (env', (c1, c2), loc) =>
adamc@1264 3545 case (decompileCon env' c1, decompileCon env' c2) of
adamc@1264 3546 (SOME c1, SOME c2) =>
adamc@1264 3547 SOME (L.DConstraint (c1, c2), loc)
adam@1586 3548 | _ => NONE) (nconstraints nd)
adamc@1264 3549
adamc@1264 3550 val ds' =
adamc@1264 3551 case SS.listItems (nvals nd) of
adamc@1264 3552 [] => ds'
adamc@1264 3553 | xs =>
adamc@1264 3554 let
adamc@1264 3555 val ewild = (L.EWild, #2 str)
adamc@1264 3556 val ds'' = map (fn x => (L.DVal (x, NONE, ewild), #2 str)) xs
adamc@1264 3557 in
adamc@1264 3558 ds'' @ ds'
adamc@1264 3559 end
adamc@1264 3560
adamc@1275 3561 val ds' =
adamc@1264 3562 case SM.listItemsi (ncons nd) of
adamc@1275 3563 [] => ds'
adamc@1264 3564 | xs =>
adam@1570 3565 map (fn (x, k) =>
adamc@1270 3566 let
adamc@1270 3567 val k =
adamc@1270 3568 case decompileKind k of
adamc@1270 3569 NONE => (L.KWild, #2 str)
adamc@1270 3570 | SOME k => k
adamc@1275 3571
adamc@1270 3572 val cwild = (L.CWild k, #2 str)
adamc@1270 3573 in
adam@1570 3574 (L.DCon (x, NONE, cwild), #2 str)
adamc@1275 3575 end) xs @ ds'
adamc@1275 3576
adamc@1275 3577 val ds = ds @ ds'
adamc@1264 3578 in
adamc@1270 3579 map (fn d as (L.DStr (x, s, (L.StrConst ds', loc')), loc) =>
adamc@1270 3580 (case SM.find (nmods nd, x) of
adamc@1270 3581 NONE => d
adamc@1270 3582 | SOME (env, nd') =>
adamc@1270 3583 (L.DStr (x, s, (L.StrConst (extend (env, nd', ds')), loc')), loc))
adamc@1270 3584 | d => d) ds
adamc@1264 3585 end
adamc@325 3586 in
adamc@1270 3587 (L.StrConst (extend (env, nd, ds)), #2 str)
adamc@325 3588 end
adamc@325 3589 | _ => str)
adamc@325 3590 | _ => str
adamc@325 3591
adamc@706 3592 and elabDecl (dAll as (d, loc), (env, denv, gs)) =
adamc@255 3593 let
adamc@904 3594 (*val () = preface ("elabDecl", SourcePrint.p_decl dAll)*)
adamc@514 3595 (*val befor = Time.now ()*)
adamc@255 3596
adamc@255 3597 val r =
adamc@255 3598 case d of
adamc@255 3599 L.DCon (x, ko, c) =>
adamc@255 3600 let
adamc@255 3601 val k' = case ko of
adamc@255 3602 NONE => kunif loc
adamc@623 3603 | SOME k => elabKind env k
adamc@255 3604
adamc@255 3605 val (c', ck, gs') = elabCon (env, denv) c
adamc@255 3606 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@255 3607 in
adamc@255 3608 checkKind env c' ck k';
adamc@255 3609
adamc@255 3610 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3611 end
adamc@805 3612 | L.DDatatype dts =>
adamc@255 3613 let
adamc@255 3614 val k = (L'.KType, loc)
adamc@805 3615
adamc@805 3616 val (dts, env) = ListUtil.foldlMap
adamc@805 3617 (fn ((x, xs, xcs), env) =>
adamc@805 3618 let
adamc@805 3619 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@805 3620 val (env, n) = E.pushCNamed env x k' NONE
adamc@805 3621 in
adamc@805 3622 ((x, n, xs, xcs), env)
adamc@805 3623 end)
adamc@805 3624 env dts
adamc@805 3625
adamc@805 3626 val (dts, (env, gs')) = ListUtil.foldlMap
adamc@805 3627 (fn ((x, n, xs, xcs), (env, gs')) =>
adamc@805 3628 let
adamc@805 3629 val t = (L'.CNamed n, loc)
adamc@805 3630 val nxs = length xs - 1
adamc@805 3631 val t = ListUtil.foldli
adamc@805 3632 (fn (i, _, t) =>
adamc@805 3633 (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@805 3634
adamc@805 3635 val (env', denv') = foldl (fn (x, (env', denv')) =>
adamc@805 3636 (E.pushCRel env' x k,
adamc@805 3637 D.enter denv')) (env, denv) xs
adamc@805 3638
adamc@805 3639 val (xcs, (used, env, gs')) =
adamc@805 3640 ListUtil.foldlMap
adamc@805 3641 (fn ((x, to), (used, env, gs)) =>
adamc@805 3642 let
adamc@805 3643 val (to, t, gs') = case to of
adamc@805 3644 NONE => (NONE, t, gs)
adamc@805 3645 | SOME t' =>
adamc@805 3646 let
adamc@805 3647 val (t', tk, gs') = elabCon (env', denv') t'
adamc@805 3648 in
adamc@805 3649 checkKind env' t' tk k;
adamc@805 3650 (SOME t', (L'.TFun (t', t), loc), enD gs' @ gs)
adamc@805 3651 end
adamc@805 3652 val t = foldr (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
adamc@805 3653
adamc@805 3654 val (env, n') = E.pushENamed env x t
adamc@805 3655 in
adamc@805 3656 if SS.member (used, x) then
adamc@805 3657 strError env (DuplicateConstructor (x, loc))
adamc@805 3658 else
adamc@805 3659 ();
adamc@805 3660 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@805 3661 end)
adamc@805 3662 (SS.empty, env, gs') xcs
adamc@805 3663 in
adamc@805 3664 ((x, n, xs, xcs), (E.pushDatatype env n xs xcs, gs'))
adamc@805 3665 end)
adamc@805 3666 (env, []) dts
adamc@255 3667 in
adamc@805 3668 ([(L'.DDatatype dts, loc)], (env, denv, gs' @ gs))
adamc@255 3669 end
adamc@255 3670
adamc@255 3671 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
adamc@255 3672
adamc@255 3673 | L.DDatatypeImp (x, m1 :: ms, s) =>
adamc@255 3674 (case E.lookupStr env m1 of
adamc@255 3675 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@255 3676 ([], (env, denv, gs)))
adamc@255 3677 | SOME (n, sgn) =>
adamc@255 3678 let
adamc@255 3679 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@255 3680 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@255 3681 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@255 3682 (strerror, sgnerror))
adamc@255 3683 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@255 3684 ((L'.StrVar n, loc), sgn) ms
adamc@255 3685 in
adamc@628 3686 case hnormCon env (L'.CModProj (n, ms, s), loc) of
adamc@628 3687 (L'.CModProj (n, ms, s), _) =>
adamc@255 3688 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@255 3689 NONE => (conError env (UnboundDatatype (loc, s));
adamc@255 3690 ([], (env, denv, gs)))
adamc@255 3691 | SOME (xs, xncs) =>
adamc@255 3692 let
adamc@255 3693 val k = (L'.KType, loc)
adamc@255 3694 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@255 3695 val t = (L'.CModProj (n, ms, s), loc)
adamc@255 3696 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@255 3697 val env = E.pushDatatype env n' xs xncs
adamc@255 3698
adamc@255 3699 val t = (L'.CNamed n', loc)
adamc@844 3700 val nxs = length xs
adamc@844 3701 val t = ListUtil.foldli (fn (i, _, t) =>
adamc@844 3702 (L'.CApp (t, (L'.CRel (nxs - 1 - i), loc)), loc))
adamc@844 3703 t xs
adamc@255 3704 val env = foldl (fn ((x, n, to), env) =>
adamc@255 3705 let
adamc@255 3706 val t = case to of
adamc@255 3707 NONE => t
adamc@255 3708 | SOME t' => (L'.TFun (t', t), loc)
adamc@255 3709
adamc@255 3710 val t = foldr (fn (x, t) =>
adamc@255 3711 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@255 3712 t xs
adamc@255 3713 in
adamc@255 3714 E.pushENamedAs env x n t
adamc@255 3715 end) env xncs
adamc@255 3716 in
adamc@628 3717 ([(L'.DDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, gs))
adamc@255 3718 end)
adamc@255 3719 | _ => (strError env (NotDatatype loc);
adamc@255 3720 ([], (env, denv, [])))
adamc@255 3721 end)
adamc@255 3722
adamc@255 3723 | L.DVal (x, co, e) =>
adamc@255 3724 let
adamc@255 3725 val (c', _, gs1) = case co of
adamc@255 3726 NONE => (cunif (loc, ktype), ktype, [])
adamc@255 3727 | SOME c => elabCon (env, denv) c
adamc@255 3728
adamc@255 3729 val (e', et, gs2) = elabExp (env, denv) e
adamc@629 3730
adamc@629 3731 val () = checkCon env e' et c'
adamc@629 3732
adamc@674 3733 val c' = normClassConstraint env c'
adamc@255 3734 val (env', n) = E.pushENamed env x c'
adamc@255 3735 in
adamc@255 3736 (*prefaces "DVal" [("x", Print.PD.string x),
adamc@362 3737 ("c'", p_con env c')];*)
adamc@628 3738 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, enD gs1 @ gs2 @ gs))
adamc@255 3739 end
adamc@255 3740 | L.DValRec vis =>
adamc@255 3741 let
adamc@255 3742 fun allowable (e, _) =
adamc@255 3743 case e of
adamc@255 3744 L.EAbs _ => true
adamc@255 3745 | L.ECAbs (_, _, _, e) => allowable e
adamc@623 3746 | L.EKAbs (_, e) => allowable e
adamc@255 3747 | L.EDisjoint (_, _, e) => allowable e
adamc@255 3748 | _ => false
adamc@255 3749
adamc@255 3750 val (vis, gs) = ListUtil.foldlMap
adamc@255 3751 (fn ((x, co, e), gs) =>
adamc@255 3752 let
adamc@255 3753 val (c', _, gs1) = case co of
adamc@255 3754 NONE => (cunif (loc, ktype), ktype, [])
adamc@255 3755 | SOME c => elabCon (env, denv) c
adamc@674 3756 val c' = normClassConstraint env c'
adamc@255 3757 in
adamc@255 3758 ((x, c', e), enD gs1 @ gs)
adamc@280 3759 end) gs vis
adamc@255 3760
adamc@255 3761 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@255 3762 let
adamc@255 3763 val (env, n) = E.pushENamed env x c'
adamc@255 3764 in
adamc@255 3765 ((x, n, c', e), env)
adamc@255 3766 end) env vis
adamc@255 3767
adamc@255 3768 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@255 3769 let
adamc@255 3770 val (e', et, gs1) = elabExp (env, denv) e
adamc@255 3771 in
adamc@628 3772 checkCon env e' et c';
adamc@255 3773 if allowable e then
adamc@255 3774 ()
adamc@255 3775 else
adamc@255 3776 expError env (IllegalRec (x, e'));
adamc@628 3777 ((x, n, c', e'), gs1 @ gs)
adamc@255 3778 end) gs vis
adamc@255 3779 in
adamc@255 3780 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@255 3781 end
adamc@255 3782
adamc@255 3783 | L.DSgn (x, sgn) =>
adamc@255 3784 let
adamc@255 3785 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@255 3786 val (env', n) = E.pushSgnNamed env x sgn'
adamc@255 3787 in
adamc@255 3788 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3789 end
adamc@255 3790
adamc@255 3791 | L.DStr (x, sgno, str) =>
adamc@255 3792 let
adamc@255 3793 val () = if x = "Basis" then
adamc@255 3794 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@255 3795 else
adamc@255 3796 ()
adamc@255 3797
adamc@255 3798 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@255 3799
adamc@255 3800 val (str', sgn', gs') =
adamc@255 3801 case formal of
adamc@255 3802 NONE =>
adamc@255 3803 let
adamc@255 3804 val (str', actual, gs') = elabStr (env, denv) str
adamc@255 3805 in
adamc@255 3806 (str', selfifyAt env {str = str', sgn = actual}, gs')
adamc@255 3807 end
adamc@255 3808 | SOME (formal, gs1) =>
adamc@255 3809 let
adamc@325 3810 val str = wildifyStr env (str, formal)
adamc@255 3811 val (str', actual, gs2) = elabStr (env, denv) str
adamc@255 3812 in
adamc@1000 3813 subSgn env loc (selfifyAt env {str = str', sgn = actual}) formal;
adamc@255 3814 (str', formal, enD gs1 @ gs2)
adamc@255 3815 end
adamc@255 3816
adamc@255 3817 val (env', n) = E.pushStrNamed env x sgn'
adamc@834 3818 val denv' =
adamc@834 3819 case #1 str' of
adamc@834 3820 L'.StrConst _ => dopenConstraints (loc, env', denv) {str = x, strs = []}
adamc@1022 3821 | L'.StrApp _ => dopenConstraints (loc, env', denv) {str = x, strs = []}
adamc@834 3822 | _ => denv
adamc@255 3823 in
adamc@255 3824 case #1 (hnormSgn env sgn') of
adamc@255 3825 L'.SgnFun _ =>
adamc@255 3826 (case #1 str' of
adamc@255 3827 L'.StrFun _ => ()
adamc@255 3828 | _ => strError env (FunctorRebind loc))
adamc@255 3829 | _ => ();
adamc@834 3830 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv', gs' @ gs))
adamc@255 3831 end
adamc@255 3832
adamc@255 3833 | L.DFfiStr (x, sgn) =>
adamc@255 3834 let
adamc@255 3835 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@255 3836
adamc@255 3837 val (env', n) = E.pushStrNamed env x sgn'
adamc@255 3838 in
adamc@255 3839 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, enD gs' @ gs))
adamc@255 3840 end
adamc@255 3841
adamc@255 3842 | L.DOpen (m, ms) =>
adamc@255 3843 (case E.lookupStr env m of
adamc@255 3844 NONE => (strError env (UnboundStr (loc, m));
adamc@255 3845 ([], (env, denv, gs)))
adamc@255 3846 | SOME (n, sgn) =>
adamc@255 3847 let
adamc@1030 3848 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@1030 3849 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@1030 3850 NONE => (strError env (UnboundStr (loc, m));
adamc@1030 3851 (strerror, sgnerror))
adamc@1030 3852 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@1030 3853 ((L'.StrVar n, loc), sgn) ms
adamc@1030 3854
adamc@1030 3855 val sgn = selfifyAt env {str = str, sgn = sgn}
adamc@255 3856
adamc@628 3857 val (ds, env') = dopen env {str = n, strs = ms, sgn = sgn}
adamc@628 3858 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = ms}
adamc@255 3859 in
adamc@255 3860 (ds, (env', denv', gs))
adamc@255 3861 end)
adamc@255 3862
adamc@255 3863 | L.DConstraint (c1, c2) =>
adamc@255 3864 let
adamc@255 3865 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@255 3866 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@255 3867 val gs3 = D.prove env denv (c1', c2', loc)
adamc@255 3868
adamc@628 3869 val denv' = D.assert env denv (c1', c2')
adamc@255 3870 in
adamc@255 3871 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@255 3872 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@255 3873
adamc@628 3874 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', enD gs1 @ enD gs2 @ enD gs3 @ gs))
adamc@255 3875 end
adamc@255 3876
adamc@255 3877 | L.DOpenConstraints (m, ms) =>
adamc@255 3878 let
adamc@255 3879 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@255 3880 in
adamc@255 3881 ([], (env, denv, gs))
adamc@255 3882 end
adamc@255 3883
adamc@255 3884 | L.DExport str =>
adamc@255 3885 let
adamc@255 3886 val (str', sgn, gs') = elabStr (env, denv) str
adamc@255 3887
adamc@255 3888 val sgn =
adamc@255 3889 case #1 (hnormSgn env sgn) of
adamc@255 3890 L'.SgnConst sgis =>
adamc@255 3891 let
adamc@255 3892 fun doOne (all as (sgi, _), env) =
adamc@255 3893 (case sgi of
adamc@255 3894 L'.SgiVal (x, n, t) =>
adamc@325 3895 let
adamc@325 3896 fun doPage (makeRes, ran) =
adamc@628 3897 case hnormCon env ran of
adamc@628 3898 (L'.CApp (tf, arg), _) =>
adamc@628 3899 (case (hnormCon env tf, hnormCon env arg) of
adamc@628 3900 ((L'.CModProj (basis, [], "transaction"), _),
adamc@720 3901 (L'.CApp (tf, arg3), _)) =>
adamc@325 3902 (case (basis = !basis_r,
adamc@720 3903 hnormCon env tf, hnormCon env arg3) of
adamc@325 3904 (true,
adamc@720 3905 (L'.CApp (tf, arg2), _),
adamc@628 3906 ((L'.CRecord (_, []), _))) =>
adamc@720 3907 (case (hnormCon env tf) of
adamc@720 3908 (L'.CApp (tf, arg1), _) =>
adamc@720 3909 (case (hnormCon env tf,
adamc@720 3910 hnormCon env arg1,
adamc@720 3911 hnormCon env arg2) of
adamc@720 3912 (tf, arg1,
adamc@720 3913 (L'.CRecord (_, []), _)) =>
adamc@720 3914 let
adamc@720 3915 val t = (L'.CApp (tf, arg1), loc)
adamc@720 3916 val t = (L'.CApp (t, arg2), loc)
adamc@720 3917 val t = (L'.CApp (t, arg3), loc)
adamc@720 3918 val t = (L'.CApp (
adamc@720 3919 (L'.CModProj
adamc@720 3920 (basis, [], "transaction"), loc),
adamc@325 3921 t), loc)
adam@1347 3922
adam@1347 3923 fun normArgs t =
adam@1347 3924 case hnormCon env t of
adam@1347 3925 (L'.TFun (dom, ran), loc) =>
adam@1347 3926 (L'.TFun (hnormCon env dom, normArgs ran), loc)
adam@1347 3927 | t' => t'
adamc@720 3928 in
adam@1347 3929 (L'.SgiVal (x, n, normArgs (makeRes t)), loc)
adamc@720 3930 end
adamc@325 3931 | _ => all)
adamc@325 3932 | _ => all)
adamc@325 3933 | _ => all)
adamc@325 3934 | _ => all)
adamc@325 3935 | _ => all
adamc@325 3936 in
adamc@628 3937 case hnormCon env t of
adamc@628 3938 (L'.TFun (dom, ran), _) =>
adamc@628 3939 (case hnormCon env dom of
adamc@628 3940 (L'.TRecord domR, _) =>
adamc@325 3941 doPage (fn t => (L'.TFun ((L'.TRecord domR,
adamc@325 3942 loc),
adamc@325 3943 t), loc), ran)
adamc@325 3944 | _ => all)
adamc@325 3945 | _ => doPage (fn t => t, t)
adamc@325 3946 end
adamc@255 3947 | _ => all,
adamc@255 3948 E.sgiBinds env all)
adamc@255 3949 in
adamc@255 3950 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@255 3951 end
adamc@255 3952 | _ => sgn
adamc@255 3953 in
adamc@255 3954 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs' @ gs))
adamc@255 3955 end
adamc@255 3956
adamc@707 3957 | L.DTable (x, c, pe, ce) =>
adamc@255 3958 let
adamc@707 3959 val cstK = (L'.KRecord (L'.KRecord (L'.KUnit, loc), loc), loc)
adamc@707 3960
adamc@255 3961 val (c', k, gs') = elabCon (env, denv) c
adamc@707 3962 val pkey = cunif (loc, cstK)
adamc@707 3963 val uniques = cunif (loc, cstK)
adamc@705 3964
adamc@705 3965 val ct = tableOf ()
adamc@705 3966 val ct = (L'.CApp (ct, c'), loc)
adamc@707 3967 val ct = (L'.CApp (ct, (L'.CConcat (pkey, uniques), loc)), loc)
adamc@705 3968
adamc@705 3969 val (env, n) = E.pushENamed env x ct
adamc@707 3970 val (pe', pet, gs'') = elabExp (env, denv) pe
adamc@707 3971 val (ce', cet, gs''') = elabExp (env, denv) ce
adamc@707 3972
adamc@707 3973 val pst = (L'.CModProj (!basis_r, [], "primary_key"), loc)
adamc@707 3974 val pst = (L'.CApp (pst, c'), loc)
adamc@707 3975 val pst = (L'.CApp (pst, pkey), loc)
adamc@704 3976
adamc@704 3977 val cst = (L'.CModProj (!basis_r, [], "sql_constraints"), loc)
adamc@704 3978 val cst = (L'.CApp (cst, c'), loc)
adamc@705 3979 val cst = (L'.CApp (cst, uniques), loc)
adamc@255 3980 in
adamc@255 3981 checkKind env c' k (L'.KRecord (L'.KType, loc), loc);
adamc@707 3982 checkCon env pe' pet pst;
adamc@707 3983 checkCon env ce' cet cst;
adamc@707 3984 ([(L'.DTable (!basis_r, x, n, c', pe', pkey, ce', uniques), loc)],
adamc@707 3985 (env, denv, gs''' @ gs'' @ enD gs' @ gs))
adamc@255 3986 end
adamc@338 3987 | L.DSequence x =>
adamc@338 3988 let
adamc@338 3989 val (env, n) = E.pushENamed env x (sequenceOf ())
adamc@338 3990 in
adamc@338 3991 ([(L'.DSequence (!basis_r, x, n), loc)], (env, denv, gs))
adamc@338 3992 end
adamc@754 3993 | L.DView (x, e) =>
adamc@754 3994 let
adamc@754 3995 val (e', t, gs') = elabExp (env, denv) e
adamc@754 3996
adamc@754 3997 val k = (L'.KRecord (L'.KType, loc), loc)
adamc@754 3998 val fs = cunif (loc, k)
adamc@754 3999 val ts = cunif (loc, (L'.KRecord k, loc))
adamc@754 4000 val tf = (L'.CApp ((L'.CMap (k, k), loc),
adamc@754 4001 (L'.CAbs ("_", k, (L'.CRecord ((L'.KType, loc), []), loc)), loc)), loc)
adamc@754 4002 val ts = (L'.CApp (tf, ts), loc)
adamc@754 4003
adamc@754 4004 val cv = viewOf ()
adamc@754 4005 val cv = (L'.CApp (cv, fs), loc)
adamc@754 4006 val (env', n) = E.pushENamed env x cv
adamc@754 4007
adamc@754 4008 val ct = queryOf ()
adamc@1191 4009 val ct = (L'.CApp (ct, (L'.CRecord ((L'.KRecord (L'.KType, loc), loc), []), loc)), loc)
adam@1395 4010 val ct = (L'.CApp (ct, (L'.CRecord ((L'.KRecord (L'.KType, loc), loc), []), loc)), loc)
adamc@754 4011 val ct = (L'.CApp (ct, ts), loc)
adamc@754 4012 val ct = (L'.CApp (ct, fs), loc)
adamc@754 4013 in
adamc@754 4014 checkCon env e' t ct;
adamc@754 4015 ([(L'.DView (!basis_r, x, n, e', fs), loc)],
adamc@754 4016 (env', denv, gs' @ gs))
adamc@754 4017 end
adamc@255 4018
adamc@563 4019 | L.DClass (x, k, c) =>
adamc@255 4020 let
adamc@623 4021 val k = elabKind env k
adamc@280 4022 val (c', ck, gs') = elabCon (env, denv) c
adamc@711 4023 val (env, n) = E.pushCNamed env x k (SOME c')
adamc@255 4024 val env = E.pushClass env n
adamc@255 4025 in
adamc@711 4026 checkKind env c' ck k;
adamc@563 4027 ([(L'.DClass (x, n, k, c'), loc)], (env, denv, enD gs' @ gs))
adamc@255 4028 end
adamc@271 4029
adamc@280 4030 | L.DDatabase s => ([(L'.DDatabase s, loc)], (env, denv, gs))
adamc@280 4031
adamc@459 4032 | L.DCookie (x, c) =>
adamc@459 4033 let
adamc@459 4034 val (c', k, gs') = elabCon (env, denv) c
adamc@459 4035 val (env, n) = E.pushENamed env x (L'.CApp (cookieOf (), c'), loc)
adamc@459 4036 in
adamc@459 4037 checkKind env c' k (L'.KType, loc);
adamc@459 4038 ([(L'.DCookie (!basis_r, x, n, c'), loc)], (env, denv, enD gs' @ gs))
adamc@459 4039 end
adamc@720 4040 | L.DStyle x =>
adamc@718 4041 let
adamc@720 4042 val (env, n) = E.pushENamed env x (styleOf ())
adamc@718 4043 in
adamc@720 4044 ([(L'.DStyle (!basis_r, x, n), loc)], (env, denv, gs))
adamc@718 4045 end
adamc@1075 4046 | L.DTask (e1, e2) =>
adamc@1073 4047 let
adamc@1075 4048 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@1075 4049 val (e2', t2, gs2) = elabExp (env, denv) e2
adamc@1075 4050
adam@1348 4051 val targ = cunif (loc, (L'.KType, loc))
adam@1348 4052
adamc@1075 4053 val t1' = (L'.CModProj (!basis_r, [], "task_kind"), loc)
adam@1348 4054 val t1' = (L'.CApp (t1', targ), loc)
adam@1348 4055
adamc@1075 4056 val t2' = (L'.CApp ((L'.CModProj (!basis_r, [], "transaction"), loc),
adamc@1075 4057 (L'.TRecord (L'.CRecord ((L'.KType, loc), []), loc), loc)), loc)
adam@1348 4058 val t2' = (L'.TFun (targ, t2'), loc)
adamc@1073 4059 in
adamc@1075 4060 checkCon env e1' t1 t1';
adamc@1075 4061 checkCon env e2' t2 t2';
adamc@1075 4062 ([(L'.DTask (e1', e2'), loc)], (env, denv, gs2 @ gs1 @ gs))
adamc@1073 4063 end
adamc@1199 4064 | L.DPolicy e1 =>
adamc@1199 4065 let
adamc@1199 4066 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@1199 4067
adamc@1199 4068 val t1' = (L'.CModProj (!basis_r, [], "sql_policy"), loc)
adamc@1199 4069 in
adamc@1199 4070 checkCon env e1' t1 t1';
adamc@1199 4071 ([(L'.DPolicy e1', loc)], (env, denv, gs1 @ gs))
adamc@1199 4072 end
adamc@459 4073
adam@1294 4074 | L.DOnError (m1, ms, s) =>
adam@1294 4075 (case E.lookupStr env m1 of
adam@1294 4076 NONE => (expError env (UnboundStrInExp (loc, m1));
adam@1294 4077 ([], (env, denv, [])))
adam@1294 4078 | SOME (n, sgn) =>
adam@1294 4079 let
adam@1294 4080 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adam@1294 4081 case E.projectStr env {sgn = sgn, str = str, field = m} of
adam@1294 4082 NONE => (conError env (UnboundStrInCon (loc, m));
adam@1294 4083 (strerror, sgnerror))
adam@1294 4084 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adam@1294 4085 ((L'.StrVar n, loc), sgn) ms
adam@1294 4086
adam@1294 4087 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adam@1294 4088 NONE => (expError env (UnboundExp (loc, s));
adam@1294 4089 cerror)
adam@1294 4090 | SOME t => t
adam@1294 4091
adam@1294 4092 val page = (L'.CModProj (!basis_r, [], "page"), loc)
adam@1294 4093 val xpage = (L'.CApp ((L'.CModProj (!basis_r, [], "transaction"), loc), page), loc)
adam@1294 4094 val func = (L'.TFun ((L'.CModProj (!basis_r, [], "xbody"), loc), xpage), loc)
adam@1294 4095 in
adam@1407 4096 (unifyCons env loc t func
adam@1407 4097 handle CUnify _ => ErrorMsg.error "onError handler has wrong type.");
adam@1294 4098 ([(L'.DOnError (n, ms, s), loc)], (env, denv, gs))
adam@1294 4099 end)
adam@1294 4100
adamc@280 4101 (*val tcs = List.filter (fn TypeClass _ => true | _ => false) (#3 (#2 r))*)
adamc@255 4102 in
adamc@904 4103 (*prefaces "/elabDecl" [("d", SourcePrint.p_decl dAll)];*)
adamc@255 4104 r
adamc@255 4105 end
adamc@211 4106
adamc@83 4107 and elabStr (env, denv) (str, loc) =
adamc@31 4108 case str of
adamc@31 4109 L.StrConst ds =>
adamc@31 4110 let
adamc@88 4111 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 4112 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 4113
adamc@63 4114 val (sgis, _, _, _, _) =
adamc@64 4115 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 4116 case sgi of
adamc@64 4117 L'.SgiConAbs (x, n, k) =>
adamc@64 4118 let
adamc@64 4119 val (cons, x) =
adamc@64 4120 if SS.member (cons, x) then
adamc@64 4121 (cons, "?" ^ x)
adamc@64 4122 else
adamc@64 4123 (SS.add (cons, x), x)
adamc@64 4124 in
adamc@64 4125 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 4126 end
adamc@64 4127 | L'.SgiCon (x, n, k, c) =>
adamc@64 4128 let
adamc@64 4129 val (cons, x) =
adamc@64 4130 if SS.member (cons, x) then
adamc@64 4131 (cons, "?" ^ x)
adamc@64 4132 else
adamc@64 4133 (SS.add (cons, x), x)
adamc@64 4134 in
adamc@64 4135 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 4136 end
adamc@805 4137 | L'.SgiDatatype dts =>
adamc@156 4138 let
adamc@805 4139 fun doOne ((x, n, xs, xncs), (cons, vals)) =
adamc@805 4140 let
adamc@805 4141 val (cons, x) =
adamc@805 4142 if SS.member (cons, x) then
adamc@805 4143 (cons, "?" ^ x)
adamc@156 4144 else
adamc@805 4145 (SS.add (cons, x), x)
adamc@805 4146
adamc@805 4147 val (xncs, vals) =
adamc@805 4148 ListUtil.foldlMap
adamc@805 4149 (fn ((x, n, t), vals) =>
adamc@805 4150 if SS.member (vals, x) then
adamc@805 4151 (("?" ^ x, n, t), vals)
adamc@805 4152 else
adamc@805 4153 ((x, n, t), SS.add (vals, x)))
adamc@805 4154 vals xncs
adamc@805 4155 in
adamc@805 4156 ((x, n, xs, xncs), (cons, vals))
adamc@805 4157 end
adamc@805 4158
adamc@805 4159 val (dts, (cons, vals)) = ListUtil.foldlMap doOne (cons, vals) dts
adamc@156 4160 in
adamc@805 4161 ((L'.SgiDatatype dts, loc) :: sgis, cons, vals, sgns, strs)
adamc@156 4162 end
adamc@191 4163 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@156 4164 let
adamc@156 4165 val (cons, x) =
adamc@156 4166 if SS.member (cons, x) then
adamc@156 4167 (cons, "?" ^ x)
adamc@156 4168 else
adamc@156 4169 (SS.add (cons, x), x)
adamc@156 4170 in
adamc@191 4171 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 4172 end
adamc@64 4173 | L'.SgiVal (x, n, c) =>
adamc@64 4174 let
adamc@64 4175 val (vals, x) =
adamc@64 4176 if SS.member (vals, x) then
adamc@64 4177 (vals, "?" ^ x)
adamc@64 4178 else
adamc@64 4179 (SS.add (vals, x), x)
adamc@64 4180 in
adamc@64 4181 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 4182 end
adamc@64 4183 | L'.SgiSgn (x, n, sgn) =>
adamc@64 4184 let
adamc@64 4185 val (sgns, x) =
adamc@64 4186 if SS.member (sgns, x) then
adamc@64 4187 (sgns, "?" ^ x)
adamc@64 4188 else
adamc@64 4189 (SS.add (sgns, x), x)
adamc@64 4190 in
adamc@64 4191 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 4192 end
adamc@64 4193
adamc@64 4194 | L'.SgiStr (x, n, sgn) =>
adamc@64 4195 let
adamc@64 4196 val (strs, x) =
adamc@64 4197 if SS.member (strs, x) then
adamc@64 4198 (strs, "?" ^ x)
adamc@64 4199 else
adamc@64 4200 (SS.add (strs, x), x)
adamc@64 4201 in
adamc@64 4202 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 4203 end
adamc@203 4204 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs)
adamc@563 4205 | L'.SgiClassAbs (x, n, k) =>
adamc@211 4206 let
adamc@211 4207 val (cons, x) =
adamc@211 4208 if SS.member (cons, x) then
adamc@211 4209 (cons, "?" ^ x)
adamc@211 4210 else
adamc@211 4211 (SS.add (cons, x), x)
adamc@211 4212 in
adamc@563 4213 ((L'.SgiClassAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@211 4214 end
adamc@563 4215 | L'.SgiClass (x, n, k, c) =>
adamc@211 4216 let
adamc@211 4217 val (cons, x) =
adamc@211 4218 if SS.member (cons, x) then
adamc@211 4219 (cons, "?" ^ x)
adamc@211 4220 else
adamc@211 4221 (SS.add (cons, x), x)
adamc@211 4222 in
adamc@563 4223 ((L'.SgiClass (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@203 4224 end)
adamc@64 4225
adamc@63 4226 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 4227 in
adamc@83 4228 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 4229 end
adamc@31 4230 | L.StrVar x =>
adamc@31 4231 (case E.lookupStr env x of
adamc@31 4232 NONE =>
adamc@31 4233 (strError env (UnboundStr (loc, x));
adamc@83 4234 (strerror, sgnerror, []))
adamc@83 4235 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 4236 | L.StrProj (str, x) =>
adamc@34 4237 let
adamc@83 4238 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 4239 in
adamc@34 4240 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 4241 NONE => (strError env (UnboundStr (loc, x));
adamc@83 4242 (strerror, sgnerror, []))
adamc@83 4243 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 4244 end
adamc@41 4245 | L.StrFun (m, dom, ranO, str) =>
adamc@41 4246 let
adamc@83 4247 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 4248 val (env', n) = E.pushStrNamed env m dom'
adamc@834 4249 val denv' = dopenConstraints (loc, env', denv) {str = m, strs = []}
adamc@834 4250 val (str', actual, gs2) = elabStr (env', denv') str
adamc@41 4251
adamc@83 4252 val (formal, gs3) =
adamc@41 4253 case ranO of
adamc@83 4254 NONE => (actual, [])
adamc@41 4255 | SOME ran =>
adamc@41 4256 let
adamc@834 4257 val (ran', gs) = elabSgn (env', denv') ran
adamc@41 4258 in
adamc@1000 4259 subSgn env' loc actual ran';
adamc@83 4260 (ran', gs)
adamc@41 4261 end
adamc@41 4262 in
adamc@41 4263 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 4264 (L'.SgnFun (m, n, dom', formal), loc),
adamc@228 4265 enD gs1 @ gs2 @ enD gs3)
adamc@41 4266 end
adamc@44 4267 | L.StrApp (str1, str2) =>
adamc@44 4268 let
adamc@83 4269 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@325 4270 val str2 =
adamc@325 4271 case sgn1 of
adamc@325 4272 (L'.SgnFun (_, _, dom, _), _) =>
adamc@1275 4273 let
adamc@1275 4274 val s = wildifyStr env (str2, dom)
adamc@1275 4275 in
adamc@1275 4276 (*Print.preface ("Wild", SourcePrint.p_str s);*)
adamc@1275 4277 s
adamc@1275 4278 end
adamc@325 4279 | _ => str2
adamc@83 4280 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@1270 4281 in
adamc@44 4282 case #1 (hnormSgn env sgn1) of
adamc@83 4283 L'.SgnError => (strerror, sgnerror, [])
adamc@44 4284 | L'.SgnFun (m, n, dom, ran) =>
adamc@1000 4285 (subSgn env loc sgn2 dom;
adamc@44 4286 case #1 (hnormSgn env ran) of
adamc@83 4287 L'.SgnError => (strerror, sgnerror, [])
adamc@44 4288 | L'.SgnConst sgis =>
adamc@44 4289 ((L'.StrApp (str1', str2'), loc),
adamc@83 4290 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 4291 gs1 @ gs2)
adamc@44 4292 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 4293 | _ => (strError env (NotFunctor sgn1);
adamc@83 4294 (strerror, sgnerror, []))
adamc@44 4295 end
adamc@31 4296
adamc@987 4297 fun resolveClass env = E.resolveClass (hnormCon env) (consEq env dummy) env
adamc@833 4298
adamc@325 4299 fun elabFile basis topStr topSgn env file =
adamc@56 4300 let
adamc@713 4301 val () = mayDelay := true
adamc@713 4302 val () = delayedUnifs := []
adamc@830 4303 val () = delayedExhaustives := []
adamc@713 4304
adamc@83 4305 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 4306 val () = case gs of
adamc@83 4307 [] => ()
adamc@207 4308 | _ => (app (fn (_, env, _, c1, c2) =>
adamc@207 4309 prefaces "Unresolved"
adamc@207 4310 [("c1", p_con env c1),
adamc@207 4311 ("c2", p_con env c2)]) gs;
adamc@207 4312 raise Fail "Unresolved disjointness constraints in Basis")
adamc@83 4313
adamc@56 4314 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@210 4315 val () = basis_r := basis_n
adamc@56 4316
adamc@628 4317 val (ds, env') = dopen env' {str = basis_n, strs = [], sgn = sgn}
adamc@56 4318
adamc@56 4319 fun discoverC r x =
adamc@56 4320 case E.lookupC env' x of
adamc@56 4321 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 4322 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 4323 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 4324
adamc@56 4325 val () = discoverC int "int"
adamc@56 4326 val () = discoverC float "float"
adamc@56 4327 val () = discoverC string "string"
adamc@821 4328 val () = discoverC char "char"
adamc@203 4329 val () = discoverC table "sql_table"
adamc@56 4330
adamc@325 4331 val (topSgn, gs) = elabSgn (env', D.empty) (L.SgnConst topSgn, ErrorMsg.dummySpan)
adamc@325 4332 val () = case gs of
adamc@325 4333 [] => ()
adamc@325 4334 | _ => raise Fail "Unresolved disjointness constraints in top.urs"
adamc@325 4335 val (topStr, topSgn', gs) = elabStr (env', D.empty) (L.StrConst topStr, ErrorMsg.dummySpan)
adamc@325 4336 val () = case gs of
adamc@325 4337 [] => ()
adamc@329 4338 | _ => app (fn Disjoint (loc, env, denv, c1, c2) =>
adamc@329 4339 (case D.prove env denv (c1, c2, loc) of
adamc@329 4340 [] => ()
adamc@329 4341 | _ =>
adamc@329 4342 (prefaces "Unresolved constraint in top.ur"
adamc@329 4343 [("loc", PD.string (ErrorMsg.spanToString loc)),
adamc@329 4344 ("c1", p_con env c1),
adamc@720 4345 ("c2", p_con env c2)];
adamc@478 4346 raise Fail "Unresolved constraint in top.ur"))
adamc@564 4347 | TypeClass (env, c, r, loc) =>
adamc@564 4348 let
adamc@564 4349 val c = normClassKey env c
adamc@564 4350 in
adamc@833 4351 case resolveClass env c of
adamc@564 4352 SOME e => r := SOME e
adamc@564 4353 | NONE => expError env (Unresolvable (loc, c))
adamc@634 4354 end) gs
adamc@564 4355
adamc@1000 4356 val () = subSgn env' ErrorMsg.dummySpan topSgn' topSgn
adamc@720 4357
adamc@325 4358 val (env', top_n) = E.pushStrNamed env' "Top" topSgn
adamc@633 4359 val () = top_r := top_n
adamc@325 4360
adamc@628 4361 val (ds', env') = dopen env' {str = top_n, strs = [], sgn = topSgn}
adamc@325 4362
adamc@83 4363 fun elabDecl' (d, (env, gs)) =
adamc@76 4364 let
adamc@76 4365 val () = resetKunif ()
adamc@76 4366 val () = resetCunif ()
adam@1345 4367 val (ds, (env', _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 4368 in
adam@1345 4369 (**)
adam@1345 4370 (ds, (env', gs))
adamc@76 4371 end
adamc@76 4372
adam@1345 4373 val (file, (env'', gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@753 4374
adamc@777 4375 fun oneSummaryRound () =
adamc@777 4376 if ErrorMsg.anyErrors () then
adamc@777 4377 ()
adamc@777 4378 else
adamc@777 4379 let
adamc@777 4380 val delayed = !delayedUnifs
adamc@777 4381 in
adamc@777 4382 delayedUnifs := [];
adamc@777 4383 app (fn (loc, env, k, s1, s2) =>
adamc@777 4384 unifySummaries env (loc, k, normalizeRecordSummary env s1, normalizeRecordSummary env s2))
adamc@777 4385 delayed
adamc@777 4386 end
adam@1576 4387
adam@1576 4388 val checkConstraintErrors = ref (fn () => ())
adamc@56 4389 in
adamc@777 4390 oneSummaryRound ();
adamc@753 4391
adamc@753 4392 if ErrorMsg.anyErrors () then
adamc@753 4393 ()
adamc@753 4394 else
adamc@753 4395 let
adamc@753 4396 fun solver gs =
adamc@753 4397 let
adamc@753 4398 val (gs, solved) =
adamc@753 4399 ListUtil.foldlMapPartial
adamc@753 4400 (fn (g, solved) =>
adamc@753 4401 case g of
adamc@753 4402 Disjoint (loc, env, denv, c1, c2) =>
adamc@753 4403 (case D.prove env denv (c1, c2, loc) of
adamc@753 4404 [] => (NONE, true)
adamc@753 4405 | _ => (SOME g, solved))
adamc@753 4406 | TypeClass (env, c, r, loc) =>
adamc@753 4407 let
adamc@753 4408 fun default () = (SOME g, solved)
adamc@753 4409
adamc@753 4410 val c = normClassKey env c
adamc@753 4411 in
adamc@833 4412 case resolveClass env c of
adamc@753 4413 SOME e => (r := SOME e;
adamc@753 4414 (NONE, true))
adamc@753 4415 | NONE =>
adamc@753 4416 case #1 (hnormCon env c) of
adamc@753 4417 L'.CApp (f, x) =>
adamc@753 4418 (case (#1 (hnormCon env f), #1 (hnormCon env x)) of
adamc@753 4419 (L'.CKApp (f, _), L'.CRecord (k, xcs)) =>
adamc@753 4420 (case #1 (hnormCon env f) of
adamc@753 4421 L'.CModProj (top_n', [], "folder") =>
adamc@753 4422 if top_n' = top_n then
adamc@753 4423 let
adamc@753 4424 val e = (L'.EModProj (top_n, ["Folder"], "nil"), loc)
adamc@753 4425 val e = (L'.EKApp (e, k), loc)
adamc@753 4426
adamc@753 4427 val (folder, _) = foldr (fn ((x, c), (folder, xcs)) =>
adamc@753 4428 let
adamc@753 4429 val e = (L'.EModProj (top_n, ["Folder"],
adamc@753 4430 "cons"), loc)
adamc@753 4431 val e = (L'.EKApp (e, k), loc)
adamc@753 4432 val e = (L'.ECApp (e,
adamc@753 4433 (L'.CRecord (k, xcs),
adamc@753 4434 loc)), loc)
adamc@753 4435 val e = (L'.ECApp (e, x), loc)
adamc@753 4436 val e = (L'.ECApp (e, c), loc)
adamc@753 4437 val e = (L'.EApp (e, folder), loc)
adamc@753 4438 in
adamc@753 4439 (e, (x, c) :: xcs)
adamc@753 4440 end)
adamc@753 4441 (e, []) xcs
adamc@753 4442 in
adamc@753 4443 (r := SOME folder;
adamc@753 4444 (NONE, true))
adamc@753 4445 end
adamc@753 4446 else
adamc@753 4447 default ()
adamc@753 4448 | _ => default ())
adamc@753 4449 | _ => default ())
adamc@753 4450 | _ => default ()
adamc@753 4451 end)
adamc@753 4452 false gs
adamc@753 4453 in
adamc@753 4454 case (gs, solved) of
adamc@753 4455 ([], _) => ()
adamc@777 4456 | (_, true) => (oneSummaryRound (); solver gs)
adamc@753 4457 | _ =>
adam@1576 4458 checkConstraintErrors :=
adam@1576 4459 (fn () => app (fn Disjoint (loc, env, denv, c1, c2) =>
adam@1576 4460 let
adam@1576 4461 val c1' = ElabOps.hnormCon env c1
adam@1576 4462 val c2' = ElabOps.hnormCon env c2
adam@1576 4463
adam@1576 4464 fun isUnif (c, _) =
adam@1576 4465 case c of
adam@1576 4466 L'.CUnif _ => true
adam@1576 4467 | _ => false
adam@1576 4468
adam@1576 4469 fun maybeAttr (c, _) =
adam@1576 4470 case c of
adam@1576 4471 L'.CRecord ((L'.KType, _), xts) => true
adam@1576 4472 | _ => false
adam@1576 4473 in
adam@1576 4474 ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adam@1576 4475 eprefaces' [("Con 1", p_con env c1),
adam@1576 4476 ("Con 2", p_con env c2),
adam@1576 4477 ("Hnormed 1", p_con env c1'),
adam@1576 4478 ("Hnormed 2", p_con env c2')]
adam@1576 4479
adam@1576 4480 (*app (fn (loc, env, k, s1, s2) =>
adam@1576 4481 eprefaces' [("s1", p_summary env (normalizeRecordSummary env s1)),
adam@1576 4482 ("s2", p_summary env (normalizeRecordSummary env s2))])
adam@1576 4483 (!delayedUnifs);*)
adam@1576 4484 end
adam@1576 4485 | TypeClass (env, c, r, loc) =>
adam@1576 4486 expError env (Unresolvable (loc, c)))
adam@1576 4487 gs)
adamc@753 4488 end
adamc@753 4489 in
adamc@753 4490 solver gs
adamc@753 4491 end;
adamc@753 4492
adamc@713 4493 mayDelay := false;
adamc@713 4494
adamc@777 4495 if ErrorMsg.anyErrors () then
adamc@777 4496 ()
adamc@777 4497 else
adamc@777 4498 (app (fn (loc, env, k, s1, s2) =>
adamc@777 4499 unifySummaries env (loc, k, normalizeRecordSummary env s1, normalizeRecordSummary env s2)
adamc@777 4500 handle CUnify' err => (ErrorMsg.errorAt loc "Error in final record unification";
adam@1582 4501 cunifyError env err;
adam@1582 4502 case !reducedSummaries of
adam@1582 4503 NONE => ()
adam@1582 4504 | SOME (s1, s2) =>
adam@1582 4505 (ErrorMsg.errorAt loc "Stuck unifying these records after canceling matching pieces:";
adam@1582 4506 eprefaces' [("Have", s1),
adam@1582 4507 ("Need", s2)])))
adamc@777 4508 (!delayedUnifs);
adamc@777 4509 delayedUnifs := []);
adamc@713 4510
adam@1346 4511 if ErrorMsg.anyErrors () then
adam@1346 4512 ()
adam@1346 4513 else
adam@1584 4514 if List.exists kunifsInDecl file then
adam@1584 4515 case U.File.findDecl kunifsInDecl file of
adam@1584 4516 NONE => ()
adam@1584 4517 | SOME d => declError env'' (KunifsRemain [d])
adam@1584 4518 else
adam@1584 4519 ();
adam@1345 4520
adam@1346 4521 if ErrorMsg.anyErrors () then
adam@1346 4522 ()
adam@1346 4523 else
adam@1584 4524 if List.exists cunifsInDecl file then
adam@1584 4525 case U.File.findDecl cunifsInDecl file of
adam@1584 4526 NONE => ()
adam@1584 4527 | SOME d => declError env'' (CunifsRemain [d])
adam@1584 4528 else
adam@1584 4529 ();
adamc@750 4530
adamc@830 4531 if ErrorMsg.anyErrors () then
adamc@830 4532 ()
adamc@830 4533 else
adamc@837 4534 app (fn all as (env, _, _, loc) =>
adamc@830 4535 case exhaustive all of
adamc@830 4536 NONE => ()
adamc@830 4537 | SOME p => expError env (Inexhaustive (loc, p)))
adamc@830 4538 (!delayedExhaustives);
adamc@830 4539
adam@1576 4540 if ErrorMsg.anyErrors () then
adam@1576 4541 ()
adam@1576 4542 else
adam@1576 4543 !checkConstraintErrors ();
adam@1576 4544
adamc@799 4545 (*preface ("file", p_file env' file);*)
adam@1531 4546
adam@1531 4547 if !dumpTypes then
adam@1531 4548 let
adam@1531 4549 open L'
adam@1531 4550 open Print.PD
adam@1531 4551 open Print
adam@1531 4552
adam@1531 4553 fun dumpDecl (d, env) =
adam@1531 4554 case #1 d of
adam@1531 4555 DCon (x, _, k, _) => (print (box [string x,
adam@1531 4556 space,
adam@1531 4557 string "::",
adam@1531 4558 space,
adam@1531 4559 p_kind env k,
adam@1531 4560 newline,
adam@1531 4561 newline]);
adam@1531 4562 E.declBinds env d)
adam@1531 4563 | DVal (x, _, t, _) => (print (box [string x,
adam@1531 4564 space,
adam@1531 4565 string ":",
adam@1531 4566 space,
adam@1531 4567 p_con env t,
adam@1531 4568 newline,
adam@1531 4569 newline]);
adam@1531 4570 E.declBinds env d)
adam@1531 4571 | DValRec vis => (app (fn (x, _, t, _) => print (box [string x,
adam@1531 4572 space,
adam@1531 4573 string ":",
adam@1531 4574 space,
adam@1531 4575 p_con env t,
adam@1531 4576 newline,
adam@1531 4577 newline])) vis;
adam@1531 4578 E.declBinds env d)
adam@1531 4579 | DStr (x, _, _, str) => (print (box [string ("<" ^ x ^ ">"),
adam@1531 4580 newline,
adam@1531 4581 newline]);
adam@1531 4582 dumpStr (str, env);
adam@1531 4583 print (box [string ("</" ^ x ^ ">"),
adam@1531 4584 newline,
adam@1531 4585 newline]);
adam@1531 4586 E.declBinds env d)
adam@1531 4587 | _ => E.declBinds env d
adam@1531 4588
adam@1531 4589 and dumpStr (str, env) =
adam@1531 4590 case #1 str of
adam@1531 4591 StrConst ds => ignore (foldl dumpDecl env ds)
adam@1531 4592 | _ => ()
adam@1531 4593 in
adam@1531 4594 ignore (foldl dumpDecl env' file)
adam@1531 4595 end
adam@1531 4596 else
adam@1531 4597 ();
adam@1345 4598
adamc@325 4599 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan)
adamc@325 4600 :: ds
adamc@325 4601 @ (L'.DStr ("Top", top_n, topSgn, topStr), ErrorMsg.dummySpan)
adamc@325 4602 :: ds' @ file
adamc@56 4603 end
adamc@2 4604
adamc@2 4605 end