annotate src/elaborate.sml @ 203:dd82457fda82

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