annotate src/elab_env.sml @ 752:bc5cfd6cb30f

Outer joins in CHANGELOG
author Adam Chlipala <adamc@hcoop.net>
date Tue, 28 Apr 2009 11:18:27 -0400
parents 059074c8d2fc
children d484df4e841a
rev   line source
adamc@674 1 (* Copyright (c) 2008-2009, Adam Chlipala
adamc@2 2 * All rights reserved.
adamc@2 3 *
adamc@2 4 * Redistribution and use in source and binary forms, with or without
adamc@2 5 * modification, are permitted provided that the following conditions are met:
adamc@2 6 *
adamc@2 7 * - Redistributions of source code must retain the above copyright notice,
adamc@2 8 * this list of conditions and the following disclaimer.
adamc@2 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@2 10 * this list of conditions and the following disclaimer in the documentation
adamc@2 11 * and/or other materials provided with the distribution.
adamc@2 12 * - The names of contributors may not be used to endorse or promote products
adamc@2 13 * derived from this software without specific prior written permission.
adamc@2 14 *
adamc@2 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@2 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@2 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@2 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@2 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@2 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@2 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@2 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@2 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@2 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@2 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@2 26 *)
adamc@2 27
adamc@2 28 structure ElabEnv :> ELAB_ENV = struct
adamc@2 29
adamc@2 30 open Elab
adamc@2 31
adamc@13 32 structure U = ElabUtil
adamc@13 33
adamc@2 34 structure IM = IntBinaryMap
adamc@2 35 structure SM = BinaryMapFn(struct
adamc@2 36 type ord_key = string
adamc@2 37 val compare = String.compare
adamc@2 38 end)
adamc@2 39
adamc@2 40 exception UnboundRel of int
adamc@2 41 exception UnboundNamed of int
adamc@2 42
adamc@13 43
adamc@13 44 (* AST utility functions *)
adamc@13 45
adamc@13 46 exception SynUnif
adamc@13 47
adamc@623 48 val liftKindInKind =
adamc@623 49 U.Kind.mapB {kind = fn bound => fn k =>
adamc@623 50 case k of
adamc@623 51 KRel xn =>
adamc@623 52 if xn < bound then
adamc@623 53 k
adamc@623 54 else
adamc@623 55 KRel (xn + 1)
adamc@623 56 | _ => k,
adamc@623 57 bind = fn (bound, _) => bound + 1}
adamc@623 58
adamc@623 59 val liftKindInCon =
adamc@623 60 U.Con.mapB {kind = fn bound => fn k =>
adamc@623 61 case k of
adamc@623 62 KRel xn =>
adamc@623 63 if xn < bound then
adamc@623 64 k
adamc@623 65 else
adamc@623 66 KRel (xn + 1)
adamc@623 67 | _ => k,
adamc@623 68 con = fn _ => fn c => c,
adamc@623 69 bind = fn (bound, U.Con.RelK _) => bound + 1
adamc@623 70 | (bound, _) => bound}
adamc@623 71
adamc@13 72 val liftConInCon =
adamc@623 73 U.Con.mapB {kind = fn _ => fn k => k,
adamc@13 74 con = fn bound => fn c =>
adamc@13 75 case c of
adamc@16 76 CRel xn =>
adamc@13 77 if xn < bound then
adamc@13 78 c
adamc@13 79 else
adamc@16 80 CRel (xn + 1)
adamc@17 81 (*| CUnif _ => raise SynUnif*)
adamc@13 82 | _ => c,
adamc@623 83 bind = fn (bound, U.Con.RelC _) => bound + 1
adamc@13 84 | (bound, _) => bound}
adamc@13 85
adamc@13 86 val lift = liftConInCon 0
adamc@13 87
adamc@623 88 val liftKindInExp =
adamc@623 89 U.Exp.mapB {kind = fn bound => fn k =>
adamc@623 90 case k of
adamc@623 91 KRel xn =>
adamc@623 92 if xn < bound then
adamc@623 93 k
adamc@623 94 else
adamc@623 95 KRel (xn + 1)
adamc@623 96 | _ => k,
adamc@623 97 con = fn _ => fn c => c,
adamc@623 98 exp = fn _ => fn e => e,
adamc@623 99 bind = fn (bound, U.Exp.RelK _) => bound + 1
adamc@623 100 | (bound, _) => bound}
adamc@623 101
adamc@448 102 val liftConInExp =
adamc@623 103 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@448 104 con = fn bound => fn c =>
adamc@448 105 case c of
adamc@448 106 CRel xn =>
adamc@448 107 if xn < bound then
adamc@448 108 c
adamc@448 109 else
adamc@448 110 CRel (xn + 1)
adamc@448 111 | _ => c,
adamc@448 112 exp = fn _ => fn e => e,
adamc@448 113 bind = fn (bound, U.Exp.RelC _) => bound + 1
adamc@448 114 | (bound, _) => bound}
adamc@448 115
adamc@211 116 val liftExpInExp =
adamc@623 117 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@211 118 con = fn _ => fn c => c,
adamc@211 119 exp = fn bound => fn e =>
adamc@211 120 case e of
adamc@211 121 ERel xn =>
adamc@211 122 if xn < bound then
adamc@211 123 e
adamc@211 124 else
adamc@211 125 ERel (xn + 1)
adamc@211 126 | _ => e,
adamc@211 127 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@211 128 | (bound, _) => bound}
adamc@211 129
adamc@211 130
adamc@211 131 val liftExp = liftExpInExp 0
adamc@13 132
adamc@448 133 val subExpInExp =
adamc@623 134 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@448 135 con = fn _ => fn c => c,
adamc@448 136 exp = fn (xn, rep) => fn e =>
adamc@448 137 case e of
adamc@448 138 ERel xn' =>
adamc@448 139 (case Int.compare (xn', xn) of
adamc@448 140 EQUAL => #1 rep
adamc@448 141 | GREATER=> ERel (xn' - 1)
adamc@448 142 | LESS => e)
adamc@448 143 | _ => e,
adamc@448 144 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
adamc@448 145 | ((xn, rep), U.Exp.RelC _) => (xn, liftConInExp 0 rep)
adamc@448 146 | (ctx, _) => ctx}
adamc@448 147
adamc@13 148 (* Back to environments *)
adamc@13 149
adamc@9 150 datatype 'a var' =
adamc@9 151 Rel' of int * 'a
adamc@9 152 | Named' of int * 'a
adamc@2 153
adamc@9 154 datatype 'a var =
adamc@9 155 NotBound
adamc@9 156 | Rel of int * 'a
adamc@9 157 | Named of int * 'a
adamc@2 158
adamc@191 159 type datatyp = string list * (string * con option) IM.map
adamc@157 160
adamc@211 161 datatype class_name =
adamc@211 162 ClNamed of int
adamc@211 163 | ClProj of int * string list * string
adamc@211 164
adamc@711 165 fun class_name_out cn =
adamc@711 166 case cn of
adamc@711 167 ClNamed n => (CNamed n, ErrorMsg.dummySpan)
adamc@711 168 | ClProj x => (CModProj x, ErrorMsg.dummySpan)
adamc@711 169
adamc@216 170 fun cn2s cn =
adamc@216 171 case cn of
adamc@216 172 ClNamed n => "Named(" ^ Int.toString n ^ ")"
adamc@216 173 | ClProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
adamc@216 174
adamc@211 175 structure CK = struct
adamc@211 176 type ord_key = class_name
adamc@211 177 open Order
adamc@211 178 fun compare x =
adamc@211 179 case x of
adamc@211 180 (ClNamed n1, ClNamed n2) => Int.compare (n1, n2)
adamc@211 181 | (ClNamed _, _) => LESS
adamc@211 182 | (_, ClNamed _) => GREATER
adamc@211 183
adamc@211 184 | (ClProj (m1, ms1, x1), ClProj (m2, ms2, x2)) =>
adamc@211 185 join (Int.compare (m1, m2),
adamc@211 186 fn () => join (joinL String.compare (ms1, ms2),
adamc@211 187 fn () => String.compare (x1, x2)))
adamc@211 188 end
adamc@211 189
adamc@677 190 structure CS = BinarySetFn(CK)
adamc@211 191 structure CM = BinaryMapFn(CK)
adamc@211 192
adamc@711 193 type class = {ground : (con * exp) list,
adamc@711 194 rules : (int * con list * con * exp) list}
adamc@711 195 val empty_class = {ground = [],
adamc@711 196 rules = []}
adamc@216 197
adamc@2 198 type env = {
adamc@623 199 renameK : int SM.map,
adamc@623 200 relK : string list,
adamc@623 201
adamc@9 202 renameC : kind var' SM.map,
adamc@2 203 relC : (string * kind) list,
adamc@11 204 namedC : (string * kind * con option) IM.map,
adamc@9 205
adamc@157 206 datatypes : datatyp IM.map,
adamc@191 207 constructors : (datatype_kind * int * string list * con option * int) SM.map,
adamc@157 208
adamc@211 209 classes : class CM.map,
adamc@211 210
adamc@9 211 renameE : con var' SM.map,
adamc@9 212 relE : (string * con) list,
adamc@31 213 namedE : (string * con) IM.map,
adamc@31 214
adamc@31 215 renameSgn : (int * sgn) SM.map,
adamc@31 216 sgn : (string * sgn) IM.map,
adamc@31 217
adamc@31 218 renameStr : (int * sgn) SM.map,
adamc@31 219 str : (string * sgn) IM.map
adamc@2 220 }
adamc@2 221
adamc@2 222 val namedCounter = ref 0
adamc@2 223
adamc@109 224 fun newNamed () =
adamc@109 225 let
adamc@109 226 val r = !namedCounter
adamc@109 227 in
adamc@109 228 namedCounter := r + 1;
adamc@109 229 r
adamc@109 230 end
adamc@109 231
adamc@2 232 val empty = {
adamc@623 233 renameK = SM.empty,
adamc@623 234 relK = [],
adamc@623 235
adamc@2 236 renameC = SM.empty,
adamc@2 237 relC = [],
adamc@9 238 namedC = IM.empty,
adamc@9 239
adamc@157 240 datatypes = IM.empty,
adamc@171 241 constructors = SM.empty,
adamc@157 242
adamc@211 243 classes = CM.empty,
adamc@211 244
adamc@9 245 renameE = SM.empty,
adamc@9 246 relE = [],
adamc@31 247 namedE = IM.empty,
adamc@31 248
adamc@31 249 renameSgn = SM.empty,
adamc@31 250 sgn = IM.empty,
adamc@31 251
adamc@31 252 renameStr = SM.empty,
adamc@31 253 str = IM.empty
adamc@2 254 }
adamc@2 255
adamc@623 256 fun pushKRel (env : env) x =
adamc@623 257 let
adamc@623 258 val renameK = SM.map (fn n => n+1) (#renameK env)
adamc@623 259 in
adamc@623 260 {renameK = SM.insert (renameK, x, 0),
adamc@623 261 relK = x :: #relK env,
adamc@623 262
adamc@623 263 renameC = SM.map (fn Rel' (n, k) => Rel' (n, liftKindInKind 0 k)
adamc@623 264 | x => x) (#renameC env),
adamc@623 265 relC = map (fn (x, k) => (x, liftKindInKind 0 k)) (#relC env),
adamc@623 266 namedC = #namedC env,
adamc@623 267
adamc@623 268 datatypes = #datatypes env,
adamc@623 269 constructors = #constructors env,
adamc@623 270
adamc@711 271 classes = CM.map (fn cl => {ground = map (fn (c, e) =>
adamc@711 272 (liftKindInCon 0 c,
adamc@711 273 e))
adamc@711 274 (#ground cl),
adamc@711 275 rules = #rules cl})
adamc@711 276 (#classes env),
adamc@623 277
adamc@623 278 renameE = SM.map (fn Rel' (n, c) => Rel' (n, liftKindInCon 0 c)
adamc@623 279 | Named' (n, c) => Named' (n, c)) (#renameE env),
adamc@623 280 relE = map (fn (x, c) => (x, liftKindInCon 0 c)) (#relE env),
adamc@623 281 namedE = #namedE env,
adamc@623 282
adamc@623 283 renameSgn = #renameSgn env,
adamc@623 284 sgn = #sgn env,
adamc@623 285
adamc@623 286 renameStr = #renameStr env,
adamc@623 287 str = #str env
adamc@623 288 }
adamc@623 289 end
adamc@623 290
adamc@623 291 fun lookupKRel (env : env) n =
adamc@623 292 (List.nth (#relK env, n))
adamc@623 293 handle Subscript => raise UnboundRel n
adamc@623 294
adamc@623 295 fun lookupK (env : env) x = SM.find (#renameK env, x)
adamc@623 296
adamc@2 297 fun pushCRel (env : env) x k =
adamc@2 298 let
adamc@9 299 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
adamc@2 300 | x => x) (#renameC env)
adamc@2 301 in
adamc@623 302 {renameK = #renameK env,
adamc@623 303 relK = #relK env,
adamc@623 304
adamc@623 305 renameC = SM.insert (renameC, x, Rel' (0, k)),
adamc@2 306 relC = (x, k) :: #relC env,
adamc@514 307 namedC = #namedC env,
adamc@9 308
adamc@157 309 datatypes = #datatypes env,
adamc@171 310 constructors = #constructors env,
adamc@157 311
adamc@674 312 classes = CM.map (fn class =>
adamc@711 313 {ground = map (fn (c, e) =>
adamc@711 314 (liftConInCon 0 c,
adamc@711 315 e))
adamc@711 316 (#ground class),
adamc@711 317 rules = #rules class})
adamc@211 318 (#classes env),
adamc@211 319
adamc@328 320 renameE = SM.map (fn Rel' (n, c) => Rel' (n, lift c)
adamc@517 321 | Named' (n, c) => Named' (n, c)) (#renameE env),
adamc@13 322 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@514 323 namedE = #namedE env,
adamc@31 324
adamc@31 325 renameSgn = #renameSgn env,
adamc@31 326 sgn = #sgn env,
adamc@31 327
adamc@31 328 renameStr = #renameStr env,
adamc@31 329 str = #str env
adamc@31 330 }
adamc@2 331 end
adamc@2 332
adamc@2 333 fun lookupCRel (env : env) n =
adamc@2 334 (List.nth (#relC env, n))
adamc@2 335 handle Subscript => raise UnboundRel n
adamc@2 336
adamc@11 337 fun pushCNamedAs (env : env) x n k co =
adamc@623 338 {renameK = #renameK env,
adamc@623 339 relK = #relK env,
adamc@623 340
adamc@623 341 renameC = SM.insert (#renameC env, x, Named' (n, k)),
adamc@5 342 relC = #relC env,
adamc@11 343 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@9 344
adamc@157 345 datatypes = #datatypes env,
adamc@171 346 constructors = #constructors env,
adamc@157 347
adamc@211 348 classes = #classes env,
adamc@211 349
adamc@9 350 renameE = #renameE env,
adamc@9 351 relE = #relE env,
adamc@31 352 namedE = #namedE env,
adamc@31 353
adamc@31 354 renameSgn = #renameSgn env,
adamc@31 355 sgn = #sgn env,
adamc@31 356
adamc@31 357 renameStr = #renameStr env,
adamc@31 358 str = #str env}
adamc@5 359
adamc@11 360 fun pushCNamed env x k co =
adamc@2 361 let
adamc@2 362 val n = !namedCounter
adamc@2 363 in
adamc@2 364 namedCounter := n + 1;
adamc@11 365 (pushCNamedAs env x n k co, n)
adamc@2 366 end
adamc@2 367
adamc@2 368 fun lookupCNamed (env : env) n =
adamc@2 369 case IM.find (#namedC env, n) of
adamc@2 370 NONE => raise UnboundNamed n
adamc@2 371 | SOME x => x
adamc@2 372
adamc@2 373 fun lookupC (env : env) x =
adamc@2 374 case SM.find (#renameC env, x) of
adamc@9 375 NONE => NotBound
adamc@9 376 | SOME (Rel' x) => Rel x
adamc@9 377 | SOME (Named' x) => Named x
adamc@9 378
adamc@191 379 fun pushDatatype (env : env) n xs xncs =
adamc@188 380 let
adamc@188 381 val dk = U.classifyDatatype xncs
adamc@188 382 in
adamc@623 383 {renameK = #renameK env,
adamc@623 384 relK = #relK env,
adamc@623 385
adamc@623 386 renameC = #renameC env,
adamc@188 387 relC = #relC env,
adamc@188 388 namedC = #namedC env,
adamc@157 389
adamc@188 390 datatypes = IM.insert (#datatypes env, n,
adamc@191 391 (xs, foldl (fn ((x, n, to), cons) =>
adamc@191 392 IM.insert (cons, n, (x, to))) IM.empty xncs)),
adamc@188 393 constructors = foldl (fn ((x, n', to), cmap) =>
adamc@191 394 SM.insert (cmap, x, (dk, n', xs, to, n)))
adamc@188 395 (#constructors env) xncs,
adamc@157 396
adamc@211 397 classes = #classes env,
adamc@211 398
adamc@188 399 renameE = #renameE env,
adamc@188 400 relE = #relE env,
adamc@188 401 namedE = #namedE env,
adamc@157 402
adamc@188 403 renameSgn = #renameSgn env,
adamc@188 404 sgn = #sgn env,
adamc@157 405
adamc@188 406 renameStr = #renameStr env,
adamc@188 407 str = #str env}
adamc@188 408 end
adamc@157 409
adamc@157 410 fun lookupDatatype (env : env) n =
adamc@157 411 case IM.find (#datatypes env, n) of
adamc@157 412 NONE => raise UnboundNamed n
adamc@157 413 | SOME x => x
adamc@157 414
adamc@191 415 fun lookupDatatypeConstructor (_, dt) n =
adamc@157 416 case IM.find (dt, n) of
adamc@157 417 NONE => raise UnboundNamed n
adamc@157 418 | SOME x => x
adamc@157 419
adamc@171 420 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
adamc@171 421
adamc@191 422 fun datatypeArgs (xs, _) = xs
adamc@191 423 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
adamc@157 424
adamc@711 425 fun listClasses (env : env) =
adamc@711 426 map (fn (cn, {ground, rules}) =>
adamc@711 427 (class_name_out cn,
adamc@711 428 ground
adamc@711 429 @ map (fn (nvs, cs, c, e) =>
adamc@711 430 let
adamc@711 431 val loc = #2 c
adamc@711 432 val c = foldr (fn (c', c) => (TFun (c', c), loc)) c cs
adamc@711 433 val c = ListUtil.foldli (fn (n, (), c) => (TCFun (Explicit,
adamc@711 434 "x" ^ Int.toString n,
adamc@711 435 (KError, loc),
adamc@711 436 c), loc))
adamc@711 437 c (List.tabulate (nvs, fn _ => ()))
adamc@711 438 in
adamc@711 439 (c, e)
adamc@711 440 end) rules)) (CM.listItemsi (#classes env))
adamc@711 441
adamc@211 442 fun pushClass (env : env) n =
adamc@623 443 {renameK = #renameK env,
adamc@623 444 relK = #relK env,
adamc@623 445
adamc@623 446 renameC = #renameC env,
adamc@211 447 relC = #relC env,
adamc@211 448 namedC = #namedC env,
adamc@211 449
adamc@211 450 datatypes = #datatypes env,
adamc@211 451 constructors = #constructors env,
adamc@211 452
adamc@675 453 classes = CM.insert (#classes env, ClNamed n, empty_class),
adamc@211 454
adamc@211 455 renameE = #renameE env,
adamc@211 456 relE = #relE env,
adamc@211 457 namedE = #namedE env,
adamc@211 458
adamc@211 459 renameSgn = #renameSgn env,
adamc@211 460 sgn = #sgn env,
adamc@211 461
adamc@211 462 renameStr = #renameStr env,
adamc@403 463 str = #str env}
adamc@211 464
adamc@211 465 fun class_name_in (c, _) =
adamc@211 466 case c of
adamc@211 467 CNamed n => SOME (ClNamed n)
adamc@211 468 | CModProj x => SOME (ClProj x)
adamc@228 469 | CUnif (_, _, _, ref (SOME c)) => class_name_in c
adamc@211 470 | _ => NONE
adamc@211 471
adamc@403 472 fun isClass (env : env) c =
adamc@403 473 let
adamc@403 474 fun find NONE = false
adamc@403 475 | find (SOME c) = Option.isSome (CM.find (#classes env, c))
adamc@403 476 in
adamc@403 477 find (class_name_in c)
adamc@403 478 end
adamc@403 479
adamc@711 480 fun class_head_in c =
adamc@711 481 case #1 c of
adamc@711 482 CApp (f, _) => class_head_in f
adamc@711 483 | CUnif (_, _, _, ref (SOME c)) => class_head_in c
adamc@711 484 | _ => class_name_in c
adamc@211 485
adamc@711 486 exception Unify
adamc@711 487
adamc@711 488 fun unifyKinds (k1, k2) =
adamc@711 489 case (#1 k1, #1 k2) of
adamc@711 490 (KType, KType) => ()
adamc@711 491 | (KArrow (d1, r1), KArrow (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
adamc@711 492 | (KName, KName) => ()
adamc@711 493 | (KRecord k1, KRecord k2) => unifyKinds (k1, k2)
adamc@711 494 | (KUnit, KUnit) => ()
adamc@711 495 | (KTuple ks1, KTuple ks2) => (ListPair.appEq unifyKinds (ks1, ks2)
adamc@711 496 handle ListPair.UnequalLengths => raise Unify)
adamc@711 497 | (KUnif (_, _, ref (SOME k1)), _) => unifyKinds (k1, k2)
adamc@711 498 | (_, KUnif (_, _, ref (SOME k2))) => unifyKinds (k1, k2)
adamc@711 499 | (KRel n1, KRel n2) => if n1 = n2 then () else raise Unify
adamc@711 500 | (KFun (_, k1), KFun (_, k2)) => unifyKinds (k1, k2)
adamc@711 501 | _ => raise Unify
adamc@711 502
adamc@711 503 fun unifyCons rs =
adamc@674 504 let
adamc@711 505 fun unify d (c1, c2) =
adamc@711 506 case (#1 c1, #1 c2) of
adamc@711 507 (CUnif (_, _, _, ref (SOME c1)), _) => unify d (c1, c2)
adamc@711 508 | (_, CUnif (_, _, _, ref (SOME c2))) => unify d (c1, c2)
adamc@711 509
adamc@750 510 | (CUnif _, _) => ()
adamc@750 511
adamc@711 512 | (c1', CRel n2) =>
adamc@711 513 if n2 < d then
adamc@711 514 case c1' of
adamc@711 515 CRel n1 => if n1 = n2 then () else raise Unify
adamc@711 516 | _ => raise Unify
adamc@711 517 else if n2 - d >= length rs then
adamc@711 518 case c1' of
adamc@711 519 CRel n1 => if n1 = n2 - length rs then () else raise Unify
adamc@711 520 | _ => raise Unify
adamc@711 521 else
adamc@711 522 let
adamc@711 523 val r = List.nth (rs, n2 - d)
adamc@711 524 in
adamc@711 525 case !r of
adamc@711 526 NONE => r := SOME c1
adamc@711 527 | SOME c2 => unify d (c1, c2)
adamc@711 528 end
adamc@711 529
adamc@711 530 | (TFun (d1, r1), TFun (d2, r2)) => (unify d (d1, d2); unify d (r1, r2))
adamc@711 531 | (TCFun (_, _, k1, r1), TCFun (_, _, k2, r2)) => (unifyKinds (k1, k2); unify (d + 1) (r1, r2))
adamc@711 532 | (TRecord c1, TRecord c2) => unify d (c1, c2)
adamc@711 533 | (TDisjoint (a1, b1, c1), TDisjoint (a2, b2, c2)) =>
adamc@711 534 (unify d (a1, a2); unify d (b1, b2); unify d (c1, c2))
adamc@711 535
adamc@711 536 | (CNamed n1, CNamed n2) => if n1 = n2 then () else raise Unify
adamc@711 537 | (CModProj (n1, ms1, x1), CModProj (n2, ms2, x2)) =>
adamc@711 538 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then () else raise Unify
adamc@711 539 | (CApp (f1, x1), CApp (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
adamc@711 540 | (CAbs (_, k1, b1), CAbs (_, k2, b2)) => (unifyKinds (k1, k2); unify (d + 1) (b1, b2))
adamc@711 541
adamc@711 542 | (CKAbs (_, b1), CKAbs (_, b2)) => unify d (b1, b2)
adamc@711 543 | (CKApp (c1, k1), CKApp (c2, k2)) => (unify d (c1, c2); unifyKinds (k1, k2))
adamc@711 544 | (TKFun (_, c1), TKFun (_, c2)) => unify d (c1, c2)
adamc@711 545
adamc@711 546 | (CName s1, CName s2) => if s1 = s2 then () else raise Unify
adamc@711 547
adamc@711 548 | (CRecord (k1, xcs1), CRecord (k2, xcs2)) =>
adamc@711 549 (unifyKinds (k1, k2);
adamc@711 550 ListPair.appEq (fn ((x1, c1), (x2, c2)) => (unify d (x1, x2); unify d (c1, c2))) (xcs1, xcs2)
adamc@711 551 handle ListPair.UnequalLengths => raise Unify)
adamc@711 552 | (CConcat (f1, x1), CConcat (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
adamc@711 553 | (CMap (d1, r1), CMap (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
adamc@711 554
adamc@711 555 | (CUnit, CUnit) => ()
adamc@711 556
adamc@711 557 | (CTuple cs1, CTuple cs2) => (ListPair.appEq (unify d) (cs1, cs2)
adamc@711 558 handle ListPair.UnequalLengths => raise Unify)
adamc@711 559 | (CProj (c1, n1), CProj (c2, n2)) => (unify d (c1, c2);
adamc@711 560 if n1 = n2 then () else raise Unify)
adamc@711 561
adamc@711 562 | _ => raise Unify
adamc@674 563 in
adamc@711 564 unify
adamc@674 565 end
adamc@674 566
adamc@711 567 fun tryUnify nRs (c1, c2) =
adamc@674 568 let
adamc@711 569 val rs = List.tabulate (nRs, fn _ => ref NONE)
adamc@674 570 in
adamc@711 571 (unifyCons rs 0 (c1, c2);
adamc@711 572 SOME (map (fn r => case !r of
adamc@711 573 NONE => raise Unify
adamc@711 574 | SOME c => c) rs))
adamc@711 575 handle Unify => NONE
adamc@674 576 end
adamc@674 577
adamc@711 578 fun unifySubst (rs : con list) =
adamc@711 579 U.Con.mapB {kind = fn _ => fn k => k,
adamc@711 580 con = fn d => fn c =>
adamc@711 581 case c of
adamc@711 582 CRel n =>
adamc@711 583 if n < d then
adamc@711 584 c
adamc@711 585 else
adamc@711 586 #1 (List.nth (rs, n - d))
adamc@711 587 | _ => c,
adamc@711 588 bind = fn (d, U.Con.RelC _) => d + 1
adamc@711 589 | (d, _) => d}
adamc@711 590 0
adamc@711 591
adamc@750 592 fun postUnify x =
adamc@750 593 let
adamc@750 594 fun unify (c1, c2) =
adamc@750 595 case (#1 c1, #1 c2) of
adamc@750 596 (CUnif (_, _, _, ref (SOME c1)), _) => unify (c1, c2)
adamc@750 597 | (_, CUnif (_, _, _, ref (SOME c2))) => unify (c1, c2)
adamc@750 598
adamc@750 599 | (CUnif (_, _, _, r), _) => r := SOME c2
adamc@750 600
adamc@750 601 | (TFun (d1, r1), TFun (d2, r2)) => (unify (d1, d2); unify (r1, r2))
adamc@750 602 | (TCFun (_, _, k1, r1), TCFun (_, _, k2, r2)) => (unifyKinds (k1, k2); unify (r1, r2))
adamc@750 603 | (TRecord c1, TRecord c2) => unify (c1, c2)
adamc@750 604 | (TDisjoint (a1, b1, c1), TDisjoint (a2, b2, c2)) =>
adamc@750 605 (unify (a1, a2); unify (b1, b2); unify (c1, c2))
adamc@750 606
adamc@750 607 | (CRel n1, CRel n2) => if n1 = n2 then () else raise Unify
adamc@750 608 | (CNamed n1, CNamed n2) => if n1 = n2 then () else raise Unify
adamc@750 609 | (CModProj (n1, ms1, x1), CModProj (n2, ms2, x2)) =>
adamc@750 610 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then () else raise Unify
adamc@750 611 | (CApp (f1, x1), CApp (f2, x2)) => (unify (f1, f2); unify (x1, x2))
adamc@750 612 | (CAbs (_, k1, b1), CAbs (_, k2, b2)) => (unifyKinds (k1, k2); unify (b1, b2))
adamc@750 613
adamc@750 614 | (CKAbs (_, b1), CKAbs (_, b2)) => unify (b1, b2)
adamc@750 615 | (CKApp (c1, k1), CKApp (c2, k2)) => (unify (c1, c2); unifyKinds (k1, k2))
adamc@750 616 | (TKFun (_, c1), TKFun (_, c2)) => unify (c1, c2)
adamc@750 617
adamc@750 618 | (CName s1, CName s2) => if s1 = s2 then () else raise Unify
adamc@750 619
adamc@750 620 | (CRecord (k1, xcs1), CRecord (k2, xcs2)) =>
adamc@750 621 (unifyKinds (k1, k2);
adamc@750 622 ListPair.appEq (fn ((x1, c1), (x2, c2)) => (unify (x1, x2); unify (c1, c2))) (xcs1, xcs2)
adamc@750 623 handle ListPair.UnequalLengths => raise Unify)
adamc@750 624 | (CConcat (f1, x1), CConcat (f2, x2)) => (unify (f1, f2); unify (x1, x2))
adamc@750 625 | (CMap (d1, r1), CMap (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
adamc@750 626
adamc@750 627 | (CUnit, CUnit) => ()
adamc@750 628
adamc@750 629 | (CTuple cs1, CTuple cs2) => (ListPair.appEq unify (cs1, cs2)
adamc@750 630 handle ListPair.UnequalLengths => raise Unify)
adamc@750 631 | (CProj (c1, n1), CProj (c2, n2)) => (unify (c1, c2);
adamc@750 632 if n1 = n2 then () else raise Unify)
adamc@750 633
adamc@750 634 | _ => raise Unify
adamc@750 635 in
adamc@750 636 unify x
adamc@750 637 end
adamc@750 638
adamc@750 639 fun postUnifies x = (postUnify x; true) handle Unify => false
adamc@750 640
adamc@750 641 fun resolveClass (hnorm : con -> con) (env : env) =
adamc@674 642 let
adamc@711 643 fun resolve c =
adamc@711 644 let
adamc@711 645 fun doHead f =
adamc@711 646 case CM.find (#classes env, f) of
adamc@711 647 NONE => NONE
adamc@711 648 | SOME class =>
adamc@711 649 let
adamc@711 650 val loc = #2 c
adamc@675 651
adamc@711 652 fun tryRules rules =
adamc@711 653 case rules of
adamc@675 654 [] => NONE
adamc@711 655 | (nRs, cs, c', e) :: rules' =>
adamc@711 656 case tryUnify nRs (c, c') of
adamc@711 657 NONE => tryRules rules'
adamc@711 658 | SOME rs =>
adamc@675 659 let
adamc@711 660 val eos = map (resolve o unifySubst rs) cs
adamc@675 661 in
adamc@750 662 if List.exists (not o Option.isSome) eos
adamc@750 663 orelse not (postUnifies (c, unifySubst rs c')) then
adamc@711 664 tryRules rules'
adamc@711 665 else
adamc@711 666 let
adamc@711 667 val es = List.mapPartial (fn x => x) eos
adamc@711 668
adamc@711 669 val e = foldr (fn (c, e) => (ECApp (e, c), loc)) e rs
adamc@711 670 val e = foldl (fn (e', e) => (EApp (e, e'), loc)) e es
adamc@711 671 in
adamc@711 672 SOME e
adamc@711 673 end
adamc@675 674 end
adamc@711 675
adamc@711 676 fun rules () = tryRules (#rules class)
adamc@711 677
adamc@711 678 fun tryGrounds ces =
adamc@711 679 case ces of
adamc@711 680 [] => rules ()
adamc@711 681 | (c', e) :: ces' =>
adamc@711 682 case tryUnify 0 (c, c') of
adamc@711 683 NONE => tryGrounds ces'
adamc@711 684 | SOME _ => SOME e
adamc@675 685 in
adamc@711 686 tryGrounds (#ground class)
adamc@675 687 end
adamc@711 688 in
adamc@750 689 case #1 c of
adamc@750 690 TRecord c =>
adamc@750 691 (case #1 (hnorm c) of
adamc@750 692 CRecord (_, xts) =>
adamc@750 693 let
adamc@750 694 fun resolver (xts, acc) =
adamc@750 695 case xts of
adamc@750 696 [] => SOME (ERecord acc, #2 c)
adamc@750 697 | (x, t) :: xts =>
adamc@750 698 let
adamc@750 699 val t = hnorm t
adamc@750 700
adamc@750 701 val t = case t of
adamc@750 702 (CApp (f, x), loc) => (CApp (hnorm f, hnorm x), loc)
adamc@750 703 | _ => t
adamc@750 704 in
adamc@750 705 case resolve t of
adamc@750 706 NONE => NONE
adamc@750 707 | SOME e => resolver (xts, (x, e, t) :: acc)
adamc@750 708 end
adamc@750 709 in
adamc@750 710 resolver (xts, [])
adamc@750 711 end
adamc@750 712 | _ => NONE)
adamc@750 713 | _ =>
adamc@750 714 case class_head_in c of
adamc@750 715 SOME f => doHead f
adamc@750 716 | _ => NONE
adamc@711 717 end
adamc@674 718 in
adamc@711 719 resolve
adamc@674 720 end
adamc@211 721
adamc@9 722 fun pushERel (env : env) x t =
adamc@9 723 let
adamc@9 724 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 725 | x => x) (#renameE env)
adamc@211 726
adamc@675 727 val classes = CM.map (fn class =>
adamc@711 728 {ground = map (fn (c, e) => (c, liftExp e)) (#ground class),
adamc@711 729 rules = #rules class}) (#classes env)
adamc@711 730 val classes = case class_head_in t of
adamc@211 731 NONE => classes
adamc@711 732 | SOME f =>
adamc@403 733 case CM.find (classes, f) of
adamc@403 734 NONE => classes
adamc@403 735 | SOME class =>
adamc@403 736 let
adamc@711 737 val class = {ground = (t, (ERel 0, #2 t)) :: #ground class,
adamc@711 738 rules = #rules class}
adamc@403 739 in
adamc@403 740 CM.insert (classes, f, class)
adamc@403 741 end
adamc@9 742 in
adamc@623 743 {renameK = #renameK env,
adamc@623 744 relK = #relK env,
adamc@623 745
adamc@623 746 renameC = #renameC env,
adamc@9 747 relC = #relC env,
adamc@9 748 namedC = #namedC env,
adamc@9 749
adamc@157 750 datatypes = #datatypes env,
adamc@171 751 constructors = #constructors env,
adamc@157 752
adamc@211 753 classes = classes,
adamc@211 754
adamc@9 755 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 756 relE = (x, t) :: #relE env,
adamc@31 757 namedE = #namedE env,
adamc@31 758
adamc@31 759 renameSgn = #renameSgn env,
adamc@31 760 sgn = #sgn env,
adamc@31 761
adamc@31 762 renameStr = #renameStr env,
adamc@31 763 str = #str env}
adamc@9 764 end
adamc@9 765
adamc@9 766 fun lookupERel (env : env) n =
adamc@9 767 (List.nth (#relE env, n))
adamc@9 768 handle Subscript => raise UnboundRel n
adamc@9 769
adamc@674 770 fun rule_in c =
adamc@674 771 let
adamc@674 772 fun quantifiers (c, nvars) =
adamc@674 773 case #1 c of
adamc@674 774 TCFun (_, _, _, c) => quantifiers (c, nvars + 1)
adamc@674 775 | _ =>
adamc@674 776 let
adamc@674 777 fun clauses (c, hyps) =
adamc@674 778 case #1 c of
adamc@674 779 TFun (hyp, c) =>
adamc@711 780 (case class_head_in hyp of
adamc@711 781 SOME _ => clauses (c, hyp :: hyps)
adamc@711 782 | NONE => NONE)
adamc@674 783 | _ =>
adamc@711 784 case class_head_in c of
adamc@674 785 NONE => NONE
adamc@711 786 | SOME f => SOME (f, nvars, rev hyps, c)
adamc@674 787 in
adamc@674 788 clauses (c, [])
adamc@674 789 end
adamc@674 790 in
adamc@711 791 quantifiers (c, 0)
adamc@677 792 end
adamc@677 793
adamc@9 794 fun pushENamedAs (env : env) x n t =
adamc@211 795 let
adamc@211 796 val classes = #classes env
adamc@674 797 val classes = case rule_in t of
adamc@211 798 NONE => classes
adamc@711 799 | SOME (f, nvs, cs, c) =>
adamc@403 800 case CM.find (classes, f) of
adamc@403 801 NONE => classes
adamc@403 802 | SOME class =>
adamc@403 803 let
adamc@675 804 val e = (ENamed n, #2 t)
adamc@675 805
adamc@675 806 val class =
adamc@711 807 {ground = #ground class,
adamc@711 808 rules = (nvs, cs, c, e) :: #rules class}
adamc@403 809 in
adamc@403 810 CM.insert (classes, f, class)
adamc@403 811 end
adamc@211 812 in
adamc@623 813 {renameK = #renameK env,
adamc@623 814 relK = #relK env,
adamc@623 815
adamc@623 816 renameC = #renameC env,
adamc@211 817 relC = #relC env,
adamc@211 818 namedC = #namedC env,
adamc@9 819
adamc@211 820 datatypes = #datatypes env,
adamc@211 821 constructors = #constructors env,
adamc@157 822
adamc@211 823 classes = classes,
adamc@31 824
adamc@211 825 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@211 826 relE = #relE env,
adamc@211 827 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@211 828
adamc@211 829 renameSgn = #renameSgn env,
adamc@211 830 sgn = #sgn env,
adamc@211 831
adamc@211 832 renameStr = #renameStr env,
adamc@211 833 str = #str env}
adamc@211 834 end
adamc@9 835
adamc@9 836 fun pushENamed env x t =
adamc@9 837 let
adamc@9 838 val n = !namedCounter
adamc@9 839 in
adamc@9 840 namedCounter := n + 1;
adamc@9 841 (pushENamedAs env x n t, n)
adamc@9 842 end
adamc@9 843
adamc@9 844 fun lookupENamed (env : env) n =
adamc@9 845 case IM.find (#namedE env, n) of
adamc@9 846 NONE => raise UnboundNamed n
adamc@9 847 | SOME x => x
adamc@9 848
adamc@471 849 fun checkENamed (env : env) n =
adamc@471 850 Option.isSome (IM.find (#namedE env, n))
adamc@471 851
adamc@9 852 fun lookupE (env : env) x =
adamc@9 853 case SM.find (#renameE env, x) of
adamc@9 854 NONE => NotBound
adamc@9 855 | SOME (Rel' x) => Rel x
adamc@9 856 | SOME (Named' x) => Named x
adamc@2 857
adamc@31 858 fun pushSgnNamedAs (env : env) x n sgis =
adamc@623 859 {renameK = #renameK env,
adamc@623 860 relK = #relK env,
adamc@623 861
adamc@623 862 renameC = #renameC env,
adamc@31 863 relC = #relC env,
adamc@31 864 namedC = #namedC env,
adamc@31 865
adamc@157 866 datatypes = #datatypes env,
adamc@171 867 constructors = #constructors env,
adamc@157 868
adamc@211 869 classes = #classes env,
adamc@211 870
adamc@31 871 renameE = #renameE env,
adamc@31 872 relE = #relE env,
adamc@31 873 namedE = #namedE env,
adamc@31 874
adamc@31 875 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
adamc@31 876 sgn = IM.insert (#sgn env, n, (x, sgis)),
adamc@31 877
adamc@31 878 renameStr = #renameStr env,
adamc@31 879 str = #str env}
adamc@31 880
adamc@31 881 fun pushSgnNamed env x sgis =
adamc@31 882 let
adamc@31 883 val n = !namedCounter
adamc@31 884 in
adamc@31 885 namedCounter := n + 1;
adamc@31 886 (pushSgnNamedAs env x n sgis, n)
adamc@31 887 end
adamc@31 888
adamc@31 889 fun lookupSgnNamed (env : env) n =
adamc@31 890 case IM.find (#sgn env, n) of
adamc@31 891 NONE => raise UnboundNamed n
adamc@31 892 | SOME x => x
adamc@31 893
adamc@31 894 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
adamc@31 895
adamc@31 896 fun lookupStrNamed (env : env) n =
adamc@31 897 case IM.find (#str env, n) of
adamc@31 898 NONE => raise UnboundNamed n
adamc@31 899 | SOME x => x
adamc@31 900
adamc@31 901 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
adamc@31 902
adamc@203 903
adamc@216 904 fun sgiSeek (sgi, (sgns, strs, cons)) =
adamc@216 905 case sgi of
adamc@216 906 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 907 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 908 | SgiDatatype (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 909 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 910 | SgiVal _ => (sgns, strs, cons)
adamc@216 911 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
adamc@216 912 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
adamc@216 913 | SgiConstraint _ => (sgns, strs, cons)
adamc@563 914 | SgiClassAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@563 915 | SgiClass (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 916
adamc@34 917 fun sgnSeek f sgis =
adamc@34 918 let
adamc@59 919 fun seek (sgis, sgns, strs, cons) =
adamc@34 920 case sgis of
adamc@34 921 [] => NONE
adamc@34 922 | (sgi, _) :: sgis =>
adamc@34 923 case f sgi of
adamc@158 924 SOME v =>
adamc@158 925 let
adamc@158 926 val cons =
adamc@158 927 case sgi of
adamc@191 928 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
adamc@191 929 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
adamc@158 930 | _ => cons
adamc@158 931 in
adamc@158 932 SOME (v, (sgns, strs, cons))
adamc@158 933 end
adamc@88 934 | NONE =>
adamc@216 935 let
adamc@216 936 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
adamc@216 937 in
adamc@216 938 seek (sgis, sgns, strs, cons)
adamc@216 939 end
adamc@34 940 in
adamc@59 941 seek (sgis, IM.empty, IM.empty, IM.empty)
adamc@34 942 end
adamc@34 943
adamc@34 944 fun id x = x
adamc@34 945
adamc@34 946 fun unravelStr (str, _) =
adamc@34 947 case str of
adamc@34 948 StrVar x => (x, [])
adamc@34 949 | StrProj (str, m) =>
adamc@34 950 let
adamc@34 951 val (x, ms) = unravelStr str
adamc@34 952 in
adamc@34 953 (x, ms @ [m])
adamc@34 954 end
adamc@34 955 | _ => raise Fail "unravelStr"
adamc@34 956
adamc@59 957 fun sgnS_con (str, (sgns, strs, cons)) c =
adamc@34 958 case c of
adamc@34 959 CModProj (m1, ms, x) =>
adamc@34 960 (case IM.find (strs, m1) of
adamc@34 961 NONE => c
adamc@34 962 | SOME m1x =>
adamc@34 963 let
adamc@34 964 val (m1, ms') = unravelStr str
adamc@34 965 in
adamc@34 966 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 967 end)
adamc@34 968 | CNamed n =>
adamc@34 969 (case IM.find (cons, n) of
adamc@34 970 NONE => c
adamc@34 971 | SOME nx =>
adamc@34 972 let
adamc@34 973 val (m1, ms) = unravelStr str
adamc@34 974 in
adamc@34 975 CModProj (m1, ms, nx)
adamc@34 976 end)
adamc@34 977 | _ => c
adamc@34 978
adamc@721 979 fun sgnS_con' (m1, ms', (sgns, strs, cons)) =
adamc@721 980 U.Con.map {kind = fn x => x,
adamc@721 981 con = fn c =>
adamc@721 982 case c of
adamc@721 983 CModProj (m1, ms, x) =>
adamc@721 984 (case IM.find (strs, m1) of
adamc@721 985 NONE => c
adamc@721 986 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
adamc@721 987 | CNamed n =>
adamc@721 988 (case IM.find (cons, n) of
adamc@721 989 NONE => c
adamc@721 990 | SOME nx => CModProj (m1, ms', nx))
adamc@721 991 | _ => c}
adamc@216 992
adamc@59 993 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
adamc@59 994 case sgn of
adamc@59 995 SgnProj (m1, ms, x) =>
adamc@59 996 (case IM.find (strs, m1) of
adamc@59 997 NONE => sgn
adamc@59 998 | SOME m1x =>
adamc@59 999 let
adamc@59 1000 val (m1, ms') = unravelStr str
adamc@59 1001 in
adamc@59 1002 SgnProj (m1, ms' @ m1x :: ms, x)
adamc@59 1003 end)
adamc@59 1004 | SgnVar n =>
adamc@59 1005 (case IM.find (sgns, n) of
adamc@59 1006 NONE => sgn
adamc@59 1007 | SOME nx =>
adamc@59 1008 let
adamc@59 1009 val (m1, ms) = unravelStr str
adamc@59 1010 in
adamc@59 1011 SgnProj (m1, ms, nx)
adamc@59 1012 end)
adamc@59 1013 | _ => sgn
adamc@59 1014
adamc@59 1015 fun sgnSubSgn x =
adamc@34 1016 ElabUtil.Sgn.map {kind = id,
adamc@59 1017 con = sgnS_con x,
adamc@34 1018 sgn_item = id,
adamc@59 1019 sgn = sgnS_sgn x}
adamc@34 1020
adamc@211 1021
adamc@211 1022
adamc@211 1023 and projectSgn env {sgn, str, field} =
adamc@211 1024 case #1 (hnormSgn env sgn) of
adamc@211 1025 SgnConst sgis =>
adamc@211 1026 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@211 1027 NONE => NONE
adamc@211 1028 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@211 1029 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@211 1030 | _ => NONE
adamc@211 1031
adamc@211 1032 and hnormSgn env (all as (sgn, loc)) =
adamc@34 1033 case sgn of
adamc@42 1034 SgnError => all
adamc@42 1035 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 1036 | SgnConst _ => all
adamc@42 1037 | SgnFun _ => all
adamc@59 1038 | SgnProj (m, ms, x) =>
adamc@59 1039 let
adamc@59 1040 val (_, sgn) = lookupStrNamed env m
adamc@59 1041 in
adamc@59 1042 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
adamc@59 1043 sgn = sgn,
adamc@59 1044 field = x} of
adamc@59 1045 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
adamc@59 1046 | SOME sgn => sgn
adamc@59 1047 end
adamc@42 1048 | SgnWhere (sgn, x, c) =>
adamc@42 1049 case #1 (hnormSgn env sgn) of
adamc@42 1050 SgnError => (SgnError, loc)
adamc@42 1051 | SgnConst sgis =>
adamc@42 1052 let
adamc@42 1053 fun traverse (pre, post) =
adamc@42 1054 case post of
adamc@42 1055 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 1056 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adamc@42 1057 if x = x' then
adamc@42 1058 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adamc@42 1059 else
adamc@42 1060 traverse (sgi :: pre, rest)
adamc@42 1061 | sgi :: rest => traverse (sgi :: pre, rest)
adamc@42 1062
adamc@42 1063 val sgis = traverse ([], sgis)
adamc@42 1064 in
adamc@42 1065 (SgnConst sgis, loc)
adamc@42 1066 end
adamc@42 1067 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adamc@42 1068
adamc@211 1069 fun enrichClasses env classes (m1, ms) sgn =
adamc@59 1070 case #1 (hnormSgn env sgn) of
adamc@59 1071 SgnConst sgis =>
adamc@211 1072 let
adamc@218 1073 val (classes, _, _, _) =
adamc@218 1074 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
adamc@211 1075 let
adamc@211 1076 fun found (x, n) =
adamc@211 1077 (CM.insert (classes,
adamc@211 1078 ClProj (m1, ms, x),
adamc@211 1079 empty_class),
adamc@216 1080 IM.insert (newClasses, n, x),
adamc@218 1081 sgiSeek (#1 sgi, fmap),
adamc@218 1082 env)
adamc@216 1083
adamc@218 1084 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
adamc@211 1085 in
adamc@211 1086 case #1 sgi of
adamc@217 1087 SgiStr (x, _, sgn) =>
adamc@217 1088 (enrichClasses env classes (m1, ms @ [x]) sgn,
adamc@217 1089 newClasses,
adamc@218 1090 sgiSeek (#1 sgi, fmap),
adamc@218 1091 env)
adamc@218 1092 | SgiSgn (x, n, sgn) =>
adamc@218 1093 (classes,
adamc@218 1094 newClasses,
adamc@218 1095 fmap,
adamc@218 1096 pushSgnNamedAs env x n sgn)
adamc@218 1097
adamc@563 1098 | SgiClassAbs (x, n, _) => found (x, n)
adamc@563 1099 | SgiClass (x, n, _, _) => found (x, n)
adamc@674 1100 | SgiVal (x, n, c) =>
adamc@674 1101 (case rule_in c of
adamc@674 1102 NONE => default ()
adamc@711 1103 | SOME (cn, nvs, cs, c) =>
adamc@674 1104 let
adamc@711 1105 val loc = #2 c
adamc@721 1106 val globalize = sgnS_con' (m1, ms, fmap)
adamc@674 1107
adamc@674 1108 val nc =
adamc@674 1109 case cn of
adamc@674 1110 ClNamed f => IM.find (newClasses, f)
adamc@674 1111 | _ => NONE
adamc@674 1112 in
adamc@674 1113 case nc of
adamc@674 1114 NONE =>
adamc@419 1115 let
adamc@419 1116 val classes =
adamc@419 1117 case CM.find (classes, cn) of
adamc@419 1118 NONE => classes
adamc@419 1119 | SOME class =>
adamc@419 1120 let
adamc@711 1121 val e = (EModProj (m1, ms, x), #2 sgn)
adamc@675 1122
adamc@675 1123 val class =
adamc@711 1124 {ground = #ground class,
adamc@711 1125 rules = (nvs,
adamc@711 1126 map globalize cs,
adamc@711 1127 globalize c,
adamc@711 1128 e) :: #rules class}
adamc@419 1129 in
adamc@419 1130 CM.insert (classes, cn, class)
adamc@419 1131 end
adamc@419 1132 in
adamc@419 1133 (classes,
adamc@419 1134 newClasses,
adamc@419 1135 fmap,
adamc@419 1136 env)
adamc@419 1137 end
adamc@674 1138 | SOME fx =>
adamc@674 1139 let
adamc@674 1140 val cn = ClProj (m1, ms, fx)
adamc@419 1141
adamc@674 1142 val classes =
adamc@674 1143 case CM.find (classes, cn) of
adamc@674 1144 NONE => classes
adamc@674 1145 | SOME class =>
adamc@674 1146 let
adamc@675 1147 val e = (EModProj (m1, ms, x), #2 sgn)
adamc@675 1148
adamc@675 1149 val class =
adamc@711 1150 {ground = #ground class,
adamc@711 1151 rules = (nvs,
adamc@711 1152 map globalize cs,
adamc@711 1153 globalize c,
adamc@711 1154 e) :: #rules class}
adamc@674 1155 in
adamc@674 1156 CM.insert (classes, cn, class)
adamc@674 1157 end
adamc@674 1158 in
adamc@674 1159 (classes,
adamc@674 1160 newClasses,
adamc@674 1161 fmap,
adamc@674 1162 env)
adamc@674 1163 end
adamc@674 1164 end)
adamc@216 1165 | _ => default ()
adamc@211 1166 end)
adamc@218 1167 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
adamc@211 1168 in
adamc@211 1169 classes
adamc@211 1170 end
adamc@211 1171 | _ => classes
adamc@211 1172
adamc@211 1173 fun pushStrNamedAs (env : env) x n sgn =
adamc@623 1174 {renameK = #renameK env,
adamc@623 1175 relK = #relK env,
adamc@623 1176
adamc@623 1177 renameC = #renameC env,
adamc@211 1178 relC = #relC env,
adamc@211 1179 namedC = #namedC env,
adamc@211 1180
adamc@211 1181 datatypes = #datatypes env,
adamc@211 1182 constructors = #constructors env,
adamc@211 1183
adamc@211 1184 classes = enrichClasses env (#classes env) (n, []) sgn,
adamc@211 1185
adamc@211 1186 renameE = #renameE env,
adamc@211 1187 relE = #relE env,
adamc@211 1188 namedE = #namedE env,
adamc@211 1189
adamc@211 1190 renameSgn = #renameSgn env,
adamc@211 1191 sgn = #sgn env,
adamc@211 1192
adamc@211 1193 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
adamc@211 1194 str = IM.insert (#str env, n, (x, sgn))}
adamc@211 1195
adamc@211 1196 fun pushStrNamed env x sgn =
adamc@211 1197 let
adamc@211 1198 val n = !namedCounter
adamc@211 1199 in
adamc@211 1200 namedCounter := n + 1;
adamc@211 1201 (pushStrNamedAs env x n sgn, n)
adamc@211 1202 end
adamc@211 1203
adamc@211 1204 fun sgiBinds env (sgi, loc) =
adamc@211 1205 case sgi of
adamc@211 1206 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@211 1207 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@211 1208 | SgiDatatype (x, n, xs, xncs) =>
adamc@211 1209 let
adamc@341 1210 val k = (KType, loc)
adamc@341 1211 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 1212
adamc@341 1213 val env = pushCNamedAs env x n k' NONE
adamc@211 1214 in
adamc@211 1215 foldl (fn ((x', n', to), env) =>
adamc@211 1216 let
adamc@211 1217 val t =
adamc@211 1218 case to of
adamc@211 1219 NONE => (CNamed n, loc)
adamc@211 1220 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 1221
adamc@211 1222 val k = (KType, loc)
adamc@211 1223 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 1224 in
adamc@211 1225 pushENamedAs env x' n' t
adamc@211 1226 end)
adamc@211 1227 env xncs
adamc@211 1228 end
adamc@211 1229 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@211 1230 let
adamc@341 1231 val k = (KType, loc)
adamc@341 1232 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 1233
adamc@341 1234 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
adamc@211 1235 in
adamc@211 1236 foldl (fn ((x', n', to), env) =>
adamc@211 1237 let
adamc@211 1238 val t =
adamc@211 1239 case to of
adamc@211 1240 NONE => (CNamed n, loc)
adamc@211 1241 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 1242
adamc@211 1243 val k = (KType, loc)
adamc@211 1244 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 1245 in
adamc@211 1246 pushENamedAs env x' n' t
adamc@211 1247 end)
adamc@211 1248 env xncs
adamc@211 1249 end
adamc@211 1250 | SgiVal (x, n, t) => pushENamedAs env x n t
adamc@211 1251 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@211 1252 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@211 1253 | SgiConstraint _ => env
adamc@211 1254
adamc@711 1255 | SgiClassAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@711 1256 | SgiClass (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@459 1257
adamc@211 1258 fun sgnSubCon x =
adamc@211 1259 ElabUtil.Con.map {kind = id,
adamc@211 1260 con = sgnS_con x}
adamc@59 1261
adamc@59 1262 fun projectStr env {sgn, str, field} =
adamc@59 1263 case #1 (hnormSgn env sgn) of
adamc@59 1264 SgnConst sgis =>
adamc@59 1265 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 1266 NONE => NONE
adamc@59 1267 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 1268 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 1269 | _ => NONE
adamc@59 1270
adamc@158 1271 fun chaseMpath env (n, ms) =
adamc@158 1272 let
adamc@158 1273 val (_, sgn) = lookupStrNamed env n
adamc@158 1274 in
adamc@158 1275 foldl (fn (m, (str, sgn)) =>
adamc@158 1276 case projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1277 NONE => raise Fail "kindof: Unknown substructure"
adamc@158 1278 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
adamc@158 1279 ((StrVar n, #2 sgn), sgn) ms
adamc@158 1280 end
adamc@158 1281
adamc@42 1282 fun projectCon env {sgn, str, field} =
adamc@42 1283 case #1 (hnormSgn env sgn) of
adamc@34 1284 SgnConst sgis =>
adamc@34 1285 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 1286 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@341 1287 | SgiDatatype (x, _, xs, _) =>
adamc@158 1288 if x = field then
adamc@341 1289 let
adamc@341 1290 val k = (KType, #2 sgn)
adamc@341 1291 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 1292 in
adamc@341 1293 SOME (k', NONE)
adamc@341 1294 end
adamc@341 1295 else
adamc@341 1296 NONE
adamc@341 1297 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
adamc@341 1298 if x = field then
adamc@341 1299 let
adamc@341 1300 val k = (KType, #2 sgn)
adamc@341 1301 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 1302 in
adamc@341 1303 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
adamc@341 1304 end
adamc@158 1305 else
adamc@158 1306 NONE
adamc@563 1307 | SgiClassAbs (x, _, k) => if x = field then
adamc@563 1308 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), NONE)
adamc@563 1309 else
adamc@563 1310 NONE
adamc@563 1311 | SgiClass (x, _, k, c) => if x = field then
adamc@563 1312 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), SOME c)
adamc@563 1313 else
adamc@563 1314 NONE
adamc@34 1315 | _ => NONE) sgis of
adamc@34 1316 NONE => NONE
adamc@34 1317 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 1318 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 1319 | _ => NONE
adamc@34 1320
adamc@157 1321 fun projectDatatype env {sgn, str, field} =
adamc@157 1322 case #1 (hnormSgn env sgn) of
adamc@157 1323 SgnConst sgis =>
adamc@191 1324 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@191 1325 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@157 1326 | _ => NONE) sgis of
adamc@157 1327 NONE => NONE
adamc@191 1328 | SOME ((xs, xncs), subs) => SOME (xs,
adamc@191 1329 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
adamc@157 1330 | _ => NONE
adamc@157 1331
adamc@174 1332 fun projectConstructor env {sgn, str, field} =
adamc@174 1333 case #1 (hnormSgn env sgn) of
adamc@174 1334 SgnConst sgis =>
adamc@174 1335 let
adamc@191 1336 fun consider (n, xs, xncs) =
adamc@174 1337 ListUtil.search (fn (x, n', to) =>
adamc@174 1338 if x <> field then
adamc@174 1339 NONE
adamc@174 1340 else
adamc@191 1341 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
adamc@174 1342 in
adamc@191 1343 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
adamc@191 1344 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
adamc@174 1345 | _ => NONE) sgis of
adamc@174 1346 NONE => NONE
adamc@191 1347 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
adamc@191 1348 sgnSubCon (str, subs) t)
adamc@174 1349 end
adamc@174 1350 | _ => NONE
adamc@174 1351
adamc@42 1352 fun projectVal env {sgn, str, field} =
adamc@42 1353 case #1 (hnormSgn env sgn) of
adamc@34 1354 SgnConst sgis =>
adamc@162 1355 let
adamc@191 1356 fun seek (n, xs, xncs) =
adamc@162 1357 ListUtil.search (fn (x, _, to) =>
adamc@162 1358 if x = field then
adamc@191 1359 SOME (let
adamc@467 1360 val base = (CNamed n, #2 sgn)
adamc@467 1361 val nxs = length xs
adamc@467 1362 val base = ListUtil.foldli (fn (i, _, base) =>
adamc@467 1363 (CApp (base,
adamc@467 1364 (CRel (nxs - i - 1), #2 sgn)),
adamc@467 1365 #2 sgn))
adamc@467 1366 base xs
adamc@467 1367
adamc@191 1368 val t =
adamc@191 1369 case to of
adamc@467 1370 NONE => base
adamc@467 1371 | SOME t => (TFun (t, base), #2 sgn)
adamc@191 1372 val k = (KType, #2 sgn)
adamc@191 1373 in
adamc@467 1374 foldr (fn (x, t) => (TCFun (Implicit, x, k, t), #2 sgn))
adamc@191 1375 t xs
adamc@191 1376 end)
adamc@162 1377 else
adamc@162 1378 NONE) xncs
adamc@162 1379 in
adamc@162 1380 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
adamc@191 1381 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
adamc@191 1382 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
adamc@162 1383 | _ => NONE) sgis of
adamc@162 1384 NONE => NONE
adamc@162 1385 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
adamc@162 1386 end
adamc@34 1387 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 1388 | _ => NONE
adamc@34 1389
adamc@88 1390 fun sgnSeekConstraints (str, sgis) =
adamc@88 1391 let
adamc@88 1392 fun seek (sgis, sgns, strs, cons, acc) =
adamc@88 1393 case sgis of
adamc@88 1394 [] => acc
adamc@88 1395 | (sgi, _) :: sgis =>
adamc@88 1396 case sgi of
adamc@88 1397 SgiConstraint (c1, c2) =>
adamc@88 1398 let
adamc@88 1399 val sub = sgnSubCon (str, (sgns, strs, cons))
adamc@88 1400 in
adamc@88 1401 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
adamc@88 1402 end
adamc@88 1403 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1404 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 1405 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 1406 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1407 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
adamc@88 1408 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
adamc@88 1409 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
adamc@563 1410 | SgiClassAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@563 1411 | SgiClass (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1412 in
adamc@88 1413 seek (sgis, IM.empty, IM.empty, IM.empty, [])
adamc@88 1414 end
adamc@88 1415
adamc@88 1416 fun projectConstraints env {sgn, str} =
adamc@88 1417 case #1 (hnormSgn env sgn) of
adamc@88 1418 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
adamc@88 1419 | SgnError => SOME []
adamc@88 1420 | _ => NONE
adamc@34 1421
adamc@447 1422 fun edeclBinds env (d, loc) =
adamc@447 1423 case d of
adamc@447 1424 EDVal (x, t, _) => pushERel env x t
adamc@447 1425 | EDValRec vis => foldl (fn ((x, t, _), env) => pushERel env x t) env vis
adamc@447 1426
adamc@157 1427 fun declBinds env (d, loc) =
adamc@157 1428 case d of
adamc@157 1429 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@191 1430 | DDatatype (x, n, xs, xncs) =>
adamc@157 1431 let
adamc@297 1432 val k = (KType, loc)
adamc@297 1433 val nxs = length xs
adamc@297 1434 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@297 1435 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@297 1436 (KArrow (k, kb), loc)))
adamc@297 1437 ((CNamed n, loc), k) xs
adamc@297 1438
adamc@297 1439 val env = pushCNamedAs env x n kb NONE
adamc@191 1440 val env = pushDatatype env n xs xncs
adamc@157 1441 in
adamc@191 1442 foldl (fn ((x', n', to), env) =>
adamc@191 1443 let
adamc@191 1444 val t =
adamc@191 1445 case to of
adamc@297 1446 NONE => tb
adamc@297 1447 | SOME t => (TFun (t, tb), loc)
adamc@297 1448 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@191 1449 in
adamc@191 1450 pushENamedAs env x' n' t
adamc@191 1451 end)
adamc@191 1452 env xncs
adamc@157 1453 end
adamc@191 1454 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
adamc@157 1455 let
adamc@157 1456 val t = (CModProj (m, ms, x'), loc)
adamc@297 1457 val k = (KType, loc)
adamc@297 1458 val nxs = length xs
adamc@297 1459 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@297 1460 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@297 1461 (KArrow (k, kb), loc)))
adamc@297 1462 ((CNamed n, loc), k) xs
adamc@297 1463
adamc@341 1464 val env = pushCNamedAs env x n kb (SOME t)
adamc@191 1465 val env = pushDatatype env n xs xncs
adamc@157 1466 in
adamc@191 1467 foldl (fn ((x', n', to), env) =>
adamc@191 1468 let
adamc@191 1469 val t =
adamc@191 1470 case to of
adamc@297 1471 NONE => tb
adamc@297 1472 | SOME t => (TFun (t, tb), loc)
adamc@297 1473 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@191 1474 in
adamc@191 1475 pushENamedAs env x' n' t
adamc@191 1476 end)
adamc@191 1477 env xncs
adamc@157 1478 end
adamc@157 1479 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@157 1480 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
adamc@157 1481 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@157 1482 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@157 1483 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@157 1484 | DConstraint _ => env
adamc@157 1485 | DExport _ => env
adamc@707 1486 | DTable (tn, x, n, c, _, pc, _, cc) =>
adamc@205 1487 let
adamc@705 1488 val ct = (CModProj (tn, [], "sql_table"), loc)
adamc@705 1489 val ct = (CApp (ct, c), loc)
adamc@707 1490 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@338 1491 in
adamc@705 1492 pushENamedAs env x n ct
adamc@338 1493 end
adamc@338 1494 | DSequence (tn, x, n) =>
adamc@338 1495 let
adamc@338 1496 val t = (CModProj (tn, [], "sql_sequence"), loc)
adamc@205 1497 in
adamc@205 1498 pushENamedAs env x n t
adamc@205 1499 end
adamc@563 1500 | DClass (x, n, k, c) =>
adamc@213 1501 let
adamc@563 1502 val k = (KArrow (k, (KType, loc)), loc)
adamc@213 1503 val env = pushCNamedAs env x n k (SOME c)
adamc@213 1504 in
adamc@213 1505 pushClass env n
adamc@213 1506 end
adamc@271 1507 | DDatabase _ => env
adamc@459 1508 | DCookie (tn, x, n, c) =>
adamc@459 1509 let
adamc@459 1510 val t = (CApp ((CModProj (tn, [], "cookie"), loc), c), loc)
adamc@459 1511 in
adamc@459 1512 pushENamedAs env x n t
adamc@459 1513 end
adamc@720 1514 | DStyle (tn, x, n) =>
adamc@718 1515 let
adamc@720 1516 val t = (CModProj (tn, [], "css_class"), loc)
adamc@718 1517 in
adamc@718 1518 pushENamedAs env x n t
adamc@718 1519 end
adamc@157 1520
adamc@243 1521 fun patBinds env (p, loc) =
adamc@243 1522 case p of
adamc@243 1523 PWild => env
adamc@243 1524 | PVar (x, t) => pushERel env x t
adamc@243 1525 | PPrim _ => env
adamc@243 1526 | PCon (_, _, _, NONE) => env
adamc@243 1527 | PCon (_, _, _, SOME p) => patBinds env p
adamc@243 1528 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
adamc@243 1529
adamc@2 1530 end