annotate src/elaborate.sml @ 139:adfa2c7a75da

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