annotate src/elaborate.sml @ 1321:4172863d049d

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