annotate src/corify.sml @ 110:3739af9e727a

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