annotate src/elaborate.sml @ 172:021f5beb6f8d

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