annotate src/elaborate.sml @ 209:1487c712eb12

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