annotate src/elab_env.sml @ 589:102e81d975e3

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