annotate src/elaborate.sml @ 5:258261a53842

Elaborating files
author Adam Chlipala <adamc@hcoop.net>
date Sat, 26 Jan 2008 16:02:47 -0500
parents 5c3cc348e9e6
children 38bf996e1c2e
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@4 30 structure L = Source
adamc@2 31 structure L' = Elab
adamc@2 32 structure E = ElabEnv
adamc@2 33 structure U = ElabUtil
adamc@2 34
adamc@3 35 open Print
adamc@3 36 open ElabPrint
adamc@3 37
adamc@2 38 fun elabKind (k, loc) =
adamc@2 39 case k of
adamc@2 40 L.KType => (L'.KType, loc)
adamc@2 41 | L.KArrow (k1, k2) => (L'.KArrow (elabKind k1, elabKind k2), loc)
adamc@2 42 | L.KName => (L'.KName, loc)
adamc@2 43 | L.KRecord k => (L'.KRecord (elabKind k), loc)
adamc@2 44
adamc@2 45 fun elabExplicitness e =
adamc@2 46 case e of
adamc@2 47 L.Explicit => L'.Explicit
adamc@2 48 | L.Implicit => L'.Implicit
adamc@2 49
adamc@2 50 fun occursKind r =
adamc@2 51 U.Kind.exists (fn L'.KUnif (_, r') => r = r'
adamc@2 52 | _ => false)
adamc@2 53
adamc@3 54 datatype kunify_error =
adamc@2 55 KOccursCheckFailed of L'.kind * L'.kind
adamc@2 56 | KIncompatible of L'.kind * L'.kind
adamc@2 57
adamc@3 58 exception KUnify' of kunify_error
adamc@3 59
adamc@3 60 fun kunifyError err =
adamc@2 61 case err of
adamc@2 62 KOccursCheckFailed (k1, k2) =>
adamc@3 63 eprefaces "Kind occurs check failed"
adamc@3 64 [("Kind 1", p_kind k1),
adamc@3 65 ("Kind 2", p_kind k2)]
adamc@2 66 | KIncompatible (k1, k2) =>
adamc@3 67 eprefaces "Incompatible kinds"
adamc@3 68 [("Kind 1", p_kind k1),
adamc@3 69 ("Kind 2", p_kind k2)]
adamc@2 70
adamc@3 71 fun unifyKinds' (k1All as (k1, _)) (k2All as (k2, _)) =
adamc@2 72 let
adamc@3 73 fun err f = raise KUnify' (f (k1All, k2All))
adamc@2 74 in
adamc@2 75 case (k1, k2) of
adamc@2 76 (L'.KType, L'.KType) => ()
adamc@2 77 | (L'.KArrow (d1, r1), L'.KArrow (d2, r2)) =>
adamc@3 78 (unifyKinds' d1 d2;
adamc@3 79 unifyKinds' r1 r2)
adamc@2 80 | (L'.KName, L'.KName) => ()
adamc@3 81 | (L'.KRecord k1, L'.KRecord k2) => unifyKinds' k1 k2
adamc@2 82
adamc@2 83 | (L'.KError, _) => ()
adamc@2 84 | (_, L'.KError) => ()
adamc@2 85
adamc@3 86 | (L'.KUnif (_, ref (SOME k1All)), _) => unifyKinds' k1All k2All
adamc@3 87 | (_, L'.KUnif (_, ref (SOME k2All))) => unifyKinds' k1All k2All
adamc@2 88
adamc@2 89 | (L'.KUnif (_, r1), L'.KUnif (_, r2)) =>
adamc@2 90 if r1 = r2 then
adamc@2 91 ()
adamc@2 92 else
adamc@2 93 r1 := SOME k2All
adamc@2 94
adamc@2 95 | (L'.KUnif (_, r), _) =>
adamc@2 96 if occursKind r k2All then
adamc@2 97 err KOccursCheckFailed
adamc@2 98 else
adamc@2 99 r := SOME k2All
adamc@2 100 | (_, L'.KUnif (_, r)) =>
adamc@2 101 if occursKind r k1All then
adamc@2 102 err KOccursCheckFailed
adamc@2 103 else
adamc@2 104 r := SOME k1All
adamc@2 105
adamc@2 106 | _ => err KIncompatible
adamc@2 107 end
adamc@2 108
adamc@3 109 exception KUnify of L'.kind * L'.kind * kunify_error
adamc@3 110
adamc@3 111 fun unifyKinds k1 k2 =
adamc@3 112 unifyKinds' k1 k2
adamc@3 113 handle KUnify' err => raise KUnify (k1, k2, err)
adamc@3 114
adamc@3 115 datatype con_error =
adamc@3 116 UnboundCon of ErrorMsg.span * string
adamc@3 117 | WrongKind of L'.con * L'.kind * L'.kind * kunify_error
adamc@3 118
adamc@5 119 fun conError env err =
adamc@3 120 case err of
adamc@3 121 UnboundCon (loc, s) =>
adamc@3 122 ErrorMsg.errorAt loc ("Unbound constructor variable " ^ s)
adamc@3 123 | WrongKind (c, k1, k2, kerr) =>
adamc@3 124 (ErrorMsg.errorAt (#2 c) "Wrong kind";
adamc@5 125 eprefaces' [("Constructor", p_con env c),
adamc@5 126 ("Have kind", p_kind k1),
adamc@5 127 ("Need kind", p_kind k2)];
adamc@3 128 kunifyError kerr)
adamc@3 129
adamc@5 130 fun checkKind env c k1 k2 =
adamc@3 131 unifyKinds k1 k2
adamc@3 132 handle KUnify (k1, k2, err) =>
adamc@5 133 conError env (WrongKind (c, k1, k2, err))
adamc@3 134
adamc@3 135 val dummy = ErrorMsg.dummySpan
adamc@3 136
adamc@3 137 val ktype = (L'.KType, dummy)
adamc@3 138 val kname = (L'.KName, dummy)
adamc@3 139
adamc@3 140 val cerror = (L'.CError, dummy)
adamc@3 141 val kerror = (L'.KError, dummy)
adamc@3 142
adamc@3 143 local
adamc@3 144 val count = ref 0
adamc@3 145 in
adamc@3 146
adamc@3 147 fun resetKunif () = count := 0
adamc@3 148
adamc@3 149 fun kunif () =
adamc@3 150 let
adamc@3 151 val n = !count
adamc@3 152 val s = if n <= 26 then
adamc@3 153 str (chr (ord #"A" + n))
adamc@3 154 else
adamc@3 155 "U" ^ Int.toString (n - 26)
adamc@3 156 in
adamc@3 157 count := n + 1;
adamc@3 158 (L'.KUnif (s, ref NONE), dummy)
adamc@3 159 end
adamc@3 160
adamc@3 161 end
adamc@3 162
adamc@3 163 fun elabCon env (c, loc) =
adamc@3 164 case c of
adamc@3 165 L.CAnnot (c, k) =>
adamc@3 166 let
adamc@3 167 val k' = elabKind k
adamc@3 168 val (c', ck) = elabCon env c
adamc@3 169 in
adamc@5 170 checkKind env c' ck k';
adamc@3 171 (c', k')
adamc@3 172 end
adamc@3 173
adamc@3 174 | L.TFun (t1, t2) =>
adamc@3 175 let
adamc@3 176 val (t1', k1) = elabCon env t1
adamc@3 177 val (t2', k2) = elabCon env t2
adamc@3 178 in
adamc@5 179 checkKind env t1' k1 ktype;
adamc@5 180 checkKind env t2' k2 ktype;
adamc@3 181 ((L'.TFun (t1', t2'), loc), ktype)
adamc@3 182 end
adamc@3 183 | L.TCFun (e, x, k, t) =>
adamc@3 184 let
adamc@3 185 val e' = elabExplicitness e
adamc@3 186 val k' = elabKind k
adamc@3 187 val env' = E.pushCRel env x k'
adamc@3 188 val (t', tk) = elabCon env' t
adamc@3 189 in
adamc@5 190 checkKind env t' tk ktype;
adamc@3 191 ((L'.TCFun (e', x, k', t'), loc), ktype)
adamc@3 192 end
adamc@3 193 | L.TRecord c =>
adamc@3 194 let
adamc@3 195 val (c', ck) = elabCon env c
adamc@3 196 val k = (L'.KRecord ktype, loc)
adamc@3 197 in
adamc@5 198 checkKind env c' ck k;
adamc@3 199 ((L'.TRecord c', loc), ktype)
adamc@3 200 end
adamc@3 201
adamc@3 202 | L.CVar s =>
adamc@3 203 (case E.lookupC env s of
adamc@3 204 E.CNotBound =>
adamc@5 205 (conError env (UnboundCon (loc, s));
adamc@3 206 (cerror, kerror))
adamc@3 207 | E.CRel (n, k) =>
adamc@3 208 ((L'.CRel n, loc), k)
adamc@3 209 | E.CNamed (n, k) =>
adamc@3 210 ((L'.CNamed n, loc), k))
adamc@3 211 | L.CApp (c1, c2) =>
adamc@3 212 let
adamc@3 213 val (c1', k1) = elabCon env c1
adamc@3 214 val (c2', k2) = elabCon env c2
adamc@3 215 val dom = kunif ()
adamc@3 216 val ran = kunif ()
adamc@3 217 in
adamc@5 218 checkKind env c1' k1 (L'.KArrow (dom, ran), loc);
adamc@5 219 checkKind env c2' k2 dom;
adamc@3 220 ((L'.CApp (c1', c2'), loc), ran)
adamc@3 221 end
adamc@3 222 | L.CAbs (e, x, k, t) =>
adamc@3 223 let
adamc@3 224 val e' = elabExplicitness e
adamc@3 225 val k' = elabKind k
adamc@3 226 val env' = E.pushCRel env x k'
adamc@3 227 val (t', tk) = elabCon env' t
adamc@3 228 in
adamc@3 229 ((L'.CAbs (e', x, k', t'), loc),
adamc@3 230 (L'.KArrow (k', tk), loc))
adamc@3 231 end
adamc@3 232
adamc@3 233 | L.CName s =>
adamc@3 234 ((L'.CName s, loc), kname)
adamc@3 235
adamc@3 236 | L.CRecord xcs =>
adamc@3 237 let
adamc@3 238 val k = kunif ()
adamc@3 239
adamc@3 240 val xcs' = map (fn (x, c) =>
adamc@3 241 let
adamc@3 242 val (x', xk) = elabCon env x
adamc@3 243 val (c', ck) = elabCon env c
adamc@3 244 in
adamc@5 245 checkKind env x' xk kname;
adamc@5 246 checkKind env c' ck k;
adamc@3 247 (x', c')
adamc@3 248 end) xcs
adamc@3 249 in
adamc@5 250 ((L'.CRecord (k, xcs'), loc), (L'.KRecord k, loc))
adamc@3 251 end
adamc@3 252 | L.CConcat (c1, c2) =>
adamc@3 253 let
adamc@3 254 val (c1', k1) = elabCon env c1
adamc@3 255 val (c2', k2) = elabCon env c2
adamc@3 256 val ku = kunif ()
adamc@3 257 val k = (L'.KRecord ku, loc)
adamc@3 258 in
adamc@5 259 checkKind env c1' k1 k;
adamc@5 260 checkKind env c2' k2 k;
adamc@3 261 ((L'.CConcat (c1', c2'), loc), k)
adamc@3 262 end
adamc@3 263
adamc@3 264 fun elabDecl env (d, loc) =
adamc@5 265 (resetKunif ();
adamc@5 266 case d of
adamc@5 267 L.DCon (x, ko, c) =>
adamc@5 268 let
adamc@5 269 val k' = case ko of
adamc@5 270 NONE => kunif ()
adamc@5 271 | SOME k => elabKind k
adamc@3 272
adamc@5 273 val (c', ck) = elabCon env c
adamc@5 274 val (env', n) = E.pushCNamed env x k'
adamc@5 275 in
adamc@5 276 checkKind env c' ck k';
adamc@5 277 (env',
adamc@5 278 (L'.DCon (x, n, k', c'), loc))
adamc@5 279 end)
adamc@3 280
adamc@5 281 fun elabFile env ds =
adamc@5 282 ListUtil.mapfoldl (fn (d, env) => elabDecl env d) env ds
adamc@2 283
adamc@2 284 end