annotate src/elaborate.sml @ 4:5c3cc348e9e6

Rename Laconic* modules to Source*
author Adam Chlipala <adamc@hcoop.net>
date Sat, 26 Jan 2008 15:29:09 -0500
parents daa4f1d7a663
children 258261a53842
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@3 119 fun conError 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@3 125 eprefaces' [("Have", p_kind k1),
adamc@3 126 ("Need", p_kind k2)];
adamc@3 127 kunifyError kerr)
adamc@3 128
adamc@3 129 fun checkKind c k1 k2 =
adamc@3 130 unifyKinds k1 k2
adamc@3 131 handle KUnify (k1, k2, err) =>
adamc@3 132 conError (WrongKind (c, k1, k2, err))
adamc@3 133
adamc@3 134 val dummy = ErrorMsg.dummySpan
adamc@3 135
adamc@3 136 val ktype = (L'.KType, dummy)
adamc@3 137 val kname = (L'.KName, dummy)
adamc@3 138
adamc@3 139 val cerror = (L'.CError, dummy)
adamc@3 140 val kerror = (L'.KError, dummy)
adamc@3 141
adamc@3 142 local
adamc@3 143 val count = ref 0
adamc@3 144 in
adamc@3 145
adamc@3 146 fun resetKunif () = count := 0
adamc@3 147
adamc@3 148 fun kunif () =
adamc@3 149 let
adamc@3 150 val n = !count
adamc@3 151 val s = if n <= 26 then
adamc@3 152 str (chr (ord #"A" + n))
adamc@3 153 else
adamc@3 154 "U" ^ Int.toString (n - 26)
adamc@3 155 in
adamc@3 156 count := n + 1;
adamc@3 157 (L'.KUnif (s, ref NONE), dummy)
adamc@3 158 end
adamc@3 159
adamc@3 160 end
adamc@3 161
adamc@3 162 fun elabCon env (c, loc) =
adamc@3 163 case c of
adamc@3 164 L.CAnnot (c, k) =>
adamc@3 165 let
adamc@3 166 val k' = elabKind k
adamc@3 167 val (c', ck) = elabCon env c
adamc@3 168 in
adamc@3 169 checkKind c' ck k';
adamc@3 170 (c', k')
adamc@3 171 end
adamc@3 172
adamc@3 173 | L.TFun (t1, t2) =>
adamc@3 174 let
adamc@3 175 val (t1', k1) = elabCon env t1
adamc@3 176 val (t2', k2) = elabCon env t2
adamc@3 177 in
adamc@3 178 checkKind t1' k1 ktype;
adamc@3 179 checkKind t2' k2 ktype;
adamc@3 180 ((L'.TFun (t1', t2'), loc), ktype)
adamc@3 181 end
adamc@3 182 | L.TCFun (e, x, k, t) =>
adamc@3 183 let
adamc@3 184 val e' = elabExplicitness e
adamc@3 185 val k' = elabKind k
adamc@3 186 val env' = E.pushCRel env x k'
adamc@3 187 val (t', tk) = elabCon env' t
adamc@3 188 in
adamc@3 189 checkKind t' tk ktype;
adamc@3 190 ((L'.TCFun (e', x, k', t'), loc), ktype)
adamc@3 191 end
adamc@3 192 | L.TRecord c =>
adamc@3 193 let
adamc@3 194 val (c', ck) = elabCon env c
adamc@3 195 val k = (L'.KRecord ktype, loc)
adamc@3 196 in
adamc@3 197 checkKind c' ck k;
adamc@3 198 ((L'.TRecord c', loc), ktype)
adamc@3 199 end
adamc@3 200
adamc@3 201 | L.CVar s =>
adamc@3 202 (case E.lookupC env s of
adamc@3 203 E.CNotBound =>
adamc@3 204 (conError (UnboundCon (loc, s));
adamc@3 205 (cerror, kerror))
adamc@3 206 | E.CRel (n, k) =>
adamc@3 207 ((L'.CRel n, loc), k)
adamc@3 208 | E.CNamed (n, k) =>
adamc@3 209 ((L'.CNamed n, loc), k))
adamc@3 210 | L.CApp (c1, c2) =>
adamc@3 211 let
adamc@3 212 val (c1', k1) = elabCon env c1
adamc@3 213 val (c2', k2) = elabCon env c2
adamc@3 214 val dom = kunif ()
adamc@3 215 val ran = kunif ()
adamc@3 216 in
adamc@3 217 checkKind c1' k1 (L'.KArrow (dom, ran), loc);
adamc@3 218 checkKind c2' k2 dom;
adamc@3 219 ((L'.CApp (c1', c2'), loc), ran)
adamc@3 220 end
adamc@3 221 | L.CAbs (e, x, k, t) =>
adamc@3 222 let
adamc@3 223 val e' = elabExplicitness e
adamc@3 224 val k' = elabKind k
adamc@3 225 val env' = E.pushCRel env x k'
adamc@3 226 val (t', tk) = elabCon env' t
adamc@3 227 in
adamc@3 228 ((L'.CAbs (e', x, k', t'), loc),
adamc@3 229 (L'.KArrow (k', tk), loc))
adamc@3 230 end
adamc@3 231
adamc@3 232 | L.CName s =>
adamc@3 233 ((L'.CName s, loc), kname)
adamc@3 234
adamc@3 235 | L.CRecord xcs =>
adamc@3 236 let
adamc@3 237 val k = kunif ()
adamc@3 238
adamc@3 239 val xcs' = map (fn (x, c) =>
adamc@3 240 let
adamc@3 241 val (x', xk) = elabCon env x
adamc@3 242 val (c', ck) = elabCon env c
adamc@3 243 in
adamc@3 244 checkKind x' xk kname;
adamc@3 245 checkKind c' ck k;
adamc@3 246 (x', c')
adamc@3 247 end) xcs
adamc@3 248 in
adamc@3 249 ((L'.CRecord (k, xcs'), loc), k)
adamc@3 250 end
adamc@3 251 | L.CConcat (c1, c2) =>
adamc@3 252 let
adamc@3 253 val (c1', k1) = elabCon env c1
adamc@3 254 val (c2', k2) = elabCon env c2
adamc@3 255 val ku = kunif ()
adamc@3 256 val k = (L'.KRecord ku, loc)
adamc@3 257 in
adamc@3 258 checkKind c1' k1 k;
adamc@3 259 checkKind c2' k2 k;
adamc@3 260 ((L'.CConcat (c1', c2'), loc), k)
adamc@3 261 end
adamc@3 262
adamc@3 263 fun elabDecl env (d, loc) =
adamc@3 264 case d of
adamc@3 265 L.DCon (x, ko, c) =>
adamc@3 266 let
adamc@3 267 val k' = case ko of
adamc@3 268 NONE => kunif ()
adamc@3 269 | SOME k => elabKind k
adamc@3 270
adamc@3 271 val (c', ck) = elabCon env c
adamc@3 272 in
adamc@3 273 checkKind c' ck k';
adamc@3 274 (E.pushCNamed env x k',
adamc@3 275 L'.DCon (x, k', c'))
adamc@3 276 end
adamc@3 277
adamc@2 278 fun elabFile _ = raise Fail ""
adamc@2 279
adamc@2 280 end