annotate src/corify.sml @ 213:0343557355fc

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