annotate src/elab_env.sml @ 1905:cd1cfecc8c72

Remove autogenerated config.h.in from version control Signed-off-by: Anders Kaseorg <andersk@mit.edu> --- .hgignore | 1 + include/urweb/config.h.in | 104 ---------------------------------------------- 2 files changed, 1 insertion(+), 104 deletions(-) delete mode 100644 include/urweb/config.h.in
author Anders Kaseorg <andersk@mit.edu>
date Fri, 22 Nov 2013 09:36:14 -0500
parents 1aa9629e3a4c
children 403f0cc65b9c
rev   line source
adamc@674 1 (* Copyright (c) 2008-2009, Adam Chlipala
adamc@2 2 * All rights reserved.
adamc@2 3 *
adamc@2 4 * Redistribution and use in source and binary forms, with or without
adamc@2 5 * modification, are permitted provided that the following conditions are met:
adamc@2 6 *
adamc@2 7 * - Redistributions of source code must retain the above copyright notice,
adamc@2 8 * this list of conditions and the following disclaimer.
adamc@2 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@2 10 * this list of conditions and the following disclaimer in the documentation
adamc@2 11 * and/or other materials provided with the distribution.
adamc@2 12 * - The names of contributors may not be used to endorse or promote products
adamc@2 13 * derived from this software without specific prior written permission.
adamc@2 14 *
adamc@2 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@2 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@2 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@2 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@2 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@2 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@2 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@2 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@2 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@2 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@2 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@2 26 *)
adamc@2 27
adamc@2 28 structure ElabEnv :> ELAB_ENV = struct
adamc@2 29
adamc@2 30 open Elab
adamc@2 31
adamc@13 32 structure U = ElabUtil
adamc@13 33
adamc@2 34 structure IM = IntBinaryMap
adamc@2 35 structure SM = BinaryMapFn(struct
adamc@2 36 type ord_key = string
adamc@2 37 val compare = String.compare
adamc@2 38 end)
adamc@2 39
adamc@2 40 exception UnboundRel of int
adamc@2 41 exception UnboundNamed of int
adamc@2 42
adamc@13 43
adamc@13 44 (* AST utility functions *)
adamc@13 45
adamc@623 46 val liftKindInKind =
adamc@623 47 U.Kind.mapB {kind = fn bound => fn k =>
adamc@623 48 case k of
adamc@623 49 KRel xn =>
adamc@623 50 if xn < bound then
adamc@623 51 k
adamc@623 52 else
adamc@623 53 KRel (xn + 1)
adamc@623 54 | _ => k,
adamc@623 55 bind = fn (bound, _) => bound + 1}
adamc@623 56
adamc@623 57 val liftKindInCon =
adamc@623 58 U.Con.mapB {kind = fn bound => fn k =>
adamc@623 59 case k of
adamc@623 60 KRel xn =>
adamc@623 61 if xn < bound then
adamc@623 62 k
adamc@623 63 else
adamc@623 64 KRel (xn + 1)
adamc@623 65 | _ => k,
adamc@623 66 con = fn _ => fn c => c,
adamc@623 67 bind = fn (bound, U.Con.RelK _) => bound + 1
adamc@623 68 | (bound, _) => bound}
adamc@623 69
adamc@13 70 val liftConInCon =
adamc@623 71 U.Con.mapB {kind = fn _ => fn k => k,
adamc@13 72 con = fn bound => fn c =>
adamc@13 73 case c of
adamc@16 74 CRel xn =>
adamc@13 75 if xn < bound then
adamc@13 76 c
adamc@13 77 else
adamc@16 78 CRel (xn + 1)
adam@1303 79 | CUnif (nl, loc, k, s, r) => CUnif (nl+1, loc, k, s, r)
adamc@13 80 | _ => c,
adamc@623 81 bind = fn (bound, U.Con.RelC _) => bound + 1
adamc@13 82 | (bound, _) => bound}
adamc@13 83
adamc@13 84 val lift = liftConInCon 0
adamc@13 85
adam@1303 86 fun mliftConInCon by c =
adam@1303 87 if by = 0 then
adam@1303 88 c
adam@1303 89 else
adam@1303 90 U.Con.mapB {kind = fn _ => fn k => k,
adam@1303 91 con = fn bound => fn c =>
adam@1303 92 case c of
adam@1303 93 CRel xn =>
adam@1303 94 if xn < bound then
adam@1303 95 c
adam@1303 96 else
adam@1303 97 CRel (xn + by)
adam@1303 98 | CUnif (nl, loc, k, s, r) => CUnif (nl+by, loc, k, s, r)
adam@1303 99 | _ => c,
adam@1303 100 bind = fn (bound, U.Con.RelC _) => bound + 1
adam@1303 101 | (bound, _) => bound} 0 c
adam@1303 102
adam@1303 103 val () = U.mliftConInCon := mliftConInCon
adam@1303 104
adamc@623 105 val liftKindInExp =
adamc@623 106 U.Exp.mapB {kind = fn bound => fn k =>
adamc@623 107 case k of
adamc@623 108 KRel xn =>
adamc@623 109 if xn < bound then
adamc@623 110 k
adamc@623 111 else
adamc@623 112 KRel (xn + 1)
adamc@623 113 | _ => k,
adamc@623 114 con = fn _ => fn c => c,
adamc@623 115 exp = fn _ => fn e => e,
adamc@623 116 bind = fn (bound, U.Exp.RelK _) => bound + 1
adamc@623 117 | (bound, _) => bound}
adamc@623 118
adamc@448 119 val liftConInExp =
adamc@623 120 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@448 121 con = fn bound => fn c =>
adamc@448 122 case c of
adamc@448 123 CRel xn =>
adamc@448 124 if xn < bound then
adamc@448 125 c
adamc@448 126 else
adamc@448 127 CRel (xn + 1)
adam@1303 128 | CUnif (nl, loc, k, s, r) => CUnif (nl+1, loc, k, s, r)
adamc@448 129 | _ => c,
adamc@448 130 exp = fn _ => fn e => e,
adamc@448 131 bind = fn (bound, U.Exp.RelC _) => bound + 1
adamc@448 132 | (bound, _) => bound}
adamc@448 133
adamc@211 134 val liftExpInExp =
adamc@623 135 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@211 136 con = fn _ => fn c => c,
adamc@211 137 exp = fn bound => fn e =>
adamc@211 138 case e of
adamc@211 139 ERel xn =>
adamc@211 140 if xn < bound then
adamc@211 141 e
adamc@211 142 else
adamc@211 143 ERel (xn + 1)
adamc@211 144 | _ => e,
adamc@211 145 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@211 146 | (bound, _) => bound}
adamc@211 147
adamc@211 148
adamc@211 149 val liftExp = liftExpInExp 0
adamc@13 150
adamc@448 151 val subExpInExp =
adamc@623 152 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@448 153 con = fn _ => fn c => c,
adamc@448 154 exp = fn (xn, rep) => fn e =>
adamc@448 155 case e of
adamc@448 156 ERel xn' =>
adamc@448 157 (case Int.compare (xn', xn) of
adamc@448 158 EQUAL => #1 rep
adamc@448 159 | GREATER=> ERel (xn' - 1)
adamc@448 160 | LESS => e)
adamc@448 161 | _ => e,
adamc@448 162 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
adamc@448 163 | ((xn, rep), U.Exp.RelC _) => (xn, liftConInExp 0 rep)
adamc@448 164 | (ctx, _) => ctx}
adamc@448 165
adamc@13 166 (* Back to environments *)
adamc@13 167
adamc@9 168 datatype 'a var' =
adamc@9 169 Rel' of int * 'a
adamc@9 170 | Named' of int * 'a
adamc@2 171
adamc@9 172 datatype 'a var =
adamc@9 173 NotBound
adamc@9 174 | Rel of int * 'a
adamc@9 175 | Named of int * 'a
adamc@2 176
adamc@191 177 type datatyp = string list * (string * con option) IM.map
adamc@157 178
adamc@211 179 datatype class_name =
adamc@211 180 ClNamed of int
adamc@211 181 | ClProj of int * string list * string
adamc@211 182
adamc@711 183 fun class_name_out cn =
adamc@711 184 case cn of
adamc@711 185 ClNamed n => (CNamed n, ErrorMsg.dummySpan)
adamc@711 186 | ClProj x => (CModProj x, ErrorMsg.dummySpan)
adamc@711 187
adamc@216 188 fun cn2s cn =
adamc@216 189 case cn of
adamc@216 190 ClNamed n => "Named(" ^ Int.toString n ^ ")"
adamc@216 191 | ClProj (m, ms, x) => "Proj(" ^ Int.toString m ^ "," ^ String.concatWith "," ms ^ "," ^ x ^ ")"
adamc@216 192
adamc@211 193 structure CK = struct
adamc@211 194 type ord_key = class_name
adamc@211 195 open Order
adamc@211 196 fun compare x =
adamc@211 197 case x of
adamc@211 198 (ClNamed n1, ClNamed n2) => Int.compare (n1, n2)
adamc@211 199 | (ClNamed _, _) => LESS
adamc@211 200 | (_, ClNamed _) => GREATER
adamc@211 201
adamc@211 202 | (ClProj (m1, ms1, x1), ClProj (m2, ms2, x2)) =>
adamc@211 203 join (Int.compare (m1, m2),
adamc@211 204 fn () => join (joinL String.compare (ms1, ms2),
adamc@211 205 fn () => String.compare (x1, x2)))
adamc@211 206 end
adamc@211 207
adamc@677 208 structure CS = BinarySetFn(CK)
adamc@211 209 structure CM = BinaryMapFn(CK)
adamc@211 210
adam@1795 211 type rules = (int * con list * con * exp) list
adam@1795 212
adam@1795 213 type class = {closedRules : rules,
adam@1795 214 openRules : rules}
adam@1795 215 val empty_class = {closedRules = [],
adam@1795 216 openRules = []}
adamc@216 217
adamc@2 218 type env = {
adamc@623 219 renameK : int SM.map,
adamc@623 220 relK : string list,
adamc@623 221
adamc@9 222 renameC : kind var' SM.map,
adamc@2 223 relC : (string * kind) list,
adamc@11 224 namedC : (string * kind * con option) IM.map,
adamc@9 225
adamc@157 226 datatypes : datatyp IM.map,
adamc@191 227 constructors : (datatype_kind * int * string list * con option * int) SM.map,
adamc@157 228
adamc@211 229 classes : class CM.map,
adamc@211 230
adamc@9 231 renameE : con var' SM.map,
adamc@9 232 relE : (string * con) list,
adamc@31 233 namedE : (string * con) IM.map,
adamc@31 234
adamc@31 235 renameSgn : (int * sgn) SM.map,
adamc@31 236 sgn : (string * sgn) IM.map,
adamc@31 237
adamc@31 238 renameStr : (int * sgn) SM.map,
adamc@31 239 str : (string * sgn) IM.map
adamc@2 240 }
adamc@2 241
adamc@2 242 val namedCounter = ref 0
adamc@2 243
adamc@109 244 fun newNamed () =
adamc@109 245 let
adamc@109 246 val r = !namedCounter
adamc@109 247 in
adamc@109 248 namedCounter := r + 1;
adamc@109 249 r
adamc@109 250 end
adamc@109 251
adamc@2 252 val empty = {
adamc@623 253 renameK = SM.empty,
adamc@623 254 relK = [],
adamc@623 255
adamc@2 256 renameC = SM.empty,
adamc@2 257 relC = [],
adamc@9 258 namedC = IM.empty,
adamc@9 259
adamc@157 260 datatypes = IM.empty,
adamc@171 261 constructors = SM.empty,
adamc@157 262
adamc@211 263 classes = CM.empty,
adamc@211 264
adamc@9 265 renameE = SM.empty,
adamc@9 266 relE = [],
adamc@31 267 namedE = IM.empty,
adamc@31 268
adamc@31 269 renameSgn = SM.empty,
adamc@31 270 sgn = IM.empty,
adamc@31 271
adamc@31 272 renameStr = SM.empty,
adamc@31 273 str = IM.empty
adamc@2 274 }
adamc@2 275
adamc@623 276 fun pushKRel (env : env) x =
adamc@623 277 let
adamc@623 278 val renameK = SM.map (fn n => n+1) (#renameK env)
adamc@623 279 in
adamc@623 280 {renameK = SM.insert (renameK, x, 0),
adamc@623 281 relK = x :: #relK env,
adamc@623 282
adamc@623 283 renameC = SM.map (fn Rel' (n, k) => Rel' (n, liftKindInKind 0 k)
adamc@623 284 | x => x) (#renameC env),
adamc@623 285 relC = map (fn (x, k) => (x, liftKindInKind 0 k)) (#relC env),
adamc@623 286 namedC = #namedC env,
adamc@623 287
adamc@623 288 datatypes = #datatypes env,
adamc@623 289 constructors = #constructors env,
adamc@623 290
adam@1795 291 classes = CM.map (fn cl => {closedRules = #closedRules cl,
adam@1795 292 openRules = map (fn (nvs, cs, c, e) =>
adam@1795 293 (nvs,
adam@1795 294 map (liftKindInCon 0) cs,
adam@1795 295 liftKindInCon 0 c,
adam@1795 296 liftKindInExp 0 e))
adam@1795 297 (#openRules cl)})
adamc@711 298 (#classes env),
adamc@623 299
adamc@623 300 renameE = SM.map (fn Rel' (n, c) => Rel' (n, liftKindInCon 0 c)
adamc@623 301 | Named' (n, c) => Named' (n, c)) (#renameE env),
adamc@623 302 relE = map (fn (x, c) => (x, liftKindInCon 0 c)) (#relE env),
adamc@623 303 namedE = #namedE env,
adamc@623 304
adamc@623 305 renameSgn = #renameSgn env,
adamc@623 306 sgn = #sgn env,
adamc@623 307
adamc@623 308 renameStr = #renameStr env,
adamc@623 309 str = #str env
adamc@623 310 }
adamc@623 311 end
adamc@623 312
adamc@623 313 fun lookupKRel (env : env) n =
adamc@623 314 (List.nth (#relK env, n))
adamc@623 315 handle Subscript => raise UnboundRel n
adamc@623 316
adamc@623 317 fun lookupK (env : env) x = SM.find (#renameK env, x)
adamc@623 318
adamc@2 319 fun pushCRel (env : env) x k =
adamc@2 320 let
adamc@9 321 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
adamc@2 322 | x => x) (#renameC env)
adamc@2 323 in
adamc@623 324 {renameK = #renameK env,
adamc@623 325 relK = #relK env,
adamc@623 326
adamc@623 327 renameC = SM.insert (renameC, x, Rel' (0, k)),
adamc@2 328 relC = (x, k) :: #relC env,
adamc@514 329 namedC = #namedC env,
adamc@9 330
adamc@157 331 datatypes = #datatypes env,
adamc@171 332 constructors = #constructors env,
adamc@157 333
adamc@674 334 classes = CM.map (fn class =>
adam@1795 335 {closedRules = #closedRules class,
adam@1795 336 openRules = map (fn (nvs, cs, c, e) =>
adam@1795 337 (nvs,
adam@1795 338 map (liftConInCon 0) cs,
adam@1795 339 liftConInCon 0 c,
adam@1795 340 liftConInExp 0 e))
adam@1795 341 (#openRules class)})
adamc@211 342 (#classes env),
adamc@211 343
adamc@328 344 renameE = SM.map (fn Rel' (n, c) => Rel' (n, lift c)
adamc@517 345 | Named' (n, c) => Named' (n, c)) (#renameE env),
adamc@13 346 relE = map (fn (x, c) => (x, lift c)) (#relE env),
adamc@514 347 namedE = #namedE env,
adamc@31 348
adamc@31 349 renameSgn = #renameSgn env,
adamc@31 350 sgn = #sgn env,
adamc@31 351
adamc@31 352 renameStr = #renameStr env,
adamc@31 353 str = #str env
adamc@31 354 }
adamc@2 355 end
adamc@2 356
adamc@2 357 fun lookupCRel (env : env) n =
adamc@2 358 (List.nth (#relC env, n))
adamc@2 359 handle Subscript => raise UnboundRel n
adamc@2 360
adamc@11 361 fun pushCNamedAs (env : env) x n k co =
adamc@623 362 {renameK = #renameK env,
adamc@623 363 relK = #relK env,
adamc@623 364
adamc@623 365 renameC = SM.insert (#renameC env, x, Named' (n, k)),
adamc@5 366 relC = #relC env,
adamc@11 367 namedC = IM.insert (#namedC env, n, (x, k, co)),
adamc@9 368
adamc@157 369 datatypes = #datatypes env,
adamc@171 370 constructors = #constructors env,
adamc@157 371
adamc@211 372 classes = #classes env,
adamc@211 373
adamc@9 374 renameE = #renameE env,
adamc@9 375 relE = #relE env,
adamc@31 376 namedE = #namedE env,
adamc@31 377
adamc@31 378 renameSgn = #renameSgn env,
adamc@31 379 sgn = #sgn env,
adamc@31 380
adamc@31 381 renameStr = #renameStr env,
adamc@31 382 str = #str env}
adamc@5 383
adamc@11 384 fun pushCNamed env x k co =
adamc@2 385 let
adamc@2 386 val n = !namedCounter
adamc@2 387 in
adamc@2 388 namedCounter := n + 1;
adamc@11 389 (pushCNamedAs env x n k co, n)
adamc@2 390 end
adamc@2 391
adamc@2 392 fun lookupCNamed (env : env) n =
adamc@2 393 case IM.find (#namedC env, n) of
adamc@2 394 NONE => raise UnboundNamed n
adamc@2 395 | SOME x => x
adamc@2 396
adamc@2 397 fun lookupC (env : env) x =
adamc@2 398 case SM.find (#renameC env, x) of
adamc@9 399 NONE => NotBound
adamc@9 400 | SOME (Rel' x) => Rel x
adamc@9 401 | SOME (Named' x) => Named x
adamc@9 402
adamc@191 403 fun pushDatatype (env : env) n xs xncs =
adamc@188 404 let
adamc@188 405 val dk = U.classifyDatatype xncs
adamc@188 406 in
adamc@623 407 {renameK = #renameK env,
adamc@623 408 relK = #relK env,
adamc@623 409
adamc@623 410 renameC = #renameC env,
adamc@188 411 relC = #relC env,
adamc@188 412 namedC = #namedC env,
adamc@157 413
adamc@188 414 datatypes = IM.insert (#datatypes env, n,
adamc@191 415 (xs, foldl (fn ((x, n, to), cons) =>
adamc@191 416 IM.insert (cons, n, (x, to))) IM.empty xncs)),
adamc@188 417 constructors = foldl (fn ((x, n', to), cmap) =>
adamc@191 418 SM.insert (cmap, x, (dk, n', xs, to, n)))
adamc@188 419 (#constructors env) xncs,
adamc@157 420
adamc@211 421 classes = #classes env,
adamc@211 422
adamc@188 423 renameE = #renameE env,
adamc@188 424 relE = #relE env,
adamc@188 425 namedE = #namedE env,
adamc@157 426
adamc@188 427 renameSgn = #renameSgn env,
adamc@188 428 sgn = #sgn env,
adamc@157 429
adamc@188 430 renameStr = #renameStr env,
adamc@188 431 str = #str env}
adamc@188 432 end
adamc@157 433
adamc@157 434 fun lookupDatatype (env : env) n =
adamc@157 435 case IM.find (#datatypes env, n) of
adamc@157 436 NONE => raise UnboundNamed n
adamc@157 437 | SOME x => x
adamc@157 438
adamc@191 439 fun lookupDatatypeConstructor (_, dt) n =
adamc@157 440 case IM.find (dt, n) of
adamc@157 441 NONE => raise UnboundNamed n
adamc@157 442 | SOME x => x
adamc@157 443
adamc@171 444 fun lookupConstructor (env : env) s = SM.find (#constructors env, s)
adamc@171 445
adamc@191 446 fun datatypeArgs (xs, _) = xs
adamc@191 447 fun constructors (_, dt) = IM.foldri (fn (n, (x, to), ls) => (x, n, to) :: ls) [] dt
adamc@157 448
adamc@711 449 fun listClasses (env : env) =
adam@1795 450 map (fn (cn, {closedRules, openRules}) =>
adamc@711 451 (class_name_out cn,
adam@1795 452 map (fn (nvs, cs, c, e) =>
adamc@711 453 let
adamc@711 454 val loc = #2 c
adamc@711 455 val c = foldr (fn (c', c) => (TFun (c', c), loc)) c cs
adamc@711 456 val c = ListUtil.foldli (fn (n, (), c) => (TCFun (Explicit,
adamc@711 457 "x" ^ Int.toString n,
adamc@711 458 (KError, loc),
adamc@711 459 c), loc))
adamc@711 460 c (List.tabulate (nvs, fn _ => ()))
adamc@711 461 in
adamc@711 462 (c, e)
adam@1795 463 end) (closedRules @ openRules))) (CM.listItemsi (#classes env))
adamc@711 464
adamc@211 465 fun pushClass (env : env) n =
adamc@623 466 {renameK = #renameK env,
adamc@623 467 relK = #relK env,
adamc@623 468
adamc@623 469 renameC = #renameC env,
adamc@211 470 relC = #relC env,
adamc@211 471 namedC = #namedC env,
adamc@211 472
adamc@211 473 datatypes = #datatypes env,
adamc@211 474 constructors = #constructors env,
adamc@211 475
adamc@675 476 classes = CM.insert (#classes env, ClNamed n, empty_class),
adamc@211 477
adamc@211 478 renameE = #renameE env,
adamc@211 479 relE = #relE env,
adamc@211 480 namedE = #namedE env,
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@403 486 str = #str env}
adamc@211 487
adamc@211 488 fun class_name_in (c, _) =
adamc@211 489 case c of
adamc@211 490 CNamed n => SOME (ClNamed n)
adamc@211 491 | CModProj x => SOME (ClProj x)
adam@1639 492 | CUnif (_, _, _, _, ref (Known c)) => class_name_in c
adamc@211 493 | _ => NONE
adamc@211 494
adamc@403 495 fun isClass (env : env) c =
adamc@403 496 let
adamc@403 497 fun find NONE = false
adamc@403 498 | find (SOME c) = Option.isSome (CM.find (#classes env, c))
adamc@403 499 in
adamc@403 500 find (class_name_in c)
adamc@403 501 end
adamc@403 502
adamc@711 503 fun class_head_in c =
adamc@711 504 case #1 c of
adamc@711 505 CApp (f, _) => class_head_in f
adam@1639 506 | CUnif (_, _, _, _, ref (Known c)) => class_head_in c
adamc@711 507 | _ => class_name_in c
adamc@211 508
adamc@711 509 exception Unify
adamc@711 510
adamc@711 511 fun unifyKinds (k1, k2) =
adamc@711 512 case (#1 k1, #1 k2) of
adamc@711 513 (KType, KType) => ()
adamc@711 514 | (KArrow (d1, r1), KArrow (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
adamc@711 515 | (KName, KName) => ()
adamc@711 516 | (KRecord k1, KRecord k2) => unifyKinds (k1, k2)
adamc@711 517 | (KUnit, KUnit) => ()
adamc@711 518 | (KTuple ks1, KTuple ks2) => (ListPair.appEq unifyKinds (ks1, ks2)
adamc@711 519 handle ListPair.UnequalLengths => raise Unify)
adam@1639 520 | (KUnif (_, _, ref (KKnown k1)), _) => unifyKinds (k1, k2)
adam@1639 521 | (_, KUnif (_, _, ref (KKnown k2))) => unifyKinds (k1, k2)
adamc@711 522 | (KRel n1, KRel n2) => if n1 = n2 then () else raise Unify
adamc@711 523 | (KFun (_, k1), KFun (_, k2)) => unifyKinds (k1, k2)
adamc@711 524 | _ => raise Unify
adamc@711 525
adamc@904 526 fun eqCons (c1, c2) =
adamc@904 527 case (#1 c1, #1 c2) of
adam@1639 528 (CUnif (nl, _, _, _, ref (Known c1)), _) => eqCons (mliftConInCon nl c1, c2)
adam@1639 529 | (_, CUnif (nl, _, _, _, ref (Known c2))) => eqCons (c1, mliftConInCon nl c2)
adamc@904 530
adamc@904 531 | (CRel n1, CRel n2) => if n1 = n2 then () else raise Unify
adamc@904 532
adamc@904 533 | (TFun (d1, r1), TFun (d2, r2)) => (eqCons (d1, d2); eqCons (r1, r2))
adamc@904 534 | (TCFun (_, _, k1, r1), TCFun (_, _, k2, r2)) => (unifyKinds (k1, k2); eqCons (r1, r2))
adamc@904 535 | (TRecord c1, TRecord c2) => eqCons (c1, c2)
adamc@904 536 | (TDisjoint (a1, b1, c1), TDisjoint (a2, b2, c2)) =>
adamc@904 537 (eqCons (a1, a2); eqCons (b1, b2); eqCons (c1, c2))
adamc@904 538
adamc@904 539 | (CNamed n1, CNamed n2) => if n1 = n2 then () else raise Unify
adamc@904 540 | (CModProj (n1, ms1, x1), CModProj (n2, ms2, x2)) =>
adamc@904 541 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then () else raise Unify
adamc@904 542 | (CApp (f1, x1), CApp (f2, x2)) => (eqCons (f1, f2); eqCons (x1, x2))
adamc@904 543 | (CAbs (_, k1, b1), CAbs (_, k2, b2)) => (unifyKinds (k1, k2); eqCons (b1, b2))
adamc@904 544
adamc@904 545 | (CKAbs (_, b1), CKAbs (_, b2)) => eqCons (b1, b2)
adamc@904 546 | (CKApp (c1, k1), CKApp (c2, k2)) => (eqCons (c1, c2); unifyKinds (k1, k2))
adamc@904 547 | (TKFun (_, c1), TKFun (_, c2)) => eqCons (c1, c2)
adamc@904 548
adamc@904 549 | (CName s1, CName s2) => if s1 = s2 then () else raise Unify
adamc@904 550
adamc@904 551 | (CRecord (k1, xcs1), CRecord (k2, xcs2)) =>
adamc@904 552 (unifyKinds (k1, k2);
adam@1575 553 if length xcs1 <> length xcs2 then
adam@1575 554 raise Unify
adam@1575 555 else
adam@1575 556 List.app (fn (x1, c1) =>
adam@1575 557 if List.exists (fn (x2, c2) => (eqCons (x1, x2); eqCons (c1, c2); true) handle Unify => false) xcs2 then
adam@1575 558 ()
adam@1575 559 else
adam@1575 560 raise Unify) xcs1)
adamc@904 561 | (CConcat (f1, x1), CConcat (f2, x2)) => (eqCons (f1, f2); eqCons (x1, x2))
adamc@904 562 | (CMap (d1, r1), CMap (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
adamc@904 563
adamc@904 564 | (CUnit, CUnit) => ()
adamc@904 565
adamc@904 566 | (CTuple cs1, CTuple cs2) => (ListPair.appEq (eqCons) (cs1, cs2)
adamc@904 567 handle ListPair.UnequalLengths => raise Unify)
adamc@904 568 | (CProj (c1, n1), CProj (c2, n2)) => (eqCons (c1, c2);
adamc@904 569 if n1 = n2 then () else raise Unify)
adamc@904 570
adamc@904 571 | _ => raise Unify
adamc@904 572
adam@1325 573 fun unifyCons (hnorm : con -> con) rs =
adamc@674 574 let
adamc@711 575 fun unify d (c1, c2) =
adam@1325 576 case (#1 (hnorm c1), #1 (hnorm c2)) of
adam@1639 577 (CUnif (nl, _, _, _, ref (Known c1)), _) => unify d (mliftConInCon nl c1, c2)
adam@1639 578 | (_, CUnif (nl, _, _, _, ref (Known c2))) => unify d (c1, mliftConInCon nl c2)
adamc@711 579
adamc@750 580 | (CUnif _, _) => ()
adamc@750 581
adamc@711 582 | (c1', CRel n2) =>
adamc@711 583 if n2 < d then
adamc@711 584 case c1' of
adamc@711 585 CRel n1 => if n1 = n2 then () else raise Unify
adamc@711 586 | _ => raise Unify
adamc@711 587 else if n2 - d >= length rs then
adamc@711 588 case c1' of
adamc@711 589 CRel n1 => if n1 = n2 - length rs then () else raise Unify
adamc@711 590 | _ => raise Unify
adamc@711 591 else
adamc@711 592 let
adamc@711 593 val r = List.nth (rs, n2 - d)
adamc@711 594 in
adamc@711 595 case !r of
adamc@711 596 NONE => r := SOME c1
adamc@904 597 | SOME c2 => eqCons (c1, c2)
adamc@711 598 end
adamc@711 599
adamc@711 600 | (TFun (d1, r1), TFun (d2, r2)) => (unify d (d1, d2); unify d (r1, r2))
adamc@711 601 | (TCFun (_, _, k1, r1), TCFun (_, _, k2, r2)) => (unifyKinds (k1, k2); unify (d + 1) (r1, r2))
adamc@711 602 | (TRecord c1, TRecord c2) => unify d (c1, c2)
adamc@711 603 | (TDisjoint (a1, b1, c1), TDisjoint (a2, b2, c2)) =>
adamc@711 604 (unify d (a1, a2); unify d (b1, b2); unify d (c1, c2))
adamc@711 605
adamc@711 606 | (CNamed n1, CNamed n2) => if n1 = n2 then () else raise Unify
adamc@711 607 | (CModProj (n1, ms1, x1), CModProj (n2, ms2, x2)) =>
adamc@711 608 if n1 = n2 andalso ms1 = ms2 andalso x1 = x2 then () else raise Unify
adamc@711 609 | (CApp (f1, x1), CApp (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
adamc@711 610 | (CAbs (_, k1, b1), CAbs (_, k2, b2)) => (unifyKinds (k1, k2); unify (d + 1) (b1, b2))
adamc@711 611
adamc@711 612 | (CKAbs (_, b1), CKAbs (_, b2)) => unify d (b1, b2)
adamc@711 613 | (CKApp (c1, k1), CKApp (c2, k2)) => (unify d (c1, c2); unifyKinds (k1, k2))
adamc@711 614 | (TKFun (_, c1), TKFun (_, c2)) => unify d (c1, c2)
adamc@711 615
adamc@711 616 | (CName s1, CName s2) => if s1 = s2 then () else raise Unify
adamc@711 617
adamc@711 618 | (CRecord (k1, xcs1), CRecord (k2, xcs2)) =>
adamc@711 619 (unifyKinds (k1, k2);
adam@1575 620 if length xcs1 <> length xcs2 then
adam@1575 621 raise Unify
adam@1575 622 else
adam@1575 623 app (fn (x1, c1) =>
adam@1575 624 if List.exists (fn (x2, c2) => (unify d (x1, x2); unify d (c1, c2); true) handle Unify => false) xcs2 then
adam@1575 625 ()
adam@1575 626 else
adam@1575 627 raise Unify) xcs1)
adamc@711 628 | (CConcat (f1, x1), CConcat (f2, x2)) => (unify d (f1, f2); unify d (x1, x2))
adamc@711 629 | (CMap (d1, r1), CMap (d2, r2)) => (unifyKinds (d1, d2); unifyKinds (r1, r2))
adamc@711 630
adamc@711 631 | (CUnit, CUnit) => ()
adamc@711 632
adamc@711 633 | (CTuple cs1, CTuple cs2) => (ListPair.appEq (unify d) (cs1, cs2)
adamc@711 634 handle ListPair.UnequalLengths => raise Unify)
adamc@711 635 | (CProj (c1, n1), CProj (c2, n2)) => (unify d (c1, c2);
adamc@711 636 if n1 = n2 then () else raise Unify)
adamc@711 637
adamc@711 638 | _ => raise Unify
adamc@674 639 in
adamc@711 640 unify
adamc@674 641 end
adamc@674 642
adam@1325 643 fun tryUnify hnorm nRs (c1, c2) =
adamc@674 644 let
adamc@711 645 val rs = List.tabulate (nRs, fn _ => ref NONE)
adamc@674 646 in
adam@1325 647 (unifyCons hnorm rs 0 (c1, c2);
adamc@711 648 SOME (map (fn r => case !r of
adamc@711 649 NONE => raise Unify
adamc@711 650 | SOME c => c) rs))
adamc@711 651 handle Unify => NONE
adamc@674 652 end
adamc@674 653
adamc@711 654 fun unifySubst (rs : con list) =
adamc@711 655 U.Con.mapB {kind = fn _ => fn k => k,
adamc@711 656 con = fn d => fn c =>
adamc@711 657 case c of
adamc@711 658 CRel n =>
adamc@711 659 if n < d then
adamc@711 660 c
adam@1795 661 else if n - d >= length rs then
adam@1795 662 CRel (n - d)
adamc@711 663 else
adamc@711 664 #1 (List.nth (rs, n - d))
adamc@711 665 | _ => c,
adamc@711 666 bind = fn (d, U.Con.RelC _) => d + 1
adamc@711 667 | (d, _) => d}
adamc@711 668 0
adamc@711 669
adamc@753 670 exception Bad of con * con
adamc@750 671
adamc@754 672 val hasUnif = U.Con.exists {kind = fn _ => false,
adam@1639 673 con = fn CUnif (_, _, _, _, ref (Unknown _)) => true
adamc@754 674 | _ => false}
adamc@754 675
adamc@754 676 fun startsWithUnif c =
adamc@754 677 let
adamc@754 678 fun firstArg (c, acc) =
adamc@754 679 case #1 c of
adamc@754 680 CApp (f, x) => firstArg (f, SOME x)
adamc@754 681 | _ => acc
adamc@754 682 in
adamc@754 683 case firstArg (c, NONE) of
adamc@754 684 NONE => false
adamc@754 685 | SOME x => hasUnif x
adamc@754 686 end
adamc@754 687
adam@1742 688 val cause = ref (NONE : con option)
adam@1742 689 fun resolveFailureCause () = !cause
adam@1742 690
adamc@753 691 fun resolveClass (hnorm : con -> con) (consEq : con * con -> bool) (env : env) =
adamc@674 692 let
adam@1742 693 fun resolve firstLevel c =
adamc@711 694 let
adam@1742 695 fun notFound () = (if firstLevel then () else cause := SOME c; NONE)
adam@1742 696
adamc@711 697 fun doHead f =
adamc@711 698 case CM.find (#classes env, f) of
adam@1742 699 NONE => notFound ()
adamc@711 700 | SOME class =>
adamc@711 701 let
adamc@711 702 val loc = #2 c
adamc@675 703
adamc@753 704 fun generalize (c as (_, loc)) =
adamc@753 705 case #1 c of
adamc@753 706 CApp (f, x) =>
adamc@753 707 let
adamc@753 708 val (f, equate) = generalize f
adamc@753 709
adamc@753 710 fun isRecord () =
adamc@753 711 let
adam@1639 712 val rk = ref (KUnknown (fn _ => true))
adamc@753 713 val k = (KUnif (loc, "k", rk), loc)
adam@1639 714 val r = ref (Unknown (fn _ => true))
adam@1303 715 val rc = (CUnif (0, loc, k, "x", r), loc)
adamc@753 716 in
adamc@753 717 ((CApp (f, rc), loc),
adamc@753 718 fn () => (if consEq (rc, x) then
adamc@753 719 true
adamc@753 720 else
adamc@753 721 (raise Bad (rc, x);
adamc@753 722 false))
adamc@753 723 andalso equate ())
adamc@753 724 end
adamc@753 725 in
adamc@753 726 case #1 x of
adamc@753 727 CConcat _ => isRecord ()
adamc@753 728 | CRecord _ => isRecord ()
adamc@753 729 | _ => ((CApp (f, x), loc), equate)
adamc@753 730 end
adamc@753 731 | _ => (c, fn () => true)
adamc@753 732
adamc@753 733 val (c, equate) = generalize c
adamc@753 734
adamc@711 735 fun tryRules rules =
adamc@711 736 case rules of
adam@1742 737 [] => notFound ()
adamc@711 738 | (nRs, cs, c', e) :: rules' =>
adam@1795 739 case tryUnify hnorm nRs (c, c') of
adamc@711 740 NONE => tryRules rules'
adamc@711 741 | SOME rs =>
adamc@675 742 let
adam@1742 743 val eos = map (resolve false o unifySubst rs) cs
adamc@675 744 in
adamc@750 745 if List.exists (not o Option.isSome) eos
adamc@753 746 orelse not (equate ())
adamc@753 747 orelse not (consEq (c, unifySubst rs c')) then
adamc@711 748 tryRules rules'
adamc@711 749 else
adamc@711 750 let
adamc@711 751 val es = List.mapPartial (fn x => x) eos
adamc@711 752
adamc@711 753 val e = foldr (fn (c, e) => (ECApp (e, c), loc)) e rs
adamc@711 754 val e = foldl (fn (e', e) => (EApp (e, e'), loc)) e es
adamc@711 755 in
adamc@711 756 SOME e
adamc@711 757 end
adamc@675 758 end
adamc@675 759 in
adam@1795 760 tryRules (#openRules class @ #closedRules class)
adamc@675 761 end
adamc@711 762 in
adamc@754 763 if startsWithUnif c then
adam@1742 764 notFound ()
adamc@754 765 else
adamc@754 766 case #1 c of
adamc@754 767 TRecord c =>
adamc@754 768 (case #1 (hnorm c) of
adamc@754 769 CRecord (_, xts) =>
adamc@754 770 let
adamc@754 771 fun resolver (xts, acc) =
adamc@754 772 case xts of
adamc@754 773 [] => SOME (ERecord acc, #2 c)
adamc@754 774 | (x, t) :: xts =>
adamc@754 775 let
adamc@754 776 val t = hnorm t
adamc@750 777
adamc@754 778 val t = case t of
adamc@754 779 (CApp (f, x), loc) => (CApp (hnorm f, hnorm x), loc)
adamc@754 780 | _ => t
adamc@754 781 in
adam@1742 782 case resolve false t of
adam@1742 783 NONE => notFound ()
adamc@754 784 | SOME e => resolver (xts, (x, e, t) :: acc)
adamc@754 785 end
adamc@754 786 in
adamc@754 787 resolver (xts, [])
adamc@754 788 end
adam@1742 789 | _ => notFound ())
adamc@754 790 | _ =>
adamc@754 791 case class_head_in c of
adamc@754 792 SOME f => doHead f
adam@1742 793 | _ => notFound ()
adamc@711 794 end
adamc@674 795 in
adam@1742 796 cause := NONE;
adam@1742 797 resolve true
adamc@674 798 end
adamc@211 799
adam@1795 800 fun rule_in c =
adam@1795 801 let
adam@1795 802 fun quantifiers (c, nvars) =
adam@1795 803 case #1 c of
adam@1795 804 CUnif (_, _, _, _, ref (Known c)) => quantifiers (c, nvars)
adam@1795 805 | TCFun (_, _, _, c) => quantifiers (c, nvars + 1)
adam@1795 806 | _ =>
adam@1795 807 let
adam@1795 808 fun clauses (c, hyps) =
adam@1795 809 case #1 c of
adam@1795 810 TFun (hyp, c) =>
adam@1795 811 (case class_head_in hyp of
adam@1795 812 SOME _ => clauses (c, hyp :: hyps)
adam@1795 813 | NONE => NONE)
adam@1795 814 | _ =>
adam@1795 815 case class_head_in c of
adam@1795 816 NONE => NONE
adam@1795 817 | SOME f => SOME (f, nvars, rev hyps, c)
adam@1795 818 in
adam@1795 819 clauses (c, [])
adam@1795 820 end
adam@1795 821 in
adam@1795 822 quantifiers (c, 0)
adam@1795 823 end
adam@1795 824
adamc@9 825 fun pushERel (env : env) x t =
adamc@9 826 let
adamc@9 827 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
adamc@9 828 | x => x) (#renameE env)
adamc@211 829
adamc@675 830 val classes = CM.map (fn class =>
adam@1795 831 {openRules = map (fn (nvs, cs, c, e) => (nvs, cs, c, liftExp e)) (#openRules class),
adam@1795 832 closedRules = #closedRules class}) (#classes env)
adam@1795 833 val classes = case rule_in t of
adamc@211 834 NONE => classes
adam@1795 835 | SOME (f, nvs, cs, c) =>
adamc@403 836 case CM.find (classes, f) of
adamc@403 837 NONE => classes
adamc@403 838 | SOME class =>
adamc@403 839 let
adam@1795 840 val rule = (nvs, cs, c, (ERel 0, #2 t))
adam@1795 841
adam@1796 842 val class = {openRules = rule :: #openRules class,
adam@1796 843 closedRules = #closedRules class}
adamc@403 844 in
adamc@403 845 CM.insert (classes, f, class)
adamc@403 846 end
adamc@9 847 in
adamc@623 848 {renameK = #renameK env,
adamc@623 849 relK = #relK env,
adamc@623 850
adamc@623 851 renameC = #renameC env,
adamc@9 852 relC = #relC env,
adamc@9 853 namedC = #namedC env,
adamc@9 854
adamc@157 855 datatypes = #datatypes env,
adamc@171 856 constructors = #constructors env,
adamc@157 857
adamc@211 858 classes = classes,
adamc@211 859
adamc@9 860 renameE = SM.insert (renameE, x, Rel' (0, t)),
adamc@9 861 relE = (x, t) :: #relE env,
adamc@31 862 namedE = #namedE env,
adamc@31 863
adamc@31 864 renameSgn = #renameSgn env,
adamc@31 865 sgn = #sgn env,
adamc@31 866
adamc@31 867 renameStr = #renameStr env,
adamc@31 868 str = #str env}
adamc@9 869 end
adamc@9 870
adamc@9 871 fun lookupERel (env : env) n =
adamc@9 872 (List.nth (#relE env, n))
adamc@9 873 handle Subscript => raise UnboundRel n
adamc@9 874
adamc@9 875 fun pushENamedAs (env : env) x n t =
adamc@211 876 let
adamc@211 877 val classes = #classes env
adamc@674 878 val classes = case rule_in t of
adamc@211 879 NONE => classes
adamc@711 880 | SOME (f, nvs, cs, c) =>
adamc@403 881 case CM.find (classes, f) of
adamc@403 882 NONE => classes
adamc@403 883 | SOME class =>
adamc@403 884 let
adamc@675 885 val e = (ENamed n, #2 t)
adamc@675 886
adamc@675 887 val class =
adam@1795 888 {openRules = #openRules class,
adam@1795 889 closedRules = (nvs, cs, c, e) :: #closedRules class}
adamc@403 890 in
adamc@403 891 CM.insert (classes, f, class)
adamc@403 892 end
adamc@211 893 in
adamc@623 894 {renameK = #renameK env,
adamc@623 895 relK = #relK env,
adamc@623 896
adamc@623 897 renameC = #renameC env,
adamc@211 898 relC = #relC env,
adamc@211 899 namedC = #namedC env,
adamc@9 900
adamc@211 901 datatypes = #datatypes env,
adamc@211 902 constructors = #constructors env,
adamc@157 903
adamc@211 904 classes = classes,
adamc@31 905
adamc@211 906 renameE = SM.insert (#renameE env, x, Named' (n, t)),
adamc@211 907 relE = #relE env,
adamc@211 908 namedE = IM.insert (#namedE env, n, (x, t)),
adamc@211 909
adamc@211 910 renameSgn = #renameSgn env,
adamc@211 911 sgn = #sgn env,
adamc@211 912
adamc@211 913 renameStr = #renameStr env,
adamc@211 914 str = #str env}
adamc@211 915 end
adamc@9 916
adamc@9 917 fun pushENamed env x t =
adamc@9 918 let
adamc@9 919 val n = !namedCounter
adamc@9 920 in
adamc@9 921 namedCounter := n + 1;
adamc@9 922 (pushENamedAs env x n t, n)
adamc@9 923 end
adamc@9 924
adamc@9 925 fun lookupENamed (env : env) n =
adamc@9 926 case IM.find (#namedE env, n) of
adamc@9 927 NONE => raise UnboundNamed n
adamc@9 928 | SOME x => x
adamc@9 929
adamc@471 930 fun checkENamed (env : env) n =
adamc@471 931 Option.isSome (IM.find (#namedE env, n))
adamc@471 932
adamc@9 933 fun lookupE (env : env) x =
adamc@9 934 case SM.find (#renameE env, x) of
adamc@9 935 NONE => NotBound
adamc@9 936 | SOME (Rel' x) => Rel x
adamc@9 937 | SOME (Named' x) => Named x
adamc@2 938
adamc@31 939 fun pushSgnNamedAs (env : env) x n sgis =
adamc@623 940 {renameK = #renameK env,
adamc@623 941 relK = #relK env,
adamc@623 942
adamc@623 943 renameC = #renameC env,
adamc@31 944 relC = #relC env,
adamc@31 945 namedC = #namedC env,
adamc@31 946
adamc@157 947 datatypes = #datatypes env,
adamc@171 948 constructors = #constructors env,
adamc@157 949
adamc@211 950 classes = #classes env,
adamc@211 951
adamc@31 952 renameE = #renameE env,
adamc@31 953 relE = #relE env,
adamc@31 954 namedE = #namedE env,
adamc@31 955
adamc@31 956 renameSgn = SM.insert (#renameSgn env, x, (n, sgis)),
adamc@31 957 sgn = IM.insert (#sgn env, n, (x, sgis)),
adamc@31 958
adamc@31 959 renameStr = #renameStr env,
adamc@31 960 str = #str env}
adamc@31 961
adamc@31 962 fun pushSgnNamed env x sgis =
adamc@31 963 let
adamc@31 964 val n = !namedCounter
adamc@31 965 in
adamc@31 966 namedCounter := n + 1;
adamc@31 967 (pushSgnNamedAs env x n sgis, n)
adamc@31 968 end
adamc@31 969
adamc@31 970 fun lookupSgnNamed (env : env) n =
adamc@31 971 case IM.find (#sgn env, n) of
adamc@31 972 NONE => raise UnboundNamed n
adamc@31 973 | SOME x => x
adamc@31 974
adamc@31 975 fun lookupSgn (env : env) x = SM.find (#renameSgn env, x)
adamc@31 976
adamc@31 977 fun lookupStrNamed (env : env) n =
adamc@31 978 case IM.find (#str env, n) of
adamc@31 979 NONE => raise UnboundNamed n
adamc@31 980 | SOME x => x
adamc@31 981
adamc@31 982 fun lookupStr (env : env) x = SM.find (#renameStr env, x)
adamc@31 983
adamc@203 984
adamc@216 985 fun sgiSeek (sgi, (sgns, strs, cons)) =
adamc@216 986 case sgi of
adamc@216 987 SgiConAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 988 | SgiCon (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@805 989 | SgiDatatype dts => (sgns, strs, foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts)
adamc@216 990 | SgiDatatypeImp (x, n, _, _, _, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 991 | SgiVal _ => (sgns, strs, cons)
adamc@216 992 | SgiSgn (x, n, _) => (IM.insert (sgns, n, x), strs, cons)
adamc@216 993 | SgiStr (x, n, _) => (sgns, IM.insert (strs, n, x), cons)
adamc@216 994 | SgiConstraint _ => (sgns, strs, cons)
adamc@563 995 | SgiClassAbs (x, n, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@563 996 | SgiClass (x, n, _, _) => (sgns, strs, IM.insert (cons, n, x))
adamc@216 997
adamc@34 998 fun sgnSeek f sgis =
adamc@34 999 let
adamc@59 1000 fun seek (sgis, sgns, strs, cons) =
adamc@34 1001 case sgis of
adamc@34 1002 [] => NONE
adamc@34 1003 | (sgi, _) :: sgis =>
adamc@34 1004 case f sgi of
adamc@158 1005 SOME v =>
adamc@158 1006 let
adamc@158 1007 val cons =
adamc@158 1008 case sgi of
adamc@805 1009 SgiDatatype dts => foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts
adamc@191 1010 | SgiDatatypeImp (x, n, _, _, _, _, _) => IM.insert (cons, n, x)
adamc@158 1011 | _ => cons
adamc@158 1012 in
adamc@158 1013 SOME (v, (sgns, strs, cons))
adamc@158 1014 end
adamc@88 1015 | NONE =>
adamc@216 1016 let
adamc@216 1017 val (sgns, strs, cons) = sgiSeek (sgi, (sgns, strs, cons))
adamc@216 1018 in
adamc@216 1019 seek (sgis, sgns, strs, cons)
adamc@216 1020 end
adamc@34 1021 in
adamc@59 1022 seek (sgis, IM.empty, IM.empty, IM.empty)
adamc@34 1023 end
adamc@34 1024
adamc@34 1025 fun id x = x
adamc@34 1026
adamc@34 1027 fun unravelStr (str, _) =
adamc@34 1028 case str of
adamc@34 1029 StrVar x => (x, [])
adamc@34 1030 | StrProj (str, m) =>
adamc@34 1031 let
adamc@34 1032 val (x, ms) = unravelStr str
adamc@34 1033 in
adamc@34 1034 (x, ms @ [m])
adamc@34 1035 end
adamc@34 1036 | _ => raise Fail "unravelStr"
adamc@34 1037
adamc@59 1038 fun sgnS_con (str, (sgns, strs, cons)) c =
adamc@34 1039 case c of
adamc@34 1040 CModProj (m1, ms, x) =>
adamc@34 1041 (case IM.find (strs, m1) of
adamc@34 1042 NONE => c
adamc@34 1043 | SOME m1x =>
adamc@34 1044 let
adamc@34 1045 val (m1, ms') = unravelStr str
adamc@34 1046 in
adamc@34 1047 CModProj (m1, ms' @ m1x :: ms, x)
adamc@34 1048 end)
adamc@34 1049 | CNamed n =>
adamc@34 1050 (case IM.find (cons, n) of
adamc@34 1051 NONE => c
adamc@34 1052 | SOME nx =>
adamc@34 1053 let
adamc@34 1054 val (m1, ms) = unravelStr str
adamc@34 1055 in
adamc@34 1056 CModProj (m1, ms, nx)
adamc@34 1057 end)
adamc@34 1058 | _ => c
adamc@34 1059
adamc@721 1060 fun sgnS_con' (m1, ms', (sgns, strs, cons)) =
adamc@721 1061 U.Con.map {kind = fn x => x,
adamc@721 1062 con = fn c =>
adamc@721 1063 case c of
kkallio@1454 1064 CModProj (m1', ms, x) =>
kkallio@1454 1065 (case IM.find (strs, m1') of
adamc@721 1066 NONE => c
adamc@721 1067 | SOME m1x => CModProj (m1, ms' @ m1x :: ms, x))
adamc@721 1068 | CNamed n =>
adamc@721 1069 (case IM.find (cons, n) of
adamc@721 1070 NONE => c
adamc@721 1071 | SOME nx => CModProj (m1, ms', nx))
adamc@721 1072 | _ => c}
adamc@216 1073
adamc@59 1074 fun sgnS_sgn (str, (sgns, strs, cons)) sgn =
adamc@59 1075 case sgn of
adamc@59 1076 SgnProj (m1, ms, x) =>
adamc@59 1077 (case IM.find (strs, m1) of
adamc@59 1078 NONE => sgn
adamc@59 1079 | SOME m1x =>
adamc@59 1080 let
adamc@59 1081 val (m1, ms') = unravelStr str
adamc@59 1082 in
adamc@59 1083 SgnProj (m1, ms' @ m1x :: ms, x)
adamc@59 1084 end)
adamc@59 1085 | SgnVar n =>
adamc@59 1086 (case IM.find (sgns, n) of
adamc@59 1087 NONE => sgn
adamc@59 1088 | SOME nx =>
adamc@59 1089 let
adamc@59 1090 val (m1, ms) = unravelStr str
adamc@59 1091 in
adamc@59 1092 SgnProj (m1, ms, nx)
adamc@59 1093 end)
adamc@59 1094 | _ => sgn
adamc@59 1095
adamc@59 1096 fun sgnSubSgn x =
adamc@34 1097 ElabUtil.Sgn.map {kind = id,
adamc@59 1098 con = sgnS_con x,
adamc@34 1099 sgn_item = id,
adamc@59 1100 sgn = sgnS_sgn x}
adamc@34 1101
adamc@211 1102
adamc@211 1103
adamc@211 1104 and projectSgn env {sgn, str, field} =
adamc@211 1105 case #1 (hnormSgn env sgn) of
adamc@211 1106 SgnConst sgis =>
adamc@211 1107 (case sgnSeek (fn SgiSgn (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@211 1108 NONE => NONE
adamc@211 1109 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@211 1110 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@211 1111 | _ => NONE
adamc@211 1112
adamc@211 1113 and hnormSgn env (all as (sgn, loc)) =
adamc@34 1114 case sgn of
adamc@42 1115 SgnError => all
adamc@42 1116 | SgnVar n => hnormSgn env (#2 (lookupSgnNamed env n))
adamc@42 1117 | SgnConst _ => all
adamc@42 1118 | SgnFun _ => all
adamc@59 1119 | SgnProj (m, ms, x) =>
adamc@59 1120 let
adamc@59 1121 val (_, sgn) = lookupStrNamed env m
adamc@59 1122 in
adamc@59 1123 case projectSgn env {str = foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms,
adamc@59 1124 sgn = sgn,
adamc@59 1125 field = x} of
adamc@59 1126 NONE => raise Fail "ElabEnv.hnormSgn: projectSgn failed"
adamc@1058 1127 | SOME sgn => hnormSgn env sgn
adamc@59 1128 end
adam@1864 1129 | SgnWhere (sgn, ms, x, c) =>
adam@1864 1130 let
adam@1864 1131 fun rewrite (sgn, ms) =
adam@1864 1132 case #1 (hnormSgn env sgn) of
adam@1864 1133 SgnError => (SgnError, loc)
adam@1864 1134 | SgnConst sgis =>
adam@1864 1135 let
adam@1864 1136 fun traverse (ms, pre, post) =
adam@1864 1137 case post of
adam@1864 1138 [] => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [1]"
adamc@42 1139
adam@1864 1140 | (sgi as (SgiConAbs (x', n, k), loc)) :: rest =>
adam@1864 1141 if List.null ms andalso x = x' then
adam@1864 1142 List.revAppend (pre, (SgiCon (x', n, k, c), loc) :: rest)
adam@1864 1143 else
adam@1864 1144 traverse (ms, sgi :: pre, rest)
adam@1864 1145
adam@1864 1146 | (sgi as (SgiStr (x', n, sgn'), loc)) :: rest =>
adam@1864 1147 (case ms of
adam@1864 1148 [] => traverse (ms, sgi :: pre, rest)
adam@1864 1149 | x :: ms' =>
adam@1864 1150 if x = x' then
adam@1864 1151 List.revAppend (pre,
adam@1864 1152 (SgiStr (x', n,
adam@1864 1153 rewrite (sgn', ms')), loc) :: rest)
adam@1864 1154 else
adam@1864 1155 traverse (ms, sgi :: pre, rest))
adam@1864 1156
adam@1864 1157 | sgi :: rest => traverse (ms, sgi :: pre, rest)
adam@1864 1158
adam@1864 1159 val sgis = traverse (ms, [], sgis)
adam@1864 1160 in
adam@1864 1161 (SgnConst sgis, loc)
adam@1864 1162 end
adam@1864 1163 | _ => raise Fail "ElabEnv.hnormSgn: Can't reduce 'where' [2]"
adam@1864 1164 in
adam@1864 1165 rewrite (sgn, ms)
adam@1864 1166 end
adamc@42 1167
adamc@849 1168 fun manifest (m, ms, loc) =
adamc@849 1169 foldl (fn (m, str) => (StrProj (str, m), loc)) (StrVar m, loc) ms
adamc@849 1170
adamc@211 1171 fun enrichClasses env classes (m1, ms) sgn =
adamc@59 1172 case #1 (hnormSgn env sgn) of
adamc@59 1173 SgnConst sgis =>
adamc@211 1174 let
adamc@218 1175 val (classes, _, _, _) =
adamc@218 1176 foldl (fn (sgi, (classes, newClasses, fmap, env)) =>
adamc@211 1177 let
adamc@211 1178 fun found (x, n) =
adamc@211 1179 (CM.insert (classes,
adamc@211 1180 ClProj (m1, ms, x),
adamc@211 1181 empty_class),
adamc@216 1182 IM.insert (newClasses, n, x),
adamc@218 1183 sgiSeek (#1 sgi, fmap),
adamc@218 1184 env)
adamc@216 1185
adamc@218 1186 fun default () = (classes, newClasses, sgiSeek (#1 sgi, fmap), env)
adamc@211 1187 in
adamc@211 1188 case #1 sgi of
adamc@217 1189 SgiStr (x, _, sgn) =>
adamc@849 1190 let
adamc@849 1191 val str = manifest (m1, ms, #2 sgi)
adamc@849 1192 val sgn' = sgnSubSgn (str, fmap) sgn
adamc@849 1193 in
adamc@849 1194 (enrichClasses env classes (m1, ms @ [x]) sgn',
adamc@849 1195 newClasses,
adamc@849 1196 sgiSeek (#1 sgi, fmap),
adamc@849 1197 env)
adamc@849 1198 end
adamc@218 1199 | SgiSgn (x, n, sgn) =>
adamc@218 1200 (classes,
adamc@218 1201 newClasses,
adamc@218 1202 fmap,
adamc@218 1203 pushSgnNamedAs env x n sgn)
adamc@218 1204
adamc@563 1205 | SgiClassAbs (x, n, _) => found (x, n)
adamc@563 1206 | SgiClass (x, n, _, _) => found (x, n)
adamc@674 1207 | SgiVal (x, n, c) =>
adamc@674 1208 (case rule_in c of
adamc@674 1209 NONE => default ()
adamc@711 1210 | SOME (cn, nvs, cs, c) =>
adamc@674 1211 let
adamc@711 1212 val loc = #2 c
adamc@721 1213 val globalize = sgnS_con' (m1, ms, fmap)
adamc@674 1214
adamc@674 1215 val nc =
adamc@674 1216 case cn of
adamc@674 1217 ClNamed f => IM.find (newClasses, f)
adamc@674 1218 | _ => NONE
adamc@674 1219 in
adamc@674 1220 case nc of
adamc@674 1221 NONE =>
adamc@419 1222 let
adamc@419 1223 val classes =
adamc@419 1224 case CM.find (classes, cn) of
adamc@419 1225 NONE => classes
adamc@419 1226 | SOME class =>
adamc@419 1227 let
adamc@711 1228 val e = (EModProj (m1, ms, x), #2 sgn)
adamc@675 1229
adamc@675 1230 val class =
adam@1795 1231 {openRules = #openRules class,
adam@1795 1232 closedRules = (nvs,
adam@1795 1233 map globalize cs,
adam@1795 1234 globalize c,
adam@1795 1235 e) :: #closedRules class}
adamc@419 1236 in
adamc@419 1237 CM.insert (classes, cn, class)
adamc@419 1238 end
adamc@419 1239 in
adamc@419 1240 (classes,
adamc@419 1241 newClasses,
adamc@419 1242 fmap,
adamc@419 1243 env)
adamc@419 1244 end
adamc@674 1245 | SOME fx =>
adamc@674 1246 let
adamc@674 1247 val cn = ClProj (m1, ms, fx)
adamc@419 1248
adamc@674 1249 val classes =
adamc@674 1250 case CM.find (classes, cn) of
adamc@674 1251 NONE => classes
adamc@674 1252 | SOME class =>
adamc@674 1253 let
adamc@675 1254 val e = (EModProj (m1, ms, x), #2 sgn)
adamc@675 1255
adamc@675 1256 val class =
adam@1795 1257 {openRules = #openRules class,
adam@1795 1258 closedRules = (nvs,
adam@1795 1259 map globalize cs,
adam@1795 1260 globalize c,
adam@1795 1261 e) :: #closedRules class}
adamc@674 1262 in
adamc@674 1263 CM.insert (classes, cn, class)
adamc@674 1264 end
adamc@674 1265 in
adamc@674 1266 (classes,
adamc@674 1267 newClasses,
adamc@674 1268 fmap,
adamc@674 1269 env)
adamc@674 1270 end
adamc@674 1271 end)
adamc@216 1272 | _ => default ()
adamc@211 1273 end)
adamc@218 1274 (classes, IM.empty, (IM.empty, IM.empty, IM.empty), env) sgis
adamc@211 1275 in
adamc@211 1276 classes
adamc@211 1277 end
adamc@211 1278 | _ => classes
adamc@211 1279
adamc@211 1280 fun pushStrNamedAs (env : env) x n sgn =
adamc@623 1281 {renameK = #renameK env,
adamc@623 1282 relK = #relK env,
adamc@623 1283
adamc@623 1284 renameC = #renameC env,
adamc@211 1285 relC = #relC env,
adamc@211 1286 namedC = #namedC env,
adamc@211 1287
adamc@211 1288 datatypes = #datatypes env,
adamc@211 1289 constructors = #constructors env,
adamc@211 1290
adamc@211 1291 classes = enrichClasses env (#classes env) (n, []) sgn,
adamc@211 1292
adamc@211 1293 renameE = #renameE env,
adamc@211 1294 relE = #relE env,
adamc@211 1295 namedE = #namedE env,
adamc@211 1296
adamc@211 1297 renameSgn = #renameSgn env,
adamc@211 1298 sgn = #sgn env,
adamc@211 1299
adamc@211 1300 renameStr = SM.insert (#renameStr env, x, (n, sgn)),
adamc@211 1301 str = IM.insert (#str env, n, (x, sgn))}
adamc@211 1302
adamc@211 1303 fun pushStrNamed env x sgn =
adamc@211 1304 let
adamc@211 1305 val n = !namedCounter
adamc@211 1306 in
adamc@211 1307 namedCounter := n + 1;
adamc@211 1308 (pushStrNamedAs env x n sgn, n)
adamc@211 1309 end
adamc@211 1310
adamc@211 1311 fun sgiBinds env (sgi, loc) =
adamc@211 1312 case sgi of
adamc@211 1313 SgiConAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@211 1314 | SgiCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@805 1315 | SgiDatatype dts =>
adamc@211 1316 let
adamc@805 1317 fun doOne ((x, n, xs, xncs), env) =
adamc@805 1318 let
adamc@805 1319 val k = (KType, loc)
adamc@805 1320 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 1321
adamc@805 1322 val env = pushCNamedAs env x n k' NONE
adamc@805 1323 in
adamc@805 1324 foldl (fn ((x', n', to), env) =>
adamc@805 1325 let
adamc@805 1326 val t =
adamc@805 1327 case to of
adamc@805 1328 NONE => (CNamed n, loc)
adamc@805 1329 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@805 1330
adamc@805 1331 val k = (KType, loc)
adamc@805 1332 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@805 1333 in
adamc@805 1334 pushENamedAs env x' n' t
adamc@805 1335 end)
adamc@805 1336 env xncs
adamc@805 1337 end
adamc@211 1338 in
adamc@805 1339 foldl doOne env dts
adamc@211 1340 end
adamc@211 1341 | SgiDatatypeImp (x, n, m1, ms, x', xs, xncs) =>
adamc@211 1342 let
adamc@341 1343 val k = (KType, loc)
adamc@341 1344 val k' = foldr (fn (_, k') => (KArrow (k, k'), loc)) k xs
adamc@341 1345
adamc@341 1346 val env = pushCNamedAs env x n k' (SOME (CModProj (m1, ms, x'), loc))
adamc@211 1347 in
adamc@211 1348 foldl (fn ((x', n', to), env) =>
adamc@211 1349 let
adamc@211 1350 val t =
adamc@211 1351 case to of
adamc@211 1352 NONE => (CNamed n, loc)
adamc@211 1353 | SOME t => (TFun (t, (CNamed n, loc)), loc)
adamc@211 1354
adamc@211 1355 val k = (KType, loc)
adamc@211 1356 val t = foldr (fn (x, t) => (TCFun (Explicit, x, k, t), loc)) t xs
adamc@211 1357 in
adamc@211 1358 pushENamedAs env x' n' t
adamc@211 1359 end)
adamc@211 1360 env xncs
adamc@211 1361 end
adamc@211 1362 | SgiVal (x, n, t) => pushENamedAs env x n t
adamc@211 1363 | SgiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@211 1364 | SgiSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@211 1365 | SgiConstraint _ => env
adamc@211 1366
adamc@711 1367 | SgiClassAbs (x, n, k) => pushCNamedAs env x n k NONE
adamc@711 1368 | SgiClass (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@459 1369
adamc@211 1370 fun sgnSubCon x =
adamc@211 1371 ElabUtil.Con.map {kind = id,
adamc@211 1372 con = sgnS_con x}
adamc@59 1373
adamc@59 1374 fun projectStr env {sgn, str, field} =
adamc@59 1375 case #1 (hnormSgn env sgn) of
adamc@59 1376 SgnConst sgis =>
adamc@59 1377 (case sgnSeek (fn SgiStr (x, _, sgn) => if x = field then SOME sgn else NONE | _ => NONE) sgis of
adamc@59 1378 NONE => NONE
adamc@59 1379 | SOME (sgn, subs) => SOME (sgnSubSgn (str, subs) sgn))
adamc@59 1380 | SgnError => SOME (SgnError, ErrorMsg.dummySpan)
adamc@59 1381 | _ => NONE
adamc@59 1382
adamc@158 1383 fun chaseMpath env (n, ms) =
adamc@158 1384 let
adamc@158 1385 val (_, sgn) = lookupStrNamed env n
adamc@158 1386 in
adamc@158 1387 foldl (fn (m, (str, sgn)) =>
adamc@158 1388 case projectStr env {sgn = sgn, str = str, field = m} of
adamc@158 1389 NONE => raise Fail "kindof: Unknown substructure"
adamc@158 1390 | SOME sgn => ((StrProj (str, m), #2 sgn), sgn))
adamc@158 1391 ((StrVar n, #2 sgn), sgn) ms
adamc@158 1392 end
adamc@158 1393
adamc@42 1394 fun projectCon env {sgn, str, field} =
adamc@42 1395 case #1 (hnormSgn env sgn) of
adamc@34 1396 SgnConst sgis =>
adamc@34 1397 (case sgnSeek (fn SgiConAbs (x, _, k) => if x = field then SOME (k, NONE) else NONE
adamc@34 1398 | SgiCon (x, _, k, c) => if x = field then SOME (k, SOME c) else NONE
adamc@805 1399 | SgiDatatype dts =>
adamc@805 1400 (case List.find (fn (x, _, xs, _) => x = field) dts of
adamc@805 1401 SOME (_, _, xs, _) =>
adamc@805 1402 let
adamc@805 1403 val k = (KType, #2 sgn)
adamc@805 1404 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@805 1405 in
adamc@805 1406 SOME (k', NONE)
adamc@805 1407 end
adamc@805 1408 | NONE => NONE)
adamc@341 1409 | SgiDatatypeImp (x, _, m1, ms, x', xs, _) =>
adamc@341 1410 if x = field then
adamc@341 1411 let
adamc@341 1412 val k = (KType, #2 sgn)
adamc@341 1413 val k' = foldl (fn (_, k') => (KArrow (k, k'), #2 sgn)) k xs
adamc@341 1414 in
adamc@341 1415 SOME (k', SOME (CModProj (m1, ms, x'), #2 sgn))
adamc@341 1416 end
adamc@158 1417 else
adamc@158 1418 NONE
adamc@563 1419 | SgiClassAbs (x, _, k) => if x = field then
kkallio@1453 1420 SOME (k, NONE)
adamc@563 1421 else
adamc@563 1422 NONE
adamc@563 1423 | SgiClass (x, _, k, c) => if x = field then
kkallio@1453 1424 SOME (k, SOME c)
adamc@563 1425 else
adamc@563 1426 NONE
adamc@34 1427 | _ => NONE) sgis of
adamc@34 1428 NONE => NONE
adamc@34 1429 | SOME ((k, co), subs) => SOME (k, Option.map (sgnSubCon (str, subs)) co))
adamc@34 1430 | SgnError => SOME ((KError, ErrorMsg.dummySpan), SOME (CError, ErrorMsg.dummySpan))
adamc@42 1431 | _ => NONE
adamc@34 1432
adamc@157 1433 fun projectDatatype env {sgn, str, field} =
adamc@157 1434 case #1 (hnormSgn env sgn) of
adamc@157 1435 SgnConst sgis =>
adamc@805 1436 (case sgnSeek (fn SgiDatatype dts =>
adamc@805 1437 (case List.find (fn (x, _, _, _) => x = field) dts of
adamc@805 1438 SOME (_, _, xs, xncs) => SOME (xs, xncs)
adamc@805 1439 | NONE => NONE)
adamc@191 1440 | SgiDatatypeImp (x, _, _, _, _, xs, xncs) => if x = field then SOME (xs, xncs) else NONE
adamc@157 1441 | _ => NONE) sgis of
adamc@157 1442 NONE => NONE
adamc@191 1443 | SOME ((xs, xncs), subs) => SOME (xs,
adamc@191 1444 map (fn (x, n, to) => (x, n, Option.map (sgnSubCon (str, subs)) to)) xncs))
adamc@157 1445 | _ => NONE
adamc@157 1446
adamc@174 1447 fun projectConstructor env {sgn, str, field} =
adamc@174 1448 case #1 (hnormSgn env sgn) of
adamc@174 1449 SgnConst sgis =>
adamc@174 1450 let
adamc@191 1451 fun consider (n, xs, xncs) =
adamc@174 1452 ListUtil.search (fn (x, n', to) =>
adamc@174 1453 if x <> field then
adamc@174 1454 NONE
adamc@174 1455 else
adamc@191 1456 SOME (U.classifyDatatype xncs, n', xs, to, (CNamed n, #2 str))) xncs
adamc@174 1457 in
adamc@805 1458 case sgnSeek (fn SgiDatatype dts =>
adamc@805 1459 let
adamc@805 1460 fun search dts =
adamc@805 1461 case dts of
adamc@805 1462 [] => NONE
adamc@805 1463 | (_, n, xs, xncs) :: dts =>
adamc@805 1464 case consider (n, xs, xncs) of
adamc@805 1465 NONE => search dts
adamc@805 1466 | v => v
adamc@805 1467 in
adamc@805 1468 search dts
adamc@805 1469 end
adamc@191 1470 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => consider (n, xs, xncs)
adamc@174 1471 | _ => NONE) sgis of
adamc@174 1472 NONE => NONE
adamc@191 1473 | SOME ((dk, n, xs, to, t), subs) => SOME (dk, n, xs, Option.map (sgnSubCon (str, subs)) to,
adamc@191 1474 sgnSubCon (str, subs) t)
adamc@174 1475 end
adamc@174 1476 | _ => NONE
adamc@174 1477
adamc@42 1478 fun projectVal env {sgn, str, field} =
adamc@42 1479 case #1 (hnormSgn env sgn) of
adamc@34 1480 SgnConst sgis =>
adamc@162 1481 let
adamc@191 1482 fun seek (n, xs, xncs) =
adamc@162 1483 ListUtil.search (fn (x, _, to) =>
adamc@162 1484 if x = field then
adamc@191 1485 SOME (let
adamc@467 1486 val base = (CNamed n, #2 sgn)
adamc@467 1487 val nxs = length xs
adamc@467 1488 val base = ListUtil.foldli (fn (i, _, base) =>
adamc@467 1489 (CApp (base,
adamc@467 1490 (CRel (nxs - i - 1), #2 sgn)),
adamc@467 1491 #2 sgn))
adamc@467 1492 base xs
adamc@467 1493
adamc@191 1494 val t =
adamc@191 1495 case to of
adamc@467 1496 NONE => base
adamc@467 1497 | SOME t => (TFun (t, base), #2 sgn)
adamc@191 1498 val k = (KType, #2 sgn)
adamc@191 1499 in
adamc@467 1500 foldr (fn (x, t) => (TCFun (Implicit, x, k, t), #2 sgn))
adamc@191 1501 t xs
adamc@191 1502 end)
adamc@162 1503 else
adamc@162 1504 NONE) xncs
adamc@162 1505 in
adamc@162 1506 case sgnSeek (fn SgiVal (x, _, c) => if x = field then SOME c else NONE
adamc@805 1507 | SgiDatatype dts =>
adamc@805 1508 let
adamc@805 1509 fun search dts =
adamc@805 1510 case dts of
adamc@805 1511 [] => NONE
adamc@805 1512 | (_, n, xs, xncs) :: dts =>
adamc@805 1513 case seek (n, xs, xncs) of
adamc@805 1514 NONE => search dts
adamc@805 1515 | v => v
adamc@805 1516 in
adamc@805 1517 search dts
adamc@805 1518 end
adamc@191 1519 | SgiDatatypeImp (_, n, _, _, _, xs, xncs) => seek (n, xs, xncs)
adamc@162 1520 | _ => NONE) sgis of
adamc@162 1521 NONE => NONE
adamc@162 1522 | SOME (c, subs) => SOME (sgnSubCon (str, subs) c)
adamc@162 1523 end
adamc@34 1524 | SgnError => SOME (CError, ErrorMsg.dummySpan)
adamc@42 1525 | _ => NONE
adamc@34 1526
adamc@88 1527 fun sgnSeekConstraints (str, sgis) =
adamc@88 1528 let
adamc@88 1529 fun seek (sgis, sgns, strs, cons, acc) =
adamc@88 1530 case sgis of
adamc@88 1531 [] => acc
adamc@88 1532 | (sgi, _) :: sgis =>
adamc@88 1533 case sgi of
adamc@88 1534 SgiConstraint (c1, c2) =>
adamc@88 1535 let
adamc@88 1536 val sub = sgnSubCon (str, (sgns, strs, cons))
adamc@88 1537 in
adamc@88 1538 seek (sgis, sgns, strs, cons, (sub c1, sub c2) :: acc)
adamc@88 1539 end
adamc@88 1540 | SgiConAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1541 | SgiCon (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@805 1542 | SgiDatatype dts => seek (sgis, sgns, strs,
adamc@805 1543 foldl (fn ((x, n, _, _), cons) => IM.insert (cons, n, x)) cons dts, acc)
adamc@191 1544 | SgiDatatypeImp (x, n, _, _, _, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1545 | SgiVal _ => seek (sgis, sgns, strs, cons, acc)
adamc@88 1546 | SgiSgn (x, n, _) => seek (sgis, IM.insert (sgns, n, x), strs, cons, acc)
adamc@88 1547 | SgiStr (x, n, _) => seek (sgis, sgns, IM.insert (strs, n, x), cons, acc)
adamc@563 1548 | SgiClassAbs (x, n, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@563 1549 | SgiClass (x, n, _, _) => seek (sgis, sgns, strs, IM.insert (cons, n, x), acc)
adamc@88 1550 in
adamc@88 1551 seek (sgis, IM.empty, IM.empty, IM.empty, [])
adamc@88 1552 end
adamc@88 1553
adamc@88 1554 fun projectConstraints env {sgn, str} =
adamc@88 1555 case #1 (hnormSgn env sgn) of
adamc@88 1556 SgnConst sgis => SOME (sgnSeekConstraints (str, sgis))
adamc@88 1557 | SgnError => SOME []
adamc@88 1558 | _ => NONE
adamc@34 1559
adamc@825 1560 fun patBinds env (p, loc) =
adamc@825 1561 case p of
adamc@825 1562 PWild => env
adamc@825 1563 | PVar (x, t) => pushERel env x t
adamc@825 1564 | PPrim _ => env
adamc@825 1565 | PCon (_, _, _, NONE) => env
adamc@825 1566 | PCon (_, _, _, SOME p) => patBinds env p
adamc@825 1567 | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps
adamc@825 1568
adamc@1272 1569 fun patBindsN (p, _) =
adamc@1272 1570 case p of
adamc@1272 1571 PWild => 0
adamc@1272 1572 | PVar _ => 1
adamc@1272 1573 | PPrim _ => 0
adamc@1272 1574 | PCon (_, _, _, NONE) => 0
adamc@1272 1575 | PCon (_, _, _, SOME p) => patBindsN p
adamc@1272 1576 | PRecord xps => foldl (fn ((_, p, _), n) => patBindsN p + n) 0 xps
adamc@1272 1577
adamc@447 1578 fun edeclBinds env (d, loc) =
adamc@447 1579 case d of
adamc@825 1580 EDVal (p, _, _) => patBinds env p
adamc@447 1581 | EDValRec vis => foldl (fn ((x, t, _), env) => pushERel env x t) env vis
adamc@447 1582
adamc@157 1583 fun declBinds env (d, loc) =
adamc@157 1584 case d of
adamc@157 1585 DCon (x, n, k, c) => pushCNamedAs env x n k (SOME c)
adamc@805 1586 | DDatatype dts =>
adamc@157 1587 let
adamc@805 1588 fun doOne ((x, n, xs, xncs), env) =
adamc@805 1589 let
adamc@805 1590 val k = (KType, loc)
adamc@805 1591 val nxs = length xs
adamc@805 1592 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@805 1593 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@805 1594 (KArrow (k, kb), loc)))
adamc@805 1595 ((CNamed n, loc), k) xs
adamc@805 1596
adamc@805 1597 val env = pushCNamedAs env x n kb NONE
adamc@805 1598 val env = pushDatatype env n xs xncs
adamc@805 1599 in
adamc@805 1600 foldl (fn ((x', n', to), env) =>
adamc@805 1601 let
adamc@805 1602 val t =
adamc@805 1603 case to of
adamc@805 1604 NONE => tb
adamc@805 1605 | SOME t => (TFun (t, tb), loc)
adamc@805 1606 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@805 1607 in
adamc@805 1608 pushENamedAs env x' n' t
adamc@805 1609 end)
adamc@805 1610 env xncs
adamc@805 1611 end
adamc@157 1612 in
adamc@805 1613 foldl doOne env dts
adamc@157 1614 end
adamc@191 1615 | DDatatypeImp (x, n, m, ms, x', xs, xncs) =>
adamc@157 1616 let
adamc@157 1617 val t = (CModProj (m, ms, x'), loc)
adamc@297 1618 val k = (KType, loc)
adamc@297 1619 val nxs = length xs
adamc@297 1620 val (tb, kb) = ListUtil.foldli (fn (i, x', (tb, kb)) =>
adamc@297 1621 ((CApp (tb, (CRel (nxs - i - 1), loc)), loc),
adamc@297 1622 (KArrow (k, kb), loc)))
adamc@297 1623 ((CNamed n, loc), k) xs
adamc@297 1624
adamc@341 1625 val env = pushCNamedAs env x n kb (SOME t)
adamc@191 1626 val env = pushDatatype env n xs xncs
adamc@157 1627 in
adamc@191 1628 foldl (fn ((x', n', to), env) =>
adamc@191 1629 let
adamc@191 1630 val t =
adamc@191 1631 case to of
adamc@297 1632 NONE => tb
adamc@297 1633 | SOME t => (TFun (t, tb), loc)
adamc@297 1634 val t = foldr (fn (x, t) => (TCFun (Implicit, x, k, t), loc)) t xs
adamc@191 1635 in
adamc@191 1636 pushENamedAs env x' n' t
adamc@191 1637 end)
adamc@191 1638 env xncs
adamc@157 1639 end
adamc@157 1640 | DVal (x, n, t, _) => pushENamedAs env x n t
adamc@157 1641 | DValRec vis => foldl (fn ((x, n, t, _), env) => pushENamedAs env x n t) env vis
adamc@157 1642 | DSgn (x, n, sgn) => pushSgnNamedAs env x n sgn
adamc@157 1643 | DStr (x, n, sgn, _) => pushStrNamedAs env x n sgn
adamc@157 1644 | DFfiStr (x, n, sgn) => pushStrNamedAs env x n sgn
adamc@157 1645 | DConstraint _ => env
adamc@157 1646 | DExport _ => env
adamc@707 1647 | DTable (tn, x, n, c, _, pc, _, cc) =>
adamc@205 1648 let
adamc@705 1649 val ct = (CModProj (tn, [], "sql_table"), loc)
adamc@705 1650 val ct = (CApp (ct, c), loc)
adamc@707 1651 val ct = (CApp (ct, (CConcat (pc, cc), loc)), loc)
adamc@338 1652 in
adamc@705 1653 pushENamedAs env x n ct
adamc@338 1654 end
adamc@338 1655 | DSequence (tn, x, n) =>
adamc@338 1656 let
adamc@338 1657 val t = (CModProj (tn, [], "sql_sequence"), loc)
adamc@205 1658 in
adamc@205 1659 pushENamedAs env x n t
adamc@205 1660 end
adamc@754 1661 | DView (tn, x, n, _, c) =>
adamc@754 1662 let
adamc@754 1663 val ct = (CModProj (tn, [], "sql_view"), loc)
adamc@754 1664 val ct = (CApp (ct, c), loc)
adamc@754 1665 in
adamc@754 1666 pushENamedAs env x n ct
adamc@754 1667 end
adamc@271 1668 | DDatabase _ => env
adamc@459 1669 | DCookie (tn, x, n, c) =>
adamc@459 1670 let
adamc@459 1671 val t = (CApp ((CModProj (tn, [], "cookie"), loc), c), loc)
adamc@459 1672 in
adamc@459 1673 pushENamedAs env x n t
adamc@459 1674 end
adamc@720 1675 | DStyle (tn, x, n) =>
adamc@718 1676 let
adamc@720 1677 val t = (CModProj (tn, [], "css_class"), loc)
adamc@718 1678 in
adamc@718 1679 pushENamedAs env x n t
adamc@718 1680 end
adamc@1075 1681 | DTask _ => env
adamc@1199 1682 | DPolicy _ => env
adam@1294 1683 | DOnError _ => env
adamc@157 1684
adamc@2 1685 end