annotate src/elab_env.sml @ 672:df6eb58de040

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