adam@1989
|
1 (* Copyright (c) 2014, Adam Chlipala
|
adam@1989
|
2 * All rights reserved.
|
adam@1989
|
3 *
|
adam@1989
|
4 * Redistribution and use in source and binary forms, with or without
|
adam@1989
|
5 * modification, are permitted provided that the following conditions are met:
|
adam@1989
|
6 *
|
adam@1989
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adam@1989
|
8 * this list of conditions and the following disclaimer.
|
adam@1989
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adam@1989
|
10 * this list of conditions and the following disclaimer in the documentation
|
adam@1989
|
11 * and/or other materials provided with the distribution.
|
adam@1989
|
12 * - The names of contributors may not be used to endorse or promote products
|
adam@1989
|
13 * derived from this software without specific prior written permission.
|
adam@1989
|
14 *
|
adam@1989
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adam@1989
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adam@1989
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adam@1989
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adam@1989
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adam@1989
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adam@1989
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adam@1989
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adam@1989
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adam@1989
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adam@1989
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adam@1989
|
26 *)
|
adam@1989
|
27
|
adam@1989
|
28 structure ExplRename :> EXPL_RENAME = struct
|
adam@1989
|
29
|
adam@1989
|
30 open Expl
|
adam@1989
|
31 structure E = ExplEnv
|
adam@1989
|
32
|
adam@1989
|
33 structure IM = IntBinaryMap
|
adam@1989
|
34
|
adam@1989
|
35 structure St :> sig
|
adam@1989
|
36 type t
|
adam@1989
|
37
|
adam@1989
|
38 val create : int -> t
|
adam@1989
|
39 val next : t -> int
|
adam@1989
|
40
|
adam@1989
|
41 val bind : t * int -> t * int
|
adam@1989
|
42 val lookup: t * int -> int option
|
adam@1989
|
43 end = struct
|
adam@1989
|
44
|
adam@1989
|
45 type t = {next : int,
|
adam@1989
|
46 renaming : int IM.map}
|
adam@1989
|
47
|
adam@1989
|
48 fun create next = {next = next,
|
adam@1989
|
49 renaming = IM.empty}
|
adam@1989
|
50
|
adam@1989
|
51 fun next (t : t) = #next t
|
adam@1989
|
52
|
adam@1989
|
53 fun bind ({next, renaming}, n) =
|
adam@1989
|
54 ({next = next + 1,
|
adam@1989
|
55 renaming = IM.insert (renaming, n, next)}, next)
|
adam@1989
|
56
|
adam@1989
|
57 fun lookup ({next, renaming}, n) =
|
adam@1989
|
58 IM.find (renaming, n)
|
adam@1989
|
59
|
adam@1989
|
60 end
|
adam@1989
|
61
|
adam@1989
|
62 fun renameCon st (all as (c, loc)) =
|
adam@1989
|
63 case c of
|
adam@1989
|
64 TFun (c1, c2) => (TFun (renameCon st c1, renameCon st c2), loc)
|
adam@1989
|
65 | TCFun (x, k, c) => (TCFun (x, k, renameCon st c), loc)
|
adam@1989
|
66 | TRecord c => (TRecord (renameCon st c), loc)
|
adam@1989
|
67 | CRel _ => all
|
adam@1989
|
68 | CNamed n =>
|
adam@1989
|
69 (case St.lookup (st, n) of
|
adam@1989
|
70 NONE => all
|
adam@1989
|
71 | SOME n' => (CNamed n', loc))
|
adam@1989
|
72 | CModProj (n, ms, x) =>
|
adam@1989
|
73 (case St.lookup (st, n) of
|
adam@1989
|
74 NONE => all
|
adam@1989
|
75 | SOME n' => (CModProj (n', ms, x), loc))
|
adam@1989
|
76 | CApp (c1, c2) => (CApp (renameCon st c1, renameCon st c2), loc)
|
adam@1989
|
77 | CAbs (x, k, c) => (CAbs (x, k, renameCon st c), loc)
|
adam@1989
|
78 | CKAbs (x, c) => (CKAbs (x, renameCon st c), loc)
|
adam@1989
|
79 | CKApp (c, k) => (CKApp (renameCon st c, k), loc)
|
adam@1989
|
80 | TKFun (x, c) => (TKFun (x, renameCon st c), loc)
|
adam@1989
|
81 | CName _ => all
|
adam@1989
|
82 | CRecord (k, xcs) => (CRecord (k, map (fn (x, c) => (renameCon st x, renameCon st c)) xcs), loc)
|
adam@1989
|
83 | CConcat (c1, c2) => (CConcat (renameCon st c1, renameCon st c2), loc)
|
adam@1989
|
84 | CMap _ => all
|
adam@1989
|
85 | CUnit => all
|
adam@1989
|
86 | CTuple cs => (CTuple (map (renameCon st) cs), loc)
|
adam@1989
|
87 | CProj (c, n) => (CProj (renameCon st c, n), loc)
|
adam@1989
|
88
|
adam@1989
|
89 fun renamePatCon st pc =
|
adam@1989
|
90 case pc of
|
adam@1989
|
91 PConVar n =>
|
adam@1989
|
92 (case St.lookup (st, n) of
|
adam@1989
|
93 NONE => pc
|
adam@1989
|
94 | SOME n' => PConVar n')
|
adam@1989
|
95 | PConProj (n, ms, x) =>
|
adam@1989
|
96 (case St.lookup (st, n) of
|
adam@1989
|
97 NONE => pc
|
adam@1989
|
98 | SOME n' => PConProj (n', ms, x))
|
adam@1989
|
99
|
adam@1989
|
100 fun renamePat st (all as (p, loc)) =
|
adam@1989
|
101 case p of
|
adam@1989
|
102 PWild => all
|
adam@1989
|
103 | PVar (x, c) => (PVar (x, renameCon st c), loc)
|
adam@1989
|
104 | PPrim _ => all
|
adam@1989
|
105 | PCon (dk, pc, cs, po) => (PCon (dk, renamePatCon st pc,
|
adam@1989
|
106 map (renameCon st) cs,
|
adam@1989
|
107 Option.map (renamePat st) po), loc)
|
adam@1989
|
108 | PRecord xpcs => (PRecord (map (fn (x, p, c) =>
|
adam@1989
|
109 (x, renamePat st p, renameCon st c)) xpcs), loc)
|
adam@1989
|
110
|
adam@1989
|
111 fun renameExp st (all as (e, loc)) =
|
adam@1989
|
112 case e of
|
adam@1989
|
113 EPrim _ => all
|
adam@1989
|
114 | ERel _ => all
|
adam@1989
|
115 | ENamed n =>
|
adam@1989
|
116 (case St.lookup (st, n) of
|
adam@1989
|
117 NONE => all
|
adam@1989
|
118 | SOME n' => (ENamed n', loc))
|
adam@1989
|
119 | EModProj (n, ms, x) =>
|
adam@1989
|
120 (case St.lookup (st, n) of
|
adam@1989
|
121 NONE => all
|
adam@1989
|
122 | SOME n' => (EModProj (n', ms, x), loc))
|
adam@1989
|
123 | EApp (e1, e2) => (EApp (renameExp st e1, renameExp st e2), loc)
|
adam@1989
|
124 | EAbs (x, dom, ran, e) => (EAbs (x, renameCon st dom, renameCon st ran, renameExp st e), loc)
|
adam@1989
|
125 | ECApp (e, c) => (ECApp (renameExp st e, renameCon st c), loc)
|
adam@1989
|
126 | ECAbs (x, k, e) => (ECAbs (x, k, renameExp st e), loc)
|
adam@1989
|
127 | EKAbs (x, e) => (EKAbs (x, renameExp st e), loc)
|
adam@1989
|
128 | EKApp (e, k) => (EKApp (renameExp st e, k), loc)
|
adam@1989
|
129 | ERecord xecs => (ERecord (map (fn (x, e, c) => (renameCon st x,
|
adam@1989
|
130 renameExp st e,
|
adam@1989
|
131 renameCon st c)) xecs), loc)
|
adam@1989
|
132 | EField (e, c, {field, rest}) => (EField (renameExp st e,
|
adam@1989
|
133 renameCon st c,
|
adam@1989
|
134 {field = renameCon st field,
|
adam@1989
|
135 rest = renameCon st rest}), loc)
|
adam@1989
|
136 | EConcat (e1, c1, e2, c2) => (EConcat (renameExp st e1,
|
adam@1989
|
137 renameCon st c1,
|
adam@1989
|
138 renameExp st e2,
|
adam@1989
|
139 renameCon st c2), loc)
|
adam@1989
|
140 | ECut (e, c, {field, rest}) => (ECut (renameExp st e,
|
adam@1989
|
141 renameCon st c,
|
adam@1989
|
142 {field = renameCon st field,
|
adam@1989
|
143 rest = renameCon st rest}), loc)
|
adam@1989
|
144 | ECutMulti (e, c, {rest}) => (ECutMulti (renameExp st e,
|
adam@1989
|
145 renameCon st c,
|
adam@1989
|
146 {rest = renameCon st rest}), loc)
|
adam@1989
|
147 | ECase (e, pes, {disc, result}) => (ECase (renameExp st e,
|
adam@1989
|
148 map (fn (p, e) => (renamePat st p, renameExp st e)) pes,
|
adam@1989
|
149 {disc = renameCon st disc,
|
adam@1989
|
150 result = renameCon st result}), loc)
|
adam@1989
|
151 | EWrite e => (EWrite (renameExp st e), loc)
|
adam@1989
|
152 | ELet (x, c1, e1, e2) => (ELet (x, renameCon st c1,
|
adam@1989
|
153 renameExp st e1,
|
adam@1989
|
154 renameExp st e2), loc)
|
adam@1989
|
155
|
adam@1989
|
156 fun renameSitem st (all as (si, loc)) =
|
adam@1989
|
157 case si of
|
adam@1989
|
158 SgiConAbs _ => all
|
adam@1989
|
159 | SgiCon (x, n, k, c) => (SgiCon (x, n, k, renameCon st c), loc)
|
adam@1989
|
160 | SgiDatatype dts => (SgiDatatype (map (fn (x, n, xs, cns) =>
|
adam@1989
|
161 (x, n, xs,
|
adam@1989
|
162 map (fn (x, n, co) =>
|
adam@1989
|
163 (x, n, Option.map (renameCon st) co)) cns)) dts),
|
adam@1989
|
164 loc)
|
adam@1989
|
165 | SgiDatatypeImp (x, n, n', xs, x', xs', cns) =>
|
adam@1989
|
166 (SgiDatatypeImp (x, n, n', xs, x', xs',
|
adam@1989
|
167 map (fn (x, n, co) =>
|
adam@1989
|
168 (x, n, Option.map (renameCon st) co)) cns), loc)
|
adam@1989
|
169 | SgiVal (x, n, c) => (SgiVal (x, n, renameCon st c), loc)
|
adam@1989
|
170 | SgiSgn (x, n, sg) => (SgiSgn (x, n, renameSgn st sg), loc)
|
adam@1989
|
171 | SgiStr (x, n, sg) => (SgiStr (x, n, renameSgn st sg), loc)
|
adam@1989
|
172
|
adam@1989
|
173 and renameSgn st (all as (sg, loc)) =
|
adam@1989
|
174 case sg of
|
adam@1989
|
175 SgnConst sis => (SgnConst (map (renameSitem st) sis), loc)
|
adam@1989
|
176 | SgnVar n =>
|
adam@1989
|
177 (case St.lookup (st, n) of
|
adam@1989
|
178 NONE => all
|
adam@1989
|
179 | SOME n' => (SgnVar n', loc))
|
adam@1989
|
180 | SgnFun (x, n, dom, ran) => (SgnFun (x, n, renameSgn st dom, renameSgn st ran), loc)
|
adam@1989
|
181 | SgnWhere (sg, xs, s, c) => (SgnWhere (renameSgn st sg, xs, s, renameCon st c), loc)
|
adam@1989
|
182 | SgnProj (n, ms, x) =>
|
adam@1989
|
183 (case St.lookup (st, n) of
|
adam@1989
|
184 NONE => all
|
adam@1989
|
185 | SOME n' => (SgnProj (n', ms, x), loc))
|
adam@1989
|
186
|
adam@1989
|
187 fun renameDecl st (all as (d, loc)) =
|
adam@1989
|
188 case d of
|
adam@1989
|
189 DCon (x, n, k, c) => (DCon (x, n, k, renameCon st c), loc)
|
adam@1989
|
190 | DDatatype dts => (DDatatype (map (fn (x, n, xs, cns) =>
|
adam@1989
|
191 (x, n, xs,
|
adam@1989
|
192 map (fn (x, n, co) =>
|
adam@1989
|
193 (x, n, Option.map (renameCon st) co)) cns)) dts),
|
adam@1989
|
194 loc)
|
adam@1989
|
195 | DDatatypeImp (x, n, n', xs, x', xs', cns) =>
|
adam@1989
|
196 (DDatatypeImp (x, n, n', xs, x', xs',
|
adam@1989
|
197 map (fn (x, n, co) =>
|
adam@1989
|
198 (x, n, Option.map (renameCon st) co)) cns), loc)
|
adam@1989
|
199 | DVal (x, n, c, e) => (DVal (x, n, renameCon st c, renameExp st e), loc)
|
adam@1989
|
200 | DValRec vis => (DValRec (map (fn (x, n, c, e) => (x, n, renameCon st c, renameExp st e)) vis), loc)
|
adam@1989
|
201 | DSgn (x, n, sg) => (DSgn (x, n, renameSgn st sg), loc)
|
adam@1989
|
202 | DStr (x, n, sg, str) => (DStr (x, n, renameSgn st sg, renameStr st str), loc)
|
adam@1989
|
203 | DFfiStr (x, n, sg) => (DFfiStr (x, n, renameSgn st sg), loc)
|
adam@1989
|
204 | DExport (n, sg, str) =>
|
adam@1989
|
205 (case St.lookup (st, n) of
|
adam@1989
|
206 NONE => all
|
adam@1989
|
207 | SOME n' => (DExport (n', renameSgn st sg, renameStr st str), loc))
|
adam@1989
|
208 | DTable (n, x, m, c1, e1, c2, e2, c3) =>
|
adam@1989
|
209 (DTable (n, x, m, renameCon st c1, renameExp st e1, renameCon st c2,
|
adam@1989
|
210 renameExp st e2, renameCon st c3), loc)
|
adam@1989
|
211 | DSequence _ => all
|
adam@1989
|
212 | DView (n, x, n', e, c) => (DView (n, x, n', renameExp st e, renameCon st c), loc)
|
adam@1989
|
213 | DDatabase _ => all
|
adam@1989
|
214 | DCookie (n, x, n', c) => (DCookie (n, x, n', renameCon st c), loc)
|
adam@1989
|
215 | DStyle _ => all
|
adam@1989
|
216 | DTask (e1, e2) => (DTask (renameExp st e1, renameExp st e2), loc)
|
adam@1989
|
217 | DPolicy e => (DPolicy (renameExp st e), loc)
|
adam@1989
|
218 | DOnError (n, xs, x) =>
|
adam@1989
|
219 (case St.lookup (st, n) of
|
adam@1989
|
220 NONE => all
|
adam@1989
|
221 | SOME n' => (DOnError (n', xs, x), loc))
|
adam@1989
|
222
|
adam@1989
|
223 and renameStr st (all as (str, loc)) =
|
adam@1989
|
224 case str of
|
adam@1989
|
225 StrConst ds => (StrConst (map (renameDecl st) ds), loc)
|
adam@1989
|
226 | StrVar n =>
|
adam@1989
|
227 (case St.lookup (st, n) of
|
adam@1989
|
228 NONE => all
|
adam@1989
|
229 | SOME n' => (StrVar n', loc))
|
adam@1989
|
230 | StrProj (str, x) => (StrProj (renameStr st str, x), loc)
|
adam@1989
|
231 | StrFun (x, n, dom, ran, str) => (StrFun (x, n, renameSgn st dom,
|
adam@1989
|
232 renameSgn st ran,
|
adam@1989
|
233 renameStr st str), loc)
|
adam@1989
|
234 | StrApp (str1, str2) => (StrApp (renameStr st str1, renameStr st str2), loc)
|
adam@1989
|
235
|
adam@1989
|
236
|
adam@1989
|
237
|
adam@1989
|
238 fun fromArity (n, loc) =
|
adam@1989
|
239 case n of
|
adam@1989
|
240 0 => (KType, loc)
|
adam@1989
|
241 | _ => (KArrow ((KType, loc), fromArity (n - 1, loc)), loc)
|
adam@1989
|
242
|
adam@1989
|
243 fun dupDecl (all as (d, loc), st) =
|
adam@1989
|
244 case d of
|
adam@1989
|
245 DCon (x, n, k, c) =>
|
adam@1989
|
246 let
|
adam@1989
|
247 val (st, n') = St.bind (st, n)
|
adam@1989
|
248 in
|
adam@1989
|
249 ([(DCon (x, n, k, renameCon st c), loc),
|
adam@1989
|
250 (DCon (x, n', k, (CNamed n, loc)), loc)],
|
adam@1989
|
251 st)
|
adam@1989
|
252 end
|
adam@1989
|
253 | DDatatype dts =>
|
adam@1989
|
254 let
|
adam@1990
|
255 val d = (DDatatype (map (fn (x, n, xs, cns) =>
|
adam@1990
|
256 (x, n, xs,
|
adam@1990
|
257 map (fn (x, n, co) =>
|
adam@1990
|
258 (x, n, Option.map (renameCon st) co)) cns)) dts),
|
adam@1990
|
259 loc)
|
adam@1990
|
260
|
adam@1989
|
261 val (dts', st) = ListUtil.foldlMap (fn ((x, n, xs, cns), st) =>
|
adam@1989
|
262 let
|
adam@1989
|
263 val (st, n') = St.bind (st, n)
|
adam@1989
|
264
|
adam@1989
|
265 val (cns', st) = ListUtil.foldlMap
|
adam@1989
|
266 (fn ((x, n, _), st) =>
|
adam@1989
|
267 let
|
adam@1989
|
268 val (st, n') =
|
adam@1989
|
269 St.bind (st, n)
|
adam@1989
|
270 in
|
adam@1989
|
271 ((x, n, n'), st)
|
adam@1989
|
272 end) st cns
|
adam@1989
|
273 in
|
adam@1989
|
274 ((x, n, length xs, n', cns'), st)
|
adam@1989
|
275 end) st dts
|
adam@1989
|
276
|
adam@1989
|
277 val env = E.declBinds E.empty d
|
adam@1989
|
278 in
|
adam@1989
|
279 (d
|
adam@1989
|
280 :: map (fn (x, n, arity, n', _) =>
|
adam@1989
|
281 (DCon (x, n', fromArity (arity, loc), (CNamed n, loc)), loc)) dts'
|
adam@1989
|
282 @ ListUtil.mapConcat (fn (_, _, _, _, cns') =>
|
adam@1989
|
283 map (fn (x, n, n') =>
|
adam@1989
|
284 (DVal (x, n', #2 (E.lookupENamed env n), (ENamed n, loc)),
|
adam@1989
|
285 loc)) cns') dts',
|
adam@1989
|
286 st)
|
adam@1989
|
287 end
|
adam@1989
|
288 | DDatatypeImp (x, n, n', xs, x', xs', cns) =>
|
adam@1989
|
289 let
|
adam@1990
|
290 val d = (DDatatypeImp (x, n, n', xs, x', xs',
|
adam@1990
|
291 map (fn (x, n, co) =>
|
adam@1990
|
292 (x, n, Option.map (renameCon st) co)) cns), loc)
|
adam@1990
|
293
|
adam@1989
|
294 val (cns', st) = ListUtil.foldlMap
|
adam@1989
|
295 (fn ((x, n, _), st) =>
|
adam@1989
|
296 let
|
adam@1989
|
297 val (st, n') =
|
adam@1989
|
298 St.bind (st, n)
|
adam@1989
|
299 in
|
adam@1989
|
300 ((x, n, n'), st)
|
adam@1989
|
301 end) st cns
|
adam@1989
|
302
|
adam@1989
|
303 val (st, n') = St.bind (st, n)
|
adam@1989
|
304
|
adam@1989
|
305 val env = E.declBinds E.empty d
|
adam@1989
|
306 in
|
adam@1989
|
307 (d
|
adam@1989
|
308 :: (DCon (x, n', fromArity (length xs, loc), (CNamed n, loc)), loc)
|
adam@1989
|
309 :: map (fn (x, n, n') =>
|
adam@1989
|
310 (DVal (x, n', #2 (E.lookupENamed env n), (ENamed n, loc)),
|
adam@1989
|
311 loc)) cns',
|
adam@1989
|
312 st)
|
adam@1989
|
313 end
|
adam@1989
|
314 | DVal (x, n, c, e) =>
|
adam@1989
|
315 let
|
adam@1989
|
316 val (st, n') = St.bind (st, n)
|
adam@1989
|
317 val c' = renameCon st c
|
adam@1989
|
318 in
|
adam@1989
|
319 ([(DVal (x, n, c', renameExp st e), loc),
|
adam@1989
|
320 (DVal (x, n', c', (ENamed n, loc)), loc)],
|
adam@1989
|
321 st)
|
adam@1989
|
322 end
|
adam@1989
|
323 | DValRec vis =>
|
adam@1989
|
324 let
|
adam@1989
|
325 val d = (DValRec (map (fn (x, n, c, e) => (x, n, renameCon st c, renameExp st e)) vis), loc)
|
adam@1989
|
326
|
adam@1989
|
327 val (vis', st) = ListUtil.foldlMap (fn ((x, n, _, _), st) =>
|
adam@1989
|
328 let
|
adam@1989
|
329 val (st, n') = St.bind (st, n)
|
adam@1989
|
330 in
|
adam@1989
|
331 ((x, n, n'), st)
|
adam@1989
|
332 end) st vis
|
adam@1989
|
333
|
adam@1989
|
334 val env = E.declBinds E.empty d
|
adam@1989
|
335 in
|
adam@1989
|
336 (d
|
adam@1989
|
337 :: map (fn (x, n, n') => (DVal (x, n', #2 (E.lookupENamed env n), (ENamed n, loc)), loc)) vis',
|
adam@1989
|
338 st)
|
adam@1989
|
339 end
|
adam@1989
|
340 | DSgn (x, n, sg) =>
|
adam@1989
|
341 let
|
adam@1989
|
342 val (st, n') = St.bind (st, n)
|
adam@1989
|
343 in
|
adam@1989
|
344 ([(DSgn (x, n, renameSgn st sg), loc),
|
adam@1989
|
345 (DSgn (x, n', (SgnVar n, loc)), loc)],
|
adam@1989
|
346 st)
|
adam@1989
|
347 end
|
adam@1989
|
348 | DStr (x, n, sg, str) =>
|
adam@1989
|
349 let
|
adam@1989
|
350 val (st, n') = St.bind (st, n)
|
adam@1989
|
351 val sg' = renameSgn st sg
|
adam@1989
|
352 in
|
adam@1989
|
353 ([(DStr (x, n, sg', renameStr st str), loc),
|
adam@1989
|
354 (DStr (x, n', sg', (StrVar n, loc)), loc)],
|
adam@1989
|
355 st)
|
adam@1989
|
356 end
|
adam@1989
|
357 | DFfiStr (x, n, sg) => ([(DFfiStr (x, n, renameSgn st sg), loc)], st)
|
adam@1989
|
358 | DExport (n, sg, str) =>
|
adam@1989
|
359 (case St.lookup (st, n) of
|
adam@1989
|
360 NONE => ([all], st)
|
adam@1989
|
361 | SOME n' => ([(DExport (n', renameSgn st sg, renameStr st str), loc)], st))
|
adam@1989
|
362 | DTable (n, x, m, c1, e1, c2, e2, c3) =>
|
adam@1989
|
363 let
|
adam@1989
|
364 val (st, m') = St.bind (st, m)
|
adam@1989
|
365
|
adam@1989
|
366 val d = (DTable (n, x, m, renameCon st c1, renameExp st e1, renameCon st c2,
|
adam@1989
|
367 renameExp st e2, renameCon st c3), loc)
|
adam@1989
|
368
|
adam@1989
|
369 val env = E.declBinds E.empty d
|
adam@1989
|
370 in
|
adam@1989
|
371 ([d, (DVal (x, m', #2 (E.lookupENamed env m), (ENamed m, loc)), loc)], st)
|
adam@1989
|
372 end
|
adam@1989
|
373 | DSequence (n, x, m) =>
|
adam@1989
|
374 let
|
adam@1989
|
375 val (st, m') = St.bind (st, m)
|
adam@1989
|
376
|
adam@1989
|
377 val env = E.declBinds E.empty all
|
adam@1989
|
378 in
|
adam@1989
|
379 ([all, (DVal (x, m', #2 (E.lookupENamed env m), (ENamed m, loc)), loc)], st)
|
adam@1989
|
380 end
|
adam@1989
|
381 | DView (n, x, m, e, c) =>
|
adam@1989
|
382 let
|
adam@1989
|
383 val (st, m') = St.bind (st, m)
|
adam@1989
|
384
|
adam@1989
|
385 val d = (DView (n, x, m, renameExp st e, renameCon st c), loc)
|
adam@1989
|
386
|
adam@1989
|
387 val env = E.declBinds E.empty d
|
adam@1989
|
388 in
|
adam@1989
|
389 ([d, (DVal (x, m', #2 (E.lookupENamed env m), (ENamed m, loc)), loc)], st)
|
adam@1989
|
390 end
|
adam@1989
|
391 | DDatabase _ => ([all], st)
|
adam@1989
|
392 | DCookie (n, x, m, c) =>
|
adam@1989
|
393 let
|
adam@1989
|
394 val (st, m') = St.bind (st, m)
|
adam@1989
|
395
|
adam@1989
|
396 val d = (DCookie (n, x, m, renameCon st c), loc)
|
adam@1989
|
397
|
adam@1989
|
398 val env = E.declBinds E.empty d
|
adam@1989
|
399 in
|
adam@1989
|
400 ([d, (DVal (x, m', #2 (E.lookupENamed env m), (ENamed m, loc)), loc)], st)
|
adam@1989
|
401 end
|
adam@1989
|
402 | DStyle (n, x, m) =>
|
adam@1989
|
403 let
|
adam@1989
|
404 val (st, m') = St.bind (st, m)
|
adam@1989
|
405
|
adam@1989
|
406 val env = E.declBinds E.empty all
|
adam@1989
|
407 in
|
adam@1989
|
408 ([all, (DVal (x, m', #2 (E.lookupENamed env m), (ENamed m, loc)), loc)], st)
|
adam@1989
|
409 end
|
adam@1989
|
410 | DTask (e1, e2) => ([(DTask (renameExp st e1, renameExp st e2), loc)], st)
|
adam@1989
|
411 | DPolicy e => ([(DPolicy (renameExp st e), loc)], st)
|
adam@1989
|
412 | DOnError (n, xs, x) =>
|
adam@1989
|
413 (case St.lookup (st, n) of
|
adam@1989
|
414 NONE => ([all], st)
|
adam@1989
|
415 | SOME n' => ([(DOnError (n', xs, x), loc)], st))
|
adam@1989
|
416
|
adam@1989
|
417 fun rename {NextId, FormalName, FormalId, Body = all as (str, loc)} =
|
adam@1989
|
418 case str of
|
adam@1989
|
419 StrConst ds =>
|
adam@1989
|
420 let
|
adam@1989
|
421 val st = St.create NextId
|
adam@1989
|
422 val (st, n) = St.bind (st, FormalId)
|
adam@1989
|
423
|
adam@1989
|
424 val (ds, st) = ListUtil.foldlMapConcat dupDecl st ds
|
adam@1991
|
425
|
adam@1991
|
426 (* Revenge of the functor parameter renamer!
|
adam@1991
|
427 * See comment in elaborate.sml for the start of the saga.
|
adam@1991
|
428 * We need to alpha-rename the argument to allow sufficient shadowing in the body. *)
|
adam@1991
|
429
|
adam@1991
|
430 fun mungeName m =
|
adam@1991
|
431 if List.exists (fn (DStr (x, _, _, _), _) => x = m
|
adam@1991
|
432 | _ => false) ds then
|
adam@1991
|
433 mungeName ("?" ^ m)
|
adam@1991
|
434 else
|
adam@1991
|
435 m
|
adam@1991
|
436
|
adam@1991
|
437 val FormalName = mungeName FormalName
|
adam@1991
|
438
|
adam@1989
|
439 val ds = (DStr (FormalName, n, (SgnConst [], loc), (StrVar FormalId, loc)), loc) :: ds
|
adam@1989
|
440 in
|
adam@1989
|
441 (St.next st, (StrConst ds, loc))
|
adam@1989
|
442 end
|
adam@1989
|
443 | _ => (NextId, all)
|
adam@1989
|
444
|
adam@1989
|
445 end
|