annotate src/elaborate.sml @ 89:d3ee072fa609

Tested constraints through functors
author Adam Chlipala <adamc@hcoop.net>
date Tue, 01 Jul 2008 16:06:58 -0400
parents 7bab29834cd6
children 94ef20a31550
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@85 254 val denv' = D.assert env denv (c1', c2')
adamc@85 255 val (c', k, gs3) = 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@85 260 ((L'.TDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3)
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@84 333 val denv' = D.assert env denv (c1', c2')
adamc@84 334 val (c', k, gs3) = 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@84 339 ((L'.CDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3)
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@83 372 map (fn cs => (loc, env, denv, cs)) (D.prove env denv (r1, r2, loc))
adamc@83 373 @ ds
adamc@83 374 end)
adamc@83 375 ds rest
adamc@83 376 in
adamc@83 377 prove (rest, ds)
adamc@83 378 end
adamc@3 379 in
adamc@83 380 (rc, (L'.KRecord k, loc), prove (xcs', gs))
adamc@3 381 end
adamc@3 382 | L.CConcat (c1, c2) =>
adamc@3 383 let
adamc@83 384 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@83 385 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@76 386 val ku = kunif loc
adamc@3 387 val k = (L'.KRecord ku, loc)
adamc@3 388 in
adamc@5 389 checkKind env c1' k1 k;
adamc@5 390 checkKind env c2' k2 k;
adamc@83 391 ((L'.CConcat (c1', c2'), loc), k,
adamc@83 392 map (fn cs => (loc, env, denv, cs)) (D.prove env denv (c1', c2', loc)) @ gs1 @ gs2)
adamc@3 393 end
adamc@67 394 | L.CFold =>
adamc@67 395 let
adamc@76 396 val dom = kunif loc
adamc@76 397 val ran = kunif loc
adamc@67 398 in
adamc@67 399 ((L'.CFold (dom, ran), loc),
adamc@83 400 foldKind (dom, ran, loc),
adamc@83 401 [])
adamc@67 402 end
adamc@3 403
adamc@83 404 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
adamc@82 405
adamc@18 406 | L.CWild k =>
adamc@18 407 let
adamc@18 408 val k' = elabKind k
adamc@18 409 in
adamc@83 410 (cunif (loc, k'), k', [])
adamc@18 411 end
adamc@18 412
adamc@6 413 fun kunifsRemain k =
adamc@6 414 case k of
adamc@76 415 L'.KUnif (_, _, ref NONE) => true
adamc@6 416 | _ => false
adamc@10 417 fun cunifsRemain c =
adamc@10 418 case c of
adamc@76 419 L'.CUnif (loc, _, _, ref NONE) => SOME loc
adamc@76 420 | _ => NONE
adamc@6 421
adamc@76 422 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
adamc@76 423 con = fn _ => false,
adamc@76 424 exp = fn _ => false,
adamc@76 425 sgn_item = fn _ => false,
adamc@76 426 sgn = fn _ => false,
adamc@76 427 str = fn _ => false,
adamc@76 428 decl = fn _ => false}
adamc@10 429
adamc@76 430 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
adamc@76 431 con = cunifsRemain,
adamc@76 432 exp = fn _ => NONE,
adamc@76 433 sgn_item = fn _ => NONE,
adamc@76 434 sgn = fn _ => NONE,
adamc@76 435 str = fn _ => NONE,
adamc@76 436 decl = fn _ => NONE}
adamc@10 437
adamc@10 438 fun occursCon r =
adamc@10 439 U.Con.exists {kind = fn _ => false,
adamc@76 440 con = fn L'.CUnif (_, _, _, r') => r = r'
adamc@10 441 | _ => false}
adamc@10 442
adamc@10 443 datatype cunify_error =
adamc@10 444 CKind of L'.kind * L'.kind * kunify_error
adamc@10 445 | COccursCheckFailed of L'.con * L'.con
adamc@10 446 | CIncompatible of L'.con * L'.con
adamc@10 447 | CExplicitness of L'.con * L'.con
adamc@12 448 | CKindof of L'.con
adamc@12 449 | CRecordFailure
adamc@10 450
adamc@10 451 exception CUnify' of cunify_error
adamc@10 452
adamc@10 453 fun cunifyError env err =
adamc@10 454 case err of
adamc@10 455 CKind (k1, k2, kerr) =>
adamc@10 456 (eprefaces "Kind unification failure"
adamc@10 457 [("Kind 1", p_kind k1),
adamc@10 458 ("Kind 2", p_kind k2)];
adamc@10 459 kunifyError kerr)
adamc@10 460 | COccursCheckFailed (c1, c2) =>
adamc@10 461 eprefaces "Constructor occurs check failed"
adamc@10 462 [("Con 1", p_con env c1),
adamc@10 463 ("Con 2", p_con env c2)]
adamc@10 464 | CIncompatible (c1, c2) =>
adamc@10 465 eprefaces "Incompatible constructors"
adamc@10 466 [("Con 1", p_con env c1),
adamc@10 467 ("Con 2", p_con env c2)]
adamc@10 468 | CExplicitness (c1, c2) =>
adamc@10 469 eprefaces "Differing constructor function explicitness"
adamc@10 470 [("Con 1", p_con env c1),
adamc@10 471 ("Con 2", p_con env c2)]
adamc@12 472 | CKindof c =>
adamc@12 473 eprefaces "Kind unification variable blocks kindof calculation"
adamc@12 474 [("Con", p_con env c)]
adamc@12 475 | CRecordFailure =>
adamc@12 476 eprefaces "Can't unify record constructors" []
adamc@10 477
adamc@13 478 exception SynUnif = E.SynUnif
adamc@12 479
adamc@81 480 open ElabOps
adamc@43 481
adamc@12 482 type record_summary = {
adamc@12 483 fields : (L'.con * L'.con) list,
adamc@12 484 unifs : (L'.con * L'.con option ref) list,
adamc@12 485 others : L'.con list
adamc@12 486 }
adamc@12 487
adamc@12 488 fun summaryToCon {fields, unifs, others} =
adamc@12 489 let
adamc@12 490 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 491 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 492 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 493 in
adamc@12 494 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 495 end
adamc@12 496
adamc@12 497 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 498
adamc@12 499 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 500
adamc@12 501 fun hnormKind (kAll as (k, _)) =
adamc@12 502 case k of
adamc@76 503 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@12 504 | _ => kAll
adamc@12 505
adamc@12 506 fun kindof env (c, loc) =
adamc@12 507 case c of
adamc@12 508 L'.TFun _ => ktype
adamc@12 509 | L'.TCFun _ => ktype
adamc@85 510 | L'.TDisjoint _ => ktype
adamc@12 511 | L'.TRecord _ => ktype
adamc@12 512
adamc@12 513 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 514 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@34 515 | L'.CModProj (n, ms, x) =>
adamc@34 516 let
adamc@34 517 val (_, sgn) = E.lookupStrNamed env n
adamc@34 518 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 519 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 520 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 521 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 522 ((L'.StrVar n, loc), sgn) ms
adamc@34 523 in
adamc@34 524 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@34 525 NONE => raise Fail "kindof: Unknown con in structure"
adamc@34 526 | SOME (k, _) => k
adamc@34 527 end
adamc@34 528
adamc@12 529 | L'.CApp (c, _) =>
adamc@12 530 (case #1 (hnormKind (kindof env c)) of
adamc@12 531 L'.KArrow (_, k) => k
adamc@12 532 | L'.KError => kerror
adamc@12 533 | _ => raise CUnify' (CKindof c))
adamc@12 534 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@84 535 | L'.CDisjoint (_, _, c) => kindof env c
adamc@12 536
adamc@12 537 | L'.CName _ => kname
adamc@12 538
adamc@12 539 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 540 | L'.CConcat (c, _) => kindof env c
adamc@67 541 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
adamc@12 542
adamc@82 543 | L'.CUnit => (L'.KUnit, loc)
adamc@82 544
adamc@12 545 | L'.CError => kerror
adamc@76 546 | L'.CUnif (_, k, _, _) => k
adamc@12 547
adamc@86 548 fun hnormCon (env, denv) c =
adamc@86 549 let
adamc@86 550 val cAll as (c, loc) = ElabOps.hnormCon env c
adamc@86 551
adamc@86 552 fun doDisj (c1, c2, c) =
adamc@86 553 let
adamc@86 554 val (c, gs) = hnormCon (env, denv) c
adamc@86 555 in
adamc@86 556 (c,
adamc@86 557 map (fn cs => (loc, env, denv, cs)) (D.prove env denv (c1, c2, loc)) @ gs)
adamc@86 558 end
adamc@86 559 in
adamc@86 560 case c of
adamc@86 561 L'.CDisjoint cs => doDisj cs
adamc@86 562 | L'.TDisjoint cs => doDisj cs
adamc@86 563 | _ => (cAll, [])
adamc@86 564 end
adamc@86 565
adamc@86 566 fun unifyRecordCons (env, denv) (c1, c2) =
adamc@12 567 let
adamc@12 568 val k1 = kindof env c1
adamc@12 569 val k2 = kindof env 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@76 658 val cr' = (L'.CUnif (dummy, k, "recd", r'), dummy)
adamc@12 659
adamc@12 660 val prefix = case (fs, others) of
adamc@12 661 ([], other :: others) =>
adamc@12 662 List.foldl (fn (other, c) =>
adamc@12 663 (L'.CConcat (c, other), dummy))
adamc@12 664 other others
adamc@12 665 | (fs, []) =>
adamc@12 666 (L'.CRecord (k, fs), dummy)
adamc@12 667 | (fs, others) =>
adamc@12 668 List.foldl (fn (other, c) =>
adamc@12 669 (L'.CConcat (c, other), dummy))
adamc@12 670 (L'.CRecord (k, fs), dummy) others
adamc@12 671 in
adamc@12 672 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 673 ([], [], (cr', r') :: rest)
adamc@12 674 end
adamc@12 675
adamc@12 676 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 677 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 678
adamc@86 679 val clear = case (fs1, others1, fs2, others2) of
adamc@86 680 ([], [], [], []) => true
adamc@12 681 | _ => false
adamc@12 682 val empty = (L'.CRecord (k, []), dummy)
adamc@12 683 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 684 case (unifs1, unifs2) of
adamc@12 685 ([], _) =>
adamc@86 686 if clear then
adamc@12 687 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 688 else
adamc@12 689 raise CUnify' CRecordFailure
adamc@12 690 | (_, []) =>
adamc@86 691 if clear then
adamc@12 692 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 693 else
adamc@12 694 raise CUnify' CRecordFailure
adamc@12 695 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 696 (r2 := SOME c1;
adamc@12 697 pairOffUnifs (rest1, rest2))
adamc@12 698 in
adamc@12 699 pairOffUnifs (unifs1, unifs2)
adamc@12 700 end
adamc@12 701
adamc@86 702 and unifyCons' (env, denv) c1 c2 =
adamc@86 703 let
adamc@86 704 val (c1, gs1) = hnormCon (env, denv) c1
adamc@86 705 val (c2, gs2) = hnormCon (env, denv) c2
adamc@86 706 in
adamc@86 707 unifyCons'' (env, denv) c1 c2;
adamc@86 708 gs1 @ gs2
adamc@86 709 end
adamc@11 710
adamc@86 711 and unifyCons'' (env, denv) (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 712 let
adamc@10 713 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 714
adamc@86 715 fun isRecord () = unifyRecordCons (env, denv) (c1All, c2All)
adamc@10 716 in
adamc@10 717 case (c1, c2) of
adamc@88 718 (L'.CUnit, L'.CUnit) => []
adamc@88 719
adamc@88 720 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@86 721 unifyCons' (env, denv) d1 d2
adamc@86 722 @ unifyCons' (env, denv) r1 r2
adamc@10 723 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 724 if expl1 <> expl2 then
adamc@10 725 err CExplicitness
adamc@10 726 else
adamc@10 727 (unifyKinds d1 d2;
adamc@86 728 unifyCons' (E.pushCRel env x1 d1, D.enter denv) r1 r2)
adamc@86 729 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' (env, denv) r1 r2
adamc@10 730
adamc@10 731 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 732 if n1 = n2 then
adamc@86 733 []
adamc@10 734 else
adamc@10 735 err CIncompatible
adamc@10 736 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 737 if n1 = n2 then
adamc@86 738 []
adamc@10 739 else
adamc@10 740 err CIncompatible
adamc@10 741
adamc@10 742 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@86 743 (unifyCons' (env, denv) d1 d2;
adamc@86 744 unifyCons' (env, denv) r1 r2)
adamc@10 745 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 746 (unifyKinds k1 k2;
adamc@86 747 unifyCons' (E.pushCRel env x1 k1, D.enter denv) c1 c2)
adamc@10 748
adamc@10 749 | (L'.CName n1, L'.CName n2) =>
adamc@10 750 if n1 = n2 then
adamc@86 751 []
adamc@10 752 else
adamc@10 753 err CIncompatible
adamc@10 754
adamc@34 755 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@34 756 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@86 757 []
adamc@34 758 else
adamc@34 759 err CIncompatible
adamc@34 760
adamc@86 761 | (L'.CError, _) => []
adamc@86 762 | (_, L'.CError) => []
adamc@10 763
adamc@86 764 | (L'.CUnif (_, _, _, ref (SOME c1All)), _) => unifyCons' (env, denv) c1All c2All
adamc@86 765 | (_, L'.CUnif (_, _, _, ref (SOME c2All))) => unifyCons' (env, denv) c1All c2All
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 | (L'.CRecord _, _) => isRecord ()
adamc@12 789 | (_, L'.CRecord _) => isRecord ()
adamc@12 790 | (L'.CConcat _, _) => isRecord ()
adamc@12 791 | (_, L'.CConcat _) => isRecord ()
adamc@12 792
adamc@71 793 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
adamc@71 794 (unifyKinds dom1 dom2;
adamc@86 795 unifyKinds ran1 ran2;
adamc@86 796 [])
adamc@71 797
adamc@10 798 | _ => err CIncompatible
adamc@10 799 end
adamc@10 800
adamc@86 801 and unifyCons (env, denv) c1 c2 =
adamc@86 802 unifyCons' (env, denv) c1 c2
adamc@10 803 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 804 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 805
adamc@10 806 datatype exp_error =
adamc@10 807 UnboundExp of ErrorMsg.span * string
adamc@34 808 | UnboundStrInExp of ErrorMsg.span * string
adamc@10 809 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 810 | Unif of string * L'.con
adamc@11 811 | WrongForm of string * L'.exp * L'.con
adamc@10 812
adamc@10 813 fun expError env err =
adamc@10 814 case err of
adamc@10 815 UnboundExp (loc, s) =>
adamc@10 816 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@34 817 | UnboundStrInExp (loc, s) =>
adamc@34 818 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@10 819 | Unify (e, c1, c2, uerr) =>
adamc@10 820 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 821 eprefaces' [("Expression", p_exp env e),
adamc@10 822 ("Have con", p_con env c1),
adamc@10 823 ("Need con", p_con env c2)];
adamc@10 824 cunifyError env uerr)
adamc@11 825 | Unif (action, c) =>
adamc@11 826 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 827 eprefaces' [("Con", p_con env c)])
adamc@11 828 | WrongForm (variety, e, t) =>
adamc@11 829 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 830 eprefaces' [("Expression", p_exp env e),
adamc@11 831 ("Type", p_con env t)])
adamc@10 832
adamc@86 833 fun checkCon (env, denv) e c1 c2 =
adamc@86 834 unifyCons (env, denv) c1 c2
adamc@10 835 handle CUnify (c1, c2, err) =>
adamc@86 836 (expError env (Unify (e, c1, c2, err));
adamc@86 837 [])
adamc@10 838
adamc@14 839 fun primType env p =
adamc@56 840 case p of
adamc@56 841 P.Int _ => !int
adamc@56 842 | P.Float _ => !float
adamc@56 843 | P.String _ => !string
adamc@14 844
adamc@71 845 fun recCons (k, nm, v, rest, loc) =
adamc@71 846 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
adamc@71 847 rest), loc)
adamc@71 848
adamc@71 849 fun foldType (dom, loc) =
adamc@71 850 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
adamc@71 851 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
adamc@71 852 (L'.TCFun (L'.Explicit, "v", dom,
adamc@71 853 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
adamc@71 854 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
adamc@71 855 (L'.CApp ((L'.CRel 3, loc),
adamc@71 856 recCons (dom,
adamc@71 857 (L'.CRel 2, loc),
adamc@71 858 (L'.CRel 1, loc),
adamc@71 859 (L'.CRel 0, loc),
adamc@71 860 loc)), loc)), loc)),
adamc@71 861 loc)), loc)), loc),
adamc@71 862 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
adamc@71 863 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
adamc@71 864 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
adamc@71 865 loc)), loc)), loc)
adamc@71 866
adamc@15 867 fun typeof env (e, loc) =
adamc@15 868 case e of
adamc@56 869 L'.EPrim p => primType env p
adamc@15 870 | L'.ERel n => #2 (E.lookupERel env n)
adamc@15 871 | L'.ENamed n => #2 (E.lookupENamed env n)
adamc@34 872 | L'.EModProj (n, ms, x) =>
adamc@34 873 let
adamc@34 874 val (_, sgn) = E.lookupStrNamed env n
adamc@34 875 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 876 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 877 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 878 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 879 ((L'.StrVar n, loc), sgn) ms
adamc@34 880 in
adamc@34 881 case E.projectVal env {sgn = sgn, str = str, field = x} of
adamc@34 882 NONE => raise Fail "typeof: Unknown val in structure"
adamc@34 883 | SOME t => t
adamc@34 884 end
adamc@34 885
adamc@15 886 | L'.EApp (e1, _) =>
adamc@15 887 (case #1 (typeof env e1) of
adamc@15 888 L'.TFun (_, c) => c
adamc@15 889 | _ => raise Fail "typeof: Bad EApp")
adamc@26 890 | L'.EAbs (_, _, ran, _) => ran
adamc@15 891 | L'.ECApp (e1, c) =>
adamc@15 892 (case #1 (typeof env e1) of
adamc@15 893 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
adamc@15 894 | _ => raise Fail "typeof: Bad ECApp")
adamc@15 895 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
adamc@15 896
adamc@29 897 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
adamc@15 898 | L'.EField (_, _, {field, ...}) => field
adamc@71 899 | L'.EFold dom => foldType (dom, loc)
adamc@15 900
adamc@15 901 | L'.EError => cerror
adamc@15 902
adamc@86 903 fun elabHead (env, denv) (e as (_, loc)) t =
adamc@15 904 let
adamc@15 905 fun unravel (t, e) =
adamc@86 906 let
adamc@86 907 val (t, gs) = hnormCon (env, denv) t
adamc@86 908 in
adamc@86 909 case t of
adamc@86 910 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@86 911 let
adamc@86 912 val u = cunif (loc, k)
adamc@86 913
adamc@86 914 val (e, t, gs') = unravel (subConInCon (0, u) t',
adamc@86 915 (L'.ECApp (e, u), loc))
adamc@86 916 in
adamc@86 917 (e, t, gs @ gs')
adamc@86 918 end
adamc@86 919 | _ => (e, t, gs)
adamc@86 920 end
adamc@15 921 in
adamc@15 922 unravel (t, e)
adamc@15 923 end
adamc@15 924
adamc@83 925 fun elabExp (env, denv) (e, loc) =
adamc@10 926 case e of
adamc@10 927 L.EAnnot (e, t) =>
adamc@10 928 let
adamc@83 929 val (e', et, gs1) = elabExp (env, denv) e
adamc@83 930 val (t', _, gs2) = elabCon (env, denv) t
adamc@86 931 val gs3 = checkCon (env, denv) e' et t'
adamc@10 932 in
adamc@86 933 (e', t', gs1 @ gs2 @ gs3)
adamc@10 934 end
adamc@10 935
adamc@83 936 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@34 937 | L.EVar ([], s) =>
adamc@10 938 (case E.lookupE env s of
adamc@10 939 E.NotBound =>
adamc@10 940 (expError env (UnboundExp (loc, s));
adamc@83 941 (eerror, cerror, []))
adamc@83 942 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
adamc@83 943 | E.Named (n, t) => ((L'.ENamed n, loc), t, []))
adamc@34 944 | L.EVar (m1 :: ms, s) =>
adamc@34 945 (case E.lookupStr env m1 of
adamc@42 946 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@83 947 (eerror, cerror, []))
adamc@34 948 | SOME (n, sgn) =>
adamc@34 949 let
adamc@34 950 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 951 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 952 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@34 953 (strerror, sgnerror))
adamc@34 954 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 955 ((L'.StrVar n, loc), sgn) ms
adamc@34 956
adamc@34 957 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@34 958 NONE => (expError env (UnboundExp (loc, s));
adamc@34 959 cerror)
adamc@34 960 | SOME t => t
adamc@34 961 in
adamc@83 962 ((L'.EModProj (n, ms, s), loc), t, [])
adamc@34 963 end)
adamc@34 964
adamc@10 965 | L.EApp (e1, e2) =>
adamc@10 966 let
adamc@83 967 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@86 968 val (e1', t1, gs2) = elabHead (env, denv) e1' t1
adamc@86 969 val (e2', t2, gs3) = elabExp (env, denv) e2
adamc@10 970
adamc@76 971 val dom = cunif (loc, ktype)
adamc@76 972 val ran = cunif (loc, ktype)
adamc@10 973 val t = (L'.TFun (dom, ran), dummy)
adamc@86 974
adamc@86 975 val gs4 = checkCon (env, denv) e1' t1 t
adamc@86 976 val gs5 = checkCon (env, denv) e2' t2 dom
adamc@10 977 in
adamc@86 978 ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2 @ gs3 @ gs4 @ gs5)
adamc@10 979 end
adamc@10 980 | L.EAbs (x, to, e) =>
adamc@10 981 let
adamc@83 982 val (t', gs1) = case to of
adamc@83 983 NONE => (cunif (loc, ktype), [])
adamc@83 984 | SOME t =>
adamc@83 985 let
adamc@83 986 val (t', tk, gs) = elabCon (env, denv) t
adamc@83 987 in
adamc@83 988 checkKind env t' tk ktype;
adamc@83 989 (t', gs)
adamc@83 990 end
adamc@83 991 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
adamc@10 992 in
adamc@26 993 ((L'.EAbs (x, t', et, e'), loc),
adamc@83 994 (L'.TFun (t', et), loc),
adamc@83 995 gs1 @ gs2)
adamc@10 996 end
adamc@10 997 | L.ECApp (e, c) =>
adamc@10 998 let
adamc@83 999 val (e', et, gs1) = elabExp (env, denv) e
adamc@86 1000 val (e', et, gs2) = elabHead (env, denv) e' et
adamc@86 1001 val (c', ck, gs3) = elabCon (env, denv) c
adamc@86 1002 val ((et', _), gs4) = hnormCon (env, denv) et
adamc@10 1003 in
adamc@86 1004 case et' of
adamc@83 1005 L'.CError => (eerror, cerror, [])
adamc@11 1006 | L'.TCFun (_, _, k, eb) =>
adamc@11 1007 let
adamc@11 1008 val () = checkKind env c' ck k
adamc@11 1009 val eb' = subConInCon (0, c') eb
adamc@11 1010 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@11 1011 cerror)
adamc@11 1012 in
adamc@86 1013 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2 @ gs3 @ gs4)
adamc@11 1014 end
adamc@11 1015
adamc@11 1016 | L'.CUnif _ =>
adamc@11 1017 (expError env (Unif ("application", et));
adamc@83 1018 (eerror, cerror, []))
adamc@11 1019
adamc@11 1020 | _ =>
adamc@11 1021 (expError env (WrongForm ("constructor function", e', et));
adamc@83 1022 (eerror, cerror, []))
adamc@10 1023 end
adamc@11 1024 | L.ECAbs (expl, x, k, e) =>
adamc@11 1025 let
adamc@11 1026 val expl' = elabExplicitness expl
adamc@11 1027 val k' = elabKind k
adamc@83 1028 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
adamc@11 1029 in
adamc@11 1030 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@83 1031 (L'.TCFun (expl', x, k', et), loc),
adamc@83 1032 gs)
adamc@11 1033 end
adamc@6 1034
adamc@85 1035 | L.EDisjoint (c1, c2, e) =>
adamc@85 1036 let
adamc@85 1037 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@85 1038 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@85 1039
adamc@85 1040 val ku1 = kunif loc
adamc@85 1041 val ku2 = kunif loc
adamc@85 1042
adamc@85 1043 val denv' = D.assert env denv (c1', c2')
adamc@85 1044 val (e', t, gs3) = elabExp (env, denv') e
adamc@85 1045 in
adamc@85 1046 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@85 1047 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@85 1048
adamc@85 1049 (e', (L'.TDisjoint (c1', c2', t), loc), gs1 @ gs2 @ gs3)
adamc@85 1050 end
adamc@85 1051
adamc@12 1052 | L.ERecord xes =>
adamc@12 1053 let
adamc@83 1054 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@83 1055 let
adamc@83 1056 val (x', xk, gs1) = elabCon (env, denv) x
adamc@83 1057 val (e', et, gs2) = elabExp (env, denv) e
adamc@83 1058 in
adamc@83 1059 checkKind env x' xk kname;
adamc@83 1060 ((x', e', et), gs1 @ gs2 @ gs)
adamc@83 1061 end)
adamc@83 1062 [] xes
adamc@86 1063
adamc@86 1064 val k = (L'.KType, loc)
adamc@86 1065
adamc@86 1066 fun prove (xets, gs) =
adamc@86 1067 case xets of
adamc@86 1068 [] => gs
adamc@86 1069 | (x, _, t) :: rest =>
adamc@86 1070 let
adamc@86 1071 val xc = (x, t)
adamc@86 1072 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@86 1073 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@86 1074 let
adamc@86 1075 val xc' = (x', t')
adamc@86 1076 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@86 1077 in
adamc@86 1078 map (fn cs => (loc, env, denv, cs)) (D.prove env denv (r1, r2, loc))
adamc@86 1079 @ gs
adamc@86 1080 end)
adamc@86 1081 gs rest
adamc@86 1082 in
adamc@86 1083 prove (rest, gs)
adamc@86 1084 end
adamc@12 1085 in
adamc@29 1086 ((L'.ERecord xes', loc),
adamc@83 1087 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@86 1088 prove (xes', gs))
adamc@12 1089 end
adamc@12 1090
adamc@12 1091 | L.EField (e, c) =>
adamc@12 1092 let
adamc@83 1093 val (e', et, gs1) = elabExp (env, denv) e
adamc@83 1094 val (c', ck, gs2) = elabCon (env, denv) c
adamc@12 1095
adamc@76 1096 val ft = cunif (loc, ktype)
adamc@76 1097 val rest = cunif (loc, ktype_record)
adamc@86 1098 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@86 1099
adamc@86 1100 val gs3 =
adamc@86 1101 checkCon (env, denv) e' et
adamc@86 1102 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@86 1103 val gs4 =
adamc@86 1104 map (fn cs => (loc, env, denv, cs))
adamc@86 1105 (D.prove env denv (first, rest, loc))
adamc@12 1106 in
adamc@86 1107 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2 @ gs3 @ gs4)
adamc@12 1108 end
adamc@71 1109
adamc@71 1110 | L.EFold =>
adamc@71 1111 let
adamc@76 1112 val dom = kunif loc
adamc@71 1113 in
adamc@83 1114 ((L'.EFold dom, loc), foldType (dom, loc), [])
adamc@71 1115 end
adamc@12 1116
adamc@12 1117
adamc@6 1118 datatype decl_error =
adamc@76 1119 KunifsRemain of ErrorMsg.span
adamc@76 1120 | CunifsRemain of ErrorMsg.span
adamc@6 1121
adamc@6 1122 fun declError env err =
adamc@6 1123 case err of
adamc@76 1124 KunifsRemain loc =>
adamc@76 1125 ErrorMsg.errorAt loc "Some kind unification variables are undetermined in declaration"
adamc@76 1126 | CunifsRemain loc =>
adamc@76 1127 ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in declaration"
adamc@6 1128
adamc@31 1129 datatype sgn_error =
adamc@31 1130 UnboundSgn of ErrorMsg.span * string
adamc@31 1131 | UnmatchedSgi of L'.sgn_item
adamc@31 1132 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 1133 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@41 1134 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 1135 | UnWhereable of L'.sgn * string
adamc@75 1136 | WhereWrongKind of L'.kind * L'.kind * kunify_error
adamc@58 1137 | NotIncludable of L'.sgn
adamc@62 1138 | DuplicateCon of ErrorMsg.span * string
adamc@62 1139 | DuplicateVal of ErrorMsg.span * string
adamc@62 1140 | DuplicateSgn of ErrorMsg.span * string
adamc@62 1141 | DuplicateStr of ErrorMsg.span * string
adamc@88 1142 | NotConstraintsable of L'.sgn
adamc@31 1143
adamc@31 1144 fun sgnError env err =
adamc@31 1145 case err of
adamc@31 1146 UnboundSgn (loc, s) =>
adamc@31 1147 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 1148 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 1149 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 1150 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 1151 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 1152 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@59 1153 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1154 ("Need", p_sgn_item env sgi2),
adamc@31 1155 ("Kind 1", p_kind k1),
adamc@31 1156 ("Kind 2", p_kind k2)];
adamc@31 1157 kunifyError kerr)
adamc@31 1158 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 1159 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@59 1160 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1161 ("Need", p_sgn_item env sgi2),
adamc@31 1162 ("Con 1", p_con env c1),
adamc@31 1163 ("Con 2", p_con env c2)];
adamc@31 1164 cunifyError env cerr)
adamc@41 1165 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1166 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1167 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1168 ("Sig 2", p_sgn env sgn2)])
adamc@42 1169 | UnWhereable (sgn, x) =>
adamc@42 1170 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1171 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1172 ("Field", PD.string x)])
adamc@75 1173 | WhereWrongKind (k1, k2, kerr) =>
adamc@75 1174 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
adamc@75 1175 eprefaces' [("Have", p_kind k1),
adamc@75 1176 ("Need", p_kind k2)];
adamc@75 1177 kunifyError kerr)
adamc@58 1178 | NotIncludable sgn =>
adamc@58 1179 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
adamc@58 1180 eprefaces' [("Signature", p_sgn env sgn)])
adamc@62 1181 | DuplicateCon (loc, s) =>
adamc@62 1182 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
adamc@62 1183 | DuplicateVal (loc, s) =>
adamc@62 1184 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
adamc@62 1185 | DuplicateSgn (loc, s) =>
adamc@62 1186 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
adamc@62 1187 | DuplicateStr (loc, s) =>
adamc@62 1188 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
adamc@88 1189 | NotConstraintsable sgn =>
adamc@88 1190 (ErrorMsg.errorAt (#2 sgn) "Invalid signature for 'open constraints'";
adamc@88 1191 eprefaces' [("Signature", p_sgn env sgn)])
adamc@31 1192
adamc@31 1193 datatype str_error =
adamc@31 1194 UnboundStr of ErrorMsg.span * string
adamc@44 1195 | NotFunctor of L'.sgn
adamc@46 1196 | FunctorRebind of ErrorMsg.span
adamc@61 1197 | UnOpenable of L'.sgn
adamc@75 1198 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
adamc@31 1199
adamc@31 1200 fun strError env err =
adamc@31 1201 case err of
adamc@31 1202 UnboundStr (loc, s) =>
adamc@31 1203 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1204 | NotFunctor sgn =>
adamc@44 1205 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1206 eprefaces' [("Signature", p_sgn env sgn)])
adamc@46 1207 | FunctorRebind loc =>
adamc@46 1208 ErrorMsg.errorAt loc "Attempt to rebind functor"
adamc@61 1209 | UnOpenable sgn =>
adamc@61 1210 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
adamc@61 1211 eprefaces' [("Signature", p_sgn env sgn)])
adamc@75 1212 | NotType (k, (k1, k2, ue)) =>
adamc@75 1213 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
adamc@75 1214 eprefaces' [("Kind", p_kind k),
adamc@75 1215 ("Subkind 1", p_kind k1),
adamc@75 1216 ("Subkind 2", p_kind k2)];
adamc@75 1217 kunifyError ue)
adamc@31 1218
adamc@42 1219 val hnormSgn = E.hnormSgn
adamc@31 1220
adamc@88 1221 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@76 1222 case sgi of
adamc@76 1223 L.SgiConAbs (x, k) =>
adamc@76 1224 let
adamc@76 1225 val k' = elabKind k
adamc@31 1226
adamc@76 1227 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1228 in
adamc@88 1229 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@76 1230 end
adamc@31 1231
adamc@76 1232 | L.SgiCon (x, ko, c) =>
adamc@76 1233 let
adamc@76 1234 val k' = case ko of
adamc@76 1235 NONE => kunif loc
adamc@76 1236 | SOME k => elabKind k
adamc@31 1237
adamc@83 1238 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1239 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1240 in
adamc@76 1241 checkKind env c' ck k';
adamc@31 1242
adamc@88 1243 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1244 end
adamc@31 1245
adamc@76 1246 | L.SgiVal (x, c) =>
adamc@76 1247 let
adamc@83 1248 val (c', ck, gs') = elabCon (env, denv) c
adamc@31 1249
adamc@76 1250 val (env', n) = E.pushENamed env x c'
adamc@76 1251 in
adamc@76 1252 (unifyKinds ck ktype
adamc@76 1253 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 1254
adamc@88 1255 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@76 1256 end
adamc@31 1257
adamc@76 1258 | L.SgiStr (x, sgn) =>
adamc@76 1259 let
adamc@83 1260 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1261 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1262 in
adamc@88 1263 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1264 end
adamc@31 1265
adamc@76 1266 | L.SgiSgn (x, sgn) =>
adamc@76 1267 let
adamc@83 1268 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1269 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1270 in
adamc@88 1271 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1272 end
adamc@31 1273
adamc@76 1274 | L.SgiInclude sgn =>
adamc@76 1275 let
adamc@83 1276 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1277 in
adamc@76 1278 case #1 (hnormSgn env sgn') of
adamc@76 1279 L'.SgnConst sgis =>
adamc@88 1280 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@76 1281 | _ => (sgnError env (NotIncludable sgn');
adamc@88 1282 ([], (env, denv, [])))
adamc@88 1283 end
adamc@88 1284
adamc@88 1285 | L.SgiConstraint (c1, c2) =>
adamc@88 1286 let
adamc@88 1287 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 1288 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@88 1289
adamc@88 1290 val denv = D.assert env denv (c1', c2')
adamc@88 1291 in
adamc@88 1292 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 1293 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 1294
adamc@88 1295 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2))
adamc@76 1296 end
adamc@31 1297
adamc@83 1298 and elabSgn (env, denv) (sgn, loc) =
adamc@31 1299 case sgn of
adamc@31 1300 L.SgnConst sgis =>
adamc@31 1301 let
adamc@88 1302 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 1303
adamc@62 1304 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 1305 case sgi of
adamc@62 1306 L'.SgiConAbs (x, _, _) =>
adamc@62 1307 (if SS.member (cons, x) then
adamc@62 1308 sgnError env (DuplicateCon (loc, x))
adamc@62 1309 else
adamc@62 1310 ();
adamc@62 1311 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1312 | L'.SgiCon (x, _, _, _) =>
adamc@62 1313 (if SS.member (cons, x) then
adamc@62 1314 sgnError env (DuplicateCon (loc, x))
adamc@62 1315 else
adamc@62 1316 ();
adamc@62 1317 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1318 | L'.SgiVal (x, _, _) =>
adamc@62 1319 (if SS.member (vals, x) then
adamc@62 1320 sgnError env (DuplicateVal (loc, x))
adamc@62 1321 else
adamc@62 1322 ();
adamc@62 1323 (cons, SS.add (vals, x), sgns, strs))
adamc@62 1324 | L'.SgiSgn (x, _, _) =>
adamc@62 1325 (if SS.member (sgns, x) then
adamc@62 1326 sgnError env (DuplicateSgn (loc, x))
adamc@62 1327 else
adamc@62 1328 ();
adamc@62 1329 (cons, vals, SS.add (sgns, x), strs))
adamc@62 1330 | L'.SgiStr (x, _, _) =>
adamc@62 1331 (if SS.member (strs, x) then
adamc@62 1332 sgnError env (DuplicateStr (loc, x))
adamc@62 1333 else
adamc@62 1334 ();
adamc@88 1335 (cons, vals, sgns, SS.add (strs, x)))
adamc@88 1336 | L'.SgiConstraint _ => (cons, vals, sgns, strs))
adamc@62 1337 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 1338 in
adamc@83 1339 ((L'.SgnConst sgis', loc), gs)
adamc@31 1340 end
adamc@31 1341 | L.SgnVar x =>
adamc@31 1342 (case E.lookupSgn env x of
adamc@31 1343 NONE =>
adamc@31 1344 (sgnError env (UnboundSgn (loc, x));
adamc@83 1345 ((L'.SgnError, loc), []))
adamc@83 1346 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 1347 | L.SgnFun (m, dom, ran) =>
adamc@41 1348 let
adamc@83 1349 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1350 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1351 val (ran', gs2) = elabSgn (env', denv) ran
adamc@41 1352 in
adamc@83 1353 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 1354 end
adamc@42 1355 | L.SgnWhere (sgn, x, c) =>
adamc@42 1356 let
adamc@83 1357 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 1358 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 1359 in
adamc@42 1360 case #1 (hnormSgn env sgn') of
adamc@83 1361 L'.SgnError => (sgnerror, [])
adamc@42 1362 | L'.SgnConst sgis =>
adamc@75 1363 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 1364 x' = x andalso
adamc@75 1365 (unifyKinds k ck
adamc@75 1366 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 1367 true)
adamc@42 1368 | _ => false) sgis then
adamc@83 1369 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 1370 else
adamc@42 1371 (sgnError env (UnWhereable (sgn', x));
adamc@83 1372 (sgnerror, []))
adamc@42 1373 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 1374 (sgnerror, []))
adamc@42 1375 end
adamc@59 1376 | L.SgnProj (m, ms, x) =>
adamc@59 1377 (case E.lookupStr env m of
adamc@59 1378 NONE => (strError env (UnboundStr (loc, m));
adamc@83 1379 (sgnerror, []))
adamc@59 1380 | SOME (n, sgn) =>
adamc@59 1381 let
adamc@59 1382 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 1383 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 1384 NONE => (strError env (UnboundStr (loc, m));
adamc@59 1385 (strerror, sgnerror))
adamc@59 1386 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 1387 ((L'.StrVar n, loc), sgn) ms
adamc@59 1388 in
adamc@59 1389 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 1390 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 1391 (sgnerror, []))
adamc@83 1392 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 1393 end)
adamc@59 1394
adamc@31 1395
adamc@66 1396 fun selfify env {str, strs, sgn} =
adamc@66 1397 case #1 (hnormSgn env sgn) of
adamc@66 1398 L'.SgnError => sgn
adamc@66 1399 | L'.SgnVar _ => sgn
adamc@66 1400
adamc@66 1401 | L'.SgnConst sgis =>
adamc@66 1402 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 1403 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@66 1404 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 1405 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 1406 | x => x) sgis), #2 sgn)
adamc@66 1407 | L'.SgnFun _ => sgn
adamc@66 1408 | L'.SgnWhere _ => sgn
adamc@66 1409 | L'.SgnProj (m, ms, x) =>
adamc@66 1410 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1411 (L'.StrVar m, #2 sgn) ms,
adamc@66 1412 sgn = #2 (E.lookupStrNamed env m),
adamc@66 1413 field = x} of
adamc@66 1414 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 1415 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 1416
adamc@66 1417 fun selfifyAt env {str, sgn} =
adamc@66 1418 let
adamc@66 1419 fun self (str, _) =
adamc@66 1420 case str of
adamc@66 1421 L'.StrVar x => SOME (x, [])
adamc@66 1422 | L'.StrProj (str, x) =>
adamc@66 1423 (case self str of
adamc@66 1424 NONE => NONE
adamc@66 1425 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 1426 | _ => NONE
adamc@66 1427 in
adamc@66 1428 case self str of
adamc@66 1429 NONE => sgn
adamc@66 1430 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 1431 end
adamc@66 1432
adamc@88 1433 fun dopen (env, denv) {str, strs, sgn} =
adamc@66 1434 let
adamc@66 1435 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1436 (L'.StrVar str, #2 sgn) strs
adamc@66 1437 in
adamc@66 1438 case #1 (hnormSgn env sgn) of
adamc@66 1439 L'.SgnConst sgis =>
adamc@88 1440 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
adamc@66 1441 case sgi of
adamc@66 1442 L'.SgiConAbs (x, n, k) =>
adamc@88 1443 let
adamc@88 1444 val c = (L'.CModProj (str, strs, x), loc)
adamc@88 1445 in
adamc@88 1446 ((L'.DCon (x, n, k, c), loc),
adamc@88 1447 (E.pushCNamedAs env' x n k (SOME c), denv'))
adamc@88 1448 end
adamc@66 1449 | L'.SgiCon (x, n, k, c) =>
adamc@66 1450 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
adamc@88 1451 (E.pushCNamedAs env' x n k (SOME c), denv'))
adamc@66 1452 | L'.SgiVal (x, n, t) =>
adamc@66 1453 ((L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc),
adamc@88 1454 (E.pushENamedAs env' x n t, denv'))
adamc@66 1455 | L'.SgiStr (x, n, sgn) =>
adamc@66 1456 ((L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc),
adamc@88 1457 (E.pushStrNamedAs env' x n sgn, denv'))
adamc@66 1458 | L'.SgiSgn (x, n, sgn) =>
adamc@66 1459 ((L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc),
adamc@88 1460 (E.pushSgnNamedAs env' x n sgn, denv'))
adamc@88 1461 | L'.SgiConstraint (c1, c2) =>
adamc@88 1462 ((L'.DConstraint (c1, c2), loc),
adamc@88 1463 (env', denv (* D.assert env denv (c1, c2) *) )))
adamc@88 1464 (env, denv) sgis
adamc@66 1465 | _ => (strError env (UnOpenable sgn);
adamc@88 1466 ([], (env, denv)))
adamc@66 1467 end
adamc@66 1468
adamc@88 1469 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@88 1470 case E.lookupStr env str of
adamc@88 1471 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1472 denv)
adamc@88 1473 | SOME (n, sgn) =>
adamc@88 1474 let
adamc@88 1475 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 1476 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 1477 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1478 (strerror, sgnerror))
adamc@88 1479 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 1480 ((L'.StrVar n, loc), sgn) strs
adamc@88 1481
adamc@88 1482 val cso = E.projectConstraints env {sgn = sgn, str = st}
adamc@88 1483
adamc@88 1484 val denv = case cso of
adamc@88 1485 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1486 denv)
adamc@88 1487 | SOME cs => foldl (fn ((c1, c2), denv) => D.assert env denv (c1, c2)) denv cs
adamc@88 1488 in
adamc@88 1489 denv
adamc@88 1490 end
adamc@88 1491
adamc@31 1492 fun sgiOfDecl (d, loc) =
adamc@31 1493 case d of
adamc@59 1494 L'.DCon (x, n, k, c) => (L'.SgiCon (x, n, k, c), loc)
adamc@59 1495 | L'.DVal (x, n, t, _) => (L'.SgiVal (x, n, t), loc)
adamc@59 1496 | L'.DSgn (x, n, sgn) => (L'.SgiSgn (x, n, sgn), loc)
adamc@59 1497 | L'.DStr (x, n, sgn, _) => (L'.SgiStr (x, n, sgn), loc)
adamc@59 1498 | L'.DFfiStr (x, n, sgn) => (L'.SgiStr (x, n, sgn), loc)
adamc@88 1499 | L'.DConstraint cs => (L'.SgiConstraint cs, loc)
adamc@88 1500
adamc@88 1501 fun sgiBindsD (env, denv) (sgi, _) =
adamc@88 1502 case sgi of
adamc@88 1503 L'.SgiConstraint (c1, c2) => D.assert env denv (c1, c2)
adamc@88 1504 | _ => denv
adamc@31 1505
adamc@86 1506 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
adamc@35 1507 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 1508 (L'.SgnError, _) => ()
adamc@31 1509 | (_, L'.SgnError) => ()
adamc@31 1510
adamc@31 1511 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1512 let
adamc@88 1513 fun folder (sgi2All as (sgi, _), (env, denv)) =
adamc@31 1514 let
adamc@31 1515 fun seek p =
adamc@31 1516 let
adamc@88 1517 fun seek (env, denv) ls =
adamc@31 1518 case ls of
adamc@31 1519 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@88 1520 (env, denv))
adamc@31 1521 | h :: t =>
adamc@31 1522 case p h of
adamc@88 1523 NONE => seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
adamc@88 1524 | SOME envs => envs
adamc@31 1525 in
adamc@88 1526 seek (env, denv) sgis1
adamc@31 1527 end
adamc@31 1528 in
adamc@31 1529 case sgi of
adamc@31 1530 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1531 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1532 let
adamc@41 1533 fun found (x', n1, k1, co1) =
adamc@41 1534 if x = x' then
adamc@41 1535 let
adamc@41 1536 val () = unifyKinds k1 k2
adamc@41 1537 handle KUnify (k1, k2, err) =>
adamc@41 1538 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 1539 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 1540 in
adamc@41 1541 SOME (if n1 = n2 then
adamc@41 1542 env
adamc@41 1543 else
adamc@88 1544 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
adamc@88 1545 denv)
adamc@41 1546 end
adamc@41 1547 else
adamc@41 1548 NONE
adamc@31 1549 in
adamc@31 1550 case sgi1 of
adamc@41 1551 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 1552 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@31 1553 | _ => NONE
adamc@31 1554 end)
adamc@31 1555
adamc@31 1556 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 1557 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1558 case sgi1 of
adamc@41 1559 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 1560 if x = x' then
adamc@41 1561 let
adamc@88 1562 fun good () = SOME (E.pushCNamedAs env x n2 k2 (SOME c2), denv)
adamc@41 1563 in
adamc@86 1564 (case unifyCons (env, denv) c1 c2 of
adamc@86 1565 [] => good ()
adamc@86 1566 | _ => NONE)
adamc@86 1567 handle CUnify (c1, c2, err) =>
adamc@86 1568 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@86 1569 good ())
adamc@41 1570 end
adamc@41 1571 else
adamc@41 1572 NONE
adamc@31 1573 | _ => NONE)
adamc@31 1574
adamc@32 1575 | L'.SgiVal (x, n2, c2) =>
adamc@32 1576 seek (fn sgi1All as (sgi1, _) =>
adamc@32 1577 case sgi1 of
adamc@41 1578 L'.SgiVal (x', n1, c1) =>
adamc@41 1579 if x = x' then
adamc@86 1580 (case unifyCons (env, denv) c1 c2 of
adamc@88 1581 [] => SOME (env, denv)
adamc@86 1582 | _ => NONE)
adamc@86 1583 handle CUnify (c1, c2, err) =>
adamc@86 1584 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@88 1585 SOME (env, denv))
adamc@41 1586 else
adamc@41 1587 NONE
adamc@32 1588 | _ => NONE)
adamc@32 1589
adamc@33 1590 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 1591 seek (fn sgi1All as (sgi1, _) =>
adamc@33 1592 case sgi1 of
adamc@41 1593 L'.SgiStr (x', n1, sgn1) =>
adamc@41 1594 if x = x' then
adamc@66 1595 let
adamc@86 1596 val () = subSgn (env, denv) sgn1 sgn2
adamc@66 1597 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 1598 val env = if n1 = n2 then
adamc@66 1599 env
adamc@66 1600 else
adamc@66 1601 E.pushStrNamedAs env x n2
adamc@66 1602 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 1603 sgn = sgn2})
adamc@66 1604 in
adamc@88 1605 SOME (env, denv)
adamc@66 1606 end
adamc@41 1607 else
adamc@41 1608 NONE
adamc@33 1609 | _ => NONE)
adamc@59 1610
adamc@59 1611 | L'.SgiSgn (x, n2, sgn2) =>
adamc@59 1612 seek (fn sgi1All as (sgi1, _) =>
adamc@59 1613 case sgi1 of
adamc@59 1614 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 1615 if x = x' then
adamc@65 1616 let
adamc@86 1617 val () = subSgn (env, denv) sgn1 sgn2
adamc@86 1618 val () = subSgn (env, denv) sgn2 sgn1
adamc@65 1619
adamc@65 1620 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 1621 val env = if n1 = n2 then
adamc@65 1622 env
adamc@65 1623 else
adamc@65 1624 E.pushSgnNamedAs env x n1 sgn2
adamc@65 1625 in
adamc@88 1626 SOME (env, denv)
adamc@65 1627 end
adamc@59 1628 else
adamc@59 1629 NONE
adamc@59 1630 | _ => NONE)
adamc@88 1631
adamc@88 1632 | L'.SgiConstraint (c2, d2) =>
adamc@88 1633 seek (fn sgi1All as (sgi1, _) =>
adamc@88 1634 case sgi1 of
adamc@88 1635 L'.SgiConstraint (c1, d1) =>
adamc@88 1636 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
adamc@88 1637 SOME (env, D.assert env denv (c2, d2))
adamc@88 1638 else
adamc@88 1639 NONE
adamc@88 1640 | _ => NONE)
adamc@31 1641 end
adamc@31 1642 in
adamc@88 1643 ignore (foldl folder (env, denv) sgis2)
adamc@31 1644 end
adamc@31 1645
adamc@41 1646 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 1647 let
adamc@43 1648 val ran1 =
adamc@43 1649 if n1 = n2 then
adamc@43 1650 ran1
adamc@43 1651 else
adamc@43 1652 subStrInSgn (n1, n2) ran1
adamc@43 1653 in
adamc@86 1654 subSgn (env, denv) dom2 dom1;
adamc@86 1655 subSgn (E.pushStrNamedAs env m2 n2 dom2, denv) ran1 ran2
adamc@43 1656 end
adamc@41 1657
adamc@41 1658 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 1659
adamc@61 1660
adamc@88 1661 fun elabDecl ((d, loc), (env, denv, gs)) =
adamc@76 1662 case d of
adamc@76 1663 L.DCon (x, ko, c) =>
adamc@76 1664 let
adamc@76 1665 val k' = case ko of
adamc@76 1666 NONE => kunif loc
adamc@76 1667 | SOME k => elabKind k
adamc@76 1668
adamc@83 1669 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1670 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1671 in
adamc@76 1672 checkKind env c' ck k';
adamc@76 1673
adamc@88 1674 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1675 end
adamc@76 1676 | L.DVal (x, co, e) =>
adamc@76 1677 let
adamc@83 1678 val (c', ck, gs1) = case co of
adamc@83 1679 NONE => (cunif (loc, ktype), ktype, [])
adamc@83 1680 | SOME c => elabCon (env, denv) c
adamc@76 1681
adamc@83 1682 val (e', et, gs2) = elabExp (env, denv) e
adamc@76 1683 val (env', n) = E.pushENamed env x c'
adamc@86 1684
adamc@86 1685 val gs3 = checkCon (env, denv) e' et c'
adamc@76 1686 in
adamc@88 1687 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, gs1 @ gs2 @ gs3 @ gs))
adamc@76 1688 end
adamc@76 1689
adamc@76 1690 | L.DSgn (x, sgn) =>
adamc@76 1691 let
adamc@83 1692 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1693 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1694 in
adamc@88 1695 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1696 end
adamc@76 1697
adamc@76 1698 | L.DStr (x, sgno, str) =>
adamc@76 1699 let
adamc@83 1700 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@76 1701
adamc@83 1702 val (str', sgn', gs') =
adamc@78 1703 case formal of
adamc@78 1704 NONE =>
adamc@78 1705 let
adamc@83 1706 val (str', actual, ds) = elabStr (env, denv) str
adamc@78 1707 in
adamc@83 1708 (str', selfifyAt env {str = str', sgn = actual}, ds)
adamc@78 1709 end
adamc@83 1710 | SOME (formal, gs1) =>
adamc@78 1711 let
adamc@78 1712 val str =
adamc@78 1713 case #1 (hnormSgn env formal) of
adamc@78 1714 L'.SgnConst sgis =>
adamc@78 1715 (case #1 str of
adamc@78 1716 L.StrConst ds =>
adamc@78 1717 let
adamc@78 1718 val needed = foldl (fn ((sgi, _), needed) =>
adamc@78 1719 case sgi of
adamc@78 1720 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
adamc@78 1721 | _ => needed)
adamc@78 1722 SS.empty sgis
adamc@78 1723
adamc@78 1724 val needed = foldl (fn ((d, _), needed) =>
adamc@78 1725 case d of
adamc@78 1726 L.DCon (x, _, _) => (SS.delete (needed, x)
adamc@78 1727 handle NotFound => needed)
adamc@78 1728 | L.DOpen _ => SS.empty
adamc@78 1729 | _ => needed)
adamc@78 1730 needed ds
adamc@78 1731 in
adamc@78 1732 case SS.listItems needed of
adamc@78 1733 [] => str
adamc@78 1734 | xs =>
adamc@78 1735 let
adamc@78 1736 val kwild = (L.KWild, #2 str)
adamc@78 1737 val cwild = (L.CWild kwild, #2 str)
adamc@78 1738 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
adamc@78 1739 in
adamc@78 1740 (L.StrConst (ds @ ds'), #2 str)
adamc@78 1741 end
adamc@78 1742 end
adamc@78 1743 | _ => str)
adamc@78 1744 | _ => str
adamc@78 1745
adamc@83 1746 val (str', actual, gs2) = elabStr (env, denv) str
adamc@78 1747 in
adamc@86 1748 subSgn (env, denv) actual formal;
adamc@83 1749 (str', formal, gs1 @ gs2)
adamc@78 1750 end
adamc@76 1751
adamc@76 1752 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1753 in
adamc@76 1754 case #1 (hnormSgn env sgn') of
adamc@76 1755 L'.SgnFun _ =>
adamc@76 1756 (case #1 str' of
adamc@76 1757 L'.StrFun _ => ()
adamc@76 1758 | _ => strError env (FunctorRebind loc))
adamc@76 1759 | _ => ();
adamc@76 1760
adamc@88 1761 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
adamc@76 1762 end
adamc@76 1763
adamc@76 1764 | L.DFfiStr (x, sgn) =>
adamc@76 1765 let
adamc@83 1766 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1767
adamc@76 1768 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1769 in
adamc@88 1770 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1771 end
adamc@76 1772
adamc@76 1773 | L.DOpen (m, ms) =>
adamc@88 1774 (case E.lookupStr env m of
adamc@88 1775 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1776 ([], (env, denv, [])))
adamc@88 1777 | SOME (n, sgn) =>
adamc@88 1778 let
adamc@88 1779 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 1780 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 1781 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1782 (strerror, sgnerror))
adamc@88 1783 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 1784 ((L'.StrVar n, loc), sgn) ms
adamc@83 1785
adamc@88 1786 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
adamc@88 1787 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
adamc@88 1788 in
adamc@88 1789 (ds, (env', denv', []))
adamc@88 1790 end)
adamc@88 1791
adamc@88 1792 | L.DConstraint (c1, c2) =>
adamc@88 1793 let
adamc@88 1794 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 1795 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@88 1796 val gs3 = map (fn cs => (loc, env, denv, cs)) (D.prove env denv (c1', c2', loc))
adamc@88 1797
adamc@88 1798 val denv' = D.assert env denv (c1', c2')
adamc@88 1799 in
adamc@88 1800 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 1801 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 1802
adamc@88 1803 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', gs1 @ gs2 @ gs3))
adamc@88 1804 end
adamc@88 1805
adamc@88 1806 | L.DOpenConstraints (m, ms) =>
adamc@88 1807 let
adamc@88 1808 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@88 1809 in
adamc@88 1810 ([], (env, denv, []))
adamc@88 1811 end
adamc@3 1812
adamc@83 1813 and elabStr (env, denv) (str, loc) =
adamc@31 1814 case str of
adamc@31 1815 L.StrConst ds =>
adamc@31 1816 let
adamc@88 1817 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@59 1818 val sgis = map sgiOfDecl ds'
adamc@63 1819
adamc@63 1820 val (sgis, _, _, _, _) =
adamc@64 1821 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 1822 case sgi of
adamc@64 1823 L'.SgiConAbs (x, n, k) =>
adamc@64 1824 let
adamc@64 1825 val (cons, x) =
adamc@64 1826 if SS.member (cons, x) then
adamc@64 1827 (cons, "?" ^ x)
adamc@64 1828 else
adamc@64 1829 (SS.add (cons, x), x)
adamc@64 1830 in
adamc@64 1831 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1832 end
adamc@64 1833 | L'.SgiCon (x, n, k, c) =>
adamc@64 1834 let
adamc@64 1835 val (cons, x) =
adamc@64 1836 if SS.member (cons, x) then
adamc@64 1837 (cons, "?" ^ x)
adamc@64 1838 else
adamc@64 1839 (SS.add (cons, x), x)
adamc@64 1840 in
adamc@64 1841 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1842 end
adamc@64 1843 | L'.SgiVal (x, n, c) =>
adamc@64 1844 let
adamc@64 1845 val (vals, x) =
adamc@64 1846 if SS.member (vals, x) then
adamc@64 1847 (vals, "?" ^ x)
adamc@64 1848 else
adamc@64 1849 (SS.add (vals, x), x)
adamc@64 1850 in
adamc@64 1851 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1852 end
adamc@64 1853 | L'.SgiSgn (x, n, sgn) =>
adamc@64 1854 let
adamc@64 1855 val (sgns, x) =
adamc@64 1856 if SS.member (sgns, x) then
adamc@64 1857 (sgns, "?" ^ x)
adamc@64 1858 else
adamc@64 1859 (SS.add (sgns, x), x)
adamc@64 1860 in
adamc@64 1861 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1862 end
adamc@64 1863
adamc@64 1864 | L'.SgiStr (x, n, sgn) =>
adamc@64 1865 let
adamc@64 1866 val (strs, x) =
adamc@64 1867 if SS.member (strs, x) then
adamc@64 1868 (strs, "?" ^ x)
adamc@64 1869 else
adamc@64 1870 (SS.add (strs, x), x)
adamc@64 1871 in
adamc@64 1872 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 1873 end
adamc@88 1874 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs))
adamc@64 1875
adamc@63 1876 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 1877 in
adamc@83 1878 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 1879 end
adamc@31 1880 | L.StrVar x =>
adamc@31 1881 (case E.lookupStr env x of
adamc@31 1882 NONE =>
adamc@31 1883 (strError env (UnboundStr (loc, x));
adamc@83 1884 (strerror, sgnerror, []))
adamc@83 1885 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 1886 | L.StrProj (str, x) =>
adamc@34 1887 let
adamc@83 1888 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 1889 in
adamc@34 1890 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 1891 NONE => (strError env (UnboundStr (loc, x));
adamc@83 1892 (strerror, sgnerror, []))
adamc@83 1893 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 1894 end
adamc@41 1895 | L.StrFun (m, dom, ranO, str) =>
adamc@41 1896 let
adamc@83 1897 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1898 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1899 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 1900
adamc@83 1901 val (formal, gs3) =
adamc@41 1902 case ranO of
adamc@83 1903 NONE => (actual, [])
adamc@41 1904 | SOME ran =>
adamc@41 1905 let
adamc@83 1906 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 1907 in
adamc@86 1908 subSgn (env', denv) actual ran';
adamc@83 1909 (ran', gs)
adamc@41 1910 end
adamc@41 1911 in
adamc@41 1912 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 1913 (L'.SgnFun (m, n, dom', formal), loc),
adamc@83 1914 gs1 @ gs2 @ gs3)
adamc@41 1915 end
adamc@44 1916 | L.StrApp (str1, str2) =>
adamc@44 1917 let
adamc@83 1918 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@83 1919 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 1920 in
adamc@44 1921 case #1 (hnormSgn env sgn1) of
adamc@83 1922 L'.SgnError => (strerror, sgnerror, [])
adamc@44 1923 | L'.SgnFun (m, n, dom, ran) =>
adamc@86 1924 (subSgn (env, denv) sgn2 dom;
adamc@44 1925 case #1 (hnormSgn env ran) of
adamc@83 1926 L'.SgnError => (strerror, sgnerror, [])
adamc@44 1927 | L'.SgnConst sgis =>
adamc@44 1928 ((L'.StrApp (str1', str2'), loc),
adamc@83 1929 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 1930 gs1 @ gs2)
adamc@44 1931 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 1932 | _ => (strError env (NotFunctor sgn1);
adamc@83 1933 (strerror, sgnerror, []))
adamc@44 1934 end
adamc@31 1935
adamc@56 1936 fun elabFile basis env file =
adamc@56 1937 let
adamc@83 1938 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 1939 val () = case gs of
adamc@83 1940 [] => ()
adamc@83 1941 | _ => raise Fail "Unresolved disjointness constraints in Basis"
adamc@83 1942
adamc@56 1943 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@56 1944
adamc@88 1945 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
adamc@56 1946
adamc@56 1947 fun discoverC r x =
adamc@56 1948 case E.lookupC env' x of
adamc@56 1949 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 1950 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 1951 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 1952
adamc@56 1953 val () = discoverC int "int"
adamc@56 1954 val () = discoverC float "float"
adamc@56 1955 val () = discoverC string "string"
adamc@56 1956
adamc@83 1957 fun elabDecl' (d, (env, gs)) =
adamc@76 1958 let
adamc@76 1959 val () = resetKunif ()
adamc@76 1960 val () = resetCunif ()
adamc@88 1961 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 1962 in
adamc@76 1963 if ErrorMsg.anyErrors () then
adamc@76 1964 ()
adamc@76 1965 else (
adamc@76 1966 if List.exists kunifsInDecl ds then
adamc@76 1967 declError env (KunifsRemain (#2 d))
adamc@76 1968 else
adamc@76 1969 ();
adamc@76 1970
adamc@76 1971 case ListUtil.search cunifsInDecl ds of
adamc@76 1972 NONE => ()
adamc@76 1973 | SOME loc =>
adamc@76 1974 declError env (CunifsRemain loc)
adamc@76 1975 );
adamc@76 1976
adamc@83 1977 (ds, (env, gs))
adamc@76 1978 end
adamc@76 1979
adamc@83 1980 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 1981 in
adamc@84 1982 if ErrorMsg.anyErrors () then
adamc@84 1983 ()
adamc@84 1984 else
adamc@84 1985 app (fn (loc, env, denv, (c1, c2)) =>
adamc@84 1986 case D.prove env denv (c1, c2, loc) of
adamc@84 1987 [] => ()
adamc@84 1988 | _ =>
adamc@86 1989 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adamc@84 1990 eprefaces' [("Con 1", p_con env c1),
adamc@84 1991 ("Con 2", p_con env c2)])) gs;
adamc@83 1992
adamc@56 1993 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
adamc@56 1994 end
adamc@2 1995
adamc@2 1996 end