annotate src/corify.sml @ 901:f17638dd3080

dlist row add working with Konqueror
author Adam Chlipala <adamc@hcoop.net>
date Thu, 06 Aug 2009 14:24:40 -0400
parents 7b380e2b9e68
children e46227efcbba
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@768 40 fun doRestify k (mods, s) =
adamc@376 41 let
adamc@376 42 val s = if String.isPrefix "wrap_" s then
adamc@376 43 String.extract (s, 5, NONE)
adamc@376 44 else
adamc@376 45 s
adamc@376 46 in
adamc@768 47 Settings.rewrite k (String.concatWith "/" (rev (s :: mods)))
adamc@376 48 end
adamc@376 49
adamc@377 50 val relify = CharVector.map (fn #"/" => #"_"
adamc@377 51 | ch => ch)
adamc@377 52
adamc@39 53 local
adamc@39 54 val count = ref 0
adamc@39 55 in
adamc@39 56
adamc@39 57 fun reset v = count := v
adamc@39 58
adamc@39 59 fun alloc () =
adamc@39 60 let
adamc@39 61 val r = !count
adamc@39 62 in
adamc@39 63 count := r + 1;
adamc@39 64 r
adamc@39 65 end
adamc@39 66
adamc@39 67 end
adamc@39 68
adamc@39 69 structure St : sig
adamc@39 70 type t
adamc@39 71
adamc@39 72 val empty : t
adamc@39 73
adamc@146 74 val debug : t -> unit
adamc@146 75
adamc@376 76 val name : t -> string list
adamc@376 77
adamc@376 78 val enter : t * string list -> t
adamc@39 79 val leave : t -> {outer : t, inner : t}
adamc@192 80 val ffi : string -> L'.con SM.map -> (string * string list * L'.con option * L'.datatype_kind) SM.map -> t
adamc@39 81
adamc@249 82 val basisIs : t * int -> t
adamc@249 83 val lookupBasis : t -> int option
adamc@249 84
adamc@73 85 datatype core_con =
adamc@73 86 CNormal of int
adamc@73 87 | CFfi of string
adamc@73 88 val bindCon : t -> string -> int -> t * int
adamc@73 89 val lookupConById : t -> int -> int option
adamc@73 90 val lookupConByName : t -> string -> core_con
adamc@48 91
adamc@177 92 val bindConstructor : t -> string -> int -> L'.patCon -> t
adamc@186 93 val lookupConstructorByNameOpt : t -> string -> L'.patCon option
adamc@177 94 val lookupConstructorByName : t -> string -> L'.patCon
adamc@177 95 val lookupConstructorById : t -> int -> L'.patCon
adamc@249 96
adamc@249 97 datatype core_val =
adamc@249 98 ENormal of int
adamc@249 99 | EFfi of string * L'.con
adamc@249 100 val bindVal : t -> string -> int -> t * int
adamc@249 101 val bindConstructorVal : t -> string -> int -> t
adamc@249 102 val lookupValById : t -> int -> int option
adamc@249 103 val lookupValByName : t -> string -> core_val
adamc@177 104
adamc@249 105 val bindStr : t -> string -> int -> t -> t
adamc@249 106 val lookupStrById : t -> int -> t
adamc@249 107 val lookupStrByName : string * t -> t
adamc@339 108 val lookupStrByNameOpt : string * t -> t option
adamc@39 109
adamc@423 110 val bindFunctor : t -> string -> int -> string -> int -> L.str -> t
adamc@423 111 val lookupFunctorById : t -> int -> string * int * L.str
adamc@423 112 val lookupFunctorByName : string * t -> string * int * L.str
adamc@249 113 end = struct
adamc@46 114
adamc@249 115 datatype flattening =
adamc@376 116 FNormal of {name : string list,
adamc@376 117 cons : int SM.map,
adamc@249 118 constructors : L'.patCon SM.map,
adamc@249 119 vals : int SM.map,
adamc@249 120 strs : flattening SM.map,
adamc@423 121 funs : (string * int * L.str) SM.map}
adamc@249 122 | FFfi of {mod : string,
adamc@249 123 vals : L'.con SM.map,
adamc@249 124 constructors : (string * string list * L'.con option * L'.datatype_kind) SM.map}
adamc@39 125
adamc@249 126 type t = {
adamc@249 127 basis : int option,
adamc@249 128 cons : int IM.map,
adamc@249 129 constructors : L'.patCon IM.map,
adamc@249 130 vals : int IM.map,
adamc@249 131 strs : flattening IM.map,
adamc@423 132 funs : (string * int * L.str) IM.map,
adamc@249 133 current : flattening,
adamc@249 134 nested : flattening list
adamc@249 135 }
adamc@39 136
adamc@249 137 val empty = {
adamc@249 138 basis = NONE,
adamc@249 139 cons = IM.empty,
adamc@249 140 constructors = IM.empty,
adamc@249 141 vals = IM.empty,
adamc@249 142 strs = IM.empty,
adamc@249 143 funs = IM.empty,
adamc@376 144 current = FNormal { name = [], cons = SM.empty, constructors = SM.empty,
adamc@376 145 vals = SM.empty, strs = SM.empty, funs = SM.empty },
adamc@249 146 nested = []
adamc@249 147 }
adamc@39 148
adamc@376 149 fun debug ({current = FNormal {cons, constructors, vals, strs, funs, ...}, ...} : t) =
adamc@249 150 print ("cons: " ^ Int.toString (SM.numItems cons) ^ "; "
adamc@249 151 ^ "constructors: " ^ Int.toString (SM.numItems constructors) ^ "; "
adamc@249 152 ^ "vals: " ^ Int.toString (SM.numItems vals) ^ "; "
adamc@249 153 ^ "strs: " ^ Int.toString (SM.numItems strs) ^ "; "
adamc@249 154 ^ "funs: " ^ Int.toString (SM.numItems funs) ^ "\n")
adamc@249 155 | debug _ = print "Not normal!\n"
adamc@146 156
adamc@376 157 fun name ({current = FNormal {name, ...}, ...} : t) = name
adamc@376 158 | name {current = FFfi {mod = name, ...}, ...} = [name]
adamc@376 159
adamc@249 160 fun basisIs ({cons, constructors, vals, strs, funs, current, nested, ...} : t, basis) =
adamc@249 161 {basis = SOME basis,
adamc@249 162 cons = cons,
adamc@249 163 constructors = constructors,
adamc@249 164 vals = vals,
adamc@249 165 strs = strs,
adamc@249 166 funs = funs,
adamc@249 167 current = current,
adamc@249 168 nested = nested}
adamc@48 169
adamc@249 170 fun lookupBasis ({basis, ...} : t) = basis
adamc@73 171
adamc@249 172 datatype core_con =
adamc@249 173 CNormal of int
adamc@249 174 | CFfi of string
adamc@39 175
adamc@249 176 datatype core_val =
adamc@249 177 ENormal of int
adamc@249 178 | EFfi of string * L'.con
adamc@188 179
adamc@249 180 fun bindCon {basis, cons, constructors, vals, strs, funs, current, nested} s n =
adamc@249 181 let
adamc@249 182 val n' = alloc ()
adamc@188 183
adamc@249 184 val current =
adamc@249 185 case current of
adamc@249 186 FFfi _ => raise Fail "Binding inside FFfi"
adamc@376 187 | FNormal {name, cons, constructors, vals, strs, funs} =>
adamc@376 188 FNormal {name = name,
adamc@376 189 cons = SM.insert (cons, s, n'),
adamc@249 190 constructors = constructors,
adamc@249 191 vals = vals,
adamc@249 192 strs = strs,
adamc@249 193 funs = funs}
adamc@249 194 in
adamc@249 195 ({basis = basis,
adamc@249 196 cons = IM.insert (cons, n, n'),
adamc@177 197 constructors = constructors,
adamc@73 198 vals = vals,
adamc@39 199 strs = strs,
adamc@46 200 funs = funs,
adamc@39 201 current = current,
adamc@249 202 nested = nested},
adamc@249 203 n')
adamc@249 204 end
adamc@39 205
adamc@249 206 fun lookupConById ({cons, ...} : t) n = IM.find (cons, n)
adamc@39 207
adamc@249 208 fun lookupConByName ({current, ...} : t) x =
adamc@249 209 case current of
adamc@249 210 FFfi {mod = m, ...} => CFfi m
adamc@249 211 | FNormal {cons, ...} =>
adamc@249 212 case SM.find (cons, x) of
adamc@249 213 NONE => raise Fail "Corify.St.lookupConByName"
adamc@249 214 | SOME n => CNormal n
adamc@249 215
adamc@249 216 fun bindVal {basis, cons, constructors, vals, strs, funs, current, nested} s n =
adamc@249 217 let
adamc@249 218 val n' = alloc ()
adamc@249 219
adamc@249 220 val current =
adamc@249 221 case current of
adamc@249 222 FFfi _ => raise Fail "Binding inside FFfi"
adamc@376 223 | FNormal {name, cons, constructors, vals, strs, funs} =>
adamc@376 224 FNormal {name = name,
adamc@376 225 cons = cons,
adamc@249 226 constructors = constructors,
adamc@249 227 vals = SM.insert (vals, s, n'),
adamc@249 228 strs = strs,
adamc@249 229 funs = funs}
adamc@249 230 in
adamc@249 231 ({basis = basis,
adamc@249 232 cons = cons,
adamc@249 233 constructors = constructors,
adamc@249 234 vals = IM.insert (vals, n, n'),
adamc@249 235 strs = strs,
adamc@249 236 funs = funs,
adamc@249 237 current = current,
adamc@249 238 nested = nested},
adamc@249 239 n')
adamc@249 240 end
adamc@249 241
adamc@249 242 fun bindConstructorVal {basis, cons, constructors, vals, strs, funs, current, nested} s n =
adamc@249 243 let
adamc@249 244 val current =
adamc@249 245 case current of
adamc@249 246 FFfi _ => raise Fail "Binding inside FFfi"
adamc@376 247 | FNormal {name, cons, constructors, vals, strs, funs} =>
adamc@376 248 FNormal {name = name,
adamc@376 249 cons = cons,
adamc@249 250 constructors = constructors,
adamc@249 251 vals = SM.insert (vals, s, n),
adamc@249 252 strs = strs,
adamc@249 253 funs = funs}
adamc@249 254 in
adamc@249 255 {basis = basis,
adamc@249 256 cons = cons,
adamc@249 257 constructors = constructors,
adamc@249 258 vals = IM.insert (vals, n, n),
adamc@249 259 strs = strs,
adamc@249 260 funs = funs,
adamc@249 261 current = current,
adamc@249 262 nested = nested}
adamc@249 263 end
adamc@249 264
adamc@249 265
adamc@249 266 fun lookupValById ({vals, ...} : t) n = IM.find (vals, n)
adamc@249 267
adamc@249 268 fun lookupValByName ({current, ...} : t) x =
adamc@249 269 case current of
adamc@249 270 FFfi {mod = m, vals, ...} =>
adamc@249 271 (case SM.find (vals, x) of
adamc@249 272 NONE => raise Fail "Corify.St.lookupValByName: no type for FFI val"
adamc@249 273 | SOME t => EFfi (m, t))
adamc@249 274 | FNormal {vals, ...} =>
adamc@249 275 case SM.find (vals, x) of
adamc@249 276 NONE => raise Fail "Corify.St.lookupValByName"
adamc@249 277 | SOME n => ENormal n
adamc@249 278
adamc@249 279 fun bindConstructor {basis, cons, constructors, vals, strs, funs, current, nested} s n n' =
adamc@249 280 let
adamc@249 281 val current =
adamc@249 282 case current of
adamc@249 283 FFfi _ => raise Fail "Binding inside FFfi"
adamc@376 284 | FNormal {name, cons, constructors, vals, strs, funs} =>
adamc@376 285 FNormal {name = name,
adamc@376 286 cons = cons,
adamc@249 287 constructors = SM.insert (constructors, s, n'),
adamc@249 288 vals = vals,
adamc@249 289 strs = strs,
adamc@249 290 funs = funs}
adamc@249 291 in
adamc@249 292 {basis = basis,
adamc@249 293 cons = cons,
adamc@249 294 constructors = IM.insert (constructors, n, n'),
adamc@249 295 vals = vals,
adamc@249 296 strs = strs,
adamc@249 297 funs = funs,
adamc@249 298 current = current,
adamc@249 299 nested = nested}
adamc@249 300 end
adamc@249 301
adamc@249 302 fun lookupConstructorById ({constructors, ...} : t) n =
adamc@249 303 case IM.find (constructors, n) of
adamc@249 304 NONE => raise Fail "Corify.St.lookupConstructorById"
adamc@249 305 | SOME x => x
adamc@249 306
adamc@249 307 fun lookupConstructorByNameOpt ({current, ...} : t) x =
adamc@249 308 case current of
adamc@249 309 FFfi {mod = m, constructors, ...} =>
adamc@249 310 (case SM.find (constructors, x) of
adamc@188 311 NONE => NONE
adamc@249 312 | SOME (n, xs, to, dk) => SOME (L'.PConFfi {mod = m, datatyp = n, params = xs, con = x, arg = to, kind = dk}))
adamc@249 313 | FNormal {constructors, ...} =>
adamc@249 314 case SM.find (constructors, x) of
adamc@249 315 NONE => NONE
adamc@249 316 | SOME n => SOME n
adamc@39 317
adamc@249 318 fun lookupConstructorByName ({current, ...} : t) x =
adamc@249 319 case current of
adamc@249 320 FFfi {mod = m, constructors, ...} =>
adamc@249 321 (case SM.find (constructors, x) of
adamc@249 322 NONE => raise Fail "Corify.St.lookupConstructorByName [1]"
adamc@249 323 | SOME (n, xs, to, dk) => L'.PConFfi {mod = m, datatyp = n, params = xs, con = x, arg = to, kind = dk})
adamc@249 324 | FNormal {constructors, ...} =>
adamc@249 325 case SM.find (constructors, x) of
adamc@249 326 NONE => raise Fail "Corify.St.lookupConstructorByName [2]"
adamc@249 327 | SOME n => n
adamc@73 328
adamc@376 329 fun enter ({basis, cons, constructors, vals, strs, funs, current, nested}, name) =
adamc@249 330 {basis = basis,
adamc@249 331 cons = cons,
adamc@249 332 constructors = constructors,
adamc@249 333 vals = vals,
adamc@249 334 strs = strs,
adamc@249 335 funs = funs,
adamc@376 336 current = FNormal {name = name,
adamc@376 337 cons = SM.empty,
adamc@249 338 constructors = SM.empty,
adamc@249 339 vals = SM.empty,
adamc@249 340 strs = SM.empty,
adamc@249 341 funs = SM.empty},
adamc@249 342 nested = current :: nested}
adamc@73 343
adamc@249 344 fun dummy (b, f) = {basis = b,
adamc@249 345 cons = IM.empty,
adamc@249 346 constructors = IM.empty,
adamc@249 347 vals = IM.empty,
adamc@249 348 strs = IM.empty,
adamc@249 349 funs = IM.empty,
adamc@249 350 current = f,
adamc@249 351 nested = []}
adamc@163 352
adamc@249 353 fun leave {basis, cons, constructors, vals, strs, funs, current, nested = m1 :: rest} =
adamc@249 354 {outer = {basis = basis,
adamc@249 355 cons = cons,
adamc@249 356 constructors = constructors,
adamc@249 357 vals = vals,
adamc@249 358 strs = strs,
adamc@249 359 funs = funs,
adamc@249 360 current = m1,
adamc@249 361 nested = rest},
adamc@249 362 inner = dummy (basis, current)}
adamc@249 363 | leave _ = raise Fail "Corify.St.leave"
adamc@177 364
adamc@249 365 fun ffi m vals constructors = dummy (NONE, FFfi {mod = m, vals = vals, constructors = constructors})
adamc@73 366
adamc@249 367 fun bindStr ({basis, cons, constructors, vals, strs, funs,
adamc@376 368 current = FNormal {name, cons = mcons, constructors = mconstructors,
adamc@249 369 vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@249 370 x n ({current = f, ...} : t) =
adamc@249 371 {basis = basis,
adamc@249 372 cons = cons,
adamc@249 373 constructors = constructors,
adamc@249 374 vals = vals,
adamc@249 375 strs = IM.insert (strs, n, f),
adamc@249 376 funs = funs,
adamc@376 377 current = FNormal {name = name,
adamc@376 378 cons = mcons,
adamc@249 379 constructors = mconstructors,
adamc@249 380 vals = mvals,
adamc@249 381 strs = SM.insert (mstrs, x, f),
adamc@249 382 funs = mfuns},
adamc@249 383 nested = nested}
adamc@249 384 | bindStr _ _ _ _ = raise Fail "Corify.St.bindStr"
adamc@73 385
adamc@249 386 fun lookupStrById ({basis, strs, ...} : t) n =
adamc@249 387 case IM.find (strs, n) of
adamc@480 388 NONE => raise Fail ("Corify.St.lookupStrById(" ^ Int.toString n ^ ")")
adamc@249 389 | SOME f => dummy (basis, f)
adamc@177 390
adamc@249 391 fun lookupStrByName (m, {basis, current = FNormal {strs, ...}, ...} : t) =
adamc@249 392 (case SM.find (strs, m) of
adamc@339 393 NONE => raise Fail "Corify.St.lookupStrByName [1]"
adamc@249 394 | SOME f => dummy (basis, f))
adamc@339 395 | lookupStrByName _ = raise Fail "Corify.St.lookupStrByName [2]"
adamc@339 396
adamc@339 397 fun lookupStrByNameOpt (m, {basis, current = FNormal {strs, ...}, ...} : t) =
adamc@339 398 (case SM.find (strs, m) of
adamc@339 399 NONE => NONE
adamc@339 400 | SOME f => SOME (dummy (basis, f)))
adamc@339 401 | lookupStrByNameOpt _ = NONE
adamc@177 402
adamc@249 403 fun bindFunctor ({basis, cons, constructors, vals, strs, funs,
adamc@376 404 current = FNormal {name, cons = mcons, constructors = mconstructors,
adamc@249 405 vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@423 406 x n xa na str =
adamc@249 407 {basis = basis,
adamc@249 408 cons = cons,
adamc@249 409 constructors = constructors,
adamc@249 410 vals = vals,
adamc@249 411 strs = strs,
adamc@423 412 funs = IM.insert (funs, n, (xa, na, str)),
adamc@376 413 current = FNormal {name = name,
adamc@376 414 cons = mcons,
adamc@249 415 constructors = mconstructors,
adamc@249 416 vals = mvals,
adamc@249 417 strs = mstrs,
adamc@423 418 funs = SM.insert (mfuns, x, (xa, na, str))},
adamc@249 419 nested = nested}
adamc@423 420 | bindFunctor _ _ _ _ _ _ = raise Fail "Corify.St.bindFunctor"
adamc@186 421
adamc@249 422 fun lookupFunctorById ({funs, ...} : t) n =
adamc@249 423 case IM.find (funs, n) of
adamc@249 424 NONE => raise Fail "Corify.St.lookupFunctorById"
adamc@249 425 | SOME v => v
adamc@177 426
adamc@249 427 fun lookupFunctorByName (m, {current = FNormal {funs, ...}, ...} : t) =
adamc@249 428 (case SM.find (funs, m) of
adamc@339 429 NONE => raise Fail "Corify.St.lookupFunctorByName [1]"
adamc@249 430 | SOME v => v)
adamc@339 431 | lookupFunctorByName _ = raise Fail "Corify.St.lookupFunctorByName [2]"
adamc@39 432
adamc@249 433 end
adamc@39 434
adamc@39 435
adamc@249 436 fun corifyKind (k, loc) =
adamc@249 437 case k of
adamc@249 438 L.KType => (L'.KType, loc)
adamc@249 439 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
adamc@249 440 | L.KName => (L'.KName, loc)
adamc@249 441 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
adamc@249 442 | L.KUnit => (L'.KUnit, loc)
adamc@249 443 | L.KTuple ks => (L'.KTuple (map corifyKind ks), loc)
adamc@48 444
adamc@626 445 | L.KRel n => (L'.KRel n, loc)
adamc@626 446 | L.KFun (x, k) => (L'.KFun (x, corifyKind k), loc)
adamc@626 447
adamc@249 448 fun corifyCon st (c, loc) =
adamc@249 449 case c of
adamc@249 450 L.TFun (t1, t2) => (L'.TFun (corifyCon st t1, corifyCon st t2), loc)
adamc@249 451 | L.TCFun (x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon st t), loc)
adamc@626 452 | L.TKFun (x, t) => (L'.TKFun (x, corifyCon st t), loc)
adamc@249 453 | L.TRecord c => (L'.TRecord (corifyCon st c), loc)
adamc@39 454
adamc@249 455 | L.CRel n => (L'.CRel n, loc)
adamc@249 456 | L.CNamed n =>
adamc@249 457 (case St.lookupConById st n of
adamc@249 458 NONE => (L'.CNamed n, loc)
adamc@249 459 | SOME n => (L'.CNamed n, loc))
adamc@249 460 | L.CModProj (m, ms, x) =>
adamc@249 461 let
adamc@249 462 val st = St.lookupStrById st m
adamc@249 463 val st = foldl St.lookupStrByName st ms
adamc@249 464 in
adamc@249 465 case St.lookupConByName st x of
adamc@249 466 St.CNormal n => (L'.CNamed n, loc)
adamc@249 467 | St.CFfi m => (L'.CFfi (m, x), loc)
adamc@249 468 end
adamc@39 469
adamc@249 470 | L.CApp (c1, c2) => (L'.CApp (corifyCon st c1, corifyCon st c2), loc)
adamc@249 471 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon st c), loc)
adamc@39 472
adamc@626 473 | L.CKApp (c1, k) => (L'.CKApp (corifyCon st c1, corifyKind k), loc)
adamc@626 474 | L.CKAbs (x, c) => (L'.CKAbs (x, corifyCon st c), loc)
adamc@626 475
adamc@249 476 | L.CName s => (L'.CName s, loc)
adamc@46 477
adamc@249 478 | L.CRecord (k, xcs) =>
adamc@249 479 (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon st c1, corifyCon st c2)) xcs), loc)
adamc@249 480 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon st c1, corifyCon st c2), loc)
adamc@621 481 | L.CMap (k1, k2) => (L'.CMap (corifyKind k1, corifyKind k2), loc)
adamc@249 482 | L.CUnit => (L'.CUnit, loc)
adamc@46 483
adamc@249 484 | L.CTuple cs => (L'.CTuple (map (corifyCon st) cs), loc)
adamc@249 485 | L.CProj (c, n) => (L'.CProj (corifyCon st c, n), loc)
adamc@213 486
adamc@249 487 fun corifyPatCon st pc =
adamc@249 488 case pc of
adamc@249 489 L.PConVar n => St.lookupConstructorById st n
adamc@249 490 | L.PConProj (m1, ms, x) =>
adamc@249 491 let
adamc@249 492 val st = St.lookupStrById st m1
adamc@249 493 val st = foldl St.lookupStrByName st ms
adamc@249 494 in
adamc@249 495 St.lookupConstructorByName st x
adamc@249 496 end
adamc@46 497
adamc@249 498 fun corifyPat st (p, loc) =
adamc@249 499 case p of
adamc@249 500 L.PWild => (L'.PWild, loc)
adamc@249 501 | L.PVar (x, t) => (L'.PVar (x, corifyCon st t), loc)
adamc@249 502 | L.PPrim p => (L'.PPrim p, loc)
adamc@249 503 | L.PCon (dk, pc, ts, po) => (L'.PCon (dk, corifyPatCon st pc, map (corifyCon st) ts,
adamc@249 504 Option.map (corifyPat st) po), loc)
adamc@249 505 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, corifyPat st p, corifyCon st t)) xps), loc)
adamc@39 506
adamc@249 507 fun corifyExp st (e, loc) =
adamc@249 508 case e of
adamc@249 509 L.EPrim p => (L'.EPrim p, loc)
adamc@249 510 | L.ERel n => (L'.ERel n, loc)
adamc@249 511 | L.ENamed n =>
adamc@249 512 (case St.lookupValById st n of
adamc@249 513 NONE => (L'.ENamed n, loc)
adamc@249 514 | SOME n => (L'.ENamed n, loc))
adamc@249 515 | L.EModProj (m, ms, x) =>
adamc@249 516 let
adamc@249 517 val st = St.lookupStrById st m
adamc@249 518 val st = foldl St.lookupStrByName st ms
adamc@249 519 in
adamc@249 520 case St.lookupConstructorByNameOpt st x of
adamc@249 521 SOME (pc as L'.PConFfi {mod = m, datatyp, params, arg, kind, ...}) =>
adamc@249 522 let
adamc@249 523 val args = ListUtil.mapi (fn (i, _) => (L'.CRel i, loc)) params
adamc@249 524 val e = case arg of
adamc@249 525 NONE => (L'.ECon (kind, pc, args, NONE), loc)
adamc@249 526 | SOME dom => (L'.EAbs ("x", dom, (L'.CFfi (m, datatyp), loc),
adamc@249 527 (L'.ECon (kind, pc, args, SOME (L'.ERel 0, loc)), loc)), loc)
adamc@192 528
adamc@249 529 val k = (L'.KType, loc)
adamc@249 530 in
adamc@249 531 foldr (fn (x, e) => (L'.ECAbs (x, k, e), loc)) e params
adamc@249 532 end
adamc@249 533 | _ =>
adamc@249 534 case St.lookupValByName st x of
adamc@249 535 St.ENormal n => (L'.ENamed n, loc)
adamc@249 536 | St.EFfi (m, t) =>
adamc@249 537 case t of
adamc@249 538 (L'.TFun (dom as (L'.TRecord (L'.CRecord (_, []), _), _), ran), _) =>
adamc@249 539 (L'.EAbs ("arg", dom, ran, (L'.EFfiApp (m, x, []), loc)), loc)
adamc@765 540 | (L'.CApp ((L'.CFfi ("Basis", "transaction"), _), dom), _) =>
adamc@765 541 (L'.EAbs ("arg", dom, (L'.TRecord (L'.CRecord ((L'.KType, loc), []), loc), loc),
adamc@765 542 (L'.EFfiApp (m, x, []), loc)), loc)
adamc@249 543 | t as (L'.TFun _, _) =>
adamc@249 544 let
adamc@249 545 fun getArgs (all as (t, _), args) =
adamc@249 546 case t of
adamc@249 547 L'.TFun (dom, ran) => getArgs (ran, dom :: args)
adamc@249 548 | _ => (all, rev args)
adamc@39 549
adamc@249 550 val (result, args) = getArgs (t, [])
adamc@456 551 val (isTransaction, result) =
adamc@456 552 case result of
adamc@456 553 (L'.CApp ((L'.CFfi ("Basis", "transaction"), _),
adamc@456 554 result), _) => (true, result)
adamc@456 555 | _ => (false, result)
adamc@16 556
adamc@456 557 fun makeApp n =
adamc@456 558 let
adamc@456 559 val (actuals, _) = foldr (fn (_, (actuals, n)) =>
adamc@456 560 ((L'.ERel n, loc) :: actuals,
adamc@456 561 n + 1)) ([], n) args
adamc@456 562 in
adamc@456 563 (L'.EFfiApp (m, x, actuals), loc)
adamc@456 564 end
adamc@456 565 val unit = (L'.TRecord (L'.CRecord ((L'.KType, loc), []), loc), loc)
adamc@456 566 val (result, app) =
adamc@456 567 if isTransaction then
adamc@456 568 ((L'.TFun (unit, result), loc),
adamc@456 569 (L'.EAbs ("_",
adamc@456 570 unit,
adamc@456 571 result,
adamc@456 572 makeApp 1), loc))
adamc@456 573 else
adamc@456 574 (result, makeApp 0)
adamc@456 575
adamc@249 576 val (abs, _, _) = foldr (fn (t, (abs, ran, n)) =>
adamc@249 577 ((L'.EAbs ("arg" ^ Int.toString n,
adamc@249 578 t,
adamc@249 579 ran,
adamc@249 580 abs), loc),
adamc@249 581 (L'.TFun (t, ran), loc),
adamc@249 582 n - 1)) (app, result, length args - 1) args
adamc@249 583 in
adamc@249 584 abs
adamc@249 585 end
adamc@249 586 | _ => (L'.EFfi (m, x), loc)
adamc@249 587 end
adamc@249 588 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
adamc@249 589 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
adamc@249 590 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
adamc@249 591 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
adamc@626 592 | L.EKApp (e1, k) => (L'.EKApp (corifyExp st e1, corifyKind k), loc)
adamc@626 593 | L.EKAbs (x, e1) => (L'.EKAbs (x, corifyExp st e1), loc)
adamc@16 594
adamc@249 595 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) =>
adamc@249 596 (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
adamc@249 597 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
adamc@249 598 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@445 599 | L.EConcat (e1, c1, e2, c2) => (L'.EConcat (corifyExp st e1, corifyCon st c1, corifyExp st e2,
adamc@445 600 corifyCon st c2), loc)
adamc@249 601 | L.ECut (e1, c, {field, rest}) => (L'.ECut (corifyExp st e1, corifyCon st c,
adamc@249 602 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@493 603 | L.ECutMulti (e1, c, {rest}) => (L'.ECutMulti (corifyExp st e1, corifyCon st c,
adamc@493 604 {rest = corifyCon st rest}), loc)
adamc@34 605
adamc@249 606 | L.ECase (e, pes, {disc, result}) =>
adamc@249 607 (L'.ECase (corifyExp st e,
adamc@249 608 map (fn (p, e) => (corifyPat st p, corifyExp st e)) pes,
adamc@249 609 {disc = corifyCon st disc, result = corifyCon st result}),
adamc@249 610 loc)
adamc@16 611
adamc@249 612 | L.EWrite e => (L'.EWrite (corifyExp st e), loc)
adamc@16 613
adamc@450 614 | L.ELet (x, t, e1, e2) => (L'.ELet (x, corifyCon st t, corifyExp st e1, corifyExp st e2), loc)
adamc@450 615
adamc@480 616 fun corifyDecl mods (all as (d, loc : EM.span), st) =
adamc@249 617 case d of
adamc@249 618 L.DCon (x, n, k, c) =>
adamc@249 619 let
adamc@249 620 val (st, n) = St.bindCon st x n
adamc@249 621 in
adamc@249 622 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
adamc@249 623 end
adamc@807 624 | L.DDatatype dts =>
adamc@249 625 let
adamc@807 626 val (dts, st) = ListUtil.foldlMap (fn ((x, n, xs, xncs), st) =>
adamc@807 627 let
adamc@807 628 val (st, n) = St.bindCon st x n
adamc@807 629 in
adamc@807 630 ((x, n, xs, xncs), st)
adamc@807 631 end)
adamc@807 632 st dts
adamc@16 633
adamc@807 634 val (dts, (st, dcons)) =
adamc@807 635 ListUtil.foldlMap
adamc@807 636 (fn ((x, n, xs, xncs), (st, dcons)) =>
adamc@807 637 let
adamc@807 638 val (xncs, st) = ListUtil.foldlMap
adamc@807 639 (fn ((x, n, co), st) =>
adamc@807 640 let
adamc@807 641 val st = St.bindConstructor st x n (L'.PConVar n)
adamc@807 642 val st = St.bindConstructorVal st x n
adamc@807 643 val co = Option.map (corifyCon st) co
adamc@807 644 in
adamc@807 645 ((x, n, co), st)
adamc@807 646 end) st xncs
adamc@192 647
adamc@807 648 val dk = ElabUtil.classifyDatatype xncs
adamc@807 649 val t = (L'.CNamed n, loc)
adamc@807 650 val nxs = length xs - 1
adamc@807 651 val t = ListUtil.foldli
adamc@807 652 (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
adamc@807 653 val k = (L'.KType, loc)
adamc@807 654 val dcons' = map (fn (x, n, to) =>
adamc@807 655 let
adamc@807 656 val args = ListUtil.mapi
adamc@807 657 (fn (i, _) => (L'.CRel (nxs - i), loc)) xs
adamc@807 658 val (e, t) =
adamc@807 659 case to of
adamc@807 660 NONE => ((L'.ECon (dk, L'.PConVar n, args, NONE),
adamc@807 661 loc), t)
adamc@807 662 | SOME t' => ((L'.EAbs ("x", t', t,
adamc@807 663 (L'.ECon (dk, L'.PConVar n,
adamc@807 664 args,
adamc@807 665 SOME (L'.ERel 0,
adamc@807 666 loc)),
adamc@807 667 loc)),
adamc@807 668 loc),
adamc@807 669 (L'.TFun (t', t), loc))
adamc@807 670
adamc@807 671 val t = foldr (fn (x, t) => (L'.TCFun (x, k, t), loc)) t xs
adamc@807 672 val e = foldr (fn (x, e) => (L'.ECAbs (x, k, e), loc)) e xs
adamc@807 673 in
adamc@807 674 (L'.DVal (x, n, t, e, ""), loc)
adamc@807 675 end) xncs
adamc@807 676 in
adamc@807 677 ((x, n, xs, xncs), (st, dcons' @ dcons))
adamc@807 678 end)
adamc@807 679 (st, []) dts
adamc@249 680 in
adamc@807 681 ((L'.DDatatype dts, loc) :: dcons, st)
adamc@807 682 end
adamc@249 683 | L.DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@249 684 let
adamc@249 685 val (st, n) = St.bindCon st x n
adamc@249 686 val c = corifyCon st (L.CModProj (m1, ms, s), loc)
adamc@177 687
adamc@249 688 val m = foldl (fn (x, m) => (L.StrProj (m, x), loc)) (L.StrVar m1, loc) ms
adamc@376 689 val (_, {inner, ...}) = corifyStr mods (m, st)
adamc@177 690
adamc@249 691 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
adamc@249 692 let
adamc@249 693 val n' = St.lookupConstructorByName inner x
adamc@249 694 val st = St.bindConstructor st x n n'
adamc@249 695 val (st, n) = St.bindVal st x n
adamc@249 696 val co = Option.map (corifyCon st) co
adamc@249 697 in
adamc@249 698 ((x, n, co), st)
adamc@249 699 end) st xncs
adamc@49 700
adamc@249 701 val nxs = length xs - 1
adamc@288 702 val cBase = c
adamc@249 703 val c = ListUtil.foldli (fn (i, _, c) => (L'.CApp (c, (L'.CRel (nxs - i), loc)), loc)) c xs
adamc@249 704 val k = (L'.KType, loc)
adamc@249 705 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
adamc@192 706
adamc@249 707 val cds = map (fn (x, n, co) =>
adamc@249 708 let
adamc@249 709 val t = case co of
adamc@249 710 NONE => c
adamc@249 711 | SOME t' => (L'.TFun (t', c), loc)
adamc@249 712 val e = corifyExp st (L.EModProj (m1, ms, x), loc)
adamc@192 713
adamc@249 714 val t = foldr (fn (x, t) => (L'.TCFun (x, k, t), loc)) t xs
adamc@249 715 in
adamc@249 716 (L'.DVal (x, n, t, e, x), loc)
adamc@249 717 end) xncs
adamc@249 718 in
adamc@288 719 ((L'.DCon (x, n, k', cBase), loc) :: cds, st)
adamc@249 720 end
adamc@249 721 | L.DVal (x, n, t, e) =>
adamc@249 722 let
adamc@249 723 val (st, n) = St.bindVal st x n
adamc@768 724 val s = doRestify Settings.Url (mods, x)
adamc@249 725 in
adamc@249 726 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e, s), loc)], st)
adamc@249 727 end
adamc@249 728 | L.DValRec vis =>
adamc@249 729 let
adamc@249 730 val (vis, st) = ListUtil.foldlMap
adamc@249 731 (fn ((x, n, t, e), st) =>
adamc@249 732 let
adamc@249 733 val (st, n) = St.bindVal st x n
adamc@249 734 in
adamc@249 735 ((x, n, t, e), st)
adamc@249 736 end)
adamc@249 737 st vis
adamc@16 738
adamc@249 739 val vis = map
adamc@249 740 (fn (x, n, t, e) =>
adamc@249 741 let
adamc@768 742 val s = doRestify Settings.Url (mods, x)
adamc@249 743 in
adamc@249 744 (x, n, corifyCon st t, corifyExp st e, s)
adamc@249 745 end)
adamc@249 746 vis
adamc@249 747 in
adamc@249 748 ([(L'.DValRec vis, loc)], st)
adamc@249 749 end
adamc@249 750 | L.DSgn _ => ([], st)
adamc@177 751
adamc@249 752 | L.DStr (x, n, _, (L.StrFun (xa, na, _, _, str), _)) =>
adamc@423 753 ([], St.bindFunctor st x n xa na str)
adamc@177 754
adamc@339 755 | L.DStr (x, n, _, (L.StrProj (str, x'), _)) =>
adamc@339 756 let
adamc@376 757 val (ds, {inner, outer}) = corifyStr mods (str, st)
adamc@339 758
adamc@339 759 val st = case St.lookupStrByNameOpt (x', inner) of
adamc@339 760 SOME st' => St.bindStr st x n st'
adamc@339 761 | NONE =>
adamc@339 762 let
adamc@423 763 val (x', n', str') = St.lookupFunctorByName (x', inner)
adamc@339 764 in
adamc@423 765 St.bindFunctor st x n x' n' str'
adamc@339 766 end
adamc@339 767 in
adamc@339 768 ([], st)
adamc@339 769 end
adamc@339 770
adamc@249 771 | L.DStr (x, n, _, str) =>
adamc@249 772 let
adamc@377 773 val mods' =
adamc@377 774 if x = "anon" then
adamc@377 775 mods
adamc@377 776 else
adamc@377 777 x :: mods
adamc@377 778
adamc@377 779 val (ds, {inner, outer}) = corifyStr mods' (str, st)
adamc@249 780 val st = St.bindStr outer x n inner
adamc@249 781 in
adamc@249 782 (ds, st)
adamc@249 783 end
adamc@16 784
adamc@249 785 | L.DFfiStr (m, n, (sgn, _)) =>
adamc@249 786 (case sgn of
adamc@249 787 L.SgnConst sgis =>
adamc@249 788 let
adamc@456 789 val (ds, cmap, conmap, st, _) =
adamc@456 790 foldl (fn ((sgi, _), (ds, cmap, conmap, st, trans)) =>
adamc@249 791 case sgi of
adamc@249 792 L.SgiConAbs (x, n, k) =>
adamc@249 793 let
adamc@249 794 val (st, n') = St.bindCon st x n
adamc@456 795
adamc@456 796 val trans =
adamc@456 797 if x = "transaction" then
adamc@456 798 SOME n
adamc@456 799 else
adamc@456 800 trans
adamc@249 801 in
adamc@249 802 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@249 803 cmap,
adamc@249 804 conmap,
adamc@456 805 st,
adamc@456 806 trans)
adamc@249 807 end
adamc@249 808 | L.SgiCon (x, n, k, _) =>
adamc@249 809 let
adamc@249 810 val (st, n') = St.bindCon st x n
adamc@249 811 in
adamc@249 812 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@249 813 cmap,
adamc@249 814 conmap,
adamc@456 815 st,
adamc@456 816 trans)
adamc@249 817 end
adamc@177 818
adamc@807 819 | L.SgiDatatype dts =>
adamc@249 820 let
adamc@249 821 val k = (L'.KType, loc)
adamc@807 822
adamc@807 823 val (dts, (ds', st, cmap, conmap)) =
adamc@249 824 ListUtil.foldlMap
adamc@807 825 (fn ((x, n, xs, xnts), (ds', st, cmap, conmap)) =>
adamc@249 826 let
adamc@813 827 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc))
adamc@813 828 k xs
adamc@813 829
adamc@807 830 val dk = ElabUtil.classifyDatatype xnts
adamc@807 831 val (st, n') = St.bindCon st x n
adamc@807 832 val (xnts, (ds', st, cmap, conmap)) =
adamc@807 833 ListUtil.foldlMap
adamc@807 834 (fn ((x', n, to), (ds', st, cmap, conmap)) =>
adamc@807 835 let
adamc@807 836 val dt = (L'.CNamed n', loc)
adamc@807 837 val args = ListUtil.mapi (fn (i, _) => (L'.CRel i, loc)) xs
adamc@163 838
adamc@807 839 val to = Option.map (corifyCon st) to
adamc@177 840
adamc@807 841 val pc = L'.PConFfi {mod = m,
adamc@807 842 datatyp = x,
adamc@807 843 params = xs,
adamc@807 844 con = x',
adamc@807 845 arg = to,
adamc@807 846 kind = dk}
adamc@130 847
adamc@807 848 fun wrapT t =
adamc@807 849 foldr (fn (x, t) => (L'.TCFun (x, k, t), loc))
adamc@807 850 t xs
adamc@807 851 fun wrapE e =
adamc@807 852 foldr (fn (x, e) => (L'.ECAbs (x, k, e), loc))
adamc@807 853 e xs
adamc@192 854
adamc@807 855 val (cmap, d) =
adamc@807 856 case to of
adamc@807 857 NONE => (SM.insert (cmap, x', wrapT dt),
adamc@807 858 (L'.DVal (x', n, wrapT dt,
adamc@807 859 wrapE
adamc@807 860 (L'.ECon (dk, pc,
adamc@807 861 args,
adamc@807 862 NONE),
adamc@807 863 loc),
adamc@807 864 ""), loc))
adamc@807 865 | SOME t =>
adamc@807 866 let
adamc@807 867 val tf = (L'.TFun (t, dt), loc)
adamc@807 868 val e = wrapE
adamc@807 869 (L'.EAbs ("x", t, tf,
adamc@807 870 (L'.ECon (dk,
adamc@807 871 pc,
adamc@807 872 args,
adamc@807 873 SOME
adamc@807 874 (L'.ERel 0,
adamc@807 875 loc)),
adamc@807 876 loc)), loc)
adamc@807 877 val d = (L'.DVal (x', n, wrapT tf,
adamc@807 878 e, ""), loc)
adamc@807 879 in
adamc@807 880 (SM.insert (cmap, x', wrapT tf), d)
adamc@807 881 end
adamc@185 882
adamc@807 883 val st = St.bindConstructor st x' n pc
adamc@807 884
adamc@807 885 val conmap = SM.insert (conmap, x',
adamc@807 886 (x, xs, to, dk))
adamc@807 887 in
adamc@807 888 ((x', n, to),
adamc@807 889 (d :: ds', st, cmap, conmap))
adamc@807 890 end) (ds', st, cmap, conmap) xnts
adamc@813 891
adamc@813 892 val d = (L'.DCon (x, n', k', (L'.CFfi (m, x), loc)), loc)
adamc@185 893 in
adamc@813 894 ((x, n', xs, xnts), (d :: ds', st, cmap, conmap))
adamc@807 895 end)
adamc@807 896 ([], st, cmap, conmap) dts
adamc@185 897 in
adamc@813 898 (List.revAppend (ds', ds),
adamc@185 899 cmap,
adamc@185 900 conmap,
adamc@456 901 st,
adamc@456 902 trans)
adamc@807 903 end
adamc@49 904
adamc@49 905 | L.SgiVal (x, _, c) =>
adamc@456 906 let
adamc@456 907 val c =
adamc@456 908 case trans of
adamc@456 909 NONE => corifyCon st c
adamc@456 910 | SOME trans =>
adamc@456 911 let
adamc@456 912 fun transactify (all as (c, loc)) =
adamc@456 913 case c of
adamc@456 914 L.TFun (dom, ran) =>
adamc@456 915 (L'.TFun (corifyCon st dom, transactify ran), loc)
adamc@456 916 | L.CApp ((L.CNamed trans', _), t) =>
adamc@456 917 if trans' = trans then
adamc@456 918 (L'.CApp ((L'.CFfi (m, "transaction"), loc),
adamc@456 919 corifyCon st t), loc)
adamc@456 920 else
adamc@456 921 corifyCon st all
adamc@456 922 | _ => corifyCon st all
adamc@456 923 in
adamc@456 924 transactify c
adamc@456 925 end
adamc@456 926 in
adamc@456 927 (ds,
adamc@456 928 SM.insert (cmap, x, c),
adamc@456 929 conmap,
adamc@456 930 st,
adamc@456 931 trans)
adamc@456 932 end
adamc@456 933 | _ => (ds, cmap, conmap, st, trans))
adamc@456 934 ([], SM.empty, SM.empty, st, NONE) sgis
adamc@49 935
adamc@185 936 val st = St.bindStr st m n (St.ffi m cmap conmap)
adamc@49 937 in
adamc@764 938 (rev ds, if m = "Basis" then St.basisIs (st, n) else st)
adamc@49 939 end
adamc@49 940 | _ => raise Fail "Non-const signature for FFI structure")
adamc@48 941
adamc@109 942 | L.DExport (en, sgn, str) =>
adamc@109 943 (case #1 sgn of
adamc@109 944 L.SgnConst sgis =>
adamc@109 945 let
adamc@109 946 fun pathify (str, _) =
adamc@109 947 case str of
adamc@109 948 L.StrVar m => SOME (m, [])
adamc@109 949 | L.StrProj (str, s) =>
adamc@109 950 Option.map (fn (m, ms) => (m, ms @ [s])) (pathify str)
adamc@109 951 | _ => NONE
adamc@109 952 in
adamc@109 953 case pathify str of
adamc@109 954 NONE => (ErrorMsg.errorAt loc "Structure is too fancy to export";
adamc@109 955 ([], st))
adamc@109 956 | SOME (m, ms) =>
adamc@109 957 let
adamc@249 958 val basis_n = case St.lookupBasis st of
adamc@249 959 NONE => raise Fail "Corify: Don't know number of Basis"
adamc@249 960 | SOME n => n
adamc@249 961
adamc@109 962 fun wrapSgi ((sgi, _), (wds, eds)) =
adamc@109 963 case sgi of
adamc@109 964 L.SgiVal (s, _, t as (L.TFun (dom, ran), _)) =>
adamc@109 965 (case (#1 dom, #1 ran) of
adamc@249 966 (L.TRecord _,
adamc@249 967 L.CApp ((L.CModProj (basis, [], "transaction"), _),
adamc@249 968 ran' as
adamc@249 969 (L.CApp
adamc@249 970 ((L.CApp
adamc@720 971 ((L.CApp ((L.CModProj (basis', [], "xml"), _),
adamc@720 972 (L.CRecord (_, [((L.CName "Html", _),
adamc@720 973 _)]), _)), _), _),
adamc@720 974 _), _), _))) =>
adamc@109 975 let
adamc@109 976 val ran = (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc)
adamc@249 977 val ranT = (L.CApp ((L.CModProj (basis, [], "transaction"), loc),
adamc@249 978 ran), loc)
adamc@109 979 val e = (L.EModProj (m, ms, s), loc)
adamc@249 980
adamc@249 981 val ef = (L.EModProj (basis, [], "bind"), loc)
adamc@564 982 val ef = (L.ECApp (ef, (L.CModProj (basis, [], "transaction"), loc)), loc)
adamc@249 983 val ef = (L.ECApp (ef, ran'), loc)
adamc@251 984 val ef = (L.ECApp (ef, ran), loc)
adamc@564 985 val ef = (L.EApp (ef, (L.EModProj (basis, [], "transaction_monad"), loc)), loc)
adamc@249 986 val ef = (L.EApp (ef, (L.EApp (e, (L.ERel 0, loc)), loc)), loc)
adamc@249 987
adamc@249 988 val eat = (L.CApp ((L.CModProj (basis, [], "transaction"), loc),
adamc@249 989 ran), loc)
adamc@249 990 val ea = (L.EAbs ("p", ran', eat,
adamc@249 991 (L.EWrite (L.ERel 0, loc), loc)), loc)
adamc@249 992
adamc@249 993 val e = (L.EApp (ef, ea), loc)
adamc@249 994 val e = (L.EAbs ("vs", dom, ran, e), loc)
adamc@109 995 in
adamc@249 996 if basis = basis_n andalso basis' = basis_n then
adamc@249 997 ((L.DVal ("wrap_" ^ s, 0,
adamc@249 998 (L.TFun (dom, ranT), loc),
adamc@249 999 e), loc) :: wds,
adamc@249 1000 (fn st =>
adamc@249 1001 case #1 (corifyExp st (L.EModProj (en, [], "wrap_" ^ s), loc)) of
adamc@249 1002 L'.ENamed n => (L'.DExport (L'.Link, n), loc)
adamc@249 1003 | _ => raise Fail "Corify: Value to export didn't corify properly")
adamc@249 1004 :: eds)
adamc@249 1005 else
adamc@249 1006 (wds, eds)
adamc@109 1007 end
adamc@109 1008 | _ => (wds, eds))
adamc@109 1009 | _ => (wds, eds)
adamc@102 1010
adamc@109 1011 val (wds, eds) = foldl wrapSgi ([], []) sgis
adamc@109 1012 val wrapper = (L.StrConst wds, loc)
adamc@376 1013 val mst = St.lookupStrById st m
adamc@376 1014 val (ds, {inner, outer}) = corifyStr (St.name mst) (wrapper, st)
adamc@109 1015 val st = St.bindStr outer "wrapper" en inner
adamc@249 1016
adamc@109 1017 val ds = ds @ map (fn f => f st) eds
adamc@109 1018 in
adamc@109 1019 (ds, st)
adamc@109 1020 end
adamc@109 1021 end
adamc@109 1022 | _ => raise Fail "Non-const signature for 'export'")
adamc@48 1023
adamc@707 1024 | L.DTable (_, x, n, c, pe, pc, ce, cc) =>
adamc@249 1025 let
adamc@249 1026 val (st, n) = St.bindVal st x n
adamc@768 1027 val s = relify (doRestify Settings.Table (mods, x))
adamc@249 1028 in
adamc@707 1029 ([(L'.DTable (x, n, corifyCon st c, s,
adamc@707 1030 corifyExp st pe, corifyCon st pc,
adamc@707 1031 corifyExp st ce, corifyCon st cc), loc)], st)
adamc@249 1032 end
adamc@338 1033 | L.DSequence (_, x, n) =>
adamc@338 1034 let
adamc@338 1035 val (st, n) = St.bindVal st x n
adamc@768 1036 val s = relify (doRestify Settings.Sequence (mods, x))
adamc@338 1037 in
adamc@338 1038 ([(L'.DSequence (x, n, s), loc)], st)
adamc@338 1039 end
adamc@754 1040 | L.DView (_, x, n, e, c) =>
adamc@754 1041 let
adamc@754 1042 val (st, n) = St.bindVal st x n
adamc@768 1043 val s = relify (doRestify Settings.View (mods, x))
adamc@754 1044 in
adamc@754 1045 ([(L'.DView (x, n, s, corifyExp st e, corifyCon st c), loc)], st)
adamc@754 1046 end
adamc@246 1047
adamc@271 1048 | L.DDatabase s => ([(L'.DDatabase s, loc)], st)
adamc@271 1049
adamc@461 1050 | L.DCookie (_, x, n, c) =>
adamc@461 1051 let
adamc@461 1052 val (st, n) = St.bindVal st x n
adamc@768 1053 val s = doRestify Settings.Cookie (mods, x)
adamc@461 1054 in
adamc@461 1055 ([(L'.DCookie (x, n, corifyCon st c, s), loc)], st)
adamc@461 1056 end
adamc@720 1057 | L.DStyle (_, x, n) =>
adamc@718 1058 let
adamc@718 1059 val (st, n) = St.bindVal st x n
adamc@768 1060 val s = relify (doRestify Settings.Style (mods, x))
adamc@718 1061 in
adamc@720 1062 ([(L'.DStyle (x, n, s), loc)], st)
adamc@718 1063 end
adamc@461 1064
adamc@376 1065 and corifyStr mods ((str, _), st) =
adamc@39 1066 case str of
adamc@39 1067 L.StrConst ds =>
adamc@39 1068 let
adamc@376 1069 val st = St.enter (st, mods)
adamc@376 1070 val (ds, st) = ListUtil.foldlMapConcat (corifyDecl mods) st ds
adamc@39 1071 in
adamc@39 1072 (ds, St.leave st)
adamc@39 1073 end
adamc@39 1074 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
adamc@39 1075 | L.StrProj (str, x) =>
adamc@39 1076 let
adamc@376 1077 val (ds, {inner, outer}) = corifyStr mods (str, st)
adamc@39 1078 in
adamc@39 1079 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
adamc@39 1080 end
adamc@46 1081 | L.StrFun _ => raise Fail "Corify of nested functor definition"
adamc@46 1082 | L.StrApp (str1, str2) =>
adamc@46 1083 let
adamc@46 1084 fun unwind' (str, _) =
adamc@46 1085 case str of
adamc@46 1086 L.StrVar n => St.lookupStrById st n
adamc@46 1087 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
adamc@46 1088 | _ => raise Fail "Corify of fancy functor application [1]"
adamc@46 1089
adamc@46 1090 fun unwind (str, _) =
adamc@46 1091 case str of
adamc@46 1092 L.StrVar n => St.lookupFunctorById st n
adamc@46 1093 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
adamc@46 1094 | _ => raise Fail "Corify of fancy functor application [2]"
adamc@46 1095
adamc@423 1096 val (xa, na, body) = unwind str1
adamc@46 1097
adamc@376 1098 val (ds1, {inner = inner', outer}) = corifyStr mods (str2, st)
adamc@376 1099
adamc@423 1100 val (ds2, {inner, outer}) = corifyStr mods (body, St.bindStr outer xa na inner')
adamc@46 1101 in
adamc@146 1102 (ds1 @ ds2, {inner = St.bindStr inner xa na inner', outer = outer})
adamc@46 1103 end
adamc@31 1104
adamc@39 1105 fun maxName ds = foldl (fn ((d, _), n) =>
adamc@39 1106 case d of
adamc@39 1107 L.DCon (_, n', _, _) => Int.max (n, n')
adamc@806 1108 | L.DDatatype dts => foldl (fn ((_, n', _, _), n) => Int.max (n, n')) n dts
adamc@191 1109 | L.DDatatypeImp (_, n', _, _, _, _, _) => Int.max (n, n')
adamc@124 1110 | L.DVal (_, n', _, _) => Int.max (n, n')
adamc@124 1111 | L.DValRec vis => foldl (fn ((_, n', _, _), n) => Int.max (n, n)) n vis
adamc@39 1112 | L.DSgn (_, n', _) => Int.max (n, n')
adamc@48 1113 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
adamc@100 1114 | L.DFfiStr (_, n', _) => Int.max (n, n')
adamc@246 1115 | L.DExport _ => n
adamc@707 1116 | L.DTable (_, _, n', _, _, _, _, _) => Int.max (n, n')
adamc@338 1117 | L.DSequence (_, _, n') => Int.max (n, n')
adamc@754 1118 | L.DView (_, _, n', _, _) => Int.max (n, n')
adamc@461 1119 | L.DDatabase _ => n
adamc@718 1120 | L.DCookie (_, _, n', _) => Int.max (n, n')
adamc@720 1121 | L.DStyle (_, _, n') => Int.max (n, n'))
adamc@249 1122 0 ds
adamc@39 1123
adamc@39 1124 and maxNameStr (str, _) =
adamc@39 1125 case str of
adamc@39 1126 L.StrConst ds => maxName ds
adamc@39 1127 | L.StrVar n => n
adamc@39 1128 | L.StrProj (str, _) => maxNameStr str
adamc@45 1129 | L.StrFun (_, _, _, _, str) => maxNameStr str
adamc@45 1130 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@39 1131
adamc@39 1132 fun corify ds =
adamc@39 1133 let
adamc@39 1134 val () = reset (maxName ds + 1)
adamc@146 1135
adamc@376 1136 val (ds, _) = ListUtil.foldlMapConcat (corifyDecl []) St.empty ds
adamc@39 1137 in
adamc@39 1138 ds
adamc@39 1139 end
adamc@16 1140
adamc@16 1141 end