annotate src/elaborate.sml @ 328:58f1260f293f

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