annotate src/elab_env.sml @ 439:322c8620bbdf

Marshaling time to SQL
author Adam Chlipala <adamc@hcoop.net>
date Thu, 30 Oct 2008 15:11:37 -0400
parents cb5897276abf
children b77863cd0be2
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@13 48 val liftConInCon =
adamc@13 49 U.Con.mapB {kind = fn k => k,
adamc@13 50 con = fn bound => fn c =>
adamc@13 51 case c of
adamc@16 52 CRel xn =>
adamc@13 53 if xn < bound then
adamc@13 54 c
adamc@13 55 else
adamc@16 56 CRel (xn + 1)
adamc@17 57 (*| CUnif _ => raise SynUnif*)
adamc@13 58 | _ => c,
adamc@13 59 bind = fn (bound, U.Con.Rel _) => bound + 1
adamc@13 60 | (bound, _) => bound}
adamc@13 61
adamc@13 62 val lift = liftConInCon 0
adamc@13 63
adamc@211 64 val liftExpInExp =
adamc@211 65 U.Exp.mapB {kind = fn k => k,
adamc@211 66 con = fn _ => fn c => c,
adamc@211 67 exp = fn bound => fn e =>
adamc@211 68 case e of
adamc@211 69 ERel xn =>
adamc@211 70 if xn < bound then
adamc@211 71 e
adamc@211 72 else
adamc@211 73 ERel (xn + 1)
adamc@211 74 | _ => e,
adamc@211 75 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@211 76 | (bound, _) => bound}
adamc@211 77
adamc@211 78
adamc@211 79 val liftExp = liftExpInExp 0
adamc@13 80
adamc@13 81 (* Back to environments *)
adamc@13 82
adamc@9 83 datatype 'a var' =
adamc@9 84 Rel' of int * 'a
adamc@9 85 | Named' of int * 'a
adamc@2 86
adamc@9 87 datatype 'a var =
adamc@9 88 NotBound
adamc@9 89 | Rel of int * 'a
adamc@9 90 | Named of int * 'a
adamc@2 91
adamc@191 92 type datatyp = string list * (string * con option) IM.map
adamc@157 93
adamc@211 94 datatype class_name =
adamc@211 95 ClNamed of int
adamc@211 96 | ClProj of int * string list * string
adamc@211 97
adamc@216 98 fun cn2s cn =
adamc@216 99 case cn of
adamc@216 100 ClNamed n => "Named(" ^ Int.toString n ^ ")"
adamc@216 101 | ClProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
adamc@216 102
adamc@211 103 structure CK = struct
adamc@211 104 type ord_key = class_name
adamc@211 105 open Order
adamc@211 106 fun compare x =
adamc@211 107 case x of
adamc@211 108 (ClNamed n1, ClNamed n2) => Int.compare (n1, n2)
adamc@211 109 | (ClNamed _, _) => LESS
adamc@211 110 | (_, ClNamed _) => GREATER
adamc@211 111
adamc@211 112 | (ClProj (m1, ms1, x1), ClProj (m2, ms2, x2)) =>
adamc@211 113 join (Int.compare (m1, m2),
adamc@211 114 fn () => join (joinL String.compare (ms1, ms2),
adamc@211 115 fn () => String.compare (x1, x2)))
adamc@211 116 end
adamc@211 117
adamc@211 118 structure CM = BinaryMapFn(CK)
adamc@211 119
adamc@211 120 datatype class_key =
adamc@211 121 CkNamed of int
adamc@211 122 | CkRel of int
adamc@211 123 | CkProj of int * string list * string
adamc@211 124
adamc@216 125 fun ck2s ck =
adamc@216 126 case ck of
adamc@216 127 CkNamed n => "Named(" ^ Int.toString n ^ ")"
adamc@216 128 | CkRel n => "Rel(" ^ Int.toString n ^ ")"
adamc@216 129 | CkProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
adamc@216 130
adamc@216 131 fun cp2s (cn, ck) = "(" ^ cn2s cn ^ "," ^ ck2s ck ^ ")"
adamc@216 132
adamc@211 133 structure KK = struct
adamc@211 134 type ord_key = class_key
adamc@211 135 open Order
adamc@211 136 fun compare x =
adamc@211 137 case x of
adamc@211 138 (CkNamed n1, CkNamed n2) => Int.compare (n1, n2)
adamc@211 139 | (CkNamed _, _) => LESS
adamc@211 140 | (_, CkNamed _) => GREATER
adamc@211 141
adamc@211 142 | (CkRel n1, CkRel n2) => Int.compare (n1, n2)
adamc@211 143 | (CkRel _, _) => LESS
adamc@211 144 | (_, CkRel _) => GREATER
adamc@211 145
adamc@211 146 | (CkProj (m1, ms1, x1), CkProj (m2, ms2, x2)) =>
adamc@211 147 join (Int.compare (m1, m2),
adamc@211 148 fn () => join (joinL String.compare (ms1, ms2),
adamc@211 149 fn () => String.compare (x1, x2)))
adamc@211 150 end
adamc@211 151
adamc@211 152 structure KM = BinaryMapFn(KK)
adamc@211 153
adamc@211 154 type class = {
adamc@211 155 ground : exp KM.map
adamc@211 156 }
adamc@211 157
adamc@211 158 val empty_class = {
adamc@211 159 ground = KM.empty
adamc@211 160 }
adamc@211 161
adamc@419 162 fun printClasses cs = (print "Classes:\n";
adamc@419 163 CM.appi (fn (cn, {ground = km}) =>
adamc@419 164 (print (cn2s cn ^ ":");
adamc@419 165 KM.appi (fn (ck, _) => print (" " ^ ck2s ck)) km;
adamc@419 166 print "\n")) cs)
adamc@216 167
adamc@2 168 type env = {
adamc@9 169 renameC : kind var' SM.map,
adamc@2 170 relC : (string * kind) list,
adamc@11 171 namedC : (string * kind * con option) IM.map,
adamc@9 172
adamc@157 173 datatypes : datatyp IM.map,
adamc@191 174 constructors : (datatype_kind * int * string list * con option * int) SM.map,
adamc@157 175
adamc@211 176 classes : class CM.map,
adamc@211 177
adamc@9 178 renameE : con var' SM.map,
adamc@9 179 relE : (string * con) list,
adamc@31 180 namedE : (string * con) IM.map,
adamc@31 181
adamc@31 182 renameSgn : (int * sgn) SM.map,
adamc@31 183 sgn : (string * sgn) IM.map,
adamc@31 184
adamc@31 185 renameStr : (int * sgn) SM.map,
adamc@31 186 str : (string * sgn) IM.map
adamc@2 187 }
adamc@2 188
adamc@2 189 val namedCounter = ref 0
adamc@2 190
adamc@109 191 fun newNamed () =
adamc@109 192 let
adamc@109 193 val r = !namedCounter
adamc@109 194 in
adamc@109 195 namedCounter := r + 1;
adamc@109 196 r
adamc@109 197 end
adamc@109 198
adamc@2 199 val empty = {
adamc@2 200 renameC = SM.empty,
adamc@2 201 relC = [],
adamc@9 202 namedC = IM.empty,
adamc@9 203
adamc@157 204 datatypes = IM.empty,
adamc@171 205 constructors = SM.empty,
adamc@157 206
adamc@211 207 classes = CM.empty,
adamc@211 208
adamc@9 209 renameE = SM.empty,
adamc@9 210 relE = [],
adamc@31 211 namedE = IM.empty,
adamc@31 212
adamc@31 213 renameSgn = SM.empty,
adamc@31 214 sgn = IM.empty,
adamc@31 215
adamc@31 216 renameStr = SM.empty,
adamc@31 217 str = IM.empty
adamc@2 218 }
adamc@2 219
adamc@211 220 fun liftClassKey ck =
adamc@211 221 case ck of
adamc@211 222 CkNamed _ => ck
adamc@211 223 | CkRel n => CkRel (n + 1)
adamc@211 224 | CkProj _ => ck
adamc@211 225
adamc@2 226 fun pushCRel (env : env) x k =
adamc@2 227 let
adamc@9 228 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
adamc@2 229 | x => x) (#renameC env)
adamc@2 230 in
adamc@9 231 {renameC = SM.insert (renameC, x, Rel' (0, k)),
adamc@2 232 relC = (x, k) :: #relC env,
adamc@13 233 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env),
adamc@9 234
adamc@157 235 datatypes = #datatypes env,
adamc@171 236 constructors = #constructors env,
adamc@157 237
adamc@211 238 classes = CM.map (fn class => {
adamc@211 239 ground = KM.foldli (fn (ck, e, km) =>
adamc@211 240 KM.insert (km, liftClassKey ck, e))
adamc@211 241 KM.empty (#ground class)
adamc@211 242 })
adamc@211 243 (#classes env),
adamc@211 244
adamc@328 245 renameE = SM.map (fn Rel' (n, c) => Rel' (n, lift c)
adamc@328 246 | Named' (n, c) => Named' (n, lift c)) (#renameE env),
adamc@13 247 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@31 248 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env),
adamc@31 249
adamc@31 250 renameSgn = #renameSgn env,
adamc@31 251 sgn = #sgn env,
adamc@31 252
adamc@31 253 renameStr = #renameStr env,
adamc@31 254 str = #str env
adamc@31 255 }
adamc@2 256 end
adamc@2 257
adamc@2 258 fun lookupCRel (env : env) n =
adamc@2 259 (List.nth (#relC env, n))
adamc@2 260 handle Subscript => raise UnboundRel n
adamc@2 261
adamc@11 262 fun pushCNamedAs (env : env) x n k co =
adamc@9 263 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
adamc@5 264 relC = #relC env,
adamc@11 265 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@9 266
adamc@157 267 datatypes = #datatypes env,
adamc@171 268 constructors = #constructors env,
adamc@157 269
adamc@211 270 classes = #classes env,
adamc@211 271
adamc@9 272 renameE = #renameE env,
adamc@9 273 relE = #relE env,
adamc@31 274 namedE = #namedE env,
adamc@31 275
adamc@31 276 renameSgn = #renameSgn env,
adamc@31 277 sgn = #sgn env,
adamc@31 278
adamc@31 279 renameStr = #renameStr env,
adamc@31 280 str = #str env}
adamc@5 281
adamc@11 282 fun pushCNamed env x k co =
adamc@2 283 let
adamc@2 284 val n = !namedCounter
adamc@2 285 in
adamc@2 286 namedCounter := n + 1;
adamc@11 287 (pushCNamedAs env x n k co, n)
adamc@2 288 end
adamc@2 289
adamc@2 290 fun lookupCNamed (env : env) n =
adamc@2 291 case IM.find (#namedC env, n) of
adamc@2 292 NONE => raise UnboundNamed n
adamc@2 293 | SOME x => x
adamc@2 294
adamc@2 295 fun lookupC (env : env) x =
adamc@2 296 case SM.find (#renameC env, x) of
adamc@9 297 NONE => NotBound
adamc@9 298 | SOME (Rel' x) => Rel x
adamc@9 299 | SOME (Named' x) => Named x
adamc@9 300
adamc@191 301 fun pushDatatype (env : env) n xs xncs =
adamc@188 302 let
adamc@188 303 val dk = U.classifyDatatype xncs
adamc@188 304 in
adamc@188 305 {renameC = #renameC env,
adamc@188 306 relC = #relC env,
adamc@188 307 namedC = #namedC env,
adamc@157 308
adamc@188 309 datatypes = IM.insert (#datatypes env, n,
adamc@191 310 (xs, foldl (fn ((x, n, to), cons) =>
adamc@191 311 IM.insert (cons, n, (x, to))) IM.empty xncs)),
adamc@188 312 constructors = foldl (fn ((x, n', to), cmap) =>
adamc@191 313 SM.insert (cmap, x, (dk, n', xs, to, n)))
adamc@188 314 (#constructors env) xncs,
adamc@157 315
adamc@211 316 classes = #classes env,
adamc@211 317
adamc@188 318 renameE = #renameE env,
adamc@188 319 relE = #relE env,
adamc@188 320 namedE = #namedE env,
adamc@157 321
adamc@188 322 renameSgn = #renameSgn env,
adamc@188 323 sgn = #sgn env,
adamc@157 324
adamc@188 325 renameStr = #renameStr env,
adamc@188 326 str = #str env}
adamc@188 327 end
adamc@157 328
adamc@157 329 fun lookupDatatype (env : env) n =
adamc@157 330 case IM.find (#datatypes env, n) of
adamc@157 331 NONE => raise UnboundNamed n
adamc@157 332 | SOME x => x
adamc@157 333
adamc@191 334 fun lookupDatatypeConstructor (_, dt) n =
adamc@157 335 case IM.find (dt, n) of
adamc@157 336 NONE => raise UnboundNamed n
adamc@157 337 | SOME x => x
adamc@157 338
adamc@171 339 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
adamc@171 340
adamc@191 341 fun datatypeArgs (xs, _) = xs
adamc@191 342 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
adamc@157 343
adamc@211 344 fun pushClass (env : env) n =
adamc@211 345 {renameC = #renameC env,
adamc@211 346 relC = #relC env,
adamc@211 347 namedC = #namedC env,
adamc@211 348
adamc@211 349 datatypes = #datatypes env,
adamc@211 350 constructors = #constructors env,
adamc@211 351
adamc@211 352 classes = CM.insert (#classes env, ClNamed n, {ground = KM.empty}),
adamc@211 353
adamc@211 354 renameE = #renameE env,
adamc@211 355 relE = #relE env,
adamc@211 356 namedE = #namedE env,
adamc@211 357
adamc@211 358 renameSgn = #renameSgn env,
adamc@211 359 sgn = #sgn env,
adamc@211 360
adamc@211 361 renameStr = #renameStr env,
adamc@403 362 str = #str env}
adamc@211 363
adamc@211 364 fun class_name_in (c, _) =
adamc@211 365 case c of
adamc@211 366 CNamed n => SOME (ClNamed n)
adamc@211 367 | CModProj x => SOME (ClProj x)
adamc@228 368 | CUnif (_, _, _, ref (SOME c)) => class_name_in c
adamc@211 369 | _ => NONE
adamc@211 370
adamc@403 371 fun isClass (env : env) c =
adamc@403 372 let
adamc@403 373 fun find NONE = false
adamc@403 374 | find (SOME c) = Option.isSome (CM.find (#classes env, c))
adamc@403 375 in
adamc@403 376 find (class_name_in c)
adamc@403 377 end
adamc@403 378
adamc@211 379 fun class_key_in (c, _) =
adamc@211 380 case c of
adamc@211 381 CRel n => SOME (CkRel n)
adamc@211 382 | CNamed n => SOME (CkNamed n)
adamc@211 383 | CModProj x => SOME (CkProj x)
adamc@228 384 | CUnif (_, _, _, ref (SOME c)) => class_key_in c
adamc@211 385 | _ => NONE
adamc@211 386
adamc@211 387 fun class_pair_in (c, _) =
adamc@211 388 case c of
adamc@211 389 CApp (f, x) =>
adamc@211 390 (case (class_name_in f, class_key_in x) of
adamc@211 391 (SOME f, SOME x) => SOME (f, x)
adamc@211 392 | _ => NONE)
adamc@211 393 | _ => NONE
adamc@211 394
adamc@211 395 fun resolveClass (env : env) c =
adamc@211 396 case class_pair_in c of
adamc@211 397 SOME (f, x) =>
adamc@211 398 (case CM.find (#classes env, f) of
adamc@211 399 NONE => NONE
adamc@211 400 | SOME class =>
adamc@211 401 case KM.find (#ground class, x) of
adamc@211 402 NONE => NONE
adamc@211 403 | SOME e => SOME e)
adamc@211 404 | _ => NONE
adamc@211 405
adamc@9 406 fun pushERel (env : env) x t =
adamc@9 407 let
adamc@9 408 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 409 | x => x) (#renameE env)
adamc@211 410
adamc@211 411 val classes = CM.map (fn class => {
adamc@211 412 ground = KM.map liftExp (#ground class)
adamc@211 413 }) (#classes env)
adamc@211 414 val classes = case class_pair_in t of
adamc@211 415 NONE => classes
adamc@211 416 | SOME (f, x) =>
adamc@403 417 case CM.find (classes, f) of
adamc@403 418 NONE => classes
adamc@403 419 | SOME class =>
adamc@403 420 let
adamc@403 421 val class = {
adamc@403 422 ground = KM.insert (#ground class, x, (ERel 0, #2 t))
adamc@403 423 }
adamc@403 424 in
adamc@403 425 CM.insert (classes, f, class)
adamc@403 426 end
adamc@9 427 in
adamc@9 428 {renameC = #renameC env,
adamc@9 429 relC = #relC env,
adamc@9 430 namedC = #namedC env,
adamc@9 431
adamc@157 432 datatypes = #datatypes env,
adamc@171 433 constructors = #constructors env,
adamc@157 434
adamc@211 435 classes = classes,
adamc@211 436
adamc@9 437 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 438 relE = (x, t) :: #relE env,
adamc@31 439 namedE = #namedE env,
adamc@31 440
adamc@31 441 renameSgn = #renameSgn env,
adamc@31 442 sgn = #sgn env,
adamc@31 443
adamc@31 444 renameStr = #renameStr env,
adamc@31 445 str = #str env}
adamc@9 446 end
adamc@9 447
adamc@9 448 fun lookupERel (env : env) n =
adamc@9 449 (List.nth (#relE env, n))
adamc@9 450 handle Subscript => raise UnboundRel n
adamc@9 451
adamc@9 452 fun pushENamedAs (env : env) x n t =
adamc@211 453 let
adamc@211 454 val classes = #classes env
adamc@211 455 val classes = case class_pair_in t of
adamc@211 456 NONE => classes
adamc@211 457 | SOME (f, x) =>
adamc@403 458 case CM.find (classes, f) of
adamc@403 459 NONE => classes
adamc@403 460 | SOME class =>
adamc@403 461 let
adamc@403 462 val class = {
adamc@403 463 ground = KM.insert (#ground class, x, (ENamed n, #2 t))
adamc@403 464 }
adamc@403 465 in
adamc@403 466 CM.insert (classes, f, class)
adamc@403 467 end
adamc@211 468 in
adamc@211 469 {renameC = #renameC env,
adamc@211 470 relC = #relC env,
adamc@211 471 namedC = #namedC env,
adamc@9 472
adamc@211 473 datatypes = #datatypes env,
adamc@211 474 constructors = #constructors env,
adamc@157 475
adamc@211 476 classes = classes,
adamc@31 477
adamc@211 478 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@211 479 relE = #relE env,
adamc@211 480 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@211 481
adamc@211 482 renameSgn = #renameSgn env,
adamc@211 483 sgn = #sgn env,
adamc@211 484
adamc@211 485 renameStr = #renameStr env,
adamc@211 486 str = #str env}
adamc@211 487 end
adamc@9 488
adamc@9 489 fun pushENamed env x t =
adamc@9 490 let
adamc@9 491 val n = !namedCounter
adamc@9 492 in
adamc@9 493 namedCounter := n + 1;
adamc@9 494 (pushENamedAs env x n t, n)
adamc@9 495 end
adamc@9 496
adamc@9 497 fun lookupENamed (env : env) n =
adamc@9 498 case IM.find (#namedE env, n) of
adamc@9 499 NONE => raise UnboundNamed n
adamc@9 500 | SOME x => x
adamc@9 501
adamc@9 502 fun lookupE (env : env) x =
adamc@9 503 case SM.find (#renameE env, x) of
adamc@9 504 NONE => NotBound
adamc@9 505 | SOME (Rel' x) => Rel x
adamc@9 506 | SOME (Named' x) => Named x
adamc@2 507
adamc@31 508 fun pushSgnNamedAs (env : env) x n sgis =
adamc@31 509 {renameC = #renameC env,
adamc@31 510 relC = #relC env,
adamc@31 511 namedC = #namedC env,
adamc@31 512
adamc@157 513 datatypes = #datatypes env,
adamc@171 514 constructors = #constructors env,
adamc@157 515
adamc@211 516 classes = #classes env,
adamc@211 517
adamc@31 518 renameE = #renameE env,
adamc@31 519 relE = #relE env,
adamc@31 520 namedE = #namedE env,
adamc@31 521
adamc@31 522 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
adamc@31 523 sgn = IM.insert (#sgn env, n, (x, sgis)),
adamc@31 524
adamc@31 525 renameStr = #renameStr env,
adamc@31 526 str = #str env}
adamc@31 527
adamc@31 528 fun pushSgnNamed env x sgis =
adamc@31 529 let
adamc@31 530 val n = !namedCounter
adamc@31 531 in
adamc@31 532 namedCounter := n + 1;
adamc@31 533 (pushSgnNamedAs env x n sgis, n)
adamc@31 534 end
adamc@31 535
adamc@31 536 fun lookupSgnNamed (env : env) n =
adamc@31 537 case IM.find (#sgn env, n) of
adamc@31 538 NONE => raise UnboundNamed n
adamc@31 539 | SOME x => x
adamc@31 540
adamc@31 541 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
adamc@31 542
adamc@31 543 fun lookupStrNamed (env : env) n =
adamc@31 544 case IM.find (#str env, n) of
adamc@31 545 NONE => raise UnboundNamed n
adamc@31 546 | SOME x => x
adamc@31 547
adamc@31 548 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
adamc@31 549
adamc@203 550
adamc@216 551 fun sgiSeek (sgi, (sgns, strs, cons)) =
adamc@216 552 case sgi of
adamc@216 553 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 554 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 555 | SgiDatatype (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 556 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 557 | SgiVal _ => (sgns, strs, cons)
adamc@216 558 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
adamc@216 559 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
adamc@216 560 | SgiConstraint _ => (sgns, strs, cons)
adamc@216 561 | SgiTable _ => (sgns, strs, cons)
adamc@338 562 | SgiSequence _ => (sgns, strs, cons)
adamc@216 563 | SgiClassAbs (x, n) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 564 | SgiClass (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 565
adamc@34 566 fun sgnSeek f sgis =
adamc@34 567 let
adamc@59 568 fun seek (sgis, sgns, strs, cons) =
adamc@34 569 case sgis of
adamc@34 570 [] => NONE
adamc@34 571 | (sgi, _) :: sgis =>
adamc@34 572 case f sgi of
adamc@158 573 SOME v =>
adamc@158 574 let
adamc@158 575 val cons =
adamc@158 576 case sgi of
adamc@191 577 SgiDatatype (x, n, _, _) => IM.insert (cons, n, x)
adamc@191 578 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
adamc@158 579 | _ => cons
adamc@158 580 in
adamc@158 581 SOME (v, (sgns, strs, cons))
adamc@158 582 end
adamc@88 583 | NONE =>
adamc@216 584 let
adamc@216 585 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
adamc@216 586 in
adamc@216 587 seek (sgis, sgns, strs, cons)
adamc@216 588 end
adamc@34 589 in
adamc@59 590 seek (sgis, IM.empty, IM.empty, IM.empty)
adamc@34 591 end
adamc@34 592
adamc@34 593 fun id x = x
adamc@34 594
adamc@34 595 fun unravelStr (str, _) =
adamc@34 596 case str of
adamc@34 597 StrVar x => (x, [])
adamc@34 598 | StrProj (str, m) =>
adamc@34 599 let
adamc@34 600 val (x, ms) = unravelStr str
adamc@34 601 in
adamc@34 602 (x, ms @ [m])
adamc@34 603 end
adamc@34 604 | _ => raise Fail "unravelStr"
adamc@34 605
adamc@59 606 fun sgnS_con (str, (sgns, strs, cons)) c =
adamc@34 607 case c of
adamc@34 608 CModProj (m1, ms, x) =>
adamc@34 609 (case IM.find (strs, m1) of
adamc@34 610 NONE => c
adamc@34 611 | SOME m1x =>
adamc@34 612 let
adamc@34 613 val (m1, ms') = unravelStr str
adamc@34 614 in
adamc@34 615 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 616 end)
adamc@34 617 | CNamed n =>
adamc@34 618 (case IM.find (cons, n) of
adamc@34 619 NONE => c
adamc@34 620 | SOME nx =>
adamc@34 621 let
adamc@34 622 val (m1, ms) = unravelStr str
adamc@34 623 in
adamc@34 624 CModProj (m1, ms, nx)
adamc@34 625 end)
adamc@34 626 | _ => c
adamc@34 627
adamc@216 628 fun sgnS_con' (m1, ms', (sgns, strs, cons)) c =
adamc@216 629 case c of
adamc@216 630 CModProj (m1, ms, x) =>
adamc@216 631 (case IM.find (strs, m1) of
adamc@216 632 NONE => c
adamc@216 633 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
adamc@216 634 | CNamed n =>
adamc@216 635 (case IM.find (cons, n) of
adamc@216 636 NONE => c
adamc@216 637 | SOME nx => CModProj (m1, ms', nx))
adamc@216 638 | _ => c
adamc@216 639
adamc@59 640 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
adamc@59 641 case sgn of
adamc@59 642 SgnProj (m1, ms, x) =>
adamc@59 643 (case IM.find (strs, m1) of
adamc@59 644 NONE => sgn
adamc@59 645 | SOME m1x =>
adamc@59 646 let
adamc@59 647 val (m1, ms') = unravelStr str
adamc@59 648 in
adamc@59 649 SgnProj (m1, ms' @ m1x :: ms, x)
adamc@59 650 end)
adamc@59 651 | SgnVar n =>
adamc@59 652 (case IM.find (sgns, n) of
adamc@59 653 NONE => sgn
adamc@59 654 | SOME nx =>
adamc@59 655 let
adamc@59 656 val (m1, ms) = unravelStr str
adamc@59 657 in
adamc@59 658 SgnProj (m1, ms, nx)
adamc@59 659 end)
adamc@59 660 | _ => sgn
adamc@59 661
adamc@59 662 fun sgnSubSgn x =
adamc@34 663 ElabUtil.Sgn.map {kind = id,
adamc@59 664 con = sgnS_con x,
adamc@34 665 sgn_item = id,
adamc@59 666 sgn = sgnS_sgn x}
adamc@34 667
adamc@211 668
adamc@211 669
adamc@211 670 and projectSgn env {sgn, str, field} =
adamc@211 671 case #1 (hnormSgn env sgn) of
adamc@211 672 SgnConst sgis =>
adamc@211 673 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@211 674 NONE => NONE
adamc@211 675 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@211 676 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@211 677 | _ => NONE
adamc@211 678
adamc@211 679 and hnormSgn env (all as (sgn, loc)) =
adamc@34 680 case sgn of
adamc@42 681 SgnError => all
adamc@42 682 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 683 | SgnConst _ => all
adamc@42 684 | SgnFun _ => all
adamc@59 685 | SgnProj (m, ms, x) =>
adamc@59 686 let
adamc@59 687 val (_, sgn) = lookupStrNamed env m
adamc@59 688 in
adamc@59 689 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
adamc@59 690 sgn = sgn,
adamc@59 691 field = x} of
adamc@59 692 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
adamc@59 693 | SOME sgn => sgn
adamc@59 694 end
adamc@42 695 | SgnWhere (sgn, x, c) =>
adamc@42 696 case #1 (hnormSgn env sgn) of
adamc@42 697 SgnError => (SgnError, loc)
adamc@42 698 | SgnConst sgis =>
adamc@42 699 let
adamc@42 700 fun traverse (pre, post) =
adamc@42 701 case post of
adamc@42 702 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 703 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adamc@42 704 if x = x' then
adamc@42 705 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adamc@42 706 else
adamc@42 707 traverse (sgi :: pre, rest)
adamc@42 708 | sgi :: rest => traverse (sgi :: pre, rest)
adamc@42 709
adamc@42 710 val sgis = traverse ([], sgis)
adamc@42 711 in
adamc@42 712 (SgnConst sgis, loc)
adamc@42 713 end
adamc@42 714 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adamc@42 715
adamc@211 716 fun enrichClasses env classes (m1, ms) sgn =
adamc@59 717 case #1 (hnormSgn env sgn) of
adamc@59 718 SgnConst sgis =>
adamc@211 719 let
adamc@218 720 val (classes, _, _, _) =
adamc@218 721 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
adamc@211 722 let
adamc@211 723 fun found (x, n) =
adamc@211 724 (CM.insert (classes,
adamc@211 725 ClProj (m1, ms, x),
adamc@211 726 empty_class),
adamc@216 727 IM.insert (newClasses, n, x),
adamc@218 728 sgiSeek (#1 sgi, fmap),
adamc@218 729 env)
adamc@216 730
adamc@218 731 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
adamc@211 732 in
adamc@211 733 case #1 sgi of
adamc@217 734 SgiStr (x, _, sgn) =>
adamc@217 735 (enrichClasses env classes (m1, ms @ [x]) sgn,
adamc@217 736 newClasses,
adamc@218 737 sgiSeek (#1 sgi, fmap),
adamc@218 738 env)
adamc@218 739 | SgiSgn (x, n, sgn) =>
adamc@218 740 (classes,
adamc@218 741 newClasses,
adamc@218 742 fmap,
adamc@218 743 pushSgnNamedAs env x n sgn)
adamc@218 744
adamc@217 745 | SgiClassAbs xn => found xn
adamc@211 746 | SgiClass (x, n, _) => found (x, n)
adamc@419 747 | SgiVal (x, n, (CApp (f, a), _)) =>
adamc@419 748 let
adamc@419 749 fun unravel c =
adamc@419 750 case #1 c of
adamc@419 751 CUnif (_, _, _, ref (SOME c)) => unravel c
adamc@419 752 | CNamed n =>
adamc@419 753 ((case lookupCNamed env n of
adamc@419 754 (_, _, SOME c) => unravel c
adamc@419 755 | _ => c)
adamc@419 756 handle UnboundNamed _ => c)
adamc@419 757 | _ => c
adamc@216 758
adamc@419 759 val nc =
adamc@419 760 case f of
adamc@419 761 (CNamed f, _) => IM.find (newClasses, f)
adamc@419 762 | _ => NONE
adamc@419 763 in
adamc@419 764 case nc of
adamc@419 765 NONE =>
adamc@419 766 (case (class_name_in (unravel f),
adamc@419 767 class_key_in (sgnS_con' (m1, ms, fmap) (#1 a), #2 a)) of
adamc@419 768 (SOME cn, SOME ck) =>
adamc@419 769 let
adamc@419 770 val classes =
adamc@419 771 case CM.find (classes, cn) of
adamc@419 772 NONE => classes
adamc@419 773 | SOME class =>
adamc@419 774 let
adamc@419 775 val class = {
adamc@419 776 ground = KM.insert (#ground class, ck,
adamc@419 777 (EModProj (m1, ms, x), #2 sgn))
adamc@419 778 }
adamc@419 779 in
adamc@419 780 CM.insert (classes, cn, class)
adamc@419 781 end
adamc@419 782 in
adamc@419 783 (classes,
adamc@419 784 newClasses,
adamc@419 785 fmap,
adamc@419 786 env)
adamc@419 787 end
adamc@419 788 | _ => default ())
adamc@419 789 | SOME fx =>
adamc@419 790 case class_key_in (sgnS_con' (m1, ms, fmap) (#1 a), #2 a) of
adamc@419 791 NONE => default ()
adamc@419 792 | SOME ck =>
adamc@419 793 let
adamc@419 794 val cn = ClProj (m1, ms, fx)
adamc@419 795
adamc@419 796 val classes =
adamc@419 797 case CM.find (classes, cn) of
adamc@419 798 NONE => classes
adamc@419 799 | SOME class =>
adamc@419 800 let
adamc@419 801 val class = {
adamc@419 802 ground = KM.insert (#ground class, ck,
adamc@419 803 (EModProj (m1, ms, x), #2 sgn))
adamc@419 804 }
adamc@419 805 in
adamc@419 806 CM.insert (classes, cn, class)
adamc@419 807 end
adamc@419 808 in
adamc@419 809 (classes,
adamc@419 810 newClasses,
adamc@419 811 fmap,
adamc@419 812 env)
adamc@419 813 end
adamc@419 814 end
adamc@217 815 | SgiVal _ => default ()
adamc@216 816 | _ => default ()
adamc@211 817 end)
adamc@218 818 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
adamc@211 819 in
adamc@211 820 classes
adamc@211 821 end
adamc@211 822 | _ => classes
adamc@211 823
adamc@211 824 fun pushStrNamedAs (env : env) x n sgn =
adamc@211 825 {renameC = #renameC env,
adamc@211 826 relC = #relC env,
adamc@211 827 namedC = #namedC env,
adamc@211 828
adamc@211 829 datatypes = #datatypes env,
adamc@211 830 constructors = #constructors env,
adamc@211 831
adamc@211 832 classes = enrichClasses env (#classes env) (n, []) sgn,
adamc@211 833
adamc@211 834 renameE = #renameE env,
adamc@211 835 relE = #relE env,
adamc@211 836 namedE = #namedE env,
adamc@211 837
adamc@211 838 renameSgn = #renameSgn env,
adamc@211 839 sgn = #sgn env,
adamc@211 840
adamc@211 841 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
adamc@211 842 str = IM.insert (#str env, n, (x, sgn))}
adamc@211 843
adamc@211 844 fun pushStrNamed env x sgn =
adamc@211 845 let
adamc@211 846 val n = !namedCounter
adamc@211 847 in
adamc@211 848 namedCounter := n + 1;
adamc@211 849 (pushStrNamedAs env x n sgn, n)
adamc@211 850 end
adamc@211 851
adamc@211 852 fun sgiBinds env (sgi, loc) =
adamc@211 853 case sgi of
adamc@211 854 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@211 855 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@211 856 | SgiDatatype (x, n, xs, xncs) =>
adamc@211 857 let
adamc@341 858 val k = (KType, loc)
adamc@341 859 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 860
adamc@341 861 val env = pushCNamedAs env x n k' NONE
adamc@211 862 in
adamc@211 863 foldl (fn ((x', n', to), env) =>
adamc@211 864 let
adamc@211 865 val t =
adamc@211 866 case to of
adamc@211 867 NONE => (CNamed n, loc)
adamc@211 868 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 869
adamc@211 870 val k = (KType, loc)
adamc@211 871 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 872 in
adamc@211 873 pushENamedAs env x' n' t
adamc@211 874 end)
adamc@211 875 env xncs
adamc@211 876 end
adamc@211 877 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@211 878 let
adamc@341 879 val k = (KType, loc)
adamc@341 880 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 881
adamc@341 882 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
adamc@211 883 in
adamc@211 884 foldl (fn ((x', n', to), env) =>
adamc@211 885 let
adamc@211 886 val t =
adamc@211 887 case to of
adamc@211 888 NONE => (CNamed n, loc)
adamc@211 889 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 890
adamc@211 891 val k = (KType, loc)
adamc@211 892 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 893 in
adamc@211 894 pushENamedAs env x' n' t
adamc@211 895 end)
adamc@211 896 env xncs
adamc@211 897 end
adamc@211 898 | SgiVal (x, n, t) => pushENamedAs env x n t
adamc@211 899 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@211 900 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@211 901 | SgiConstraint _ => env
adamc@211 902
adamc@211 903 | SgiTable (tn, x, n, c) =>
adamc@211 904 let
adamc@338 905 val t = (CApp ((CModProj (tn, [], "sql_table"), loc), c), loc)
adamc@338 906 in
adamc@338 907 pushENamedAs env x n t
adamc@338 908 end
adamc@338 909 | SgiSequence (tn, x, n) =>
adamc@338 910 let
adamc@338 911 val t = (CModProj (tn, [], "sql_sequence"), loc)
adamc@211 912 in
adamc@211 913 pushENamedAs env x n t
adamc@211 914 end
adamc@211 915
adamc@211 916 | SgiClassAbs (x, n) => pushCNamedAs env x n (KArrow ((KType, loc), (KType, loc)), loc) NONE
adamc@211 917 | SgiClass (x, n, c) => pushCNamedAs env x n (KArrow ((KType, loc), (KType, loc)), loc) (SOME c)
adamc@211 918
adamc@211 919
adamc@211 920 fun sgnSubCon x =
adamc@211 921 ElabUtil.Con.map {kind = id,
adamc@211 922 con = sgnS_con x}
adamc@59 923
adamc@59 924 fun projectStr env {sgn, str, field} =
adamc@59 925 case #1 (hnormSgn env sgn) of
adamc@59 926 SgnConst sgis =>
adamc@59 927 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 928 NONE => NONE
adamc@59 929 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 930 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 931 | _ => NONE
adamc@59 932
adamc@158 933 fun chaseMpath env (n, ms) =
adamc@158 934 let
adamc@158 935 val (_, sgn) = lookupStrNamed env n
adamc@158 936 in
adamc@158 937 foldl (fn (m, (str, sgn)) =>
adamc@158 938 case projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 939 NONE => raise Fail "kindof: Unknown substructure"
adamc@158 940 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
adamc@158 941 ((StrVar n, #2 sgn), sgn) ms
adamc@158 942 end
adamc@158 943
adamc@42 944 fun projectCon env {sgn, str, field} =
adamc@42 945 case #1 (hnormSgn env sgn) of
adamc@34 946 SgnConst sgis =>
adamc@34 947 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 948 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@341 949 | SgiDatatype (x, _, xs, _) =>
adamc@158 950 if x = field then
adamc@341 951 let
adamc@341 952 val k = (KType, #2 sgn)
adamc@341 953 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 954 in
adamc@341 955 SOME (k', NONE)
adamc@341 956 end
adamc@341 957 else
adamc@341 958 NONE
adamc@341 959 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
adamc@341 960 if x = field then
adamc@341 961 let
adamc@341 962 val k = (KType, #2 sgn)
adamc@341 963 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 964 in
adamc@341 965 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
adamc@341 966 end
adamc@158 967 else
adamc@158 968 NONE
adamc@216 969 | SgiClassAbs (x, _) => if x = field then
adamc@216 970 SOME ((KArrow ((KType, #2 sgn), (KType, #2 sgn)), #2 sgn), NONE)
adamc@216 971 else
adamc@216 972 NONE
adamc@216 973 | SgiClass (x, _, c) => if x = field then
adamc@216 974 SOME ((KArrow ((KType, #2 sgn), (KType, #2 sgn)), #2 sgn), SOME c)
adamc@216 975 else
adamc@216 976 NONE
adamc@34 977 | _ => NONE) sgis of
adamc@34 978 NONE => NONE
adamc@34 979 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 980 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 981 | _ => NONE
adamc@34 982
adamc@157 983 fun projectDatatype env {sgn, str, field} =
adamc@157 984 case #1 (hnormSgn env sgn) of
adamc@157 985 SgnConst sgis =>
adamc@191 986 (case sgnSeek (fn SgiDatatype (x, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@191 987 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@157 988 | _ => NONE) sgis of
adamc@157 989 NONE => NONE
adamc@191 990 | SOME ((xs, xncs), subs) => SOME (xs,
adamc@191 991 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
adamc@157 992 | _ => NONE
adamc@157 993
adamc@174 994 fun projectConstructor env {sgn, str, field} =
adamc@174 995 case #1 (hnormSgn env sgn) of
adamc@174 996 SgnConst sgis =>
adamc@174 997 let
adamc@191 998 fun consider (n, xs, xncs) =
adamc@174 999 ListUtil.search (fn (x, n', to) =>
adamc@174 1000 if x <> field then
adamc@174 1001 NONE
adamc@174 1002 else
adamc@191 1003 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
adamc@174 1004 in
adamc@191 1005 case sgnSeek (fn SgiDatatype (_, n, xs, xncs) => consider (n, xs, xncs)
adamc@191 1006 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
adamc@174 1007 | _ => NONE) sgis of
adamc@174 1008 NONE => NONE
adamc@191 1009 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
adamc@191 1010 sgnSubCon (str, subs) t)
adamc@174 1011 end
adamc@174 1012 | _ => NONE
adamc@174 1013
adamc@42 1014 fun projectVal env {sgn, str, field} =
adamc@42 1015 case #1 (hnormSgn env sgn) of
adamc@34 1016 SgnConst sgis =>
adamc@162 1017 let
adamc@191 1018 fun seek (n, xs, xncs) =
adamc@162 1019 ListUtil.search (fn (x, _, to) =>
adamc@162 1020 if x = field then
adamc@191 1021 SOME (let
adamc@191 1022 val t =
adamc@191 1023 case to of
adamc@191 1024 NONE => (CNamed n, #2 sgn)
adamc@191 1025 | SOME t => (TFun (t, (CNamed n, #2 sgn)), #2 sgn)
adamc@191 1026 val k = (KType, #2 sgn)
adamc@191 1027 in
adamc@191 1028 foldr (fn (x, t) => (TCFun (Explicit, x, k, t), #2 sgn))
adamc@191 1029 t xs
adamc@191 1030 end)
adamc@162 1031 else
adamc@162 1032 NONE) xncs
adamc@162 1033 in
adamc@162 1034 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
adamc@191 1035 | SgiDatatype (_, n, xs, xncs) => seek (n, xs, xncs)
adamc@191 1036 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
adamc@162 1037 | _ => NONE) sgis of
adamc@162 1038 NONE => NONE
adamc@162 1039 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
adamc@162 1040 end
adamc@34 1041 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 1042 | _ => NONE
adamc@34 1043
adamc@88 1044 fun sgnSeekConstraints (str, sgis) =
adamc@88 1045 let
adamc@88 1046 fun seek (sgis, sgns, strs, cons, acc) =
adamc@88 1047 case sgis of
adamc@88 1048 [] => acc
adamc@88 1049 | (sgi, _) :: sgis =>
adamc@88 1050 case sgi of
adamc@88 1051 SgiConstraint (c1, c2) =>
adamc@88 1052 let
adamc@88 1053 val sub = sgnSubCon (str, (sgns, strs, cons))
adamc@88 1054 in
adamc@88 1055 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
adamc@88 1056 end
adamc@88 1057 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1058 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 1059 | SgiDatatype (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@191 1060 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1061 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
adamc@88 1062 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
adamc@88 1063 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
adamc@203 1064 | SgiTable _ => seek (sgis, sgns, strs, cons, acc)
adamc@338 1065 | SgiSequence _ => seek (sgis, sgns, strs, cons, acc)
adamc@211 1066 | SgiClassAbs (x, n) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@211 1067 | SgiClass (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1068 in
adamc@88 1069 seek (sgis, IM.empty, IM.empty, IM.empty, [])
adamc@88 1070 end
adamc@88 1071
adamc@88 1072 fun projectConstraints env {sgn, str} =
adamc@88 1073 case #1 (hnormSgn env sgn) of
adamc@88 1074 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
adamc@88 1075 | SgnError => SOME []
adamc@88 1076 | _ => NONE
adamc@34 1077
adamc@157 1078 fun declBinds env (d, loc) =
adamc@157 1079 case d of
adamc@157 1080 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@191 1081 | DDatatype (x, n, xs, xncs) =>
adamc@157 1082 let
adamc@297 1083 val k = (KType, loc)
adamc@297 1084 val nxs = length xs
adamc@297 1085 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@297 1086 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@297 1087 (KArrow (k, kb), loc)))
adamc@297 1088 ((CNamed n, loc), k) xs
adamc@297 1089
adamc@297 1090 val env = pushCNamedAs env x n kb NONE
adamc@191 1091 val env = pushDatatype env n xs xncs
adamc@157 1092 in
adamc@191 1093 foldl (fn ((x', n', to), env) =>
adamc@191 1094 let
adamc@191 1095 val t =
adamc@191 1096 case to of
adamc@297 1097 NONE => tb
adamc@297 1098 | SOME t => (TFun (t, tb), loc)
adamc@297 1099 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@191 1100 in
adamc@191 1101 pushENamedAs env x' n' t
adamc@191 1102 end)
adamc@191 1103 env xncs
adamc@157 1104 end
adamc@191 1105 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
adamc@157 1106 let
adamc@157 1107 val t = (CModProj (m, ms, x'), loc)
adamc@297 1108 val k = (KType, loc)
adamc@297 1109 val nxs = length xs
adamc@297 1110 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@297 1111 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@297 1112 (KArrow (k, kb), loc)))
adamc@297 1113 ((CNamed n, loc), k) xs
adamc@297 1114
adamc@341 1115 val env = pushCNamedAs env x n kb (SOME t)
adamc@191 1116 val env = pushDatatype env n xs xncs
adamc@157 1117 in
adamc@191 1118 foldl (fn ((x', n', to), env) =>
adamc@191 1119 let
adamc@191 1120 val t =
adamc@191 1121 case to of
adamc@297 1122 NONE => tb
adamc@297 1123 | SOME t => (TFun (t, tb), loc)
adamc@297 1124 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@191 1125 in
adamc@191 1126 pushENamedAs env x' n' t
adamc@191 1127 end)
adamc@191 1128 env xncs
adamc@157 1129 end
adamc@157 1130 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@157 1131 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
adamc@157 1132 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@157 1133 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@157 1134 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@157 1135 | DConstraint _ => env
adamc@157 1136 | DExport _ => env
adamc@205 1137 | DTable (tn, x, n, c) =>
adamc@205 1138 let
adamc@338 1139 val t = (CApp ((CModProj (tn, [], "sql_table"), loc), c), loc)
adamc@338 1140 in
adamc@338 1141 pushENamedAs env x n t
adamc@338 1142 end
adamc@338 1143 | DSequence (tn, x, n) =>
adamc@338 1144 let
adamc@338 1145 val t = (CModProj (tn, [], "sql_sequence"), loc)
adamc@205 1146 in
adamc@205 1147 pushENamedAs env x n t
adamc@205 1148 end
adamc@213 1149 | DClass (x, n, c) =>
adamc@213 1150 let
adamc@213 1151 val k = (KArrow ((KType, loc), (KType, loc)), loc)
adamc@213 1152 val env = pushCNamedAs env x n k (SOME c)
adamc@213 1153 in
adamc@213 1154 pushClass env n
adamc@213 1155 end
adamc@271 1156 | DDatabase _ => env
adamc@157 1157
adamc@243 1158 fun patBinds env (p, loc) =
adamc@243 1159 case p of
adamc@243 1160 PWild => env
adamc@243 1161 | PVar (x, t) => pushERel env x t
adamc@243 1162 | PPrim _ => env
adamc@243 1163 | PCon (_, _, _, NONE) => env
adamc@243 1164 | PCon (_, _, _, SOME p) => patBinds env p
adamc@243 1165 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
adamc@243 1166
adamc@2 1167 end