annotate src/elaborate.sml @ 81:60d97de1bbe8

Factor some operations into ElabOps
author Adam Chlipala <adamc@hcoop.net>
date Tue, 01 Jul 2008 09:29:49 -0400
parents 321cb9805c8e
children b4f2a258e52c
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@12 419
adamc@81 420 open ElabOps
adamc@43 421
adamc@12 422 type record_summary = {
adamc@12 423 fields : (L'.con * L'.con) list,
adamc@12 424 unifs : (L'.con * L'.con option ref) list,
adamc@12 425 others : L'.con list
adamc@12 426 }
adamc@12 427
adamc@12 428 fun summaryToCon {fields, unifs, others} =
adamc@12 429 let
adamc@12 430 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 431 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 432 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 433 in
adamc@12 434 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 435 end
adamc@12 436
adamc@12 437 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 438
adamc@12 439 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 440
adamc@12 441 fun hnormKind (kAll as (k, _)) =
adamc@12 442 case k of
adamc@76 443 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@12 444 | _ => kAll
adamc@12 445
adamc@12 446 fun kindof env (c, loc) =
adamc@12 447 case c of
adamc@12 448 L'.TFun _ => ktype
adamc@12 449 | L'.TCFun _ => ktype
adamc@12 450 | L'.TRecord _ => ktype
adamc@12 451
adamc@12 452 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 453 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@34 454 | L'.CModProj (n, ms, x) =>
adamc@34 455 let
adamc@34 456 val (_, sgn) = E.lookupStrNamed env n
adamc@34 457 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 458 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 459 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 460 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 461 ((L'.StrVar n, loc), sgn) ms
adamc@34 462 in
adamc@34 463 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@34 464 NONE => raise Fail "kindof: Unknown con in structure"
adamc@34 465 | SOME (k, _) => k
adamc@34 466 end
adamc@34 467
adamc@12 468 | L'.CApp (c, _) =>
adamc@12 469 (case #1 (hnormKind (kindof env c)) of
adamc@12 470 L'.KArrow (_, k) => k
adamc@12 471 | L'.KError => kerror
adamc@12 472 | _ => raise CUnify' (CKindof c))
adamc@12 473 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@12 474
adamc@12 475 | L'.CName _ => kname
adamc@12 476
adamc@12 477 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 478 | L'.CConcat (c, _) => kindof env c
adamc@67 479 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
adamc@12 480
adamc@12 481 | L'.CError => kerror
adamc@76 482 | L'.CUnif (_, k, _, _) => k
adamc@12 483
adamc@12 484 fun unifyRecordCons env (c1, c2) =
adamc@12 485 let
adamc@12 486 val k1 = kindof env c1
adamc@12 487 val k2 = kindof env c2
adamc@12 488 in
adamc@12 489 unifyKinds k1 k2;
adamc@12 490 unifySummaries env (k1, recordSummary env c1, recordSummary env c2)
adamc@12 491 end
adamc@12 492
adamc@12 493 and recordSummary env c : record_summary =
adamc@12 494 case hnormCon env c of
adamc@12 495 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
adamc@12 496 | (L'.CConcat (c1, c2), _) =>
adamc@12 497 let
adamc@12 498 val s1 = recordSummary env c1
adamc@12 499 val s2 = recordSummary env c2
adamc@12 500 in
adamc@12 501 {fields = #fields s1 @ #fields s2,
adamc@12 502 unifs = #unifs s1 @ #unifs s2,
adamc@12 503 others = #others s1 @ #others s2}
adamc@12 504 end
adamc@76 505 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary env c
adamc@76 506 | c' as (L'.CUnif (_, _, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
adamc@12 507 | c' => {fields = [], unifs = [], others = [c']}
adamc@12 508
adamc@12 509 and consEq env (c1, c2) =
adamc@12 510 (unifyCons env c1 c2;
adamc@12 511 true)
adamc@12 512 handle CUnify _ => false
adamc@12 513
adamc@80 514 and consNeq env (c1, c2) =
adamc@80 515 case (#1 (hnormCon env c1), #1 (hnormCon env c2)) of
adamc@80 516 (L'.CName x1, L'.CName x2) => x1 <> x2
adamc@80 517 | _ => false
adamc@80 518
adamc@12 519 and unifySummaries env (k, s1 : record_summary, s2 : record_summary) =
adamc@12 520 let
adamc@14 521 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@14 522 ("#2", p_summary env s2)]*)
adamc@12 523
adamc@12 524 fun eatMatching p (ls1, ls2) =
adamc@12 525 let
adamc@12 526 fun em (ls1, ls2, passed1) =
adamc@12 527 case ls1 of
adamc@12 528 [] => (rev passed1, ls2)
adamc@12 529 | h1 :: t1 =>
adamc@12 530 let
adamc@12 531 fun search (ls2', passed2) =
adamc@12 532 case ls2' of
adamc@12 533 [] => em (t1, ls2, h1 :: passed1)
adamc@12 534 | h2 :: t2 =>
adamc@12 535 if p (h1, h2) then
adamc@12 536 em (t1, List.revAppend (passed2, t2), passed1)
adamc@12 537 else
adamc@12 538 search (t2, h2 :: passed2)
adamc@12 539 in
adamc@12 540 search (ls2, [])
adamc@12 541 end
adamc@12 542 in
adamc@12 543 em (ls1, ls2, [])
adamc@12 544 end
adamc@12 545
adamc@12 546 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@80 547 not (consNeq env (x1, x2))
adamc@80 548 andalso consEq env (c1, c2)
adamc@80 549 andalso consEq env (x1, x2))
adamc@79 550 (#fields s1, #fields s2)
adamc@14 551 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@14 552 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@12 553 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@12 554 val (others1, others2) = eatMatching (consEq env) (#others s1, #others s2)
adamc@12 555
adamc@12 556 fun unifFields (fs, others, unifs) =
adamc@12 557 case (fs, others, unifs) of
adamc@12 558 ([], [], _) => ([], [], unifs)
adamc@12 559 | (_, _, []) => (fs, others, [])
adamc@12 560 | (_, _, (_, r) :: rest) =>
adamc@12 561 let
adamc@12 562 val r' = ref NONE
adamc@76 563 val cr' = (L'.CUnif (dummy, k, "recd", r'), dummy)
adamc@12 564
adamc@12 565 val prefix = case (fs, others) of
adamc@12 566 ([], other :: others) =>
adamc@12 567 List.foldl (fn (other, c) =>
adamc@12 568 (L'.CConcat (c, other), dummy))
adamc@12 569 other others
adamc@12 570 | (fs, []) =>
adamc@12 571 (L'.CRecord (k, fs), dummy)
adamc@12 572 | (fs, others) =>
adamc@12 573 List.foldl (fn (other, c) =>
adamc@12 574 (L'.CConcat (c, other), dummy))
adamc@12 575 (L'.CRecord (k, fs), dummy) others
adamc@12 576 in
adamc@12 577 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 578 ([], [], (cr', r') :: rest)
adamc@12 579 end
adamc@12 580
adamc@12 581 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 582 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 583
adamc@12 584 val clear1 = case (fs1, others1) of
adamc@12 585 ([], []) => true
adamc@12 586 | _ => false
adamc@12 587 val clear2 = case (fs2, others2) of
adamc@12 588 ([], []) => true
adamc@12 589 | _ => false
adamc@12 590 val empty = (L'.CRecord (k, []), dummy)
adamc@12 591 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 592 case (unifs1, unifs2) of
adamc@12 593 ([], _) =>
adamc@12 594 if clear1 then
adamc@12 595 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 596 else
adamc@12 597 raise CUnify' CRecordFailure
adamc@12 598 | (_, []) =>
adamc@12 599 if clear2 then
adamc@12 600 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 601 else
adamc@12 602 raise CUnify' CRecordFailure
adamc@12 603 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 604 (r2 := SOME c1;
adamc@12 605 pairOffUnifs (rest1, rest2))
adamc@12 606 in
adamc@12 607 pairOffUnifs (unifs1, unifs2)
adamc@12 608 end
adamc@12 609
adamc@11 610
adamc@12 611 and unifyCons' env c1 c2 =
adamc@11 612 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
adamc@11 613
adamc@11 614 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 615 let
adamc@10 616 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 617
adamc@12 618 fun isRecord () = unifyRecordCons env (c1All, c2All)
adamc@10 619 in
adamc@10 620 case (c1, c2) of
adamc@10 621 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@10 622 (unifyCons' env d1 d2;
adamc@10 623 unifyCons' env r1 r2)
adamc@10 624 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 625 if expl1 <> expl2 then
adamc@10 626 err CExplicitness
adamc@10 627 else
adamc@10 628 (unifyKinds d1 d2;
adamc@10 629 unifyCons' (E.pushCRel env x1 d1) r1 r2)
adamc@10 630 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
adamc@10 631
adamc@10 632 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 633 if n1 = n2 then
adamc@10 634 ()
adamc@10 635 else
adamc@10 636 err CIncompatible
adamc@10 637 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 638 if n1 = n2 then
adamc@10 639 ()
adamc@10 640 else
adamc@10 641 err CIncompatible
adamc@10 642
adamc@10 643 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@10 644 (unifyCons' env d1 d2;
adamc@10 645 unifyCons' env r1 r2)
adamc@10 646 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 647 (unifyKinds k1 k2;
adamc@10 648 unifyCons' (E.pushCRel env x1 k1) c1 c2)
adamc@10 649
adamc@10 650 | (L'.CName n1, L'.CName n2) =>
adamc@10 651 if n1 = n2 then
adamc@10 652 ()
adamc@10 653 else
adamc@10 654 err CIncompatible
adamc@10 655
adamc@34 656 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@34 657 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@34 658 ()
adamc@34 659 else
adamc@34 660 err CIncompatible
adamc@34 661
adamc@10 662 | (L'.CError, _) => ()
adamc@10 663 | (_, L'.CError) => ()
adamc@10 664
adamc@76 665 | (L'.CUnif (_, _, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
adamc@76 666 | (_, L'.CUnif (_, _, _, ref (SOME c2All))) => unifyCons' env c1All c2All
adamc@10 667
adamc@76 668 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
adamc@10 669 if r1 = r2 then
adamc@10 670 ()
adamc@10 671 else
adamc@10 672 (unifyKinds k1 k2;
adamc@10 673 r1 := SOME c2All)
adamc@10 674
adamc@76 675 | (L'.CUnif (_, _, _, r), _) =>
adamc@10 676 if occursCon r c2All then
adamc@10 677 err COccursCheckFailed
adamc@10 678 else
adamc@10 679 r := SOME c2All
adamc@76 680 | (_, L'.CUnif (_, _, _, r)) =>
adamc@10 681 if occursCon r c1All then
adamc@10 682 err COccursCheckFailed
adamc@10 683 else
adamc@10 684 r := SOME c1All
adamc@10 685
adamc@12 686 | (L'.CRecord _, _) => isRecord ()
adamc@12 687 | (_, L'.CRecord _) => isRecord ()
adamc@12 688 | (L'.CConcat _, _) => isRecord ()
adamc@12 689 | (_, L'.CConcat _) => isRecord ()
adamc@12 690
adamc@71 691 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
adamc@71 692 (unifyKinds dom1 dom2;
adamc@71 693 unifyKinds ran1 ran2)
adamc@71 694
adamc@10 695 | _ => err CIncompatible
adamc@10 696 end
adamc@10 697
adamc@12 698 and unifyCons env c1 c2 =
adamc@10 699 unifyCons' env c1 c2
adamc@10 700 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 701 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 702
adamc@10 703 datatype exp_error =
adamc@10 704 UnboundExp of ErrorMsg.span * string
adamc@34 705 | UnboundStrInExp of ErrorMsg.span * string
adamc@10 706 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 707 | Unif of string * L'.con
adamc@11 708 | WrongForm of string * L'.exp * L'.con
adamc@10 709
adamc@10 710 fun expError env err =
adamc@10 711 case err of
adamc@10 712 UnboundExp (loc, s) =>
adamc@10 713 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@34 714 | UnboundStrInExp (loc, s) =>
adamc@34 715 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@10 716 | Unify (e, c1, c2, uerr) =>
adamc@10 717 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 718 eprefaces' [("Expression", p_exp env e),
adamc@10 719 ("Have con", p_con env c1),
adamc@10 720 ("Need con", p_con env c2)];
adamc@10 721 cunifyError env uerr)
adamc@11 722 | Unif (action, c) =>
adamc@11 723 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 724 eprefaces' [("Con", p_con env c)])
adamc@11 725 | WrongForm (variety, e, t) =>
adamc@11 726 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 727 eprefaces' [("Expression", p_exp env e),
adamc@11 728 ("Type", p_con env t)])
adamc@10 729
adamc@10 730 fun checkCon env e c1 c2 =
adamc@10 731 unifyCons env c1 c2
adamc@10 732 handle CUnify (c1, c2, err) =>
adamc@10 733 expError env (Unify (e, c1, c2, err))
adamc@10 734
adamc@14 735 fun primType env p =
adamc@56 736 case p of
adamc@56 737 P.Int _ => !int
adamc@56 738 | P.Float _ => !float
adamc@56 739 | P.String _ => !string
adamc@14 740
adamc@71 741 fun recCons (k, nm, v, rest, loc) =
adamc@71 742 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
adamc@71 743 rest), loc)
adamc@71 744
adamc@71 745 fun foldType (dom, loc) =
adamc@71 746 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
adamc@71 747 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
adamc@71 748 (L'.TCFun (L'.Explicit, "v", dom,
adamc@71 749 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
adamc@71 750 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
adamc@71 751 (L'.CApp ((L'.CRel 3, loc),
adamc@71 752 recCons (dom,
adamc@71 753 (L'.CRel 2, loc),
adamc@71 754 (L'.CRel 1, loc),
adamc@71 755 (L'.CRel 0, loc),
adamc@71 756 loc)), loc)), loc)),
adamc@71 757 loc)), loc)), loc),
adamc@71 758 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
adamc@71 759 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
adamc@71 760 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
adamc@71 761 loc)), loc)), loc)
adamc@71 762
adamc@15 763 fun typeof env (e, loc) =
adamc@15 764 case e of
adamc@56 765 L'.EPrim p => primType env p
adamc@15 766 | L'.ERel n => #2 (E.lookupERel env n)
adamc@15 767 | L'.ENamed n => #2 (E.lookupENamed env n)
adamc@34 768 | L'.EModProj (n, ms, x) =>
adamc@34 769 let
adamc@34 770 val (_, sgn) = E.lookupStrNamed env n
adamc@34 771 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 772 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 773 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 774 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 775 ((L'.StrVar n, loc), sgn) ms
adamc@34 776 in
adamc@34 777 case E.projectVal env {sgn = sgn, str = str, field = x} of
adamc@34 778 NONE => raise Fail "typeof: Unknown val in structure"
adamc@34 779 | SOME t => t
adamc@34 780 end
adamc@34 781
adamc@15 782 | L'.EApp (e1, _) =>
adamc@15 783 (case #1 (typeof env e1) of
adamc@15 784 L'.TFun (_, c) => c
adamc@15 785 | _ => raise Fail "typeof: Bad EApp")
adamc@26 786 | L'.EAbs (_, _, ran, _) => ran
adamc@15 787 | L'.ECApp (e1, c) =>
adamc@15 788 (case #1 (typeof env e1) of
adamc@15 789 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
adamc@15 790 | _ => raise Fail "typeof: Bad ECApp")
adamc@15 791 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
adamc@15 792
adamc@29 793 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
adamc@15 794 | L'.EField (_, _, {field, ...}) => field
adamc@71 795 | L'.EFold dom => foldType (dom, loc)
adamc@15 796
adamc@15 797 | L'.EError => cerror
adamc@15 798
adamc@15 799 fun elabHead env (e as (_, loc)) t =
adamc@15 800 let
adamc@15 801 fun unravel (t, e) =
adamc@15 802 case hnormCon env t of
adamc@15 803 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@15 804 let
adamc@76 805 val u = cunif (loc, k)
adamc@15 806 in
adamc@15 807 unravel (subConInCon (0, u) t',
adamc@15 808 (L'.ECApp (e, u), loc))
adamc@15 809 end
adamc@15 810 | _ => (e, t)
adamc@15 811 in
adamc@15 812 unravel (t, e)
adamc@15 813 end
adamc@15 814
adamc@10 815 fun elabExp env (e, loc) =
adamc@10 816 case e of
adamc@10 817 L.EAnnot (e, t) =>
adamc@10 818 let
adamc@10 819 val (e', et) = elabExp env e
adamc@10 820 val (t', _) = elabCon env t
adamc@10 821 in
adamc@10 822 checkCon env e' et t';
adamc@10 823 (e', t')
adamc@10 824 end
adamc@10 825
adamc@56 826 | L.EPrim p => ((L'.EPrim p, loc), primType env p)
adamc@34 827 | L.EVar ([], s) =>
adamc@10 828 (case E.lookupE env s of
adamc@10 829 E.NotBound =>
adamc@10 830 (expError env (UnboundExp (loc, s));
adamc@10 831 (eerror, cerror))
adamc@10 832 | E.Rel (n, t) => ((L'.ERel n, loc), t)
adamc@10 833 | E.Named (n, t) => ((L'.ENamed n, loc), t))
adamc@34 834 | L.EVar (m1 :: ms, s) =>
adamc@34 835 (case E.lookupStr env m1 of
adamc@42 836 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@34 837 (eerror, cerror))
adamc@34 838 | SOME (n, sgn) =>
adamc@34 839 let
adamc@34 840 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 841 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 842 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@34 843 (strerror, sgnerror))
adamc@34 844 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 845 ((L'.StrVar n, loc), sgn) ms
adamc@34 846
adamc@34 847 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@34 848 NONE => (expError env (UnboundExp (loc, s));
adamc@34 849 cerror)
adamc@34 850 | SOME t => t
adamc@34 851 in
adamc@34 852 ((L'.EModProj (n, ms, s), loc), t)
adamc@34 853 end)
adamc@34 854
adamc@10 855 | L.EApp (e1, e2) =>
adamc@10 856 let
adamc@10 857 val (e1', t1) = elabExp env e1
adamc@15 858 val (e1', t1) = elabHead env e1' t1
adamc@10 859 val (e2', t2) = elabExp env e2
adamc@10 860
adamc@76 861 val dom = cunif (loc, ktype)
adamc@76 862 val ran = cunif (loc, ktype)
adamc@10 863 val t = (L'.TFun (dom, ran), dummy)
adamc@10 864 in
adamc@10 865 checkCon env e1' t1 t;
adamc@10 866 checkCon env e2' t2 dom;
adamc@10 867 ((L'.EApp (e1', e2'), loc), ran)
adamc@10 868 end
adamc@10 869 | L.EAbs (x, to, e) =>
adamc@10 870 let
adamc@10 871 val t' = case to of
adamc@76 872 NONE => cunif (loc, ktype)
adamc@10 873 | SOME t =>
adamc@10 874 let
adamc@10 875 val (t', tk) = elabCon env t
adamc@10 876 in
adamc@10 877 checkKind env t' tk ktype;
adamc@10 878 t'
adamc@10 879 end
adamc@10 880 val (e', et) = elabExp (E.pushERel env x t') e
adamc@10 881 in
adamc@26 882 ((L'.EAbs (x, t', et, e'), loc),
adamc@10 883 (L'.TFun (t', et), loc))
adamc@10 884 end
adamc@10 885 | L.ECApp (e, c) =>
adamc@10 886 let
adamc@10 887 val (e', et) = elabExp env e
adamc@15 888 val (e', et) = elabHead env e' et
adamc@10 889 val (c', ck) = elabCon env c
adamc@10 890 in
adamc@11 891 case #1 (hnormCon env et) of
adamc@11 892 L'.CError => (eerror, cerror)
adamc@11 893 | L'.TCFun (_, _, k, eb) =>
adamc@11 894 let
adamc@11 895 val () = checkKind env c' ck k
adamc@11 896 val eb' = subConInCon (0, c') eb
adamc@11 897 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@11 898 cerror)
adamc@11 899 in
adamc@11 900 ((L'.ECApp (e', c'), loc), eb')
adamc@11 901 end
adamc@11 902
adamc@11 903 | L'.CUnif _ =>
adamc@11 904 (expError env (Unif ("application", et));
adamc@11 905 (eerror, cerror))
adamc@11 906
adamc@11 907 | _ =>
adamc@11 908 (expError env (WrongForm ("constructor function", e', et));
adamc@11 909 (eerror, cerror))
adamc@10 910 end
adamc@11 911 | L.ECAbs (expl, x, k, e) =>
adamc@11 912 let
adamc@11 913 val expl' = elabExplicitness expl
adamc@11 914 val k' = elabKind k
adamc@11 915 val (e', et) = elabExp (E.pushCRel env x k') e
adamc@11 916 in
adamc@11 917 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@11 918 (L'.TCFun (expl', x, k', et), loc))
adamc@11 919 end
adamc@6 920
adamc@12 921 | L.ERecord xes =>
adamc@12 922 let
adamc@12 923 val xes' = map (fn (x, e) =>
adamc@12 924 let
adamc@12 925 val (x', xk) = elabCon env x
adamc@12 926 val (e', et) = elabExp env e
adamc@12 927 in
adamc@12 928 checkKind env x' xk kname;
adamc@12 929 (x', e', et)
adamc@12 930 end) xes
adamc@12 931 in
adamc@29 932 ((L'.ERecord xes', loc),
adamc@12 933 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc))
adamc@12 934 end
adamc@12 935
adamc@12 936 | L.EField (e, c) =>
adamc@12 937 let
adamc@12 938 val (e', et) = elabExp env e
adamc@12 939 val (c', ck) = elabCon env c
adamc@12 940
adamc@76 941 val ft = cunif (loc, ktype)
adamc@76 942 val rest = cunif (loc, ktype_record)
adamc@12 943 in
adamc@12 944 checkKind env c' ck kname;
adamc@12 945 checkCon env e' et (L'.TRecord (L'.CConcat ((L'.CRecord (ktype, [(c', ft)]), loc), rest), loc), loc);
adamc@12 946 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft)
adamc@12 947 end
adamc@71 948
adamc@71 949 | L.EFold =>
adamc@71 950 let
adamc@76 951 val dom = kunif loc
adamc@71 952 in
adamc@71 953 ((L'.EFold dom, loc), foldType (dom, loc))
adamc@71 954 end
adamc@12 955
adamc@12 956
adamc@6 957 datatype decl_error =
adamc@76 958 KunifsRemain of ErrorMsg.span
adamc@76 959 | CunifsRemain of ErrorMsg.span
adamc@6 960
adamc@6 961 fun declError env err =
adamc@6 962 case err of
adamc@76 963 KunifsRemain loc =>
adamc@76 964 ErrorMsg.errorAt loc "Some kind unification variables are undetermined in declaration"
adamc@76 965 | CunifsRemain loc =>
adamc@76 966 ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in declaration"
adamc@6 967
adamc@31 968 datatype sgn_error =
adamc@31 969 UnboundSgn of ErrorMsg.span * string
adamc@31 970 | UnmatchedSgi of L'.sgn_item
adamc@31 971 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 972 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@41 973 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 974 | UnWhereable of L'.sgn * string
adamc@75 975 | WhereWrongKind of L'.kind * L'.kind * kunify_error
adamc@58 976 | NotIncludable of L'.sgn
adamc@62 977 | DuplicateCon of ErrorMsg.span * string
adamc@62 978 | DuplicateVal of ErrorMsg.span * string
adamc@62 979 | DuplicateSgn of ErrorMsg.span * string
adamc@62 980 | DuplicateStr of ErrorMsg.span * string
adamc@31 981
adamc@31 982 fun sgnError env err =
adamc@31 983 case err of
adamc@31 984 UnboundSgn (loc, s) =>
adamc@31 985 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 986 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 987 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 988 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 989 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 990 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@59 991 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 992 ("Need", p_sgn_item env sgi2),
adamc@31 993 ("Kind 1", p_kind k1),
adamc@31 994 ("Kind 2", p_kind k2)];
adamc@31 995 kunifyError kerr)
adamc@31 996 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 997 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@59 998 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 999 ("Need", p_sgn_item env sgi2),
adamc@31 1000 ("Con 1", p_con env c1),
adamc@31 1001 ("Con 2", p_con env c2)];
adamc@31 1002 cunifyError env cerr)
adamc@41 1003 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1004 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1005 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1006 ("Sig 2", p_sgn env sgn2)])
adamc@42 1007 | UnWhereable (sgn, x) =>
adamc@42 1008 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1009 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1010 ("Field", PD.string x)])
adamc@75 1011 | WhereWrongKind (k1, k2, kerr) =>
adamc@75 1012 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
adamc@75 1013 eprefaces' [("Have", p_kind k1),
adamc@75 1014 ("Need", p_kind k2)];
adamc@75 1015 kunifyError kerr)
adamc@58 1016 | NotIncludable sgn =>
adamc@58 1017 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
adamc@58 1018 eprefaces' [("Signature", p_sgn env sgn)])
adamc@62 1019 | DuplicateCon (loc, s) =>
adamc@62 1020 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
adamc@62 1021 | DuplicateVal (loc, s) =>
adamc@62 1022 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
adamc@62 1023 | DuplicateSgn (loc, s) =>
adamc@62 1024 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
adamc@62 1025 | DuplicateStr (loc, s) =>
adamc@62 1026 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
adamc@31 1027
adamc@31 1028 datatype str_error =
adamc@31 1029 UnboundStr of ErrorMsg.span * string
adamc@44 1030 | NotFunctor of L'.sgn
adamc@46 1031 | FunctorRebind of ErrorMsg.span
adamc@61 1032 | UnOpenable of L'.sgn
adamc@75 1033 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
adamc@31 1034
adamc@31 1035 fun strError env err =
adamc@31 1036 case err of
adamc@31 1037 UnboundStr (loc, s) =>
adamc@31 1038 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1039 | NotFunctor sgn =>
adamc@44 1040 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1041 eprefaces' [("Signature", p_sgn env sgn)])
adamc@46 1042 | FunctorRebind loc =>
adamc@46 1043 ErrorMsg.errorAt loc "Attempt to rebind functor"
adamc@61 1044 | UnOpenable sgn =>
adamc@61 1045 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
adamc@61 1046 eprefaces' [("Signature", p_sgn env sgn)])
adamc@75 1047 | NotType (k, (k1, k2, ue)) =>
adamc@75 1048 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
adamc@75 1049 eprefaces' [("Kind", p_kind k),
adamc@75 1050 ("Subkind 1", p_kind k1),
adamc@75 1051 ("Subkind 2", p_kind k2)];
adamc@75 1052 kunifyError ue)
adamc@31 1053
adamc@42 1054 val hnormSgn = E.hnormSgn
adamc@31 1055
adamc@31 1056 fun elabSgn_item ((sgi, loc), env) =
adamc@76 1057 case sgi of
adamc@76 1058 L.SgiConAbs (x, k) =>
adamc@76 1059 let
adamc@76 1060 val k' = elabKind k
adamc@31 1061
adamc@76 1062 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1063 in
adamc@76 1064 ([(L'.SgiConAbs (x, n, k'), loc)], env')
adamc@76 1065 end
adamc@31 1066
adamc@76 1067 | L.SgiCon (x, ko, c) =>
adamc@76 1068 let
adamc@76 1069 val k' = case ko of
adamc@76 1070 NONE => kunif loc
adamc@76 1071 | SOME k => elabKind k
adamc@31 1072
adamc@76 1073 val (c', ck) = elabCon env c
adamc@76 1074 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1075 in
adamc@76 1076 checkKind env c' ck k';
adamc@31 1077
adamc@76 1078 ([(L'.SgiCon (x, n, k', c'), loc)], env')
adamc@76 1079 end
adamc@31 1080
adamc@76 1081 | L.SgiVal (x, c) =>
adamc@76 1082 let
adamc@76 1083 val (c', ck) = elabCon env c
adamc@31 1084
adamc@76 1085 val (env', n) = E.pushENamed env x c'
adamc@76 1086 in
adamc@76 1087 (unifyKinds ck ktype
adamc@76 1088 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 1089
adamc@76 1090 ([(L'.SgiVal (x, n, c'), loc)], env')
adamc@76 1091 end
adamc@31 1092
adamc@76 1093 | L.SgiStr (x, sgn) =>
adamc@76 1094 let
adamc@76 1095 val sgn' = elabSgn env sgn
adamc@76 1096 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1097 in
adamc@76 1098 ([(L'.SgiStr (x, n, sgn'), loc)], env')
adamc@76 1099 end
adamc@31 1100
adamc@76 1101 | L.SgiSgn (x, sgn) =>
adamc@76 1102 let
adamc@76 1103 val sgn' = elabSgn env sgn
adamc@76 1104 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1105 in
adamc@76 1106 ([(L'.SgiSgn (x, n, sgn'), loc)], env')
adamc@76 1107 end
adamc@31 1108
adamc@76 1109 | L.SgiInclude sgn =>
adamc@76 1110 let
adamc@76 1111 val sgn' = elabSgn env sgn
adamc@76 1112 in
adamc@76 1113 case #1 (hnormSgn env sgn') of
adamc@76 1114 L'.SgnConst sgis =>
adamc@76 1115 (sgis, foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis)
adamc@76 1116 | _ => (sgnError env (NotIncludable sgn');
adamc@76 1117 ([], env))
adamc@76 1118 end
adamc@31 1119
adamc@31 1120 and elabSgn env (sgn, loc) =
adamc@31 1121 case sgn of
adamc@31 1122 L.SgnConst sgis =>
adamc@31 1123 let
adamc@58 1124 val (sgis', _) = ListUtil.foldlMapConcat elabSgn_item env sgis
adamc@62 1125
adamc@62 1126 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 1127 case sgi of
adamc@62 1128 L'.SgiConAbs (x, _, _) =>
adamc@62 1129 (if SS.member (cons, x) then
adamc@62 1130 sgnError env (DuplicateCon (loc, x))
adamc@62 1131 else
adamc@62 1132 ();
adamc@62 1133 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1134 | L'.SgiCon (x, _, _, _) =>
adamc@62 1135 (if SS.member (cons, x) then
adamc@62 1136 sgnError env (DuplicateCon (loc, x))
adamc@62 1137 else
adamc@62 1138 ();
adamc@62 1139 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1140 | L'.SgiVal (x, _, _) =>
adamc@62 1141 (if SS.member (vals, x) then
adamc@62 1142 sgnError env (DuplicateVal (loc, x))
adamc@62 1143 else
adamc@62 1144 ();
adamc@62 1145 (cons, SS.add (vals, x), sgns, strs))
adamc@62 1146 | L'.SgiSgn (x, _, _) =>
adamc@62 1147 (if SS.member (sgns, x) then
adamc@62 1148 sgnError env (DuplicateSgn (loc, x))
adamc@62 1149 else
adamc@62 1150 ();
adamc@62 1151 (cons, vals, SS.add (sgns, x), strs))
adamc@62 1152 | L'.SgiStr (x, _, _) =>
adamc@62 1153 (if SS.member (strs, x) then
adamc@62 1154 sgnError env (DuplicateStr (loc, x))
adamc@62 1155 else
adamc@62 1156 ();
adamc@62 1157 (cons, vals, sgns, SS.add (strs, x))))
adamc@62 1158 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 1159 in
adamc@31 1160 (L'.SgnConst sgis', loc)
adamc@31 1161 end
adamc@31 1162 | L.SgnVar x =>
adamc@31 1163 (case E.lookupSgn env x of
adamc@31 1164 NONE =>
adamc@31 1165 (sgnError env (UnboundSgn (loc, x));
adamc@31 1166 (L'.SgnError, loc))
adamc@31 1167 | SOME (n, sgis) => (L'.SgnVar n, loc))
adamc@41 1168 | L.SgnFun (m, dom, ran) =>
adamc@41 1169 let
adamc@41 1170 val dom' = elabSgn env dom
adamc@41 1171 val (env', n) = E.pushStrNamed env m dom'
adamc@41 1172 val ran' = elabSgn env' ran
adamc@41 1173 in
adamc@41 1174 (L'.SgnFun (m, n, dom', ran'), loc)
adamc@41 1175 end
adamc@42 1176 | L.SgnWhere (sgn, x, c) =>
adamc@42 1177 let
adamc@42 1178 val sgn' = elabSgn env sgn
adamc@42 1179 val (c', ck) = elabCon env c
adamc@42 1180 in
adamc@42 1181 case #1 (hnormSgn env sgn') of
adamc@42 1182 L'.SgnError => sgnerror
adamc@42 1183 | L'.SgnConst sgis =>
adamc@75 1184 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 1185 x' = x andalso
adamc@75 1186 (unifyKinds k ck
adamc@75 1187 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 1188 true)
adamc@42 1189 | _ => false) sgis then
adamc@42 1190 (L'.SgnWhere (sgn', x, c'), loc)
adamc@42 1191 else
adamc@42 1192 (sgnError env (UnWhereable (sgn', x));
adamc@42 1193 sgnerror)
adamc@42 1194 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@42 1195 sgnerror)
adamc@42 1196 end
adamc@59 1197 | L.SgnProj (m, ms, x) =>
adamc@59 1198 (case E.lookupStr env m of
adamc@59 1199 NONE => (strError env (UnboundStr (loc, m));
adamc@59 1200 sgnerror)
adamc@59 1201 | SOME (n, sgn) =>
adamc@59 1202 let
adamc@59 1203 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 1204 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 1205 NONE => (strError env (UnboundStr (loc, m));
adamc@59 1206 (strerror, sgnerror))
adamc@59 1207 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 1208 ((L'.StrVar n, loc), sgn) ms
adamc@59 1209 in
adamc@59 1210 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 1211 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@59 1212 sgnerror)
adamc@59 1213 | SOME _ => (L'.SgnProj (n, ms, x), loc)
adamc@59 1214 end)
adamc@59 1215
adamc@31 1216
adamc@66 1217 fun selfify env {str, strs, sgn} =
adamc@66 1218 case #1 (hnormSgn env sgn) of
adamc@66 1219 L'.SgnError => sgn
adamc@66 1220 | L'.SgnVar _ => sgn
adamc@66 1221
adamc@66 1222 | L'.SgnConst sgis =>
adamc@66 1223 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 1224 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@66 1225 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 1226 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 1227 | x => x) sgis), #2 sgn)
adamc@66 1228 | L'.SgnFun _ => sgn
adamc@66 1229 | L'.SgnWhere _ => sgn
adamc@66 1230 | L'.SgnProj (m, ms, x) =>
adamc@66 1231 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1232 (L'.StrVar m, #2 sgn) ms,
adamc@66 1233 sgn = #2 (E.lookupStrNamed env m),
adamc@66 1234 field = x} of
adamc@66 1235 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 1236 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 1237
adamc@66 1238 fun selfifyAt env {str, sgn} =
adamc@66 1239 let
adamc@66 1240 fun self (str, _) =
adamc@66 1241 case str of
adamc@66 1242 L'.StrVar x => SOME (x, [])
adamc@66 1243 | L'.StrProj (str, x) =>
adamc@66 1244 (case self str of
adamc@66 1245 NONE => NONE
adamc@66 1246 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 1247 | _ => NONE
adamc@66 1248 in
adamc@66 1249 case self str of
adamc@66 1250 NONE => sgn
adamc@66 1251 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 1252 end
adamc@66 1253
adamc@66 1254 fun dopen env {str, strs, sgn} =
adamc@66 1255 let
adamc@66 1256 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1257 (L'.StrVar str, #2 sgn) strs
adamc@66 1258 in
adamc@66 1259 case #1 (hnormSgn env sgn) of
adamc@66 1260 L'.SgnConst sgis =>
adamc@66 1261 ListUtil.foldlMap (fn ((sgi, loc), env') =>
adamc@66 1262 case sgi of
adamc@66 1263 L'.SgiConAbs (x, n, k) =>
adamc@66 1264 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
adamc@66 1265 E.pushCNamedAs env' x n k NONE)
adamc@66 1266 | L'.SgiCon (x, n, k, c) =>
adamc@66 1267 ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc),
adamc@66 1268 E.pushCNamedAs env' x n k (SOME c))
adamc@66 1269 | L'.SgiVal (x, n, t) =>
adamc@66 1270 ((L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc),
adamc@66 1271 E.pushENamedAs env' x n t)
adamc@66 1272 | L'.SgiStr (x, n, sgn) =>
adamc@66 1273 ((L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc),
adamc@66 1274 E.pushStrNamedAs env' x n sgn)
adamc@66 1275 | L'.SgiSgn (x, n, sgn) =>
adamc@66 1276 ((L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc),
adamc@66 1277 E.pushSgnNamedAs env' x n sgn))
adamc@66 1278 env sgis
adamc@66 1279 | _ => (strError env (UnOpenable sgn);
adamc@66 1280 ([], env))
adamc@66 1281 end
adamc@66 1282
adamc@31 1283 fun sgiOfDecl (d, loc) =
adamc@31 1284 case d of
adamc@59 1285 L'.DCon (x, n, k, c) => (L'.SgiCon (x, n, k, c), loc)
adamc@59 1286 | L'.DVal (x, n, t, _) => (L'.SgiVal (x, n, t), loc)
adamc@59 1287 | L'.DSgn (x, n, sgn) => (L'.SgiSgn (x, n, sgn), loc)
adamc@59 1288 | L'.DStr (x, n, sgn, _) => (L'.SgiStr (x, n, sgn), loc)
adamc@59 1289 | L'.DFfiStr (x, n, sgn) => (L'.SgiStr (x, n, sgn), loc)
adamc@31 1290
adamc@35 1291 fun subSgn env sgn1 (sgn2 as (_, loc2)) =
adamc@35 1292 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 1293 (L'.SgnError, _) => ()
adamc@31 1294 | (_, L'.SgnError) => ()
adamc@31 1295
adamc@31 1296 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1297 let
adamc@31 1298 fun folder (sgi2All as (sgi, _), env) =
adamc@31 1299 let
adamc@31 1300 fun seek p =
adamc@31 1301 let
adamc@31 1302 fun seek env ls =
adamc@31 1303 case ls of
adamc@31 1304 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@31 1305 env)
adamc@31 1306 | h :: t =>
adamc@31 1307 case p h of
adamc@31 1308 NONE => seek (E.sgiBinds env h) t
adamc@31 1309 | SOME env => env
adamc@31 1310 in
adamc@31 1311 seek env sgis1
adamc@31 1312 end
adamc@31 1313 in
adamc@31 1314 case sgi of
adamc@31 1315 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1316 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1317 let
adamc@41 1318 fun found (x', n1, k1, co1) =
adamc@41 1319 if x = x' then
adamc@41 1320 let
adamc@41 1321 val () = unifyKinds k1 k2
adamc@41 1322 handle KUnify (k1, k2, err) =>
adamc@41 1323 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 1324 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 1325 in
adamc@41 1326 SOME (if n1 = n2 then
adamc@41 1327 env
adamc@41 1328 else
adamc@41 1329 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)))
adamc@41 1330 end
adamc@41 1331 else
adamc@41 1332 NONE
adamc@31 1333 in
adamc@31 1334 case sgi1 of
adamc@41 1335 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 1336 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@31 1337 | _ => NONE
adamc@31 1338 end)
adamc@31 1339
adamc@31 1340 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 1341 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1342 case sgi1 of
adamc@41 1343 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 1344 if x = x' then
adamc@41 1345 let
adamc@41 1346 val () = unifyCons env c1 c2
adamc@41 1347 handle CUnify (c1, c2, err) =>
adamc@41 1348 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
adamc@41 1349 in
adamc@41 1350 SOME (E.pushCNamedAs env x n2 k2 (SOME c2))
adamc@41 1351 end
adamc@41 1352 else
adamc@41 1353 NONE
adamc@31 1354 | _ => NONE)
adamc@31 1355
adamc@32 1356 | L'.SgiVal (x, n2, c2) =>
adamc@32 1357 seek (fn sgi1All as (sgi1, _) =>
adamc@32 1358 case sgi1 of
adamc@41 1359 L'.SgiVal (x', n1, c1) =>
adamc@41 1360 if x = x' then
adamc@41 1361 let
adamc@41 1362 val () = unifyCons env c1 c2
adamc@41 1363 handle CUnify (c1, c2, err) =>
adamc@41 1364 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
adamc@41 1365 in
adamc@41 1366 SOME env
adamc@41 1367 end
adamc@41 1368 else
adamc@41 1369 NONE
adamc@32 1370 | _ => NONE)
adamc@32 1371
adamc@33 1372 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 1373 seek (fn sgi1All as (sgi1, _) =>
adamc@33 1374 case sgi1 of
adamc@41 1375 L'.SgiStr (x', n1, sgn1) =>
adamc@41 1376 if x = x' then
adamc@66 1377 let
adamc@66 1378 val () = subSgn env sgn1 sgn2
adamc@66 1379 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 1380 val env = if n1 = n2 then
adamc@66 1381 env
adamc@66 1382 else
adamc@66 1383 E.pushStrNamedAs env x n2
adamc@66 1384 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 1385 sgn = sgn2})
adamc@66 1386 in
adamc@66 1387 SOME env
adamc@66 1388 end
adamc@41 1389 else
adamc@41 1390 NONE
adamc@33 1391 | _ => NONE)
adamc@59 1392
adamc@59 1393 | L'.SgiSgn (x, n2, sgn2) =>
adamc@59 1394 seek (fn sgi1All as (sgi1, _) =>
adamc@59 1395 case sgi1 of
adamc@59 1396 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 1397 if x = x' then
adamc@65 1398 let
adamc@65 1399 val () = subSgn env sgn1 sgn2
adamc@65 1400 val () = subSgn env sgn2 sgn1
adamc@65 1401
adamc@65 1402 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 1403 val env = if n1 = n2 then
adamc@65 1404 env
adamc@65 1405 else
adamc@65 1406 E.pushSgnNamedAs env x n1 sgn2
adamc@65 1407 in
adamc@65 1408 SOME env
adamc@65 1409 end
adamc@59 1410 else
adamc@59 1411 NONE
adamc@59 1412 | _ => NONE)
adamc@31 1413 end
adamc@31 1414 in
adamc@31 1415 ignore (foldl folder env sgis2)
adamc@31 1416 end
adamc@31 1417
adamc@41 1418 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 1419 let
adamc@43 1420 val ran1 =
adamc@43 1421 if n1 = n2 then
adamc@43 1422 ran1
adamc@43 1423 else
adamc@43 1424 subStrInSgn (n1, n2) ran1
adamc@43 1425 in
adamc@43 1426 subSgn env dom2 dom1;
adamc@43 1427 subSgn (E.pushStrNamedAs env m2 n2 dom2) ran1 ran2
adamc@43 1428 end
adamc@41 1429
adamc@41 1430 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 1431
adamc@61 1432
adamc@31 1433 fun elabDecl ((d, loc), env) =
adamc@76 1434 case d of
adamc@76 1435 L.DCon (x, ko, c) =>
adamc@76 1436 let
adamc@76 1437 val k' = case ko of
adamc@76 1438 NONE => kunif loc
adamc@76 1439 | SOME k => elabKind k
adamc@76 1440
adamc@76 1441 val (c', ck) = elabCon env c
adamc@76 1442 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1443 in
adamc@76 1444 checkKind env c' ck k';
adamc@76 1445
adamc@76 1446 ([(L'.DCon (x, n, k', c'), loc)], env')
adamc@76 1447 end
adamc@76 1448 | L.DVal (x, co, e) =>
adamc@76 1449 let
adamc@76 1450 val (c', ck) = case co of
adamc@76 1451 NONE => (cunif (loc, ktype), ktype)
adamc@76 1452 | SOME c => elabCon env c
adamc@76 1453
adamc@76 1454 val (e', et) = elabExp env e
adamc@76 1455 val (env', n) = E.pushENamed env x c'
adamc@76 1456 in
adamc@76 1457 checkCon env e' et c';
adamc@76 1458
adamc@76 1459 ([(L'.DVal (x, n, c', e'), loc)], env')
adamc@76 1460 end
adamc@76 1461
adamc@76 1462 | L.DSgn (x, sgn) =>
adamc@76 1463 let
adamc@76 1464 val sgn' = elabSgn env sgn
adamc@76 1465 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1466 in
adamc@76 1467 ([(L'.DSgn (x, n, sgn'), loc)], env')
adamc@76 1468 end
adamc@76 1469
adamc@76 1470 | L.DStr (x, sgno, str) =>
adamc@76 1471 let
adamc@76 1472 val formal = Option.map (elabSgn env) sgno
adamc@76 1473
adamc@78 1474 val (str', sgn') =
adamc@78 1475 case formal of
adamc@78 1476 NONE =>
adamc@78 1477 let
adamc@78 1478 val (str', actual) = elabStr env str
adamc@78 1479 in
adamc@78 1480 (str', selfifyAt env {str = str', sgn = actual})
adamc@78 1481 end
adamc@78 1482 | SOME formal =>
adamc@78 1483 let
adamc@78 1484 val str =
adamc@78 1485 case #1 (hnormSgn env formal) of
adamc@78 1486 L'.SgnConst sgis =>
adamc@78 1487 (case #1 str of
adamc@78 1488 L.StrConst ds =>
adamc@78 1489 let
adamc@78 1490 val needed = foldl (fn ((sgi, _), needed) =>
adamc@78 1491 case sgi of
adamc@78 1492 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
adamc@78 1493 | _ => needed)
adamc@78 1494 SS.empty sgis
adamc@78 1495
adamc@78 1496 val needed = foldl (fn ((d, _), needed) =>
adamc@78 1497 case d of
adamc@78 1498 L.DCon (x, _, _) => (SS.delete (needed, x)
adamc@78 1499 handle NotFound => needed)
adamc@78 1500 | L.DOpen _ => SS.empty
adamc@78 1501 | _ => needed)
adamc@78 1502 needed ds
adamc@78 1503 in
adamc@78 1504 case SS.listItems needed of
adamc@78 1505 [] => str
adamc@78 1506 | xs =>
adamc@78 1507 let
adamc@78 1508 val kwild = (L.KWild, #2 str)
adamc@78 1509 val cwild = (L.CWild kwild, #2 str)
adamc@78 1510 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
adamc@78 1511 in
adamc@78 1512 (L.StrConst (ds @ ds'), #2 str)
adamc@78 1513 end
adamc@78 1514 end
adamc@78 1515 | _ => str)
adamc@78 1516 | _ => str
adamc@78 1517
adamc@78 1518 val (str', actual) = elabStr env str
adamc@78 1519 in
adamc@78 1520 subSgn env actual formal;
adamc@78 1521 (str', formal)
adamc@78 1522 end
adamc@76 1523
adamc@76 1524 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1525 in
adamc@76 1526 case #1 (hnormSgn env sgn') of
adamc@76 1527 L'.SgnFun _ =>
adamc@76 1528 (case #1 str' of
adamc@76 1529 L'.StrFun _ => ()
adamc@76 1530 | _ => strError env (FunctorRebind loc))
adamc@76 1531 | _ => ();
adamc@76 1532
adamc@76 1533 ([(L'.DStr (x, n, sgn', str'), loc)], env')
adamc@76 1534 end
adamc@76 1535
adamc@76 1536 | L.DFfiStr (x, sgn) =>
adamc@76 1537 let
adamc@76 1538 val sgn' = elabSgn env sgn
adamc@76 1539
adamc@76 1540 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1541 in
adamc@76 1542 ([(L'.DFfiStr (x, n, sgn'), loc)], env')
adamc@76 1543 end
adamc@76 1544
adamc@76 1545 | L.DOpen (m, ms) =>
adamc@76 1546 case E.lookupStr env m of
adamc@76 1547 NONE => (strError env (UnboundStr (loc, m));
adamc@76 1548 ([], env))
adamc@76 1549 | SOME (n, sgn) =>
adamc@30 1550 let
adamc@76 1551 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@76 1552 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@76 1553 NONE => (strError env (UnboundStr (loc, m));
adamc@76 1554 (strerror, sgnerror))
adamc@76 1555 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@76 1556 ((L'.StrVar n, loc), sgn) ms
adamc@30 1557 in
adamc@76 1558 dopen env {str = n, strs = ms, sgn = sgn}
adamc@30 1559 end
adamc@3 1560
adamc@31 1561 and elabStr env (str, loc) =
adamc@31 1562 case str of
adamc@31 1563 L.StrConst ds =>
adamc@31 1564 let
adamc@61 1565 val (ds', env') = ListUtil.foldlMapConcat elabDecl env ds
adamc@59 1566 val sgis = map sgiOfDecl ds'
adamc@63 1567
adamc@63 1568 val (sgis, _, _, _, _) =
adamc@64 1569 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 1570 case sgi of
adamc@64 1571 L'.SgiConAbs (x, n, k) =>
adamc@64 1572 let
adamc@64 1573 val (cons, x) =
adamc@64 1574 if SS.member (cons, x) then
adamc@64 1575 (cons, "?" ^ x)
adamc@64 1576 else
adamc@64 1577 (SS.add (cons, x), x)
adamc@64 1578 in
adamc@64 1579 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1580 end
adamc@64 1581 | L'.SgiCon (x, n, k, c) =>
adamc@64 1582 let
adamc@64 1583 val (cons, x) =
adamc@64 1584 if SS.member (cons, x) then
adamc@64 1585 (cons, "?" ^ x)
adamc@64 1586 else
adamc@64 1587 (SS.add (cons, x), x)
adamc@64 1588 in
adamc@64 1589 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1590 end
adamc@64 1591 | L'.SgiVal (x, n, c) =>
adamc@64 1592 let
adamc@64 1593 val (vals, x) =
adamc@64 1594 if SS.member (vals, x) then
adamc@64 1595 (vals, "?" ^ x)
adamc@64 1596 else
adamc@64 1597 (SS.add (vals, x), x)
adamc@64 1598 in
adamc@64 1599 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1600 end
adamc@64 1601 | L'.SgiSgn (x, n, sgn) =>
adamc@64 1602 let
adamc@64 1603 val (sgns, x) =
adamc@64 1604 if SS.member (sgns, x) then
adamc@64 1605 (sgns, "?" ^ x)
adamc@64 1606 else
adamc@64 1607 (SS.add (sgns, x), x)
adamc@64 1608 in
adamc@64 1609 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1610 end
adamc@64 1611
adamc@64 1612 | L'.SgiStr (x, n, sgn) =>
adamc@64 1613 let
adamc@64 1614 val (strs, x) =
adamc@64 1615 if SS.member (strs, x) then
adamc@64 1616 (strs, "?" ^ x)
adamc@64 1617 else
adamc@64 1618 (SS.add (strs, x), x)
adamc@64 1619 in
adamc@64 1620 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 1621 end)
adamc@64 1622
adamc@63 1623 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 1624 in
adamc@31 1625 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc))
adamc@31 1626 end
adamc@31 1627 | L.StrVar x =>
adamc@31 1628 (case E.lookupStr env x of
adamc@31 1629 NONE =>
adamc@31 1630 (strError env (UnboundStr (loc, x));
adamc@31 1631 (strerror, sgnerror))
adamc@31 1632 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn))
adamc@34 1633 | L.StrProj (str, x) =>
adamc@34 1634 let
adamc@34 1635 val (str', sgn) = elabStr env str
adamc@34 1636 in
adamc@34 1637 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 1638 NONE => (strError env (UnboundStr (loc, x));
adamc@34 1639 (strerror, sgnerror))
adamc@34 1640 | SOME sgn => ((L'.StrProj (str', x), loc), sgn)
adamc@34 1641 end
adamc@41 1642 | L.StrFun (m, dom, ranO, str) =>
adamc@41 1643 let
adamc@41 1644 val dom' = elabSgn env dom
adamc@41 1645 val (env', n) = E.pushStrNamed env m dom'
adamc@41 1646 val (str', actual) = elabStr env' str
adamc@41 1647
adamc@41 1648 val formal =
adamc@41 1649 case ranO of
adamc@41 1650 NONE => actual
adamc@41 1651 | SOME ran =>
adamc@41 1652 let
adamc@42 1653 val ran' = elabSgn env' ran
adamc@41 1654 in
adamc@41 1655 subSgn env' actual ran';
adamc@41 1656 ran'
adamc@41 1657 end
adamc@41 1658 in
adamc@41 1659 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@41 1660 (L'.SgnFun (m, n, dom', formal), loc))
adamc@41 1661 end
adamc@44 1662 | L.StrApp (str1, str2) =>
adamc@44 1663 let
adamc@44 1664 val (str1', sgn1) = elabStr env str1
adamc@44 1665 val (str2', sgn2) = elabStr env str2
adamc@44 1666 in
adamc@44 1667 case #1 (hnormSgn env sgn1) of
adamc@44 1668 L'.SgnError => (strerror, sgnerror)
adamc@44 1669 | L'.SgnFun (m, n, dom, ran) =>
adamc@44 1670 (subSgn env sgn2 dom;
adamc@44 1671 case #1 (hnormSgn env ran) of
adamc@44 1672 L'.SgnError => (strerror, sgnerror)
adamc@44 1673 | L'.SgnConst sgis =>
adamc@44 1674 ((L'.StrApp (str1', str2'), loc),
adamc@44 1675 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc))
adamc@44 1676 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 1677 | _ => (strError env (NotFunctor sgn1);
adamc@44 1678 (strerror, sgnerror))
adamc@44 1679 end
adamc@31 1680
adamc@56 1681 fun elabFile basis env file =
adamc@56 1682 let
adamc@56 1683 val sgn = elabSgn env (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@56 1684 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@56 1685
adamc@61 1686 val (ds, env') = dopen env' {str = basis_n, strs = [], sgn = sgn}
adamc@56 1687
adamc@56 1688 fun discoverC r x =
adamc@56 1689 case E.lookupC env' x of
adamc@56 1690 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 1691 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 1692 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 1693
adamc@56 1694 val () = discoverC int "int"
adamc@56 1695 val () = discoverC float "float"
adamc@56 1696 val () = discoverC string "string"
adamc@56 1697
adamc@76 1698 fun elabDecl' (d, env) =
adamc@76 1699 let
adamc@76 1700 val () = resetKunif ()
adamc@76 1701 val () = resetCunif ()
adamc@76 1702 val (ds, env) = elabDecl (d, env)
adamc@76 1703 in
adamc@76 1704 if ErrorMsg.anyErrors () then
adamc@76 1705 ()
adamc@76 1706 else (
adamc@76 1707 if List.exists kunifsInDecl ds then
adamc@76 1708 declError env (KunifsRemain (#2 d))
adamc@76 1709 else
adamc@76 1710 ();
adamc@76 1711
adamc@76 1712 case ListUtil.search cunifsInDecl ds of
adamc@76 1713 NONE => ()
adamc@76 1714 | SOME loc =>
adamc@76 1715 declError env (CunifsRemain loc)
adamc@76 1716 );
adamc@76 1717
adamc@76 1718 (ds, env)
adamc@76 1719 end
adamc@76 1720
adamc@76 1721 val (file, _) = ListUtil.foldlMapConcat elabDecl' env' file
adamc@56 1722 in
adamc@56 1723 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
adamc@56 1724 end
adamc@2 1725
adamc@2 1726 end