annotate src/corify.sml @ 343:ca98644af384

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