annotate src/elaborate.sml @ 156:34ccd7d2bea8

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