annotate src/corify.sml @ 144:f0d3402184d1

Simple forms work
author Adam Chlipala <adamc@hcoop.net>
date Tue, 22 Jul 2008 15:12:20 -0400
parents adfa2c7a75da
children 80ac94b54e41
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@111 379 val s =
adamc@111 380 if String.isPrefix "wrap_" x then
adamc@111 381 String.extract (x, 5, NONE)
adamc@111 382 else
adamc@111 383 x
adamc@39 384 in
adamc@111 385 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e, s), loc)], st)
adamc@39 386 end
adamc@125 387 | L.DValRec vis =>
adamc@125 388 let
adamc@125 389 val (vis, st) = ListUtil.foldlMap
adamc@130 390 (fn ((x, n, t, e), st) =>
adamc@130 391 let
adamc@130 392 val (st, n) = St.bindVal st x n
adamc@130 393 in
adamc@130 394 ((x, n, t, e), st)
adamc@130 395 end)
adamc@130 396 st vis
adamc@130 397
adamc@130 398 val vis = map
adamc@130 399 (fn (x, n, t, e) =>
adamc@130 400 let
adamc@130 401 val s =
adamc@130 402 if String.isPrefix "wrap_" x then
adamc@130 403 String.extract (x, 5, NONE)
adamc@130 404 else
adamc@130 405 x
adamc@130 406 in
adamc@130 407 (x, n, corifyCon st t, corifyExp st e, s)
adamc@130 408 end)
adamc@130 409 vis
adamc@125 410 in
adamc@125 411 ([(L'.DValRec vis, loc)], st)
adamc@125 412 end
adamc@39 413 | L.DSgn _ => ([], st)
adamc@16 414
adamc@46 415 | L.DStr (x, n, _, (L.StrFun (_, na, _, _, str), _)) =>
adamc@46 416 ([], St.bindFunctor st x n na str)
adamc@46 417
adamc@39 418 | L.DStr (x, n, _, str) =>
adamc@39 419 let
adamc@39 420 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 421 val st = St.bindStr outer x n inner
adamc@39 422 in
adamc@39 423 (ds, st)
adamc@39 424 end
adamc@16 425
adamc@49 426 | L.DFfiStr (m, n, (sgn, _)) =>
adamc@49 427 (case sgn of
adamc@49 428 L.SgnConst sgis =>
adamc@49 429 let
adamc@49 430 val (ds, cmap, st) =
adamc@49 431 foldl (fn ((sgi, _), (ds, cmap, st)) =>
adamc@49 432 case sgi of
adamc@49 433 L.SgiConAbs (x, n, k) =>
adamc@49 434 let
adamc@73 435 val (st, n') = St.bindCon st x n
adamc@49 436 in
adamc@49 437 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 438 cmap,
adamc@49 439 st)
adamc@49 440 end
adamc@49 441 | L.SgiCon (x, n, k, _) =>
adamc@49 442 let
adamc@73 443 val (st, n') = St.bindCon st x n
adamc@49 444 in
adamc@49 445 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 446 cmap,
adamc@49 447 st)
adamc@49 448 end
adamc@49 449
adamc@49 450 | L.SgiVal (x, _, c) =>
adamc@49 451 (ds,
adamc@49 452 SM.insert (cmap, x, corifyCon st c),
adamc@49 453 st)
adamc@49 454 | _ => (ds, cmap, st)) ([], SM.empty, st) sgis
adamc@49 455
adamc@49 456 val st = St.bindStr st m n (St.ffi m cmap)
adamc@49 457 in
adamc@49 458 (rev ds, st)
adamc@49 459 end
adamc@49 460 | _ => raise Fail "Non-const signature for FFI structure")
adamc@48 461
adamc@109 462 | L.DExport (en, sgn, str) =>
adamc@109 463 (case #1 sgn of
adamc@109 464 L.SgnConst sgis =>
adamc@109 465 let
adamc@109 466 fun pathify (str, _) =
adamc@109 467 case str of
adamc@109 468 L.StrVar m => SOME (m, [])
adamc@109 469 | L.StrProj (str, s) =>
adamc@109 470 Option.map (fn (m, ms) => (m, ms @ [s])) (pathify str)
adamc@109 471 | _ => NONE
adamc@109 472 in
adamc@109 473 case pathify str of
adamc@109 474 NONE => (ErrorMsg.errorAt loc "Structure is too fancy to export";
adamc@109 475 ([], st))
adamc@109 476 | SOME (m, ms) =>
adamc@109 477 let
adamc@109 478 fun wrapSgi ((sgi, _), (wds, eds)) =
adamc@109 479 case sgi of
adamc@109 480 L.SgiVal (s, _, t as (L.TFun (dom, ran), _)) =>
adamc@109 481 (case (#1 dom, #1 ran) of
adamc@109 482 (L.TRecord _,
adamc@139 483 L.CApp
adamc@139 484 ((L.CApp
adamc@139 485 ((L.CApp ((L.CModProj (_, [], "xml"), _),
adamc@139 486 (L.CRecord (_, [((L.CName "Html", _),
adamc@139 487 _)]), _)), _), _), _), _)) =>
adamc@109 488 let
adamc@109 489 val ran = (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc)
adamc@109 490 val e = (L.EModProj (m, ms, s), loc)
adamc@109 491 val e = (L.EAbs ("vs", dom, ran,
adamc@109 492 (L.EWrite (L.EApp (e, (L.ERel 0, loc)), loc), loc)), loc)
adamc@109 493 in
adamc@109 494 ((L.DVal ("wrap_" ^ s, 0,
adamc@109 495 (L.TFun (dom, ran), loc),
adamc@109 496 e), loc) :: wds,
adamc@109 497 (fn st =>
adamc@109 498 case #1 (corifyExp st (L.EModProj (en, [], "wrap_" ^ s), loc)) of
adamc@144 499 L'.ENamed n => (L'.DExport (L'.Link, n), loc)
adamc@109 500 | _ => raise Fail "Corify: Value to export didn't corify properly")
adamc@109 501 :: eds)
adamc@109 502 end
adamc@109 503 | _ => (wds, eds))
adamc@109 504 | _ => (wds, eds)
adamc@102 505
adamc@109 506 val (wds, eds) = foldl wrapSgi ([], []) sgis
adamc@109 507 val wrapper = (L.StrConst wds, loc)
adamc@109 508 val (ds, {inner, outer}) = corifyStr (wrapper, st)
adamc@109 509 val st = St.bindStr outer "wrapper" en inner
adamc@109 510
adamc@109 511 val ds = ds @ map (fn f => f st) eds
adamc@109 512 in
adamc@109 513 (ds, st)
adamc@109 514 end
adamc@109 515 end
adamc@109 516 | _ => raise Fail "Non-const signature for 'export'")
adamc@109 517
adamc@48 518
adamc@39 519 and corifyStr ((str, _), st) =
adamc@39 520 case str of
adamc@39 521 L.StrConst ds =>
adamc@39 522 let
adamc@39 523 val st = St.enter st
adamc@39 524 val (ds, st) = ListUtil.foldlMapConcat corifyDecl st ds
adamc@39 525 in
adamc@39 526 (ds, St.leave st)
adamc@39 527 end
adamc@39 528 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
adamc@39 529 | L.StrProj (str, x) =>
adamc@39 530 let
adamc@39 531 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 532 in
adamc@39 533 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
adamc@39 534 end
adamc@46 535 | L.StrFun _ => raise Fail "Corify of nested functor definition"
adamc@46 536 | L.StrApp (str1, str2) =>
adamc@46 537 let
adamc@46 538 fun unwind' (str, _) =
adamc@46 539 case str of
adamc@46 540 L.StrVar n => St.lookupStrById st n
adamc@46 541 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
adamc@46 542 | _ => raise Fail "Corify of fancy functor application [1]"
adamc@46 543
adamc@46 544 fun unwind (str, _) =
adamc@46 545 case str of
adamc@46 546 L.StrVar n => St.lookupFunctorById st n
adamc@46 547 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
adamc@46 548 | _ => raise Fail "Corify of fancy functor application [2]"
adamc@46 549
adamc@46 550 val (na, body) = unwind str1
adamc@46 551
adamc@46 552 val (ds1, {inner, outer}) = corifyStr (str2, st)
adamc@46 553 val (ds2, sts) = corifyStr (body, St.bindStr outer "ARG" na inner)
adamc@46 554 in
adamc@46 555 (ds1 @ ds2, sts)
adamc@46 556 end
adamc@31 557
adamc@39 558 fun maxName ds = foldl (fn ((d, _), n) =>
adamc@39 559 case d of
adamc@39 560 L.DCon (_, n', _, _) => Int.max (n, n')
adamc@124 561 | L.DVal (_, n', _, _) => Int.max (n, n')
adamc@124 562 | L.DValRec vis => foldl (fn ((_, n', _, _), n) => Int.max (n, n)) n vis
adamc@39 563 | L.DSgn (_, n', _) => Int.max (n, n')
adamc@48 564 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
adamc@100 565 | L.DFfiStr (_, n', _) => Int.max (n, n')
adamc@109 566 | L.DExport _ => n)
adamc@39 567 0 ds
adamc@39 568
adamc@39 569 and maxNameStr (str, _) =
adamc@39 570 case str of
adamc@39 571 L.StrConst ds => maxName ds
adamc@39 572 | L.StrVar n => n
adamc@39 573 | L.StrProj (str, _) => maxNameStr str
adamc@45 574 | L.StrFun (_, _, _, _, str) => maxNameStr str
adamc@45 575 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@39 576
adamc@39 577 fun corify ds =
adamc@39 578 let
adamc@39 579 val () = reset (maxName ds + 1)
adamc@39 580 val (ds, _) = ListUtil.foldlMapConcat corifyDecl St.empty ds
adamc@39 581 in
adamc@39 582 ds
adamc@39 583 end
adamc@16 584
adamc@16 585 end