annotate src/elaborate.sml @ 182:d11754ffe252

Compiled pattern matching to C
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 12:43:20 -0400
parents 33d4a8eea484
children 8e9f97508f0d
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@171 936 fun pcon (pc, po, to, dn) =
adamc@171 937
adamc@171 938 case (po, to) of
adamc@171 939 (NONE, SOME _) => (expError env (PatHasNoArg loc);
adamc@171 940 rerror)
adamc@171 941 | (SOME _, NONE) => (expError env (PatHasArg loc);
adamc@171 942 rerror)
adamc@174 943 | (NONE, NONE) => (((L'.PCon (pc, NONE), loc), dn),
adamc@171 944 (env, bound))
adamc@171 945 | (SOME p, SOME t) =>
adamc@171 946 let
adamc@174 947 val ((p', pt), (env, bound)) = elabPat (p, (env, denv, bound))
adamc@171 948 in
adamc@174 949 (((L'.PCon (pc, SOME p'), loc), dn),
adamc@171 950 (env, bound))
adamc@171 951 end
adamc@171 952 in
adamc@171 953 case p of
adamc@171 954 L.PWild => (((L'.PWild, loc), cunif (loc, (L'.KType, loc))),
adamc@171 955 (env, bound))
adamc@171 956 | L.PVar x =>
adamc@171 957 let
adamc@171 958 val t = if SS.member (bound, x) then
adamc@171 959 (expError env (DuplicatePatternVariable (loc, x));
adamc@171 960 terror)
adamc@171 961 else
adamc@171 962 cunif (loc, (L'.KType, loc))
adamc@171 963 in
adamc@182 964 (((L'.PVar (x, t), loc), t),
adamc@171 965 (E.pushERel env x t, SS.add (bound, x)))
adamc@171 966 end
adamc@173 967 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
adamc@173 968 (env, bound))
adamc@171 969 | L.PCon ([], x, po) =>
adamc@171 970 (case E.lookupConstructor env x of
adamc@174 971 NONE => (expError env (UnboundConstructor (loc, [], x));
adamc@171 972 rerror)
adamc@174 973 | SOME (n, to, dn) => pcon (L'.PConVar n, po, to, (L'.CNamed dn, loc)))
adamc@174 974 | L.PCon (m1 :: ms, x, po) =>
adamc@174 975 (case E.lookupStr env m1 of
adamc@174 976 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@174 977 rerror)
adamc@174 978 | SOME (n, sgn) =>
adamc@174 979 let
adamc@174 980 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@174 981 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@182 982 NONE => raise Fail "elabPat: Unknown substructure"
adamc@174 983 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@174 984 ((L'.StrVar n, loc), sgn) ms
adamc@174 985 in
adamc@174 986 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 987 NONE => (expError env (UnboundConstructor (loc, m1 :: ms, x));
adamc@174 988 rerror)
adamc@174 989 | SOME (_, to, dn) => pcon (L'.PConProj (n, ms, x), po, to, dn)
adamc@174 990 end)
adamc@174 991
adamc@175 992 | L.PRecord (xps, flex) =>
adamc@175 993 let
adamc@175 994 val (xpts, (env, bound, _)) =
adamc@175 995 ListUtil.foldlMap (fn ((x, p), (env, bound, fbound)) =>
adamc@175 996 let
adamc@175 997 val ((p', t), (env, bound)) = elabPat (p, (env, denv, bound))
adamc@175 998 in
adamc@175 999 if SS.member (fbound, x) then
adamc@175 1000 expError env (DuplicatePatField (loc, x))
adamc@175 1001 else
adamc@175 1002 ();
adamc@175 1003 ((x, p', t), (env, bound, SS.add (fbound, x)))
adamc@175 1004 end)
adamc@175 1005 (env, bound, SS.empty) xps
adamc@175 1006
adamc@175 1007 val k = (L'.KType, loc)
adamc@175 1008 val c = (L'.CRecord (k, map (fn (x, _, t) => ((L'.CName x, loc), t)) xpts), loc)
adamc@176 1009 val c =
adamc@175 1010 if flex then
adamc@176 1011 (L'.CConcat (c, cunif (loc, (L'.KRecord k, loc))), loc)
adamc@175 1012 else
adamc@176 1013 c
adamc@175 1014 in
adamc@182 1015 (((L'.PRecord xpts, loc),
adamc@175 1016 (L'.TRecord c, loc)),
adamc@175 1017 (env, bound))
adamc@175 1018 end
adamc@175 1019
adamc@171 1020 end
adamc@171 1021
adamc@172 1022 datatype coverage =
adamc@172 1023 Wild
adamc@173 1024 | None
adamc@172 1025 | Datatype of coverage IM.map
adamc@175 1026 | Record of coverage SM.map list
adamc@172 1027
adamc@172 1028 fun exhaustive (env, denv, t, ps) =
adamc@172 1029 let
adamc@172 1030 fun pcCoverage pc =
adamc@172 1031 case pc of
adamc@172 1032 L'.PConVar n => n
adamc@174 1033 | L'.PConProj (m1, ms, x) =>
adamc@174 1034 let
adamc@174 1035 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@174 1036 in
adamc@174 1037 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 1038 NONE => raise Fail "exhaustive: Can't project constructor"
adamc@174 1039 | SOME (n, _, _) => n
adamc@174 1040 end
adamc@172 1041
adamc@172 1042 fun coverage (p, _) =
adamc@172 1043 case p of
adamc@172 1044 L'.PWild => Wild
adamc@172 1045 | L'.PVar _ => Wild
adamc@173 1046 | L'.PPrim _ => None
adamc@172 1047 | L'.PCon (pc, NONE) => Datatype (IM.insert (IM.empty, pcCoverage pc, Wild))
adamc@172 1048 | L'.PCon (pc, SOME p) => Datatype (IM.insert (IM.empty, pcCoverage pc, coverage p))
adamc@182 1049 | L'.PRecord xps => Record [foldl (fn ((x, p, _), fmap) =>
adamc@182 1050 SM.insert (fmap, x, coverage p)) SM.empty xps]
adamc@176 1051
adamc@172 1052 fun merge (c1, c2) =
adamc@172 1053 case (c1, c2) of
adamc@173 1054 (None, _) => c2
adamc@173 1055 | (_, None) => c1
adamc@173 1056
adamc@173 1057 | (Wild, _) => Wild
adamc@172 1058 | (_, Wild) => Wild
adamc@172 1059
adamc@172 1060 | (Datatype cm1, Datatype cm2) => Datatype (IM.unionWith merge (cm1, cm2))
adamc@172 1061
adamc@175 1062 | (Record fm1, Record fm2) => Record (fm1 @ fm2)
adamc@175 1063
adamc@175 1064 | _ => None
adamc@175 1065
adamc@172 1066 fun combinedCoverage ps =
adamc@172 1067 case ps of
adamc@172 1068 [] => raise Fail "Empty pattern list for coverage checking"
adamc@172 1069 | [p] => coverage p
adamc@172 1070 | p :: ps => merge (coverage p, combinedCoverage ps)
adamc@172 1071
adamc@175 1072 fun enumerateCases t =
adamc@175 1073 let
adamc@175 1074 fun dtype cons =
adamc@175 1075 ListUtil.mapConcat (fn (_, n, to) =>
adamc@175 1076 case to of
adamc@175 1077 NONE => [Datatype (IM.insert (IM.empty, n, Wild))]
adamc@175 1078 | SOME t => map (fn c => Datatype (IM.insert (IM.empty, n, c)))
adamc@175 1079 (enumerateCases t)) cons
adamc@175 1080 in
adamc@175 1081 case #1 (#1 (hnormCon (env, denv) t)) of
adamc@175 1082 L'.CNamed n =>
adamc@175 1083 (let
adamc@175 1084 val dt = E.lookupDatatype env n
adamc@175 1085 val cons = E.constructors dt
adamc@175 1086 in
adamc@175 1087 dtype cons
adamc@175 1088 end handle E.UnboundNamed _ => [Wild])
adamc@175 1089 | L'.TRecord c =>
adamc@175 1090 (case #1 (#1 (hnormCon (env, denv) c)) of
adamc@175 1091 L'.CRecord (_, xts) =>
adamc@175 1092 let
adamc@175 1093 val xts = map (fn (x, t) => (#1 (hnormCon (env, denv) x), t)) xts
adamc@175 1094
adamc@175 1095 fun exponentiate fs =
adamc@175 1096 case fs of
adamc@175 1097 [] => [SM.empty]
adamc@175 1098 | ((L'.CName x, _), t) :: rest =>
adamc@175 1099 let
adamc@175 1100 val this = enumerateCases t
adamc@175 1101 val rest = exponentiate rest
adamc@175 1102 in
adamc@175 1103 ListUtil.mapConcat (fn fmap =>
adamc@175 1104 map (fn c => SM.insert (fmap, x, c)) this) rest
adamc@175 1105 end
adamc@175 1106 | _ => raise Fail "exponentiate: Not CName"
adamc@175 1107 in
adamc@175 1108 if List.exists (fn ((L'.CName _, _), _) => false
adamc@175 1109 | (c, _) => true) xts then
adamc@175 1110 [Wild]
adamc@175 1111 else
adamc@175 1112 map (fn ls => Record [ls]) (exponentiate xts)
adamc@175 1113 end
adamc@175 1114 | _ => [Wild])
adamc@175 1115 | _ => [Wild]
adamc@175 1116 end
adamc@175 1117
adamc@175 1118 fun coverageImp (c1, c2) =
adamc@175 1119 case (c1, c2) of
adamc@175 1120 (Wild, _) => true
adamc@175 1121
adamc@175 1122 | (Datatype cmap1, Datatype cmap2) =>
adamc@175 1123 List.all (fn (n, c2) =>
adamc@175 1124 case IM.find (cmap1, n) of
adamc@175 1125 NONE => false
adamc@175 1126 | SOME c1 => coverageImp (c1, c2)) (IM.listItemsi cmap2)
adamc@175 1127
adamc@175 1128 | (Record fmaps1, Record fmaps2) =>
adamc@175 1129 List.all (fn fmap2 =>
adamc@175 1130 List.exists (fn fmap1 =>
adamc@175 1131 List.all (fn (x, c2) =>
adamc@175 1132 case SM.find (fmap1, x) of
adamc@175 1133 NONE => true
adamc@175 1134 | SOME c1 => coverageImp (c1, c2))
adamc@175 1135 (SM.listItemsi fmap2))
adamc@175 1136 fmaps1) fmaps2
adamc@175 1137
adamc@175 1138 | _ => false
adamc@175 1139
adamc@172 1140 fun isTotal (c, t) =
adamc@172 1141 case c of
adamc@173 1142 None => (false, [])
adamc@173 1143 | Wild => (true, [])
adamc@172 1144 | Datatype cm =>
adamc@172 1145 let
adamc@172 1146 val ((t, _), gs) = hnormCon (env, denv) t
adamc@174 1147
adamc@174 1148 fun dtype cons =
adamc@174 1149 foldl (fn ((_, n, to), (total, gs)) =>
adamc@174 1150 case IM.find (cm, n) of
adamc@174 1151 NONE => (false, gs)
adamc@174 1152 | SOME c' =>
adamc@174 1153 case to of
adamc@174 1154 NONE => (total, gs)
adamc@174 1155 | SOME t' =>
adamc@174 1156 let
adamc@174 1157 val (total, gs') = isTotal (c', t')
adamc@174 1158 in
adamc@174 1159 (total, gs' @ gs)
adamc@174 1160 end)
adamc@174 1161 (true, gs) cons
adamc@172 1162 in
adamc@172 1163 case t of
adamc@172 1164 L'.CNamed n =>
adamc@172 1165 let
adamc@172 1166 val dt = E.lookupDatatype env n
adamc@172 1167 val cons = E.constructors dt
adamc@172 1168 in
adamc@174 1169 dtype cons
adamc@174 1170 end
adamc@174 1171 | L'.CModProj (m1, ms, x) =>
adamc@174 1172 let
adamc@174 1173 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@174 1174 in
adamc@174 1175 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
adamc@174 1176 NONE => raise Fail "isTotal: Can't project datatype"
adamc@174 1177 | SOME cons => dtype cons
adamc@172 1178 end
adamc@172 1179 | L'.CError => (true, gs)
adamc@172 1180 | _ => raise Fail "isTotal: Not a datatype"
adamc@172 1181 end
adamc@175 1182 | Record _ => (List.all (fn c2 => coverageImp (c, c2)) (enumerateCases t), [])
adamc@172 1183 in
adamc@172 1184 isTotal (combinedCoverage ps, t)
adamc@172 1185 end
adamc@172 1186
adamc@92 1187 fun elabExp (env, denv) (eAll as (e, loc)) =
adamc@91 1188 let
adamc@92 1189
adamc@92 1190 in
adamc@92 1191 (*eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*)
adamc@10 1192
adamc@91 1193 case e of
adamc@91 1194 L.EAnnot (e, t) =>
adamc@91 1195 let
adamc@91 1196 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1197 val (t', _, gs2) = elabCon (env, denv) t
adamc@91 1198 val gs3 = checkCon (env, denv) e' et t'
adamc@91 1199 in
adamc@91 1200 (e', t', gs1 @ gs2 @ gs3)
adamc@91 1201 end
adamc@34 1202
adamc@91 1203 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@91 1204 | L.EVar ([], s) =>
adamc@91 1205 (case E.lookupE env s of
adamc@91 1206 E.NotBound =>
adamc@91 1207 (expError env (UnboundExp (loc, s));
adamc@91 1208 (eerror, cerror, []))
adamc@91 1209 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
adamc@91 1210 | E.Named (n, t) => ((L'.ENamed n, loc), t, []))
adamc@91 1211 | L.EVar (m1 :: ms, s) =>
adamc@91 1212 (case E.lookupStr env m1 of
adamc@91 1213 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@91 1214 (eerror, cerror, []))
adamc@91 1215 | SOME (n, sgn) =>
adamc@91 1216 let
adamc@91 1217 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@91 1218 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@91 1219 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@91 1220 (strerror, sgnerror))
adamc@91 1221 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@91 1222 ((L'.StrVar n, loc), sgn) ms
adamc@10 1223
adamc@91 1224 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@91 1225 NONE => (expError env (UnboundExp (loc, s));
adamc@91 1226 cerror)
adamc@91 1227 | SOME t => t
adamc@91 1228 in
adamc@91 1229 ((L'.EModProj (n, ms, s), loc), t, [])
adamc@91 1230 end)
adamc@86 1231
adamc@91 1232 | L.EApp (e1, e2) =>
adamc@91 1233 let
adamc@91 1234 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@91 1235 val (e1', t1, gs2) = elabHead (env, denv) e1' t1
adamc@91 1236 val (e2', t2, gs3) = elabExp (env, denv) e2
adamc@71 1237
adamc@91 1238 val dom = cunif (loc, ktype)
adamc@91 1239 val ran = cunif (loc, ktype)
adamc@91 1240 val t = (L'.TFun (dom, ran), dummy)
adamc@91 1241
adamc@91 1242 val gs4 = checkCon (env, denv) e1' t1 t
adamc@91 1243 val gs5 = checkCon (env, denv) e2' t2 dom
adamc@91 1244 in
adamc@91 1245 ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2 @ gs3 @ gs4 @ gs5)
adamc@91 1246 end
adamc@91 1247 | L.EAbs (x, to, e) =>
adamc@91 1248 let
adamc@91 1249 val (t', gs1) = case to of
adamc@91 1250 NONE => (cunif (loc, ktype), [])
adamc@91 1251 | SOME t =>
adamc@91 1252 let
adamc@91 1253 val (t', tk, gs) = elabCon (env, denv) t
adamc@91 1254 in
adamc@91 1255 checkKind env t' tk ktype;
adamc@91 1256 (t', gs)
adamc@91 1257 end
adamc@91 1258 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
adamc@91 1259 in
adamc@91 1260 ((L'.EAbs (x, t', et, e'), loc),
adamc@91 1261 (L'.TFun (t', et), loc),
adamc@91 1262 gs1 @ gs2)
adamc@91 1263 end
adamc@91 1264 | L.ECApp (e, c) =>
adamc@91 1265 let
adamc@91 1266 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1267 val (e', et, gs2) = elabHead (env, denv) e' et
adamc@91 1268 val (c', ck, gs3) = elabCon (env, denv) c
adamc@91 1269 val ((et', _), gs4) = hnormCon (env, denv) et
adamc@91 1270 in
adamc@91 1271 case et' of
adamc@91 1272 L'.CError => (eerror, cerror, [])
adamc@91 1273 | L'.TCFun (_, _, k, eb) =>
adamc@91 1274 let
adamc@91 1275 val () = checkKind env c' ck k
adamc@91 1276 val eb' = subConInCon (0, c') eb
adamc@91 1277 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@91 1278 cerror)
adamc@91 1279 in
adamc@91 1280 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1281 end
adamc@91 1282
adamc@91 1283 | L'.CUnif _ =>
adamc@91 1284 (expError env (Unif ("application", et));
adamc@91 1285 (eerror, cerror, []))
adamc@91 1286
adamc@91 1287 | _ =>
adamc@91 1288 (expError env (WrongForm ("constructor function", e', et));
adamc@91 1289 (eerror, cerror, []))
adamc@91 1290 end
adamc@91 1291 | L.ECAbs (expl, x, k, e) =>
adamc@91 1292 let
adamc@91 1293 val expl' = elabExplicitness expl
adamc@91 1294 val k' = elabKind k
adamc@91 1295 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
adamc@91 1296 in
adamc@91 1297 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@91 1298 (L'.TCFun (expl', x, k', et), loc),
adamc@91 1299 gs)
adamc@91 1300 end
adamc@91 1301
adamc@91 1302 | L.EDisjoint (c1, c2, e) =>
adamc@91 1303 let
adamc@91 1304 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@91 1305 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@91 1306
adamc@91 1307 val ku1 = kunif loc
adamc@91 1308 val ku2 = kunif loc
adamc@91 1309
adamc@91 1310 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@91 1311 val (e', t, gs4) = elabExp (env, denv') e
adamc@91 1312 in
adamc@91 1313 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@91 1314 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@91 1315
adamc@91 1316 (e', (L'.TDisjoint (c1', c2', t), loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1317 end
adamc@91 1318
adamc@91 1319 | L.ERecord xes =>
adamc@91 1320 let
adamc@91 1321 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@91 1322 let
adamc@91 1323 val (x', xk, gs1) = elabCon (env, denv) x
adamc@91 1324 val (e', et, gs2) = elabExp (env, denv) e
adamc@91 1325 in
adamc@91 1326 checkKind env x' xk kname;
adamc@91 1327 ((x', e', et), gs1 @ gs2 @ gs)
adamc@91 1328 end)
adamc@91 1329 [] xes
adamc@91 1330
adamc@91 1331 val k = (L'.KType, loc)
adamc@91 1332
adamc@91 1333 fun prove (xets, gs) =
adamc@91 1334 case xets of
adamc@91 1335 [] => gs
adamc@91 1336 | (x, _, t) :: rest =>
adamc@91 1337 let
adamc@91 1338 val xc = (x, t)
adamc@91 1339 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@91 1340 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@91 1341 let
adamc@91 1342 val xc' = (x', t')
adamc@91 1343 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@91 1344 in
adamc@91 1345 D.prove env denv (r1, r2, loc) @ gs
adamc@91 1346 end)
adamc@91 1347 gs rest
adamc@91 1348 in
adamc@91 1349 prove (rest, gs)
adamc@91 1350 end
adamc@91 1351 in
adamc@91 1352 ((L'.ERecord xes', loc),
adamc@91 1353 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@91 1354 prove (xes', gs))
adamc@91 1355 end
adamc@91 1356
adamc@91 1357 | L.EField (e, c) =>
adamc@91 1358 let
adamc@91 1359 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1360 val (c', ck, gs2) = elabCon (env, denv) c
adamc@91 1361
adamc@91 1362 val ft = cunif (loc, ktype)
adamc@91 1363 val rest = cunif (loc, ktype_record)
adamc@91 1364 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@91 1365
adamc@91 1366 val gs3 =
adamc@91 1367 checkCon (env, denv) e' et
adamc@91 1368 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@91 1369 val gs4 = D.prove env denv (first, rest, loc)
adamc@91 1370 in
adamc@91 1371 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1372 end
adamc@91 1373
adamc@149 1374 | L.ECut (e, c) =>
adamc@149 1375 let
adamc@149 1376 val (e', et, gs1) = elabExp (env, denv) e
adamc@149 1377 val (c', ck, gs2) = elabCon (env, denv) c
adamc@149 1378
adamc@149 1379 val ft = cunif (loc, ktype)
adamc@149 1380 val rest = cunif (loc, ktype_record)
adamc@149 1381 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@149 1382
adamc@149 1383 val gs3 =
adamc@149 1384 checkCon (env, denv) e' et
adamc@149 1385 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@149 1386 val gs4 = D.prove env denv (first, rest, loc)
adamc@149 1387 in
adamc@149 1388 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@149 1389 end
adamc@149 1390
adamc@91 1391 | L.EFold =>
adamc@91 1392 let
adamc@91 1393 val dom = kunif loc
adamc@91 1394 in
adamc@91 1395 ((L'.EFold dom, loc), foldType (dom, loc), [])
adamc@91 1396 end
adamc@170 1397
adamc@171 1398 | L.ECase (e, pes) =>
adamc@171 1399 let
adamc@171 1400 val (e', et, gs1) = elabExp (env, denv) e
adamc@171 1401 val result = cunif (loc, (L'.KType, loc))
adamc@171 1402 val (pes', gs) = ListUtil.foldlMap
adamc@171 1403 (fn ((p, e), gs) =>
adamc@171 1404 let
adamc@174 1405 val ((p', pt), (env, _)) = elabPat (p, (env, denv, SS.empty))
adamc@171 1406
adamc@171 1407 val gs1 = checkPatCon (env, denv) p' pt et
adamc@171 1408 val (e', et, gs2) = elabExp (env, denv) e
adamc@171 1409 val gs3 = checkCon (env, denv) e' et result
adamc@171 1410 in
adamc@171 1411 ((p', e'), gs1 @ gs2 @ gs3 @ gs)
adamc@171 1412 end)
adamc@171 1413 gs1 pes
adamc@172 1414
adamc@172 1415 val (total, gs') = exhaustive (env, denv, et, map #1 pes')
adamc@171 1416 in
adamc@172 1417 if total then
adamc@172 1418 ()
adamc@172 1419 else
adamc@172 1420 expError env (Inexhaustive loc);
adamc@172 1421
adamc@182 1422 ((L'.ECase (e', pes', {disc = et, result = result}), loc), result, gs' @ gs)
adamc@171 1423 end
adamc@91 1424 end
adamc@12 1425
adamc@12 1426
adamc@6 1427 datatype decl_error =
adamc@91 1428 KunifsRemain of L'.decl list
adamc@91 1429 | CunifsRemain of L'.decl list
adamc@91 1430
adamc@91 1431 fun lspan [] = ErrorMsg.dummySpan
adamc@91 1432 | lspan ((_, loc) :: _) = loc
adamc@6 1433
adamc@6 1434 fun declError env err =
adamc@6 1435 case err of
adamc@91 1436 KunifsRemain ds =>
adamc@91 1437 (ErrorMsg.errorAt (lspan ds) "Some kind unification variables are undetermined in declaration";
adamc@91 1438 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@91 1439 | CunifsRemain ds =>
adamc@91 1440 (ErrorMsg.errorAt (lspan ds) "Some constructor unification variables are undetermined in declaration";
adamc@91 1441 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@6 1442
adamc@31 1443 datatype sgn_error =
adamc@31 1444 UnboundSgn of ErrorMsg.span * string
adamc@31 1445 | UnmatchedSgi of L'.sgn_item
adamc@31 1446 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 1447 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@156 1448 | SgiMismatchedDatatypes of L'.sgn_item * L'.sgn_item * (L'.con * L'.con * cunify_error) option
adamc@41 1449 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 1450 | UnWhereable of L'.sgn * string
adamc@75 1451 | WhereWrongKind of L'.kind * L'.kind * kunify_error
adamc@58 1452 | NotIncludable of L'.sgn
adamc@62 1453 | DuplicateCon of ErrorMsg.span * string
adamc@62 1454 | DuplicateVal of ErrorMsg.span * string
adamc@62 1455 | DuplicateSgn of ErrorMsg.span * string
adamc@62 1456 | DuplicateStr of ErrorMsg.span * string
adamc@88 1457 | NotConstraintsable of L'.sgn
adamc@31 1458
adamc@31 1459 fun sgnError env err =
adamc@31 1460 case err of
adamc@31 1461 UnboundSgn (loc, s) =>
adamc@31 1462 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 1463 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 1464 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 1465 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 1466 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 1467 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@59 1468 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1469 ("Need", p_sgn_item env sgi2),
adamc@31 1470 ("Kind 1", p_kind k1),
adamc@31 1471 ("Kind 2", p_kind k2)];
adamc@31 1472 kunifyError kerr)
adamc@31 1473 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 1474 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@59 1475 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1476 ("Need", p_sgn_item env sgi2),
adamc@31 1477 ("Con 1", p_con env c1),
adamc@31 1478 ("Con 2", p_con env c2)];
adamc@31 1479 cunifyError env cerr)
adamc@156 1480 | SgiMismatchedDatatypes (sgi1, sgi2, cerro) =>
adamc@156 1481 (ErrorMsg.errorAt (#2 sgi1) "Mismatched 'datatype' specifications:";
adamc@156 1482 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@156 1483 ("Need", p_sgn_item env sgi2)];
adamc@156 1484 Option.app (fn (c1, c2, ue) =>
adamc@156 1485 (eprefaces "Unification error"
adamc@156 1486 [("Con 1", p_con env c1),
adamc@156 1487 ("Con 2", p_con env c2)];
adamc@156 1488 cunifyError env ue)) cerro)
adamc@41 1489 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1490 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1491 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1492 ("Sig 2", p_sgn env sgn2)])
adamc@42 1493 | UnWhereable (sgn, x) =>
adamc@42 1494 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1495 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1496 ("Field", PD.string x)])
adamc@75 1497 | WhereWrongKind (k1, k2, kerr) =>
adamc@75 1498 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
adamc@75 1499 eprefaces' [("Have", p_kind k1),
adamc@75 1500 ("Need", p_kind k2)];
adamc@75 1501 kunifyError kerr)
adamc@58 1502 | NotIncludable sgn =>
adamc@58 1503 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
adamc@58 1504 eprefaces' [("Signature", p_sgn env sgn)])
adamc@62 1505 | DuplicateCon (loc, s) =>
adamc@62 1506 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
adamc@62 1507 | DuplicateVal (loc, s) =>
adamc@62 1508 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
adamc@62 1509 | DuplicateSgn (loc, s) =>
adamc@62 1510 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
adamc@62 1511 | DuplicateStr (loc, s) =>
adamc@62 1512 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
adamc@88 1513 | NotConstraintsable sgn =>
adamc@88 1514 (ErrorMsg.errorAt (#2 sgn) "Invalid signature for 'open constraints'";
adamc@88 1515 eprefaces' [("Signature", p_sgn env sgn)])
adamc@31 1516
adamc@31 1517 datatype str_error =
adamc@31 1518 UnboundStr of ErrorMsg.span * string
adamc@44 1519 | NotFunctor of L'.sgn
adamc@46 1520 | FunctorRebind of ErrorMsg.span
adamc@61 1521 | UnOpenable of L'.sgn
adamc@75 1522 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
adamc@156 1523 | DuplicateConstructor of string * ErrorMsg.span
adamc@158 1524 | NotDatatype of ErrorMsg.span
adamc@31 1525
adamc@31 1526 fun strError env err =
adamc@31 1527 case err of
adamc@31 1528 UnboundStr (loc, s) =>
adamc@31 1529 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1530 | NotFunctor sgn =>
adamc@44 1531 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1532 eprefaces' [("Signature", p_sgn env sgn)])
adamc@46 1533 | FunctorRebind loc =>
adamc@46 1534 ErrorMsg.errorAt loc "Attempt to rebind functor"
adamc@61 1535 | UnOpenable sgn =>
adamc@61 1536 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
adamc@61 1537 eprefaces' [("Signature", p_sgn env sgn)])
adamc@75 1538 | NotType (k, (k1, k2, ue)) =>
adamc@75 1539 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
adamc@75 1540 eprefaces' [("Kind", p_kind k),
adamc@75 1541 ("Subkind 1", p_kind k1),
adamc@75 1542 ("Subkind 2", p_kind k2)];
adamc@75 1543 kunifyError ue)
adamc@156 1544 | DuplicateConstructor (x, loc) =>
adamc@156 1545 ErrorMsg.errorAt loc ("Duplicate datatype constructor " ^ x)
adamc@158 1546 | NotDatatype loc =>
adamc@158 1547 ErrorMsg.errorAt loc "Trying to import non-datatype as a datatype"
adamc@31 1548
adamc@42 1549 val hnormSgn = E.hnormSgn
adamc@31 1550
adamc@88 1551 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@76 1552 case sgi of
adamc@76 1553 L.SgiConAbs (x, k) =>
adamc@76 1554 let
adamc@76 1555 val k' = elabKind k
adamc@31 1556
adamc@76 1557 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1558 in
adamc@88 1559 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@76 1560 end
adamc@31 1561
adamc@76 1562 | L.SgiCon (x, ko, c) =>
adamc@76 1563 let
adamc@76 1564 val k' = case ko of
adamc@76 1565 NONE => kunif loc
adamc@76 1566 | SOME k => elabKind k
adamc@31 1567
adamc@83 1568 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1569 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1570 in
adamc@76 1571 checkKind env c' ck k';
adamc@31 1572
adamc@88 1573 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1574 end
adamc@31 1575
adamc@157 1576 | L.SgiDatatype (x, xcs) =>
adamc@157 1577 let
adamc@157 1578 val k = (L'.KType, loc)
adamc@157 1579 val (env, n) = E.pushCNamed env x k NONE
adamc@157 1580 val t = (L'.CNamed n, loc)
adamc@157 1581
adamc@157 1582 val (xcs, (used, env, gs)) =
adamc@157 1583 ListUtil.foldlMap
adamc@157 1584 (fn ((x, to), (used, env, gs)) =>
adamc@157 1585 let
adamc@157 1586 val (to, t, gs') = case to of
adamc@157 1587 NONE => (NONE, t, gs)
adamc@157 1588 | SOME t' =>
adamc@157 1589 let
adamc@157 1590 val (t', tk, gs') = elabCon (env, denv) t'
adamc@157 1591 in
adamc@157 1592 checkKind env t' tk k;
adamc@157 1593 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@157 1594 end
adamc@157 1595
adamc@157 1596 val (env, n') = E.pushENamed env x t
adamc@157 1597 in
adamc@157 1598 if SS.member (used, x) then
adamc@157 1599 strError env (DuplicateConstructor (x, loc))
adamc@157 1600 else
adamc@157 1601 ();
adamc@157 1602 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@157 1603 end)
adamc@157 1604 (SS.empty, env, []) xcs
adamc@157 1605 in
adamc@157 1606 ([(L'.SgiDatatype (x, n, xcs), loc)], (env, denv, gs))
adamc@157 1607 end
adamc@156 1608
adamc@158 1609 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
adamc@158 1610
adamc@158 1611 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
adamc@158 1612 (case E.lookupStr env m1 of
adamc@158 1613 NONE => (strError env (UnboundStr (loc, m1));
adamc@158 1614 ([], (env, denv, gs)))
adamc@158 1615 | SOME (n, sgn) =>
adamc@158 1616 let
adamc@158 1617 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@158 1618 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1619 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@158 1620 (strerror, sgnerror))
adamc@158 1621 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@158 1622 ((L'.StrVar n, loc), sgn) ms
adamc@158 1623 in
adamc@161 1624 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@161 1625 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@161 1626 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@161 1627 NONE => (conError env (UnboundDatatype (loc, s));
adamc@161 1628 ([], (env, denv, gs)))
adamc@161 1629 | SOME xncs =>
adamc@161 1630 let
adamc@161 1631 val k = (L'.KType, loc)
adamc@161 1632 val t = (L'.CModProj (n, ms, s), loc)
adamc@161 1633 val (env, n') = E.pushCNamed env x k (SOME t)
adamc@161 1634 val env = E.pushDatatype env n' xncs
adamc@158 1635
adamc@161 1636 val t = (L'.CNamed n', loc)
adamc@161 1637 val env = foldl (fn ((x, n, to), env) =>
adamc@161 1638 let
adamc@161 1639 val t = case to of
adamc@161 1640 NONE => t
adamc@161 1641 | SOME t' => (L'.TFun (t', t), loc)
adamc@161 1642 in
adamc@161 1643 E.pushENamedAs env x n t
adamc@161 1644 end) env xncs
adamc@161 1645 in
adamc@162 1646 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xncs), loc)], (env, denv, gs))
adamc@161 1647 end)
adamc@161 1648 | _ => (strError env (NotDatatype loc);
adamc@161 1649 ([], (env, denv, [])))
adamc@158 1650 end)
adamc@156 1651
adamc@76 1652 | L.SgiVal (x, c) =>
adamc@76 1653 let
adamc@83 1654 val (c', ck, gs') = elabCon (env, denv) c
adamc@31 1655
adamc@76 1656 val (env', n) = E.pushENamed env x c'
adamc@76 1657 in
adamc@76 1658 (unifyKinds ck ktype
adamc@76 1659 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 1660
adamc@88 1661 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@76 1662 end
adamc@31 1663
adamc@76 1664 | L.SgiStr (x, sgn) =>
adamc@76 1665 let
adamc@83 1666 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1667 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1668 in
adamc@88 1669 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1670 end
adamc@31 1671
adamc@76 1672 | L.SgiSgn (x, sgn) =>
adamc@76 1673 let
adamc@83 1674 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1675 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1676 in
adamc@88 1677 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1678 end
adamc@31 1679
adamc@76 1680 | L.SgiInclude sgn =>
adamc@76 1681 let
adamc@83 1682 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1683 in
adamc@76 1684 case #1 (hnormSgn env sgn') of
adamc@76 1685 L'.SgnConst sgis =>
adamc@88 1686 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@76 1687 | _ => (sgnError env (NotIncludable sgn');
adamc@88 1688 ([], (env, denv, [])))
adamc@88 1689 end
adamc@88 1690
adamc@88 1691 | L.SgiConstraint (c1, c2) =>
adamc@88 1692 let
adamc@88 1693 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 1694 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@88 1695
adamc@90 1696 val (denv, gs3) = D.assert env denv (c1', c2')
adamc@88 1697 in
adamc@88 1698 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 1699 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 1700
adamc@90 1701 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3))
adamc@76 1702 end
adamc@31 1703
adamc@83 1704 and elabSgn (env, denv) (sgn, loc) =
adamc@31 1705 case sgn of
adamc@31 1706 L.SgnConst sgis =>
adamc@31 1707 let
adamc@88 1708 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 1709
adamc@62 1710 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 1711 case sgi of
adamc@62 1712 L'.SgiConAbs (x, _, _) =>
adamc@62 1713 (if SS.member (cons, x) then
adamc@62 1714 sgnError env (DuplicateCon (loc, x))
adamc@62 1715 else
adamc@62 1716 ();
adamc@62 1717 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1718 | L'.SgiCon (x, _, _, _) =>
adamc@62 1719 (if SS.member (cons, x) then
adamc@62 1720 sgnError env (DuplicateCon (loc, x))
adamc@62 1721 else
adamc@62 1722 ();
adamc@62 1723 (SS.add (cons, x), vals, sgns, strs))
adamc@156 1724 | L'.SgiDatatype (x, _, xncs) =>
adamc@156 1725 let
adamc@156 1726 val vals = foldl (fn ((x, _, _), vals) =>
adamc@156 1727 (if SS.member (vals, x) then
adamc@156 1728 sgnError env (DuplicateVal (loc, x))
adamc@156 1729 else
adamc@156 1730 ();
adamc@156 1731 SS.add (vals, x)))
adamc@156 1732 vals xncs
adamc@156 1733 in
adamc@156 1734 if SS.member (cons, x) then
adamc@156 1735 sgnError env (DuplicateCon (loc, x))
adamc@156 1736 else
adamc@156 1737 ();
adamc@156 1738 (SS.add (cons, x), vals, sgns, strs)
adamc@156 1739 end
adamc@162 1740 | L'.SgiDatatypeImp (x, _, _, _, _, _) =>
adamc@156 1741 (if SS.member (cons, x) then
adamc@156 1742 sgnError env (DuplicateCon (loc, x))
adamc@156 1743 else
adamc@156 1744 ();
adamc@156 1745 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1746 | L'.SgiVal (x, _, _) =>
adamc@62 1747 (if SS.member (vals, x) then
adamc@62 1748 sgnError env (DuplicateVal (loc, x))
adamc@62 1749 else
adamc@62 1750 ();
adamc@62 1751 (cons, SS.add (vals, x), sgns, strs))
adamc@62 1752 | L'.SgiSgn (x, _, _) =>
adamc@62 1753 (if SS.member (sgns, x) then
adamc@62 1754 sgnError env (DuplicateSgn (loc, x))
adamc@62 1755 else
adamc@62 1756 ();
adamc@62 1757 (cons, vals, SS.add (sgns, x), strs))
adamc@62 1758 | L'.SgiStr (x, _, _) =>
adamc@62 1759 (if SS.member (strs, x) then
adamc@62 1760 sgnError env (DuplicateStr (loc, x))
adamc@62 1761 else
adamc@62 1762 ();
adamc@88 1763 (cons, vals, sgns, SS.add (strs, x)))
adamc@88 1764 | L'.SgiConstraint _ => (cons, vals, sgns, strs))
adamc@62 1765 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 1766 in
adamc@83 1767 ((L'.SgnConst sgis', loc), gs)
adamc@31 1768 end
adamc@31 1769 | L.SgnVar x =>
adamc@31 1770 (case E.lookupSgn env x of
adamc@31 1771 NONE =>
adamc@31 1772 (sgnError env (UnboundSgn (loc, x));
adamc@83 1773 ((L'.SgnError, loc), []))
adamc@83 1774 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 1775 | L.SgnFun (m, dom, ran) =>
adamc@41 1776 let
adamc@83 1777 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1778 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1779 val (ran', gs2) = elabSgn (env', denv) ran
adamc@41 1780 in
adamc@83 1781 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 1782 end
adamc@42 1783 | L.SgnWhere (sgn, x, c) =>
adamc@42 1784 let
adamc@83 1785 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 1786 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 1787 in
adamc@42 1788 case #1 (hnormSgn env sgn') of
adamc@83 1789 L'.SgnError => (sgnerror, [])
adamc@42 1790 | L'.SgnConst sgis =>
adamc@75 1791 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 1792 x' = x andalso
adamc@75 1793 (unifyKinds k ck
adamc@75 1794 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 1795 true)
adamc@42 1796 | _ => false) sgis then
adamc@83 1797 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 1798 else
adamc@42 1799 (sgnError env (UnWhereable (sgn', x));
adamc@83 1800 (sgnerror, []))
adamc@42 1801 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 1802 (sgnerror, []))
adamc@42 1803 end
adamc@59 1804 | L.SgnProj (m, ms, x) =>
adamc@59 1805 (case E.lookupStr env m of
adamc@59 1806 NONE => (strError env (UnboundStr (loc, m));
adamc@83 1807 (sgnerror, []))
adamc@59 1808 | SOME (n, sgn) =>
adamc@59 1809 let
adamc@59 1810 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 1811 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 1812 NONE => (strError env (UnboundStr (loc, m));
adamc@59 1813 (strerror, sgnerror))
adamc@59 1814 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 1815 ((L'.StrVar n, loc), sgn) ms
adamc@59 1816 in
adamc@59 1817 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 1818 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 1819 (sgnerror, []))
adamc@83 1820 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 1821 end)
adamc@59 1822
adamc@31 1823
adamc@66 1824 fun selfify env {str, strs, sgn} =
adamc@66 1825 case #1 (hnormSgn env sgn) of
adamc@66 1826 L'.SgnError => sgn
adamc@66 1827 | L'.SgnVar _ => sgn
adamc@66 1828
adamc@66 1829 | L'.SgnConst sgis =>
adamc@66 1830 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 1831 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@158 1832 | (L'.SgiDatatype (x, n, xncs), loc) =>
adamc@162 1833 (L'.SgiDatatypeImp (x, n, str, strs, x, xncs), loc)
adamc@66 1834 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 1835 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 1836 | x => x) sgis), #2 sgn)
adamc@66 1837 | L'.SgnFun _ => sgn
adamc@66 1838 | L'.SgnWhere _ => sgn
adamc@66 1839 | L'.SgnProj (m, ms, x) =>
adamc@66 1840 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1841 (L'.StrVar m, #2 sgn) ms,
adamc@66 1842 sgn = #2 (E.lookupStrNamed env m),
adamc@66 1843 field = x} of
adamc@66 1844 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 1845 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 1846
adamc@66 1847 fun selfifyAt env {str, sgn} =
adamc@66 1848 let
adamc@66 1849 fun self (str, _) =
adamc@66 1850 case str of
adamc@66 1851 L'.StrVar x => SOME (x, [])
adamc@66 1852 | L'.StrProj (str, x) =>
adamc@66 1853 (case self str of
adamc@66 1854 NONE => NONE
adamc@66 1855 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 1856 | _ => NONE
adamc@66 1857 in
adamc@66 1858 case self str of
adamc@66 1859 NONE => sgn
adamc@66 1860 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 1861 end
adamc@66 1862
adamc@88 1863 fun dopen (env, denv) {str, strs, sgn} =
adamc@66 1864 let
adamc@66 1865 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1866 (L'.StrVar str, #2 sgn) strs
adamc@66 1867 in
adamc@66 1868 case #1 (hnormSgn env sgn) of
adamc@66 1869 L'.SgnConst sgis =>
adamc@88 1870 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
adamc@162 1871 let
adamc@162 1872 val d =
adamc@162 1873 case sgi of
adamc@162 1874 L'.SgiConAbs (x, n, k) =>
adamc@162 1875 let
adamc@162 1876 val c = (L'.CModProj (str, strs, x), loc)
adamc@162 1877 in
adamc@162 1878 (L'.DCon (x, n, k, c), loc)
adamc@162 1879 end
adamc@162 1880 | L'.SgiCon (x, n, k, c) =>
adamc@162 1881 (L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@162 1882 | L'.SgiDatatype (x, n, xncs) =>
adamc@162 1883 (L'.DDatatypeImp (x, n, str, strs, x, xncs), loc)
adamc@162 1884 | L'.SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
adamc@162 1885 (L'.DDatatypeImp (x, n, m1, ms, x', xncs), loc)
adamc@162 1886 | L'.SgiVal (x, n, t) =>
adamc@162 1887 (L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)
adamc@162 1888 | L'.SgiStr (x, n, sgn) =>
adamc@162 1889 (L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)
adamc@162 1890 | L'.SgiSgn (x, n, sgn) =>
adamc@162 1891 (L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)
adamc@162 1892 | L'.SgiConstraint (c1, c2) =>
adamc@162 1893 (L'.DConstraint (c1, c2), loc)
adamc@162 1894 in
adamc@162 1895 (d, (E.declBinds env' d, denv'))
adamc@162 1896 end)
adamc@88 1897 (env, denv) sgis
adamc@66 1898 | _ => (strError env (UnOpenable sgn);
adamc@88 1899 ([], (env, denv)))
adamc@66 1900 end
adamc@66 1901
adamc@88 1902 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@88 1903 case E.lookupStr env str of
adamc@88 1904 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1905 denv)
adamc@88 1906 | SOME (n, sgn) =>
adamc@88 1907 let
adamc@88 1908 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 1909 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 1910 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1911 (strerror, sgnerror))
adamc@88 1912 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 1913 ((L'.StrVar n, loc), sgn) strs
adamc@88 1914
adamc@88 1915 val cso = E.projectConstraints env {sgn = sgn, str = st}
adamc@88 1916
adamc@88 1917 val denv = case cso of
adamc@88 1918 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1919 denv)
adamc@90 1920 | SOME cs => foldl (fn ((c1, c2), denv) =>
adamc@90 1921 let
adamc@90 1922 val (denv, gs) = D.assert env denv (c1, c2)
adamc@90 1923 in
adamc@90 1924 case gs of
adamc@90 1925 [] => ()
adamc@90 1926 | _ => raise Fail "dopenConstraints: Sub-constraints remain";
adamc@90 1927
adamc@90 1928 denv
adamc@90 1929 end) denv cs
adamc@88 1930 in
adamc@88 1931 denv
adamc@88 1932 end
adamc@88 1933
adamc@31 1934 fun sgiOfDecl (d, loc) =
adamc@31 1935 case d of
adamc@123 1936 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@156 1937 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
adamc@156 1938 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
adamc@123 1939 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 1940 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 1941 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 1942 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 1943 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 1944 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 1945 | L'.DExport _ => []
adamc@88 1946
adamc@88 1947 fun sgiBindsD (env, denv) (sgi, _) =
adamc@88 1948 case sgi of
adamc@90 1949 L'.SgiConstraint (c1, c2) =>
adamc@90 1950 (case D.assert env denv (c1, c2) of
adamc@90 1951 (denv, []) => denv
adamc@90 1952 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
adamc@88 1953 | _ => denv
adamc@31 1954
adamc@86 1955 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
adamc@35 1956 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 1957 (L'.SgnError, _) => ()
adamc@31 1958 | (_, L'.SgnError) => ()
adamc@31 1959
adamc@31 1960 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1961 let
adamc@156 1962 fun folder (sgi2All as (sgi, loc), (env, denv)) =
adamc@31 1963 let
adamc@31 1964 fun seek p =
adamc@31 1965 let
adamc@88 1966 fun seek (env, denv) ls =
adamc@31 1967 case ls of
adamc@31 1968 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@88 1969 (env, denv))
adamc@31 1970 | h :: t =>
adamc@31 1971 case p h of
adamc@88 1972 NONE => seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
adamc@88 1973 | SOME envs => envs
adamc@31 1974 in
adamc@88 1975 seek (env, denv) sgis1
adamc@31 1976 end
adamc@31 1977 in
adamc@31 1978 case sgi of
adamc@31 1979 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1980 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1981 let
adamc@41 1982 fun found (x', n1, k1, co1) =
adamc@41 1983 if x = x' then
adamc@41 1984 let
adamc@41 1985 val () = unifyKinds k1 k2
adamc@41 1986 handle KUnify (k1, k2, err) =>
adamc@41 1987 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 1988 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 1989 in
adamc@41 1990 SOME (if n1 = n2 then
adamc@41 1991 env
adamc@41 1992 else
adamc@88 1993 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
adamc@88 1994 denv)
adamc@41 1995 end
adamc@41 1996 else
adamc@41 1997 NONE
adamc@31 1998 in
adamc@31 1999 case sgi1 of
adamc@41 2000 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 2001 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@159 2002 | L'.SgiDatatype (x', n1, _) => found (x', n1, (L'.KType, loc), NONE)
adamc@162 2003 | L'.SgiDatatypeImp (x', n1, m1, ms, s, _) =>
adamc@159 2004 found (x', n1, (L'.KType, loc), SOME (L'.CModProj (m1, ms, s), loc))
adamc@31 2005 | _ => NONE
adamc@31 2006 end)
adamc@31 2007
adamc@31 2008 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 2009 seek (fn sgi1All as (sgi1, _) =>
adamc@31 2010 case sgi1 of
adamc@41 2011 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 2012 if x = x' then
adamc@41 2013 let
adamc@88 2014 fun good () = SOME (E.pushCNamedAs env x n2 k2 (SOME c2), denv)
adamc@41 2015 in
adamc@86 2016 (case unifyCons (env, denv) c1 c2 of
adamc@86 2017 [] => good ()
adamc@86 2018 | _ => NONE)
adamc@86 2019 handle CUnify (c1, c2, err) =>
adamc@86 2020 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@86 2021 good ())
adamc@41 2022 end
adamc@41 2023 else
adamc@41 2024 NONE
adamc@31 2025 | _ => NONE)
adamc@31 2026
adamc@156 2027 | L'.SgiDatatype (x, n2, xncs2) =>
adamc@156 2028 seek (fn sgi1All as (sgi1, _) =>
adamc@160 2029 let
adamc@160 2030 fun found (n1, xncs1) =
adamc@160 2031 let
adamc@160 2032 fun mismatched ue =
adamc@160 2033 (sgnError env (SgiMismatchedDatatypes (sgi1All, sgi2All, ue));
adamc@160 2034 SOME (env, denv))
adamc@156 2035
adamc@160 2036 fun good () =
adamc@160 2037 let
adamc@160 2038 val env = E.sgiBinds env sgi2All
adamc@160 2039 val env = if n1 = n2 then
adamc@160 2040 env
adamc@160 2041 else
adamc@160 2042 E.pushCNamedAs env x n1 (L'.KType, loc)
adamc@160 2043 (SOME (L'.CNamed n1, loc))
adamc@160 2044 in
adamc@160 2045 SOME (env, denv)
adamc@160 2046 end
adamc@156 2047
adamc@160 2048 fun xncBad ((x1, _, t1), (x2, _, t2)) =
adamc@160 2049 String.compare (x1, x2) <> EQUAL
adamc@160 2050 orelse case (t1, t2) of
adamc@160 2051 (NONE, NONE) => false
adamc@160 2052 | (SOME t1, SOME t2) =>
adamc@160 2053 not (List.null (unifyCons (env, denv) t1 t2))
adamc@160 2054 | _ => true
adamc@160 2055 in
adamc@160 2056 (if length xncs1 <> length xncs2
adamc@160 2057 orelse ListPair.exists xncBad (xncs1, xncs2) then
adamc@160 2058 mismatched NONE
adamc@160 2059 else
adamc@160 2060 good ())
adamc@160 2061 handle CUnify ue => mismatched (SOME ue)
adamc@160 2062 end
adamc@160 2063 in
adamc@160 2064 case sgi1 of
adamc@160 2065 L'.SgiDatatype (x', n1, xncs1) =>
adamc@160 2066 if x' = x then
adamc@160 2067 found (n1, xncs1)
adamc@160 2068 else
adamc@160 2069 NONE
adamc@162 2070 | L'.SgiDatatypeImp (x', n1, _, _, _, xncs1) =>
adamc@162 2071 if x' = x then
adamc@162 2072 found (n1, xncs1)
adamc@162 2073 else
adamc@162 2074 NONE
adamc@160 2075 | _ => NONE
adamc@160 2076 end)
adamc@156 2077
adamc@162 2078 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, _) =>
adamc@158 2079 seek (fn sgi1All as (sgi1, _) =>
adamc@158 2080 case sgi1 of
adamc@162 2081 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _) =>
adamc@158 2082 if x = x' then
adamc@158 2083 let
adamc@158 2084 val k = (L'.KType, loc)
adamc@158 2085 val t1 = (L'.CModProj (m11, ms1, s1), loc)
adamc@158 2086 val t2 = (L'.CModProj (m12, ms2, s2), loc)
adamc@158 2087
adamc@158 2088 fun good () =
adamc@158 2089 let
adamc@158 2090 val env = E.pushCNamedAs env x n1 k (SOME t1)
adamc@158 2091 val env = E.pushCNamedAs env x n2 k (SOME t2)
adamc@158 2092 in
adamc@158 2093 SOME (env, denv)
adamc@158 2094 end
adamc@158 2095 in
adamc@158 2096 (case unifyCons (env, denv) t1 t2 of
adamc@158 2097 [] => good ()
adamc@158 2098 | _ => NONE)
adamc@158 2099 handle CUnify (c1, c2, err) =>
adamc@158 2100 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@158 2101 good ())
adamc@158 2102 end
adamc@158 2103 else
adamc@158 2104 NONE
adamc@158 2105
adamc@158 2106 | _ => NONE)
adamc@156 2107
adamc@32 2108 | L'.SgiVal (x, n2, c2) =>
adamc@32 2109 seek (fn sgi1All as (sgi1, _) =>
adamc@32 2110 case sgi1 of
adamc@41 2111 L'.SgiVal (x', n1, c1) =>
adamc@41 2112 if x = x' then
adamc@86 2113 (case unifyCons (env, denv) c1 c2 of
adamc@88 2114 [] => SOME (env, denv)
adamc@86 2115 | _ => NONE)
adamc@86 2116 handle CUnify (c1, c2, err) =>
adamc@86 2117 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@88 2118 SOME (env, denv))
adamc@41 2119 else
adamc@41 2120 NONE
adamc@32 2121 | _ => NONE)
adamc@32 2122
adamc@33 2123 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 2124 seek (fn sgi1All as (sgi1, _) =>
adamc@33 2125 case sgi1 of
adamc@41 2126 L'.SgiStr (x', n1, sgn1) =>
adamc@41 2127 if x = x' then
adamc@66 2128 let
adamc@86 2129 val () = subSgn (env, denv) sgn1 sgn2
adamc@66 2130 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 2131 val env = if n1 = n2 then
adamc@66 2132 env
adamc@66 2133 else
adamc@66 2134 E.pushStrNamedAs env x n2
adamc@66 2135 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 2136 sgn = sgn2})
adamc@66 2137 in
adamc@88 2138 SOME (env, denv)
adamc@66 2139 end
adamc@41 2140 else
adamc@41 2141 NONE
adamc@33 2142 | _ => NONE)
adamc@59 2143
adamc@59 2144 | L'.SgiSgn (x, n2, sgn2) =>
adamc@59 2145 seek (fn sgi1All as (sgi1, _) =>
adamc@59 2146 case sgi1 of
adamc@59 2147 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 2148 if x = x' then
adamc@65 2149 let
adamc@86 2150 val () = subSgn (env, denv) sgn1 sgn2
adamc@86 2151 val () = subSgn (env, denv) sgn2 sgn1
adamc@65 2152
adamc@65 2153 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 2154 val env = if n1 = n2 then
adamc@65 2155 env
adamc@65 2156 else
adamc@65 2157 E.pushSgnNamedAs env x n1 sgn2
adamc@65 2158 in
adamc@88 2159 SOME (env, denv)
adamc@65 2160 end
adamc@59 2161 else
adamc@59 2162 NONE
adamc@59 2163 | _ => NONE)
adamc@88 2164
adamc@88 2165 | L'.SgiConstraint (c2, d2) =>
adamc@88 2166 seek (fn sgi1All as (sgi1, _) =>
adamc@88 2167 case sgi1 of
adamc@88 2168 L'.SgiConstraint (c1, d1) =>
adamc@88 2169 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
adamc@90 2170 let
adamc@90 2171 val (denv, gs) = D.assert env denv (c2, d2)
adamc@90 2172 in
adamc@90 2173 case gs of
adamc@90 2174 [] => ()
adamc@90 2175 | _ => raise Fail "subSgn: Sub-constraints remain";
adamc@90 2176
adamc@90 2177 SOME (env, denv)
adamc@90 2178 end
adamc@88 2179 else
adamc@88 2180 NONE
adamc@88 2181 | _ => NONE)
adamc@31 2182 end
adamc@31 2183 in
adamc@88 2184 ignore (foldl folder (env, denv) sgis2)
adamc@31 2185 end
adamc@31 2186
adamc@41 2187 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 2188 let
adamc@43 2189 val ran1 =
adamc@43 2190 if n1 = n2 then
adamc@43 2191 ran1
adamc@43 2192 else
adamc@43 2193 subStrInSgn (n1, n2) ran1
adamc@43 2194 in
adamc@86 2195 subSgn (env, denv) dom2 dom1;
adamc@86 2196 subSgn (E.pushStrNamedAs env m2 n2 dom2, denv) ran1 ran2
adamc@43 2197 end
adamc@41 2198
adamc@41 2199 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 2200
adamc@61 2201
adamc@88 2202 fun elabDecl ((d, loc), (env, denv, gs)) =
adamc@76 2203 case d of
adamc@76 2204 L.DCon (x, ko, c) =>
adamc@76 2205 let
adamc@76 2206 val k' = case ko of
adamc@76 2207 NONE => kunif loc
adamc@76 2208 | SOME k => elabKind k
adamc@76 2209
adamc@83 2210 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 2211 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 2212 in
adamc@76 2213 checkKind env c' ck k';
adamc@76 2214
adamc@88 2215 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 2216 end
adamc@156 2217 | L.DDatatype (x, xcs) =>
adamc@156 2218 let
adamc@156 2219 val k = (L'.KType, loc)
adamc@156 2220 val (env, n) = E.pushCNamed env x k NONE
adamc@156 2221 val t = (L'.CNamed n, loc)
adamc@156 2222
adamc@156 2223 val (xcs, (used, env, gs)) =
adamc@156 2224 ListUtil.foldlMap
adamc@156 2225 (fn ((x, to), (used, env, gs)) =>
adamc@156 2226 let
adamc@156 2227 val (to, t, gs') = case to of
adamc@156 2228 NONE => (NONE, t, gs)
adamc@156 2229 | SOME t' =>
adamc@156 2230 let
adamc@156 2231 val (t', tk, gs') = elabCon (env, denv) t'
adamc@156 2232 in
adamc@156 2233 checkKind env t' tk k;
adamc@156 2234 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@156 2235 end
adamc@156 2236
adamc@156 2237 val (env, n') = E.pushENamed env x t
adamc@156 2238 in
adamc@156 2239 if SS.member (used, x) then
adamc@156 2240 strError env (DuplicateConstructor (x, loc))
adamc@156 2241 else
adamc@156 2242 ();
adamc@156 2243 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@156 2244 end)
adamc@156 2245 (SS.empty, env, []) xcs
adamc@171 2246
adamc@171 2247 val env = E.pushDatatype env n xcs
adamc@156 2248 in
adamc@156 2249 ([(L'.DDatatype (x, n, xcs), loc)], (env, denv, gs))
adamc@156 2250 end
adamc@156 2251
adamc@157 2252 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
adamc@157 2253
adamc@157 2254 | L.DDatatypeImp (x, m1 :: ms, s) =>
adamc@157 2255 (case E.lookupStr env m1 of
adamc@157 2256 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@157 2257 ([], (env, denv, gs)))
adamc@157 2258 | SOME (n, sgn) =>
adamc@157 2259 let
adamc@157 2260 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@157 2261 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@157 2262 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@157 2263 (strerror, sgnerror))
adamc@157 2264 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@157 2265 ((L'.StrVar n, loc), sgn) ms
adamc@157 2266 in
adamc@158 2267 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@158 2268 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@158 2269 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@158 2270 NONE => (conError env (UnboundDatatype (loc, s));
adamc@158 2271 ([], (env, denv, gs)))
adamc@158 2272 | SOME xncs =>
adamc@158 2273 let
adamc@158 2274 val k = (L'.KType, loc)
adamc@158 2275 val t = (L'.CModProj (n, ms, s), loc)
adamc@158 2276 val (env, n') = E.pushCNamed env x k (SOME t)
adamc@158 2277 val env = E.pushDatatype env n' xncs
adamc@157 2278
adamc@158 2279 val t = (L'.CNamed n', loc)
adamc@158 2280 val env = foldl (fn ((x, n, to), env) =>
adamc@158 2281 let
adamc@158 2282 val t = case to of
adamc@158 2283 NONE => t
adamc@158 2284 | SOME t' => (L'.TFun (t', t), loc)
adamc@158 2285 in
adamc@158 2286 E.pushENamedAs env x n t
adamc@158 2287 end) env xncs
adamc@158 2288 in
adamc@162 2289 ([(L'.DDatatypeImp (x, n', n, ms, s, xncs), loc)], (env, denv, gs))
adamc@158 2290 end)
adamc@158 2291 | _ => (strError env (NotDatatype loc);
adamc@158 2292 ([], (env, denv, [])))
adamc@157 2293 end)
adamc@157 2294
adamc@76 2295 | L.DVal (x, co, e) =>
adamc@76 2296 let
adamc@123 2297 val (c', _, gs1) = case co of
adamc@83 2298 NONE => (cunif (loc, ktype), ktype, [])
adamc@83 2299 | SOME c => elabCon (env, denv) c
adamc@76 2300
adamc@83 2301 val (e', et, gs2) = elabExp (env, denv) e
adamc@76 2302 val (env', n) = E.pushENamed env x c'
adamc@86 2303
adamc@86 2304 val gs3 = checkCon (env, denv) e' et c'
adamc@76 2305 in
adamc@88 2306 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, gs1 @ gs2 @ gs3 @ gs))
adamc@76 2307 end
adamc@123 2308 | L.DValRec vis =>
adamc@123 2309 let
adamc@123 2310 val (vis, gs) = ListUtil.foldlMap
adamc@123 2311 (fn ((x, co, e), gs) =>
adamc@123 2312 let
adamc@123 2313 val (c', _, gs1) = case co of
adamc@123 2314 NONE => (cunif (loc, ktype), ktype, [])
adamc@123 2315 | SOME c => elabCon (env, denv) c
adamc@123 2316 in
adamc@123 2317 ((x, c', e), gs1 @ gs)
adamc@123 2318 end) [] vis
adamc@123 2319
adamc@123 2320 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@123 2321 let
adamc@123 2322 val (env, n) = E.pushENamed env x c'
adamc@123 2323 in
adamc@123 2324 ((x, n, c', e), env)
adamc@123 2325 end) env vis
adamc@123 2326
adamc@123 2327 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@123 2328 let
adamc@123 2329 val (e', et, gs1) = elabExp (env, denv) e
adamc@123 2330
adamc@123 2331 val gs2 = checkCon (env, denv) e' et c'
adamc@123 2332 in
adamc@123 2333 ((x, n, c', e'), gs1 @ gs2 @ gs)
adamc@123 2334 end) gs vis
adamc@123 2335 in
adamc@123 2336 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@123 2337 end
adamc@76 2338
adamc@76 2339 | L.DSgn (x, sgn) =>
adamc@76 2340 let
adamc@83 2341 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2342 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 2343 in
adamc@88 2344 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2345 end
adamc@76 2346
adamc@76 2347 | L.DStr (x, sgno, str) =>
adamc@76 2348 let
adamc@93 2349 val () = if x = "Basis" then
adamc@93 2350 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@93 2351 else
adamc@93 2352 ()
adamc@93 2353
adamc@83 2354 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@76 2355
adamc@83 2356 val (str', sgn', gs') =
adamc@78 2357 case formal of
adamc@78 2358 NONE =>
adamc@78 2359 let
adamc@83 2360 val (str', actual, ds) = elabStr (env, denv) str
adamc@78 2361 in
adamc@83 2362 (str', selfifyAt env {str = str', sgn = actual}, ds)
adamc@78 2363 end
adamc@83 2364 | SOME (formal, gs1) =>
adamc@78 2365 let
adamc@78 2366 val str =
adamc@78 2367 case #1 (hnormSgn env formal) of
adamc@78 2368 L'.SgnConst sgis =>
adamc@78 2369 (case #1 str of
adamc@78 2370 L.StrConst ds =>
adamc@78 2371 let
adamc@78 2372 val needed = foldl (fn ((sgi, _), needed) =>
adamc@78 2373 case sgi of
adamc@78 2374 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
adamc@78 2375 | _ => needed)
adamc@78 2376 SS.empty sgis
adamc@78 2377
adamc@78 2378 val needed = foldl (fn ((d, _), needed) =>
adamc@78 2379 case d of
adamc@78 2380 L.DCon (x, _, _) => (SS.delete (needed, x)
adamc@78 2381 handle NotFound => needed)
adamc@78 2382 | L.DOpen _ => SS.empty
adamc@78 2383 | _ => needed)
adamc@78 2384 needed ds
adamc@78 2385 in
adamc@78 2386 case SS.listItems needed of
adamc@78 2387 [] => str
adamc@78 2388 | xs =>
adamc@78 2389 let
adamc@78 2390 val kwild = (L.KWild, #2 str)
adamc@78 2391 val cwild = (L.CWild kwild, #2 str)
adamc@78 2392 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
adamc@78 2393 in
adamc@78 2394 (L.StrConst (ds @ ds'), #2 str)
adamc@78 2395 end
adamc@78 2396 end
adamc@78 2397 | _ => str)
adamc@78 2398 | _ => str
adamc@78 2399
adamc@83 2400 val (str', actual, gs2) = elabStr (env, denv) str
adamc@78 2401 in
adamc@158 2402 subSgn (env, denv) (selfifyAt env {str = str', sgn = actual}) formal;
adamc@83 2403 (str', formal, gs1 @ gs2)
adamc@78 2404 end
adamc@76 2405
adamc@76 2406 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2407 in
adamc@76 2408 case #1 (hnormSgn env sgn') of
adamc@76 2409 L'.SgnFun _ =>
adamc@76 2410 (case #1 str' of
adamc@76 2411 L'.StrFun _ => ()
adamc@76 2412 | _ => strError env (FunctorRebind loc))
adamc@76 2413 | _ => ();
adamc@76 2414
adamc@88 2415 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
adamc@76 2416 end
adamc@76 2417
adamc@76 2418 | L.DFfiStr (x, sgn) =>
adamc@76 2419 let
adamc@83 2420 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2421
adamc@76 2422 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2423 in
adamc@88 2424 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2425 end
adamc@76 2426
adamc@76 2427 | L.DOpen (m, ms) =>
adamc@88 2428 (case E.lookupStr env m of
adamc@88 2429 NONE => (strError env (UnboundStr (loc, m));
adamc@88 2430 ([], (env, denv, [])))
adamc@88 2431 | SOME (n, sgn) =>
adamc@88 2432 let
adamc@88 2433 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 2434 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 2435 NONE => (strError env (UnboundStr (loc, m));
adamc@88 2436 (strerror, sgnerror))
adamc@88 2437 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 2438 ((L'.StrVar n, loc), sgn) ms
adamc@83 2439
adamc@88 2440 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
adamc@88 2441 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
adamc@88 2442 in
adamc@88 2443 (ds, (env', denv', []))
adamc@88 2444 end)
adamc@88 2445
adamc@88 2446 | L.DConstraint (c1, c2) =>
adamc@88 2447 let
adamc@88 2448 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 2449 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@90 2450 val gs3 = D.prove env denv (c1', c2', loc)
adamc@88 2451
adamc@90 2452 val (denv', gs4) = D.assert env denv (c1', c2')
adamc@88 2453 in
adamc@88 2454 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 2455 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 2456
adamc@90 2457 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', gs1 @ gs2 @ gs3 @ gs4))
adamc@88 2458 end
adamc@88 2459
adamc@88 2460 | L.DOpenConstraints (m, ms) =>
adamc@88 2461 let
adamc@88 2462 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@88 2463 in
adamc@88 2464 ([], (env, denv, []))
adamc@88 2465 end
adamc@3 2466
adamc@109 2467 | L.DExport str =>
adamc@100 2468 let
adamc@109 2469 val (str', sgn, gs) = elabStr (env, denv) str
adamc@100 2470
adamc@109 2471 val sgn =
adamc@109 2472 case #1 (hnormSgn env sgn) of
adamc@109 2473 L'.SgnConst sgis =>
adamc@109 2474 let
adamc@146 2475 fun doOne (all as (sgi, _), env) =
adamc@146 2476 (case sgi of
adamc@146 2477 L'.SgiVal (x, n, t) =>
adamc@146 2478 (case hnormCon (env, denv) t of
adamc@146 2479 ((L'.TFun (dom, ran), _), []) =>
adamc@146 2480 (case (hnormCon (env, denv) dom, hnormCon (env, denv) ran) of
adamc@146 2481 (((L'.TRecord domR, _), []),
adamc@146 2482 ((L'.CApp (tf, arg3), _), [])) =>
adamc@146 2483 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
adamc@146 2484 (((L'.CApp (tf, arg2), _), []),
adamc@146 2485 (((L'.CRecord (_, []), _), []))) =>
adamc@146 2486 (case (hnormCon (env, denv) tf) of
adamc@146 2487 ((L'.CApp (tf, arg1), _), []) =>
adamc@146 2488 (case (hnormCon (env, denv) tf,
adamc@146 2489 hnormCon (env, denv) domR,
adamc@152 2490 hnormCon (env, denv) arg1,
adamc@146 2491 hnormCon (env, denv) arg2) of
adamc@152 2492 ((tf, []), (domR, []), (arg1, []),
adamc@146 2493 ((L'.CRecord (_, []), _), [])) =>
adamc@146 2494 let
adamc@146 2495 val t = (L'.CApp (tf, arg1), loc)
adamc@146 2496 val t = (L'.CApp (t, arg2), loc)
adamc@146 2497 val t = (L'.CApp (t, arg3), loc)
adamc@146 2498 in
adamc@146 2499 (L'.SgiVal (x, n, (L'.TFun ((L'.TRecord domR, loc),
adamc@146 2500 t),
adamc@146 2501 loc)), loc)
adamc@146 2502 end
adamc@146 2503 | _ => all)
adamc@146 2504 | _ => all)
adamc@146 2505 | _ => all)
adamc@146 2506 | _ => all)
adamc@146 2507 | _ => all)
adamc@146 2508 | _ => all,
adamc@146 2509 E.sgiBinds env all)
adamc@109 2510 in
adamc@146 2511 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@109 2512 end
adamc@109 2513 | _ => sgn
adamc@100 2514 in
adamc@109 2515 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs))
adamc@100 2516 end
adamc@100 2517
adamc@83 2518 and elabStr (env, denv) (str, loc) =
adamc@31 2519 case str of
adamc@31 2520 L.StrConst ds =>
adamc@31 2521 let
adamc@88 2522 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 2523 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 2524
adamc@63 2525 val (sgis, _, _, _, _) =
adamc@64 2526 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 2527 case sgi of
adamc@64 2528 L'.SgiConAbs (x, n, k) =>
adamc@64 2529 let
adamc@64 2530 val (cons, x) =
adamc@64 2531 if SS.member (cons, x) then
adamc@64 2532 (cons, "?" ^ x)
adamc@64 2533 else
adamc@64 2534 (SS.add (cons, x), x)
adamc@64 2535 in
adamc@64 2536 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2537 end
adamc@64 2538 | L'.SgiCon (x, n, k, c) =>
adamc@64 2539 let
adamc@64 2540 val (cons, x) =
adamc@64 2541 if SS.member (cons, x) then
adamc@64 2542 (cons, "?" ^ x)
adamc@64 2543 else
adamc@64 2544 (SS.add (cons, x), x)
adamc@64 2545 in
adamc@64 2546 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2547 end
adamc@156 2548 | L'.SgiDatatype (x, n, xncs) =>
adamc@156 2549 let
adamc@156 2550 val (cons, x) =
adamc@156 2551 if SS.member (cons, x) then
adamc@156 2552 (cons, "?" ^ x)
adamc@156 2553 else
adamc@156 2554 (SS.add (cons, x), x)
adamc@156 2555
adamc@156 2556 val (xncs, vals) =
adamc@156 2557 ListUtil.foldlMap
adamc@156 2558 (fn ((x, n, t), vals) =>
adamc@156 2559 if SS.member (vals, x) then
adamc@156 2560 (("?" ^ x, n, t), vals)
adamc@156 2561 else
adamc@156 2562 ((x, n, t), SS.add (vals, x)))
adamc@156 2563 vals xncs
adamc@156 2564 in
adamc@156 2565 ((L'.SgiDatatype (x, n, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 2566 end
adamc@162 2567 | L'.SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
adamc@156 2568 let
adamc@156 2569 val (cons, x) =
adamc@156 2570 if SS.member (cons, x) then
adamc@156 2571 (cons, "?" ^ x)
adamc@156 2572 else
adamc@156 2573 (SS.add (cons, x), x)
adamc@156 2574 in
adamc@162 2575 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 2576 end
adamc@64 2577 | L'.SgiVal (x, n, c) =>
adamc@64 2578 let
adamc@64 2579 val (vals, x) =
adamc@64 2580 if SS.member (vals, x) then
adamc@64 2581 (vals, "?" ^ x)
adamc@64 2582 else
adamc@64 2583 (SS.add (vals, x), x)
adamc@64 2584 in
adamc@64 2585 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2586 end
adamc@64 2587 | L'.SgiSgn (x, n, sgn) =>
adamc@64 2588 let
adamc@64 2589 val (sgns, x) =
adamc@64 2590 if SS.member (sgns, x) then
adamc@64 2591 (sgns, "?" ^ x)
adamc@64 2592 else
adamc@64 2593 (SS.add (sgns, x), x)
adamc@64 2594 in
adamc@64 2595 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2596 end
adamc@64 2597
adamc@64 2598 | L'.SgiStr (x, n, sgn) =>
adamc@64 2599 let
adamc@64 2600 val (strs, x) =
adamc@64 2601 if SS.member (strs, x) then
adamc@64 2602 (strs, "?" ^ x)
adamc@64 2603 else
adamc@64 2604 (SS.add (strs, x), x)
adamc@64 2605 in
adamc@64 2606 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 2607 end
adamc@88 2608 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs))
adamc@64 2609
adamc@63 2610 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 2611 in
adamc@83 2612 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 2613 end
adamc@31 2614 | L.StrVar x =>
adamc@31 2615 (case E.lookupStr env x of
adamc@31 2616 NONE =>
adamc@31 2617 (strError env (UnboundStr (loc, x));
adamc@83 2618 (strerror, sgnerror, []))
adamc@83 2619 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 2620 | L.StrProj (str, x) =>
adamc@34 2621 let
adamc@83 2622 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 2623 in
adamc@34 2624 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 2625 NONE => (strError env (UnboundStr (loc, x));
adamc@83 2626 (strerror, sgnerror, []))
adamc@83 2627 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 2628 end
adamc@41 2629 | L.StrFun (m, dom, ranO, str) =>
adamc@41 2630 let
adamc@83 2631 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2632 val (env', n) = E.pushStrNamed env m dom'
adamc@83 2633 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 2634
adamc@83 2635 val (formal, gs3) =
adamc@41 2636 case ranO of
adamc@83 2637 NONE => (actual, [])
adamc@41 2638 | SOME ran =>
adamc@41 2639 let
adamc@83 2640 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 2641 in
adamc@86 2642 subSgn (env', denv) actual ran';
adamc@83 2643 (ran', gs)
adamc@41 2644 end
adamc@41 2645 in
adamc@41 2646 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 2647 (L'.SgnFun (m, n, dom', formal), loc),
adamc@83 2648 gs1 @ gs2 @ gs3)
adamc@41 2649 end
adamc@44 2650 | L.StrApp (str1, str2) =>
adamc@44 2651 let
adamc@83 2652 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@83 2653 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 2654 in
adamc@44 2655 case #1 (hnormSgn env sgn1) of
adamc@83 2656 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2657 | L'.SgnFun (m, n, dom, ran) =>
adamc@86 2658 (subSgn (env, denv) sgn2 dom;
adamc@44 2659 case #1 (hnormSgn env ran) of
adamc@83 2660 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2661 | L'.SgnConst sgis =>
adamc@44 2662 ((L'.StrApp (str1', str2'), loc),
adamc@83 2663 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 2664 gs1 @ gs2)
adamc@44 2665 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 2666 | _ => (strError env (NotFunctor sgn1);
adamc@83 2667 (strerror, sgnerror, []))
adamc@44 2668 end
adamc@31 2669
adamc@56 2670 fun elabFile basis env file =
adamc@56 2671 let
adamc@83 2672 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 2673 val () = case gs of
adamc@83 2674 [] => ()
adamc@83 2675 | _ => raise Fail "Unresolved disjointness constraints in Basis"
adamc@83 2676
adamc@56 2677 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@56 2678
adamc@88 2679 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
adamc@56 2680
adamc@56 2681 fun discoverC r x =
adamc@56 2682 case E.lookupC env' x of
adamc@56 2683 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 2684 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 2685 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 2686
adamc@56 2687 val () = discoverC int "int"
adamc@56 2688 val () = discoverC float "float"
adamc@56 2689 val () = discoverC string "string"
adamc@56 2690
adamc@83 2691 fun elabDecl' (d, (env, gs)) =
adamc@76 2692 let
adamc@76 2693 val () = resetKunif ()
adamc@76 2694 val () = resetCunif ()
adamc@88 2695 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 2696 in
adamc@76 2697 if ErrorMsg.anyErrors () then
adamc@76 2698 ()
adamc@76 2699 else (
adamc@76 2700 if List.exists kunifsInDecl ds then
adamc@91 2701 declError env (KunifsRemain ds)
adamc@76 2702 else
adamc@76 2703 ();
adamc@76 2704
adamc@76 2705 case ListUtil.search cunifsInDecl ds of
adamc@76 2706 NONE => ()
adamc@76 2707 | SOME loc =>
adamc@91 2708 declError env (CunifsRemain ds)
adamc@76 2709 );
adamc@76 2710
adamc@83 2711 (ds, (env, gs))
adamc@76 2712 end
adamc@76 2713
adamc@83 2714 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 2715 in
adamc@84 2716 if ErrorMsg.anyErrors () then
adamc@84 2717 ()
adamc@84 2718 else
adamc@90 2719 app (fn (loc, env, denv, c1, c2) =>
adamc@84 2720 case D.prove env denv (c1, c2, loc) of
adamc@84 2721 [] => ()
adamc@84 2722 | _ =>
adamc@86 2723 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adamc@84 2724 eprefaces' [("Con 1", p_con env c1),
adamc@90 2725 ("Con 2", p_con env c2),
adamc@90 2726 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
adamc@90 2727 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))])) gs;
adamc@83 2728
adamc@56 2729 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
adamc@56 2730 end
adamc@2 2731
adamc@2 2732 end