annotate src/elaborate.sml @ 26:4ab19c19665f

Closure conversion
author Adam Chlipala <adamc@hcoop.net>
date Tue, 10 Jun 2008 15:56:33 -0400
parents 9a578171de9e
children 537db4ee89f4
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@2 35
adamc@3 36 open Print
adamc@3 37 open ElabPrint
adamc@3 38
adamc@2 39 fun elabExplicitness e =
adamc@2 40 case e of
adamc@2 41 L.Explicit => L'.Explicit
adamc@2 42 | L.Implicit => L'.Implicit
adamc@2 43
adamc@2 44 fun occursKind r =
adamc@2 45 U.Kind.exists (fn L'.KUnif (_, r') => r = r'
adamc@2 46 | _ => false)
adamc@2 47
adamc@3 48 datatype kunify_error =
adamc@2 49 KOccursCheckFailed of L'.kind * L'.kind
adamc@2 50 | KIncompatible of L'.kind * L'.kind
adamc@2 51
adamc@3 52 exception KUnify' of kunify_error
adamc@3 53
adamc@3 54 fun kunifyError err =
adamc@2 55 case err of
adamc@2 56 KOccursCheckFailed (k1, k2) =>
adamc@3 57 eprefaces "Kind occurs check failed"
adamc@3 58 [("Kind 1", p_kind k1),
adamc@3 59 ("Kind 2", p_kind k2)]
adamc@2 60 | KIncompatible (k1, k2) =>
adamc@3 61 eprefaces "Incompatible kinds"
adamc@3 62 [("Kind 1", p_kind k1),
adamc@3 63 ("Kind 2", p_kind k2)]
adamc@2 64
adamc@3 65 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@2 66 let
adamc@3 67 fun err f = raise KUnify' (f (k1All, k2All))
adamc@2 68 in
adamc@2 69 case (k1, k2) of
adamc@2 70 (L'.KType, L'.KType) => ()
adamc@2 71 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@3 72 (unifyKinds' d1 d2;
adamc@3 73 unifyKinds' r1 r2)
adamc@2 74 | (L'.KName, L'.KName) => ()
adamc@3 75 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
adamc@2 76
adamc@2 77 | (L'.KError, _) => ()
adamc@2 78 | (_, L'.KError) => ()
adamc@2 79
adamc@3 80 | (L'.KUnif (_, ref (SOME k1All)), _) => unifyKinds' k1All k2All
adamc@3 81 | (_, L'.KUnif (_, ref (SOME k2All))) => unifyKinds' k1All k2All
adamc@2 82
adamc@2 83 | (L'.KUnif (_, r1), L'.KUnif (_, r2)) =>
adamc@2 84 if r1 = r2 then
adamc@2 85 ()
adamc@2 86 else
adamc@2 87 r1 := SOME k2All
adamc@2 88
adamc@2 89 | (L'.KUnif (_, r), _) =>
adamc@2 90 if occursKind r k2All then
adamc@2 91 err KOccursCheckFailed
adamc@2 92 else
adamc@2 93 r := SOME k2All
adamc@2 94 | (_, L'.KUnif (_, r)) =>
adamc@2 95 if occursKind r k1All then
adamc@2 96 err KOccursCheckFailed
adamc@2 97 else
adamc@2 98 r := SOME k1All
adamc@2 99
adamc@2 100 | _ => err KIncompatible
adamc@2 101 end
adamc@2 102
adamc@3 103 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@3 104
adamc@3 105 fun unifyKinds k1 k2 =
adamc@3 106 unifyKinds' k1 k2
adamc@3 107 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@3 108
adamc@3 109 datatype con_error =
adamc@3 110 UnboundCon of ErrorMsg.span * string
adamc@3 111 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
adamc@3 112
adamc@5 113 fun conError env err =
adamc@3 114 case err of
adamc@3 115 UnboundCon (loc, s) =>
adamc@3 116 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
adamc@3 117 | WrongKind (c, k1, k2, kerr) =>
adamc@3 118 (ErrorMsg.errorAt (#2 c) "Wrong kind";
adamc@5 119 eprefaces' [("Constructor", p_con env c),
adamc@5 120 ("Have kind", p_kind k1),
adamc@5 121 ("Need kind", p_kind k2)];
adamc@3 122 kunifyError kerr)
adamc@3 123
adamc@5 124 fun checkKind env c k1 k2 =
adamc@3 125 unifyKinds k1 k2
adamc@3 126 handle KUnify (k1, k2, err) =>
adamc@5 127 conError env (WrongKind (c, k1, k2, err))
adamc@3 128
adamc@3 129 val dummy = ErrorMsg.dummySpan
adamc@3 130
adamc@3 131 val ktype = (L'.KType, dummy)
adamc@3 132 val kname = (L'.KName, dummy)
adamc@12 133 val ktype_record = (L'.KRecord ktype, dummy)
adamc@3 134
adamc@3 135 val cerror = (L'.CError, dummy)
adamc@3 136 val kerror = (L'.KError, dummy)
adamc@10 137 val eerror = (L'.EError, dummy)
adamc@3 138
adamc@3 139 local
adamc@3 140 val count = ref 0
adamc@3 141 in
adamc@3 142
adamc@3 143 fun resetKunif () = count := 0
adamc@3 144
adamc@3 145 fun kunif () =
adamc@3 146 let
adamc@3 147 val n = !count
adamc@3 148 val s = if n <= 26 then
adamc@3 149 str (chr (ord #"A" + n))
adamc@3 150 else
adamc@3 151 "U" ^ Int.toString (n - 26)
adamc@3 152 in
adamc@3 153 count := n + 1;
adamc@3 154 (L'.KUnif (s, ref NONE), dummy)
adamc@3 155 end
adamc@3 156
adamc@3 157 end
adamc@3 158
adamc@10 159 local
adamc@10 160 val count = ref 0
adamc@10 161 in
adamc@10 162
adamc@10 163 fun resetCunif () = count := 0
adamc@10 164
adamc@10 165 fun cunif k =
adamc@10 166 let
adamc@10 167 val n = !count
adamc@10 168 val s = if n <= 26 then
adamc@10 169 str (chr (ord #"A" + n))
adamc@10 170 else
adamc@10 171 "U" ^ Int.toString (n - 26)
adamc@10 172 in
adamc@10 173 count := n + 1;
adamc@10 174 (L'.CUnif (k, s, ref NONE), dummy)
adamc@10 175 end
adamc@10 176
adamc@10 177 end
adamc@10 178
adamc@18 179 fun elabKind (k, loc) =
adamc@18 180 case k of
adamc@18 181 L.KType => (L'.KType, loc)
adamc@18 182 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
adamc@18 183 | L.KName => (L'.KName, loc)
adamc@18 184 | L.KRecord k => (L'.KRecord (elabKind k), loc)
adamc@18 185 | L.KWild => kunif ()
adamc@18 186
adamc@3 187 fun elabCon env (c, loc) =
adamc@3 188 case c of
adamc@3 189 L.CAnnot (c, k) =>
adamc@3 190 let
adamc@3 191 val k' = elabKind k
adamc@3 192 val (c', ck) = elabCon env c
adamc@3 193 in
adamc@5 194 checkKind env c' ck k';
adamc@3 195 (c', k')
adamc@3 196 end
adamc@3 197
adamc@3 198 | L.TFun (t1, t2) =>
adamc@3 199 let
adamc@3 200 val (t1', k1) = elabCon env t1
adamc@3 201 val (t2', k2) = elabCon env t2
adamc@3 202 in
adamc@5 203 checkKind env t1' k1 ktype;
adamc@5 204 checkKind env t2' k2 ktype;
adamc@3 205 ((L'.TFun (t1', t2'), loc), ktype)
adamc@3 206 end
adamc@3 207 | L.TCFun (e, x, k, t) =>
adamc@3 208 let
adamc@3 209 val e' = elabExplicitness e
adamc@3 210 val k' = elabKind k
adamc@3 211 val env' = E.pushCRel env x k'
adamc@3 212 val (t', tk) = elabCon env' t
adamc@3 213 in
adamc@5 214 checkKind env t' tk ktype;
adamc@3 215 ((L'.TCFun (e', x, k', t'), loc), ktype)
adamc@3 216 end
adamc@3 217 | L.TRecord c =>
adamc@3 218 let
adamc@3 219 val (c', ck) = elabCon env c
adamc@3 220 val k = (L'.KRecord ktype, loc)
adamc@3 221 in
adamc@5 222 checkKind env c' ck k;
adamc@3 223 ((L'.TRecord c', loc), ktype)
adamc@3 224 end
adamc@3 225
adamc@3 226 | L.CVar s =>
adamc@3 227 (case E.lookupC env s of
adamc@9 228 E.NotBound =>
adamc@5 229 (conError env (UnboundCon (loc, s));
adamc@3 230 (cerror, kerror))
adamc@9 231 | E.Rel (n, k) =>
adamc@3 232 ((L'.CRel n, loc), k)
adamc@9 233 | E.Named (n, k) =>
adamc@3 234 ((L'.CNamed n, loc), k))
adamc@3 235 | L.CApp (c1, c2) =>
adamc@3 236 let
adamc@3 237 val (c1', k1) = elabCon env c1
adamc@3 238 val (c2', k2) = elabCon env c2
adamc@3 239 val dom = kunif ()
adamc@3 240 val ran = kunif ()
adamc@3 241 in
adamc@5 242 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@5 243 checkKind env c2' k2 dom;
adamc@3 244 ((L'.CApp (c1', c2'), loc), ran)
adamc@3 245 end
adamc@8 246 | L.CAbs (x, k, t) =>
adamc@3 247 let
adamc@3 248 val k' = elabKind k
adamc@3 249 val env' = E.pushCRel env x k'
adamc@3 250 val (t', tk) = elabCon env' t
adamc@3 251 in
adamc@8 252 ((L'.CAbs (x, k', t'), loc),
adamc@3 253 (L'.KArrow (k', tk), loc))
adamc@3 254 end
adamc@3 255
adamc@3 256 | L.CName s =>
adamc@3 257 ((L'.CName s, loc), kname)
adamc@3 258
adamc@3 259 | L.CRecord xcs =>
adamc@3 260 let
adamc@3 261 val k = kunif ()
adamc@3 262
adamc@3 263 val xcs' = map (fn (x, c) =>
adamc@3 264 let
adamc@3 265 val (x', xk) = elabCon env x
adamc@3 266 val (c', ck) = elabCon env c
adamc@3 267 in
adamc@5 268 checkKind env x' xk kname;
adamc@5 269 checkKind env c' ck k;
adamc@3 270 (x', c')
adamc@3 271 end) xcs
adamc@3 272 in
adamc@5 273 ((L'.CRecord (k, xcs'), loc), (L'.KRecord k, loc))
adamc@3 274 end
adamc@3 275 | L.CConcat (c1, c2) =>
adamc@3 276 let
adamc@3 277 val (c1', k1) = elabCon env c1
adamc@3 278 val (c2', k2) = elabCon env c2
adamc@3 279 val ku = kunif ()
adamc@3 280 val k = (L'.KRecord ku, loc)
adamc@3 281 in
adamc@5 282 checkKind env c1' k1 k;
adamc@5 283 checkKind env c2' k2 k;
adamc@3 284 ((L'.CConcat (c1', c2'), loc), k)
adamc@3 285 end
adamc@3 286
adamc@18 287 | L.CWild k =>
adamc@18 288 let
adamc@18 289 val k' = elabKind k
adamc@18 290 in
adamc@18 291 (cunif k', k')
adamc@18 292 end
adamc@18 293
adamc@6 294 fun kunifsRemain k =
adamc@6 295 case k of
adamc@6 296 L'.KUnif (_, ref NONE) => true
adamc@6 297 | _ => false
adamc@10 298 fun cunifsRemain c =
adamc@10 299 case c of
adamc@10 300 L'.CUnif (_, _, ref NONE) => true
adamc@10 301 | _ => false
adamc@6 302
adamc@6 303 val kunifsInKind = U.Kind.exists kunifsRemain
adamc@6 304 val kunifsInCon = U.Con.exists {kind = kunifsRemain,
adamc@6 305 con = fn _ => false}
adamc@10 306 val kunifsInExp = U.Exp.exists {kind = kunifsRemain,
adamc@10 307 con = fn _ => false,
adamc@10 308 exp = fn _ => false}
adamc@10 309
adamc@10 310 val cunifsInCon = U.Con.exists {kind = fn _ => false,
adamc@10 311 con = cunifsRemain}
adamc@10 312 val cunifsInExp = U.Exp.exists {kind = fn _ => false,
adamc@10 313 con = cunifsRemain,
adamc@10 314 exp = fn _ => false}
adamc@10 315
adamc@10 316 fun occursCon r =
adamc@10 317 U.Con.exists {kind = fn _ => false,
adamc@10 318 con = fn L'.CUnif (_, _, r') => r = r'
adamc@10 319 | _ => false}
adamc@10 320
adamc@10 321 datatype cunify_error =
adamc@10 322 CKind of L'.kind * L'.kind * kunify_error
adamc@10 323 | COccursCheckFailed of L'.con * L'.con
adamc@10 324 | CIncompatible of L'.con * L'.con
adamc@10 325 | CExplicitness of L'.con * L'.con
adamc@12 326 | CKindof of L'.con
adamc@12 327 | CRecordFailure
adamc@10 328
adamc@10 329 exception CUnify' of cunify_error
adamc@10 330
adamc@10 331 fun cunifyError env err =
adamc@10 332 case err of
adamc@10 333 CKind (k1, k2, kerr) =>
adamc@10 334 (eprefaces "Kind unification failure"
adamc@10 335 [("Kind 1", p_kind k1),
adamc@10 336 ("Kind 2", p_kind k2)];
adamc@10 337 kunifyError kerr)
adamc@10 338 | COccursCheckFailed (c1, c2) =>
adamc@10 339 eprefaces "Constructor occurs check failed"
adamc@10 340 [("Con 1", p_con env c1),
adamc@10 341 ("Con 2", p_con env c2)]
adamc@10 342 | CIncompatible (c1, c2) =>
adamc@10 343 eprefaces "Incompatible constructors"
adamc@10 344 [("Con 1", p_con env c1),
adamc@10 345 ("Con 2", p_con env c2)]
adamc@10 346 | CExplicitness (c1, c2) =>
adamc@10 347 eprefaces "Differing constructor function explicitness"
adamc@10 348 [("Con 1", p_con env c1),
adamc@10 349 ("Con 2", p_con env c2)]
adamc@12 350 | CKindof c =>
adamc@12 351 eprefaces "Kind unification variable blocks kindof calculation"
adamc@12 352 [("Con", p_con env c)]
adamc@12 353 | CRecordFailure =>
adamc@12 354 eprefaces "Can't unify record constructors" []
adamc@10 355
adamc@13 356 exception SynUnif = E.SynUnif
adamc@13 357 val liftConInCon = E.liftConInCon
adamc@12 358
adamc@12 359 val subConInCon =
adamc@12 360 U.Con.mapB {kind = fn k => k,
adamc@12 361 con = fn (xn, rep) => fn c =>
adamc@12 362 case c of
adamc@12 363 L'.CRel xn' =>
adamc@12 364 if xn = xn' then
adamc@12 365 #1 rep
adamc@12 366 else
adamc@12 367 c
adamc@17 368 (*| L'.CUnif _ => raise SynUnif*)
adamc@12 369 | _ => c,
adamc@12 370 bind = fn ((xn, rep), U.Con.Rel _) => (xn+1, liftConInCon 0 rep)
adamc@12 371 | (ctx, _) => ctx}
adamc@12 372
adamc@12 373 type record_summary = {
adamc@12 374 fields : (L'.con * L'.con) list,
adamc@12 375 unifs : (L'.con * L'.con option ref) list,
adamc@12 376 others : L'.con list
adamc@12 377 }
adamc@12 378
adamc@12 379 fun summaryToCon {fields, unifs, others} =
adamc@12 380 let
adamc@12 381 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 382 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 383 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 384 in
adamc@12 385 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 386 end
adamc@12 387
adamc@12 388 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 389
adamc@12 390 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 391
adamc@12 392 fun hnormKind (kAll as (k, _)) =
adamc@12 393 case k of
adamc@12 394 L'.KUnif (_, ref (SOME k)) => hnormKind k
adamc@12 395 | _ => kAll
adamc@12 396
adamc@12 397 fun kindof env (c, loc) =
adamc@12 398 case c of
adamc@12 399 L'.TFun _ => ktype
adamc@12 400 | L'.TCFun _ => ktype
adamc@12 401 | L'.TRecord _ => ktype
adamc@12 402
adamc@12 403 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 404 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@12 405 | L'.CApp (c, _) =>
adamc@12 406 (case #1 (hnormKind (kindof env c)) of
adamc@12 407 L'.KArrow (_, k) => k
adamc@12 408 | L'.KError => kerror
adamc@12 409 | _ => raise CUnify' (CKindof c))
adamc@12 410 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@12 411
adamc@12 412 | L'.CName _ => kname
adamc@12 413
adamc@12 414 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 415 | L'.CConcat (c, _) => kindof env c
adamc@12 416
adamc@12 417 | L'.CError => kerror
adamc@12 418 | L'.CUnif (k, _, _) => k
adamc@12 419
adamc@12 420 fun unifyRecordCons env (c1, c2) =
adamc@12 421 let
adamc@12 422 val k1 = kindof env c1
adamc@12 423 val k2 = kindof env c2
adamc@12 424 in
adamc@12 425 unifyKinds k1 k2;
adamc@12 426 unifySummaries env (k1, recordSummary env c1, recordSummary env c2)
adamc@12 427 end
adamc@12 428
adamc@12 429 and recordSummary env c : record_summary =
adamc@12 430 case hnormCon env c of
adamc@12 431 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
adamc@12 432 | (L'.CConcat (c1, c2), _) =>
adamc@12 433 let
adamc@12 434 val s1 = recordSummary env c1
adamc@12 435 val s2 = recordSummary env c2
adamc@12 436 in
adamc@12 437 {fields = #fields s1 @ #fields s2,
adamc@12 438 unifs = #unifs s1 @ #unifs s2,
adamc@12 439 others = #others s1 @ #others s2}
adamc@12 440 end
adamc@12 441 | (L'.CUnif (_, _, ref (SOME c)), _) => recordSummary env c
adamc@12 442 | c' as (L'.CUnif (_, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
adamc@12 443 | c' => {fields = [], unifs = [], others = [c']}
adamc@12 444
adamc@12 445 and consEq env (c1, c2) =
adamc@12 446 (unifyCons env c1 c2;
adamc@12 447 true)
adamc@12 448 handle CUnify _ => false
adamc@12 449
adamc@12 450 and unifySummaries env (k, s1 : record_summary, s2 : record_summary) =
adamc@12 451 let
adamc@14 452 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@14 453 ("#2", p_summary env s2)]*)
adamc@12 454
adamc@12 455 fun eatMatching p (ls1, ls2) =
adamc@12 456 let
adamc@12 457 fun em (ls1, ls2, passed1) =
adamc@12 458 case ls1 of
adamc@12 459 [] => (rev passed1, ls2)
adamc@12 460 | h1 :: t1 =>
adamc@12 461 let
adamc@12 462 fun search (ls2', passed2) =
adamc@12 463 case ls2' of
adamc@12 464 [] => em (t1, ls2, h1 :: passed1)
adamc@12 465 | h2 :: t2 =>
adamc@12 466 if p (h1, h2) then
adamc@12 467 em (t1, List.revAppend (passed2, t2), passed1)
adamc@12 468 else
adamc@12 469 search (t2, h2 :: passed2)
adamc@12 470 in
adamc@12 471 search (ls2, [])
adamc@12 472 end
adamc@12 473 in
adamc@12 474 em (ls1, ls2, [])
adamc@12 475 end
adamc@12 476
adamc@12 477 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@12 478 if consEq env (x1, x2) then
adamc@12 479 (unifyCons env c1 c2;
adamc@12 480 true)
adamc@12 481 else
adamc@12 482 false) (#fields s1, #fields s2)
adamc@14 483 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@14 484 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@12 485 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@12 486 val (others1, others2) = eatMatching (consEq env) (#others s1, #others s2)
adamc@12 487
adamc@12 488 fun unifFields (fs, others, unifs) =
adamc@12 489 case (fs, others, unifs) of
adamc@12 490 ([], [], _) => ([], [], unifs)
adamc@12 491 | (_, _, []) => (fs, others, [])
adamc@12 492 | (_, _, (_, r) :: rest) =>
adamc@12 493 let
adamc@12 494 val r' = ref NONE
adamc@12 495 val cr' = (L'.CUnif (k, "recd", r'), dummy)
adamc@12 496
adamc@12 497 val prefix = case (fs, others) of
adamc@12 498 ([], other :: others) =>
adamc@12 499 List.foldl (fn (other, c) =>
adamc@12 500 (L'.CConcat (c, other), dummy))
adamc@12 501 other others
adamc@12 502 | (fs, []) =>
adamc@12 503 (L'.CRecord (k, fs), dummy)
adamc@12 504 | (fs, others) =>
adamc@12 505 List.foldl (fn (other, c) =>
adamc@12 506 (L'.CConcat (c, other), dummy))
adamc@12 507 (L'.CRecord (k, fs), dummy) others
adamc@12 508 in
adamc@12 509 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 510 ([], [], (cr', r') :: rest)
adamc@12 511 end
adamc@12 512
adamc@12 513 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 514 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 515
adamc@12 516 val clear1 = case (fs1, others1) of
adamc@12 517 ([], []) => true
adamc@12 518 | _ => false
adamc@12 519 val clear2 = case (fs2, others2) of
adamc@12 520 ([], []) => true
adamc@12 521 | _ => false
adamc@12 522 val empty = (L'.CRecord (k, []), dummy)
adamc@12 523 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 524 case (unifs1, unifs2) of
adamc@12 525 ([], _) =>
adamc@12 526 if clear1 then
adamc@12 527 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 528 else
adamc@12 529 raise CUnify' CRecordFailure
adamc@12 530 | (_, []) =>
adamc@12 531 if clear2 then
adamc@12 532 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 533 else
adamc@12 534 raise CUnify' CRecordFailure
adamc@12 535 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 536 (r2 := SOME c1;
adamc@12 537 pairOffUnifs (rest1, rest2))
adamc@12 538 in
adamc@12 539 pairOffUnifs (unifs1, unifs2)
adamc@12 540 end
adamc@12 541
adamc@12 542 and hnormCon env (cAll as (c, _)) =
adamc@11 543 case c of
adamc@11 544 L'.CUnif (_, _, ref (SOME c)) => hnormCon env c
adamc@11 545
adamc@11 546 | L'.CNamed xn =>
adamc@11 547 (case E.lookupCNamed env xn of
adamc@11 548 (_, _, SOME c') => hnormCon env c'
adamc@11 549 | _ => cAll)
adamc@11 550
adamc@12 551 | L'.CApp (c1, c2) =>
adamc@12 552 (case hnormCon env c1 of
adamc@12 553 (L'.CAbs (_, _, cb), _) =>
adamc@12 554 ((hnormCon env (subConInCon (0, c2) cb))
adamc@12 555 handle SynUnif => cAll)
adamc@12 556 | _ => cAll)
adamc@12 557
adamc@12 558 | L'.CConcat (c1, c2) =>
adamc@12 559 (case (hnormCon env c1, hnormCon env c2) of
adamc@12 560 ((L'.CRecord (k, xcs1), loc), (L'.CRecord (_, xcs2), _)) =>
adamc@12 561 (L'.CRecord (k, xcs1 @ xcs2), loc)
adamc@12 562 | _ => cAll)
adamc@12 563
adamc@11 564 | _ => cAll
adamc@11 565
adamc@12 566 and unifyCons' env c1 c2 =
adamc@11 567 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
adamc@11 568
adamc@11 569 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 570 let
adamc@10 571 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 572
adamc@12 573 fun isRecord () = unifyRecordCons env (c1All, c2All)
adamc@10 574 in
adamc@10 575 case (c1, c2) of
adamc@10 576 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@10 577 (unifyCons' env d1 d2;
adamc@10 578 unifyCons' env r1 r2)
adamc@10 579 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 580 if expl1 <> expl2 then
adamc@10 581 err CExplicitness
adamc@10 582 else
adamc@10 583 (unifyKinds d1 d2;
adamc@10 584 unifyCons' (E.pushCRel env x1 d1) r1 r2)
adamc@10 585 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
adamc@10 586
adamc@10 587 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 588 if n1 = n2 then
adamc@10 589 ()
adamc@10 590 else
adamc@10 591 err CIncompatible
adamc@10 592 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 593 if n1 = n2 then
adamc@10 594 ()
adamc@10 595 else
adamc@10 596 err CIncompatible
adamc@10 597
adamc@10 598 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@10 599 (unifyCons' env d1 d2;
adamc@10 600 unifyCons' env r1 r2)
adamc@10 601 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 602 (unifyKinds k1 k2;
adamc@10 603 unifyCons' (E.pushCRel env x1 k1) c1 c2)
adamc@10 604
adamc@10 605 | (L'.CName n1, L'.CName n2) =>
adamc@10 606 if n1 = n2 then
adamc@10 607 ()
adamc@10 608 else
adamc@10 609 err CIncompatible
adamc@10 610
adamc@10 611 | (L'.CError, _) => ()
adamc@10 612 | (_, L'.CError) => ()
adamc@10 613
adamc@10 614 | (L'.CUnif (_, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
adamc@10 615 | (_, L'.CUnif (_, _, ref (SOME c2All))) => unifyCons' env c1All c2All
adamc@10 616
adamc@10 617 | (L'.CUnif (k1, _, r1), L'.CUnif (k2, _, r2)) =>
adamc@10 618 if r1 = r2 then
adamc@10 619 ()
adamc@10 620 else
adamc@10 621 (unifyKinds k1 k2;
adamc@10 622 r1 := SOME c2All)
adamc@10 623
adamc@10 624 | (L'.CUnif (_, _, r), _) =>
adamc@10 625 if occursCon r c2All then
adamc@10 626 err COccursCheckFailed
adamc@10 627 else
adamc@10 628 r := SOME c2All
adamc@10 629 | (_, L'.CUnif (_, _, r)) =>
adamc@10 630 if occursCon r c1All then
adamc@10 631 err COccursCheckFailed
adamc@10 632 else
adamc@10 633 r := SOME c1All
adamc@10 634
adamc@12 635 | (L'.CRecord _, _) => isRecord ()
adamc@12 636 | (_, L'.CRecord _) => isRecord ()
adamc@12 637 | (L'.CConcat _, _) => isRecord ()
adamc@12 638 | (_, L'.CConcat _) => isRecord ()
adamc@12 639
adamc@10 640 | _ => err CIncompatible
adamc@10 641 end
adamc@10 642
adamc@12 643 and unifyCons env c1 c2 =
adamc@10 644 unifyCons' env c1 c2
adamc@10 645 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 646 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 647
adamc@10 648 datatype exp_error =
adamc@10 649 UnboundExp of ErrorMsg.span * string
adamc@10 650 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 651 | Unif of string * L'.con
adamc@11 652 | WrongForm of string * L'.exp * L'.con
adamc@10 653
adamc@10 654 fun expError env err =
adamc@10 655 case err of
adamc@10 656 UnboundExp (loc, s) =>
adamc@10 657 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@10 658 | Unify (e, c1, c2, uerr) =>
adamc@10 659 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 660 eprefaces' [("Expression", p_exp env e),
adamc@10 661 ("Have con", p_con env c1),
adamc@10 662 ("Need con", p_con env c2)];
adamc@10 663 cunifyError env uerr)
adamc@11 664 | Unif (action, c) =>
adamc@11 665 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 666 eprefaces' [("Con", p_con env c)])
adamc@11 667 | WrongForm (variety, e, t) =>
adamc@11 668 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 669 eprefaces' [("Expression", p_exp env e),
adamc@11 670 ("Type", p_con env t)])
adamc@10 671
adamc@10 672 fun checkCon env e c1 c2 =
adamc@10 673 unifyCons env c1 c2
adamc@10 674 handle CUnify (c1, c2, err) =>
adamc@10 675 expError env (Unify (e, c1, c2, err))
adamc@10 676
adamc@14 677 fun primType env p =
adamc@14 678 let
adamc@14 679 val s = case p of
adamc@14 680 P.Int _ => "int"
adamc@14 681 | P.Float _ => "float"
adamc@14 682 | P.String _ => "string"
adamc@14 683 in
adamc@14 684 case E.lookupC env s of
adamc@14 685 E.NotBound => raise Fail ("Primitive type " ^ s ^ " unbound")
adamc@14 686 | E.Rel _ => raise Fail ("Primitive type " ^ s ^ " bound as relative")
adamc@14 687 | E.Named (n, (L'.KType, _)) => L'.CNamed n
adamc@14 688 | E.Named _ => raise Fail ("Primitive type " ^ s ^ " bound at non-Type kind")
adamc@14 689 end
adamc@14 690
adamc@15 691 fun typeof env (e, loc) =
adamc@15 692 case e of
adamc@15 693 L'.EPrim p => (primType env p, loc)
adamc@15 694 | L'.ERel n => #2 (E.lookupERel env n)
adamc@15 695 | L'.ENamed n => #2 (E.lookupENamed env n)
adamc@15 696 | L'.EApp (e1, _) =>
adamc@15 697 (case #1 (typeof env e1) of
adamc@15 698 L'.TFun (_, c) => c
adamc@15 699 | _ => raise Fail "typeof: Bad EApp")
adamc@26 700 | L'.EAbs (_, _, ran, _) => ran
adamc@15 701 | L'.ECApp (e1, c) =>
adamc@15 702 (case #1 (typeof env e1) of
adamc@15 703 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
adamc@15 704 | _ => raise Fail "typeof: Bad ECApp")
adamc@15 705 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
adamc@15 706
adamc@15 707 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, e) => (x, typeof env e)) xes), loc), loc)
adamc@15 708 | L'.EField (_, _, {field, ...}) => field
adamc@15 709
adamc@15 710 | L'.EError => cerror
adamc@15 711
adamc@15 712 fun elabHead env (e as (_, loc)) t =
adamc@15 713 let
adamc@15 714 fun unravel (t, e) =
adamc@15 715 case hnormCon env t of
adamc@15 716 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@15 717 let
adamc@15 718 val u = cunif k
adamc@15 719 in
adamc@15 720 unravel (subConInCon (0, u) t',
adamc@15 721 (L'.ECApp (e, u), loc))
adamc@15 722 end
adamc@15 723 | _ => (e, t)
adamc@15 724 in
adamc@15 725 unravel (t, e)
adamc@15 726 end
adamc@15 727
adamc@10 728 fun elabExp env (e, loc) =
adamc@10 729 case e of
adamc@10 730 L.EAnnot (e, t) =>
adamc@10 731 let
adamc@10 732 val (e', et) = elabExp env e
adamc@10 733 val (t', _) = elabCon env t
adamc@10 734 in
adamc@10 735 checkCon env e' et t';
adamc@10 736 (e', t')
adamc@10 737 end
adamc@10 738
adamc@14 739 | L.EPrim p => ((L'.EPrim p, loc), (primType env p, loc))
adamc@10 740 | L.EVar s =>
adamc@10 741 (case E.lookupE env s of
adamc@10 742 E.NotBound =>
adamc@10 743 (expError env (UnboundExp (loc, s));
adamc@10 744 (eerror, cerror))
adamc@10 745 | E.Rel (n, t) => ((L'.ERel n, loc), t)
adamc@10 746 | E.Named (n, t) => ((L'.ENamed n, loc), t))
adamc@10 747 | L.EApp (e1, e2) =>
adamc@10 748 let
adamc@10 749 val (e1', t1) = elabExp env e1
adamc@15 750 val (e1', t1) = elabHead env e1' t1
adamc@10 751 val (e2', t2) = elabExp env e2
adamc@10 752
adamc@10 753 val dom = cunif ktype
adamc@10 754 val ran = cunif ktype
adamc@10 755 val t = (L'.TFun (dom, ran), dummy)
adamc@10 756 in
adamc@10 757 checkCon env e1' t1 t;
adamc@10 758 checkCon env e2' t2 dom;
adamc@10 759 ((L'.EApp (e1', e2'), loc), ran)
adamc@10 760 end
adamc@10 761 | L.EAbs (x, to, e) =>
adamc@10 762 let
adamc@10 763 val t' = case to of
adamc@10 764 NONE => cunif ktype
adamc@10 765 | SOME t =>
adamc@10 766 let
adamc@10 767 val (t', tk) = elabCon env t
adamc@10 768 in
adamc@10 769 checkKind env t' tk ktype;
adamc@10 770 t'
adamc@10 771 end
adamc@10 772 val (e', et) = elabExp (E.pushERel env x t') e
adamc@10 773 in
adamc@26 774 ((L'.EAbs (x, t', et, e'), loc),
adamc@10 775 (L'.TFun (t', et), loc))
adamc@10 776 end
adamc@10 777 | L.ECApp (e, c) =>
adamc@10 778 let
adamc@10 779 val (e', et) = elabExp env e
adamc@15 780 val (e', et) = elabHead env e' et
adamc@10 781 val (c', ck) = elabCon env c
adamc@10 782 in
adamc@11 783 case #1 (hnormCon env et) of
adamc@11 784 L'.CError => (eerror, cerror)
adamc@11 785 | L'.TCFun (_, _, k, eb) =>
adamc@11 786 let
adamc@11 787 val () = checkKind env c' ck k
adamc@11 788 val eb' = subConInCon (0, c') eb
adamc@11 789 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@11 790 cerror)
adamc@11 791 in
adamc@11 792 ((L'.ECApp (e', c'), loc), eb')
adamc@11 793 end
adamc@11 794
adamc@11 795 | L'.CUnif _ =>
adamc@11 796 (expError env (Unif ("application", et));
adamc@11 797 (eerror, cerror))
adamc@11 798
adamc@11 799 | _ =>
adamc@11 800 (expError env (WrongForm ("constructor function", e', et));
adamc@11 801 (eerror, cerror))
adamc@10 802 end
adamc@11 803 | L.ECAbs (expl, x, k, e) =>
adamc@11 804 let
adamc@11 805 val expl' = elabExplicitness expl
adamc@11 806 val k' = elabKind k
adamc@11 807 val (e', et) = elabExp (E.pushCRel env x k') e
adamc@11 808 in
adamc@11 809 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@11 810 (L'.TCFun (expl', x, k', et), loc))
adamc@11 811 end
adamc@6 812
adamc@12 813 | L.ERecord xes =>
adamc@12 814 let
adamc@12 815 val xes' = map (fn (x, e) =>
adamc@12 816 let
adamc@12 817 val (x', xk) = elabCon env x
adamc@12 818 val (e', et) = elabExp env e
adamc@12 819 in
adamc@12 820 checkKind env x' xk kname;
adamc@12 821 (x', e', et)
adamc@12 822 end) xes
adamc@12 823 in
adamc@12 824 ((L'.ERecord (map (fn (x', e', _) => (x', e')) xes'), loc),
adamc@12 825 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc))
adamc@12 826 end
adamc@12 827
adamc@12 828 | L.EField (e, c) =>
adamc@12 829 let
adamc@12 830 val (e', et) = elabExp env e
adamc@12 831 val (c', ck) = elabCon env c
adamc@12 832
adamc@12 833 val ft = cunif ktype
adamc@12 834 val rest = cunif ktype_record
adamc@12 835 in
adamc@12 836 checkKind env c' ck kname;
adamc@12 837 checkCon env e' et (L'.TRecord (L'.CConcat ((L'.CRecord (ktype, [(c', ft)]), loc), rest), loc), loc);
adamc@12 838 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft)
adamc@12 839 end
adamc@12 840
adamc@12 841
adamc@6 842 datatype decl_error =
adamc@6 843 KunifsRemainKind of ErrorMsg.span * L'.kind
adamc@6 844 | KunifsRemainCon of ErrorMsg.span * L'.con
adamc@10 845 | KunifsRemainExp of ErrorMsg.span * L'.exp
adamc@10 846 | CunifsRemainCon of ErrorMsg.span * L'.con
adamc@10 847 | CunifsRemainExp of ErrorMsg.span * L'.exp
adamc@6 848
adamc@6 849 fun declError env err =
adamc@6 850 case err of
adamc@6 851 KunifsRemainKind (loc, k) =>
adamc@6 852 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in kind";
adamc@6 853 eprefaces' [("Kind", p_kind k)])
adamc@6 854 | KunifsRemainCon (loc, c) =>
adamc@6 855 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in constructor";
adamc@6 856 eprefaces' [("Constructor", p_con env c)])
adamc@10 857 | KunifsRemainExp (loc, e) =>
adamc@10 858 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in expression";
adamc@10 859 eprefaces' [("Expression", p_exp env e)])
adamc@10 860 | CunifsRemainCon (loc, c) =>
adamc@10 861 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in constructor";
adamc@10 862 eprefaces' [("Constructor", p_con env c)])
adamc@10 863 | CunifsRemainExp (loc, e) =>
adamc@10 864 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in expression";
adamc@10 865 eprefaces' [("Expression", p_exp env e)])
adamc@6 866
adamc@3 867 fun elabDecl env (d, loc) =
adamc@5 868 (resetKunif ();
adamc@12 869 resetCunif ();
adamc@5 870 case d of
adamc@5 871 L.DCon (x, ko, c) =>
adamc@5 872 let
adamc@5 873 val k' = case ko of
adamc@5 874 NONE => kunif ()
adamc@5 875 | SOME k => elabKind k
adamc@3 876
adamc@5 877 val (c', ck) = elabCon env c
adamc@11 878 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@5 879 in
adamc@5 880 checkKind env c' ck k';
adamc@6 881
adamc@15 882 if ErrorMsg.anyErrors () then
adamc@15 883 ()
adamc@15 884 else (
adamc@15 885 if kunifsInKind k' then
adamc@15 886 declError env (KunifsRemainKind (loc, k'))
adamc@15 887 else
adamc@15 888 ();
adamc@6 889
adamc@15 890 if kunifsInCon c' then
adamc@15 891 declError env (KunifsRemainCon (loc, c'))
adamc@15 892 else
adamc@15 893 ()
adamc@15 894 );
adamc@6 895
adamc@5 896 (env',
adamc@5 897 (L'.DCon (x, n, k', c'), loc))
adamc@10 898 end
adamc@10 899 | L.DVal (x, co, e) =>
adamc@10 900 let
adamc@10 901 val (c', ck) = case co of
adamc@10 902 NONE => (cunif ktype, ktype)
adamc@10 903 | SOME c => elabCon env c
adamc@10 904
adamc@10 905 val (e', et) = elabExp env e
adamc@10 906 val (env', n) = E.pushENamed env x c'
adamc@10 907 in
adamc@10 908 checkCon env e' et c';
adamc@10 909
adamc@15 910 if ErrorMsg.anyErrors () then
adamc@15 911 ()
adamc@15 912 else (
adamc@15 913 if kunifsInCon c' then
adamc@15 914 declError env (KunifsRemainCon (loc, c'))
adamc@15 915 else
adamc@15 916 ();
adamc@10 917
adamc@15 918 if cunifsInCon c' then
adamc@15 919 declError env (CunifsRemainCon (loc, c'))
adamc@15 920 else
adamc@15 921 ();
adamc@10 922
adamc@15 923 if kunifsInExp e' then
adamc@15 924 declError env (KunifsRemainExp (loc, e'))
adamc@15 925 else
adamc@15 926 ();
adamc@10 927
adamc@15 928 if cunifsInExp e' then
adamc@15 929 declError env (CunifsRemainExp (loc, e'))
adamc@15 930 else
adamc@15 931 ());
adamc@10 932
adamc@10 933 (env',
adamc@10 934 (L'.DVal (x, n, c', e'), loc))
adamc@5 935 end)
adamc@3 936
adamc@5 937 fun elabFile env ds =
adamc@5 938 ListUtil.mapfoldl (fn (d, env) => elabDecl env d) env ds
adamc@2 939
adamc@2 940 end