annotate src/corify.sml @ 322:aa89b73d83e4

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