annotate src/elaborate.sml @ 67:9f89f0b00b84

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