annotate src/corify.sml @ 177:5d030ee143e2

Case through corify
author Adam Chlipala <adamc@hcoop.net>
date Sat, 02 Aug 2008 11:15:32 -0400
parents 33d4a8eea484
children d11754ffe252
rev   line source
adamc@16 1 (* Copyright (c) 2008, Adam Chlipala
adamc@16 2 * All rights reserved.
adamc@16 3 *
adamc@16 4 * Redistribution and use in source and binary forms, with or without
adamc@16 5 * modification, are permitted provided that the following conditions are met:
adamc@16 6 *
adamc@16 7 * - Redistributions of source code must retain the above copyright notice,
adamc@16 8 * this list of conditions and the following disclaimer.
adamc@16 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@16 10 * this list of conditions and the following disclaimer in the documentation
adamc@16 11 * and/or other materials provided with the distribution.
adamc@16 12 * - The names of contributors may not be used to endorse or promote products
adamc@16 13 * derived from this software without specific prior written permission.
adamc@16 14 *
adamc@16 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@16 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@16 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@16 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@16 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@16 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@16 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@16 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@16 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@16 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@16 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@16 26 *)
adamc@16 27
adamc@16 28 structure Corify :> CORIFY = struct
adamc@16 29
adamc@16 30 structure EM = ErrorMsg
adamc@39 31 structure L = Expl
adamc@16 32 structure L' = Core
adamc@16 33
adamc@39 34 structure IM = IntBinaryMap
adamc@39 35 structure SM = BinaryMapFn(struct
adamc@39 36 type ord_key = string
adamc@39 37 val compare = String.compare
adamc@39 38 end)
adamc@39 39
adamc@39 40 local
adamc@39 41 val count = ref 0
adamc@39 42 in
adamc@39 43
adamc@39 44 fun reset v = count := v
adamc@39 45
adamc@39 46 fun alloc () =
adamc@39 47 let
adamc@39 48 val r = !count
adamc@39 49 in
adamc@39 50 count := r + 1;
adamc@39 51 r
adamc@39 52 end
adamc@39 53
adamc@39 54 end
adamc@39 55
adamc@39 56 structure St : sig
adamc@39 57 type t
adamc@39 58
adamc@39 59 val empty : t
adamc@39 60
adamc@146 61 val debug : t -> unit
adamc@146 62
adamc@39 63 val enter : t -> t
adamc@39 64 val leave : t -> {outer : t, inner : t}
adamc@49 65 val ffi : string -> L'.con SM.map -> t
adamc@39 66
adamc@73 67 datatype core_con =
adamc@73 68 CNormal of int
adamc@73 69 | CFfi of string
adamc@73 70 val bindCon : t -> string -> int -> t * int
adamc@73 71 val lookupConById : t -> int -> int option
adamc@73 72 val lookupConByName : t -> string -> core_con
adamc@48 73
adamc@177 74 val bindConstructor : t -> string -> int -> L'.patCon -> t
adamc@177 75 val lookupConstructorByName : t -> string -> L'.patCon
adamc@177 76 val lookupConstructorById : t -> int -> L'.patCon
adamc@177 77
adamc@73 78 datatype core_val =
adamc@73 79 ENormal of int
adamc@73 80 | EFfi of string * L'.con
adamc@73 81 val bindVal : t -> string -> int -> t * int
adamc@177 82 val bindConstructorVal : t -> string -> int -> t
adamc@73 83 val lookupValById : t -> int -> int option
adamc@73 84 val lookupValByName : t -> string -> core_val
adamc@39 85
adamc@39 86 val bindStr : t -> string -> int -> t -> t
adamc@39 87 val lookupStrById : t -> int -> t
adamc@39 88 val lookupStrByName : string * t -> t
adamc@46 89
adamc@146 90 val bindFunctor : t -> string -> int -> string -> int -> L.str -> t
adamc@146 91 val lookupFunctorById : t -> int -> string * int * L.str
adamc@146 92 val lookupFunctorByName : string * t -> string * int * L.str
adamc@39 93 end = struct
adamc@39 94
adamc@48 95 datatype flattening =
adamc@73 96 FNormal of {cons : int SM.map,
adamc@177 97 constructors : L'.patCon SM.map,
adamc@73 98 vals : int SM.map,
adamc@48 99 strs : flattening SM.map,
adamc@146 100 funs : (string * int * L.str) SM.map}
adamc@73 101 | FFfi of {mod : string,
adamc@73 102 vals : L'.con SM.map}
adamc@39 103
adamc@39 104 type t = {
adamc@73 105 cons : int IM.map,
adamc@177 106 constructors : L'.patCon IM.map,
adamc@73 107 vals : int IM.map,
adamc@39 108 strs : flattening IM.map,
adamc@146 109 funs : (string * int * L.str) IM.map,
adamc@39 110 current : flattening,
adamc@39 111 nested : flattening list
adamc@39 112 }
adamc@39 113
adamc@39 114 val empty = {
adamc@73 115 cons = IM.empty,
adamc@177 116 constructors = IM.empty,
adamc@73 117 vals = IM.empty,
adamc@39 118 strs = IM.empty,
adamc@46 119 funs = IM.empty,
adamc@177 120 current = FNormal { cons = SM.empty, constructors = SM.empty, vals = SM.empty, strs = SM.empty, funs = SM.empty },
adamc@39 121 nested = []
adamc@39 122 }
adamc@39 123
adamc@177 124 fun debug ({current = FNormal {cons, constructors, vals, strs, funs}, ...} : t) =
adamc@146 125 print ("cons: " ^ Int.toString (SM.numItems cons) ^ "; "
adamc@177 126 ^ "constructors: " ^ Int.toString (SM.numItems constructors) ^ "; "
adamc@146 127 ^ "vals: " ^ Int.toString (SM.numItems vals) ^ "; "
adamc@146 128 ^ "strs: " ^ Int.toString (SM.numItems strs) ^ "; "
adamc@146 129 ^ "funs: " ^ Int.toString (SM.numItems funs) ^ "\n")
adamc@146 130 | debug _ = print "Not normal!\n"
adamc@146 131
adamc@73 132 datatype core_con =
adamc@73 133 CNormal of int
adamc@73 134 | CFfi of string
adamc@48 135
adamc@73 136 datatype core_val =
adamc@73 137 ENormal of int
adamc@73 138 | EFfi of string * L'.con
adamc@73 139
adamc@177 140 fun bindCon {cons, constructors, vals, strs, funs, current, nested} s n =
adamc@39 141 let
adamc@39 142 val n' = alloc ()
adamc@39 143
adamc@39 144 val current =
adamc@48 145 case current of
adamc@48 146 FFfi _ => raise Fail "Binding inside FFfi"
adamc@177 147 | FNormal {cons, constructors, vals, strs, funs} =>
adamc@73 148 FNormal {cons = SM.insert (cons, s, n'),
adamc@177 149 constructors = constructors,
adamc@73 150 vals = vals,
adamc@48 151 strs = strs,
adamc@48 152 funs = funs}
adamc@39 153 in
adamc@73 154 ({cons = IM.insert (cons, n, n'),
adamc@177 155 constructors = constructors,
adamc@73 156 vals = vals,
adamc@39 157 strs = strs,
adamc@46 158 funs = funs,
adamc@39 159 current = current,
adamc@39 160 nested = nested},
adamc@39 161 n')
adamc@39 162 end
adamc@39 163
adamc@73 164 fun lookupConById ({cons, ...} : t) n = IM.find (cons, n)
adamc@39 165
adamc@73 166 fun lookupConByName ({current, ...} : t) x =
adamc@48 167 case current of
adamc@73 168 FFfi {mod = m, ...} => CFfi m
adamc@73 169 | FNormal {cons, ...} =>
adamc@73 170 case SM.find (cons, x) of
adamc@73 171 NONE => raise Fail "Corify.St.lookupConByName"
adamc@73 172 | SOME n => CNormal n
adamc@39 173
adamc@177 174 fun bindVal {cons, constructors, vals, strs, funs, current, nested} s n =
adamc@73 175 let
adamc@73 176 val n' = alloc ()
adamc@73 177
adamc@73 178 val current =
adamc@73 179 case current of
adamc@73 180 FFfi _ => raise Fail "Binding inside FFfi"
adamc@177 181 | FNormal {cons, constructors, vals, strs, funs} =>
adamc@73 182 FNormal {cons = cons,
adamc@177 183 constructors = constructors,
adamc@73 184 vals = SM.insert (vals, s, n'),
adamc@73 185 strs = strs,
adamc@73 186 funs = funs}
adamc@73 187 in
adamc@73 188 ({cons = cons,
adamc@177 189 constructors = constructors,
adamc@73 190 vals = IM.insert (vals, n, n'),
adamc@73 191 strs = strs,
adamc@73 192 funs = funs,
adamc@73 193 current = current,
adamc@73 194 nested = nested},
adamc@73 195 n')
adamc@73 196 end
adamc@73 197
adamc@177 198 fun bindConstructorVal {cons, constructors, vals, strs, funs, current, nested} s n =
adamc@163 199 let
adamc@163 200 val current =
adamc@163 201 case current of
adamc@163 202 FFfi _ => raise Fail "Binding inside FFfi"
adamc@177 203 | FNormal {cons, constructors, vals, strs, funs} =>
adamc@163 204 FNormal {cons = cons,
adamc@177 205 constructors = constructors,
adamc@163 206 vals = SM.insert (vals, s, n),
adamc@163 207 strs = strs,
adamc@163 208 funs = funs}
adamc@163 209 in
adamc@163 210 {cons = cons,
adamc@177 211 constructors = constructors,
adamc@163 212 vals = IM.insert (vals, n, n),
adamc@163 213 strs = strs,
adamc@163 214 funs = funs,
adamc@163 215 current = current,
adamc@163 216 nested = nested}
adamc@163 217 end
adamc@163 218
adamc@177 219
adamc@73 220 fun lookupValById ({vals, ...} : t) n = IM.find (vals, n)
adamc@73 221
adamc@73 222 fun lookupValByName ({current, ...} : t) x =
adamc@73 223 case current of
adamc@73 224 FFfi {mod = m, vals, ...} =>
adamc@73 225 (case SM.find (vals, x) of
adamc@73 226 NONE => raise Fail "Corify.St.lookupValByName: no type for FFI val"
adamc@73 227 | SOME t => EFfi (m, t))
adamc@73 228 | FNormal {vals, ...} =>
adamc@73 229 case SM.find (vals, x) of
adamc@73 230 NONE => raise Fail "Corify.St.lookupValByName"
adamc@73 231 | SOME n => ENormal n
adamc@73 232
adamc@177 233 fun bindConstructor {cons, constructors, vals, strs, funs, current, nested} s n n' =
adamc@177 234 let
adamc@177 235 val current =
adamc@177 236 case current of
adamc@177 237 FFfi _ => raise Fail "Binding inside FFfi"
adamc@177 238 | FNormal {cons, constructors, vals, strs, funs} =>
adamc@177 239 FNormal {cons = cons,
adamc@177 240 constructors = SM.insert (constructors, s, n'),
adamc@177 241 vals = vals,
adamc@177 242 strs = strs,
adamc@177 243 funs = funs}
adamc@177 244 in
adamc@177 245 {cons = cons,
adamc@177 246 constructors = IM.insert (constructors, n, n'),
adamc@177 247 vals = vals,
adamc@177 248 strs = strs,
adamc@177 249 funs = funs,
adamc@177 250 current = current,
adamc@177 251 nested = nested}
adamc@177 252 end
adamc@177 253
adamc@177 254 fun lookupConstructorById ({constructors, ...} : t) n =
adamc@177 255 case IM.find (constructors, n) of
adamc@177 256 NONE => raise Fail "Corify.St.lookupConstructorById"
adamc@177 257 | SOME x => x
adamc@177 258
adamc@177 259 fun lookupConstructorByName ({current, ...} : t) x =
adamc@177 260 case current of
adamc@177 261 FFfi {mod = m, ...} => L'.PConFfi (m, x)
adamc@177 262 | FNormal {constructors, ...} =>
adamc@177 263 case SM.find (constructors, x) of
adamc@177 264 NONE => raise Fail "Corify.St.lookupConstructorByName"
adamc@177 265 | SOME n => n
adamc@177 266
adamc@177 267 fun enter {cons, constructors, vals, strs, funs, current, nested} =
adamc@73 268 {cons = cons,
adamc@177 269 constructors = constructors,
adamc@73 270 vals = vals,
adamc@39 271 strs = strs,
adamc@46 272 funs = funs,
adamc@73 273 current = FNormal {cons = SM.empty,
adamc@177 274 constructors = SM.empty,
adamc@73 275 vals = SM.empty,
adamc@48 276 strs = SM.empty,
adamc@48 277 funs = SM.empty},
adamc@39 278 nested = current :: nested}
adamc@39 279
adamc@73 280 fun dummy f = {cons = IM.empty,
adamc@177 281 constructors = IM.empty,
adamc@73 282 vals = IM.empty,
adamc@39 283 strs = IM.empty,
adamc@46 284 funs = IM.empty,
adamc@39 285 current = f,
adamc@39 286 nested = []}
adamc@39 287
adamc@177 288 fun leave {cons, constructors, vals, strs, funs, current, nested = m1 :: rest} =
adamc@73 289 {outer = {cons = cons,
adamc@177 290 constructors = constructors,
adamc@73 291 vals = vals,
adamc@39 292 strs = strs,
adamc@46 293 funs = funs,
adamc@39 294 current = m1,
adamc@39 295 nested = rest},
adamc@39 296 inner = dummy current}
adamc@39 297 | leave _ = raise Fail "Corify.St.leave"
adamc@39 298
adamc@73 299 fun ffi m vals = dummy (FFfi {mod = m, vals = vals})
adamc@48 300
adamc@177 301 fun bindStr ({cons, constructors, vals, strs, funs,
adamc@177 302 current = FNormal {cons = mcons, constructors = mconstructors,
adamc@177 303 vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@46 304 x n ({current = f, ...} : t) =
adamc@73 305 {cons = cons,
adamc@177 306 constructors = constructors,
adamc@73 307 vals = vals,
adamc@39 308 strs = IM.insert (strs, n, f),
adamc@46 309 funs = funs,
adamc@73 310 current = FNormal {cons = mcons,
adamc@177 311 constructors = mconstructors,
adamc@73 312 vals = mvals,
adamc@73 313 strs = SM.insert (mstrs, x, f),
adamc@73 314 funs = mfuns},
adamc@39 315 nested = nested}
adamc@48 316 | bindStr _ _ _ _ = raise Fail "Corify.St.bindStr"
adamc@39 317
adamc@39 318 fun lookupStrById ({strs, ...} : t) n =
adamc@39 319 case IM.find (strs, n) of
adamc@46 320 NONE => raise Fail "Corify.St.lookupStrById"
adamc@39 321 | SOME f => dummy f
adamc@39 322
adamc@48 323 fun lookupStrByName (m, {current = FNormal {strs, ...}, ...} : t) =
adamc@48 324 (case SM.find (strs, m) of
adamc@48 325 NONE => raise Fail "Corify.St.lookupStrByName"
adamc@48 326 | SOME f => dummy f)
adamc@48 327 | lookupStrByName _ = raise Fail "Corify.St.lookupStrByName"
adamc@39 328
adamc@177 329 fun bindFunctor ({cons, constructors, vals, strs, funs,
adamc@177 330 current = FNormal {cons = mcons, constructors = mconstructors,
adamc@177 331 vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@146 332 x n xa na str =
adamc@73 333 {cons = cons,
adamc@177 334 constructors = constructors,
adamc@73 335 vals = vals,
adamc@46 336 strs = strs,
adamc@146 337 funs = IM.insert (funs, n, (xa, na, str)),
adamc@73 338 current = FNormal {cons = mcons,
adamc@177 339 constructors = mconstructors,
adamc@73 340 vals = mvals,
adamc@48 341 strs = mstrs,
adamc@146 342 funs = SM.insert (mfuns, x, (xa, na, str))},
adamc@46 343 nested = nested}
adamc@146 344 | bindFunctor _ _ _ _ _ _ = raise Fail "Corify.St.bindFunctor"
adamc@46 345
adamc@46 346 fun lookupFunctorById ({funs, ...} : t) n =
adamc@46 347 case IM.find (funs, n) of
adamc@46 348 NONE => raise Fail "Corify.St.lookupFunctorById"
adamc@46 349 | SOME v => v
adamc@46 350
adamc@48 351 fun lookupFunctorByName (m, {current = FNormal {funs, ...}, ...} : t) =
adamc@48 352 (case SM.find (funs, m) of
adamc@48 353 NONE => raise Fail "Corify.St.lookupFunctorByName"
adamc@48 354 | SOME v => v)
adamc@48 355 | lookupFunctorByName _ = raise Fail "Corify.St.lookupFunctorByName"
adamc@46 356
adamc@39 357 end
adamc@39 358
adamc@39 359
adamc@16 360 fun corifyKind (k, loc) =
adamc@16 361 case k of
adamc@16 362 L.KType => (L'.KType, loc)
adamc@16 363 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
adamc@16 364 | L.KName => (L'.KName, loc)
adamc@16 365 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
adamc@87 366 | L.KUnit => (L'.KUnit, loc)
adamc@16 367
adamc@39 368 fun corifyCon st (c, loc) =
adamc@16 369 case c of
adamc@39 370 L.TFun (t1, t2) => (L'.TFun (corifyCon st t1, corifyCon st t2), loc)
adamc@39 371 | L.TCFun (x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon st t), loc)
adamc@39 372 | L.TRecord c => (L'.TRecord (corifyCon st c), loc)
adamc@16 373
adamc@16 374 | L.CRel n => (L'.CRel n, loc)
adamc@39 375 | L.CNamed n =>
adamc@73 376 (case St.lookupConById st n of
adamc@39 377 NONE => (L'.CNamed n, loc)
adamc@39 378 | SOME n => (L'.CNamed n, loc))
adamc@39 379 | L.CModProj (m, ms, x) =>
adamc@39 380 let
adamc@39 381 val st = St.lookupStrById st m
adamc@39 382 val st = foldl St.lookupStrByName st ms
adamc@39 383 in
adamc@73 384 case St.lookupConByName st x of
adamc@73 385 St.CNormal n => (L'.CNamed n, loc)
adamc@73 386 | St.CFfi m => (L'.CFfi (m, x), loc)
adamc@39 387 end
adamc@34 388
adamc@39 389 | L.CApp (c1, c2) => (L'.CApp (corifyCon st c1, corifyCon st c2), loc)
adamc@39 390 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon st c), loc)
adamc@16 391
adamc@16 392 | L.CName s => (L'.CName s, loc)
adamc@16 393
adamc@39 394 | L.CRecord (k, xcs) =>
adamc@39 395 (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon st c1, corifyCon st c2)) xcs), loc)
adamc@39 396 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon st c1, corifyCon st c2), loc)
adamc@69 397 | L.CFold (k1, k2) => (L'.CFold (corifyKind k1, corifyKind k2), loc)
adamc@87 398 | L.CUnit => (L'.CUnit, loc)
adamc@16 399
adamc@177 400 fun corifyPatCon st pc =
adamc@177 401 case pc of
adamc@177 402 L.PConVar n => St.lookupConstructorById st n
adamc@177 403 | L.PConProj (m1, ms, x) =>
adamc@177 404 let
adamc@177 405 val st = St.lookupStrById st m1
adamc@177 406 val st = foldl St.lookupStrByName st ms
adamc@177 407 in
adamc@177 408 St.lookupConstructorByName st x
adamc@177 409 end
adamc@177 410
adamc@177 411 fun corifyPat st (p, loc) =
adamc@177 412 case p of
adamc@177 413 L.PWild => (L'.PWild, loc)
adamc@177 414 | L.PVar x => (L'.PVar x, loc)
adamc@177 415 | L.PPrim p => (L'.PPrim p, loc)
adamc@177 416 | L.PCon (pc, po) => (L'.PCon (corifyPatCon st pc, Option.map (corifyPat st) po), loc)
adamc@177 417 | L.PRecord xps => (L'.PRecord (map (fn (x, p) => (x, corifyPat st p)) xps), loc)
adamc@177 418
adamc@39 419 fun corifyExp st (e, loc) =
adamc@16 420 case e of
adamc@16 421 L.EPrim p => (L'.EPrim p, loc)
adamc@16 422 | L.ERel n => (L'.ERel n, loc)
adamc@39 423 | L.ENamed n =>
adamc@73 424 (case St.lookupValById st n of
adamc@39 425 NONE => (L'.ENamed n, loc)
adamc@39 426 | SOME n => (L'.ENamed n, loc))
adamc@39 427 | L.EModProj (m, ms, x) =>
adamc@39 428 let
adamc@39 429 val st = St.lookupStrById st m
adamc@39 430 val st = foldl St.lookupStrByName st ms
adamc@39 431 in
adamc@73 432 case St.lookupValByName st x of
adamc@73 433 St.ENormal n => (L'.ENamed n, loc)
adamc@73 434 | St.EFfi (m, t) =>
adamc@49 435 case t of
adamc@50 436 (L'.TFun (dom as (L'.TRecord (L'.CRecord (_, []), _), _), ran), _) =>
adamc@50 437 (L'.EAbs ("arg", dom, ran, (L'.EFfiApp (m, x, []), loc)), loc)
adamc@50 438 | t as (L'.TFun _, _) =>
adamc@49 439 let
adamc@49 440 fun getArgs (all as (t, _), args) =
adamc@49 441 case t of
adamc@49 442 L'.TFun (dom, ran) => getArgs (ran, dom :: args)
adamc@49 443 | _ => (all, rev args)
adamc@49 444
adamc@49 445 val (result, args) = getArgs (t, [])
adamc@49 446
adamc@50 447 val (actuals, _) = foldr (fn (_, (actuals, n)) =>
adamc@50 448 ((L'.ERel n, loc) :: actuals,
adamc@50 449 n + 1)) ([], 0) args
adamc@50 450 val app = (L'.EFfiApp (m, x, actuals), loc)
adamc@49 451 val (abs, _, _) = foldr (fn (t, (abs, ran, n)) =>
adamc@49 452 ((L'.EAbs ("arg" ^ Int.toString n,
adamc@49 453 t,
adamc@49 454 ran,
adamc@49 455 abs), loc),
adamc@49 456 (L'.TFun (t, ran), loc),
adamc@49 457 n - 1)) (app, result, length args - 1) args
adamc@49 458 in
adamc@49 459 abs
adamc@49 460 end
adamc@49 461 | _ => (L'.EFfi (m, x), loc)
adamc@39 462 end
adamc@39 463 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
adamc@39 464 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
adamc@39 465 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
adamc@39 466 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
adamc@16 467
adamc@110 468 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) =>
adamc@110 469 (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
adamc@39 470 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
adamc@39 471 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@149 472 | L.ECut (e1, c, {field, rest}) => (L'.ECut (corifyExp st e1, corifyCon st c,
adamc@149 473 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@73 474 | L.EFold k => (L'.EFold (corifyKind k), loc)
adamc@177 475
adamc@177 476 | L.ECase (e, pes, t) => (L'.ECase (corifyExp st e,
adamc@177 477 map (fn (p, e) => (corifyPat st p, corifyExp st e)) pes,
adamc@177 478 corifyCon st t),
adamc@177 479 loc)
adamc@177 480
adamc@109 481 | L.EWrite e => (L'.EWrite (corifyExp st e), loc)
adamc@16 482
adamc@39 483 fun corifyDecl ((d, loc : EM.span), st) =
adamc@39 484 case d of
adamc@39 485 L.DCon (x, n, k, c) =>
adamc@39 486 let
adamc@73 487 val (st, n) = St.bindCon st x n
adamc@39 488 in
adamc@39 489 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
adamc@39 490 end
adamc@163 491 | L.DDatatype (x, n, xncs) =>
adamc@163 492 let
adamc@163 493 val (st, n) = St.bindCon st x n
adamc@163 494 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@163 495 let
adamc@177 496 val st = St.bindConstructor st x n (L'.PConVar n)
adamc@177 497 val st = St.bindConstructorVal st x n
adamc@163 498 val co = Option.map (corifyCon st) co
adamc@163 499 in
adamc@163 500 ((x, n, co), st)
adamc@163 501 end) st xncs
adamc@177 502
adamc@177 503 val t = (L'.CNamed n, loc)
adamc@177 504 val dcons = map (fn (x, n, to) =>
adamc@177 505 let
adamc@177 506 val (e, t) =
adamc@177 507 case to of
adamc@177 508 NONE => ((L'.ECon (n, NONE), loc), t)
adamc@177 509 | SOME t' => ((L'.EAbs ("x", t', t,
adamc@177 510 (L'.ECon (n, SOME (L'.ERel 0, loc)), loc)),
adamc@177 511 loc),
adamc@177 512 (L'.TFun (t', t), loc))
adamc@177 513 in
adamc@177 514 (L'.DVal (x, n, t, e, ""), loc)
adamc@177 515 end) xncs
adamc@163 516 in
adamc@177 517 ((L'.DDatatype (x, n, xncs), loc) :: dcons, st)
adamc@163 518 end
adamc@163 519 | L.DDatatypeImp (x, n, m1, ms, s, xncs) =>
adamc@163 520 let
adamc@163 521 val (st, n) = St.bindCon st x n
adamc@163 522 val c = corifyCon st (L.CModProj (m1, ms, s), loc)
adamc@163 523
adamc@177 524 val m = foldl (fn (x, m) => (L.StrProj (m, x), loc)) (L.StrVar m1, loc) ms
adamc@177 525 val (_, {inner, ...}) = corifyStr (m, st)
adamc@177 526
adamc@163 527 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@163 528 let
adamc@177 529 val n' = St.lookupConstructorByName inner x
adamc@177 530 val st = St.bindConstructor st x n n'
adamc@163 531 val (st, n) = St.bindVal st x n
adamc@163 532 val co = Option.map (corifyCon st) co
adamc@163 533 in
adamc@163 534 ((x, n, co), st)
adamc@163 535 end) st xncs
adamc@177 536
adamc@163 537 val cds = map (fn (x, n, co) =>
adamc@163 538 let
adamc@163 539 val t = case co of
adamc@163 540 NONE => c
adamc@163 541 | SOME t' => (L'.TFun (t', c), loc)
adamc@163 542 val e = corifyExp st (L.EModProj (m1, ms, x), loc)
adamc@163 543 in
adamc@163 544 (L'.DVal (x, n, t, e, x), loc)
adamc@163 545 end) xncs
adamc@163 546 in
adamc@163 547 ((L'.DCon (x, n, (L'.KType, loc), c), loc) :: cds, st)
adamc@163 548 end
adamc@39 549 | L.DVal (x, n, t, e) =>
adamc@39 550 let
adamc@73 551 val (st, n) = St.bindVal st x n
adamc@111 552 val s =
adamc@111 553 if String.isPrefix "wrap_" x then
adamc@111 554 String.extract (x, 5, NONE)
adamc@111 555 else
adamc@111 556 x
adamc@39 557 in
adamc@111 558 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e, s), loc)], st)
adamc@39 559 end
adamc@125 560 | L.DValRec vis =>
adamc@125 561 let
adamc@125 562 val (vis, st) = ListUtil.foldlMap
adamc@130 563 (fn ((x, n, t, e), st) =>
adamc@130 564 let
adamc@130 565 val (st, n) = St.bindVal st x n
adamc@130 566 in
adamc@130 567 ((x, n, t, e), st)
adamc@130 568 end)
adamc@130 569 st vis
adamc@130 570
adamc@130 571 val vis = map
adamc@130 572 (fn (x, n, t, e) =>
adamc@130 573 let
adamc@130 574 val s =
adamc@130 575 if String.isPrefix "wrap_" x then
adamc@130 576 String.extract (x, 5, NONE)
adamc@130 577 else
adamc@130 578 x
adamc@130 579 in
adamc@130 580 (x, n, corifyCon st t, corifyExp st e, s)
adamc@130 581 end)
adamc@130 582 vis
adamc@125 583 in
adamc@125 584 ([(L'.DValRec vis, loc)], st)
adamc@125 585 end
adamc@39 586 | L.DSgn _ => ([], st)
adamc@16 587
adamc@146 588 | L.DStr (x, n, _, (L.StrFun (xa, na, _, _, str), _)) =>
adamc@146 589 ([], St.bindFunctor st x n xa na str)
adamc@46 590
adamc@39 591 | L.DStr (x, n, _, str) =>
adamc@39 592 let
adamc@39 593 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 594 val st = St.bindStr outer x n inner
adamc@39 595 in
adamc@39 596 (ds, st)
adamc@39 597 end
adamc@16 598
adamc@49 599 | L.DFfiStr (m, n, (sgn, _)) =>
adamc@49 600 (case sgn of
adamc@49 601 L.SgnConst sgis =>
adamc@49 602 let
adamc@49 603 val (ds, cmap, st) =
adamc@49 604 foldl (fn ((sgi, _), (ds, cmap, st)) =>
adamc@49 605 case sgi of
adamc@49 606 L.SgiConAbs (x, n, k) =>
adamc@49 607 let
adamc@73 608 val (st, n') = St.bindCon st x n
adamc@49 609 in
adamc@49 610 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 611 cmap,
adamc@49 612 st)
adamc@49 613 end
adamc@49 614 | L.SgiCon (x, n, k, _) =>
adamc@49 615 let
adamc@73 616 val (st, n') = St.bindCon st x n
adamc@49 617 in
adamc@49 618 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 619 cmap,
adamc@49 620 st)
adamc@49 621 end
adamc@49 622
adamc@49 623 | L.SgiVal (x, _, c) =>
adamc@49 624 (ds,
adamc@49 625 SM.insert (cmap, x, corifyCon st c),
adamc@49 626 st)
adamc@49 627 | _ => (ds, cmap, st)) ([], SM.empty, st) sgis
adamc@49 628
adamc@49 629 val st = St.bindStr st m n (St.ffi m cmap)
adamc@49 630 in
adamc@49 631 (rev ds, st)
adamc@49 632 end
adamc@49 633 | _ => raise Fail "Non-const signature for FFI structure")
adamc@48 634
adamc@109 635 | L.DExport (en, sgn, str) =>
adamc@109 636 (case #1 sgn of
adamc@109 637 L.SgnConst sgis =>
adamc@109 638 let
adamc@109 639 fun pathify (str, _) =
adamc@109 640 case str of
adamc@109 641 L.StrVar m => SOME (m, [])
adamc@109 642 | L.StrProj (str, s) =>
adamc@109 643 Option.map (fn (m, ms) => (m, ms @ [s])) (pathify str)
adamc@109 644 | _ => NONE
adamc@109 645 in
adamc@109 646 case pathify str of
adamc@109 647 NONE => (ErrorMsg.errorAt loc "Structure is too fancy to export";
adamc@109 648 ([], st))
adamc@109 649 | SOME (m, ms) =>
adamc@109 650 let
adamc@109 651 fun wrapSgi ((sgi, _), (wds, eds)) =
adamc@109 652 case sgi of
adamc@109 653 L.SgiVal (s, _, t as (L.TFun (dom, ran), _)) =>
adamc@109 654 (case (#1 dom, #1 ran) of
adamc@147 655 (L.TRecord (L.CRecord (_, []), _),
adamc@139 656 L.CApp
adamc@139 657 ((L.CApp
adamc@139 658 ((L.CApp ((L.CModProj (_, [], "xml"), _),
adamc@139 659 (L.CRecord (_, [((L.CName "Html", _),
adamc@139 660 _)]), _)), _), _), _), _)) =>
adamc@109 661 let
adamc@109 662 val ran = (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc)
adamc@109 663 val e = (L.EModProj (m, ms, s), loc)
adamc@109 664 val e = (L.EAbs ("vs", dom, ran,
adamc@109 665 (L.EWrite (L.EApp (e, (L.ERel 0, loc)), loc), loc)), loc)
adamc@109 666 in
adamc@109 667 ((L.DVal ("wrap_" ^ s, 0,
adamc@109 668 (L.TFun (dom, ran), loc),
adamc@109 669 e), loc) :: wds,
adamc@109 670 (fn st =>
adamc@109 671 case #1 (corifyExp st (L.EModProj (en, [], "wrap_" ^ s), loc)) of
adamc@144 672 L'.ENamed n => (L'.DExport (L'.Link, n), loc)
adamc@109 673 | _ => raise Fail "Corify: Value to export didn't corify properly")
adamc@109 674 :: eds)
adamc@109 675 end
adamc@109 676 | _ => (wds, eds))
adamc@109 677 | _ => (wds, eds)
adamc@102 678
adamc@109 679 val (wds, eds) = foldl wrapSgi ([], []) sgis
adamc@109 680 val wrapper = (L.StrConst wds, loc)
adamc@109 681 val (ds, {inner, outer}) = corifyStr (wrapper, st)
adamc@109 682 val st = St.bindStr outer "wrapper" en inner
adamc@109 683
adamc@109 684 val ds = ds @ map (fn f => f st) eds
adamc@109 685 in
adamc@109 686 (ds, st)
adamc@109 687 end
adamc@109 688 end
adamc@109 689 | _ => raise Fail "Non-const signature for 'export'")
adamc@48 690
adamc@39 691 and corifyStr ((str, _), st) =
adamc@39 692 case str of
adamc@39 693 L.StrConst ds =>
adamc@39 694 let
adamc@39 695 val st = St.enter st
adamc@39 696 val (ds, st) = ListUtil.foldlMapConcat corifyDecl st ds
adamc@39 697 in
adamc@39 698 (ds, St.leave st)
adamc@39 699 end
adamc@39 700 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
adamc@39 701 | L.StrProj (str, x) =>
adamc@39 702 let
adamc@39 703 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 704 in
adamc@39 705 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
adamc@39 706 end
adamc@46 707 | L.StrFun _ => raise Fail "Corify of nested functor definition"
adamc@46 708 | L.StrApp (str1, str2) =>
adamc@46 709 let
adamc@46 710 fun unwind' (str, _) =
adamc@46 711 case str of
adamc@46 712 L.StrVar n => St.lookupStrById st n
adamc@46 713 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
adamc@46 714 | _ => raise Fail "Corify of fancy functor application [1]"
adamc@46 715
adamc@46 716 fun unwind (str, _) =
adamc@46 717 case str of
adamc@46 718 L.StrVar n => St.lookupFunctorById st n
adamc@46 719 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
adamc@46 720 | _ => raise Fail "Corify of fancy functor application [2]"
adamc@46 721
adamc@146 722 val (xa, na, body) = unwind str1
adamc@46 723
adamc@146 724 val (ds1, {inner = inner', outer}) = corifyStr (str2, st)
adamc@146 725 val (ds2, {inner, outer}) = corifyStr (body, St.bindStr outer xa na inner')
adamc@46 726 in
adamc@146 727 (ds1 @ ds2, {inner = St.bindStr inner xa na inner', outer = outer})
adamc@46 728 end
adamc@31 729
adamc@39 730 fun maxName ds = foldl (fn ((d, _), n) =>
adamc@39 731 case d of
adamc@39 732 L.DCon (_, n', _, _) => Int.max (n, n')
adamc@162 733 | L.DDatatype (_, n', _) => Int.max (n, n')
adamc@162 734 | L.DDatatypeImp (_, n', _, _, _, _) => Int.max (n, n')
adamc@124 735 | L.DVal (_, n', _, _) => Int.max (n, n')
adamc@124 736 | L.DValRec vis => foldl (fn ((_, n', _, _), n) => Int.max (n, n)) n vis
adamc@39 737 | L.DSgn (_, n', _) => Int.max (n, n')
adamc@48 738 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
adamc@100 739 | L.DFfiStr (_, n', _) => Int.max (n, n')
adamc@109 740 | L.DExport _ => n)
adamc@39 741 0 ds
adamc@39 742
adamc@39 743 and maxNameStr (str, _) =
adamc@39 744 case str of
adamc@39 745 L.StrConst ds => maxName ds
adamc@39 746 | L.StrVar n => n
adamc@39 747 | L.StrProj (str, _) => maxNameStr str
adamc@45 748 | L.StrFun (_, _, _, _, str) => maxNameStr str
adamc@45 749 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@39 750
adamc@39 751 fun corify ds =
adamc@39 752 let
adamc@39 753 val () = reset (maxName ds + 1)
adamc@146 754
adamc@39 755 val (ds, _) = ListUtil.foldlMapConcat corifyDecl St.empty ds
adamc@39 756 in
adamc@39 757 ds
adamc@39 758 end
adamc@16 759
adamc@16 760 end