annotate src/corify.sml @ 162:06a98129b23f

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