annotate src/elaborate.sml @ 62:d72b89a1b150

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