annotate src/elaborate.sml @ 191:aa54250f58ac

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