annotate src/elaborate.sml @ 10:dde5c52e5e5e

Start of elaborating expressions
author Adam Chlipala <adamc@hcoop.net>
date Fri, 28 Mar 2008 13:59:03 -0400
parents 14b533dbe6cc
children e97c6d335869
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@4 30 structure L = Source
adamc@2 31 structure L' = Elab
adamc@2 32 structure E = ElabEnv
adamc@2 33 structure U = ElabUtil
adamc@2 34
adamc@3 35 open Print
adamc@3 36 open ElabPrint
adamc@3 37
adamc@2 38 fun elabKind (k, loc) =
adamc@2 39 case k of
adamc@2 40 L.KType => (L'.KType, loc)
adamc@2 41 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
adamc@2 42 | L.KName => (L'.KName, loc)
adamc@2 43 | L.KRecord k => (L'.KRecord (elabKind k), loc)
adamc@2 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@2 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@2 77 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@3 78 (unifyKinds' d1 d2;
adamc@3 79 unifyKinds' r1 r2)
adamc@2 80 | (L'.KName, L'.KName) => ()
adamc@3 81 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
adamc@2 82
adamc@2 83 | (L'.KError, _) => ()
adamc@2 84 | (_, L'.KError) => ()
adamc@2 85
adamc@3 86 | (L'.KUnif (_, ref (SOME k1All)), _) => unifyKinds' k1All k2All
adamc@3 87 | (_, L'.KUnif (_, ref (SOME k2All))) => unifyKinds' k1All k2All
adamc@2 88
adamc@2 89 | (L'.KUnif (_, r1), L'.KUnif (_, r2)) =>
adamc@2 90 if r1 = r2 then
adamc@2 91 ()
adamc@2 92 else
adamc@2 93 r1 := SOME k2All
adamc@2 94
adamc@2 95 | (L'.KUnif (_, r), _) =>
adamc@2 96 if occursKind r k2All then
adamc@2 97 err KOccursCheckFailed
adamc@2 98 else
adamc@2 99 r := SOME k2All
adamc@2 100 | (_, L'.KUnif (_, r)) =>
adamc@2 101 if occursKind r k1All then
adamc@2 102 err KOccursCheckFailed
adamc@2 103 else
adamc@2 104 r := SOME k1All
adamc@2 105
adamc@2 106 | _ => err KIncompatible
adamc@2 107 end
adamc@2 108
adamc@3 109 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@3 110
adamc@3 111 fun unifyKinds k1 k2 =
adamc@3 112 unifyKinds' k1 k2
adamc@3 113 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@3 114
adamc@3 115 datatype con_error =
adamc@3 116 UnboundCon of ErrorMsg.span * string
adamc@3 117 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
adamc@3 118
adamc@5 119 fun conError env err =
adamc@3 120 case err of
adamc@3 121 UnboundCon (loc, s) =>
adamc@3 122 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
adamc@3 123 | WrongKind (c, k1, k2, kerr) =>
adamc@3 124 (ErrorMsg.errorAt (#2 c) "Wrong kind";
adamc@5 125 eprefaces' [("Constructor", p_con env c),
adamc@5 126 ("Have kind", p_kind k1),
adamc@5 127 ("Need kind", p_kind k2)];
adamc@3 128 kunifyError kerr)
adamc@3 129
adamc@5 130 fun checkKind env c k1 k2 =
adamc@3 131 unifyKinds k1 k2
adamc@3 132 handle KUnify (k1, k2, err) =>
adamc@5 133 conError env (WrongKind (c, k1, k2, err))
adamc@3 134
adamc@3 135 val dummy = ErrorMsg.dummySpan
adamc@3 136
adamc@3 137 val ktype = (L'.KType, dummy)
adamc@3 138 val kname = (L'.KName, dummy)
adamc@3 139
adamc@3 140 val cerror = (L'.CError, dummy)
adamc@3 141 val kerror = (L'.KError, dummy)
adamc@10 142 val eerror = (L'.EError, dummy)
adamc@3 143
adamc@3 144 local
adamc@3 145 val count = ref 0
adamc@3 146 in
adamc@3 147
adamc@3 148 fun resetKunif () = count := 0
adamc@3 149
adamc@3 150 fun kunif () =
adamc@3 151 let
adamc@3 152 val n = !count
adamc@3 153 val s = if n <= 26 then
adamc@3 154 str (chr (ord #"A" + n))
adamc@3 155 else
adamc@3 156 "U" ^ Int.toString (n - 26)
adamc@3 157 in
adamc@3 158 count := n + 1;
adamc@3 159 (L'.KUnif (s, ref NONE), dummy)
adamc@3 160 end
adamc@3 161
adamc@3 162 end
adamc@3 163
adamc@10 164 local
adamc@10 165 val count = ref 0
adamc@10 166 in
adamc@10 167
adamc@10 168 fun resetCunif () = count := 0
adamc@10 169
adamc@10 170 fun cunif k =
adamc@10 171 let
adamc@10 172 val n = !count
adamc@10 173 val s = if n <= 26 then
adamc@10 174 str (chr (ord #"A" + n))
adamc@10 175 else
adamc@10 176 "U" ^ Int.toString (n - 26)
adamc@10 177 in
adamc@10 178 count := n + 1;
adamc@10 179 (L'.CUnif (k, s, ref NONE), dummy)
adamc@10 180 end
adamc@10 181
adamc@10 182 end
adamc@10 183
adamc@3 184 fun elabCon env (c, loc) =
adamc@3 185 case c of
adamc@3 186 L.CAnnot (c, k) =>
adamc@3 187 let
adamc@3 188 val k' = elabKind k
adamc@3 189 val (c', ck) = elabCon env c
adamc@3 190 in
adamc@5 191 checkKind env c' ck k';
adamc@3 192 (c', k')
adamc@3 193 end
adamc@3 194
adamc@3 195 | L.TFun (t1, t2) =>
adamc@3 196 let
adamc@3 197 val (t1', k1) = elabCon env t1
adamc@3 198 val (t2', k2) = elabCon env t2
adamc@3 199 in
adamc@5 200 checkKind env t1' k1 ktype;
adamc@5 201 checkKind env t2' k2 ktype;
adamc@3 202 ((L'.TFun (t1', t2'), loc), ktype)
adamc@3 203 end
adamc@3 204 | L.TCFun (e, x, k, t) =>
adamc@3 205 let
adamc@3 206 val e' = elabExplicitness e
adamc@3 207 val k' = elabKind k
adamc@3 208 val env' = E.pushCRel env x k'
adamc@3 209 val (t', tk) = elabCon env' t
adamc@3 210 in
adamc@5 211 checkKind env t' tk ktype;
adamc@3 212 ((L'.TCFun (e', x, k', t'), loc), ktype)
adamc@3 213 end
adamc@3 214 | L.TRecord c =>
adamc@3 215 let
adamc@3 216 val (c', ck) = elabCon env c
adamc@3 217 val k = (L'.KRecord ktype, loc)
adamc@3 218 in
adamc@5 219 checkKind env c' ck k;
adamc@3 220 ((L'.TRecord c', loc), ktype)
adamc@3 221 end
adamc@3 222
adamc@3 223 | L.CVar s =>
adamc@3 224 (case E.lookupC env s of
adamc@9 225 E.NotBound =>
adamc@5 226 (conError env (UnboundCon (loc, s));
adamc@3 227 (cerror, kerror))
adamc@9 228 | E.Rel (n, k) =>
adamc@3 229 ((L'.CRel n, loc), k)
adamc@9 230 | E.Named (n, k) =>
adamc@3 231 ((L'.CNamed n, loc), k))
adamc@3 232 | L.CApp (c1, c2) =>
adamc@3 233 let
adamc@3 234 val (c1', k1) = elabCon env c1
adamc@3 235 val (c2', k2) = elabCon env c2
adamc@3 236 val dom = kunif ()
adamc@3 237 val ran = kunif ()
adamc@3 238 in
adamc@5 239 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@5 240 checkKind env c2' k2 dom;
adamc@3 241 ((L'.CApp (c1', c2'), loc), ran)
adamc@3 242 end
adamc@8 243 | L.CAbs (x, k, t) =>
adamc@3 244 let
adamc@3 245 val k' = elabKind k
adamc@3 246 val env' = E.pushCRel env x k'
adamc@3 247 val (t', tk) = elabCon env' t
adamc@3 248 in
adamc@8 249 ((L'.CAbs (x, k', t'), loc),
adamc@3 250 (L'.KArrow (k', tk), loc))
adamc@3 251 end
adamc@3 252
adamc@3 253 | L.CName s =>
adamc@3 254 ((L'.CName s, loc), kname)
adamc@3 255
adamc@3 256 | L.CRecord xcs =>
adamc@3 257 let
adamc@3 258 val k = kunif ()
adamc@3 259
adamc@3 260 val xcs' = map (fn (x, c) =>
adamc@3 261 let
adamc@3 262 val (x', xk) = elabCon env x
adamc@3 263 val (c', ck) = elabCon env c
adamc@3 264 in
adamc@5 265 checkKind env x' xk kname;
adamc@5 266 checkKind env c' ck k;
adamc@3 267 (x', c')
adamc@3 268 end) xcs
adamc@3 269 in
adamc@5 270 ((L'.CRecord (k, xcs'), loc), (L'.KRecord k, loc))
adamc@3 271 end
adamc@3 272 | L.CConcat (c1, c2) =>
adamc@3 273 let
adamc@3 274 val (c1', k1) = elabCon env c1
adamc@3 275 val (c2', k2) = elabCon env c2
adamc@3 276 val ku = kunif ()
adamc@3 277 val k = (L'.KRecord ku, loc)
adamc@3 278 in
adamc@5 279 checkKind env c1' k1 k;
adamc@5 280 checkKind env c2' k2 k;
adamc@3 281 ((L'.CConcat (c1', c2'), loc), k)
adamc@3 282 end
adamc@3 283
adamc@6 284 fun kunifsRemain k =
adamc@6 285 case k of
adamc@6 286 L'.KUnif (_, ref NONE) => true
adamc@6 287 | _ => false
adamc@10 288 fun cunifsRemain c =
adamc@10 289 case c of
adamc@10 290 L'.CUnif (_, _, ref NONE) => true
adamc@10 291 | _ => false
adamc@6 292
adamc@6 293 val kunifsInKind = U.Kind.exists kunifsRemain
adamc@6 294 val kunifsInCon = U.Con.exists {kind = kunifsRemain,
adamc@6 295 con = fn _ => false}
adamc@10 296 val kunifsInExp = U.Exp.exists {kind = kunifsRemain,
adamc@10 297 con = fn _ => false,
adamc@10 298 exp = fn _ => false}
adamc@10 299
adamc@10 300 val cunifsInCon = U.Con.exists {kind = fn _ => false,
adamc@10 301 con = cunifsRemain}
adamc@10 302 val cunifsInExp = U.Exp.exists {kind = fn _ => false,
adamc@10 303 con = cunifsRemain,
adamc@10 304 exp = fn _ => false}
adamc@10 305
adamc@10 306 fun occursCon r =
adamc@10 307 U.Con.exists {kind = fn _ => false,
adamc@10 308 con = fn L'.CUnif (_, _, r') => r = r'
adamc@10 309 | _ => false}
adamc@10 310
adamc@10 311 datatype cunify_error =
adamc@10 312 CKind of L'.kind * L'.kind * kunify_error
adamc@10 313 | COccursCheckFailed of L'.con * L'.con
adamc@10 314 | CIncompatible of L'.con * L'.con
adamc@10 315 | CExplicitness of L'.con * L'.con
adamc@10 316
adamc@10 317 exception CUnify' of cunify_error
adamc@10 318
adamc@10 319 fun cunifyError env err =
adamc@10 320 case err of
adamc@10 321 CKind (k1, k2, kerr) =>
adamc@10 322 (eprefaces "Kind unification failure"
adamc@10 323 [("Kind 1", p_kind k1),
adamc@10 324 ("Kind 2", p_kind k2)];
adamc@10 325 kunifyError kerr)
adamc@10 326 | COccursCheckFailed (c1, c2) =>
adamc@10 327 eprefaces "Constructor occurs check failed"
adamc@10 328 [("Con 1", p_con env c1),
adamc@10 329 ("Con 2", p_con env c2)]
adamc@10 330 | CIncompatible (c1, c2) =>
adamc@10 331 eprefaces "Incompatible constructors"
adamc@10 332 [("Con 1", p_con env c1),
adamc@10 333 ("Con 2", p_con env c2)]
adamc@10 334 | CExplicitness (c1, c2) =>
adamc@10 335 eprefaces "Differing constructor function explicitness"
adamc@10 336 [("Con 1", p_con env c1),
adamc@10 337 ("Con 2", p_con env c2)]
adamc@10 338
adamc@10 339 fun unifyCons' env (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 340 let
adamc@10 341 fun err f = raise CUnify' (f (c1All, c2All))
adamc@10 342 in
adamc@10 343 case (c1, c2) of
adamc@10 344 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@10 345 (unifyCons' env d1 d2;
adamc@10 346 unifyCons' env r1 r2)
adamc@10 347 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 348 if expl1 <> expl2 then
adamc@10 349 err CExplicitness
adamc@10 350 else
adamc@10 351 (unifyKinds d1 d2;
adamc@10 352 unifyCons' (E.pushCRel env x1 d1) r1 r2)
adamc@10 353 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
adamc@10 354
adamc@10 355 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 356 if n1 = n2 then
adamc@10 357 ()
adamc@10 358 else
adamc@10 359 err CIncompatible
adamc@10 360 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 361 if n1 = n2 then
adamc@10 362 ()
adamc@10 363 else
adamc@10 364 err CIncompatible
adamc@10 365
adamc@10 366 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@10 367 (unifyCons' env d1 d2;
adamc@10 368 unifyCons' env r1 r2)
adamc@10 369 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 370 (unifyKinds k1 k2;
adamc@10 371 unifyCons' (E.pushCRel env x1 k1) c1 c2)
adamc@10 372
adamc@10 373 | (L'.CName n1, L'.CName n2) =>
adamc@10 374 if n1 = n2 then
adamc@10 375 ()
adamc@10 376 else
adamc@10 377 err CIncompatible
adamc@10 378
adamc@10 379 | (L'.CRecord (k1, rs1), L'.CRecord (k2, rs2)) =>
adamc@10 380 (unifyKinds k1 k2;
adamc@10 381 ((ListPair.appEq (fn ((n1, v1), (n2, v2)) =>
adamc@10 382 (unifyCons' env n1 n2;
adamc@10 383 unifyCons' env v1 v2)) (rs1, rs2))
adamc@10 384 handle ListPair.UnequalLengths => err CIncompatible))
adamc@10 385 | (L'.CConcat (d1, r1), L'.CConcat (d2, r2)) =>
adamc@10 386 (unifyCons' env d1 d2;
adamc@10 387 unifyCons' env r1 r2)
adamc@10 388
adamc@10 389
adamc@10 390 | (L'.CError, _) => ()
adamc@10 391 | (_, L'.CError) => ()
adamc@10 392
adamc@10 393 | (L'.CUnif (_, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
adamc@10 394 | (_, L'.CUnif (_, _, ref (SOME c2All))) => unifyCons' env c1All c2All
adamc@10 395
adamc@10 396 | (L'.CUnif (k1, _, r1), L'.CUnif (k2, _, r2)) =>
adamc@10 397 if r1 = r2 then
adamc@10 398 ()
adamc@10 399 else
adamc@10 400 (unifyKinds k1 k2;
adamc@10 401 r1 := SOME c2All)
adamc@10 402
adamc@10 403 | (L'.CUnif (_, _, r), _) =>
adamc@10 404 if occursCon r c2All then
adamc@10 405 err COccursCheckFailed
adamc@10 406 else
adamc@10 407 r := SOME c2All
adamc@10 408 | (_, L'.CUnif (_, _, r)) =>
adamc@10 409 if occursCon r c1All then
adamc@10 410 err COccursCheckFailed
adamc@10 411 else
adamc@10 412 r := SOME c1All
adamc@10 413
adamc@10 414 | _ => err CIncompatible
adamc@10 415 end
adamc@10 416
adamc@10 417 exception CUnify of L'.con * L'.con * cunify_error
adamc@10 418
adamc@10 419 fun unifyCons env c1 c2 =
adamc@10 420 unifyCons' env c1 c2
adamc@10 421 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 422 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 423
adamc@10 424 datatype exp_error =
adamc@10 425 UnboundExp of ErrorMsg.span * string
adamc@10 426 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@10 427
adamc@10 428 fun expError env err =
adamc@10 429 case err of
adamc@10 430 UnboundExp (loc, s) =>
adamc@10 431 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@10 432 | Unify (e, c1, c2, uerr) =>
adamc@10 433 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 434 eprefaces' [("Expression", p_exp env e),
adamc@10 435 ("Have con", p_con env c1),
adamc@10 436 ("Need con", p_con env c2)];
adamc@10 437 cunifyError env uerr)
adamc@10 438
adamc@10 439 fun checkCon env e c1 c2 =
adamc@10 440 unifyCons env c1 c2
adamc@10 441 handle CUnify (c1, c2, err) =>
adamc@10 442 expError env (Unify (e, c1, c2, err))
adamc@10 443
adamc@10 444 fun elabExp env (e, loc) =
adamc@10 445 case e of
adamc@10 446 L.EAnnot (e, t) =>
adamc@10 447 let
adamc@10 448 val (e', et) = elabExp env e
adamc@10 449 val (t', _) = elabCon env t
adamc@10 450 in
adamc@10 451 checkCon env e' et t';
adamc@10 452 (e', t')
adamc@10 453 end
adamc@10 454
adamc@10 455 | L.EVar s =>
adamc@10 456 (case E.lookupE env s of
adamc@10 457 E.NotBound =>
adamc@10 458 (expError env (UnboundExp (loc, s));
adamc@10 459 (eerror, cerror))
adamc@10 460 | E.Rel (n, t) => ((L'.ERel n, loc), t)
adamc@10 461 | E.Named (n, t) => ((L'.ENamed n, loc), t))
adamc@10 462 | L.EApp (e1, e2) =>
adamc@10 463 let
adamc@10 464 val (e1', t1) = elabExp env e1
adamc@10 465 val (e2', t2) = elabExp env e2
adamc@10 466
adamc@10 467 val dom = cunif ktype
adamc@10 468 val ran = cunif ktype
adamc@10 469 val t = (L'.TFun (dom, ran), dummy)
adamc@10 470 in
adamc@10 471 checkCon env e1' t1 t;
adamc@10 472 checkCon env e2' t2 dom;
adamc@10 473 ((L'.EApp (e1', e2'), loc), ran)
adamc@10 474 end
adamc@10 475 | L.EAbs (x, to, e) =>
adamc@10 476 let
adamc@10 477 val t' = case to of
adamc@10 478 NONE => cunif ktype
adamc@10 479 | SOME t =>
adamc@10 480 let
adamc@10 481 val (t', tk) = elabCon env t
adamc@10 482 in
adamc@10 483 checkKind env t' tk ktype;
adamc@10 484 t'
adamc@10 485 end
adamc@10 486 val (e', et) = elabExp (E.pushERel env x t') e
adamc@10 487 in
adamc@10 488 ((L'.EAbs (x, t', e'), loc),
adamc@10 489 (L'.TFun (t', et), loc))
adamc@10 490 end
adamc@10 491 | L.ECApp (e, c) =>
adamc@10 492 let
adamc@10 493 val (e', et) = elabExp env e
adamc@10 494 val (c', ck) = elabCon env c
adamc@10 495 in
adamc@10 496 raise Fail "ECApp"
adamc@10 497 end
adamc@10 498 | L.ECAbs _ => raise Fail "ECAbs"
adamc@6 499
adamc@6 500 datatype decl_error =
adamc@6 501 KunifsRemainKind of ErrorMsg.span * L'.kind
adamc@6 502 | KunifsRemainCon of ErrorMsg.span * L'.con
adamc@10 503 | KunifsRemainExp of ErrorMsg.span * L'.exp
adamc@10 504 | CunifsRemainCon of ErrorMsg.span * L'.con
adamc@10 505 | CunifsRemainExp of ErrorMsg.span * L'.exp
adamc@6 506
adamc@6 507 fun declError env err =
adamc@6 508 case err of
adamc@6 509 KunifsRemainKind (loc, k) =>
adamc@6 510 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in kind";
adamc@6 511 eprefaces' [("Kind", p_kind k)])
adamc@6 512 | KunifsRemainCon (loc, c) =>
adamc@6 513 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in constructor";
adamc@6 514 eprefaces' [("Constructor", p_con env c)])
adamc@10 515 | KunifsRemainExp (loc, e) =>
adamc@10 516 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in expression";
adamc@10 517 eprefaces' [("Expression", p_exp env e)])
adamc@10 518 | CunifsRemainCon (loc, c) =>
adamc@10 519 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in constructor";
adamc@10 520 eprefaces' [("Constructor", p_con env c)])
adamc@10 521 | CunifsRemainExp (loc, e) =>
adamc@10 522 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in expression";
adamc@10 523 eprefaces' [("Expression", p_exp env e)])
adamc@6 524
adamc@3 525 fun elabDecl env (d, loc) =
adamc@5 526 (resetKunif ();
adamc@5 527 case d of
adamc@5 528 L.DCon (x, ko, c) =>
adamc@5 529 let
adamc@5 530 val k' = case ko of
adamc@5 531 NONE => kunif ()
adamc@5 532 | SOME k => elabKind k
adamc@3 533
adamc@5 534 val (c', ck) = elabCon env c
adamc@5 535 val (env', n) = E.pushCNamed env x k'
adamc@5 536 in
adamc@5 537 checkKind env c' ck k';
adamc@6 538
adamc@6 539 if kunifsInKind k' then
adamc@6 540 declError env (KunifsRemainKind (loc, k'))
adamc@6 541 else
adamc@6 542 ();
adamc@6 543
adamc@6 544 if kunifsInCon c' then
adamc@6 545 declError env (KunifsRemainCon (loc, c'))
adamc@6 546 else
adamc@6 547 ();
adamc@6 548
adamc@5 549 (env',
adamc@5 550 (L'.DCon (x, n, k', c'), loc))
adamc@10 551 end
adamc@10 552 | L.DVal (x, co, e) =>
adamc@10 553 let
adamc@10 554 val (c', ck) = case co of
adamc@10 555 NONE => (cunif ktype, ktype)
adamc@10 556 | SOME c => elabCon env c
adamc@10 557
adamc@10 558 val (e', et) = elabExp env e
adamc@10 559 val (env', n) = E.pushENamed env x c'
adamc@10 560 in
adamc@10 561 checkCon env e' et c';
adamc@10 562
adamc@10 563 if kunifsInCon c' then
adamc@10 564 declError env (KunifsRemainCon (loc, c'))
adamc@10 565 else
adamc@10 566 ();
adamc@10 567
adamc@10 568 if cunifsInCon c' then
adamc@10 569 declError env (CunifsRemainCon (loc, c'))
adamc@10 570 else
adamc@10 571 ();
adamc@10 572
adamc@10 573 if kunifsInExp e' then
adamc@10 574 declError env (KunifsRemainExp (loc, e'))
adamc@10 575 else
adamc@10 576 ();
adamc@10 577
adamc@10 578 if cunifsInExp e' then
adamc@10 579 declError env (CunifsRemainExp (loc, e'))
adamc@10 580 else
adamc@10 581 ();
adamc@10 582
adamc@10 583 (env',
adamc@10 584 (L'.DVal (x, n, c', e'), loc))
adamc@5 585 end)
adamc@3 586
adamc@5 587 fun elabFile env ds =
adamc@5 588 ListUtil.mapfoldl (fn (d, env) => elabDecl env d) env ds
adamc@2 589
adamc@2 590 end