annotate src/elaborate.sml @ 207:cc68da3801bc

Non-star SELECT
author Adam Chlipala <adamc@hcoop.net>
date Thu, 14 Aug 2008 18:35:08 -0400
parents cb8493759a7b
children 1487c712eb12
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@82 35 structure D = Disjoint
adamc@2 36
adamc@3 37 open Print
adamc@3 38 open ElabPrint
adamc@3 39
adamc@172 40 structure IM = IntBinaryMap
adamc@175 41
adamc@175 42 structure SK = struct
adamc@175 43 type ord_key = string
adamc@175 44 val compare = String.compare
adamc@175 45 end
adamc@175 46
adamc@175 47 structure SS = BinarySetFn(SK)
adamc@175 48 structure SM = BinaryMapFn(SK)
adamc@62 49
adamc@2 50 fun elabExplicitness e =
adamc@2 51 case e of
adamc@2 52 L.Explicit => L'.Explicit
adamc@2 53 | L.Implicit => L'.Implicit
adamc@2 54
adamc@2 55 fun occursKind r =
adamc@76 56 U.Kind.exists (fn L'.KUnif (_, _, r') => r = r'
adamc@2 57 | _ => false)
adamc@2 58
adamc@3 59 datatype kunify_error =
adamc@2 60 KOccursCheckFailed of L'.kind * L'.kind
adamc@2 61 | KIncompatible of L'.kind * L'.kind
adamc@2 62
adamc@3 63 exception KUnify' of kunify_error
adamc@3 64
adamc@3 65 fun kunifyError err =
adamc@2 66 case err of
adamc@2 67 KOccursCheckFailed (k1, k2) =>
adamc@3 68 eprefaces "Kind occurs check failed"
adamc@3 69 [("Kind 1", p_kind k1),
adamc@3 70 ("Kind 2", p_kind k2)]
adamc@2 71 | KIncompatible (k1, k2) =>
adamc@3 72 eprefaces "Incompatible kinds"
adamc@3 73 [("Kind 1", p_kind k1),
adamc@3 74 ("Kind 2", p_kind k2)]
adamc@2 75
adamc@3 76 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@2 77 let
adamc@3 78 fun err f = raise KUnify' (f (k1All, k2All))
adamc@2 79 in
adamc@2 80 case (k1, k2) of
adamc@2 81 (L'.KType, L'.KType) => ()
adamc@82 82 | (L'.KUnit, L'.KUnit) => ()
adamc@82 83
adamc@2 84 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@3 85 (unifyKinds' d1 d2;
adamc@3 86 unifyKinds' r1 r2)
adamc@2 87 | (L'.KName, L'.KName) => ()
adamc@3 88 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
adamc@207 89 | (L'.KTuple ks1, L'.KTuple ks2) =>
adamc@207 90 ((ListPair.appEq (fn (k1, k2) => unifyKinds' k1 k2) (ks1, ks2))
adamc@207 91 handle ListPair.UnequalLengths => err KIncompatible)
adamc@2 92
adamc@2 93 | (L'.KError, _) => ()
adamc@2 94 | (_, L'.KError) => ()
adamc@2 95
adamc@76 96 | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' k1All k2All
adamc@76 97 | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' k1All k2All
adamc@2 98
adamc@76 99 | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) =>
adamc@2 100 if r1 = r2 then
adamc@2 101 ()
adamc@2 102 else
adamc@2 103 r1 := SOME k2All
adamc@2 104
adamc@76 105 | (L'.KUnif (_, _, r), _) =>
adamc@2 106 if occursKind r k2All then
adamc@2 107 err KOccursCheckFailed
adamc@2 108 else
adamc@2 109 r := SOME k2All
adamc@76 110 | (_, L'.KUnif (_, _, r)) =>
adamc@2 111 if occursKind r k1All then
adamc@2 112 err KOccursCheckFailed
adamc@2 113 else
adamc@2 114 r := SOME k1All
adamc@2 115
adamc@2 116 | _ => err KIncompatible
adamc@2 117 end
adamc@2 118
adamc@3 119 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@3 120
adamc@3 121 fun unifyKinds k1 k2 =
adamc@3 122 unifyKinds' k1 k2
adamc@3 123 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@3 124
adamc@3 125 datatype con_error =
adamc@3 126 UnboundCon of ErrorMsg.span * string
adamc@157 127 | UnboundDatatype of ErrorMsg.span * string
adamc@34 128 | UnboundStrInCon of ErrorMsg.span * string
adamc@3 129 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
adamc@67 130 | DuplicateField of ErrorMsg.span * string
adamc@207 131 | ProjBounds of L'.con * int
adamc@207 132 | ProjMismatch of L'.con * L'.kind
adamc@3 133
adamc@5 134 fun conError env err =
adamc@3 135 case err of
adamc@3 136 UnboundCon (loc, s) =>
adamc@3 137 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
adamc@157 138 | UnboundDatatype (loc, s) =>
adamc@157 139 ErrorMsg.errorAt loc ("Unbound datatype " ^ s)
adamc@34 140 | UnboundStrInCon (loc, s) =>
adamc@34 141 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@3 142 | WrongKind (c, k1, k2, kerr) =>
adamc@3 143 (ErrorMsg.errorAt (#2 c) "Wrong kind";
adamc@5 144 eprefaces' [("Constructor", p_con env c),
adamc@5 145 ("Have kind", p_kind k1),
adamc@5 146 ("Need kind", p_kind k2)];
adamc@3 147 kunifyError kerr)
adamc@67 148 | DuplicateField (loc, s) =>
adamc@67 149 ErrorMsg.errorAt loc ("Duplicate record field " ^ s)
adamc@207 150 | ProjBounds (c, n) =>
adamc@207 151 (ErrorMsg.errorAt (#2 c) "Out of bounds constructor projection";
adamc@207 152 eprefaces' [("Constructor", p_con env c),
adamc@207 153 ("Index", Print.PD.string (Int.toString n))])
adamc@207 154 | ProjMismatch (c, k) =>
adamc@207 155 (ErrorMsg.errorAt (#2 c) "Projection from non-tuple constructor";
adamc@207 156 eprefaces' [("Constructor", p_con env c),
adamc@207 157 ("Kind", p_kind k)])
adamc@3 158
adamc@5 159 fun checkKind env c k1 k2 =
adamc@3 160 unifyKinds k1 k2
adamc@3 161 handle KUnify (k1, k2, err) =>
adamc@5 162 conError env (WrongKind (c, k1, k2, err))
adamc@3 163
adamc@3 164 val dummy = ErrorMsg.dummySpan
adamc@3 165
adamc@3 166 val ktype = (L'.KType, dummy)
adamc@3 167 val kname = (L'.KName, dummy)
adamc@12 168 val ktype_record = (L'.KRecord ktype, dummy)
adamc@3 169
adamc@3 170 val cerror = (L'.CError, dummy)
adamc@3 171 val kerror = (L'.KError, dummy)
adamc@10 172 val eerror = (L'.EError, dummy)
adamc@31 173 val sgnerror = (L'.SgnError, dummy)
adamc@31 174 val strerror = (L'.StrError, dummy)
adamc@3 175
adamc@56 176 val int = ref cerror
adamc@56 177 val float = ref cerror
adamc@56 178 val string = ref cerror
adamc@203 179 val table = ref cerror
adamc@56 180
adamc@3 181 local
adamc@3 182 val count = ref 0
adamc@3 183 in
adamc@3 184
adamc@3 185 fun resetKunif () = count := 0
adamc@3 186
adamc@76 187 fun kunif loc =
adamc@3 188 let
adamc@3 189 val n = !count
adamc@3 190 val s = if n <= 26 then
adamc@3 191 str (chr (ord #"A" + n))
adamc@3 192 else
adamc@3 193 "U" ^ Int.toString (n - 26)
adamc@3 194 in
adamc@3 195 count := n + 1;
adamc@76 196 (L'.KUnif (loc, s, ref NONE), dummy)
adamc@3 197 end
adamc@3 198
adamc@3 199 end
adamc@3 200
adamc@10 201 local
adamc@10 202 val count = ref 0
adamc@10 203 in
adamc@10 204
adamc@10 205 fun resetCunif () = count := 0
adamc@10 206
adamc@76 207 fun cunif (loc, k) =
adamc@10 208 let
adamc@10 209 val n = !count
adamc@10 210 val s = if n <= 26 then
adamc@10 211 str (chr (ord #"A" + n))
adamc@10 212 else
adamc@10 213 "U" ^ Int.toString (n - 26)
adamc@10 214 in
adamc@10 215 count := n + 1;
adamc@76 216 (L'.CUnif (loc, k, s, ref NONE), dummy)
adamc@10 217 end
adamc@10 218
adamc@10 219 end
adamc@10 220
adamc@18 221 fun elabKind (k, loc) =
adamc@18 222 case k of
adamc@18 223 L.KType => (L'.KType, loc)
adamc@18 224 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
adamc@18 225 | L.KName => (L'.KName, loc)
adamc@18 226 | L.KRecord k => (L'.KRecord (elabKind k), loc)
adamc@82 227 | L.KUnit => (L'.KUnit, loc)
adamc@207 228 | L.KTuple ks => (L'.KTuple (map elabKind ks), loc)
adamc@76 229 | L.KWild => kunif loc
adamc@18 230
adamc@67 231 fun foldKind (dom, ran, loc)=
adamc@67 232 (L'.KArrow ((L'.KArrow ((L'.KName, loc),
adamc@67 233 (L'.KArrow (dom,
adamc@67 234 (L'.KArrow (ran, ran), loc)), loc)), loc),
adamc@67 235 (L'.KArrow (ran,
adamc@67 236 (L'.KArrow ((L'.KRecord dom, loc),
adamc@67 237 ran), loc)), loc)), loc)
adamc@67 238
adamc@207 239 fun hnormKind (kAll as (k, _)) =
adamc@207 240 case k of
adamc@207 241 L'.KUnif (_, _, ref (SOME k)) => hnormKind k
adamc@207 242 | _ => kAll
adamc@207 243
adamc@83 244 fun elabCon (env, denv) (c, loc) =
adamc@3 245 case c of
adamc@3 246 L.CAnnot (c, k) =>
adamc@3 247 let
adamc@3 248 val k' = elabKind k
adamc@83 249 val (c', ck, gs) = elabCon (env, denv) c
adamc@3 250 in
adamc@5 251 checkKind env c' ck k';
adamc@83 252 (c', k', gs)
adamc@3 253 end
adamc@3 254
adamc@3 255 | L.TFun (t1, t2) =>
adamc@3 256 let
adamc@83 257 val (t1', k1, gs1) = elabCon (env, denv) t1
adamc@83 258 val (t2', k2, gs2) = elabCon (env, denv) t2
adamc@3 259 in
adamc@5 260 checkKind env t1' k1 ktype;
adamc@5 261 checkKind env t2' k2 ktype;
adamc@83 262 ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2)
adamc@3 263 end
adamc@3 264 | L.TCFun (e, x, k, t) =>
adamc@3 265 let
adamc@3 266 val e' = elabExplicitness e
adamc@3 267 val k' = elabKind k
adamc@3 268 val env' = E.pushCRel env x k'
adamc@83 269 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@3 270 in
adamc@5 271 checkKind env t' tk ktype;
adamc@83 272 ((L'.TCFun (e', x, k', t'), loc), ktype, gs)
adamc@3 273 end
adamc@85 274 | L.TDisjoint (c1, c2, c) =>
adamc@85 275 let
adamc@85 276 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@85 277 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@85 278
adamc@85 279 val ku1 = kunif loc
adamc@85 280 val ku2 = kunif loc
adamc@85 281
adamc@90 282 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@90 283 val (c', k, gs4) = elabCon (env, denv') c
adamc@85 284 in
adamc@85 285 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@85 286 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@85 287
adamc@90 288 ((L'.TDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4)
adamc@85 289 end
adamc@3 290 | L.TRecord c =>
adamc@3 291 let
adamc@83 292 val (c', ck, gs) = elabCon (env, denv) c
adamc@3 293 val k = (L'.KRecord ktype, loc)
adamc@3 294 in
adamc@5 295 checkKind env c' ck k;
adamc@83 296 ((L'.TRecord c', loc), ktype, gs)
adamc@3 297 end
adamc@3 298
adamc@34 299 | L.CVar ([], s) =>
adamc@3 300 (case E.lookupC env s of
adamc@9 301 E.NotBound =>
adamc@5 302 (conError env (UnboundCon (loc, s));
adamc@83 303 (cerror, kerror, []))
adamc@9 304 | E.Rel (n, k) =>
adamc@83 305 ((L'.CRel n, loc), k, [])
adamc@9 306 | E.Named (n, k) =>
adamc@83 307 ((L'.CNamed n, loc), k, []))
adamc@34 308 | L.CVar (m1 :: ms, s) =>
adamc@34 309 (case E.lookupStr env m1 of
adamc@42 310 NONE => (conError env (UnboundStrInCon (loc, m1));
adamc@83 311 (cerror, kerror, []))
adamc@34 312 | SOME (n, sgn) =>
adamc@34 313 let
adamc@34 314 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 315 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 316 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@34 317 (strerror, sgnerror))
adamc@34 318 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 319 ((L'.StrVar n, loc), sgn) ms
adamc@34 320
adamc@34 321 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
adamc@34 322 NONE => (conError env (UnboundCon (loc, s));
adamc@34 323 kerror)
adamc@34 324 | SOME (k, _) => k
adamc@34 325 in
adamc@83 326 ((L'.CModProj (n, ms, s), loc), k, [])
adamc@34 327 end)
adamc@34 328
adamc@3 329 | L.CApp (c1, c2) =>
adamc@3 330 let
adamc@83 331 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@83 332 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@76 333 val dom = kunif loc
adamc@76 334 val ran = kunif loc
adamc@3 335 in
adamc@5 336 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@5 337 checkKind env c2' k2 dom;
adamc@83 338 ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2)
adamc@3 339 end
adamc@67 340 | L.CAbs (x, ko, t) =>
adamc@3 341 let
adamc@67 342 val k' = case ko of
adamc@76 343 NONE => kunif loc
adamc@67 344 | SOME k => elabKind k
adamc@3 345 val env' = E.pushCRel env x k'
adamc@83 346 val (t', tk, gs) = elabCon (env', D.enter denv) t
adamc@3 347 in
adamc@8 348 ((L'.CAbs (x, k', t'), loc),
adamc@83 349 (L'.KArrow (k', tk), loc),
adamc@83 350 gs)
adamc@3 351 end
adamc@3 352
adamc@84 353 | L.CDisjoint (c1, c2, c) =>
adamc@84 354 let
adamc@84 355 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@84 356 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@84 357
adamc@84 358 val ku1 = kunif loc
adamc@84 359 val ku2 = kunif loc
adamc@84 360
adamc@90 361 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@90 362 val (c', k, gs4) = elabCon (env, denv') c
adamc@84 363 in
adamc@84 364 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@84 365 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@84 366
adamc@90 367 ((L'.CDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4)
adamc@84 368 end
adamc@84 369
adamc@3 370 | L.CName s =>
adamc@83 371 ((L'.CName s, loc), kname, [])
adamc@3 372
adamc@3 373 | L.CRecord xcs =>
adamc@3 374 let
adamc@76 375 val k = kunif loc
adamc@3 376
adamc@83 377 val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) =>
adamc@83 378 let
adamc@83 379 val (x', xk, gs1) = elabCon (env, denv) x
adamc@83 380 val (c', ck, gs2) = elabCon (env, denv) c
adamc@83 381 in
adamc@83 382 checkKind env x' xk kname;
adamc@83 383 checkKind env c' ck k;
adamc@83 384 ((x', c'), gs1 @ gs2 @ gs)
adamc@83 385 end) [] xcs
adamc@67 386
adamc@67 387 val rc = (L'.CRecord (k, xcs'), loc)
adamc@67 388 (* Add duplicate field checking later. *)
adamc@83 389
adamc@83 390 fun prove (xcs, ds) =
adamc@83 391 case xcs of
adamc@83 392 [] => ds
adamc@83 393 | xc :: rest =>
adamc@83 394 let
adamc@83 395 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@83 396 val ds = foldl (fn (xc', ds) =>
adamc@83 397 let
adamc@83 398 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@83 399 in
adamc@90 400 D.prove env denv (r1, r2, loc) @ ds
adamc@83 401 end)
adamc@83 402 ds rest
adamc@83 403 in
adamc@83 404 prove (rest, ds)
adamc@83 405 end
adamc@3 406 in
adamc@83 407 (rc, (L'.KRecord k, loc), prove (xcs', gs))
adamc@3 408 end
adamc@3 409 | L.CConcat (c1, c2) =>
adamc@3 410 let
adamc@83 411 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@83 412 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@76 413 val ku = kunif loc
adamc@3 414 val k = (L'.KRecord ku, loc)
adamc@3 415 in
adamc@5 416 checkKind env c1' k1 k;
adamc@5 417 checkKind env c2' k2 k;
adamc@83 418 ((L'.CConcat (c1', c2'), loc), k,
adamc@90 419 D.prove env denv (c1', c2', loc) @ gs1 @ gs2)
adamc@3 420 end
adamc@67 421 | L.CFold =>
adamc@67 422 let
adamc@76 423 val dom = kunif loc
adamc@76 424 val ran = kunif loc
adamc@67 425 in
adamc@67 426 ((L'.CFold (dom, ran), loc),
adamc@83 427 foldKind (dom, ran, loc),
adamc@83 428 [])
adamc@67 429 end
adamc@3 430
adamc@83 431 | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), [])
adamc@82 432
adamc@207 433 | L.CTuple cs =>
adamc@207 434 let
adamc@207 435 val (cs', ks, gs) = foldl (fn (c, (cs', ks, gs)) =>
adamc@207 436 let
adamc@207 437 val (c', k, gs') = elabCon (env, denv) c
adamc@207 438 in
adamc@207 439 (c' :: cs', k :: ks, gs' @ gs)
adamc@207 440 end) ([], [], []) cs
adamc@207 441 in
adamc@207 442 ((L'.CTuple (rev cs'), loc), (L'.KTuple (rev ks), loc), gs)
adamc@207 443 end
adamc@207 444 | L.CProj (c, n) =>
adamc@207 445 let
adamc@207 446 val (c', k, gs) = elabCon (env, denv) c
adamc@207 447 in
adamc@207 448 case hnormKind k of
adamc@207 449 (L'.KTuple ks, _) =>
adamc@207 450 if n <= 0 orelse n > length ks then
adamc@207 451 (conError env (ProjBounds (c', n));
adamc@207 452 (cerror, kerror, []))
adamc@207 453 else
adamc@207 454 ((L'.CProj (c', n), loc), List.nth (ks, n - 1), gs)
adamc@207 455 | k => (conError env (ProjMismatch (c', k));
adamc@207 456 (cerror, kerror, []))
adamc@207 457 end
adamc@207 458
adamc@18 459 | L.CWild k =>
adamc@18 460 let
adamc@18 461 val k' = elabKind k
adamc@18 462 in
adamc@83 463 (cunif (loc, k'), k', [])
adamc@18 464 end
adamc@18 465
adamc@6 466 fun kunifsRemain k =
adamc@6 467 case k of
adamc@76 468 L'.KUnif (_, _, ref NONE) => true
adamc@6 469 | _ => false
adamc@10 470 fun cunifsRemain c =
adamc@10 471 case c of
adamc@76 472 L'.CUnif (loc, _, _, ref NONE) => SOME loc
adamc@76 473 | _ => NONE
adamc@6 474
adamc@76 475 val kunifsInDecl = U.Decl.exists {kind = kunifsRemain,
adamc@76 476 con = fn _ => false,
adamc@76 477 exp = fn _ => false,
adamc@76 478 sgn_item = fn _ => false,
adamc@76 479 sgn = fn _ => false,
adamc@76 480 str = fn _ => false,
adamc@76 481 decl = fn _ => false}
adamc@10 482
adamc@76 483 val cunifsInDecl = U.Decl.search {kind = fn _ => NONE,
adamc@76 484 con = cunifsRemain,
adamc@76 485 exp = fn _ => NONE,
adamc@76 486 sgn_item = fn _ => NONE,
adamc@76 487 sgn = fn _ => NONE,
adamc@76 488 str = fn _ => NONE,
adamc@76 489 decl = fn _ => NONE}
adamc@10 490
adamc@10 491 fun occursCon r =
adamc@10 492 U.Con.exists {kind = fn _ => false,
adamc@76 493 con = fn L'.CUnif (_, _, _, r') => r = r'
adamc@10 494 | _ => false}
adamc@10 495
adamc@10 496 datatype cunify_error =
adamc@10 497 CKind of L'.kind * L'.kind * kunify_error
adamc@10 498 | COccursCheckFailed of L'.con * L'.con
adamc@10 499 | CIncompatible of L'.con * L'.con
adamc@10 500 | CExplicitness of L'.con * L'.con
adamc@141 501 | CKindof of L'.kind * L'.con
adamc@153 502 | CRecordFailure of PD.pp_desc * PD.pp_desc
adamc@10 503
adamc@10 504 exception CUnify' of cunify_error
adamc@10 505
adamc@10 506 fun cunifyError env err =
adamc@10 507 case err of
adamc@10 508 CKind (k1, k2, kerr) =>
adamc@10 509 (eprefaces "Kind unification failure"
adamc@10 510 [("Kind 1", p_kind k1),
adamc@10 511 ("Kind 2", p_kind k2)];
adamc@10 512 kunifyError kerr)
adamc@10 513 | COccursCheckFailed (c1, c2) =>
adamc@10 514 eprefaces "Constructor occurs check failed"
adamc@10 515 [("Con 1", p_con env c1),
adamc@10 516 ("Con 2", p_con env c2)]
adamc@10 517 | CIncompatible (c1, c2) =>
adamc@10 518 eprefaces "Incompatible constructors"
adamc@10 519 [("Con 1", p_con env c1),
adamc@10 520 ("Con 2", p_con env c2)]
adamc@10 521 | CExplicitness (c1, c2) =>
adamc@10 522 eprefaces "Differing constructor function explicitness"
adamc@10 523 [("Con 1", p_con env c1),
adamc@10 524 ("Con 2", p_con env c2)]
adamc@141 525 | CKindof (k, c) =>
adamc@147 526 eprefaces "Unexpected kind for kindof calculation"
adamc@141 527 [("Kind", p_kind k),
adamc@141 528 ("Con", p_con env c)]
adamc@153 529 | CRecordFailure (s1, s2) =>
adamc@153 530 eprefaces "Can't unify record constructors"
adamc@153 531 [("Summary 1", s1),
adamc@153 532 ("Summary 2", s2)]
adamc@10 533
adamc@13 534 exception SynUnif = E.SynUnif
adamc@12 535
adamc@81 536 open ElabOps
adamc@43 537
adamc@12 538 type record_summary = {
adamc@12 539 fields : (L'.con * L'.con) list,
adamc@12 540 unifs : (L'.con * L'.con option ref) list,
adamc@12 541 others : L'.con list
adamc@12 542 }
adamc@12 543
adamc@12 544 fun summaryToCon {fields, unifs, others} =
adamc@12 545 let
adamc@12 546 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 547 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 548 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 549 in
adamc@12 550 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 551 end
adamc@12 552
adamc@12 553 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 554
adamc@12 555 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 556
adamc@12 557 fun kindof env (c, loc) =
adamc@12 558 case c of
adamc@12 559 L'.TFun _ => ktype
adamc@12 560 | L'.TCFun _ => ktype
adamc@85 561 | L'.TDisjoint _ => ktype
adamc@12 562 | L'.TRecord _ => ktype
adamc@12 563
adamc@12 564 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 565 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@34 566 | L'.CModProj (n, ms, x) =>
adamc@34 567 let
adamc@34 568 val (_, sgn) = E.lookupStrNamed env n
adamc@34 569 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 570 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 571 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 572 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 573 ((L'.StrVar n, loc), sgn) ms
adamc@34 574 in
adamc@34 575 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@34 576 NONE => raise Fail "kindof: Unknown con in structure"
adamc@34 577 | SOME (k, _) => k
adamc@34 578 end
adamc@34 579
adamc@12 580 | L'.CApp (c, _) =>
adamc@141 581 (case hnormKind (kindof env c) of
adamc@141 582 (L'.KArrow (_, k), _) => k
adamc@141 583 | (L'.KError, _) => kerror
adamc@141 584 | k => raise CUnify' (CKindof (k, c)))
adamc@12 585 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@84 586 | L'.CDisjoint (_, _, c) => kindof env c
adamc@12 587
adamc@12 588 | L'.CName _ => kname
adamc@12 589
adamc@12 590 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 591 | L'.CConcat (c, _) => kindof env c
adamc@67 592 | L'.CFold (dom, ran) => foldKind (dom, ran, loc)
adamc@12 593
adamc@82 594 | L'.CUnit => (L'.KUnit, loc)
adamc@82 595
adamc@207 596 | L'.CTuple cs => (L'.KTuple (map (kindof env) cs), loc)
adamc@207 597 | L'.CProj (c, n) =>
adamc@207 598 (case hnormKind (kindof env c) of
adamc@207 599 (L'.KTuple ks, _) => List.nth (ks, n - 1)
adamc@207 600 | k => raise CUnify' (CKindof (k, c)))
adamc@207 601
adamc@12 602 | L'.CError => kerror
adamc@76 603 | L'.CUnif (_, k, _, _) => k
adamc@12 604
adamc@90 605 val hnormCon = D.hnormCon
adamc@86 606
adamc@205 607 datatype con_summary =
adamc@205 608 Nil
adamc@205 609 | Cons
adamc@205 610 | Unknown
adamc@205 611
adamc@205 612 fun compatible cs =
adamc@205 613 case cs of
adamc@205 614 (Unknown, _) => false
adamc@205 615 | (_, Unknown) => false
adamc@205 616 | (s1, s2) => s1 = s2
adamc@205 617
adamc@205 618 fun summarizeCon (env, denv) c =
adamc@205 619 let
adamc@205 620 val (c, gs) = hnormCon (env, denv) c
adamc@205 621 in
adamc@205 622 case #1 c of
adamc@205 623 L'.CRecord (_, []) => (Nil, gs)
adamc@205 624 | L'.CRecord (_, _ :: _) => (Cons, gs)
adamc@205 625 | L'.CConcat ((L'.CRecord (_, _ :: _), _), _) => (Cons, gs)
adamc@205 626 | L'.CDisjoint (_, _, c) =>
adamc@205 627 let
adamc@205 628 val (s, gs') = summarizeCon (env, denv) c
adamc@205 629 in
adamc@205 630 (s, gs @ gs')
adamc@205 631 end
adamc@205 632 | _ => (Unknown, gs)
adamc@205 633 end
adamc@205 634
adamc@205 635 fun p_con_summary s =
adamc@205 636 Print.PD.string (case s of
adamc@205 637 Nil => "Nil"
adamc@205 638 | Cons => "Cons"
adamc@205 639 | Unknown => "Unknown")
adamc@205 640
adamc@86 641 fun unifyRecordCons (env, denv) (c1, c2) =
adamc@12 642 let
adamc@92 643 fun rkindof c =
adamc@147 644 case hnormKind (kindof env c) of
adamc@92 645 (L'.KRecord k, _) => k
adamc@141 646 | (L'.KError, _) => kerror
adamc@141 647 | k => raise CUnify' (CKindof (k, c))
adamc@92 648
adamc@92 649 val k1 = rkindof c1
adamc@92 650 val k2 = rkindof c2
adamc@86 651
adamc@86 652 val (r1, gs1) = recordSummary (env, denv) c1
adamc@86 653 val (r2, gs2) = recordSummary (env, denv) c2
adamc@12 654 in
adamc@12 655 unifyKinds k1 k2;
adamc@86 656 unifySummaries (env, denv) (k1, r1, r2);
adamc@86 657 gs1 @ gs2
adamc@12 658 end
adamc@12 659
adamc@86 660 and recordSummary (env, denv) c =
adamc@86 661 let
adamc@86 662 val (c, gs) = hnormCon (env, denv) c
adamc@12 663
adamc@86 664 val (sum, gs') =
adamc@86 665 case c of
adamc@86 666 (L'.CRecord (_, xcs), _) => ({fields = xcs, unifs = [], others = []}, [])
adamc@86 667 | (L'.CConcat (c1, c2), _) =>
adamc@86 668 let
adamc@86 669 val (s1, gs1) = recordSummary (env, denv) c1
adamc@86 670 val (s2, gs2) = recordSummary (env, denv) c2
adamc@86 671 in
adamc@86 672 ({fields = #fields s1 @ #fields s2,
adamc@86 673 unifs = #unifs s1 @ #unifs s2,
adamc@86 674 others = #others s1 @ #others s2},
adamc@86 675 gs1 @ gs2)
adamc@86 676 end
adamc@86 677 | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary (env, denv) c
adamc@86 678 | c' as (L'.CUnif (_, _, _, r), _) => ({fields = [], unifs = [(c', r)], others = []}, [])
adamc@86 679 | c' => ({fields = [], unifs = [], others = [c']}, [])
adamc@86 680 in
adamc@86 681 (sum, gs @ gs')
adamc@86 682 end
adamc@86 683
adamc@86 684 and consEq (env, denv) (c1, c2) =
adamc@86 685 (case unifyCons (env, denv) c1 c2 of
adamc@86 686 [] => true
adamc@86 687 | _ => false)
adamc@12 688 handle CUnify _ => false
adamc@12 689
adamc@80 690 and consNeq env (c1, c2) =
adamc@86 691 case (#1 (ElabOps.hnormCon env c1), #1 (ElabOps.hnormCon env c2)) of
adamc@80 692 (L'.CName x1, L'.CName x2) => x1 <> x2
adamc@80 693 | _ => false
adamc@80 694
adamc@86 695 and unifySummaries (env, denv) (k, s1 : record_summary, s2 : record_summary) =
adamc@12 696 let
adamc@14 697 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@14 698 ("#2", p_summary env s2)]*)
adamc@12 699
adamc@12 700 fun eatMatching p (ls1, ls2) =
adamc@12 701 let
adamc@12 702 fun em (ls1, ls2, passed1) =
adamc@12 703 case ls1 of
adamc@12 704 [] => (rev passed1, ls2)
adamc@12 705 | h1 :: t1 =>
adamc@12 706 let
adamc@12 707 fun search (ls2', passed2) =
adamc@12 708 case ls2' of
adamc@12 709 [] => em (t1, ls2, h1 :: passed1)
adamc@12 710 | h2 :: t2 =>
adamc@12 711 if p (h1, h2) then
adamc@12 712 em (t1, List.revAppend (passed2, t2), passed1)
adamc@12 713 else
adamc@12 714 search (t2, h2 :: passed2)
adamc@12 715 in
adamc@12 716 search (ls2, [])
adamc@12 717 end
adamc@12 718 in
adamc@12 719 em (ls1, ls2, [])
adamc@12 720 end
adamc@12 721
adamc@12 722 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@80 723 not (consNeq env (x1, x2))
adamc@86 724 andalso consEq (env, denv) (c1, c2)
adamc@86 725 andalso consEq (env, denv) (x1, x2))
adamc@79 726 (#fields s1, #fields s2)
adamc@14 727 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@14 728 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@12 729 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@86 730 val (others1, others2) = eatMatching (consEq (env, denv)) (#others s1, #others s2)
adamc@12 731
adamc@12 732 fun unifFields (fs, others, unifs) =
adamc@12 733 case (fs, others, unifs) of
adamc@12 734 ([], [], _) => ([], [], unifs)
adamc@12 735 | (_, _, []) => (fs, others, [])
adamc@12 736 | (_, _, (_, r) :: rest) =>
adamc@12 737 let
adamc@12 738 val r' = ref NONE
adamc@141 739 val kr = (L'.KRecord k, dummy)
adamc@141 740 val cr' = (L'.CUnif (dummy, kr, "recd", r'), dummy)
adamc@12 741
adamc@12 742 val prefix = case (fs, others) of
adamc@12 743 ([], other :: others) =>
adamc@12 744 List.foldl (fn (other, c) =>
adamc@12 745 (L'.CConcat (c, other), dummy))
adamc@12 746 other others
adamc@12 747 | (fs, []) =>
adamc@12 748 (L'.CRecord (k, fs), dummy)
adamc@12 749 | (fs, others) =>
adamc@12 750 List.foldl (fn (other, c) =>
adamc@12 751 (L'.CConcat (c, other), dummy))
adamc@12 752 (L'.CRecord (k, fs), dummy) others
adamc@12 753 in
adamc@12 754 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 755 ([], [], (cr', r') :: rest)
adamc@12 756 end
adamc@12 757
adamc@12 758 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 759 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 760
adamc@86 761 val clear = case (fs1, others1, fs2, others2) of
adamc@86 762 ([], [], [], []) => true
adamc@12 763 | _ => false
adamc@12 764 val empty = (L'.CRecord (k, []), dummy)
adamc@12 765 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 766 case (unifs1, unifs2) of
adamc@12 767 ([], _) =>
adamc@86 768 if clear then
adamc@12 769 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 770 else
adamc@153 771 raise CUnify' (CRecordFailure (p_summary env s1, p_summary env s2))
adamc@12 772 | (_, []) =>
adamc@86 773 if clear then
adamc@12 774 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 775 else
adamc@153 776 raise CUnify' (CRecordFailure (p_summary env s1, p_summary env s2))
adamc@12 777 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 778 (r2 := SOME c1;
adamc@12 779 pairOffUnifs (rest1, rest2))
adamc@12 780 in
adamc@12 781 pairOffUnifs (unifs1, unifs2)
adamc@12 782 end
adamc@12 783
adamc@86 784 and unifyCons' (env, denv) c1 c2 =
adamc@86 785 let
adamc@86 786 val (c1, gs1) = hnormCon (env, denv) c1
adamc@86 787 val (c2, gs2) = hnormCon (env, denv) c2
adamc@86 788 in
adamc@205 789 let
adamc@205 790 val gs3 = unifyCons'' (env, denv) c1 c2
adamc@205 791 in
adamc@205 792 gs1 @ gs2 @ gs3
adamc@205 793 end
adamc@205 794 handle ex =>
adamc@205 795 let
adamc@205 796 val loc = #2 c1
adamc@205 797
adamc@205 798 fun unfold (dom, f, i, r, c) =
adamc@205 799 let
adamc@205 800 val nm = cunif (loc, (L'.KName, loc))
adamc@205 801 val v = cunif (loc, dom)
adamc@205 802 val rest = cunif (loc, (L'.KRecord dom, loc))
adamc@205 803
adamc@205 804 val (iS, gs3) = summarizeCon (env, denv) i
adamc@205 805
adamc@205 806 val app = (L'.CApp (f, nm), loc)
adamc@205 807 val app = (L'.CApp (app, v), loc)
adamc@205 808 val app = (L'.CApp (app, rest), loc)
adamc@205 809 val (appS, gs4) = summarizeCon (env, denv) app
adamc@205 810
adamc@205 811 val (cS, gs5) = summarizeCon (env, denv) c
adamc@205 812 in
adamc@205 813 (*prefaces "Summaries" [("iS", p_con_summary iS),
adamc@205 814 ("appS", p_con_summary appS),
adamc@205 815 ("cS", p_con_summary cS)];*)
adamc@205 816
adamc@205 817 if compatible (iS, appS) then
adamc@205 818 raise ex
adamc@205 819 else if compatible (cS, iS) then
adamc@205 820 let
adamc@205 821 (*val () = prefaces "Same?" [("i", p_con env i),
adamc@205 822 ("c", p_con env c)]*)
adamc@205 823 val gs6 = unifyCons (env, denv) i c
adamc@205 824 (*val () = TextIO.print "Yes!\n"*)
adamc@205 825
adamc@205 826 val gs7 = unifyCons (env, denv) r (L'.CRecord (dom, []), loc)
adamc@205 827 in
adamc@205 828 gs1 @ gs2 @ gs3 @ gs4 @ gs5 @ gs6 @ gs7
adamc@205 829 end
adamc@205 830 else if compatible (cS, appS) then
adamc@205 831 let
adamc@205 832 (*val () = prefaces "Same?" [("app", p_con env app),
adamc@205 833 ("c", p_con env c),
adamc@205 834 ("app'", p_con env (#1 (hnormCon (env, denv) app)))]*)
adamc@205 835 val gs6 = unifyCons (env, denv) app c
adamc@205 836 (*val () = TextIO.print "Yes!\n"*)
adamc@205 837
adamc@205 838 val singleton = (L'.CRecord (dom, [(nm, v)]), loc)
adamc@205 839 val concat = (L'.CConcat (singleton, rest), loc)
adamc@205 840 val gs7 = unifyCons (env, denv) r concat
adamc@205 841 in
adamc@206 842 gs1 @ gs2 @ gs3 @ gs4 @ gs5 @ gs6 @ gs7
adamc@205 843 end
adamc@205 844 else
adamc@205 845 raise ex
adamc@205 846 end
adamc@205 847 handle _ => raise ex
adamc@205 848 in
adamc@205 849 case (#1 c1, #1 c2) of
adamc@205 850 (L'.CApp ((L'.CApp ((L'.CApp ((L'.CFold (dom, _), _), f), _), i), _), r), _) =>
adamc@205 851 unfold (dom, f, i, r, c2)
adamc@205 852 | (_, L'.CApp ((L'.CApp ((L'.CApp ((L'.CFold (dom, _), _), f), _), i), _), r)) =>
adamc@205 853 unfold (dom, f, i, r, c1)
adamc@205 854 | _ => raise ex
adamc@205 855 end
adamc@86 856 end
adamc@11 857
adamc@205 858 and unifyCons'' (env, denv) (c1All as (c1, loc)) (c2All as (c2, _)) =
adamc@10 859 let
adamc@10 860 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 861
adamc@86 862 fun isRecord () = unifyRecordCons (env, denv) (c1All, c2All)
adamc@10 863 in
adamc@92 864 (*eprefaces "unifyCons''" [("c1All", p_con env c1All),
adamc@92 865 ("c2All", p_con env c2All)];*)
adamc@92 866
adamc@10 867 case (c1, c2) of
adamc@88 868 (L'.CUnit, L'.CUnit) => []
adamc@88 869
adamc@88 870 | (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@86 871 unifyCons' (env, denv) d1 d2
adamc@86 872 @ unifyCons' (env, denv) r1 r2
adamc@10 873 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 874 if expl1 <> expl2 then
adamc@10 875 err CExplicitness
adamc@10 876 else
adamc@10 877 (unifyKinds d1 d2;
adamc@86 878 unifyCons' (E.pushCRel env x1 d1, D.enter denv) r1 r2)
adamc@86 879 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' (env, denv) r1 r2
adamc@10 880
adamc@10 881 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 882 if n1 = n2 then
adamc@86 883 []
adamc@10 884 else
adamc@10 885 err CIncompatible
adamc@10 886 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 887 if n1 = n2 then
adamc@86 888 []
adamc@10 889 else
adamc@10 890 err CIncompatible
adamc@10 891
adamc@10 892 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@86 893 (unifyCons' (env, denv) d1 d2;
adamc@86 894 unifyCons' (env, denv) r1 r2)
adamc@10 895 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 896 (unifyKinds k1 k2;
adamc@86 897 unifyCons' (E.pushCRel env x1 k1, D.enter denv) c1 c2)
adamc@10 898
adamc@10 899 | (L'.CName n1, L'.CName n2) =>
adamc@10 900 if n1 = n2 then
adamc@86 901 []
adamc@10 902 else
adamc@10 903 err CIncompatible
adamc@10 904
adamc@34 905 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@34 906 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@86 907 []
adamc@34 908 else
adamc@34 909 err CIncompatible
adamc@34 910
adamc@207 911 | (L'.CTuple cs1, L'.CTuple cs2) =>
adamc@207 912 ((ListPair.foldlEq (fn (c1, c2, gs) =>
adamc@207 913 let
adamc@207 914 val gs' = unifyCons' (env, denv) c1 c2
adamc@207 915 in
adamc@207 916 gs' @ gs
adamc@207 917 end) [] (cs1, cs2))
adamc@207 918 handle ListPair.UnequalLengths => err CIncompatible)
adamc@207 919 | (L'.CProj (c1, n1), L'.CProj (c2, n2)) =>
adamc@207 920 if n1 = n2 then
adamc@207 921 unifyCons' (env, denv) c1 c2
adamc@207 922 else
adamc@207 923 err CIncompatible
adamc@207 924
adamc@86 925 | (L'.CError, _) => []
adamc@86 926 | (_, L'.CError) => []
adamc@10 927
adamc@92 928 | (L'.CRecord _, _) => isRecord ()
adamc@92 929 | (_, L'.CRecord _) => isRecord ()
adamc@92 930 | (L'.CConcat _, _) => isRecord ()
adamc@92 931 | (_, L'.CConcat _) => isRecord ()
adamc@10 932
adamc@76 933 | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) =>
adamc@10 934 if r1 = r2 then
adamc@86 935 []
adamc@10 936 else
adamc@10 937 (unifyKinds k1 k2;
adamc@86 938 r1 := SOME c2All;
adamc@86 939 [])
adamc@10 940
adamc@76 941 | (L'.CUnif (_, _, _, r), _) =>
adamc@10 942 if occursCon r c2All then
adamc@10 943 err COccursCheckFailed
adamc@10 944 else
adamc@86 945 (r := SOME c2All;
adamc@86 946 [])
adamc@76 947 | (_, L'.CUnif (_, _, _, r)) =>
adamc@10 948 if occursCon r c1All then
adamc@10 949 err COccursCheckFailed
adamc@10 950 else
adamc@86 951 (r := SOME c1All;
adamc@86 952 [])
adamc@10 953
adamc@71 954 | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) =>
adamc@71 955 (unifyKinds dom1 dom2;
adamc@86 956 unifyKinds ran1 ran2;
adamc@86 957 [])
adamc@71 958
adamc@205 959
adamc@10 960 | _ => err CIncompatible
adamc@10 961 end
adamc@10 962
adamc@86 963 and unifyCons (env, denv) c1 c2 =
adamc@86 964 unifyCons' (env, denv) c1 c2
adamc@10 965 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 966 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 967
adamc@10 968 datatype exp_error =
adamc@10 969 UnboundExp of ErrorMsg.span * string
adamc@34 970 | UnboundStrInExp of ErrorMsg.span * string
adamc@10 971 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 972 | Unif of string * L'.con
adamc@11 973 | WrongForm of string * L'.exp * L'.con
adamc@92 974 | IncompatibleCons of L'.con * L'.con
adamc@171 975 | DuplicatePatternVariable of ErrorMsg.span * string
adamc@171 976 | PatUnify of L'.pat * L'.con * L'.con * cunify_error
adamc@174 977 | UnboundConstructor of ErrorMsg.span * string list * string
adamc@171 978 | PatHasArg of ErrorMsg.span
adamc@171 979 | PatHasNoArg of ErrorMsg.span
adamc@172 980 | Inexhaustive of ErrorMsg.span
adamc@175 981 | DuplicatePatField of ErrorMsg.span * string
adamc@10 982
adamc@10 983 fun expError env err =
adamc@10 984 case err of
adamc@10 985 UnboundExp (loc, s) =>
adamc@10 986 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@34 987 | UnboundStrInExp (loc, s) =>
adamc@34 988 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@10 989 | Unify (e, c1, c2, uerr) =>
adamc@10 990 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 991 eprefaces' [("Expression", p_exp env e),
adamc@10 992 ("Have con", p_con env c1),
adamc@10 993 ("Need con", p_con env c2)];
adamc@10 994 cunifyError env uerr)
adamc@11 995 | Unif (action, c) =>
adamc@11 996 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 997 eprefaces' [("Con", p_con env c)])
adamc@11 998 | WrongForm (variety, e, t) =>
adamc@11 999 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 1000 eprefaces' [("Expression", p_exp env e),
adamc@11 1001 ("Type", p_con env t)])
adamc@92 1002 | IncompatibleCons (c1, c2) =>
adamc@92 1003 (ErrorMsg.errorAt (#2 c1) "Incompatible constructors";
adamc@92 1004 eprefaces' [("Con 1", p_con env c1),
adamc@92 1005 ("Con 2", p_con env c2)])
adamc@171 1006 | DuplicatePatternVariable (loc, s) =>
adamc@171 1007 ErrorMsg.errorAt loc ("Duplicate pattern variable " ^ s)
adamc@171 1008 | PatUnify (p, c1, c2, uerr) =>
adamc@171 1009 (ErrorMsg.errorAt (#2 p) "Unification failure for pattern";
adamc@171 1010 eprefaces' [("Pattern", p_pat env p),
adamc@171 1011 ("Have con", p_con env c1),
adamc@171 1012 ("Need con", p_con env c2)];
adamc@171 1013 cunifyError env uerr)
adamc@174 1014 | UnboundConstructor (loc, ms, s) =>
adamc@174 1015 ErrorMsg.errorAt loc ("Unbound constructor " ^ String.concatWith "." (ms @ [s]) ^ " in pattern")
adamc@171 1016 | PatHasArg loc =>
adamc@171 1017 ErrorMsg.errorAt loc "Constructor expects no argument but is used with argument"
adamc@171 1018 | PatHasNoArg loc =>
adamc@171 1019 ErrorMsg.errorAt loc "Constructor expects argument but is used with no argument"
adamc@172 1020 | Inexhaustive loc =>
adamc@172 1021 ErrorMsg.errorAt loc "Inexhaustive 'case'"
adamc@175 1022 | DuplicatePatField (loc, s) =>
adamc@175 1023 ErrorMsg.errorAt loc ("Duplicate record field " ^ s ^ " in pattern")
adamc@10 1024
adamc@86 1025 fun checkCon (env, denv) e c1 c2 =
adamc@86 1026 unifyCons (env, denv) c1 c2
adamc@10 1027 handle CUnify (c1, c2, err) =>
adamc@86 1028 (expError env (Unify (e, c1, c2, err));
adamc@86 1029 [])
adamc@10 1030
adamc@171 1031 fun checkPatCon (env, denv) p c1 c2 =
adamc@171 1032 unifyCons (env, denv) c1 c2
adamc@171 1033 handle CUnify (c1, c2, err) =>
adamc@171 1034 (expError env (PatUnify (p, c1, c2, err));
adamc@171 1035 [])
adamc@171 1036
adamc@14 1037 fun primType env p =
adamc@56 1038 case p of
adamc@56 1039 P.Int _ => !int
adamc@56 1040 | P.Float _ => !float
adamc@56 1041 | P.String _ => !string
adamc@14 1042
adamc@71 1043 fun recCons (k, nm, v, rest, loc) =
adamc@71 1044 (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc),
adamc@71 1045 rest), loc)
adamc@71 1046
adamc@71 1047 fun foldType (dom, loc) =
adamc@71 1048 (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc),
adamc@71 1049 (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc),
adamc@71 1050 (L'.TCFun (L'.Explicit, "v", dom,
adamc@71 1051 (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc),
adamc@71 1052 (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc),
adamc@71 1053 (L'.CApp ((L'.CRel 3, loc),
adamc@71 1054 recCons (dom,
adamc@71 1055 (L'.CRel 2, loc),
adamc@71 1056 (L'.CRel 1, loc),
adamc@71 1057 (L'.CRel 0, loc),
adamc@71 1058 loc)), loc)), loc)),
adamc@71 1059 loc)), loc)), loc),
adamc@71 1060 (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc),
adamc@71 1061 (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc),
adamc@71 1062 (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)),
adamc@71 1063 loc)), loc)), loc)
adamc@71 1064
adamc@86 1065 fun elabHead (env, denv) (e as (_, loc)) t =
adamc@15 1066 let
adamc@15 1067 fun unravel (t, e) =
adamc@86 1068 let
adamc@86 1069 val (t, gs) = hnormCon (env, denv) t
adamc@86 1070 in
adamc@86 1071 case t of
adamc@86 1072 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@86 1073 let
adamc@86 1074 val u = cunif (loc, k)
adamc@86 1075
adamc@86 1076 val (e, t, gs') = unravel (subConInCon (0, u) t',
adamc@86 1077 (L'.ECApp (e, u), loc))
adamc@86 1078 in
adamc@86 1079 (e, t, gs @ gs')
adamc@86 1080 end
adamc@86 1081 | _ => (e, t, gs)
adamc@86 1082 end
adamc@15 1083 in
adamc@15 1084 unravel (t, e)
adamc@15 1085 end
adamc@15 1086
adamc@174 1087 fun elabPat (pAll as (p, loc), (env, denv, bound)) =
adamc@171 1088 let
adamc@171 1089 val perror = (L'.PWild, loc)
adamc@171 1090 val terror = (L'.CError, loc)
adamc@171 1091 val pterror = (perror, terror)
adamc@171 1092 val rerror = (pterror, (env, bound))
adamc@171 1093
adamc@191 1094 fun pcon (pc, po, xs, to, dn, dk) =
adamc@188 1095 case (po, to) of
adamc@188 1096 (NONE, SOME _) => (expError env (PatHasNoArg loc);
adamc@188 1097 rerror)
adamc@188 1098 | (SOME _, NONE) => (expError env (PatHasArg loc);
adamc@188 1099 rerror)
adamc@191 1100 | (NONE, NONE) =>
adamc@191 1101 let
adamc@191 1102 val k = (L'.KType, loc)
adamc@191 1103 val unifs = map (fn _ => cunif (loc, k)) xs
adamc@191 1104 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
adamc@191 1105 in
adamc@191 1106 (((L'.PCon (dk, pc, unifs, NONE), loc), dn),
adamc@191 1107 (env, bound))
adamc@191 1108 end
adamc@188 1109 | (SOME p, SOME t) =>
adamc@188 1110 let
adamc@188 1111 val ((p', pt), (env, bound)) = elabPat (p, (env, denv, bound))
adamc@191 1112
adamc@191 1113 val k = (L'.KType, loc)
adamc@191 1114 val unifs = map (fn _ => cunif (loc, k)) xs
adamc@194 1115 val nxs = length unifs - 1
adamc@194 1116 val t = ListUtil.foldli (fn (i, u, t) => subConInCon (nxs - i, u) t) t unifs
adamc@191 1117 val dn = foldl (fn (u, dn) => (L'.CApp (dn, u), loc)) dn unifs
adamc@188 1118 in
adamc@191 1119 ignore (checkPatCon (env, denv) p' pt t);
adamc@191 1120 (((L'.PCon (dk, pc, unifs, SOME p'), loc), dn),
adamc@188 1121 (env, bound))
adamc@188 1122 end
adamc@171 1123 in
adamc@171 1124 case p of
adamc@171 1125 L.PWild => (((L'.PWild, loc), cunif (loc, (L'.KType, loc))),
adamc@171 1126 (env, bound))
adamc@171 1127 | L.PVar x =>
adamc@171 1128 let
adamc@171 1129 val t = if SS.member (bound, x) then
adamc@171 1130 (expError env (DuplicatePatternVariable (loc, x));
adamc@171 1131 terror)
adamc@171 1132 else
adamc@171 1133 cunif (loc, (L'.KType, loc))
adamc@171 1134 in
adamc@182 1135 (((L'.PVar (x, t), loc), t),
adamc@171 1136 (E.pushERel env x t, SS.add (bound, x)))
adamc@171 1137 end
adamc@173 1138 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
adamc@173 1139 (env, bound))
adamc@171 1140 | L.PCon ([], x, po) =>
adamc@171 1141 (case E.lookupConstructor env x of
adamc@174 1142 NONE => (expError env (UnboundConstructor (loc, [], x));
adamc@171 1143 rerror)
adamc@191 1144 | SOME (dk, n, xs, to, dn) => pcon (L'.PConVar n, po, xs, to, (L'.CNamed dn, loc), dk))
adamc@174 1145 | L.PCon (m1 :: ms, x, po) =>
adamc@174 1146 (case E.lookupStr env m1 of
adamc@174 1147 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@174 1148 rerror)
adamc@174 1149 | SOME (n, sgn) =>
adamc@174 1150 let
adamc@174 1151 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@174 1152 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@182 1153 NONE => raise Fail "elabPat: Unknown substructure"
adamc@174 1154 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@174 1155 ((L'.StrVar n, loc), sgn) ms
adamc@174 1156 in
adamc@174 1157 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 1158 NONE => (expError env (UnboundConstructor (loc, m1 :: ms, x));
adamc@174 1159 rerror)
adamc@191 1160 | SOME (dk, _, xs, to, dn) => pcon (L'.PConProj (n, ms, x), po, xs, to, dn, dk)
adamc@174 1161 end)
adamc@174 1162
adamc@175 1163 | L.PRecord (xps, flex) =>
adamc@175 1164 let
adamc@175 1165 val (xpts, (env, bound, _)) =
adamc@175 1166 ListUtil.foldlMap (fn ((x, p), (env, bound, fbound)) =>
adamc@175 1167 let
adamc@175 1168 val ((p', t), (env, bound)) = elabPat (p, (env, denv, bound))
adamc@175 1169 in
adamc@175 1170 if SS.member (fbound, x) then
adamc@175 1171 expError env (DuplicatePatField (loc, x))
adamc@175 1172 else
adamc@175 1173 ();
adamc@175 1174 ((x, p', t), (env, bound, SS.add (fbound, x)))
adamc@175 1175 end)
adamc@175 1176 (env, bound, SS.empty) xps
adamc@175 1177
adamc@175 1178 val k = (L'.KType, loc)
adamc@175 1179 val c = (L'.CRecord (k, map (fn (x, _, t) => ((L'.CName x, loc), t)) xpts), loc)
adamc@176 1180 val c =
adamc@175 1181 if flex then
adamc@176 1182 (L'.CConcat (c, cunif (loc, (L'.KRecord k, loc))), loc)
adamc@175 1183 else
adamc@176 1184 c
adamc@175 1185 in
adamc@182 1186 (((L'.PRecord xpts, loc),
adamc@175 1187 (L'.TRecord c, loc)),
adamc@175 1188 (env, bound))
adamc@175 1189 end
adamc@175 1190
adamc@171 1191 end
adamc@171 1192
adamc@172 1193 datatype coverage =
adamc@172 1194 Wild
adamc@173 1195 | None
adamc@172 1196 | Datatype of coverage IM.map
adamc@175 1197 | Record of coverage SM.map list
adamc@172 1198
adamc@172 1199 fun exhaustive (env, denv, t, ps) =
adamc@172 1200 let
adamc@172 1201 fun pcCoverage pc =
adamc@172 1202 case pc of
adamc@172 1203 L'.PConVar n => n
adamc@174 1204 | L'.PConProj (m1, ms, x) =>
adamc@174 1205 let
adamc@174 1206 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@174 1207 in
adamc@174 1208 case E.projectConstructor env {str = str, sgn = sgn, field = x} of
adamc@174 1209 NONE => raise Fail "exhaustive: Can't project constructor"
adamc@191 1210 | SOME (_, n, _, _, _) => n
adamc@174 1211 end
adamc@172 1212
adamc@172 1213 fun coverage (p, _) =
adamc@172 1214 case p of
adamc@172 1215 L'.PWild => Wild
adamc@172 1216 | L'.PVar _ => Wild
adamc@173 1217 | L'.PPrim _ => None
adamc@191 1218 | L'.PCon (_, pc, _, NONE) => Datatype (IM.insert (IM.empty, pcCoverage pc, Wild))
adamc@191 1219 | L'.PCon (_, pc, _, SOME p) => Datatype (IM.insert (IM.empty, pcCoverage pc, coverage p))
adamc@182 1220 | L'.PRecord xps => Record [foldl (fn ((x, p, _), fmap) =>
adamc@182 1221 SM.insert (fmap, x, coverage p)) SM.empty xps]
adamc@176 1222
adamc@172 1223 fun merge (c1, c2) =
adamc@172 1224 case (c1, c2) of
adamc@173 1225 (None, _) => c2
adamc@173 1226 | (_, None) => c1
adamc@173 1227
adamc@173 1228 | (Wild, _) => Wild
adamc@172 1229 | (_, Wild) => Wild
adamc@172 1230
adamc@172 1231 | (Datatype cm1, Datatype cm2) => Datatype (IM.unionWith merge (cm1, cm2))
adamc@172 1232
adamc@175 1233 | (Record fm1, Record fm2) => Record (fm1 @ fm2)
adamc@175 1234
adamc@175 1235 | _ => None
adamc@175 1236
adamc@172 1237 fun combinedCoverage ps =
adamc@172 1238 case ps of
adamc@172 1239 [] => raise Fail "Empty pattern list for coverage checking"
adamc@172 1240 | [p] => coverage p
adamc@172 1241 | p :: ps => merge (coverage p, combinedCoverage ps)
adamc@172 1242
adamc@175 1243 fun enumerateCases t =
adamc@175 1244 let
adamc@175 1245 fun dtype cons =
adamc@175 1246 ListUtil.mapConcat (fn (_, n, to) =>
adamc@175 1247 case to of
adamc@175 1248 NONE => [Datatype (IM.insert (IM.empty, n, Wild))]
adamc@175 1249 | SOME t => map (fn c => Datatype (IM.insert (IM.empty, n, c)))
adamc@175 1250 (enumerateCases t)) cons
adamc@175 1251 in
adamc@175 1252 case #1 (#1 (hnormCon (env, denv) t)) of
adamc@175 1253 L'.CNamed n =>
adamc@175 1254 (let
adamc@175 1255 val dt = E.lookupDatatype env n
adamc@175 1256 val cons = E.constructors dt
adamc@175 1257 in
adamc@175 1258 dtype cons
adamc@175 1259 end handle E.UnboundNamed _ => [Wild])
adamc@175 1260 | L'.TRecord c =>
adamc@175 1261 (case #1 (#1 (hnormCon (env, denv) c)) of
adamc@175 1262 L'.CRecord (_, xts) =>
adamc@175 1263 let
adamc@175 1264 val xts = map (fn (x, t) => (#1 (hnormCon (env, denv) x), t)) xts
adamc@175 1265
adamc@175 1266 fun exponentiate fs =
adamc@175 1267 case fs of
adamc@175 1268 [] => [SM.empty]
adamc@175 1269 | ((L'.CName x, _), t) :: rest =>
adamc@175 1270 let
adamc@175 1271 val this = enumerateCases t
adamc@175 1272 val rest = exponentiate rest
adamc@175 1273 in
adamc@175 1274 ListUtil.mapConcat (fn fmap =>
adamc@175 1275 map (fn c => SM.insert (fmap, x, c)) this) rest
adamc@175 1276 end
adamc@175 1277 | _ => raise Fail "exponentiate: Not CName"
adamc@175 1278 in
adamc@175 1279 if List.exists (fn ((L'.CName _, _), _) => false
adamc@175 1280 | (c, _) => true) xts then
adamc@175 1281 [Wild]
adamc@175 1282 else
adamc@175 1283 map (fn ls => Record [ls]) (exponentiate xts)
adamc@175 1284 end
adamc@175 1285 | _ => [Wild])
adamc@175 1286 | _ => [Wild]
adamc@175 1287 end
adamc@175 1288
adamc@175 1289 fun coverageImp (c1, c2) =
adamc@175 1290 case (c1, c2) of
adamc@175 1291 (Wild, _) => true
adamc@175 1292
adamc@175 1293 | (Datatype cmap1, Datatype cmap2) =>
adamc@175 1294 List.all (fn (n, c2) =>
adamc@175 1295 case IM.find (cmap1, n) of
adamc@175 1296 NONE => false
adamc@175 1297 | SOME c1 => coverageImp (c1, c2)) (IM.listItemsi cmap2)
adamc@175 1298
adamc@175 1299 | (Record fmaps1, Record fmaps2) =>
adamc@175 1300 List.all (fn fmap2 =>
adamc@175 1301 List.exists (fn fmap1 =>
adamc@175 1302 List.all (fn (x, c2) =>
adamc@175 1303 case SM.find (fmap1, x) of
adamc@175 1304 NONE => true
adamc@175 1305 | SOME c1 => coverageImp (c1, c2))
adamc@175 1306 (SM.listItemsi fmap2))
adamc@175 1307 fmaps1) fmaps2
adamc@175 1308
adamc@175 1309 | _ => false
adamc@175 1310
adamc@172 1311 fun isTotal (c, t) =
adamc@172 1312 case c of
adamc@173 1313 None => (false, [])
adamc@173 1314 | Wild => (true, [])
adamc@172 1315 | Datatype cm =>
adamc@172 1316 let
adamc@172 1317 val ((t, _), gs) = hnormCon (env, denv) t
adamc@174 1318
adamc@174 1319 fun dtype cons =
adamc@174 1320 foldl (fn ((_, n, to), (total, gs)) =>
adamc@174 1321 case IM.find (cm, n) of
adamc@174 1322 NONE => (false, gs)
adamc@174 1323 | SOME c' =>
adamc@174 1324 case to of
adamc@174 1325 NONE => (total, gs)
adamc@174 1326 | SOME t' =>
adamc@174 1327 let
adamc@174 1328 val (total, gs') = isTotal (c', t')
adamc@174 1329 in
adamc@174 1330 (total, gs' @ gs)
adamc@174 1331 end)
adamc@174 1332 (true, gs) cons
adamc@191 1333
adamc@191 1334 fun unapp t =
adamc@191 1335 case t of
adamc@191 1336 L'.CApp ((t, _), _) => unapp t
adamc@191 1337 | _ => t
adamc@172 1338 in
adamc@191 1339 case unapp t of
adamc@172 1340 L'.CNamed n =>
adamc@172 1341 let
adamc@172 1342 val dt = E.lookupDatatype env n
adamc@172 1343 val cons = E.constructors dt
adamc@172 1344 in
adamc@174 1345 dtype cons
adamc@174 1346 end
adamc@174 1347 | L'.CModProj (m1, ms, x) =>
adamc@174 1348 let
adamc@174 1349 val (str, sgn) = E.chaseMpath env (m1, ms)
adamc@174 1350 in
adamc@174 1351 case E.projectDatatype env {str = str, sgn = sgn, field = x} of
adamc@174 1352 NONE => raise Fail "isTotal: Can't project datatype"
adamc@191 1353 | SOME (_, cons) => dtype cons
adamc@172 1354 end
adamc@172 1355 | L'.CError => (true, gs)
adamc@172 1356 | _ => raise Fail "isTotal: Not a datatype"
adamc@172 1357 end
adamc@175 1358 | Record _ => (List.all (fn c2 => coverageImp (c, c2)) (enumerateCases t), [])
adamc@172 1359 in
adamc@172 1360 isTotal (combinedCoverage ps, t)
adamc@172 1361 end
adamc@172 1362
adamc@92 1363 fun elabExp (env, denv) (eAll as (e, loc)) =
adamc@91 1364 let
adamc@92 1365
adamc@92 1366 in
adamc@92 1367 (*eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*)
adamc@10 1368
adamc@91 1369 case e of
adamc@91 1370 L.EAnnot (e, t) =>
adamc@91 1371 let
adamc@91 1372 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1373 val (t', _, gs2) = elabCon (env, denv) t
adamc@91 1374 val gs3 = checkCon (env, denv) e' et t'
adamc@91 1375 in
adamc@91 1376 (e', t', gs1 @ gs2 @ gs3)
adamc@91 1377 end
adamc@34 1378
adamc@91 1379 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@91 1380 | L.EVar ([], s) =>
adamc@91 1381 (case E.lookupE env s of
adamc@91 1382 E.NotBound =>
adamc@91 1383 (expError env (UnboundExp (loc, s));
adamc@91 1384 (eerror, cerror, []))
adamc@91 1385 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
adamc@191 1386 | E.Named (n, t) =>
adamc@191 1387 if Char.isUpper (String.sub (s, 0)) then
adamc@191 1388 elabHead (env, denv) (L'.ENamed n, loc) t
adamc@191 1389 else
adamc@191 1390 ((L'.ENamed n, loc), t, []))
adamc@91 1391 | L.EVar (m1 :: ms, s) =>
adamc@91 1392 (case E.lookupStr env m1 of
adamc@91 1393 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@91 1394 (eerror, cerror, []))
adamc@91 1395 | SOME (n, sgn) =>
adamc@91 1396 let
adamc@91 1397 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@91 1398 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@91 1399 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@91 1400 (strerror, sgnerror))
adamc@91 1401 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@91 1402 ((L'.StrVar n, loc), sgn) ms
adamc@10 1403
adamc@91 1404 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@91 1405 NONE => (expError env (UnboundExp (loc, s));
adamc@91 1406 cerror)
adamc@91 1407 | SOME t => t
adamc@91 1408 in
adamc@91 1409 ((L'.EModProj (n, ms, s), loc), t, [])
adamc@91 1410 end)
adamc@86 1411
adamc@91 1412 | L.EApp (e1, e2) =>
adamc@91 1413 let
adamc@91 1414 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@91 1415 val (e1', t1, gs2) = elabHead (env, denv) e1' t1
adamc@91 1416 val (e2', t2, gs3) = elabExp (env, denv) e2
adamc@71 1417
adamc@91 1418 val dom = cunif (loc, ktype)
adamc@91 1419 val ran = cunif (loc, ktype)
adamc@91 1420 val t = (L'.TFun (dom, ran), dummy)
adamc@91 1421
adamc@91 1422 val gs4 = checkCon (env, denv) e1' t1 t
adamc@91 1423 val gs5 = checkCon (env, denv) e2' t2 dom
adamc@205 1424
adamc@205 1425 val gs = gs1 @ gs2 @ gs3 @ gs4 @ gs5
adamc@91 1426 in
adamc@205 1427 ((L'.EApp (e1', e2'), loc), ran, gs)
adamc@91 1428 end
adamc@91 1429 | L.EAbs (x, to, e) =>
adamc@91 1430 let
adamc@91 1431 val (t', gs1) = case to of
adamc@91 1432 NONE => (cunif (loc, ktype), [])
adamc@91 1433 | SOME t =>
adamc@91 1434 let
adamc@91 1435 val (t', tk, gs) = elabCon (env, denv) t
adamc@91 1436 in
adamc@91 1437 checkKind env t' tk ktype;
adamc@91 1438 (t', gs)
adamc@91 1439 end
adamc@91 1440 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
adamc@91 1441 in
adamc@91 1442 ((L'.EAbs (x, t', et, e'), loc),
adamc@91 1443 (L'.TFun (t', et), loc),
adamc@91 1444 gs1 @ gs2)
adamc@91 1445 end
adamc@91 1446 | L.ECApp (e, c) =>
adamc@91 1447 let
adamc@91 1448 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1449 val (e', et, gs2) = elabHead (env, denv) e' et
adamc@91 1450 val (c', ck, gs3) = elabCon (env, denv) c
adamc@91 1451 val ((et', _), gs4) = hnormCon (env, denv) et
adamc@91 1452 in
adamc@91 1453 case et' of
adamc@91 1454 L'.CError => (eerror, cerror, [])
adamc@91 1455 | L'.TCFun (_, _, k, eb) =>
adamc@91 1456 let
adamc@91 1457 val () = checkKind env c' ck k
adamc@91 1458 val eb' = subConInCon (0, c') eb
adamc@91 1459 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@91 1460 cerror)
adamc@91 1461 in
adamc@91 1462 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1463 end
adamc@91 1464
adamc@91 1465 | L'.CUnif _ =>
adamc@91 1466 (expError env (Unif ("application", et));
adamc@91 1467 (eerror, cerror, []))
adamc@91 1468
adamc@91 1469 | _ =>
adamc@91 1470 (expError env (WrongForm ("constructor function", e', et));
adamc@91 1471 (eerror, cerror, []))
adamc@91 1472 end
adamc@91 1473 | L.ECAbs (expl, x, k, e) =>
adamc@91 1474 let
adamc@91 1475 val expl' = elabExplicitness expl
adamc@91 1476 val k' = elabKind k
adamc@91 1477 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
adamc@91 1478 in
adamc@91 1479 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@91 1480 (L'.TCFun (expl', x, k', et), loc),
adamc@91 1481 gs)
adamc@91 1482 end
adamc@91 1483
adamc@91 1484 | L.EDisjoint (c1, c2, e) =>
adamc@91 1485 let
adamc@91 1486 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@91 1487 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@91 1488
adamc@91 1489 val ku1 = kunif loc
adamc@91 1490 val ku2 = kunif loc
adamc@91 1491
adamc@91 1492 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@91 1493 val (e', t, gs4) = elabExp (env, denv') e
adamc@91 1494 in
adamc@91 1495 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@91 1496 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@91 1497
adamc@91 1498 (e', (L'.TDisjoint (c1', c2', t), loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1499 end
adamc@91 1500
adamc@91 1501 | L.ERecord xes =>
adamc@91 1502 let
adamc@91 1503 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@91 1504 let
adamc@91 1505 val (x', xk, gs1) = elabCon (env, denv) x
adamc@91 1506 val (e', et, gs2) = elabExp (env, denv) e
adamc@91 1507 in
adamc@91 1508 checkKind env x' xk kname;
adamc@91 1509 ((x', e', et), gs1 @ gs2 @ gs)
adamc@91 1510 end)
adamc@91 1511 [] xes
adamc@91 1512
adamc@91 1513 val k = (L'.KType, loc)
adamc@91 1514
adamc@91 1515 fun prove (xets, gs) =
adamc@91 1516 case xets of
adamc@91 1517 [] => gs
adamc@91 1518 | (x, _, t) :: rest =>
adamc@91 1519 let
adamc@91 1520 val xc = (x, t)
adamc@91 1521 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@91 1522 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@91 1523 let
adamc@91 1524 val xc' = (x', t')
adamc@91 1525 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@91 1526 in
adamc@91 1527 D.prove env denv (r1, r2, loc) @ gs
adamc@91 1528 end)
adamc@91 1529 gs rest
adamc@91 1530 in
adamc@91 1531 prove (rest, gs)
adamc@91 1532 end
adamc@91 1533 in
adamc@91 1534 ((L'.ERecord xes', loc),
adamc@91 1535 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@91 1536 prove (xes', gs))
adamc@91 1537 end
adamc@91 1538
adamc@91 1539 | L.EField (e, c) =>
adamc@91 1540 let
adamc@91 1541 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1542 val (c', ck, gs2) = elabCon (env, denv) c
adamc@91 1543
adamc@91 1544 val ft = cunif (loc, ktype)
adamc@91 1545 val rest = cunif (loc, ktype_record)
adamc@91 1546 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@91 1547
adamc@91 1548 val gs3 =
adamc@91 1549 checkCon (env, denv) e' et
adamc@91 1550 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@91 1551 val gs4 = D.prove env denv (first, rest, loc)
adamc@91 1552 in
adamc@91 1553 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1554 end
adamc@91 1555
adamc@149 1556 | L.ECut (e, c) =>
adamc@149 1557 let
adamc@149 1558 val (e', et, gs1) = elabExp (env, denv) e
adamc@149 1559 val (c', ck, gs2) = elabCon (env, denv) c
adamc@149 1560
adamc@149 1561 val ft = cunif (loc, ktype)
adamc@149 1562 val rest = cunif (loc, ktype_record)
adamc@149 1563 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@149 1564
adamc@149 1565 val gs3 =
adamc@149 1566 checkCon (env, denv) e' et
adamc@149 1567 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@149 1568 val gs4 = D.prove env denv (first, rest, loc)
adamc@149 1569 in
adamc@149 1570 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@149 1571 end
adamc@149 1572
adamc@91 1573 | L.EFold =>
adamc@91 1574 let
adamc@91 1575 val dom = kunif loc
adamc@91 1576 in
adamc@91 1577 ((L'.EFold dom, loc), foldType (dom, loc), [])
adamc@91 1578 end
adamc@170 1579
adamc@171 1580 | L.ECase (e, pes) =>
adamc@171 1581 let
adamc@171 1582 val (e', et, gs1) = elabExp (env, denv) e
adamc@171 1583 val result = cunif (loc, (L'.KType, loc))
adamc@171 1584 val (pes', gs) = ListUtil.foldlMap
adamc@171 1585 (fn ((p, e), gs) =>
adamc@171 1586 let
adamc@174 1587 val ((p', pt), (env, _)) = elabPat (p, (env, denv, SS.empty))
adamc@171 1588
adamc@171 1589 val gs1 = checkPatCon (env, denv) p' pt et
adamc@171 1590 val (e', et, gs2) = elabExp (env, denv) e
adamc@171 1591 val gs3 = checkCon (env, denv) e' et result
adamc@171 1592 in
adamc@171 1593 ((p', e'), gs1 @ gs2 @ gs3 @ gs)
adamc@171 1594 end)
adamc@171 1595 gs1 pes
adamc@172 1596
adamc@172 1597 val (total, gs') = exhaustive (env, denv, et, map #1 pes')
adamc@171 1598 in
adamc@172 1599 if total then
adamc@172 1600 ()
adamc@172 1601 else
adamc@172 1602 expError env (Inexhaustive loc);
adamc@172 1603
adamc@182 1604 ((L'.ECase (e', pes', {disc = et, result = result}), loc), result, gs' @ gs)
adamc@171 1605 end
adamc@91 1606 end
adamc@12 1607
adamc@12 1608
adamc@6 1609 datatype decl_error =
adamc@91 1610 KunifsRemain of L'.decl list
adamc@91 1611 | CunifsRemain of L'.decl list
adamc@91 1612
adamc@91 1613 fun lspan [] = ErrorMsg.dummySpan
adamc@91 1614 | lspan ((_, loc) :: _) = loc
adamc@6 1615
adamc@6 1616 fun declError env err =
adamc@6 1617 case err of
adamc@91 1618 KunifsRemain ds =>
adamc@91 1619 (ErrorMsg.errorAt (lspan ds) "Some kind unification variables are undetermined in declaration";
adamc@91 1620 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@91 1621 | CunifsRemain ds =>
adamc@91 1622 (ErrorMsg.errorAt (lspan ds) "Some constructor unification variables are undetermined in declaration";
adamc@91 1623 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@6 1624
adamc@31 1625 datatype sgn_error =
adamc@31 1626 UnboundSgn of ErrorMsg.span * string
adamc@31 1627 | UnmatchedSgi of L'.sgn_item
adamc@31 1628 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 1629 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@156 1630 | SgiMismatchedDatatypes of L'.sgn_item * L'.sgn_item * (L'.con * L'.con * cunify_error) option
adamc@41 1631 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 1632 | UnWhereable of L'.sgn * string
adamc@75 1633 | WhereWrongKind of L'.kind * L'.kind * kunify_error
adamc@58 1634 | NotIncludable of L'.sgn
adamc@62 1635 | DuplicateCon of ErrorMsg.span * string
adamc@62 1636 | DuplicateVal of ErrorMsg.span * string
adamc@62 1637 | DuplicateSgn of ErrorMsg.span * string
adamc@62 1638 | DuplicateStr of ErrorMsg.span * string
adamc@88 1639 | NotConstraintsable of L'.sgn
adamc@31 1640
adamc@31 1641 fun sgnError env err =
adamc@31 1642 case err of
adamc@31 1643 UnboundSgn (loc, s) =>
adamc@31 1644 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 1645 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 1646 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 1647 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 1648 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 1649 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@59 1650 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1651 ("Need", p_sgn_item env sgi2),
adamc@31 1652 ("Kind 1", p_kind k1),
adamc@31 1653 ("Kind 2", p_kind k2)];
adamc@31 1654 kunifyError kerr)
adamc@31 1655 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 1656 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@59 1657 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1658 ("Need", p_sgn_item env sgi2),
adamc@31 1659 ("Con 1", p_con env c1),
adamc@31 1660 ("Con 2", p_con env c2)];
adamc@31 1661 cunifyError env cerr)
adamc@156 1662 | SgiMismatchedDatatypes (sgi1, sgi2, cerro) =>
adamc@156 1663 (ErrorMsg.errorAt (#2 sgi1) "Mismatched 'datatype' specifications:";
adamc@156 1664 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@156 1665 ("Need", p_sgn_item env sgi2)];
adamc@156 1666 Option.app (fn (c1, c2, ue) =>
adamc@156 1667 (eprefaces "Unification error"
adamc@156 1668 [("Con 1", p_con env c1),
adamc@156 1669 ("Con 2", p_con env c2)];
adamc@156 1670 cunifyError env ue)) cerro)
adamc@41 1671 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1672 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1673 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1674 ("Sig 2", p_sgn env sgn2)])
adamc@42 1675 | UnWhereable (sgn, x) =>
adamc@42 1676 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1677 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1678 ("Field", PD.string x)])
adamc@75 1679 | WhereWrongKind (k1, k2, kerr) =>
adamc@75 1680 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
adamc@75 1681 eprefaces' [("Have", p_kind k1),
adamc@75 1682 ("Need", p_kind k2)];
adamc@75 1683 kunifyError kerr)
adamc@58 1684 | NotIncludable sgn =>
adamc@58 1685 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
adamc@58 1686 eprefaces' [("Signature", p_sgn env sgn)])
adamc@62 1687 | DuplicateCon (loc, s) =>
adamc@62 1688 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
adamc@62 1689 | DuplicateVal (loc, s) =>
adamc@62 1690 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
adamc@62 1691 | DuplicateSgn (loc, s) =>
adamc@62 1692 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
adamc@62 1693 | DuplicateStr (loc, s) =>
adamc@62 1694 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
adamc@88 1695 | NotConstraintsable sgn =>
adamc@88 1696 (ErrorMsg.errorAt (#2 sgn) "Invalid signature for 'open constraints'";
adamc@88 1697 eprefaces' [("Signature", p_sgn env sgn)])
adamc@31 1698
adamc@31 1699 datatype str_error =
adamc@31 1700 UnboundStr of ErrorMsg.span * string
adamc@44 1701 | NotFunctor of L'.sgn
adamc@46 1702 | FunctorRebind of ErrorMsg.span
adamc@61 1703 | UnOpenable of L'.sgn
adamc@75 1704 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
adamc@156 1705 | DuplicateConstructor of string * ErrorMsg.span
adamc@158 1706 | NotDatatype of ErrorMsg.span
adamc@31 1707
adamc@31 1708 fun strError env err =
adamc@31 1709 case err of
adamc@31 1710 UnboundStr (loc, s) =>
adamc@31 1711 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1712 | NotFunctor sgn =>
adamc@44 1713 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1714 eprefaces' [("Signature", p_sgn env sgn)])
adamc@46 1715 | FunctorRebind loc =>
adamc@46 1716 ErrorMsg.errorAt loc "Attempt to rebind functor"
adamc@61 1717 | UnOpenable sgn =>
adamc@61 1718 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
adamc@61 1719 eprefaces' [("Signature", p_sgn env sgn)])
adamc@75 1720 | NotType (k, (k1, k2, ue)) =>
adamc@75 1721 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
adamc@75 1722 eprefaces' [("Kind", p_kind k),
adamc@75 1723 ("Subkind 1", p_kind k1),
adamc@75 1724 ("Subkind 2", p_kind k2)];
adamc@75 1725 kunifyError ue)
adamc@156 1726 | DuplicateConstructor (x, loc) =>
adamc@156 1727 ErrorMsg.errorAt loc ("Duplicate datatype constructor " ^ x)
adamc@158 1728 | NotDatatype loc =>
adamc@158 1729 ErrorMsg.errorAt loc "Trying to import non-datatype as a datatype"
adamc@31 1730
adamc@42 1731 val hnormSgn = E.hnormSgn
adamc@31 1732
adamc@205 1733 fun tableOf' env =
adamc@205 1734 case E.lookupStr env "Basis" of
adamc@205 1735 NONE => raise Fail "Elaborate.tableOf: Can't find Basis"
adamc@205 1736 | SOME (n, _) => n
adamc@205 1737
adamc@205 1738 fun tableOf env = (L'.CModProj (tableOf' env, [], "sql_table"), ErrorMsg.dummySpan)
adamc@205 1739
adamc@88 1740 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@76 1741 case sgi of
adamc@76 1742 L.SgiConAbs (x, k) =>
adamc@76 1743 let
adamc@76 1744 val k' = elabKind k
adamc@31 1745
adamc@76 1746 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1747 in
adamc@88 1748 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@76 1749 end
adamc@31 1750
adamc@76 1751 | L.SgiCon (x, ko, c) =>
adamc@76 1752 let
adamc@76 1753 val k' = case ko of
adamc@76 1754 NONE => kunif loc
adamc@76 1755 | SOME k => elabKind k
adamc@31 1756
adamc@83 1757 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1758 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1759 in
adamc@76 1760 checkKind env c' ck k';
adamc@31 1761
adamc@88 1762 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1763 end
adamc@31 1764
adamc@191 1765 | L.SgiDatatype (x, xs, xcs) =>
adamc@157 1766 let
adamc@157 1767 val k = (L'.KType, loc)
adamc@191 1768 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 1769 val (env, n) = E.pushCNamed env x k' NONE
adamc@157 1770 val t = (L'.CNamed n, loc)
adamc@194 1771 val nxs = length xs - 1
adamc@194 1772 val t = ListUtil.foldli (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@157 1773
adamc@157 1774 val (xcs, (used, env, gs)) =
adamc@157 1775 ListUtil.foldlMap
adamc@157 1776 (fn ((x, to), (used, env, gs)) =>
adamc@157 1777 let
adamc@157 1778 val (to, t, gs') = case to of
adamc@157 1779 NONE => (NONE, t, gs)
adamc@157 1780 | SOME t' =>
adamc@157 1781 let
adamc@157 1782 val (t', tk, gs') = elabCon (env, denv) t'
adamc@157 1783 in
adamc@157 1784 checkKind env t' tk k;
adamc@157 1785 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@157 1786 end
adamc@191 1787 val t = foldl (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
adamc@157 1788
adamc@157 1789 val (env, n') = E.pushENamed env x t
adamc@157 1790 in
adamc@157 1791 if SS.member (used, x) then
adamc@157 1792 strError env (DuplicateConstructor (x, loc))
adamc@157 1793 else
adamc@157 1794 ();
adamc@157 1795 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@157 1796 end)
adamc@157 1797 (SS.empty, env, []) xcs
adamc@191 1798
adamc@191 1799 val env = E.pushDatatype env n xs xcs
adamc@157 1800 in
adamc@191 1801 ([(L'.SgiDatatype (x, n, xs, xcs), loc)], (env, denv, gs))
adamc@157 1802 end
adamc@156 1803
adamc@158 1804 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
adamc@158 1805
adamc@158 1806 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
adamc@158 1807 (case E.lookupStr env m1 of
adamc@158 1808 NONE => (strError env (UnboundStr (loc, m1));
adamc@158 1809 ([], (env, denv, gs)))
adamc@158 1810 | SOME (n, sgn) =>
adamc@158 1811 let
adamc@158 1812 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@158 1813 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1814 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@158 1815 (strerror, sgnerror))
adamc@158 1816 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@158 1817 ((L'.StrVar n, loc), sgn) ms
adamc@158 1818 in
adamc@161 1819 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@161 1820 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@161 1821 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@161 1822 NONE => (conError env (UnboundDatatype (loc, s));
adamc@161 1823 ([], (env, denv, gs)))
adamc@191 1824 | SOME (xs, xncs) =>
adamc@161 1825 let
adamc@161 1826 val k = (L'.KType, loc)
adamc@191 1827 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 1828
adamc@161 1829 val t = (L'.CModProj (n, ms, s), loc)
adamc@191 1830 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@191 1831 val env = E.pushDatatype env n' xs xncs
adamc@158 1832
adamc@161 1833 val t = (L'.CNamed n', loc)
adamc@161 1834 val env = foldl (fn ((x, n, to), env) =>
adamc@161 1835 let
adamc@161 1836 val t = case to of
adamc@161 1837 NONE => t
adamc@161 1838 | SOME t' => (L'.TFun (t', t), loc)
adamc@191 1839
adamc@191 1840 val t = foldr (fn (x, t) =>
adamc@191 1841 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@191 1842 t xs
adamc@161 1843 in
adamc@161 1844 E.pushENamedAs env x n t
adamc@161 1845 end) env xncs
adamc@161 1846 in
adamc@191 1847 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, gs))
adamc@161 1848 end)
adamc@161 1849 | _ => (strError env (NotDatatype loc);
adamc@161 1850 ([], (env, denv, [])))
adamc@158 1851 end)
adamc@156 1852
adamc@76 1853 | L.SgiVal (x, c) =>
adamc@76 1854 let
adamc@83 1855 val (c', ck, gs') = elabCon (env, denv) c
adamc@31 1856
adamc@76 1857 val (env', n) = E.pushENamed env x c'
adamc@76 1858 in
adamc@76 1859 (unifyKinds ck ktype
adamc@76 1860 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 1861
adamc@88 1862 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@76 1863 end
adamc@31 1864
adamc@76 1865 | L.SgiStr (x, sgn) =>
adamc@76 1866 let
adamc@83 1867 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1868 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1869 in
adamc@88 1870 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1871 end
adamc@31 1872
adamc@76 1873 | L.SgiSgn (x, sgn) =>
adamc@76 1874 let
adamc@83 1875 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1876 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1877 in
adamc@88 1878 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1879 end
adamc@31 1880
adamc@76 1881 | L.SgiInclude sgn =>
adamc@76 1882 let
adamc@83 1883 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1884 in
adamc@76 1885 case #1 (hnormSgn env sgn') of
adamc@76 1886 L'.SgnConst sgis =>
adamc@88 1887 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@76 1888 | _ => (sgnError env (NotIncludable sgn');
adamc@88 1889 ([], (env, denv, [])))
adamc@88 1890 end
adamc@88 1891
adamc@88 1892 | L.SgiConstraint (c1, c2) =>
adamc@88 1893 let
adamc@88 1894 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 1895 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@88 1896
adamc@90 1897 val (denv, gs3) = D.assert env denv (c1', c2')
adamc@88 1898 in
adamc@88 1899 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 1900 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 1901
adamc@90 1902 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3))
adamc@76 1903 end
adamc@31 1904
adamc@203 1905 | L.SgiTable (x, c) =>
adamc@203 1906 let
adamc@203 1907 val (c', k, gs) = elabCon (env, denv) c
adamc@205 1908 val (env, n) = E.pushENamed env x (L'.CApp (tableOf env, c'), loc)
adamc@203 1909 in
adamc@203 1910 checkKind env c' k (L'.KRecord (L'.KType, loc), loc);
adamc@205 1911 ([(L'.SgiTable (tableOf' env, x, n, c'), loc)], (env, denv, gs))
adamc@203 1912 end
adamc@203 1913
adamc@83 1914 and elabSgn (env, denv) (sgn, loc) =
adamc@31 1915 case sgn of
adamc@31 1916 L.SgnConst sgis =>
adamc@31 1917 let
adamc@88 1918 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 1919
adamc@62 1920 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 1921 case sgi of
adamc@62 1922 L'.SgiConAbs (x, _, _) =>
adamc@62 1923 (if SS.member (cons, x) then
adamc@62 1924 sgnError env (DuplicateCon (loc, x))
adamc@62 1925 else
adamc@62 1926 ();
adamc@62 1927 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1928 | L'.SgiCon (x, _, _, _) =>
adamc@62 1929 (if SS.member (cons, x) then
adamc@62 1930 sgnError env (DuplicateCon (loc, x))
adamc@62 1931 else
adamc@62 1932 ();
adamc@62 1933 (SS.add (cons, x), vals, sgns, strs))
adamc@191 1934 | L'.SgiDatatype (x, _, _, xncs) =>
adamc@156 1935 let
adamc@156 1936 val vals = foldl (fn ((x, _, _), vals) =>
adamc@156 1937 (if SS.member (vals, x) then
adamc@156 1938 sgnError env (DuplicateVal (loc, x))
adamc@156 1939 else
adamc@156 1940 ();
adamc@156 1941 SS.add (vals, x)))
adamc@156 1942 vals xncs
adamc@156 1943 in
adamc@156 1944 if SS.member (cons, x) then
adamc@156 1945 sgnError env (DuplicateCon (loc, x))
adamc@156 1946 else
adamc@156 1947 ();
adamc@156 1948 (SS.add (cons, x), vals, sgns, strs)
adamc@156 1949 end
adamc@191 1950 | L'.SgiDatatypeImp (x, _, _, _, _, _, _) =>
adamc@156 1951 (if SS.member (cons, x) then
adamc@156 1952 sgnError env (DuplicateCon (loc, x))
adamc@156 1953 else
adamc@156 1954 ();
adamc@156 1955 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1956 | L'.SgiVal (x, _, _) =>
adamc@62 1957 (if SS.member (vals, x) then
adamc@62 1958 sgnError env (DuplicateVal (loc, x))
adamc@62 1959 else
adamc@62 1960 ();
adamc@62 1961 (cons, SS.add (vals, x), sgns, strs))
adamc@62 1962 | L'.SgiSgn (x, _, _) =>
adamc@62 1963 (if SS.member (sgns, x) then
adamc@62 1964 sgnError env (DuplicateSgn (loc, x))
adamc@62 1965 else
adamc@62 1966 ();
adamc@62 1967 (cons, vals, SS.add (sgns, x), strs))
adamc@62 1968 | L'.SgiStr (x, _, _) =>
adamc@62 1969 (if SS.member (strs, x) then
adamc@62 1970 sgnError env (DuplicateStr (loc, x))
adamc@62 1971 else
adamc@62 1972 ();
adamc@88 1973 (cons, vals, sgns, SS.add (strs, x)))
adamc@203 1974 | L'.SgiConstraint _ => (cons, vals, sgns, strs)
adamc@205 1975 | L'.SgiTable (_, x, _, _) =>
adamc@203 1976 (if SS.member (vals, x) then
adamc@203 1977 sgnError env (DuplicateVal (loc, x))
adamc@203 1978 else
adamc@203 1979 ();
adamc@203 1980 (cons, SS.add (vals, x), sgns, strs)))
adamc@62 1981 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 1982 in
adamc@83 1983 ((L'.SgnConst sgis', loc), gs)
adamc@31 1984 end
adamc@31 1985 | L.SgnVar x =>
adamc@31 1986 (case E.lookupSgn env x of
adamc@31 1987 NONE =>
adamc@31 1988 (sgnError env (UnboundSgn (loc, x));
adamc@83 1989 ((L'.SgnError, loc), []))
adamc@83 1990 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 1991 | L.SgnFun (m, dom, ran) =>
adamc@41 1992 let
adamc@83 1993 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1994 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1995 val (ran', gs2) = elabSgn (env', denv) ran
adamc@41 1996 in
adamc@83 1997 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 1998 end
adamc@42 1999 | L.SgnWhere (sgn, x, c) =>
adamc@42 2000 let
adamc@83 2001 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 2002 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 2003 in
adamc@42 2004 case #1 (hnormSgn env sgn') of
adamc@83 2005 L'.SgnError => (sgnerror, [])
adamc@42 2006 | L'.SgnConst sgis =>
adamc@75 2007 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 2008 x' = x andalso
adamc@75 2009 (unifyKinds k ck
adamc@75 2010 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 2011 true)
adamc@42 2012 | _ => false) sgis then
adamc@83 2013 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 2014 else
adamc@42 2015 (sgnError env (UnWhereable (sgn', x));
adamc@83 2016 (sgnerror, []))
adamc@42 2017 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 2018 (sgnerror, []))
adamc@42 2019 end
adamc@59 2020 | L.SgnProj (m, ms, x) =>
adamc@59 2021 (case E.lookupStr env m of
adamc@59 2022 NONE => (strError env (UnboundStr (loc, m));
adamc@83 2023 (sgnerror, []))
adamc@59 2024 | SOME (n, sgn) =>
adamc@59 2025 let
adamc@59 2026 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 2027 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 2028 NONE => (strError env (UnboundStr (loc, m));
adamc@59 2029 (strerror, sgnerror))
adamc@59 2030 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 2031 ((L'.StrVar n, loc), sgn) ms
adamc@59 2032 in
adamc@59 2033 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 2034 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 2035 (sgnerror, []))
adamc@83 2036 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 2037 end)
adamc@59 2038
adamc@31 2039
adamc@66 2040 fun selfify env {str, strs, sgn} =
adamc@66 2041 case #1 (hnormSgn env sgn) of
adamc@66 2042 L'.SgnError => sgn
adamc@66 2043 | L'.SgnVar _ => sgn
adamc@66 2044
adamc@66 2045 | L'.SgnConst sgis =>
adamc@66 2046 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 2047 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@191 2048 | (L'.SgiDatatype (x, n, xs, xncs), loc) =>
adamc@191 2049 (L'.SgiDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
adamc@66 2050 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 2051 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 2052 | x => x) sgis), #2 sgn)
adamc@66 2053 | L'.SgnFun _ => sgn
adamc@66 2054 | L'.SgnWhere _ => sgn
adamc@66 2055 | L'.SgnProj (m, ms, x) =>
adamc@66 2056 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2057 (L'.StrVar m, #2 sgn) ms,
adamc@66 2058 sgn = #2 (E.lookupStrNamed env m),
adamc@66 2059 field = x} of
adamc@66 2060 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 2061 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 2062
adamc@66 2063 fun selfifyAt env {str, sgn} =
adamc@66 2064 let
adamc@66 2065 fun self (str, _) =
adamc@66 2066 case str of
adamc@66 2067 L'.StrVar x => SOME (x, [])
adamc@66 2068 | L'.StrProj (str, x) =>
adamc@66 2069 (case self str of
adamc@66 2070 NONE => NONE
adamc@66 2071 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 2072 | _ => NONE
adamc@66 2073 in
adamc@66 2074 case self str of
adamc@66 2075 NONE => sgn
adamc@66 2076 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 2077 end
adamc@66 2078
adamc@88 2079 fun dopen (env, denv) {str, strs, sgn} =
adamc@66 2080 let
adamc@66 2081 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 2082 (L'.StrVar str, #2 sgn) strs
adamc@66 2083 in
adamc@66 2084 case #1 (hnormSgn env sgn) of
adamc@66 2085 L'.SgnConst sgis =>
adamc@88 2086 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
adamc@162 2087 let
adamc@162 2088 val d =
adamc@162 2089 case sgi of
adamc@162 2090 L'.SgiConAbs (x, n, k) =>
adamc@162 2091 let
adamc@162 2092 val c = (L'.CModProj (str, strs, x), loc)
adamc@162 2093 in
adamc@162 2094 (L'.DCon (x, n, k, c), loc)
adamc@162 2095 end
adamc@162 2096 | L'.SgiCon (x, n, k, c) =>
adamc@162 2097 (L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@191 2098 | L'.SgiDatatype (x, n, xs, xncs) =>
adamc@191 2099 (L'.DDatatypeImp (x, n, str, strs, x, xs, xncs), loc)
adamc@191 2100 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@191 2101 (L'.DDatatypeImp (x, n, m1, ms, x', xs, xncs), loc)
adamc@162 2102 | L'.SgiVal (x, n, t) =>
adamc@162 2103 (L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)
adamc@162 2104 | L'.SgiStr (x, n, sgn) =>
adamc@162 2105 (L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)
adamc@162 2106 | L'.SgiSgn (x, n, sgn) =>
adamc@162 2107 (L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)
adamc@162 2108 | L'.SgiConstraint (c1, c2) =>
adamc@162 2109 (L'.DConstraint (c1, c2), loc)
adamc@205 2110 | L'.SgiTable (_, x, n, c) =>
adamc@203 2111 (L'.DVal (x, n, (L'.CApp (tableOf env, c), loc),
adamc@203 2112 (L'.EModProj (str, strs, x), loc)), loc)
adamc@162 2113 in
adamc@162 2114 (d, (E.declBinds env' d, denv'))
adamc@162 2115 end)
adamc@88 2116 (env, denv) sgis
adamc@66 2117 | _ => (strError env (UnOpenable sgn);
adamc@88 2118 ([], (env, denv)))
adamc@66 2119 end
adamc@66 2120
adamc@88 2121 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@88 2122 case E.lookupStr env str of
adamc@88 2123 NONE => (strError env (UnboundStr (loc, str));
adamc@88 2124 denv)
adamc@88 2125 | SOME (n, sgn) =>
adamc@88 2126 let
adamc@88 2127 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 2128 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 2129 NONE => (strError env (UnboundStr (loc, m));
adamc@88 2130 (strerror, sgnerror))
adamc@88 2131 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 2132 ((L'.StrVar n, loc), sgn) strs
adamc@88 2133
adamc@88 2134 val cso = E.projectConstraints env {sgn = sgn, str = st}
adamc@88 2135
adamc@88 2136 val denv = case cso of
adamc@88 2137 NONE => (strError env (UnboundStr (loc, str));
adamc@88 2138 denv)
adamc@90 2139 | SOME cs => foldl (fn ((c1, c2), denv) =>
adamc@90 2140 let
adamc@90 2141 val (denv, gs) = D.assert env denv (c1, c2)
adamc@90 2142 in
adamc@90 2143 case gs of
adamc@90 2144 [] => ()
adamc@90 2145 | _ => raise Fail "dopenConstraints: Sub-constraints remain";
adamc@90 2146
adamc@90 2147 denv
adamc@90 2148 end) denv cs
adamc@88 2149 in
adamc@88 2150 denv
adamc@88 2151 end
adamc@88 2152
adamc@31 2153 fun sgiOfDecl (d, loc) =
adamc@31 2154 case d of
adamc@123 2155 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@156 2156 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
adamc@156 2157 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
adamc@123 2158 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 2159 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 2160 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 2161 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2162 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 2163 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 2164 | L'.DExport _ => []
adamc@205 2165 | L'.DTable (tn, x, n, c) => [(L'.SgiTable (tn, x, n, c), loc)]
adamc@88 2166
adamc@88 2167 fun sgiBindsD (env, denv) (sgi, _) =
adamc@88 2168 case sgi of
adamc@90 2169 L'.SgiConstraint (c1, c2) =>
adamc@90 2170 (case D.assert env denv (c1, c2) of
adamc@90 2171 (denv, []) => denv
adamc@90 2172 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
adamc@88 2173 | _ => denv
adamc@31 2174
adamc@86 2175 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
adamc@35 2176 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 2177 (L'.SgnError, _) => ()
adamc@31 2178 | (_, L'.SgnError) => ()
adamc@31 2179
adamc@31 2180 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 2181 let
adamc@156 2182 fun folder (sgi2All as (sgi, loc), (env, denv)) =
adamc@31 2183 let
adamc@31 2184 fun seek p =
adamc@31 2185 let
adamc@88 2186 fun seek (env, denv) ls =
adamc@31 2187 case ls of
adamc@31 2188 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@88 2189 (env, denv))
adamc@31 2190 | h :: t =>
adamc@31 2191 case p h of
adamc@88 2192 NONE => seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
adamc@88 2193 | SOME envs => envs
adamc@31 2194 in
adamc@88 2195 seek (env, denv) sgis1
adamc@31 2196 end
adamc@31 2197 in
adamc@31 2198 case sgi of
adamc@31 2199 L'.SgiConAbs (x, n2, k2) =>
adamc@31 2200 seek (fn sgi1All as (sgi1, _) =>
adamc@31 2201 let
adamc@41 2202 fun found (x', n1, k1, co1) =
adamc@41 2203 if x = x' then
adamc@41 2204 let
adamc@41 2205 val () = unifyKinds k1 k2
adamc@41 2206 handle KUnify (k1, k2, err) =>
adamc@41 2207 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 2208 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 2209 in
adamc@41 2210 SOME (if n1 = n2 then
adamc@41 2211 env
adamc@41 2212 else
adamc@88 2213 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
adamc@88 2214 denv)
adamc@41 2215 end
adamc@41 2216 else
adamc@41 2217 NONE
adamc@31 2218 in
adamc@31 2219 case sgi1 of
adamc@41 2220 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 2221 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@191 2222 | L'.SgiDatatype (x', n1, xs, _) =>
adamc@191 2223 let
adamc@191 2224 val k = (L'.KType, loc)
adamc@191 2225 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2226 in
adamc@191 2227 found (x', n1, k', NONE)
adamc@191 2228 end
adamc@191 2229 | L'.SgiDatatypeImp (x', n1, m1, ms, s, xs, _) =>
adamc@191 2230 let
adamc@191 2231 val k = (L'.KType, loc)
adamc@191 2232 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2233 in
adamc@191 2234 found (x', n1, k', SOME (L'.CModProj (m1, ms, s), loc))
adamc@191 2235 end
adamc@31 2236 | _ => NONE
adamc@31 2237 end)
adamc@31 2238
adamc@31 2239 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 2240 seek (fn sgi1All as (sgi1, _) =>
adamc@31 2241 case sgi1 of
adamc@41 2242 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 2243 if x = x' then
adamc@41 2244 let
adamc@88 2245 fun good () = SOME (E.pushCNamedAs env x n2 k2 (SOME c2), denv)
adamc@41 2246 in
adamc@86 2247 (case unifyCons (env, denv) c1 c2 of
adamc@86 2248 [] => good ()
adamc@86 2249 | _ => NONE)
adamc@86 2250 handle CUnify (c1, c2, err) =>
adamc@86 2251 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@86 2252 good ())
adamc@41 2253 end
adamc@41 2254 else
adamc@41 2255 NONE
adamc@31 2256 | _ => NONE)
adamc@31 2257
adamc@191 2258 | L'.SgiDatatype (x, n2, xs2, xncs2) =>
adamc@156 2259 seek (fn sgi1All as (sgi1, _) =>
adamc@160 2260 let
adamc@191 2261 fun found (n1, xs1, xncs1) =
adamc@160 2262 let
adamc@160 2263 fun mismatched ue =
adamc@160 2264 (sgnError env (SgiMismatchedDatatypes (sgi1All, sgi2All, ue));
adamc@160 2265 SOME (env, denv))
adamc@156 2266
adamc@191 2267 val k = (L'.KType, loc)
adamc@191 2268 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs1
adamc@191 2269
adamc@160 2270 fun good () =
adamc@160 2271 let
adamc@160 2272 val env = E.sgiBinds env sgi2All
adamc@160 2273 val env = if n1 = n2 then
adamc@160 2274 env
adamc@160 2275 else
adamc@160 2276 E.pushCNamedAs env x n1 (L'.KType, loc)
adamc@160 2277 (SOME (L'.CNamed n1, loc))
adamc@160 2278 in
adamc@160 2279 SOME (env, denv)
adamc@160 2280 end
adamc@156 2281
adamc@191 2282 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs1
adamc@160 2283 fun xncBad ((x1, _, t1), (x2, _, t2)) =
adamc@160 2284 String.compare (x1, x2) <> EQUAL
adamc@160 2285 orelse case (t1, t2) of
adamc@160 2286 (NONE, NONE) => false
adamc@160 2287 | (SOME t1, SOME t2) =>
adamc@160 2288 not (List.null (unifyCons (env, denv) t1 t2))
adamc@160 2289 | _ => true
adamc@160 2290 in
adamc@191 2291 (if xs1 <> xs2
adamc@191 2292 orelse length xncs1 <> length xncs2
adamc@160 2293 orelse ListPair.exists xncBad (xncs1, xncs2) then
adamc@160 2294 mismatched NONE
adamc@160 2295 else
adamc@160 2296 good ())
adamc@160 2297 handle CUnify ue => mismatched (SOME ue)
adamc@160 2298 end
adamc@160 2299 in
adamc@160 2300 case sgi1 of
adamc@191 2301 L'.SgiDatatype (x', n1, xs, xncs1) =>
adamc@160 2302 if x' = x then
adamc@191 2303 found (n1, xs, xncs1)
adamc@160 2304 else
adamc@160 2305 NONE
adamc@191 2306 | L'.SgiDatatypeImp (x', n1, _, _, _, xs, xncs1) =>
adamc@162 2307 if x' = x then
adamc@191 2308 found (n1, xs, xncs1)
adamc@162 2309 else
adamc@162 2310 NONE
adamc@160 2311 | _ => NONE
adamc@160 2312 end)
adamc@156 2313
adamc@191 2314 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, xs, _) =>
adamc@158 2315 seek (fn sgi1All as (sgi1, _) =>
adamc@158 2316 case sgi1 of
adamc@191 2317 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _, _) =>
adamc@158 2318 if x = x' then
adamc@158 2319 let
adamc@158 2320 val k = (L'.KType, loc)
adamc@191 2321 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@158 2322 val t1 = (L'.CModProj (m11, ms1, s1), loc)
adamc@158 2323 val t2 = (L'.CModProj (m12, ms2, s2), loc)
adamc@158 2324
adamc@158 2325 fun good () =
adamc@158 2326 let
adamc@191 2327 val env = E.pushCNamedAs env x n1 k' (SOME t1)
adamc@191 2328 val env = E.pushCNamedAs env x n2 k' (SOME t2)
adamc@158 2329 in
adamc@158 2330 SOME (env, denv)
adamc@158 2331 end
adamc@158 2332 in
adamc@158 2333 (case unifyCons (env, denv) t1 t2 of
adamc@158 2334 [] => good ()
adamc@158 2335 | _ => NONE)
adamc@158 2336 handle CUnify (c1, c2, err) =>
adamc@158 2337 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@158 2338 good ())
adamc@158 2339 end
adamc@158 2340 else
adamc@158 2341 NONE
adamc@158 2342
adamc@158 2343 | _ => NONE)
adamc@156 2344
adamc@32 2345 | L'.SgiVal (x, n2, c2) =>
adamc@32 2346 seek (fn sgi1All as (sgi1, _) =>
adamc@32 2347 case sgi1 of
adamc@41 2348 L'.SgiVal (x', n1, c1) =>
adamc@41 2349 if x = x' then
adamc@86 2350 (case unifyCons (env, denv) c1 c2 of
adamc@88 2351 [] => SOME (env, denv)
adamc@86 2352 | _ => NONE)
adamc@86 2353 handle CUnify (c1, c2, err) =>
adamc@86 2354 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@88 2355 SOME (env, denv))
adamc@41 2356 else
adamc@41 2357 NONE
adamc@205 2358 | L'.SgiTable (_, x', n1, c1) =>
adamc@203 2359 if x = x' then
adamc@203 2360 (case unifyCons (env, denv) (L'.CApp (tableOf env, c1), loc) c2 of
adamc@203 2361 [] => SOME (env, denv)
adamc@203 2362 | _ => NONE)
adamc@203 2363 handle CUnify (c1, c2, err) =>
adamc@203 2364 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@203 2365 SOME (env, denv))
adamc@203 2366 else
adamc@203 2367 NONE
adamc@32 2368 | _ => NONE)
adamc@32 2369
adamc@33 2370 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 2371 seek (fn sgi1All as (sgi1, _) =>
adamc@33 2372 case sgi1 of
adamc@41 2373 L'.SgiStr (x', n1, sgn1) =>
adamc@41 2374 if x = x' then
adamc@66 2375 let
adamc@86 2376 val () = subSgn (env, denv) sgn1 sgn2
adamc@66 2377 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 2378 val env = if n1 = n2 then
adamc@66 2379 env
adamc@66 2380 else
adamc@66 2381 E.pushStrNamedAs env x n2
adamc@66 2382 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 2383 sgn = sgn2})
adamc@66 2384 in
adamc@88 2385 SOME (env, denv)
adamc@66 2386 end
adamc@41 2387 else
adamc@41 2388 NONE
adamc@33 2389 | _ => NONE)
adamc@59 2390
adamc@59 2391 | L'.SgiSgn (x, n2, sgn2) =>
adamc@59 2392 seek (fn sgi1All as (sgi1, _) =>
adamc@59 2393 case sgi1 of
adamc@59 2394 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 2395 if x = x' then
adamc@65 2396 let
adamc@86 2397 val () = subSgn (env, denv) sgn1 sgn2
adamc@86 2398 val () = subSgn (env, denv) sgn2 sgn1
adamc@65 2399
adamc@65 2400 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 2401 val env = if n1 = n2 then
adamc@65 2402 env
adamc@65 2403 else
adamc@65 2404 E.pushSgnNamedAs env x n1 sgn2
adamc@65 2405 in
adamc@88 2406 SOME (env, denv)
adamc@65 2407 end
adamc@59 2408 else
adamc@59 2409 NONE
adamc@59 2410 | _ => NONE)
adamc@88 2411
adamc@88 2412 | L'.SgiConstraint (c2, d2) =>
adamc@88 2413 seek (fn sgi1All as (sgi1, _) =>
adamc@88 2414 case sgi1 of
adamc@88 2415 L'.SgiConstraint (c1, d1) =>
adamc@88 2416 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
adamc@90 2417 let
adamc@90 2418 val (denv, gs) = D.assert env denv (c2, d2)
adamc@90 2419 in
adamc@90 2420 case gs of
adamc@90 2421 [] => ()
adamc@90 2422 | _ => raise Fail "subSgn: Sub-constraints remain";
adamc@90 2423
adamc@90 2424 SOME (env, denv)
adamc@90 2425 end
adamc@88 2426 else
adamc@88 2427 NONE
adamc@88 2428 | _ => NONE)
adamc@203 2429
adamc@205 2430 | L'.SgiTable (_, x, n2, c2) =>
adamc@203 2431 seek (fn sgi1All as (sgi1, _) =>
adamc@203 2432 case sgi1 of
adamc@205 2433 L'.SgiTable (_, x', n1, c1) =>
adamc@203 2434 if x = x' then
adamc@203 2435 (case unifyCons (env, denv) c1 c2 of
adamc@203 2436 [] => SOME (env, denv)
adamc@203 2437 | _ => NONE)
adamc@203 2438 handle CUnify (c1, c2, err) =>
adamc@203 2439 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@203 2440 SOME (env, denv))
adamc@203 2441 else
adamc@203 2442 NONE
adamc@203 2443 | _ => NONE)
adamc@31 2444 end
adamc@31 2445 in
adamc@88 2446 ignore (foldl folder (env, denv) sgis2)
adamc@31 2447 end
adamc@31 2448
adamc@41 2449 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 2450 let
adamc@43 2451 val ran1 =
adamc@43 2452 if n1 = n2 then
adamc@43 2453 ran1
adamc@43 2454 else
adamc@43 2455 subStrInSgn (n1, n2) ran1
adamc@43 2456 in
adamc@86 2457 subSgn (env, denv) dom2 dom1;
adamc@86 2458 subSgn (E.pushStrNamedAs env m2 n2 dom2, denv) ran1 ran2
adamc@43 2459 end
adamc@41 2460
adamc@41 2461 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 2462
adamc@61 2463
adamc@88 2464 fun elabDecl ((d, loc), (env, denv, gs)) =
adamc@76 2465 case d of
adamc@76 2466 L.DCon (x, ko, c) =>
adamc@76 2467 let
adamc@76 2468 val k' = case ko of
adamc@76 2469 NONE => kunif loc
adamc@76 2470 | SOME k => elabKind k
adamc@76 2471
adamc@83 2472 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 2473 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 2474 in
adamc@76 2475 checkKind env c' ck k';
adamc@76 2476
adamc@88 2477 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 2478 end
adamc@191 2479 | L.DDatatype (x, xs, xcs) =>
adamc@156 2480 let
adamc@156 2481 val k = (L'.KType, loc)
adamc@191 2482 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@191 2483 val (env, n) = E.pushCNamed env x k' NONE
adamc@156 2484 val t = (L'.CNamed n, loc)
adamc@194 2485 val nxs = length xs - 1
adamc@194 2486 val t = ListUtil.foldli (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@191 2487
adamc@191 2488 val (env', denv') = foldl (fn (x, (env', denv')) =>
adamc@191 2489 (E.pushCRel env' x k,
adamc@191 2490 D.enter denv')) (env, denv) xs
adamc@156 2491
adamc@156 2492 val (xcs, (used, env, gs)) =
adamc@156 2493 ListUtil.foldlMap
adamc@156 2494 (fn ((x, to), (used, env, gs)) =>
adamc@156 2495 let
adamc@156 2496 val (to, t, gs') = case to of
adamc@156 2497 NONE => (NONE, t, gs)
adamc@156 2498 | SOME t' =>
adamc@156 2499 let
adamc@191 2500 val (t', tk, gs') = elabCon (env', denv') t'
adamc@156 2501 in
adamc@191 2502 checkKind env' t' tk k;
adamc@156 2503 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@156 2504 end
adamc@191 2505 val t = foldr (fn (x, t) => (L'.TCFun (L'.Implicit, x, k, t), loc)) t xs
adamc@156 2506
adamc@156 2507 val (env, n') = E.pushENamed env x t
adamc@156 2508 in
adamc@156 2509 if SS.member (used, x) then
adamc@156 2510 strError env (DuplicateConstructor (x, loc))
adamc@156 2511 else
adamc@156 2512 ();
adamc@156 2513 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@156 2514 end)
adamc@156 2515 (SS.empty, env, []) xcs
adamc@171 2516
adamc@191 2517 val env = E.pushDatatype env n xs xcs
adamc@156 2518 in
adamc@191 2519 ([(L'.DDatatype (x, n, xs, xcs), loc)], (env, denv, gs))
adamc@156 2520 end
adamc@156 2521
adamc@157 2522 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
adamc@157 2523
adamc@157 2524 | L.DDatatypeImp (x, m1 :: ms, s) =>
adamc@157 2525 (case E.lookupStr env m1 of
adamc@157 2526 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@157 2527 ([], (env, denv, gs)))
adamc@157 2528 | SOME (n, sgn) =>
adamc@157 2529 let
adamc@157 2530 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@157 2531 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@157 2532 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@157 2533 (strerror, sgnerror))
adamc@157 2534 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@157 2535 ((L'.StrVar n, loc), sgn) ms
adamc@157 2536 in
adamc@158 2537 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@158 2538 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@158 2539 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@158 2540 NONE => (conError env (UnboundDatatype (loc, s));
adamc@158 2541 ([], (env, denv, gs)))
adamc@191 2542 | SOME (xs, xncs) =>
adamc@158 2543 let
adamc@158 2544 val k = (L'.KType, loc)
adamc@191 2545 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@158 2546 val t = (L'.CModProj (n, ms, s), loc)
adamc@191 2547 val (env, n') = E.pushCNamed env x k' (SOME t)
adamc@191 2548 val env = E.pushDatatype env n' xs xncs
adamc@157 2549
adamc@158 2550 val t = (L'.CNamed n', loc)
adamc@158 2551 val env = foldl (fn ((x, n, to), env) =>
adamc@158 2552 let
adamc@158 2553 val t = case to of
adamc@158 2554 NONE => t
adamc@158 2555 | SOME t' => (L'.TFun (t', t), loc)
adamc@191 2556
adamc@191 2557 val t = foldr (fn (x, t) =>
adamc@191 2558 (L'.TCFun (L'.Implicit, x, k, t), loc))
adamc@191 2559 t xs
adamc@158 2560 in
adamc@158 2561 E.pushENamedAs env x n t
adamc@158 2562 end) env xncs
adamc@158 2563 in
adamc@191 2564 ([(L'.DDatatypeImp (x, n', n, ms, s, xs, xncs), loc)], (env, denv, gs))
adamc@158 2565 end)
adamc@158 2566 | _ => (strError env (NotDatatype loc);
adamc@158 2567 ([], (env, denv, [])))
adamc@157 2568 end)
adamc@157 2569
adamc@76 2570 | L.DVal (x, co, e) =>
adamc@76 2571 let
adamc@123 2572 val (c', _, gs1) = case co of
adamc@83 2573 NONE => (cunif (loc, ktype), ktype, [])
adamc@83 2574 | SOME c => elabCon (env, denv) c
adamc@76 2575
adamc@83 2576 val (e', et, gs2) = elabExp (env, denv) e
adamc@76 2577 val (env', n) = E.pushENamed env x c'
adamc@86 2578
adamc@86 2579 val gs3 = checkCon (env, denv) e' et c'
adamc@76 2580 in
adamc@88 2581 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, gs1 @ gs2 @ gs3 @ gs))
adamc@76 2582 end
adamc@123 2583 | L.DValRec vis =>
adamc@123 2584 let
adamc@123 2585 val (vis, gs) = ListUtil.foldlMap
adamc@123 2586 (fn ((x, co, e), gs) =>
adamc@123 2587 let
adamc@123 2588 val (c', _, gs1) = case co of
adamc@123 2589 NONE => (cunif (loc, ktype), ktype, [])
adamc@123 2590 | SOME c => elabCon (env, denv) c
adamc@123 2591 in
adamc@123 2592 ((x, c', e), gs1 @ gs)
adamc@123 2593 end) [] vis
adamc@123 2594
adamc@123 2595 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@123 2596 let
adamc@123 2597 val (env, n) = E.pushENamed env x c'
adamc@123 2598 in
adamc@123 2599 ((x, n, c', e), env)
adamc@123 2600 end) env vis
adamc@123 2601
adamc@123 2602 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@123 2603 let
adamc@123 2604 val (e', et, gs1) = elabExp (env, denv) e
adamc@123 2605
adamc@123 2606 val gs2 = checkCon (env, denv) e' et c'
adamc@123 2607 in
adamc@123 2608 ((x, n, c', e'), gs1 @ gs2 @ gs)
adamc@123 2609 end) gs vis
adamc@123 2610 in
adamc@123 2611 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@123 2612 end
adamc@76 2613
adamc@76 2614 | L.DSgn (x, sgn) =>
adamc@76 2615 let
adamc@83 2616 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2617 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 2618 in
adamc@88 2619 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2620 end
adamc@76 2621
adamc@76 2622 | L.DStr (x, sgno, str) =>
adamc@76 2623 let
adamc@93 2624 val () = if x = "Basis" then
adamc@93 2625 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@93 2626 else
adamc@93 2627 ()
adamc@93 2628
adamc@83 2629 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@76 2630
adamc@83 2631 val (str', sgn', gs') =
adamc@78 2632 case formal of
adamc@78 2633 NONE =>
adamc@78 2634 let
adamc@83 2635 val (str', actual, ds) = elabStr (env, denv) str
adamc@78 2636 in
adamc@83 2637 (str', selfifyAt env {str = str', sgn = actual}, ds)
adamc@78 2638 end
adamc@83 2639 | SOME (formal, gs1) =>
adamc@78 2640 let
adamc@78 2641 val str =
adamc@78 2642 case #1 (hnormSgn env formal) of
adamc@78 2643 L'.SgnConst sgis =>
adamc@78 2644 (case #1 str of
adamc@78 2645 L.StrConst ds =>
adamc@78 2646 let
adamc@78 2647 val needed = foldl (fn ((sgi, _), needed) =>
adamc@78 2648 case sgi of
adamc@78 2649 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
adamc@78 2650 | _ => needed)
adamc@78 2651 SS.empty sgis
adamc@78 2652
adamc@78 2653 val needed = foldl (fn ((d, _), needed) =>
adamc@78 2654 case d of
adamc@78 2655 L.DCon (x, _, _) => (SS.delete (needed, x)
adamc@78 2656 handle NotFound => needed)
adamc@78 2657 | L.DOpen _ => SS.empty
adamc@78 2658 | _ => needed)
adamc@78 2659 needed ds
adamc@78 2660 in
adamc@78 2661 case SS.listItems needed of
adamc@78 2662 [] => str
adamc@78 2663 | xs =>
adamc@78 2664 let
adamc@78 2665 val kwild = (L.KWild, #2 str)
adamc@78 2666 val cwild = (L.CWild kwild, #2 str)
adamc@78 2667 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
adamc@78 2668 in
adamc@78 2669 (L.StrConst (ds @ ds'), #2 str)
adamc@78 2670 end
adamc@78 2671 end
adamc@78 2672 | _ => str)
adamc@78 2673 | _ => str
adamc@78 2674
adamc@83 2675 val (str', actual, gs2) = elabStr (env, denv) str
adamc@78 2676 in
adamc@158 2677 subSgn (env, denv) (selfifyAt env {str = str', sgn = actual}) formal;
adamc@83 2678 (str', formal, gs1 @ gs2)
adamc@78 2679 end
adamc@76 2680
adamc@76 2681 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2682 in
adamc@76 2683 case #1 (hnormSgn env sgn') of
adamc@76 2684 L'.SgnFun _ =>
adamc@76 2685 (case #1 str' of
adamc@76 2686 L'.StrFun _ => ()
adamc@76 2687 | _ => strError env (FunctorRebind loc))
adamc@76 2688 | _ => ();
adamc@76 2689
adamc@88 2690 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
adamc@76 2691 end
adamc@76 2692
adamc@76 2693 | L.DFfiStr (x, sgn) =>
adamc@76 2694 let
adamc@83 2695 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2696
adamc@76 2697 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2698 in
adamc@88 2699 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2700 end
adamc@76 2701
adamc@76 2702 | L.DOpen (m, ms) =>
adamc@88 2703 (case E.lookupStr env m of
adamc@88 2704 NONE => (strError env (UnboundStr (loc, m));
adamc@205 2705 ([], (env, denv, gs)))
adamc@88 2706 | SOME (n, sgn) =>
adamc@88 2707 let
adamc@88 2708 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 2709 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 2710 NONE => (strError env (UnboundStr (loc, m));
adamc@88 2711 (strerror, sgnerror))
adamc@88 2712 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 2713 ((L'.StrVar n, loc), sgn) ms
adamc@83 2714
adamc@88 2715 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
adamc@88 2716 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
adamc@88 2717 in
adamc@205 2718 (ds, (env', denv', gs))
adamc@88 2719 end)
adamc@88 2720
adamc@88 2721 | L.DConstraint (c1, c2) =>
adamc@88 2722 let
adamc@88 2723 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 2724 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@90 2725 val gs3 = D.prove env denv (c1', c2', loc)
adamc@88 2726
adamc@90 2727 val (denv', gs4) = D.assert env denv (c1', c2')
adamc@88 2728 in
adamc@88 2729 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 2730 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 2731
adamc@205 2732 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', gs1 @ gs2 @ gs3 @ gs4 @ gs))
adamc@88 2733 end
adamc@88 2734
adamc@88 2735 | L.DOpenConstraints (m, ms) =>
adamc@88 2736 let
adamc@88 2737 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@88 2738 in
adamc@205 2739 ([], (env, denv, gs))
adamc@88 2740 end
adamc@3 2741
adamc@109 2742 | L.DExport str =>
adamc@100 2743 let
adamc@205 2744 val (str', sgn, gs') = elabStr (env, denv) str
adamc@100 2745
adamc@109 2746 val sgn =
adamc@109 2747 case #1 (hnormSgn env sgn) of
adamc@109 2748 L'.SgnConst sgis =>
adamc@109 2749 let
adamc@146 2750 fun doOne (all as (sgi, _), env) =
adamc@146 2751 (case sgi of
adamc@146 2752 L'.SgiVal (x, n, t) =>
adamc@146 2753 (case hnormCon (env, denv) t of
adamc@146 2754 ((L'.TFun (dom, ran), _), []) =>
adamc@146 2755 (case (hnormCon (env, denv) dom, hnormCon (env, denv) ran) of
adamc@146 2756 (((L'.TRecord domR, _), []),
adamc@146 2757 ((L'.CApp (tf, arg3), _), [])) =>
adamc@146 2758 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
adamc@146 2759 (((L'.CApp (tf, arg2), _), []),
adamc@146 2760 (((L'.CRecord (_, []), _), []))) =>
adamc@146 2761 (case (hnormCon (env, denv) tf) of
adamc@146 2762 ((L'.CApp (tf, arg1), _), []) =>
adamc@146 2763 (case (hnormCon (env, denv) tf,
adamc@146 2764 hnormCon (env, denv) domR,
adamc@152 2765 hnormCon (env, denv) arg1,
adamc@146 2766 hnormCon (env, denv) arg2) of
adamc@152 2767 ((tf, []), (domR, []), (arg1, []),
adamc@146 2768 ((L'.CRecord (_, []), _), [])) =>
adamc@146 2769 let
adamc@146 2770 val t = (L'.CApp (tf, arg1), loc)
adamc@146 2771 val t = (L'.CApp (t, arg2), loc)
adamc@146 2772 val t = (L'.CApp (t, arg3), loc)
adamc@146 2773 in
adamc@146 2774 (L'.SgiVal (x, n, (L'.TFun ((L'.TRecord domR, loc),
adamc@146 2775 t),
adamc@146 2776 loc)), loc)
adamc@146 2777 end
adamc@146 2778 | _ => all)
adamc@146 2779 | _ => all)
adamc@146 2780 | _ => all)
adamc@146 2781 | _ => all)
adamc@146 2782 | _ => all)
adamc@146 2783 | _ => all,
adamc@146 2784 E.sgiBinds env all)
adamc@109 2785 in
adamc@146 2786 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@109 2787 end
adamc@109 2788 | _ => sgn
adamc@100 2789 in
adamc@205 2790 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs' @ gs))
adamc@100 2791 end
adamc@100 2792
adamc@203 2793 | L.DTable (x, c) =>
adamc@203 2794 let
adamc@205 2795 val (c', k, gs') = elabCon (env, denv) c
adamc@205 2796 val (env, n) = E.pushENamed env x (L'.CApp (tableOf env, c'), loc)
adamc@203 2797 in
adamc@203 2798 checkKind env c' k (L'.KRecord (L'.KType, loc), loc);
adamc@205 2799 ([(L'.DTable (tableOf' env, x, n, c'), loc)], (env, denv, gs' @ gs))
adamc@203 2800 end
adamc@203 2801
adamc@83 2802 and elabStr (env, denv) (str, loc) =
adamc@31 2803 case str of
adamc@31 2804 L.StrConst ds =>
adamc@31 2805 let
adamc@88 2806 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 2807 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 2808
adamc@63 2809 val (sgis, _, _, _, _) =
adamc@64 2810 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 2811 case sgi of
adamc@64 2812 L'.SgiConAbs (x, n, k) =>
adamc@64 2813 let
adamc@64 2814 val (cons, x) =
adamc@64 2815 if SS.member (cons, x) then
adamc@64 2816 (cons, "?" ^ x)
adamc@64 2817 else
adamc@64 2818 (SS.add (cons, x), x)
adamc@64 2819 in
adamc@64 2820 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2821 end
adamc@64 2822 | L'.SgiCon (x, n, k, c) =>
adamc@64 2823 let
adamc@64 2824 val (cons, x) =
adamc@64 2825 if SS.member (cons, x) then
adamc@64 2826 (cons, "?" ^ x)
adamc@64 2827 else
adamc@64 2828 (SS.add (cons, x), x)
adamc@64 2829 in
adamc@64 2830 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2831 end
adamc@191 2832 | L'.SgiDatatype (x, n, xs, xncs) =>
adamc@156 2833 let
adamc@156 2834 val (cons, x) =
adamc@156 2835 if SS.member (cons, x) then
adamc@156 2836 (cons, "?" ^ x)
adamc@156 2837 else
adamc@156 2838 (SS.add (cons, x), x)
adamc@156 2839
adamc@156 2840 val (xncs, vals) =
adamc@156 2841 ListUtil.foldlMap
adamc@156 2842 (fn ((x, n, t), vals) =>
adamc@156 2843 if SS.member (vals, x) then
adamc@156 2844 (("?" ^ x, n, t), vals)
adamc@156 2845 else
adamc@156 2846 ((x, n, t), SS.add (vals, x)))
adamc@156 2847 vals xncs
adamc@156 2848 in
adamc@191 2849 ((L'.SgiDatatype (x, n, xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 2850 end
adamc@191 2851 | L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@156 2852 let
adamc@156 2853 val (cons, x) =
adamc@156 2854 if SS.member (cons, x) then
adamc@156 2855 (cons, "?" ^ x)
adamc@156 2856 else
adamc@156 2857 (SS.add (cons, x), x)
adamc@156 2858 in
adamc@191 2859 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xs, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 2860 end
adamc@64 2861 | L'.SgiVal (x, n, c) =>
adamc@64 2862 let
adamc@64 2863 val (vals, x) =
adamc@64 2864 if SS.member (vals, x) then
adamc@64 2865 (vals, "?" ^ x)
adamc@64 2866 else
adamc@64 2867 (SS.add (vals, x), x)
adamc@64 2868 in
adamc@64 2869 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2870 end
adamc@64 2871 | L'.SgiSgn (x, n, sgn) =>
adamc@64 2872 let
adamc@64 2873 val (sgns, x) =
adamc@64 2874 if SS.member (sgns, x) then
adamc@64 2875 (sgns, "?" ^ x)
adamc@64 2876 else
adamc@64 2877 (SS.add (sgns, x), x)
adamc@64 2878 in
adamc@64 2879 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2880 end
adamc@64 2881
adamc@64 2882 | L'.SgiStr (x, n, sgn) =>
adamc@64 2883 let
adamc@64 2884 val (strs, x) =
adamc@64 2885 if SS.member (strs, x) then
adamc@64 2886 (strs, "?" ^ x)
adamc@64 2887 else
adamc@64 2888 (SS.add (strs, x), x)
adamc@64 2889 in
adamc@64 2890 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 2891 end
adamc@203 2892 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs)
adamc@205 2893 | L'.SgiTable (tn, x, n, c) =>
adamc@203 2894 let
adamc@203 2895 val (vals, x) =
adamc@203 2896 if SS.member (vals, x) then
adamc@203 2897 (vals, "?" ^ x)
adamc@203 2898 else
adamc@203 2899 (SS.add (vals, x), x)
adamc@203 2900 in
adamc@205 2901 ((L'.SgiTable (tn, x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@203 2902 end)
adamc@64 2903
adamc@63 2904 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 2905 in
adamc@83 2906 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 2907 end
adamc@31 2908 | L.StrVar x =>
adamc@31 2909 (case E.lookupStr env x of
adamc@31 2910 NONE =>
adamc@31 2911 (strError env (UnboundStr (loc, x));
adamc@83 2912 (strerror, sgnerror, []))
adamc@83 2913 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 2914 | L.StrProj (str, x) =>
adamc@34 2915 let
adamc@83 2916 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 2917 in
adamc@34 2918 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 2919 NONE => (strError env (UnboundStr (loc, x));
adamc@83 2920 (strerror, sgnerror, []))
adamc@83 2921 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 2922 end
adamc@41 2923 | L.StrFun (m, dom, ranO, str) =>
adamc@41 2924 let
adamc@83 2925 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2926 val (env', n) = E.pushStrNamed env m dom'
adamc@83 2927 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 2928
adamc@83 2929 val (formal, gs3) =
adamc@41 2930 case ranO of
adamc@83 2931 NONE => (actual, [])
adamc@41 2932 | SOME ran =>
adamc@41 2933 let
adamc@83 2934 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 2935 in
adamc@86 2936 subSgn (env', denv) actual ran';
adamc@83 2937 (ran', gs)
adamc@41 2938 end
adamc@41 2939 in
adamc@41 2940 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 2941 (L'.SgnFun (m, n, dom', formal), loc),
adamc@83 2942 gs1 @ gs2 @ gs3)
adamc@41 2943 end
adamc@44 2944 | L.StrApp (str1, str2) =>
adamc@44 2945 let
adamc@83 2946 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@83 2947 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 2948 in
adamc@44 2949 case #1 (hnormSgn env sgn1) of
adamc@83 2950 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2951 | L'.SgnFun (m, n, dom, ran) =>
adamc@86 2952 (subSgn (env, denv) sgn2 dom;
adamc@44 2953 case #1 (hnormSgn env ran) of
adamc@83 2954 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2955 | L'.SgnConst sgis =>
adamc@44 2956 ((L'.StrApp (str1', str2'), loc),
adamc@83 2957 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 2958 gs1 @ gs2)
adamc@44 2959 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 2960 | _ => (strError env (NotFunctor sgn1);
adamc@83 2961 (strerror, sgnerror, []))
adamc@44 2962 end
adamc@31 2963
adamc@56 2964 fun elabFile basis env file =
adamc@56 2965 let
adamc@83 2966 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 2967 val () = case gs of
adamc@83 2968 [] => ()
adamc@207 2969 | _ => (app (fn (_, env, _, c1, c2) =>
adamc@207 2970 prefaces "Unresolved"
adamc@207 2971 [("c1", p_con env c1),
adamc@207 2972 ("c2", p_con env c2)]) gs;
adamc@207 2973 raise Fail "Unresolved disjointness constraints in Basis")
adamc@83 2974
adamc@56 2975 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@56 2976
adamc@88 2977 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
adamc@56 2978
adamc@56 2979 fun discoverC r x =
adamc@56 2980 case E.lookupC env' x of
adamc@56 2981 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 2982 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 2983 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 2984
adamc@56 2985 val () = discoverC int "int"
adamc@56 2986 val () = discoverC float "float"
adamc@56 2987 val () = discoverC string "string"
adamc@203 2988 val () = discoverC table "sql_table"
adamc@56 2989
adamc@83 2990 fun elabDecl' (d, (env, gs)) =
adamc@76 2991 let
adamc@76 2992 val () = resetKunif ()
adamc@76 2993 val () = resetCunif ()
adamc@88 2994 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 2995 in
adamc@76 2996 if ErrorMsg.anyErrors () then
adamc@76 2997 ()
adamc@76 2998 else (
adamc@76 2999 if List.exists kunifsInDecl ds then
adamc@91 3000 declError env (KunifsRemain ds)
adamc@76 3001 else
adamc@76 3002 ();
adamc@76 3003
adamc@76 3004 case ListUtil.search cunifsInDecl ds of
adamc@76 3005 NONE => ()
adamc@76 3006 | SOME loc =>
adamc@91 3007 declError env (CunifsRemain ds)
adamc@76 3008 );
adamc@76 3009
adamc@83 3010 (ds, (env, gs))
adamc@76 3011 end
adamc@76 3012
adamc@83 3013 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 3014 in
adamc@84 3015 if ErrorMsg.anyErrors () then
adamc@84 3016 ()
adamc@84 3017 else
adamc@90 3018 app (fn (loc, env, denv, c1, c2) =>
adamc@84 3019 case D.prove env denv (c1, c2, loc) of
adamc@84 3020 [] => ()
adamc@84 3021 | _ =>
adamc@86 3022 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adamc@84 3023 eprefaces' [("Con 1", p_con env c1),
adamc@90 3024 ("Con 2", p_con env c2),
adamc@90 3025 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
adamc@90 3026 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))])) gs;
adamc@83 3027
adamc@56 3028 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
adamc@56 3029 end
adamc@2 3030
adamc@2 3031 end