annotate src/elaborate.sml @ 348:b88f4297167f

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