annotate src/corify.sml @ 191:aa54250f58ac

Parametrized datatypes through explify
author Adam Chlipala <adamc@hcoop.net>
date Fri, 08 Aug 2008 10:28:32 -0400
parents 8e9f97508f0d
children 9bbf4d383381
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@188 65 val ffi : string -> L'.con SM.map -> (string * 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@188 104 constructors : (string * 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@188 266 | SOME (n, to, dk) => SOME (L'.PConFfi {mod = m, datatyp = n, 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@188 277 | SOME (n, to, dk) => L'.PConFfi {mod = m, datatyp = n, 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@191 432 | L.PCon (dk, pc, ts, po) => raise Fail "Corify PCon" (*(L'.PCon (dk, corifyPatCon st pc, Option.map (corifyPat st) po), loc)*)
adamc@188 433 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, corifyPat st p, corifyCon st t)) xps), loc)
adamc@39 434
adamc@188 435 fun corifyExp st (e, loc) =
adamc@188 436 case e of
adamc@188 437 L.EPrim p => (L'.EPrim p, loc)
adamc@188 438 | L.ERel n => (L'.ERel n, loc)
adamc@188 439 | L.ENamed n =>
adamc@188 440 (case St.lookupValById st n of
adamc@188 441 NONE => (L'.ENamed n, loc)
adamc@188 442 | SOME n => (L'.ENamed n, loc))
adamc@188 443 | L.EModProj (m, ms, x) =>
adamc@188 444 let
adamc@188 445 val st = St.lookupStrById st m
adamc@188 446 val st = foldl St.lookupStrByName st ms
adamc@188 447 in
adamc@188 448 case St.lookupConstructorByNameOpt st x of
adamc@188 449 SOME (pc as L'.PConFfi {mod = m, datatyp, arg, kind, ...}) =>
adamc@188 450 (case arg of
adamc@188 451 NONE => (L'.ECon (kind, pc, NONE), loc)
adamc@188 452 | SOME dom => (L'.EAbs ("x", dom, (L'.CFfi (m, datatyp), loc),
adamc@188 453 (L'.ECon (kind, pc, SOME (L'.ERel 0, loc)), loc)), loc))
adamc@188 454 | _ =>
adamc@188 455 case St.lookupValByName st x of
adamc@188 456 St.ENormal n => (L'.ENamed n, loc)
adamc@188 457 | St.EFfi (m, t) =>
adamc@188 458 case t of
adamc@188 459 (L'.TFun (dom as (L'.TRecord (L'.CRecord (_, []), _), _), ran), _) =>
adamc@188 460 (L'.EAbs ("arg", dom, ran, (L'.EFfiApp (m, x, []), loc)), loc)
adamc@188 461 | t as (L'.TFun _, _) =>
adamc@188 462 let
adamc@188 463 fun getArgs (all as (t, _), args) =
adamc@188 464 case t of
adamc@188 465 L'.TFun (dom, ran) => getArgs (ran, dom :: args)
adamc@188 466 | _ => (all, rev args)
adamc@39 467
adamc@188 468 val (result, args) = getArgs (t, [])
adamc@16 469
adamc@188 470 val (actuals, _) = foldr (fn (_, (actuals, n)) =>
adamc@188 471 ((L'.ERel n, loc) :: actuals,
adamc@188 472 n + 1)) ([], 0) args
adamc@188 473 val app = (L'.EFfiApp (m, x, actuals), loc)
adamc@188 474 val (abs, _, _) = foldr (fn (t, (abs, ran, n)) =>
adamc@188 475 ((L'.EAbs ("arg" ^ Int.toString n,
adamc@188 476 t,
adamc@188 477 ran,
adamc@188 478 abs), loc),
adamc@188 479 (L'.TFun (t, ran), loc),
adamc@188 480 n - 1)) (app, result, length args - 1) args
adamc@188 481 in
adamc@188 482 abs
adamc@188 483 end
adamc@188 484 | _ => (L'.EFfi (m, x), loc)
adamc@188 485 end
adamc@188 486 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
adamc@188 487 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
adamc@188 488 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
adamc@188 489 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
adamc@16 490
adamc@188 491 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) =>
adamc@188 492 (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
adamc@188 493 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
adamc@188 494 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@188 495 | L.ECut (e1, c, {field, rest}) => (L'.ECut (corifyExp st e1, corifyCon st c,
adamc@188 496 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@188 497 | L.EFold k => (L'.EFold (corifyKind k), loc)
adamc@34 498
adamc@188 499 | L.ECase (e, pes, {disc, result}) =>
adamc@188 500 (L'.ECase (corifyExp st e,
adamc@188 501 map (fn (p, e) => (corifyPat st p, corifyExp st e)) pes,
adamc@188 502 {disc = corifyCon st disc, result = corifyCon st result}),
adamc@188 503 loc)
adamc@16 504
adamc@188 505 | L.EWrite e => (L'.EWrite (corifyExp st e), loc)
adamc@16 506
adamc@188 507 fun corifyDecl ((d, loc : EM.span), st) =
adamc@188 508 case d of
adamc@188 509 L.DCon (x, n, k, c) =>
adamc@188 510 let
adamc@188 511 val (st, n) = St.bindCon st x n
adamc@188 512 in
adamc@188 513 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
adamc@188 514 end
adamc@191 515 | L.DDatatype (x, n, xs, xncs) => raise Fail "Corify DDatatype"
adamc@191 516 (*let
adamc@188 517 val (st, n) = St.bindCon st x n
adamc@188 518 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@188 519 let
adamc@188 520 val st = St.bindConstructor st x n (L'.PConVar n)
adamc@188 521 val st = St.bindConstructorVal st x n
adamc@188 522 val co = Option.map (corifyCon st) co
adamc@188 523 in
adamc@188 524 ((x, n, co), st)
adamc@188 525 end) st xncs
adamc@16 526
adamc@188 527 val dk = CoreUtil.classifyDatatype xncs
adamc@188 528 val t = (L'.CNamed n, loc)
adamc@188 529 val dcons = map (fn (x, n, to) =>
adamc@188 530 let
adamc@188 531 val (e, t) =
adamc@188 532 case to of
adamc@188 533 NONE => ((L'.ECon (dk, L'.PConVar n, NONE), loc), t)
adamc@188 534 | SOME t' => ((L'.EAbs ("x", t', t,
adamc@188 535 (L'.ECon (dk, L'.PConVar n, SOME (L'.ERel 0, loc)),
adamc@188 536 loc)),
adamc@188 537 loc),
adamc@188 538 (L'.TFun (t', t), loc))
adamc@188 539 in
adamc@188 540 (L'.DVal (x, n, t, e, ""), loc)
adamc@188 541 end) xncs
adamc@188 542 in
adamc@188 543 ((L'.DDatatype (x, n, xncs), loc) :: dcons, st)
adamc@191 544 end*)
adamc@191 545 | L.DDatatypeImp (x, n, m1, ms, s, xs, xncs) => raise Fail "Corify DDatatypeImp"
adamc@191 546 (*let
adamc@188 547 val (st, n) = St.bindCon st x n
adamc@188 548 val c = corifyCon st (L.CModProj (m1, ms, s), loc)
adamc@177 549
adamc@188 550 val m = foldl (fn (x, m) => (L.StrProj (m, x), loc)) (L.StrVar m1, loc) ms
adamc@188 551 val (_, {inner, ...}) = corifyStr (m, st)
adamc@177 552
adamc@188 553 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@188 554 let
adamc@188 555 val n' = St.lookupConstructorByName inner x
adamc@188 556 val st = St.bindConstructor st x n n'
adamc@188 557 val (st, n) = St.bindVal st x n
adamc@188 558 val co = Option.map (corifyCon st) co
adamc@188 559 in
adamc@188 560 ((x, n, co), st)
adamc@188 561 end) st xncs
adamc@49 562
adamc@188 563 val cds = map (fn (x, n, co) =>
adamc@188 564 let
adamc@188 565 val t = case co of
adamc@188 566 NONE => c
adamc@188 567 | SOME t' => (L'.TFun (t', c), loc)
adamc@188 568 val e = corifyExp st (L.EModProj (m1, ms, x), loc)
adamc@188 569 in
adamc@188 570 (L'.DVal (x, n, t, e, x), loc)
adamc@188 571 end) xncs
adamc@188 572 in
adamc@188 573 ((L'.DCon (x, n, (L'.KType, loc), c), loc) :: cds, st)
adamc@191 574 end*)
adamc@188 575 | L.DVal (x, n, t, e) =>
adamc@188 576 let
adamc@188 577 val (st, n) = St.bindVal st x n
adamc@188 578 val s =
adamc@188 579 if String.isPrefix "wrap_" x then
adamc@188 580 String.extract (x, 5, NONE)
adamc@188 581 else
adamc@188 582 x
adamc@188 583 in
adamc@188 584 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e, s), loc)], st)
adamc@188 585 end
adamc@188 586 | L.DValRec vis =>
adamc@188 587 let
adamc@188 588 val (vis, st) = ListUtil.foldlMap
adamc@188 589 (fn ((x, n, t, e), st) =>
adamc@188 590 let
adamc@188 591 val (st, n) = St.bindVal st x n
adamc@188 592 in
adamc@188 593 ((x, n, t, e), st)
adamc@188 594 end)
adamc@188 595 st vis
adamc@16 596
adamc@188 597 val vis = map
adamc@188 598 (fn (x, n, t, e) =>
adamc@188 599 let
adamc@188 600 val s =
adamc@188 601 if String.isPrefix "wrap_" x then
adamc@188 602 String.extract (x, 5, NONE)
adamc@188 603 else
adamc@188 604 x
adamc@188 605 in
adamc@188 606 (x, n, corifyCon st t, corifyExp st e, s)
adamc@188 607 end)
adamc@188 608 vis
adamc@188 609 in
adamc@188 610 ([(L'.DValRec vis, loc)], st)
adamc@188 611 end
adamc@188 612 | L.DSgn _ => ([], st)
adamc@177 613
adamc@188 614 | L.DStr (x, n, _, (L.StrFun (xa, na, _, _, str), _)) =>
adamc@188 615 ([], St.bindFunctor st x n xa na str)
adamc@177 616
adamc@188 617 | L.DStr (x, n, _, str) =>
adamc@188 618 let
adamc@188 619 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@188 620 val st = St.bindStr outer x n inner
adamc@188 621 in
adamc@188 622 (ds, st)
adamc@188 623 end
adamc@16 624
adamc@188 625 | L.DFfiStr (m, n, (sgn, _)) =>
adamc@188 626 (case sgn of
adamc@188 627 L.SgnConst sgis =>
adamc@188 628 let
adamc@188 629 val (ds, cmap, conmap, st) =
adamc@188 630 foldl (fn ((sgi, _), (ds, cmap, conmap, st)) =>
adamc@188 631 case sgi of
adamc@188 632 L.SgiConAbs (x, n, k) =>
adamc@188 633 let
adamc@188 634 val (st, n') = St.bindCon st x n
adamc@188 635 in
adamc@188 636 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@188 637 cmap,
adamc@188 638 conmap,
adamc@188 639 st)
adamc@188 640 end
adamc@188 641 | L.SgiCon (x, n, k, _) =>
adamc@188 642 let
adamc@188 643 val (st, n') = St.bindCon st x n
adamc@188 644 in
adamc@188 645 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@188 646 cmap,
adamc@188 647 conmap,
adamc@188 648 st)
adamc@188 649 end
adamc@177 650
adamc@191 651 | L.SgiDatatype (x, n, xs, xnts) => raise Fail "Corify FFI SgiDatatype"
adamc@191 652 (*let
adamc@188 653 val dk = ExplUtil.classifyDatatype xnts
adamc@188 654 val (st, n') = St.bindCon st x n
adamc@188 655 val (xnts, (ds', st, cmap, conmap)) =
adamc@188 656 ListUtil.foldlMap
adamc@188 657 (fn ((x', n, to), (ds', st, cmap, conmap)) =>
adamc@188 658 let
adamc@188 659 val dt = (L'.CNamed n', loc)
adamc@163 660
adamc@188 661 val to = Option.map (corifyCon st) to
adamc@177 662
adamc@188 663 val pc = L'.PConFfi {mod = m,
adamc@188 664 datatyp = x,
adamc@188 665 con = x',
adamc@188 666 arg = to,
adamc@188 667 kind = dk}
adamc@130 668
adamc@188 669 val (cmap, d) =
adamc@188 670 case to of
adamc@188 671 NONE => (SM.insert (cmap, x', dt),
adamc@188 672 (L'.DVal (x', n, dt,
adamc@188 673 (L'.ECon (dk, pc, NONE), loc),
adamc@188 674 ""), loc))
adamc@188 675 | SOME t =>
adamc@188 676 let
adamc@188 677 val tf = (L'.TFun (t, dt), loc)
adamc@188 678 val d = (L'.DVal (x', n, tf,
adamc@188 679 (L'.EAbs ("x", t, tf,
adamc@188 680 (L'.ECon (dk, pc,
adamc@188 681 SOME (L'.ERel 0,
adamc@186 682 loc)),
adamc@186 683 loc)), loc), ""), loc)
adamc@185 684 in
adamc@186 685 (SM.insert (cmap, x', tf), d)
adamc@185 686 end
adamc@185 687
adamc@186 688 val st = St.bindConstructor st x' n pc
adamc@186 689 (*val st = St.bindConstructorVal st x' n*)
adamc@186 690
adamc@188 691 val conmap = SM.insert (conmap, x', (x, to, dk))
adamc@185 692 in
adamc@185 693 ((x', n, to),
adamc@186 694 (d :: ds', st, cmap, conmap))
adamc@186 695 end) ([], st, cmap, conmap) xnts
adamc@185 696 in
adamc@186 697 (ds' @ (L'.DDatatype (x, n', xnts), loc) :: ds,
adamc@185 698 cmap,
adamc@185 699 conmap,
adamc@49 700 st)
adamc@191 701 end*)
adamc@49 702
adamc@49 703 | L.SgiVal (x, _, c) =>
adamc@49 704 (ds,
adamc@49 705 SM.insert (cmap, x, corifyCon st c),
adamc@185 706 conmap,
adamc@49 707 st)
adamc@185 708 | _ => (ds, cmap, conmap, st)) ([], SM.empty, SM.empty, st) sgis
adamc@49 709
adamc@185 710 val st = St.bindStr st m n (St.ffi m cmap conmap)
adamc@49 711 in
adamc@49 712 (rev ds, st)
adamc@49 713 end
adamc@49 714 | _ => raise Fail "Non-const signature for FFI structure")
adamc@48 715
adamc@109 716 | L.DExport (en, sgn, str) =>
adamc@109 717 (case #1 sgn of
adamc@109 718 L.SgnConst sgis =>
adamc@109 719 let
adamc@109 720 fun pathify (str, _) =
adamc@109 721 case str of
adamc@109 722 L.StrVar m => SOME (m, [])
adamc@109 723 | L.StrProj (str, s) =>
adamc@109 724 Option.map (fn (m, ms) => (m, ms @ [s])) (pathify str)
adamc@109 725 | _ => NONE
adamc@109 726 in
adamc@109 727 case pathify str of
adamc@109 728 NONE => (ErrorMsg.errorAt loc "Structure is too fancy to export";
adamc@109 729 ([], st))
adamc@109 730 | SOME (m, ms) =>
adamc@109 731 let
adamc@109 732 fun wrapSgi ((sgi, _), (wds, eds)) =
adamc@109 733 case sgi of
adamc@109 734 L.SgiVal (s, _, t as (L.TFun (dom, ran), _)) =>
adamc@109 735 (case (#1 dom, #1 ran) of
adamc@147 736 (L.TRecord (L.CRecord (_, []), _),
adamc@139 737 L.CApp
adamc@139 738 ((L.CApp
adamc@139 739 ((L.CApp ((L.CModProj (_, [], "xml"), _),
adamc@139 740 (L.CRecord (_, [((L.CName "Html", _),
adamc@139 741 _)]), _)), _), _), _), _)) =>
adamc@109 742 let
adamc@109 743 val ran = (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc)
adamc@109 744 val e = (L.EModProj (m, ms, s), loc)
adamc@109 745 val e = (L.EAbs ("vs", dom, ran,
adamc@109 746 (L.EWrite (L.EApp (e, (L.ERel 0, loc)), loc), loc)), loc)
adamc@109 747 in
adamc@109 748 ((L.DVal ("wrap_" ^ s, 0,
adamc@109 749 (L.TFun (dom, ran), loc),
adamc@109 750 e), loc) :: wds,
adamc@109 751 (fn st =>
adamc@109 752 case #1 (corifyExp st (L.EModProj (en, [], "wrap_" ^ s), loc)) of
adamc@144 753 L'.ENamed n => (L'.DExport (L'.Link, n), loc)
adamc@109 754 | _ => raise Fail "Corify: Value to export didn't corify properly")
adamc@109 755 :: eds)
adamc@109 756 end
adamc@109 757 | _ => (wds, eds))
adamc@109 758 | _ => (wds, eds)
adamc@102 759
adamc@109 760 val (wds, eds) = foldl wrapSgi ([], []) sgis
adamc@109 761 val wrapper = (L.StrConst wds, loc)
adamc@109 762 val (ds, {inner, outer}) = corifyStr (wrapper, st)
adamc@109 763 val st = St.bindStr outer "wrapper" en inner
adamc@109 764
adamc@109 765 val ds = ds @ map (fn f => f st) eds
adamc@109 766 in
adamc@109 767 (ds, st)
adamc@109 768 end
adamc@109 769 end
adamc@109 770 | _ => raise Fail "Non-const signature for 'export'")
adamc@48 771
adamc@39 772 and corifyStr ((str, _), st) =
adamc@39 773 case str of
adamc@39 774 L.StrConst ds =>
adamc@39 775 let
adamc@39 776 val st = St.enter st
adamc@39 777 val (ds, st) = ListUtil.foldlMapConcat corifyDecl st ds
adamc@39 778 in
adamc@39 779 (ds, St.leave st)
adamc@39 780 end
adamc@39 781 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
adamc@39 782 | L.StrProj (str, x) =>
adamc@39 783 let
adamc@39 784 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 785 in
adamc@39 786 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
adamc@39 787 end
adamc@46 788 | L.StrFun _ => raise Fail "Corify of nested functor definition"
adamc@46 789 | L.StrApp (str1, str2) =>
adamc@46 790 let
adamc@46 791 fun unwind' (str, _) =
adamc@46 792 case str of
adamc@46 793 L.StrVar n => St.lookupStrById st n
adamc@46 794 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
adamc@46 795 | _ => raise Fail "Corify of fancy functor application [1]"
adamc@46 796
adamc@46 797 fun unwind (str, _) =
adamc@46 798 case str of
adamc@46 799 L.StrVar n => St.lookupFunctorById st n
adamc@46 800 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
adamc@46 801 | _ => raise Fail "Corify of fancy functor application [2]"
adamc@46 802
adamc@146 803 val (xa, na, body) = unwind str1
adamc@46 804
adamc@146 805 val (ds1, {inner = inner', outer}) = corifyStr (str2, st)
adamc@146 806 val (ds2, {inner, outer}) = corifyStr (body, St.bindStr outer xa na inner')
adamc@46 807 in
adamc@146 808 (ds1 @ ds2, {inner = St.bindStr inner xa na inner', outer = outer})
adamc@46 809 end
adamc@31 810
adamc@39 811 fun maxName ds = foldl (fn ((d, _), n) =>
adamc@39 812 case d of
adamc@39 813 L.DCon (_, n', _, _) => Int.max (n, n')
adamc@191 814 | L.DDatatype (_, n', _, _) => Int.max (n, n')
adamc@191 815 | L.DDatatypeImp (_, n', _, _, _, _, _) => Int.max (n, n')
adamc@124 816 | L.DVal (_, n', _, _) => Int.max (n, n')
adamc@124 817 | L.DValRec vis => foldl (fn ((_, n', _, _), n) => Int.max (n, n)) n vis
adamc@39 818 | L.DSgn (_, n', _) => Int.max (n, n')
adamc@48 819 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
adamc@100 820 | L.DFfiStr (_, n', _) => Int.max (n, n')
adamc@109 821 | L.DExport _ => n)
adamc@39 822 0 ds
adamc@39 823
adamc@39 824 and maxNameStr (str, _) =
adamc@39 825 case str of
adamc@39 826 L.StrConst ds => maxName ds
adamc@39 827 | L.StrVar n => n
adamc@39 828 | L.StrProj (str, _) => maxNameStr str
adamc@45 829 | L.StrFun (_, _, _, _, str) => maxNameStr str
adamc@45 830 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@39 831
adamc@39 832 fun corify ds =
adamc@39 833 let
adamc@39 834 val () = reset (maxName ds + 1)
adamc@146 835
adamc@39 836 val (ds, _) = ListUtil.foldlMapConcat corifyDecl St.empty ds
adamc@39 837 in
adamc@39 838 ds
adamc@39 839 end
adamc@16 840
adamc@16 841 end