annotate src/elaborate.sml @ 78:a6d45c6819c9

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