annotate src/corify.sml @ 193:8a70e2919e86

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