annotate src/elaborate.sml @ 206:cb8493759a7b

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