annotate src/elab_env.sml @ 753:d484df4e841a

Preparing to allow views in SELECT FROM clauses
author Adam Chlipala <adamc@hcoop.net>
date Tue, 28 Apr 2009 14:02:23 -0400
parents 059074c8d2fc
children 8688e01ae469
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@753 592 exception Bad of con * con
adamc@750 593
adamc@753 594 fun resolveClass (hnorm : con -> con) (consEq : con * con -> bool) (env : env) =
adamc@674 595 let
adamc@711 596 fun resolve c =
adamc@711 597 let
adamc@711 598 fun doHead f =
adamc@711 599 case CM.find (#classes env, f) of
adamc@711 600 NONE => NONE
adamc@711 601 | SOME class =>
adamc@711 602 let
adamc@711 603 val loc = #2 c
adamc@675 604
adamc@753 605 fun generalize (c as (_, loc)) =
adamc@753 606 case #1 c of
adamc@753 607 CApp (f, x) =>
adamc@753 608 let
adamc@753 609 val (f, equate) = generalize f
adamc@753 610
adamc@753 611 fun isRecord () =
adamc@753 612 let
adamc@753 613 val rk = ref NONE
adamc@753 614 val k = (KUnif (loc, "k", rk), loc)
adamc@753 615 val r = ref NONE
adamc@753 616 val rc = (CUnif (loc, k, "x", r), loc)
adamc@753 617 in
adamc@753 618 ((CApp (f, rc), loc),
adamc@753 619 fn () => (if consEq (rc, x) then
adamc@753 620 true
adamc@753 621 else
adamc@753 622 (raise Bad (rc, x);
adamc@753 623 false))
adamc@753 624 andalso equate ())
adamc@753 625 end
adamc@753 626 in
adamc@753 627 case #1 x of
adamc@753 628 CConcat _ => isRecord ()
adamc@753 629 | CRecord _ => isRecord ()
adamc@753 630 | _ => ((CApp (f, x), loc), equate)
adamc@753 631 end
adamc@753 632 | _ => (c, fn () => true)
adamc@753 633
adamc@753 634 val (c, equate) = generalize c
adamc@753 635
adamc@711 636 fun tryRules rules =
adamc@711 637 case rules of
adamc@675 638 [] => NONE
adamc@711 639 | (nRs, cs, c', e) :: rules' =>
adamc@711 640 case tryUnify nRs (c, c') of
adamc@711 641 NONE => tryRules rules'
adamc@711 642 | SOME rs =>
adamc@675 643 let
adamc@711 644 val eos = map (resolve o unifySubst rs) cs
adamc@675 645 in
adamc@750 646 if List.exists (not o Option.isSome) eos
adamc@753 647 orelse not (equate ())
adamc@753 648 orelse not (consEq (c, unifySubst rs c')) then
adamc@711 649 tryRules rules'
adamc@711 650 else
adamc@711 651 let
adamc@711 652 val es = List.mapPartial (fn x => x) eos
adamc@711 653
adamc@711 654 val e = foldr (fn (c, e) => (ECApp (e, c), loc)) e rs
adamc@711 655 val e = foldl (fn (e', e) => (EApp (e, e'), loc)) e es
adamc@711 656 in
adamc@711 657 SOME e
adamc@711 658 end
adamc@675 659 end
adamc@711 660
adamc@711 661 fun rules () = tryRules (#rules class)
adamc@711 662
adamc@711 663 fun tryGrounds ces =
adamc@711 664 case ces of
adamc@711 665 [] => rules ()
adamc@711 666 | (c', e) :: ces' =>
adamc@711 667 case tryUnify 0 (c, c') of
adamc@711 668 NONE => tryGrounds ces'
adamc@711 669 | SOME _ => SOME e
adamc@675 670 in
adamc@711 671 tryGrounds (#ground class)
adamc@675 672 end
adamc@711 673 in
adamc@750 674 case #1 c of
adamc@750 675 TRecord c =>
adamc@750 676 (case #1 (hnorm c) of
adamc@750 677 CRecord (_, xts) =>
adamc@750 678 let
adamc@750 679 fun resolver (xts, acc) =
adamc@750 680 case xts of
adamc@750 681 [] => SOME (ERecord acc, #2 c)
adamc@750 682 | (x, t) :: xts =>
adamc@750 683 let
adamc@750 684 val t = hnorm t
adamc@750 685
adamc@750 686 val t = case t of
adamc@750 687 (CApp (f, x), loc) => (CApp (hnorm f, hnorm x), loc)
adamc@750 688 | _ => t
adamc@750 689 in
adamc@750 690 case resolve t of
adamc@750 691 NONE => NONE
adamc@750 692 | SOME e => resolver (xts, (x, e, t) :: acc)
adamc@750 693 end
adamc@750 694 in
adamc@750 695 resolver (xts, [])
adamc@750 696 end
adamc@750 697 | _ => NONE)
adamc@750 698 | _ =>
adamc@750 699 case class_head_in c of
adamc@750 700 SOME f => doHead f
adamc@750 701 | _ => NONE
adamc@711 702 end
adamc@674 703 in
adamc@711 704 resolve
adamc@674 705 end
adamc@211 706
adamc@9 707 fun pushERel (env : env) x t =
adamc@9 708 let
adamc@9 709 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 710 | x => x) (#renameE env)
adamc@211 711
adamc@675 712 val classes = CM.map (fn class =>
adamc@711 713 {ground = map (fn (c, e) => (c, liftExp e)) (#ground class),
adamc@711 714 rules = #rules class}) (#classes env)
adamc@711 715 val classes = case class_head_in t of
adamc@211 716 NONE => classes
adamc@711 717 | SOME f =>
adamc@403 718 case CM.find (classes, f) of
adamc@403 719 NONE => classes
adamc@403 720 | SOME class =>
adamc@403 721 let
adamc@711 722 val class = {ground = (t, (ERel 0, #2 t)) :: #ground class,
adamc@711 723 rules = #rules class}
adamc@403 724 in
adamc@403 725 CM.insert (classes, f, class)
adamc@403 726 end
adamc@9 727 in
adamc@623 728 {renameK = #renameK env,
adamc@623 729 relK = #relK env,
adamc@623 730
adamc@623 731 renameC = #renameC env,
adamc@9 732 relC = #relC env,
adamc@9 733 namedC = #namedC env,
adamc@9 734
adamc@157 735 datatypes = #datatypes env,
adamc@171 736 constructors = #constructors env,
adamc@157 737
adamc@211 738 classes = classes,
adamc@211 739
adamc@9 740 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 741 relE = (x, t) :: #relE env,
adamc@31 742 namedE = #namedE env,
adamc@31 743
adamc@31 744 renameSgn = #renameSgn env,
adamc@31 745 sgn = #sgn env,
adamc@31 746
adamc@31 747 renameStr = #renameStr env,
adamc@31 748 str = #str env}
adamc@9 749 end
adamc@9 750
adamc@9 751 fun lookupERel (env : env) n =
adamc@9 752 (List.nth (#relE env, n))
adamc@9 753 handle Subscript => raise UnboundRel n
adamc@9 754
adamc@674 755 fun rule_in c =
adamc@674 756 let
adamc@674 757 fun quantifiers (c, nvars) =
adamc@674 758 case #1 c of
adamc@674 759 TCFun (_, _, _, c) => quantifiers (c, nvars + 1)
adamc@674 760 | _ =>
adamc@674 761 let
adamc@674 762 fun clauses (c, hyps) =
adamc@674 763 case #1 c of
adamc@674 764 TFun (hyp, c) =>
adamc@711 765 (case class_head_in hyp of
adamc@711 766 SOME _ => clauses (c, hyp :: hyps)
adamc@711 767 | NONE => NONE)
adamc@674 768 | _ =>
adamc@711 769 case class_head_in c of
adamc@674 770 NONE => NONE
adamc@711 771 | SOME f => SOME (f, nvars, rev hyps, c)
adamc@674 772 in
adamc@674 773 clauses (c, [])
adamc@674 774 end
adamc@674 775 in
adamc@711 776 quantifiers (c, 0)
adamc@677 777 end
adamc@677 778
adamc@9 779 fun pushENamedAs (env : env) x n t =
adamc@211 780 let
adamc@211 781 val classes = #classes env
adamc@674 782 val classes = case rule_in t of
adamc@211 783 NONE => classes
adamc@711 784 | SOME (f, nvs, cs, c) =>
adamc@403 785 case CM.find (classes, f) of
adamc@403 786 NONE => classes
adamc@403 787 | SOME class =>
adamc@403 788 let
adamc@675 789 val e = (ENamed n, #2 t)
adamc@675 790
adamc@675 791 val class =
adamc@711 792 {ground = #ground class,
adamc@711 793 rules = (nvs, cs, c, e) :: #rules class}
adamc@403 794 in
adamc@403 795 CM.insert (classes, f, class)
adamc@403 796 end
adamc@211 797 in
adamc@623 798 {renameK = #renameK env,
adamc@623 799 relK = #relK env,
adamc@623 800
adamc@623 801 renameC = #renameC env,
adamc@211 802 relC = #relC env,
adamc@211 803 namedC = #namedC env,
adamc@9 804
adamc@211 805 datatypes = #datatypes env,
adamc@211 806 constructors = #constructors env,
adamc@157 807
adamc@211 808 classes = classes,
adamc@31 809
adamc@211 810 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@211 811 relE = #relE env,
adamc@211 812 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@211 813
adamc@211 814 renameSgn = #renameSgn env,
adamc@211 815 sgn = #sgn env,
adamc@211 816
adamc@211 817 renameStr = #renameStr env,
adamc@211 818 str = #str env}
adamc@211 819 end
adamc@9 820
adamc@9 821 fun pushENamed env x t =
adamc@9 822 let
adamc@9 823 val n = !namedCounter
adamc@9 824 in
adamc@9 825 namedCounter := n + 1;
adamc@9 826 (pushENamedAs env x n t, n)
adamc@9 827 end
adamc@9 828
adamc@9 829 fun lookupENamed (env : env) n =
adamc@9 830 case IM.find (#namedE env, n) of
adamc@9 831 NONE => raise UnboundNamed n
adamc@9 832 | SOME x => x
adamc@9 833
adamc@471 834 fun checkENamed (env : env) n =
adamc@471 835 Option.isSome (IM.find (#namedE env, n))
adamc@471 836
adamc@9 837 fun lookupE (env : env) x =
adamc@9 838 case SM.find (#renameE env, x) of
adamc@9 839 NONE => NotBound
adamc@9 840 | SOME (Rel' x) => Rel x
adamc@9 841 | SOME (Named' x) => Named x
adamc@2 842
adamc@31 843 fun pushSgnNamedAs (env : env) x n sgis =
adamc@623 844 {renameK = #renameK env,
adamc@623 845 relK = #relK env,
adamc@623 846
adamc@623 847 renameC = #renameC env,
adamc@31 848 relC = #relC env,
adamc@31 849 namedC = #namedC env,
adamc@31 850
adamc@157 851 datatypes = #datatypes env,
adamc@171 852 constructors = #constructors env,
adamc@157 853
adamc@211 854 classes = #classes env,
adamc@211 855
adamc@31 856 renameE = #renameE env,
adamc@31 857 relE = #relE env,
adamc@31 858 namedE = #namedE env,
adamc@31 859
adamc@31 860 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
adamc@31 861 sgn = IM.insert (#sgn env, n, (x, sgis)),
adamc@31 862
adamc@31 863 renameStr = #renameStr env,
adamc@31 864 str = #str env}
adamc@31 865
adamc@31 866 fun pushSgnNamed env x sgis =
adamc@31 867 let
adamc@31 868 val n = !namedCounter
adamc@31 869 in
adamc@31 870 namedCounter := n + 1;
adamc@31 871 (pushSgnNamedAs env x n sgis, n)
adamc@31 872 end
adamc@31 873
adamc@31 874 fun lookupSgnNamed (env : env) n =
adamc@31 875 case IM.find (#sgn env, n) of
adamc@31 876 NONE => raise UnboundNamed n
adamc@31 877 | SOME x => x
adamc@31 878
adamc@31 879 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
adamc@31 880
adamc@31 881 fun lookupStrNamed (env : env) n =
adamc@31 882 case IM.find (#str env, n) of
adamc@31 883 NONE => raise UnboundNamed n
adamc@31 884 | SOME x => x
adamc@31 885
adamc@31 886 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
adamc@31 887
adamc@203 888
adamc@216 889 fun sgiSeek (sgi, (sgns, strs, cons)) =
adamc@216 890 case sgi of
adamc@216 891 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 892 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 893 | SgiDatatype (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 894 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 895 | SgiVal _ => (sgns, strs, cons)
adamc@216 896 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
adamc@216 897 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
adamc@216 898 | SgiConstraint _ => (sgns, strs, cons)
adamc@563 899 | SgiClassAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@563 900 | SgiClass (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 901
adamc@34 902 fun sgnSeek f sgis =
adamc@34 903 let
adamc@59 904 fun seek (sgis, sgns, strs, cons) =
adamc@34 905 case sgis of
adamc@34 906 [] => NONE
adamc@34 907 | (sgi, _) :: sgis =>
adamc@34 908 case f sgi of
adamc@158 909 SOME v =>
adamc@158 910 let
adamc@158 911 val cons =
adamc@158 912 case sgi of
adamc@191 913 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
adamc@191 914 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
adamc@158 915 | _ => cons
adamc@158 916 in
adamc@158 917 SOME (v, (sgns, strs, cons))
adamc@158 918 end
adamc@88 919 | NONE =>
adamc@216 920 let
adamc@216 921 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
adamc@216 922 in
adamc@216 923 seek (sgis, sgns, strs, cons)
adamc@216 924 end
adamc@34 925 in
adamc@59 926 seek (sgis, IM.empty, IM.empty, IM.empty)
adamc@34 927 end
adamc@34 928
adamc@34 929 fun id x = x
adamc@34 930
adamc@34 931 fun unravelStr (str, _) =
adamc@34 932 case str of
adamc@34 933 StrVar x => (x, [])
adamc@34 934 | StrProj (str, m) =>
adamc@34 935 let
adamc@34 936 val (x, ms) = unravelStr str
adamc@34 937 in
adamc@34 938 (x, ms @ [m])
adamc@34 939 end
adamc@34 940 | _ => raise Fail "unravelStr"
adamc@34 941
adamc@59 942 fun sgnS_con (str, (sgns, strs, cons)) c =
adamc@34 943 case c of
adamc@34 944 CModProj (m1, ms, x) =>
adamc@34 945 (case IM.find (strs, m1) of
adamc@34 946 NONE => c
adamc@34 947 | SOME m1x =>
adamc@34 948 let
adamc@34 949 val (m1, ms') = unravelStr str
adamc@34 950 in
adamc@34 951 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 952 end)
adamc@34 953 | CNamed n =>
adamc@34 954 (case IM.find (cons, n) of
adamc@34 955 NONE => c
adamc@34 956 | SOME nx =>
adamc@34 957 let
adamc@34 958 val (m1, ms) = unravelStr str
adamc@34 959 in
adamc@34 960 CModProj (m1, ms, nx)
adamc@34 961 end)
adamc@34 962 | _ => c
adamc@34 963
adamc@721 964 fun sgnS_con' (m1, ms', (sgns, strs, cons)) =
adamc@721 965 U.Con.map {kind = fn x => x,
adamc@721 966 con = fn c =>
adamc@721 967 case c of
adamc@721 968 CModProj (m1, ms, x) =>
adamc@721 969 (case IM.find (strs, m1) of
adamc@721 970 NONE => c
adamc@721 971 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
adamc@721 972 | CNamed n =>
adamc@721 973 (case IM.find (cons, n) of
adamc@721 974 NONE => c
adamc@721 975 | SOME nx => CModProj (m1, ms', nx))
adamc@721 976 | _ => c}
adamc@216 977
adamc@59 978 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
adamc@59 979 case sgn of
adamc@59 980 SgnProj (m1, ms, x) =>
adamc@59 981 (case IM.find (strs, m1) of
adamc@59 982 NONE => sgn
adamc@59 983 | SOME m1x =>
adamc@59 984 let
adamc@59 985 val (m1, ms') = unravelStr str
adamc@59 986 in
adamc@59 987 SgnProj (m1, ms' @ m1x :: ms, x)
adamc@59 988 end)
adamc@59 989 | SgnVar n =>
adamc@59 990 (case IM.find (sgns, n) of
adamc@59 991 NONE => sgn
adamc@59 992 | SOME nx =>
adamc@59 993 let
adamc@59 994 val (m1, ms) = unravelStr str
adamc@59 995 in
adamc@59 996 SgnProj (m1, ms, nx)
adamc@59 997 end)
adamc@59 998 | _ => sgn
adamc@59 999
adamc@59 1000 fun sgnSubSgn x =
adamc@34 1001 ElabUtil.Sgn.map {kind = id,
adamc@59 1002 con = sgnS_con x,
adamc@34 1003 sgn_item = id,
adamc@59 1004 sgn = sgnS_sgn x}
adamc@34 1005
adamc@211 1006
adamc@211 1007
adamc@211 1008 and projectSgn env {sgn, str, field} =
adamc@211 1009 case #1 (hnormSgn env sgn) of
adamc@211 1010 SgnConst sgis =>
adamc@211 1011 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@211 1012 NONE => NONE
adamc@211 1013 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@211 1014 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@211 1015 | _ => NONE
adamc@211 1016
adamc@211 1017 and hnormSgn env (all as (sgn, loc)) =
adamc@34 1018 case sgn of
adamc@42 1019 SgnError => all
adamc@42 1020 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 1021 | SgnConst _ => all
adamc@42 1022 | SgnFun _ => all
adamc@59 1023 | SgnProj (m, ms, x) =>
adamc@59 1024 let
adamc@59 1025 val (_, sgn) = lookupStrNamed env m
adamc@59 1026 in
adamc@59 1027 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
adamc@59 1028 sgn = sgn,
adamc@59 1029 field = x} of
adamc@59 1030 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
adamc@59 1031 | SOME sgn => sgn
adamc@59 1032 end
adamc@42 1033 | SgnWhere (sgn, x, c) =>
adamc@42 1034 case #1 (hnormSgn env sgn) of
adamc@42 1035 SgnError => (SgnError, loc)
adamc@42 1036 | SgnConst sgis =>
adamc@42 1037 let
adamc@42 1038 fun traverse (pre, post) =
adamc@42 1039 case post of
adamc@42 1040 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 1041 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adamc@42 1042 if x = x' then
adamc@42 1043 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adamc@42 1044 else
adamc@42 1045 traverse (sgi :: pre, rest)
adamc@42 1046 | sgi :: rest => traverse (sgi :: pre, rest)
adamc@42 1047
adamc@42 1048 val sgis = traverse ([], sgis)
adamc@42 1049 in
adamc@42 1050 (SgnConst sgis, loc)
adamc@42 1051 end
adamc@42 1052 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adamc@42 1053
adamc@211 1054 fun enrichClasses env classes (m1, ms) sgn =
adamc@59 1055 case #1 (hnormSgn env sgn) of
adamc@59 1056 SgnConst sgis =>
adamc@211 1057 let
adamc@218 1058 val (classes, _, _, _) =
adamc@218 1059 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
adamc@211 1060 let
adamc@211 1061 fun found (x, n) =
adamc@211 1062 (CM.insert (classes,
adamc@211 1063 ClProj (m1, ms, x),
adamc@211 1064 empty_class),
adamc@216 1065 IM.insert (newClasses, n, x),
adamc@218 1066 sgiSeek (#1 sgi, fmap),
adamc@218 1067 env)
adamc@216 1068
adamc@218 1069 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
adamc@211 1070 in
adamc@211 1071 case #1 sgi of
adamc@217 1072 SgiStr (x, _, sgn) =>
adamc@217 1073 (enrichClasses env classes (m1, ms @ [x]) sgn,
adamc@217 1074 newClasses,
adamc@218 1075 sgiSeek (#1 sgi, fmap),
adamc@218 1076 env)
adamc@218 1077 | SgiSgn (x, n, sgn) =>
adamc@218 1078 (classes,
adamc@218 1079 newClasses,
adamc@218 1080 fmap,
adamc@218 1081 pushSgnNamedAs env x n sgn)
adamc@218 1082
adamc@563 1083 | SgiClassAbs (x, n, _) => found (x, n)
adamc@563 1084 | SgiClass (x, n, _, _) => found (x, n)
adamc@674 1085 | SgiVal (x, n, c) =>
adamc@674 1086 (case rule_in c of
adamc@674 1087 NONE => default ()
adamc@711 1088 | SOME (cn, nvs, cs, c) =>
adamc@674 1089 let
adamc@711 1090 val loc = #2 c
adamc@721 1091 val globalize = sgnS_con' (m1, ms, fmap)
adamc@674 1092
adamc@674 1093 val nc =
adamc@674 1094 case cn of
adamc@674 1095 ClNamed f => IM.find (newClasses, f)
adamc@674 1096 | _ => NONE
adamc@674 1097 in
adamc@674 1098 case nc of
adamc@674 1099 NONE =>
adamc@419 1100 let
adamc@419 1101 val classes =
adamc@419 1102 case CM.find (classes, cn) of
adamc@419 1103 NONE => classes
adamc@419 1104 | SOME class =>
adamc@419 1105 let
adamc@711 1106 val e = (EModProj (m1, ms, x), #2 sgn)
adamc@675 1107
adamc@675 1108 val class =
adamc@711 1109 {ground = #ground class,
adamc@711 1110 rules = (nvs,
adamc@711 1111 map globalize cs,
adamc@711 1112 globalize c,
adamc@711 1113 e) :: #rules class}
adamc@419 1114 in
adamc@419 1115 CM.insert (classes, cn, class)
adamc@419 1116 end
adamc@419 1117 in
adamc@419 1118 (classes,
adamc@419 1119 newClasses,
adamc@419 1120 fmap,
adamc@419 1121 env)
adamc@419 1122 end
adamc@674 1123 | SOME fx =>
adamc@674 1124 let
adamc@674 1125 val cn = ClProj (m1, ms, fx)
adamc@419 1126
adamc@674 1127 val classes =
adamc@674 1128 case CM.find (classes, cn) of
adamc@674 1129 NONE => classes
adamc@674 1130 | SOME class =>
adamc@674 1131 let
adamc@675 1132 val e = (EModProj (m1, ms, x), #2 sgn)
adamc@675 1133
adamc@675 1134 val class =
adamc@711 1135 {ground = #ground class,
adamc@711 1136 rules = (nvs,
adamc@711 1137 map globalize cs,
adamc@711 1138 globalize c,
adamc@711 1139 e) :: #rules class}
adamc@674 1140 in
adamc@674 1141 CM.insert (classes, cn, class)
adamc@674 1142 end
adamc@674 1143 in
adamc@674 1144 (classes,
adamc@674 1145 newClasses,
adamc@674 1146 fmap,
adamc@674 1147 env)
adamc@674 1148 end
adamc@674 1149 end)
adamc@216 1150 | _ => default ()
adamc@211 1151 end)
adamc@218 1152 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
adamc@211 1153 in
adamc@211 1154 classes
adamc@211 1155 end
adamc@211 1156 | _ => classes
adamc@211 1157
adamc@211 1158 fun pushStrNamedAs (env : env) x n sgn =
adamc@623 1159 {renameK = #renameK env,
adamc@623 1160 relK = #relK env,
adamc@623 1161
adamc@623 1162 renameC = #renameC env,
adamc@211 1163 relC = #relC env,
adamc@211 1164 namedC = #namedC env,
adamc@211 1165
adamc@211 1166 datatypes = #datatypes env,
adamc@211 1167 constructors = #constructors env,
adamc@211 1168
adamc@211 1169 classes = enrichClasses env (#classes env) (n, []) sgn,
adamc@211 1170
adamc@211 1171 renameE = #renameE env,
adamc@211 1172 relE = #relE env,
adamc@211 1173 namedE = #namedE env,
adamc@211 1174
adamc@211 1175 renameSgn = #renameSgn env,
adamc@211 1176 sgn = #sgn env,
adamc@211 1177
adamc@211 1178 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
adamc@211 1179 str = IM.insert (#str env, n, (x, sgn))}
adamc@211 1180
adamc@211 1181 fun pushStrNamed env x sgn =
adamc@211 1182 let
adamc@211 1183 val n = !namedCounter
adamc@211 1184 in
adamc@211 1185 namedCounter := n + 1;
adamc@211 1186 (pushStrNamedAs env x n sgn, n)
adamc@211 1187 end
adamc@211 1188
adamc@211 1189 fun sgiBinds env (sgi, loc) =
adamc@211 1190 case sgi of
adamc@211 1191 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@211 1192 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@211 1193 | SgiDatatype (x, n, xs, xncs) =>
adamc@211 1194 let
adamc@341 1195 val k = (KType, loc)
adamc@341 1196 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 1197
adamc@341 1198 val env = pushCNamedAs env x n k' NONE
adamc@211 1199 in
adamc@211 1200 foldl (fn ((x', n', to), env) =>
adamc@211 1201 let
adamc@211 1202 val t =
adamc@211 1203 case to of
adamc@211 1204 NONE => (CNamed n, loc)
adamc@211 1205 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 1206
adamc@211 1207 val k = (KType, loc)
adamc@211 1208 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 1209 in
adamc@211 1210 pushENamedAs env x' n' t
adamc@211 1211 end)
adamc@211 1212 env xncs
adamc@211 1213 end
adamc@211 1214 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@211 1215 let
adamc@341 1216 val k = (KType, loc)
adamc@341 1217 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 1218
adamc@341 1219 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
adamc@211 1220 in
adamc@211 1221 foldl (fn ((x', n', to), env) =>
adamc@211 1222 let
adamc@211 1223 val t =
adamc@211 1224 case to of
adamc@211 1225 NONE => (CNamed n, loc)
adamc@211 1226 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 1227
adamc@211 1228 val k = (KType, loc)
adamc@211 1229 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 1230 in
adamc@211 1231 pushENamedAs env x' n' t
adamc@211 1232 end)
adamc@211 1233 env xncs
adamc@211 1234 end
adamc@211 1235 | SgiVal (x, n, t) => pushENamedAs env x n t
adamc@211 1236 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@211 1237 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@211 1238 | SgiConstraint _ => env
adamc@211 1239
adamc@711 1240 | SgiClassAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@711 1241 | SgiClass (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@459 1242
adamc@211 1243 fun sgnSubCon x =
adamc@211 1244 ElabUtil.Con.map {kind = id,
adamc@211 1245 con = sgnS_con x}
adamc@59 1246
adamc@59 1247 fun projectStr env {sgn, str, field} =
adamc@59 1248 case #1 (hnormSgn env sgn) of
adamc@59 1249 SgnConst sgis =>
adamc@59 1250 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 1251 NONE => NONE
adamc@59 1252 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 1253 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 1254 | _ => NONE
adamc@59 1255
adamc@158 1256 fun chaseMpath env (n, ms) =
adamc@158 1257 let
adamc@158 1258 val (_, sgn) = lookupStrNamed env n
adamc@158 1259 in
adamc@158 1260 foldl (fn (m, (str, sgn)) =>
adamc@158 1261 case projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1262 NONE => raise Fail "kindof: Unknown substructure"
adamc@158 1263 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
adamc@158 1264 ((StrVar n, #2 sgn), sgn) ms
adamc@158 1265 end
adamc@158 1266
adamc@42 1267 fun projectCon env {sgn, str, field} =
adamc@42 1268 case #1 (hnormSgn env sgn) of
adamc@34 1269 SgnConst sgis =>
adamc@34 1270 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 1271 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@341 1272 | SgiDatatype (x, _, xs, _) =>
adamc@158 1273 if x = field then
adamc@341 1274 let
adamc@341 1275 val k = (KType, #2 sgn)
adamc@341 1276 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 1277 in
adamc@341 1278 SOME (k', NONE)
adamc@341 1279 end
adamc@341 1280 else
adamc@341 1281 NONE
adamc@341 1282 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
adamc@341 1283 if x = field then
adamc@341 1284 let
adamc@341 1285 val k = (KType, #2 sgn)
adamc@341 1286 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 1287 in
adamc@341 1288 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
adamc@341 1289 end
adamc@158 1290 else
adamc@158 1291 NONE
adamc@563 1292 | SgiClassAbs (x, _, k) => if x = field then
adamc@563 1293 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), NONE)
adamc@563 1294 else
adamc@563 1295 NONE
adamc@563 1296 | SgiClass (x, _, k, c) => if x = field then
adamc@563 1297 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), SOME c)
adamc@563 1298 else
adamc@563 1299 NONE
adamc@34 1300 | _ => NONE) sgis of
adamc@34 1301 NONE => NONE
adamc@34 1302 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 1303 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 1304 | _ => NONE
adamc@34 1305
adamc@157 1306 fun projectDatatype env {sgn, str, field} =
adamc@157 1307 case #1 (hnormSgn env sgn) of
adamc@157 1308 SgnConst sgis =>
adamc@191 1309 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@191 1310 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@157 1311 | _ => NONE) sgis of
adamc@157 1312 NONE => NONE
adamc@191 1313 | SOME ((xs, xncs), subs) => SOME (xs,
adamc@191 1314 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
adamc@157 1315 | _ => NONE
adamc@157 1316
adamc@174 1317 fun projectConstructor env {sgn, str, field} =
adamc@174 1318 case #1 (hnormSgn env sgn) of
adamc@174 1319 SgnConst sgis =>
adamc@174 1320 let
adamc@191 1321 fun consider (n, xs, xncs) =
adamc@174 1322 ListUtil.search (fn (x, n', to) =>
adamc@174 1323 if x <> field then
adamc@174 1324 NONE
adamc@174 1325 else
adamc@191 1326 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
adamc@174 1327 in
adamc@191 1328 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
adamc@191 1329 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
adamc@174 1330 | _ => NONE) sgis of
adamc@174 1331 NONE => NONE
adamc@191 1332 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
adamc@191 1333 sgnSubCon (str, subs) t)
adamc@174 1334 end
adamc@174 1335 | _ => NONE
adamc@174 1336
adamc@42 1337 fun projectVal env {sgn, str, field} =
adamc@42 1338 case #1 (hnormSgn env sgn) of
adamc@34 1339 SgnConst sgis =>
adamc@162 1340 let
adamc@191 1341 fun seek (n, xs, xncs) =
adamc@162 1342 ListUtil.search (fn (x, _, to) =>
adamc@162 1343 if x = field then
adamc@191 1344 SOME (let
adamc@467 1345 val base = (CNamed n, #2 sgn)
adamc@467 1346 val nxs = length xs
adamc@467 1347 val base = ListUtil.foldli (fn (i, _, base) =>
adamc@467 1348 (CApp (base,
adamc@467 1349 (CRel (nxs - i - 1), #2 sgn)),
adamc@467 1350 #2 sgn))
adamc@467 1351 base xs
adamc@467 1352
adamc@191 1353 val t =
adamc@191 1354 case to of
adamc@467 1355 NONE => base
adamc@467 1356 | SOME t => (TFun (t, base), #2 sgn)
adamc@191 1357 val k = (KType, #2 sgn)
adamc@191 1358 in
adamc@467 1359 foldr (fn (x, t) => (TCFun (Implicit, x, k, t), #2 sgn))
adamc@191 1360 t xs
adamc@191 1361 end)
adamc@162 1362 else
adamc@162 1363 NONE) xncs
adamc@162 1364 in
adamc@162 1365 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
adamc@191 1366 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
adamc@191 1367 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
adamc@162 1368 | _ => NONE) sgis of
adamc@162 1369 NONE => NONE
adamc@162 1370 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
adamc@162 1371 end
adamc@34 1372 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 1373 | _ => NONE
adamc@34 1374
adamc@88 1375 fun sgnSeekConstraints (str, sgis) =
adamc@88 1376 let
adamc@88 1377 fun seek (sgis, sgns, strs, cons, acc) =
adamc@88 1378 case sgis of
adamc@88 1379 [] => acc
adamc@88 1380 | (sgi, _) :: sgis =>
adamc@88 1381 case sgi of
adamc@88 1382 SgiConstraint (c1, c2) =>
adamc@88 1383 let
adamc@88 1384 val sub = sgnSubCon (str, (sgns, strs, cons))
adamc@88 1385 in
adamc@88 1386 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
adamc@88 1387 end
adamc@88 1388 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1389 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 1390 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 1391 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1392 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
adamc@88 1393 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
adamc@88 1394 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
adamc@563 1395 | SgiClassAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@563 1396 | SgiClass (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1397 in
adamc@88 1398 seek (sgis, IM.empty, IM.empty, IM.empty, [])
adamc@88 1399 end
adamc@88 1400
adamc@88 1401 fun projectConstraints env {sgn, str} =
adamc@88 1402 case #1 (hnormSgn env sgn) of
adamc@88 1403 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
adamc@88 1404 | SgnError => SOME []
adamc@88 1405 | _ => NONE
adamc@34 1406
adamc@447 1407 fun edeclBinds env (d, loc) =
adamc@447 1408 case d of
adamc@447 1409 EDVal (x, t, _) => pushERel env x t
adamc@447 1410 | EDValRec vis => foldl (fn ((x, t, _), env) => pushERel env x t) env vis
adamc@447 1411
adamc@157 1412 fun declBinds env (d, loc) =
adamc@157 1413 case d of
adamc@157 1414 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@191 1415 | DDatatype (x, n, xs, xncs) =>
adamc@157 1416 let
adamc@297 1417 val k = (KType, loc)
adamc@297 1418 val nxs = length xs
adamc@297 1419 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@297 1420 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@297 1421 (KArrow (k, kb), loc)))
adamc@297 1422 ((CNamed n, loc), k) xs
adamc@297 1423
adamc@297 1424 val env = pushCNamedAs env x n kb NONE
adamc@191 1425 val env = pushDatatype env n xs xncs
adamc@157 1426 in
adamc@191 1427 foldl (fn ((x', n', to), env) =>
adamc@191 1428 let
adamc@191 1429 val t =
adamc@191 1430 case to of
adamc@297 1431 NONE => tb
adamc@297 1432 | SOME t => (TFun (t, tb), loc)
adamc@297 1433 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@191 1434 in
adamc@191 1435 pushENamedAs env x' n' t
adamc@191 1436 end)
adamc@191 1437 env xncs
adamc@157 1438 end
adamc@191 1439 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
adamc@157 1440 let
adamc@157 1441 val t = (CModProj (m, ms, x'), loc)
adamc@297 1442 val k = (KType, loc)
adamc@297 1443 val nxs = length xs
adamc@297 1444 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@297 1445 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@297 1446 (KArrow (k, kb), loc)))
adamc@297 1447 ((CNamed n, loc), k) xs
adamc@297 1448
adamc@341 1449 val env = pushCNamedAs env x n kb (SOME t)
adamc@191 1450 val env = pushDatatype env n xs xncs
adamc@157 1451 in
adamc@191 1452 foldl (fn ((x', n', to), env) =>
adamc@191 1453 let
adamc@191 1454 val t =
adamc@191 1455 case to of
adamc@297 1456 NONE => tb
adamc@297 1457 | SOME t => (TFun (t, tb), loc)
adamc@297 1458 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@191 1459 in
adamc@191 1460 pushENamedAs env x' n' t
adamc@191 1461 end)
adamc@191 1462 env xncs
adamc@157 1463 end
adamc@157 1464 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@157 1465 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
adamc@157 1466 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@157 1467 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@157 1468 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@157 1469 | DConstraint _ => env
adamc@157 1470 | DExport _ => env
adamc@707 1471 | DTable (tn, x, n, c, _, pc, _, cc) =>
adamc@205 1472 let
adamc@705 1473 val ct = (CModProj (tn, [], "sql_table"), loc)
adamc@705 1474 val ct = (CApp (ct, c), loc)
adamc@707 1475 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@338 1476 in
adamc@705 1477 pushENamedAs env x n ct
adamc@338 1478 end
adamc@338 1479 | DSequence (tn, x, n) =>
adamc@338 1480 let
adamc@338 1481 val t = (CModProj (tn, [], "sql_sequence"), loc)
adamc@205 1482 in
adamc@205 1483 pushENamedAs env x n t
adamc@205 1484 end
adamc@563 1485 | DClass (x, n, k, c) =>
adamc@213 1486 let
adamc@563 1487 val k = (KArrow (k, (KType, loc)), loc)
adamc@213 1488 val env = pushCNamedAs env x n k (SOME c)
adamc@213 1489 in
adamc@213 1490 pushClass env n
adamc@213 1491 end
adamc@271 1492 | DDatabase _ => env
adamc@459 1493 | DCookie (tn, x, n, c) =>
adamc@459 1494 let
adamc@459 1495 val t = (CApp ((CModProj (tn, [], "cookie"), loc), c), loc)
adamc@459 1496 in
adamc@459 1497 pushENamedAs env x n t
adamc@459 1498 end
adamc@720 1499 | DStyle (tn, x, n) =>
adamc@718 1500 let
adamc@720 1501 val t = (CModProj (tn, [], "css_class"), loc)
adamc@718 1502 in
adamc@718 1503 pushENamedAs env x n t
adamc@718 1504 end
adamc@157 1505
adamc@243 1506 fun patBinds env (p, loc) =
adamc@243 1507 case p of
adamc@243 1508 PWild => env
adamc@243 1509 | PVar (x, t) => pushERel env x t
adamc@243 1510 | PPrim _ => env
adamc@243 1511 | PCon (_, _, _, NONE) => env
adamc@243 1512 | PCon (_, _, _, SOME p) => patBinds env p
adamc@243 1513 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
adamc@243 1514
adamc@2 1515 end