annotate src/elaborate.sml @ 58:fd8a81ecd598

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