annotate src/corify.sml @ 176:33d4a8eea484

Case through explify
author Adam Chlipala <adamc@hcoop.net>
date Thu, 31 Jul 2008 16:28:55 -0400
parents 80192edca30d
children 5d030ee143e2
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@73 74 datatype core_val =
adamc@73 75 ENormal of int
adamc@73 76 | EFfi of string * L'.con
adamc@73 77 val bindVal : t -> string -> int -> t * int
adamc@163 78 val bindConstructor : t -> string -> int -> t
adamc@73 79 val lookupValById : t -> int -> int option
adamc@73 80 val lookupValByName : t -> string -> core_val
adamc@39 81
adamc@39 82 val bindStr : t -> string -> int -> t -> t
adamc@39 83 val lookupStrById : t -> int -> t
adamc@39 84 val lookupStrByName : string * t -> t
adamc@46 85
adamc@146 86 val bindFunctor : t -> string -> int -> string -> int -> L.str -> t
adamc@146 87 val lookupFunctorById : t -> int -> string * int * L.str
adamc@146 88 val lookupFunctorByName : string * t -> string * int * L.str
adamc@39 89 end = struct
adamc@39 90
adamc@48 91 datatype flattening =
adamc@73 92 FNormal of {cons : int SM.map,
adamc@73 93 vals : int SM.map,
adamc@48 94 strs : flattening SM.map,
adamc@146 95 funs : (string * int * L.str) SM.map}
adamc@73 96 | FFfi of {mod : string,
adamc@73 97 vals : L'.con SM.map}
adamc@39 98
adamc@39 99 type t = {
adamc@73 100 cons : int IM.map,
adamc@73 101 vals : int IM.map,
adamc@39 102 strs : flattening IM.map,
adamc@146 103 funs : (string * int * L.str) IM.map,
adamc@39 104 current : flattening,
adamc@39 105 nested : flattening list
adamc@39 106 }
adamc@39 107
adamc@39 108 val empty = {
adamc@73 109 cons = IM.empty,
adamc@73 110 vals = IM.empty,
adamc@39 111 strs = IM.empty,
adamc@46 112 funs = IM.empty,
adamc@73 113 current = FNormal { cons = SM.empty, vals = SM.empty, strs = SM.empty, funs = SM.empty },
adamc@39 114 nested = []
adamc@39 115 }
adamc@39 116
adamc@146 117 fun debug ({current = FNormal {cons, vals, strs, funs}, ...} : t) =
adamc@146 118 print ("cons: " ^ Int.toString (SM.numItems cons) ^ "; "
adamc@146 119 ^ "vals: " ^ Int.toString (SM.numItems vals) ^ "; "
adamc@146 120 ^ "strs: " ^ Int.toString (SM.numItems strs) ^ "; "
adamc@146 121 ^ "funs: " ^ Int.toString (SM.numItems funs) ^ "\n")
adamc@146 122 | debug _ = print "Not normal!\n"
adamc@146 123
adamc@73 124 datatype core_con =
adamc@73 125 CNormal of int
adamc@73 126 | CFfi of string
adamc@48 127
adamc@73 128 datatype core_val =
adamc@73 129 ENormal of int
adamc@73 130 | EFfi of string * L'.con
adamc@73 131
adamc@73 132 fun bindCon {cons, vals, strs, funs, current, nested} s n =
adamc@39 133 let
adamc@39 134 val n' = alloc ()
adamc@39 135
adamc@39 136 val current =
adamc@48 137 case current of
adamc@48 138 FFfi _ => raise Fail "Binding inside FFfi"
adamc@73 139 | FNormal {cons, vals, strs, funs} =>
adamc@73 140 FNormal {cons = SM.insert (cons, s, n'),
adamc@73 141 vals = vals,
adamc@48 142 strs = strs,
adamc@48 143 funs = funs}
adamc@39 144 in
adamc@73 145 ({cons = IM.insert (cons, n, n'),
adamc@73 146 vals = vals,
adamc@39 147 strs = strs,
adamc@46 148 funs = funs,
adamc@39 149 current = current,
adamc@39 150 nested = nested},
adamc@39 151 n')
adamc@39 152 end
adamc@39 153
adamc@73 154 fun lookupConById ({cons, ...} : t) n = IM.find (cons, n)
adamc@39 155
adamc@73 156 fun lookupConByName ({current, ...} : t) x =
adamc@48 157 case current of
adamc@73 158 FFfi {mod = m, ...} => CFfi m
adamc@73 159 | FNormal {cons, ...} =>
adamc@73 160 case SM.find (cons, x) of
adamc@73 161 NONE => raise Fail "Corify.St.lookupConByName"
adamc@73 162 | SOME n => CNormal n
adamc@39 163
adamc@73 164 fun bindVal {cons, vals, strs, funs, current, nested} s n =
adamc@73 165 let
adamc@73 166 val n' = alloc ()
adamc@73 167
adamc@73 168 val current =
adamc@73 169 case current of
adamc@73 170 FFfi _ => raise Fail "Binding inside FFfi"
adamc@73 171 | FNormal {cons, vals, strs, funs} =>
adamc@73 172 FNormal {cons = cons,
adamc@73 173 vals = SM.insert (vals, s, n'),
adamc@73 174 strs = strs,
adamc@73 175 funs = funs}
adamc@73 176 in
adamc@73 177 ({cons = cons,
adamc@73 178 vals = IM.insert (vals, n, n'),
adamc@73 179 strs = strs,
adamc@73 180 funs = funs,
adamc@73 181 current = current,
adamc@73 182 nested = nested},
adamc@73 183 n')
adamc@73 184 end
adamc@73 185
adamc@163 186 fun bindConstructor {cons, vals, strs, funs, current, nested} s n =
adamc@163 187 let
adamc@163 188 val current =
adamc@163 189 case current of
adamc@163 190 FFfi _ => raise Fail "Binding inside FFfi"
adamc@163 191 | FNormal {cons, vals, strs, funs} =>
adamc@163 192 FNormal {cons = cons,
adamc@163 193 vals = SM.insert (vals, s, n),
adamc@163 194 strs = strs,
adamc@163 195 funs = funs}
adamc@163 196 in
adamc@163 197 {cons = cons,
adamc@163 198 vals = IM.insert (vals, n, n),
adamc@163 199 strs = strs,
adamc@163 200 funs = funs,
adamc@163 201 current = current,
adamc@163 202 nested = nested}
adamc@163 203 end
adamc@163 204
adamc@73 205 fun lookupValById ({vals, ...} : t) n = IM.find (vals, n)
adamc@73 206
adamc@73 207 fun lookupValByName ({current, ...} : t) x =
adamc@73 208 case current of
adamc@73 209 FFfi {mod = m, vals, ...} =>
adamc@73 210 (case SM.find (vals, x) of
adamc@73 211 NONE => raise Fail "Corify.St.lookupValByName: no type for FFI val"
adamc@73 212 | SOME t => EFfi (m, t))
adamc@73 213 | FNormal {vals, ...} =>
adamc@73 214 case SM.find (vals, x) of
adamc@73 215 NONE => raise Fail "Corify.St.lookupValByName"
adamc@73 216 | SOME n => ENormal n
adamc@73 217
adamc@73 218 fun enter {cons, vals, strs, funs, current, nested} =
adamc@73 219 {cons = cons,
adamc@73 220 vals = vals,
adamc@39 221 strs = strs,
adamc@46 222 funs = funs,
adamc@73 223 current = FNormal {cons = SM.empty,
adamc@73 224 vals = SM.empty,
adamc@48 225 strs = SM.empty,
adamc@48 226 funs = SM.empty},
adamc@39 227 nested = current :: nested}
adamc@39 228
adamc@73 229 fun dummy f = {cons = IM.empty,
adamc@73 230 vals = IM.empty,
adamc@39 231 strs = IM.empty,
adamc@46 232 funs = IM.empty,
adamc@39 233 current = f,
adamc@39 234 nested = []}
adamc@39 235
adamc@73 236 fun leave {cons, vals, strs, funs, current, nested = m1 :: rest} =
adamc@73 237 {outer = {cons = cons,
adamc@73 238 vals = vals,
adamc@39 239 strs = strs,
adamc@46 240 funs = funs,
adamc@39 241 current = m1,
adamc@39 242 nested = rest},
adamc@39 243 inner = dummy current}
adamc@39 244 | leave _ = raise Fail "Corify.St.leave"
adamc@39 245
adamc@73 246 fun ffi m vals = dummy (FFfi {mod = m, vals = vals})
adamc@48 247
adamc@73 248 fun bindStr ({cons, vals, strs, funs,
adamc@73 249 current = FNormal {cons = mcons, vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@46 250 x n ({current = f, ...} : t) =
adamc@73 251 {cons = cons,
adamc@73 252 vals = vals,
adamc@39 253 strs = IM.insert (strs, n, f),
adamc@46 254 funs = funs,
adamc@73 255 current = FNormal {cons = mcons,
adamc@73 256 vals = mvals,
adamc@73 257 strs = SM.insert (mstrs, x, f),
adamc@73 258 funs = mfuns},
adamc@39 259 nested = nested}
adamc@48 260 | bindStr _ _ _ _ = raise Fail "Corify.St.bindStr"
adamc@39 261
adamc@39 262 fun lookupStrById ({strs, ...} : t) n =
adamc@39 263 case IM.find (strs, n) of
adamc@46 264 NONE => raise Fail "Corify.St.lookupStrById"
adamc@39 265 | SOME f => dummy f
adamc@39 266
adamc@48 267 fun lookupStrByName (m, {current = FNormal {strs, ...}, ...} : t) =
adamc@48 268 (case SM.find (strs, m) of
adamc@48 269 NONE => raise Fail "Corify.St.lookupStrByName"
adamc@48 270 | SOME f => dummy f)
adamc@48 271 | lookupStrByName _ = raise Fail "Corify.St.lookupStrByName"
adamc@39 272
adamc@73 273 fun bindFunctor ({cons, vals, strs, funs,
adamc@73 274 current = FNormal {cons = mcons, vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@146 275 x n xa na str =
adamc@73 276 {cons = cons,
adamc@73 277 vals = vals,
adamc@46 278 strs = strs,
adamc@146 279 funs = IM.insert (funs, n, (xa, na, str)),
adamc@73 280 current = FNormal {cons = mcons,
adamc@73 281 vals = mvals,
adamc@48 282 strs = mstrs,
adamc@146 283 funs = SM.insert (mfuns, x, (xa, na, str))},
adamc@46 284 nested = nested}
adamc@146 285 | bindFunctor _ _ _ _ _ _ = raise Fail "Corify.St.bindFunctor"
adamc@46 286
adamc@46 287 fun lookupFunctorById ({funs, ...} : t) n =
adamc@46 288 case IM.find (funs, n) of
adamc@46 289 NONE => raise Fail "Corify.St.lookupFunctorById"
adamc@46 290 | SOME v => v
adamc@46 291
adamc@48 292 fun lookupFunctorByName (m, {current = FNormal {funs, ...}, ...} : t) =
adamc@48 293 (case SM.find (funs, m) of
adamc@48 294 NONE => raise Fail "Corify.St.lookupFunctorByName"
adamc@48 295 | SOME v => v)
adamc@48 296 | lookupFunctorByName _ = raise Fail "Corify.St.lookupFunctorByName"
adamc@46 297
adamc@39 298 end
adamc@39 299
adamc@39 300
adamc@16 301 fun corifyKind (k, loc) =
adamc@16 302 case k of
adamc@16 303 L.KType => (L'.KType, loc)
adamc@16 304 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
adamc@16 305 | L.KName => (L'.KName, loc)
adamc@16 306 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
adamc@87 307 | L.KUnit => (L'.KUnit, loc)
adamc@16 308
adamc@39 309 fun corifyCon st (c, loc) =
adamc@16 310 case c of
adamc@39 311 L.TFun (t1, t2) => (L'.TFun (corifyCon st t1, corifyCon st t2), loc)
adamc@39 312 | L.TCFun (x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon st t), loc)
adamc@39 313 | L.TRecord c => (L'.TRecord (corifyCon st c), loc)
adamc@16 314
adamc@16 315 | L.CRel n => (L'.CRel n, loc)
adamc@39 316 | L.CNamed n =>
adamc@73 317 (case St.lookupConById st n of
adamc@39 318 NONE => (L'.CNamed n, loc)
adamc@39 319 | SOME n => (L'.CNamed n, loc))
adamc@39 320 | L.CModProj (m, ms, x) =>
adamc@39 321 let
adamc@39 322 val st = St.lookupStrById st m
adamc@39 323 val st = foldl St.lookupStrByName st ms
adamc@39 324 in
adamc@73 325 case St.lookupConByName st x of
adamc@73 326 St.CNormal n => (L'.CNamed n, loc)
adamc@73 327 | St.CFfi m => (L'.CFfi (m, x), loc)
adamc@39 328 end
adamc@34 329
adamc@39 330 | L.CApp (c1, c2) => (L'.CApp (corifyCon st c1, corifyCon st c2), loc)
adamc@39 331 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon st c), loc)
adamc@16 332
adamc@16 333 | L.CName s => (L'.CName s, loc)
adamc@16 334
adamc@39 335 | L.CRecord (k, xcs) =>
adamc@39 336 (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon st c1, corifyCon st c2)) xcs), loc)
adamc@39 337 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon st c1, corifyCon st c2), loc)
adamc@69 338 | L.CFold (k1, k2) => (L'.CFold (corifyKind k1, corifyKind k2), loc)
adamc@87 339 | L.CUnit => (L'.CUnit, loc)
adamc@16 340
adamc@39 341 fun corifyExp st (e, loc) =
adamc@16 342 case e of
adamc@16 343 L.EPrim p => (L'.EPrim p, loc)
adamc@16 344 | L.ERel n => (L'.ERel n, loc)
adamc@39 345 | L.ENamed n =>
adamc@73 346 (case St.lookupValById st n of
adamc@39 347 NONE => (L'.ENamed n, loc)
adamc@39 348 | SOME n => (L'.ENamed n, loc))
adamc@39 349 | L.EModProj (m, ms, x) =>
adamc@39 350 let
adamc@39 351 val st = St.lookupStrById st m
adamc@39 352 val st = foldl St.lookupStrByName st ms
adamc@39 353 in
adamc@73 354 case St.lookupValByName st x of
adamc@73 355 St.ENormal n => (L'.ENamed n, loc)
adamc@73 356 | St.EFfi (m, t) =>
adamc@49 357 case t of
adamc@50 358 (L'.TFun (dom as (L'.TRecord (L'.CRecord (_, []), _), _), ran), _) =>
adamc@50 359 (L'.EAbs ("arg", dom, ran, (L'.EFfiApp (m, x, []), loc)), loc)
adamc@50 360 | t as (L'.TFun _, _) =>
adamc@49 361 let
adamc@49 362 fun getArgs (all as (t, _), args) =
adamc@49 363 case t of
adamc@49 364 L'.TFun (dom, ran) => getArgs (ran, dom :: args)
adamc@49 365 | _ => (all, rev args)
adamc@49 366
adamc@49 367 val (result, args) = getArgs (t, [])
adamc@49 368
adamc@50 369 val (actuals, _) = foldr (fn (_, (actuals, n)) =>
adamc@50 370 ((L'.ERel n, loc) :: actuals,
adamc@50 371 n + 1)) ([], 0) args
adamc@50 372 val app = (L'.EFfiApp (m, x, actuals), loc)
adamc@49 373 val (abs, _, _) = foldr (fn (t, (abs, ran, n)) =>
adamc@49 374 ((L'.EAbs ("arg" ^ Int.toString n,
adamc@49 375 t,
adamc@49 376 ran,
adamc@49 377 abs), loc),
adamc@49 378 (L'.TFun (t, ran), loc),
adamc@49 379 n - 1)) (app, result, length args - 1) args
adamc@49 380 in
adamc@49 381 abs
adamc@49 382 end
adamc@49 383 | _ => (L'.EFfi (m, x), loc)
adamc@39 384 end
adamc@39 385 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
adamc@39 386 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
adamc@39 387 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
adamc@39 388 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
adamc@16 389
adamc@110 390 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) =>
adamc@110 391 (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
adamc@39 392 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
adamc@39 393 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@149 394 | L.ECut (e1, c, {field, rest}) => (L'.ECut (corifyExp st e1, corifyCon st c,
adamc@149 395 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@73 396 | L.EFold k => (L'.EFold (corifyKind k), loc)
adamc@176 397 | L.ECase _ => raise Fail "Corify ECase"
adamc@109 398 | L.EWrite e => (L'.EWrite (corifyExp st e), loc)
adamc@16 399
adamc@39 400 fun corifyDecl ((d, loc : EM.span), st) =
adamc@39 401 case d of
adamc@39 402 L.DCon (x, n, k, c) =>
adamc@39 403 let
adamc@73 404 val (st, n) = St.bindCon st x n
adamc@39 405 in
adamc@39 406 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
adamc@39 407 end
adamc@163 408 | L.DDatatype (x, n, xncs) =>
adamc@163 409 let
adamc@163 410 val (st, n) = St.bindCon st x n
adamc@163 411 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@163 412 let
adamc@163 413 val st = St.bindConstructor st x n
adamc@163 414 val co = Option.map (corifyCon st) co
adamc@163 415 in
adamc@163 416 ((x, n, co), st)
adamc@163 417 end) st xncs
adamc@163 418 in
adamc@163 419 ([(L'.DDatatype (x, n, xncs), loc)], st)
adamc@163 420 end
adamc@163 421 | L.DDatatypeImp (x, n, m1, ms, s, xncs) =>
adamc@163 422 let
adamc@163 423 val (st, n) = St.bindCon st x n
adamc@163 424 val c = corifyCon st (L.CModProj (m1, ms, s), loc)
adamc@163 425
adamc@163 426 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@163 427 let
adamc@163 428 val (st, n) = St.bindVal st x n
adamc@163 429 val co = Option.map (corifyCon st) co
adamc@163 430 in
adamc@163 431 ((x, n, co), st)
adamc@163 432 end) st xncs
adamc@163 433
adamc@163 434 val cds = map (fn (x, n, co) =>
adamc@163 435 let
adamc@163 436 val t = case co of
adamc@163 437 NONE => c
adamc@163 438 | SOME t' => (L'.TFun (t', c), loc)
adamc@163 439 val e = corifyExp st (L.EModProj (m1, ms, x), loc)
adamc@163 440 in
adamc@163 441 (L'.DVal (x, n, t, e, x), loc)
adamc@163 442 end) xncs
adamc@163 443 in
adamc@163 444 ((L'.DCon (x, n, (L'.KType, loc), c), loc) :: cds, st)
adamc@163 445 end
adamc@39 446 | L.DVal (x, n, t, e) =>
adamc@39 447 let
adamc@73 448 val (st, n) = St.bindVal st x n
adamc@111 449 val s =
adamc@111 450 if String.isPrefix "wrap_" x then
adamc@111 451 String.extract (x, 5, NONE)
adamc@111 452 else
adamc@111 453 x
adamc@39 454 in
adamc@111 455 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e, s), loc)], st)
adamc@39 456 end
adamc@125 457 | L.DValRec vis =>
adamc@125 458 let
adamc@125 459 val (vis, st) = ListUtil.foldlMap
adamc@130 460 (fn ((x, n, t, e), st) =>
adamc@130 461 let
adamc@130 462 val (st, n) = St.bindVal st x n
adamc@130 463 in
adamc@130 464 ((x, n, t, e), st)
adamc@130 465 end)
adamc@130 466 st vis
adamc@130 467
adamc@130 468 val vis = map
adamc@130 469 (fn (x, n, t, e) =>
adamc@130 470 let
adamc@130 471 val s =
adamc@130 472 if String.isPrefix "wrap_" x then
adamc@130 473 String.extract (x, 5, NONE)
adamc@130 474 else
adamc@130 475 x
adamc@130 476 in
adamc@130 477 (x, n, corifyCon st t, corifyExp st e, s)
adamc@130 478 end)
adamc@130 479 vis
adamc@125 480 in
adamc@125 481 ([(L'.DValRec vis, loc)], st)
adamc@125 482 end
adamc@39 483 | L.DSgn _ => ([], st)
adamc@16 484
adamc@146 485 | L.DStr (x, n, _, (L.StrFun (xa, na, _, _, str), _)) =>
adamc@146 486 ([], St.bindFunctor st x n xa na str)
adamc@46 487
adamc@39 488 | L.DStr (x, n, _, str) =>
adamc@39 489 let
adamc@39 490 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 491 val st = St.bindStr outer x n inner
adamc@39 492 in
adamc@39 493 (ds, st)
adamc@39 494 end
adamc@16 495
adamc@49 496 | L.DFfiStr (m, n, (sgn, _)) =>
adamc@49 497 (case sgn of
adamc@49 498 L.SgnConst sgis =>
adamc@49 499 let
adamc@49 500 val (ds, cmap, st) =
adamc@49 501 foldl (fn ((sgi, _), (ds, cmap, st)) =>
adamc@49 502 case sgi of
adamc@49 503 L.SgiConAbs (x, n, k) =>
adamc@49 504 let
adamc@73 505 val (st, n') = St.bindCon st x n
adamc@49 506 in
adamc@49 507 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 508 cmap,
adamc@49 509 st)
adamc@49 510 end
adamc@49 511 | L.SgiCon (x, n, k, _) =>
adamc@49 512 let
adamc@73 513 val (st, n') = St.bindCon st x n
adamc@49 514 in
adamc@49 515 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 516 cmap,
adamc@49 517 st)
adamc@49 518 end
adamc@49 519
adamc@49 520 | L.SgiVal (x, _, c) =>
adamc@49 521 (ds,
adamc@49 522 SM.insert (cmap, x, corifyCon st c),
adamc@49 523 st)
adamc@49 524 | _ => (ds, cmap, st)) ([], SM.empty, st) sgis
adamc@49 525
adamc@49 526 val st = St.bindStr st m n (St.ffi m cmap)
adamc@49 527 in
adamc@49 528 (rev ds, st)
adamc@49 529 end
adamc@49 530 | _ => raise Fail "Non-const signature for FFI structure")
adamc@48 531
adamc@109 532 | L.DExport (en, sgn, str) =>
adamc@109 533 (case #1 sgn of
adamc@109 534 L.SgnConst sgis =>
adamc@109 535 let
adamc@109 536 fun pathify (str, _) =
adamc@109 537 case str of
adamc@109 538 L.StrVar m => SOME (m, [])
adamc@109 539 | L.StrProj (str, s) =>
adamc@109 540 Option.map (fn (m, ms) => (m, ms @ [s])) (pathify str)
adamc@109 541 | _ => NONE
adamc@109 542 in
adamc@109 543 case pathify str of
adamc@109 544 NONE => (ErrorMsg.errorAt loc "Structure is too fancy to export";
adamc@109 545 ([], st))
adamc@109 546 | SOME (m, ms) =>
adamc@109 547 let
adamc@109 548 fun wrapSgi ((sgi, _), (wds, eds)) =
adamc@109 549 case sgi of
adamc@109 550 L.SgiVal (s, _, t as (L.TFun (dom, ran), _)) =>
adamc@109 551 (case (#1 dom, #1 ran) of
adamc@147 552 (L.TRecord (L.CRecord (_, []), _),
adamc@139 553 L.CApp
adamc@139 554 ((L.CApp
adamc@139 555 ((L.CApp ((L.CModProj (_, [], "xml"), _),
adamc@139 556 (L.CRecord (_, [((L.CName "Html", _),
adamc@139 557 _)]), _)), _), _), _), _)) =>
adamc@109 558 let
adamc@109 559 val ran = (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc)
adamc@109 560 val e = (L.EModProj (m, ms, s), loc)
adamc@109 561 val e = (L.EAbs ("vs", dom, ran,
adamc@109 562 (L.EWrite (L.EApp (e, (L.ERel 0, loc)), loc), loc)), loc)
adamc@109 563 in
adamc@109 564 ((L.DVal ("wrap_" ^ s, 0,
adamc@109 565 (L.TFun (dom, ran), loc),
adamc@109 566 e), loc) :: wds,
adamc@109 567 (fn st =>
adamc@109 568 case #1 (corifyExp st (L.EModProj (en, [], "wrap_" ^ s), loc)) of
adamc@144 569 L'.ENamed n => (L'.DExport (L'.Link, n), loc)
adamc@109 570 | _ => raise Fail "Corify: Value to export didn't corify properly")
adamc@109 571 :: eds)
adamc@109 572 end
adamc@109 573 | _ => (wds, eds))
adamc@109 574 | _ => (wds, eds)
adamc@102 575
adamc@109 576 val (wds, eds) = foldl wrapSgi ([], []) sgis
adamc@109 577 val wrapper = (L.StrConst wds, loc)
adamc@109 578 val (ds, {inner, outer}) = corifyStr (wrapper, st)
adamc@109 579 val st = St.bindStr outer "wrapper" en inner
adamc@109 580
adamc@109 581 val ds = ds @ map (fn f => f st) eds
adamc@109 582 in
adamc@109 583 (ds, st)
adamc@109 584 end
adamc@109 585 end
adamc@109 586 | _ => raise Fail "Non-const signature for 'export'")
adamc@48 587
adamc@39 588 and corifyStr ((str, _), st) =
adamc@39 589 case str of
adamc@39 590 L.StrConst ds =>
adamc@39 591 let
adamc@39 592 val st = St.enter st
adamc@39 593 val (ds, st) = ListUtil.foldlMapConcat corifyDecl st ds
adamc@39 594 in
adamc@39 595 (ds, St.leave st)
adamc@39 596 end
adamc@39 597 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
adamc@39 598 | L.StrProj (str, x) =>
adamc@39 599 let
adamc@39 600 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 601 in
adamc@39 602 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
adamc@39 603 end
adamc@46 604 | L.StrFun _ => raise Fail "Corify of nested functor definition"
adamc@46 605 | L.StrApp (str1, str2) =>
adamc@46 606 let
adamc@46 607 fun unwind' (str, _) =
adamc@46 608 case str of
adamc@46 609 L.StrVar n => St.lookupStrById st n
adamc@46 610 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
adamc@46 611 | _ => raise Fail "Corify of fancy functor application [1]"
adamc@46 612
adamc@46 613 fun unwind (str, _) =
adamc@46 614 case str of
adamc@46 615 L.StrVar n => St.lookupFunctorById st n
adamc@46 616 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
adamc@46 617 | _ => raise Fail "Corify of fancy functor application [2]"
adamc@46 618
adamc@146 619 val (xa, na, body) = unwind str1
adamc@46 620
adamc@146 621 val (ds1, {inner = inner', outer}) = corifyStr (str2, st)
adamc@146 622 val (ds2, {inner, outer}) = corifyStr (body, St.bindStr outer xa na inner')
adamc@46 623 in
adamc@146 624 (ds1 @ ds2, {inner = St.bindStr inner xa na inner', outer = outer})
adamc@46 625 end
adamc@31 626
adamc@39 627 fun maxName ds = foldl (fn ((d, _), n) =>
adamc@39 628 case d of
adamc@39 629 L.DCon (_, n', _, _) => Int.max (n, n')
adamc@162 630 | L.DDatatype (_, n', _) => Int.max (n, n')
adamc@162 631 | L.DDatatypeImp (_, n', _, _, _, _) => Int.max (n, n')
adamc@124 632 | L.DVal (_, n', _, _) => Int.max (n, n')
adamc@124 633 | L.DValRec vis => foldl (fn ((_, n', _, _), n) => Int.max (n, n)) n vis
adamc@39 634 | L.DSgn (_, n', _) => Int.max (n, n')
adamc@48 635 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
adamc@100 636 | L.DFfiStr (_, n', _) => Int.max (n, n')
adamc@109 637 | L.DExport _ => n)
adamc@39 638 0 ds
adamc@39 639
adamc@39 640 and maxNameStr (str, _) =
adamc@39 641 case str of
adamc@39 642 L.StrConst ds => maxName ds
adamc@39 643 | L.StrVar n => n
adamc@39 644 | L.StrProj (str, _) => maxNameStr str
adamc@45 645 | L.StrFun (_, _, _, _, str) => maxNameStr str
adamc@45 646 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@39 647
adamc@39 648 fun corify ds =
adamc@39 649 let
adamc@39 650 val () = reset (maxName ds + 1)
adamc@146 651
adamc@39 652 val (ds, _) = ListUtil.foldlMapConcat corifyDecl St.empty ds
adamc@39 653 in
adamc@39 654 ds
adamc@39 655 end
adamc@16 656
adamc@16 657 end