annotate src/elab_env.sml @ 2196:100352dbae36

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