annotate src/elaborate.sml @ 83:0a1baddd8ab2

Threading disjointness conditions through Elaborate
author Adam Chlipala <adamc@hcoop.net>
date Tue, 01 Jul 2008 11:39:14 -0400
parents b4f2a258e52c
children e86370850c30
rev   line source
adamc@2 1 (* Copyright (c) 2008, Adam Chlipala
adamc@2 2 * All rights reserved.
adamc@2 3 *
adamc@2 4 * Redistribution and use in source and binary forms, with or without
adamc@2 5 * modification, are permitted provided that the following conditions are met:
adamc@2 6 *
adamc@2 7 * - Redistributions of source code must retain the above copyright notice,
adamc@2 8 * this list of conditions and the following disclaimer.
adamc@2 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@2 10 * this list of conditions and the following disclaimer in the documentation
adamc@2 11 * and/or other materials provided with the distribution.
adamc@2 12 * - The names of contributors may not be used to endorse or promote products
adamc@2 13 * derived from this software without specific prior written permission.
adamc@2 14 *
adamc@2 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@2 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@2 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@2 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@2 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@2 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@2 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@2 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@2 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@2 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@2 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@2 26 *)
adamc@2 27
adamc@2 28 structure Elaborate :> ELABORATE = struct
adamc@2 29
adamc@14 30 structure P = Prim
adamc@4 31 structure L = Source
adamc@2 32 structure L' = Elab
adamc@2 33 structure E = ElabEnv
adamc@2 34 structure U = ElabUtil
adamc@82 35 structure D = Disjoint
adamc@2 36
adamc@3 37 open Print
adamc@3 38 open ElabPrint
adamc@3 39
adamc@62 40 structure SS = BinarySetFn(struct
adamc@62 41 type ord_key = string
adamc@62 42 val compare = String.compare
adamc@62 43 end)
adamc@62 44
adamc@2 45 fun elabExplicitness e =
adamc@2 46 case e of
adamc@2 47 L.Explicit => L'.Explicit
adamc@2 48 | L.Implicit => L'.Implicit
adamc@2 49
adamc@2 50 fun occursKind r =
adamc@76 51 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
adamc@2 52 | _ => false)
adamc@2 53
adamc@3 54 datatype kunify_error =
adamc@2 55 KOccursCheckFailed of L'.kind * L'.kind
adamc@2 56 | KIncompatible of L'.kind * L'.kind
adamc@2 57
adamc@3 58 exception KUnify' of kunify_error
adamc@3 59
adamc@3 60 fun kunifyError err =
adamc@2 61 case err of
adamc@2 62 KOccursCheckFailed (k1, k2) =>
adamc@3 63 eprefaces "Kind occurs check failed"
adamc@3 64 [("Kind 1", p_kind k1),
adamc@3 65 ("Kind 2", p_kind k2)]
adamc@2 66 | KIncompatible (k1, k2) =>
adamc@3 67 eprefaces "Incompatible kinds"
adamc@3 68 [("Kind 1", p_kind k1),
adamc@3 69 ("Kind 2", p_kind k2)]
adamc@2 70
adamc@3 71 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@2 72 let
adamc@3 73 fun err f = raise KUnify' (f (k1All, k2All))
adamc@2 74 in
adamc@2 75 case (k1, k2) of
adamc@2 76 (L'.KType, L'.KType) => ()
adamc@82 77 | (L'.KUnit, L'.KUnit) => ()
adamc@82 78
adamc@2 79 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@3 80 (unifyKinds' d1 d2;
adamc@3 81 unifyKinds' r1 r2)
adamc@2 82 | (L'.KName, L'.KName) => ()
adamc@3 83 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
adamc@2 84
adamc@2 85 | (L'.KError, _) => ()
adamc@2 86 | (_, L'.KError) => ()
adamc@2 87
adamc@76 88 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' k1All k2All
adamc@76 89 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' k1All k2All
adamc@2 90
adamc@76 91 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
adamc@2 92 if r1 = r2 then
adamc@2 93 ()
adamc@2 94 else
adamc@2 95 r1 := SOME k2All
adamc@2 96
adamc@76 97 | (L'.KUnif (_, _, r), _) =>
adamc@2 98 if occursKind r k2All then
adamc@2 99 err KOccursCheckFailed
adamc@2 100 else
adamc@2 101 r := SOME k2All
adamc@76 102 | (_, L'.KUnif (_, _, r)) =>
adamc@2 103 if occursKind r k1All then
adamc@2 104 err KOccursCheckFailed
adamc@2 105 else
adamc@2 106 r := SOME k1All
adamc@2 107
adamc@2 108 | _ => err KIncompatible
adamc@2 109 end
adamc@2 110
adamc@3 111 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@3 112
adamc@3 113 fun unifyKinds k1 k2 =
adamc@3 114 unifyKinds' k1 k2
adamc@3 115 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@3 116
adamc@3 117 datatype con_error =
adamc@3 118 UnboundCon of ErrorMsg.span * string
adamc@34 119 | UnboundStrInCon of ErrorMsg.span * string
adamc@3 120 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
adamc@67 121 | DuplicateField of ErrorMsg.span * string
adamc@3 122
adamc@5 123 fun conError env err =
adamc@3 124 case err of
adamc@3 125 UnboundCon (loc, s) =>
adamc@3 126 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
adamc@34 127 | UnboundStrInCon (loc, s) =>
adamc@34 128 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@3 129 | WrongKind (c, k1, k2, kerr) =>
adamc@3 130 (ErrorMsg.errorAt (#2 c) "Wrong kind";
adamc@5 131 eprefaces' [("Constructor", p_con env c),
adamc@5 132 ("Have kind", p_kind k1),
adamc@5 133 ("Need kind", p_kind k2)];
adamc@3 134 kunifyError kerr)
adamc@67 135 | DuplicateField (loc, s) =>
adamc@67 136 ErrorMsg.errorAt loc ("Duplicate record field " ^ s)
adamc@3 137
adamc@5 138 fun checkKind env c k1 k2 =
adamc@3 139 unifyKinds k1 k2
adamc@3 140 handle KUnify (k1, k2, err) =>
adamc@5 141 conError env (WrongKind (c, k1, k2, err))
adamc@3 142
adamc@3 143 val dummy = ErrorMsg.dummySpan
adamc@3 144
adamc@3 145 val ktype = (L'.KType, dummy)
adamc@3 146 val kname = (L'.KName, dummy)
adamc@12 147 val ktype_record = (L'.KRecord ktype, dummy)
adamc@3 148
adamc@3 149 val cerror = (L'.CError, dummy)
adamc@3 150 val kerror = (L'.KError, dummy)
adamc@10 151 val eerror = (L'.EError, dummy)
adamc@31 152 val sgnerror = (L'.SgnError, dummy)
adamc@31 153 val strerror = (L'.StrError, dummy)
adamc@3 154
adamc@56 155 val int = ref cerror
adamc@56 156 val float = ref cerror
adamc@56 157 val string = ref cerror
adamc@56 158
adamc@3 159 local
adamc@3 160 val count = ref 0
adamc@3 161 in
adamc@3 162
adamc@3 163 fun resetKunif () = count := 0
adamc@3 164
adamc@76 165 fun kunif loc =
adamc@3 166 let
adamc@3 167 val n = !count
adamc@3 168 val s = if n <= 26 then
adamc@3 169 str (chr (ord #"A" + n))
adamc@3 170 else
adamc@3 171 "U" ^ Int.toString (n - 26)
adamc@3 172 in
adamc@3 173 count := n + 1;
adamc@76 174 (L'.KUnif (loc, s, ref NONE), dummy)
adamc@3 175 end
adamc@3 176
adamc@3 177 end
adamc@3 178
adamc@10 179 local
adamc@10 180 val count = ref 0
adamc@10 181 in
adamc@10 182
adamc@10 183 fun resetCunif () = count := 0
adamc@10 184
adamc@76 185 fun cunif (loc, k) =
adamc@10 186 let
adamc@10 187 val n = !count
adamc@10 188 val s = if n <= 26 then
adamc@10 189 str (chr (ord #"A" + n))
adamc@10 190 else
adamc@10 191 "U" ^ Int.toString (n - 26)
adamc@10 192 in
adamc@10 193 count := n + 1;
adamc@76 194 (L'.CUnif (loc, k, s, ref NONE), dummy)
adamc@10 195 end
adamc@10 196
adamc@10 197 end
adamc@10 198
adamc@18 199 fun elabKind (k, loc) =
adamc@18 200 case k of
adamc@18 201 L.KType => (L'.KType, loc)
adamc@18 202 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
adamc@18 203 | L.KName => (L'.KName, loc)
adamc@18 204 | L.KRecord k => (L'.KRecord (elabKind k), loc)
adamc@82 205 | L.KUnit => (L'.KUnit, loc)
adamc@76 206 | L.KWild => kunif loc
adamc@18 207
adamc@67 208 fun foldKind (dom, ran, loc)=
adamc@67 209 (L'.KArrow ((L'.KArrow ((L'.KName, loc),
adamc@67 210 (L'.KArrow (dom,
adamc@67 211 (L'.KArrow (ran, ran), loc)), loc)), loc),
adamc@67 212 (L'.KArrow (ran,
adamc@67 213 (L'.KArrow ((L'.KRecord dom, loc),
adamc@67 214 ran), loc)), loc)), loc)
adamc@67 215
adamc@83 216 fun elabCon (env, denv) (c, loc) =
adamc@3 217 case c of
adamc@3 218 L.CAnnot (c, k) =>
adamc@3 219 let
adamc@3 220 val k' = elabKind k
adamc@83 221 val (c', ck, gs) = elabCon (env, denv) c
adamc@3 222 in
adamc@5 223 checkKind env c' ck k';
adamc@83 224 (c', k', gs)
adamc@3 225 end
adamc@3 226
adamc@3 227 | L.TFun (t1, t2) =>
adamc@3 228 let
adamc@83 229 val (t1', k1, gs1) = elabCon (env, denv) t1
adamc@83 230 val (t2', k2, gs2) = elabCon (env, denv) t2
adamc@3 231 in
adamc@5 232 checkKind env t1' k1 ktype;
adamc@5 233 checkKind env t2' k2 ktype;
adamc@83 234 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
adamc@3 235 end
adamc@3 236 | L.TCFun (e, x, k, t) =>
adamc@3 237 let
adamc@3 238 val e' = elabExplicitness e
adamc@3 239 val k' = elabKind k
adamc@3 240 val env' = E.pushCRel env x k'
adamc@83 241 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@3 242 in
adamc@5 243 checkKind env t' tk ktype;
adamc@83 244 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
adamc@3 245 end
adamc@3 246 | L.TRecord c =>
adamc@3 247 let
adamc@83 248 val (c', ck, gs) = elabCon (env, denv) c
adamc@3 249 val k = (L'.KRecord ktype, loc)
adamc@3 250 in
adamc@5 251 checkKind env c' ck k;
adamc@83 252 ((L'.TRecord c', loc), ktype, gs)
adamc@3 253 end
adamc@3 254
adamc@34 255 | L.CVar ([], s) =>
adamc@3 256 (case E.lookupC env s of
adamc@9 257 E.NotBound =>
adamc@5 258 (conError env (UnboundCon (loc, s));
adamc@83 259 (cerror, kerror, []))
adamc@9 260 | E.Rel (n, k) =>
adamc@83 261 ((L'.CRel n, loc), k, [])
adamc@9 262 | E.Named (n, k) =>
adamc@83 263 ((L'.CNamed n, loc), k, []))
adamc@34 264 | L.CVar (m1 :: ms, s) =>
adamc@34 265 (case E.lookupStr env m1 of
adamc@42 266 NONE => (conError env (UnboundStrInCon (loc, m1));
adamc@83 267 (cerror, kerror, []))
adamc@34 268 | SOME (n, sgn) =>
adamc@34 269 let
adamc@34 270 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 271 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 272 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@34 273 (strerror, sgnerror))
adamc@34 274 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 275 ((L'.StrVar n, loc), sgn) ms
adamc@34 276
adamc@34 277 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
adamc@34 278 NONE => (conError env (UnboundCon (loc, s));
adamc@34 279 kerror)
adamc@34 280 | SOME (k, _) => k
adamc@34 281 in
adamc@83 282 ((L'.CModProj (n, ms, s), loc), k, [])
adamc@34 283 end)
adamc@34 284
adamc@3 285 | L.CApp (c1, c2) =>
adamc@3 286 let
adamc@83 287 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@83 288 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@76 289 val dom = kunif loc
adamc@76 290 val ran = kunif loc
adamc@3 291 in
adamc@5 292 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@5 293 checkKind env c2' k2 dom;
adamc@83 294 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
adamc@3 295 end
adamc@67 296 | L.CAbs (x, ko, t) =>
adamc@3 297 let
adamc@67 298 val k' = case ko of
adamc@76 299 NONE => kunif loc
adamc@67 300 | SOME k => elabKind k
adamc@3 301 val env' = E.pushCRel env x k'
adamc@83 302 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@3 303 in
adamc@8 304 ((L'.CAbs (x, k', t'), loc),
adamc@83 305 (L'.KArrow (k', tk), loc),
adamc@83 306 gs)
adamc@3 307 end
adamc@3 308
adamc@3 309 | L.CName s =>
adamc@83 310 ((L'.CName s, loc), kname, [])
adamc@3 311
adamc@3 312 | L.CRecord xcs =>
adamc@3 313 let
adamc@76 314 val k = kunif loc
adamc@3 315
adamc@83 316 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
adamc@83 317 let
adamc@83 318 val (x', xk, gs1) = elabCon (env, denv) x
adamc@83 319 val (c', ck, gs2) = elabCon (env, denv) c
adamc@83 320 in
adamc@83 321 checkKind env x' xk kname;
adamc@83 322 checkKind env c' ck k;
adamc@83 323 ((x', c'), gs1 @ gs2 @ gs)
adamc@83 324 end) [] xcs
adamc@67 325
adamc@67 326 val rc = (L'.CRecord (k, xcs'), loc)
adamc@67 327 (* Add duplicate field checking later. *)
adamc@83 328
adamc@83 329 fun prove (xcs, ds) =
adamc@83 330 case xcs of
adamc@83 331 [] => ds
adamc@83 332 | xc :: rest =>
adamc@83 333 let
adamc@83 334 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@83 335 val ds = foldl (fn (xc', ds) =>
adamc@83 336 let
adamc@83 337 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@83 338 in
adamc@83 339 map (fn cs => (loc, env, denv, cs)) (D.prove env denv (r1, r2, loc))
adamc@83 340 @ ds
adamc@83 341 end)
adamc@83 342 ds rest
adamc@83 343 in
adamc@83 344 prove (rest, ds)
adamc@83 345 end
adamc@3 346 in
adamc@83 347 (rc, (L'.KRecord k, loc), prove (xcs', gs))
adamc@3 348 end
adamc@3 349 | L.CConcat (c1, c2) =>
adamc@3 350 let
adamc@83 351 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@83 352 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@76 353 val ku = kunif loc
adamc@3 354 val k = (L'.KRecord ku, loc)
adamc@3 355 in
adamc@5 356 checkKind env c1' k1 k;
adamc@5 357 checkKind env c2' k2 k;
adamc@83 358 ((L'.CConcat (c1', c2'), loc), k,
adamc@83 359 map (fn cs => (loc, env, denv, cs)) (D.prove env denv (c1', c2', loc)) @ gs1 @ gs2)
adamc@3 360 end
adamc@67 361 | L.CFold =>
adamc@67 362 let
adamc@76 363 val dom = kunif loc
adamc@76 364 val ran = kunif loc
adamc@67 365 in
adamc@67 366 ((L'.CFold (dom, ran), loc),
adamc@83 367 foldKind (dom, ran, loc),
adamc@83 368 [])
adamc@67 369 end
adamc@3 370
adamc@83 371 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
adamc@82 372
adamc@18 373 | L.CWild k =>
adamc@18 374 let
adamc@18 375 val k' = elabKind k
adamc@18 376 in
adamc@83 377 (cunif (loc, k'), k', [])
adamc@18 378 end
adamc@18 379
adamc@6 380 fun kunifsRemain k =
adamc@6 381 case k of
adamc@76 382 L'.KUnif (_, _, ref NONE) => true
adamc@6 383 | _ => false
adamc@10 384 fun cunifsRemain c =
adamc@10 385 case c of
adamc@76 386 L'.CUnif (loc, _, _, ref NONE) => SOME loc
adamc@76 387 | _ => NONE
adamc@6 388
adamc@76 389 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
adamc@76 390 con = fn _ => false,
adamc@76 391 exp = fn _ => false,
adamc@76 392 sgn_item = fn _ => false,
adamc@76 393 sgn = fn _ => false,
adamc@76 394 str = fn _ => false,
adamc@76 395 decl = fn _ => false}
adamc@10 396
adamc@76 397 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
adamc@76 398 con = cunifsRemain,
adamc@76 399 exp = fn _ => NONE,
adamc@76 400 sgn_item = fn _ => NONE,
adamc@76 401 sgn = fn _ => NONE,
adamc@76 402 str = fn _ => NONE,
adamc@76 403 decl = fn _ => NONE}
adamc@10 404
adamc@10 405 fun occursCon r =
adamc@10 406 U.Con.exists {kind = fn _ => false,
adamc@76 407 con = fn L'.CUnif (_, _, _, r') => r = r'
adamc@10 408 | _ => false}
adamc@10 409
adamc@10 410 datatype cunify_error =
adamc@10 411 CKind of L'.kind * L'.kind * kunify_error
adamc@10 412 | COccursCheckFailed of L'.con * L'.con
adamc@10 413 | CIncompatible of L'.con * L'.con
adamc@10 414 | CExplicitness of L'.con * L'.con
adamc@12 415 | CKindof of L'.con
adamc@12 416 | CRecordFailure
adamc@10 417
adamc@10 418 exception CUnify' of cunify_error
adamc@10 419
adamc@10 420 fun cunifyError env err =
adamc@10 421 case err of
adamc@10 422 CKind (k1, k2, kerr) =>
adamc@10 423 (eprefaces "Kind unification failure"
adamc@10 424 [("Kind 1", p_kind k1),
adamc@10 425 ("Kind 2", p_kind k2)];
adamc@10 426 kunifyError kerr)
adamc@10 427 | COccursCheckFailed (c1, c2) =>
adamc@10 428 eprefaces "Constructor occurs check failed"
adamc@10 429 [("Con 1", p_con env c1),
adamc@10 430 ("Con 2", p_con env c2)]
adamc@10 431 | CIncompatible (c1, c2) =>
adamc@10 432 eprefaces "Incompatible constructors"
adamc@10 433 [("Con 1", p_con env c1),
adamc@10 434 ("Con 2", p_con env c2)]
adamc@10 435 | CExplicitness (c1, c2) =>
adamc@10 436 eprefaces "Differing constructor function explicitness"
adamc@10 437 [("Con 1", p_con env c1),
adamc@10 438 ("Con 2", p_con env c2)]
adamc@12 439 | CKindof c =>
adamc@12 440 eprefaces "Kind unification variable blocks kindof calculation"
adamc@12 441 [("Con", p_con env c)]
adamc@12 442 | CRecordFailure =>
adamc@12 443 eprefaces "Can't unify record constructors" []
adamc@10 444
adamc@13 445 exception SynUnif = E.SynUnif
adamc@12 446
adamc@81 447 open ElabOps
adamc@43 448
adamc@12 449 type record_summary = {
adamc@12 450 fields : (L'.con * L'.con) list,
adamc@12 451 unifs : (L'.con * L'.con option ref) list,
adamc@12 452 others : L'.con list
adamc@12 453 }
adamc@12 454
adamc@12 455 fun summaryToCon {fields, unifs, others} =
adamc@12 456 let
adamc@12 457 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 458 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 459 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 460 in
adamc@12 461 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 462 end
adamc@12 463
adamc@12 464 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 465
adamc@12 466 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 467
adamc@12 468 fun hnormKind (kAll as (k, _)) =
adamc@12 469 case k of
adamc@76 470 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@12 471 | _ => kAll
adamc@12 472
adamc@12 473 fun kindof env (c, loc) =
adamc@12 474 case c of
adamc@12 475 L'.TFun _ => ktype
adamc@12 476 | L'.TCFun _ => ktype
adamc@12 477 | L'.TRecord _ => ktype
adamc@12 478
adamc@12 479 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 480 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@34 481 | L'.CModProj (n, ms, x) =>
adamc@34 482 let
adamc@34 483 val (_, sgn) = E.lookupStrNamed env n
adamc@34 484 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 485 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 486 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 487 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 488 ((L'.StrVar n, loc), sgn) ms
adamc@34 489 in
adamc@34 490 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@34 491 NONE => raise Fail "kindof: Unknown con in structure"
adamc@34 492 | SOME (k, _) => k
adamc@34 493 end
adamc@34 494
adamc@12 495 | L'.CApp (c, _) =>
adamc@12 496 (case #1 (hnormKind (kindof env c)) of
adamc@12 497 L'.KArrow (_, k) => k
adamc@12 498 | L'.KError => kerror
adamc@12 499 | _ => raise CUnify' (CKindof c))
adamc@12 500 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@12 501
adamc@12 502 | L'.CName _ => kname
adamc@12 503
adamc@12 504 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 505 | L'.CConcat (c, _) => kindof env c
adamc@67 506 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
adamc@12 507
adamc@82 508 | L'.CUnit => (L'.KUnit, loc)
adamc@82 509
adamc@12 510 | L'.CError => kerror
adamc@76 511 | L'.CUnif (_, k, _, _) => k
adamc@12 512
adamc@12 513 fun unifyRecordCons env (c1, c2) =
adamc@12 514 let
adamc@12 515 val k1 = kindof env c1
adamc@12 516 val k2 = kindof env c2
adamc@12 517 in
adamc@12 518 unifyKinds k1 k2;
adamc@12 519 unifySummaries env (k1, recordSummary env c1, recordSummary env c2)
adamc@12 520 end
adamc@12 521
adamc@12 522 and recordSummary env c : record_summary =
adamc@12 523 case hnormCon env c of
adamc@12 524 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
adamc@12 525 | (L'.CConcat (c1, c2), _) =>
adamc@12 526 let
adamc@12 527 val s1 = recordSummary env c1
adamc@12 528 val s2 = recordSummary env c2
adamc@12 529 in
adamc@12 530 {fields = #fields s1 @ #fields s2,
adamc@12 531 unifs = #unifs s1 @ #unifs s2,
adamc@12 532 others = #others s1 @ #others s2}
adamc@12 533 end
adamc@76 534 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary env c
adamc@76 535 | c' as (L'.CUnif (_, _, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
adamc@12 536 | c' => {fields = [], unifs = [], others = [c']}
adamc@12 537
adamc@12 538 and consEq env (c1, c2) =
adamc@12 539 (unifyCons env c1 c2;
adamc@12 540 true)
adamc@12 541 handle CUnify _ => false
adamc@12 542
adamc@80 543 and consNeq env (c1, c2) =
adamc@80 544 case (#1 (hnormCon env c1), #1 (hnormCon env c2)) of
adamc@80 545 (L'.CName x1, L'.CName x2) => x1 <> x2
adamc@80 546 | _ => false
adamc@80 547
adamc@12 548 and unifySummaries env (k, s1 : record_summary, s2 : record_summary) =
adamc@12 549 let
adamc@14 550 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@14 551 ("#2", p_summary env s2)]*)
adamc@12 552
adamc@12 553 fun eatMatching p (ls1, ls2) =
adamc@12 554 let
adamc@12 555 fun em (ls1, ls2, passed1) =
adamc@12 556 case ls1 of
adamc@12 557 [] => (rev passed1, ls2)
adamc@12 558 | h1 :: t1 =>
adamc@12 559 let
adamc@12 560 fun search (ls2', passed2) =
adamc@12 561 case ls2' of
adamc@12 562 [] => em (t1, ls2, h1 :: passed1)
adamc@12 563 | h2 :: t2 =>
adamc@12 564 if p (h1, h2) then
adamc@12 565 em (t1, List.revAppend (passed2, t2), passed1)
adamc@12 566 else
adamc@12 567 search (t2, h2 :: passed2)
adamc@12 568 in
adamc@12 569 search (ls2, [])
adamc@12 570 end
adamc@12 571 in
adamc@12 572 em (ls1, ls2, [])
adamc@12 573 end
adamc@12 574
adamc@12 575 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@80 576 not (consNeq env (x1, x2))
adamc@80 577 andalso consEq env (c1, c2)
adamc@80 578 andalso consEq env (x1, x2))
adamc@79 579 (#fields s1, #fields s2)
adamc@14 580 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@14 581 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@12 582 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@12 583 val (others1, others2) = eatMatching (consEq env) (#others s1, #others s2)
adamc@12 584
adamc@12 585 fun unifFields (fs, others, unifs) =
adamc@12 586 case (fs, others, unifs) of
adamc@12 587 ([], [], _) => ([], [], unifs)
adamc@12 588 | (_, _, []) => (fs, others, [])
adamc@12 589 | (_, _, (_, r) :: rest) =>
adamc@12 590 let
adamc@12 591 val r' = ref NONE
adamc@76 592 val cr' = (L'.CUnif (dummy, k, "recd", r'), dummy)
adamc@12 593
adamc@12 594 val prefix = case (fs, others) of
adamc@12 595 ([], other :: others) =>
adamc@12 596 List.foldl (fn (other, c) =>
adamc@12 597 (L'.CConcat (c, other), dummy))
adamc@12 598 other others
adamc@12 599 | (fs, []) =>
adamc@12 600 (L'.CRecord (k, fs), dummy)
adamc@12 601 | (fs, others) =>
adamc@12 602 List.foldl (fn (other, c) =>
adamc@12 603 (L'.CConcat (c, other), dummy))
adamc@12 604 (L'.CRecord (k, fs), dummy) others
adamc@12 605 in
adamc@12 606 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 607 ([], [], (cr', r') :: rest)
adamc@12 608 end
adamc@12 609
adamc@12 610 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 611 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 612
adamc@12 613 val clear1 = case (fs1, others1) of
adamc@12 614 ([], []) => true
adamc@12 615 | _ => false
adamc@12 616 val clear2 = case (fs2, others2) of
adamc@12 617 ([], []) => true
adamc@12 618 | _ => false
adamc@12 619 val empty = (L'.CRecord (k, []), dummy)
adamc@12 620 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 621 case (unifs1, unifs2) of
adamc@12 622 ([], _) =>
adamc@12 623 if clear1 then
adamc@12 624 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 625 else
adamc@12 626 raise CUnify' CRecordFailure
adamc@12 627 | (_, []) =>
adamc@12 628 if clear2 then
adamc@12 629 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 630 else
adamc@12 631 raise CUnify' CRecordFailure
adamc@12 632 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 633 (r2 := SOME c1;
adamc@12 634 pairOffUnifs (rest1, rest2))
adamc@12 635 in
adamc@12 636 pairOffUnifs (unifs1, unifs2)
adamc@12 637 end
adamc@12 638
adamc@11 639
adamc@12 640 and unifyCons' env c1 c2 =
adamc@11 641 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
adamc@11 642
adamc@11 643 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 644 let
adamc@10 645 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 646
adamc@12 647 fun isRecord () = unifyRecordCons env (c1All, c2All)
adamc@10 648 in
adamc@10 649 case (c1, c2) of
adamc@10 650 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@10 651 (unifyCons' env d1 d2;
adamc@10 652 unifyCons' env r1 r2)
adamc@10 653 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 654 if expl1 <> expl2 then
adamc@10 655 err CExplicitness
adamc@10 656 else
adamc@10 657 (unifyKinds d1 d2;
adamc@10 658 unifyCons' (E.pushCRel env x1 d1) r1 r2)
adamc@10 659 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
adamc@10 660
adamc@10 661 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 662 if n1 = n2 then
adamc@10 663 ()
adamc@10 664 else
adamc@10 665 err CIncompatible
adamc@10 666 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 667 if n1 = n2 then
adamc@10 668 ()
adamc@10 669 else
adamc@10 670 err CIncompatible
adamc@10 671
adamc@10 672 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@10 673 (unifyCons' env d1 d2;
adamc@10 674 unifyCons' env r1 r2)
adamc@10 675 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 676 (unifyKinds k1 k2;
adamc@10 677 unifyCons' (E.pushCRel env x1 k1) c1 c2)
adamc@10 678
adamc@10 679 | (L'.CName n1, L'.CName n2) =>
adamc@10 680 if n1 = n2 then
adamc@10 681 ()
adamc@10 682 else
adamc@10 683 err CIncompatible
adamc@10 684
adamc@34 685 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@34 686 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@34 687 ()
adamc@34 688 else
adamc@34 689 err CIncompatible
adamc@34 690
adamc@10 691 | (L'.CError, _) => ()
adamc@10 692 | (_, L'.CError) => ()
adamc@10 693
adamc@76 694 | (L'.CUnif (_, _, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
adamc@76 695 | (_, L'.CUnif (_, _, _, ref (SOME c2All))) => unifyCons' env c1All c2All
adamc@10 696
adamc@76 697 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
adamc@10 698 if r1 = r2 then
adamc@10 699 ()
adamc@10 700 else
adamc@10 701 (unifyKinds k1 k2;
adamc@10 702 r1 := SOME c2All)
adamc@10 703
adamc@76 704 | (L'.CUnif (_, _, _, r), _) =>
adamc@10 705 if occursCon r c2All then
adamc@10 706 err COccursCheckFailed
adamc@10 707 else
adamc@10 708 r := SOME c2All
adamc@76 709 | (_, L'.CUnif (_, _, _, r)) =>
adamc@10 710 if occursCon r c1All then
adamc@10 711 err COccursCheckFailed
adamc@10 712 else
adamc@10 713 r := SOME c1All
adamc@10 714
adamc@12 715 | (L'.CRecord _, _) => isRecord ()
adamc@12 716 | (_, L'.CRecord _) => isRecord ()
adamc@12 717 | (L'.CConcat _, _) => isRecord ()
adamc@12 718 | (_, L'.CConcat _) => isRecord ()
adamc@12 719
adamc@71 720 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
adamc@71 721 (unifyKinds dom1 dom2;
adamc@71 722 unifyKinds ran1 ran2)
adamc@71 723
adamc@10 724 | _ => err CIncompatible
adamc@10 725 end
adamc@10 726
adamc@12 727 and unifyCons env c1 c2 =
adamc@10 728 unifyCons' env c1 c2
adamc@10 729 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 730 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 731
adamc@10 732 datatype exp_error =
adamc@10 733 UnboundExp of ErrorMsg.span * string
adamc@34 734 | UnboundStrInExp of ErrorMsg.span * string
adamc@10 735 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 736 | Unif of string * L'.con
adamc@11 737 | WrongForm of string * L'.exp * L'.con
adamc@10 738
adamc@10 739 fun expError env err =
adamc@10 740 case err of
adamc@10 741 UnboundExp (loc, s) =>
adamc@10 742 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@34 743 | UnboundStrInExp (loc, s) =>
adamc@34 744 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@10 745 | Unify (e, c1, c2, uerr) =>
adamc@10 746 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 747 eprefaces' [("Expression", p_exp env e),
adamc@10 748 ("Have con", p_con env c1),
adamc@10 749 ("Need con", p_con env c2)];
adamc@10 750 cunifyError env uerr)
adamc@11 751 | Unif (action, c) =>
adamc@11 752 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 753 eprefaces' [("Con", p_con env c)])
adamc@11 754 | WrongForm (variety, e, t) =>
adamc@11 755 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 756 eprefaces' [("Expression", p_exp env e),
adamc@11 757 ("Type", p_con env t)])
adamc@10 758
adamc@10 759 fun checkCon env e c1 c2 =
adamc@10 760 unifyCons env c1 c2
adamc@10 761 handle CUnify (c1, c2, err) =>
adamc@10 762 expError env (Unify (e, c1, c2, err))
adamc@10 763
adamc@14 764 fun primType env p =
adamc@56 765 case p of
adamc@56 766 P.Int _ => !int
adamc@56 767 | P.Float _ => !float
adamc@56 768 | P.String _ => !string
adamc@14 769
adamc@71 770 fun recCons (k, nm, v, rest, loc) =
adamc@71 771 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
adamc@71 772 rest), loc)
adamc@71 773
adamc@71 774 fun foldType (dom, loc) =
adamc@71 775 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
adamc@71 776 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
adamc@71 777 (L'.TCFun (L'.Explicit, "v", dom,
adamc@71 778 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
adamc@71 779 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
adamc@71 780 (L'.CApp ((L'.CRel 3, loc),
adamc@71 781 recCons (dom,
adamc@71 782 (L'.CRel 2, loc),
adamc@71 783 (L'.CRel 1, loc),
adamc@71 784 (L'.CRel 0, loc),
adamc@71 785 loc)), loc)), loc)),
adamc@71 786 loc)), loc)), loc),
adamc@71 787 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
adamc@71 788 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
adamc@71 789 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
adamc@71 790 loc)), loc)), loc)
adamc@71 791
adamc@15 792 fun typeof env (e, loc) =
adamc@15 793 case e of
adamc@56 794 L'.EPrim p => primType env p
adamc@15 795 | L'.ERel n => #2 (E.lookupERel env n)
adamc@15 796 | L'.ENamed n => #2 (E.lookupENamed env n)
adamc@34 797 | L'.EModProj (n, ms, x) =>
adamc@34 798 let
adamc@34 799 val (_, sgn) = E.lookupStrNamed env n
adamc@34 800 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 801 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 802 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 803 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 804 ((L'.StrVar n, loc), sgn) ms
adamc@34 805 in
adamc@34 806 case E.projectVal env {sgn = sgn, str = str, field = x} of
adamc@34 807 NONE => raise Fail "typeof: Unknown val in structure"
adamc@34 808 | SOME t => t
adamc@34 809 end
adamc@34 810
adamc@15 811 | L'.EApp (e1, _) =>
adamc@15 812 (case #1 (typeof env e1) of
adamc@15 813 L'.TFun (_, c) => c
adamc@15 814 | _ => raise Fail "typeof: Bad EApp")
adamc@26 815 | L'.EAbs (_, _, ran, _) => ran
adamc@15 816 | L'.ECApp (e1, c) =>
adamc@15 817 (case #1 (typeof env e1) of
adamc@15 818 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
adamc@15 819 | _ => raise Fail "typeof: Bad ECApp")
adamc@15 820 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
adamc@15 821
adamc@29 822 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
adamc@15 823 | L'.EField (_, _, {field, ...}) => field
adamc@71 824 | L'.EFold dom => foldType (dom, loc)
adamc@15 825
adamc@15 826 | L'.EError => cerror
adamc@15 827
adamc@15 828 fun elabHead env (e as (_, loc)) t =
adamc@15 829 let
adamc@15 830 fun unravel (t, e) =
adamc@15 831 case hnormCon env t of
adamc@15 832 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@15 833 let
adamc@76 834 val u = cunif (loc, k)
adamc@15 835 in
adamc@15 836 unravel (subConInCon (0, u) t',
adamc@15 837 (L'.ECApp (e, u), loc))
adamc@15 838 end
adamc@15 839 | _ => (e, t)
adamc@15 840 in
adamc@15 841 unravel (t, e)
adamc@15 842 end
adamc@15 843
adamc@83 844 fun elabExp (env, denv) (e, loc) =
adamc@10 845 case e of
adamc@10 846 L.EAnnot (e, t) =>
adamc@10 847 let
adamc@83 848 val (e', et, gs1) = elabExp (env, denv) e
adamc@83 849 val (t', _, gs2) = elabCon (env, denv) t
adamc@10 850 in
adamc@10 851 checkCon env e' et t';
adamc@83 852 (e', t', gs1 @ gs2)
adamc@10 853 end
adamc@10 854
adamc@83 855 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@34 856 | L.EVar ([], s) =>
adamc@10 857 (case E.lookupE env s of
adamc@10 858 E.NotBound =>
adamc@10 859 (expError env (UnboundExp (loc, s));
adamc@83 860 (eerror, cerror, []))
adamc@83 861 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
adamc@83 862 | E.Named (n, t) => ((L'.ENamed n, loc), t, []))
adamc@34 863 | L.EVar (m1 :: ms, s) =>
adamc@34 864 (case E.lookupStr env m1 of
adamc@42 865 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@83 866 (eerror, cerror, []))
adamc@34 867 | SOME (n, sgn) =>
adamc@34 868 let
adamc@34 869 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 870 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 871 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@34 872 (strerror, sgnerror))
adamc@34 873 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 874 ((L'.StrVar n, loc), sgn) ms
adamc@34 875
adamc@34 876 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@34 877 NONE => (expError env (UnboundExp (loc, s));
adamc@34 878 cerror)
adamc@34 879 | SOME t => t
adamc@34 880 in
adamc@83 881 ((L'.EModProj (n, ms, s), loc), t, [])
adamc@34 882 end)
adamc@34 883
adamc@10 884 | L.EApp (e1, e2) =>
adamc@10 885 let
adamc@83 886 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@15 887 val (e1', t1) = elabHead env e1' t1
adamc@83 888 val (e2', t2, gs2) = elabExp (env, denv) e2
adamc@10 889
adamc@76 890 val dom = cunif (loc, ktype)
adamc@76 891 val ran = cunif (loc, ktype)
adamc@10 892 val t = (L'.TFun (dom, ran), dummy)
adamc@10 893 in
adamc@10 894 checkCon env e1' t1 t;
adamc@10 895 checkCon env e2' t2 dom;
adamc@83 896 ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2)
adamc@10 897 end
adamc@10 898 | L.EAbs (x, to, e) =>
adamc@10 899 let
adamc@83 900 val (t', gs1) = case to of
adamc@83 901 NONE => (cunif (loc, ktype), [])
adamc@83 902 | SOME t =>
adamc@83 903 let
adamc@83 904 val (t', tk, gs) = elabCon (env, denv) t
adamc@83 905 in
adamc@83 906 checkKind env t' tk ktype;
adamc@83 907 (t', gs)
adamc@83 908 end
adamc@83 909 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
adamc@10 910 in
adamc@26 911 ((L'.EAbs (x, t', et, e'), loc),
adamc@83 912 (L'.TFun (t', et), loc),
adamc@83 913 gs1 @ gs2)
adamc@10 914 end
adamc@10 915 | L.ECApp (e, c) =>
adamc@10 916 let
adamc@83 917 val (e', et, gs1) = elabExp (env, denv) e
adamc@15 918 val (e', et) = elabHead env e' et
adamc@83 919 val (c', ck, gs2) = elabCon (env, denv) c
adamc@10 920 in
adamc@11 921 case #1 (hnormCon env et) of
adamc@83 922 L'.CError => (eerror, cerror, [])
adamc@11 923 | L'.TCFun (_, _, k, eb) =>
adamc@11 924 let
adamc@11 925 val () = checkKind env c' ck k
adamc@11 926 val eb' = subConInCon (0, c') eb
adamc@11 927 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@11 928 cerror)
adamc@11 929 in
adamc@83 930 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2)
adamc@11 931 end
adamc@11 932
adamc@11 933 | L'.CUnif _ =>
adamc@11 934 (expError env (Unif ("application", et));
adamc@83 935 (eerror, cerror, []))
adamc@11 936
adamc@11 937 | _ =>
adamc@11 938 (expError env (WrongForm ("constructor function", e', et));
adamc@83 939 (eerror, cerror, []))
adamc@10 940 end
adamc@11 941 | L.ECAbs (expl, x, k, e) =>
adamc@11 942 let
adamc@11 943 val expl' = elabExplicitness expl
adamc@11 944 val k' = elabKind k
adamc@83 945 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
adamc@11 946 in
adamc@11 947 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@83 948 (L'.TCFun (expl', x, k', et), loc),
adamc@83 949 gs)
adamc@11 950 end
adamc@6 951
adamc@12 952 | L.ERecord xes =>
adamc@12 953 let
adamc@83 954 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@83 955 let
adamc@83 956 val (x', xk, gs1) = elabCon (env, denv) x
adamc@83 957 val (e', et, gs2) = elabExp (env, denv) e
adamc@83 958 in
adamc@83 959 checkKind env x' xk kname;
adamc@83 960 ((x', e', et), gs1 @ gs2 @ gs)
adamc@83 961 end)
adamc@83 962 [] xes
adamc@12 963 in
adamc@29 964 ((L'.ERecord xes', loc),
adamc@83 965 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@83 966 gs)
adamc@12 967 end
adamc@12 968
adamc@12 969 | L.EField (e, c) =>
adamc@12 970 let
adamc@83 971 val (e', et, gs1) = elabExp (env, denv) e
adamc@83 972 val (c', ck, gs2) = elabCon (env, denv) c
adamc@12 973
adamc@76 974 val ft = cunif (loc, ktype)
adamc@76 975 val rest = cunif (loc, ktype_record)
adamc@12 976 in
adamc@12 977 checkKind env c' ck kname;
adamc@12 978 checkCon env e' et (L'.TRecord (L'.CConcat ((L'.CRecord (ktype, [(c', ft)]), loc), rest), loc), loc);
adamc@83 979 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2)
adamc@12 980 end
adamc@71 981
adamc@71 982 | L.EFold =>
adamc@71 983 let
adamc@76 984 val dom = kunif loc
adamc@71 985 in
adamc@83 986 ((L'.EFold dom, loc), foldType (dom, loc), [])
adamc@71 987 end
adamc@12 988
adamc@12 989
adamc@6 990 datatype decl_error =
adamc@76 991 KunifsRemain of ErrorMsg.span
adamc@76 992 | CunifsRemain of ErrorMsg.span
adamc@6 993
adamc@6 994 fun declError env err =
adamc@6 995 case err of
adamc@76 996 KunifsRemain loc =>
adamc@76 997 ErrorMsg.errorAt loc "Some kind unification variables are undetermined in declaration"
adamc@76 998 | CunifsRemain loc =>
adamc@76 999 ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in declaration"
adamc@6 1000
adamc@31 1001 datatype sgn_error =
adamc@31 1002 UnboundSgn of ErrorMsg.span * string
adamc@31 1003 | UnmatchedSgi of L'.sgn_item
adamc@31 1004 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 1005 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@41 1006 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 1007 | UnWhereable of L'.sgn * string
adamc@75 1008 | WhereWrongKind of L'.kind * L'.kind * kunify_error
adamc@58 1009 | NotIncludable of L'.sgn
adamc@62 1010 | DuplicateCon of ErrorMsg.span * string
adamc@62 1011 | DuplicateVal of ErrorMsg.span * string
adamc@62 1012 | DuplicateSgn of ErrorMsg.span * string
adamc@62 1013 | DuplicateStr of ErrorMsg.span * string
adamc@31 1014
adamc@31 1015 fun sgnError env err =
adamc@31 1016 case err of
adamc@31 1017 UnboundSgn (loc, s) =>
adamc@31 1018 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 1019 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 1020 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 1021 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 1022 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 1023 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@59 1024 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1025 ("Need", p_sgn_item env sgi2),
adamc@31 1026 ("Kind 1", p_kind k1),
adamc@31 1027 ("Kind 2", p_kind k2)];
adamc@31 1028 kunifyError kerr)
adamc@31 1029 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 1030 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@59 1031 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1032 ("Need", p_sgn_item env sgi2),
adamc@31 1033 ("Con 1", p_con env c1),
adamc@31 1034 ("Con 2", p_con env c2)];
adamc@31 1035 cunifyError env cerr)
adamc@41 1036 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1037 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1038 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1039 ("Sig 2", p_sgn env sgn2)])
adamc@42 1040 | UnWhereable (sgn, x) =>
adamc@42 1041 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1042 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1043 ("Field", PD.string x)])
adamc@75 1044 | WhereWrongKind (k1, k2, kerr) =>
adamc@75 1045 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
adamc@75 1046 eprefaces' [("Have", p_kind k1),
adamc@75 1047 ("Need", p_kind k2)];
adamc@75 1048 kunifyError kerr)
adamc@58 1049 | NotIncludable sgn =>
adamc@58 1050 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
adamc@58 1051 eprefaces' [("Signature", p_sgn env sgn)])
adamc@62 1052 | DuplicateCon (loc, s) =>
adamc@62 1053 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
adamc@62 1054 | DuplicateVal (loc, s) =>
adamc@62 1055 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
adamc@62 1056 | DuplicateSgn (loc, s) =>
adamc@62 1057 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
adamc@62 1058 | DuplicateStr (loc, s) =>
adamc@62 1059 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
adamc@31 1060
adamc@31 1061 datatype str_error =
adamc@31 1062 UnboundStr of ErrorMsg.span * string
adamc@44 1063 | NotFunctor of L'.sgn
adamc@46 1064 | FunctorRebind of ErrorMsg.span
adamc@61 1065 | UnOpenable of L'.sgn
adamc@75 1066 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
adamc@31 1067
adamc@31 1068 fun strError env err =
adamc@31 1069 case err of
adamc@31 1070 UnboundStr (loc, s) =>
adamc@31 1071 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1072 | NotFunctor sgn =>
adamc@44 1073 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1074 eprefaces' [("Signature", p_sgn env sgn)])
adamc@46 1075 | FunctorRebind loc =>
adamc@46 1076 ErrorMsg.errorAt loc "Attempt to rebind functor"
adamc@61 1077 | UnOpenable sgn =>
adamc@61 1078 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
adamc@61 1079 eprefaces' [("Signature", p_sgn env sgn)])
adamc@75 1080 | NotType (k, (k1, k2, ue)) =>
adamc@75 1081 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
adamc@75 1082 eprefaces' [("Kind", p_kind k),
adamc@75 1083 ("Subkind 1", p_kind k1),
adamc@75 1084 ("Subkind 2", p_kind k2)];
adamc@75 1085 kunifyError ue)
adamc@31 1086
adamc@42 1087 val hnormSgn = E.hnormSgn
adamc@31 1088
adamc@83 1089 fun elabSgn_item denv ((sgi, loc), (env, gs)) =
adamc@76 1090 case sgi of
adamc@76 1091 L.SgiConAbs (x, k) =>
adamc@76 1092 let
adamc@76 1093 val k' = elabKind k
adamc@31 1094
adamc@76 1095 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1096 in
adamc@83 1097 ([(L'.SgiConAbs (x, n, k'), loc)], (env', gs))
adamc@76 1098 end
adamc@31 1099
adamc@76 1100 | L.SgiCon (x, ko, c) =>
adamc@76 1101 let
adamc@76 1102 val k' = case ko of
adamc@76 1103 NONE => kunif loc
adamc@76 1104 | SOME k => elabKind k
adamc@31 1105
adamc@83 1106 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1107 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1108 in
adamc@76 1109 checkKind env c' ck k';
adamc@31 1110
adamc@83 1111 ([(L'.SgiCon (x, n, k', c'), loc)], (env', gs' @ gs))
adamc@76 1112 end
adamc@31 1113
adamc@76 1114 | L.SgiVal (x, c) =>
adamc@76 1115 let
adamc@83 1116 val (c', ck, gs') = elabCon (env, denv) c
adamc@31 1117
adamc@76 1118 val (env', n) = E.pushENamed env x c'
adamc@76 1119 in
adamc@76 1120 (unifyKinds ck ktype
adamc@76 1121 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 1122
adamc@83 1123 ([(L'.SgiVal (x, n, c'), loc)], (env', gs' @ gs))
adamc@76 1124 end
adamc@31 1125
adamc@76 1126 | L.SgiStr (x, sgn) =>
adamc@76 1127 let
adamc@83 1128 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1129 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1130 in
adamc@83 1131 ([(L'.SgiStr (x, n, sgn'), loc)], (env', gs' @ gs))
adamc@76 1132 end
adamc@31 1133
adamc@76 1134 | L.SgiSgn (x, sgn) =>
adamc@76 1135 let
adamc@83 1136 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1137 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1138 in
adamc@83 1139 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', gs' @ gs))
adamc@76 1140 end
adamc@31 1141
adamc@76 1142 | L.SgiInclude sgn =>
adamc@76 1143 let
adamc@83 1144 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1145 in
adamc@76 1146 case #1 (hnormSgn env sgn') of
adamc@76 1147 L'.SgnConst sgis =>
adamc@83 1148 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, gs' @ gs))
adamc@76 1149 | _ => (sgnError env (NotIncludable sgn');
adamc@83 1150 ([], (env, [])))
adamc@76 1151 end
adamc@31 1152
adamc@83 1153 and elabSgn (env, denv) (sgn, loc) =
adamc@31 1154 case sgn of
adamc@31 1155 L.SgnConst sgis =>
adamc@31 1156 let
adamc@83 1157 val (sgis', (_, gs)) = ListUtil.foldlMapConcat (elabSgn_item denv) (env, []) sgis
adamc@62 1158
adamc@62 1159 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 1160 case sgi of
adamc@62 1161 L'.SgiConAbs (x, _, _) =>
adamc@62 1162 (if SS.member (cons, x) then
adamc@62 1163 sgnError env (DuplicateCon (loc, x))
adamc@62 1164 else
adamc@62 1165 ();
adamc@62 1166 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1167 | L'.SgiCon (x, _, _, _) =>
adamc@62 1168 (if SS.member (cons, x) then
adamc@62 1169 sgnError env (DuplicateCon (loc, x))
adamc@62 1170 else
adamc@62 1171 ();
adamc@62 1172 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1173 | L'.SgiVal (x, _, _) =>
adamc@62 1174 (if SS.member (vals, x) then
adamc@62 1175 sgnError env (DuplicateVal (loc, x))
adamc@62 1176 else
adamc@62 1177 ();
adamc@62 1178 (cons, SS.add (vals, x), sgns, strs))
adamc@62 1179 | L'.SgiSgn (x, _, _) =>
adamc@62 1180 (if SS.member (sgns, x) then
adamc@62 1181 sgnError env (DuplicateSgn (loc, x))
adamc@62 1182 else
adamc@62 1183 ();
adamc@62 1184 (cons, vals, SS.add (sgns, x), strs))
adamc@62 1185 | L'.SgiStr (x, _, _) =>
adamc@62 1186 (if SS.member (strs, x) then
adamc@62 1187 sgnError env (DuplicateStr (loc, x))
adamc@62 1188 else
adamc@62 1189 ();
adamc@62 1190 (cons, vals, sgns, SS.add (strs, x))))
adamc@62 1191 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 1192 in
adamc@83 1193 ((L'.SgnConst sgis', loc), gs)
adamc@31 1194 end
adamc@31 1195 | L.SgnVar x =>
adamc@31 1196 (case E.lookupSgn env x of
adamc@31 1197 NONE =>
adamc@31 1198 (sgnError env (UnboundSgn (loc, x));
adamc@83 1199 ((L'.SgnError, loc), []))
adamc@83 1200 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 1201 | L.SgnFun (m, dom, ran) =>
adamc@41 1202 let
adamc@83 1203 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1204 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1205 val (ran', gs2) = elabSgn (env', denv) ran
adamc@41 1206 in
adamc@83 1207 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 1208 end
adamc@42 1209 | L.SgnWhere (sgn, x, c) =>
adamc@42 1210 let
adamc@83 1211 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 1212 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 1213 in
adamc@42 1214 case #1 (hnormSgn env sgn') of
adamc@83 1215 L'.SgnError => (sgnerror, [])
adamc@42 1216 | L'.SgnConst sgis =>
adamc@75 1217 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 1218 x' = x andalso
adamc@75 1219 (unifyKinds k ck
adamc@75 1220 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 1221 true)
adamc@42 1222 | _ => false) sgis then
adamc@83 1223 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 1224 else
adamc@42 1225 (sgnError env (UnWhereable (sgn', x));
adamc@83 1226 (sgnerror, []))
adamc@42 1227 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 1228 (sgnerror, []))
adamc@42 1229 end
adamc@59 1230 | L.SgnProj (m, ms, x) =>
adamc@59 1231 (case E.lookupStr env m of
adamc@59 1232 NONE => (strError env (UnboundStr (loc, m));
adamc@83 1233 (sgnerror, []))
adamc@59 1234 | SOME (n, sgn) =>
adamc@59 1235 let
adamc@59 1236 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 1237 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 1238 NONE => (strError env (UnboundStr (loc, m));
adamc@59 1239 (strerror, sgnerror))
adamc@59 1240 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 1241 ((L'.StrVar n, loc), sgn) ms
adamc@59 1242 in
adamc@59 1243 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 1244 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 1245 (sgnerror, []))
adamc@83 1246 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 1247 end)
adamc@59 1248
adamc@31 1249
adamc@66 1250 fun selfify env {str, strs, sgn} =
adamc@66 1251 case #1 (hnormSgn env sgn) of
adamc@66 1252 L'.SgnError => sgn
adamc@66 1253 | L'.SgnVar _ => sgn
adamc@66 1254
adamc@66 1255 | L'.SgnConst sgis =>
adamc@66 1256 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 1257 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@66 1258 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 1259 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 1260 | x => x) sgis), #2 sgn)
adamc@66 1261 | L'.SgnFun _ => sgn
adamc@66 1262 | L'.SgnWhere _ => sgn
adamc@66 1263 | L'.SgnProj (m, ms, x) =>
adamc@66 1264 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1265 (L'.StrVar m, #2 sgn) ms,
adamc@66 1266 sgn = #2 (E.lookupStrNamed env m),
adamc@66 1267 field = x} of
adamc@66 1268 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 1269 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 1270
adamc@66 1271 fun selfifyAt env {str, sgn} =
adamc@66 1272 let
adamc@66 1273 fun self (str, _) =
adamc@66 1274 case str of
adamc@66 1275 L'.StrVar x => SOME (x, [])
adamc@66 1276 | L'.StrProj (str, x) =>
adamc@66 1277 (case self str of
adamc@66 1278 NONE => NONE
adamc@66 1279 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 1280 | _ => NONE
adamc@66 1281 in
adamc@66 1282 case self str of
adamc@66 1283 NONE => sgn
adamc@66 1284 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 1285 end
adamc@66 1286
adamc@66 1287 fun dopen env {str, strs, sgn} =
adamc@66 1288 let
adamc@66 1289 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1290 (L'.StrVar str, #2 sgn) strs
adamc@66 1291 in
adamc@66 1292 case #1 (hnormSgn env sgn) of
adamc@66 1293 L'.SgnConst sgis =>
adamc@66 1294 ListUtil.foldlMap (fn ((sgi, loc), env') =>
adamc@66 1295 case sgi of
adamc@66 1296 L'.SgiConAbs (x, n, k) =>
adamc@66 1297 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
adamc@66 1298 E.pushCNamedAs env' x n k NONE)
adamc@66 1299 | L'.SgiCon (x, n, k, c) =>
adamc@66 1300 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
adamc@66 1301 E.pushCNamedAs env' x n k (SOME c))
adamc@66 1302 | L'.SgiVal (x, n, t) =>
adamc@66 1303 ((L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc),
adamc@66 1304 E.pushENamedAs env' x n t)
adamc@66 1305 | L'.SgiStr (x, n, sgn) =>
adamc@66 1306 ((L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc),
adamc@66 1307 E.pushStrNamedAs env' x n sgn)
adamc@66 1308 | L'.SgiSgn (x, n, sgn) =>
adamc@66 1309 ((L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc),
adamc@66 1310 E.pushSgnNamedAs env' x n sgn))
adamc@66 1311 env sgis
adamc@66 1312 | _ => (strError env (UnOpenable sgn);
adamc@66 1313 ([], env))
adamc@66 1314 end
adamc@66 1315
adamc@31 1316 fun sgiOfDecl (d, loc) =
adamc@31 1317 case d of
adamc@59 1318 L'.DCon (x, n, k, c) => (L'.SgiCon (x, n, k, c), loc)
adamc@59 1319 | L'.DVal (x, n, t, _) => (L'.SgiVal (x, n, t), loc)
adamc@59 1320 | L'.DSgn (x, n, sgn) => (L'.SgiSgn (x, n, sgn), loc)
adamc@59 1321 | L'.DStr (x, n, sgn, _) => (L'.SgiStr (x, n, sgn), loc)
adamc@59 1322 | L'.DFfiStr (x, n, sgn) => (L'.SgiStr (x, n, sgn), loc)
adamc@31 1323
adamc@35 1324 fun subSgn env sgn1 (sgn2 as (_, loc2)) =
adamc@35 1325 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 1326 (L'.SgnError, _) => ()
adamc@31 1327 | (_, L'.SgnError) => ()
adamc@31 1328
adamc@31 1329 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1330 let
adamc@31 1331 fun folder (sgi2All as (sgi, _), env) =
adamc@31 1332 let
adamc@31 1333 fun seek p =
adamc@31 1334 let
adamc@31 1335 fun seek env ls =
adamc@31 1336 case ls of
adamc@31 1337 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@31 1338 env)
adamc@31 1339 | h :: t =>
adamc@31 1340 case p h of
adamc@31 1341 NONE => seek (E.sgiBinds env h) t
adamc@31 1342 | SOME env => env
adamc@31 1343 in
adamc@31 1344 seek env sgis1
adamc@31 1345 end
adamc@31 1346 in
adamc@31 1347 case sgi of
adamc@31 1348 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1349 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1350 let
adamc@41 1351 fun found (x', n1, k1, co1) =
adamc@41 1352 if x = x' then
adamc@41 1353 let
adamc@41 1354 val () = unifyKinds k1 k2
adamc@41 1355 handle KUnify (k1, k2, err) =>
adamc@41 1356 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 1357 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 1358 in
adamc@41 1359 SOME (if n1 = n2 then
adamc@41 1360 env
adamc@41 1361 else
adamc@41 1362 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)))
adamc@41 1363 end
adamc@41 1364 else
adamc@41 1365 NONE
adamc@31 1366 in
adamc@31 1367 case sgi1 of
adamc@41 1368 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 1369 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@31 1370 | _ => NONE
adamc@31 1371 end)
adamc@31 1372
adamc@31 1373 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 1374 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1375 case sgi1 of
adamc@41 1376 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 1377 if x = x' then
adamc@41 1378 let
adamc@41 1379 val () = unifyCons env c1 c2
adamc@41 1380 handle CUnify (c1, c2, err) =>
adamc@41 1381 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
adamc@41 1382 in
adamc@41 1383 SOME (E.pushCNamedAs env x n2 k2 (SOME c2))
adamc@41 1384 end
adamc@41 1385 else
adamc@41 1386 NONE
adamc@31 1387 | _ => NONE)
adamc@31 1388
adamc@32 1389 | L'.SgiVal (x, n2, c2) =>
adamc@32 1390 seek (fn sgi1All as (sgi1, _) =>
adamc@32 1391 case sgi1 of
adamc@41 1392 L'.SgiVal (x', n1, c1) =>
adamc@41 1393 if x = x' then
adamc@41 1394 let
adamc@41 1395 val () = unifyCons env c1 c2
adamc@41 1396 handle CUnify (c1, c2, err) =>
adamc@41 1397 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
adamc@41 1398 in
adamc@41 1399 SOME env
adamc@41 1400 end
adamc@41 1401 else
adamc@41 1402 NONE
adamc@32 1403 | _ => NONE)
adamc@32 1404
adamc@33 1405 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 1406 seek (fn sgi1All as (sgi1, _) =>
adamc@33 1407 case sgi1 of
adamc@41 1408 L'.SgiStr (x', n1, sgn1) =>
adamc@41 1409 if x = x' then
adamc@66 1410 let
adamc@66 1411 val () = subSgn env sgn1 sgn2
adamc@66 1412 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 1413 val env = if n1 = n2 then
adamc@66 1414 env
adamc@66 1415 else
adamc@66 1416 E.pushStrNamedAs env x n2
adamc@66 1417 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 1418 sgn = sgn2})
adamc@66 1419 in
adamc@66 1420 SOME env
adamc@66 1421 end
adamc@41 1422 else
adamc@41 1423 NONE
adamc@33 1424 | _ => NONE)
adamc@59 1425
adamc@59 1426 | L'.SgiSgn (x, n2, sgn2) =>
adamc@59 1427 seek (fn sgi1All as (sgi1, _) =>
adamc@59 1428 case sgi1 of
adamc@59 1429 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 1430 if x = x' then
adamc@65 1431 let
adamc@65 1432 val () = subSgn env sgn1 sgn2
adamc@65 1433 val () = subSgn env sgn2 sgn1
adamc@65 1434
adamc@65 1435 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 1436 val env = if n1 = n2 then
adamc@65 1437 env
adamc@65 1438 else
adamc@65 1439 E.pushSgnNamedAs env x n1 sgn2
adamc@65 1440 in
adamc@65 1441 SOME env
adamc@65 1442 end
adamc@59 1443 else
adamc@59 1444 NONE
adamc@59 1445 | _ => NONE)
adamc@31 1446 end
adamc@31 1447 in
adamc@31 1448 ignore (foldl folder env sgis2)
adamc@31 1449 end
adamc@31 1450
adamc@41 1451 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 1452 let
adamc@43 1453 val ran1 =
adamc@43 1454 if n1 = n2 then
adamc@43 1455 ran1
adamc@43 1456 else
adamc@43 1457 subStrInSgn (n1, n2) ran1
adamc@43 1458 in
adamc@43 1459 subSgn env dom2 dom1;
adamc@43 1460 subSgn (E.pushStrNamedAs env m2 n2 dom2) ran1 ran2
adamc@43 1461 end
adamc@41 1462
adamc@41 1463 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 1464
adamc@61 1465
adamc@83 1466 fun elabDecl denv ((d, loc), (env, gs)) =
adamc@76 1467 case d of
adamc@76 1468 L.DCon (x, ko, c) =>
adamc@76 1469 let
adamc@76 1470 val k' = case ko of
adamc@76 1471 NONE => kunif loc
adamc@76 1472 | SOME k => elabKind k
adamc@76 1473
adamc@83 1474 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1475 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1476 in
adamc@76 1477 checkKind env c' ck k';
adamc@76 1478
adamc@83 1479 ([(L'.DCon (x, n, k', c'), loc)], (env', gs' @ gs))
adamc@76 1480 end
adamc@76 1481 | L.DVal (x, co, e) =>
adamc@76 1482 let
adamc@83 1483 val (c', ck, gs1) = case co of
adamc@83 1484 NONE => (cunif (loc, ktype), ktype, [])
adamc@83 1485 | SOME c => elabCon (env, denv) c
adamc@76 1486
adamc@83 1487 val (e', et, gs2) = elabExp (env, denv) e
adamc@76 1488 val (env', n) = E.pushENamed env x c'
adamc@76 1489 in
adamc@76 1490 checkCon env e' et c';
adamc@76 1491
adamc@83 1492 ([(L'.DVal (x, n, c', e'), loc)], (env', gs1 @ gs2 @ gs))
adamc@76 1493 end
adamc@76 1494
adamc@76 1495 | L.DSgn (x, sgn) =>
adamc@76 1496 let
adamc@83 1497 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1498 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1499 in
adamc@83 1500 ([(L'.DSgn (x, n, sgn'), loc)], (env', gs' @ gs))
adamc@76 1501 end
adamc@76 1502
adamc@76 1503 | L.DStr (x, sgno, str) =>
adamc@76 1504 let
adamc@83 1505 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@76 1506
adamc@83 1507 val (str', sgn', gs') =
adamc@78 1508 case formal of
adamc@78 1509 NONE =>
adamc@78 1510 let
adamc@83 1511 val (str', actual, ds) = elabStr (env, denv) str
adamc@78 1512 in
adamc@83 1513 (str', selfifyAt env {str = str', sgn = actual}, ds)
adamc@78 1514 end
adamc@83 1515 | SOME (formal, gs1) =>
adamc@78 1516 let
adamc@78 1517 val str =
adamc@78 1518 case #1 (hnormSgn env formal) of
adamc@78 1519 L'.SgnConst sgis =>
adamc@78 1520 (case #1 str of
adamc@78 1521 L.StrConst ds =>
adamc@78 1522 let
adamc@78 1523 val needed = foldl (fn ((sgi, _), needed) =>
adamc@78 1524 case sgi of
adamc@78 1525 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
adamc@78 1526 | _ => needed)
adamc@78 1527 SS.empty sgis
adamc@78 1528
adamc@78 1529 val needed = foldl (fn ((d, _), needed) =>
adamc@78 1530 case d of
adamc@78 1531 L.DCon (x, _, _) => (SS.delete (needed, x)
adamc@78 1532 handle NotFound => needed)
adamc@78 1533 | L.DOpen _ => SS.empty
adamc@78 1534 | _ => needed)
adamc@78 1535 needed ds
adamc@78 1536 in
adamc@78 1537 case SS.listItems needed of
adamc@78 1538 [] => str
adamc@78 1539 | xs =>
adamc@78 1540 let
adamc@78 1541 val kwild = (L.KWild, #2 str)
adamc@78 1542 val cwild = (L.CWild kwild, #2 str)
adamc@78 1543 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
adamc@78 1544 in
adamc@78 1545 (L.StrConst (ds @ ds'), #2 str)
adamc@78 1546 end
adamc@78 1547 end
adamc@78 1548 | _ => str)
adamc@78 1549 | _ => str
adamc@78 1550
adamc@83 1551 val (str', actual, gs2) = elabStr (env, denv) str
adamc@78 1552 in
adamc@78 1553 subSgn env actual formal;
adamc@83 1554 (str', formal, gs1 @ gs2)
adamc@78 1555 end
adamc@76 1556
adamc@76 1557 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1558 in
adamc@76 1559 case #1 (hnormSgn env sgn') of
adamc@76 1560 L'.SgnFun _ =>
adamc@76 1561 (case #1 str' of
adamc@76 1562 L'.StrFun _ => ()
adamc@76 1563 | _ => strError env (FunctorRebind loc))
adamc@76 1564 | _ => ();
adamc@76 1565
adamc@83 1566 ([(L'.DStr (x, n, sgn', str'), loc)], (env', gs' @ gs))
adamc@76 1567 end
adamc@76 1568
adamc@76 1569 | L.DFfiStr (x, sgn) =>
adamc@76 1570 let
adamc@83 1571 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1572
adamc@76 1573 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1574 in
adamc@83 1575 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', gs' @ gs))
adamc@76 1576 end
adamc@76 1577
adamc@76 1578 | L.DOpen (m, ms) =>
adamc@76 1579 case E.lookupStr env m of
adamc@76 1580 NONE => (strError env (UnboundStr (loc, m));
adamc@83 1581 ([], (env, [])))
adamc@76 1582 | SOME (n, sgn) =>
adamc@30 1583 let
adamc@76 1584 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@76 1585 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@76 1586 NONE => (strError env (UnboundStr (loc, m));
adamc@76 1587 (strerror, sgnerror))
adamc@76 1588 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@76 1589 ((L'.StrVar n, loc), sgn) ms
adamc@83 1590
adamc@83 1591 val (ds, env') = dopen env {str = n, strs = ms, sgn = sgn}
adamc@30 1592 in
adamc@83 1593 (ds, (env', []))
adamc@30 1594 end
adamc@3 1595
adamc@83 1596 and elabStr (env, denv) (str, loc) =
adamc@31 1597 case str of
adamc@31 1598 L.StrConst ds =>
adamc@31 1599 let
adamc@83 1600 val (ds', (env', gs)) = ListUtil.foldlMapConcat (elabDecl denv) (env, []) ds
adamc@59 1601 val sgis = map sgiOfDecl ds'
adamc@63 1602
adamc@63 1603 val (sgis, _, _, _, _) =
adamc@64 1604 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 1605 case sgi of
adamc@64 1606 L'.SgiConAbs (x, n, k) =>
adamc@64 1607 let
adamc@64 1608 val (cons, x) =
adamc@64 1609 if SS.member (cons, x) then
adamc@64 1610 (cons, "?" ^ x)
adamc@64 1611 else
adamc@64 1612 (SS.add (cons, x), x)
adamc@64 1613 in
adamc@64 1614 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1615 end
adamc@64 1616 | L'.SgiCon (x, n, k, c) =>
adamc@64 1617 let
adamc@64 1618 val (cons, x) =
adamc@64 1619 if SS.member (cons, x) then
adamc@64 1620 (cons, "?" ^ x)
adamc@64 1621 else
adamc@64 1622 (SS.add (cons, x), x)
adamc@64 1623 in
adamc@64 1624 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1625 end
adamc@64 1626 | L'.SgiVal (x, n, c) =>
adamc@64 1627 let
adamc@64 1628 val (vals, x) =
adamc@64 1629 if SS.member (vals, x) then
adamc@64 1630 (vals, "?" ^ x)
adamc@64 1631 else
adamc@64 1632 (SS.add (vals, x), x)
adamc@64 1633 in
adamc@64 1634 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1635 end
adamc@64 1636 | L'.SgiSgn (x, n, sgn) =>
adamc@64 1637 let
adamc@64 1638 val (sgns, x) =
adamc@64 1639 if SS.member (sgns, x) then
adamc@64 1640 (sgns, "?" ^ x)
adamc@64 1641 else
adamc@64 1642 (SS.add (sgns, x), x)
adamc@64 1643 in
adamc@64 1644 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1645 end
adamc@64 1646
adamc@64 1647 | L'.SgiStr (x, n, sgn) =>
adamc@64 1648 let
adamc@64 1649 val (strs, x) =
adamc@64 1650 if SS.member (strs, x) then
adamc@64 1651 (strs, "?" ^ x)
adamc@64 1652 else
adamc@64 1653 (SS.add (strs, x), x)
adamc@64 1654 in
adamc@64 1655 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1656 end)
adamc@64 1657
adamc@63 1658 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 1659 in
adamc@83 1660 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 1661 end
adamc@31 1662 | L.StrVar x =>
adamc@31 1663 (case E.lookupStr env x of
adamc@31 1664 NONE =>
adamc@31 1665 (strError env (UnboundStr (loc, x));
adamc@83 1666 (strerror, sgnerror, []))
adamc@83 1667 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 1668 | L.StrProj (str, x) =>
adamc@34 1669 let
adamc@83 1670 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 1671 in
adamc@34 1672 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 1673 NONE => (strError env (UnboundStr (loc, x));
adamc@83 1674 (strerror, sgnerror, []))
adamc@83 1675 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 1676 end
adamc@41 1677 | L.StrFun (m, dom, ranO, str) =>
adamc@41 1678 let
adamc@83 1679 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1680 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1681 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 1682
adamc@83 1683 val (formal, gs3) =
adamc@41 1684 case ranO of
adamc@83 1685 NONE => (actual, [])
adamc@41 1686 | SOME ran =>
adamc@41 1687 let
adamc@83 1688 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 1689 in
adamc@41 1690 subSgn env' actual ran';
adamc@83 1691 (ran', gs)
adamc@41 1692 end
adamc@41 1693 in
adamc@41 1694 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 1695 (L'.SgnFun (m, n, dom', formal), loc),
adamc@83 1696 gs1 @ gs2 @ gs3)
adamc@41 1697 end
adamc@44 1698 | L.StrApp (str1, str2) =>
adamc@44 1699 let
adamc@83 1700 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@83 1701 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 1702 in
adamc@44 1703 case #1 (hnormSgn env sgn1) of
adamc@83 1704 L'.SgnError => (strerror, sgnerror, [])
adamc@44 1705 | L'.SgnFun (m, n, dom, ran) =>
adamc@44 1706 (subSgn env sgn2 dom;
adamc@44 1707 case #1 (hnormSgn env ran) of
adamc@83 1708 L'.SgnError => (strerror, sgnerror, [])
adamc@44 1709 | L'.SgnConst sgis =>
adamc@44 1710 ((L'.StrApp (str1', str2'), loc),
adamc@83 1711 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 1712 gs1 @ gs2)
adamc@44 1713 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 1714 | _ => (strError env (NotFunctor sgn1);
adamc@83 1715 (strerror, sgnerror, []))
adamc@44 1716 end
adamc@31 1717
adamc@56 1718 fun elabFile basis env file =
adamc@56 1719 let
adamc@83 1720 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 1721 val () = case gs of
adamc@83 1722 [] => ()
adamc@83 1723 | _ => raise Fail "Unresolved disjointness constraints in Basis"
adamc@83 1724
adamc@56 1725 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@56 1726
adamc@61 1727 val (ds, env') = dopen env' {str = basis_n, strs = [], sgn = sgn}
adamc@56 1728
adamc@56 1729 fun discoverC r x =
adamc@56 1730 case E.lookupC env' x of
adamc@56 1731 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 1732 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 1733 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 1734
adamc@56 1735 val () = discoverC int "int"
adamc@56 1736 val () = discoverC float "float"
adamc@56 1737 val () = discoverC string "string"
adamc@56 1738
adamc@83 1739 fun elabDecl' (d, (env, gs)) =
adamc@76 1740 let
adamc@76 1741 val () = resetKunif ()
adamc@76 1742 val () = resetCunif ()
adamc@83 1743 val (ds, (env, gs)) = elabDecl D.empty (d, (env, gs))
adamc@76 1744 in
adamc@76 1745 if ErrorMsg.anyErrors () then
adamc@76 1746 ()
adamc@76 1747 else (
adamc@76 1748 if List.exists kunifsInDecl ds then
adamc@76 1749 declError env (KunifsRemain (#2 d))
adamc@76 1750 else
adamc@76 1751 ();
adamc@76 1752
adamc@76 1753 case ListUtil.search cunifsInDecl ds of
adamc@76 1754 NONE => ()
adamc@76 1755 | SOME loc =>
adamc@76 1756 declError env (CunifsRemain loc)
adamc@76 1757 );
adamc@76 1758
adamc@83 1759 (ds, (env, gs))
adamc@76 1760 end
adamc@76 1761
adamc@83 1762 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 1763 in
adamc@83 1764 app (fn (loc, env, denv, (c1, c2)) =>
adamc@83 1765 case D.prove env denv (c1, c2, loc) of
adamc@83 1766 [] => ()
adamc@83 1767 | _ =>
adamc@83 1768 (ErrorMsg.errorAt loc "Remaining constraint";
adamc@83 1769 eprefaces' [("Con 1", p_con env c1),
adamc@83 1770 ("Con 2", p_con env c2)])) gs;
adamc@83 1771
adamc@56 1772 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
adamc@56 1773 end
adamc@2 1774
adamc@2 1775 end