annotate src/corify.sml @ 186:88d46972de53

bool in Basis
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 18:53:20 -0400
parents 19ee24bffbc0
children 8e9f97508f0d
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@186 65 val ffi : string -> L'.con SM.map -> (string * L'.con option) SM.map -> t
adamc@39 66
adamc@73 67 datatype core_con =
adamc@73 68 CNormal of int
adamc@73 69 | CFfi of string
adamc@73 70 val bindCon : t -> string -> int -> t * int
adamc@73 71 val lookupConById : t -> int -> int option
adamc@73 72 val lookupConByName : t -> string -> core_con
adamc@48 73
adamc@177 74 val bindConstructor : t -> string -> int -> L'.patCon -> t
adamc@186 75 val lookupConstructorByNameOpt : t -> string -> L'.patCon option
adamc@177 76 val lookupConstructorByName : t -> string -> L'.patCon
adamc@177 77 val lookupConstructorById : t -> int -> L'.patCon
adamc@177 78
adamc@73 79 datatype core_val =
adamc@73 80 ENormal of int
adamc@73 81 | EFfi of string * L'.con
adamc@73 82 val bindVal : t -> string -> int -> t * int
adamc@177 83 val bindConstructorVal : t -> string -> int -> t
adamc@73 84 val lookupValById : t -> int -> int option
adamc@73 85 val lookupValByName : t -> string -> core_val
adamc@39 86
adamc@39 87 val bindStr : t -> string -> int -> t -> t
adamc@39 88 val lookupStrById : t -> int -> t
adamc@39 89 val lookupStrByName : string * t -> t
adamc@46 90
adamc@146 91 val bindFunctor : t -> string -> int -> string -> int -> L.str -> t
adamc@146 92 val lookupFunctorById : t -> int -> string * int * L.str
adamc@146 93 val lookupFunctorByName : string * t -> string * int * L.str
adamc@39 94 end = struct
adamc@39 95
adamc@48 96 datatype flattening =
adamc@73 97 FNormal of {cons : int SM.map,
adamc@177 98 constructors : L'.patCon SM.map,
adamc@73 99 vals : int SM.map,
adamc@48 100 strs : flattening SM.map,
adamc@146 101 funs : (string * int * L.str) SM.map}
adamc@73 102 | FFfi of {mod : string,
adamc@185 103 vals : L'.con SM.map,
adamc@186 104 constructors : (string * L'.con option) SM.map}
adamc@39 105
adamc@39 106 type t = {
adamc@73 107 cons : int IM.map,
adamc@177 108 constructors : L'.patCon IM.map,
adamc@73 109 vals : int IM.map,
adamc@39 110 strs : flattening IM.map,
adamc@146 111 funs : (string * int * L.str) IM.map,
adamc@39 112 current : flattening,
adamc@39 113 nested : flattening list
adamc@39 114 }
adamc@39 115
adamc@39 116 val empty = {
adamc@73 117 cons = IM.empty,
adamc@177 118 constructors = IM.empty,
adamc@73 119 vals = IM.empty,
adamc@39 120 strs = IM.empty,
adamc@46 121 funs = IM.empty,
adamc@177 122 current = FNormal { cons = SM.empty, constructors = SM.empty, vals = SM.empty, strs = SM.empty, funs = SM.empty },
adamc@39 123 nested = []
adamc@39 124 }
adamc@39 125
adamc@177 126 fun debug ({current = FNormal {cons, constructors, vals, strs, funs}, ...} : t) =
adamc@146 127 print ("cons: " ^ Int.toString (SM.numItems cons) ^ "; "
adamc@177 128 ^ "constructors: " ^ Int.toString (SM.numItems constructors) ^ "; "
adamc@146 129 ^ "vals: " ^ Int.toString (SM.numItems vals) ^ "; "
adamc@146 130 ^ "strs: " ^ Int.toString (SM.numItems strs) ^ "; "
adamc@146 131 ^ "funs: " ^ Int.toString (SM.numItems funs) ^ "\n")
adamc@146 132 | debug _ = print "Not normal!\n"
adamc@146 133
adamc@73 134 datatype core_con =
adamc@73 135 CNormal of int
adamc@73 136 | CFfi of string
adamc@48 137
adamc@73 138 datatype core_val =
adamc@73 139 ENormal of int
adamc@73 140 | EFfi of string * L'.con
adamc@73 141
adamc@177 142 fun bindCon {cons, constructors, vals, strs, funs, current, nested} s n =
adamc@39 143 let
adamc@39 144 val n' = alloc ()
adamc@39 145
adamc@39 146 val current =
adamc@48 147 case current of
adamc@48 148 FFfi _ => raise Fail "Binding inside FFfi"
adamc@177 149 | FNormal {cons, constructors, vals, strs, funs} =>
adamc@73 150 FNormal {cons = SM.insert (cons, s, n'),
adamc@177 151 constructors = constructors,
adamc@73 152 vals = vals,
adamc@48 153 strs = strs,
adamc@48 154 funs = funs}
adamc@39 155 in
adamc@73 156 ({cons = IM.insert (cons, n, n'),
adamc@177 157 constructors = constructors,
adamc@73 158 vals = vals,
adamc@39 159 strs = strs,
adamc@46 160 funs = funs,
adamc@39 161 current = current,
adamc@39 162 nested = nested},
adamc@39 163 n')
adamc@39 164 end
adamc@39 165
adamc@73 166 fun lookupConById ({cons, ...} : t) n = IM.find (cons, n)
adamc@39 167
adamc@73 168 fun lookupConByName ({current, ...} : t) x =
adamc@48 169 case current of
adamc@73 170 FFfi {mod = m, ...} => CFfi m
adamc@73 171 | FNormal {cons, ...} =>
adamc@73 172 case SM.find (cons, x) of
adamc@73 173 NONE => raise Fail "Corify.St.lookupConByName"
adamc@73 174 | SOME n => CNormal n
adamc@39 175
adamc@177 176 fun bindVal {cons, constructors, vals, strs, funs, current, nested} s n =
adamc@73 177 let
adamc@73 178 val n' = alloc ()
adamc@73 179
adamc@73 180 val current =
adamc@73 181 case current of
adamc@73 182 FFfi _ => raise Fail "Binding inside FFfi"
adamc@177 183 | FNormal {cons, constructors, vals, strs, funs} =>
adamc@73 184 FNormal {cons = cons,
adamc@177 185 constructors = constructors,
adamc@73 186 vals = SM.insert (vals, s, n'),
adamc@73 187 strs = strs,
adamc@73 188 funs = funs}
adamc@73 189 in
adamc@73 190 ({cons = cons,
adamc@177 191 constructors = constructors,
adamc@73 192 vals = IM.insert (vals, n, n'),
adamc@73 193 strs = strs,
adamc@73 194 funs = funs,
adamc@73 195 current = current,
adamc@73 196 nested = nested},
adamc@73 197 n')
adamc@73 198 end
adamc@73 199
adamc@177 200 fun bindConstructorVal {cons, constructors, vals, strs, funs, current, nested} s n =
adamc@163 201 let
adamc@163 202 val current =
adamc@163 203 case current of
adamc@163 204 FFfi _ => raise Fail "Binding inside FFfi"
adamc@177 205 | FNormal {cons, constructors, vals, strs, funs} =>
adamc@163 206 FNormal {cons = cons,
adamc@177 207 constructors = constructors,
adamc@163 208 vals = SM.insert (vals, s, n),
adamc@163 209 strs = strs,
adamc@163 210 funs = funs}
adamc@163 211 in
adamc@163 212 {cons = cons,
adamc@177 213 constructors = constructors,
adamc@163 214 vals = IM.insert (vals, n, n),
adamc@163 215 strs = strs,
adamc@163 216 funs = funs,
adamc@163 217 current = current,
adamc@163 218 nested = nested}
adamc@163 219 end
adamc@163 220
adamc@177 221
adamc@73 222 fun lookupValById ({vals, ...} : t) n = IM.find (vals, n)
adamc@73 223
adamc@73 224 fun lookupValByName ({current, ...} : t) x =
adamc@73 225 case current of
adamc@73 226 FFfi {mod = m, vals, ...} =>
adamc@73 227 (case SM.find (vals, x) of
adamc@73 228 NONE => raise Fail "Corify.St.lookupValByName: no type for FFI val"
adamc@73 229 | SOME t => EFfi (m, t))
adamc@73 230 | FNormal {vals, ...} =>
adamc@73 231 case SM.find (vals, x) of
adamc@73 232 NONE => raise Fail "Corify.St.lookupValByName"
adamc@73 233 | SOME n => ENormal n
adamc@73 234
adamc@177 235 fun bindConstructor {cons, constructors, vals, strs, funs, current, nested} s n n' =
adamc@177 236 let
adamc@177 237 val current =
adamc@177 238 case current of
adamc@177 239 FFfi _ => raise Fail "Binding inside FFfi"
adamc@177 240 | FNormal {cons, constructors, vals, strs, funs} =>
adamc@177 241 FNormal {cons = cons,
adamc@177 242 constructors = SM.insert (constructors, s, n'),
adamc@177 243 vals = vals,
adamc@177 244 strs = strs,
adamc@177 245 funs = funs}
adamc@177 246 in
adamc@177 247 {cons = cons,
adamc@177 248 constructors = IM.insert (constructors, n, n'),
adamc@177 249 vals = vals,
adamc@177 250 strs = strs,
adamc@177 251 funs = funs,
adamc@177 252 current = current,
adamc@177 253 nested = nested}
adamc@177 254 end
adamc@177 255
adamc@177 256 fun lookupConstructorById ({constructors, ...} : t) n =
adamc@177 257 case IM.find (constructors, n) of
adamc@177 258 NONE => raise Fail "Corify.St.lookupConstructorById"
adamc@177 259 | SOME x => x
adamc@177 260
adamc@186 261 fun lookupConstructorByNameOpt ({current, ...} : t) x =
adamc@186 262 case current of
adamc@186 263 FFfi {mod = m, constructors, ...} =>
adamc@186 264 (case SM.find (constructors, x) of
adamc@186 265 NONE => NONE
adamc@186 266 | SOME (n, to) => SOME (L'.PConFfi {mod = m, datatyp = n, con = x, arg = to}))
adamc@186 267 | FNormal {constructors, ...} =>
adamc@186 268 case SM.find (constructors, x) of
adamc@186 269 NONE => NONE
adamc@186 270 | SOME n => SOME n
adamc@186 271
adamc@177 272 fun lookupConstructorByName ({current, ...} : t) x =
adamc@177 273 case current of
adamc@185 274 FFfi {mod = m, constructors, ...} =>
adamc@185 275 (case SM.find (constructors, x) of
adamc@185 276 NONE => raise Fail "Corify.St.lookupConstructorByName [1]"
adamc@186 277 | SOME (n, to) => L'.PConFfi {mod = m, datatyp = n, con = x, arg = to})
adamc@177 278 | FNormal {constructors, ...} =>
adamc@177 279 case SM.find (constructors, x) of
adamc@185 280 NONE => raise Fail "Corify.St.lookupConstructorByName [2]"
adamc@177 281 | SOME n => n
adamc@177 282
adamc@177 283 fun enter {cons, constructors, vals, strs, funs, current, nested} =
adamc@73 284 {cons = cons,
adamc@177 285 constructors = constructors,
adamc@73 286 vals = vals,
adamc@39 287 strs = strs,
adamc@46 288 funs = funs,
adamc@73 289 current = FNormal {cons = SM.empty,
adamc@177 290 constructors = SM.empty,
adamc@73 291 vals = SM.empty,
adamc@48 292 strs = SM.empty,
adamc@48 293 funs = SM.empty},
adamc@39 294 nested = current :: nested}
adamc@39 295
adamc@73 296 fun dummy f = {cons = IM.empty,
adamc@177 297 constructors = IM.empty,
adamc@73 298 vals = IM.empty,
adamc@39 299 strs = IM.empty,
adamc@46 300 funs = IM.empty,
adamc@39 301 current = f,
adamc@39 302 nested = []}
adamc@39 303
adamc@177 304 fun leave {cons, constructors, vals, strs, funs, current, nested = m1 :: rest} =
adamc@73 305 {outer = {cons = cons,
adamc@177 306 constructors = constructors,
adamc@73 307 vals = vals,
adamc@39 308 strs = strs,
adamc@46 309 funs = funs,
adamc@39 310 current = m1,
adamc@39 311 nested = rest},
adamc@39 312 inner = dummy current}
adamc@39 313 | leave _ = raise Fail "Corify.St.leave"
adamc@39 314
adamc@185 315 fun ffi m vals constructors = dummy (FFfi {mod = m, vals = vals, constructors = constructors})
adamc@48 316
adamc@177 317 fun bindStr ({cons, constructors, vals, strs, funs,
adamc@177 318 current = FNormal {cons = mcons, constructors = mconstructors,
adamc@177 319 vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@46 320 x n ({current = f, ...} : t) =
adamc@73 321 {cons = cons,
adamc@177 322 constructors = constructors,
adamc@73 323 vals = vals,
adamc@39 324 strs = IM.insert (strs, n, f),
adamc@46 325 funs = funs,
adamc@73 326 current = FNormal {cons = mcons,
adamc@177 327 constructors = mconstructors,
adamc@73 328 vals = mvals,
adamc@73 329 strs = SM.insert (mstrs, x, f),
adamc@73 330 funs = mfuns},
adamc@39 331 nested = nested}
adamc@48 332 | bindStr _ _ _ _ = raise Fail "Corify.St.bindStr"
adamc@39 333
adamc@39 334 fun lookupStrById ({strs, ...} : t) n =
adamc@39 335 case IM.find (strs, n) of
adamc@46 336 NONE => raise Fail "Corify.St.lookupStrById"
adamc@39 337 | SOME f => dummy f
adamc@39 338
adamc@48 339 fun lookupStrByName (m, {current = FNormal {strs, ...}, ...} : t) =
adamc@48 340 (case SM.find (strs, m) of
adamc@48 341 NONE => raise Fail "Corify.St.lookupStrByName"
adamc@48 342 | SOME f => dummy f)
adamc@48 343 | lookupStrByName _ = raise Fail "Corify.St.lookupStrByName"
adamc@39 344
adamc@177 345 fun bindFunctor ({cons, constructors, vals, strs, funs,
adamc@177 346 current = FNormal {cons = mcons, constructors = mconstructors,
adamc@177 347 vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@146 348 x n xa na str =
adamc@73 349 {cons = cons,
adamc@177 350 constructors = constructors,
adamc@73 351 vals = vals,
adamc@46 352 strs = strs,
adamc@146 353 funs = IM.insert (funs, n, (xa, na, str)),
adamc@73 354 current = FNormal {cons = mcons,
adamc@177 355 constructors = mconstructors,
adamc@73 356 vals = mvals,
adamc@48 357 strs = mstrs,
adamc@146 358 funs = SM.insert (mfuns, x, (xa, na, str))},
adamc@46 359 nested = nested}
adamc@146 360 | bindFunctor _ _ _ _ _ _ = raise Fail "Corify.St.bindFunctor"
adamc@46 361
adamc@46 362 fun lookupFunctorById ({funs, ...} : t) n =
adamc@46 363 case IM.find (funs, n) of
adamc@46 364 NONE => raise Fail "Corify.St.lookupFunctorById"
adamc@46 365 | SOME v => v
adamc@46 366
adamc@48 367 fun lookupFunctorByName (m, {current = FNormal {funs, ...}, ...} : t) =
adamc@48 368 (case SM.find (funs, m) of
adamc@48 369 NONE => raise Fail "Corify.St.lookupFunctorByName"
adamc@48 370 | SOME v => v)
adamc@48 371 | lookupFunctorByName _ = raise Fail "Corify.St.lookupFunctorByName"
adamc@46 372
adamc@39 373 end
adamc@39 374
adamc@39 375
adamc@16 376 fun corifyKind (k, loc) =
adamc@16 377 case k of
adamc@16 378 L.KType => (L'.KType, loc)
adamc@16 379 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
adamc@16 380 | L.KName => (L'.KName, loc)
adamc@16 381 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
adamc@87 382 | L.KUnit => (L'.KUnit, loc)
adamc@16 383
adamc@39 384 fun corifyCon st (c, loc) =
adamc@16 385 case c of
adamc@39 386 L.TFun (t1, t2) => (L'.TFun (corifyCon st t1, corifyCon st t2), loc)
adamc@39 387 | L.TCFun (x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon st t), loc)
adamc@39 388 | L.TRecord c => (L'.TRecord (corifyCon st c), loc)
adamc@16 389
adamc@16 390 | L.CRel n => (L'.CRel n, loc)
adamc@39 391 | L.CNamed n =>
adamc@73 392 (case St.lookupConById st n of
adamc@39 393 NONE => (L'.CNamed n, loc)
adamc@39 394 | SOME n => (L'.CNamed n, loc))
adamc@39 395 | L.CModProj (m, ms, x) =>
adamc@39 396 let
adamc@39 397 val st = St.lookupStrById st m
adamc@39 398 val st = foldl St.lookupStrByName st ms
adamc@39 399 in
adamc@73 400 case St.lookupConByName st x of
adamc@73 401 St.CNormal n => (L'.CNamed n, loc)
adamc@73 402 | St.CFfi m => (L'.CFfi (m, x), loc)
adamc@39 403 end
adamc@34 404
adamc@39 405 | L.CApp (c1, c2) => (L'.CApp (corifyCon st c1, corifyCon st c2), loc)
adamc@39 406 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon st c), loc)
adamc@16 407
adamc@16 408 | L.CName s => (L'.CName s, loc)
adamc@16 409
adamc@39 410 | L.CRecord (k, xcs) =>
adamc@39 411 (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon st c1, corifyCon st c2)) xcs), loc)
adamc@39 412 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon st c1, corifyCon st c2), loc)
adamc@69 413 | L.CFold (k1, k2) => (L'.CFold (corifyKind k1, corifyKind k2), loc)
adamc@87 414 | L.CUnit => (L'.CUnit, loc)
adamc@16 415
adamc@177 416 fun corifyPatCon st pc =
adamc@177 417 case pc of
adamc@177 418 L.PConVar n => St.lookupConstructorById st n
adamc@177 419 | L.PConProj (m1, ms, x) =>
adamc@177 420 let
adamc@177 421 val st = St.lookupStrById st m1
adamc@177 422 val st = foldl St.lookupStrByName st ms
adamc@177 423 in
adamc@177 424 St.lookupConstructorByName st x
adamc@177 425 end
adamc@177 426
adamc@177 427 fun corifyPat st (p, loc) =
adamc@177 428 case p of
adamc@177 429 L.PWild => (L'.PWild, loc)
adamc@182 430 | L.PVar (x, t) => (L'.PVar (x, corifyCon st t), loc)
adamc@177 431 | L.PPrim p => (L'.PPrim p, loc)
adamc@177 432 | L.PCon (pc, po) => (L'.PCon (corifyPatCon st pc, Option.map (corifyPat st) po), loc)
adamc@182 433 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, corifyPat st p, corifyCon st t)) xps), loc)
adamc@177 434
adamc@39 435 fun corifyExp st (e, loc) =
adamc@16 436 case e of
adamc@16 437 L.EPrim p => (L'.EPrim p, loc)
adamc@16 438 | L.ERel n => (L'.ERel n, loc)
adamc@39 439 | L.ENamed n =>
adamc@73 440 (case St.lookupValById st n of
adamc@39 441 NONE => (L'.ENamed n, loc)
adamc@39 442 | SOME n => (L'.ENamed n, loc))
adamc@39 443 | L.EModProj (m, ms, x) =>
adamc@39 444 let
adamc@39 445 val st = St.lookupStrById st m
adamc@39 446 val st = foldl St.lookupStrByName st ms
adamc@39 447 in
adamc@186 448 case St.lookupConstructorByNameOpt st x of
adamc@186 449 SOME (pc as L'.PConFfi {mod = m, datatyp, arg, ...}) =>
adamc@186 450 (case arg of
adamc@186 451 NONE => (L'.ECon (pc, NONE), loc)
adamc@186 452 | SOME dom => (L'.EAbs ("x", dom, (L'.CFfi (m, datatyp), loc),
adamc@186 453 (L'.ECon (pc, SOME (L'.ERel 0, loc)), loc)), loc))
adamc@186 454 | _ =>
adamc@186 455 case St.lookupValByName st x of
adamc@186 456 St.ENormal n => (L'.ENamed n, loc)
adamc@186 457 | St.EFfi (m, t) =>
adamc@186 458 case t of
adamc@186 459 (L'.TFun (dom as (L'.TRecord (L'.CRecord (_, []), _), _), ran), _) =>
adamc@186 460 (L'.EAbs ("arg", dom, ran, (L'.EFfiApp (m, x, []), loc)), loc)
adamc@186 461 | t as (L'.TFun _, _) =>
adamc@186 462 let
adamc@186 463 fun getArgs (all as (t, _), args) =
adamc@186 464 case t of
adamc@186 465 L'.TFun (dom, ran) => getArgs (ran, dom :: args)
adamc@186 466 | _ => (all, rev args)
adamc@186 467
adamc@186 468 val (result, args) = getArgs (t, [])
adamc@49 469
adamc@186 470 val (actuals, _) = foldr (fn (_, (actuals, n)) =>
adamc@186 471 ((L'.ERel n, loc) :: actuals,
adamc@186 472 n + 1)) ([], 0) args
adamc@186 473 val app = (L'.EFfiApp (m, x, actuals), loc)
adamc@186 474 val (abs, _, _) = foldr (fn (t, (abs, ran, n)) =>
adamc@186 475 ((L'.EAbs ("arg" ^ Int.toString n,
adamc@186 476 t,
adamc@186 477 ran,
adamc@186 478 abs), loc),
adamc@186 479 (L'.TFun (t, ran), loc),
adamc@186 480 n - 1)) (app, result, length args - 1) args
adamc@186 481 in
adamc@186 482 abs
adamc@186 483 end
adamc@186 484 | _ => (L'.EFfi (m, x), loc)
adamc@39 485 end
adamc@39 486 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
adamc@39 487 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
adamc@39 488 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
adamc@39 489 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
adamc@16 490
adamc@110 491 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) =>
adamc@110 492 (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
adamc@39 493 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
adamc@39 494 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@149 495 | L.ECut (e1, c, {field, rest}) => (L'.ECut (corifyExp st e1, corifyCon st c,
adamc@149 496 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@73 497 | L.EFold k => (L'.EFold (corifyKind k), loc)
adamc@177 498
adamc@182 499 | L.ECase (e, pes, {disc, result}) =>
adamc@182 500 (L'.ECase (corifyExp st e,
adamc@182 501 map (fn (p, e) => (corifyPat st p, corifyExp st e)) pes,
adamc@182 502 {disc = corifyCon st disc, result = corifyCon st result}),
adamc@182 503 loc)
adamc@177 504
adamc@109 505 | L.EWrite e => (L'.EWrite (corifyExp st e), loc)
adamc@16 506
adamc@39 507 fun corifyDecl ((d, loc : EM.span), st) =
adamc@39 508 case d of
adamc@39 509 L.DCon (x, n, k, c) =>
adamc@39 510 let
adamc@73 511 val (st, n) = St.bindCon st x n
adamc@39 512 in
adamc@39 513 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
adamc@39 514 end
adamc@163 515 | L.DDatatype (x, n, xncs) =>
adamc@163 516 let
adamc@163 517 val (st, n) = St.bindCon st x n
adamc@163 518 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@163 519 let
adamc@177 520 val st = St.bindConstructor st x n (L'.PConVar n)
adamc@177 521 val st = St.bindConstructorVal st x n
adamc@163 522 val co = Option.map (corifyCon st) co
adamc@163 523 in
adamc@163 524 ((x, n, co), st)
adamc@163 525 end) st xncs
adamc@177 526
adamc@177 527 val t = (L'.CNamed n, loc)
adamc@177 528 val dcons = map (fn (x, n, to) =>
adamc@177 529 let
adamc@177 530 val (e, t) =
adamc@177 531 case to of
adamc@185 532 NONE => ((L'.ECon (L'.PConVar n, NONE), loc), t)
adamc@177 533 | SOME t' => ((L'.EAbs ("x", t', t,
adamc@185 534 (L'.ECon (L'.PConVar n, SOME (L'.ERel 0, loc)), loc)),
adamc@177 535 loc),
adamc@177 536 (L'.TFun (t', t), loc))
adamc@177 537 in
adamc@177 538 (L'.DVal (x, n, t, e, ""), loc)
adamc@177 539 end) xncs
adamc@163 540 in
adamc@177 541 ((L'.DDatatype (x, n, xncs), loc) :: dcons, st)
adamc@163 542 end
adamc@163 543 | L.DDatatypeImp (x, n, m1, ms, s, xncs) =>
adamc@163 544 let
adamc@163 545 val (st, n) = St.bindCon st x n
adamc@163 546 val c = corifyCon st (L.CModProj (m1, ms, s), loc)
adamc@163 547
adamc@177 548 val m = foldl (fn (x, m) => (L.StrProj (m, x), loc)) (L.StrVar m1, loc) ms
adamc@177 549 val (_, {inner, ...}) = corifyStr (m, st)
adamc@177 550
adamc@163 551 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@163 552 let
adamc@177 553 val n' = St.lookupConstructorByName inner x
adamc@177 554 val st = St.bindConstructor st x n n'
adamc@163 555 val (st, n) = St.bindVal st x n
adamc@163 556 val co = Option.map (corifyCon st) co
adamc@163 557 in
adamc@163 558 ((x, n, co), st)
adamc@163 559 end) st xncs
adamc@177 560
adamc@163 561 val cds = map (fn (x, n, co) =>
adamc@163 562 let
adamc@163 563 val t = case co of
adamc@163 564 NONE => c
adamc@163 565 | SOME t' => (L'.TFun (t', c), loc)
adamc@163 566 val e = corifyExp st (L.EModProj (m1, ms, x), loc)
adamc@163 567 in
adamc@163 568 (L'.DVal (x, n, t, e, x), loc)
adamc@163 569 end) xncs
adamc@163 570 in
adamc@163 571 ((L'.DCon (x, n, (L'.KType, loc), c), loc) :: cds, st)
adamc@163 572 end
adamc@39 573 | L.DVal (x, n, t, e) =>
adamc@39 574 let
adamc@73 575 val (st, n) = St.bindVal st x n
adamc@111 576 val s =
adamc@111 577 if String.isPrefix "wrap_" x then
adamc@111 578 String.extract (x, 5, NONE)
adamc@111 579 else
adamc@111 580 x
adamc@39 581 in
adamc@111 582 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e, s), loc)], st)
adamc@39 583 end
adamc@125 584 | L.DValRec vis =>
adamc@125 585 let
adamc@125 586 val (vis, st) = ListUtil.foldlMap
adamc@130 587 (fn ((x, n, t, e), st) =>
adamc@130 588 let
adamc@130 589 val (st, n) = St.bindVal st x n
adamc@130 590 in
adamc@130 591 ((x, n, t, e), st)
adamc@130 592 end)
adamc@130 593 st vis
adamc@130 594
adamc@130 595 val vis = map
adamc@130 596 (fn (x, n, t, e) =>
adamc@130 597 let
adamc@130 598 val s =
adamc@130 599 if String.isPrefix "wrap_" x then
adamc@130 600 String.extract (x, 5, NONE)
adamc@130 601 else
adamc@130 602 x
adamc@130 603 in
adamc@130 604 (x, n, corifyCon st t, corifyExp st e, s)
adamc@130 605 end)
adamc@130 606 vis
adamc@125 607 in
adamc@125 608 ([(L'.DValRec vis, loc)], st)
adamc@125 609 end
adamc@39 610 | L.DSgn _ => ([], st)
adamc@16 611
adamc@146 612 | L.DStr (x, n, _, (L.StrFun (xa, na, _, _, str), _)) =>
adamc@146 613 ([], St.bindFunctor st x n xa na str)
adamc@46 614
adamc@39 615 | L.DStr (x, n, _, str) =>
adamc@39 616 let
adamc@39 617 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 618 val st = St.bindStr outer x n inner
adamc@39 619 in
adamc@39 620 (ds, st)
adamc@39 621 end
adamc@16 622
adamc@49 623 | L.DFfiStr (m, n, (sgn, _)) =>
adamc@49 624 (case sgn of
adamc@49 625 L.SgnConst sgis =>
adamc@49 626 let
adamc@185 627 val (ds, cmap, conmap, st) =
adamc@185 628 foldl (fn ((sgi, _), (ds, cmap, conmap, st)) =>
adamc@49 629 case sgi of
adamc@49 630 L.SgiConAbs (x, n, k) =>
adamc@49 631 let
adamc@73 632 val (st, n') = St.bindCon st x n
adamc@49 633 in
adamc@49 634 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 635 cmap,
adamc@185 636 conmap,
adamc@49 637 st)
adamc@49 638 end
adamc@49 639 | L.SgiCon (x, n, k, _) =>
adamc@49 640 let
adamc@73 641 val (st, n') = St.bindCon st x n
adamc@49 642 in
adamc@49 643 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 644 cmap,
adamc@185 645 conmap,
adamc@185 646 st)
adamc@185 647 end
adamc@185 648
adamc@185 649 | L.SgiDatatype (x, n, xnts) =>
adamc@185 650 let
adamc@185 651 val (st, n') = St.bindCon st x n
adamc@186 652 val (xnts, (ds', st, cmap, conmap)) =
adamc@185 653 ListUtil.foldlMap
adamc@186 654 (fn ((x', n, to), (ds', st, cmap, conmap)) =>
adamc@185 655 let
adamc@185 656 val dt = (L'.CNamed n', loc)
adamc@185 657
adamc@186 658 val to = Option.map (corifyCon st) to
adamc@186 659
adamc@186 660 val pc = L'.PConFfi {mod = m,
adamc@186 661 datatyp = x,
adamc@186 662 con = x',
adamc@186 663 arg = to}
adamc@186 664
adamc@186 665 val (cmap, d) =
adamc@185 666 case to of
adamc@186 667 NONE => (SM.insert (cmap, x', dt),
adamc@186 668 (L'.DVal (x', n, dt,
adamc@186 669 (L'.ECon (pc, NONE), loc),
adamc@186 670 ""), loc))
adamc@185 671 | SOME t =>
adamc@185 672 let
adamc@186 673 val tf = (L'.TFun (t, dt), loc)
adamc@186 674 val d = (L'.DVal (x', n, tf,
adamc@186 675 (L'.EAbs ("x", t, tf,
adamc@186 676 (L'.ECon (pc,
adamc@186 677 SOME (L'.ERel 0,
adamc@186 678 loc)),
adamc@186 679 loc)), loc), ""), loc)
adamc@185 680 in
adamc@186 681 (SM.insert (cmap, x', tf), d)
adamc@185 682 end
adamc@185 683
adamc@186 684 val st = St.bindConstructor st x' n pc
adamc@186 685 (*val st = St.bindConstructorVal st x' n*)
adamc@186 686
adamc@186 687 val conmap = SM.insert (conmap, x', (x, to))
adamc@185 688 in
adamc@185 689 ((x', n, to),
adamc@186 690 (d :: ds', st, cmap, conmap))
adamc@186 691 end) ([], st, cmap, conmap) xnts
adamc@185 692 in
adamc@186 693 (ds' @ (L'.DDatatype (x, n', xnts), loc) :: ds,
adamc@185 694 cmap,
adamc@185 695 conmap,
adamc@49 696 st)
adamc@49 697 end
adamc@49 698
adamc@49 699 | L.SgiVal (x, _, c) =>
adamc@49 700 (ds,
adamc@49 701 SM.insert (cmap, x, corifyCon st c),
adamc@185 702 conmap,
adamc@49 703 st)
adamc@185 704 | _ => (ds, cmap, conmap, st)) ([], SM.empty, SM.empty, st) sgis
adamc@49 705
adamc@185 706 val st = St.bindStr st m n (St.ffi m cmap conmap)
adamc@49 707 in
adamc@49 708 (rev ds, st)
adamc@49 709 end
adamc@49 710 | _ => raise Fail "Non-const signature for FFI structure")
adamc@48 711
adamc@109 712 | L.DExport (en, sgn, str) =>
adamc@109 713 (case #1 sgn of
adamc@109 714 L.SgnConst sgis =>
adamc@109 715 let
adamc@109 716 fun pathify (str, _) =
adamc@109 717 case str of
adamc@109 718 L.StrVar m => SOME (m, [])
adamc@109 719 | L.StrProj (str, s) =>
adamc@109 720 Option.map (fn (m, ms) => (m, ms @ [s])) (pathify str)
adamc@109 721 | _ => NONE
adamc@109 722 in
adamc@109 723 case pathify str of
adamc@109 724 NONE => (ErrorMsg.errorAt loc "Structure is too fancy to export";
adamc@109 725 ([], st))
adamc@109 726 | SOME (m, ms) =>
adamc@109 727 let
adamc@109 728 fun wrapSgi ((sgi, _), (wds, eds)) =
adamc@109 729 case sgi of
adamc@109 730 L.SgiVal (s, _, t as (L.TFun (dom, ran), _)) =>
adamc@109 731 (case (#1 dom, #1 ran) of
adamc@147 732 (L.TRecord (L.CRecord (_, []), _),
adamc@139 733 L.CApp
adamc@139 734 ((L.CApp
adamc@139 735 ((L.CApp ((L.CModProj (_, [], "xml"), _),
adamc@139 736 (L.CRecord (_, [((L.CName "Html", _),
adamc@139 737 _)]), _)), _), _), _), _)) =>
adamc@109 738 let
adamc@109 739 val ran = (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc)
adamc@109 740 val e = (L.EModProj (m, ms, s), loc)
adamc@109 741 val e = (L.EAbs ("vs", dom, ran,
adamc@109 742 (L.EWrite (L.EApp (e, (L.ERel 0, loc)), loc), loc)), loc)
adamc@109 743 in
adamc@109 744 ((L.DVal ("wrap_" ^ s, 0,
adamc@109 745 (L.TFun (dom, ran), loc),
adamc@109 746 e), loc) :: wds,
adamc@109 747 (fn st =>
adamc@109 748 case #1 (corifyExp st (L.EModProj (en, [], "wrap_" ^ s), loc)) of
adamc@144 749 L'.ENamed n => (L'.DExport (L'.Link, n), loc)
adamc@109 750 | _ => raise Fail "Corify: Value to export didn't corify properly")
adamc@109 751 :: eds)
adamc@109 752 end
adamc@109 753 | _ => (wds, eds))
adamc@109 754 | _ => (wds, eds)
adamc@102 755
adamc@109 756 val (wds, eds) = foldl wrapSgi ([], []) sgis
adamc@109 757 val wrapper = (L.StrConst wds, loc)
adamc@109 758 val (ds, {inner, outer}) = corifyStr (wrapper, st)
adamc@109 759 val st = St.bindStr outer "wrapper" en inner
adamc@109 760
adamc@109 761 val ds = ds @ map (fn f => f st) eds
adamc@109 762 in
adamc@109 763 (ds, st)
adamc@109 764 end
adamc@109 765 end
adamc@109 766 | _ => raise Fail "Non-const signature for 'export'")
adamc@48 767
adamc@39 768 and corifyStr ((str, _), st) =
adamc@39 769 case str of
adamc@39 770 L.StrConst ds =>
adamc@39 771 let
adamc@39 772 val st = St.enter st
adamc@39 773 val (ds, st) = ListUtil.foldlMapConcat corifyDecl st ds
adamc@39 774 in
adamc@39 775 (ds, St.leave st)
adamc@39 776 end
adamc@39 777 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
adamc@39 778 | L.StrProj (str, x) =>
adamc@39 779 let
adamc@39 780 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 781 in
adamc@39 782 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
adamc@39 783 end
adamc@46 784 | L.StrFun _ => raise Fail "Corify of nested functor definition"
adamc@46 785 | L.StrApp (str1, str2) =>
adamc@46 786 let
adamc@46 787 fun unwind' (str, _) =
adamc@46 788 case str of
adamc@46 789 L.StrVar n => St.lookupStrById st n
adamc@46 790 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
adamc@46 791 | _ => raise Fail "Corify of fancy functor application [1]"
adamc@46 792
adamc@46 793 fun unwind (str, _) =
adamc@46 794 case str of
adamc@46 795 L.StrVar n => St.lookupFunctorById st n
adamc@46 796 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
adamc@46 797 | _ => raise Fail "Corify of fancy functor application [2]"
adamc@46 798
adamc@146 799 val (xa, na, body) = unwind str1
adamc@46 800
adamc@146 801 val (ds1, {inner = inner', outer}) = corifyStr (str2, st)
adamc@146 802 val (ds2, {inner, outer}) = corifyStr (body, St.bindStr outer xa na inner')
adamc@46 803 in
adamc@146 804 (ds1 @ ds2, {inner = St.bindStr inner xa na inner', outer = outer})
adamc@46 805 end
adamc@31 806
adamc@39 807 fun maxName ds = foldl (fn ((d, _), n) =>
adamc@39 808 case d of
adamc@39 809 L.DCon (_, n', _, _) => Int.max (n, n')
adamc@162 810 | L.DDatatype (_, n', _) => Int.max (n, n')
adamc@162 811 | L.DDatatypeImp (_, n', _, _, _, _) => Int.max (n, n')
adamc@124 812 | L.DVal (_, n', _, _) => Int.max (n, n')
adamc@124 813 | L.DValRec vis => foldl (fn ((_, n', _, _), n) => Int.max (n, n)) n vis
adamc@39 814 | L.DSgn (_, n', _) => Int.max (n, n')
adamc@48 815 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
adamc@100 816 | L.DFfiStr (_, n', _) => Int.max (n, n')
adamc@109 817 | L.DExport _ => n)
adamc@39 818 0 ds
adamc@39 819
adamc@39 820 and maxNameStr (str, _) =
adamc@39 821 case str of
adamc@39 822 L.StrConst ds => maxName ds
adamc@39 823 | L.StrVar n => n
adamc@39 824 | L.StrProj (str, _) => maxNameStr str
adamc@45 825 | L.StrFun (_, _, _, _, str) => maxNameStr str
adamc@45 826 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@39 827
adamc@39 828 fun corify ds =
adamc@39 829 let
adamc@39 830 val () = reset (maxName ds + 1)
adamc@146 831
adamc@39 832 val (ds, _) = ListUtil.foldlMapConcat corifyDecl St.empty ds
adamc@39 833 in
adamc@39 834 ds
adamc@39 835 end
adamc@16 836
adamc@16 837 end