annotate src/elaborate.sml @ 44:a9f3ce2d1b9b

Elaborating functor applications
author Adam Chlipala <adamc@hcoop.net>
date Thu, 19 Jun 2008 17:04:08 -0400
parents d94c484337d0
children 44a1bc863f0f
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@2 35
adamc@3 36 open Print
adamc@3 37 open ElabPrint
adamc@3 38
adamc@2 39 fun elabExplicitness e =
adamc@2 40 case e of
adamc@2 41 L.Explicit => L'.Explicit
adamc@2 42 | L.Implicit => L'.Implicit
adamc@2 43
adamc@2 44 fun occursKind r =
adamc@2 45 U.Kind.exists (fn L'.KUnif (_, r') => r = r'
adamc@2 46 | _ => false)
adamc@2 47
adamc@3 48 datatype kunify_error =
adamc@2 49 KOccursCheckFailed of L'.kind * L'.kind
adamc@2 50 | KIncompatible of L'.kind * L'.kind
adamc@2 51
adamc@3 52 exception KUnify' of kunify_error
adamc@3 53
adamc@3 54 fun kunifyError err =
adamc@2 55 case err of
adamc@2 56 KOccursCheckFailed (k1, k2) =>
adamc@3 57 eprefaces "Kind occurs check failed"
adamc@3 58 [("Kind 1", p_kind k1),
adamc@3 59 ("Kind 2", p_kind k2)]
adamc@2 60 | KIncompatible (k1, k2) =>
adamc@3 61 eprefaces "Incompatible kinds"
adamc@3 62 [("Kind 1", p_kind k1),
adamc@3 63 ("Kind 2", p_kind k2)]
adamc@2 64
adamc@3 65 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@2 66 let
adamc@3 67 fun err f = raise KUnify' (f (k1All, k2All))
adamc@2 68 in
adamc@2 69 case (k1, k2) of
adamc@2 70 (L'.KType, L'.KType) => ()
adamc@2 71 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@3 72 (unifyKinds' d1 d2;
adamc@3 73 unifyKinds' r1 r2)
adamc@2 74 | (L'.KName, L'.KName) => ()
adamc@3 75 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
adamc@2 76
adamc@2 77 | (L'.KError, _) => ()
adamc@2 78 | (_, L'.KError) => ()
adamc@2 79
adamc@3 80 | (L'.KUnif (_, ref (SOME k1All)), _) => unifyKinds' k1All k2All
adamc@3 81 | (_, L'.KUnif (_, ref (SOME k2All))) => unifyKinds' k1All k2All
adamc@2 82
adamc@2 83 | (L'.KUnif (_, r1), L'.KUnif (_, r2)) =>
adamc@2 84 if r1 = r2 then
adamc@2 85 ()
adamc@2 86 else
adamc@2 87 r1 := SOME k2All
adamc@2 88
adamc@2 89 | (L'.KUnif (_, r), _) =>
adamc@2 90 if occursKind r k2All then
adamc@2 91 err KOccursCheckFailed
adamc@2 92 else
adamc@2 93 r := SOME k2All
adamc@2 94 | (_, L'.KUnif (_, r)) =>
adamc@2 95 if occursKind r k1All then
adamc@2 96 err KOccursCheckFailed
adamc@2 97 else
adamc@2 98 r := SOME k1All
adamc@2 99
adamc@2 100 | _ => err KIncompatible
adamc@2 101 end
adamc@2 102
adamc@3 103 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@3 104
adamc@3 105 fun unifyKinds k1 k2 =
adamc@3 106 unifyKinds' k1 k2
adamc@3 107 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@3 108
adamc@3 109 datatype con_error =
adamc@3 110 UnboundCon of ErrorMsg.span * string
adamc@34 111 | UnboundStrInCon of ErrorMsg.span * string
adamc@3 112 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
adamc@3 113
adamc@5 114 fun conError env err =
adamc@3 115 case err of
adamc@3 116 UnboundCon (loc, s) =>
adamc@3 117 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
adamc@34 118 | UnboundStrInCon (loc, s) =>
adamc@34 119 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@3 120 | WrongKind (c, k1, k2, kerr) =>
adamc@3 121 (ErrorMsg.errorAt (#2 c) "Wrong kind";
adamc@5 122 eprefaces' [("Constructor", p_con env c),
adamc@5 123 ("Have kind", p_kind k1),
adamc@5 124 ("Need kind", p_kind k2)];
adamc@3 125 kunifyError kerr)
adamc@3 126
adamc@5 127 fun checkKind env c k1 k2 =
adamc@3 128 unifyKinds k1 k2
adamc@3 129 handle KUnify (k1, k2, err) =>
adamc@5 130 conError env (WrongKind (c, k1, k2, err))
adamc@3 131
adamc@3 132 val dummy = ErrorMsg.dummySpan
adamc@3 133
adamc@3 134 val ktype = (L'.KType, dummy)
adamc@3 135 val kname = (L'.KName, dummy)
adamc@12 136 val ktype_record = (L'.KRecord ktype, dummy)
adamc@3 137
adamc@3 138 val cerror = (L'.CError, dummy)
adamc@3 139 val kerror = (L'.KError, dummy)
adamc@10 140 val eerror = (L'.EError, dummy)
adamc@31 141 val sgnerror = (L'.SgnError, dummy)
adamc@31 142 val strerror = (L'.StrError, dummy)
adamc@3 143
adamc@3 144 local
adamc@3 145 val count = ref 0
adamc@3 146 in
adamc@3 147
adamc@3 148 fun resetKunif () = count := 0
adamc@3 149
adamc@3 150 fun kunif () =
adamc@3 151 let
adamc@3 152 val n = !count
adamc@3 153 val s = if n <= 26 then
adamc@3 154 str (chr (ord #"A" + n))
adamc@3 155 else
adamc@3 156 "U" ^ Int.toString (n - 26)
adamc@3 157 in
adamc@3 158 count := n + 1;
adamc@3 159 (L'.KUnif (s, ref NONE), dummy)
adamc@3 160 end
adamc@3 161
adamc@3 162 end
adamc@3 163
adamc@10 164 local
adamc@10 165 val count = ref 0
adamc@10 166 in
adamc@10 167
adamc@10 168 fun resetCunif () = count := 0
adamc@10 169
adamc@10 170 fun cunif k =
adamc@10 171 let
adamc@10 172 val n = !count
adamc@10 173 val s = if n <= 26 then
adamc@10 174 str (chr (ord #"A" + n))
adamc@10 175 else
adamc@10 176 "U" ^ Int.toString (n - 26)
adamc@10 177 in
adamc@10 178 count := n + 1;
adamc@10 179 (L'.CUnif (k, s, ref NONE), dummy)
adamc@10 180 end
adamc@10 181
adamc@10 182 end
adamc@10 183
adamc@18 184 fun elabKind (k, loc) =
adamc@18 185 case k of
adamc@18 186 L.KType => (L'.KType, loc)
adamc@18 187 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
adamc@18 188 | L.KName => (L'.KName, loc)
adamc@18 189 | L.KRecord k => (L'.KRecord (elabKind k), loc)
adamc@18 190 | L.KWild => kunif ()
adamc@18 191
adamc@3 192 fun elabCon env (c, loc) =
adamc@3 193 case c of
adamc@3 194 L.CAnnot (c, k) =>
adamc@3 195 let
adamc@3 196 val k' = elabKind k
adamc@3 197 val (c', ck) = elabCon env c
adamc@3 198 in
adamc@5 199 checkKind env c' ck k';
adamc@3 200 (c', k')
adamc@3 201 end
adamc@3 202
adamc@3 203 | L.TFun (t1, t2) =>
adamc@3 204 let
adamc@3 205 val (t1', k1) = elabCon env t1
adamc@3 206 val (t2', k2) = elabCon env t2
adamc@3 207 in
adamc@5 208 checkKind env t1' k1 ktype;
adamc@5 209 checkKind env t2' k2 ktype;
adamc@3 210 ((L'.TFun (t1', t2'), loc), ktype)
adamc@3 211 end
adamc@3 212 | L.TCFun (e, x, k, t) =>
adamc@3 213 let
adamc@3 214 val e' = elabExplicitness e
adamc@3 215 val k' = elabKind k
adamc@3 216 val env' = E.pushCRel env x k'
adamc@3 217 val (t', tk) = elabCon env' t
adamc@3 218 in
adamc@5 219 checkKind env t' tk ktype;
adamc@3 220 ((L'.TCFun (e', x, k', t'), loc), ktype)
adamc@3 221 end
adamc@3 222 | L.TRecord c =>
adamc@3 223 let
adamc@3 224 val (c', ck) = elabCon env c
adamc@3 225 val k = (L'.KRecord ktype, loc)
adamc@3 226 in
adamc@5 227 checkKind env c' ck k;
adamc@3 228 ((L'.TRecord c', loc), ktype)
adamc@3 229 end
adamc@3 230
adamc@34 231 | L.CVar ([], s) =>
adamc@3 232 (case E.lookupC env s of
adamc@9 233 E.NotBound =>
adamc@5 234 (conError env (UnboundCon (loc, s));
adamc@3 235 (cerror, kerror))
adamc@9 236 | E.Rel (n, k) =>
adamc@3 237 ((L'.CRel n, loc), k)
adamc@9 238 | E.Named (n, k) =>
adamc@3 239 ((L'.CNamed n, loc), k))
adamc@34 240 | L.CVar (m1 :: ms, s) =>
adamc@34 241 (case E.lookupStr env m1 of
adamc@42 242 NONE => (conError env (UnboundStrInCon (loc, m1));
adamc@34 243 (cerror, kerror))
adamc@34 244 | SOME (n, sgn) =>
adamc@34 245 let
adamc@34 246 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 247 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 248 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@34 249 (strerror, sgnerror))
adamc@34 250 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 251 ((L'.StrVar n, loc), sgn) ms
adamc@34 252
adamc@34 253 val k = case E.projectCon env {sgn = sgn, str = str, field = s} of
adamc@34 254 NONE => (conError env (UnboundCon (loc, s));
adamc@34 255 kerror)
adamc@34 256 | SOME (k, _) => k
adamc@34 257 in
adamc@34 258 ((L'.CModProj (n, ms, s), loc), k)
adamc@34 259 end)
adamc@34 260
adamc@3 261 | L.CApp (c1, c2) =>
adamc@3 262 let
adamc@3 263 val (c1', k1) = elabCon env c1
adamc@3 264 val (c2', k2) = elabCon env c2
adamc@3 265 val dom = kunif ()
adamc@3 266 val ran = kunif ()
adamc@3 267 in
adamc@5 268 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@5 269 checkKind env c2' k2 dom;
adamc@3 270 ((L'.CApp (c1', c2'), loc), ran)
adamc@3 271 end
adamc@8 272 | L.CAbs (x, k, t) =>
adamc@3 273 let
adamc@3 274 val k' = elabKind k
adamc@3 275 val env' = E.pushCRel env x k'
adamc@3 276 val (t', tk) = elabCon env' t
adamc@3 277 in
adamc@8 278 ((L'.CAbs (x, k', t'), loc),
adamc@3 279 (L'.KArrow (k', tk), loc))
adamc@3 280 end
adamc@3 281
adamc@3 282 | L.CName s =>
adamc@3 283 ((L'.CName s, loc), kname)
adamc@3 284
adamc@3 285 | L.CRecord xcs =>
adamc@3 286 let
adamc@3 287 val k = kunif ()
adamc@3 288
adamc@3 289 val xcs' = map (fn (x, c) =>
adamc@3 290 let
adamc@3 291 val (x', xk) = elabCon env x
adamc@3 292 val (c', ck) = elabCon env c
adamc@3 293 in
adamc@5 294 checkKind env x' xk kname;
adamc@5 295 checkKind env c' ck k;
adamc@3 296 (x', c')
adamc@3 297 end) xcs
adamc@3 298 in
adamc@5 299 ((L'.CRecord (k, xcs'), loc), (L'.KRecord k, loc))
adamc@3 300 end
adamc@3 301 | L.CConcat (c1, c2) =>
adamc@3 302 let
adamc@3 303 val (c1', k1) = elabCon env c1
adamc@3 304 val (c2', k2) = elabCon env c2
adamc@3 305 val ku = kunif ()
adamc@3 306 val k = (L'.KRecord ku, loc)
adamc@3 307 in
adamc@5 308 checkKind env c1' k1 k;
adamc@5 309 checkKind env c2' k2 k;
adamc@3 310 ((L'.CConcat (c1', c2'), loc), k)
adamc@3 311 end
adamc@3 312
adamc@18 313 | L.CWild k =>
adamc@18 314 let
adamc@18 315 val k' = elabKind k
adamc@18 316 in
adamc@18 317 (cunif k', k')
adamc@18 318 end
adamc@18 319
adamc@6 320 fun kunifsRemain k =
adamc@6 321 case k of
adamc@6 322 L'.KUnif (_, ref NONE) => true
adamc@6 323 | _ => false
adamc@10 324 fun cunifsRemain c =
adamc@10 325 case c of
adamc@10 326 L'.CUnif (_, _, ref NONE) => true
adamc@10 327 | _ => false
adamc@6 328
adamc@6 329 val kunifsInKind = U.Kind.exists kunifsRemain
adamc@6 330 val kunifsInCon = U.Con.exists {kind = kunifsRemain,
adamc@6 331 con = fn _ => false}
adamc@10 332 val kunifsInExp = U.Exp.exists {kind = kunifsRemain,
adamc@10 333 con = fn _ => false,
adamc@10 334 exp = fn _ => false}
adamc@10 335
adamc@10 336 val cunifsInCon = U.Con.exists {kind = fn _ => false,
adamc@10 337 con = cunifsRemain}
adamc@10 338 val cunifsInExp = U.Exp.exists {kind = fn _ => false,
adamc@10 339 con = cunifsRemain,
adamc@10 340 exp = fn _ => false}
adamc@10 341
adamc@10 342 fun occursCon r =
adamc@10 343 U.Con.exists {kind = fn _ => false,
adamc@10 344 con = fn L'.CUnif (_, _, r') => r = r'
adamc@10 345 | _ => false}
adamc@10 346
adamc@10 347 datatype cunify_error =
adamc@10 348 CKind of L'.kind * L'.kind * kunify_error
adamc@10 349 | COccursCheckFailed of L'.con * L'.con
adamc@10 350 | CIncompatible of L'.con * L'.con
adamc@10 351 | CExplicitness of L'.con * L'.con
adamc@12 352 | CKindof of L'.con
adamc@12 353 | CRecordFailure
adamc@10 354
adamc@10 355 exception CUnify' of cunify_error
adamc@10 356
adamc@10 357 fun cunifyError env err =
adamc@10 358 case err of
adamc@10 359 CKind (k1, k2, kerr) =>
adamc@10 360 (eprefaces "Kind unification failure"
adamc@10 361 [("Kind 1", p_kind k1),
adamc@10 362 ("Kind 2", p_kind k2)];
adamc@10 363 kunifyError kerr)
adamc@10 364 | COccursCheckFailed (c1, c2) =>
adamc@10 365 eprefaces "Constructor occurs check failed"
adamc@10 366 [("Con 1", p_con env c1),
adamc@10 367 ("Con 2", p_con env c2)]
adamc@10 368 | CIncompatible (c1, c2) =>
adamc@10 369 eprefaces "Incompatible constructors"
adamc@10 370 [("Con 1", p_con env c1),
adamc@10 371 ("Con 2", p_con env c2)]
adamc@10 372 | CExplicitness (c1, c2) =>
adamc@10 373 eprefaces "Differing constructor function explicitness"
adamc@10 374 [("Con 1", p_con env c1),
adamc@10 375 ("Con 2", p_con env c2)]
adamc@12 376 | CKindof c =>
adamc@12 377 eprefaces "Kind unification variable blocks kindof calculation"
adamc@12 378 [("Con", p_con env c)]
adamc@12 379 | CRecordFailure =>
adamc@12 380 eprefaces "Can't unify record constructors" []
adamc@10 381
adamc@13 382 exception SynUnif = E.SynUnif
adamc@13 383 val liftConInCon = E.liftConInCon
adamc@12 384
adamc@12 385 val subConInCon =
adamc@12 386 U.Con.mapB {kind = fn k => k,
adamc@12 387 con = fn (xn, rep) => fn c =>
adamc@12 388 case c of
adamc@12 389 L'.CRel xn' =>
adamc@12 390 if xn = xn' then
adamc@12 391 #1 rep
adamc@12 392 else
adamc@12 393 c
adamc@17 394 (*| L'.CUnif _ => raise SynUnif*)
adamc@12 395 | _ => c,
adamc@12 396 bind = fn ((xn, rep), U.Con.Rel _) => (xn+1, liftConInCon 0 rep)
adamc@12 397 | (ctx, _) => ctx}
adamc@12 398
adamc@43 399 fun subStrInSgn (m1, m2) =
adamc@43 400 U.Sgn.map {kind = fn k => k,
adamc@43 401 con = fn c as L'.CModProj (m1', ms, x) =>
adamc@43 402 if m1 = m1' then
adamc@43 403 L'.CModProj (m2, ms, x)
adamc@43 404 else
adamc@43 405 c
adamc@43 406 | c => c,
adamc@43 407 sgn_item = fn sgi => sgi,
adamc@43 408 sgn = fn sgn => sgn}
adamc@43 409
adamc@12 410 type record_summary = {
adamc@12 411 fields : (L'.con * L'.con) list,
adamc@12 412 unifs : (L'.con * L'.con option ref) list,
adamc@12 413 others : L'.con list
adamc@12 414 }
adamc@12 415
adamc@12 416 fun summaryToCon {fields, unifs, others} =
adamc@12 417 let
adamc@12 418 val c = (L'.CRecord (ktype, []), dummy)
adamc@12 419 val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others
adamc@12 420 val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs
adamc@12 421 in
adamc@12 422 (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy)
adamc@12 423 end
adamc@12 424
adamc@12 425 fun p_summary env s = p_con env (summaryToCon s)
adamc@12 426
adamc@12 427 exception CUnify of L'.con * L'.con * cunify_error
adamc@12 428
adamc@12 429 fun hnormKind (kAll as (k, _)) =
adamc@12 430 case k of
adamc@12 431 L'.KUnif (_, ref (SOME k)) => hnormKind k
adamc@12 432 | _ => kAll
adamc@12 433
adamc@12 434 fun kindof env (c, loc) =
adamc@12 435 case c of
adamc@12 436 L'.TFun _ => ktype
adamc@12 437 | L'.TCFun _ => ktype
adamc@12 438 | L'.TRecord _ => ktype
adamc@12 439
adamc@12 440 | L'.CRel xn => #2 (E.lookupCRel env xn)
adamc@12 441 | L'.CNamed xn => #2 (E.lookupCNamed env xn)
adamc@34 442 | L'.CModProj (n, ms, x) =>
adamc@34 443 let
adamc@34 444 val (_, sgn) = E.lookupStrNamed env n
adamc@34 445 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 446 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 447 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 448 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 449 ((L'.StrVar n, loc), sgn) ms
adamc@34 450 in
adamc@34 451 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@34 452 NONE => raise Fail "kindof: Unknown con in structure"
adamc@34 453 | SOME (k, _) => k
adamc@34 454 end
adamc@34 455
adamc@12 456 | L'.CApp (c, _) =>
adamc@12 457 (case #1 (hnormKind (kindof env c)) of
adamc@12 458 L'.KArrow (_, k) => k
adamc@12 459 | L'.KError => kerror
adamc@12 460 | _ => raise CUnify' (CKindof c))
adamc@12 461 | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc)
adamc@12 462
adamc@12 463 | L'.CName _ => kname
adamc@12 464
adamc@12 465 | L'.CRecord (k, _) => (L'.KRecord k, loc)
adamc@12 466 | L'.CConcat (c, _) => kindof env c
adamc@12 467
adamc@12 468 | L'.CError => kerror
adamc@12 469 | L'.CUnif (k, _, _) => k
adamc@12 470
adamc@12 471 fun unifyRecordCons env (c1, c2) =
adamc@12 472 let
adamc@12 473 val k1 = kindof env c1
adamc@12 474 val k2 = kindof env c2
adamc@12 475 in
adamc@12 476 unifyKinds k1 k2;
adamc@12 477 unifySummaries env (k1, recordSummary env c1, recordSummary env c2)
adamc@12 478 end
adamc@12 479
adamc@12 480 and recordSummary env c : record_summary =
adamc@12 481 case hnormCon env c of
adamc@12 482 (L'.CRecord (_, xcs), _) => {fields = xcs, unifs = [], others = []}
adamc@12 483 | (L'.CConcat (c1, c2), _) =>
adamc@12 484 let
adamc@12 485 val s1 = recordSummary env c1
adamc@12 486 val s2 = recordSummary env c2
adamc@12 487 in
adamc@12 488 {fields = #fields s1 @ #fields s2,
adamc@12 489 unifs = #unifs s1 @ #unifs s2,
adamc@12 490 others = #others s1 @ #others s2}
adamc@12 491 end
adamc@12 492 | (L'.CUnif (_, _, ref (SOME c)), _) => recordSummary env c
adamc@12 493 | c' as (L'.CUnif (_, _, r), _) => {fields = [], unifs = [(c', r)], others = []}
adamc@12 494 | c' => {fields = [], unifs = [], others = [c']}
adamc@12 495
adamc@12 496 and consEq env (c1, c2) =
adamc@12 497 (unifyCons env c1 c2;
adamc@12 498 true)
adamc@12 499 handle CUnify _ => false
adamc@12 500
adamc@12 501 and unifySummaries env (k, s1 : record_summary, s2 : record_summary) =
adamc@12 502 let
adamc@14 503 (*val () = eprefaces "Summaries" [("#1", p_summary env s1),
adamc@14 504 ("#2", p_summary env s2)]*)
adamc@12 505
adamc@12 506 fun eatMatching p (ls1, ls2) =
adamc@12 507 let
adamc@12 508 fun em (ls1, ls2, passed1) =
adamc@12 509 case ls1 of
adamc@12 510 [] => (rev passed1, ls2)
adamc@12 511 | h1 :: t1 =>
adamc@12 512 let
adamc@12 513 fun search (ls2', passed2) =
adamc@12 514 case ls2' of
adamc@12 515 [] => em (t1, ls2, h1 :: passed1)
adamc@12 516 | h2 :: t2 =>
adamc@12 517 if p (h1, h2) then
adamc@12 518 em (t1, List.revAppend (passed2, t2), passed1)
adamc@12 519 else
adamc@12 520 search (t2, h2 :: passed2)
adamc@12 521 in
adamc@12 522 search (ls2, [])
adamc@12 523 end
adamc@12 524 in
adamc@12 525 em (ls1, ls2, [])
adamc@12 526 end
adamc@12 527
adamc@12 528 val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) =>
adamc@12 529 if consEq env (x1, x2) then
adamc@12 530 (unifyCons env c1 c2;
adamc@12 531 true)
adamc@12 532 else
adamc@12 533 false) (#fields s1, #fields s2)
adamc@14 534 (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}),
adamc@14 535 ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*)
adamc@12 536 val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2)
adamc@12 537 val (others1, others2) = eatMatching (consEq env) (#others s1, #others s2)
adamc@12 538
adamc@12 539 fun unifFields (fs, others, unifs) =
adamc@12 540 case (fs, others, unifs) of
adamc@12 541 ([], [], _) => ([], [], unifs)
adamc@12 542 | (_, _, []) => (fs, others, [])
adamc@12 543 | (_, _, (_, r) :: rest) =>
adamc@12 544 let
adamc@12 545 val r' = ref NONE
adamc@12 546 val cr' = (L'.CUnif (k, "recd", r'), dummy)
adamc@12 547
adamc@12 548 val prefix = case (fs, others) of
adamc@12 549 ([], other :: others) =>
adamc@12 550 List.foldl (fn (other, c) =>
adamc@12 551 (L'.CConcat (c, other), dummy))
adamc@12 552 other others
adamc@12 553 | (fs, []) =>
adamc@12 554 (L'.CRecord (k, fs), dummy)
adamc@12 555 | (fs, others) =>
adamc@12 556 List.foldl (fn (other, c) =>
adamc@12 557 (L'.CConcat (c, other), dummy))
adamc@12 558 (L'.CRecord (k, fs), dummy) others
adamc@12 559 in
adamc@12 560 r := SOME (L'.CConcat (prefix, cr'), dummy);
adamc@12 561 ([], [], (cr', r') :: rest)
adamc@12 562 end
adamc@12 563
adamc@12 564 val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2)
adamc@12 565 val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1)
adamc@12 566
adamc@12 567 val clear1 = case (fs1, others1) of
adamc@12 568 ([], []) => true
adamc@12 569 | _ => false
adamc@12 570 val clear2 = case (fs2, others2) of
adamc@12 571 ([], []) => true
adamc@12 572 | _ => false
adamc@12 573 val empty = (L'.CRecord (k, []), dummy)
adamc@12 574 fun pairOffUnifs (unifs1, unifs2) =
adamc@12 575 case (unifs1, unifs2) of
adamc@12 576 ([], _) =>
adamc@12 577 if clear1 then
adamc@12 578 List.app (fn (_, r) => r := SOME empty) unifs2
adamc@12 579 else
adamc@12 580 raise CUnify' CRecordFailure
adamc@12 581 | (_, []) =>
adamc@12 582 if clear2 then
adamc@12 583 List.app (fn (_, r) => r := SOME empty) unifs1
adamc@12 584 else
adamc@12 585 raise CUnify' CRecordFailure
adamc@12 586 | ((c1, _) :: rest1, (_, r2) :: rest2) =>
adamc@12 587 (r2 := SOME c1;
adamc@12 588 pairOffUnifs (rest1, rest2))
adamc@12 589 in
adamc@12 590 pairOffUnifs (unifs1, unifs2)
adamc@12 591 end
adamc@12 592
adamc@34 593 and hnormCon env (cAll as (c, loc)) =
adamc@11 594 case c of
adamc@11 595 L'.CUnif (_, _, ref (SOME c)) => hnormCon env c
adamc@11 596
adamc@11 597 | L'.CNamed xn =>
adamc@11 598 (case E.lookupCNamed env xn of
adamc@11 599 (_, _, SOME c') => hnormCon env c'
adamc@11 600 | _ => cAll)
adamc@11 601
adamc@34 602 | L'.CModProj (n, ms, x) =>
adamc@34 603 let
adamc@34 604 val (_, sgn) = E.lookupStrNamed env n
adamc@34 605 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 606 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 607 NONE => raise Fail "hnormCon: Unknown substructure"
adamc@34 608 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 609 ((L'.StrVar n, loc), sgn) ms
adamc@34 610 in
adamc@34 611 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@34 612 NONE => raise Fail "kindof: Unknown con in structure"
adamc@34 613 | SOME (_, NONE) => cAll
adamc@34 614 | SOME (_, SOME c) => hnormCon env c
adamc@34 615 end
adamc@34 616
adamc@12 617 | L'.CApp (c1, c2) =>
adamc@12 618 (case hnormCon env c1 of
adamc@12 619 (L'.CAbs (_, _, cb), _) =>
adamc@12 620 ((hnormCon env (subConInCon (0, c2) cb))
adamc@12 621 handle SynUnif => cAll)
adamc@12 622 | _ => cAll)
adamc@12 623
adamc@12 624 | L'.CConcat (c1, c2) =>
adamc@12 625 (case (hnormCon env c1, hnormCon env c2) of
adamc@12 626 ((L'.CRecord (k, xcs1), loc), (L'.CRecord (_, xcs2), _)) =>
adamc@12 627 (L'.CRecord (k, xcs1 @ xcs2), loc)
adamc@12 628 | _ => cAll)
adamc@12 629
adamc@11 630 | _ => cAll
adamc@11 631
adamc@12 632 and unifyCons' env c1 c2 =
adamc@11 633 unifyCons'' env (hnormCon env c1) (hnormCon env c2)
adamc@11 634
adamc@11 635 and unifyCons'' env (c1All as (c1, _)) (c2All as (c2, _)) =
adamc@10 636 let
adamc@10 637 fun err f = raise CUnify' (f (c1All, c2All))
adamc@12 638
adamc@12 639 fun isRecord () = unifyRecordCons env (c1All, c2All)
adamc@10 640 in
adamc@10 641 case (c1, c2) of
adamc@10 642 (L'.TFun (d1, r1), L'.TFun (d2, r2)) =>
adamc@10 643 (unifyCons' env d1 d2;
adamc@10 644 unifyCons' env r1 r2)
adamc@10 645 | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) =>
adamc@10 646 if expl1 <> expl2 then
adamc@10 647 err CExplicitness
adamc@10 648 else
adamc@10 649 (unifyKinds d1 d2;
adamc@10 650 unifyCons' (E.pushCRel env x1 d1) r1 r2)
adamc@10 651 | (L'.TRecord r1, L'.TRecord r2) => unifyCons' env r1 r2
adamc@10 652
adamc@10 653 | (L'.CRel n1, L'.CRel n2) =>
adamc@10 654 if n1 = n2 then
adamc@10 655 ()
adamc@10 656 else
adamc@10 657 err CIncompatible
adamc@10 658 | (L'.CNamed n1, L'.CNamed n2) =>
adamc@10 659 if n1 = n2 then
adamc@10 660 ()
adamc@10 661 else
adamc@10 662 err CIncompatible
adamc@10 663
adamc@10 664 | (L'.CApp (d1, r1), L'.CApp (d2, r2)) =>
adamc@10 665 (unifyCons' env d1 d2;
adamc@10 666 unifyCons' env r1 r2)
adamc@10 667 | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) =>
adamc@10 668 (unifyKinds k1 k2;
adamc@10 669 unifyCons' (E.pushCRel env x1 k1) c1 c2)
adamc@10 670
adamc@10 671 | (L'.CName n1, L'.CName n2) =>
adamc@10 672 if n1 = n2 then
adamc@10 673 ()
adamc@10 674 else
adamc@10 675 err CIncompatible
adamc@10 676
adamc@34 677 | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) =>
adamc@34 678 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then
adamc@34 679 ()
adamc@34 680 else
adamc@34 681 err CIncompatible
adamc@34 682
adamc@10 683 | (L'.CError, _) => ()
adamc@10 684 | (_, L'.CError) => ()
adamc@10 685
adamc@10 686 | (L'.CUnif (_, _, ref (SOME c1All)), _) => unifyCons' env c1All c2All
adamc@10 687 | (_, L'.CUnif (_, _, ref (SOME c2All))) => unifyCons' env c1All c2All
adamc@10 688
adamc@10 689 | (L'.CUnif (k1, _, r1), L'.CUnif (k2, _, r2)) =>
adamc@10 690 if r1 = r2 then
adamc@10 691 ()
adamc@10 692 else
adamc@10 693 (unifyKinds k1 k2;
adamc@10 694 r1 := SOME c2All)
adamc@10 695
adamc@10 696 | (L'.CUnif (_, _, r), _) =>
adamc@10 697 if occursCon r c2All then
adamc@10 698 err COccursCheckFailed
adamc@10 699 else
adamc@10 700 r := SOME c2All
adamc@10 701 | (_, L'.CUnif (_, _, r)) =>
adamc@10 702 if occursCon r c1All then
adamc@10 703 err COccursCheckFailed
adamc@10 704 else
adamc@10 705 r := SOME c1All
adamc@10 706
adamc@12 707 | (L'.CRecord _, _) => isRecord ()
adamc@12 708 | (_, L'.CRecord _) => isRecord ()
adamc@12 709 | (L'.CConcat _, _) => isRecord ()
adamc@12 710 | (_, L'.CConcat _) => isRecord ()
adamc@12 711
adamc@10 712 | _ => err CIncompatible
adamc@10 713 end
adamc@10 714
adamc@12 715 and unifyCons env c1 c2 =
adamc@10 716 unifyCons' env c1 c2
adamc@10 717 handle CUnify' err => raise CUnify (c1, c2, err)
adamc@10 718 | KUnify args => raise CUnify (c1, c2, CKind args)
adamc@10 719
adamc@10 720 datatype exp_error =
adamc@10 721 UnboundExp of ErrorMsg.span * string
adamc@34 722 | UnboundStrInExp of ErrorMsg.span * string
adamc@10 723 | Unify of L'.exp * L'.con * L'.con * cunify_error
adamc@11 724 | Unif of string * L'.con
adamc@11 725 | WrongForm of string * L'.exp * L'.con
adamc@10 726
adamc@10 727 fun expError env err =
adamc@10 728 case err of
adamc@10 729 UnboundExp (loc, s) =>
adamc@10 730 ErrorMsg.errorAt loc ("Unbound expression variable " ^ s)
adamc@34 731 | UnboundStrInExp (loc, s) =>
adamc@34 732 ErrorMsg.errorAt loc ("Unbound structure " ^ s)
adamc@10 733 | Unify (e, c1, c2, uerr) =>
adamc@10 734 (ErrorMsg.errorAt (#2 e) "Unification failure";
adamc@10 735 eprefaces' [("Expression", p_exp env e),
adamc@10 736 ("Have con", p_con env c1),
adamc@10 737 ("Need con", p_con env c2)];
adamc@10 738 cunifyError env uerr)
adamc@11 739 | Unif (action, c) =>
adamc@11 740 (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action);
adamc@11 741 eprefaces' [("Con", p_con env c)])
adamc@11 742 | WrongForm (variety, e, t) =>
adamc@11 743 (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety);
adamc@11 744 eprefaces' [("Expression", p_exp env e),
adamc@11 745 ("Type", p_con env t)])
adamc@10 746
adamc@10 747 fun checkCon env e c1 c2 =
adamc@10 748 unifyCons env c1 c2
adamc@10 749 handle CUnify (c1, c2, err) =>
adamc@10 750 expError env (Unify (e, c1, c2, err))
adamc@10 751
adamc@14 752 fun primType env p =
adamc@14 753 let
adamc@14 754 val s = case p of
adamc@14 755 P.Int _ => "int"
adamc@14 756 | P.Float _ => "float"
adamc@14 757 | P.String _ => "string"
adamc@14 758 in
adamc@14 759 case E.lookupC env s of
adamc@14 760 E.NotBound => raise Fail ("Primitive type " ^ s ^ " unbound")
adamc@14 761 | E.Rel _ => raise Fail ("Primitive type " ^ s ^ " bound as relative")
adamc@14 762 | E.Named (n, (L'.KType, _)) => L'.CNamed n
adamc@14 763 | E.Named _ => raise Fail ("Primitive type " ^ s ^ " bound at non-Type kind")
adamc@14 764 end
adamc@14 765
adamc@15 766 fun typeof env (e, loc) =
adamc@15 767 case e of
adamc@15 768 L'.EPrim p => (primType env p, loc)
adamc@15 769 | L'.ERel n => #2 (E.lookupERel env n)
adamc@15 770 | L'.ENamed n => #2 (E.lookupENamed env n)
adamc@34 771 | L'.EModProj (n, ms, x) =>
adamc@34 772 let
adamc@34 773 val (_, sgn) = E.lookupStrNamed env n
adamc@34 774 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 775 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 776 NONE => raise Fail "kindof: Unknown substructure"
adamc@34 777 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 778 ((L'.StrVar n, loc), sgn) ms
adamc@34 779 in
adamc@34 780 case E.projectVal env {sgn = sgn, str = str, field = x} of
adamc@34 781 NONE => raise Fail "typeof: Unknown val in structure"
adamc@34 782 | SOME t => t
adamc@34 783 end
adamc@34 784
adamc@15 785 | L'.EApp (e1, _) =>
adamc@15 786 (case #1 (typeof env e1) of
adamc@15 787 L'.TFun (_, c) => c
adamc@15 788 | _ => raise Fail "typeof: Bad EApp")
adamc@26 789 | L'.EAbs (_, _, ran, _) => ran
adamc@15 790 | L'.ECApp (e1, c) =>
adamc@15 791 (case #1 (typeof env e1) of
adamc@15 792 L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1
adamc@15 793 | _ => raise Fail "typeof: Bad ECApp")
adamc@15 794 | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc)
adamc@15 795
adamc@29 796 | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc)
adamc@15 797 | L'.EField (_, _, {field, ...}) => field
adamc@15 798
adamc@15 799 | L'.EError => cerror
adamc@15 800
adamc@15 801 fun elabHead env (e as (_, loc)) t =
adamc@15 802 let
adamc@15 803 fun unravel (t, e) =
adamc@15 804 case hnormCon env t of
adamc@15 805 (L'.TCFun (L'.Implicit, x, k, t'), _) =>
adamc@15 806 let
adamc@15 807 val u = cunif k
adamc@15 808 in
adamc@15 809 unravel (subConInCon (0, u) t',
adamc@15 810 (L'.ECApp (e, u), loc))
adamc@15 811 end
adamc@15 812 | _ => (e, t)
adamc@15 813 in
adamc@15 814 unravel (t, e)
adamc@15 815 end
adamc@15 816
adamc@10 817 fun elabExp env (e, loc) =
adamc@10 818 case e of
adamc@10 819 L.EAnnot (e, t) =>
adamc@10 820 let
adamc@10 821 val (e', et) = elabExp env e
adamc@10 822 val (t', _) = elabCon env t
adamc@10 823 in
adamc@10 824 checkCon env e' et t';
adamc@10 825 (e', t')
adamc@10 826 end
adamc@10 827
adamc@14 828 | L.EPrim p => ((L'.EPrim p, loc), (primType env p, loc))
adamc@34 829 | L.EVar ([], s) =>
adamc@10 830 (case E.lookupE env s of
adamc@10 831 E.NotBound =>
adamc@10 832 (expError env (UnboundExp (loc, s));
adamc@10 833 (eerror, cerror))
adamc@10 834 | E.Rel (n, t) => ((L'.ERel n, loc), t)
adamc@10 835 | E.Named (n, t) => ((L'.ENamed n, loc), t))
adamc@34 836 | L.EVar (m1 :: ms, s) =>
adamc@34 837 (case E.lookupStr env m1 of
adamc@42 838 NONE => (expError env (UnboundStrInExp (loc, m1));
adamc@34 839 (eerror, cerror))
adamc@34 840 | SOME (n, sgn) =>
adamc@34 841 let
adamc@34 842 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@34 843 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@34 844 NONE => (conError env (UnboundStrInCon (loc, m));
adamc@34 845 (strerror, sgnerror))
adamc@34 846 | SOME sgn => ((L'.StrProj (str, m), loc), sgn))
adamc@34 847 ((L'.StrVar n, loc), sgn) ms
adamc@34 848
adamc@34 849 val t = case E.projectVal env {sgn = sgn, str = str, field = s} of
adamc@34 850 NONE => (expError env (UnboundExp (loc, s));
adamc@34 851 cerror)
adamc@34 852 | SOME t => t
adamc@34 853 in
adamc@34 854 ((L'.EModProj (n, ms, s), loc), t)
adamc@34 855 end)
adamc@34 856
adamc@10 857 | L.EApp (e1, e2) =>
adamc@10 858 let
adamc@10 859 val (e1', t1) = elabExp env e1
adamc@15 860 val (e1', t1) = elabHead env e1' t1
adamc@10 861 val (e2', t2) = elabExp env e2
adamc@10 862
adamc@10 863 val dom = cunif ktype
adamc@10 864 val ran = cunif ktype
adamc@10 865 val t = (L'.TFun (dom, ran), dummy)
adamc@10 866 in
adamc@10 867 checkCon env e1' t1 t;
adamc@10 868 checkCon env e2' t2 dom;
adamc@10 869 ((L'.EApp (e1', e2'), loc), ran)
adamc@10 870 end
adamc@10 871 | L.EAbs (x, to, e) =>
adamc@10 872 let
adamc@10 873 val t' = case to of
adamc@10 874 NONE => cunif ktype
adamc@10 875 | SOME t =>
adamc@10 876 let
adamc@10 877 val (t', tk) = elabCon env t
adamc@10 878 in
adamc@10 879 checkKind env t' tk ktype;
adamc@10 880 t'
adamc@10 881 end
adamc@10 882 val (e', et) = elabExp (E.pushERel env x t') e
adamc@10 883 in
adamc@26 884 ((L'.EAbs (x, t', et, e'), loc),
adamc@10 885 (L'.TFun (t', et), loc))
adamc@10 886 end
adamc@10 887 | L.ECApp (e, c) =>
adamc@10 888 let
adamc@10 889 val (e', et) = elabExp env e
adamc@15 890 val (e', et) = elabHead env e' et
adamc@10 891 val (c', ck) = elabCon env c
adamc@10 892 in
adamc@11 893 case #1 (hnormCon env et) of
adamc@11 894 L'.CError => (eerror, cerror)
adamc@11 895 | L'.TCFun (_, _, k, eb) =>
adamc@11 896 let
adamc@11 897 val () = checkKind env c' ck k
adamc@11 898 val eb' = subConInCon (0, c') eb
adamc@11 899 handle SynUnif => (expError env (Unif ("substitution", eb));
adamc@11 900 cerror)
adamc@11 901 in
adamc@11 902 ((L'.ECApp (e', c'), loc), eb')
adamc@11 903 end
adamc@11 904
adamc@11 905 | L'.CUnif _ =>
adamc@11 906 (expError env (Unif ("application", et));
adamc@11 907 (eerror, cerror))
adamc@11 908
adamc@11 909 | _ =>
adamc@11 910 (expError env (WrongForm ("constructor function", e', et));
adamc@11 911 (eerror, cerror))
adamc@10 912 end
adamc@11 913 | L.ECAbs (expl, x, k, e) =>
adamc@11 914 let
adamc@11 915 val expl' = elabExplicitness expl
adamc@11 916 val k' = elabKind k
adamc@11 917 val (e', et) = elabExp (E.pushCRel env x k') e
adamc@11 918 in
adamc@11 919 ((L'.ECAbs (expl', x, k', e'), loc),
adamc@11 920 (L'.TCFun (expl', x, k', et), loc))
adamc@11 921 end
adamc@6 922
adamc@12 923 | L.ERecord xes =>
adamc@12 924 let
adamc@12 925 val xes' = map (fn (x, e) =>
adamc@12 926 let
adamc@12 927 val (x', xk) = elabCon env x
adamc@12 928 val (e', et) = elabExp env e
adamc@12 929 in
adamc@12 930 checkKind env x' xk kname;
adamc@12 931 (x', e', et)
adamc@12 932 end) xes
adamc@12 933 in
adamc@29 934 ((L'.ERecord xes', loc),
adamc@12 935 (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc))
adamc@12 936 end
adamc@12 937
adamc@12 938 | L.EField (e, c) =>
adamc@12 939 let
adamc@12 940 val (e', et) = elabExp env e
adamc@12 941 val (c', ck) = elabCon env c
adamc@12 942
adamc@12 943 val ft = cunif ktype
adamc@12 944 val rest = cunif ktype_record
adamc@12 945 in
adamc@12 946 checkKind env c' ck kname;
adamc@12 947 checkCon env e' et (L'.TRecord (L'.CConcat ((L'.CRecord (ktype, [(c', ft)]), loc), rest), loc), loc);
adamc@12 948 ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft)
adamc@12 949 end
adamc@12 950
adamc@12 951
adamc@6 952 datatype decl_error =
adamc@6 953 KunifsRemainKind of ErrorMsg.span * L'.kind
adamc@6 954 | KunifsRemainCon of ErrorMsg.span * L'.con
adamc@10 955 | KunifsRemainExp of ErrorMsg.span * L'.exp
adamc@10 956 | CunifsRemainCon of ErrorMsg.span * L'.con
adamc@10 957 | CunifsRemainExp of ErrorMsg.span * L'.exp
adamc@6 958
adamc@6 959 fun declError env err =
adamc@6 960 case err of
adamc@6 961 KunifsRemainKind (loc, k) =>
adamc@6 962 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in kind";
adamc@6 963 eprefaces' [("Kind", p_kind k)])
adamc@6 964 | KunifsRemainCon (loc, c) =>
adamc@6 965 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in constructor";
adamc@6 966 eprefaces' [("Constructor", p_con env c)])
adamc@10 967 | KunifsRemainExp (loc, e) =>
adamc@10 968 (ErrorMsg.errorAt loc "Some kind unification variables are undetermined in expression";
adamc@10 969 eprefaces' [("Expression", p_exp env e)])
adamc@10 970 | CunifsRemainCon (loc, c) =>
adamc@10 971 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in constructor";
adamc@10 972 eprefaces' [("Constructor", p_con env c)])
adamc@10 973 | CunifsRemainExp (loc, e) =>
adamc@10 974 (ErrorMsg.errorAt loc "Some constructor unification variables are undetermined in expression";
adamc@10 975 eprefaces' [("Expression", p_exp env e)])
adamc@6 976
adamc@31 977 datatype sgn_error =
adamc@31 978 UnboundSgn of ErrorMsg.span * string
adamc@31 979 | UnmatchedSgi of L'.sgn_item
adamc@31 980 | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error
adamc@31 981 | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error
adamc@41 982 | SgnWrongForm of L'.sgn * L'.sgn
adamc@42 983 | UnWhereable of L'.sgn * string
adamc@31 984
adamc@31 985 fun sgnError env err =
adamc@31 986 case err of
adamc@31 987 UnboundSgn (loc, s) =>
adamc@31 988 ErrorMsg.errorAt loc ("Unbound signature variable " ^ s)
adamc@31 989 | UnmatchedSgi (sgi as (_, loc)) =>
adamc@31 990 (ErrorMsg.errorAt loc "Unmatched signature item";
adamc@31 991 eprefaces' [("Item", p_sgn_item env sgi)])
adamc@31 992 | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) =>
adamc@31 993 (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:";
adamc@31 994 eprefaces' [("Item 1", p_sgn_item env sgi1),
adamc@31 995 ("Item 2", p_sgn_item env sgi2),
adamc@31 996 ("Kind 1", p_kind k1),
adamc@31 997 ("Kind 2", p_kind k2)];
adamc@31 998 kunifyError kerr)
adamc@31 999 | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) =>
adamc@31 1000 (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:";
adamc@31 1001 eprefaces' [("Item 1", p_sgn_item env sgi1),
adamc@31 1002 ("Item 2", p_sgn_item env sgi2),
adamc@31 1003 ("Con 1", p_con env c1),
adamc@31 1004 ("Con 2", p_con env c2)];
adamc@31 1005 cunifyError env cerr)
adamc@41 1006 | SgnWrongForm (sgn1, sgn2) =>
adamc@41 1007 (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:";
adamc@41 1008 eprefaces' [("Sig 1", p_sgn env sgn1),
adamc@41 1009 ("Sig 2", p_sgn env sgn2)])
adamc@42 1010 | UnWhereable (sgn, x) =>
adamc@42 1011 (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'";
adamc@42 1012 eprefaces' [("Signature", p_sgn env sgn),
adamc@42 1013 ("Field", PD.string x)])
adamc@31 1014
adamc@31 1015 datatype str_error =
adamc@31 1016 UnboundStr of ErrorMsg.span * string
adamc@44 1017 | NotFunctor of L'.sgn
adamc@31 1018
adamc@31 1019 fun strError env err =
adamc@31 1020 case err of
adamc@31 1021 UnboundStr (loc, s) =>
adamc@31 1022 ErrorMsg.errorAt loc ("Unbound structure variable " ^ s)
adamc@44 1023 | NotFunctor sgn =>
adamc@44 1024 (ErrorMsg.errorAt (#2 sgn) "Application of non-functor";
adamc@44 1025 eprefaces' [("Signature", p_sgn env sgn)])
adamc@31 1026
adamc@42 1027 val hnormSgn = E.hnormSgn
adamc@31 1028
adamc@31 1029 fun elabSgn_item ((sgi, loc), env) =
adamc@31 1030 let
adamc@31 1031
adamc@31 1032 in
adamc@31 1033 resetKunif ();
adamc@31 1034 resetCunif ();
adamc@31 1035 case sgi of
adamc@31 1036 L.SgiConAbs (x, k) =>
adamc@31 1037 let
adamc@31 1038 val k' = elabKind k
adamc@31 1039
adamc@31 1040 val (env', n) = E.pushCNamed env x k' NONE
adamc@31 1041 in
adamc@31 1042 if ErrorMsg.anyErrors () then
adamc@31 1043 ()
adamc@31 1044 else (
adamc@31 1045 if kunifsInKind k' then
adamc@31 1046 declError env (KunifsRemainKind (loc, k'))
adamc@31 1047 else
adamc@31 1048 ()
adamc@31 1049 );
adamc@31 1050
adamc@31 1051 ((L'.SgiConAbs (x, n, k'), loc), env')
adamc@31 1052 end
adamc@31 1053
adamc@31 1054 | L.SgiCon (x, ko, c) =>
adamc@31 1055 let
adamc@31 1056 val k' = case ko of
adamc@31 1057 NONE => kunif ()
adamc@31 1058 | SOME k => elabKind k
adamc@31 1059
adamc@31 1060 val (c', ck) = elabCon env c
adamc@31 1061 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@31 1062 in
adamc@31 1063 checkKind env c' ck k';
adamc@31 1064
adamc@31 1065 if ErrorMsg.anyErrors () then
adamc@31 1066 ()
adamc@31 1067 else (
adamc@31 1068 if kunifsInKind k' then
adamc@31 1069 declError env (KunifsRemainKind (loc, k'))
adamc@31 1070 else
adamc@31 1071 ();
adamc@31 1072
adamc@31 1073 if kunifsInCon c' then
adamc@31 1074 declError env (KunifsRemainCon (loc, c'))
adamc@31 1075 else
adamc@31 1076 ()
adamc@31 1077 );
adamc@31 1078
adamc@31 1079 ((L'.SgiCon (x, n, k', c'), loc), env')
adamc@31 1080 end
adamc@31 1081
adamc@31 1082 | L.SgiVal (x, c) =>
adamc@31 1083 let
adamc@31 1084 val (c', ck) = elabCon env c
adamc@31 1085
adamc@31 1086 val (env', n) = E.pushENamed env x c'
adamc@31 1087 in
adamc@31 1088 unifyKinds ck ktype;
adamc@31 1089
adamc@31 1090 if ErrorMsg.anyErrors () then
adamc@31 1091 ()
adamc@31 1092 else (
adamc@31 1093 if kunifsInCon c' then
adamc@31 1094 declError env (KunifsRemainCon (loc, c'))
adamc@31 1095 else
adamc@31 1096 ()
adamc@31 1097 );
adamc@31 1098
adamc@31 1099 ((L'.SgiVal (x, n, c'), loc), env')
adamc@31 1100 end
adamc@31 1101
adamc@31 1102 | L.SgiStr (x, sgn) =>
adamc@31 1103 let
adamc@31 1104 val sgn' = elabSgn env sgn
adamc@31 1105 val (env', n) = E.pushStrNamed env x sgn'
adamc@31 1106 in
adamc@31 1107 ((L'.SgiStr (x, n, sgn'), loc), env')
adamc@31 1108 end
adamc@31 1109
adamc@31 1110 end
adamc@31 1111
adamc@31 1112 and elabSgn env (sgn, loc) =
adamc@31 1113 case sgn of
adamc@31 1114 L.SgnConst sgis =>
adamc@31 1115 let
adamc@31 1116 val (sgis', _) = ListUtil.foldlMap elabSgn_item env sgis
adamc@31 1117 in
adamc@31 1118 (L'.SgnConst sgis', loc)
adamc@31 1119 end
adamc@31 1120 | L.SgnVar x =>
adamc@31 1121 (case E.lookupSgn env x of
adamc@31 1122 NONE =>
adamc@31 1123 (sgnError env (UnboundSgn (loc, x));
adamc@31 1124 (L'.SgnError, loc))
adamc@31 1125 | SOME (n, sgis) => (L'.SgnVar n, loc))
adamc@41 1126 | L.SgnFun (m, dom, ran) =>
adamc@41 1127 let
adamc@41 1128 val dom' = elabSgn env dom
adamc@41 1129 val (env', n) = E.pushStrNamed env m dom'
adamc@41 1130 val ran' = elabSgn env' ran
adamc@41 1131 in
adamc@41 1132 (L'.SgnFun (m, n, dom', ran'), loc)
adamc@41 1133 end
adamc@42 1134 | L.SgnWhere (sgn, x, c) =>
adamc@42 1135 let
adamc@42 1136 val sgn' = elabSgn env sgn
adamc@42 1137 val (c', ck) = elabCon env c
adamc@42 1138 in
adamc@42 1139 case #1 (hnormSgn env sgn') of
adamc@42 1140 L'.SgnError => sgnerror
adamc@42 1141 | L'.SgnConst sgis =>
adamc@42 1142 if List.exists (fn (L'.SgiConAbs (x, _, k), _) =>
adamc@42 1143 (unifyKinds k ck;
adamc@42 1144 true)
adamc@42 1145 | _ => false) sgis then
adamc@42 1146 (L'.SgnWhere (sgn', x, c'), loc)
adamc@42 1147 else
adamc@42 1148 (sgnError env (UnWhereable (sgn', x));
adamc@42 1149 sgnerror)
adamc@42 1150 | _ => (sgnError env (UnWhereable (sgn', x));
adamc@42 1151 sgnerror)
adamc@42 1152 end
adamc@31 1153
adamc@31 1154 fun sgiOfDecl (d, loc) =
adamc@31 1155 case d of
adamc@31 1156 L'.DCon (x, n, k, c) => SOME (L'.SgiCon (x, n, k, c), loc)
adamc@31 1157 | L'.DVal (x, n, t, _) => SOME (L'.SgiVal (x, n, t), loc)
adamc@31 1158 | L'.DSgn _ => NONE
adamc@31 1159 | L'.DStr (x, n, sgn, _) => SOME (L'.SgiStr (x, n, sgn), loc)
adamc@31 1160
adamc@35 1161 fun subSgn env sgn1 (sgn2 as (_, loc2)) =
adamc@35 1162 case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of
adamc@31 1163 (L'.SgnError, _) => ()
adamc@31 1164 | (_, L'.SgnError) => ()
adamc@31 1165
adamc@31 1166 | (L'.SgnConst sgis1, L'.SgnConst sgis2) =>
adamc@31 1167 let
adamc@31 1168 fun folder (sgi2All as (sgi, _), env) =
adamc@31 1169 let
adamc@31 1170 fun seek p =
adamc@31 1171 let
adamc@31 1172 fun seek env ls =
adamc@31 1173 case ls of
adamc@31 1174 [] => (sgnError env (UnmatchedSgi sgi2All);
adamc@31 1175 env)
adamc@31 1176 | h :: t =>
adamc@31 1177 case p h of
adamc@31 1178 NONE => seek (E.sgiBinds env h) t
adamc@31 1179 | SOME env => env
adamc@31 1180 in
adamc@31 1181 seek env sgis1
adamc@31 1182 end
adamc@31 1183 in
adamc@31 1184 case sgi of
adamc@31 1185 L'.SgiConAbs (x, n2, k2) =>
adamc@31 1186 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1187 let
adamc@41 1188 fun found (x', n1, k1, co1) =
adamc@41 1189 if x = x' then
adamc@41 1190 let
adamc@41 1191 val () = unifyKinds k1 k2
adamc@41 1192 handle KUnify (k1, k2, err) =>
adamc@41 1193 sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err))
adamc@41 1194 val env = E.pushCNamedAs env x n1 k1 co1
adamc@41 1195 in
adamc@41 1196 SOME (if n1 = n2 then
adamc@41 1197 env
adamc@41 1198 else
adamc@41 1199 E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)))
adamc@41 1200 end
adamc@41 1201 else
adamc@41 1202 NONE
adamc@31 1203 in
adamc@31 1204 case sgi1 of
adamc@41 1205 L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE)
adamc@41 1206 | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1)
adamc@31 1207 | _ => NONE
adamc@31 1208 end)
adamc@31 1209
adamc@31 1210 | L'.SgiCon (x, n2, k2, c2) =>
adamc@31 1211 seek (fn sgi1All as (sgi1, _) =>
adamc@31 1212 case sgi1 of
adamc@41 1213 L'.SgiCon (x', n1, k1, c1) =>
adamc@41 1214 if x = x' then
adamc@41 1215 let
adamc@41 1216 val () = unifyCons env c1 c2
adamc@41 1217 handle CUnify (c1, c2, err) =>
adamc@41 1218 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
adamc@41 1219 in
adamc@41 1220 SOME (E.pushCNamedAs env x n2 k2 (SOME c2))
adamc@41 1221 end
adamc@41 1222 else
adamc@41 1223 NONE
adamc@31 1224 | _ => NONE)
adamc@31 1225
adamc@32 1226 | L'.SgiVal (x, n2, c2) =>
adamc@32 1227 seek (fn sgi1All as (sgi1, _) =>
adamc@32 1228 case sgi1 of
adamc@41 1229 L'.SgiVal (x', n1, c1) =>
adamc@41 1230 if x = x' then
adamc@41 1231 let
adamc@41 1232 val () = unifyCons env c1 c2
adamc@41 1233 handle CUnify (c1, c2, err) =>
adamc@41 1234 sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err))
adamc@41 1235 in
adamc@41 1236 SOME env
adamc@41 1237 end
adamc@41 1238 else
adamc@41 1239 NONE
adamc@32 1240 | _ => NONE)
adamc@32 1241
adamc@33 1242 | L'.SgiStr (x, n2, sgn2) =>
adamc@33 1243 seek (fn sgi1All as (sgi1, _) =>
adamc@33 1244 case sgi1 of
adamc@41 1245 L'.SgiStr (x', n1, sgn1) =>
adamc@41 1246 if x = x' then
adamc@41 1247 (subSgn env sgn1 sgn2;
adamc@41 1248 SOME env)
adamc@41 1249 else
adamc@41 1250 NONE
adamc@33 1251 | _ => NONE)
adamc@33 1252 (* Add type equations between structures here some day. *)
adamc@31 1253 end
adamc@31 1254 in
adamc@31 1255 ignore (foldl folder env sgis2)
adamc@31 1256 end
adamc@31 1257
adamc@41 1258 | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) =>
adamc@43 1259 let
adamc@43 1260 val ran1 =
adamc@43 1261 if n1 = n2 then
adamc@43 1262 ran1
adamc@43 1263 else
adamc@43 1264 subStrInSgn (n1, n2) ran1
adamc@43 1265 in
adamc@43 1266 subSgn env dom2 dom1;
adamc@43 1267 subSgn (E.pushStrNamedAs env m2 n2 dom2) ran1 ran2
adamc@43 1268 end
adamc@41 1269
adamc@41 1270 | _ => sgnError env (SgnWrongForm (sgn1, sgn2))
adamc@41 1271
adamc@35 1272 fun selfify env {str, strs, sgn} =
adamc@35 1273 case #1 (hnormSgn env sgn) of
adamc@35 1274 L'.SgnError => sgn
adamc@35 1275 | L'.SgnVar _ => sgn
adamc@35 1276
adamc@35 1277 | L'.SgnConst sgis =>
adamc@35 1278 (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) =>
adamc@35 1279 (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc)
adamc@35 1280 | (L'.SgiStr (x, n, sgn), loc) =>
adamc@35 1281 (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc)
adamc@35 1282 | x => x) sgis), #2 sgn)
adamc@41 1283 | L'.SgnFun _ => sgn
adamc@42 1284 | L'.SgnWhere _ => sgn
adamc@35 1285
adamc@44 1286 fun selfifyAt env {str, sgn} =
adamc@44 1287 let
adamc@44 1288 fun self (str, _) =
adamc@44 1289 case str of
adamc@44 1290 L'.StrVar x => SOME (x, [])
adamc@44 1291 | L'.StrProj (str, x) =>
adamc@44 1292 (case self str of
adamc@44 1293 NONE => NONE
adamc@44 1294 | SOME (m, ms) => SOME (m, ms @ [x]))
adamc@44 1295 | _ => NONE
adamc@44 1296 in
adamc@44 1297 case self str of
adamc@44 1298 NONE => sgn
adamc@44 1299 | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs}
adamc@44 1300 end
adamc@44 1301
adamc@31 1302 fun elabDecl ((d, loc), env) =
adamc@30 1303 let
adamc@30 1304
adamc@30 1305 in
adamc@30 1306 resetKunif ();
adamc@30 1307 resetCunif ();
adamc@30 1308 case d of
adamc@30 1309 L.DCon (x, ko, c) =>
adamc@30 1310 let
adamc@30 1311 val k' = case ko of
adamc@30 1312 NONE => kunif ()
adamc@30 1313 | SOME k => elabKind k
adamc@3 1314
adamc@30 1315 val (c', ck) = elabCon env c
adamc@30 1316 val (env', n) = E.pushCNamed env x k' (SOME c')
adamc@30 1317 in
adamc@30 1318 checkKind env c' ck k';
adamc@6 1319
adamc@30 1320 if ErrorMsg.anyErrors () then
adamc@30 1321 ()
adamc@30 1322 else (
adamc@30 1323 if kunifsInKind k' then
adamc@30 1324 declError env (KunifsRemainKind (loc, k'))
adamc@30 1325 else
adamc@30 1326 ();
adamc@6 1327
adamc@30 1328 if kunifsInCon c' then
adamc@30 1329 declError env (KunifsRemainCon (loc, c'))
adamc@30 1330 else
adamc@30 1331 ()
adamc@30 1332 );
adamc@6 1333
adamc@31 1334 ((L'.DCon (x, n, k', c'), loc), env')
adamc@30 1335 end
adamc@30 1336 | L.DVal (x, co, e) =>
adamc@30 1337 let
adamc@30 1338 val (c', ck) = case co of
adamc@30 1339 NONE => (cunif ktype, ktype)
adamc@30 1340 | SOME c => elabCon env c
adamc@10 1341
adamc@30 1342 val (e', et) = elabExp env e
adamc@30 1343 val (env', n) = E.pushENamed env x c'
adamc@30 1344 in
adamc@30 1345 checkCon env e' et c';
adamc@10 1346
adamc@30 1347 if ErrorMsg.anyErrors () then
adamc@30 1348 ()
adamc@30 1349 else (
adamc@30 1350 if kunifsInCon c' then
adamc@30 1351 declError env (KunifsRemainCon (loc, c'))
adamc@30 1352 else
adamc@30 1353 ();
adamc@10 1354
adamc@30 1355 if cunifsInCon c' then
adamc@30 1356 declError env (CunifsRemainCon (loc, c'))
adamc@30 1357 else
adamc@30 1358 ();
adamc@10 1359
adamc@30 1360 if kunifsInExp e' then
adamc@30 1361 declError env (KunifsRemainExp (loc, e'))
adamc@30 1362 else
adamc@30 1363 ();
adamc@10 1364
adamc@30 1365 if cunifsInExp e' then
adamc@30 1366 declError env (CunifsRemainExp (loc, e'))
adamc@30 1367 else
adamc@30 1368 ());
adamc@10 1369
adamc@31 1370 ((L'.DVal (x, n, c', e'), loc), env')
adamc@30 1371 end
adamc@30 1372
adamc@31 1373 | L.DSgn (x, sgn) =>
adamc@31 1374 let
adamc@31 1375 val sgn' = elabSgn env sgn
adamc@31 1376 val (env', n) = E.pushSgnNamed env x sgn'
adamc@31 1377 in
adamc@31 1378 ((L'.DSgn (x, n, sgn'), loc), env')
adamc@31 1379 end
adamc@31 1380
adamc@31 1381 | L.DStr (x, sgno, str) =>
adamc@31 1382 let
adamc@31 1383 val formal = Option.map (elabSgn env) sgno
adamc@31 1384 val (str', actual) = elabStr env str
adamc@31 1385
adamc@31 1386 val sgn' = case formal of
adamc@44 1387 NONE => selfifyAt env {str = str', sgn = actual}
adamc@31 1388 | SOME formal =>
adamc@31 1389 (subSgn env actual formal;
adamc@31 1390 formal)
adamc@31 1391
adamc@31 1392 val (env', n) = E.pushStrNamed env x sgn'
adamc@31 1393 in
adamc@31 1394 ((L'.DStr (x, n, sgn', str'), loc), env')
adamc@31 1395 end
adamc@30 1396 end
adamc@3 1397
adamc@31 1398 and elabStr env (str, loc) =
adamc@31 1399 case str of
adamc@31 1400 L.StrConst ds =>
adamc@31 1401 let
adamc@31 1402 val (ds', env') = ListUtil.foldlMap elabDecl env ds
adamc@31 1403 val sgis = List.mapPartial sgiOfDecl ds'
adamc@31 1404 in
adamc@31 1405 ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc))
adamc@31 1406 end
adamc@31 1407 | L.StrVar x =>
adamc@31 1408 (case E.lookupStr env x of
adamc@31 1409 NONE =>
adamc@31 1410 (strError env (UnboundStr (loc, x));
adamc@31 1411 (strerror, sgnerror))
adamc@31 1412 | SOME (n, sgn) => ((L'.StrVar n, loc), sgn))
adamc@34 1413 | L.StrProj (str, x) =>
adamc@34 1414 let
adamc@34 1415 val (str', sgn) = elabStr env str
adamc@34 1416 in
adamc@34 1417 case E.projectStr env {str = str', sgn = sgn, field = x} of
adamc@34 1418 NONE => (strError env (UnboundStr (loc, x));
adamc@34 1419 (strerror, sgnerror))
adamc@34 1420 | SOME sgn => ((L'.StrProj (str', x), loc), sgn)
adamc@34 1421 end
adamc@41 1422 | L.StrFun (m, dom, ranO, str) =>
adamc@41 1423 let
adamc@41 1424 val dom' = elabSgn env dom
adamc@41 1425 val (env', n) = E.pushStrNamed env m dom'
adamc@41 1426 val (str', actual) = elabStr env' str
adamc@41 1427
adamc@41 1428 val formal =
adamc@41 1429 case ranO of
adamc@41 1430 NONE => actual
adamc@41 1431 | SOME ran =>
adamc@41 1432 let
adamc@42 1433 val ran' = elabSgn env' ran
adamc@41 1434 in
adamc@41 1435 subSgn env' actual ran';
adamc@41 1436 ran'
adamc@41 1437 end
adamc@41 1438 in
adamc@41 1439 ((L'.StrFun (m, n, dom', formal, str'), loc),
adamc@41 1440 (L'.SgnFun (m, n, dom', formal), loc))
adamc@41 1441 end
adamc@44 1442 | L.StrApp (str1, str2) =>
adamc@44 1443 let
adamc@44 1444 val (str1', sgn1) = elabStr env str1
adamc@44 1445 val (str2', sgn2) = elabStr env str2
adamc@44 1446 in
adamc@44 1447 case #1 (hnormSgn env sgn1) of
adamc@44 1448 L'.SgnError => (strerror, sgnerror)
adamc@44 1449 | L'.SgnFun (m, n, dom, ran) =>
adamc@44 1450 (subSgn env sgn2 dom;
adamc@44 1451 case #1 (hnormSgn env ran) of
adamc@44 1452 L'.SgnError => (strerror, sgnerror)
adamc@44 1453 | L'.SgnConst sgis =>
adamc@44 1454 ((L'.StrApp (str1', str2'), loc),
adamc@44 1455 (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc))
adamc@44 1456 | _ => raise Fail "Unable to hnormSgn in functor application")
adamc@44 1457 | _ => (strError env (NotFunctor sgn1);
adamc@44 1458 (strerror, sgnerror))
adamc@44 1459 end
adamc@31 1460
adamc@31 1461 val elabFile = ListUtil.foldlMap elabDecl
adamc@2 1462
adamc@2 1463 end