annotate src/elaborate.sml @ 173:8221b95cc24c

Patterns for int and string constants
author Adam Chlipala <adamc@hcoop.net>
date Thu, 31 Jul 2008 10:44:52 -0400
parents 021f5beb6f8d
children 7ee424760d2f
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@173 999 | L.PPrim p => (((L'.PPrim p, loc), primType env p),
adamc@173 1000 (env, bound))
adamc@171 1001 | L.PCon ([], x, po) =>
adamc@171 1002 (case E.lookupConstructor env x of
adamc@171 1003 NONE => (expError env (UnboundConstructor (loc, x));
adamc@171 1004 rerror)
adamc@171 1005 | SOME (n, to, dn) => pcon (L'.PConVar n, po, to, dn))
adamc@171 1006 | L.PCon _ => raise Fail "uhoh"
adamc@171 1007 end
adamc@171 1008
adamc@172 1009 datatype coverage =
adamc@172 1010 Wild
adamc@173 1011 | None
adamc@172 1012 | Datatype of coverage IM.map
adamc@172 1013
adamc@172 1014 fun exhaustive (env, denv, t, ps) =
adamc@172 1015 let
adamc@172 1016 fun pcCoverage pc =
adamc@172 1017 case pc of
adamc@172 1018 L'.PConVar n => n
adamc@172 1019 | _ => raise Fail "uh oh^2"
adamc@172 1020
adamc@172 1021 fun coverage (p, _) =
adamc@172 1022 case p of
adamc@172 1023 L'.PWild => Wild
adamc@172 1024 | L'.PVar _ => Wild
adamc@173 1025 | L'.PPrim _ => None
adamc@172 1026 | L'.PCon (pc, NONE) => Datatype (IM.insert (IM.empty, pcCoverage pc, Wild))
adamc@172 1027 | L'.PCon (pc, SOME p) => Datatype (IM.insert (IM.empty, pcCoverage pc, coverage p))
adamc@172 1028
adamc@172 1029 fun merge (c1, c2) =
adamc@172 1030 case (c1, c2) of
adamc@173 1031 (None, _) => c2
adamc@173 1032 | (_, None) => c1
adamc@173 1033
adamc@173 1034 | (Wild, _) => Wild
adamc@172 1035 | (_, Wild) => Wild
adamc@172 1036
adamc@172 1037 | (Datatype cm1, Datatype cm2) => Datatype (IM.unionWith merge (cm1, cm2))
adamc@172 1038
adamc@172 1039 fun combinedCoverage ps =
adamc@172 1040 case ps of
adamc@172 1041 [] => raise Fail "Empty pattern list for coverage checking"
adamc@172 1042 | [p] => coverage p
adamc@172 1043 | p :: ps => merge (coverage p, combinedCoverage ps)
adamc@172 1044
adamc@172 1045 fun isTotal (c, t) =
adamc@172 1046 case c of
adamc@173 1047 None => (false, [])
adamc@173 1048 | Wild => (true, [])
adamc@172 1049 | Datatype cm =>
adamc@172 1050 let
adamc@172 1051 val ((t, _), gs) = hnormCon (env, denv) t
adamc@172 1052 in
adamc@172 1053 case t of
adamc@172 1054 L'.CNamed n =>
adamc@172 1055 let
adamc@172 1056 val dt = E.lookupDatatype env n
adamc@172 1057 val cons = E.constructors dt
adamc@172 1058 in
adamc@172 1059 foldl (fn ((_, n, to), (total, gs)) =>
adamc@172 1060 case IM.find (cm, n) of
adamc@172 1061 NONE => (false, gs)
adamc@172 1062 | SOME c' =>
adamc@172 1063 case to of
adamc@172 1064 NONE => (total, gs)
adamc@172 1065 | SOME t' =>
adamc@172 1066 let
adamc@172 1067 val (total, gs') = isTotal (c', t')
adamc@172 1068 in
adamc@172 1069 (total, gs' @ gs)
adamc@172 1070 end)
adamc@172 1071 (true, gs) cons
adamc@172 1072 end
adamc@172 1073 | L'.CError => (true, gs)
adamc@172 1074 | _ => raise Fail "isTotal: Not a datatype"
adamc@172 1075 end
adamc@172 1076 in
adamc@172 1077 isTotal (combinedCoverage ps, t)
adamc@172 1078 end
adamc@172 1079
adamc@92 1080 fun elabExp (env, denv) (eAll as (e, loc)) =
adamc@91 1081 let
adamc@92 1082
adamc@92 1083 in
adamc@92 1084 (*eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*)
adamc@10 1085
adamc@91 1086 case e of
adamc@91 1087 L.EAnnot (e, t) =>
adamc@91 1088 let
adamc@91 1089 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1090 val (t', _, gs2) = elabCon (env, denv) t
adamc@91 1091 val gs3 = checkCon (env, denv) e' et t'
adamc@91 1092 in
adamc@91 1093 (e', t', gs1 @ gs2 @ gs3)
adamc@91 1094 end
adamc@34 1095
adamc@91 1096 | L.EPrim p => ((L'.EPrim p, loc), primType env p, [])
adamc@91 1097 | L.EVar ([], s) =>
adamc@91 1098 (case E.lookupE env s of
adamc@91 1099 E.NotBound =>
adamc@91 1100 (expError env (UnboundExp (loc, s));
adamc@91 1101 (eerror, cerror, []))
adamc@91 1102 | E.Rel (n, t) => ((L'.ERel n, loc), t, [])
adamc@91 1103 | E.Named (n, t) => ((L'.ENamed n, loc), t, []))
adamc@91 1104 | L.EVar (m1 :: ms, s) =>
adamc@91 1105 (case E.lookupStr env m1 of
adamc@91 1106 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@91 1107 (eerror, cerror, []))
adamc@91 1108 | SOME (n, sgn) =>
adamc@91 1109 let
adamc@91 1110 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@91 1111 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@91 1112 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@91 1113 (strerror, sgnerror))
adamc@91 1114 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@91 1115 ((L'.StrVar n, loc), sgn) ms
adamc@10 1116
adamc@91 1117 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@91 1118 NONE => (expError env (UnboundExp (loc, s));
adamc@91 1119 cerror)
adamc@91 1120 | SOME t => t
adamc@91 1121 in
adamc@91 1122 ((L'.EModProj (n, ms, s), loc), t, [])
adamc@91 1123 end)
adamc@86 1124
adamc@91 1125 | L.EApp (e1, e2) =>
adamc@91 1126 let
adamc@91 1127 val (e1', t1, gs1) = elabExp (env, denv) e1
adamc@91 1128 val (e1', t1, gs2) = elabHead (env, denv) e1' t1
adamc@91 1129 val (e2', t2, gs3) = elabExp (env, denv) e2
adamc@71 1130
adamc@91 1131 val dom = cunif (loc, ktype)
adamc@91 1132 val ran = cunif (loc, ktype)
adamc@91 1133 val t = (L'.TFun (dom, ran), dummy)
adamc@91 1134
adamc@91 1135 val gs4 = checkCon (env, denv) e1' t1 t
adamc@91 1136 val gs5 = checkCon (env, denv) e2' t2 dom
adamc@91 1137 in
adamc@91 1138 ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2 @ gs3 @ gs4 @ gs5)
adamc@91 1139 end
adamc@91 1140 | L.EAbs (x, to, e) =>
adamc@91 1141 let
adamc@91 1142 val (t', gs1) = case to of
adamc@91 1143 NONE => (cunif (loc, ktype), [])
adamc@91 1144 | SOME t =>
adamc@91 1145 let
adamc@91 1146 val (t', tk, gs) = elabCon (env, denv) t
adamc@91 1147 in
adamc@91 1148 checkKind env t' tk ktype;
adamc@91 1149 (t', gs)
adamc@91 1150 end
adamc@91 1151 val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e
adamc@91 1152 in
adamc@91 1153 ((L'.EAbs (x, t', et, e'), loc),
adamc@91 1154 (L'.TFun (t', et), loc),
adamc@91 1155 gs1 @ gs2)
adamc@91 1156 end
adamc@91 1157 | L.ECApp (e, c) =>
adamc@91 1158 let
adamc@91 1159 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1160 val (e', et, gs2) = elabHead (env, denv) e' et
adamc@91 1161 val (c', ck, gs3) = elabCon (env, denv) c
adamc@91 1162 val ((et', _), gs4) = hnormCon (env, denv) et
adamc@91 1163 in
adamc@91 1164 case et' of
adamc@91 1165 L'.CError => (eerror, cerror, [])
adamc@91 1166 | L'.TCFun (_, _, k, eb) =>
adamc@91 1167 let
adamc@91 1168 val () = checkKind env c' ck k
adamc@91 1169 val eb' = subConInCon (0, c') eb
adamc@91 1170 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@91 1171 cerror)
adamc@91 1172 in
adamc@91 1173 ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1174 end
adamc@91 1175
adamc@91 1176 | L'.CUnif _ =>
adamc@91 1177 (expError env (Unif ("application", et));
adamc@91 1178 (eerror, cerror, []))
adamc@91 1179
adamc@91 1180 | _ =>
adamc@91 1181 (expError env (WrongForm ("constructor function", e', et));
adamc@91 1182 (eerror, cerror, []))
adamc@91 1183 end
adamc@91 1184 | L.ECAbs (expl, x, k, e) =>
adamc@91 1185 let
adamc@91 1186 val expl' = elabExplicitness expl
adamc@91 1187 val k' = elabKind k
adamc@91 1188 val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e
adamc@91 1189 in
adamc@91 1190 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@91 1191 (L'.TCFun (expl', x, k', et), loc),
adamc@91 1192 gs)
adamc@91 1193 end
adamc@91 1194
adamc@91 1195 | L.EDisjoint (c1, c2, e) =>
adamc@91 1196 let
adamc@91 1197 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@91 1198 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@91 1199
adamc@91 1200 val ku1 = kunif loc
adamc@91 1201 val ku2 = kunif loc
adamc@91 1202
adamc@91 1203 val (denv', gs3) = D.assert env denv (c1', c2')
adamc@91 1204 val (e', t, gs4) = elabExp (env, denv') e
adamc@91 1205 in
adamc@91 1206 checkKind env c1' k1 (L'.KRecord ku1, loc);
adamc@91 1207 checkKind env c2' k2 (L'.KRecord ku2, loc);
adamc@91 1208
adamc@91 1209 (e', (L'.TDisjoint (c1', c2', t), loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1210 end
adamc@91 1211
adamc@91 1212 | L.ERecord xes =>
adamc@91 1213 let
adamc@91 1214 val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) =>
adamc@91 1215 let
adamc@91 1216 val (x', xk, gs1) = elabCon (env, denv) x
adamc@91 1217 val (e', et, gs2) = elabExp (env, denv) e
adamc@91 1218 in
adamc@91 1219 checkKind env x' xk kname;
adamc@91 1220 ((x', e', et), gs1 @ gs2 @ gs)
adamc@91 1221 end)
adamc@91 1222 [] xes
adamc@91 1223
adamc@91 1224 val k = (L'.KType, loc)
adamc@91 1225
adamc@91 1226 fun prove (xets, gs) =
adamc@91 1227 case xets of
adamc@91 1228 [] => gs
adamc@91 1229 | (x, _, t) :: rest =>
adamc@91 1230 let
adamc@91 1231 val xc = (x, t)
adamc@91 1232 val r1 = (L'.CRecord (k, [xc]), loc)
adamc@91 1233 val gs = foldl (fn ((x', _, t'), gs) =>
adamc@91 1234 let
adamc@91 1235 val xc' = (x', t')
adamc@91 1236 val r2 = (L'.CRecord (k, [xc']), loc)
adamc@91 1237 in
adamc@91 1238 D.prove env denv (r1, r2, loc) @ gs
adamc@91 1239 end)
adamc@91 1240 gs rest
adamc@91 1241 in
adamc@91 1242 prove (rest, gs)
adamc@91 1243 end
adamc@91 1244 in
adamc@91 1245 ((L'.ERecord xes', loc),
adamc@91 1246 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc),
adamc@91 1247 prove (xes', gs))
adamc@91 1248 end
adamc@91 1249
adamc@91 1250 | L.EField (e, c) =>
adamc@91 1251 let
adamc@91 1252 val (e', et, gs1) = elabExp (env, denv) e
adamc@91 1253 val (c', ck, gs2) = elabCon (env, denv) c
adamc@91 1254
adamc@91 1255 val ft = cunif (loc, ktype)
adamc@91 1256 val rest = cunif (loc, ktype_record)
adamc@91 1257 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@91 1258
adamc@91 1259 val gs3 =
adamc@91 1260 checkCon (env, denv) e' et
adamc@91 1261 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@91 1262 val gs4 = D.prove env denv (first, rest, loc)
adamc@91 1263 in
adamc@91 1264 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2 @ gs3 @ gs4)
adamc@91 1265 end
adamc@91 1266
adamc@149 1267 | L.ECut (e, c) =>
adamc@149 1268 let
adamc@149 1269 val (e', et, gs1) = elabExp (env, denv) e
adamc@149 1270 val (c', ck, gs2) = elabCon (env, denv) c
adamc@149 1271
adamc@149 1272 val ft = cunif (loc, ktype)
adamc@149 1273 val rest = cunif (loc, ktype_record)
adamc@149 1274 val first = (L'.CRecord (ktype, [(c', ft)]), loc)
adamc@149 1275
adamc@149 1276 val gs3 =
adamc@149 1277 checkCon (env, denv) e' et
adamc@149 1278 (L'.TRecord (L'.CConcat (first, rest), loc), loc)
adamc@149 1279 val gs4 = D.prove env denv (first, rest, loc)
adamc@149 1280 in
adamc@149 1281 ((L'.ECut (e', c', {field = ft, rest = rest}), loc), (L'.TRecord rest, loc), gs1 @ gs2 @ gs3 @ gs4)
adamc@149 1282 end
adamc@149 1283
adamc@91 1284 | L.EFold =>
adamc@91 1285 let
adamc@91 1286 val dom = kunif loc
adamc@91 1287 in
adamc@91 1288 ((L'.EFold dom, loc), foldType (dom, loc), [])
adamc@91 1289 end
adamc@170 1290
adamc@171 1291 | L.ECase (e, pes) =>
adamc@171 1292 let
adamc@171 1293 val (e', et, gs1) = elabExp (env, denv) e
adamc@171 1294 val result = cunif (loc, (L'.KType, loc))
adamc@171 1295 val (pes', gs) = ListUtil.foldlMap
adamc@171 1296 (fn ((p, e), gs) =>
adamc@171 1297 let
adamc@171 1298 val ((p', pt), (env, _)) = elabPat (p, (env, SS.empty))
adamc@171 1299
adamc@171 1300 val gs1 = checkPatCon (env, denv) p' pt et
adamc@171 1301 val (e', et, gs2) = elabExp (env, denv) e
adamc@171 1302 val gs3 = checkCon (env, denv) e' et result
adamc@171 1303 in
adamc@171 1304 ((p', e'), gs1 @ gs2 @ gs3 @ gs)
adamc@171 1305 end)
adamc@171 1306 gs1 pes
adamc@172 1307
adamc@172 1308 val (total, gs') = exhaustive (env, denv, et, map #1 pes')
adamc@171 1309 in
adamc@172 1310 if total then
adamc@172 1311 ()
adamc@172 1312 else
adamc@172 1313 expError env (Inexhaustive loc);
adamc@172 1314
adamc@172 1315 ((L'.ECase (e', pes', result), loc), result, gs' @ gs)
adamc@171 1316 end
adamc@91 1317 end
adamc@12 1318
adamc@12 1319
adamc@6 1320 datatype decl_error =
adamc@91 1321 KunifsRemain of L'.decl list
adamc@91 1322 | CunifsRemain of L'.decl list
adamc@91 1323
adamc@91 1324 fun lspan [] = ErrorMsg.dummySpan
adamc@91 1325 | lspan ((_, loc) :: _) = loc
adamc@6 1326
adamc@6 1327 fun declError env err =
adamc@6 1328 case err of
adamc@91 1329 KunifsRemain ds =>
adamc@91 1330 (ErrorMsg.errorAt (lspan ds) "Some kind unification variables are undetermined in declaration";
adamc@91 1331 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@91 1332 | CunifsRemain ds =>
adamc@91 1333 (ErrorMsg.errorAt (lspan ds) "Some constructor unification variables are undetermined in declaration";
adamc@91 1334 eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)])
adamc@6 1335
adamc@31 1336 datatype sgn_error =
adamc@31 1337 UnboundSgn of ErrorMsg.span * string
adamc@31 1338 | UnmatchedSgi of L'.sgn_item
adamc@31 1339 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 1340 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@156 1341 | SgiMismatchedDatatypes of L'.sgn_item * L'.sgn_item * (L'.con * L'.con * cunify_error) option
adamc@41 1342 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 1343 | UnWhereable of L'.sgn * string
adamc@75 1344 | WhereWrongKind of L'.kind * L'.kind * kunify_error
adamc@58 1345 | NotIncludable of L'.sgn
adamc@62 1346 | DuplicateCon of ErrorMsg.span * string
adamc@62 1347 | DuplicateVal of ErrorMsg.span * string
adamc@62 1348 | DuplicateSgn of ErrorMsg.span * string
adamc@62 1349 | DuplicateStr of ErrorMsg.span * string
adamc@88 1350 | NotConstraintsable of L'.sgn
adamc@31 1351
adamc@31 1352 fun sgnError env err =
adamc@31 1353 case err of
adamc@31 1354 UnboundSgn (loc, s) =>
adamc@31 1355 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 1356 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 1357 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 1358 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 1359 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 1360 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@59 1361 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1362 ("Need", p_sgn_item env sgi2),
adamc@31 1363 ("Kind 1", p_kind k1),
adamc@31 1364 ("Kind 2", p_kind k2)];
adamc@31 1365 kunifyError kerr)
adamc@31 1366 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 1367 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@59 1368 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@59 1369 ("Need", p_sgn_item env sgi2),
adamc@31 1370 ("Con 1", p_con env c1),
adamc@31 1371 ("Con 2", p_con env c2)];
adamc@31 1372 cunifyError env cerr)
adamc@156 1373 | SgiMismatchedDatatypes (sgi1, sgi2, cerro) =>
adamc@156 1374 (ErrorMsg.errorAt (#2 sgi1) "Mismatched 'datatype' specifications:";
adamc@156 1375 eprefaces' [("Have", p_sgn_item env sgi1),
adamc@156 1376 ("Need", p_sgn_item env sgi2)];
adamc@156 1377 Option.app (fn (c1, c2, ue) =>
adamc@156 1378 (eprefaces "Unification error"
adamc@156 1379 [("Con 1", p_con env c1),
adamc@156 1380 ("Con 2", p_con env c2)];
adamc@156 1381 cunifyError env ue)) cerro)
adamc@41 1382 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1383 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1384 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1385 ("Sig 2", p_sgn env sgn2)])
adamc@42 1386 | UnWhereable (sgn, x) =>
adamc@42 1387 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1388 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1389 ("Field", PD.string x)])
adamc@75 1390 | WhereWrongKind (k1, k2, kerr) =>
adamc@75 1391 (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'";
adamc@75 1392 eprefaces' [("Have", p_kind k1),
adamc@75 1393 ("Need", p_kind k2)];
adamc@75 1394 kunifyError kerr)
adamc@58 1395 | NotIncludable sgn =>
adamc@58 1396 (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'";
adamc@58 1397 eprefaces' [("Signature", p_sgn env sgn)])
adamc@62 1398 | DuplicateCon (loc, s) =>
adamc@62 1399 ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature")
adamc@62 1400 | DuplicateVal (loc, s) =>
adamc@62 1401 ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature")
adamc@62 1402 | DuplicateSgn (loc, s) =>
adamc@62 1403 ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature")
adamc@62 1404 | DuplicateStr (loc, s) =>
adamc@62 1405 ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature")
adamc@88 1406 | NotConstraintsable sgn =>
adamc@88 1407 (ErrorMsg.errorAt (#2 sgn) "Invalid signature for 'open constraints'";
adamc@88 1408 eprefaces' [("Signature", p_sgn env sgn)])
adamc@31 1409
adamc@31 1410 datatype str_error =
adamc@31 1411 UnboundStr of ErrorMsg.span * string
adamc@44 1412 | NotFunctor of L'.sgn
adamc@46 1413 | FunctorRebind of ErrorMsg.span
adamc@61 1414 | UnOpenable of L'.sgn
adamc@75 1415 | NotType of L'.kind * (L'.kind * L'.kind * kunify_error)
adamc@156 1416 | DuplicateConstructor of string * ErrorMsg.span
adamc@158 1417 | NotDatatype of ErrorMsg.span
adamc@31 1418
adamc@31 1419 fun strError env err =
adamc@31 1420 case err of
adamc@31 1421 UnboundStr (loc, s) =>
adamc@31 1422 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1423 | NotFunctor sgn =>
adamc@44 1424 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1425 eprefaces' [("Signature", p_sgn env sgn)])
adamc@46 1426 | FunctorRebind loc =>
adamc@46 1427 ErrorMsg.errorAt loc "Attempt to rebind functor"
adamc@61 1428 | UnOpenable sgn =>
adamc@61 1429 (ErrorMsg.errorAt (#2 sgn) "Un-openable structure";
adamc@61 1430 eprefaces' [("Signature", p_sgn env sgn)])
adamc@75 1431 | NotType (k, (k1, k2, ue)) =>
adamc@75 1432 (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'";
adamc@75 1433 eprefaces' [("Kind", p_kind k),
adamc@75 1434 ("Subkind 1", p_kind k1),
adamc@75 1435 ("Subkind 2", p_kind k2)];
adamc@75 1436 kunifyError ue)
adamc@156 1437 | DuplicateConstructor (x, loc) =>
adamc@156 1438 ErrorMsg.errorAt loc ("Duplicate datatype constructor " ^ x)
adamc@158 1439 | NotDatatype loc =>
adamc@158 1440 ErrorMsg.errorAt loc "Trying to import non-datatype as a datatype"
adamc@31 1441
adamc@42 1442 val hnormSgn = E.hnormSgn
adamc@31 1443
adamc@88 1444 fun elabSgn_item ((sgi, loc), (env, denv, gs)) =
adamc@76 1445 case sgi of
adamc@76 1446 L.SgiConAbs (x, k) =>
adamc@76 1447 let
adamc@76 1448 val k' = elabKind k
adamc@31 1449
adamc@76 1450 val (env', n) = E.pushCNamed env x k' NONE
adamc@76 1451 in
adamc@88 1452 ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs))
adamc@76 1453 end
adamc@31 1454
adamc@76 1455 | L.SgiCon (x, ko, c) =>
adamc@76 1456 let
adamc@76 1457 val k' = case ko of
adamc@76 1458 NONE => kunif loc
adamc@76 1459 | SOME k => elabKind k
adamc@31 1460
adamc@83 1461 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 1462 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 1463 in
adamc@76 1464 checkKind env c' ck k';
adamc@31 1465
adamc@88 1466 ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 1467 end
adamc@31 1468
adamc@157 1469 | L.SgiDatatype (x, xcs) =>
adamc@157 1470 let
adamc@157 1471 val k = (L'.KType, loc)
adamc@157 1472 val (env, n) = E.pushCNamed env x k NONE
adamc@157 1473 val t = (L'.CNamed n, loc)
adamc@157 1474
adamc@157 1475 val (xcs, (used, env, gs)) =
adamc@157 1476 ListUtil.foldlMap
adamc@157 1477 (fn ((x, to), (used, env, gs)) =>
adamc@157 1478 let
adamc@157 1479 val (to, t, gs') = case to of
adamc@157 1480 NONE => (NONE, t, gs)
adamc@157 1481 | SOME t' =>
adamc@157 1482 let
adamc@157 1483 val (t', tk, gs') = elabCon (env, denv) t'
adamc@157 1484 in
adamc@157 1485 checkKind env t' tk k;
adamc@157 1486 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@157 1487 end
adamc@157 1488
adamc@157 1489 val (env, n') = E.pushENamed env x t
adamc@157 1490 in
adamc@157 1491 if SS.member (used, x) then
adamc@157 1492 strError env (DuplicateConstructor (x, loc))
adamc@157 1493 else
adamc@157 1494 ();
adamc@157 1495 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@157 1496 end)
adamc@157 1497 (SS.empty, env, []) xcs
adamc@157 1498 in
adamc@157 1499 ([(L'.SgiDatatype (x, n, xcs), loc)], (env, denv, gs))
adamc@157 1500 end
adamc@156 1501
adamc@158 1502 | L.SgiDatatypeImp (_, [], _) => raise Fail "Empty SgiDatatypeImp"
adamc@158 1503
adamc@158 1504 | L.SgiDatatypeImp (x, m1 :: ms, s) =>
adamc@158 1505 (case E.lookupStr env m1 of
adamc@158 1506 NONE => (strError env (UnboundStr (loc, m1));
adamc@158 1507 ([], (env, denv, gs)))
adamc@158 1508 | SOME (n, sgn) =>
adamc@158 1509 let
adamc@158 1510 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@158 1511 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1512 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@158 1513 (strerror, sgnerror))
adamc@158 1514 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@158 1515 ((L'.StrVar n, loc), sgn) ms
adamc@158 1516 in
adamc@161 1517 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@161 1518 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@161 1519 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@161 1520 NONE => (conError env (UnboundDatatype (loc, s));
adamc@161 1521 ([], (env, denv, gs)))
adamc@161 1522 | SOME xncs =>
adamc@161 1523 let
adamc@161 1524 val k = (L'.KType, loc)
adamc@161 1525 val t = (L'.CModProj (n, ms, s), loc)
adamc@161 1526 val (env, n') = E.pushCNamed env x k (SOME t)
adamc@161 1527 val env = E.pushDatatype env n' xncs
adamc@158 1528
adamc@161 1529 val t = (L'.CNamed n', loc)
adamc@161 1530 val env = foldl (fn ((x, n, to), env) =>
adamc@161 1531 let
adamc@161 1532 val t = case to of
adamc@161 1533 NONE => t
adamc@161 1534 | SOME t' => (L'.TFun (t', t), loc)
adamc@161 1535 in
adamc@161 1536 E.pushENamedAs env x n t
adamc@161 1537 end) env xncs
adamc@161 1538 in
adamc@162 1539 ([(L'.SgiDatatypeImp (x, n', n, ms, s, xncs), loc)], (env, denv, gs))
adamc@161 1540 end)
adamc@161 1541 | _ => (strError env (NotDatatype loc);
adamc@161 1542 ([], (env, denv, [])))
adamc@158 1543 end)
adamc@156 1544
adamc@76 1545 | L.SgiVal (x, c) =>
adamc@76 1546 let
adamc@83 1547 val (c', ck, gs') = elabCon (env, denv) c
adamc@31 1548
adamc@76 1549 val (env', n) = E.pushENamed env x c'
adamc@76 1550 in
adamc@76 1551 (unifyKinds ck ktype
adamc@76 1552 handle KUnify ue => strError env (NotType (ck, ue)));
adamc@31 1553
adamc@88 1554 ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs))
adamc@76 1555 end
adamc@31 1556
adamc@76 1557 | L.SgiStr (x, sgn) =>
adamc@76 1558 let
adamc@83 1559 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1560 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 1561 in
adamc@88 1562 ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1563 end
adamc@31 1564
adamc@76 1565 | L.SgiSgn (x, sgn) =>
adamc@76 1566 let
adamc@83 1567 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1568 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 1569 in
adamc@88 1570 ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 1571 end
adamc@31 1572
adamc@76 1573 | L.SgiInclude sgn =>
adamc@76 1574 let
adamc@83 1575 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 1576 in
adamc@76 1577 case #1 (hnormSgn env sgn') of
adamc@76 1578 L'.SgnConst sgis =>
adamc@88 1579 (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs))
adamc@76 1580 | _ => (sgnError env (NotIncludable sgn');
adamc@88 1581 ([], (env, denv, [])))
adamc@88 1582 end
adamc@88 1583
adamc@88 1584 | L.SgiConstraint (c1, c2) =>
adamc@88 1585 let
adamc@88 1586 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 1587 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@88 1588
adamc@90 1589 val (denv, gs3) = D.assert env denv (c1', c2')
adamc@88 1590 in
adamc@88 1591 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 1592 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 1593
adamc@90 1594 ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3))
adamc@76 1595 end
adamc@31 1596
adamc@83 1597 and elabSgn (env, denv) (sgn, loc) =
adamc@31 1598 case sgn of
adamc@31 1599 L.SgnConst sgis =>
adamc@31 1600 let
adamc@88 1601 val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis
adamc@62 1602
adamc@62 1603 val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) =>
adamc@62 1604 case sgi of
adamc@62 1605 L'.SgiConAbs (x, _, _) =>
adamc@62 1606 (if SS.member (cons, x) then
adamc@62 1607 sgnError env (DuplicateCon (loc, x))
adamc@62 1608 else
adamc@62 1609 ();
adamc@62 1610 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1611 | L'.SgiCon (x, _, _, _) =>
adamc@62 1612 (if SS.member (cons, x) then
adamc@62 1613 sgnError env (DuplicateCon (loc, x))
adamc@62 1614 else
adamc@62 1615 ();
adamc@62 1616 (SS.add (cons, x), vals, sgns, strs))
adamc@156 1617 | L'.SgiDatatype (x, _, xncs) =>
adamc@156 1618 let
adamc@156 1619 val vals = foldl (fn ((x, _, _), vals) =>
adamc@156 1620 (if SS.member (vals, x) then
adamc@156 1621 sgnError env (DuplicateVal (loc, x))
adamc@156 1622 else
adamc@156 1623 ();
adamc@156 1624 SS.add (vals, x)))
adamc@156 1625 vals xncs
adamc@156 1626 in
adamc@156 1627 if SS.member (cons, x) then
adamc@156 1628 sgnError env (DuplicateCon (loc, x))
adamc@156 1629 else
adamc@156 1630 ();
adamc@156 1631 (SS.add (cons, x), vals, sgns, strs)
adamc@156 1632 end
adamc@162 1633 | L'.SgiDatatypeImp (x, _, _, _, _, _) =>
adamc@156 1634 (if SS.member (cons, x) then
adamc@156 1635 sgnError env (DuplicateCon (loc, x))
adamc@156 1636 else
adamc@156 1637 ();
adamc@156 1638 (SS.add (cons, x), vals, sgns, strs))
adamc@62 1639 | L'.SgiVal (x, _, _) =>
adamc@62 1640 (if SS.member (vals, x) then
adamc@62 1641 sgnError env (DuplicateVal (loc, x))
adamc@62 1642 else
adamc@62 1643 ();
adamc@62 1644 (cons, SS.add (vals, x), sgns, strs))
adamc@62 1645 | L'.SgiSgn (x, _, _) =>
adamc@62 1646 (if SS.member (sgns, x) then
adamc@62 1647 sgnError env (DuplicateSgn (loc, x))
adamc@62 1648 else
adamc@62 1649 ();
adamc@62 1650 (cons, vals, SS.add (sgns, x), strs))
adamc@62 1651 | L'.SgiStr (x, _, _) =>
adamc@62 1652 (if SS.member (strs, x) then
adamc@62 1653 sgnError env (DuplicateStr (loc, x))
adamc@62 1654 else
adamc@62 1655 ();
adamc@88 1656 (cons, vals, sgns, SS.add (strs, x)))
adamc@88 1657 | L'.SgiConstraint _ => (cons, vals, sgns, strs))
adamc@62 1658 (SS.empty, SS.empty, SS.empty, SS.empty) sgis'
adamc@31 1659 in
adamc@83 1660 ((L'.SgnConst sgis', loc), gs)
adamc@31 1661 end
adamc@31 1662 | L.SgnVar x =>
adamc@31 1663 (case E.lookupSgn env x of
adamc@31 1664 NONE =>
adamc@31 1665 (sgnError env (UnboundSgn (loc, x));
adamc@83 1666 ((L'.SgnError, loc), []))
adamc@83 1667 | SOME (n, sgis) => ((L'.SgnVar n, loc), []))
adamc@41 1668 | L.SgnFun (m, dom, ran) =>
adamc@41 1669 let
adamc@83 1670 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 1671 val (env', n) = E.pushStrNamed env m dom'
adamc@83 1672 val (ran', gs2) = elabSgn (env', denv) ran
adamc@41 1673 in
adamc@83 1674 ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2)
adamc@41 1675 end
adamc@42 1676 | L.SgnWhere (sgn, x, c) =>
adamc@42 1677 let
adamc@83 1678 val (sgn', ds1) = elabSgn (env, denv) sgn
adamc@83 1679 val (c', ck, ds2) = elabCon (env, denv) c
adamc@42 1680 in
adamc@42 1681 case #1 (hnormSgn env sgn') of
adamc@83 1682 L'.SgnError => (sgnerror, [])
adamc@42 1683 | L'.SgnConst sgis =>
adamc@75 1684 if List.exists (fn (L'.SgiConAbs (x', _, k), _) =>
adamc@75 1685 x' = x andalso
adamc@75 1686 (unifyKinds k ck
adamc@75 1687 handle KUnify x => sgnError env (WhereWrongKind x);
adamc@42 1688 true)
adamc@42 1689 | _ => false) sgis then
adamc@83 1690 ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2)
adamc@42 1691 else
adamc@42 1692 (sgnError env (UnWhereable (sgn', x));
adamc@83 1693 (sgnerror, []))
adamc@42 1694 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@83 1695 (sgnerror, []))
adamc@42 1696 end
adamc@59 1697 | L.SgnProj (m, ms, x) =>
adamc@59 1698 (case E.lookupStr env m of
adamc@59 1699 NONE => (strError env (UnboundStr (loc, m));
adamc@83 1700 (sgnerror, []))
adamc@59 1701 | SOME (n, sgn) =>
adamc@59 1702 let
adamc@59 1703 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@59 1704 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@59 1705 NONE => (strError env (UnboundStr (loc, m));
adamc@59 1706 (strerror, sgnerror))
adamc@59 1707 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@59 1708 ((L'.StrVar n, loc), sgn) ms
adamc@59 1709 in
adamc@59 1710 case E.projectSgn env {sgn = sgn, str = str, field = x} of
adamc@59 1711 NONE => (sgnError env (UnboundSgn (loc, x));
adamc@83 1712 (sgnerror, []))
adamc@83 1713 | SOME _ => ((L'.SgnProj (n, ms, x), loc), [])
adamc@59 1714 end)
adamc@59 1715
adamc@31 1716
adamc@66 1717 fun selfify env {str, strs, sgn} =
adamc@66 1718 case #1 (hnormSgn env sgn) of
adamc@66 1719 L'.SgnError => sgn
adamc@66 1720 | L'.SgnVar _ => sgn
adamc@66 1721
adamc@66 1722 | L'.SgnConst sgis =>
adamc@66 1723 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@66 1724 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@158 1725 | (L'.SgiDatatype (x, n, xncs), loc) =>
adamc@162 1726 (L'.SgiDatatypeImp (x, n, str, strs, x, xncs), loc)
adamc@66 1727 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@66 1728 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@66 1729 | x => x) sgis), #2 sgn)
adamc@66 1730 | L'.SgnFun _ => sgn
adamc@66 1731 | L'.SgnWhere _ => sgn
adamc@66 1732 | L'.SgnProj (m, ms, x) =>
adamc@66 1733 case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1734 (L'.StrVar m, #2 sgn) ms,
adamc@66 1735 sgn = #2 (E.lookupStrNamed env m),
adamc@66 1736 field = x} of
adamc@66 1737 NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE"
adamc@66 1738 | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn}
adamc@66 1739
adamc@66 1740 fun selfifyAt env {str, sgn} =
adamc@66 1741 let
adamc@66 1742 fun self (str, _) =
adamc@66 1743 case str of
adamc@66 1744 L'.StrVar x => SOME (x, [])
adamc@66 1745 | L'.StrProj (str, x) =>
adamc@66 1746 (case self str of
adamc@66 1747 NONE => NONE
adamc@66 1748 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@66 1749 | _ => NONE
adamc@66 1750 in
adamc@66 1751 case self str of
adamc@66 1752 NONE => sgn
adamc@66 1753 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@66 1754 end
adamc@66 1755
adamc@88 1756 fun dopen (env, denv) {str, strs, sgn} =
adamc@66 1757 let
adamc@66 1758 val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn))
adamc@66 1759 (L'.StrVar str, #2 sgn) strs
adamc@66 1760 in
adamc@66 1761 case #1 (hnormSgn env sgn) of
adamc@66 1762 L'.SgnConst sgis =>
adamc@88 1763 ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) =>
adamc@162 1764 let
adamc@162 1765 val d =
adamc@162 1766 case sgi of
adamc@162 1767 L'.SgiConAbs (x, n, k) =>
adamc@162 1768 let
adamc@162 1769 val c = (L'.CModProj (str, strs, x), loc)
adamc@162 1770 in
adamc@162 1771 (L'.DCon (x, n, k, c), loc)
adamc@162 1772 end
adamc@162 1773 | L'.SgiCon (x, n, k, c) =>
adamc@162 1774 (L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@162 1775 | L'.SgiDatatype (x, n, xncs) =>
adamc@162 1776 (L'.DDatatypeImp (x, n, str, strs, x, xncs), loc)
adamc@162 1777 | L'.SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
adamc@162 1778 (L'.DDatatypeImp (x, n, m1, ms, x', xncs), loc)
adamc@162 1779 | L'.SgiVal (x, n, t) =>
adamc@162 1780 (L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc)
adamc@162 1781 | L'.SgiStr (x, n, sgn) =>
adamc@162 1782 (L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc)
adamc@162 1783 | L'.SgiSgn (x, n, sgn) =>
adamc@162 1784 (L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc)
adamc@162 1785 | L'.SgiConstraint (c1, c2) =>
adamc@162 1786 (L'.DConstraint (c1, c2), loc)
adamc@162 1787 in
adamc@162 1788 (d, (E.declBinds env' d, denv'))
adamc@162 1789 end)
adamc@88 1790 (env, denv) sgis
adamc@66 1791 | _ => (strError env (UnOpenable sgn);
adamc@88 1792 ([], (env, denv)))
adamc@66 1793 end
adamc@66 1794
adamc@88 1795 fun dopenConstraints (loc, env, denv) {str, strs} =
adamc@88 1796 case E.lookupStr env str of
adamc@88 1797 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1798 denv)
adamc@88 1799 | SOME (n, sgn) =>
adamc@88 1800 let
adamc@88 1801 val (st, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 1802 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 1803 NONE => (strError env (UnboundStr (loc, m));
adamc@88 1804 (strerror, sgnerror))
adamc@88 1805 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 1806 ((L'.StrVar n, loc), sgn) strs
adamc@88 1807
adamc@88 1808 val cso = E.projectConstraints env {sgn = sgn, str = st}
adamc@88 1809
adamc@88 1810 val denv = case cso of
adamc@88 1811 NONE => (strError env (UnboundStr (loc, str));
adamc@88 1812 denv)
adamc@90 1813 | SOME cs => foldl (fn ((c1, c2), denv) =>
adamc@90 1814 let
adamc@90 1815 val (denv, gs) = D.assert env denv (c1, c2)
adamc@90 1816 in
adamc@90 1817 case gs of
adamc@90 1818 [] => ()
adamc@90 1819 | _ => raise Fail "dopenConstraints: Sub-constraints remain";
adamc@90 1820
adamc@90 1821 denv
adamc@90 1822 end) denv cs
adamc@88 1823 in
adamc@88 1824 denv
adamc@88 1825 end
adamc@88 1826
adamc@31 1827 fun sgiOfDecl (d, loc) =
adamc@31 1828 case d of
adamc@123 1829 L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)]
adamc@156 1830 | L'.DDatatype x => [(L'.SgiDatatype x, loc)]
adamc@156 1831 | L'.DDatatypeImp x => [(L'.SgiDatatypeImp x, loc)]
adamc@123 1832 | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)]
adamc@123 1833 | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis
adamc@123 1834 | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)]
adamc@123 1835 | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 1836 | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)]
adamc@123 1837 | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)]
adamc@123 1838 | L'.DExport _ => []
adamc@88 1839
adamc@88 1840 fun sgiBindsD (env, denv) (sgi, _) =
adamc@88 1841 case sgi of
adamc@90 1842 L'.SgiConstraint (c1, c2) =>
adamc@90 1843 (case D.assert env denv (c1, c2) of
adamc@90 1844 (denv, []) => denv
adamc@90 1845 | _ => raise Fail "sgiBindsD: Sub-constraints remain")
adamc@88 1846 | _ => denv
adamc@31 1847
adamc@86 1848 fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) =
adamc@35 1849 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 1850 (L'.SgnError, _) => ()
adamc@31 1851 | (_, L'.SgnError) => ()
adamc@31 1852
adamc@31 1853 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1854 let
adamc@156 1855 fun folder (sgi2All as (sgi, loc), (env, denv)) =
adamc@31 1856 let
adamc@31 1857 fun seek p =
adamc@31 1858 let
adamc@88 1859 fun seek (env, denv) ls =
adamc@31 1860 case ls of
adamc@31 1861 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@88 1862 (env, denv))
adamc@31 1863 | h :: t =>
adamc@31 1864 case p h of
adamc@88 1865 NONE => seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t
adamc@88 1866 | SOME envs => envs
adamc@31 1867 in
adamc@88 1868 seek (env, denv) sgis1
adamc@31 1869 end
adamc@31 1870 in
adamc@31 1871 case sgi of
adamc@31 1872 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1873 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1874 let
adamc@41 1875 fun found (x', n1, k1, co1) =
adamc@41 1876 if x = x' then
adamc@41 1877 let
adamc@41 1878 val () = unifyKinds k1 k2
adamc@41 1879 handle KUnify (k1, k2, err) =>
adamc@41 1880 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 1881 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 1882 in
adamc@41 1883 SOME (if n1 = n2 then
adamc@41 1884 env
adamc@41 1885 else
adamc@88 1886 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)),
adamc@88 1887 denv)
adamc@41 1888 end
adamc@41 1889 else
adamc@41 1890 NONE
adamc@31 1891 in
adamc@31 1892 case sgi1 of
adamc@41 1893 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 1894 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@159 1895 | L'.SgiDatatype (x', n1, _) => found (x', n1, (L'.KType, loc), NONE)
adamc@162 1896 | L'.SgiDatatypeImp (x', n1, m1, ms, s, _) =>
adamc@159 1897 found (x', n1, (L'.KType, loc), SOME (L'.CModProj (m1, ms, s), loc))
adamc@31 1898 | _ => NONE
adamc@31 1899 end)
adamc@31 1900
adamc@31 1901 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 1902 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1903 case sgi1 of
adamc@41 1904 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 1905 if x = x' then
adamc@41 1906 let
adamc@88 1907 fun good () = SOME (E.pushCNamedAs env x n2 k2 (SOME c2), denv)
adamc@41 1908 in
adamc@86 1909 (case unifyCons (env, denv) c1 c2 of
adamc@86 1910 [] => good ()
adamc@86 1911 | _ => NONE)
adamc@86 1912 handle CUnify (c1, c2, err) =>
adamc@86 1913 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@86 1914 good ())
adamc@41 1915 end
adamc@41 1916 else
adamc@41 1917 NONE
adamc@31 1918 | _ => NONE)
adamc@31 1919
adamc@156 1920 | L'.SgiDatatype (x, n2, xncs2) =>
adamc@156 1921 seek (fn sgi1All as (sgi1, _) =>
adamc@160 1922 let
adamc@160 1923 fun found (n1, xncs1) =
adamc@160 1924 let
adamc@160 1925 fun mismatched ue =
adamc@160 1926 (sgnError env (SgiMismatchedDatatypes (sgi1All, sgi2All, ue));
adamc@160 1927 SOME (env, denv))
adamc@156 1928
adamc@160 1929 fun good () =
adamc@160 1930 let
adamc@160 1931 val env = E.sgiBinds env sgi2All
adamc@160 1932 val env = if n1 = n2 then
adamc@160 1933 env
adamc@160 1934 else
adamc@160 1935 E.pushCNamedAs env x n1 (L'.KType, loc)
adamc@160 1936 (SOME (L'.CNamed n1, loc))
adamc@160 1937 in
adamc@160 1938 SOME (env, denv)
adamc@160 1939 end
adamc@156 1940
adamc@160 1941 fun xncBad ((x1, _, t1), (x2, _, t2)) =
adamc@160 1942 String.compare (x1, x2) <> EQUAL
adamc@160 1943 orelse case (t1, t2) of
adamc@160 1944 (NONE, NONE) => false
adamc@160 1945 | (SOME t1, SOME t2) =>
adamc@160 1946 not (List.null (unifyCons (env, denv) t1 t2))
adamc@160 1947 | _ => true
adamc@160 1948 in
adamc@160 1949 (if length xncs1 <> length xncs2
adamc@160 1950 orelse ListPair.exists xncBad (xncs1, xncs2) then
adamc@160 1951 mismatched NONE
adamc@160 1952 else
adamc@160 1953 good ())
adamc@160 1954 handle CUnify ue => mismatched (SOME ue)
adamc@160 1955 end
adamc@160 1956 in
adamc@160 1957 case sgi1 of
adamc@160 1958 L'.SgiDatatype (x', n1, xncs1) =>
adamc@160 1959 if x' = x then
adamc@160 1960 found (n1, xncs1)
adamc@160 1961 else
adamc@160 1962 NONE
adamc@162 1963 | L'.SgiDatatypeImp (x', n1, _, _, _, xncs1) =>
adamc@162 1964 if x' = x then
adamc@162 1965 found (n1, xncs1)
adamc@162 1966 else
adamc@162 1967 NONE
adamc@160 1968 | _ => NONE
adamc@160 1969 end)
adamc@156 1970
adamc@162 1971 | L'.SgiDatatypeImp (x, n2, m12, ms2, s2, _) =>
adamc@158 1972 seek (fn sgi1All as (sgi1, _) =>
adamc@158 1973 case sgi1 of
adamc@162 1974 L'.SgiDatatypeImp (x', n1, m11, ms1, s1, _) =>
adamc@158 1975 if x = x' then
adamc@158 1976 let
adamc@158 1977 val k = (L'.KType, loc)
adamc@158 1978 val t1 = (L'.CModProj (m11, ms1, s1), loc)
adamc@158 1979 val t2 = (L'.CModProj (m12, ms2, s2), loc)
adamc@158 1980
adamc@158 1981 fun good () =
adamc@158 1982 let
adamc@158 1983 val env = E.pushCNamedAs env x n1 k (SOME t1)
adamc@158 1984 val env = E.pushCNamedAs env x n2 k (SOME t2)
adamc@158 1985 in
adamc@158 1986 SOME (env, denv)
adamc@158 1987 end
adamc@158 1988 in
adamc@158 1989 (case unifyCons (env, denv) t1 t2 of
adamc@158 1990 [] => good ()
adamc@158 1991 | _ => NONE)
adamc@158 1992 handle CUnify (c1, c2, err) =>
adamc@158 1993 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@158 1994 good ())
adamc@158 1995 end
adamc@158 1996 else
adamc@158 1997 NONE
adamc@158 1998
adamc@158 1999 | _ => NONE)
adamc@156 2000
adamc@32 2001 | L'.SgiVal (x, n2, c2) =>
adamc@32 2002 seek (fn sgi1All as (sgi1, _) =>
adamc@32 2003 case sgi1 of
adamc@41 2004 L'.SgiVal (x', n1, c1) =>
adamc@41 2005 if x = x' then
adamc@86 2006 (case unifyCons (env, denv) c1 c2 of
adamc@88 2007 [] => SOME (env, denv)
adamc@86 2008 | _ => NONE)
adamc@86 2009 handle CUnify (c1, c2, err) =>
adamc@86 2010 (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err));
adamc@88 2011 SOME (env, denv))
adamc@41 2012 else
adamc@41 2013 NONE
adamc@32 2014 | _ => NONE)
adamc@32 2015
adamc@33 2016 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 2017 seek (fn sgi1All as (sgi1, _) =>
adamc@33 2018 case sgi1 of
adamc@41 2019 L'.SgiStr (x', n1, sgn1) =>
adamc@41 2020 if x = x' then
adamc@66 2021 let
adamc@86 2022 val () = subSgn (env, denv) sgn1 sgn2
adamc@66 2023 val env = E.pushStrNamedAs env x n1 sgn1
adamc@66 2024 val env = if n1 = n2 then
adamc@66 2025 env
adamc@66 2026 else
adamc@66 2027 E.pushStrNamedAs env x n2
adamc@66 2028 (selfifyAt env {str = (L'.StrVar n1, #2 sgn2),
adamc@66 2029 sgn = sgn2})
adamc@66 2030 in
adamc@88 2031 SOME (env, denv)
adamc@66 2032 end
adamc@41 2033 else
adamc@41 2034 NONE
adamc@33 2035 | _ => NONE)
adamc@59 2036
adamc@59 2037 | L'.SgiSgn (x, n2, sgn2) =>
adamc@59 2038 seek (fn sgi1All as (sgi1, _) =>
adamc@59 2039 case sgi1 of
adamc@59 2040 L'.SgiSgn (x', n1, sgn1) =>
adamc@59 2041 if x = x' then
adamc@65 2042 let
adamc@86 2043 val () = subSgn (env, denv) sgn1 sgn2
adamc@86 2044 val () = subSgn (env, denv) sgn2 sgn1
adamc@65 2045
adamc@65 2046 val env = E.pushSgnNamedAs env x n2 sgn2
adamc@65 2047 val env = if n1 = n2 then
adamc@65 2048 env
adamc@65 2049 else
adamc@65 2050 E.pushSgnNamedAs env x n1 sgn2
adamc@65 2051 in
adamc@88 2052 SOME (env, denv)
adamc@65 2053 end
adamc@59 2054 else
adamc@59 2055 NONE
adamc@59 2056 | _ => NONE)
adamc@88 2057
adamc@88 2058 | L'.SgiConstraint (c2, d2) =>
adamc@88 2059 seek (fn sgi1All as (sgi1, _) =>
adamc@88 2060 case sgi1 of
adamc@88 2061 L'.SgiConstraint (c1, d1) =>
adamc@88 2062 if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then
adamc@90 2063 let
adamc@90 2064 val (denv, gs) = D.assert env denv (c2, d2)
adamc@90 2065 in
adamc@90 2066 case gs of
adamc@90 2067 [] => ()
adamc@90 2068 | _ => raise Fail "subSgn: Sub-constraints remain";
adamc@90 2069
adamc@90 2070 SOME (env, denv)
adamc@90 2071 end
adamc@88 2072 else
adamc@88 2073 NONE
adamc@88 2074 | _ => NONE)
adamc@31 2075 end
adamc@31 2076 in
adamc@88 2077 ignore (foldl folder (env, denv) sgis2)
adamc@31 2078 end
adamc@31 2079
adamc@41 2080 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 2081 let
adamc@43 2082 val ran1 =
adamc@43 2083 if n1 = n2 then
adamc@43 2084 ran1
adamc@43 2085 else
adamc@43 2086 subStrInSgn (n1, n2) ran1
adamc@43 2087 in
adamc@86 2088 subSgn (env, denv) dom2 dom1;
adamc@86 2089 subSgn (E.pushStrNamedAs env m2 n2 dom2, denv) ran1 ran2
adamc@43 2090 end
adamc@41 2091
adamc@41 2092 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 2093
adamc@61 2094
adamc@88 2095 fun elabDecl ((d, loc), (env, denv, gs)) =
adamc@76 2096 case d of
adamc@76 2097 L.DCon (x, ko, c) =>
adamc@76 2098 let
adamc@76 2099 val k' = case ko of
adamc@76 2100 NONE => kunif loc
adamc@76 2101 | SOME k => elabKind k
adamc@76 2102
adamc@83 2103 val (c', ck, gs') = elabCon (env, denv) c
adamc@76 2104 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@76 2105 in
adamc@76 2106 checkKind env c' ck k';
adamc@76 2107
adamc@88 2108 ([(L'.DCon (x, n, k', c'), loc)], (env', denv, gs' @ gs))
adamc@76 2109 end
adamc@156 2110 | L.DDatatype (x, xcs) =>
adamc@156 2111 let
adamc@156 2112 val k = (L'.KType, loc)
adamc@156 2113 val (env, n) = E.pushCNamed env x k NONE
adamc@156 2114 val t = (L'.CNamed n, loc)
adamc@156 2115
adamc@156 2116 val (xcs, (used, env, gs)) =
adamc@156 2117 ListUtil.foldlMap
adamc@156 2118 (fn ((x, to), (used, env, gs)) =>
adamc@156 2119 let
adamc@156 2120 val (to, t, gs') = case to of
adamc@156 2121 NONE => (NONE, t, gs)
adamc@156 2122 | SOME t' =>
adamc@156 2123 let
adamc@156 2124 val (t', tk, gs') = elabCon (env, denv) t'
adamc@156 2125 in
adamc@156 2126 checkKind env t' tk k;
adamc@156 2127 (SOME t', (L'.TFun (t', t), loc), gs' @ gs)
adamc@156 2128 end
adamc@156 2129
adamc@156 2130 val (env, n') = E.pushENamed env x t
adamc@156 2131 in
adamc@156 2132 if SS.member (used, x) then
adamc@156 2133 strError env (DuplicateConstructor (x, loc))
adamc@156 2134 else
adamc@156 2135 ();
adamc@156 2136 ((x, n', to), (SS.add (used, x), env, gs'))
adamc@156 2137 end)
adamc@156 2138 (SS.empty, env, []) xcs
adamc@171 2139
adamc@171 2140 val env = E.pushDatatype env n xcs
adamc@156 2141 in
adamc@156 2142 ([(L'.DDatatype (x, n, xcs), loc)], (env, denv, gs))
adamc@156 2143 end
adamc@156 2144
adamc@157 2145 | L.DDatatypeImp (_, [], _) => raise Fail "Empty DDatatypeImp"
adamc@157 2146
adamc@157 2147 | L.DDatatypeImp (x, m1 :: ms, s) =>
adamc@157 2148 (case E.lookupStr env m1 of
adamc@157 2149 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@157 2150 ([], (env, denv, gs)))
adamc@157 2151 | SOME (n, sgn) =>
adamc@157 2152 let
adamc@157 2153 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@157 2154 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@157 2155 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@157 2156 (strerror, sgnerror))
adamc@157 2157 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@157 2158 ((L'.StrVar n, loc), sgn) ms
adamc@157 2159 in
adamc@158 2160 case hnormCon (env, denv) (L'.CModProj (n, ms, s), loc) of
adamc@158 2161 ((L'.CModProj (n, ms, s), _), gs) =>
adamc@158 2162 (case E.projectDatatype env {sgn = sgn, str = str, field = s} of
adamc@158 2163 NONE => (conError env (UnboundDatatype (loc, s));
adamc@158 2164 ([], (env, denv, gs)))
adamc@158 2165 | SOME xncs =>
adamc@158 2166 let
adamc@158 2167 val k = (L'.KType, loc)
adamc@158 2168 val t = (L'.CModProj (n, ms, s), loc)
adamc@158 2169 val (env, n') = E.pushCNamed env x k (SOME t)
adamc@158 2170 val env = E.pushDatatype env n' xncs
adamc@157 2171
adamc@158 2172 val t = (L'.CNamed n', loc)
adamc@158 2173 val env = foldl (fn ((x, n, to), env) =>
adamc@158 2174 let
adamc@158 2175 val t = case to of
adamc@158 2176 NONE => t
adamc@158 2177 | SOME t' => (L'.TFun (t', t), loc)
adamc@158 2178 in
adamc@158 2179 E.pushENamedAs env x n t
adamc@158 2180 end) env xncs
adamc@158 2181 in
adamc@162 2182 ([(L'.DDatatypeImp (x, n', n, ms, s, xncs), loc)], (env, denv, gs))
adamc@158 2183 end)
adamc@158 2184 | _ => (strError env (NotDatatype loc);
adamc@158 2185 ([], (env, denv, [])))
adamc@157 2186 end)
adamc@157 2187
adamc@76 2188 | L.DVal (x, co, e) =>
adamc@76 2189 let
adamc@123 2190 val (c', _, gs1) = case co of
adamc@83 2191 NONE => (cunif (loc, ktype), ktype, [])
adamc@83 2192 | SOME c => elabCon (env, denv) c
adamc@76 2193
adamc@83 2194 val (e', et, gs2) = elabExp (env, denv) e
adamc@76 2195 val (env', n) = E.pushENamed env x c'
adamc@86 2196
adamc@86 2197 val gs3 = checkCon (env, denv) e' et c'
adamc@76 2198 in
adamc@88 2199 ([(L'.DVal (x, n, c', e'), loc)], (env', denv, gs1 @ gs2 @ gs3 @ gs))
adamc@76 2200 end
adamc@123 2201 | L.DValRec vis =>
adamc@123 2202 let
adamc@123 2203 val (vis, gs) = ListUtil.foldlMap
adamc@123 2204 (fn ((x, co, e), gs) =>
adamc@123 2205 let
adamc@123 2206 val (c', _, gs1) = case co of
adamc@123 2207 NONE => (cunif (loc, ktype), ktype, [])
adamc@123 2208 | SOME c => elabCon (env, denv) c
adamc@123 2209 in
adamc@123 2210 ((x, c', e), gs1 @ gs)
adamc@123 2211 end) [] vis
adamc@123 2212
adamc@123 2213 val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) =>
adamc@123 2214 let
adamc@123 2215 val (env, n) = E.pushENamed env x c'
adamc@123 2216 in
adamc@123 2217 ((x, n, c', e), env)
adamc@123 2218 end) env vis
adamc@123 2219
adamc@123 2220 val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) =>
adamc@123 2221 let
adamc@123 2222 val (e', et, gs1) = elabExp (env, denv) e
adamc@123 2223
adamc@123 2224 val gs2 = checkCon (env, denv) e' et c'
adamc@123 2225 in
adamc@123 2226 ((x, n, c', e'), gs1 @ gs2 @ gs)
adamc@123 2227 end) gs vis
adamc@123 2228 in
adamc@123 2229 ([(L'.DValRec vis, loc)], (env, denv, gs))
adamc@123 2230 end
adamc@76 2231
adamc@76 2232 | L.DSgn (x, sgn) =>
adamc@76 2233 let
adamc@83 2234 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2235 val (env', n) = E.pushSgnNamed env x sgn'
adamc@76 2236 in
adamc@88 2237 ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2238 end
adamc@76 2239
adamc@76 2240 | L.DStr (x, sgno, str) =>
adamc@76 2241 let
adamc@93 2242 val () = if x = "Basis" then
adamc@93 2243 raise Fail "Not allowed to redefine structure 'Basis'"
adamc@93 2244 else
adamc@93 2245 ()
adamc@93 2246
adamc@83 2247 val formal = Option.map (elabSgn (env, denv)) sgno
adamc@76 2248
adamc@83 2249 val (str', sgn', gs') =
adamc@78 2250 case formal of
adamc@78 2251 NONE =>
adamc@78 2252 let
adamc@83 2253 val (str', actual, ds) = elabStr (env, denv) str
adamc@78 2254 in
adamc@83 2255 (str', selfifyAt env {str = str', sgn = actual}, ds)
adamc@78 2256 end
adamc@83 2257 | SOME (formal, gs1) =>
adamc@78 2258 let
adamc@78 2259 val str =
adamc@78 2260 case #1 (hnormSgn env formal) of
adamc@78 2261 L'.SgnConst sgis =>
adamc@78 2262 (case #1 str of
adamc@78 2263 L.StrConst ds =>
adamc@78 2264 let
adamc@78 2265 val needed = foldl (fn ((sgi, _), needed) =>
adamc@78 2266 case sgi of
adamc@78 2267 L'.SgiConAbs (x, _, _) => SS.add (needed, x)
adamc@78 2268 | _ => needed)
adamc@78 2269 SS.empty sgis
adamc@78 2270
adamc@78 2271 val needed = foldl (fn ((d, _), needed) =>
adamc@78 2272 case d of
adamc@78 2273 L.DCon (x, _, _) => (SS.delete (needed, x)
adamc@78 2274 handle NotFound => needed)
adamc@78 2275 | L.DOpen _ => SS.empty
adamc@78 2276 | _ => needed)
adamc@78 2277 needed ds
adamc@78 2278 in
adamc@78 2279 case SS.listItems needed of
adamc@78 2280 [] => str
adamc@78 2281 | xs =>
adamc@78 2282 let
adamc@78 2283 val kwild = (L.KWild, #2 str)
adamc@78 2284 val cwild = (L.CWild kwild, #2 str)
adamc@78 2285 val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs
adamc@78 2286 in
adamc@78 2287 (L.StrConst (ds @ ds'), #2 str)
adamc@78 2288 end
adamc@78 2289 end
adamc@78 2290 | _ => str)
adamc@78 2291 | _ => str
adamc@78 2292
adamc@83 2293 val (str', actual, gs2) = elabStr (env, denv) str
adamc@78 2294 in
adamc@158 2295 subSgn (env, denv) (selfifyAt env {str = str', sgn = actual}) formal;
adamc@83 2296 (str', formal, gs1 @ gs2)
adamc@78 2297 end
adamc@76 2298
adamc@76 2299 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2300 in
adamc@76 2301 case #1 (hnormSgn env sgn') of
adamc@76 2302 L'.SgnFun _ =>
adamc@76 2303 (case #1 str' of
adamc@76 2304 L'.StrFun _ => ()
adamc@76 2305 | _ => strError env (FunctorRebind loc))
adamc@76 2306 | _ => ();
adamc@76 2307
adamc@88 2308 ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs))
adamc@76 2309 end
adamc@76 2310
adamc@76 2311 | L.DFfiStr (x, sgn) =>
adamc@76 2312 let
adamc@83 2313 val (sgn', gs') = elabSgn (env, denv) sgn
adamc@76 2314
adamc@76 2315 val (env', n) = E.pushStrNamed env x sgn'
adamc@76 2316 in
adamc@88 2317 ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs))
adamc@76 2318 end
adamc@76 2319
adamc@76 2320 | L.DOpen (m, ms) =>
adamc@88 2321 (case E.lookupStr env m of
adamc@88 2322 NONE => (strError env (UnboundStr (loc, m));
adamc@88 2323 ([], (env, denv, [])))
adamc@88 2324 | SOME (n, sgn) =>
adamc@88 2325 let
adamc@88 2326 val (_, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@88 2327 case E.projectStr env {str = str, sgn = sgn, field = m} of
adamc@88 2328 NONE => (strError env (UnboundStr (loc, m));
adamc@88 2329 (strerror, sgnerror))
adamc@88 2330 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@88 2331 ((L'.StrVar n, loc), sgn) ms
adamc@83 2332
adamc@88 2333 val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn}
adamc@88 2334 val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms}
adamc@88 2335 in
adamc@88 2336 (ds, (env', denv', []))
adamc@88 2337 end)
adamc@88 2338
adamc@88 2339 | L.DConstraint (c1, c2) =>
adamc@88 2340 let
adamc@88 2341 val (c1', k1, gs1) = elabCon (env, denv) c1
adamc@88 2342 val (c2', k2, gs2) = elabCon (env, denv) c2
adamc@90 2343 val gs3 = D.prove env denv (c1', c2', loc)
adamc@88 2344
adamc@90 2345 val (denv', gs4) = D.assert env denv (c1', c2')
adamc@88 2346 in
adamc@88 2347 checkKind env c1' k1 (L'.KRecord (kunif loc), loc);
adamc@88 2348 checkKind env c2' k2 (L'.KRecord (kunif loc), loc);
adamc@88 2349
adamc@90 2350 ([(L'.DConstraint (c1', c2'), loc)], (env, denv', gs1 @ gs2 @ gs3 @ gs4))
adamc@88 2351 end
adamc@88 2352
adamc@88 2353 | L.DOpenConstraints (m, ms) =>
adamc@88 2354 let
adamc@88 2355 val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms}
adamc@88 2356 in
adamc@88 2357 ([], (env, denv, []))
adamc@88 2358 end
adamc@3 2359
adamc@109 2360 | L.DExport str =>
adamc@100 2361 let
adamc@109 2362 val (str', sgn, gs) = elabStr (env, denv) str
adamc@100 2363
adamc@109 2364 val sgn =
adamc@109 2365 case #1 (hnormSgn env sgn) of
adamc@109 2366 L'.SgnConst sgis =>
adamc@109 2367 let
adamc@146 2368 fun doOne (all as (sgi, _), env) =
adamc@146 2369 (case sgi of
adamc@146 2370 L'.SgiVal (x, n, t) =>
adamc@146 2371 (case hnormCon (env, denv) t of
adamc@146 2372 ((L'.TFun (dom, ran), _), []) =>
adamc@146 2373 (case (hnormCon (env, denv) dom, hnormCon (env, denv) ran) of
adamc@146 2374 (((L'.TRecord domR, _), []),
adamc@146 2375 ((L'.CApp (tf, arg3), _), [])) =>
adamc@146 2376 (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of
adamc@146 2377 (((L'.CApp (tf, arg2), _), []),
adamc@146 2378 (((L'.CRecord (_, []), _), []))) =>
adamc@146 2379 (case (hnormCon (env, denv) tf) of
adamc@146 2380 ((L'.CApp (tf, arg1), _), []) =>
adamc@146 2381 (case (hnormCon (env, denv) tf,
adamc@146 2382 hnormCon (env, denv) domR,
adamc@152 2383 hnormCon (env, denv) arg1,
adamc@146 2384 hnormCon (env, denv) arg2) of
adamc@152 2385 ((tf, []), (domR, []), (arg1, []),
adamc@146 2386 ((L'.CRecord (_, []), _), [])) =>
adamc@146 2387 let
adamc@146 2388 val t = (L'.CApp (tf, arg1), loc)
adamc@146 2389 val t = (L'.CApp (t, arg2), loc)
adamc@146 2390 val t = (L'.CApp (t, arg3), loc)
adamc@146 2391 in
adamc@146 2392 (L'.SgiVal (x, n, (L'.TFun ((L'.TRecord domR, loc),
adamc@146 2393 t),
adamc@146 2394 loc)), loc)
adamc@146 2395 end
adamc@146 2396 | _ => all)
adamc@146 2397 | _ => all)
adamc@146 2398 | _ => all)
adamc@146 2399 | _ => all)
adamc@146 2400 | _ => all)
adamc@146 2401 | _ => all,
adamc@146 2402 E.sgiBinds env all)
adamc@109 2403 in
adamc@146 2404 (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc)
adamc@109 2405 end
adamc@109 2406 | _ => sgn
adamc@100 2407 in
adamc@109 2408 ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs))
adamc@100 2409 end
adamc@100 2410
adamc@83 2411 and elabStr (env, denv) (str, loc) =
adamc@31 2412 case str of
adamc@31 2413 L.StrConst ds =>
adamc@31 2414 let
adamc@88 2415 val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds
adamc@123 2416 val sgis = ListUtil.mapConcat sgiOfDecl ds'
adamc@63 2417
adamc@63 2418 val (sgis, _, _, _, _) =
adamc@64 2419 foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) =>
adamc@63 2420 case sgi of
adamc@64 2421 L'.SgiConAbs (x, n, k) =>
adamc@64 2422 let
adamc@64 2423 val (cons, x) =
adamc@64 2424 if SS.member (cons, x) then
adamc@64 2425 (cons, "?" ^ x)
adamc@64 2426 else
adamc@64 2427 (SS.add (cons, x), x)
adamc@64 2428 in
adamc@64 2429 ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2430 end
adamc@64 2431 | L'.SgiCon (x, n, k, c) =>
adamc@64 2432 let
adamc@64 2433 val (cons, x) =
adamc@64 2434 if SS.member (cons, x) then
adamc@64 2435 (cons, "?" ^ x)
adamc@64 2436 else
adamc@64 2437 (SS.add (cons, x), x)
adamc@64 2438 in
adamc@64 2439 ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2440 end
adamc@156 2441 | L'.SgiDatatype (x, n, xncs) =>
adamc@156 2442 let
adamc@156 2443 val (cons, x) =
adamc@156 2444 if SS.member (cons, x) then
adamc@156 2445 (cons, "?" ^ x)
adamc@156 2446 else
adamc@156 2447 (SS.add (cons, x), x)
adamc@156 2448
adamc@156 2449 val (xncs, vals) =
adamc@156 2450 ListUtil.foldlMap
adamc@156 2451 (fn ((x, n, t), vals) =>
adamc@156 2452 if SS.member (vals, x) then
adamc@156 2453 (("?" ^ x, n, t), vals)
adamc@156 2454 else
adamc@156 2455 ((x, n, t), SS.add (vals, x)))
adamc@156 2456 vals xncs
adamc@156 2457 in
adamc@156 2458 ((L'.SgiDatatype (x, n, xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 2459 end
adamc@162 2460 | L'.SgiDatatypeImp (x, n, m1, ms, x', xncs) =>
adamc@156 2461 let
adamc@156 2462 val (cons, x) =
adamc@156 2463 if SS.member (cons, x) then
adamc@156 2464 (cons, "?" ^ x)
adamc@156 2465 else
adamc@156 2466 (SS.add (cons, x), x)
adamc@156 2467 in
adamc@162 2468 ((L'.SgiDatatypeImp (x, n, m1, ms, x', xncs), loc) :: sgis, cons, vals, sgns, strs)
adamc@156 2469 end
adamc@64 2470 | L'.SgiVal (x, n, c) =>
adamc@64 2471 let
adamc@64 2472 val (vals, x) =
adamc@64 2473 if SS.member (vals, x) then
adamc@64 2474 (vals, "?" ^ x)
adamc@64 2475 else
adamc@64 2476 (SS.add (vals, x), x)
adamc@64 2477 in
adamc@64 2478 ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2479 end
adamc@64 2480 | L'.SgiSgn (x, n, sgn) =>
adamc@64 2481 let
adamc@64 2482 val (sgns, x) =
adamc@64 2483 if SS.member (sgns, x) then
adamc@64 2484 (sgns, "?" ^ x)
adamc@64 2485 else
adamc@64 2486 (SS.add (sgns, x), x)
adamc@64 2487 in
adamc@64 2488 ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@64 2489 end
adamc@64 2490
adamc@64 2491 | L'.SgiStr (x, n, sgn) =>
adamc@64 2492 let
adamc@64 2493 val (strs, x) =
adamc@64 2494 if SS.member (strs, x) then
adamc@64 2495 (strs, "?" ^ x)
adamc@64 2496 else
adamc@64 2497 (SS.add (strs, x), x)
adamc@64 2498 in
adamc@64 2499 ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs)
adamc@88 2500 end
adamc@88 2501 | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs))
adamc@64 2502
adamc@63 2503 ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis
adamc@31 2504 in
adamc@83 2505 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs)
adamc@31 2506 end
adamc@31 2507 | L.StrVar x =>
adamc@31 2508 (case E.lookupStr env x of
adamc@31 2509 NONE =>
adamc@31 2510 (strError env (UnboundStr (loc, x));
adamc@83 2511 (strerror, sgnerror, []))
adamc@83 2512 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, []))
adamc@34 2513 | L.StrProj (str, x) =>
adamc@34 2514 let
adamc@83 2515 val (str', sgn, gs) = elabStr (env, denv) str
adamc@34 2516 in
adamc@34 2517 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 2518 NONE => (strError env (UnboundStr (loc, x));
adamc@83 2519 (strerror, sgnerror, []))
adamc@83 2520 | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs)
adamc@34 2521 end
adamc@41 2522 | L.StrFun (m, dom, ranO, str) =>
adamc@41 2523 let
adamc@83 2524 val (dom', gs1) = elabSgn (env, denv) dom
adamc@41 2525 val (env', n) = E.pushStrNamed env m dom'
adamc@83 2526 val (str', actual, gs2) = elabStr (env', denv) str
adamc@41 2527
adamc@83 2528 val (formal, gs3) =
adamc@41 2529 case ranO of
adamc@83 2530 NONE => (actual, [])
adamc@41 2531 | SOME ran =>
adamc@41 2532 let
adamc@83 2533 val (ran', gs) = elabSgn (env', denv) ran
adamc@41 2534 in
adamc@86 2535 subSgn (env', denv) actual ran';
adamc@83 2536 (ran', gs)
adamc@41 2537 end
adamc@41 2538 in
adamc@41 2539 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@83 2540 (L'.SgnFun (m, n, dom', formal), loc),
adamc@83 2541 gs1 @ gs2 @ gs3)
adamc@41 2542 end
adamc@44 2543 | L.StrApp (str1, str2) =>
adamc@44 2544 let
adamc@83 2545 val (str1', sgn1, gs1) = elabStr (env, denv) str1
adamc@83 2546 val (str2', sgn2, gs2) = elabStr (env, denv) str2
adamc@44 2547 in
adamc@44 2548 case #1 (hnormSgn env sgn1) of
adamc@83 2549 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2550 | L'.SgnFun (m, n, dom, ran) =>
adamc@86 2551 (subSgn (env, denv) sgn2 dom;
adamc@44 2552 case #1 (hnormSgn env ran) of
adamc@83 2553 L'.SgnError => (strerror, sgnerror, [])
adamc@44 2554 | L'.SgnConst sgis =>
adamc@44 2555 ((L'.StrApp (str1', str2'), loc),
adamc@83 2556 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc),
adamc@83 2557 gs1 @ gs2)
adamc@44 2558 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 2559 | _ => (strError env (NotFunctor sgn1);
adamc@83 2560 (strerror, sgnerror, []))
adamc@44 2561 end
adamc@31 2562
adamc@56 2563 fun elabFile basis env file =
adamc@56 2564 let
adamc@83 2565 val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan)
adamc@83 2566 val () = case gs of
adamc@83 2567 [] => ()
adamc@83 2568 | _ => raise Fail "Unresolved disjointness constraints in Basis"
adamc@83 2569
adamc@56 2570 val (env', basis_n) = E.pushStrNamed env "Basis" sgn
adamc@56 2571
adamc@88 2572 val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn}
adamc@56 2573
adamc@56 2574 fun discoverC r x =
adamc@56 2575 case E.lookupC env' x of
adamc@56 2576 E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis")
adamc@56 2577 | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis")
adamc@56 2578 | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc)
adamc@56 2579
adamc@56 2580 val () = discoverC int "int"
adamc@56 2581 val () = discoverC float "float"
adamc@56 2582 val () = discoverC string "string"
adamc@56 2583
adamc@83 2584 fun elabDecl' (d, (env, gs)) =
adamc@76 2585 let
adamc@76 2586 val () = resetKunif ()
adamc@76 2587 val () = resetCunif ()
adamc@88 2588 val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs))
adamc@76 2589 in
adamc@76 2590 if ErrorMsg.anyErrors () then
adamc@76 2591 ()
adamc@76 2592 else (
adamc@76 2593 if List.exists kunifsInDecl ds then
adamc@91 2594 declError env (KunifsRemain ds)
adamc@76 2595 else
adamc@76 2596 ();
adamc@76 2597
adamc@76 2598 case ListUtil.search cunifsInDecl ds of
adamc@76 2599 NONE => ()
adamc@76 2600 | SOME loc =>
adamc@91 2601 declError env (CunifsRemain ds)
adamc@76 2602 );
adamc@76 2603
adamc@83 2604 (ds, (env, gs))
adamc@76 2605 end
adamc@76 2606
adamc@83 2607 val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file
adamc@56 2608 in
adamc@84 2609 if ErrorMsg.anyErrors () then
adamc@84 2610 ()
adamc@84 2611 else
adamc@90 2612 app (fn (loc, env, denv, c1, c2) =>
adamc@84 2613 case D.prove env denv (c1, c2, loc) of
adamc@84 2614 [] => ()
adamc@84 2615 | _ =>
adamc@86 2616 (ErrorMsg.errorAt loc "Couldn't prove field name disjointness";
adamc@84 2617 eprefaces' [("Con 1", p_con env c1),
adamc@90 2618 ("Con 2", p_con env c2),
adamc@90 2619 ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)),
adamc@90 2620 ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))])) gs;
adamc@83 2621
adamc@56 2622 (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file
adamc@56 2623 end
adamc@2 2624
adamc@2 2625 end