annotate src/elaborate.sml @ 33:535c324f0b35

Matching structures in signatures
author Adam Chlipala <adamc@hcoop.net>
date Thu, 12 Jun 2008 17:41:32 -0400
parents 0ff8c2728634
children 44b5405e74c7
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@31 138 val sgnerror = (L'.SgnError, dummy)
adamc@31 139 val strerror = (L'.StrError, dummy)
adamc@3 140
adamc@3 141 local
adamc@3 142 val count = ref 0
adamc@3 143 in
adamc@3 144
adamc@3 145 fun resetKunif () = count := 0
adamc@3 146
adamc@3 147 fun kunif () =
adamc@3 148 let
adamc@3 149 val n = !count
adamc@3 150 val s = if n <= 26 then
adamc@3 151 str (chr (ord #"A" + n))
adamc@3 152 else
adamc@3 153 "U" ^ Int.toString (n - 26)
adamc@3 154 in
adamc@3 155 count := n + 1;
adamc@3 156 (L'.KUnif (s, ref NONE), dummy)
adamc@3 157 end
adamc@3 158
adamc@3 159 end
adamc@3 160
adamc@10 161 local
adamc@10 162 val count = ref 0
adamc@10 163 in
adamc@10 164
adamc@10 165 fun resetCunif () = count := 0
adamc@10 166
adamc@10 167 fun cunif k =
adamc@10 168 let
adamc@10 169 val n = !count
adamc@10 170 val s = if n <= 26 then
adamc@10 171 str (chr (ord #"A" + n))
adamc@10 172 else
adamc@10 173 "U" ^ Int.toString (n - 26)
adamc@10 174 in
adamc@10 175 count := n + 1;
adamc@10 176 (L'.CUnif (k, s, ref NONE), dummy)
adamc@10 177 end
adamc@10 178
adamc@10 179 end
adamc@10 180
adamc@18 181 fun elabKind (k, loc) =
adamc@18 182 case k of
adamc@18 183 L.KType => (L'.KType, loc)
adamc@18 184 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
adamc@18 185 | L.KName => (L'.KName, loc)
adamc@18 186 | L.KRecord k => (L'.KRecord (elabKind k), loc)
adamc@18 187 | L.KWild => kunif ()
adamc@18 188
adamc@3 189 fun elabCon env (c, loc) =
adamc@3 190 case c of
adamc@3 191 L.CAnnot (c, k) =>
adamc@3 192 let
adamc@3 193 val k' = elabKind k
adamc@3 194 val (c', ck) = elabCon env c
adamc@3 195 in
adamc@5 196 checkKind env c' ck k';
adamc@3 197 (c', k')
adamc@3 198 end
adamc@3 199
adamc@3 200 | L.TFun (t1, t2) =>
adamc@3 201 let
adamc@3 202 val (t1', k1) = elabCon env t1
adamc@3 203 val (t2', k2) = elabCon env t2
adamc@3 204 in
adamc@5 205 checkKind env t1' k1 ktype;
adamc@5 206 checkKind env t2' k2 ktype;
adamc@3 207 ((L'.TFun (t1', t2'), loc), ktype)
adamc@3 208 end
adamc@3 209 | L.TCFun (e, x, k, t) =>
adamc@3 210 let
adamc@3 211 val e' = elabExplicitness e
adamc@3 212 val k' = elabKind k
adamc@3 213 val env' = E.pushCRel env x k'
adamc@3 214 val (t', tk) = elabCon env' t
adamc@3 215 in
adamc@5 216 checkKind env t' tk ktype;
adamc@3 217 ((L'.TCFun (e', x, k', t'), loc), ktype)
adamc@3 218 end
adamc@3 219 | L.TRecord c =>
adamc@3 220 let
adamc@3 221 val (c', ck) = elabCon env c
adamc@3 222 val k = (L'.KRecord ktype, loc)
adamc@3 223 in
adamc@5 224 checkKind env c' ck k;
adamc@3 225 ((L'.TRecord c', loc), ktype)
adamc@3 226 end
adamc@3 227
adamc@3 228 | L.CVar s =>
adamc@3 229 (case E.lookupC env s of
adamc@9 230 E.NotBound =>
adamc@5 231 (conError env (UnboundCon (loc, s));
adamc@3 232 (cerror, kerror))
adamc@9 233 | E.Rel (n, k) =>
adamc@3 234 ((L'.CRel n, loc), k)
adamc@9 235 | E.Named (n, k) =>
adamc@3 236 ((L'.CNamed n, loc), k))
adamc@3 237 | L.CApp (c1, c2) =>
adamc@3 238 let
adamc@3 239 val (c1', k1) = elabCon env c1
adamc@3 240 val (c2', k2) = elabCon env c2
adamc@3 241 val dom = kunif ()
adamc@3 242 val ran = kunif ()
adamc@3 243 in
adamc@5 244 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@5 245 checkKind env c2' k2 dom;
adamc@3 246 ((L'.CApp (c1', c2'), loc), ran)
adamc@3 247 end
adamc@8 248 | L.CAbs (x, k, t) =>
adamc@3 249 let
adamc@3 250 val k' = elabKind k
adamc@3 251 val env' = E.pushCRel env x k'
adamc@3 252 val (t', tk) = elabCon env' t
adamc@3 253 in
adamc@8 254 ((L'.CAbs (x, k', t'), loc),
adamc@3 255 (L'.KArrow (k', tk), loc))
adamc@3 256 end
adamc@3 257
adamc@3 258 | L.CName s =>
adamc@3 259 ((L'.CName s, loc), kname)
adamc@3 260
adamc@3 261 | L.CRecord xcs =>
adamc@3 262 let
adamc@3 263 val k = kunif ()
adamc@3 264
adamc@3 265 val xcs' = map (fn (x, c) =>
adamc@3 266 let
adamc@3 267 val (x', xk) = elabCon env x
adamc@3 268 val (c', ck) = elabCon env c
adamc@3 269 in
adamc@5 270 checkKind env x' xk kname;
adamc@5 271 checkKind env c' ck k;
adamc@3 272 (x', c')
adamc@3 273 end) xcs
adamc@3 274 in
adamc@5 275 ((L'.CRecord (k, xcs'), loc), (L'.KRecord k, loc))
adamc@3 276 end
adamc@3 277 | L.CConcat (c1, c2) =>
adamc@3 278 let
adamc@3 279 val (c1', k1) = elabCon env c1
adamc@3 280 val (c2', k2) = elabCon env c2
adamc@3 281 val ku = kunif ()
adamc@3 282 val k = (L'.KRecord ku, loc)
adamc@3 283 in
adamc@5 284 checkKind env c1' k1 k;
adamc@5 285 checkKind env c2' k2 k;
adamc@3 286 ((L'.CConcat (c1', c2'), loc), k)
adamc@3 287 end
adamc@3 288
adamc@18 289 | L.CWild k =>
adamc@18 290 let
adamc@18 291 val k' = elabKind k
adamc@18 292 in
adamc@18 293 (cunif k', k')
adamc@18 294 end
adamc@18 295
adamc@6 296 fun kunifsRemain k =
adamc@6 297 case k of
adamc@6 298 L'.KUnif (_, ref NONE) => true
adamc@6 299 | _ => false
adamc@10 300 fun cunifsRemain c =
adamc@10 301 case c of
adamc@10 302 L'.CUnif (_, _, ref NONE) => true
adamc@10 303 | _ => false
adamc@6 304
adamc@6 305 val kunifsInKind = U.Kind.exists kunifsRemain
adamc@6 306 val kunifsInCon = U.Con.exists {kind = kunifsRemain,
adamc@6 307 con = fn _ => false}
adamc@10 308 val kunifsInExp = U.Exp.exists {kind = kunifsRemain,
adamc@10 309 con = fn _ => false,
adamc@10 310 exp = fn _ => false}
adamc@10 311
adamc@10 312 val cunifsInCon = U.Con.exists {kind = fn _ => false,
adamc@10 313 con = cunifsRemain}
adamc@10 314 val cunifsInExp = U.Exp.exists {kind = fn _ => false,
adamc@10 315 con = cunifsRemain,
adamc@10 316 exp = fn _ => false}
adamc@10 317
adamc@10 318 fun occursCon r =
adamc@10 319 U.Con.exists {kind = fn _ => false,
adamc@10 320 con = fn L'.CUnif (_, _, r') => r = r'
adamc@10 321 | _ => false}
adamc@10 322
adamc@10 323 datatype cunify_error =
adamc@10 324 CKind of L'.kind * L'.kind * kunify_error
adamc@10 325 | COccursCheckFailed of L'.con * L'.con
adamc@10 326 | CIncompatible of L'.con * L'.con
adamc@10 327 | CExplicitness of L'.con * L'.con
adamc@12 328 | CKindof of L'.con
adamc@12 329 | CRecordFailure
adamc@10 330
adamc@10 331 exception CUnify' of cunify_error
adamc@10 332
adamc@10 333 fun cunifyError env err =
adamc@10 334 case err of
adamc@10 335 CKind (k1, k2, kerr) =>
adamc@10 336 (eprefaces "Kind unification failure"
adamc@10 337 [("Kind 1", p_kind k1),
adamc@10 338 ("Kind 2", p_kind k2)];
adamc@10 339 kunifyError kerr)
adamc@10 340 | COccursCheckFailed (c1, c2) =>
adamc@10 341 eprefaces "Constructor occurs check failed"
adamc@10 342 [("Con 1", p_con env c1),
adamc@10 343 ("Con 2", p_con env c2)]
adamc@10 344 | CIncompatible (c1, c2) =>
adamc@10 345 eprefaces "Incompatible constructors"
adamc@10 346 [("Con 1", p_con env c1),
adamc@10 347 ("Con 2", p_con env c2)]
adamc@10 348 | CExplicitness (c1, c2) =>
adamc@10 349 eprefaces "Differing constructor function explicitness"
adamc@10 350 [("Con 1", p_con env c1),
adamc@10 351 ("Con 2", p_con env c2)]
adamc@12 352 | CKindof c =>
adamc@12 353 eprefaces "Kind unification variable blocks kindof calculation"
adamc@12 354 [("Con", p_con env c)]
adamc@12 355 | CRecordFailure =>
adamc@12 356 eprefaces "Can't unify record constructors" []
adamc@10 357
adamc@13 358 exception SynUnif = E.SynUnif
adamc@13 359 val liftConInCon = E.liftConInCon
adamc@12 360
adamc@12 361 val subConInCon =
adamc@12 362 U.Con.mapB {kind = fn k => k,
adamc@12 363 con = fn (xn, rep) => fn c =>
adamc@12 364 case c of
adamc@12 365 L'.CRel xn' =>
adamc@12 366 if xn = xn' then
adamc@12 367 #1 rep
adamc@12 368 else
adamc@12 369 c
adamc@17 370 (*| L'.CUnif _ => raise SynUnif*)
adamc@12 371 | _ => c,
adamc@12 372 bind = fn ((xn, rep), U.Con.Rel _) => (xn+1, liftConInCon 0 rep)
adamc@12 373 | (ctx, _) => ctx}
adamc@12 374
adamc@12 375 type record_summary = {
adamc@12 376 fields : (L'.con * L'.con) list,
adamc@12 377 unifs : (L'.con * L'.con option ref) list,
adamc@12 378 others : L'.con list
adamc@12 379 }
adamc@12 380
adamc@12 381 fun summaryToCon {fields, unifs, others} =
adamc@12 382 let
adamc@12 383 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 384 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 385 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 386 in
adamc@12 387 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 388 end
adamc@12 389
adamc@12 390 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 391
adamc@12 392 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 393
adamc@12 394 fun hnormKind (kAll as (k, _)) =
adamc@12 395 case k of
adamc@12 396 L'.KUnif (_, ref (SOME k)) => hnormKind k
adamc@12 397 | _ => kAll
adamc@12 398
adamc@12 399 fun kindof env (c, loc) =
adamc@12 400 case c of
adamc@12 401 L'.TFun _ => ktype
adamc@12 402 | L'.TCFun _ => ktype
adamc@12 403 | L'.TRecord _ => ktype
adamc@12 404
adamc@12 405 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 406 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@12 407 | L'.CApp (c, _) =>
adamc@12 408 (case #1 (hnormKind (kindof env c)) of
adamc@12 409 L'.KArrow (_, k) => k
adamc@12 410 | L'.KError => kerror
adamc@12 411 | _ => raise CUnify' (CKindof c))
adamc@12 412 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@12 413
adamc@12 414 | L'.CName _ => kname
adamc@12 415
adamc@12 416 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 417 | L'.CConcat (c, _) => kindof env c
adamc@12 418
adamc@12 419 | L'.CError => kerror
adamc@12 420 | L'.CUnif (k, _, _) => k
adamc@12 421
adamc@12 422 fun unifyRecordCons env (c1, c2) =
adamc@12 423 let
adamc@12 424 val k1 = kindof env c1
adamc@12 425 val k2 = kindof env c2
adamc@12 426 in
adamc@12 427 unifyKinds k1 k2;
adamc@12 428 unifySummaries env (k1, recordSummary env c1, recordSummary env c2)
adamc@12 429 end
adamc@12 430
adamc@12 431 and recordSummary env c : record_summary =
adamc@12 432 case hnormCon env c of
adamc@12 433 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
adamc@12 434 | (L'.CConcat (c1, c2), _) =>
adamc@12 435 let
adamc@12 436 val s1 = recordSummary env c1
adamc@12 437 val s2 = recordSummary env c2
adamc@12 438 in
adamc@12 439 {fields = #fields s1 @ #fields s2,
adamc@12 440 unifs = #unifs s1 @ #unifs s2,
adamc@12 441 others = #others s1 @ #others s2}
adamc@12 442 end
adamc@12 443 | (L'.CUnif (_, _, ref (SOME c)), _) => recordSummary env c
adamc@12 444 | c' as (L'.CUnif (_, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
adamc@12 445 | c' => {fields = [], unifs = [], others = [c']}
adamc@12 446
adamc@12 447 and consEq env (c1, c2) =
adamc@12 448 (unifyCons env c1 c2;
adamc@12 449 true)
adamc@12 450 handle CUnify _ => false
adamc@12 451
adamc@12 452 and unifySummaries env (k, s1 : record_summary, s2 : record_summary) =
adamc@12 453 let
adamc@14 454 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@14 455 ("#2", p_summary env s2)]*)
adamc@12 456
adamc@12 457 fun eatMatching p (ls1, ls2) =
adamc@12 458 let
adamc@12 459 fun em (ls1, ls2, passed1) =
adamc@12 460 case ls1 of
adamc@12 461 [] => (rev passed1, ls2)
adamc@12 462 | h1 :: t1 =>
adamc@12 463 let
adamc@12 464 fun search (ls2', passed2) =
adamc@12 465 case ls2' of
adamc@12 466 [] => em (t1, ls2, h1 :: passed1)
adamc@12 467 | h2 :: t2 =>
adamc@12 468 if p (h1, h2) then
adamc@12 469 em (t1, List.revAppend (passed2, t2), passed1)
adamc@12 470 else
adamc@12 471 search (t2, h2 :: passed2)
adamc@12 472 in
adamc@12 473 search (ls2, [])
adamc@12 474 end
adamc@12 475 in
adamc@12 476 em (ls1, ls2, [])
adamc@12 477 end
adamc@12 478
adamc@12 479 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@12 480 if consEq env (x1, x2) then
adamc@12 481 (unifyCons env c1 c2;
adamc@12 482 true)
adamc@12 483 else
adamc@12 484 false) (#fields s1, #fields s2)
adamc@14 485 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@14 486 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@12 487 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@12 488 val (others1, others2) = eatMatching (consEq env) (#others s1, #others s2)
adamc@12 489
adamc@12 490 fun unifFields (fs, others, unifs) =
adamc@12 491 case (fs, others, unifs) of
adamc@12 492 ([], [], _) => ([], [], unifs)
adamc@12 493 | (_, _, []) => (fs, others, [])
adamc@12 494 | (_, _, (_, r) :: rest) =>
adamc@12 495 let
adamc@12 496 val r' = ref NONE
adamc@12 497 val cr' = (L'.CUnif (k, "recd", r'), dummy)
adamc@12 498
adamc@12 499 val prefix = case (fs, others) of
adamc@12 500 ([], other :: others) =>
adamc@12 501 List.foldl (fn (other, c) =>
adamc@12 502 (L'.CConcat (c, other), dummy))
adamc@12 503 other others
adamc@12 504 | (fs, []) =>
adamc@12 505 (L'.CRecord (k, fs), dummy)
adamc@12 506 | (fs, others) =>
adamc@12 507 List.foldl (fn (other, c) =>
adamc@12 508 (L'.CConcat (c, other), dummy))
adamc@12 509 (L'.CRecord (k, fs), dummy) others
adamc@12 510 in
adamc@12 511 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 512 ([], [], (cr', r') :: rest)
adamc@12 513 end
adamc@12 514
adamc@12 515 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 516 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 517
adamc@12 518 val clear1 = case (fs1, others1) of
adamc@12 519 ([], []) => true
adamc@12 520 | _ => false
adamc@12 521 val clear2 = case (fs2, others2) of
adamc@12 522 ([], []) => true
adamc@12 523 | _ => false
adamc@12 524 val empty = (L'.CRecord (k, []), dummy)
adamc@12 525 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 526 case (unifs1, unifs2) of
adamc@12 527 ([], _) =>
adamc@12 528 if clear1 then
adamc@12 529 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 530 else
adamc@12 531 raise CUnify' CRecordFailure
adamc@12 532 | (_, []) =>
adamc@12 533 if clear2 then
adamc@12 534 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 535 else
adamc@12 536 raise CUnify' CRecordFailure
adamc@12 537 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 538 (r2 := SOME c1;
adamc@12 539 pairOffUnifs (rest1, rest2))
adamc@12 540 in
adamc@12 541 pairOffUnifs (unifs1, unifs2)
adamc@12 542 end
adamc@12 543
adamc@12 544 and hnormCon env (cAll as (c, _)) =
adamc@11 545 case c of
adamc@11 546 L'.CUnif (_, _, ref (SOME c)) => hnormCon env c
adamc@11 547
adamc@11 548 | L'.CNamed xn =>
adamc@11 549 (case E.lookupCNamed env xn of
adamc@11 550 (_, _, SOME c') => hnormCon env c'
adamc@11 551 | _ => cAll)
adamc@11 552
adamc@12 553 | L'.CApp (c1, c2) =>
adamc@12 554 (case hnormCon env c1 of
adamc@12 555 (L'.CAbs (_, _, cb), _) =>
adamc@12 556 ((hnormCon env (subConInCon (0, c2) cb))
adamc@12 557 handle SynUnif => cAll)
adamc@12 558 | _ => cAll)
adamc@12 559
adamc@12 560 | L'.CConcat (c1, c2) =>
adamc@12 561 (case (hnormCon env c1, hnormCon env c2) of
adamc@12 562 ((L'.CRecord (k, xcs1), loc), (L'.CRecord (_, xcs2), _)) =>
adamc@12 563 (L'.CRecord (k, xcs1 @ xcs2), loc)
adamc@12 564 | _ => cAll)
adamc@12 565
adamc@11 566 | _ => cAll
adamc@11 567
adamc@12 568 and unifyCons' env c1 c2 =
adamc@11 569 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
adamc@11 570
adamc@11 571 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 572 let
adamc@10 573 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 574
adamc@12 575 fun isRecord () = unifyRecordCons env (c1All, c2All)
adamc@10 576 in
adamc@10 577 case (c1, c2) of
adamc@10 578 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@10 579 (unifyCons' env d1 d2;
adamc@10 580 unifyCons' env r1 r2)
adamc@10 581 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 582 if expl1 <> expl2 then
adamc@10 583 err CExplicitness
adamc@10 584 else
adamc@10 585 (unifyKinds d1 d2;
adamc@10 586 unifyCons' (E.pushCRel env x1 d1) r1 r2)
adamc@10 587 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
adamc@10 588
adamc@10 589 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 590 if n1 = n2 then
adamc@10 591 ()
adamc@10 592 else
adamc@10 593 err CIncompatible
adamc@10 594 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 595 if n1 = n2 then
adamc@10 596 ()
adamc@10 597 else
adamc@10 598 err CIncompatible
adamc@10 599
adamc@10 600 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@10 601 (unifyCons' env d1 d2;
adamc@10 602 unifyCons' env r1 r2)
adamc@10 603 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 604 (unifyKinds k1 k2;
adamc@10 605 unifyCons' (E.pushCRel env x1 k1) c1 c2)
adamc@10 606
adamc@10 607 | (L'.CName n1, L'.CName n2) =>
adamc@10 608 if n1 = n2 then
adamc@10 609 ()
adamc@10 610 else
adamc@10 611 err CIncompatible
adamc@10 612
adamc@10 613 | (L'.CError, _) => ()
adamc@10 614 | (_, L'.CError) => ()
adamc@10 615
adamc@10 616 | (L'.CUnif (_, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
adamc@10 617 | (_, L'.CUnif (_, _, ref (SOME c2All))) => unifyCons' env c1All c2All
adamc@10 618
adamc@10 619 | (L'.CUnif (k1, _, r1), L'.CUnif (k2, _, r2)) =>
adamc@10 620 if r1 = r2 then
adamc@10 621 ()
adamc@10 622 else
adamc@10 623 (unifyKinds k1 k2;
adamc@10 624 r1 := SOME c2All)
adamc@10 625
adamc@10 626 | (L'.CUnif (_, _, r), _) =>
adamc@10 627 if occursCon r c2All then
adamc@10 628 err COccursCheckFailed
adamc@10 629 else
adamc@10 630 r := SOME c2All
adamc@10 631 | (_, L'.CUnif (_, _, r)) =>
adamc@10 632 if occursCon r c1All then
adamc@10 633 err COccursCheckFailed
adamc@10 634 else
adamc@10 635 r := SOME c1All
adamc@10 636
adamc@12 637 | (L'.CRecord _, _) => isRecord ()
adamc@12 638 | (_, L'.CRecord _) => isRecord ()
adamc@12 639 | (L'.CConcat _, _) => isRecord ()
adamc@12 640 | (_, L'.CConcat _) => isRecord ()
adamc@12 641
adamc@10 642 | _ => err CIncompatible
adamc@10 643 end
adamc@10 644
adamc@12 645 and unifyCons env c1 c2 =
adamc@10 646 unifyCons' env c1 c2
adamc@10 647 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 648 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 649
adamc@10 650 datatype exp_error =
adamc@10 651 UnboundExp of ErrorMsg.span * string
adamc@10 652 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 653 | Unif of string * L'.con
adamc@11 654 | WrongForm of string * L'.exp * L'.con
adamc@10 655
adamc@10 656 fun expError env err =
adamc@10 657 case err of
adamc@10 658 UnboundExp (loc, s) =>
adamc@10 659 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@10 660 | Unify (e, c1, c2, uerr) =>
adamc@10 661 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 662 eprefaces' [("Expression", p_exp env e),
adamc@10 663 ("Have con", p_con env c1),
adamc@10 664 ("Need con", p_con env c2)];
adamc@10 665 cunifyError env uerr)
adamc@11 666 | Unif (action, c) =>
adamc@11 667 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 668 eprefaces' [("Con", p_con env c)])
adamc@11 669 | WrongForm (variety, e, t) =>
adamc@11 670 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 671 eprefaces' [("Expression", p_exp env e),
adamc@11 672 ("Type", p_con env t)])
adamc@10 673
adamc@10 674 fun checkCon env e c1 c2 =
adamc@10 675 unifyCons env c1 c2
adamc@10 676 handle CUnify (c1, c2, err) =>
adamc@10 677 expError env (Unify (e, c1, c2, err))
adamc@10 678
adamc@14 679 fun primType env p =
adamc@14 680 let
adamc@14 681 val s = case p of
adamc@14 682 P.Int _ => "int"
adamc@14 683 | P.Float _ => "float"
adamc@14 684 | P.String _ => "string"
adamc@14 685 in
adamc@14 686 case E.lookupC env s of
adamc@14 687 E.NotBound => raise Fail ("Primitive type " ^ s ^ " unbound")
adamc@14 688 | E.Rel _ => raise Fail ("Primitive type " ^ s ^ " bound as relative")
adamc@14 689 | E.Named (n, (L'.KType, _)) => L'.CNamed n
adamc@14 690 | E.Named _ => raise Fail ("Primitive type " ^ s ^ " bound at non-Type kind")
adamc@14 691 end
adamc@14 692
adamc@15 693 fun typeof env (e, loc) =
adamc@15 694 case e of
adamc@15 695 L'.EPrim p => (primType env p, loc)
adamc@15 696 | L'.ERel n => #2 (E.lookupERel env n)
adamc@15 697 | L'.ENamed n => #2 (E.lookupENamed env n)
adamc@15 698 | L'.EApp (e1, _) =>
adamc@15 699 (case #1 (typeof env e1) of
adamc@15 700 L'.TFun (_, c) => c
adamc@15 701 | _ => raise Fail "typeof: Bad EApp")
adamc@26 702 | L'.EAbs (_, _, ran, _) => ran
adamc@15 703 | L'.ECApp (e1, c) =>
adamc@15 704 (case #1 (typeof env e1) of
adamc@15 705 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
adamc@15 706 | _ => raise Fail "typeof: Bad ECApp")
adamc@15 707 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
adamc@15 708
adamc@29 709 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
adamc@15 710 | L'.EField (_, _, {field, ...}) => field
adamc@15 711
adamc@15 712 | L'.EError => cerror
adamc@15 713
adamc@15 714 fun elabHead env (e as (_, loc)) t =
adamc@15 715 let
adamc@15 716 fun unravel (t, e) =
adamc@15 717 case hnormCon env t of
adamc@15 718 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@15 719 let
adamc@15 720 val u = cunif k
adamc@15 721 in
adamc@15 722 unravel (subConInCon (0, u) t',
adamc@15 723 (L'.ECApp (e, u), loc))
adamc@15 724 end
adamc@15 725 | _ => (e, t)
adamc@15 726 in
adamc@15 727 unravel (t, e)
adamc@15 728 end
adamc@15 729
adamc@10 730 fun elabExp env (e, loc) =
adamc@10 731 case e of
adamc@10 732 L.EAnnot (e, t) =>
adamc@10 733 let
adamc@10 734 val (e', et) = elabExp env e
adamc@10 735 val (t', _) = elabCon env t
adamc@10 736 in
adamc@10 737 checkCon env e' et t';
adamc@10 738 (e', t')
adamc@10 739 end
adamc@10 740
adamc@14 741 | L.EPrim p => ((L'.EPrim p, loc), (primType env p, loc))
adamc@10 742 | L.EVar s =>
adamc@10 743 (case E.lookupE env s of
adamc@10 744 E.NotBound =>
adamc@10 745 (expError env (UnboundExp (loc, s));
adamc@10 746 (eerror, cerror))
adamc@10 747 | E.Rel (n, t) => ((L'.ERel n, loc), t)
adamc@10 748 | E.Named (n, t) => ((L'.ENamed n, loc), t))
adamc@10 749 | L.EApp (e1, e2) =>
adamc@10 750 let
adamc@10 751 val (e1', t1) = elabExp env e1
adamc@15 752 val (e1', t1) = elabHead env e1' t1
adamc@10 753 val (e2', t2) = elabExp env e2
adamc@10 754
adamc@10 755 val dom = cunif ktype
adamc@10 756 val ran = cunif ktype
adamc@10 757 val t = (L'.TFun (dom, ran), dummy)
adamc@10 758 in
adamc@10 759 checkCon env e1' t1 t;
adamc@10 760 checkCon env e2' t2 dom;
adamc@10 761 ((L'.EApp (e1', e2'), loc), ran)
adamc@10 762 end
adamc@10 763 | L.EAbs (x, to, e) =>
adamc@10 764 let
adamc@10 765 val t' = case to of
adamc@10 766 NONE => cunif ktype
adamc@10 767 | SOME t =>
adamc@10 768 let
adamc@10 769 val (t', tk) = elabCon env t
adamc@10 770 in
adamc@10 771 checkKind env t' tk ktype;
adamc@10 772 t'
adamc@10 773 end
adamc@10 774 val (e', et) = elabExp (E.pushERel env x t') e
adamc@10 775 in
adamc@26 776 ((L'.EAbs (x, t', et, e'), loc),
adamc@10 777 (L'.TFun (t', et), loc))
adamc@10 778 end
adamc@10 779 | L.ECApp (e, c) =>
adamc@10 780 let
adamc@10 781 val (e', et) = elabExp env e
adamc@15 782 val (e', et) = elabHead env e' et
adamc@10 783 val (c', ck) = elabCon env c
adamc@10 784 in
adamc@11 785 case #1 (hnormCon env et) of
adamc@11 786 L'.CError => (eerror, cerror)
adamc@11 787 | L'.TCFun (_, _, k, eb) =>
adamc@11 788 let
adamc@11 789 val () = checkKind env c' ck k
adamc@11 790 val eb' = subConInCon (0, c') eb
adamc@11 791 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@11 792 cerror)
adamc@11 793 in
adamc@11 794 ((L'.ECApp (e', c'), loc), eb')
adamc@11 795 end
adamc@11 796
adamc@11 797 | L'.CUnif _ =>
adamc@11 798 (expError env (Unif ("application", et));
adamc@11 799 (eerror, cerror))
adamc@11 800
adamc@11 801 | _ =>
adamc@11 802 (expError env (WrongForm ("constructor function", e', et));
adamc@11 803 (eerror, cerror))
adamc@10 804 end
adamc@11 805 | L.ECAbs (expl, x, k, e) =>
adamc@11 806 let
adamc@11 807 val expl' = elabExplicitness expl
adamc@11 808 val k' = elabKind k
adamc@11 809 val (e', et) = elabExp (E.pushCRel env x k') e
adamc@11 810 in
adamc@11 811 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@11 812 (L'.TCFun (expl', x, k', et), loc))
adamc@11 813 end
adamc@6 814
adamc@12 815 | L.ERecord xes =>
adamc@12 816 let
adamc@12 817 val xes' = map (fn (x, e) =>
adamc@12 818 let
adamc@12 819 val (x', xk) = elabCon env x
adamc@12 820 val (e', et) = elabExp env e
adamc@12 821 in
adamc@12 822 checkKind env x' xk kname;
adamc@12 823 (x', e', et)
adamc@12 824 end) xes
adamc@12 825 in
adamc@29 826 ((L'.ERecord xes', loc),
adamc@12 827 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc))
adamc@12 828 end
adamc@12 829
adamc@12 830 | L.EField (e, c) =>
adamc@12 831 let
adamc@12 832 val (e', et) = elabExp env e
adamc@12 833 val (c', ck) = elabCon env c
adamc@12 834
adamc@12 835 val ft = cunif ktype
adamc@12 836 val rest = cunif ktype_record
adamc@12 837 in
adamc@12 838 checkKind env c' ck kname;
adamc@12 839 checkCon env e' et (L'.TRecord (L'.CConcat ((L'.CRecord (ktype, [(c', ft)]), loc), rest), loc), loc);
adamc@12 840 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft)
adamc@12 841 end
adamc@12 842
adamc@12 843
adamc@6 844 datatype decl_error =
adamc@6 845 KunifsRemainKind of ErrorMsg.span * L'.kind
adamc@6 846 | KunifsRemainCon of ErrorMsg.span * L'.con
adamc@10 847 | KunifsRemainExp of ErrorMsg.span * L'.exp
adamc@10 848 | CunifsRemainCon of ErrorMsg.span * L'.con
adamc@10 849 | CunifsRemainExp of ErrorMsg.span * L'.exp
adamc@6 850
adamc@6 851 fun declError env err =
adamc@6 852 case err of
adamc@6 853 KunifsRemainKind (loc, k) =>
adamc@6 854 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in kind";
adamc@6 855 eprefaces' [("Kind", p_kind k)])
adamc@6 856 | KunifsRemainCon (loc, c) =>
adamc@6 857 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in constructor";
adamc@6 858 eprefaces' [("Constructor", p_con env c)])
adamc@10 859 | KunifsRemainExp (loc, e) =>
adamc@10 860 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in expression";
adamc@10 861 eprefaces' [("Expression", p_exp env e)])
adamc@10 862 | CunifsRemainCon (loc, c) =>
adamc@10 863 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in constructor";
adamc@10 864 eprefaces' [("Constructor", p_con env c)])
adamc@10 865 | CunifsRemainExp (loc, e) =>
adamc@10 866 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in expression";
adamc@10 867 eprefaces' [("Expression", p_exp env e)])
adamc@6 868
adamc@31 869 datatype sgn_error =
adamc@31 870 UnboundSgn of ErrorMsg.span * string
adamc@31 871 | UnmatchedSgi of L'.sgn_item
adamc@31 872 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 873 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@31 874
adamc@31 875 fun sgnError env err =
adamc@31 876 case err of
adamc@31 877 UnboundSgn (loc, s) =>
adamc@31 878 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 879 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 880 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 881 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 882 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 883 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@31 884 eprefaces' [("Item 1", p_sgn_item env sgi1),
adamc@31 885 ("Item 2", p_sgn_item env sgi2),
adamc@31 886 ("Kind 1", p_kind k1),
adamc@31 887 ("Kind 2", p_kind k2)];
adamc@31 888 kunifyError kerr)
adamc@31 889 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 890 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@31 891 eprefaces' [("Item 1", p_sgn_item env sgi1),
adamc@31 892 ("Item 2", p_sgn_item env sgi2),
adamc@31 893 ("Con 1", p_con env c1),
adamc@31 894 ("Con 2", p_con env c2)];
adamc@31 895 cunifyError env cerr)
adamc@31 896
adamc@31 897 datatype str_error =
adamc@31 898 UnboundStr of ErrorMsg.span * string
adamc@31 899
adamc@31 900 fun strError env err =
adamc@31 901 case err of
adamc@31 902 UnboundStr (loc, s) =>
adamc@31 903 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@31 904
adamc@31 905
adamc@31 906 fun elabSgn_item ((sgi, loc), env) =
adamc@31 907 let
adamc@31 908
adamc@31 909 in
adamc@31 910 resetKunif ();
adamc@31 911 resetCunif ();
adamc@31 912 case sgi of
adamc@31 913 L.SgiConAbs (x, k) =>
adamc@31 914 let
adamc@31 915 val k' = elabKind k
adamc@31 916
adamc@31 917 val (env', n) = E.pushCNamed env x k' NONE
adamc@31 918 in
adamc@31 919 if ErrorMsg.anyErrors () then
adamc@31 920 ()
adamc@31 921 else (
adamc@31 922 if kunifsInKind k' then
adamc@31 923 declError env (KunifsRemainKind (loc, k'))
adamc@31 924 else
adamc@31 925 ()
adamc@31 926 );
adamc@31 927
adamc@31 928 ((L'.SgiConAbs (x, n, k'), loc), env')
adamc@31 929 end
adamc@31 930
adamc@31 931 | L.SgiCon (x, ko, c) =>
adamc@31 932 let
adamc@31 933 val k' = case ko of
adamc@31 934 NONE => kunif ()
adamc@31 935 | SOME k => elabKind k
adamc@31 936
adamc@31 937 val (c', ck) = elabCon env c
adamc@31 938 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@31 939 in
adamc@31 940 checkKind env c' ck k';
adamc@31 941
adamc@31 942 if ErrorMsg.anyErrors () then
adamc@31 943 ()
adamc@31 944 else (
adamc@31 945 if kunifsInKind k' then
adamc@31 946 declError env (KunifsRemainKind (loc, k'))
adamc@31 947 else
adamc@31 948 ();
adamc@31 949
adamc@31 950 if kunifsInCon c' then
adamc@31 951 declError env (KunifsRemainCon (loc, c'))
adamc@31 952 else
adamc@31 953 ()
adamc@31 954 );
adamc@31 955
adamc@31 956 ((L'.SgiCon (x, n, k', c'), loc), env')
adamc@31 957 end
adamc@31 958
adamc@31 959 | L.SgiVal (x, c) =>
adamc@31 960 let
adamc@31 961 val (c', ck) = elabCon env c
adamc@31 962
adamc@31 963 val (env', n) = E.pushENamed env x c'
adamc@31 964 in
adamc@31 965 unifyKinds ck ktype;
adamc@31 966
adamc@31 967 if ErrorMsg.anyErrors () then
adamc@31 968 ()
adamc@31 969 else (
adamc@31 970 if kunifsInCon c' then
adamc@31 971 declError env (KunifsRemainCon (loc, c'))
adamc@31 972 else
adamc@31 973 ()
adamc@31 974 );
adamc@31 975
adamc@31 976 ((L'.SgiVal (x, n, c'), loc), env')
adamc@31 977 end
adamc@31 978
adamc@31 979 | L.SgiStr (x, sgn) =>
adamc@31 980 let
adamc@31 981 val sgn' = elabSgn env sgn
adamc@31 982 val (env', n) = E.pushStrNamed env x sgn'
adamc@31 983 in
adamc@31 984 ((L'.SgiStr (x, n, sgn'), loc), env')
adamc@31 985 end
adamc@31 986
adamc@31 987 end
adamc@31 988
adamc@31 989 and elabSgn env (sgn, loc) =
adamc@31 990 case sgn of
adamc@31 991 L.SgnConst sgis =>
adamc@31 992 let
adamc@31 993 val (sgis', _) = ListUtil.foldlMap elabSgn_item env sgis
adamc@31 994 in
adamc@31 995 (L'.SgnConst sgis', loc)
adamc@31 996 end
adamc@31 997 | L.SgnVar x =>
adamc@31 998 (case E.lookupSgn env x of
adamc@31 999 NONE =>
adamc@31 1000 (sgnError env (UnboundSgn (loc, x));
adamc@31 1001 (L'.SgnError, loc))
adamc@31 1002 | SOME (n, sgis) => (L'.SgnVar n, loc))
adamc@31 1003
adamc@31 1004 fun sgiOfDecl (d, loc) =
adamc@31 1005 case d of
adamc@31 1006 L'.DCon (x, n, k, c) => SOME (L'.SgiCon (x, n, k, c), loc)
adamc@31 1007 | L'.DVal (x, n, t, _) => SOME (L'.SgiVal (x, n, t), loc)
adamc@31 1008 | L'.DSgn _ => NONE
adamc@31 1009 | L'.DStr (x, n, sgn, _) => SOME (L'.SgiStr (x, n, sgn), loc)
adamc@31 1010
adamc@31 1011 fun subSgn env (all1 as (sgn1, _)) (all2 as (sgn2, loc2)) =
adamc@31 1012 case (sgn1, sgn2) of
adamc@31 1013 (L'.SgnError, _) => ()
adamc@31 1014 | (_, L'.SgnError) => ()
adamc@31 1015
adamc@31 1016 | (L'.SgnVar n, _) =>
adamc@31 1017 subSgn env (#2 (E.lookupSgnNamed env n)) all2
adamc@31 1018 | (_, L'.SgnVar n) =>
adamc@31 1019 subSgn env all1 (#2 (E.lookupSgnNamed env n))
adamc@31 1020
adamc@31 1021 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1022 let
adamc@31 1023 fun folder (sgi2All as (sgi, _), env) =
adamc@31 1024 let
adamc@31 1025 fun seek p =
adamc@31 1026 let
adamc@31 1027 fun seek env ls =
adamc@31 1028 case ls of
adamc@31 1029 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@31 1030 env)
adamc@31 1031 | h :: t =>
adamc@31 1032 case p h of
adamc@31 1033 NONE => seek (E.sgiBinds env h) t
adamc@31 1034 | SOME env => env
adamc@31 1035 in
adamc@31 1036 seek env sgis1
adamc@31 1037 end
adamc@31 1038 in
adamc@31 1039 case sgi of
adamc@31 1040 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1041 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1042 let
adamc@31 1043 fun found (x, n1, k1, co1) =
adamc@31 1044 let
adamc@31 1045 val () = unifyKinds k1 k2
adamc@31 1046 handle KUnify (k1, k2, err) =>
adamc@31 1047 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@31 1048 val env = E.pushCNamedAs env x n1 k1 co1
adamc@31 1049 in
adamc@31 1050 SOME (if n1 = n2 then
adamc@31 1051 env
adamc@31 1052 else
adamc@31 1053 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)))
adamc@31 1054 end
adamc@31 1055 in
adamc@31 1056 case sgi1 of
adamc@31 1057 L'.SgiConAbs (x, n1, k1) => found (x, n1, k1, NONE)
adamc@31 1058 | L'.SgiCon (x, n1, k1, c1) => found (x, n1, k1, SOME c1)
adamc@31 1059 | _ => NONE
adamc@31 1060 end)
adamc@31 1061
adamc@31 1062 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 1063 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1064 case sgi1 of
adamc@31 1065 L'.SgiCon (x, n1, k1, c1) =>
adamc@31 1066 let
adamc@31 1067 val () = unifyCons env c1 c2
adamc@31 1068 handle CUnify (c1, c2, err) =>
adamc@31 1069 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
adamc@31 1070 in
adamc@31 1071 SOME (E.pushCNamedAs env x n2 k2 (SOME c2))
adamc@31 1072 end
adamc@31 1073 | _ => NONE)
adamc@31 1074
adamc@32 1075 | L'.SgiVal (x, n2, c2) =>
adamc@32 1076 seek (fn sgi1All as (sgi1, _) =>
adamc@32 1077 case sgi1 of
adamc@32 1078 L'.SgiVal (x, n1, c1) =>
adamc@32 1079 let
adamc@32 1080 val () = unifyCons env c1 c2
adamc@32 1081 handle CUnify (c1, c2, err) =>
adamc@32 1082 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
adamc@32 1083 in
adamc@32 1084 SOME env
adamc@32 1085 end
adamc@32 1086 | _ => NONE)
adamc@32 1087
adamc@33 1088 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 1089 seek (fn sgi1All as (sgi1, _) =>
adamc@33 1090 case sgi1 of
adamc@33 1091 L'.SgiStr (x, n1, sgn1) =>
adamc@33 1092 (subSgn env sgn1 sgn2;
adamc@33 1093 SOME env)
adamc@33 1094 | _ => NONE)
adamc@33 1095 (* Add type equations between structures here some day. *)
adamc@31 1096 end
adamc@31 1097 in
adamc@31 1098 ignore (foldl folder env sgis2)
adamc@31 1099 end
adamc@31 1100
adamc@31 1101 fun elabDecl ((d, loc), env) =
adamc@30 1102 let
adamc@30 1103
adamc@30 1104 in
adamc@30 1105 resetKunif ();
adamc@30 1106 resetCunif ();
adamc@30 1107 case d of
adamc@30 1108 L.DCon (x, ko, c) =>
adamc@30 1109 let
adamc@30 1110 val k' = case ko of
adamc@30 1111 NONE => kunif ()
adamc@30 1112 | SOME k => elabKind k
adamc@3 1113
adamc@30 1114 val (c', ck) = elabCon env c
adamc@30 1115 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@30 1116 in
adamc@30 1117 checkKind env c' ck k';
adamc@6 1118
adamc@30 1119 if ErrorMsg.anyErrors () then
adamc@30 1120 ()
adamc@30 1121 else (
adamc@30 1122 if kunifsInKind k' then
adamc@30 1123 declError env (KunifsRemainKind (loc, k'))
adamc@30 1124 else
adamc@30 1125 ();
adamc@6 1126
adamc@30 1127 if kunifsInCon c' then
adamc@30 1128 declError env (KunifsRemainCon (loc, c'))
adamc@30 1129 else
adamc@30 1130 ()
adamc@30 1131 );
adamc@6 1132
adamc@31 1133 ((L'.DCon (x, n, k', c'), loc), env')
adamc@30 1134 end
adamc@30 1135 | L.DVal (x, co, e) =>
adamc@30 1136 let
adamc@30 1137 val (c', ck) = case co of
adamc@30 1138 NONE => (cunif ktype, ktype)
adamc@30 1139 | SOME c => elabCon env c
adamc@10 1140
adamc@30 1141 val (e', et) = elabExp env e
adamc@30 1142 val (env', n) = E.pushENamed env x c'
adamc@30 1143 in
adamc@30 1144 checkCon env e' et c';
adamc@10 1145
adamc@30 1146 if ErrorMsg.anyErrors () then
adamc@30 1147 ()
adamc@30 1148 else (
adamc@30 1149 if kunifsInCon c' then
adamc@30 1150 declError env (KunifsRemainCon (loc, c'))
adamc@30 1151 else
adamc@30 1152 ();
adamc@10 1153
adamc@30 1154 if cunifsInCon c' then
adamc@30 1155 declError env (CunifsRemainCon (loc, c'))
adamc@30 1156 else
adamc@30 1157 ();
adamc@10 1158
adamc@30 1159 if kunifsInExp e' then
adamc@30 1160 declError env (KunifsRemainExp (loc, e'))
adamc@30 1161 else
adamc@30 1162 ();
adamc@10 1163
adamc@30 1164 if cunifsInExp e' then
adamc@30 1165 declError env (CunifsRemainExp (loc, e'))
adamc@30 1166 else
adamc@30 1167 ());
adamc@10 1168
adamc@31 1169 ((L'.DVal (x, n, c', e'), loc), env')
adamc@30 1170 end
adamc@30 1171
adamc@31 1172 | L.DSgn (x, sgn) =>
adamc@31 1173 let
adamc@31 1174 val sgn' = elabSgn env sgn
adamc@31 1175 val (env', n) = E.pushSgnNamed env x sgn'
adamc@31 1176 in
adamc@31 1177 ((L'.DSgn (x, n, sgn'), loc), env')
adamc@31 1178 end
adamc@31 1179
adamc@31 1180 | L.DStr (x, sgno, str) =>
adamc@31 1181 let
adamc@31 1182 val formal = Option.map (elabSgn env) sgno
adamc@31 1183 val (str', actual) = elabStr env str
adamc@31 1184
adamc@31 1185 val sgn' = case formal of
adamc@31 1186 NONE => actual
adamc@31 1187 | SOME formal =>
adamc@31 1188 (subSgn env actual formal;
adamc@31 1189 formal)
adamc@31 1190
adamc@31 1191 val (env', n) = E.pushStrNamed env x sgn'
adamc@31 1192 in
adamc@31 1193 ((L'.DStr (x, n, sgn', str'), loc), env')
adamc@31 1194 end
adamc@30 1195 end
adamc@3 1196
adamc@31 1197 and elabStr env (str, loc) =
adamc@31 1198 case str of
adamc@31 1199 L.StrConst ds =>
adamc@31 1200 let
adamc@31 1201 val (ds', env') = ListUtil.foldlMap elabDecl env ds
adamc@31 1202 val sgis = List.mapPartial sgiOfDecl ds'
adamc@31 1203 in
adamc@31 1204 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc))
adamc@31 1205 end
adamc@31 1206 | L.StrVar x =>
adamc@31 1207 (case E.lookupStr env x of
adamc@31 1208 NONE =>
adamc@31 1209 (strError env (UnboundStr (loc, x));
adamc@31 1210 (strerror, sgnerror))
adamc@31 1211 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn))
adamc@31 1212
adamc@31 1213 val elabFile = ListUtil.foldlMap elabDecl
adamc@2 1214
adamc@2 1215 end