annotate src/corify.sml @ 79:37847b504cc6

More hardcore unification
author Adam Chlipala <adamc@hcoop.net>
date Sun, 29 Jun 2008 11:20:34 -0400
parents 8b611ecc5f2d
children 275aaeb73f1f
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@39 61 val enter : t -> t
adamc@39 62 val leave : t -> {outer : t, inner : t}
adamc@49 63 val ffi : string -> L'.con SM.map -> t
adamc@39 64
adamc@73 65 datatype core_con =
adamc@73 66 CNormal of int
adamc@73 67 | CFfi of string
adamc@73 68 val bindCon : t -> string -> int -> t * int
adamc@73 69 val lookupConById : t -> int -> int option
adamc@73 70 val lookupConByName : t -> string -> core_con
adamc@48 71
adamc@73 72 datatype core_val =
adamc@73 73 ENormal of int
adamc@73 74 | EFfi of string * L'.con
adamc@73 75 val bindVal : t -> string -> int -> t * int
adamc@73 76 val lookupValById : t -> int -> int option
adamc@73 77 val lookupValByName : t -> string -> core_val
adamc@39 78
adamc@39 79 val bindStr : t -> string -> int -> t -> t
adamc@39 80 val lookupStrById : t -> int -> t
adamc@39 81 val lookupStrByName : string * t -> t
adamc@46 82
adamc@46 83 val bindFunctor : t -> string -> int -> int -> L.str -> t
adamc@46 84 val lookupFunctorById : t -> int -> int * L.str
adamc@46 85 val lookupFunctorByName : string * t -> int * L.str
adamc@39 86 end = struct
adamc@39 87
adamc@48 88 datatype flattening =
adamc@73 89 FNormal of {cons : int SM.map,
adamc@73 90 vals : int SM.map,
adamc@48 91 strs : flattening SM.map,
adamc@48 92 funs : (int * L.str) SM.map}
adamc@73 93 | FFfi of {mod : string,
adamc@73 94 vals : L'.con SM.map}
adamc@39 95
adamc@39 96 type t = {
adamc@73 97 cons : int IM.map,
adamc@73 98 vals : int IM.map,
adamc@39 99 strs : flattening IM.map,
adamc@46 100 funs : (int * L.str) IM.map,
adamc@39 101 current : flattening,
adamc@39 102 nested : flattening list
adamc@39 103 }
adamc@39 104
adamc@39 105 val empty = {
adamc@73 106 cons = IM.empty,
adamc@73 107 vals = IM.empty,
adamc@39 108 strs = IM.empty,
adamc@46 109 funs = IM.empty,
adamc@73 110 current = FNormal { cons = SM.empty, vals = SM.empty, strs = SM.empty, funs = SM.empty },
adamc@39 111 nested = []
adamc@39 112 }
adamc@39 113
adamc@73 114 datatype core_con =
adamc@73 115 CNormal of int
adamc@73 116 | CFfi of string
adamc@48 117
adamc@73 118 datatype core_val =
adamc@73 119 ENormal of int
adamc@73 120 | EFfi of string * L'.con
adamc@73 121
adamc@73 122 fun bindCon {cons, vals, strs, funs, current, nested} s n =
adamc@39 123 let
adamc@39 124 val n' = alloc ()
adamc@39 125
adamc@39 126 val current =
adamc@48 127 case current of
adamc@48 128 FFfi _ => raise Fail "Binding inside FFfi"
adamc@73 129 | FNormal {cons, vals, strs, funs} =>
adamc@73 130 FNormal {cons = SM.insert (cons, s, n'),
adamc@73 131 vals = vals,
adamc@48 132 strs = strs,
adamc@48 133 funs = funs}
adamc@39 134 in
adamc@73 135 ({cons = IM.insert (cons, n, n'),
adamc@73 136 vals = vals,
adamc@39 137 strs = strs,
adamc@46 138 funs = funs,
adamc@39 139 current = current,
adamc@39 140 nested = nested},
adamc@39 141 n')
adamc@39 142 end
adamc@39 143
adamc@73 144 fun lookupConById ({cons, ...} : t) n = IM.find (cons, n)
adamc@39 145
adamc@73 146 fun lookupConByName ({current, ...} : t) x =
adamc@48 147 case current of
adamc@73 148 FFfi {mod = m, ...} => CFfi m
adamc@73 149 | FNormal {cons, ...} =>
adamc@73 150 case SM.find (cons, x) of
adamc@73 151 NONE => raise Fail "Corify.St.lookupConByName"
adamc@73 152 | SOME n => CNormal n
adamc@39 153
adamc@73 154 fun bindVal {cons, vals, strs, funs, current, nested} s n =
adamc@73 155 let
adamc@73 156 val n' = alloc ()
adamc@73 157
adamc@73 158 val current =
adamc@73 159 case current of
adamc@73 160 FFfi _ => raise Fail "Binding inside FFfi"
adamc@73 161 | FNormal {cons, vals, strs, funs} =>
adamc@73 162 FNormal {cons = cons,
adamc@73 163 vals = SM.insert (vals, s, n'),
adamc@73 164 strs = strs,
adamc@73 165 funs = funs}
adamc@73 166 in
adamc@73 167 ({cons = cons,
adamc@73 168 vals = IM.insert (vals, n, n'),
adamc@73 169 strs = strs,
adamc@73 170 funs = funs,
adamc@73 171 current = current,
adamc@73 172 nested = nested},
adamc@73 173 n')
adamc@73 174 end
adamc@73 175
adamc@73 176 fun lookupValById ({vals, ...} : t) n = IM.find (vals, n)
adamc@73 177
adamc@73 178 fun lookupValByName ({current, ...} : t) x =
adamc@73 179 case current of
adamc@73 180 FFfi {mod = m, vals, ...} =>
adamc@73 181 (case SM.find (vals, x) of
adamc@73 182 NONE => raise Fail "Corify.St.lookupValByName: no type for FFI val"
adamc@73 183 | SOME t => EFfi (m, t))
adamc@73 184 | FNormal {vals, ...} =>
adamc@73 185 case SM.find (vals, x) of
adamc@73 186 NONE => raise Fail "Corify.St.lookupValByName"
adamc@73 187 | SOME n => ENormal n
adamc@73 188
adamc@73 189 fun enter {cons, vals, strs, funs, current, nested} =
adamc@73 190 {cons = cons,
adamc@73 191 vals = vals,
adamc@39 192 strs = strs,
adamc@46 193 funs = funs,
adamc@73 194 current = FNormal {cons = SM.empty,
adamc@73 195 vals = SM.empty,
adamc@48 196 strs = SM.empty,
adamc@48 197 funs = SM.empty},
adamc@39 198 nested = current :: nested}
adamc@39 199
adamc@73 200 fun dummy f = {cons = IM.empty,
adamc@73 201 vals = IM.empty,
adamc@39 202 strs = IM.empty,
adamc@46 203 funs = IM.empty,
adamc@39 204 current = f,
adamc@39 205 nested = []}
adamc@39 206
adamc@73 207 fun leave {cons, vals, strs, funs, current, nested = m1 :: rest} =
adamc@73 208 {outer = {cons = cons,
adamc@73 209 vals = vals,
adamc@39 210 strs = strs,
adamc@46 211 funs = funs,
adamc@39 212 current = m1,
adamc@39 213 nested = rest},
adamc@39 214 inner = dummy current}
adamc@39 215 | leave _ = raise Fail "Corify.St.leave"
adamc@39 216
adamc@73 217 fun ffi m vals = dummy (FFfi {mod = m, vals = vals})
adamc@48 218
adamc@73 219 fun bindStr ({cons, vals, strs, funs,
adamc@73 220 current = FNormal {cons = mcons, vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@46 221 x n ({current = f, ...} : t) =
adamc@73 222 {cons = cons,
adamc@73 223 vals = vals,
adamc@39 224 strs = IM.insert (strs, n, f),
adamc@46 225 funs = funs,
adamc@73 226 current = FNormal {cons = mcons,
adamc@73 227 vals = mvals,
adamc@73 228 strs = SM.insert (mstrs, x, f),
adamc@73 229 funs = mfuns},
adamc@39 230 nested = nested}
adamc@48 231 | bindStr _ _ _ _ = raise Fail "Corify.St.bindStr"
adamc@39 232
adamc@39 233 fun lookupStrById ({strs, ...} : t) n =
adamc@39 234 case IM.find (strs, n) of
adamc@46 235 NONE => raise Fail "Corify.St.lookupStrById"
adamc@39 236 | SOME f => dummy f
adamc@39 237
adamc@48 238 fun lookupStrByName (m, {current = FNormal {strs, ...}, ...} : t) =
adamc@48 239 (case SM.find (strs, m) of
adamc@48 240 NONE => raise Fail "Corify.St.lookupStrByName"
adamc@48 241 | SOME f => dummy f)
adamc@48 242 | lookupStrByName _ = raise Fail "Corify.St.lookupStrByName"
adamc@39 243
adamc@73 244 fun bindFunctor ({cons, vals, strs, funs,
adamc@73 245 current = FNormal {cons = mcons, vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
adamc@46 246 x n na str =
adamc@73 247 {cons = cons,
adamc@73 248 vals = vals,
adamc@46 249 strs = strs,
adamc@46 250 funs = IM.insert (funs, n, (na, str)),
adamc@73 251 current = FNormal {cons = mcons,
adamc@73 252 vals = mvals,
adamc@48 253 strs = mstrs,
adamc@48 254 funs = SM.insert (mfuns, x, (na, str))},
adamc@46 255 nested = nested}
adamc@48 256 | bindFunctor _ _ _ _ _ = raise Fail "Corify.St.bindFunctor"
adamc@46 257
adamc@46 258 fun lookupFunctorById ({funs, ...} : t) n =
adamc@46 259 case IM.find (funs, n) of
adamc@46 260 NONE => raise Fail "Corify.St.lookupFunctorById"
adamc@46 261 | SOME v => v
adamc@46 262
adamc@48 263 fun lookupFunctorByName (m, {current = FNormal {funs, ...}, ...} : t) =
adamc@48 264 (case SM.find (funs, m) of
adamc@48 265 NONE => raise Fail "Corify.St.lookupFunctorByName"
adamc@48 266 | SOME v => v)
adamc@48 267 | lookupFunctorByName _ = raise Fail "Corify.St.lookupFunctorByName"
adamc@46 268
adamc@39 269 end
adamc@39 270
adamc@39 271
adamc@16 272 fun corifyKind (k, loc) =
adamc@16 273 case k of
adamc@16 274 L.KType => (L'.KType, loc)
adamc@16 275 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
adamc@16 276 | L.KName => (L'.KName, loc)
adamc@16 277 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
adamc@16 278
adamc@39 279 fun corifyCon st (c, loc) =
adamc@16 280 case c of
adamc@39 281 L.TFun (t1, t2) => (L'.TFun (corifyCon st t1, corifyCon st t2), loc)
adamc@39 282 | L.TCFun (x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon st t), loc)
adamc@39 283 | L.TRecord c => (L'.TRecord (corifyCon st c), loc)
adamc@16 284
adamc@16 285 | L.CRel n => (L'.CRel n, loc)
adamc@39 286 | L.CNamed n =>
adamc@73 287 (case St.lookupConById st n of
adamc@39 288 NONE => (L'.CNamed n, loc)
adamc@39 289 | SOME n => (L'.CNamed n, loc))
adamc@39 290 | L.CModProj (m, ms, x) =>
adamc@39 291 let
adamc@39 292 val st = St.lookupStrById st m
adamc@39 293 val st = foldl St.lookupStrByName st ms
adamc@39 294 in
adamc@73 295 case St.lookupConByName st x of
adamc@73 296 St.CNormal n => (L'.CNamed n, loc)
adamc@73 297 | St.CFfi m => (L'.CFfi (m, x), loc)
adamc@39 298 end
adamc@34 299
adamc@39 300 | L.CApp (c1, c2) => (L'.CApp (corifyCon st c1, corifyCon st c2), loc)
adamc@39 301 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon st c), loc)
adamc@16 302
adamc@16 303 | L.CName s => (L'.CName s, loc)
adamc@16 304
adamc@39 305 | L.CRecord (k, xcs) =>
adamc@39 306 (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon st c1, corifyCon st c2)) xcs), loc)
adamc@39 307 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon st c1, corifyCon st c2), loc)
adamc@69 308 | L.CFold (k1, k2) => (L'.CFold (corifyKind k1, corifyKind k2), loc)
adamc@16 309
adamc@39 310 fun corifyExp st (e, loc) =
adamc@16 311 case e of
adamc@16 312 L.EPrim p => (L'.EPrim p, loc)
adamc@16 313 | L.ERel n => (L'.ERel n, loc)
adamc@39 314 | L.ENamed n =>
adamc@73 315 (case St.lookupValById st n of
adamc@39 316 NONE => (L'.ENamed n, loc)
adamc@39 317 | SOME n => (L'.ENamed n, loc))
adamc@39 318 | L.EModProj (m, ms, x) =>
adamc@39 319 let
adamc@39 320 val st = St.lookupStrById st m
adamc@39 321 val st = foldl St.lookupStrByName st ms
adamc@39 322 in
adamc@73 323 case St.lookupValByName st x of
adamc@73 324 St.ENormal n => (L'.ENamed n, loc)
adamc@73 325 | St.EFfi (m, t) =>
adamc@49 326 case t of
adamc@50 327 (L'.TFun (dom as (L'.TRecord (L'.CRecord (_, []), _), _), ran), _) =>
adamc@50 328 (L'.EAbs ("arg", dom, ran, (L'.EFfiApp (m, x, []), loc)), loc)
adamc@50 329 | t as (L'.TFun _, _) =>
adamc@49 330 let
adamc@49 331 fun getArgs (all as (t, _), args) =
adamc@49 332 case t of
adamc@49 333 L'.TFun (dom, ran) => getArgs (ran, dom :: args)
adamc@49 334 | _ => (all, rev args)
adamc@49 335
adamc@49 336 val (result, args) = getArgs (t, [])
adamc@49 337
adamc@50 338 val (actuals, _) = foldr (fn (_, (actuals, n)) =>
adamc@50 339 ((L'.ERel n, loc) :: actuals,
adamc@50 340 n + 1)) ([], 0) args
adamc@50 341 val app = (L'.EFfiApp (m, x, actuals), loc)
adamc@49 342 val (abs, _, _) = foldr (fn (t, (abs, ran, n)) =>
adamc@49 343 ((L'.EAbs ("arg" ^ Int.toString n,
adamc@49 344 t,
adamc@49 345 ran,
adamc@49 346 abs), loc),
adamc@49 347 (L'.TFun (t, ran), loc),
adamc@49 348 n - 1)) (app, result, length args - 1) args
adamc@49 349 in
adamc@49 350 abs
adamc@49 351 end
adamc@49 352 | _ => (L'.EFfi (m, x), loc)
adamc@39 353 end
adamc@39 354 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
adamc@39 355 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
adamc@39 356 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
adamc@39 357 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
adamc@16 358
adamc@39 359 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) => (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
adamc@39 360 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
adamc@39 361 {field = corifyCon st field, rest = corifyCon st rest}), loc)
adamc@73 362 | L.EFold k => (L'.EFold (corifyKind k), loc)
adamc@16 363
adamc@39 364 fun corifyDecl ((d, loc : EM.span), st) =
adamc@39 365 case d of
adamc@39 366 L.DCon (x, n, k, c) =>
adamc@39 367 let
adamc@73 368 val (st, n) = St.bindCon st x n
adamc@39 369 in
adamc@39 370 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
adamc@39 371 end
adamc@39 372 | L.DVal (x, n, t, e) =>
adamc@39 373 let
adamc@73 374 val (st, n) = St.bindVal st x n
adamc@39 375 in
adamc@39 376 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e), loc)], st)
adamc@39 377 end
adamc@39 378
adamc@39 379 | L.DSgn _ => ([], st)
adamc@16 380
adamc@46 381 | L.DStr (x, n, _, (L.StrFun (_, na, _, _, str), _)) =>
adamc@46 382 ([], St.bindFunctor st x n na str)
adamc@46 383
adamc@39 384 | L.DStr (x, n, _, str) =>
adamc@39 385 let
adamc@39 386 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 387 val st = St.bindStr outer x n inner
adamc@39 388 in
adamc@39 389 (ds, st)
adamc@39 390 end
adamc@16 391
adamc@49 392 | L.DFfiStr (m, n, (sgn, _)) =>
adamc@49 393 (case sgn of
adamc@49 394 L.SgnConst sgis =>
adamc@49 395 let
adamc@49 396 val (ds, cmap, st) =
adamc@49 397 foldl (fn ((sgi, _), (ds, cmap, st)) =>
adamc@49 398 case sgi of
adamc@49 399 L.SgiConAbs (x, n, k) =>
adamc@49 400 let
adamc@73 401 val (st, n') = St.bindCon st x n
adamc@49 402 in
adamc@49 403 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 404 cmap,
adamc@49 405 st)
adamc@49 406 end
adamc@49 407 | L.SgiCon (x, n, k, _) =>
adamc@49 408 let
adamc@73 409 val (st, n') = St.bindCon st x n
adamc@49 410 in
adamc@49 411 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
adamc@49 412 cmap,
adamc@49 413 st)
adamc@49 414 end
adamc@49 415
adamc@49 416 | L.SgiVal (x, _, c) =>
adamc@49 417 (ds,
adamc@49 418 SM.insert (cmap, x, corifyCon st c),
adamc@49 419 st)
adamc@49 420 | _ => (ds, cmap, st)) ([], SM.empty, st) sgis
adamc@49 421
adamc@49 422 val st = St.bindStr st m n (St.ffi m cmap)
adamc@49 423 in
adamc@49 424 (rev ds, st)
adamc@49 425 end
adamc@49 426 | _ => raise Fail "Non-const signature for FFI structure")
adamc@48 427
adamc@48 428
adamc@39 429 and corifyStr ((str, _), st) =
adamc@39 430 case str of
adamc@39 431 L.StrConst ds =>
adamc@39 432 let
adamc@39 433 val st = St.enter st
adamc@39 434 val (ds, st) = ListUtil.foldlMapConcat corifyDecl st ds
adamc@39 435 in
adamc@39 436 (ds, St.leave st)
adamc@39 437 end
adamc@39 438 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
adamc@39 439 | L.StrProj (str, x) =>
adamc@39 440 let
adamc@39 441 val (ds, {inner, outer}) = corifyStr (str, st)
adamc@39 442 in
adamc@39 443 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
adamc@39 444 end
adamc@46 445 | L.StrFun _ => raise Fail "Corify of nested functor definition"
adamc@46 446 | L.StrApp (str1, str2) =>
adamc@46 447 let
adamc@46 448 fun unwind' (str, _) =
adamc@46 449 case str of
adamc@46 450 L.StrVar n => St.lookupStrById st n
adamc@46 451 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
adamc@46 452 | _ => raise Fail "Corify of fancy functor application [1]"
adamc@46 453
adamc@46 454 fun unwind (str, _) =
adamc@46 455 case str of
adamc@46 456 L.StrVar n => St.lookupFunctorById st n
adamc@46 457 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
adamc@46 458 | _ => raise Fail "Corify of fancy functor application [2]"
adamc@46 459
adamc@46 460 val (na, body) = unwind str1
adamc@46 461
adamc@46 462 val (ds1, {inner, outer}) = corifyStr (str2, st)
adamc@46 463 val (ds2, sts) = corifyStr (body, St.bindStr outer "ARG" na inner)
adamc@46 464 in
adamc@46 465 (ds1 @ ds2, sts)
adamc@46 466 end
adamc@31 467
adamc@39 468 fun maxName ds = foldl (fn ((d, _), n) =>
adamc@39 469 case d of
adamc@39 470 L.DCon (_, n', _, _) => Int.max (n, n')
adamc@39 471 | L.DVal (_, n', _ , _) => Int.max (n, n')
adamc@39 472 | L.DSgn (_, n', _) => Int.max (n, n')
adamc@48 473 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
adamc@48 474 | L.DFfiStr (_, n', _) => Int.max (n, n'))
adamc@39 475 0 ds
adamc@39 476
adamc@39 477 and maxNameStr (str, _) =
adamc@39 478 case str of
adamc@39 479 L.StrConst ds => maxName ds
adamc@39 480 | L.StrVar n => n
adamc@39 481 | L.StrProj (str, _) => maxNameStr str
adamc@45 482 | L.StrFun (_, _, _, _, str) => maxNameStr str
adamc@45 483 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
adamc@39 484
adamc@39 485 fun corify ds =
adamc@39 486 let
adamc@39 487 val () = reset (maxName ds + 1)
adamc@39 488 val (ds, _) = ListUtil.foldlMapConcat corifyDecl St.empty ds
adamc@39 489 in
adamc@39 490 ds
adamc@39 491 end
adamc@16 492
adamc@16 493 end