adamc@2: (* Copyright (c) 2008, Adam Chlipala adamc@2: * All rights reserved. adamc@2: * adamc@2: * Redistribution and use in source and binary forms, with or without adamc@2: * modification, are permitted provided that the following conditions are met: adamc@2: * adamc@2: * - Redistributions of source code must retain the above copyright notice, adamc@2: * this list of conditions and the following disclaimer. adamc@2: * - Redistributions in binary form must reproduce the above copyright notice, adamc@2: * this list of conditions and the following disclaimer in the documentation adamc@2: * and/or other materials provided with the distribution. adamc@2: * - The names of contributors may not be used to endorse or promote products adamc@2: * derived from this software without specific prior written permission. adamc@2: * adamc@2: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@2: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@2: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@2: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@2: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@2: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@2: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@2: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@2: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@2: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@2: * POSSIBILITY OF SUCH DAMAGE. adamc@2: *) adamc@2: adamc@2: structure Elaborate :> ELABORATE = struct adamc@2: adamc@14: structure P = Prim adamc@4: structure L = Source adamc@2: structure L' = Elab adamc@2: structure E = ElabEnv adamc@2: structure U = ElabUtil adamc@82: structure D = Disjoint adamc@2: adamc@3: open Print adamc@3: open ElabPrint adamc@3: adamc@62: structure SS = BinarySetFn(struct adamc@62: type ord_key = string adamc@62: val compare = String.compare adamc@62: end) adamc@62: adamc@2: fun elabExplicitness e = adamc@2: case e of adamc@2: L.Explicit => L'.Explicit adamc@2: | L.Implicit => L'.Implicit adamc@2: adamc@2: fun occursKind r = adamc@76: U.Kind.exists (fn L'.KUnif (_, _, r') => r = r' adamc@2: | _ => false) adamc@2: adamc@3: datatype kunify_error = adamc@2: KOccursCheckFailed of L'.kind * L'.kind adamc@2: | KIncompatible of L'.kind * L'.kind adamc@2: adamc@3: exception KUnify' of kunify_error adamc@3: adamc@3: fun kunifyError err = adamc@2: case err of adamc@2: KOccursCheckFailed (k1, k2) => adamc@3: eprefaces "Kind occurs check failed" adamc@3: [("Kind 1", p_kind k1), adamc@3: ("Kind 2", p_kind k2)] adamc@2: | KIncompatible (k1, k2) => adamc@3: eprefaces "Incompatible kinds" adamc@3: [("Kind 1", p_kind k1), adamc@3: ("Kind 2", p_kind k2)] adamc@2: adamc@3: fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) = adamc@2: let adamc@3: fun err f = raise KUnify' (f (k1All, k2All)) adamc@2: in adamc@2: case (k1, k2) of adamc@2: (L'.KType, L'.KType) => () adamc@82: | (L'.KUnit, L'.KUnit) => () adamc@82: adamc@2: | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) => adamc@3: (unifyKinds' d1 d2; adamc@3: unifyKinds' r1 r2) adamc@2: | (L'.KName, L'.KName) => () adamc@3: | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2 adamc@2: adamc@2: | (L'.KError, _) => () adamc@2: | (_, L'.KError) => () adamc@2: adamc@76: | (L'.KUnif (_, _, ref (SOME k1All)), _) => unifyKinds' k1All k2All adamc@76: | (_, L'.KUnif (_, _, ref (SOME k2All))) => unifyKinds' k1All k2All adamc@2: adamc@76: | (L'.KUnif (_, _, r1), L'.KUnif (_, _, r2)) => adamc@2: if r1 = r2 then adamc@2: () adamc@2: else adamc@2: r1 := SOME k2All adamc@2: adamc@76: | (L'.KUnif (_, _, r), _) => adamc@2: if occursKind r k2All then adamc@2: err KOccursCheckFailed adamc@2: else adamc@2: r := SOME k2All adamc@76: | (_, L'.KUnif (_, _, r)) => adamc@2: if occursKind r k1All then adamc@2: err KOccursCheckFailed adamc@2: else adamc@2: r := SOME k1All adamc@2: adamc@2: | _ => err KIncompatible adamc@2: end adamc@2: adamc@3: exception KUnify of L'.kind * L'.kind * kunify_error adamc@3: adamc@3: fun unifyKinds k1 k2 = adamc@3: unifyKinds' k1 k2 adamc@3: handle KUnify' err => raise KUnify (k1, k2, err) adamc@3: adamc@3: datatype con_error = adamc@3: UnboundCon of ErrorMsg.span * string adamc@34: | UnboundStrInCon of ErrorMsg.span * string adamc@3: | WrongKind of L'.con * L'.kind * L'.kind * kunify_error adamc@67: | DuplicateField of ErrorMsg.span * string adamc@3: adamc@5: fun conError env err = adamc@3: case err of adamc@3: UnboundCon (loc, s) => adamc@3: ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s) adamc@34: | UnboundStrInCon (loc, s) => adamc@34: ErrorMsg.errorAt loc ("Unbound structure " ^ s) adamc@3: | WrongKind (c, k1, k2, kerr) => adamc@3: (ErrorMsg.errorAt (#2 c) "Wrong kind"; adamc@5: eprefaces' [("Constructor", p_con env c), adamc@5: ("Have kind", p_kind k1), adamc@5: ("Need kind", p_kind k2)]; adamc@3: kunifyError kerr) adamc@67: | DuplicateField (loc, s) => adamc@67: ErrorMsg.errorAt loc ("Duplicate record field " ^ s) adamc@3: adamc@5: fun checkKind env c k1 k2 = adamc@3: unifyKinds k1 k2 adamc@3: handle KUnify (k1, k2, err) => adamc@5: conError env (WrongKind (c, k1, k2, err)) adamc@3: adamc@3: val dummy = ErrorMsg.dummySpan adamc@3: adamc@3: val ktype = (L'.KType, dummy) adamc@3: val kname = (L'.KName, dummy) adamc@12: val ktype_record = (L'.KRecord ktype, dummy) adamc@3: adamc@3: val cerror = (L'.CError, dummy) adamc@3: val kerror = (L'.KError, dummy) adamc@10: val eerror = (L'.EError, dummy) adamc@31: val sgnerror = (L'.SgnError, dummy) adamc@31: val strerror = (L'.StrError, dummy) adamc@3: adamc@56: val int = ref cerror adamc@56: val float = ref cerror adamc@56: val string = ref cerror adamc@56: adamc@3: local adamc@3: val count = ref 0 adamc@3: in adamc@3: adamc@3: fun resetKunif () = count := 0 adamc@3: adamc@76: fun kunif loc = adamc@3: let adamc@3: val n = !count adamc@3: val s = if n <= 26 then adamc@3: str (chr (ord #"A" + n)) adamc@3: else adamc@3: "U" ^ Int.toString (n - 26) adamc@3: in adamc@3: count := n + 1; adamc@76: (L'.KUnif (loc, s, ref NONE), dummy) adamc@3: end adamc@3: adamc@3: end adamc@3: adamc@10: local adamc@10: val count = ref 0 adamc@10: in adamc@10: adamc@10: fun resetCunif () = count := 0 adamc@10: adamc@76: fun cunif (loc, k) = adamc@10: let adamc@10: val n = !count adamc@10: val s = if n <= 26 then adamc@10: str (chr (ord #"A" + n)) adamc@10: else adamc@10: "U" ^ Int.toString (n - 26) adamc@10: in adamc@10: count := n + 1; adamc@76: (L'.CUnif (loc, k, s, ref NONE), dummy) adamc@10: end adamc@10: adamc@10: end adamc@10: adamc@18: fun elabKind (k, loc) = adamc@18: case k of adamc@18: L.KType => (L'.KType, loc) adamc@18: | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc) adamc@18: | L.KName => (L'.KName, loc) adamc@18: | L.KRecord k => (L'.KRecord (elabKind k), loc) adamc@82: | L.KUnit => (L'.KUnit, loc) adamc@76: | L.KWild => kunif loc adamc@18: adamc@67: fun foldKind (dom, ran, loc)= adamc@67: (L'.KArrow ((L'.KArrow ((L'.KName, loc), adamc@67: (L'.KArrow (dom, adamc@67: (L'.KArrow (ran, ran), loc)), loc)), loc), adamc@67: (L'.KArrow (ran, adamc@67: (L'.KArrow ((L'.KRecord dom, loc), adamc@67: ran), loc)), loc)), loc) adamc@67: adamc@83: fun elabCon (env, denv) (c, loc) = adamc@3: case c of adamc@3: L.CAnnot (c, k) => adamc@3: let adamc@3: val k' = elabKind k adamc@83: val (c', ck, gs) = elabCon (env, denv) c adamc@3: in adamc@5: checkKind env c' ck k'; adamc@83: (c', k', gs) adamc@3: end adamc@3: adamc@3: | L.TFun (t1, t2) => adamc@3: let adamc@83: val (t1', k1, gs1) = elabCon (env, denv) t1 adamc@83: val (t2', k2, gs2) = elabCon (env, denv) t2 adamc@3: in adamc@5: checkKind env t1' k1 ktype; adamc@5: checkKind env t2' k2 ktype; adamc@83: ((L'.TFun (t1', t2'), loc), ktype, gs1 @ gs2) adamc@3: end adamc@3: | L.TCFun (e, x, k, t) => adamc@3: let adamc@3: val e' = elabExplicitness e adamc@3: val k' = elabKind k adamc@3: val env' = E.pushCRel env x k' adamc@83: val (t', tk, gs) = elabCon (env', D.enter denv) t adamc@3: in adamc@5: checkKind env t' tk ktype; adamc@83: ((L'.TCFun (e', x, k', t'), loc), ktype, gs) adamc@3: end adamc@85: | L.TDisjoint (c1, c2, c) => adamc@85: let adamc@85: val (c1', k1, gs1) = elabCon (env, denv) c1 adamc@85: val (c2', k2, gs2) = elabCon (env, denv) c2 adamc@85: adamc@85: val ku1 = kunif loc adamc@85: val ku2 = kunif loc adamc@85: adamc@90: val (denv', gs3) = D.assert env denv (c1', c2') adamc@90: val (c', k, gs4) = elabCon (env, denv') c adamc@85: in adamc@85: checkKind env c1' k1 (L'.KRecord ku1, loc); adamc@85: checkKind env c2' k2 (L'.KRecord ku2, loc); adamc@85: adamc@90: ((L'.TDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4) adamc@85: end adamc@3: | L.TRecord c => adamc@3: let adamc@83: val (c', ck, gs) = elabCon (env, denv) c adamc@3: val k = (L'.KRecord ktype, loc) adamc@3: in adamc@5: checkKind env c' ck k; adamc@83: ((L'.TRecord c', loc), ktype, gs) adamc@3: end adamc@3: adamc@34: | L.CVar ([], s) => adamc@3: (case E.lookupC env s of adamc@9: E.NotBound => adamc@5: (conError env (UnboundCon (loc, s)); adamc@83: (cerror, kerror, [])) adamc@9: | E.Rel (n, k) => adamc@83: ((L'.CRel n, loc), k, []) adamc@9: | E.Named (n, k) => adamc@83: ((L'.CNamed n, loc), k, [])) adamc@34: | L.CVar (m1 :: ms, s) => adamc@34: (case E.lookupStr env m1 of adamc@42: NONE => (conError env (UnboundStrInCon (loc, m1)); adamc@83: (cerror, kerror, [])) adamc@34: | SOME (n, sgn) => adamc@34: let adamc@34: val (str, sgn) = foldl (fn (m, (str, sgn)) => adamc@34: case E.projectStr env {sgn = sgn, str = str, field = m} of adamc@34: NONE => (conError env (UnboundStrInCon (loc, m)); adamc@34: (strerror, sgnerror)) adamc@34: | SOME sgn => ((L'.StrProj (str, m), loc), sgn)) adamc@34: ((L'.StrVar n, loc), sgn) ms adamc@34: adamc@34: val k = case E.projectCon env {sgn = sgn, str = str, field = s} of adamc@34: NONE => (conError env (UnboundCon (loc, s)); adamc@34: kerror) adamc@34: | SOME (k, _) => k adamc@34: in adamc@83: ((L'.CModProj (n, ms, s), loc), k, []) adamc@34: end) adamc@34: adamc@3: | L.CApp (c1, c2) => adamc@3: let adamc@83: val (c1', k1, gs1) = elabCon (env, denv) c1 adamc@83: val (c2', k2, gs2) = elabCon (env, denv) c2 adamc@76: val dom = kunif loc adamc@76: val ran = kunif loc adamc@3: in adamc@5: checkKind env c1' k1 (L'.KArrow (dom, ran), loc); adamc@5: checkKind env c2' k2 dom; adamc@83: ((L'.CApp (c1', c2'), loc), ran, gs1 @ gs2) adamc@3: end adamc@67: | L.CAbs (x, ko, t) => adamc@3: let adamc@67: val k' = case ko of adamc@76: NONE => kunif loc adamc@67: | SOME k => elabKind k adamc@3: val env' = E.pushCRel env x k' adamc@83: val (t', tk, gs) = elabCon (env', D.enter denv) t adamc@3: in adamc@8: ((L'.CAbs (x, k', t'), loc), adamc@83: (L'.KArrow (k', tk), loc), adamc@83: gs) adamc@3: end adamc@3: adamc@84: | L.CDisjoint (c1, c2, c) => adamc@84: let adamc@84: val (c1', k1, gs1) = elabCon (env, denv) c1 adamc@84: val (c2', k2, gs2) = elabCon (env, denv) c2 adamc@84: adamc@84: val ku1 = kunif loc adamc@84: val ku2 = kunif loc adamc@84: adamc@90: val (denv', gs3) = D.assert env denv (c1', c2') adamc@90: val (c', k, gs4) = elabCon (env, denv') c adamc@84: in adamc@84: checkKind env c1' k1 (L'.KRecord ku1, loc); adamc@84: checkKind env c2' k2 (L'.KRecord ku2, loc); adamc@84: adamc@90: ((L'.CDisjoint (c1', c2', c'), loc), k, gs1 @ gs2 @ gs3 @ gs4) adamc@84: end adamc@84: adamc@3: | L.CName s => adamc@83: ((L'.CName s, loc), kname, []) adamc@3: adamc@3: | L.CRecord xcs => adamc@3: let adamc@76: val k = kunif loc adamc@3: adamc@83: val (xcs', gs) = ListUtil.foldlMap (fn ((x, c), gs) => adamc@83: let adamc@83: val (x', xk, gs1) = elabCon (env, denv) x adamc@83: val (c', ck, gs2) = elabCon (env, denv) c adamc@83: in adamc@83: checkKind env x' xk kname; adamc@83: checkKind env c' ck k; adamc@83: ((x', c'), gs1 @ gs2 @ gs) adamc@83: end) [] xcs adamc@67: adamc@67: val rc = (L'.CRecord (k, xcs'), loc) adamc@67: (* Add duplicate field checking later. *) adamc@83: adamc@83: fun prove (xcs, ds) = adamc@83: case xcs of adamc@83: [] => ds adamc@83: | xc :: rest => adamc@83: let adamc@83: val r1 = (L'.CRecord (k, [xc]), loc) adamc@83: val ds = foldl (fn (xc', ds) => adamc@83: let adamc@83: val r2 = (L'.CRecord (k, [xc']), loc) adamc@83: in adamc@90: D.prove env denv (r1, r2, loc) @ ds adamc@83: end) adamc@83: ds rest adamc@83: in adamc@83: prove (rest, ds) adamc@83: end adamc@3: in adamc@83: (rc, (L'.KRecord k, loc), prove (xcs', gs)) adamc@3: end adamc@3: | L.CConcat (c1, c2) => adamc@3: let adamc@83: val (c1', k1, gs1) = elabCon (env, denv) c1 adamc@83: val (c2', k2, gs2) = elabCon (env, denv) c2 adamc@76: val ku = kunif loc adamc@3: val k = (L'.KRecord ku, loc) adamc@3: in adamc@5: checkKind env c1' k1 k; adamc@5: checkKind env c2' k2 k; adamc@83: ((L'.CConcat (c1', c2'), loc), k, adamc@90: D.prove env denv (c1', c2', loc) @ gs1 @ gs2) adamc@3: end adamc@67: | L.CFold => adamc@67: let adamc@76: val dom = kunif loc adamc@76: val ran = kunif loc adamc@67: in adamc@67: ((L'.CFold (dom, ran), loc), adamc@83: foldKind (dom, ran, loc), adamc@83: []) adamc@67: end adamc@3: adamc@83: | L.CUnit => ((L'.CUnit, loc), (L'.KUnit, loc), []) adamc@82: adamc@18: | L.CWild k => adamc@18: let adamc@18: val k' = elabKind k adamc@18: in adamc@83: (cunif (loc, k'), k', []) adamc@18: end adamc@18: adamc@6: fun kunifsRemain k = adamc@6: case k of adamc@76: L'.KUnif (_, _, ref NONE) => true adamc@6: | _ => false adamc@10: fun cunifsRemain c = adamc@10: case c of adamc@76: L'.CUnif (loc, _, _, ref NONE) => SOME loc adamc@76: | _ => NONE adamc@6: adamc@76: val kunifsInDecl = U.Decl.exists {kind = kunifsRemain, adamc@76: con = fn _ => false, adamc@76: exp = fn _ => false, adamc@76: sgn_item = fn _ => false, adamc@76: sgn = fn _ => false, adamc@76: str = fn _ => false, adamc@76: decl = fn _ => false} adamc@10: adamc@76: val cunifsInDecl = U.Decl.search {kind = fn _ => NONE, adamc@76: con = cunifsRemain, adamc@76: exp = fn _ => NONE, adamc@76: sgn_item = fn _ => NONE, adamc@76: sgn = fn _ => NONE, adamc@76: str = fn _ => NONE, adamc@76: decl = fn _ => NONE} adamc@10: adamc@10: fun occursCon r = adamc@10: U.Con.exists {kind = fn _ => false, adamc@76: con = fn L'.CUnif (_, _, _, r') => r = r' adamc@10: | _ => false} adamc@10: adamc@10: datatype cunify_error = adamc@10: CKind of L'.kind * L'.kind * kunify_error adamc@10: | COccursCheckFailed of L'.con * L'.con adamc@10: | CIncompatible of L'.con * L'.con adamc@10: | CExplicitness of L'.con * L'.con adamc@141: | CKindof of L'.kind * L'.con adamc@12: | CRecordFailure adamc@10: adamc@10: exception CUnify' of cunify_error adamc@10: adamc@10: fun cunifyError env err = adamc@10: case err of adamc@10: CKind (k1, k2, kerr) => adamc@10: (eprefaces "Kind unification failure" adamc@10: [("Kind 1", p_kind k1), adamc@10: ("Kind 2", p_kind k2)]; adamc@10: kunifyError kerr) adamc@10: | COccursCheckFailed (c1, c2) => adamc@10: eprefaces "Constructor occurs check failed" adamc@10: [("Con 1", p_con env c1), adamc@10: ("Con 2", p_con env c2)] adamc@10: | CIncompatible (c1, c2) => adamc@10: eprefaces "Incompatible constructors" adamc@10: [("Con 1", p_con env c1), adamc@10: ("Con 2", p_con env c2)] adamc@10: | CExplicitness (c1, c2) => adamc@10: eprefaces "Differing constructor function explicitness" adamc@10: [("Con 1", p_con env c1), adamc@10: ("Con 2", p_con env c2)] adamc@141: | CKindof (k, c) => adamc@147: eprefaces "Unexpected kind for kindof calculation" adamc@141: [("Kind", p_kind k), adamc@141: ("Con", p_con env c)] adamc@12: | CRecordFailure => adamc@12: eprefaces "Can't unify record constructors" [] adamc@10: adamc@13: exception SynUnif = E.SynUnif adamc@12: adamc@81: open ElabOps adamc@43: adamc@12: type record_summary = { adamc@12: fields : (L'.con * L'.con) list, adamc@12: unifs : (L'.con * L'.con option ref) list, adamc@12: others : L'.con list adamc@12: } adamc@12: adamc@12: fun summaryToCon {fields, unifs, others} = adamc@12: let adamc@12: val c = (L'.CRecord (ktype, []), dummy) adamc@12: val c = List.foldr (fn (c', c) => (L'.CConcat (c', c), dummy)) c others adamc@12: val c = List.foldr (fn ((c', _), c) => (L'.CConcat (c', c), dummy)) c unifs adamc@12: in adamc@12: (L'.CConcat ((L'.CRecord (ktype, fields), dummy), c), dummy) adamc@12: end adamc@12: adamc@12: fun p_summary env s = p_con env (summaryToCon s) adamc@12: adamc@12: exception CUnify of L'.con * L'.con * cunify_error adamc@12: adamc@12: fun hnormKind (kAll as (k, _)) = adamc@12: case k of adamc@76: L'.KUnif (_, _, ref (SOME k)) => hnormKind k adamc@12: | _ => kAll adamc@12: adamc@12: fun kindof env (c, loc) = adamc@12: case c of adamc@12: L'.TFun _ => ktype adamc@12: | L'.TCFun _ => ktype adamc@85: | L'.TDisjoint _ => ktype adamc@12: | L'.TRecord _ => ktype adamc@12: adamc@12: | L'.CRel xn => #2 (E.lookupCRel env xn) adamc@12: | L'.CNamed xn => #2 (E.lookupCNamed env xn) adamc@34: | L'.CModProj (n, ms, x) => adamc@34: let adamc@34: val (_, sgn) = E.lookupStrNamed env n adamc@34: val (str, sgn) = foldl (fn (m, (str, sgn)) => adamc@34: case E.projectStr env {sgn = sgn, str = str, field = m} of adamc@34: NONE => raise Fail "kindof: Unknown substructure" adamc@34: | SOME sgn => ((L'.StrProj (str, m), loc), sgn)) adamc@34: ((L'.StrVar n, loc), sgn) ms adamc@34: in adamc@34: case E.projectCon env {sgn = sgn, str = str, field = x} of adamc@34: NONE => raise Fail "kindof: Unknown con in structure" adamc@34: | SOME (k, _) => k adamc@34: end adamc@34: adamc@12: | L'.CApp (c, _) => adamc@141: (case hnormKind (kindof env c) of adamc@141: (L'.KArrow (_, k), _) => k adamc@141: | (L'.KError, _) => kerror adamc@141: | k => raise CUnify' (CKindof (k, c))) adamc@12: | L'.CAbs (x, k, c) => (L'.KArrow (k, kindof (E.pushCRel env x k) c), loc) adamc@84: | L'.CDisjoint (_, _, c) => kindof env c adamc@12: adamc@12: | L'.CName _ => kname adamc@12: adamc@12: | L'.CRecord (k, _) => (L'.KRecord k, loc) adamc@12: | L'.CConcat (c, _) => kindof env c adamc@67: | L'.CFold (dom, ran) => foldKind (dom, ran, loc) adamc@12: adamc@82: | L'.CUnit => (L'.KUnit, loc) adamc@82: adamc@12: | L'.CError => kerror adamc@76: | L'.CUnif (_, k, _, _) => k adamc@12: adamc@90: val hnormCon = D.hnormCon adamc@86: adamc@86: fun unifyRecordCons (env, denv) (c1, c2) = adamc@12: let adamc@92: fun rkindof c = adamc@147: case hnormKind (kindof env c) of adamc@92: (L'.KRecord k, _) => k adamc@141: | (L'.KError, _) => kerror adamc@141: | k => raise CUnify' (CKindof (k, c)) adamc@92: adamc@92: val k1 = rkindof c1 adamc@92: val k2 = rkindof c2 adamc@86: adamc@86: val (r1, gs1) = recordSummary (env, denv) c1 adamc@86: val (r2, gs2) = recordSummary (env, denv) c2 adamc@12: in adamc@12: unifyKinds k1 k2; adamc@86: unifySummaries (env, denv) (k1, r1, r2); adamc@86: gs1 @ gs2 adamc@12: end adamc@12: adamc@86: and recordSummary (env, denv) c = adamc@86: let adamc@86: val (c, gs) = hnormCon (env, denv) c adamc@12: adamc@86: val (sum, gs') = adamc@86: case c of adamc@86: (L'.CRecord (_, xcs), _) => ({fields = xcs, unifs = [], others = []}, []) adamc@86: | (L'.CConcat (c1, c2), _) => adamc@86: let adamc@86: val (s1, gs1) = recordSummary (env, denv) c1 adamc@86: val (s2, gs2) = recordSummary (env, denv) c2 adamc@86: in adamc@86: ({fields = #fields s1 @ #fields s2, adamc@86: unifs = #unifs s1 @ #unifs s2, adamc@86: others = #others s1 @ #others s2}, adamc@86: gs1 @ gs2) adamc@86: end adamc@86: | (L'.CUnif (_, _, _, ref (SOME c)), _) => recordSummary (env, denv) c adamc@86: | c' as (L'.CUnif (_, _, _, r), _) => ({fields = [], unifs = [(c', r)], others = []}, []) adamc@86: | c' => ({fields = [], unifs = [], others = [c']}, []) adamc@86: in adamc@86: (sum, gs @ gs') adamc@86: end adamc@86: adamc@86: and consEq (env, denv) (c1, c2) = adamc@86: (case unifyCons (env, denv) c1 c2 of adamc@86: [] => true adamc@86: | _ => false) adamc@12: handle CUnify _ => false adamc@12: adamc@80: and consNeq env (c1, c2) = adamc@86: case (#1 (ElabOps.hnormCon env c1), #1 (ElabOps.hnormCon env c2)) of adamc@80: (L'.CName x1, L'.CName x2) => x1 <> x2 adamc@80: | _ => false adamc@80: adamc@86: and unifySummaries (env, denv) (k, s1 : record_summary, s2 : record_summary) = adamc@12: let adamc@14: (*val () = eprefaces "Summaries" [("#1", p_summary env s1), adamc@14: ("#2", p_summary env s2)]*) adamc@12: adamc@12: fun eatMatching p (ls1, ls2) = adamc@12: let adamc@12: fun em (ls1, ls2, passed1) = adamc@12: case ls1 of adamc@12: [] => (rev passed1, ls2) adamc@12: | h1 :: t1 => adamc@12: let adamc@12: fun search (ls2', passed2) = adamc@12: case ls2' of adamc@12: [] => em (t1, ls2, h1 :: passed1) adamc@12: | h2 :: t2 => adamc@12: if p (h1, h2) then adamc@12: em (t1, List.revAppend (passed2, t2), passed1) adamc@12: else adamc@12: search (t2, h2 :: passed2) adamc@12: in adamc@12: search (ls2, []) adamc@12: end adamc@12: in adamc@12: em (ls1, ls2, []) adamc@12: end adamc@12: adamc@12: val (fs1, fs2) = eatMatching (fn ((x1, c1), (x2, c2)) => adamc@80: not (consNeq env (x1, x2)) adamc@86: andalso consEq (env, denv) (c1, c2) adamc@86: andalso consEq (env, denv) (x1, x2)) adamc@79: (#fields s1, #fields s2) adamc@14: (*val () = eprefaces "Summaries2" [("#1", p_summary env {fields = fs1, unifs = #unifs s1, others = #others s1}), adamc@14: ("#2", p_summary env {fields = fs2, unifs = #unifs s2, others = #others s2})]*) adamc@12: val (unifs1, unifs2) = eatMatching (fn ((_, r1), (_, r2)) => r1 = r2) (#unifs s1, #unifs s2) adamc@86: val (others1, others2) = eatMatching (consEq (env, denv)) (#others s1, #others s2) adamc@12: adamc@12: fun unifFields (fs, others, unifs) = adamc@12: case (fs, others, unifs) of adamc@12: ([], [], _) => ([], [], unifs) adamc@12: | (_, _, []) => (fs, others, []) adamc@12: | (_, _, (_, r) :: rest) => adamc@12: let adamc@12: val r' = ref NONE adamc@141: val kr = (L'.KRecord k, dummy) adamc@141: val cr' = (L'.CUnif (dummy, kr, "recd", r'), dummy) adamc@12: adamc@12: val prefix = case (fs, others) of adamc@12: ([], other :: others) => adamc@12: List.foldl (fn (other, c) => adamc@12: (L'.CConcat (c, other), dummy)) adamc@12: other others adamc@12: | (fs, []) => adamc@12: (L'.CRecord (k, fs), dummy) adamc@12: | (fs, others) => adamc@12: List.foldl (fn (other, c) => adamc@12: (L'.CConcat (c, other), dummy)) adamc@12: (L'.CRecord (k, fs), dummy) others adamc@12: in adamc@12: r := SOME (L'.CConcat (prefix, cr'), dummy); adamc@12: ([], [], (cr', r') :: rest) adamc@12: end adamc@12: adamc@12: val (fs1, others1, unifs2) = unifFields (fs1, others1, unifs2) adamc@12: val (fs2, others2, unifs1) = unifFields (fs2, others2, unifs1) adamc@12: adamc@86: val clear = case (fs1, others1, fs2, others2) of adamc@86: ([], [], [], []) => true adamc@12: | _ => false adamc@12: val empty = (L'.CRecord (k, []), dummy) adamc@12: fun pairOffUnifs (unifs1, unifs2) = adamc@12: case (unifs1, unifs2) of adamc@12: ([], _) => adamc@86: if clear then adamc@12: List.app (fn (_, r) => r := SOME empty) unifs2 adamc@12: else adamc@12: raise CUnify' CRecordFailure adamc@12: | (_, []) => adamc@86: if clear then adamc@12: List.app (fn (_, r) => r := SOME empty) unifs1 adamc@12: else adamc@12: raise CUnify' CRecordFailure adamc@12: | ((c1, _) :: rest1, (_, r2) :: rest2) => adamc@12: (r2 := SOME c1; adamc@12: pairOffUnifs (rest1, rest2)) adamc@12: in adamc@12: pairOffUnifs (unifs1, unifs2) adamc@12: end adamc@12: adamc@86: and unifyCons' (env, denv) c1 c2 = adamc@86: let adamc@86: val (c1, gs1) = hnormCon (env, denv) c1 adamc@86: val (c2, gs2) = hnormCon (env, denv) c2 adamc@90: val gs3 = unifyCons'' (env, denv) c1 c2 adamc@86: in adamc@90: gs1 @ gs2 @ gs3 adamc@86: end adamc@11: adamc@86: and unifyCons'' (env, denv) (c1All as (c1, _)) (c2All as (c2, _)) = adamc@10: let adamc@10: fun err f = raise CUnify' (f (c1All, c2All)) adamc@12: adamc@86: fun isRecord () = unifyRecordCons (env, denv) (c1All, c2All) adamc@10: in adamc@92: (*eprefaces "unifyCons''" [("c1All", p_con env c1All), adamc@92: ("c2All", p_con env c2All)];*) adamc@92: adamc@10: case (c1, c2) of adamc@88: (L'.CUnit, L'.CUnit) => [] adamc@88: adamc@88: | (L'.TFun (d1, r1), L'.TFun (d2, r2)) => adamc@86: unifyCons' (env, denv) d1 d2 adamc@86: @ unifyCons' (env, denv) r1 r2 adamc@10: | (L'.TCFun (expl1, x1, d1, r1), L'.TCFun (expl2, _, d2, r2)) => adamc@10: if expl1 <> expl2 then adamc@10: err CExplicitness adamc@10: else adamc@10: (unifyKinds d1 d2; adamc@86: unifyCons' (E.pushCRel env x1 d1, D.enter denv) r1 r2) adamc@86: | (L'.TRecord r1, L'.TRecord r2) => unifyCons' (env, denv) r1 r2 adamc@10: adamc@10: | (L'.CRel n1, L'.CRel n2) => adamc@10: if n1 = n2 then adamc@86: [] adamc@10: else adamc@10: err CIncompatible adamc@10: | (L'.CNamed n1, L'.CNamed n2) => adamc@10: if n1 = n2 then adamc@86: [] adamc@10: else adamc@10: err CIncompatible adamc@10: adamc@10: | (L'.CApp (d1, r1), L'.CApp (d2, r2)) => adamc@86: (unifyCons' (env, denv) d1 d2; adamc@86: unifyCons' (env, denv) r1 r2) adamc@10: | (L'.CAbs (x1, k1, c1), L'.CAbs (_, k2, c2)) => adamc@10: (unifyKinds k1 k2; adamc@86: unifyCons' (E.pushCRel env x1 k1, D.enter denv) c1 c2) adamc@10: adamc@10: | (L'.CName n1, L'.CName n2) => adamc@10: if n1 = n2 then adamc@86: [] adamc@10: else adamc@10: err CIncompatible adamc@10: adamc@34: | (L'.CModProj (n1, ms1, x1), L'.CModProj (n2, ms2, x2)) => adamc@34: if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then adamc@86: [] adamc@34: else adamc@34: err CIncompatible adamc@34: adamc@86: | (L'.CError, _) => [] adamc@86: | (_, L'.CError) => [] adamc@10: adamc@92: | (L'.CRecord _, _) => isRecord () adamc@92: | (_, L'.CRecord _) => isRecord () adamc@92: | (L'.CConcat _, _) => isRecord () adamc@92: | (_, L'.CConcat _) => isRecord () adamc@92: (*| (L'.CUnif (_, (L'.KRecord _, _), _, _), _) => isRecord () adamc@92: | (_, L'.CUnif (_, (L'.KRecord _, _), _, _)) => isRecord ()*) adamc@10: adamc@76: | (L'.CUnif (_, k1, _, r1), L'.CUnif (_, k2, _, r2)) => adamc@10: if r1 = r2 then adamc@86: [] adamc@10: else adamc@10: (unifyKinds k1 k2; adamc@86: r1 := SOME c2All; adamc@86: []) adamc@10: adamc@76: | (L'.CUnif (_, _, _, r), _) => adamc@10: if occursCon r c2All then adamc@10: err COccursCheckFailed adamc@10: else adamc@86: (r := SOME c2All; adamc@86: []) adamc@76: | (_, L'.CUnif (_, _, _, r)) => adamc@10: if occursCon r c1All then adamc@10: err COccursCheckFailed adamc@10: else adamc@86: (r := SOME c1All; adamc@86: []) adamc@10: adamc@12: adamc@71: | (L'.CFold (dom1, ran1), L'.CFold (dom2, ran2)) => adamc@71: (unifyKinds dom1 dom2; adamc@86: unifyKinds ran1 ran2; adamc@86: []) adamc@71: adamc@10: | _ => err CIncompatible adamc@10: end adamc@10: adamc@86: and unifyCons (env, denv) c1 c2 = adamc@86: unifyCons' (env, denv) c1 c2 adamc@10: handle CUnify' err => raise CUnify (c1, c2, err) adamc@10: | KUnify args => raise CUnify (c1, c2, CKind args) adamc@10: adamc@10: datatype exp_error = adamc@10: UnboundExp of ErrorMsg.span * string adamc@34: | UnboundStrInExp of ErrorMsg.span * string adamc@10: | Unify of L'.exp * L'.con * L'.con * cunify_error adamc@11: | Unif of string * L'.con adamc@11: | WrongForm of string * L'.exp * L'.con adamc@92: | IncompatibleCons of L'.con * L'.con adamc@10: adamc@10: fun expError env err = adamc@10: case err of adamc@10: UnboundExp (loc, s) => adamc@10: ErrorMsg.errorAt loc ("Unbound expression variable " ^ s) adamc@34: | UnboundStrInExp (loc, s) => adamc@34: ErrorMsg.errorAt loc ("Unbound structure " ^ s) adamc@10: | Unify (e, c1, c2, uerr) => adamc@10: (ErrorMsg.errorAt (#2 e) "Unification failure"; adamc@10: eprefaces' [("Expression", p_exp env e), adamc@10: ("Have con", p_con env c1), adamc@10: ("Need con", p_con env c2)]; adamc@10: cunifyError env uerr) adamc@11: | Unif (action, c) => adamc@11: (ErrorMsg.errorAt (#2 c) ("Unification variable blocks " ^ action); adamc@11: eprefaces' [("Con", p_con env c)]) adamc@11: | WrongForm (variety, e, t) => adamc@11: (ErrorMsg.errorAt (#2 e) ("Expression is not a " ^ variety); adamc@11: eprefaces' [("Expression", p_exp env e), adamc@11: ("Type", p_con env t)]) adamc@92: | IncompatibleCons (c1, c2) => adamc@92: (ErrorMsg.errorAt (#2 c1) "Incompatible constructors"; adamc@92: eprefaces' [("Con 1", p_con env c1), adamc@92: ("Con 2", p_con env c2)]) adamc@10: adamc@86: fun checkCon (env, denv) e c1 c2 = adamc@86: unifyCons (env, denv) c1 c2 adamc@10: handle CUnify (c1, c2, err) => adamc@86: (expError env (Unify (e, c1, c2, err)); adamc@86: []) adamc@10: adamc@14: fun primType env p = adamc@56: case p of adamc@56: P.Int _ => !int adamc@56: | P.Float _ => !float adamc@56: | P.String _ => !string adamc@14: adamc@71: fun recCons (k, nm, v, rest, loc) = adamc@71: (L'.CConcat ((L'.CRecord (k, [(nm, v)]), loc), adamc@71: rest), loc) adamc@71: adamc@71: fun foldType (dom, loc) = adamc@71: (L'.TCFun (L'.Explicit, "ran", (L'.KArrow ((L'.KRecord dom, loc), (L'.KType, loc)), loc), adamc@71: (L'.TFun ((L'.TCFun (L'.Explicit, "nm", (L'.KName, loc), adamc@71: (L'.TCFun (L'.Explicit, "v", dom, adamc@71: (L'.TCFun (L'.Explicit, "rest", (L'.KRecord dom, loc), adamc@71: (L'.TFun ((L'.CApp ((L'.CRel 3, loc), (L'.CRel 0, loc)), loc), adamc@71: (L'.CApp ((L'.CRel 3, loc), adamc@71: recCons (dom, adamc@71: (L'.CRel 2, loc), adamc@71: (L'.CRel 1, loc), adamc@71: (L'.CRel 0, loc), adamc@71: loc)), loc)), loc)), adamc@71: loc)), loc)), loc), adamc@71: (L'.TFun ((L'.CApp ((L'.CRel 0, loc), (L'.CRecord (dom, []), loc)), loc), adamc@71: (L'.TCFun (L'.Explicit, "r", (L'.KRecord dom, loc), adamc@71: (L'.CApp ((L'.CRel 1, loc), (L'.CRel 0, loc)), loc)), loc)), adamc@71: loc)), loc)), loc) adamc@71: adamc@15: fun typeof env (e, loc) = adamc@15: case e of adamc@56: L'.EPrim p => primType env p adamc@15: | L'.ERel n => #2 (E.lookupERel env n) adamc@15: | L'.ENamed n => #2 (E.lookupENamed env n) adamc@34: | L'.EModProj (n, ms, x) => adamc@34: let adamc@34: val (_, sgn) = E.lookupStrNamed env n adamc@34: val (str, sgn) = foldl (fn (m, (str, sgn)) => adamc@34: case E.projectStr env {sgn = sgn, str = str, field = m} of adamc@147: NONE => raise Fail "typeof: Unknown substructure" adamc@34: | SOME sgn => ((L'.StrProj (str, m), loc), sgn)) adamc@34: ((L'.StrVar n, loc), sgn) ms adamc@34: in adamc@34: case E.projectVal env {sgn = sgn, str = str, field = x} of adamc@34: NONE => raise Fail "typeof: Unknown val in structure" adamc@34: | SOME t => t adamc@34: end adamc@34: adamc@15: | L'.EApp (e1, _) => adamc@15: (case #1 (typeof env e1) of adamc@15: L'.TFun (_, c) => c adamc@15: | _ => raise Fail "typeof: Bad EApp") adamc@26: | L'.EAbs (_, _, ran, _) => ran adamc@15: | L'.ECApp (e1, c) => adamc@15: (case #1 (typeof env e1) of adamc@15: L'.TCFun (_, _, _, c1) => subConInCon (0, c) c1 adamc@15: | _ => raise Fail "typeof: Bad ECApp") adamc@15: | L'.ECAbs (expl, x, k, e1) => (L'.TCFun (expl, x, k, typeof (E.pushCRel env x k) e1), loc) adamc@15: adamc@29: | L'.ERecord xes => (L'.TRecord (L'.CRecord (ktype, map (fn (x, _, t) => (x, t)) xes), loc), loc) adamc@15: | L'.EField (_, _, {field, ...}) => field adamc@71: | L'.EFold dom => foldType (dom, loc) adamc@15: adamc@15: | L'.EError => cerror adamc@15: adamc@86: fun elabHead (env, denv) (e as (_, loc)) t = adamc@15: let adamc@15: fun unravel (t, e) = adamc@86: let adamc@86: val (t, gs) = hnormCon (env, denv) t adamc@86: in adamc@86: case t of adamc@86: (L'.TCFun (L'.Implicit, x, k, t'), _) => adamc@86: let adamc@86: val u = cunif (loc, k) adamc@86: adamc@86: val (e, t, gs') = unravel (subConInCon (0, u) t', adamc@86: (L'.ECApp (e, u), loc)) adamc@86: in adamc@86: (e, t, gs @ gs') adamc@86: end adamc@86: | _ => (e, t, gs) adamc@86: end adamc@15: in adamc@15: unravel (t, e) adamc@15: end adamc@15: adamc@92: fun elabExp (env, denv) (eAll as (e, loc)) = adamc@91: let adamc@92: adamc@92: in adamc@92: (*eprefaces "elabExp" [("eAll", SourcePrint.p_exp eAll)];*) adamc@10: adamc@91: case e of adamc@91: L.EAnnot (e, t) => adamc@91: let adamc@91: val (e', et, gs1) = elabExp (env, denv) e adamc@91: val (t', _, gs2) = elabCon (env, denv) t adamc@91: val gs3 = checkCon (env, denv) e' et t' adamc@91: in adamc@91: (e', t', gs1 @ gs2 @ gs3) adamc@91: end adamc@34: adamc@91: | L.EPrim p => ((L'.EPrim p, loc), primType env p, []) adamc@91: | L.EVar ([], s) => adamc@91: (case E.lookupE env s of adamc@91: E.NotBound => adamc@91: (expError env (UnboundExp (loc, s)); adamc@91: (eerror, cerror, [])) adamc@91: | E.Rel (n, t) => ((L'.ERel n, loc), t, []) adamc@91: | E.Named (n, t) => ((L'.ENamed n, loc), t, [])) adamc@91: | L.EVar (m1 :: ms, s) => adamc@91: (case E.lookupStr env m1 of adamc@91: NONE => (expError env (UnboundStrInExp (loc, m1)); adamc@91: (eerror, cerror, [])) adamc@91: | SOME (n, sgn) => adamc@91: let adamc@91: val (str, sgn) = foldl (fn (m, (str, sgn)) => adamc@91: case E.projectStr env {sgn = sgn, str = str, field = m} of adamc@91: NONE => (conError env (UnboundStrInCon (loc, m)); adamc@91: (strerror, sgnerror)) adamc@91: | SOME sgn => ((L'.StrProj (str, m), loc), sgn)) adamc@91: ((L'.StrVar n, loc), sgn) ms adamc@10: adamc@91: val t = case E.projectVal env {sgn = sgn, str = str, field = s} of adamc@91: NONE => (expError env (UnboundExp (loc, s)); adamc@91: cerror) adamc@91: | SOME t => t adamc@91: in adamc@91: ((L'.EModProj (n, ms, s), loc), t, []) adamc@91: end) adamc@86: adamc@91: | L.EApp (e1, e2) => adamc@91: let adamc@91: val (e1', t1, gs1) = elabExp (env, denv) e1 adamc@91: val (e1', t1, gs2) = elabHead (env, denv) e1' t1 adamc@91: val (e2', t2, gs3) = elabExp (env, denv) e2 adamc@71: adamc@91: val dom = cunif (loc, ktype) adamc@91: val ran = cunif (loc, ktype) adamc@91: val t = (L'.TFun (dom, ran), dummy) adamc@91: adamc@91: val gs4 = checkCon (env, denv) e1' t1 t adamc@91: val gs5 = checkCon (env, denv) e2' t2 dom adamc@91: in adamc@91: ((L'.EApp (e1', e2'), loc), ran, gs1 @ gs2 @ gs3 @ gs4 @ gs5) adamc@91: end adamc@91: | L.EAbs (x, to, e) => adamc@91: let adamc@91: val (t', gs1) = case to of adamc@91: NONE => (cunif (loc, ktype), []) adamc@91: | SOME t => adamc@91: let adamc@91: val (t', tk, gs) = elabCon (env, denv) t adamc@91: in adamc@91: checkKind env t' tk ktype; adamc@91: (t', gs) adamc@91: end adamc@91: val (e', et, gs2) = elabExp (E.pushERel env x t', denv) e adamc@91: in adamc@91: ((L'.EAbs (x, t', et, e'), loc), adamc@91: (L'.TFun (t', et), loc), adamc@91: gs1 @ gs2) adamc@91: end adamc@91: | L.ECApp (e, c) => adamc@91: let adamc@91: val (e', et, gs1) = elabExp (env, denv) e adamc@91: val (e', et, gs2) = elabHead (env, denv) e' et adamc@91: val (c', ck, gs3) = elabCon (env, denv) c adamc@91: val ((et', _), gs4) = hnormCon (env, denv) et adamc@91: in adamc@91: case et' of adamc@91: L'.CError => (eerror, cerror, []) adamc@91: | L'.TCFun (_, _, k, eb) => adamc@91: let adamc@91: val () = checkKind env c' ck k adamc@91: val eb' = subConInCon (0, c') eb adamc@91: handle SynUnif => (expError env (Unif ("substitution", eb)); adamc@91: cerror) adamc@91: in adamc@91: ((L'.ECApp (e', c'), loc), eb', gs1 @ gs2 @ gs3 @ gs4) adamc@91: end adamc@91: adamc@91: | L'.CUnif _ => adamc@91: (expError env (Unif ("application", et)); adamc@91: (eerror, cerror, [])) adamc@91: adamc@91: | _ => adamc@91: (expError env (WrongForm ("constructor function", e', et)); adamc@91: (eerror, cerror, [])) adamc@91: end adamc@91: | L.ECAbs (expl, x, k, e) => adamc@91: let adamc@91: val expl' = elabExplicitness expl adamc@91: val k' = elabKind k adamc@91: val (e', et, gs) = elabExp (E.pushCRel env x k', D.enter denv) e adamc@91: in adamc@91: ((L'.ECAbs (expl', x, k', e'), loc), adamc@91: (L'.TCFun (expl', x, k', et), loc), adamc@91: gs) adamc@91: end adamc@91: adamc@91: | L.EDisjoint (c1, c2, e) => adamc@91: let adamc@91: val (c1', k1, gs1) = elabCon (env, denv) c1 adamc@91: val (c2', k2, gs2) = elabCon (env, denv) c2 adamc@91: adamc@91: val ku1 = kunif loc adamc@91: val ku2 = kunif loc adamc@91: adamc@91: val (denv', gs3) = D.assert env denv (c1', c2') adamc@91: val (e', t, gs4) = elabExp (env, denv') e adamc@91: in adamc@91: checkKind env c1' k1 (L'.KRecord ku1, loc); adamc@91: checkKind env c2' k2 (L'.KRecord ku2, loc); adamc@91: adamc@91: (e', (L'.TDisjoint (c1', c2', t), loc), gs1 @ gs2 @ gs3 @ gs4) adamc@91: end adamc@91: adamc@91: | L.ERecord xes => adamc@91: let adamc@91: val (xes', gs) = ListUtil.foldlMap (fn ((x, e), gs) => adamc@91: let adamc@91: val (x', xk, gs1) = elabCon (env, denv) x adamc@91: val (e', et, gs2) = elabExp (env, denv) e adamc@91: in adamc@91: checkKind env x' xk kname; adamc@91: ((x', e', et), gs1 @ gs2 @ gs) adamc@91: end) adamc@91: [] xes adamc@91: adamc@91: val k = (L'.KType, loc) adamc@91: adamc@91: fun prove (xets, gs) = adamc@91: case xets of adamc@91: [] => gs adamc@91: | (x, _, t) :: rest => adamc@91: let adamc@91: val xc = (x, t) adamc@91: val r1 = (L'.CRecord (k, [xc]), loc) adamc@91: val gs = foldl (fn ((x', _, t'), gs) => adamc@91: let adamc@91: val xc' = (x', t') adamc@91: val r2 = (L'.CRecord (k, [xc']), loc) adamc@91: in adamc@91: D.prove env denv (r1, r2, loc) @ gs adamc@91: end) adamc@91: gs rest adamc@91: in adamc@91: prove (rest, gs) adamc@91: end adamc@91: in adamc@91: ((L'.ERecord xes', loc), adamc@91: (L'.TRecord (L'.CRecord (ktype, map (fn (x', _, et) => (x', et)) xes'), loc), loc), adamc@91: prove (xes', gs)) adamc@91: end adamc@91: adamc@91: | L.EField (e, c) => adamc@91: let adamc@91: val (e', et, gs1) = elabExp (env, denv) e adamc@91: val (c', ck, gs2) = elabCon (env, denv) c adamc@91: adamc@91: val ft = cunif (loc, ktype) adamc@91: val rest = cunif (loc, ktype_record) adamc@91: val first = (L'.CRecord (ktype, [(c', ft)]), loc) adamc@91: adamc@91: val gs3 = adamc@91: checkCon (env, denv) e' et adamc@91: (L'.TRecord (L'.CConcat (first, rest), loc), loc) adamc@91: val gs4 = D.prove env denv (first, rest, loc) adamc@91: in adamc@91: ((L'.EField (e', c', {field = ft, rest = rest}), loc), ft, gs1 @ gs2 @ gs3 @ gs4) adamc@91: end adamc@91: adamc@91: | L.EFold => adamc@91: let adamc@91: val dom = kunif loc adamc@91: in adamc@91: ((L'.EFold dom, loc), foldType (dom, loc), []) adamc@91: end adamc@91: end adamc@12: adamc@12: adamc@6: datatype decl_error = adamc@91: KunifsRemain of L'.decl list adamc@91: | CunifsRemain of L'.decl list adamc@91: adamc@91: fun lspan [] = ErrorMsg.dummySpan adamc@91: | lspan ((_, loc) :: _) = loc adamc@6: adamc@6: fun declError env err = adamc@6: case err of adamc@91: KunifsRemain ds => adamc@91: (ErrorMsg.errorAt (lspan ds) "Some kind unification variables are undetermined in declaration"; adamc@91: eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)]) adamc@91: | CunifsRemain ds => adamc@91: (ErrorMsg.errorAt (lspan ds) "Some constructor unification variables are undetermined in declaration"; adamc@91: eprefaces' [("Decl", p_list_sep PD.newline (p_decl env) ds)]) adamc@6: adamc@31: datatype sgn_error = adamc@31: UnboundSgn of ErrorMsg.span * string adamc@31: | UnmatchedSgi of L'.sgn_item adamc@31: | SgiWrongKind of L'.sgn_item * L'.kind * L'.sgn_item * L'.kind * kunify_error adamc@31: | SgiWrongCon of L'.sgn_item * L'.con * L'.sgn_item * L'.con * cunify_error adamc@41: | SgnWrongForm of L'.sgn * L'.sgn adamc@42: | UnWhereable of L'.sgn * string adamc@75: | WhereWrongKind of L'.kind * L'.kind * kunify_error adamc@58: | NotIncludable of L'.sgn adamc@62: | DuplicateCon of ErrorMsg.span * string adamc@62: | DuplicateVal of ErrorMsg.span * string adamc@62: | DuplicateSgn of ErrorMsg.span * string adamc@62: | DuplicateStr of ErrorMsg.span * string adamc@88: | NotConstraintsable of L'.sgn adamc@31: adamc@31: fun sgnError env err = adamc@31: case err of adamc@31: UnboundSgn (loc, s) => adamc@31: ErrorMsg.errorAt loc ("Unbound signature variable " ^ s) adamc@31: | UnmatchedSgi (sgi as (_, loc)) => adamc@31: (ErrorMsg.errorAt loc "Unmatched signature item"; adamc@31: eprefaces' [("Item", p_sgn_item env sgi)]) adamc@31: | SgiWrongKind (sgi1, k1, sgi2, k2, kerr) => adamc@31: (ErrorMsg.errorAt (#2 sgi1) "Kind unification failure in signature matching:"; adamc@59: eprefaces' [("Have", p_sgn_item env sgi1), adamc@59: ("Need", p_sgn_item env sgi2), adamc@31: ("Kind 1", p_kind k1), adamc@31: ("Kind 2", p_kind k2)]; adamc@31: kunifyError kerr) adamc@31: | SgiWrongCon (sgi1, c1, sgi2, c2, cerr) => adamc@31: (ErrorMsg.errorAt (#2 sgi1) "Constructor unification failure in signature matching:"; adamc@59: eprefaces' [("Have", p_sgn_item env sgi1), adamc@59: ("Need", p_sgn_item env sgi2), adamc@31: ("Con 1", p_con env c1), adamc@31: ("Con 2", p_con env c2)]; adamc@31: cunifyError env cerr) adamc@41: | SgnWrongForm (sgn1, sgn2) => adamc@41: (ErrorMsg.errorAt (#2 sgn1) "Incompatible signatures:"; adamc@41: eprefaces' [("Sig 1", p_sgn env sgn1), adamc@41: ("Sig 2", p_sgn env sgn2)]) adamc@42: | UnWhereable (sgn, x) => adamc@42: (ErrorMsg.errorAt (#2 sgn) "Unavailable field for 'where'"; adamc@42: eprefaces' [("Signature", p_sgn env sgn), adamc@42: ("Field", PD.string x)]) adamc@75: | WhereWrongKind (k1, k2, kerr) => adamc@75: (ErrorMsg.errorAt (#2 k1) "Wrong kind for 'where'"; adamc@75: eprefaces' [("Have", p_kind k1), adamc@75: ("Need", p_kind k2)]; adamc@75: kunifyError kerr) adamc@58: | NotIncludable sgn => adamc@58: (ErrorMsg.errorAt (#2 sgn) "Invalid signature to 'include'"; adamc@58: eprefaces' [("Signature", p_sgn env sgn)]) adamc@62: | DuplicateCon (loc, s) => adamc@62: ErrorMsg.errorAt loc ("Duplicate constructor " ^ s ^ " in signature") adamc@62: | DuplicateVal (loc, s) => adamc@62: ErrorMsg.errorAt loc ("Duplicate value " ^ s ^ " in signature") adamc@62: | DuplicateSgn (loc, s) => adamc@62: ErrorMsg.errorAt loc ("Duplicate signature " ^ s ^ " in signature") adamc@62: | DuplicateStr (loc, s) => adamc@62: ErrorMsg.errorAt loc ("Duplicate structure " ^ s ^ " in signature") adamc@88: | NotConstraintsable sgn => adamc@88: (ErrorMsg.errorAt (#2 sgn) "Invalid signature for 'open constraints'"; adamc@88: eprefaces' [("Signature", p_sgn env sgn)]) adamc@31: adamc@31: datatype str_error = adamc@31: UnboundStr of ErrorMsg.span * string adamc@44: | NotFunctor of L'.sgn adamc@46: | FunctorRebind of ErrorMsg.span adamc@61: | UnOpenable of L'.sgn adamc@75: | NotType of L'.kind * (L'.kind * L'.kind * kunify_error) adamc@31: adamc@31: fun strError env err = adamc@31: case err of adamc@31: UnboundStr (loc, s) => adamc@31: ErrorMsg.errorAt loc ("Unbound structure variable " ^ s) adamc@44: | NotFunctor sgn => adamc@44: (ErrorMsg.errorAt (#2 sgn) "Application of non-functor"; adamc@44: eprefaces' [("Signature", p_sgn env sgn)]) adamc@46: | FunctorRebind loc => adamc@46: ErrorMsg.errorAt loc "Attempt to rebind functor" adamc@61: | UnOpenable sgn => adamc@61: (ErrorMsg.errorAt (#2 sgn) "Un-openable structure"; adamc@61: eprefaces' [("Signature", p_sgn env sgn)]) adamc@75: | NotType (k, (k1, k2, ue)) => adamc@75: (ErrorMsg.errorAt (#2 k) "'val' type kind is not 'Type'"; adamc@75: eprefaces' [("Kind", p_kind k), adamc@75: ("Subkind 1", p_kind k1), adamc@75: ("Subkind 2", p_kind k2)]; adamc@75: kunifyError ue) adamc@31: adamc@42: val hnormSgn = E.hnormSgn adamc@31: adamc@88: fun elabSgn_item ((sgi, loc), (env, denv, gs)) = adamc@76: case sgi of adamc@76: L.SgiConAbs (x, k) => adamc@76: let adamc@76: val k' = elabKind k adamc@31: adamc@76: val (env', n) = E.pushCNamed env x k' NONE adamc@76: in adamc@88: ([(L'.SgiConAbs (x, n, k'), loc)], (env', denv, gs)) adamc@76: end adamc@31: adamc@76: | L.SgiCon (x, ko, c) => adamc@76: let adamc@76: val k' = case ko of adamc@76: NONE => kunif loc adamc@76: | SOME k => elabKind k adamc@31: adamc@83: val (c', ck, gs') = elabCon (env, denv) c adamc@76: val (env', n) = E.pushCNamed env x k' (SOME c') adamc@76: in adamc@76: checkKind env c' ck k'; adamc@31: adamc@88: ([(L'.SgiCon (x, n, k', c'), loc)], (env', denv, gs' @ gs)) adamc@76: end adamc@31: adamc@76: | L.SgiVal (x, c) => adamc@76: let adamc@83: val (c', ck, gs') = elabCon (env, denv) c adamc@31: adamc@76: val (env', n) = E.pushENamed env x c' adamc@76: in adamc@76: (unifyKinds ck ktype adamc@76: handle KUnify ue => strError env (NotType (ck, ue))); adamc@31: adamc@88: ([(L'.SgiVal (x, n, c'), loc)], (env', denv, gs' @ gs)) adamc@76: end adamc@31: adamc@76: | L.SgiStr (x, sgn) => adamc@76: let adamc@83: val (sgn', gs') = elabSgn (env, denv) sgn adamc@76: val (env', n) = E.pushStrNamed env x sgn' adamc@76: in adamc@88: ([(L'.SgiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs)) adamc@76: end adamc@31: adamc@76: | L.SgiSgn (x, sgn) => adamc@76: let adamc@83: val (sgn', gs') = elabSgn (env, denv) sgn adamc@76: val (env', n) = E.pushSgnNamed env x sgn' adamc@76: in adamc@88: ([(L'.SgiSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs)) adamc@76: end adamc@31: adamc@76: | L.SgiInclude sgn => adamc@76: let adamc@83: val (sgn', gs') = elabSgn (env, denv) sgn adamc@76: in adamc@76: case #1 (hnormSgn env sgn') of adamc@76: L'.SgnConst sgis => adamc@88: (sgis, (foldl (fn (sgi, env) => E.sgiBinds env sgi) env sgis, denv, gs' @ gs)) adamc@76: | _ => (sgnError env (NotIncludable sgn'); adamc@88: ([], (env, denv, []))) adamc@88: end adamc@88: adamc@88: | L.SgiConstraint (c1, c2) => adamc@88: let adamc@88: val (c1', k1, gs1) = elabCon (env, denv) c1 adamc@88: val (c2', k2, gs2) = elabCon (env, denv) c2 adamc@88: adamc@90: val (denv, gs3) = D.assert env denv (c1', c2') adamc@88: in adamc@88: checkKind env c1' k1 (L'.KRecord (kunif loc), loc); adamc@88: checkKind env c2' k2 (L'.KRecord (kunif loc), loc); adamc@88: adamc@90: ([(L'.SgiConstraint (c1', c2'), loc)], (env, denv, gs1 @ gs2 @ gs3)) adamc@76: end adamc@31: adamc@83: and elabSgn (env, denv) (sgn, loc) = adamc@31: case sgn of adamc@31: L.SgnConst sgis => adamc@31: let adamc@88: val (sgis', (_, _, gs)) = ListUtil.foldlMapConcat elabSgn_item (env, denv, []) sgis adamc@62: adamc@62: val _ = foldl (fn ((sgi, loc), (cons, vals, sgns, strs)) => adamc@62: case sgi of adamc@62: L'.SgiConAbs (x, _, _) => adamc@62: (if SS.member (cons, x) then adamc@62: sgnError env (DuplicateCon (loc, x)) adamc@62: else adamc@62: (); adamc@62: (SS.add (cons, x), vals, sgns, strs)) adamc@62: | L'.SgiCon (x, _, _, _) => adamc@62: (if SS.member (cons, x) then adamc@62: sgnError env (DuplicateCon (loc, x)) adamc@62: else adamc@62: (); adamc@62: (SS.add (cons, x), vals, sgns, strs)) adamc@62: | L'.SgiVal (x, _, _) => adamc@62: (if SS.member (vals, x) then adamc@62: sgnError env (DuplicateVal (loc, x)) adamc@62: else adamc@62: (); adamc@62: (cons, SS.add (vals, x), sgns, strs)) adamc@62: | L'.SgiSgn (x, _, _) => adamc@62: (if SS.member (sgns, x) then adamc@62: sgnError env (DuplicateSgn (loc, x)) adamc@62: else adamc@62: (); adamc@62: (cons, vals, SS.add (sgns, x), strs)) adamc@62: | L'.SgiStr (x, _, _) => adamc@62: (if SS.member (strs, x) then adamc@62: sgnError env (DuplicateStr (loc, x)) adamc@62: else adamc@62: (); adamc@88: (cons, vals, sgns, SS.add (strs, x))) adamc@88: | L'.SgiConstraint _ => (cons, vals, sgns, strs)) adamc@62: (SS.empty, SS.empty, SS.empty, SS.empty) sgis' adamc@31: in adamc@83: ((L'.SgnConst sgis', loc), gs) adamc@31: end adamc@31: | L.SgnVar x => adamc@31: (case E.lookupSgn env x of adamc@31: NONE => adamc@31: (sgnError env (UnboundSgn (loc, x)); adamc@83: ((L'.SgnError, loc), [])) adamc@83: | SOME (n, sgis) => ((L'.SgnVar n, loc), [])) adamc@41: | L.SgnFun (m, dom, ran) => adamc@41: let adamc@83: val (dom', gs1) = elabSgn (env, denv) dom adamc@41: val (env', n) = E.pushStrNamed env m dom' adamc@83: val (ran', gs2) = elabSgn (env', denv) ran adamc@41: in adamc@83: ((L'.SgnFun (m, n, dom', ran'), loc), gs1 @ gs2) adamc@41: end adamc@42: | L.SgnWhere (sgn, x, c) => adamc@42: let adamc@83: val (sgn', ds1) = elabSgn (env, denv) sgn adamc@83: val (c', ck, ds2) = elabCon (env, denv) c adamc@42: in adamc@42: case #1 (hnormSgn env sgn') of adamc@83: L'.SgnError => (sgnerror, []) adamc@42: | L'.SgnConst sgis => adamc@75: if List.exists (fn (L'.SgiConAbs (x', _, k), _) => adamc@75: x' = x andalso adamc@75: (unifyKinds k ck adamc@75: handle KUnify x => sgnError env (WhereWrongKind x); adamc@42: true) adamc@42: | _ => false) sgis then adamc@83: ((L'.SgnWhere (sgn', x, c'), loc), ds1 @ ds2) adamc@42: else adamc@42: (sgnError env (UnWhereable (sgn', x)); adamc@83: (sgnerror, [])) adamc@42: | _ => (sgnError env (UnWhereable (sgn', x)); adamc@83: (sgnerror, [])) adamc@42: end adamc@59: | L.SgnProj (m, ms, x) => adamc@59: (case E.lookupStr env m of adamc@59: NONE => (strError env (UnboundStr (loc, m)); adamc@83: (sgnerror, [])) adamc@59: | SOME (n, sgn) => adamc@59: let adamc@59: val (str, sgn) = foldl (fn (m, (str, sgn)) => adamc@59: case E.projectStr env {sgn = sgn, str = str, field = m} of adamc@59: NONE => (strError env (UnboundStr (loc, m)); adamc@59: (strerror, sgnerror)) adamc@59: | SOME sgn => ((L'.StrProj (str, m), loc), sgn)) adamc@59: ((L'.StrVar n, loc), sgn) ms adamc@59: in adamc@59: case E.projectSgn env {sgn = sgn, str = str, field = x} of adamc@59: NONE => (sgnError env (UnboundSgn (loc, x)); adamc@83: (sgnerror, [])) adamc@83: | SOME _ => ((L'.SgnProj (n, ms, x), loc), []) adamc@59: end) adamc@59: adamc@31: adamc@66: fun selfify env {str, strs, sgn} = adamc@66: case #1 (hnormSgn env sgn) of adamc@66: L'.SgnError => sgn adamc@66: | L'.SgnVar _ => sgn adamc@66: adamc@66: | L'.SgnConst sgis => adamc@66: (L'.SgnConst (map (fn (L'.SgiConAbs (x, n, k), loc) => adamc@66: (L'.SgiCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc) adamc@66: | (L'.SgiStr (x, n, sgn), loc) => adamc@66: (L'.SgiStr (x, n, selfify env {str = str, strs = strs @ [x], sgn = sgn}), loc) adamc@66: | x => x) sgis), #2 sgn) adamc@66: | L'.SgnFun _ => sgn adamc@66: | L'.SgnWhere _ => sgn adamc@66: | L'.SgnProj (m, ms, x) => adamc@66: case E.projectSgn env {str = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn)) adamc@66: (L'.StrVar m, #2 sgn) ms, adamc@66: sgn = #2 (E.lookupStrNamed env m), adamc@66: field = x} of adamc@66: NONE => raise Fail "Elaborate.selfify: projectSgn returns NONE" adamc@66: | SOME sgn => selfify env {str = str, strs = strs, sgn = sgn} adamc@66: adamc@66: fun selfifyAt env {str, sgn} = adamc@66: let adamc@66: fun self (str, _) = adamc@66: case str of adamc@66: L'.StrVar x => SOME (x, []) adamc@66: | L'.StrProj (str, x) => adamc@66: (case self str of adamc@66: NONE => NONE adamc@66: | SOME (m, ms) => SOME (m, ms @ [x])) adamc@66: | _ => NONE adamc@66: in adamc@66: case self str of adamc@66: NONE => sgn adamc@66: | SOME (str, strs) => selfify env {sgn = sgn, str = str, strs = strs} adamc@66: end adamc@66: adamc@88: fun dopen (env, denv) {str, strs, sgn} = adamc@66: let adamc@66: val m = foldl (fn (m, str) => (L'.StrProj (str, m), #2 sgn)) adamc@66: (L'.StrVar str, #2 sgn) strs adamc@66: in adamc@66: case #1 (hnormSgn env sgn) of adamc@66: L'.SgnConst sgis => adamc@88: ListUtil.foldlMap (fn ((sgi, loc), (env', denv')) => adamc@66: case sgi of adamc@66: L'.SgiConAbs (x, n, k) => adamc@88: let adamc@88: val c = (L'.CModProj (str, strs, x), loc) adamc@88: in adamc@88: ((L'.DCon (x, n, k, c), loc), adamc@88: (E.pushCNamedAs env' x n k (SOME c), denv')) adamc@88: end adamc@66: | L'.SgiCon (x, n, k, c) => adamc@66: ((L'.DCon (x, n, k, (L'.CModProj (str, strs, x), loc)), loc), adamc@88: (E.pushCNamedAs env' x n k (SOME c), denv')) adamc@66: | L'.SgiVal (x, n, t) => adamc@66: ((L'.DVal (x, n, t, (L'.EModProj (str, strs, x), loc)), loc), adamc@88: (E.pushENamedAs env' x n t, denv')) adamc@66: | L'.SgiStr (x, n, sgn) => adamc@66: ((L'.DStr (x, n, sgn, (L'.StrProj (m, x), loc)), loc), adamc@88: (E.pushStrNamedAs env' x n sgn, denv')) adamc@66: | L'.SgiSgn (x, n, sgn) => adamc@66: ((L'.DSgn (x, n, (L'.SgnProj (str, strs, x), loc)), loc), adamc@88: (E.pushSgnNamedAs env' x n sgn, denv')) adamc@88: | L'.SgiConstraint (c1, c2) => adamc@88: ((L'.DConstraint (c1, c2), loc), adamc@88: (env', denv (* D.assert env denv (c1, c2) *) ))) adamc@88: (env, denv) sgis adamc@66: | _ => (strError env (UnOpenable sgn); adamc@88: ([], (env, denv))) adamc@66: end adamc@66: adamc@88: fun dopenConstraints (loc, env, denv) {str, strs} = adamc@88: case E.lookupStr env str of adamc@88: NONE => (strError env (UnboundStr (loc, str)); adamc@88: denv) adamc@88: | SOME (n, sgn) => adamc@88: let adamc@88: val (st, sgn) = foldl (fn (m, (str, sgn)) => adamc@88: case E.projectStr env {str = str, sgn = sgn, field = m} of adamc@88: NONE => (strError env (UnboundStr (loc, m)); adamc@88: (strerror, sgnerror)) adamc@88: | SOME sgn => ((L'.StrProj (str, m), loc), sgn)) adamc@88: ((L'.StrVar n, loc), sgn) strs adamc@88: adamc@88: val cso = E.projectConstraints env {sgn = sgn, str = st} adamc@88: adamc@88: val denv = case cso of adamc@88: NONE => (strError env (UnboundStr (loc, str)); adamc@88: denv) adamc@90: | SOME cs => foldl (fn ((c1, c2), denv) => adamc@90: let adamc@90: val (denv, gs) = D.assert env denv (c1, c2) adamc@90: in adamc@90: case gs of adamc@90: [] => () adamc@90: | _ => raise Fail "dopenConstraints: Sub-constraints remain"; adamc@90: adamc@90: denv adamc@90: end) denv cs adamc@88: in adamc@88: denv adamc@88: end adamc@88: adamc@31: fun sgiOfDecl (d, loc) = adamc@31: case d of adamc@123: L'.DCon (x, n, k, c) => [(L'.SgiCon (x, n, k, c), loc)] adamc@123: | L'.DVal (x, n, t, _) => [(L'.SgiVal (x, n, t), loc)] adamc@123: | L'.DValRec vis => map (fn (x, n, t, _) => (L'.SgiVal (x, n, t), loc)) vis adamc@123: | L'.DSgn (x, n, sgn) => [(L'.SgiSgn (x, n, sgn), loc)] adamc@123: | L'.DStr (x, n, sgn, _) => [(L'.SgiStr (x, n, sgn), loc)] adamc@123: | L'.DFfiStr (x, n, sgn) => [(L'.SgiStr (x, n, sgn), loc)] adamc@123: | L'.DConstraint cs => [(L'.SgiConstraint cs, loc)] adamc@123: | L'.DExport _ => [] adamc@88: adamc@88: fun sgiBindsD (env, denv) (sgi, _) = adamc@88: case sgi of adamc@90: L'.SgiConstraint (c1, c2) => adamc@90: (case D.assert env denv (c1, c2) of adamc@90: (denv, []) => denv adamc@90: | _ => raise Fail "sgiBindsD: Sub-constraints remain") adamc@88: | _ => denv adamc@31: adamc@86: fun subSgn (env, denv) sgn1 (sgn2 as (_, loc2)) = adamc@35: case (#1 (hnormSgn env sgn1), #1 (hnormSgn env sgn2)) of adamc@31: (L'.SgnError, _) => () adamc@31: | (_, L'.SgnError) => () adamc@31: adamc@31: | (L'.SgnConst sgis1, L'.SgnConst sgis2) => adamc@31: let adamc@88: fun folder (sgi2All as (sgi, _), (env, denv)) = adamc@31: let adamc@31: fun seek p = adamc@31: let adamc@88: fun seek (env, denv) ls = adamc@31: case ls of adamc@31: [] => (sgnError env (UnmatchedSgi sgi2All); adamc@88: (env, denv)) adamc@31: | h :: t => adamc@31: case p h of adamc@88: NONE => seek (E.sgiBinds env h, sgiBindsD (env, denv) h) t adamc@88: | SOME envs => envs adamc@31: in adamc@88: seek (env, denv) sgis1 adamc@31: end adamc@31: in adamc@31: case sgi of adamc@31: L'.SgiConAbs (x, n2, k2) => adamc@31: seek (fn sgi1All as (sgi1, _) => adamc@31: let adamc@41: fun found (x', n1, k1, co1) = adamc@41: if x = x' then adamc@41: let adamc@41: val () = unifyKinds k1 k2 adamc@41: handle KUnify (k1, k2, err) => adamc@41: sgnError env (SgiWrongKind (sgi1All, k1, sgi2All, k2, err)) adamc@41: val env = E.pushCNamedAs env x n1 k1 co1 adamc@41: in adamc@41: SOME (if n1 = n2 then adamc@41: env adamc@41: else adamc@88: E.pushCNamedAs env x n2 k2 (SOME (L'.CNamed n1, loc2)), adamc@88: denv) adamc@41: end adamc@41: else adamc@41: NONE adamc@31: in adamc@31: case sgi1 of adamc@41: L'.SgiConAbs (x', n1, k1) => found (x', n1, k1, NONE) adamc@41: | L'.SgiCon (x', n1, k1, c1) => found (x', n1, k1, SOME c1) adamc@31: | _ => NONE adamc@31: end) adamc@31: adamc@31: | L'.SgiCon (x, n2, k2, c2) => adamc@31: seek (fn sgi1All as (sgi1, _) => adamc@31: case sgi1 of adamc@41: L'.SgiCon (x', n1, k1, c1) => adamc@41: if x = x' then adamc@41: let adamc@88: fun good () = SOME (E.pushCNamedAs env x n2 k2 (SOME c2), denv) adamc@41: in adamc@86: (case unifyCons (env, denv) c1 c2 of adamc@86: [] => good () adamc@86: | _ => NONE) adamc@86: handle CUnify (c1, c2, err) => adamc@86: (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err)); adamc@86: good ()) adamc@41: end adamc@41: else adamc@41: NONE adamc@31: | _ => NONE) adamc@31: adamc@32: | L'.SgiVal (x, n2, c2) => adamc@32: seek (fn sgi1All as (sgi1, _) => adamc@32: case sgi1 of adamc@41: L'.SgiVal (x', n1, c1) => adamc@41: if x = x' then adamc@86: (case unifyCons (env, denv) c1 c2 of adamc@88: [] => SOME (env, denv) adamc@86: | _ => NONE) adamc@86: handle CUnify (c1, c2, err) => adamc@86: (sgnError env (SgiWrongCon (sgi1All, c1, sgi2All, c2, err)); adamc@88: SOME (env, denv)) adamc@41: else adamc@41: NONE adamc@32: | _ => NONE) adamc@32: adamc@33: | L'.SgiStr (x, n2, sgn2) => adamc@33: seek (fn sgi1All as (sgi1, _) => adamc@33: case sgi1 of adamc@41: L'.SgiStr (x', n1, sgn1) => adamc@41: if x = x' then adamc@66: let adamc@86: val () = subSgn (env, denv) sgn1 sgn2 adamc@66: val env = E.pushStrNamedAs env x n1 sgn1 adamc@66: val env = if n1 = n2 then adamc@66: env adamc@66: else adamc@66: E.pushStrNamedAs env x n2 adamc@66: (selfifyAt env {str = (L'.StrVar n1, #2 sgn2), adamc@66: sgn = sgn2}) adamc@66: in adamc@88: SOME (env, denv) adamc@66: end adamc@41: else adamc@41: NONE adamc@33: | _ => NONE) adamc@59: adamc@59: | L'.SgiSgn (x, n2, sgn2) => adamc@59: seek (fn sgi1All as (sgi1, _) => adamc@59: case sgi1 of adamc@59: L'.SgiSgn (x', n1, sgn1) => adamc@59: if x = x' then adamc@65: let adamc@86: val () = subSgn (env, denv) sgn1 sgn2 adamc@86: val () = subSgn (env, denv) sgn2 sgn1 adamc@65: adamc@65: val env = E.pushSgnNamedAs env x n2 sgn2 adamc@65: val env = if n1 = n2 then adamc@65: env adamc@65: else adamc@65: E.pushSgnNamedAs env x n1 sgn2 adamc@65: in adamc@88: SOME (env, denv) adamc@65: end adamc@59: else adamc@59: NONE adamc@59: | _ => NONE) adamc@88: adamc@88: | L'.SgiConstraint (c2, d2) => adamc@88: seek (fn sgi1All as (sgi1, _) => adamc@88: case sgi1 of adamc@88: L'.SgiConstraint (c1, d1) => adamc@88: if consEq (env, denv) (c1, c2) andalso consEq (env, denv) (d1, d2) then adamc@90: let adamc@90: val (denv, gs) = D.assert env denv (c2, d2) adamc@90: in adamc@90: case gs of adamc@90: [] => () adamc@90: | _ => raise Fail "subSgn: Sub-constraints remain"; adamc@90: adamc@90: SOME (env, denv) adamc@90: end adamc@88: else adamc@88: NONE adamc@88: | _ => NONE) adamc@31: end adamc@31: in adamc@88: ignore (foldl folder (env, denv) sgis2) adamc@31: end adamc@31: adamc@41: | (L'.SgnFun (m1, n1, dom1, ran1), L'.SgnFun (m2, n2, dom2, ran2)) => adamc@43: let adamc@43: val ran1 = adamc@43: if n1 = n2 then adamc@43: ran1 adamc@43: else adamc@43: subStrInSgn (n1, n2) ran1 adamc@43: in adamc@86: subSgn (env, denv) dom2 dom1; adamc@86: subSgn (E.pushStrNamedAs env m2 n2 dom2, denv) ran1 ran2 adamc@43: end adamc@41: adamc@41: | _ => sgnError env (SgnWrongForm (sgn1, sgn2)) adamc@41: adamc@61: adamc@88: fun elabDecl ((d, loc), (env, denv, gs)) = adamc@76: case d of adamc@76: L.DCon (x, ko, c) => adamc@76: let adamc@76: val k' = case ko of adamc@76: NONE => kunif loc adamc@76: | SOME k => elabKind k adamc@76: adamc@83: val (c', ck, gs') = elabCon (env, denv) c adamc@76: val (env', n) = E.pushCNamed env x k' (SOME c') adamc@76: in adamc@76: checkKind env c' ck k'; adamc@76: adamc@88: ([(L'.DCon (x, n, k', c'), loc)], (env', denv, gs' @ gs)) adamc@76: end adamc@76: | L.DVal (x, co, e) => adamc@76: let adamc@123: val (c', _, gs1) = case co of adamc@83: NONE => (cunif (loc, ktype), ktype, []) adamc@83: | SOME c => elabCon (env, denv) c adamc@76: adamc@83: val (e', et, gs2) = elabExp (env, denv) e adamc@76: val (env', n) = E.pushENamed env x c' adamc@86: adamc@86: val gs3 = checkCon (env, denv) e' et c' adamc@76: in adamc@88: ([(L'.DVal (x, n, c', e'), loc)], (env', denv, gs1 @ gs2 @ gs3 @ gs)) adamc@76: end adamc@123: | L.DValRec vis => adamc@123: let adamc@123: val (vis, gs) = ListUtil.foldlMap adamc@123: (fn ((x, co, e), gs) => adamc@123: let adamc@123: val (c', _, gs1) = case co of adamc@123: NONE => (cunif (loc, ktype), ktype, []) adamc@123: | SOME c => elabCon (env, denv) c adamc@123: in adamc@123: ((x, c', e), gs1 @ gs) adamc@123: end) [] vis adamc@123: adamc@123: val (vis, env) = ListUtil.foldlMap (fn ((x, c', e), env) => adamc@123: let adamc@123: val (env, n) = E.pushENamed env x c' adamc@123: in adamc@123: ((x, n, c', e), env) adamc@123: end) env vis adamc@123: adamc@123: val (vis, gs) = ListUtil.foldlMap (fn ((x, n, c', e), gs) => adamc@123: let adamc@123: val (e', et, gs1) = elabExp (env, denv) e adamc@123: adamc@123: val gs2 = checkCon (env, denv) e' et c' adamc@123: in adamc@123: ((x, n, c', e'), gs1 @ gs2 @ gs) adamc@123: end) gs vis adamc@123: in adamc@123: ([(L'.DValRec vis, loc)], (env, denv, gs)) adamc@123: end adamc@76: adamc@76: | L.DSgn (x, sgn) => adamc@76: let adamc@83: val (sgn', gs') = elabSgn (env, denv) sgn adamc@76: val (env', n) = E.pushSgnNamed env x sgn' adamc@76: in adamc@88: ([(L'.DSgn (x, n, sgn'), loc)], (env', denv, gs' @ gs)) adamc@76: end adamc@76: adamc@76: | L.DStr (x, sgno, str) => adamc@76: let adamc@93: val () = if x = "Basis" then adamc@93: raise Fail "Not allowed to redefine structure 'Basis'" adamc@93: else adamc@93: () adamc@93: adamc@83: val formal = Option.map (elabSgn (env, denv)) sgno adamc@76: adamc@83: val (str', sgn', gs') = adamc@78: case formal of adamc@78: NONE => adamc@78: let adamc@83: val (str', actual, ds) = elabStr (env, denv) str adamc@78: in adamc@83: (str', selfifyAt env {str = str', sgn = actual}, ds) adamc@78: end adamc@83: | SOME (formal, gs1) => adamc@78: let adamc@78: val str = adamc@78: case #1 (hnormSgn env formal) of adamc@78: L'.SgnConst sgis => adamc@78: (case #1 str of adamc@78: L.StrConst ds => adamc@78: let adamc@78: val needed = foldl (fn ((sgi, _), needed) => adamc@78: case sgi of adamc@78: L'.SgiConAbs (x, _, _) => SS.add (needed, x) adamc@78: | _ => needed) adamc@78: SS.empty sgis adamc@78: adamc@78: val needed = foldl (fn ((d, _), needed) => adamc@78: case d of adamc@78: L.DCon (x, _, _) => (SS.delete (needed, x) adamc@78: handle NotFound => needed) adamc@78: | L.DOpen _ => SS.empty adamc@78: | _ => needed) adamc@78: needed ds adamc@78: in adamc@78: case SS.listItems needed of adamc@78: [] => str adamc@78: | xs => adamc@78: let adamc@78: val kwild = (L.KWild, #2 str) adamc@78: val cwild = (L.CWild kwild, #2 str) adamc@78: val ds' = map (fn x => (L.DCon (x, NONE, cwild), #2 str)) xs adamc@78: in adamc@78: (L.StrConst (ds @ ds'), #2 str) adamc@78: end adamc@78: end adamc@78: | _ => str) adamc@78: | _ => str adamc@78: adamc@83: val (str', actual, gs2) = elabStr (env, denv) str adamc@78: in adamc@86: subSgn (env, denv) actual formal; adamc@83: (str', formal, gs1 @ gs2) adamc@78: end adamc@76: adamc@76: val (env', n) = E.pushStrNamed env x sgn' adamc@76: in adamc@76: case #1 (hnormSgn env sgn') of adamc@76: L'.SgnFun _ => adamc@76: (case #1 str' of adamc@76: L'.StrFun _ => () adamc@76: | _ => strError env (FunctorRebind loc)) adamc@76: | _ => (); adamc@76: adamc@88: ([(L'.DStr (x, n, sgn', str'), loc)], (env', denv, gs' @ gs)) adamc@76: end adamc@76: adamc@76: | L.DFfiStr (x, sgn) => adamc@76: let adamc@83: val (sgn', gs') = elabSgn (env, denv) sgn adamc@76: adamc@76: val (env', n) = E.pushStrNamed env x sgn' adamc@76: in adamc@88: ([(L'.DFfiStr (x, n, sgn'), loc)], (env', denv, gs' @ gs)) adamc@76: end adamc@76: adamc@76: | L.DOpen (m, ms) => adamc@88: (case E.lookupStr env m of adamc@88: NONE => (strError env (UnboundStr (loc, m)); adamc@88: ([], (env, denv, []))) adamc@88: | SOME (n, sgn) => adamc@88: let adamc@88: val (_, sgn) = foldl (fn (m, (str, sgn)) => adamc@88: case E.projectStr env {str = str, sgn = sgn, field = m} of adamc@88: NONE => (strError env (UnboundStr (loc, m)); adamc@88: (strerror, sgnerror)) adamc@88: | SOME sgn => ((L'.StrProj (str, m), loc), sgn)) adamc@88: ((L'.StrVar n, loc), sgn) ms adamc@83: adamc@88: val (ds, (env', denv')) = dopen (env, denv) {str = n, strs = ms, sgn = sgn} adamc@88: val denv' = dopenConstraints (loc, env', denv') {str = m, strs = ms} adamc@88: in adamc@88: (ds, (env', denv', [])) adamc@88: end) adamc@88: adamc@88: | L.DConstraint (c1, c2) => adamc@88: let adamc@88: val (c1', k1, gs1) = elabCon (env, denv) c1 adamc@88: val (c2', k2, gs2) = elabCon (env, denv) c2 adamc@90: val gs3 = D.prove env denv (c1', c2', loc) adamc@88: adamc@90: val (denv', gs4) = D.assert env denv (c1', c2') adamc@88: in adamc@88: checkKind env c1' k1 (L'.KRecord (kunif loc), loc); adamc@88: checkKind env c2' k2 (L'.KRecord (kunif loc), loc); adamc@88: adamc@90: ([(L'.DConstraint (c1', c2'), loc)], (env, denv', gs1 @ gs2 @ gs3 @ gs4)) adamc@88: end adamc@88: adamc@88: | L.DOpenConstraints (m, ms) => adamc@88: let adamc@88: val denv = dopenConstraints (loc, env, denv) {str = m, strs = ms} adamc@88: in adamc@88: ([], (env, denv, [])) adamc@88: end adamc@3: adamc@109: | L.DExport str => adamc@100: let adamc@109: val (str', sgn, gs) = elabStr (env, denv) str adamc@100: adamc@109: val sgn = adamc@109: case #1 (hnormSgn env sgn) of adamc@109: L'.SgnConst sgis => adamc@109: let adamc@146: fun doOne (all as (sgi, _), env) = adamc@146: (case sgi of adamc@146: L'.SgiVal (x, n, t) => adamc@146: (case hnormCon (env, denv) t of adamc@146: ((L'.TFun (dom, ran), _), []) => adamc@146: (case (hnormCon (env, denv) dom, hnormCon (env, denv) ran) of adamc@146: (((L'.TRecord domR, _), []), adamc@146: ((L'.CApp (tf, arg3), _), [])) => adamc@146: (case (hnormCon (env, denv) tf, hnormCon (env, denv) arg3) of adamc@146: (((L'.CApp (tf, arg2), _), []), adamc@146: (((L'.CRecord (_, []), _), []))) => adamc@146: (case (hnormCon (env, denv) tf) of adamc@146: ((L'.CApp (tf, arg1), _), []) => adamc@146: (case (hnormCon (env, denv) tf, adamc@146: hnormCon (env, denv) domR, adamc@146: hnormCon (env, denv) arg2) of adamc@146: ((tf, []), (domR, []), adamc@146: ((L'.CRecord (_, []), _), [])) => adamc@146: let adamc@146: val t = (L'.CApp (tf, arg1), loc) adamc@146: val t = (L'.CApp (t, arg2), loc) adamc@146: val t = (L'.CApp (t, arg3), loc) adamc@146: in adamc@146: (L'.SgiVal (x, n, (L'.TFun ((L'.TRecord domR, loc), adamc@146: t), adamc@146: loc)), loc) adamc@146: end adamc@146: | _ => all) adamc@146: | _ => all) adamc@146: | _ => all) adamc@146: | _ => all) adamc@146: | _ => all) adamc@146: | _ => all, adamc@146: E.sgiBinds env all) adamc@109: in adamc@146: (L'.SgnConst (#1 (ListUtil.foldlMap doOne env sgis)), loc) adamc@109: end adamc@109: | _ => sgn adamc@100: in adamc@109: ([(L'.DExport (E.newNamed (), sgn, str'), loc)], (env, denv, gs)) adamc@100: end adamc@100: adamc@83: and elabStr (env, denv) (str, loc) = adamc@31: case str of adamc@31: L.StrConst ds => adamc@31: let adamc@88: val (ds', (_, _, gs)) = ListUtil.foldlMapConcat elabDecl (env, denv, []) ds adamc@123: val sgis = ListUtil.mapConcat sgiOfDecl ds' adamc@63: adamc@63: val (sgis, _, _, _, _) = adamc@64: foldr (fn ((sgi, loc), (sgis, cons, vals, sgns, strs)) => adamc@63: case sgi of adamc@64: L'.SgiConAbs (x, n, k) => adamc@64: let adamc@64: val (cons, x) = adamc@64: if SS.member (cons, x) then adamc@64: (cons, "?" ^ x) adamc@64: else adamc@64: (SS.add (cons, x), x) adamc@64: in adamc@64: ((L'.SgiConAbs (x, n, k), loc) :: sgis, cons, vals, sgns, strs) adamc@64: end adamc@64: | L'.SgiCon (x, n, k, c) => adamc@64: let adamc@64: val (cons, x) = adamc@64: if SS.member (cons, x) then adamc@64: (cons, "?" ^ x) adamc@64: else adamc@64: (SS.add (cons, x), x) adamc@64: in adamc@64: ((L'.SgiCon (x, n, k, c), loc) :: sgis, cons, vals, sgns, strs) adamc@64: end adamc@64: | L'.SgiVal (x, n, c) => adamc@64: let adamc@64: val (vals, x) = adamc@64: if SS.member (vals, x) then adamc@64: (vals, "?" ^ x) adamc@64: else adamc@64: (SS.add (vals, x), x) adamc@64: in adamc@64: ((L'.SgiVal (x, n, c), loc) :: sgis, cons, vals, sgns, strs) adamc@64: end adamc@64: | L'.SgiSgn (x, n, sgn) => adamc@64: let adamc@64: val (sgns, x) = adamc@64: if SS.member (sgns, x) then adamc@64: (sgns, "?" ^ x) adamc@64: else adamc@64: (SS.add (sgns, x), x) adamc@64: in adamc@64: ((L'.SgiSgn (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs) adamc@64: end adamc@64: adamc@64: | L'.SgiStr (x, n, sgn) => adamc@64: let adamc@64: val (strs, x) = adamc@64: if SS.member (strs, x) then adamc@64: (strs, "?" ^ x) adamc@64: else adamc@64: (SS.add (strs, x), x) adamc@64: in adamc@64: ((L'.SgiStr (x, n, sgn), loc) :: sgis, cons, vals, sgns, strs) adamc@88: end adamc@88: | L'.SgiConstraint _ => ((sgi, loc) :: sgis, cons, vals, sgns, strs)) adamc@64: adamc@63: ([], SS.empty, SS.empty, SS.empty, SS.empty) sgis adamc@31: in adamc@83: ((L'.StrConst ds', loc), (L'.SgnConst sgis, loc), gs) adamc@31: end adamc@31: | L.StrVar x => adamc@31: (case E.lookupStr env x of adamc@31: NONE => adamc@31: (strError env (UnboundStr (loc, x)); adamc@83: (strerror, sgnerror, [])) adamc@83: | SOME (n, sgn) => ((L'.StrVar n, loc), sgn, [])) adamc@34: | L.StrProj (str, x) => adamc@34: let adamc@83: val (str', sgn, gs) = elabStr (env, denv) str adamc@34: in adamc@34: case E.projectStr env {str = str', sgn = sgn, field = x} of adamc@34: NONE => (strError env (UnboundStr (loc, x)); adamc@83: (strerror, sgnerror, [])) adamc@83: | SOME sgn => ((L'.StrProj (str', x), loc), sgn, gs) adamc@34: end adamc@41: | L.StrFun (m, dom, ranO, str) => adamc@41: let adamc@83: val (dom', gs1) = elabSgn (env, denv) dom adamc@41: val (env', n) = E.pushStrNamed env m dom' adamc@83: val (str', actual, gs2) = elabStr (env', denv) str adamc@41: adamc@83: val (formal, gs3) = adamc@41: case ranO of adamc@83: NONE => (actual, []) adamc@41: | SOME ran => adamc@41: let adamc@83: val (ran', gs) = elabSgn (env', denv) ran adamc@41: in adamc@86: subSgn (env', denv) actual ran'; adamc@83: (ran', gs) adamc@41: end adamc@41: in adamc@41: ((L'.StrFun (m, n, dom', formal, str'), loc), adamc@83: (L'.SgnFun (m, n, dom', formal), loc), adamc@83: gs1 @ gs2 @ gs3) adamc@41: end adamc@44: | L.StrApp (str1, str2) => adamc@44: let adamc@83: val (str1', sgn1, gs1) = elabStr (env, denv) str1 adamc@83: val (str2', sgn2, gs2) = elabStr (env, denv) str2 adamc@44: in adamc@44: case #1 (hnormSgn env sgn1) of adamc@83: L'.SgnError => (strerror, sgnerror, []) adamc@44: | L'.SgnFun (m, n, dom, ran) => adamc@86: (subSgn (env, denv) sgn2 dom; adamc@44: case #1 (hnormSgn env ran) of adamc@83: L'.SgnError => (strerror, sgnerror, []) adamc@44: | L'.SgnConst sgis => adamc@44: ((L'.StrApp (str1', str2'), loc), adamc@83: (L'.SgnConst ((L'.SgiStr (m, n, selfifyAt env {str = str2', sgn = sgn2}), loc) :: sgis), loc), adamc@83: gs1 @ gs2) adamc@44: | _ => raise Fail "Unable to hnormSgn in functor application") adamc@44: | _ => (strError env (NotFunctor sgn1); adamc@83: (strerror, sgnerror, [])) adamc@44: end adamc@31: adamc@56: fun elabFile basis env file = adamc@56: let adamc@83: val (sgn, gs) = elabSgn (env, D.empty) (L.SgnConst basis, ErrorMsg.dummySpan) adamc@83: val () = case gs of adamc@83: [] => () adamc@83: | _ => raise Fail "Unresolved disjointness constraints in Basis" adamc@83: adamc@56: val (env', basis_n) = E.pushStrNamed env "Basis" sgn adamc@56: adamc@88: val (ds, (env', _)) = dopen (env', D.empty) {str = basis_n, strs = [], sgn = sgn} adamc@56: adamc@56: fun discoverC r x = adamc@56: case E.lookupC env' x of adamc@56: E.NotBound => raise Fail ("Constructor " ^ x ^ " unbound in Basis") adamc@56: | E.Rel _ => raise Fail ("Constructor " ^ x ^ " bound relatively in Basis") adamc@56: | E.Named (n, (_, loc)) => r := (L'.CNamed n, loc) adamc@56: adamc@56: val () = discoverC int "int" adamc@56: val () = discoverC float "float" adamc@56: val () = discoverC string "string" adamc@56: adamc@83: fun elabDecl' (d, (env, gs)) = adamc@76: let adamc@76: val () = resetKunif () adamc@76: val () = resetCunif () adamc@88: val (ds, (env, _, gs)) = elabDecl (d, (env, D.empty, gs)) adamc@76: in adamc@76: if ErrorMsg.anyErrors () then adamc@76: () adamc@76: else ( adamc@76: if List.exists kunifsInDecl ds then adamc@91: declError env (KunifsRemain ds) adamc@76: else adamc@76: (); adamc@76: adamc@76: case ListUtil.search cunifsInDecl ds of adamc@76: NONE => () adamc@76: | SOME loc => adamc@91: declError env (CunifsRemain ds) adamc@76: ); adamc@76: adamc@83: (ds, (env, gs)) adamc@76: end adamc@76: adamc@83: val (file, (_, gs)) = ListUtil.foldlMapConcat elabDecl' (env', []) file adamc@56: in adamc@84: if ErrorMsg.anyErrors () then adamc@84: () adamc@84: else adamc@90: app (fn (loc, env, denv, c1, c2) => adamc@84: case D.prove env denv (c1, c2, loc) of adamc@84: [] => () adamc@84: | _ => adamc@86: (ErrorMsg.errorAt loc "Couldn't prove field name disjointness"; adamc@84: eprefaces' [("Con 1", p_con env c1), adamc@90: ("Con 2", p_con env c2), adamc@90: ("Hnormed 1", p_con env (ElabOps.hnormCon env c1)), adamc@90: ("Hnormed 2", p_con env (ElabOps.hnormCon env c2))])) gs; adamc@83: adamc@56: (L'.DFfiStr ("Basis", basis_n, sgn), ErrorMsg.dummySpan) :: ds @ file adamc@56: end adamc@2: adamc@2: end