annotate src/elaborate.sml @ 238:44a1663ad893

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