annotate src/elaborate.sml @ 188:8e9f97508f0d

Datatype representation optimization
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 19:49:21 -0400
parents d11754ffe252
children aa54250f58ac
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@2 50 fun elabExplicitness e =
adamc@2 51 case e of
adamc@2 52 L.Explicit => L'.Explicit
adamc@2 53 | L.Implicit => L'.Implicit
adamc@2 54
adamc@2 55 fun occursKind r =
adamc@76 56 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
adamc@2 57 | _ => false)
adamc@2 58
adamc@3 59 datatype kunify_error =
adamc@2 60 KOccursCheckFailed of L'.kind * L'.kind
adamc@2 61 | KIncompatible of L'.kind * L'.kind
adamc@2 62
adamc@3 63 exception KUnify' of kunify_error
adamc@3 64
adamc@3 65 fun kunifyError err =
adamc@2 66 case err of
adamc@2 67 KOccursCheckFailed (k1, k2) =>
adamc@3 68 eprefaces "Kind occurs check failed"
adamc@3 69 [("Kind 1", p_kind k1),
adamc@3 70 ("Kind 2", p_kind k2)]
adamc@2 71 | KIncompatible (k1, k2) =>
adamc@3 72 eprefaces "Incompatible kinds"
adamc@3 73 [("Kind 1", p_kind k1),
adamc@3 74 ("Kind 2", p_kind k2)]
adamc@2 75
adamc@3 76 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@2 77 let
adamc@3 78 fun err f = raise KUnify' (f (k1All, k2All))
adamc@2 79 in
adamc@2 80 case (k1, k2) of
adamc@2 81 (L'.KType, L'.KType) => ()
adamc@82 82 | (L'.KUnit, L'.KUnit) => ()
adamc@82 83
adamc@2 84 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@3 85 (unifyKinds' d1 d2;
adamc@3 86 unifyKinds' r1 r2)
adamc@2 87 | (L'.KName, L'.KName) => ()
adamc@3 88 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
adamc@2 89
adamc@2 90 | (L'.KError, _) => ()
adamc@2 91 | (_, L'.KError) => ()
adamc@2 92
adamc@76 93 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' k1All k2All
adamc@76 94 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' k1All k2All
adamc@2 95
adamc@76 96 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
adamc@2 97 if r1 = r2 then
adamc@2 98 ()
adamc@2 99 else
adamc@2 100 r1 := SOME k2All
adamc@2 101
adamc@76 102 | (L'.KUnif (_, _, r), _) =>
adamc@2 103 if occursKind r k2All then
adamc@2 104 err KOccursCheckFailed
adamc@2 105 else
adamc@2 106 r := SOME k2All
adamc@76 107 | (_, L'.KUnif (_, _, r)) =>
adamc@2 108 if occursKind r k1All then
adamc@2 109 err KOccursCheckFailed
adamc@2 110 else
adamc@2 111 r := SOME k1All
adamc@2 112
adamc@2 113 | _ => err KIncompatible
adamc@2 114 end
adamc@2 115
adamc@3 116 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@3 117
adamc@3 118 fun unifyKinds k1 k2 =
adamc@3 119 unifyKinds' k1 k2
adamc@3 120 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@3 121
adamc@3 122 datatype con_error =
adamc@3 123 UnboundCon of ErrorMsg.span * string
adamc@157 124 | UnboundDatatype of ErrorMsg.span * string
adamc@34 125 | UnboundStrInCon of ErrorMsg.span * string
adamc@3 126 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
adamc@67 127 | DuplicateField of ErrorMsg.span * string
adamc@3 128
adamc@5 129 fun conError env err =
adamc@3 130 case err of
adamc@3 131 UnboundCon (loc, s) =>
adamc@3 132 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
adamc@157 133 | UnboundDatatype (loc, s) =>
adamc@157 134 ErrorMsg.errorAt loc ("Unbound datatype " ^ s)
adamc@34 135 | UnboundStrInCon (loc, s) =>
adamc@34 136 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@3 137 | WrongKind (c, k1, k2, kerr) =>
adamc@3 138 (ErrorMsg.errorAt (#2 c) "Wrong kind";
adamc@5 139 eprefaces' [("Constructor", p_con env c),
adamc@5 140 ("Have kind", p_kind k1),
adamc@5 141 ("Need kind", p_kind k2)];
adamc@3 142 kunifyError kerr)
adamc@67 143 | DuplicateField (loc, s) =>
adamc@67 144 ErrorMsg.errorAt loc ("Duplicate record field " ^ s)
adamc@3 145
adamc@5 146 fun checkKind env c k1 k2 =
adamc@3 147 unifyKinds k1 k2
adamc@3 148 handle KUnify (k1, k2, err) =>
adamc@5 149 conError env (WrongKind (c, k1, k2, err))
adamc@3 150
adamc@3 151 val dummy = ErrorMsg.dummySpan
adamc@3 152
adamc@3 153 val ktype = (L'.KType, dummy)
adamc@3 154 val kname = (L'.KName, dummy)
adamc@12 155 val ktype_record = (L'.KRecord ktype, dummy)
adamc@3 156
adamc@3 157 val cerror = (L'.CError, dummy)
adamc@3 158 val kerror = (L'.KError, dummy)
adamc@10 159 val eerror = (L'.EError, dummy)
adamc@31 160 val sgnerror = (L'.SgnError, dummy)
adamc@31 161 val strerror = (L'.StrError, dummy)
adamc@3 162
adamc@56 163 val int = ref cerror
adamc@56 164 val float = ref cerror
adamc@56 165 val string = ref cerror
adamc@56 166
adamc@3 167 local
adamc@3 168 val count = ref 0
adamc@3 169 in
adamc@3 170
adamc@3 171 fun resetKunif () = count := 0
adamc@3 172
adamc@76 173 fun kunif loc =
adamc@3 174 let
adamc@3 175 val n = !count
adamc@3 176 val s = if n <= 26 then
adamc@3 177 str (chr (ord #"A" + n))
adamc@3 178 else
adamc@3 179 "U" ^ Int.toString (n - 26)
adamc@3 180 in
adamc@3 181 count := n + 1;
adamc@76 182 (L'.KUnif (loc, s, ref NONE), dummy)
adamc@3 183 end
adamc@3 184
adamc@3 185 end
adamc@3 186
adamc@10 187 local
adamc@10 188 val count = ref 0
adamc@10 189 in
adamc@10 190
adamc@10 191 fun resetCunif () = count := 0
adamc@10 192
adamc@76 193 fun cunif (loc, k) =
adamc@10 194 let
adamc@10 195 val n = !count
adamc@10 196 val s = if n <= 26 then
adamc@10 197 str (chr (ord #"A" + n))
adamc@10 198 else
adamc@10 199 "U" ^ Int.toString (n - 26)
adamc@10 200 in
adamc@10 201 count := n + 1;
adamc@76 202 (L'.CUnif (loc, k, s, ref NONE), dummy)
adamc@10 203 end
adamc@10 204
adamc@10 205 end
adamc@10 206
adamc@18 207 fun elabKind (k, loc) =
adamc@18 208 case k of
adamc@18 209 L.KType => (L'.KType, loc)
adamc@18 210 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
adamc@18 211 | L.KName => (L'.KName, loc)
adamc@18 212 | L.KRecord k => (L'.KRecord (elabKind k), loc)
adamc@82 213 | L.KUnit => (L'.KUnit, loc)
adamc@76 214 | L.KWild => kunif loc
adamc@18 215
adamc@67 216 fun foldKind (dom, ran, loc)=
adamc@67 217 (L'.KArrow ((L'.KArrow ((L'.KName, loc),
adamc@67 218 (L'.KArrow (dom,
adamc@67 219 (L'.KArrow (ran, ran), loc)), loc)), loc),
adamc@67 220 (L'.KArrow (ran,
adamc@67 221 (L'.KArrow ((L'.KRecord dom, loc),
adamc@67 222 ran), loc)), loc)), loc)
adamc@67 223
adamc@83 224 fun elabCon (env, denv) (c, loc) =
adamc@3 225 case c of
adamc@3 226 L.CAnnot (c, k) =>
adamc@3 227 let
adamc@3 228 val k' = elabKind k
adamc@83 229 val (c', ck, gs) = elabCon (env, denv) c
adamc@3 230 in
adamc@5 231 checkKind env c' ck k';
adamc@83 232 (c', k', gs)
adamc@3 233 end
adamc@3 234
adamc@3 235 | L.TFun (t1, t2) =>
adamc@3 236 let
adamc@83 237 val (t1', k1, gs1) = elabCon (env, denv) t1
adamc@83 238 val (t2', k2, gs2) = elabCon (env, denv) t2
adamc@3 239 in
adamc@5 240 checkKind env t1' k1 ktype;
adamc@5 241 checkKind env t2' k2 ktype;
adamc@83 242 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
adamc@3 243 end
adamc@3 244 | L.TCFun (e, x, k, t) =>
adamc@3 245 let
adamc@3 246 val e' = elabExplicitness e
adamc@3 247 val k' = elabKind k
adamc@3 248 val env' = E.pushCRel env x k'
adamc@83 249 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@3 250 in
adamc@5 251 checkKind env t' tk ktype;
adamc@83 252 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
adamc@3 253 end
adamc@85 254 | L.TDisjoint (c1, c2, c) =>
adamc@85 255 let
adamc@85 256 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@85 257 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@85 258
adamc@85 259 val ku1 = kunif loc
adamc@85 260 val ku2 = kunif loc
adamc@85 261
adamc@90 262 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@90 263 val (c', k, gs4) = elabCon (env, denv') c
adamc@85 264 in
adamc@85 265 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@85 266 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@85 267
adamc@90 268 ((L'.TDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4)
adamc@85 269 end
adamc@3 270 | L.TRecord c =>
adamc@3 271 let
adamc@83 272 val (c', ck, gs) = elabCon (env, denv) c
adamc@3 273 val k = (L'.KRecord ktype, loc)
adamc@3 274 in
adamc@5 275 checkKind env c' ck k;
adamc@83 276 ((L'.TRecord c', loc), ktype, gs)
adamc@3 277 end
adamc@3 278
adamc@34 279 | L.CVar ([], s) =>
adamc@3 280 (case E.lookupC env s of
adamc@9 281 E.NotBound =>
adamc@5 282 (conError env (UnboundCon (loc, s));
adamc@83 283 (cerror, kerror, []))
adamc@9 284 | E.Rel (n, k) =>
adamc@83 285 ((L'.CRel n, loc), k, [])
adamc@9 286 | E.Named (n, k) =>
adamc@83 287 ((L'.CNamed n, loc), k, []))
adamc@34 288 | L.CVar (m1 :: ms, s) =>
adamc@34 289 (case E.lookupStr env m1 of
adamc@42 290 NONE => (conError env (UnboundStrInCon (loc, m1));
adamc@83 291 (cerror, kerror, []))
adamc@34 292 | SOME (n, sgn) =>
adamc@34 293 let
adamc@34 294 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 295 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 296 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@34 297 (strerror, sgnerror))
adamc@34 298 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 299 ((L'.StrVar n, loc), sgn) ms
adamc@34 300
adamc@34 301 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
adamc@34 302 NONE => (conError env (UnboundCon (loc, s));
adamc@34 303 kerror)
adamc@34 304 | SOME (k, _) => k
adamc@34 305 in
adamc@83 306 ((L'.CModProj (n, ms, s), loc), k, [])
adamc@34 307 end)
adamc@34 308
adamc@3 309 | L.CApp (c1, c2) =>
adamc@3 310 let
adamc@83 311 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@83 312 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@76 313 val dom = kunif loc
adamc@76 314 val ran = kunif loc
adamc@3 315 in
adamc@5 316 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@5 317 checkKind env c2' k2 dom;
adamc@83 318 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
adamc@3 319 end
adamc@67 320 | L.CAbs (x, ko, t) =>
adamc@3 321 let
adamc@67 322 val k' = case ko of
adamc@76 323 NONE => kunif loc
adamc@67 324 | SOME k => elabKind k
adamc@3 325 val env' = E.pushCRel env x k'
adamc@83 326 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@3 327 in
adamc@8 328 ((L'.CAbs (x, k', t'), loc),
adamc@83 329 (L'.KArrow (k', tk), loc),
adamc@83 330 gs)
adamc@3 331 end
adamc@3 332
adamc@84 333 | L.CDisjoint (c1, c2, c) =>
adamc@84 334 let
adamc@84 335 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@84 336 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@84 337
adamc@84 338 val ku1 = kunif loc
adamc@84 339 val ku2 = kunif loc
adamc@84 340
adamc@90 341 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@90 342 val (c', k, gs4) = elabCon (env, denv') c
adamc@84 343 in
adamc@84 344 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@84 345 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@84 346
adamc@90 347 ((L'.CDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4)
adamc@84 348 end
adamc@84 349
adamc@3 350 | L.CName s =>
adamc@83 351 ((L'.CName s, loc), kname, [])
adamc@3 352
adamc@3 353 | L.CRecord xcs =>
adamc@3 354 let
adamc@76 355 val k = kunif loc
adamc@3 356
adamc@83 357 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
adamc@83 358 let
adamc@83 359 val (x', xk, gs1) = elabCon (env, denv) x
adamc@83 360 val (c', ck, gs2) = elabCon (env, denv) c
adamc@83 361 in
adamc@83 362 checkKind env x' xk kname;
adamc@83 363 checkKind env c' ck k;
adamc@83 364 ((x', c'), gs1 @ gs2 @ gs)
adamc@83 365 end) [] xcs
adamc@67 366
adamc@67 367 val rc = (L'.CRecord (k, xcs'), loc)
adamc@67 368 (* Add duplicate field checking later. *)
adamc@83 369
adamc@83 370 fun prove (xcs, ds) =
adamc@83 371 case xcs of
adamc@83 372 [] => ds
adamc@83 373 | xc :: rest =>
adamc@83 374 let
adamc@83 375 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@83 376 val ds = foldl (fn (xc', ds) =>
adamc@83 377 let
adamc@83 378 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@83 379 in
adamc@90 380 D.prove env denv (r1, r2, loc) @ ds
adamc@83 381 end)
adamc@83 382 ds rest
adamc@83 383 in
adamc@83 384 prove (rest, ds)
adamc@83 385 end
adamc@3 386 in
adamc@83 387 (rc, (L'.KRecord k, loc), prove (xcs', gs))
adamc@3 388 end
adamc@3 389 | L.CConcat (c1, c2) =>
adamc@3 390 let
adamc@83 391 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@83 392 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@76 393 val ku = kunif loc
adamc@3 394 val k = (L'.KRecord ku, loc)
adamc@3 395 in
adamc@5 396 checkKind env c1' k1 k;
adamc@5 397 checkKind env c2' k2 k;
adamc@83 398 ((L'.CConcat (c1', c2'), loc), k,
adamc@90 399 D.prove env denv (c1', c2', loc) @ gs1 @ gs2)
adamc@3 400 end
adamc@67 401 | L.CFold =>
adamc@67 402 let
adamc@76 403 val dom = kunif loc
adamc@76 404 val ran = kunif loc
adamc@67 405 in
adamc@67 406 ((L'.CFold (dom, ran), loc),
adamc@83 407 foldKind (dom, ran, loc),
adamc@83 408 [])
adamc@67 409 end
adamc@3 410
adamc@83 411 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
adamc@82 412
adamc@18 413 | L.CWild k =>
adamc@18 414 let
adamc@18 415 val k' = elabKind k
adamc@18 416 in
adamc@83 417 (cunif (loc, k'), k', [])
adamc@18 418 end
adamc@18 419
adamc@6 420 fun kunifsRemain k =
adamc@6 421 case k of
adamc@76 422 L'.KUnif (_, _, ref NONE) => true
adamc@6 423 | _ => false
adamc@10 424 fun cunifsRemain c =
adamc@10 425 case c of
adamc@76 426 L'.CUnif (loc, _, _, ref NONE) => SOME loc
adamc@76 427 | _ => NONE
adamc@6 428
adamc@76 429 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
adamc@76 430 con = fn _ => false,
adamc@76 431 exp = fn _ => false,
adamc@76 432 sgn_item = fn _ => false,
adamc@76 433 sgn = fn _ => false,
adamc@76 434 str = fn _ => false,
adamc@76 435 decl = fn _ => false}
adamc@10 436
adamc@76 437 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
adamc@76 438 con = cunifsRemain,
adamc@76 439 exp = fn _ => NONE,
adamc@76 440 sgn_item = fn _ => NONE,
adamc@76 441 sgn = fn _ => NONE,
adamc@76 442 str = fn _ => NONE,
adamc@76 443 decl = fn _ => NONE}
adamc@10 444
adamc@10 445 fun occursCon r =
adamc@10 446 U.Con.exists {kind = fn _ => false,
adamc@76 447 con = fn L'.CUnif (_, _, _, r') => r = r'
adamc@10 448 | _ => false}
adamc@10 449
adamc@10 450 datatype cunify_error =
adamc@10 451 CKind of L'.kind * L'.kind * kunify_error
adamc@10 452 | COccursCheckFailed of L'.con * L'.con
adamc@10 453 | CIncompatible of L'.con * L'.con
adamc@10 454 | CExplicitness of L'.con * L'.con
adamc@141 455 | CKindof of L'.kind * L'.con
adamc@153 456 | CRecordFailure of PD.pp_desc * PD.pp_desc
adamc@10 457
adamc@10 458 exception CUnify' of cunify_error
adamc@10 459
adamc@10 460 fun cunifyError env err =
adamc@10 461 case err of
adamc@10 462 CKind (k1, k2, kerr) =>
adamc@10 463 (eprefaces "Kind unification failure"
adamc@10 464 [("Kind 1", p_kind k1),
adamc@10 465 ("Kind 2", p_kind k2)];
adamc@10 466 kunifyError kerr)
adamc@10 467 | COccursCheckFailed (c1, c2) =>
adamc@10 468 eprefaces "Constructor occurs check failed"
adamc@10 469 [("Con 1", p_con env c1),
adamc@10 470 ("Con 2", p_con env c2)]
adamc@10 471 | CIncompatible (c1, c2) =>
adamc@10 472 eprefaces "Incompatible constructors"
adamc@10 473 [("Con 1", p_con env c1),
adamc@10 474 ("Con 2", p_con env c2)]
adamc@10 475 | CExplicitness (c1, c2) =>
adamc@10 476 eprefaces "Differing constructor function explicitness"
adamc@10 477 [("Con 1", p_con env c1),
adamc@10 478 ("Con 2", p_con env c2)]
adamc@141 479 | CKindof (k, c) =>
adamc@147 480 eprefaces "Unexpected kind for kindof calculation"
adamc@141 481 [("Kind", p_kind k),
adamc@141 482 ("Con", p_con env c)]
adamc@153 483 | CRecordFailure (s1, s2) =>
adamc@153 484 eprefaces "Can't unify record constructors"
adamc@153 485 [("Summary 1", s1),
adamc@153 486 ("Summary 2", s2)]
adamc@10 487
adamc@13 488 exception SynUnif = E.SynUnif
adamc@12 489
adamc@81 490 open ElabOps
adamc@43 491
adamc@12 492 type record_summary = {
adamc@12 493 fields : (L'.con * L'.con) list,
adamc@12 494 unifs : (L'.con * L'.con option ref) list,
adamc@12 495 others : L'.con list
adamc@12 496 }
adamc@12 497
adamc@12 498 fun summaryToCon {fields, unifs, others} =
adamc@12 499 let
adamc@12 500 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 501 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 502 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 503 in
adamc@12 504 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 505 end
adamc@12 506
adamc@12 507 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 508
adamc@12 509 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 510
adamc@12 511 fun hnormKind (kAll as (k, _)) =
adamc@12 512 case k of
adamc@76 513 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@12 514 | _ => kAll
adamc@12 515
adamc@12 516 fun kindof env (c, loc) =
adamc@12 517 case c of
adamc@12 518 L'.TFun _ => ktype
adamc@12 519 | L'.TCFun _ => ktype
adamc@85 520 | L'.TDisjoint _ => ktype
adamc@12 521 | L'.TRecord _ => ktype
adamc@12 522
adamc@12 523 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 524 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@34 525 | L'.CModProj (n, ms, x) =>
adamc@34 526 let
adamc@34 527 val (_, sgn) = E.lookupStrNamed env n
adamc@34 528 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 529 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 530 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 531 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 532 ((L'.StrVar n, loc), sgn) ms
adamc@34 533 in
adamc@34 534 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@34 535 NONE => raise Fail "kindof: Unknown con in structure"
adamc@34 536 | SOME (k, _) => k
adamc@34 537 end
adamc@34 538
adamc@12 539 | L'.CApp (c, _) =>
adamc@141 540 (case hnormKind (kindof env c) of
adamc@141 541 (L'.KArrow (_, k), _) => k
adamc@141 542 | (L'.KError, _) => kerror
adamc@141 543 | k => raise CUnify' (CKindof (k, c)))
adamc@12 544 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@84 545 | L'.CDisjoint (_, _, c) => kindof env c
adamc@12 546
adamc@12 547 | L'.CName _ => kname
adamc@12 548
adamc@12 549 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 550 | L'.CConcat (c, _) => kindof env c
adamc@67 551 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
adamc@12 552
adamc@82 553 | L'.CUnit => (L'.KUnit, loc)
adamc@82 554
adamc@12 555 | L'.CError => kerror
adamc@76 556 | L'.CUnif (_, k, _, _) => k
adamc@12 557
adamc@90 558 val hnormCon = D.hnormCon
adamc@86 559
adamc@86 560 fun unifyRecordCons (env, denv) (c1, c2) =
adamc@12 561 let
adamc@92 562 fun rkindof c =
adamc@147 563 case hnormKind (kindof env c) of
adamc@92 564 (L'.KRecord k, _) => k
adamc@141 565 | (L'.KError, _) => kerror
adamc@141 566 | k => raise CUnify' (CKindof (k, c))
adamc@92 567
adamc@92 568 val k1 = rkindof c1
adamc@92 569 val k2 = rkindof c2
adamc@86 570
adamc@86 571 val (r1, gs1) = recordSummary (env, denv) c1
adamc@86 572 val (r2, gs2) = recordSummary (env, denv) c2
adamc@12 573 in
adamc@12 574 unifyKinds k1 k2;
adamc@86 575 unifySummaries (env, denv) (k1, r1, r2);
adamc@86 576 gs1 @ gs2
adamc@12 577 end
adamc@12 578
adamc@86 579 and recordSummary (env, denv) c =
adamc@86 580 let
adamc@86 581 val (c, gs) = hnormCon (env, denv) c
adamc@12 582
adamc@86 583 val (sum, gs') =
adamc@86 584 case c of
adamc@86 585 (L'.CRecord (_, xcs), _) => ({fields = xcs, unifs = [], others = []}, [])
adamc@86 586 | (L'.CConcat (c1, c2), _) =>
adamc@86 587 let
adamc@86 588 val (s1, gs1) = recordSummary (env, denv) c1
adamc@86 589 val (s2, gs2) = recordSummary (env, denv) c2
adamc@86 590 in
adamc@86 591 ({fields = #fields s1 @ #fields s2,
adamc@86 592 unifs = #unifs s1 @ #unifs s2,
adamc@86 593 others = #others s1 @ #others s2},
adamc@86 594 gs1 @ gs2)
adamc@86 595 end
adamc@86 596 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary (env, denv) c
adamc@86 597 | c' as (L'.CUnif (_, _, _, r), _) => ({fields = [], unifs = [(c', r)], others = []}, [])
adamc@86 598 | c' => ({fields = [], unifs = [], others = [c']}, [])
adamc@86 599 in
adamc@86 600 (sum, gs @ gs')
adamc@86 601 end
adamc@86 602
adamc@86 603 and consEq (env, denv) (c1, c2) =
adamc@86 604 (case unifyCons (env, denv) c1 c2 of
adamc@86 605 [] => true
adamc@86 606 | _ => false)
adamc@12 607 handle CUnify _ => false
adamc@12 608
adamc@80 609 and consNeq env (c1, c2) =
adamc@86 610 case (#1 (ElabOps.hnormCon env c1), #1 (ElabOps.hnormCon env c2)) of
adamc@80 611 (L'.CName x1, L'.CName x2) => x1 <> x2
adamc@80 612 | _ => false
adamc@80 613
adamc@86 614 and unifySummaries (env, denv) (k, s1 : record_summary, s2 : record_summary) =
adamc@12 615 let
adamc@14 616 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@14 617 ("#2", p_summary env s2)]*)
adamc@12 618
adamc@12 619 fun eatMatching p (ls1, ls2) =
adamc@12 620 let
adamc@12 621 fun em (ls1, ls2, passed1) =
adamc@12 622 case ls1 of
adamc@12 623 [] => (rev passed1, ls2)
adamc@12 624 | h1 :: t1 =>
adamc@12 625 let
adamc@12 626 fun search (ls2', passed2) =
adamc@12 627 case ls2' of
adamc@12 628 [] => em (t1, ls2, h1 :: passed1)
adamc@12 629 | h2 :: t2 =>
adamc@12 630 if p (h1, h2) then
adamc@12 631 em (t1, List.revAppend (passed2, t2), passed1)
adamc@12 632 else
adamc@12 633 search (t2, h2 :: passed2)
adamc@12 634 in
adamc@12 635 search (ls2, [])
adamc@12 636 end
adamc@12 637 in
adamc@12 638 em (ls1, ls2, [])
adamc@12 639 end
adamc@12 640
adamc@12 641 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@80 642 not (consNeq env (x1, x2))
adamc@86 643 andalso consEq (env, denv) (c1, c2)
adamc@86 644 andalso consEq (env, denv) (x1, x2))
adamc@79 645 (#fields s1, #fields s2)
adamc@14 646 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@14 647 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@12 648 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@86 649 val (others1, others2) = eatMatching (consEq (env, denv)) (#others s1, #others s2)
adamc@12 650
adamc@12 651 fun unifFields (fs, others, unifs) =
adamc@12 652 case (fs, others, unifs) of
adamc@12 653 ([], [], _) => ([], [], unifs)
adamc@12 654 | (_, _, []) => (fs, others, [])
adamc@12 655 | (_, _, (_, r) :: rest) =>
adamc@12 656 let
adamc@12 657 val r' = ref NONE
adamc@141 658 val kr = (L'.KRecord k, dummy)
adamc@141 659 val cr' = (L'.CUnif (dummy, kr, "recd", r'), dummy)
adamc@12 660
adamc@12 661 val prefix = case (fs, others) of
adamc@12 662 ([], other :: others) =>
adamc@12 663 List.foldl (fn (other, c) =>
adamc@12 664 (L'.CConcat (c, other), dummy))
adamc@12 665 other others
adamc@12 666 | (fs, []) =>
adamc@12 667 (L'.CRecord (k, fs), dummy)
adamc@12 668 | (fs, others) =>
adamc@12 669 List.foldl (fn (other, c) =>
adamc@12 670 (L'.CConcat (c, other), dummy))
adamc@12 671 (L'.CRecord (k, fs), dummy) others
adamc@12 672 in
adamc@12 673 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 674 ([], [], (cr', r') :: rest)
adamc@12 675 end
adamc@12 676
adamc@12 677 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 678 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 679
adamc@86 680 val clear = case (fs1, others1, fs2, others2) of
adamc@86 681 ([], [], [], []) => true
adamc@12 682 | _ => false
adamc@12 683 val empty = (L'.CRecord (k, []), dummy)
adamc@12 684 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 685 case (unifs1, unifs2) of
adamc@12 686 ([], _) =>
adamc@86 687 if clear then
adamc@12 688 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 689 else
adamc@153 690 raise CUnify' (CRecordFailure (p_summary env s1, p_summary env s2))
adamc@12 691 | (_, []) =>
adamc@86 692 if clear then
adamc@12 693 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 694 else
adamc@153 695 raise CUnify' (CRecordFailure (p_summary env s1, p_summary env s2))
adamc@12 696 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 697 (r2 := SOME c1;
adamc@12 698 pairOffUnifs (rest1, rest2))
adamc@12 699 in
adamc@12 700 pairOffUnifs (unifs1, unifs2)
adamc@12 701 end
adamc@12 702
adamc@86 703 and unifyCons' (env, denv) c1 c2 =
adamc@86 704 let
adamc@86 705 val (c1, gs1) = hnormCon (env, denv) c1
adamc@86 706 val (c2, gs2) = hnormCon (env, denv) c2
adamc@90 707 val gs3 = unifyCons'' (env, denv) c1 c2
adamc@86 708 in
adamc@90 709 gs1 @ gs2 @ gs3
adamc@86 710 end
adamc@11 711
adamc@86 712 and unifyCons'' (env, denv) (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 713 let
adamc@10 714 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 715
adamc@86 716 fun isRecord () = unifyRecordCons (env, denv) (c1All, c2All)
adamc@10 717 in
adamc@92 718 (*eprefaces "unifyCons''" [("c1All", p_con env c1All),
adamc@92 719 ("c2All", p_con env c2All)];*)
adamc@92 720
adamc@10 721 case (c1, c2) of
adamc@88 722 (L'.CUnit, L'.CUnit) => []
adamc@88 723
adamc@88 724 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@86 725 unifyCons' (env, denv) d1 d2
adamc@86 726 @ unifyCons' (env, denv) r1 r2
adamc@10 727 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 728 if expl1 <> expl2 then
adamc@10 729 err CExplicitness
adamc@10 730 else
adamc@10 731 (unifyKinds d1 d2;
adamc@86 732 unifyCons' (E.pushCRel env x1 d1, D.enter denv) r1 r2)
adamc@86 733 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' (env, denv) r1 r2
adamc@10 734
adamc@10 735 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 736 if n1 = n2 then
adamc@86 737 []
adamc@10 738 else
adamc@10 739 err CIncompatible
adamc@10 740 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 741 if n1 = n2 then
adamc@86 742 []
adamc@10 743 else
adamc@10 744 err CIncompatible
adamc@10 745
adamc@10 746 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@86 747 (unifyCons' (env, denv) d1 d2;
adamc@86 748 unifyCons' (env, denv) r1 r2)
adamc@10 749 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 750 (unifyKinds k1 k2;
adamc@86 751 unifyCons' (E.pushCRel env x1 k1, D.enter denv) c1 c2)
adamc@10 752
adamc@10 753 | (L'.CName n1, L'.CName n2) =>
adamc@10 754 if n1 = n2 then
adamc@86 755 []
adamc@10 756 else
adamc@10 757 err CIncompatible
adamc@10 758
adamc@34 759 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@34 760 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@86 761 []
adamc@34 762 else
adamc@34 763 err CIncompatible
adamc@34 764
adamc@86 765 | (L'.CError, _) => []
adamc@86 766 | (_, L'.CError) => []
adamc@10 767
adamc@92 768 | (L'.CRecord _, _) => isRecord ()
adamc@92 769 | (_, L'.CRecord _) => isRecord ()
adamc@92 770 | (L'.CConcat _, _) => isRecord ()
adamc@92 771 | (_, L'.CConcat _) => isRecord ()
adamc@92 772 (*| (L'.CUnif (_, (L'.KRecord _, _), _, _), _) => isRecord ()
adamc@92 773 | (_, L'.CUnif (_, (L'.KRecord _, _), _, _)) => isRecord ()*)
adamc@10 774
adamc@76 775 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
adamc@10 776 if r1 = r2 then
adamc@86 777 []
adamc@10 778 else
adamc@10 779 (unifyKinds k1 k2;
adamc@86 780 r1 := SOME c2All;
adamc@86 781 [])
adamc@10 782
adamc@76 783 | (L'.CUnif (_, _, _, r), _) =>
adamc@10 784 if occursCon r c2All then
adamc@10 785 err COccursCheckFailed
adamc@10 786 else
adamc@86 787 (r := SOME c2All;
adamc@86 788 [])
adamc@76 789 | (_, L'.CUnif (_, _, _, r)) =>
adamc@10 790 if occursCon r c1All then
adamc@10 791 err COccursCheckFailed
adamc@10 792 else
adamc@86 793 (r := SOME c1All;
adamc@86 794 [])
adamc@10 795
adamc@12 796
adamc@71 797 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
adamc@71 798 (unifyKinds dom1 dom2;
adamc@86 799 unifyKinds ran1 ran2;
adamc@86 800 [])
adamc@71 801
adamc@10 802 | _ => err CIncompatible
adamc@10 803 end
adamc@10 804
adamc@86 805 and unifyCons (env, denv) c1 c2 =
adamc@86 806 unifyCons' (env, denv) c1 c2
adamc@10 807 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 808 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 809
adamc@10 810 datatype exp_error =
adamc@10 811 UnboundExp of ErrorMsg.span * string
adamc@34 812 | UnboundStrInExp of ErrorMsg.span * string
adamc@10 813 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 814 | Unif of string * L'.con
adamc@11 815 | WrongForm of string * L'.exp * L'.con
adamc@92 816 | IncompatibleCons of L'.con * L'.con
adamc@171 817 | DuplicatePatternVariable of ErrorMsg.span * string
adamc@171 818 | PatUnify of L'.pat * L'.con * L'.con * cunify_error
adamc@174 819 | UnboundConstructor of ErrorMsg.span * string list * string
adamc@171 820 | PatHasArg of ErrorMsg.span
adamc@171 821 | PatHasNoArg of ErrorMsg.span
adamc@172 822 | Inexhaustive of ErrorMsg.span
adamc@175 823 | DuplicatePatField of ErrorMsg.span * string
adamc@10 824
adamc@10 825 fun expError env err =
adamc@10 826 case err of
adamc@10 827 UnboundExp (loc, s) =>
adamc@10 828 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@34 829 | UnboundStrInExp (loc, s) =>
adamc@34 830 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@10 831 | Unify (e, c1, c2, uerr) =>
adamc@10 832 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 833 eprefaces' [("Expression", p_exp env e),
adamc@10 834 ("Have con", p_con env c1),
adamc@10 835 ("Need con", p_con env c2)];
adamc@10 836 cunifyError env uerr)
adamc@11 837 | Unif (action, c) =>
adamc@11 838 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 839 eprefaces' [("Con", p_con env c)])
adamc@11 840 | WrongForm (variety, e, t) =>
adamc@11 841 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 842 eprefaces' [("Expression", p_exp env e),
adamc@11 843 ("Type", p_con env t)])
adamc@92 844 | IncompatibleCons (c1, c2) =>
adamc@92 845 (ErrorMsg.errorAt (#2 c1) "Incompatible constructors";
adamc@92 846 eprefaces' [("Con 1", p_con env c1),
adamc@92 847 ("Con 2", p_con env c2)])
adamc@171 848 | DuplicatePatternVariable (loc, s) =>
adamc@171 849 ErrorMsg.errorAt loc ("Duplicate pattern variable " ^ s)
adamc@171 850 | PatUnify (p, c1, c2, uerr) =>
adamc@171 851 (ErrorMsg.errorAt (#2 p) "Unification failure for pattern";
adamc@171 852 eprefaces' [("Pattern", p_pat env p),
adamc@171 853 ("Have con", p_con env c1),
adamc@171 854 ("Need con", p_con env c2)];
adamc@171 855 cunifyError env uerr)
adamc@174 856 | UnboundConstructor (loc, ms, s) =>
adamc@174 857 ErrorMsg.errorAt loc ("Unbound constructor " ^ String.concatWith "." (ms @ [s]) ^ " in pattern")
adamc@171 858 | PatHasArg loc =>
adamc@171 859 ErrorMsg.errorAt loc "Constructor expects no argument but is used with argument"
adamc@171 860 | PatHasNoArg loc =>
adamc@171 861 ErrorMsg.errorAt loc "Constructor expects argument but is used with no argument"
adamc@172 862 | Inexhaustive loc =>
adamc@172 863 ErrorMsg.errorAt loc "Inexhaustive 'case'"
adamc@175 864 | DuplicatePatField (loc, s) =>
adamc@175 865 ErrorMsg.errorAt loc ("Duplicate record field " ^ s ^ " in pattern")
adamc@10 866
adamc@86 867 fun checkCon (env, denv) e c1 c2 =
adamc@86 868 unifyCons (env, denv) c1 c2
adamc@10 869 handle CUnify (c1, c2, err) =>
adamc@86 870 (expError env (Unify (e, c1, c2, err));
adamc@86 871 [])
adamc@10 872
adamc@171 873 fun checkPatCon (env, denv) p c1 c2 =
adamc@171 874 unifyCons (env, denv) c1 c2
adamc@171 875 handle CUnify (c1, c2, err) =>
adamc@171 876 (expError env (PatUnify (p, c1, c2, err));
adamc@171 877 [])
adamc@171 878
adamc@14 879 fun primType env p =
adamc@56 880 case p of
adamc@56 881 P.Int _ => !int
adamc@56 882 | P.Float _ => !float
adamc@56 883 | P.String _ => !string
adamc@14 884
adamc@71 885 fun recCons (k, nm, v, rest, loc) =
adamc@71 886 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
adamc@71 887 rest), loc)
adamc@71 888
adamc@71 889 fun foldType (dom, loc) =
adamc@71 890 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
adamc@71 891 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
adamc@71 892 (L'.TCFun (L'.Explicit, "v", dom,
adamc@71 893 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
adamc@71 894 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
adamc@71 895 (L'.CApp ((L'.CRel 3, loc),
adamc@71 896 recCons (dom,
adamc@71 897 (L'.CRel 2, loc),
adamc@71 898 (L'.CRel 1, loc),
adamc@71 899 (L'.CRel 0, loc),
adamc@71 900 loc)), loc)), loc)),
adamc@71 901 loc)), loc)), loc),
adamc@71 902 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
adamc@71 903 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
adamc@71 904 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
adamc@71 905 loc)), loc)), loc)
adamc@71 906
adamc@86 907 fun elabHead (env, denv) (e as (_, loc)) t =
adamc@15 908 let
adamc@15 909 fun unravel (t, e) =
adamc@86 910 let
adamc@86 911 val (t, gs) = hnormCon (env, denv) t
adamc@86 912 in
adamc@86 913 case t of
adamc@86 914 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@86 915 let
adamc@86 916 val u = cunif (loc, k)
adamc@86 917
adamc@86 918 val (e, t, gs') = unravel (subConInCon (0, u) t',
adamc@86 919 (L'.ECApp (e, u), loc))
adamc@86 920 in
adamc@86 921 (e, t, gs @ gs')
adamc@86 922 end
adamc@86 923 | _ => (e, t, gs)
adamc@86 924 end
adamc@15 925 in
adamc@15 926 unravel (t, e)
adamc@15 927 end
adamc@15 928
adamc@174 929 fun elabPat (pAll as (p, loc), (env, denv, bound)) =
adamc@171 930 let
adamc@171 931 val perror = (L'.PWild, loc)
adamc@171 932 val terror = (L'.CError, loc)
adamc@171 933 val pterror = (perror, terror)
adamc@171 934 val rerror = (pterror, (env, bound))
adamc@171 935
adamc@188 936 fun pcon (pc, po, to, dn, dk) =
adamc@188 937 case (po, to) of
adamc@188 938 (NONE, SOME _) => (expError env (PatHasNoArg loc);
adamc@188 939 rerror)
adamc@188 940 | (SOME _, NONE) => (expError env (PatHasArg loc);
adamc@188 941 rerror)
adamc@188 942 | (NONE, NONE) => (((L'.PCon (dk, pc, NONE), loc), dn),
adamc@188 943 (env, bound))
adamc@188 944 | (SOME p, SOME t) =>
adamc@188 945 let
adamc@188 946 val ((p', pt), (env, bound)) = elabPat (p, (env, denv, bound))
adamc@188 947 in
adamc@188 948 (((L'.PCon (dk, pc, SOME p'), loc), dn),
adamc@188 949 (env, bound))
adamc@188 950 end
adamc@171 951 in
adamc@171 952 case p of
adamc@171 953 L.PWild => (((L'.PWild, loc), cunif (loc, (L'.KType, loc))),
adamc@171 954 (env, bound))
adamc@171 955 | L.PVar x =>
adamc@171 956 let
adamc@171 957 val t = if SS.member (bound, x) then
adamc@171 958 (expError env (DuplicatePatternVariable (loc, x));
adamc@171 959 terror)
adamc@171 960 else
adamc@171 961 cunif (loc, (L'.KType, loc))
adamc@171 962 in
adamc@182 963 (((L'.PVar (x, t), loc), t),
adamc@171 964 (E.pushERel env x t, SS.add (bound, x)))
adamc@171 965 end
adamc@173 966 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
adamc@173 967 (env, bound))
adamc@171 968 | L.PCon ([], x, po) =>
adamc@171 969 (case E.lookupConstructor env x of
adamc@174 970 NONE => (expError env (UnboundConstructor (loc, [], x));
adamc@171 971 rerror)
adamc@188 972 | SOME (dk, n, to, dn) => pcon (L'.PConVar n, po, to, (L'.CNamed dn, loc), dk))
adamc@174 973 | L.PCon (m1 :: ms, x, po) =>
adamc@174 974 (case E.lookupStr env m1 of
adamc@174 975 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@174 976 rerror)
adamc@174 977 | SOME (n, sgn) =>
adamc@174 978 let
adamc@174 979 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@174 980 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@182 981 NONE => raise Fail "elabPat: Unknown substructure"
adamc@174 982 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@174 983 ((L'.StrVar n, loc), sgn) ms
adamc@174 984 in
adamc@174 985 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 986 NONE => (expError env (UnboundConstructor (loc, m1 :: ms, x));
adamc@174 987 rerror)
adamc@188 988 | SOME (dk, _, to, dn) => pcon (L'.PConProj (n, ms, x), po, to, dn, dk)
adamc@174 989 end)
adamc@174 990
adamc@175 991 | L.PRecord (xps, flex) =>
adamc@175 992 let
adamc@175 993 val (xpts, (env, bound, _)) =
adamc@175 994 ListUtil.foldlMap (fn ((x, p), (env, bound, fbound)) =>
adamc@175 995 let
adamc@175 996 val ((p', t), (env, bound)) = elabPat (p, (env, denv, bound))
adamc@175 997 in
adamc@175 998 if SS.member (fbound, x) then
adamc@175 999 expError env (DuplicatePatField (loc, x))
adamc@175 1000 else
adamc@175 1001 ();
adamc@175 1002 ((x, p', t), (env, bound, SS.add (fbound, x)))
adamc@175 1003 end)
adamc@175 1004 (env, bound, SS.empty) xps
adamc@175 1005
adamc@175 1006 val k = (L'.KType, loc)
adamc@175 1007 val c = (L'.CRecord (k, map (fn (x, _, t) => ((L'.CName x, loc), t)) xpts), loc)
adamc@176 1008 val c =
adamc@175 1009 if flex then
adamc@176 1010 (L'.CConcat (c, cunif (loc, (L'.KRecord k, loc))), loc)
adamc@175 1011 else
adamc@176 1012 c
adamc@175 1013 in
adamc@182 1014 (((L'.PRecord xpts, loc),
adamc@175 1015 (L'.TRecord c, loc)),
adamc@175 1016 (env, bound))
adamc@175 1017 end
adamc@175 1018
adamc@171 1019 end
adamc@171 1020
adamc@172 1021 datatype coverage =
adamc@172 1022 Wild
adamc@173 1023 | None
adamc@172 1024 | Datatype of coverage IM.map
adamc@175 1025 | Record of coverage SM.map list
adamc@172 1026
adamc@172 1027 fun exhaustive (env, denv, t, ps) =
adamc@172 1028 let
adamc@172 1029 fun pcCoverage pc =
adamc@172 1030 case pc of
adamc@172 1031 L'.PConVar n => n
adamc@174 1032 | L'.PConProj (m1, ms, x) =>
adamc@174 1033 let
adamc@174 1034 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@174 1035 in
adamc@174 1036 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 1037 NONE => raise Fail "exhaustive: Can't project constructor"
adamc@188 1038 | SOME (_, n, _, _) => n
adamc@174 1039 end
adamc@172 1040
adamc@172 1041 fun coverage (p, _) =
adamc@172 1042 case p of
adamc@172 1043 L'.PWild => Wild
adamc@172 1044 | L'.PVar _ => Wild
adamc@173 1045 | L'.PPrim _ => None
adamc@188 1046 | L'.PCon (_, pc, NONE) => Datatype (IM.insert (IM.empty, pcCoverage pc, Wild))
adamc@188 1047 | L'.PCon (_, pc, SOME p) => Datatype (IM.insert (IM.empty, pcCoverage pc, coverage p))
adamc@182 1048 | L'.PRecord xps => Record [foldl (fn ((x, p, _), fmap) =>
adamc@182 1049 SM.insert (fmap, x, coverage p)) SM.empty xps]
adamc@176 1050
adamc@172 1051 fun merge (c1, c2) =
adamc@172 1052 case (c1, c2) of
adamc@173 1053 (None, _) => c2
adamc@173 1054 | (_, None) => c1
adamc@173 1055
adamc@173 1056 | (Wild, _) => Wild
adamc@172 1057 | (_, Wild) => Wild
adamc@172 1058
adamc@172 1059 | (Datatype cm1, Datatype cm2) => Datatype (IM.unionWith merge (cm1, cm2))
adamc@172 1060
adamc@175 1061 | (Record fm1, Record fm2) => Record (fm1 @ fm2)
adamc@175 1062
adamc@175 1063 | _ => None
adamc@175 1064
adamc@172 1065 fun combinedCoverage ps =
adamc@172 1066 case ps of
adamc@172 1067 [] => raise Fail "Empty pattern list for coverage checking"
adamc@172 1068 | [p] => coverage p
adamc@172 1069 | p :: ps => merge (coverage p, combinedCoverage ps)
adamc@172 1070
adamc@175 1071 fun enumerateCases t =
adamc@175 1072 let
adamc@175 1073 fun dtype cons =
adamc@175 1074 ListUtil.mapConcat (fn (_, n, to) =>
adamc@175 1075 case to of
adamc@175 1076 NONE => [Datatype (IM.insert (IM.empty, n, Wild))]
adamc@175 1077 | SOME t => map (fn c => Datatype (IM.insert (IM.empty, n, c)))
adamc@175 1078 (enumerateCases t)) cons
adamc@175 1079 in
adamc@175 1080 case #1 (#1 (hnormCon (env, denv) t)) of
adamc@175 1081 L'.CNamed n =>
adamc@175 1082 (let
adamc@175 1083 val dt = E.lookupDatatype env n
adamc@175 1084 val cons = E.constructors dt
adamc@175 1085 in
adamc@175 1086 dtype cons
adamc@175 1087 end handle E.UnboundNamed _ => [Wild])
adamc@175 1088 | L'.TRecord c =>
adamc@175 1089 (case #1 (#1 (hnormCon (env, denv) c)) of
adamc@175 1090 L'.CRecord (_, xts) =>
adamc@175 1091 let
adamc@175 1092 val xts = map (fn (x, t) => (#1 (hnormCon (env, denv) x), t)) xts
adamc@175 1093
adamc@175 1094 fun exponentiate fs =
adamc@175 1095 case fs of
adamc@175 1096 [] => [SM.empty]
adamc@175 1097 | ((L'.CName x, _), t) :: rest =>
adamc@175 1098 let
adamc@175 1099 val this = enumerateCases t
adamc@175 1100 val rest = exponentiate rest
adamc@175 1101 in
adamc@175 1102 ListUtil.mapConcat (fn fmap =>
adamc@175 1103 map (fn c => SM.insert (fmap, x, c)) this) rest
adamc@175 1104 end
adamc@175 1105 | _ => raise Fail "exponentiate: Not CName"
adamc@175 1106 in
adamc@175 1107 if List.exists (fn ((L'.CName _, _), _) => false
adamc@175 1108 | (c, _) => true) xts then
adamc@175 1109 [Wild]
adamc@175 1110 else
adamc@175 1111 map (fn ls => Record [ls]) (exponentiate xts)
adamc@175 1112 end
adamc@175 1113 | _ => [Wild])
adamc@175 1114 | _ => [Wild]
adamc@175 1115 end
adamc@175 1116
adamc@175 1117 fun coverageImp (c1, c2) =
adamc@175 1118 case (c1, c2) of
adamc@175 1119 (Wild, _) => true
adamc@175 1120
adamc@175 1121 | (Datatype cmap1, Datatype cmap2) =>
adamc@175 1122 List.all (fn (n, c2) =>
adamc@175 1123 case IM.find (cmap1, n) of
adamc@175 1124 NONE => false
adamc@175 1125 | SOME c1 => coverageImp (c1, c2)) (IM.listItemsi cmap2)
adamc@175 1126
adamc@175 1127 | (Record fmaps1, Record fmaps2) =>
adamc@175 1128 List.all (fn fmap2 =>
adamc@175 1129 List.exists (fn fmap1 =>
adamc@175 1130 List.all (fn (x, c2) =>
adamc@175 1131 case SM.find (fmap1, x) of
adamc@175 1132 NONE => true
adamc@175 1133 | SOME c1 => coverageImp (c1, c2))
adamc@175 1134 (SM.listItemsi fmap2))
adamc@175 1135 fmaps1) fmaps2
adamc@175 1136
adamc@175 1137 | _ => false
adamc@175 1138
adamc@172 1139 fun isTotal (c, t) =
adamc@172 1140 case c of
adamc@173 1141 None => (false, [])
adamc@173 1142 | Wild => (true, [])
adamc@172 1143 | Datatype cm =>
adamc@172 1144 let
adamc@172 1145 val ((t, _), gs) = hnormCon (env, denv) t
adamc@174 1146
adamc@174 1147 fun dtype cons =
adamc@174 1148 foldl (fn ((_, n, to), (total, gs)) =>
adamc@174 1149 case IM.find (cm, n) of
adamc@174 1150 NONE => (false, gs)
adamc@174 1151 | SOME c' =>
adamc@174 1152 case to of
adamc@174 1153 NONE => (total, gs)
adamc@174 1154 | SOME t' =>
adamc@174 1155 let
adamc@174 1156 val (total, gs') = isTotal (c', t')
adamc@174 1157 in
adamc@174 1158 (total, gs' @ gs)
adamc@174 1159 end)
adamc@174 1160 (true, gs) cons
adamc@172 1161 in
adamc@172 1162 case t of
adamc@172 1163 L'.CNamed n =>
adamc@172 1164 let
adamc@172 1165 val dt = E.lookupDatatype env n
adamc@172 1166 val cons = E.constructors dt
adamc@172 1167 in
adamc@174 1168 dtype cons
adamc@174 1169 end
adamc@174 1170 | L'.CModProj (m1, ms, x) =>
adamc@174 1171 let
adamc@174 1172 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@174 1173 in
adamc@174 1174 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
adamc@174 1175 NONE => raise Fail "isTotal: Can't project datatype"
adamc@174 1176 | SOME cons => dtype cons
adamc@172 1177 end
adamc@172 1178 | L'.CError => (true, gs)
adamc@172 1179 | _ => raise Fail "isTotal: Not a datatype"
adamc@172 1180 end
adamc@175 1181 | Record _ => (List.all (fn c2 => coverageImp (c, c2)) (enumerateCases t), [])
adamc@172 1182 in
adamc@172 1183 isTotal (combinedCoverage ps, t)
adamc@172 1184 end
adamc@172 1185
adamc@92 1186 fun elabExp (env, denv) (eAll as (e, loc)) =
adamc@91 1187 let
adamc@92 1188
adamc@92 1189 in
adamc@92 1190 (*eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*)
adamc@10 1191
adamc@91 1192 case e of
adamc@91 1193 L.EAnnot (e, t) =>
adamc@91 1194 let
adamc@91 1195 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1196 val (t', _, gs2) = elabCon (env, denv) t
adamc@91 1197 val gs3 = checkCon (env, denv) e' et t'
adamc@91 1198 in
adamc@91 1199 (e', t', gs1 @ gs2 @ gs3)
adamc@91 1200 end
adamc@34 1201
adamc@91 1202 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@91 1203 | L.EVar ([], s) =>
adamc@91 1204 (case E.lookupE env s of
adamc@91 1205 E.NotBound =>
adamc@91 1206 (expError env (UnboundExp (loc, s));
adamc@91 1207 (eerror, cerror, []))
adamc@91 1208 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
adamc@91 1209 | E.Named (n, t) => ((L'.ENamed n, loc), t, []))
adamc@91 1210 | L.EVar (m1 :: ms, s) =>
adamc@91 1211 (case E.lookupStr env m1 of
adamc@91 1212 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@91 1213 (eerror, cerror, []))
adamc@91 1214 | SOME (n, sgn) =>
adamc@91 1215 let
adamc@91 1216 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@91 1217 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@91 1218 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@91 1219 (strerror, sgnerror))
adamc@91 1220 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@91 1221 ((L'.StrVar n, loc), sgn) ms
adamc@10 1222
adamc@91 1223 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@91 1224 NONE => (expError env (UnboundExp (loc, s));
adamc@91 1225 cerror)
adamc@91 1226 | SOME t => t
adamc@91 1227 in
adamc@91 1228 ((L'.EModProj (n, ms, s), loc), t, [])
adamc@91 1229 end)
adamc@86 1230
adamc@91 1231 | L.EApp (e1, e2) =>
adamc@91 1232 let
adamc@91 1233 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@91 1234 val (e1', t1, gs2) = elabHead (env, denv) e1' t1
adamc@91 1235 val (e2', t2, gs3) = elabExp (env, denv) e2
adamc@71 1236
adamc@91 1237 val dom = cunif (loc, ktype)
adamc@91 1238 val ran = cunif (loc, ktype)
adamc@91 1239 val t = (L'.TFun (dom, ran), dummy)
adamc@91 1240
adamc@91 1241 val gs4 = checkCon (env, denv) e1' t1 t
adamc@91 1242 val gs5 = checkCon (env, denv) e2' t2 dom
adamc@91 1243 in
adamc@91 1244 ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2 @ gs3 @ gs4 @ gs5)
adamc@91 1245 end
adamc@91 1246 | L.EAbs (x, to, e) =>
adamc@91 1247 let
adamc@91 1248 val (t', gs1) = case to of
adamc@91 1249 NONE => (cunif (loc, ktype), [])
adamc@91 1250 | SOME t =>
adamc@91 1251 let
adamc@91 1252 val (t', tk, gs) = elabCon (env, denv) t
adamc@91 1253 in
adamc@91 1254 checkKind env t' tk ktype;
adamc@91 1255 (t', gs)
adamc@91 1256 end
adamc@91 1257 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
adamc@91 1258 in
adamc@91 1259 ((L'.EAbs (x, t', et, e'), loc),
adamc@91 1260 (L'.TFun (t', et), loc),
adamc@91 1261 gs1 @ gs2)
adamc@91 1262 end
adamc@91 1263 | L.ECApp (e, c) =>
adamc@91 1264 let
adamc@91 1265 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1266 val (e', et, gs2) = elabHead (env, denv) e' et
adamc@91 1267 val (c', ck, gs3) = elabCon (env, denv) c
adamc@91 1268 val ((et', _), gs4) = hnormCon (env, denv) et
adamc@91 1269 in
adamc@91 1270 case et' of
adamc@91 1271 L'.CError => (eerror, cerror, [])
adamc@91 1272 | L'.TCFun (_, _, k, eb) =>
adamc@91 1273 let
adamc@91 1274 val () = checkKind env c' ck k
adamc@91 1275 val eb' = subConInCon (0, c') eb
adamc@91 1276 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@91 1277 cerror)
adamc@91 1278 in
adamc@91 1279 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1280 end
adamc@91 1281
adamc@91 1282 | L'.CUnif _ =>
adamc@91 1283 (expError env (Unif ("application", et));
adamc@91 1284 (eerror, cerror, []))
adamc@91 1285
adamc@91 1286 | _ =>
adamc@91 1287 (expError env (WrongForm ("constructor function", e', et));
adamc@91 1288 (eerror, cerror, []))
adamc@91 1289 end
adamc@91 1290 | L.ECAbs (expl, x, k, e) =>
adamc@91 1291 let
adamc@91 1292 val expl' = elabExplicitness expl
adamc@91 1293 val k' = elabKind k
adamc@91 1294 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
adamc@91 1295 in
adamc@91 1296 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@91 1297 (L'.TCFun (expl', x, k', et), loc),
adamc@91 1298 gs)
adamc@91 1299 end
adamc@91 1300
adamc@91 1301 | L.EDisjoint (c1, c2, e) =>
adamc@91 1302 let
adamc@91 1303 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@91 1304 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@91 1305
adamc@91 1306 val ku1 = kunif loc
adamc@91 1307 val ku2 = kunif loc
adamc@91 1308
adamc@91 1309 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@91 1310 val (e', t, gs4) = elabExp (env, denv') e
adamc@91 1311 in
adamc@91 1312 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@91 1313 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@91 1314
adamc@91 1315 (e', (L'.TDisjoint (c1', c2', t), loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1316 end
adamc@91 1317
adamc@91 1318 | L.ERecord xes =>
adamc@91 1319 let
adamc@91 1320 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@91 1321 let
adamc@91 1322 val (x', xk, gs1) = elabCon (env, denv) x
adamc@91 1323 val (e', et, gs2) = elabExp (env, denv) e
adamc@91 1324 in
adamc@91 1325 checkKind env x' xk kname;
adamc@91 1326 ((x', e', et), gs1 @ gs2 @ gs)
adamc@91 1327 end)
adamc@91 1328 [] xes
adamc@91 1329
adamc@91 1330 val k = (L'.KType, loc)
adamc@91 1331
adamc@91 1332 fun prove (xets, gs) =
adamc@91 1333 case xets of
adamc@91 1334 [] => gs
adamc@91 1335 | (x, _, t) :: rest =>
adamc@91 1336 let
adamc@91 1337 val xc = (x, t)
adamc@91 1338 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@91 1339 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@91 1340 let
adamc@91 1341 val xc' = (x', t')
adamc@91 1342 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@91 1343 in
adamc@91 1344 D.prove env denv (r1, r2, loc) @ gs
adamc@91 1345 end)
adamc@91 1346 gs rest
adamc@91 1347 in
adamc@91 1348 prove (rest, gs)
adamc@91 1349 end
adamc@91 1350 in
adamc@91 1351 ((L'.ERecord xes', loc),
adamc@91 1352 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@91 1353 prove (xes', gs))
adamc@91 1354 end
adamc@91 1355
adamc@91 1356 | L.EField (e, c) =>
adamc@91 1357 let
adamc@91 1358 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1359 val (c', ck, gs2) = elabCon (env, denv) c
adamc@91 1360
adamc@91 1361 val ft = cunif (loc, ktype)
adamc@91 1362 val rest = cunif (loc, ktype_record)
adamc@91 1363 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@91 1364
adamc@91 1365 val gs3 =
adamc@91 1366 checkCon (env, denv) e' et
adamc@91 1367 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@91 1368 val gs4 = D.prove env denv (first, rest, loc)
adamc@91 1369 in
adamc@91 1370 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1371 end
adamc@91 1372
adamc@149 1373 | L.ECut (e, c) =>
adamc@149 1374 let
adamc@149 1375 val (e', et, gs1) = elabExp (env, denv) e
adamc@149 1376 val (c', ck, gs2) = elabCon (env, denv) c
adamc@149 1377
adamc@149 1378 val ft = cunif (loc, ktype)
adamc@149 1379 val rest = cunif (loc, ktype_record)
adamc@149 1380 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@149 1381
adamc@149 1382 val gs3 =
adamc@149 1383 checkCon (env, denv) e' et
adamc@149 1384 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@149 1385 val gs4 = D.prove env denv (first, rest, loc)
adamc@149 1386 in
adamc@149 1387 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@149 1388 end
adamc@149 1389
adamc@91 1390 | L.EFold =>
adamc@91 1391 let
adamc@91 1392 val dom = kunif loc
adamc@91 1393 in
adamc@91 1394 ((L'.EFold dom, loc), foldType (dom, loc), [])
adamc@91 1395 end
adamc@170 1396
adamc@171 1397 | L.ECase (e, pes) =>
adamc@171 1398 let
adamc@171 1399 val (e', et, gs1) = elabExp (env, denv) e
adamc@171 1400 val result = cunif (loc, (L'.KType, loc))
adamc@171 1401 val (pes', gs) = ListUtil.foldlMap
adamc@171 1402 (fn ((p, e), gs) =>
adamc@171 1403 let
adamc@174 1404 val ((p', pt), (env, _)) = elabPat (p, (env, denv, SS.empty))
adamc@171 1405
adamc@171 1406 val gs1 = checkPatCon (env, denv) p' pt et
adamc@171 1407 val (e', et, gs2) = elabExp (env, denv) e
adamc@171 1408 val gs3 = checkCon (env, denv) e' et result
adamc@171 1409 in
adamc@171 1410 ((p', e'), gs1 @ gs2 @ gs3 @ gs)
adamc@171 1411 end)
adamc@171 1412 gs1 pes
adamc@172 1413
adamc@172 1414 val (total, gs') = exhaustive (env, denv, et, map #1 pes')
adamc@171 1415 in
adamc@172 1416 if total then
adamc@172 1417 ()
adamc@172 1418 else
adamc@172 1419 expError env (Inexhaustive loc);
adamc@172 1420
adamc@182 1421 ((L'.ECase (e', pes', {disc = et, result = result}), loc), result, gs' @ gs)
adamc@171 1422 end
adamc@91 1423 end
adamc@12 1424
adamc@12 1425
adamc@6 1426 datatype decl_error =
adamc@91 1427 KunifsRemain of L'.decl list
adamc@91 1428 | CunifsRemain of L'.decl list
adamc@91 1429
adamc@91 1430 fun lspan [] = ErrorMsg.dummySpan
adamc@91 1431 | lspan ((_, loc) :: _) = loc
adamc@6 1432
adamc@6 1433 fun declError env err =
adamc@6 1434 case err of
adamc@91 1435 KunifsRemain ds =>
adamc@91 1436 (ErrorMsg.errorAt (lspan ds) "Some kind unification variables are undetermined in declaration";
adamc@91 1437 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@91 1438 | CunifsRemain ds =>
adamc@91 1439 (ErrorMsg.errorAt (lspan ds) "Some constructor unification variables are undetermined in declaration";
adamc@91 1440 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@6 1441
adamc@31 1442 datatype sgn_error =
adamc@31 1443 UnboundSgn of ErrorMsg.span * string
adamc@31 1444 | UnmatchedSgi of L'.sgn_item
adamc@31 1445 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 1446 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@156 1447 | SgiMismatchedDatatypes of L'.sgn_item * L'.sgn_item * (L'.con * L'.con * cunify_error) option
adamc@41 1448 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 1449 | UnWhereable of L'.sgn * string
adamc@75 1450 | WhereWrongKind of L'.kind * L'.kind * kunify_error
adamc@58 1451 | NotIncludable of L'.sgn
adamc@62 1452 | DuplicateCon of ErrorMsg.span * string
adamc@62 1453 | DuplicateVal of ErrorMsg.span * string
adamc@62 1454 | DuplicateSgn of ErrorMsg.span * string
adamc@62 1455 | DuplicateStr of ErrorMsg.span * string
adamc@88 1456 | NotConstraintsable of L'.sgn
adamc@31 1457
adamc@31 1458 fun sgnError env err =
adamc@31 1459 case err of
adamc@31 1460 UnboundSgn (loc, s) =>
adamc@31 1461 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 1462 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 1463 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 1464 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 1465 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 1466 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@59 1467 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1468 ("Need", p_sgn_item env sgi2),
adamc@31 1469 ("Kind 1", p_kind k1),
adamc@31 1470 ("Kind 2", p_kind k2)];
adamc@31 1471 kunifyError kerr)
adamc@31 1472 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 1473 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@59 1474 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1475 ("Need", p_sgn_item env sgi2),
adamc@31 1476 ("Con 1", p_con env c1),
adamc@31 1477 ("Con 2", p_con env c2)];
adamc@31 1478 cunifyError env cerr)
adamc@156 1479 | SgiMismatchedDatatypes (sgi1, sgi2, cerro) =>
adamc@156 1480 (ErrorMsg.errorAt (#2 sgi1) "Mismatched 'datatype' specifications:";
adamc@156 1481 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@156 1482 ("Need", p_sgn_item env sgi2)];
adamc@156 1483 Option.app (fn (c1, c2, ue) =>
adamc@156 1484 (eprefaces "Unification error"
adamc@156 1485 [("Con 1", p_con env c1),
adamc@156 1486 ("Con 2", p_con env c2)];
adamc@156 1487 cunifyError env ue)) cerro)
adamc@41 1488 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1489 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1490 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1491 ("Sig 2", p_sgn env sgn2)])
adamc@42 1492 | UnWhereable (sgn, x) =>
adamc@42 1493 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1494 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1495 ("Field", PD.string x)])
adamc@75 1496 | WhereWrongKind (k1, k2, kerr) =>
adamc@75 1497 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
adamc@75 1498 eprefaces' [("Have", p_kind k1),
adamc@75 1499 ("Need", p_kind k2)];
adamc@75 1500 kunifyError kerr)
adamc@58 1501 | NotIncludable sgn =>
adamc@58 1502 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
adamc@58 1503 eprefaces' [("Signature", p_sgn env sgn)])
adamc@62 1504 | DuplicateCon (loc, s) =>
adamc@62 1505 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
adamc@62 1506 | DuplicateVal (loc, s) =>
adamc@62 1507 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
adamc@62 1508 | DuplicateSgn (loc, s) =>
adamc@62 1509 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
adamc@62 1510 | DuplicateStr (loc, s) =>
adamc@62 1511 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
adamc@88 1512 | NotConstraintsable sgn =>
adamc@88 1513 (ErrorMsg.errorAt (#2 sgn) "Invalid signature for 'open constraints'";
adamc@88 1514 eprefaces' [("Signature", p_sgn env sgn)])
adamc@31 1515
adamc@31 1516 datatype str_error =
adamc@31 1517 UnboundStr of ErrorMsg.span * string
adamc@44 1518 | NotFunctor of L'.sgn
adamc@46 1519 | FunctorRebind of ErrorMsg.span
adamc@61 1520 | UnOpenable of L'.sgn
adamc@75 1521 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
adamc@156 1522 | DuplicateConstructor of string * ErrorMsg.span
adamc@158 1523 | NotDatatype of ErrorMsg.span
adamc@31 1524
adamc@31 1525 fun strError env err =
adamc@31 1526 case err of
adamc@31 1527 UnboundStr (loc, s) =>
adamc@31 1528 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1529 | NotFunctor sgn =>
adamc@44 1530 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1531 eprefaces' [("Signature", p_sgn env sgn)])
adamc@46 1532 | FunctorRebind loc =>
adamc@46 1533 ErrorMsg.errorAt loc "Attempt to rebind functor"
adamc@61 1534 | UnOpenable sgn =>
adamc@61 1535 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
adamc@61 1536 eprefaces' [("Signature", p_sgn env sgn)])
adamc@75 1537 | NotType (k, (k1, k2, ue)) =>
adamc@75 1538 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
adamc@75 1539 eprefaces' [("Kind", p_kind k),
adamc@75 1540 ("Subkind 1", p_kind k1),
adamc@75 1541 ("Subkind 2", p_kind k2)];
adamc@75 1542 kunifyError ue)
adamc@156 1543 | DuplicateConstructor (x, loc) =>
adamc@156 1544 ErrorMsg.errorAt loc ("Duplicate datatype constructor " ^ x)
adamc@158 1545 | NotDatatype loc =>
adamc@158 1546 ErrorMsg.errorAt loc "Trying to import non-datatype as a datatype"
adamc@31 1547
adamc@42 1548 val hnormSgn = E.hnormSgn
adamc@31 1549
adamc@88 1550 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@76 1551 case sgi of
adamc@76 1552 L.SgiConAbs (x, k) =>
adamc@76 1553 let
adamc@76 1554 val k' = elabKind k
adamc@31 1555
adamc@76 1556 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1557 in
adamc@88 1558 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@76 1559 end
adamc@31 1560
adamc@76 1561 | L.SgiCon (x, ko, c) =>
adamc@76 1562 let
adamc@76 1563 val k' = case ko of
adamc@76 1564 NONE => kunif loc
adamc@76 1565 | SOME k => elabKind k
adamc@31 1566
adamc@83 1567 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1568 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1569 in
adamc@76 1570 checkKind env c' ck k';
adamc@31 1571
adamc@88 1572 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1573 end
adamc@31 1574
adamc@157 1575 | L.SgiDatatype (x, xcs) =>
adamc@157 1576 let
adamc@157 1577 val k = (L'.KType, loc)
adamc@157 1578 val (env, n) = E.pushCNamed env x k NONE
adamc@157 1579 val t = (L'.CNamed n, loc)
adamc@157 1580
adamc@157 1581 val (xcs, (used, env, gs)) =
adamc@157 1582 ListUtil.foldlMap
adamc@157 1583 (fn ((x, to), (used, env, gs)) =>
adamc@157 1584 let
adamc@157 1585 val (to, t, gs') = case to of
adamc@157 1586 NONE => (NONE, t, gs)
adamc@157 1587 | SOME t' =>
adamc@157 1588 let
adamc@157 1589 val (t', tk, gs') = elabCon (env, denv) t'
adamc@157 1590 in
adamc@157 1591 checkKind env t' tk k;
adamc@157 1592 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@157 1593 end
adamc@157 1594
adamc@157 1595 val (env, n') = E.pushENamed env x t
adamc@157 1596 in
adamc@157 1597 if SS.member (used, x) then
adamc@157 1598 strError env (DuplicateConstructor (x, loc))
adamc@157 1599 else
adamc@157 1600 ();
adamc@157 1601 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@157 1602 end)
adamc@157 1603 (SS.empty, env, []) xcs
adamc@157 1604 in
adamc@157 1605 ([(L'.SgiDatatype (x, n, xcs), loc)], (env, denv, gs))
adamc@157 1606 end
adamc@156 1607
adamc@158 1608 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
adamc@158 1609
adamc@158 1610 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
adamc@158 1611 (case E.lookupStr env m1 of
adamc@158 1612 NONE => (strError env (UnboundStr (loc, m1));
adamc@158 1613 ([], (env, denv, gs)))
adamc@158 1614 | SOME (n, sgn) =>
adamc@158 1615 let
adamc@158 1616 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@158 1617 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1618 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@158 1619 (strerror, sgnerror))
adamc@158 1620 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@158 1621 ((L'.StrVar n, loc), sgn) ms
adamc@158 1622 in
adamc@161 1623 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@161 1624 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@161 1625 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@161 1626 NONE => (conError env (UnboundDatatype (loc, s));
adamc@161 1627 ([], (env, denv, gs)))
adamc@161 1628 | SOME xncs =>
adamc@161 1629 let
adamc@161 1630 val k = (L'.KType, loc)
adamc@161 1631 val t = (L'.CModProj (n, ms, s), loc)
adamc@161 1632 val (env, n') = E.pushCNamed env x k (SOME t)
adamc@161 1633 val env = E.pushDatatype env n' xncs
adamc@158 1634
adamc@161 1635 val t = (L'.CNamed n', loc)
adamc@161 1636 val env = foldl (fn ((x, n, to), env) =>
adamc@161 1637 let
adamc@161 1638 val t = case to of
adamc@161 1639 NONE => t
adamc@161 1640 | SOME t' => (L'.TFun (t', t), loc)
adamc@161 1641 in
adamc@161 1642 E.pushENamedAs env x n t
adamc@161 1643 end) env xncs
adamc@161 1644 in
adamc@162 1645 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xncs), loc)], (env, denv, gs))
adamc@161 1646 end)
adamc@161 1647 | _ => (strError env (NotDatatype loc);
adamc@161 1648 ([], (env, denv, [])))
adamc@158 1649 end)
adamc@156 1650
adamc@76 1651 | L.SgiVal (x, c) =>
adamc@76 1652 let
adamc@83 1653 val (c', ck, gs') = elabCon (env, denv) c
adamc@31 1654
adamc@76 1655 val (env', n) = E.pushENamed env x c'
adamc@76 1656 in
adamc@76 1657 (unifyKinds ck ktype
adamc@76 1658 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 1659
adamc@88 1660 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@76 1661 end
adamc@31 1662
adamc@76 1663 | L.SgiStr (x, sgn) =>
adamc@76 1664 let
adamc@83 1665 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1666 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1667 in
adamc@88 1668 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1669 end
adamc@31 1670
adamc@76 1671 | L.SgiSgn (x, sgn) =>
adamc@76 1672 let
adamc@83 1673 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1674 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1675 in
adamc@88 1676 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1677 end
adamc@31 1678
adamc@76 1679 | L.SgiInclude sgn =>
adamc@76 1680 let
adamc@83 1681 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1682 in
adamc@76 1683 case #1 (hnormSgn env sgn') of
adamc@76 1684 L'.SgnConst sgis =>
adamc@88 1685 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@76 1686 | _ => (sgnError env (NotIncludable sgn');
adamc@88 1687 ([], (env, denv, [])))
adamc@88 1688 end
adamc@88 1689
adamc@88 1690 | L.SgiConstraint (c1, c2) =>
adamc@88 1691 let
adamc@88 1692 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 1693 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@88 1694
adamc@90 1695 val (denv, gs3) = D.assert env denv (c1', c2')
adamc@88 1696 in
adamc@88 1697 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 1698 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 1699
adamc@90 1700 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3))
adamc@76 1701 end
adamc@31 1702
adamc@83 1703 and elabSgn (env, denv) (sgn, loc) =
adamc@31 1704 case sgn of
adamc@31 1705 L.SgnConst sgis =>
adamc@31 1706 let
adamc@88 1707 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 1708
adamc@62 1709 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 1710 case sgi of
adamc@62 1711 L'.SgiConAbs (x, _, _) =>
adamc@62 1712 (if SS.member (cons, x) then
adamc@62 1713 sgnError env (DuplicateCon (loc, x))
adamc@62 1714 else
adamc@62 1715 ();
adamc@62 1716 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1717 | L'.SgiCon (x, _, _, _) =>
adamc@62 1718 (if SS.member (cons, x) then
adamc@62 1719 sgnError env (DuplicateCon (loc, x))
adamc@62 1720 else
adamc@62 1721 ();
adamc@62 1722 (SS.add (cons, x), vals, sgns, strs))
adamc@156 1723 | L'.SgiDatatype (x, _, xncs) =>
adamc@156 1724 let
adamc@156 1725 val vals = foldl (fn ((x, _, _), vals) =>
adamc@156 1726 (if SS.member (vals, x) then
adamc@156 1727 sgnError env (DuplicateVal (loc, x))
adamc@156 1728 else
adamc@156 1729 ();
adamc@156 1730 SS.add (vals, x)))
adamc@156 1731 vals xncs
adamc@156 1732 in
adamc@156 1733 if SS.member (cons, x) then
adamc@156 1734 sgnError env (DuplicateCon (loc, x))
adamc@156 1735 else
adamc@156 1736 ();
adamc@156 1737 (SS.add (cons, x), vals, sgns, strs)
adamc@156 1738 end
adamc@162 1739 | L'.SgiDatatypeImp (x, _, _, _, _, _) =>
adamc@156 1740 (if SS.member (cons, x) then
adamc@156 1741 sgnError env (DuplicateCon (loc, x))
adamc@156 1742 else
adamc@156 1743 ();
adamc@156 1744 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1745 | L'.SgiVal (x, _, _) =>
adamc@62 1746 (if SS.member (vals, x) then
adamc@62 1747 sgnError env (DuplicateVal (loc, x))
adamc@62 1748 else
adamc@62 1749 ();
adamc@62 1750 (cons, SS.add (vals, x), sgns, strs))
adamc@62 1751 | L'.SgiSgn (x, _, _) =>
adamc@62 1752 (if SS.member (sgns, x) then
adamc@62 1753 sgnError env (DuplicateSgn (loc, x))
adamc@62 1754 else
adamc@62 1755 ();
adamc@62 1756 (cons, vals, SS.add (sgns, x), strs))
adamc@62 1757 | L'.SgiStr (x, _, _) =>
adamc@62 1758 (if SS.member (strs, x) then
adamc@62 1759 sgnError env (DuplicateStr (loc, x))
adamc@62 1760 else
adamc@62 1761 ();
adamc@88 1762 (cons, vals, sgns, SS.add (strs, x)))
adamc@88 1763 | L'.SgiConstraint _ => (cons, vals, sgns, strs))
adamc@62 1764 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 1765 in
adamc@83 1766 ((L'.SgnConst sgis', loc), gs)
adamc@31 1767 end
adamc@31 1768 | L.SgnVar x =>
adamc@31 1769 (case E.lookupSgn env x of
adamc@31 1770 NONE =>
adamc@31 1771 (sgnError env (UnboundSgn (loc, x));
adamc@83 1772 ((L'.SgnError, loc), []))
adamc@83 1773 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 1774 | L.SgnFun (m, dom, ran) =>
adamc@41 1775 let
adamc@83 1776 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1777 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1778 val (ran', gs2) = elabSgn (env', denv) ran
adamc@41 1779 in
adamc@83 1780 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 1781 end
adamc@42 1782 | L.SgnWhere (sgn, x, c) =>
adamc@42 1783 let
adamc@83 1784 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 1785 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 1786 in
adamc@42 1787 case #1 (hnormSgn env sgn') of
adamc@83 1788 L'.SgnError => (sgnerror, [])
adamc@42 1789 | L'.SgnConst sgis =>
adamc@75 1790 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 1791 x' = x andalso
adamc@75 1792 (unifyKinds k ck
adamc@75 1793 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 1794 true)
adamc@42 1795 | _ => false) sgis then
adamc@83 1796 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 1797 else
adamc@42 1798 (sgnError env (UnWhereable (sgn', x));
adamc@83 1799 (sgnerror, []))
adamc@42 1800 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 1801 (sgnerror, []))
adamc@42 1802 end
adamc@59 1803 | L.SgnProj (m, ms, x) =>
adamc@59 1804 (case E.lookupStr env m of
adamc@59 1805 NONE => (strError env (UnboundStr (loc, m));
adamc@83 1806 (sgnerror, []))
adamc@59 1807 | SOME (n, sgn) =>
adamc@59 1808 let
adamc@59 1809 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 1810 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 1811 NONE => (strError env (UnboundStr (loc, m));
adamc@59 1812 (strerror, sgnerror))
adamc@59 1813 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 1814 ((L'.StrVar n, loc), sgn) ms
adamc@59 1815 in
adamc@59 1816 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 1817 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 1818 (sgnerror, []))
adamc@83 1819 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 1820 end)
adamc@59 1821
adamc@31 1822
adamc@66 1823 fun selfify env {str, strs, sgn} =
adamc@66 1824 case #1 (hnormSgn env sgn) of
adamc@66 1825 L'.SgnError => sgn
adamc@66 1826 | L'.SgnVar _ => sgn
adamc@66 1827
adamc@66 1828 | L'.SgnConst sgis =>
adamc@66 1829 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 1830 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@158 1831 | (L'.SgiDatatype (x, n, xncs), loc) =>
adamc@162 1832 (L'.SgiDatatypeImp (x, n, str, strs, x, xncs), loc)
adamc@66 1833 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 1834 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 1835 | x => x) sgis), #2 sgn)
adamc@66 1836 | L'.SgnFun _ => sgn
adamc@66 1837 | L'.SgnWhere _ => sgn
adamc@66 1838 | L'.SgnProj (m, ms, x) =>
adamc@66 1839 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1840 (L'.StrVar m, #2 sgn) ms,
adamc@66 1841 sgn = #2 (E.lookupStrNamed env m),
adamc@66 1842 field = x} of
adamc@66 1843 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 1844 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 1845
adamc@66 1846 fun selfifyAt env {str, sgn} =
adamc@66 1847 let
adamc@66 1848 fun self (str, _) =
adamc@66 1849 case str of
adamc@66 1850 L'.StrVar x => SOME (x, [])
adamc@66 1851 | L'.StrProj (str, x) =>
adamc@66 1852 (case self str of
adamc@66 1853 NONE => NONE
adamc@66 1854 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 1855 | _ => NONE
adamc@66 1856 in
adamc@66 1857 case self str of
adamc@66 1858 NONE => sgn
adamc@66 1859 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 1860 end
adamc@66 1861
adamc@88 1862 fun dopen (env, denv) {str, strs, sgn} =
adamc@66 1863 let
adamc@66 1864 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1865 (L'.StrVar str, #2 sgn) strs
adamc@66 1866 in
adamc@66 1867 case #1 (hnormSgn env sgn) of
adamc@66 1868 L'.SgnConst sgis =>
adamc@88 1869 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
adamc@162 1870 let
adamc@162 1871 val d =
adamc@162 1872 case sgi of
adamc@162 1873 L'.SgiConAbs (x, n, k) =>
adamc@162 1874 let
adamc@162 1875 val c = (L'.CModProj (str, strs, x), loc)
adamc@162 1876 in
adamc@162 1877 (L'.DCon (x, n, k, c), loc)
adamc@162 1878 end
adamc@162 1879 | L'.SgiCon (x, n, k, c) =>
adamc@162 1880 (L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@162 1881 | L'.SgiDatatype (x, n, xncs) =>
adamc@162 1882 (L'.DDatatypeImp (x, n, str, strs, x, xncs), loc)
adamc@162 1883 | L'.SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
adamc@162 1884 (L'.DDatatypeImp (x, n, m1, ms, x', xncs), loc)
adamc@162 1885 | L'.SgiVal (x, n, t) =>
adamc@162 1886 (L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)
adamc@162 1887 | L'.SgiStr (x, n, sgn) =>
adamc@162 1888 (L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)
adamc@162 1889 | L'.SgiSgn (x, n, sgn) =>
adamc@162 1890 (L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)
adamc@162 1891 | L'.SgiConstraint (c1, c2) =>
adamc@162 1892 (L'.DConstraint (c1, c2), loc)
adamc@162 1893 in
adamc@162 1894 (d, (E.declBinds env' d, denv'))
adamc@162 1895 end)
adamc@88 1896 (env, denv) sgis
adamc@66 1897 | _ => (strError env (UnOpenable sgn);
adamc@88 1898 ([], (env, denv)))
adamc@66 1899 end
adamc@66 1900
adamc@88 1901 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@88 1902 case E.lookupStr env str of
adamc@88 1903 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1904 denv)
adamc@88 1905 | SOME (n, sgn) =>
adamc@88 1906 let
adamc@88 1907 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 1908 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 1909 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1910 (strerror, sgnerror))
adamc@88 1911 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 1912 ((L'.StrVar n, loc), sgn) strs
adamc@88 1913
adamc@88 1914 val cso = E.projectConstraints env {sgn = sgn, str = st}
adamc@88 1915
adamc@88 1916 val denv = case cso of
adamc@88 1917 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1918 denv)
adamc@90 1919 | SOME cs => foldl (fn ((c1, c2), denv) =>
adamc@90 1920 let
adamc@90 1921 val (denv, gs) = D.assert env denv (c1, c2)
adamc@90 1922 in
adamc@90 1923 case gs of
adamc@90 1924 [] => ()
adamc@90 1925 | _ => raise Fail "dopenConstraints: Sub-constraints remain";
adamc@90 1926
adamc@90 1927 denv
adamc@90 1928 end) denv cs
adamc@88 1929 in
adamc@88 1930 denv
adamc@88 1931 end
adamc@88 1932
adamc@31 1933 fun sgiOfDecl (d, loc) =
adamc@31 1934 case d of
adamc@123 1935 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@156 1936 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
adamc@156 1937 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
adamc@123 1938 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 1939 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 1940 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 1941 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 1942 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 1943 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 1944 | L'.DExport _ => []
adamc@88 1945
adamc@88 1946 fun sgiBindsD (env, denv) (sgi, _) =
adamc@88 1947 case sgi of
adamc@90 1948 L'.SgiConstraint (c1, c2) =>
adamc@90 1949 (case D.assert env denv (c1, c2) of
adamc@90 1950 (denv, []) => denv
adamc@90 1951 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
adamc@88 1952 | _ => denv
adamc@31 1953
adamc@86 1954 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
adamc@35 1955 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 1956 (L'.SgnError, _) => ()
adamc@31 1957 | (_, L'.SgnError) => ()
adamc@31 1958
adamc@31 1959 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1960 let
adamc@156 1961 fun folder (sgi2All as (sgi, loc), (env, denv)) =
adamc@31 1962 let
adamc@31 1963 fun seek p =
adamc@31 1964 let
adamc@88 1965 fun seek (env, denv) ls =
adamc@31 1966 case ls of
adamc@31 1967 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@88 1968 (env, denv))
adamc@31 1969 | h :: t =>
adamc@31 1970 case p h of
adamc@88 1971 NONE => seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
adamc@88 1972 | SOME envs => envs
adamc@31 1973 in
adamc@88 1974 seek (env, denv) sgis1
adamc@31 1975 end
adamc@31 1976 in
adamc@31 1977 case sgi of
adamc@31 1978 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1979 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1980 let
adamc@41 1981 fun found (x', n1, k1, co1) =
adamc@41 1982 if x = x' then
adamc@41 1983 let
adamc@41 1984 val () = unifyKinds k1 k2
adamc@41 1985 handle KUnify (k1, k2, err) =>
adamc@41 1986 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 1987 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 1988 in
adamc@41 1989 SOME (if n1 = n2 then
adamc@41 1990 env
adamc@41 1991 else
adamc@88 1992 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
adamc@88 1993 denv)
adamc@41 1994 end
adamc@41 1995 else
adamc@41 1996 NONE
adamc@31 1997 in
adamc@31 1998 case sgi1 of
adamc@41 1999 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 2000 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@159 2001 | L'.SgiDatatype (x', n1, _) => found (x', n1, (L'.KType, loc), NONE)
adamc@162 2002 | L'.SgiDatatypeImp (x', n1, m1, ms, s, _) =>
adamc@159 2003 found (x', n1, (L'.KType, loc), SOME (L'.CModProj (m1, ms, s), loc))
adamc@31 2004 | _ => NONE
adamc@31 2005 end)
adamc@31 2006
adamc@31 2007 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 2008 seek (fn sgi1All as (sgi1, _) =>
adamc@31 2009 case sgi1 of
adamc@41 2010 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 2011 if x = x' then
adamc@41 2012 let
adamc@88 2013 fun good () = SOME (E.pushCNamedAs env x n2 k2 (SOME c2), denv)
adamc@41 2014 in
adamc@86 2015 (case unifyCons (env, denv) c1 c2 of
adamc@86 2016 [] => good ()
adamc@86 2017 | _ => NONE)
adamc@86 2018 handle CUnify (c1, c2, err) =>
adamc@86 2019 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@86 2020 good ())
adamc@41 2021 end
adamc@41 2022 else
adamc@41 2023 NONE
adamc@31 2024 | _ => NONE)
adamc@31 2025
adamc@156 2026 | L'.SgiDatatype (x, n2, xncs2) =>
adamc@156 2027 seek (fn sgi1All as (sgi1, _) =>
adamc@160 2028 let
adamc@160 2029 fun found (n1, xncs1) =
adamc@160 2030 let
adamc@160 2031 fun mismatched ue =
adamc@160 2032 (sgnError env (SgiMismatchedDatatypes (sgi1All, sgi2All, ue));
adamc@160 2033 SOME (env, denv))
adamc@156 2034
adamc@160 2035 fun good () =
adamc@160 2036 let
adamc@160 2037 val env = E.sgiBinds env sgi2All
adamc@160 2038 val env = if n1 = n2 then
adamc@160 2039 env
adamc@160 2040 else
adamc@160 2041 E.pushCNamedAs env x n1 (L'.KType, loc)
adamc@160 2042 (SOME (L'.CNamed n1, loc))
adamc@160 2043 in
adamc@160 2044 SOME (env, denv)
adamc@160 2045 end
adamc@156 2046
adamc@160 2047 fun xncBad ((x1, _, t1), (x2, _, t2)) =
adamc@160 2048 String.compare (x1, x2) <> EQUAL
adamc@160 2049 orelse case (t1, t2) of
adamc@160 2050 (NONE, NONE) => false
adamc@160 2051 | (SOME t1, SOME t2) =>
adamc@160 2052 not (List.null (unifyCons (env, denv) t1 t2))
adamc@160 2053 | _ => true
adamc@160 2054 in
adamc@160 2055 (if length xncs1 <> length xncs2
adamc@160 2056 orelse ListPair.exists xncBad (xncs1, xncs2) then
adamc@160 2057 mismatched NONE
adamc@160 2058 else
adamc@160 2059 good ())
adamc@160 2060 handle CUnify ue => mismatched (SOME ue)
adamc@160 2061 end
adamc@160 2062 in
adamc@160 2063 case sgi1 of
adamc@160 2064 L'.SgiDatatype (x', n1, xncs1) =>
adamc@160 2065 if x' = x then
adamc@160 2066 found (n1, xncs1)
adamc@160 2067 else
adamc@160 2068 NONE
adamc@162 2069 | L'.SgiDatatypeImp (x', n1, _, _, _, xncs1) =>
adamc@162 2070 if x' = x then
adamc@162 2071 found (n1, xncs1)
adamc@162 2072 else
adamc@162 2073 NONE
adamc@160 2074 | _ => NONE
adamc@160 2075 end)
adamc@156 2076
adamc@162 2077 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, _) =>
adamc@158 2078 seek (fn sgi1All as (sgi1, _) =>
adamc@158 2079 case sgi1 of
adamc@162 2080 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _) =>
adamc@158 2081 if x = x' then
adamc@158 2082 let
adamc@158 2083 val k = (L'.KType, loc)
adamc@158 2084 val t1 = (L'.CModProj (m11, ms1, s1), loc)
adamc@158 2085 val t2 = (L'.CModProj (m12, ms2, s2), loc)
adamc@158 2086
adamc@158 2087 fun good () =
adamc@158 2088 let
adamc@158 2089 val env = E.pushCNamedAs env x n1 k (SOME t1)
adamc@158 2090 val env = E.pushCNamedAs env x n2 k (SOME t2)
adamc@158 2091 in
adamc@158 2092 SOME (env, denv)
adamc@158 2093 end
adamc@158 2094 in
adamc@158 2095 (case unifyCons (env, denv) t1 t2 of
adamc@158 2096 [] => good ()
adamc@158 2097 | _ => NONE)
adamc@158 2098 handle CUnify (c1, c2, err) =>
adamc@158 2099 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@158 2100 good ())
adamc@158 2101 end
adamc@158 2102 else
adamc@158 2103 NONE
adamc@158 2104
adamc@158 2105 | _ => NONE)
adamc@156 2106
adamc@32 2107 | L'.SgiVal (x, n2, c2) =>
adamc@32 2108 seek (fn sgi1All as (sgi1, _) =>
adamc@32 2109 case sgi1 of
adamc@41 2110 L'.SgiVal (x', n1, c1) =>
adamc@41 2111 if x = x' then
adamc@86 2112 (case unifyCons (env, denv) c1 c2 of
adamc@88 2113 [] => SOME (env, denv)
adamc@86 2114 | _ => NONE)
adamc@86 2115 handle CUnify (c1, c2, err) =>
adamc@86 2116 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@88 2117 SOME (env, denv))
adamc@41 2118 else
adamc@41 2119 NONE
adamc@32 2120 | _ => NONE)
adamc@32 2121
adamc@33 2122 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 2123 seek (fn sgi1All as (sgi1, _) =>
adamc@33 2124 case sgi1 of
adamc@41 2125 L'.SgiStr (x', n1, sgn1) =>
adamc@41 2126 if x = x' then
adamc@66 2127 let
adamc@86 2128 val () = subSgn (env, denv) sgn1 sgn2
adamc@66 2129 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 2130 val env = if n1 = n2 then
adamc@66 2131 env
adamc@66 2132 else
adamc@66 2133 E.pushStrNamedAs env x n2
adamc@66 2134 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 2135 sgn = sgn2})
adamc@66 2136 in
adamc@88 2137 SOME (env, denv)
adamc@66 2138 end
adamc@41 2139 else
adamc@41 2140 NONE
adamc@33 2141 | _ => NONE)
adamc@59 2142
adamc@59 2143 | L'.SgiSgn (x, n2, sgn2) =>
adamc@59 2144 seek (fn sgi1All as (sgi1, _) =>
adamc@59 2145 case sgi1 of
adamc@59 2146 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 2147 if x = x' then
adamc@65 2148 let
adamc@86 2149 val () = subSgn (env, denv) sgn1 sgn2
adamc@86 2150 val () = subSgn (env, denv) sgn2 sgn1
adamc@65 2151
adamc@65 2152 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 2153 val env = if n1 = n2 then
adamc@65 2154 env
adamc@65 2155 else
adamc@65 2156 E.pushSgnNamedAs env x n1 sgn2
adamc@65 2157 in
adamc@88 2158 SOME (env, denv)
adamc@65 2159 end
adamc@59 2160 else
adamc@59 2161 NONE
adamc@59 2162 | _ => NONE)
adamc@88 2163
adamc@88 2164 | L'.SgiConstraint (c2, d2) =>
adamc@88 2165 seek (fn sgi1All as (sgi1, _) =>
adamc@88 2166 case sgi1 of
adamc@88 2167 L'.SgiConstraint (c1, d1) =>
adamc@88 2168 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
adamc@90 2169 let
adamc@90 2170 val (denv, gs) = D.assert env denv (c2, d2)
adamc@90 2171 in
adamc@90 2172 case gs of
adamc@90 2173 [] => ()
adamc@90 2174 | _ => raise Fail "subSgn: Sub-constraints remain";
adamc@90 2175
adamc@90 2176 SOME (env, denv)
adamc@90 2177 end
adamc@88 2178 else
adamc@88 2179 NONE
adamc@88 2180 | _ => NONE)
adamc@31 2181 end
adamc@31 2182 in
adamc@88 2183 ignore (foldl folder (env, denv) sgis2)
adamc@31 2184 end
adamc@31 2185
adamc@41 2186 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 2187 let
adamc@43 2188 val ran1 =
adamc@43 2189 if n1 = n2 then
adamc@43 2190 ran1
adamc@43 2191 else
adamc@43 2192 subStrInSgn (n1, n2) ran1
adamc@43 2193 in
adamc@86 2194 subSgn (env, denv) dom2 dom1;
adamc@86 2195 subSgn (E.pushStrNamedAs env m2 n2 dom2, denv) ran1 ran2
adamc@43 2196 end
adamc@41 2197
adamc@41 2198 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 2199
adamc@61 2200
adamc@88 2201 fun elabDecl ((d, loc), (env, denv, gs)) =
adamc@76 2202 case d of
adamc@76 2203 L.DCon (x, ko, c) =>
adamc@76 2204 let
adamc@76 2205 val k' = case ko of
adamc@76 2206 NONE => kunif loc
adamc@76 2207 | SOME k => elabKind k
adamc@76 2208
adamc@83 2209 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 2210 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 2211 in
adamc@76 2212 checkKind env c' ck k';
adamc@76 2213
adamc@88 2214 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 2215 end
adamc@156 2216 | L.DDatatype (x, xcs) =>
adamc@156 2217 let
adamc@156 2218 val k = (L'.KType, loc)
adamc@156 2219 val (env, n) = E.pushCNamed env x k NONE
adamc@156 2220 val t = (L'.CNamed n, loc)
adamc@156 2221
adamc@156 2222 val (xcs, (used, env, gs)) =
adamc@156 2223 ListUtil.foldlMap
adamc@156 2224 (fn ((x, to), (used, env, gs)) =>
adamc@156 2225 let
adamc@156 2226 val (to, t, gs') = case to of
adamc@156 2227 NONE => (NONE, t, gs)
adamc@156 2228 | SOME t' =>
adamc@156 2229 let
adamc@156 2230 val (t', tk, gs') = elabCon (env, denv) t'
adamc@156 2231 in
adamc@156 2232 checkKind env t' tk k;
adamc@156 2233 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@156 2234 end
adamc@156 2235
adamc@156 2236 val (env, n') = E.pushENamed env x t
adamc@156 2237 in
adamc@156 2238 if SS.member (used, x) then
adamc@156 2239 strError env (DuplicateConstructor (x, loc))
adamc@156 2240 else
adamc@156 2241 ();
adamc@156 2242 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@156 2243 end)
adamc@156 2244 (SS.empty, env, []) xcs
adamc@171 2245
adamc@171 2246 val env = E.pushDatatype env n xcs
adamc@156 2247 in
adamc@156 2248 ([(L'.DDatatype (x, n, xcs), loc)], (env, denv, gs))
adamc@156 2249 end
adamc@156 2250
adamc@157 2251 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
adamc@157 2252
adamc@157 2253 | L.DDatatypeImp (x, m1 :: ms, s) =>
adamc@157 2254 (case E.lookupStr env m1 of
adamc@157 2255 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@157 2256 ([], (env, denv, gs)))
adamc@157 2257 | SOME (n, sgn) =>
adamc@157 2258 let
adamc@157 2259 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@157 2260 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@157 2261 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@157 2262 (strerror, sgnerror))
adamc@157 2263 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@157 2264 ((L'.StrVar n, loc), sgn) ms
adamc@157 2265 in
adamc@158 2266 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@158 2267 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@158 2268 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@158 2269 NONE => (conError env (UnboundDatatype (loc, s));
adamc@158 2270 ([], (env, denv, gs)))
adamc@158 2271 | SOME xncs =>
adamc@158 2272 let
adamc@158 2273 val k = (L'.KType, loc)
adamc@158 2274 val t = (L'.CModProj (n, ms, s), loc)
adamc@158 2275 val (env, n') = E.pushCNamed env x k (SOME t)
adamc@158 2276 val env = E.pushDatatype env n' xncs
adamc@157 2277
adamc@158 2278 val t = (L'.CNamed n', loc)
adamc@158 2279 val env = foldl (fn ((x, n, to), env) =>
adamc@158 2280 let
adamc@158 2281 val t = case to of
adamc@158 2282 NONE => t
adamc@158 2283 | SOME t' => (L'.TFun (t', t), loc)
adamc@158 2284 in
adamc@158 2285 E.pushENamedAs env x n t
adamc@158 2286 end) env xncs
adamc@158 2287 in
adamc@162 2288 ([(L'.DDatatypeImp (x, n', n, ms, s, xncs), loc)], (env, denv, gs))
adamc@158 2289 end)
adamc@158 2290 | _ => (strError env (NotDatatype loc);
adamc@158 2291 ([], (env, denv, [])))
adamc@157 2292 end)
adamc@157 2293
adamc@76 2294 | L.DVal (x, co, e) =>
adamc@76 2295 let
adamc@123 2296 val (c', _, gs1) = case co of
adamc@83 2297 NONE => (cunif (loc, ktype), ktype, [])
adamc@83 2298 | SOME c => elabCon (env, denv) c
adamc@76 2299
adamc@83 2300 val (e', et, gs2) = elabExp (env, denv) e
adamc@76 2301 val (env', n) = E.pushENamed env x c'
adamc@86 2302
adamc@86 2303 val gs3 = checkCon (env, denv) e' et c'
adamc@76 2304 in
adamc@88 2305 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, gs1 @ gs2 @ gs3 @ gs))
adamc@76 2306 end
adamc@123 2307 | L.DValRec vis =>
adamc@123 2308 let
adamc@123 2309 val (vis, gs) = ListUtil.foldlMap
adamc@123 2310 (fn ((x, co, e), gs) =>
adamc@123 2311 let
adamc@123 2312 val (c', _, gs1) = case co of
adamc@123 2313 NONE => (cunif (loc, ktype), ktype, [])
adamc@123 2314 | SOME c => elabCon (env, denv) c
adamc@123 2315 in
adamc@123 2316 ((x, c', e), gs1 @ gs)
adamc@123 2317 end) [] vis
adamc@123 2318
adamc@123 2319 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@123 2320 let
adamc@123 2321 val (env, n) = E.pushENamed env x c'
adamc@123 2322 in
adamc@123 2323 ((x, n, c', e), env)
adamc@123 2324 end) env vis
adamc@123 2325
adamc@123 2326 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@123 2327 let
adamc@123 2328 val (e', et, gs1) = elabExp (env, denv) e
adamc@123 2329
adamc@123 2330 val gs2 = checkCon (env, denv) e' et c'
adamc@123 2331 in
adamc@123 2332 ((x, n, c', e'), gs1 @ gs2 @ gs)
adamc@123 2333 end) gs vis
adamc@123 2334 in
adamc@123 2335 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@123 2336 end
adamc@76 2337
adamc@76 2338 | L.DSgn (x, sgn) =>
adamc@76 2339 let
adamc@83 2340 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2341 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 2342 in
adamc@88 2343 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2344 end
adamc@76 2345
adamc@76 2346 | L.DStr (x, sgno, str) =>
adamc@76 2347 let
adamc@93 2348 val () = if x = "Basis" then
adamc@93 2349 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@93 2350 else
adamc@93 2351 ()
adamc@93 2352
adamc@83 2353 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@76 2354
adamc@83 2355 val (str', sgn', gs') =
adamc@78 2356 case formal of
adamc@78 2357 NONE =>
adamc@78 2358 let
adamc@83 2359 val (str', actual, ds) = elabStr (env, denv) str
adamc@78 2360 in
adamc@83 2361 (str', selfifyAt env {str = str', sgn = actual}, ds)
adamc@78 2362 end
adamc@83 2363 | SOME (formal, gs1) =>
adamc@78 2364 let
adamc@78 2365 val str =
adamc@78 2366 case #1 (hnormSgn env formal) of
adamc@78 2367 L'.SgnConst sgis =>
adamc@78 2368 (case #1 str of
adamc@78 2369 L.StrConst ds =>
adamc@78 2370 let
adamc@78 2371 val needed = foldl (fn ((sgi, _), needed) =>
adamc@78 2372 case sgi of
adamc@78 2373 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
adamc@78 2374 | _ => needed)
adamc@78 2375 SS.empty sgis
adamc@78 2376
adamc@78 2377 val needed = foldl (fn ((d, _), needed) =>
adamc@78 2378 case d of
adamc@78 2379 L.DCon (x, _, _) => (SS.delete (needed, x)
adamc@78 2380 handle NotFound => needed)
adamc@78 2381 | L.DOpen _ => SS.empty
adamc@78 2382 | _ => needed)
adamc@78 2383 needed ds
adamc@78 2384 in
adamc@78 2385 case SS.listItems needed of
adamc@78 2386 [] => str
adamc@78 2387 | xs =>
adamc@78 2388 let
adamc@78 2389 val kwild = (L.KWild, #2 str)
adamc@78 2390 val cwild = (L.CWild kwild, #2 str)
adamc@78 2391 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
adamc@78 2392 in
adamc@78 2393 (L.StrConst (ds @ ds'), #2 str)
adamc@78 2394 end
adamc@78 2395 end
adamc@78 2396 | _ => str)
adamc@78 2397 | _ => str
adamc@78 2398
adamc@83 2399 val (str', actual, gs2) = elabStr (env, denv) str
adamc@78 2400 in
adamc@158 2401 subSgn (env, denv) (selfifyAt env {str = str', sgn = actual}) formal;
adamc@83 2402 (str', formal, gs1 @ gs2)
adamc@78 2403 end
adamc@76 2404
adamc@76 2405 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2406 in
adamc@76 2407 case #1 (hnormSgn env sgn') of
adamc@76 2408 L'.SgnFun _ =>
adamc@76 2409 (case #1 str' of
adamc@76 2410 L'.StrFun _ => ()
adamc@76 2411 | _ => strError env (FunctorRebind loc))
adamc@76 2412 | _ => ();
adamc@76 2413
adamc@88 2414 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
adamc@76 2415 end
adamc@76 2416
adamc@76 2417 | L.DFfiStr (x, sgn) =>
adamc@76 2418 let
adamc@83 2419 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2420
adamc@76 2421 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2422 in
adamc@88 2423 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2424 end
adamc@76 2425
adamc@76 2426 | L.DOpen (m, ms) =>
adamc@88 2427 (case E.lookupStr env m of
adamc@88 2428 NONE => (strError env (UnboundStr (loc, m));
adamc@88 2429 ([], (env, denv, [])))
adamc@88 2430 | SOME (n, sgn) =>
adamc@88 2431 let
adamc@88 2432 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 2433 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 2434 NONE => (strError env (UnboundStr (loc, m));
adamc@88 2435 (strerror, sgnerror))
adamc@88 2436 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 2437 ((L'.StrVar n, loc), sgn) ms
adamc@83 2438
adamc@88 2439 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
adamc@88 2440 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
adamc@88 2441 in
adamc@88 2442 (ds, (env', denv', []))
adamc@88 2443 end)
adamc@88 2444
adamc@88 2445 | L.DConstraint (c1, c2) =>
adamc@88 2446 let
adamc@88 2447 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 2448 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@90 2449 val gs3 = D.prove env denv (c1', c2', loc)
adamc@88 2450
adamc@90 2451 val (denv', gs4) = D.assert env denv (c1', c2')
adamc@88 2452 in
adamc@88 2453 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 2454 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 2455
adamc@90 2456 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', gs1 @ gs2 @ gs3 @ gs4))
adamc@88 2457 end
adamc@88 2458
adamc@88 2459 | L.DOpenConstraints (m, ms) =>
adamc@88 2460 let
adamc@88 2461 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@88 2462 in
adamc@88 2463 ([], (env, denv, []))
adamc@88 2464 end
adamc@3 2465
adamc@109 2466 | L.DExport str =>
adamc@100 2467 let
adamc@109 2468 val (str', sgn, gs) = elabStr (env, denv) str
adamc@100 2469
adamc@109 2470 val sgn =
adamc@109 2471 case #1 (hnormSgn env sgn) of
adamc@109 2472 L'.SgnConst sgis =>
adamc@109 2473 let
adamc@146 2474 fun doOne (all as (sgi, _), env) =
adamc@146 2475 (case sgi of
adamc@146 2476 L'.SgiVal (x, n, t) =>
adamc@146 2477 (case hnormCon (env, denv) t of
adamc@146 2478 ((L'.TFun (dom, ran), _), []) =>
adamc@146 2479 (case (hnormCon (env, denv) dom, hnormCon (env, denv) ran) of
adamc@146 2480 (((L'.TRecord domR, _), []),
adamc@146 2481 ((L'.CApp (tf, arg3), _), [])) =>
adamc@146 2482 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
adamc@146 2483 (((L'.CApp (tf, arg2), _), []),
adamc@146 2484 (((L'.CRecord (_, []), _), []))) =>
adamc@146 2485 (case (hnormCon (env, denv) tf) of
adamc@146 2486 ((L'.CApp (tf, arg1), _), []) =>
adamc@146 2487 (case (hnormCon (env, denv) tf,
adamc@146 2488 hnormCon (env, denv) domR,
adamc@152 2489 hnormCon (env, denv) arg1,
adamc@146 2490 hnormCon (env, denv) arg2) of
adamc@152 2491 ((tf, []), (domR, []), (arg1, []),
adamc@146 2492 ((L'.CRecord (_, []), _), [])) =>
adamc@146 2493 let
adamc@146 2494 val t = (L'.CApp (tf, arg1), loc)
adamc@146 2495 val t = (L'.CApp (t, arg2), loc)
adamc@146 2496 val t = (L'.CApp (t, arg3), loc)
adamc@146 2497 in
adamc@146 2498 (L'.SgiVal (x, n, (L'.TFun ((L'.TRecord domR, loc),
adamc@146 2499 t),
adamc@146 2500 loc)), loc)
adamc@146 2501 end
adamc@146 2502 | _ => all)
adamc@146 2503 | _ => all)
adamc@146 2504 | _ => all)
adamc@146 2505 | _ => all)
adamc@146 2506 | _ => all)
adamc@146 2507 | _ => all,
adamc@146 2508 E.sgiBinds env all)
adamc@109 2509 in
adamc@146 2510 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@109 2511 end
adamc@109 2512 | _ => sgn
adamc@100 2513 in
adamc@109 2514 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs))
adamc@100 2515 end
adamc@100 2516
adamc@83 2517 and elabStr (env, denv) (str, loc) =
adamc@31 2518 case str of
adamc@31 2519 L.StrConst ds =>
adamc@31 2520 let
adamc@88 2521 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 2522 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 2523
adamc@63 2524 val (sgis, _, _, _, _) =
adamc@64 2525 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 2526 case sgi of
adamc@64 2527 L'.SgiConAbs (x, n, k) =>
adamc@64 2528 let
adamc@64 2529 val (cons, x) =
adamc@64 2530 if SS.member (cons, x) then
adamc@64 2531 (cons, "?" ^ x)
adamc@64 2532 else
adamc@64 2533 (SS.add (cons, x), x)
adamc@64 2534 in
adamc@64 2535 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2536 end
adamc@64 2537 | L'.SgiCon (x, n, k, c) =>
adamc@64 2538 let
adamc@64 2539 val (cons, x) =
adamc@64 2540 if SS.member (cons, x) then
adamc@64 2541 (cons, "?" ^ x)
adamc@64 2542 else
adamc@64 2543 (SS.add (cons, x), x)
adamc@64 2544 in
adamc@64 2545 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2546 end
adamc@156 2547 | L'.SgiDatatype (x, n, xncs) =>
adamc@156 2548 let
adamc@156 2549 val (cons, x) =
adamc@156 2550 if SS.member (cons, x) then
adamc@156 2551 (cons, "?" ^ x)
adamc@156 2552 else
adamc@156 2553 (SS.add (cons, x), x)
adamc@156 2554
adamc@156 2555 val (xncs, vals) =
adamc@156 2556 ListUtil.foldlMap
adamc@156 2557 (fn ((x, n, t), vals) =>
adamc@156 2558 if SS.member (vals, x) then
adamc@156 2559 (("?" ^ x, n, t), vals)
adamc@156 2560 else
adamc@156 2561 ((x, n, t), SS.add (vals, x)))
adamc@156 2562 vals xncs
adamc@156 2563 in
adamc@156 2564 ((L'.SgiDatatype (x, n, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 2565 end
adamc@162 2566 | L'.SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
adamc@156 2567 let
adamc@156 2568 val (cons, x) =
adamc@156 2569 if SS.member (cons, x) then
adamc@156 2570 (cons, "?" ^ x)
adamc@156 2571 else
adamc@156 2572 (SS.add (cons, x), x)
adamc@156 2573 in
adamc@162 2574 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 2575 end
adamc@64 2576 | L'.SgiVal (x, n, c) =>
adamc@64 2577 let
adamc@64 2578 val (vals, x) =
adamc@64 2579 if SS.member (vals, x) then
adamc@64 2580 (vals, "?" ^ x)
adamc@64 2581 else
adamc@64 2582 (SS.add (vals, x), x)
adamc@64 2583 in
adamc@64 2584 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2585 end
adamc@64 2586 | L'.SgiSgn (x, n, sgn) =>
adamc@64 2587 let
adamc@64 2588 val (sgns, x) =
adamc@64 2589 if SS.member (sgns, x) then
adamc@64 2590 (sgns, "?" ^ x)
adamc@64 2591 else
adamc@64 2592 (SS.add (sgns, x), x)
adamc@64 2593 in
adamc@64 2594 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2595 end
adamc@64 2596
adamc@64 2597 | L'.SgiStr (x, n, sgn) =>
adamc@64 2598 let
adamc@64 2599 val (strs, x) =
adamc@64 2600 if SS.member (strs, x) then
adamc@64 2601 (strs, "?" ^ x)
adamc@64 2602 else
adamc@64 2603 (SS.add (strs, x), x)
adamc@64 2604 in
adamc@64 2605 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 2606 end
adamc@88 2607 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs))
adamc@64 2608
adamc@63 2609 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 2610 in
adamc@83 2611 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 2612 end
adamc@31 2613 | L.StrVar x =>
adamc@31 2614 (case E.lookupStr env x of
adamc@31 2615 NONE =>
adamc@31 2616 (strError env (UnboundStr (loc, x));
adamc@83 2617 (strerror, sgnerror, []))
adamc@83 2618 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 2619 | L.StrProj (str, x) =>
adamc@34 2620 let
adamc@83 2621 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 2622 in
adamc@34 2623 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 2624 NONE => (strError env (UnboundStr (loc, x));
adamc@83 2625 (strerror, sgnerror, []))
adamc@83 2626 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 2627 end
adamc@41 2628 | L.StrFun (m, dom, ranO, str) =>
adamc@41 2629 let
adamc@83 2630 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2631 val (env', n) = E.pushStrNamed env m dom'
adamc@83 2632 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 2633
adamc@83 2634 val (formal, gs3) =
adamc@41 2635 case ranO of
adamc@83 2636 NONE => (actual, [])
adamc@41 2637 | SOME ran =>
adamc@41 2638 let
adamc@83 2639 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 2640 in
adamc@86 2641 subSgn (env', denv) actual ran';
adamc@83 2642 (ran', gs)
adamc@41 2643 end
adamc@41 2644 in
adamc@41 2645 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 2646 (L'.SgnFun (m, n, dom', formal), loc),
adamc@83 2647 gs1 @ gs2 @ gs3)
adamc@41 2648 end
adamc@44 2649 | L.StrApp (str1, str2) =>
adamc@44 2650 let
adamc@83 2651 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@83 2652 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 2653 in
adamc@44 2654 case #1 (hnormSgn env sgn1) of
adamc@83 2655 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2656 | L'.SgnFun (m, n, dom, ran) =>
adamc@86 2657 (subSgn (env, denv) sgn2 dom;
adamc@44 2658 case #1 (hnormSgn env ran) of
adamc@83 2659 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2660 | L'.SgnConst sgis =>
adamc@44 2661 ((L'.StrApp (str1', str2'), loc),
adamc@83 2662 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 2663 gs1 @ gs2)
adamc@44 2664 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 2665 | _ => (strError env (NotFunctor sgn1);
adamc@83 2666 (strerror, sgnerror, []))
adamc@44 2667 end
adamc@31 2668
adamc@56 2669 fun elabFile basis env file =
adamc@56 2670 let
adamc@83 2671 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 2672 val () = case gs of
adamc@83 2673 [] => ()
adamc@83 2674 | _ => raise Fail "Unresolved disjointness constraints in Basis"
adamc@83 2675
adamc@56 2676 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@56 2677
adamc@88 2678 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
adamc@56 2679
adamc@56 2680 fun discoverC r x =
adamc@56 2681 case E.lookupC env' x of
adamc@56 2682 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 2683 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 2684 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 2685
adamc@56 2686 val () = discoverC int "int"
adamc@56 2687 val () = discoverC float "float"
adamc@56 2688 val () = discoverC string "string"
adamc@56 2689
adamc@83 2690 fun elabDecl' (d, (env, gs)) =
adamc@76 2691 let
adamc@76 2692 val () = resetKunif ()
adamc@76 2693 val () = resetCunif ()
adamc@88 2694 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 2695 in
adamc@76 2696 if ErrorMsg.anyErrors () then
adamc@76 2697 ()
adamc@76 2698 else (
adamc@76 2699 if List.exists kunifsInDecl ds then
adamc@91 2700 declError env (KunifsRemain ds)
adamc@76 2701 else
adamc@76 2702 ();
adamc@76 2703
adamc@76 2704 case ListUtil.search cunifsInDecl ds of
adamc@76 2705 NONE => ()
adamc@76 2706 | SOME loc =>
adamc@91 2707 declError env (CunifsRemain ds)
adamc@76 2708 );
adamc@76 2709
adamc@83 2710 (ds, (env, gs))
adamc@76 2711 end
adamc@76 2712
adamc@83 2713 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 2714 in
adamc@84 2715 if ErrorMsg.anyErrors () then
adamc@84 2716 ()
adamc@84 2717 else
adamc@90 2718 app (fn (loc, env, denv, c1, c2) =>
adamc@84 2719 case D.prove env denv (c1, c2, loc) of
adamc@84 2720 [] => ()
adamc@84 2721 | _ =>
adamc@86 2722 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adamc@84 2723 eprefaces' [("Con 1", p_con env c1),
adamc@90 2724 ("Con 2", p_con env c2),
adamc@90 2725 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
adamc@90 2726 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))])) gs;
adamc@83 2727
adamc@56 2728 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
adamc@56 2729 end
adamc@2 2730
adamc@2 2731 end