annotate src/elaborate.sml @ 147:eb16f2aadbe9

Meta-programming forms
author Adam Chlipala <adamc@hcoop.net>
date Tue, 22 Jul 2008 18:46:04 -0400
parents 80ac94b54e41
children 7420fa18d657
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@141 447 | CKindof of L'.kind * 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@141 471 | CKindof (k, c) =>
adamc@147 472 eprefaces "Unexpected kind for kindof calculation"
adamc@141 473 [("Kind", p_kind k),
adamc@141 474 ("Con", p_con env c)]
adamc@12 475 | CRecordFailure =>
adamc@12 476 eprefaces "Can't unify record constructors" []
adamc@10 477
adamc@13 478 exception SynUnif = E.SynUnif
adamc@12 479
adamc@81 480 open ElabOps
adamc@43 481
adamc@12 482 type record_summary = {
adamc@12 483 fields : (L'.con * L'.con) list,
adamc@12 484 unifs : (L'.con * L'.con option ref) list,
adamc@12 485 others : L'.con list
adamc@12 486 }
adamc@12 487
adamc@12 488 fun summaryToCon {fields, unifs, others} =
adamc@12 489 let
adamc@12 490 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 491 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 492 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 493 in
adamc@12 494 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 495 end
adamc@12 496
adamc@12 497 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 498
adamc@12 499 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 500
adamc@12 501 fun hnormKind (kAll as (k, _)) =
adamc@12 502 case k of
adamc@76 503 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@12 504 | _ => kAll
adamc@12 505
adamc@12 506 fun kindof env (c, loc) =
adamc@12 507 case c of
adamc@12 508 L'.TFun _ => ktype
adamc@12 509 | L'.TCFun _ => ktype
adamc@85 510 | L'.TDisjoint _ => ktype
adamc@12 511 | L'.TRecord _ => ktype
adamc@12 512
adamc@12 513 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 514 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@34 515 | L'.CModProj (n, ms, x) =>
adamc@34 516 let
adamc@34 517 val (_, sgn) = E.lookupStrNamed env n
adamc@34 518 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 519 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 520 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 521 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 522 ((L'.StrVar n, loc), sgn) ms
adamc@34 523 in
adamc@34 524 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@34 525 NONE => raise Fail "kindof: Unknown con in structure"
adamc@34 526 | SOME (k, _) => k
adamc@34 527 end
adamc@34 528
adamc@12 529 | L'.CApp (c, _) =>
adamc@141 530 (case hnormKind (kindof env c) of
adamc@141 531 (L'.KArrow (_, k), _) => k
adamc@141 532 | (L'.KError, _) => kerror
adamc@141 533 | k => raise CUnify' (CKindof (k, c)))
adamc@12 534 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@84 535 | L'.CDisjoint (_, _, c) => kindof env c
adamc@12 536
adamc@12 537 | L'.CName _ => kname
adamc@12 538
adamc@12 539 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 540 | L'.CConcat (c, _) => kindof env c
adamc@67 541 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
adamc@12 542
adamc@82 543 | L'.CUnit => (L'.KUnit, loc)
adamc@82 544
adamc@12 545 | L'.CError => kerror
adamc@76 546 | L'.CUnif (_, k, _, _) => k
adamc@12 547
adamc@90 548 val hnormCon = D.hnormCon
adamc@86 549
adamc@86 550 fun unifyRecordCons (env, denv) (c1, c2) =
adamc@12 551 let
adamc@92 552 fun rkindof c =
adamc@147 553 case hnormKind (kindof env c) of
adamc@92 554 (L'.KRecord k, _) => k
adamc@141 555 | (L'.KError, _) => kerror
adamc@141 556 | k => raise CUnify' (CKindof (k, c))
adamc@92 557
adamc@92 558 val k1 = rkindof c1
adamc@92 559 val k2 = rkindof c2
adamc@86 560
adamc@86 561 val (r1, gs1) = recordSummary (env, denv) c1
adamc@86 562 val (r2, gs2) = recordSummary (env, denv) c2
adamc@12 563 in
adamc@12 564 unifyKinds k1 k2;
adamc@86 565 unifySummaries (env, denv) (k1, r1, r2);
adamc@86 566 gs1 @ gs2
adamc@12 567 end
adamc@12 568
adamc@86 569 and recordSummary (env, denv) c =
adamc@86 570 let
adamc@86 571 val (c, gs) = hnormCon (env, denv) c
adamc@12 572
adamc@86 573 val (sum, gs') =
adamc@86 574 case c of
adamc@86 575 (L'.CRecord (_, xcs), _) => ({fields = xcs, unifs = [], others = []}, [])
adamc@86 576 | (L'.CConcat (c1, c2), _) =>
adamc@86 577 let
adamc@86 578 val (s1, gs1) = recordSummary (env, denv) c1
adamc@86 579 val (s2, gs2) = recordSummary (env, denv) c2
adamc@86 580 in
adamc@86 581 ({fields = #fields s1 @ #fields s2,
adamc@86 582 unifs = #unifs s1 @ #unifs s2,
adamc@86 583 others = #others s1 @ #others s2},
adamc@86 584 gs1 @ gs2)
adamc@86 585 end
adamc@86 586 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary (env, denv) c
adamc@86 587 | c' as (L'.CUnif (_, _, _, r), _) => ({fields = [], unifs = [(c', r)], others = []}, [])
adamc@86 588 | c' => ({fields = [], unifs = [], others = [c']}, [])
adamc@86 589 in
adamc@86 590 (sum, gs @ gs')
adamc@86 591 end
adamc@86 592
adamc@86 593 and consEq (env, denv) (c1, c2) =
adamc@86 594 (case unifyCons (env, denv) c1 c2 of
adamc@86 595 [] => true
adamc@86 596 | _ => false)
adamc@12 597 handle CUnify _ => false
adamc@12 598
adamc@80 599 and consNeq env (c1, c2) =
adamc@86 600 case (#1 (ElabOps.hnormCon env c1), #1 (ElabOps.hnormCon env c2)) of
adamc@80 601 (L'.CName x1, L'.CName x2) => x1 <> x2
adamc@80 602 | _ => false
adamc@80 603
adamc@86 604 and unifySummaries (env, denv) (k, s1 : record_summary, s2 : record_summary) =
adamc@12 605 let
adamc@14 606 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@14 607 ("#2", p_summary env s2)]*)
adamc@12 608
adamc@12 609 fun eatMatching p (ls1, ls2) =
adamc@12 610 let
adamc@12 611 fun em (ls1, ls2, passed1) =
adamc@12 612 case ls1 of
adamc@12 613 [] => (rev passed1, ls2)
adamc@12 614 | h1 :: t1 =>
adamc@12 615 let
adamc@12 616 fun search (ls2', passed2) =
adamc@12 617 case ls2' of
adamc@12 618 [] => em (t1, ls2, h1 :: passed1)
adamc@12 619 | h2 :: t2 =>
adamc@12 620 if p (h1, h2) then
adamc@12 621 em (t1, List.revAppend (passed2, t2), passed1)
adamc@12 622 else
adamc@12 623 search (t2, h2 :: passed2)
adamc@12 624 in
adamc@12 625 search (ls2, [])
adamc@12 626 end
adamc@12 627 in
adamc@12 628 em (ls1, ls2, [])
adamc@12 629 end
adamc@12 630
adamc@12 631 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@80 632 not (consNeq env (x1, x2))
adamc@86 633 andalso consEq (env, denv) (c1, c2)
adamc@86 634 andalso consEq (env, denv) (x1, x2))
adamc@79 635 (#fields s1, #fields s2)
adamc@14 636 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@14 637 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@12 638 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@86 639 val (others1, others2) = eatMatching (consEq (env, denv)) (#others s1, #others s2)
adamc@12 640
adamc@12 641 fun unifFields (fs, others, unifs) =
adamc@12 642 case (fs, others, unifs) of
adamc@12 643 ([], [], _) => ([], [], unifs)
adamc@12 644 | (_, _, []) => (fs, others, [])
adamc@12 645 | (_, _, (_, r) :: rest) =>
adamc@12 646 let
adamc@12 647 val r' = ref NONE
adamc@141 648 val kr = (L'.KRecord k, dummy)
adamc@141 649 val cr' = (L'.CUnif (dummy, kr, "recd", r'), dummy)
adamc@12 650
adamc@12 651 val prefix = case (fs, others) of
adamc@12 652 ([], other :: others) =>
adamc@12 653 List.foldl (fn (other, c) =>
adamc@12 654 (L'.CConcat (c, other), dummy))
adamc@12 655 other others
adamc@12 656 | (fs, []) =>
adamc@12 657 (L'.CRecord (k, fs), dummy)
adamc@12 658 | (fs, others) =>
adamc@12 659 List.foldl (fn (other, c) =>
adamc@12 660 (L'.CConcat (c, other), dummy))
adamc@12 661 (L'.CRecord (k, fs), dummy) others
adamc@12 662 in
adamc@12 663 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 664 ([], [], (cr', r') :: rest)
adamc@12 665 end
adamc@12 666
adamc@12 667 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 668 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 669
adamc@86 670 val clear = case (fs1, others1, fs2, others2) of
adamc@86 671 ([], [], [], []) => true
adamc@12 672 | _ => false
adamc@12 673 val empty = (L'.CRecord (k, []), dummy)
adamc@12 674 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 675 case (unifs1, unifs2) of
adamc@12 676 ([], _) =>
adamc@86 677 if clear then
adamc@12 678 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 679 else
adamc@12 680 raise CUnify' CRecordFailure
adamc@12 681 | (_, []) =>
adamc@86 682 if clear then
adamc@12 683 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 684 else
adamc@12 685 raise CUnify' CRecordFailure
adamc@12 686 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 687 (r2 := SOME c1;
adamc@12 688 pairOffUnifs (rest1, rest2))
adamc@12 689 in
adamc@12 690 pairOffUnifs (unifs1, unifs2)
adamc@12 691 end
adamc@12 692
adamc@86 693 and unifyCons' (env, denv) c1 c2 =
adamc@86 694 let
adamc@86 695 val (c1, gs1) = hnormCon (env, denv) c1
adamc@86 696 val (c2, gs2) = hnormCon (env, denv) c2
adamc@90 697 val gs3 = unifyCons'' (env, denv) c1 c2
adamc@86 698 in
adamc@90 699 gs1 @ gs2 @ gs3
adamc@86 700 end
adamc@11 701
adamc@86 702 and unifyCons'' (env, denv) (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 703 let
adamc@10 704 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 705
adamc@86 706 fun isRecord () = unifyRecordCons (env, denv) (c1All, c2All)
adamc@10 707 in
adamc@92 708 (*eprefaces "unifyCons''" [("c1All", p_con env c1All),
adamc@92 709 ("c2All", p_con env c2All)];*)
adamc@92 710
adamc@10 711 case (c1, c2) of
adamc@88 712 (L'.CUnit, L'.CUnit) => []
adamc@88 713
adamc@88 714 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@86 715 unifyCons' (env, denv) d1 d2
adamc@86 716 @ unifyCons' (env, denv) r1 r2
adamc@10 717 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 718 if expl1 <> expl2 then
adamc@10 719 err CExplicitness
adamc@10 720 else
adamc@10 721 (unifyKinds d1 d2;
adamc@86 722 unifyCons' (E.pushCRel env x1 d1, D.enter denv) r1 r2)
adamc@86 723 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' (env, denv) r1 r2
adamc@10 724
adamc@10 725 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 726 if n1 = n2 then
adamc@86 727 []
adamc@10 728 else
adamc@10 729 err CIncompatible
adamc@10 730 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 731 if n1 = n2 then
adamc@86 732 []
adamc@10 733 else
adamc@10 734 err CIncompatible
adamc@10 735
adamc@10 736 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@86 737 (unifyCons' (env, denv) d1 d2;
adamc@86 738 unifyCons' (env, denv) r1 r2)
adamc@10 739 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 740 (unifyKinds k1 k2;
adamc@86 741 unifyCons' (E.pushCRel env x1 k1, D.enter denv) c1 c2)
adamc@10 742
adamc@10 743 | (L'.CName n1, L'.CName n2) =>
adamc@10 744 if n1 = n2 then
adamc@86 745 []
adamc@10 746 else
adamc@10 747 err CIncompatible
adamc@10 748
adamc@34 749 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@34 750 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@86 751 []
adamc@34 752 else
adamc@34 753 err CIncompatible
adamc@34 754
adamc@86 755 | (L'.CError, _) => []
adamc@86 756 | (_, L'.CError) => []
adamc@10 757
adamc@92 758 | (L'.CRecord _, _) => isRecord ()
adamc@92 759 | (_, L'.CRecord _) => isRecord ()
adamc@92 760 | (L'.CConcat _, _) => isRecord ()
adamc@92 761 | (_, L'.CConcat _) => isRecord ()
adamc@92 762 (*| (L'.CUnif (_, (L'.KRecord _, _), _, _), _) => isRecord ()
adamc@92 763 | (_, L'.CUnif (_, (L'.KRecord _, _), _, _)) => isRecord ()*)
adamc@10 764
adamc@76 765 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
adamc@10 766 if r1 = r2 then
adamc@86 767 []
adamc@10 768 else
adamc@10 769 (unifyKinds k1 k2;
adamc@86 770 r1 := SOME c2All;
adamc@86 771 [])
adamc@10 772
adamc@76 773 | (L'.CUnif (_, _, _, r), _) =>
adamc@10 774 if occursCon r c2All then
adamc@10 775 err COccursCheckFailed
adamc@10 776 else
adamc@86 777 (r := SOME c2All;
adamc@86 778 [])
adamc@76 779 | (_, L'.CUnif (_, _, _, r)) =>
adamc@10 780 if occursCon r c1All then
adamc@10 781 err COccursCheckFailed
adamc@10 782 else
adamc@86 783 (r := SOME c1All;
adamc@86 784 [])
adamc@10 785
adamc@12 786
adamc@71 787 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
adamc@71 788 (unifyKinds dom1 dom2;
adamc@86 789 unifyKinds ran1 ran2;
adamc@86 790 [])
adamc@71 791
adamc@10 792 | _ => err CIncompatible
adamc@10 793 end
adamc@10 794
adamc@86 795 and unifyCons (env, denv) c1 c2 =
adamc@86 796 unifyCons' (env, denv) c1 c2
adamc@10 797 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 798 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 799
adamc@10 800 datatype exp_error =
adamc@10 801 UnboundExp of ErrorMsg.span * string
adamc@34 802 | UnboundStrInExp of ErrorMsg.span * string
adamc@10 803 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 804 | Unif of string * L'.con
adamc@11 805 | WrongForm of string * L'.exp * L'.con
adamc@92 806 | IncompatibleCons of L'.con * L'.con
adamc@10 807
adamc@10 808 fun expError env err =
adamc@10 809 case err of
adamc@10 810 UnboundExp (loc, s) =>
adamc@10 811 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@34 812 | UnboundStrInExp (loc, s) =>
adamc@34 813 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@10 814 | Unify (e, c1, c2, uerr) =>
adamc@10 815 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 816 eprefaces' [("Expression", p_exp env e),
adamc@10 817 ("Have con", p_con env c1),
adamc@10 818 ("Need con", p_con env c2)];
adamc@10 819 cunifyError env uerr)
adamc@11 820 | Unif (action, c) =>
adamc@11 821 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 822 eprefaces' [("Con", p_con env c)])
adamc@11 823 | WrongForm (variety, e, t) =>
adamc@11 824 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 825 eprefaces' [("Expression", p_exp env e),
adamc@11 826 ("Type", p_con env t)])
adamc@92 827 | IncompatibleCons (c1, c2) =>
adamc@92 828 (ErrorMsg.errorAt (#2 c1) "Incompatible constructors";
adamc@92 829 eprefaces' [("Con 1", p_con env c1),
adamc@92 830 ("Con 2", p_con env c2)])
adamc@10 831
adamc@86 832 fun checkCon (env, denv) e c1 c2 =
adamc@86 833 unifyCons (env, denv) c1 c2
adamc@10 834 handle CUnify (c1, c2, err) =>
adamc@86 835 (expError env (Unify (e, c1, c2, err));
adamc@86 836 [])
adamc@10 837
adamc@14 838 fun primType env p =
adamc@56 839 case p of
adamc@56 840 P.Int _ => !int
adamc@56 841 | P.Float _ => !float
adamc@56 842 | P.String _ => !string
adamc@14 843
adamc@71 844 fun recCons (k, nm, v, rest, loc) =
adamc@71 845 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
adamc@71 846 rest), loc)
adamc@71 847
adamc@71 848 fun foldType (dom, loc) =
adamc@71 849 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
adamc@71 850 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
adamc@71 851 (L'.TCFun (L'.Explicit, "v", dom,
adamc@71 852 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
adamc@71 853 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
adamc@71 854 (L'.CApp ((L'.CRel 3, loc),
adamc@71 855 recCons (dom,
adamc@71 856 (L'.CRel 2, loc),
adamc@71 857 (L'.CRel 1, loc),
adamc@71 858 (L'.CRel 0, loc),
adamc@71 859 loc)), loc)), loc)),
adamc@71 860 loc)), loc)), loc),
adamc@71 861 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
adamc@71 862 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
adamc@71 863 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
adamc@71 864 loc)), loc)), loc)
adamc@71 865
adamc@15 866 fun typeof env (e, loc) =
adamc@15 867 case e of
adamc@56 868 L'.EPrim p => primType env p
adamc@15 869 | L'.ERel n => #2 (E.lookupERel env n)
adamc@15 870 | L'.ENamed n => #2 (E.lookupENamed env n)
adamc@34 871 | L'.EModProj (n, ms, x) =>
adamc@34 872 let
adamc@34 873 val (_, sgn) = E.lookupStrNamed env n
adamc@34 874 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 875 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@147 876 NONE => raise Fail "typeof: Unknown substructure"
adamc@34 877 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 878 ((L'.StrVar n, loc), sgn) ms
adamc@34 879 in
adamc@34 880 case E.projectVal env {sgn = sgn, str = str, field = x} of
adamc@34 881 NONE => raise Fail "typeof: Unknown val in structure"
adamc@34 882 | SOME t => t
adamc@34 883 end
adamc@34 884
adamc@15 885 | L'.EApp (e1, _) =>
adamc@15 886 (case #1 (typeof env e1) of
adamc@15 887 L'.TFun (_, c) => c
adamc@15 888 | _ => raise Fail "typeof: Bad EApp")
adamc@26 889 | L'.EAbs (_, _, ran, _) => ran
adamc@15 890 | L'.ECApp (e1, c) =>
adamc@15 891 (case #1 (typeof env e1) of
adamc@15 892 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
adamc@15 893 | _ => raise Fail "typeof: Bad ECApp")
adamc@15 894 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
adamc@15 895
adamc@29 896 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
adamc@15 897 | L'.EField (_, _, {field, ...}) => field
adamc@71 898 | L'.EFold dom => foldType (dom, loc)
adamc@15 899
adamc@15 900 | L'.EError => cerror
adamc@15 901
adamc@86 902 fun elabHead (env, denv) (e as (_, loc)) t =
adamc@15 903 let
adamc@15 904 fun unravel (t, e) =
adamc@86 905 let
adamc@86 906 val (t, gs) = hnormCon (env, denv) t
adamc@86 907 in
adamc@86 908 case t of
adamc@86 909 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@86 910 let
adamc@86 911 val u = cunif (loc, k)
adamc@86 912
adamc@86 913 val (e, t, gs') = unravel (subConInCon (0, u) t',
adamc@86 914 (L'.ECApp (e, u), loc))
adamc@86 915 in
adamc@86 916 (e, t, gs @ gs')
adamc@86 917 end
adamc@86 918 | _ => (e, t, gs)
adamc@86 919 end
adamc@15 920 in
adamc@15 921 unravel (t, e)
adamc@15 922 end
adamc@15 923
adamc@92 924 fun elabExp (env, denv) (eAll as (e, loc)) =
adamc@91 925 let
adamc@92 926
adamc@92 927 in
adamc@92 928 (*eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*)
adamc@10 929
adamc@91 930 case e of
adamc@91 931 L.EAnnot (e, t) =>
adamc@91 932 let
adamc@91 933 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 934 val (t', _, gs2) = elabCon (env, denv) t
adamc@91 935 val gs3 = checkCon (env, denv) e' et t'
adamc@91 936 in
adamc@91 937 (e', t', gs1 @ gs2 @ gs3)
adamc@91 938 end
adamc@34 939
adamc@91 940 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@91 941 | L.EVar ([], s) =>
adamc@91 942 (case E.lookupE env s of
adamc@91 943 E.NotBound =>
adamc@91 944 (expError env (UnboundExp (loc, s));
adamc@91 945 (eerror, cerror, []))
adamc@91 946 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
adamc@91 947 | E.Named (n, t) => ((L'.ENamed n, loc), t, []))
adamc@91 948 | L.EVar (m1 :: ms, s) =>
adamc@91 949 (case E.lookupStr env m1 of
adamc@91 950 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@91 951 (eerror, cerror, []))
adamc@91 952 | SOME (n, sgn) =>
adamc@91 953 let
adamc@91 954 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@91 955 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@91 956 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@91 957 (strerror, sgnerror))
adamc@91 958 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@91 959 ((L'.StrVar n, loc), sgn) ms
adamc@10 960
adamc@91 961 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@91 962 NONE => (expError env (UnboundExp (loc, s));
adamc@91 963 cerror)
adamc@91 964 | SOME t => t
adamc@91 965 in
adamc@91 966 ((L'.EModProj (n, ms, s), loc), t, [])
adamc@91 967 end)
adamc@86 968
adamc@91 969 | L.EApp (e1, e2) =>
adamc@91 970 let
adamc@91 971 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@91 972 val (e1', t1, gs2) = elabHead (env, denv) e1' t1
adamc@91 973 val (e2', t2, gs3) = elabExp (env, denv) e2
adamc@71 974
adamc@91 975 val dom = cunif (loc, ktype)
adamc@91 976 val ran = cunif (loc, ktype)
adamc@91 977 val t = (L'.TFun (dom, ran), dummy)
adamc@91 978
adamc@91 979 val gs4 = checkCon (env, denv) e1' t1 t
adamc@91 980 val gs5 = checkCon (env, denv) e2' t2 dom
adamc@91 981 in
adamc@91 982 ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2 @ gs3 @ gs4 @ gs5)
adamc@91 983 end
adamc@91 984 | L.EAbs (x, to, e) =>
adamc@91 985 let
adamc@91 986 val (t', gs1) = case to of
adamc@91 987 NONE => (cunif (loc, ktype), [])
adamc@91 988 | SOME t =>
adamc@91 989 let
adamc@91 990 val (t', tk, gs) = elabCon (env, denv) t
adamc@91 991 in
adamc@91 992 checkKind env t' tk ktype;
adamc@91 993 (t', gs)
adamc@91 994 end
adamc@91 995 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
adamc@91 996 in
adamc@91 997 ((L'.EAbs (x, t', et, e'), loc),
adamc@91 998 (L'.TFun (t', et), loc),
adamc@91 999 gs1 @ gs2)
adamc@91 1000 end
adamc@91 1001 | L.ECApp (e, c) =>
adamc@91 1002 let
adamc@91 1003 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1004 val (e', et, gs2) = elabHead (env, denv) e' et
adamc@91 1005 val (c', ck, gs3) = elabCon (env, denv) c
adamc@91 1006 val ((et', _), gs4) = hnormCon (env, denv) et
adamc@91 1007 in
adamc@91 1008 case et' of
adamc@91 1009 L'.CError => (eerror, cerror, [])
adamc@91 1010 | L'.TCFun (_, _, k, eb) =>
adamc@91 1011 let
adamc@91 1012 val () = checkKind env c' ck k
adamc@91 1013 val eb' = subConInCon (0, c') eb
adamc@91 1014 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@91 1015 cerror)
adamc@91 1016 in
adamc@91 1017 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1018 end
adamc@91 1019
adamc@91 1020 | L'.CUnif _ =>
adamc@91 1021 (expError env (Unif ("application", et));
adamc@91 1022 (eerror, cerror, []))
adamc@91 1023
adamc@91 1024 | _ =>
adamc@91 1025 (expError env (WrongForm ("constructor function", e', et));
adamc@91 1026 (eerror, cerror, []))
adamc@91 1027 end
adamc@91 1028 | L.ECAbs (expl, x, k, e) =>
adamc@91 1029 let
adamc@91 1030 val expl' = elabExplicitness expl
adamc@91 1031 val k' = elabKind k
adamc@91 1032 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
adamc@91 1033 in
adamc@91 1034 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@91 1035 (L'.TCFun (expl', x, k', et), loc),
adamc@91 1036 gs)
adamc@91 1037 end
adamc@91 1038
adamc@91 1039 | L.EDisjoint (c1, c2, e) =>
adamc@91 1040 let
adamc@91 1041 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@91 1042 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@91 1043
adamc@91 1044 val ku1 = kunif loc
adamc@91 1045 val ku2 = kunif loc
adamc@91 1046
adamc@91 1047 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@91 1048 val (e', t, gs4) = elabExp (env, denv') e
adamc@91 1049 in
adamc@91 1050 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@91 1051 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@91 1052
adamc@91 1053 (e', (L'.TDisjoint (c1', c2', t), loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1054 end
adamc@91 1055
adamc@91 1056 | L.ERecord xes =>
adamc@91 1057 let
adamc@91 1058 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@91 1059 let
adamc@91 1060 val (x', xk, gs1) = elabCon (env, denv) x
adamc@91 1061 val (e', et, gs2) = elabExp (env, denv) e
adamc@91 1062 in
adamc@91 1063 checkKind env x' xk kname;
adamc@91 1064 ((x', e', et), gs1 @ gs2 @ gs)
adamc@91 1065 end)
adamc@91 1066 [] xes
adamc@91 1067
adamc@91 1068 val k = (L'.KType, loc)
adamc@91 1069
adamc@91 1070 fun prove (xets, gs) =
adamc@91 1071 case xets of
adamc@91 1072 [] => gs
adamc@91 1073 | (x, _, t) :: rest =>
adamc@91 1074 let
adamc@91 1075 val xc = (x, t)
adamc@91 1076 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@91 1077 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@91 1078 let
adamc@91 1079 val xc' = (x', t')
adamc@91 1080 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@91 1081 in
adamc@91 1082 D.prove env denv (r1, r2, loc) @ gs
adamc@91 1083 end)
adamc@91 1084 gs rest
adamc@91 1085 in
adamc@91 1086 prove (rest, gs)
adamc@91 1087 end
adamc@91 1088 in
adamc@91 1089 ((L'.ERecord xes', loc),
adamc@91 1090 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@91 1091 prove (xes', gs))
adamc@91 1092 end
adamc@91 1093
adamc@91 1094 | L.EField (e, c) =>
adamc@91 1095 let
adamc@91 1096 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1097 val (c', ck, gs2) = elabCon (env, denv) c
adamc@91 1098
adamc@91 1099 val ft = cunif (loc, ktype)
adamc@91 1100 val rest = cunif (loc, ktype_record)
adamc@91 1101 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@91 1102
adamc@91 1103 val gs3 =
adamc@91 1104 checkCon (env, denv) e' et
adamc@91 1105 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@91 1106 val gs4 = D.prove env denv (first, rest, loc)
adamc@91 1107 in
adamc@91 1108 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1109 end
adamc@91 1110
adamc@91 1111 | L.EFold =>
adamc@91 1112 let
adamc@91 1113 val dom = kunif loc
adamc@91 1114 in
adamc@91 1115 ((L'.EFold dom, loc), foldType (dom, loc), [])
adamc@91 1116 end
adamc@91 1117 end
adamc@12 1118
adamc@12 1119
adamc@6 1120 datatype decl_error =
adamc@91 1121 KunifsRemain of L'.decl list
adamc@91 1122 | CunifsRemain of L'.decl list
adamc@91 1123
adamc@91 1124 fun lspan [] = ErrorMsg.dummySpan
adamc@91 1125 | lspan ((_, loc) :: _) = loc
adamc@6 1126
adamc@6 1127 fun declError env err =
adamc@6 1128 case err of
adamc@91 1129 KunifsRemain ds =>
adamc@91 1130 (ErrorMsg.errorAt (lspan ds) "Some kind unification variables are undetermined in declaration";
adamc@91 1131 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@91 1132 | CunifsRemain ds =>
adamc@91 1133 (ErrorMsg.errorAt (lspan ds) "Some constructor unification variables are undetermined in declaration";
adamc@91 1134 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@6 1135
adamc@31 1136 datatype sgn_error =
adamc@31 1137 UnboundSgn of ErrorMsg.span * string
adamc@31 1138 | UnmatchedSgi of L'.sgn_item
adamc@31 1139 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 1140 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@41 1141 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 1142 | UnWhereable of L'.sgn * string
adamc@75 1143 | WhereWrongKind of L'.kind * L'.kind * kunify_error
adamc@58 1144 | NotIncludable of L'.sgn
adamc@62 1145 | DuplicateCon of ErrorMsg.span * string
adamc@62 1146 | DuplicateVal of ErrorMsg.span * string
adamc@62 1147 | DuplicateSgn of ErrorMsg.span * string
adamc@62 1148 | DuplicateStr of ErrorMsg.span * string
adamc@88 1149 | NotConstraintsable of L'.sgn
adamc@31 1150
adamc@31 1151 fun sgnError env err =
adamc@31 1152 case err of
adamc@31 1153 UnboundSgn (loc, s) =>
adamc@31 1154 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 1155 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 1156 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 1157 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 1158 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 1159 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@59 1160 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1161 ("Need", p_sgn_item env sgi2),
adamc@31 1162 ("Kind 1", p_kind k1),
adamc@31 1163 ("Kind 2", p_kind k2)];
adamc@31 1164 kunifyError kerr)
adamc@31 1165 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 1166 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@59 1167 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1168 ("Need", p_sgn_item env sgi2),
adamc@31 1169 ("Con 1", p_con env c1),
adamc@31 1170 ("Con 2", p_con env c2)];
adamc@31 1171 cunifyError env cerr)
adamc@41 1172 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1173 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1174 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1175 ("Sig 2", p_sgn env sgn2)])
adamc@42 1176 | UnWhereable (sgn, x) =>
adamc@42 1177 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1178 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1179 ("Field", PD.string x)])
adamc@75 1180 | WhereWrongKind (k1, k2, kerr) =>
adamc@75 1181 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
adamc@75 1182 eprefaces' [("Have", p_kind k1),
adamc@75 1183 ("Need", p_kind k2)];
adamc@75 1184 kunifyError kerr)
adamc@58 1185 | NotIncludable sgn =>
adamc@58 1186 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
adamc@58 1187 eprefaces' [("Signature", p_sgn env sgn)])
adamc@62 1188 | DuplicateCon (loc, s) =>
adamc@62 1189 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
adamc@62 1190 | DuplicateVal (loc, s) =>
adamc@62 1191 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
adamc@62 1192 | DuplicateSgn (loc, s) =>
adamc@62 1193 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
adamc@62 1194 | DuplicateStr (loc, s) =>
adamc@62 1195 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
adamc@88 1196 | NotConstraintsable sgn =>
adamc@88 1197 (ErrorMsg.errorAt (#2 sgn) "Invalid signature for 'open constraints'";
adamc@88 1198 eprefaces' [("Signature", p_sgn env sgn)])
adamc@31 1199
adamc@31 1200 datatype str_error =
adamc@31 1201 UnboundStr of ErrorMsg.span * string
adamc@44 1202 | NotFunctor of L'.sgn
adamc@46 1203 | FunctorRebind of ErrorMsg.span
adamc@61 1204 | UnOpenable of L'.sgn
adamc@75 1205 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
adamc@31 1206
adamc@31 1207 fun strError env err =
adamc@31 1208 case err of
adamc@31 1209 UnboundStr (loc, s) =>
adamc@31 1210 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1211 | NotFunctor sgn =>
adamc@44 1212 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1213 eprefaces' [("Signature", p_sgn env sgn)])
adamc@46 1214 | FunctorRebind loc =>
adamc@46 1215 ErrorMsg.errorAt loc "Attempt to rebind functor"
adamc@61 1216 | UnOpenable sgn =>
adamc@61 1217 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
adamc@61 1218 eprefaces' [("Signature", p_sgn env sgn)])
adamc@75 1219 | NotType (k, (k1, k2, ue)) =>
adamc@75 1220 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
adamc@75 1221 eprefaces' [("Kind", p_kind k),
adamc@75 1222 ("Subkind 1", p_kind k1),
adamc@75 1223 ("Subkind 2", p_kind k2)];
adamc@75 1224 kunifyError ue)
adamc@31 1225
adamc@42 1226 val hnormSgn = E.hnormSgn
adamc@31 1227
adamc@88 1228 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@76 1229 case sgi of
adamc@76 1230 L.SgiConAbs (x, k) =>
adamc@76 1231 let
adamc@76 1232 val k' = elabKind k
adamc@31 1233
adamc@76 1234 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1235 in
adamc@88 1236 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@76 1237 end
adamc@31 1238
adamc@76 1239 | L.SgiCon (x, ko, c) =>
adamc@76 1240 let
adamc@76 1241 val k' = case ko of
adamc@76 1242 NONE => kunif loc
adamc@76 1243 | SOME k => elabKind k
adamc@31 1244
adamc@83 1245 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1246 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1247 in
adamc@76 1248 checkKind env c' ck k';
adamc@31 1249
adamc@88 1250 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1251 end
adamc@31 1252
adamc@76 1253 | L.SgiVal (x, c) =>
adamc@76 1254 let
adamc@83 1255 val (c', ck, gs') = elabCon (env, denv) c
adamc@31 1256
adamc@76 1257 val (env', n) = E.pushENamed env x c'
adamc@76 1258 in
adamc@76 1259 (unifyKinds ck ktype
adamc@76 1260 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 1261
adamc@88 1262 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@76 1263 end
adamc@31 1264
adamc@76 1265 | L.SgiStr (x, sgn) =>
adamc@76 1266 let
adamc@83 1267 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1268 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1269 in
adamc@88 1270 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1271 end
adamc@31 1272
adamc@76 1273 | L.SgiSgn (x, sgn) =>
adamc@76 1274 let
adamc@83 1275 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1276 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1277 in
adamc@88 1278 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1279 end
adamc@31 1280
adamc@76 1281 | L.SgiInclude sgn =>
adamc@76 1282 let
adamc@83 1283 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1284 in
adamc@76 1285 case #1 (hnormSgn env sgn') of
adamc@76 1286 L'.SgnConst sgis =>
adamc@88 1287 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@76 1288 | _ => (sgnError env (NotIncludable sgn');
adamc@88 1289 ([], (env, denv, [])))
adamc@88 1290 end
adamc@88 1291
adamc@88 1292 | L.SgiConstraint (c1, c2) =>
adamc@88 1293 let
adamc@88 1294 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 1295 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@88 1296
adamc@90 1297 val (denv, gs3) = D.assert env denv (c1', c2')
adamc@88 1298 in
adamc@88 1299 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 1300 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 1301
adamc@90 1302 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3))
adamc@76 1303 end
adamc@31 1304
adamc@83 1305 and elabSgn (env, denv) (sgn, loc) =
adamc@31 1306 case sgn of
adamc@31 1307 L.SgnConst sgis =>
adamc@31 1308 let
adamc@88 1309 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 1310
adamc@62 1311 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 1312 case sgi of
adamc@62 1313 L'.SgiConAbs (x, _, _) =>
adamc@62 1314 (if SS.member (cons, x) then
adamc@62 1315 sgnError env (DuplicateCon (loc, x))
adamc@62 1316 else
adamc@62 1317 ();
adamc@62 1318 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1319 | L'.SgiCon (x, _, _, _) =>
adamc@62 1320 (if SS.member (cons, x) then
adamc@62 1321 sgnError env (DuplicateCon (loc, x))
adamc@62 1322 else
adamc@62 1323 ();
adamc@62 1324 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1325 | L'.SgiVal (x, _, _) =>
adamc@62 1326 (if SS.member (vals, x) then
adamc@62 1327 sgnError env (DuplicateVal (loc, x))
adamc@62 1328 else
adamc@62 1329 ();
adamc@62 1330 (cons, SS.add (vals, x), sgns, strs))
adamc@62 1331 | L'.SgiSgn (x, _, _) =>
adamc@62 1332 (if SS.member (sgns, x) then
adamc@62 1333 sgnError env (DuplicateSgn (loc, x))
adamc@62 1334 else
adamc@62 1335 ();
adamc@62 1336 (cons, vals, SS.add (sgns, x), strs))
adamc@62 1337 | L'.SgiStr (x, _, _) =>
adamc@62 1338 (if SS.member (strs, x) then
adamc@62 1339 sgnError env (DuplicateStr (loc, x))
adamc@62 1340 else
adamc@62 1341 ();
adamc@88 1342 (cons, vals, sgns, SS.add (strs, x)))
adamc@88 1343 | L'.SgiConstraint _ => (cons, vals, sgns, strs))
adamc@62 1344 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 1345 in
adamc@83 1346 ((L'.SgnConst sgis', loc), gs)
adamc@31 1347 end
adamc@31 1348 | L.SgnVar x =>
adamc@31 1349 (case E.lookupSgn env x of
adamc@31 1350 NONE =>
adamc@31 1351 (sgnError env (UnboundSgn (loc, x));
adamc@83 1352 ((L'.SgnError, loc), []))
adamc@83 1353 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 1354 | L.SgnFun (m, dom, ran) =>
adamc@41 1355 let
adamc@83 1356 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1357 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1358 val (ran', gs2) = elabSgn (env', denv) ran
adamc@41 1359 in
adamc@83 1360 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 1361 end
adamc@42 1362 | L.SgnWhere (sgn, x, c) =>
adamc@42 1363 let
adamc@83 1364 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 1365 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 1366 in
adamc@42 1367 case #1 (hnormSgn env sgn') of
adamc@83 1368 L'.SgnError => (sgnerror, [])
adamc@42 1369 | L'.SgnConst sgis =>
adamc@75 1370 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 1371 x' = x andalso
adamc@75 1372 (unifyKinds k ck
adamc@75 1373 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 1374 true)
adamc@42 1375 | _ => false) sgis then
adamc@83 1376 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 1377 else
adamc@42 1378 (sgnError env (UnWhereable (sgn', x));
adamc@83 1379 (sgnerror, []))
adamc@42 1380 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 1381 (sgnerror, []))
adamc@42 1382 end
adamc@59 1383 | L.SgnProj (m, ms, x) =>
adamc@59 1384 (case E.lookupStr env m of
adamc@59 1385 NONE => (strError env (UnboundStr (loc, m));
adamc@83 1386 (sgnerror, []))
adamc@59 1387 | SOME (n, sgn) =>
adamc@59 1388 let
adamc@59 1389 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 1390 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 1391 NONE => (strError env (UnboundStr (loc, m));
adamc@59 1392 (strerror, sgnerror))
adamc@59 1393 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 1394 ((L'.StrVar n, loc), sgn) ms
adamc@59 1395 in
adamc@59 1396 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 1397 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 1398 (sgnerror, []))
adamc@83 1399 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 1400 end)
adamc@59 1401
adamc@31 1402
adamc@66 1403 fun selfify env {str, strs, sgn} =
adamc@66 1404 case #1 (hnormSgn env sgn) of
adamc@66 1405 L'.SgnError => sgn
adamc@66 1406 | L'.SgnVar _ => sgn
adamc@66 1407
adamc@66 1408 | L'.SgnConst sgis =>
adamc@66 1409 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 1410 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@66 1411 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 1412 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 1413 | x => x) sgis), #2 sgn)
adamc@66 1414 | L'.SgnFun _ => sgn
adamc@66 1415 | L'.SgnWhere _ => sgn
adamc@66 1416 | L'.SgnProj (m, ms, x) =>
adamc@66 1417 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1418 (L'.StrVar m, #2 sgn) ms,
adamc@66 1419 sgn = #2 (E.lookupStrNamed env m),
adamc@66 1420 field = x} of
adamc@66 1421 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 1422 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 1423
adamc@66 1424 fun selfifyAt env {str, sgn} =
adamc@66 1425 let
adamc@66 1426 fun self (str, _) =
adamc@66 1427 case str of
adamc@66 1428 L'.StrVar x => SOME (x, [])
adamc@66 1429 | L'.StrProj (str, x) =>
adamc@66 1430 (case self str of
adamc@66 1431 NONE => NONE
adamc@66 1432 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 1433 | _ => NONE
adamc@66 1434 in
adamc@66 1435 case self str of
adamc@66 1436 NONE => sgn
adamc@66 1437 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 1438 end
adamc@66 1439
adamc@88 1440 fun dopen (env, denv) {str, strs, sgn} =
adamc@66 1441 let
adamc@66 1442 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1443 (L'.StrVar str, #2 sgn) strs
adamc@66 1444 in
adamc@66 1445 case #1 (hnormSgn env sgn) of
adamc@66 1446 L'.SgnConst sgis =>
adamc@88 1447 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
adamc@66 1448 case sgi of
adamc@66 1449 L'.SgiConAbs (x, n, k) =>
adamc@88 1450 let
adamc@88 1451 val c = (L'.CModProj (str, strs, x), loc)
adamc@88 1452 in
adamc@88 1453 ((L'.DCon (x, n, k, c), loc),
adamc@88 1454 (E.pushCNamedAs env' x n k (SOME c), denv'))
adamc@88 1455 end
adamc@66 1456 | L'.SgiCon (x, n, k, c) =>
adamc@66 1457 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
adamc@88 1458 (E.pushCNamedAs env' x n k (SOME c), denv'))
adamc@66 1459 | L'.SgiVal (x, n, t) =>
adamc@66 1460 ((L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc),
adamc@88 1461 (E.pushENamedAs env' x n t, denv'))
adamc@66 1462 | L'.SgiStr (x, n, sgn) =>
adamc@66 1463 ((L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc),
adamc@88 1464 (E.pushStrNamedAs env' x n sgn, denv'))
adamc@66 1465 | L'.SgiSgn (x, n, sgn) =>
adamc@66 1466 ((L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc),
adamc@88 1467 (E.pushSgnNamedAs env' x n sgn, denv'))
adamc@88 1468 | L'.SgiConstraint (c1, c2) =>
adamc@88 1469 ((L'.DConstraint (c1, c2), loc),
adamc@88 1470 (env', denv (* D.assert env denv (c1, c2) *) )))
adamc@88 1471 (env, denv) sgis
adamc@66 1472 | _ => (strError env (UnOpenable sgn);
adamc@88 1473 ([], (env, denv)))
adamc@66 1474 end
adamc@66 1475
adamc@88 1476 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@88 1477 case E.lookupStr env str of
adamc@88 1478 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1479 denv)
adamc@88 1480 | SOME (n, sgn) =>
adamc@88 1481 let
adamc@88 1482 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 1483 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 1484 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1485 (strerror, sgnerror))
adamc@88 1486 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 1487 ((L'.StrVar n, loc), sgn) strs
adamc@88 1488
adamc@88 1489 val cso = E.projectConstraints env {sgn = sgn, str = st}
adamc@88 1490
adamc@88 1491 val denv = case cso of
adamc@88 1492 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1493 denv)
adamc@90 1494 | SOME cs => foldl (fn ((c1, c2), denv) =>
adamc@90 1495 let
adamc@90 1496 val (denv, gs) = D.assert env denv (c1, c2)
adamc@90 1497 in
adamc@90 1498 case gs of
adamc@90 1499 [] => ()
adamc@90 1500 | _ => raise Fail "dopenConstraints: Sub-constraints remain";
adamc@90 1501
adamc@90 1502 denv
adamc@90 1503 end) denv cs
adamc@88 1504 in
adamc@88 1505 denv
adamc@88 1506 end
adamc@88 1507
adamc@31 1508 fun sgiOfDecl (d, loc) =
adamc@31 1509 case d of
adamc@123 1510 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@123 1511 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 1512 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 1513 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 1514 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 1515 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 1516 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 1517 | L'.DExport _ => []
adamc@88 1518
adamc@88 1519 fun sgiBindsD (env, denv) (sgi, _) =
adamc@88 1520 case sgi of
adamc@90 1521 L'.SgiConstraint (c1, c2) =>
adamc@90 1522 (case D.assert env denv (c1, c2) of
adamc@90 1523 (denv, []) => denv
adamc@90 1524 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
adamc@88 1525 | _ => denv
adamc@31 1526
adamc@86 1527 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
adamc@35 1528 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 1529 (L'.SgnError, _) => ()
adamc@31 1530 | (_, L'.SgnError) => ()
adamc@31 1531
adamc@31 1532 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1533 let
adamc@88 1534 fun folder (sgi2All as (sgi, _), (env, denv)) =
adamc@31 1535 let
adamc@31 1536 fun seek p =
adamc@31 1537 let
adamc@88 1538 fun seek (env, denv) ls =
adamc@31 1539 case ls of
adamc@31 1540 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@88 1541 (env, denv))
adamc@31 1542 | h :: t =>
adamc@31 1543 case p h of
adamc@88 1544 NONE => seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
adamc@88 1545 | SOME envs => envs
adamc@31 1546 in
adamc@88 1547 seek (env, denv) sgis1
adamc@31 1548 end
adamc@31 1549 in
adamc@31 1550 case sgi of
adamc@31 1551 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1552 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1553 let
adamc@41 1554 fun found (x', n1, k1, co1) =
adamc@41 1555 if x = x' then
adamc@41 1556 let
adamc@41 1557 val () = unifyKinds k1 k2
adamc@41 1558 handle KUnify (k1, k2, err) =>
adamc@41 1559 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 1560 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 1561 in
adamc@41 1562 SOME (if n1 = n2 then
adamc@41 1563 env
adamc@41 1564 else
adamc@88 1565 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
adamc@88 1566 denv)
adamc@41 1567 end
adamc@41 1568 else
adamc@41 1569 NONE
adamc@31 1570 in
adamc@31 1571 case sgi1 of
adamc@41 1572 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 1573 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@31 1574 | _ => NONE
adamc@31 1575 end)
adamc@31 1576
adamc@31 1577 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 1578 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1579 case sgi1 of
adamc@41 1580 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 1581 if x = x' then
adamc@41 1582 let
adamc@88 1583 fun good () = SOME (E.pushCNamedAs env x n2 k2 (SOME c2), denv)
adamc@41 1584 in
adamc@86 1585 (case unifyCons (env, denv) c1 c2 of
adamc@86 1586 [] => good ()
adamc@86 1587 | _ => NONE)
adamc@86 1588 handle CUnify (c1, c2, err) =>
adamc@86 1589 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@86 1590 good ())
adamc@41 1591 end
adamc@41 1592 else
adamc@41 1593 NONE
adamc@31 1594 | _ => NONE)
adamc@31 1595
adamc@32 1596 | L'.SgiVal (x, n2, c2) =>
adamc@32 1597 seek (fn sgi1All as (sgi1, _) =>
adamc@32 1598 case sgi1 of
adamc@41 1599 L'.SgiVal (x', n1, c1) =>
adamc@41 1600 if x = x' then
adamc@86 1601 (case unifyCons (env, denv) c1 c2 of
adamc@88 1602 [] => SOME (env, denv)
adamc@86 1603 | _ => NONE)
adamc@86 1604 handle CUnify (c1, c2, err) =>
adamc@86 1605 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@88 1606 SOME (env, denv))
adamc@41 1607 else
adamc@41 1608 NONE
adamc@32 1609 | _ => NONE)
adamc@32 1610
adamc@33 1611 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 1612 seek (fn sgi1All as (sgi1, _) =>
adamc@33 1613 case sgi1 of
adamc@41 1614 L'.SgiStr (x', n1, sgn1) =>
adamc@41 1615 if x = x' then
adamc@66 1616 let
adamc@86 1617 val () = subSgn (env, denv) sgn1 sgn2
adamc@66 1618 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 1619 val env = if n1 = n2 then
adamc@66 1620 env
adamc@66 1621 else
adamc@66 1622 E.pushStrNamedAs env x n2
adamc@66 1623 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 1624 sgn = sgn2})
adamc@66 1625 in
adamc@88 1626 SOME (env, denv)
adamc@66 1627 end
adamc@41 1628 else
adamc@41 1629 NONE
adamc@33 1630 | _ => NONE)
adamc@59 1631
adamc@59 1632 | L'.SgiSgn (x, n2, sgn2) =>
adamc@59 1633 seek (fn sgi1All as (sgi1, _) =>
adamc@59 1634 case sgi1 of
adamc@59 1635 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 1636 if x = x' then
adamc@65 1637 let
adamc@86 1638 val () = subSgn (env, denv) sgn1 sgn2
adamc@86 1639 val () = subSgn (env, denv) sgn2 sgn1
adamc@65 1640
adamc@65 1641 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 1642 val env = if n1 = n2 then
adamc@65 1643 env
adamc@65 1644 else
adamc@65 1645 E.pushSgnNamedAs env x n1 sgn2
adamc@65 1646 in
adamc@88 1647 SOME (env, denv)
adamc@65 1648 end
adamc@59 1649 else
adamc@59 1650 NONE
adamc@59 1651 | _ => NONE)
adamc@88 1652
adamc@88 1653 | L'.SgiConstraint (c2, d2) =>
adamc@88 1654 seek (fn sgi1All as (sgi1, _) =>
adamc@88 1655 case sgi1 of
adamc@88 1656 L'.SgiConstraint (c1, d1) =>
adamc@88 1657 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
adamc@90 1658 let
adamc@90 1659 val (denv, gs) = D.assert env denv (c2, d2)
adamc@90 1660 in
adamc@90 1661 case gs of
adamc@90 1662 [] => ()
adamc@90 1663 | _ => raise Fail "subSgn: Sub-constraints remain";
adamc@90 1664
adamc@90 1665 SOME (env, denv)
adamc@90 1666 end
adamc@88 1667 else
adamc@88 1668 NONE
adamc@88 1669 | _ => NONE)
adamc@31 1670 end
adamc@31 1671 in
adamc@88 1672 ignore (foldl folder (env, denv) sgis2)
adamc@31 1673 end
adamc@31 1674
adamc@41 1675 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 1676 let
adamc@43 1677 val ran1 =
adamc@43 1678 if n1 = n2 then
adamc@43 1679 ran1
adamc@43 1680 else
adamc@43 1681 subStrInSgn (n1, n2) ran1
adamc@43 1682 in
adamc@86 1683 subSgn (env, denv) dom2 dom1;
adamc@86 1684 subSgn (E.pushStrNamedAs env m2 n2 dom2, denv) ran1 ran2
adamc@43 1685 end
adamc@41 1686
adamc@41 1687 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 1688
adamc@61 1689
adamc@88 1690 fun elabDecl ((d, loc), (env, denv, gs)) =
adamc@76 1691 case d of
adamc@76 1692 L.DCon (x, ko, c) =>
adamc@76 1693 let
adamc@76 1694 val k' = case ko of
adamc@76 1695 NONE => kunif loc
adamc@76 1696 | SOME k => elabKind k
adamc@76 1697
adamc@83 1698 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1699 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1700 in
adamc@76 1701 checkKind env c' ck k';
adamc@76 1702
adamc@88 1703 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1704 end
adamc@76 1705 | L.DVal (x, co, e) =>
adamc@76 1706 let
adamc@123 1707 val (c', _, gs1) = case co of
adamc@83 1708 NONE => (cunif (loc, ktype), ktype, [])
adamc@83 1709 | SOME c => elabCon (env, denv) c
adamc@76 1710
adamc@83 1711 val (e', et, gs2) = elabExp (env, denv) e
adamc@76 1712 val (env', n) = E.pushENamed env x c'
adamc@86 1713
adamc@86 1714 val gs3 = checkCon (env, denv) e' et c'
adamc@76 1715 in
adamc@88 1716 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, gs1 @ gs2 @ gs3 @ gs))
adamc@76 1717 end
adamc@123 1718 | L.DValRec vis =>
adamc@123 1719 let
adamc@123 1720 val (vis, gs) = ListUtil.foldlMap
adamc@123 1721 (fn ((x, co, e), gs) =>
adamc@123 1722 let
adamc@123 1723 val (c', _, gs1) = case co of
adamc@123 1724 NONE => (cunif (loc, ktype), ktype, [])
adamc@123 1725 | SOME c => elabCon (env, denv) c
adamc@123 1726 in
adamc@123 1727 ((x, c', e), gs1 @ gs)
adamc@123 1728 end) [] vis
adamc@123 1729
adamc@123 1730 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@123 1731 let
adamc@123 1732 val (env, n) = E.pushENamed env x c'
adamc@123 1733 in
adamc@123 1734 ((x, n, c', e), env)
adamc@123 1735 end) env vis
adamc@123 1736
adamc@123 1737 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@123 1738 let
adamc@123 1739 val (e', et, gs1) = elabExp (env, denv) e
adamc@123 1740
adamc@123 1741 val gs2 = checkCon (env, denv) e' et c'
adamc@123 1742 in
adamc@123 1743 ((x, n, c', e'), gs1 @ gs2 @ gs)
adamc@123 1744 end) gs vis
adamc@123 1745 in
adamc@123 1746 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@123 1747 end
adamc@76 1748
adamc@76 1749 | L.DSgn (x, sgn) =>
adamc@76 1750 let
adamc@83 1751 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1752 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1753 in
adamc@88 1754 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1755 end
adamc@76 1756
adamc@76 1757 | L.DStr (x, sgno, str) =>
adamc@76 1758 let
adamc@93 1759 val () = if x = "Basis" then
adamc@93 1760 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@93 1761 else
adamc@93 1762 ()
adamc@93 1763
adamc@83 1764 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@76 1765
adamc@83 1766 val (str', sgn', gs') =
adamc@78 1767 case formal of
adamc@78 1768 NONE =>
adamc@78 1769 let
adamc@83 1770 val (str', actual, ds) = elabStr (env, denv) str
adamc@78 1771 in
adamc@83 1772 (str', selfifyAt env {str = str', sgn = actual}, ds)
adamc@78 1773 end
adamc@83 1774 | SOME (formal, gs1) =>
adamc@78 1775 let
adamc@78 1776 val str =
adamc@78 1777 case #1 (hnormSgn env formal) of
adamc@78 1778 L'.SgnConst sgis =>
adamc@78 1779 (case #1 str of
adamc@78 1780 L.StrConst ds =>
adamc@78 1781 let
adamc@78 1782 val needed = foldl (fn ((sgi, _), needed) =>
adamc@78 1783 case sgi of
adamc@78 1784 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
adamc@78 1785 | _ => needed)
adamc@78 1786 SS.empty sgis
adamc@78 1787
adamc@78 1788 val needed = foldl (fn ((d, _), needed) =>
adamc@78 1789 case d of
adamc@78 1790 L.DCon (x, _, _) => (SS.delete (needed, x)
adamc@78 1791 handle NotFound => needed)
adamc@78 1792 | L.DOpen _ => SS.empty
adamc@78 1793 | _ => needed)
adamc@78 1794 needed ds
adamc@78 1795 in
adamc@78 1796 case SS.listItems needed of
adamc@78 1797 [] => str
adamc@78 1798 | xs =>
adamc@78 1799 let
adamc@78 1800 val kwild = (L.KWild, #2 str)
adamc@78 1801 val cwild = (L.CWild kwild, #2 str)
adamc@78 1802 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
adamc@78 1803 in
adamc@78 1804 (L.StrConst (ds @ ds'), #2 str)
adamc@78 1805 end
adamc@78 1806 end
adamc@78 1807 | _ => str)
adamc@78 1808 | _ => str
adamc@78 1809
adamc@83 1810 val (str', actual, gs2) = elabStr (env, denv) str
adamc@78 1811 in
adamc@86 1812 subSgn (env, denv) actual formal;
adamc@83 1813 (str', formal, gs1 @ gs2)
adamc@78 1814 end
adamc@76 1815
adamc@76 1816 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1817 in
adamc@76 1818 case #1 (hnormSgn env sgn') of
adamc@76 1819 L'.SgnFun _ =>
adamc@76 1820 (case #1 str' of
adamc@76 1821 L'.StrFun _ => ()
adamc@76 1822 | _ => strError env (FunctorRebind loc))
adamc@76 1823 | _ => ();
adamc@76 1824
adamc@88 1825 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
adamc@76 1826 end
adamc@76 1827
adamc@76 1828 | L.DFfiStr (x, sgn) =>
adamc@76 1829 let
adamc@83 1830 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1831
adamc@76 1832 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1833 in
adamc@88 1834 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1835 end
adamc@76 1836
adamc@76 1837 | L.DOpen (m, ms) =>
adamc@88 1838 (case E.lookupStr env m of
adamc@88 1839 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1840 ([], (env, denv, [])))
adamc@88 1841 | SOME (n, sgn) =>
adamc@88 1842 let
adamc@88 1843 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 1844 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 1845 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1846 (strerror, sgnerror))
adamc@88 1847 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 1848 ((L'.StrVar n, loc), sgn) ms
adamc@83 1849
adamc@88 1850 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
adamc@88 1851 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
adamc@88 1852 in
adamc@88 1853 (ds, (env', denv', []))
adamc@88 1854 end)
adamc@88 1855
adamc@88 1856 | L.DConstraint (c1, c2) =>
adamc@88 1857 let
adamc@88 1858 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 1859 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@90 1860 val gs3 = D.prove env denv (c1', c2', loc)
adamc@88 1861
adamc@90 1862 val (denv', gs4) = D.assert env denv (c1', c2')
adamc@88 1863 in
adamc@88 1864 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 1865 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 1866
adamc@90 1867 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', gs1 @ gs2 @ gs3 @ gs4))
adamc@88 1868 end
adamc@88 1869
adamc@88 1870 | L.DOpenConstraints (m, ms) =>
adamc@88 1871 let
adamc@88 1872 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@88 1873 in
adamc@88 1874 ([], (env, denv, []))
adamc@88 1875 end
adamc@3 1876
adamc@109 1877 | L.DExport str =>
adamc@100 1878 let
adamc@109 1879 val (str', sgn, gs) = elabStr (env, denv) str
adamc@100 1880
adamc@109 1881 val sgn =
adamc@109 1882 case #1 (hnormSgn env sgn) of
adamc@109 1883 L'.SgnConst sgis =>
adamc@109 1884 let
adamc@146 1885 fun doOne (all as (sgi, _), env) =
adamc@146 1886 (case sgi of
adamc@146 1887 L'.SgiVal (x, n, t) =>
adamc@146 1888 (case hnormCon (env, denv) t of
adamc@146 1889 ((L'.TFun (dom, ran), _), []) =>
adamc@146 1890 (case (hnormCon (env, denv) dom, hnormCon (env, denv) ran) of
adamc@146 1891 (((L'.TRecord domR, _), []),
adamc@146 1892 ((L'.CApp (tf, arg3), _), [])) =>
adamc@146 1893 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
adamc@146 1894 (((L'.CApp (tf, arg2), _), []),
adamc@146 1895 (((L'.CRecord (_, []), _), []))) =>
adamc@146 1896 (case (hnormCon (env, denv) tf) of
adamc@146 1897 ((L'.CApp (tf, arg1), _), []) =>
adamc@146 1898 (case (hnormCon (env, denv) tf,
adamc@146 1899 hnormCon (env, denv) domR,
adamc@146 1900 hnormCon (env, denv) arg2) of
adamc@146 1901 ((tf, []), (domR, []),
adamc@146 1902 ((L'.CRecord (_, []), _), [])) =>
adamc@146 1903 let
adamc@146 1904 val t = (L'.CApp (tf, arg1), loc)
adamc@146 1905 val t = (L'.CApp (t, arg2), loc)
adamc@146 1906 val t = (L'.CApp (t, arg3), loc)
adamc@146 1907 in
adamc@146 1908 (L'.SgiVal (x, n, (L'.TFun ((L'.TRecord domR, loc),
adamc@146 1909 t),
adamc@146 1910 loc)), loc)
adamc@146 1911 end
adamc@146 1912 | _ => all)
adamc@146 1913 | _ => all)
adamc@146 1914 | _ => all)
adamc@146 1915 | _ => all)
adamc@146 1916 | _ => all)
adamc@146 1917 | _ => all,
adamc@146 1918 E.sgiBinds env all)
adamc@109 1919 in
adamc@146 1920 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@109 1921 end
adamc@109 1922 | _ => sgn
adamc@100 1923 in
adamc@109 1924 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs))
adamc@100 1925 end
adamc@100 1926
adamc@83 1927 and elabStr (env, denv) (str, loc) =
adamc@31 1928 case str of
adamc@31 1929 L.StrConst ds =>
adamc@31 1930 let
adamc@88 1931 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 1932 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 1933
adamc@63 1934 val (sgis, _, _, _, _) =
adamc@64 1935 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 1936 case sgi of
adamc@64 1937 L'.SgiConAbs (x, n, k) =>
adamc@64 1938 let
adamc@64 1939 val (cons, x) =
adamc@64 1940 if SS.member (cons, x) then
adamc@64 1941 (cons, "?" ^ x)
adamc@64 1942 else
adamc@64 1943 (SS.add (cons, x), x)
adamc@64 1944 in
adamc@64 1945 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1946 end
adamc@64 1947 | L'.SgiCon (x, n, k, c) =>
adamc@64 1948 let
adamc@64 1949 val (cons, x) =
adamc@64 1950 if SS.member (cons, x) then
adamc@64 1951 (cons, "?" ^ x)
adamc@64 1952 else
adamc@64 1953 (SS.add (cons, x), x)
adamc@64 1954 in
adamc@64 1955 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1956 end
adamc@64 1957 | L'.SgiVal (x, n, c) =>
adamc@64 1958 let
adamc@64 1959 val (vals, x) =
adamc@64 1960 if SS.member (vals, x) then
adamc@64 1961 (vals, "?" ^ x)
adamc@64 1962 else
adamc@64 1963 (SS.add (vals, x), x)
adamc@64 1964 in
adamc@64 1965 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1966 end
adamc@64 1967 | L'.SgiSgn (x, n, sgn) =>
adamc@64 1968 let
adamc@64 1969 val (sgns, x) =
adamc@64 1970 if SS.member (sgns, x) then
adamc@64 1971 (sgns, "?" ^ x)
adamc@64 1972 else
adamc@64 1973 (SS.add (sgns, x), x)
adamc@64 1974 in
adamc@64 1975 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1976 end
adamc@64 1977
adamc@64 1978 | L'.SgiStr (x, n, sgn) =>
adamc@64 1979 let
adamc@64 1980 val (strs, x) =
adamc@64 1981 if SS.member (strs, x) then
adamc@64 1982 (strs, "?" ^ x)
adamc@64 1983 else
adamc@64 1984 (SS.add (strs, x), x)
adamc@64 1985 in
adamc@64 1986 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 1987 end
adamc@88 1988 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs))
adamc@64 1989
adamc@63 1990 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 1991 in
adamc@83 1992 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 1993 end
adamc@31 1994 | L.StrVar x =>
adamc@31 1995 (case E.lookupStr env x of
adamc@31 1996 NONE =>
adamc@31 1997 (strError env (UnboundStr (loc, x));
adamc@83 1998 (strerror, sgnerror, []))
adamc@83 1999 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 2000 | L.StrProj (str, x) =>
adamc@34 2001 let
adamc@83 2002 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 2003 in
adamc@34 2004 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 2005 NONE => (strError env (UnboundStr (loc, x));
adamc@83 2006 (strerror, sgnerror, []))
adamc@83 2007 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 2008 end
adamc@41 2009 | L.StrFun (m, dom, ranO, str) =>
adamc@41 2010 let
adamc@83 2011 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2012 val (env', n) = E.pushStrNamed env m dom'
adamc@83 2013 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 2014
adamc@83 2015 val (formal, gs3) =
adamc@41 2016 case ranO of
adamc@83 2017 NONE => (actual, [])
adamc@41 2018 | SOME ran =>
adamc@41 2019 let
adamc@83 2020 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 2021 in
adamc@86 2022 subSgn (env', denv) actual ran';
adamc@83 2023 (ran', gs)
adamc@41 2024 end
adamc@41 2025 in
adamc@41 2026 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 2027 (L'.SgnFun (m, n, dom', formal), loc),
adamc@83 2028 gs1 @ gs2 @ gs3)
adamc@41 2029 end
adamc@44 2030 | L.StrApp (str1, str2) =>
adamc@44 2031 let
adamc@83 2032 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@83 2033 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 2034 in
adamc@44 2035 case #1 (hnormSgn env sgn1) of
adamc@83 2036 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2037 | L'.SgnFun (m, n, dom, ran) =>
adamc@86 2038 (subSgn (env, denv) sgn2 dom;
adamc@44 2039 case #1 (hnormSgn env ran) of
adamc@83 2040 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2041 | L'.SgnConst sgis =>
adamc@44 2042 ((L'.StrApp (str1', str2'), loc),
adamc@83 2043 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 2044 gs1 @ gs2)
adamc@44 2045 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 2046 | _ => (strError env (NotFunctor sgn1);
adamc@83 2047 (strerror, sgnerror, []))
adamc@44 2048 end
adamc@31 2049
adamc@56 2050 fun elabFile basis env file =
adamc@56 2051 let
adamc@83 2052 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 2053 val () = case gs of
adamc@83 2054 [] => ()
adamc@83 2055 | _ => raise Fail "Unresolved disjointness constraints in Basis"
adamc@83 2056
adamc@56 2057 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@56 2058
adamc@88 2059 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
adamc@56 2060
adamc@56 2061 fun discoverC r x =
adamc@56 2062 case E.lookupC env' x of
adamc@56 2063 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 2064 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 2065 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 2066
adamc@56 2067 val () = discoverC int "int"
adamc@56 2068 val () = discoverC float "float"
adamc@56 2069 val () = discoverC string "string"
adamc@56 2070
adamc@83 2071 fun elabDecl' (d, (env, gs)) =
adamc@76 2072 let
adamc@76 2073 val () = resetKunif ()
adamc@76 2074 val () = resetCunif ()
adamc@88 2075 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 2076 in
adamc@76 2077 if ErrorMsg.anyErrors () then
adamc@76 2078 ()
adamc@76 2079 else (
adamc@76 2080 if List.exists kunifsInDecl ds then
adamc@91 2081 declError env (KunifsRemain ds)
adamc@76 2082 else
adamc@76 2083 ();
adamc@76 2084
adamc@76 2085 case ListUtil.search cunifsInDecl ds of
adamc@76 2086 NONE => ()
adamc@76 2087 | SOME loc =>
adamc@91 2088 declError env (CunifsRemain ds)
adamc@76 2089 );
adamc@76 2090
adamc@83 2091 (ds, (env, gs))
adamc@76 2092 end
adamc@76 2093
adamc@83 2094 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 2095 in
adamc@84 2096 if ErrorMsg.anyErrors () then
adamc@84 2097 ()
adamc@84 2098 else
adamc@90 2099 app (fn (loc, env, denv, c1, c2) =>
adamc@84 2100 case D.prove env denv (c1, c2, loc) of
adamc@84 2101 [] => ()
adamc@84 2102 | _ =>
adamc@86 2103 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adamc@84 2104 eprefaces' [("Con 1", p_con env c1),
adamc@90 2105 ("Con 2", p_con env c2),
adamc@90 2106 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
adamc@90 2107 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))])) gs;
adamc@83 2108
adamc@56 2109 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
adamc@56 2110 end
adamc@2 2111
adamc@2 2112 end