adamc@2
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@2
|
2 * All rights reserved.
|
adamc@2
|
3 *
|
adamc@2
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@2
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@2
|
6 *
|
adamc@2
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@2
|
8 * this list of conditions and the following disclaimer.
|
adamc@2
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@2
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@2
|
11 * and/or other materials provided with the distribution.
|
adamc@2
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@2
|
13 * derived from this software without specific prior written permission.
|
adamc@2
|
14 *
|
adamc@2
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@2
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@2
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@2
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@2
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@2
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@2
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@2
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@2
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@2
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@2
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@2
|
26 *)
|
adamc@2
|
27
|
adamc@2
|
28 structure ElabEnv :> ELAB_ENV = struct
|
adamc@2
|
29
|
adamc@2
|
30 open Elab
|
adamc@2
|
31
|
adamc@2
|
32 structure IM = IntBinaryMap
|
adamc@2
|
33 structure SM = BinaryMapFn(struct
|
adamc@2
|
34 type ord_key = string
|
adamc@2
|
35 val compare = String.compare
|
adamc@2
|
36 end)
|
adamc@2
|
37
|
adamc@2
|
38 exception UnboundRel of int
|
adamc@2
|
39 exception UnboundNamed of int
|
adamc@2
|
40
|
adamc@9
|
41 datatype 'a var' =
|
adamc@9
|
42 Rel' of int * 'a
|
adamc@9
|
43 | Named' of int * 'a
|
adamc@2
|
44
|
adamc@9
|
45 datatype 'a var =
|
adamc@9
|
46 NotBound
|
adamc@9
|
47 | Rel of int * 'a
|
adamc@9
|
48 | Named of int * 'a
|
adamc@2
|
49
|
adamc@2
|
50 type env = {
|
adamc@9
|
51 renameC : kind var' SM.map,
|
adamc@2
|
52 relC : (string * kind) list,
|
adamc@11
|
53 namedC : (string * kind * con option) IM.map,
|
adamc@9
|
54
|
adamc@9
|
55 renameE : con var' SM.map,
|
adamc@9
|
56 relE : (string * con) list,
|
adamc@9
|
57 namedE : (string * con) IM.map
|
adamc@2
|
58 }
|
adamc@2
|
59
|
adamc@2
|
60 val namedCounter = ref 0
|
adamc@2
|
61
|
adamc@2
|
62 val empty = {
|
adamc@2
|
63 renameC = SM.empty,
|
adamc@2
|
64 relC = [],
|
adamc@9
|
65 namedC = IM.empty,
|
adamc@9
|
66
|
adamc@9
|
67 renameE = SM.empty,
|
adamc@9
|
68 relE = [],
|
adamc@9
|
69 namedE = IM.empty
|
adamc@2
|
70 }
|
adamc@2
|
71
|
adamc@2
|
72 fun pushCRel (env : env) x k =
|
adamc@2
|
73 let
|
adamc@9
|
74 val renameC = SM.map (fn Rel' (n, k) => Rel' (n+1, k)
|
adamc@2
|
75 | x => x) (#renameC env)
|
adamc@2
|
76 in
|
adamc@9
|
77 {renameC = SM.insert (renameC, x, Rel' (0, k)),
|
adamc@2
|
78 relC = (x, k) :: #relC env,
|
adamc@9
|
79 namedC = #namedC env,
|
adamc@9
|
80
|
adamc@9
|
81 renameE = #renameE env,
|
adamc@9
|
82 relE = #relE env,
|
adamc@9
|
83 namedE = #namedE env}
|
adamc@2
|
84 end
|
adamc@2
|
85
|
adamc@2
|
86 fun lookupCRel (env : env) n =
|
adamc@2
|
87 (List.nth (#relC env, n))
|
adamc@2
|
88 handle Subscript => raise UnboundRel n
|
adamc@2
|
89
|
adamc@11
|
90 fun pushCNamedAs (env : env) x n k co =
|
adamc@9
|
91 {renameC = SM.insert (#renameC env, x, Named' (n, k)),
|
adamc@5
|
92 relC = #relC env,
|
adamc@11
|
93 namedC = IM.insert (#namedC env, n, (x, k, co)),
|
adamc@9
|
94
|
adamc@9
|
95 renameE = #renameE env,
|
adamc@9
|
96 relE = #relE env,
|
adamc@9
|
97 namedE = #namedE env}
|
adamc@5
|
98
|
adamc@11
|
99 fun pushCNamed env x k co =
|
adamc@2
|
100 let
|
adamc@2
|
101 val n = !namedCounter
|
adamc@2
|
102 in
|
adamc@2
|
103 namedCounter := n + 1;
|
adamc@11
|
104 (pushCNamedAs env x n k co, n)
|
adamc@2
|
105 end
|
adamc@2
|
106
|
adamc@2
|
107 fun lookupCNamed (env : env) n =
|
adamc@2
|
108 case IM.find (#namedC env, n) of
|
adamc@2
|
109 NONE => raise UnboundNamed n
|
adamc@2
|
110 | SOME x => x
|
adamc@2
|
111
|
adamc@2
|
112 fun lookupC (env : env) x =
|
adamc@2
|
113 case SM.find (#renameC env, x) of
|
adamc@9
|
114 NONE => NotBound
|
adamc@9
|
115 | SOME (Rel' x) => Rel x
|
adamc@9
|
116 | SOME (Named' x) => Named x
|
adamc@9
|
117
|
adamc@9
|
118 fun pushERel (env : env) x t =
|
adamc@9
|
119 let
|
adamc@9
|
120 val renameE = SM.map (fn Rel' (n, t) => Rel' (n+1, t)
|
adamc@9
|
121 | x => x) (#renameE env)
|
adamc@9
|
122 in
|
adamc@9
|
123 {renameC = #renameC env,
|
adamc@9
|
124 relC = #relC env,
|
adamc@9
|
125 namedC = #namedC env,
|
adamc@9
|
126
|
adamc@9
|
127 renameE = SM.insert (renameE, x, Rel' (0, t)),
|
adamc@9
|
128 relE = (x, t) :: #relE env,
|
adamc@9
|
129 namedE = #namedE env}
|
adamc@9
|
130 end
|
adamc@9
|
131
|
adamc@9
|
132 fun lookupERel (env : env) n =
|
adamc@9
|
133 (List.nth (#relE env, n))
|
adamc@9
|
134 handle Subscript => raise UnboundRel n
|
adamc@9
|
135
|
adamc@9
|
136 fun pushENamedAs (env : env) x n t =
|
adamc@9
|
137 {renameC = #renameC env,
|
adamc@9
|
138 relC = #relC env,
|
adamc@9
|
139 namedC = #namedC env,
|
adamc@9
|
140
|
adamc@9
|
141 renameE = SM.insert (#renameE env, x, Named' (n, t)),
|
adamc@9
|
142 relE = #relE env,
|
adamc@9
|
143 namedE = IM.insert (#namedE env, n, (x, t))}
|
adamc@9
|
144
|
adamc@9
|
145 fun pushENamed env x t =
|
adamc@9
|
146 let
|
adamc@9
|
147 val n = !namedCounter
|
adamc@9
|
148 in
|
adamc@9
|
149 namedCounter := n + 1;
|
adamc@9
|
150 (pushENamedAs env x n t, n)
|
adamc@9
|
151 end
|
adamc@9
|
152
|
adamc@9
|
153 fun lookupENamed (env : env) n =
|
adamc@9
|
154 case IM.find (#namedE env, n) of
|
adamc@9
|
155 NONE => raise UnboundNamed n
|
adamc@9
|
156 | SOME x => x
|
adamc@9
|
157
|
adamc@9
|
158 fun lookupE (env : env) x =
|
adamc@9
|
159 case SM.find (#renameE env, x) of
|
adamc@9
|
160 NONE => NotBound
|
adamc@9
|
161 | SOME (Rel' x) => Rel x
|
adamc@9
|
162 | SOME (Named' x) => Named x
|
adamc@2
|
163
|
adamc@2
|
164 end
|