annotate src/elab_env.sml @ 677:81573f62d6c3

Enforce termination of type class instances
author Adam Chlipala <adamc@hcoop.net>
date Thu, 26 Mar 2009 15:54:04 -0400
parents 43430b7190f4
children f0224c7f12bb
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@216 165 fun cn2s cn =
adamc@216 166 case cn of
adamc@216 167 ClNamed n => "Named(" ^ Int.toString n ^ ")"
adamc@216 168 | ClProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
adamc@216 169
adamc@211 170 structure CK = struct
adamc@211 171 type ord_key = class_name
adamc@211 172 open Order
adamc@211 173 fun compare x =
adamc@211 174 case x of
adamc@211 175 (ClNamed n1, ClNamed n2) => Int.compare (n1, n2)
adamc@211 176 | (ClNamed _, _) => LESS
adamc@211 177 | (_, ClNamed _) => GREATER
adamc@211 178
adamc@211 179 | (ClProj (m1, ms1, x1), ClProj (m2, ms2, x2)) =>
adamc@211 180 join (Int.compare (m1, m2),
adamc@211 181 fn () => join (joinL String.compare (ms1, ms2),
adamc@211 182 fn () => String.compare (x1, x2)))
adamc@211 183 end
adamc@211 184
adamc@677 185 structure CS = BinarySetFn(CK)
adamc@211 186 structure CM = BinaryMapFn(CK)
adamc@211 187
adamc@211 188 datatype class_key =
adamc@211 189 CkNamed of int
adamc@211 190 | CkRel of int
adamc@211 191 | CkProj of int * string list * string
adamc@467 192 | CkApp of class_key * class_key
adamc@211 193
adamc@216 194 fun ck2s ck =
adamc@216 195 case ck of
adamc@216 196 CkNamed n => "Named(" ^ Int.toString n ^ ")"
adamc@216 197 | CkRel n => "Rel(" ^ Int.toString n ^ ")"
adamc@216 198 | CkProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
adamc@467 199 | CkApp (ck1, ck2) => "App(" ^ ck2s ck1 ^ ", " ^ ck2s ck2 ^ ")"
adamc@216 200
adamc@674 201 type class_key_n = class_key * int
adamc@674 202
adamc@674 203 fun ckn2s (ck, n) = ck2s ck ^ "[" ^ Int.toString n ^ "]"
adamc@674 204
adamc@216 205 fun cp2s (cn, ck) = "(" ^ cn2s cn ^ "," ^ ck2s ck ^ ")"
adamc@216 206
adamc@211 207 structure KK = struct
adamc@674 208 type ord_key = class_key_n
adamc@211 209 open Order
adamc@674 210 fun compare' x =
adamc@211 211 case x of
adamc@211 212 (CkNamed n1, CkNamed n2) => Int.compare (n1, n2)
adamc@211 213 | (CkNamed _, _) => LESS
adamc@211 214 | (_, CkNamed _) => GREATER
adamc@211 215
adamc@211 216 | (CkRel n1, CkRel n2) => Int.compare (n1, n2)
adamc@211 217 | (CkRel _, _) => LESS
adamc@211 218 | (_, CkRel _) => GREATER
adamc@211 219
adamc@211 220 | (CkProj (m1, ms1, x1), CkProj (m2, ms2, x2)) =>
adamc@211 221 join (Int.compare (m1, m2),
adamc@211 222 fn () => join (joinL String.compare (ms1, ms2),
adamc@211 223 fn () => String.compare (x1, x2)))
adamc@467 224 | (CkProj _, _) => LESS
adamc@467 225 | (_, CkProj _) => GREATER
adamc@467 226
adamc@467 227 | (CkApp (f1, x1), CkApp (f2, x2)) =>
adamc@674 228 join (compare' (f1, f2),
adamc@674 229 fn () => compare' (x1, x2))
adamc@674 230 fun compare ((k1, n1), (k2, n2)) =
adamc@674 231 join (Int.compare (n1, n2),
adamc@674 232 fn () => compare' (k1, k2))
adamc@211 233 end
adamc@211 234
adamc@211 235 structure KM = BinaryMapFn(KK)
adamc@211 236
adamc@675 237 type class = {ground : ((class_name * class_key) list * exp) KM.map,
adamc@675 238 inclusions : exp CM.map}
adamc@675 239 val empty_class = {ground = KM.empty,
adamc@675 240 inclusions = CM.empty}
adamc@211 241
adamc@419 242 fun printClasses cs = (print "Classes:\n";
adamc@675 243 CM.appi (fn (cn, {ground = km, ...} : class) =>
adamc@419 244 (print (cn2s cn ^ ":");
adamc@674 245 KM.appi (fn (ck, _) => print (" " ^ ckn2s ck)) km;
adamc@419 246 print "\n")) cs)
adamc@216 247
adamc@2 248 type env = {
adamc@623 249 renameK : int SM.map,
adamc@623 250 relK : string list,
adamc@623 251
adamc@9 252 renameC : kind var' SM.map,
adamc@2 253 relC : (string * kind) list,
adamc@11 254 namedC : (string * kind * con option) IM.map,
adamc@9 255
adamc@157 256 datatypes : datatyp IM.map,
adamc@191 257 constructors : (datatype_kind * int * string list * con option * int) SM.map,
adamc@157 258
adamc@211 259 classes : class CM.map,
adamc@211 260
adamc@9 261 renameE : con var' SM.map,
adamc@9 262 relE : (string * con) list,
adamc@31 263 namedE : (string * con) IM.map,
adamc@31 264
adamc@31 265 renameSgn : (int * sgn) SM.map,
adamc@31 266 sgn : (string * sgn) IM.map,
adamc@31 267
adamc@31 268 renameStr : (int * sgn) SM.map,
adamc@31 269 str : (string * sgn) IM.map
adamc@2 270 }
adamc@2 271
adamc@2 272 val namedCounter = ref 0
adamc@2 273
adamc@109 274 fun newNamed () =
adamc@109 275 let
adamc@109 276 val r = !namedCounter
adamc@109 277 in
adamc@109 278 namedCounter := r + 1;
adamc@109 279 r
adamc@109 280 end
adamc@109 281
adamc@2 282 val empty = {
adamc@623 283 renameK = SM.empty,
adamc@623 284 relK = [],
adamc@623 285
adamc@2 286 renameC = SM.empty,
adamc@2 287 relC = [],
adamc@9 288 namedC = IM.empty,
adamc@9 289
adamc@157 290 datatypes = IM.empty,
adamc@171 291 constructors = SM.empty,
adamc@157 292
adamc@211 293 classes = CM.empty,
adamc@211 294
adamc@9 295 renameE = SM.empty,
adamc@9 296 relE = [],
adamc@31 297 namedE = IM.empty,
adamc@31 298
adamc@31 299 renameSgn = SM.empty,
adamc@31 300 sgn = IM.empty,
adamc@31 301
adamc@31 302 renameStr = SM.empty,
adamc@31 303 str = IM.empty
adamc@2 304 }
adamc@2 305
adamc@674 306 fun liftClassKey' ck =
adamc@211 307 case ck of
adamc@211 308 CkNamed _ => ck
adamc@211 309 | CkRel n => CkRel (n + 1)
adamc@211 310 | CkProj _ => ck
adamc@674 311 | CkApp (ck1, ck2) => CkApp (liftClassKey' ck1, liftClassKey' ck2)
adamc@674 312
adamc@674 313 fun liftClassKey (ck, n) = (liftClassKey' ck, n)
adamc@211 314
adamc@623 315 fun pushKRel (env : env) x =
adamc@623 316 let
adamc@623 317 val renameK = SM.map (fn n => n+1) (#renameK env)
adamc@623 318 in
adamc@623 319 {renameK = SM.insert (renameK, x, 0),
adamc@623 320 relK = x :: #relK env,
adamc@623 321
adamc@623 322 renameC = SM.map (fn Rel' (n, k) => Rel' (n, liftKindInKind 0 k)
adamc@623 323 | x => x) (#renameC env),
adamc@623 324 relC = map (fn (x, k) => (x, liftKindInKind 0 k)) (#relC env),
adamc@623 325 namedC = #namedC env,
adamc@623 326
adamc@623 327 datatypes = #datatypes env,
adamc@623 328 constructors = #constructors env,
adamc@623 329
adamc@623 330 classes = #classes env,
adamc@623 331
adamc@623 332 renameE = SM.map (fn Rel' (n, c) => Rel' (n, liftKindInCon 0 c)
adamc@623 333 | Named' (n, c) => Named' (n, c)) (#renameE env),
adamc@623 334 relE = map (fn (x, c) => (x, liftKindInCon 0 c)) (#relE env),
adamc@623 335 namedE = #namedE env,
adamc@623 336
adamc@623 337 renameSgn = #renameSgn env,
adamc@623 338 sgn = #sgn env,
adamc@623 339
adamc@623 340 renameStr = #renameStr env,
adamc@623 341 str = #str env
adamc@623 342 }
adamc@623 343 end
adamc@623 344
adamc@623 345 fun lookupKRel (env : env) n =
adamc@623 346 (List.nth (#relK env, n))
adamc@623 347 handle Subscript => raise UnboundRel n
adamc@623 348
adamc@623 349 fun lookupK (env : env) x = SM.find (#renameK env, x)
adamc@623 350
adamc@2 351 fun pushCRel (env : env) x k =
adamc@2 352 let
adamc@9 353 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
adamc@2 354 | x => x) (#renameC env)
adamc@2 355 in
adamc@623 356 {renameK = #renameK env,
adamc@623 357 relK = #relK env,
adamc@623 358
adamc@623 359 renameC = SM.insert (renameC, x, Rel' (0, k)),
adamc@2 360 relC = (x, k) :: #relC env,
adamc@514 361 namedC = #namedC env,
adamc@9 362
adamc@157 363 datatypes = #datatypes env,
adamc@171 364 constructors = #constructors env,
adamc@157 365
adamc@674 366 classes = CM.map (fn class =>
adamc@675 367 {ground = KM.foldli (fn (ck, e, km) =>
adamc@675 368 KM.insert (km, liftClassKey ck, e))
adamc@675 369 KM.empty (#ground class),
adamc@675 370 inclusions = #inclusions class})
adamc@211 371 (#classes env),
adamc@211 372
adamc@328 373 renameE = SM.map (fn Rel' (n, c) => Rel' (n, lift c)
adamc@517 374 | Named' (n, c) => Named' (n, c)) (#renameE env),
adamc@13 375 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@514 376 namedE = #namedE env,
adamc@31 377
adamc@31 378 renameSgn = #renameSgn env,
adamc@31 379 sgn = #sgn env,
adamc@31 380
adamc@31 381 renameStr = #renameStr env,
adamc@31 382 str = #str env
adamc@31 383 }
adamc@2 384 end
adamc@2 385
adamc@2 386 fun lookupCRel (env : env) n =
adamc@2 387 (List.nth (#relC env, n))
adamc@2 388 handle Subscript => raise UnboundRel n
adamc@2 389
adamc@11 390 fun pushCNamedAs (env : env) x n k co =
adamc@623 391 {renameK = #renameK env,
adamc@623 392 relK = #relK env,
adamc@623 393
adamc@623 394 renameC = SM.insert (#renameC env, x, Named' (n, k)),
adamc@5 395 relC = #relC env,
adamc@11 396 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@9 397
adamc@157 398 datatypes = #datatypes env,
adamc@171 399 constructors = #constructors env,
adamc@157 400
adamc@211 401 classes = #classes env,
adamc@211 402
adamc@9 403 renameE = #renameE env,
adamc@9 404 relE = #relE env,
adamc@31 405 namedE = #namedE env,
adamc@31 406
adamc@31 407 renameSgn = #renameSgn env,
adamc@31 408 sgn = #sgn env,
adamc@31 409
adamc@31 410 renameStr = #renameStr env,
adamc@31 411 str = #str env}
adamc@5 412
adamc@11 413 fun pushCNamed env x k co =
adamc@2 414 let
adamc@2 415 val n = !namedCounter
adamc@2 416 in
adamc@2 417 namedCounter := n + 1;
adamc@11 418 (pushCNamedAs env x n k co, n)
adamc@2 419 end
adamc@2 420
adamc@2 421 fun lookupCNamed (env : env) n =
adamc@2 422 case IM.find (#namedC env, n) of
adamc@2 423 NONE => raise UnboundNamed n
adamc@2 424 | SOME x => x
adamc@2 425
adamc@2 426 fun lookupC (env : env) x =
adamc@2 427 case SM.find (#renameC env, x) of
adamc@9 428 NONE => NotBound
adamc@9 429 | SOME (Rel' x) => Rel x
adamc@9 430 | SOME (Named' x) => Named x
adamc@9 431
adamc@191 432 fun pushDatatype (env : env) n xs xncs =
adamc@188 433 let
adamc@188 434 val dk = U.classifyDatatype xncs
adamc@188 435 in
adamc@623 436 {renameK = #renameK env,
adamc@623 437 relK = #relK env,
adamc@623 438
adamc@623 439 renameC = #renameC env,
adamc@188 440 relC = #relC env,
adamc@188 441 namedC = #namedC env,
adamc@157 442
adamc@188 443 datatypes = IM.insert (#datatypes env, n,
adamc@191 444 (xs, foldl (fn ((x, n, to), cons) =>
adamc@191 445 IM.insert (cons, n, (x, to))) IM.empty xncs)),
adamc@188 446 constructors = foldl (fn ((x, n', to), cmap) =>
adamc@191 447 SM.insert (cmap, x, (dk, n', xs, to, n)))
adamc@188 448 (#constructors env) xncs,
adamc@157 449
adamc@211 450 classes = #classes env,
adamc@211 451
adamc@188 452 renameE = #renameE env,
adamc@188 453 relE = #relE env,
adamc@188 454 namedE = #namedE env,
adamc@157 455
adamc@188 456 renameSgn = #renameSgn env,
adamc@188 457 sgn = #sgn env,
adamc@157 458
adamc@188 459 renameStr = #renameStr env,
adamc@188 460 str = #str env}
adamc@188 461 end
adamc@157 462
adamc@157 463 fun lookupDatatype (env : env) n =
adamc@157 464 case IM.find (#datatypes env, n) of
adamc@157 465 NONE => raise UnboundNamed n
adamc@157 466 | SOME x => x
adamc@157 467
adamc@191 468 fun lookupDatatypeConstructor (_, dt) n =
adamc@157 469 case IM.find (dt, n) of
adamc@157 470 NONE => raise UnboundNamed n
adamc@157 471 | SOME x => x
adamc@157 472
adamc@171 473 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
adamc@171 474
adamc@191 475 fun datatypeArgs (xs, _) = xs
adamc@191 476 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
adamc@157 477
adamc@211 478 fun pushClass (env : env) n =
adamc@623 479 {renameK = #renameK env,
adamc@623 480 relK = #relK env,
adamc@623 481
adamc@623 482 renameC = #renameC env,
adamc@211 483 relC = #relC env,
adamc@211 484 namedC = #namedC env,
adamc@211 485
adamc@211 486 datatypes = #datatypes env,
adamc@211 487 constructors = #constructors env,
adamc@211 488
adamc@675 489 classes = CM.insert (#classes env, ClNamed n, empty_class),
adamc@211 490
adamc@211 491 renameE = #renameE env,
adamc@211 492 relE = #relE env,
adamc@211 493 namedE = #namedE env,
adamc@211 494
adamc@211 495 renameSgn = #renameSgn env,
adamc@211 496 sgn = #sgn env,
adamc@211 497
adamc@211 498 renameStr = #renameStr env,
adamc@403 499 str = #str env}
adamc@211 500
adamc@211 501 fun class_name_in (c, _) =
adamc@211 502 case c of
adamc@211 503 CNamed n => SOME (ClNamed n)
adamc@211 504 | CModProj x => SOME (ClProj x)
adamc@228 505 | CUnif (_, _, _, ref (SOME c)) => class_name_in c
adamc@211 506 | _ => NONE
adamc@211 507
adamc@403 508 fun isClass (env : env) c =
adamc@403 509 let
adamc@403 510 fun find NONE = false
adamc@403 511 | find (SOME c) = Option.isSome (CM.find (#classes env, c))
adamc@403 512 in
adamc@403 513 find (class_name_in c)
adamc@403 514 end
adamc@403 515
adamc@211 516 fun class_key_in (c, _) =
adamc@211 517 case c of
adamc@211 518 CRel n => SOME (CkRel n)
adamc@211 519 | CNamed n => SOME (CkNamed n)
adamc@211 520 | CModProj x => SOME (CkProj x)
adamc@228 521 | CUnif (_, _, _, ref (SOME c)) => class_key_in c
adamc@467 522 | CApp (c1, c2) =>
adamc@467 523 (case (class_key_in c1, class_key_in c2) of
adamc@467 524 (SOME k1, SOME k2) => SOME (CkApp (k1, k2))
adamc@467 525 | _ => NONE)
adamc@211 526 | _ => NONE
adamc@211 527
adamc@674 528 fun class_key_out loc =
adamc@674 529 let
adamc@674 530 fun cko k =
adamc@674 531 case k of
adamc@674 532 CkRel n => (CRel n, loc)
adamc@674 533 | CkNamed n => (CNamed n, loc)
adamc@674 534 | CkProj x => (CModProj x, loc)
adamc@674 535 | CkApp (k1, k2) => (CApp (cko k1, cko k2), loc)
adamc@674 536 in
adamc@674 537 cko
adamc@674 538 end
adamc@674 539
adamc@211 540 fun class_pair_in (c, _) =
adamc@211 541 case c of
adamc@211 542 CApp (f, x) =>
adamc@211 543 (case (class_name_in f, class_key_in x) of
adamc@211 544 (SOME f, SOME x) => SOME (f, x)
adamc@211 545 | _ => NONE)
adamc@452 546 | CUnif (_, _, _, ref (SOME c)) => class_pair_in c
adamc@211 547 | _ => NONE
adamc@211 548
adamc@674 549 fun sub_class_key (n, c) =
adamc@674 550 let
adamc@674 551 fun csk k =
adamc@674 552 case k of
adamc@674 553 CkRel n' => if n' = n then
adamc@674 554 c
adamc@674 555 else
adamc@674 556 k
adamc@674 557 | CkNamed _ => k
adamc@674 558 | CkProj _ => k
adamc@674 559 | CkApp (k1, k2) => CkApp (csk k1, csk k2)
adamc@674 560 in
adamc@674 561 csk
adamc@674 562 end
adamc@674 563
adamc@211 564 fun resolveClass (env : env) c =
adamc@674 565 let
adamc@674 566 fun doPair (f, x) =
adamc@674 567 case CM.find (#classes env, f) of
adamc@674 568 NONE => NONE
adamc@674 569 | SOME class =>
adamc@674 570 let
adamc@674 571 val loc = #2 c
adamc@675 572
adamc@675 573 fun tryIncs () =
adamc@675 574 let
adamc@675 575 fun tryIncs fs =
adamc@675 576 case fs of
adamc@675 577 [] => NONE
adamc@675 578 | (f', e') :: fs =>
adamc@675 579 case doPair (f', x) of
adamc@675 580 NONE => tryIncs fs
adamc@675 581 | SOME e =>
adamc@675 582 let
adamc@675 583 val e' = (ECApp (e', class_key_out loc x), loc)
adamc@675 584 val e' = (EApp (e', e), loc)
adamc@675 585 in
adamc@675 586 SOME e'
adamc@675 587 end
adamc@675 588 in
adamc@675 589 tryIncs (CM.listItemsi (#inclusions class))
adamc@675 590 end
adamc@675 591
adamc@674 592 fun tryRules (k, args) =
adamc@674 593 let
adamc@674 594 val len = length args
adamc@675 595
adamc@675 596 fun tryNext () =
adamc@675 597 case k of
adamc@675 598 CkApp (k1, k2) => tryRules (k1, k2 :: args)
adamc@675 599 | _ => tryIncs ()
adamc@674 600 in
adamc@675 601 case KM.find (#ground class, (k, length args)) of
adamc@674 602 SOME (cs, e) =>
adamc@674 603 let
adamc@674 604 val es = map (fn (cn, ck) =>
adamc@674 605 let
adamc@674 606 val ck = ListUtil.foldli (fn (i, arg, ck) =>
adamc@674 607 sub_class_key (len - i - 1,
adamc@674 608 arg)
adamc@674 609 ck)
adamc@674 610 ck args
adamc@674 611 in
adamc@674 612 doPair (cn, ck)
adamc@674 613 end) cs
adamc@674 614 in
adamc@674 615 if List.exists (not o Option.isSome) es then
adamc@675 616 tryNext ()
adamc@674 617 else
adamc@674 618 let
adamc@674 619 val e = foldl (fn (arg, e) => (ECApp (e, class_key_out loc arg), loc))
adamc@674 620 e args
adamc@674 621 val e = foldr (fn (pf, e) => (EApp (e, pf), loc))
adamc@674 622 e (List.mapPartial (fn x => x) es)
adamc@674 623 in
adamc@674 624 SOME e
adamc@674 625 end
adamc@674 626 end
adamc@675 627 | NONE => tryNext ()
adamc@674 628 end
adamc@674 629 in
adamc@674 630 tryRules (x, [])
adamc@674 631 end
adamc@674 632 in
adamc@674 633 case class_pair_in c of
adamc@674 634 SOME p => doPair p
adamc@674 635 | _ => NONE
adamc@674 636 end
adamc@211 637
adamc@9 638 fun pushERel (env : env) x t =
adamc@9 639 let
adamc@9 640 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 641 | x => x) (#renameE env)
adamc@211 642
adamc@675 643 val classes = CM.map (fn class =>
adamc@675 644 {ground = KM.map (fn (ps, e) => (ps, liftExp e)) (#ground class),
adamc@675 645 inclusions = #inclusions class}) (#classes env)
adamc@211 646 val classes = case class_pair_in t of
adamc@211 647 NONE => classes
adamc@211 648 | SOME (f, x) =>
adamc@403 649 case CM.find (classes, f) of
adamc@403 650 NONE => classes
adamc@403 651 | SOME class =>
adamc@403 652 let
adamc@675 653 val class = {ground = KM.insert (#ground class, (x, 0), ([], (ERel 0, #2 t))),
adamc@675 654 inclusions = #inclusions class}
adamc@403 655 in
adamc@403 656 CM.insert (classes, f, class)
adamc@403 657 end
adamc@9 658 in
adamc@623 659 {renameK = #renameK env,
adamc@623 660 relK = #relK env,
adamc@623 661
adamc@623 662 renameC = #renameC env,
adamc@9 663 relC = #relC env,
adamc@9 664 namedC = #namedC env,
adamc@9 665
adamc@157 666 datatypes = #datatypes env,
adamc@171 667 constructors = #constructors env,
adamc@157 668
adamc@211 669 classes = classes,
adamc@211 670
adamc@9 671 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 672 relE = (x, t) :: #relE env,
adamc@31 673 namedE = #namedE env,
adamc@31 674
adamc@31 675 renameSgn = #renameSgn env,
adamc@31 676 sgn = #sgn env,
adamc@31 677
adamc@31 678 renameStr = #renameStr env,
adamc@31 679 str = #str env}
adamc@9 680 end
adamc@9 681
adamc@9 682 fun lookupERel (env : env) n =
adamc@9 683 (List.nth (#relE env, n))
adamc@9 684 handle Subscript => raise UnboundRel n
adamc@9 685
adamc@675 686 datatype rule =
adamc@675 687 Normal of int * (class_name * class_key) list * class_key
adamc@675 688 | Inclusion of class_name
adamc@675 689
adamc@674 690 fun rule_in c =
adamc@674 691 let
adamc@674 692 fun quantifiers (c, nvars) =
adamc@674 693 case #1 c of
adamc@674 694 TCFun (_, _, _, c) => quantifiers (c, nvars + 1)
adamc@674 695 | _ =>
adamc@674 696 let
adamc@674 697 fun clauses (c, hyps) =
adamc@674 698 case #1 c of
adamc@674 699 TFun (hyp, c) =>
adamc@674 700 (case class_pair_in hyp of
adamc@677 701 SOME (p as (_, CkRel _)) => clauses (c, p :: hyps)
adamc@677 702 | _ => NONE)
adamc@674 703 | _ =>
adamc@674 704 case class_pair_in c of
adamc@674 705 NONE => NONE
adamc@674 706 | SOME (cn, ck) =>
adamc@674 707 let
adamc@674 708 fun dearg (ck, i) =
adamc@674 709 if i >= nvars then
adamc@675 710 SOME (cn, Normal (nvars, hyps, ck))
adamc@674 711 else case ck of
adamc@674 712 CkApp (ck, CkRel i') =>
adamc@674 713 if i' = i then
adamc@674 714 dearg (ck, i + 1)
adamc@674 715 else
adamc@674 716 NONE
adamc@674 717 | _ => NONE
adamc@674 718 in
adamc@674 719 dearg (ck, 0)
adamc@674 720 end
adamc@674 721 in
adamc@674 722 clauses (c, [])
adamc@674 723 end
adamc@674 724 in
adamc@675 725 case #1 c of
adamc@675 726 TCFun (_, _, _, (TFun ((CApp (f1, (CRel 0, _)), _),
adamc@675 727 (CApp (f2, (CRel 0, _)), _)), _)) =>
adamc@675 728 (case (class_name_in f1, class_name_in f2) of
adamc@675 729 (SOME f1, SOME f2) => SOME (f2, Inclusion f1)
adamc@675 730 | _ => NONE)
adamc@675 731 | _ => quantifiers (c, 0)
adamc@674 732 end
adamc@674 733
adamc@677 734 fun inclusion (classes : class CM.map, init, inclusions, f, e : exp) =
adamc@677 735 let
adamc@677 736 fun search (f, fs) =
adamc@677 737 if f = init then
adamc@677 738 NONE
adamc@677 739 else if CS.member (fs, f) then
adamc@677 740 SOME fs
adamc@677 741 else
adamc@677 742 let
adamc@677 743 val fs = CS.add (fs, f)
adamc@677 744 in
adamc@677 745 case CM.find (classes, f) of
adamc@677 746 NONE => SOME fs
adamc@677 747 | SOME {inclusions = fs', ...} =>
adamc@677 748 CM.foldli (fn (f', _, fs) =>
adamc@677 749 case fs of
adamc@677 750 NONE => NONE
adamc@677 751 | SOME fs => search (f', fs)) (SOME fs) fs'
adamc@677 752 end
adamc@677 753 in
adamc@677 754 case search (f, CS.empty) of
adamc@677 755 SOME _ => CM.insert (inclusions, f, e)
adamc@677 756 | NONE => (ErrorMsg.errorAt (#2 e) "Type class inclusion would create a cycle";
adamc@677 757 inclusions)
adamc@677 758 end
adamc@677 759
adamc@9 760 fun pushENamedAs (env : env) x n t =
adamc@211 761 let
adamc@211 762 val classes = #classes env
adamc@674 763 val classes = case rule_in t of
adamc@211 764 NONE => classes
adamc@675 765 | SOME (f, rule) =>
adamc@403 766 case CM.find (classes, f) of
adamc@403 767 NONE => classes
adamc@403 768 | SOME class =>
adamc@403 769 let
adamc@675 770 val e = (ENamed n, #2 t)
adamc@675 771
adamc@675 772 val class =
adamc@675 773 case rule of
adamc@675 774 Normal (nvars, hyps, x) =>
adamc@675 775 {ground = KM.insert (#ground class, (x, nvars), (hyps, e)),
adamc@675 776 inclusions = #inclusions class}
adamc@675 777 | Inclusion f' =>
adamc@675 778 {ground = #ground class,
adamc@677 779 inclusions = inclusion (classes, f, #inclusions class, f', e)}
adamc@403 780 in
adamc@403 781 CM.insert (classes, f, class)
adamc@403 782 end
adamc@211 783 in
adamc@623 784 {renameK = #renameK env,
adamc@623 785 relK = #relK env,
adamc@623 786
adamc@623 787 renameC = #renameC env,
adamc@211 788 relC = #relC env,
adamc@211 789 namedC = #namedC env,
adamc@9 790
adamc@211 791 datatypes = #datatypes env,
adamc@211 792 constructors = #constructors env,
adamc@157 793
adamc@211 794 classes = classes,
adamc@31 795
adamc@211 796 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@211 797 relE = #relE env,
adamc@211 798 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@211 799
adamc@211 800 renameSgn = #renameSgn env,
adamc@211 801 sgn = #sgn env,
adamc@211 802
adamc@211 803 renameStr = #renameStr env,
adamc@211 804 str = #str env}
adamc@211 805 end
adamc@9 806
adamc@9 807 fun pushENamed env x t =
adamc@9 808 let
adamc@9 809 val n = !namedCounter
adamc@9 810 in
adamc@9 811 namedCounter := n + 1;
adamc@9 812 (pushENamedAs env x n t, n)
adamc@9 813 end
adamc@9 814
adamc@9 815 fun lookupENamed (env : env) n =
adamc@9 816 case IM.find (#namedE env, n) of
adamc@9 817 NONE => raise UnboundNamed n
adamc@9 818 | SOME x => x
adamc@9 819
adamc@471 820 fun checkENamed (env : env) n =
adamc@471 821 Option.isSome (IM.find (#namedE env, n))
adamc@471 822
adamc@9 823 fun lookupE (env : env) x =
adamc@9 824 case SM.find (#renameE env, x) of
adamc@9 825 NONE => NotBound
adamc@9 826 | SOME (Rel' x) => Rel x
adamc@9 827 | SOME (Named' x) => Named x
adamc@2 828
adamc@31 829 fun pushSgnNamedAs (env : env) x n sgis =
adamc@623 830 {renameK = #renameK env,
adamc@623 831 relK = #relK env,
adamc@623 832
adamc@623 833 renameC = #renameC env,
adamc@31 834 relC = #relC env,
adamc@31 835 namedC = #namedC env,
adamc@31 836
adamc@157 837 datatypes = #datatypes env,
adamc@171 838 constructors = #constructors env,
adamc@157 839
adamc@211 840 classes = #classes env,
adamc@211 841
adamc@31 842 renameE = #renameE env,
adamc@31 843 relE = #relE env,
adamc@31 844 namedE = #namedE env,
adamc@31 845
adamc@31 846 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
adamc@31 847 sgn = IM.insert (#sgn env, n, (x, sgis)),
adamc@31 848
adamc@31 849 renameStr = #renameStr env,
adamc@31 850 str = #str env}
adamc@31 851
adamc@31 852 fun pushSgnNamed env x sgis =
adamc@31 853 let
adamc@31 854 val n = !namedCounter
adamc@31 855 in
adamc@31 856 namedCounter := n + 1;
adamc@31 857 (pushSgnNamedAs env x n sgis, n)
adamc@31 858 end
adamc@31 859
adamc@31 860 fun lookupSgnNamed (env : env) n =
adamc@31 861 case IM.find (#sgn env, n) of
adamc@31 862 NONE => raise UnboundNamed n
adamc@31 863 | SOME x => x
adamc@31 864
adamc@31 865 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
adamc@31 866
adamc@31 867 fun lookupStrNamed (env : env) n =
adamc@31 868 case IM.find (#str env, n) of
adamc@31 869 NONE => raise UnboundNamed n
adamc@31 870 | SOME x => x
adamc@31 871
adamc@31 872 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
adamc@31 873
adamc@203 874
adamc@216 875 fun sgiSeek (sgi, (sgns, strs, cons)) =
adamc@216 876 case sgi of
adamc@216 877 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 878 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 879 | SgiDatatype (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 880 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 881 | SgiVal _ => (sgns, strs, cons)
adamc@216 882 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
adamc@216 883 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
adamc@216 884 | SgiConstraint _ => (sgns, strs, cons)
adamc@563 885 | SgiClassAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@563 886 | SgiClass (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 887
adamc@34 888 fun sgnSeek f sgis =
adamc@34 889 let
adamc@59 890 fun seek (sgis, sgns, strs, cons) =
adamc@34 891 case sgis of
adamc@34 892 [] => NONE
adamc@34 893 | (sgi, _) :: sgis =>
adamc@34 894 case f sgi of
adamc@158 895 SOME v =>
adamc@158 896 let
adamc@158 897 val cons =
adamc@158 898 case sgi of
adamc@191 899 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
adamc@191 900 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
adamc@158 901 | _ => cons
adamc@158 902 in
adamc@158 903 SOME (v, (sgns, strs, cons))
adamc@158 904 end
adamc@88 905 | NONE =>
adamc@216 906 let
adamc@216 907 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
adamc@216 908 in
adamc@216 909 seek (sgis, sgns, strs, cons)
adamc@216 910 end
adamc@34 911 in
adamc@59 912 seek (sgis, IM.empty, IM.empty, IM.empty)
adamc@34 913 end
adamc@34 914
adamc@34 915 fun id x = x
adamc@34 916
adamc@34 917 fun unravelStr (str, _) =
adamc@34 918 case str of
adamc@34 919 StrVar x => (x, [])
adamc@34 920 | StrProj (str, m) =>
adamc@34 921 let
adamc@34 922 val (x, ms) = unravelStr str
adamc@34 923 in
adamc@34 924 (x, ms @ [m])
adamc@34 925 end
adamc@34 926 | _ => raise Fail "unravelStr"
adamc@34 927
adamc@59 928 fun sgnS_con (str, (sgns, strs, cons)) c =
adamc@34 929 case c of
adamc@34 930 CModProj (m1, ms, x) =>
adamc@34 931 (case IM.find (strs, m1) of
adamc@34 932 NONE => c
adamc@34 933 | SOME m1x =>
adamc@34 934 let
adamc@34 935 val (m1, ms') = unravelStr str
adamc@34 936 in
adamc@34 937 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 938 end)
adamc@34 939 | CNamed n =>
adamc@34 940 (case IM.find (cons, n) of
adamc@34 941 NONE => c
adamc@34 942 | SOME nx =>
adamc@34 943 let
adamc@34 944 val (m1, ms) = unravelStr str
adamc@34 945 in
adamc@34 946 CModProj (m1, ms, nx)
adamc@34 947 end)
adamc@34 948 | _ => c
adamc@34 949
adamc@467 950 fun sgnS_con' (arg as (m1, ms', (sgns, strs, cons))) c =
adamc@216 951 case c of
adamc@216 952 CModProj (m1, ms, x) =>
adamc@216 953 (case IM.find (strs, m1) of
adamc@216 954 NONE => c
adamc@216 955 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
adamc@216 956 | CNamed n =>
adamc@216 957 (case IM.find (cons, n) of
adamc@216 958 NONE => c
adamc@216 959 | SOME nx => CModProj (m1, ms', nx))
adamc@467 960 | CApp (c1, c2) => CApp ((sgnS_con' arg (#1 c1), #2 c1),
adamc@467 961 (sgnS_con' arg (#1 c2), #2 c2))
adamc@216 962 | _ => c
adamc@216 963
adamc@674 964 fun sgnS_class_name (arg as (m1, ms', (sgns, strs, cons))) nm =
adamc@674 965 case nm of
adamc@674 966 ClProj (m1, ms, x) =>
adamc@674 967 (case IM.find (strs, m1) of
adamc@674 968 NONE => nm
adamc@674 969 | SOME m1x => ClProj (m1, ms' @ m1x :: ms, x))
adamc@674 970 | ClNamed n =>
adamc@674 971 (case IM.find (cons, n) of
adamc@674 972 NONE => nm
adamc@674 973 | SOME nx => ClProj (m1, ms', nx))
adamc@674 974
adamc@674 975 fun sgnS_class_key (arg as (m1, ms', (sgns, strs, cons))) k =
adamc@674 976 case k of
adamc@674 977 CkProj (m1, ms, x) =>
adamc@674 978 (case IM.find (strs, m1) of
adamc@674 979 NONE => k
adamc@674 980 | SOME m1x => CkProj (m1, ms' @ m1x :: ms, x))
adamc@674 981 | CkNamed n =>
adamc@674 982 (case IM.find (cons, n) of
adamc@674 983 NONE => k
adamc@674 984 | SOME nx => CkProj (m1, ms', nx))
adamc@674 985 | CkApp (k1, k2) => CkApp (sgnS_class_key arg k1,
adamc@674 986 sgnS_class_key arg k2)
adamc@674 987 | _ => k
adamc@674 988
adamc@59 989 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
adamc@59 990 case sgn of
adamc@59 991 SgnProj (m1, ms, x) =>
adamc@59 992 (case IM.find (strs, m1) of
adamc@59 993 NONE => sgn
adamc@59 994 | SOME m1x =>
adamc@59 995 let
adamc@59 996 val (m1, ms') = unravelStr str
adamc@59 997 in
adamc@59 998 SgnProj (m1, ms' @ m1x :: ms, x)
adamc@59 999 end)
adamc@59 1000 | SgnVar n =>
adamc@59 1001 (case IM.find (sgns, n) of
adamc@59 1002 NONE => sgn
adamc@59 1003 | SOME nx =>
adamc@59 1004 let
adamc@59 1005 val (m1, ms) = unravelStr str
adamc@59 1006 in
adamc@59 1007 SgnProj (m1, ms, nx)
adamc@59 1008 end)
adamc@59 1009 | _ => sgn
adamc@59 1010
adamc@59 1011 fun sgnSubSgn x =
adamc@34 1012 ElabUtil.Sgn.map {kind = id,
adamc@59 1013 con = sgnS_con x,
adamc@34 1014 sgn_item = id,
adamc@59 1015 sgn = sgnS_sgn x}
adamc@34 1016
adamc@211 1017
adamc@211 1018
adamc@211 1019 and projectSgn env {sgn, str, field} =
adamc@211 1020 case #1 (hnormSgn env sgn) of
adamc@211 1021 SgnConst sgis =>
adamc@211 1022 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@211 1023 NONE => NONE
adamc@211 1024 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@211 1025 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@211 1026 | _ => NONE
adamc@211 1027
adamc@211 1028 and hnormSgn env (all as (sgn, loc)) =
adamc@34 1029 case sgn of
adamc@42 1030 SgnError => all
adamc@42 1031 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 1032 | SgnConst _ => all
adamc@42 1033 | SgnFun _ => all
adamc@59 1034 | SgnProj (m, ms, x) =>
adamc@59 1035 let
adamc@59 1036 val (_, sgn) = lookupStrNamed env m
adamc@59 1037 in
adamc@59 1038 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
adamc@59 1039 sgn = sgn,
adamc@59 1040 field = x} of
adamc@59 1041 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
adamc@59 1042 | SOME sgn => sgn
adamc@59 1043 end
adamc@42 1044 | SgnWhere (sgn, x, c) =>
adamc@42 1045 case #1 (hnormSgn env sgn) of
adamc@42 1046 SgnError => (SgnError, loc)
adamc@42 1047 | SgnConst sgis =>
adamc@42 1048 let
adamc@42 1049 fun traverse (pre, post) =
adamc@42 1050 case post of
adamc@42 1051 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 1052 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adamc@42 1053 if x = x' then
adamc@42 1054 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adamc@42 1055 else
adamc@42 1056 traverse (sgi :: pre, rest)
adamc@42 1057 | sgi :: rest => traverse (sgi :: pre, rest)
adamc@42 1058
adamc@42 1059 val sgis = traverse ([], sgis)
adamc@42 1060 in
adamc@42 1061 (SgnConst sgis, loc)
adamc@42 1062 end
adamc@42 1063 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adamc@42 1064
adamc@211 1065 fun enrichClasses env classes (m1, ms) sgn =
adamc@59 1066 case #1 (hnormSgn env sgn) of
adamc@59 1067 SgnConst sgis =>
adamc@211 1068 let
adamc@218 1069 val (classes, _, _, _) =
adamc@218 1070 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
adamc@211 1071 let
adamc@211 1072 fun found (x, n) =
adamc@211 1073 (CM.insert (classes,
adamc@211 1074 ClProj (m1, ms, x),
adamc@211 1075 empty_class),
adamc@216 1076 IM.insert (newClasses, n, x),
adamc@218 1077 sgiSeek (#1 sgi, fmap),
adamc@218 1078 env)
adamc@216 1079
adamc@218 1080 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
adamc@211 1081 in
adamc@211 1082 case #1 sgi of
adamc@217 1083 SgiStr (x, _, sgn) =>
adamc@217 1084 (enrichClasses env classes (m1, ms @ [x]) sgn,
adamc@217 1085 newClasses,
adamc@218 1086 sgiSeek (#1 sgi, fmap),
adamc@218 1087 env)
adamc@218 1088 | SgiSgn (x, n, sgn) =>
adamc@218 1089 (classes,
adamc@218 1090 newClasses,
adamc@218 1091 fmap,
adamc@218 1092 pushSgnNamedAs env x n sgn)
adamc@218 1093
adamc@563 1094 | SgiClassAbs (x, n, _) => found (x, n)
adamc@563 1095 | SgiClass (x, n, _, _) => found (x, n)
adamc@674 1096 | SgiVal (x, n, c) =>
adamc@674 1097 (case rule_in c of
adamc@674 1098 NONE => default ()
adamc@675 1099 | SOME (cn, rule) =>
adamc@674 1100 let
adamc@675 1101 val globalizeN = sgnS_class_name (m1, ms, fmap)
adamc@674 1102 val globalize = sgnS_class_key (m1, ms, fmap)
adamc@216 1103
adamc@674 1104 fun unravel c =
adamc@674 1105 case c of
adamc@674 1106 ClNamed n =>
adamc@674 1107 ((case lookupCNamed env n of
adamc@674 1108 (_, _, SOME c') =>
adamc@674 1109 (case class_name_in c' of
adamc@674 1110 NONE => c
adamc@674 1111 | SOME k => unravel k)
adamc@674 1112 | _ => c)
adamc@674 1113 handle UnboundNamed _ => c)
adamc@674 1114 | _ => c
adamc@674 1115
adamc@674 1116 val nc =
adamc@674 1117 case cn of
adamc@674 1118 ClNamed f => IM.find (newClasses, f)
adamc@674 1119 | _ => NONE
adamc@674 1120 in
adamc@674 1121 case nc of
adamc@674 1122 NONE =>
adamc@419 1123 let
adamc@419 1124 val classes =
adamc@419 1125 case CM.find (classes, cn) of
adamc@419 1126 NONE => classes
adamc@419 1127 | SOME class =>
adamc@419 1128 let
adamc@675 1129 val e = (EModProj (m1, ms, x),
adamc@675 1130 #2 sgn)
adamc@675 1131
adamc@675 1132 val class =
adamc@675 1133 case rule of
adamc@675 1134 Normal (nvars, hyps, a) =>
adamc@675 1135 {ground =
adamc@675 1136 KM.insert (#ground class, (globalize a, nvars),
adamc@675 1137 (map (fn (n, k) =>
adamc@675 1138 (globalizeN n,
adamc@675 1139 globalize k)) hyps, e)),
adamc@675 1140 inclusions = #inclusions class}
adamc@675 1141 | Inclusion f' =>
adamc@675 1142 {ground = #ground class,
adamc@677 1143 inclusions = inclusion (classes, cn,
adamc@677 1144 #inclusions class,
adamc@675 1145 globalizeN f', e)}
adamc@419 1146 in
adamc@419 1147 CM.insert (classes, cn, class)
adamc@419 1148 end
adamc@419 1149 in
adamc@419 1150 (classes,
adamc@419 1151 newClasses,
adamc@419 1152 fmap,
adamc@419 1153 env)
adamc@419 1154 end
adamc@674 1155 | SOME fx =>
adamc@674 1156 let
adamc@674 1157 val cn = ClProj (m1, ms, fx)
adamc@419 1158
adamc@674 1159 val classes =
adamc@674 1160 case CM.find (classes, cn) of
adamc@674 1161 NONE => classes
adamc@674 1162 | SOME class =>
adamc@674 1163 let
adamc@675 1164 val e = (EModProj (m1, ms, x), #2 sgn)
adamc@675 1165
adamc@675 1166 val class =
adamc@675 1167 case rule of
adamc@675 1168 Normal (nvars, hyps, a) =>
adamc@675 1169 {ground =
adamc@675 1170 KM.insert (#ground class, (globalize a, nvars),
adamc@675 1171 (map (fn (n, k) =>
adamc@675 1172 (globalizeN n,
adamc@675 1173 globalize k)) hyps, e)),
adamc@675 1174 inclusions = #inclusions class}
adamc@675 1175 | Inclusion f' =>
adamc@675 1176 {ground = #ground class,
adamc@677 1177 inclusions = inclusion (classes, cn,
adamc@677 1178 #inclusions class,
adamc@675 1179 globalizeN f', e)}
adamc@674 1180 in
adamc@674 1181 CM.insert (classes, cn, class)
adamc@674 1182 end
adamc@674 1183 in
adamc@674 1184 (classes,
adamc@674 1185 newClasses,
adamc@674 1186 fmap,
adamc@674 1187 env)
adamc@674 1188 end
adamc@674 1189 end)
adamc@216 1190 | _ => default ()
adamc@211 1191 end)
adamc@218 1192 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
adamc@211 1193 in
adamc@211 1194 classes
adamc@211 1195 end
adamc@211 1196 | _ => classes
adamc@211 1197
adamc@211 1198 fun pushStrNamedAs (env : env) x n sgn =
adamc@623 1199 {renameK = #renameK env,
adamc@623 1200 relK = #relK env,
adamc@623 1201
adamc@623 1202 renameC = #renameC env,
adamc@211 1203 relC = #relC env,
adamc@211 1204 namedC = #namedC env,
adamc@211 1205
adamc@211 1206 datatypes = #datatypes env,
adamc@211 1207 constructors = #constructors env,
adamc@211 1208
adamc@211 1209 classes = enrichClasses env (#classes env) (n, []) sgn,
adamc@211 1210
adamc@211 1211 renameE = #renameE env,
adamc@211 1212 relE = #relE env,
adamc@211 1213 namedE = #namedE env,
adamc@211 1214
adamc@211 1215 renameSgn = #renameSgn env,
adamc@211 1216 sgn = #sgn env,
adamc@211 1217
adamc@211 1218 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
adamc@211 1219 str = IM.insert (#str env, n, (x, sgn))}
adamc@211 1220
adamc@211 1221 fun pushStrNamed env x sgn =
adamc@211 1222 let
adamc@211 1223 val n = !namedCounter
adamc@211 1224 in
adamc@211 1225 namedCounter := n + 1;
adamc@211 1226 (pushStrNamedAs env x n sgn, n)
adamc@211 1227 end
adamc@211 1228
adamc@211 1229 fun sgiBinds env (sgi, loc) =
adamc@211 1230 case sgi of
adamc@211 1231 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@211 1232 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@211 1233 | SgiDatatype (x, n, xs, xncs) =>
adamc@211 1234 let
adamc@341 1235 val k = (KType, loc)
adamc@341 1236 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 1237
adamc@341 1238 val env = pushCNamedAs env x n k' NONE
adamc@211 1239 in
adamc@211 1240 foldl (fn ((x', n', to), env) =>
adamc@211 1241 let
adamc@211 1242 val t =
adamc@211 1243 case to of
adamc@211 1244 NONE => (CNamed n, loc)
adamc@211 1245 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 1246
adamc@211 1247 val k = (KType, loc)
adamc@211 1248 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 1249 in
adamc@211 1250 pushENamedAs env x' n' t
adamc@211 1251 end)
adamc@211 1252 env xncs
adamc@211 1253 end
adamc@211 1254 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@211 1255 let
adamc@341 1256 val k = (KType, loc)
adamc@341 1257 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 1258
adamc@341 1259 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
adamc@211 1260 in
adamc@211 1261 foldl (fn ((x', n', to), env) =>
adamc@211 1262 let
adamc@211 1263 val t =
adamc@211 1264 case to of
adamc@211 1265 NONE => (CNamed n, loc)
adamc@211 1266 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 1267
adamc@211 1268 val k = (KType, loc)
adamc@211 1269 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 1270 in
adamc@211 1271 pushENamedAs env x' n' t
adamc@211 1272 end)
adamc@211 1273 env xncs
adamc@211 1274 end
adamc@211 1275 | SgiVal (x, n, t) => pushENamedAs env x n t
adamc@211 1276 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@211 1277 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@211 1278 | SgiConstraint _ => env
adamc@211 1279
adamc@563 1280 | SgiClassAbs (x, n, k) => pushCNamedAs env x n (KArrow (k, (KType, loc)), loc) NONE
adamc@563 1281 | SgiClass (x, n, k, c) => pushCNamedAs env x n (KArrow (k, (KType, loc)), loc) (SOME c)
adamc@459 1282
adamc@211 1283 fun sgnSubCon x =
adamc@211 1284 ElabUtil.Con.map {kind = id,
adamc@211 1285 con = sgnS_con x}
adamc@59 1286
adamc@59 1287 fun projectStr env {sgn, str, field} =
adamc@59 1288 case #1 (hnormSgn env sgn) of
adamc@59 1289 SgnConst sgis =>
adamc@59 1290 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 1291 NONE => NONE
adamc@59 1292 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 1293 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 1294 | _ => NONE
adamc@59 1295
adamc@158 1296 fun chaseMpath env (n, ms) =
adamc@158 1297 let
adamc@158 1298 val (_, sgn) = lookupStrNamed env n
adamc@158 1299 in
adamc@158 1300 foldl (fn (m, (str, sgn)) =>
adamc@158 1301 case projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1302 NONE => raise Fail "kindof: Unknown substructure"
adamc@158 1303 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
adamc@158 1304 ((StrVar n, #2 sgn), sgn) ms
adamc@158 1305 end
adamc@158 1306
adamc@42 1307 fun projectCon env {sgn, str, field} =
adamc@42 1308 case #1 (hnormSgn env sgn) of
adamc@34 1309 SgnConst sgis =>
adamc@34 1310 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 1311 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@341 1312 | SgiDatatype (x, _, xs, _) =>
adamc@158 1313 if x = field then
adamc@341 1314 let
adamc@341 1315 val k = (KType, #2 sgn)
adamc@341 1316 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 1317 in
adamc@341 1318 SOME (k', NONE)
adamc@341 1319 end
adamc@341 1320 else
adamc@341 1321 NONE
adamc@341 1322 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
adamc@341 1323 if x = field then
adamc@341 1324 let
adamc@341 1325 val k = (KType, #2 sgn)
adamc@341 1326 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 1327 in
adamc@341 1328 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
adamc@341 1329 end
adamc@158 1330 else
adamc@158 1331 NONE
adamc@563 1332 | SgiClassAbs (x, _, k) => if x = field then
adamc@563 1333 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), NONE)
adamc@563 1334 else
adamc@563 1335 NONE
adamc@563 1336 | SgiClass (x, _, k, c) => if x = field then
adamc@563 1337 SOME ((KArrow (k, (KType, #2 sgn)), #2 sgn), SOME c)
adamc@563 1338 else
adamc@563 1339 NONE
adamc@34 1340 | _ => NONE) sgis of
adamc@34 1341 NONE => NONE
adamc@34 1342 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 1343 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 1344 | _ => NONE
adamc@34 1345
adamc@157 1346 fun projectDatatype env {sgn, str, field} =
adamc@157 1347 case #1 (hnormSgn env sgn) of
adamc@157 1348 SgnConst sgis =>
adamc@191 1349 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@191 1350 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@157 1351 | _ => NONE) sgis of
adamc@157 1352 NONE => NONE
adamc@191 1353 | SOME ((xs, xncs), subs) => SOME (xs,
adamc@191 1354 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
adamc@157 1355 | _ => NONE
adamc@157 1356
adamc@174 1357 fun projectConstructor env {sgn, str, field} =
adamc@174 1358 case #1 (hnormSgn env sgn) of
adamc@174 1359 SgnConst sgis =>
adamc@174 1360 let
adamc@191 1361 fun consider (n, xs, xncs) =
adamc@174 1362 ListUtil.search (fn (x, n', to) =>
adamc@174 1363 if x <> field then
adamc@174 1364 NONE
adamc@174 1365 else
adamc@191 1366 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
adamc@174 1367 in
adamc@191 1368 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
adamc@191 1369 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
adamc@174 1370 | _ => NONE) sgis of
adamc@174 1371 NONE => NONE
adamc@191 1372 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
adamc@191 1373 sgnSubCon (str, subs) t)
adamc@174 1374 end
adamc@174 1375 | _ => NONE
adamc@174 1376
adamc@42 1377 fun projectVal env {sgn, str, field} =
adamc@42 1378 case #1 (hnormSgn env sgn) of
adamc@34 1379 SgnConst sgis =>
adamc@162 1380 let
adamc@191 1381 fun seek (n, xs, xncs) =
adamc@162 1382 ListUtil.search (fn (x, _, to) =>
adamc@162 1383 if x = field then
adamc@191 1384 SOME (let
adamc@467 1385 val base = (CNamed n, #2 sgn)
adamc@467 1386 val nxs = length xs
adamc@467 1387 val base = ListUtil.foldli (fn (i, _, base) =>
adamc@467 1388 (CApp (base,
adamc@467 1389 (CRel (nxs - i - 1), #2 sgn)),
adamc@467 1390 #2 sgn))
adamc@467 1391 base xs
adamc@467 1392
adamc@191 1393 val t =
adamc@191 1394 case to of
adamc@467 1395 NONE => base
adamc@467 1396 | SOME t => (TFun (t, base), #2 sgn)
adamc@191 1397 val k = (KType, #2 sgn)
adamc@191 1398 in
adamc@467 1399 foldr (fn (x, t) => (TCFun (Implicit, x, k, t), #2 sgn))
adamc@191 1400 t xs
adamc@191 1401 end)
adamc@162 1402 else
adamc@162 1403 NONE) xncs
adamc@162 1404 in
adamc@162 1405 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
adamc@191 1406 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
adamc@191 1407 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
adamc@162 1408 | _ => NONE) sgis of
adamc@162 1409 NONE => NONE
adamc@162 1410 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
adamc@162 1411 end
adamc@34 1412 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 1413 | _ => NONE
adamc@34 1414
adamc@88 1415 fun sgnSeekConstraints (str, sgis) =
adamc@88 1416 let
adamc@88 1417 fun seek (sgis, sgns, strs, cons, acc) =
adamc@88 1418 case sgis of
adamc@88 1419 [] => acc
adamc@88 1420 | (sgi, _) :: sgis =>
adamc@88 1421 case sgi of
adamc@88 1422 SgiConstraint (c1, c2) =>
adamc@88 1423 let
adamc@88 1424 val sub = sgnSubCon (str, (sgns, strs, cons))
adamc@88 1425 in
adamc@88 1426 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
adamc@88 1427 end
adamc@88 1428 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1429 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 1430 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 1431 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1432 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
adamc@88 1433 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
adamc@88 1434 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
adamc@563 1435 | SgiClassAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@563 1436 | SgiClass (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1437 in
adamc@88 1438 seek (sgis, IM.empty, IM.empty, IM.empty, [])
adamc@88 1439 end
adamc@88 1440
adamc@88 1441 fun projectConstraints env {sgn, str} =
adamc@88 1442 case #1 (hnormSgn env sgn) of
adamc@88 1443 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
adamc@88 1444 | SgnError => SOME []
adamc@88 1445 | _ => NONE
adamc@34 1446
adamc@447 1447 fun edeclBinds env (d, loc) =
adamc@447 1448 case d of
adamc@447 1449 EDVal (x, t, _) => pushERel env x t
adamc@447 1450 | EDValRec vis => foldl (fn ((x, t, _), env) => pushERel env x t) env vis
adamc@447 1451
adamc@157 1452 fun declBinds env (d, loc) =
adamc@157 1453 case d of
adamc@157 1454 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@191 1455 | DDatatype (x, n, xs, xncs) =>
adamc@157 1456 let
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@297 1464 val env = pushCNamedAs env x n kb NONE
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@191 1479 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
adamc@157 1480 let
adamc@157 1481 val t = (CModProj (m, ms, x'), loc)
adamc@297 1482 val k = (KType, loc)
adamc@297 1483 val nxs = length xs
adamc@297 1484 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@297 1485 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@297 1486 (KArrow (k, kb), loc)))
adamc@297 1487 ((CNamed n, loc), k) xs
adamc@297 1488
adamc@341 1489 val env = pushCNamedAs env x n kb (SOME t)
adamc@191 1490 val env = pushDatatype env n xs xncs
adamc@157 1491 in
adamc@191 1492 foldl (fn ((x', n', to), env) =>
adamc@191 1493 let
adamc@191 1494 val t =
adamc@191 1495 case to of
adamc@297 1496 NONE => tb
adamc@297 1497 | SOME t => (TFun (t, tb), loc)
adamc@297 1498 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@191 1499 in
adamc@191 1500 pushENamedAs env x' n' t
adamc@191 1501 end)
adamc@191 1502 env xncs
adamc@157 1503 end
adamc@157 1504 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@157 1505 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
adamc@157 1506 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@157 1507 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@157 1508 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@157 1509 | DConstraint _ => env
adamc@157 1510 | DExport _ => env
adamc@205 1511 | DTable (tn, x, n, c) =>
adamc@205 1512 let
adamc@338 1513 val t = (CApp ((CModProj (tn, [], "sql_table"), loc), c), loc)
adamc@338 1514 in
adamc@338 1515 pushENamedAs env x n t
adamc@338 1516 end
adamc@338 1517 | DSequence (tn, x, n) =>
adamc@338 1518 let
adamc@338 1519 val t = (CModProj (tn, [], "sql_sequence"), loc)
adamc@205 1520 in
adamc@205 1521 pushENamedAs env x n t
adamc@205 1522 end
adamc@563 1523 | DClass (x, n, k, c) =>
adamc@213 1524 let
adamc@563 1525 val k = (KArrow (k, (KType, loc)), loc)
adamc@213 1526 val env = pushCNamedAs env x n k (SOME c)
adamc@213 1527 in
adamc@213 1528 pushClass env n
adamc@213 1529 end
adamc@271 1530 | DDatabase _ => env
adamc@459 1531 | DCookie (tn, x, n, c) =>
adamc@459 1532 let
adamc@459 1533 val t = (CApp ((CModProj (tn, [], "cookie"), loc), c), loc)
adamc@459 1534 in
adamc@459 1535 pushENamedAs env x n t
adamc@459 1536 end
adamc@157 1537
adamc@243 1538 fun patBinds env (p, loc) =
adamc@243 1539 case p of
adamc@243 1540 PWild => env
adamc@243 1541 | PVar (x, t) => pushERel env x t
adamc@243 1542 | PPrim _ => env
adamc@243 1543 | PCon (_, _, _, NONE) => env
adamc@243 1544 | PCon (_, _, _, SOME p) => patBinds env p
adamc@243 1545 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
adamc@243 1546
adamc@2 1547 end