adamc@38
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@38
|
2 * All rights reserved.
|
adamc@38
|
3 *
|
adamc@38
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@38
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@38
|
6 *
|
adamc@38
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@38
|
8 * this list of conditions and the following disclaimer.
|
adamc@38
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@38
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@38
|
11 * and/or other materials provided with the distribution.
|
adamc@38
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@38
|
13 * derived from this software without specific prior written permission.
|
adamc@38
|
14 *
|
adamc@38
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@38
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@38
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@38
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@38
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@38
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@38
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@38
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@38
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@38
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@38
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@38
|
26 *)
|
adamc@38
|
27
|
adamc@38
|
28 structure ExplUtil :> EXPL_UTIL = struct
|
adamc@38
|
29
|
adamc@38
|
30 open Expl
|
adamc@38
|
31
|
adamc@38
|
32 structure S = Search
|
adamc@38
|
33
|
adamc@38
|
34 structure Kind = struct
|
adamc@38
|
35
|
adamc@38
|
36 fun mapfold f =
|
adamc@38
|
37 let
|
adamc@38
|
38 fun mfk k acc =
|
adamc@38
|
39 S.bindP (mfk' k acc, f)
|
adamc@38
|
40
|
adamc@38
|
41 and mfk' (kAll as (k, loc)) =
|
adamc@38
|
42 case k of
|
adamc@38
|
43 KType => S.return2 kAll
|
adamc@38
|
44
|
adamc@38
|
45 | KArrow (k1, k2) =>
|
adamc@38
|
46 S.bind2 (mfk k1,
|
adamc@38
|
47 fn k1' =>
|
adamc@38
|
48 S.map2 (mfk k2,
|
adamc@38
|
49 fn k2' =>
|
adamc@38
|
50 (KArrow (k1', k2'), loc)))
|
adamc@38
|
51
|
adamc@38
|
52 | KName => S.return2 kAll
|
adamc@38
|
53
|
adamc@38
|
54 | KRecord k =>
|
adamc@38
|
55 S.map2 (mfk k,
|
adamc@38
|
56 fn k' =>
|
adamc@38
|
57 (KRecord k', loc))
|
adamc@87
|
58
|
adamc@87
|
59 | KUnit => S.return2 kAll
|
adamc@213
|
60
|
adamc@213
|
61 | KTuple ks =>
|
adamc@213
|
62 S.map2 (ListUtil.mapfold mfk ks,
|
adamc@213
|
63 fn ks' =>
|
adamc@213
|
64 (KTuple ks', loc))
|
adamc@38
|
65 in
|
adamc@38
|
66 mfk
|
adamc@38
|
67 end
|
adamc@38
|
68
|
adamc@38
|
69 fun exists f k =
|
adamc@38
|
70 case mapfold (fn k => fn () =>
|
adamc@38
|
71 if f k then
|
adamc@38
|
72 S.Return ()
|
adamc@38
|
73 else
|
adamc@38
|
74 S.Continue (k, ())) k () of
|
adamc@38
|
75 S.Return _ => true
|
adamc@38
|
76 | S.Continue _ => false
|
adamc@38
|
77
|
adamc@38
|
78 end
|
adamc@38
|
79
|
adamc@38
|
80 structure Con = struct
|
adamc@38
|
81
|
adamc@38
|
82 datatype binder =
|
adamc@38
|
83 Rel of string * Expl.kind
|
adamc@38
|
84 | Named of string * Expl.kind
|
adamc@38
|
85
|
adamc@38
|
86 fun mapfoldB {kind = fk, con = fc, bind} =
|
adamc@38
|
87 let
|
adamc@38
|
88 val mfk = Kind.mapfold fk
|
adamc@38
|
89
|
adamc@38
|
90 fun mfc ctx c acc =
|
adamc@38
|
91 S.bindP (mfc' ctx c acc, fc ctx)
|
adamc@38
|
92
|
adamc@38
|
93 and mfc' ctx (cAll as (c, loc)) =
|
adamc@38
|
94 case c of
|
adamc@38
|
95 TFun (c1, c2) =>
|
adamc@38
|
96 S.bind2 (mfc ctx c1,
|
adamc@38
|
97 fn c1' =>
|
adamc@38
|
98 S.map2 (mfc ctx c2,
|
adamc@38
|
99 fn c2' =>
|
adamc@38
|
100 (TFun (c1', c2'), loc)))
|
adamc@38
|
101 | TCFun (x, k, c) =>
|
adamc@38
|
102 S.bind2 (mfk k,
|
adamc@38
|
103 fn k' =>
|
adamc@38
|
104 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@38
|
105 fn c' =>
|
adamc@38
|
106 (TCFun (x, k', c'), loc)))
|
adamc@38
|
107 | TRecord c =>
|
adamc@38
|
108 S.map2 (mfc ctx c,
|
adamc@38
|
109 fn c' =>
|
adamc@38
|
110 (TRecord c', loc))
|
adamc@38
|
111
|
adamc@38
|
112 | CRel _ => S.return2 cAll
|
adamc@38
|
113 | CNamed _ => S.return2 cAll
|
adamc@38
|
114 | CModProj _ => S.return2 cAll
|
adamc@38
|
115 | CApp (c1, c2) =>
|
adamc@38
|
116 S.bind2 (mfc ctx c1,
|
adamc@38
|
117 fn c1' =>
|
adamc@38
|
118 S.map2 (mfc ctx c2,
|
adamc@38
|
119 fn c2' =>
|
adamc@38
|
120 (CApp (c1', c2'), loc)))
|
adamc@38
|
121 | CAbs (x, k, c) =>
|
adamc@38
|
122 S.bind2 (mfk k,
|
adamc@38
|
123 fn k' =>
|
adamc@38
|
124 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@38
|
125 fn c' =>
|
adamc@38
|
126 (CAbs (x, k', c'), loc)))
|
adamc@38
|
127
|
adamc@38
|
128 | CName _ => S.return2 cAll
|
adamc@38
|
129
|
adamc@38
|
130 | CRecord (k, xcs) =>
|
adamc@38
|
131 S.bind2 (mfk k,
|
adamc@38
|
132 fn k' =>
|
adamc@38
|
133 S.map2 (ListUtil.mapfold (fn (x, c) =>
|
adamc@38
|
134 S.bind2 (mfc ctx x,
|
adamc@38
|
135 fn x' =>
|
adamc@38
|
136 S.map2 (mfc ctx c,
|
adamc@38
|
137 fn c' =>
|
adamc@38
|
138 (x', c'))))
|
adamc@38
|
139 xcs,
|
adamc@38
|
140 fn xcs' =>
|
adamc@38
|
141 (CRecord (k', xcs'), loc)))
|
adamc@38
|
142 | CConcat (c1, c2) =>
|
adamc@38
|
143 S.bind2 (mfc ctx c1,
|
adamc@38
|
144 fn c1' =>
|
adamc@38
|
145 S.map2 (mfc ctx c2,
|
adamc@38
|
146 fn c2' =>
|
adamc@38
|
147 (CConcat (c1', c2'), loc)))
|
adamc@621
|
148 | CMap (k1, k2) =>
|
adamc@68
|
149 S.bind2 (mfk k1,
|
adamc@68
|
150 fn k1' =>
|
adamc@68
|
151 S.map2 (mfk k2,
|
adamc@68
|
152 fn k2' =>
|
adamc@621
|
153 (CMap (k1', k2'), loc)))
|
adamc@87
|
154
|
adamc@87
|
155 | CUnit => S.return2 cAll
|
adamc@213
|
156
|
adamc@213
|
157 | CTuple cs =>
|
adamc@213
|
158 S.map2 (ListUtil.mapfold (mfc ctx) cs,
|
adamc@213
|
159 fn cs' =>
|
adamc@213
|
160 (CTuple cs', loc))
|
adamc@213
|
161
|
adamc@213
|
162 | CProj (c, n) =>
|
adamc@213
|
163 S.map2 (mfc ctx c,
|
adamc@213
|
164 fn c' =>
|
adamc@213
|
165 (CProj (c', n), loc))
|
adamc@38
|
166 in
|
adamc@38
|
167 mfc
|
adamc@38
|
168 end
|
adamc@38
|
169
|
adamc@38
|
170 fun mapfold {kind = fk, con = fc} =
|
adamc@38
|
171 mapfoldB {kind = fk,
|
adamc@38
|
172 con = fn () => fc,
|
adamc@38
|
173 bind = fn ((), _) => ()} ()
|
adamc@38
|
174
|
adamc@38
|
175 fun mapB {kind, con, bind} ctx c =
|
adamc@38
|
176 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@38
|
177 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@38
|
178 bind = bind} ctx c () of
|
adamc@38
|
179 S.Continue (c, ()) => c
|
adamc@38
|
180 | S.Return _ => raise Fail "ExplUtil.Con.mapB: Impossible"
|
adamc@38
|
181
|
adamc@38
|
182 fun map {kind, con} s =
|
adamc@38
|
183 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@38
|
184 con = fn c => fn () => S.Continue (con c, ())} s () of
|
adamc@38
|
185 S.Return () => raise Fail "ExplUtil.Con.map: Impossible"
|
adamc@38
|
186 | S.Continue (s, ()) => s
|
adamc@38
|
187
|
adamc@38
|
188 fun exists {kind, con} k =
|
adamc@38
|
189 case mapfold {kind = fn k => fn () =>
|
adamc@38
|
190 if kind k then
|
adamc@38
|
191 S.Return ()
|
adamc@38
|
192 else
|
adamc@38
|
193 S.Continue (k, ()),
|
adamc@38
|
194 con = fn c => fn () =>
|
adamc@38
|
195 if con c then
|
adamc@38
|
196 S.Return ()
|
adamc@38
|
197 else
|
adamc@38
|
198 S.Continue (c, ())} k () of
|
adamc@38
|
199 S.Return _ => true
|
adamc@38
|
200 | S.Continue _ => false
|
adamc@38
|
201
|
adamc@38
|
202 end
|
adamc@38
|
203
|
adamc@38
|
204 structure Exp = struct
|
adamc@38
|
205
|
adamc@38
|
206 datatype binder =
|
adamc@38
|
207 RelC of string * Expl.kind
|
adamc@38
|
208 | NamedC of string * Expl.kind
|
adamc@38
|
209 | RelE of string * Expl.con
|
adamc@38
|
210 | NamedE of string * Expl.con
|
adamc@38
|
211
|
adamc@38
|
212 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
|
adamc@38
|
213 let
|
adamc@38
|
214 val mfk = Kind.mapfold fk
|
adamc@38
|
215
|
adamc@38
|
216 fun bind' (ctx, b) =
|
adamc@38
|
217 let
|
adamc@38
|
218 val b' = case b of
|
adamc@38
|
219 Con.Rel x => RelC x
|
adamc@38
|
220 | Con.Named x => NamedC x
|
adamc@38
|
221 in
|
adamc@38
|
222 bind (ctx, b')
|
adamc@38
|
223 end
|
adamc@38
|
224 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@38
|
225
|
adamc@38
|
226 fun mfe ctx e acc =
|
adamc@38
|
227 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@38
|
228
|
adamc@38
|
229 and mfe' ctx (eAll as (e, loc)) =
|
adamc@38
|
230 case e of
|
adamc@38
|
231 EPrim _ => S.return2 eAll
|
adamc@38
|
232 | ERel _ => S.return2 eAll
|
adamc@38
|
233 | ENamed _ => S.return2 eAll
|
adamc@38
|
234 | EModProj _ => S.return2 eAll
|
adamc@38
|
235 | EApp (e1, e2) =>
|
adamc@38
|
236 S.bind2 (mfe ctx e1,
|
adamc@38
|
237 fn e1' =>
|
adamc@38
|
238 S.map2 (mfe ctx e2,
|
adamc@38
|
239 fn e2' =>
|
adamc@38
|
240 (EApp (e1', e2'), loc)))
|
adamc@38
|
241 | EAbs (x, dom, ran, e) =>
|
adamc@38
|
242 S.bind2 (mfc ctx dom,
|
adamc@38
|
243 fn dom' =>
|
adamc@38
|
244 S.bind2 (mfc ctx ran,
|
adamc@38
|
245 fn ran' =>
|
adamc@38
|
246 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@38
|
247 fn e' =>
|
adamc@38
|
248 (EAbs (x, dom', ran', e'), loc))))
|
adamc@38
|
249
|
adamc@38
|
250 | ECApp (e, c) =>
|
adamc@38
|
251 S.bind2 (mfe ctx e,
|
adamc@38
|
252 fn e' =>
|
adamc@38
|
253 S.map2 (mfc ctx c,
|
adamc@38
|
254 fn c' =>
|
adamc@38
|
255 (ECApp (e', c'), loc)))
|
adamc@38
|
256 | ECAbs (x, k, e) =>
|
adamc@38
|
257 S.bind2 (mfk k,
|
adamc@38
|
258 fn k' =>
|
adamc@38
|
259 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
|
adamc@38
|
260 fn e' =>
|
adamc@38
|
261 (ECAbs (x, k', e'), loc)))
|
adamc@38
|
262
|
adamc@38
|
263 | ERecord xes =>
|
adamc@38
|
264 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@38
|
265 S.bind2 (mfc ctx x,
|
adamc@38
|
266 fn x' =>
|
adamc@38
|
267 S.bind2 (mfe ctx e,
|
adamc@38
|
268 fn e' =>
|
adamc@38
|
269 S.map2 (mfc ctx t,
|
adamc@38
|
270 fn t' =>
|
adamc@38
|
271 (x', e', t')))))
|
adamc@38
|
272 xes,
|
adamc@38
|
273 fn xes' =>
|
adamc@38
|
274 (ERecord xes', loc))
|
adamc@38
|
275 | EField (e, c, {field, rest}) =>
|
adamc@38
|
276 S.bind2 (mfe ctx e,
|
adamc@38
|
277 fn e' =>
|
adamc@38
|
278 S.bind2 (mfc ctx c,
|
adamc@38
|
279 fn c' =>
|
adamc@38
|
280 S.bind2 (mfc ctx field,
|
adamc@38
|
281 fn field' =>
|
adamc@38
|
282 S.map2 (mfc ctx rest,
|
adamc@38
|
283 fn rest' =>
|
adamc@38
|
284 (EField (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@445
|
285 | EConcat (e1, c1, e2, c2) =>
|
adamc@339
|
286 S.bind2 (mfe ctx e1,
|
adamc@339
|
287 fn e1' =>
|
adamc@445
|
288 S.bind2 (mfc ctx c1,
|
adamc@445
|
289 fn c1' =>
|
adamc@339
|
290 S.bind2 (mfe ctx e2,
|
adamc@339
|
291 fn e2' =>
|
adamc@445
|
292 S.map2 (mfc ctx c2,
|
adamc@445
|
293 fn c2' =>
|
adamc@445
|
294 (EConcat (e1', c1', e2', c2'),
|
adamc@445
|
295 loc)))))
|
adamc@149
|
296 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
297 S.bind2 (mfe ctx e,
|
adamc@149
|
298 fn e' =>
|
adamc@149
|
299 S.bind2 (mfc ctx c,
|
adamc@149
|
300 fn c' =>
|
adamc@149
|
301 S.bind2 (mfc ctx field,
|
adamc@149
|
302 fn field' =>
|
adamc@149
|
303 S.map2 (mfc ctx rest,
|
adamc@149
|
304 fn rest' =>
|
adamc@149
|
305 (ECut (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@493
|
306 | ECutMulti (e, c, {rest}) =>
|
adamc@493
|
307 S.bind2 (mfe ctx e,
|
adamc@493
|
308 fn e' =>
|
adamc@493
|
309 S.bind2 (mfc ctx c,
|
adamc@493
|
310 fn c' =>
|
adamc@493
|
311 S.map2 (mfc ctx rest,
|
adamc@493
|
312 fn rest' =>
|
adamc@493
|
313 (ECutMulti (e', c', {rest = rest'}), loc))))
|
adamc@72
|
314 | EFold k =>
|
adamc@72
|
315 S.map2 (mfk k,
|
adamc@72
|
316 fn k' =>
|
adamc@72
|
317 (EFold k', loc))
|
adamc@109
|
318
|
adamc@109
|
319 | EWrite e =>
|
adamc@109
|
320 S.map2 (mfe ctx e,
|
adamc@109
|
321 fn e' =>
|
adamc@109
|
322 (EWrite e', loc))
|
adamc@176
|
323
|
adamc@182
|
324 | ECase (e, pes, {disc, result}) =>
|
adamc@176
|
325 S.bind2 (mfe ctx e,
|
adamc@176
|
326 fn e' =>
|
adamc@176
|
327 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@176
|
328 S.map2 (mfe ctx e,
|
adamc@176
|
329 fn e' => (p, e'))) pes,
|
adamc@176
|
330 fn pes' =>
|
adamc@182
|
331 S.bind2 (mfc ctx disc,
|
adamc@182
|
332 fn disc' =>
|
adamc@182
|
333 S.map2 (mfc ctx result,
|
adamc@182
|
334 fn result' =>
|
adamc@182
|
335 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@449
|
336
|
adamc@449
|
337 | ELet (x, t, e1, e2) =>
|
adamc@449
|
338 S.bind2 (mfc ctx t,
|
adamc@449
|
339 fn t' =>
|
adamc@449
|
340 S.bind2 (mfe ctx e1,
|
adamc@449
|
341 fn e1' =>
|
adamc@453
|
342 S.map2 (mfe (bind (ctx, RelE (x, t))) e2,
|
adamc@449
|
343 fn e2' =>
|
adamc@449
|
344 (ELet (x, t', e1', e2'), loc))))
|
adamc@38
|
345 in
|
adamc@38
|
346 mfe
|
adamc@38
|
347 end
|
adamc@38
|
348
|
adamc@38
|
349 fun mapfold {kind = fk, con = fc, exp = fe} =
|
adamc@38
|
350 mapfoldB {kind = fk,
|
adamc@38
|
351 con = fn () => fc,
|
adamc@38
|
352 exp = fn () => fe,
|
adamc@38
|
353 bind = fn ((), _) => ()} ()
|
adamc@38
|
354
|
adamc@38
|
355 fun exists {kind, con, exp} k =
|
adamc@38
|
356 case mapfold {kind = fn k => fn () =>
|
adamc@38
|
357 if kind k then
|
adamc@38
|
358 S.Return ()
|
adamc@38
|
359 else
|
adamc@38
|
360 S.Continue (k, ()),
|
adamc@38
|
361 con = fn c => fn () =>
|
adamc@38
|
362 if con c then
|
adamc@38
|
363 S.Return ()
|
adamc@38
|
364 else
|
adamc@38
|
365 S.Continue (c, ()),
|
adamc@38
|
366 exp = fn e => fn () =>
|
adamc@38
|
367 if exp e then
|
adamc@38
|
368 S.Return ()
|
adamc@38
|
369 else
|
adamc@38
|
370 S.Continue (e, ())} k () of
|
adamc@38
|
371 S.Return _ => true
|
adamc@38
|
372 | S.Continue _ => false
|
adamc@38
|
373
|
adamc@38
|
374 end
|
adamc@38
|
375
|
adamc@38
|
376 structure Sgn = struct
|
adamc@38
|
377
|
adamc@38
|
378 datatype binder =
|
adamc@38
|
379 RelC of string * Expl.kind
|
adamc@38
|
380 | NamedC of string * Expl.kind
|
adamc@38
|
381 | Str of string * Expl.sgn
|
adamc@64
|
382 | Sgn of string * Expl.sgn
|
adamc@38
|
383
|
adamc@38
|
384 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
|
adamc@38
|
385 let
|
adamc@38
|
386 fun bind' (ctx, b) =
|
adamc@38
|
387 let
|
adamc@38
|
388 val b' = case b of
|
adamc@38
|
389 Con.Rel x => RelC x
|
adamc@38
|
390 | Con.Named x => NamedC x
|
adamc@38
|
391 in
|
adamc@38
|
392 bind (ctx, b')
|
adamc@38
|
393 end
|
adamc@38
|
394 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
|
adamc@38
|
395
|
adamc@38
|
396 val kind = Kind.mapfold kind
|
adamc@38
|
397
|
adamc@38
|
398 fun sgi ctx si acc =
|
adamc@38
|
399 S.bindP (sgi' ctx si acc, sgn_item ctx)
|
adamc@38
|
400
|
adamc@162
|
401 and sgi' ctx (siAll as (si, loc)) =
|
adamc@38
|
402 case si of
|
adamc@38
|
403 SgiConAbs (x, n, k) =>
|
adamc@38
|
404 S.map2 (kind k,
|
adamc@38
|
405 fn k' =>
|
adamc@38
|
406 (SgiConAbs (x, n, k'), loc))
|
adamc@38
|
407 | SgiCon (x, n, k, c) =>
|
adamc@38
|
408 S.bind2 (kind k,
|
adamc@38
|
409 fn k' =>
|
adamc@38
|
410 S.map2 (con ctx c,
|
adamc@38
|
411 fn c' =>
|
adamc@38
|
412 (SgiCon (x, n, k', c'), loc)))
|
adamc@191
|
413 | SgiDatatype (x, n, xs, xncs) =>
|
adamc@162
|
414 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
415 case c of
|
adamc@162
|
416 NONE => S.return2 (x, n, c)
|
adamc@162
|
417 | SOME c =>
|
adamc@162
|
418 S.map2 (con ctx c,
|
adamc@162
|
419 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
420 fn xncs' =>
|
adamc@191
|
421 (SgiDatatype (x, n, xs, xncs'), loc))
|
adamc@191
|
422 | SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
423 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
424 case c of
|
adamc@162
|
425 NONE => S.return2 (x, n, c)
|
adamc@162
|
426 | SOME c =>
|
adamc@162
|
427 S.map2 (con ctx c,
|
adamc@162
|
428 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
429 fn xncs' =>
|
adamc@191
|
430 (SgiDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@38
|
431 | SgiVal (x, n, c) =>
|
adamc@38
|
432 S.map2 (con ctx c,
|
adamc@38
|
433 fn c' =>
|
adamc@38
|
434 (SgiVal (x, n, c'), loc))
|
adamc@38
|
435 | SgiStr (x, n, s) =>
|
adamc@38
|
436 S.map2 (sg ctx s,
|
adamc@38
|
437 fn s' =>
|
adamc@38
|
438 (SgiStr (x, n, s'), loc))
|
adamc@64
|
439 | SgiSgn (x, n, s) =>
|
adamc@64
|
440 S.map2 (sg ctx s,
|
adamc@64
|
441 fn s' =>
|
adamc@64
|
442 (SgiSgn (x, n, s'), loc))
|
adamc@38
|
443
|
adamc@38
|
444 and sg ctx s acc =
|
adamc@38
|
445 S.bindP (sg' ctx s acc, sgn ctx)
|
adamc@38
|
446
|
adamc@38
|
447 and sg' ctx (sAll as (s, loc)) =
|
adamc@38
|
448 case s of
|
adamc@38
|
449 SgnConst sgis =>
|
adamc@38
|
450 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
|
adamc@38
|
451 (case #1 si of
|
adamc@38
|
452 SgiConAbs (x, _, k) =>
|
adamc@38
|
453 bind (ctx, NamedC (x, k))
|
adamc@38
|
454 | SgiCon (x, _, k, _) =>
|
adamc@38
|
455 bind (ctx, NamedC (x, k))
|
adamc@191
|
456 | SgiDatatype (x, n, _, xncs) =>
|
adamc@162
|
457 bind (ctx, NamedC (x, (KType, loc)))
|
adamc@191
|
458 | SgiDatatypeImp (x, _, _, _, _, _, _) =>
|
adamc@162
|
459 bind (ctx, NamedC (x, (KType, loc)))
|
adamc@38
|
460 | SgiVal _ => ctx
|
adamc@38
|
461 | SgiStr (x, _, sgn) =>
|
adamc@64
|
462 bind (ctx, Str (x, sgn))
|
adamc@64
|
463 | SgiSgn (x, _, sgn) =>
|
adamc@460
|
464 bind (ctx, Sgn (x, sgn)),
|
adamc@38
|
465 sgi ctx si)) ctx sgis,
|
adamc@38
|
466 fn sgis' =>
|
adamc@38
|
467 (SgnConst sgis', loc))
|
adamc@38
|
468
|
adamc@38
|
469 | SgnVar _ => S.return2 sAll
|
adamc@45
|
470
|
adamc@45
|
471 | SgnFun (m, n, s1, s2) =>
|
adamc@45
|
472 S.bind2 (sg ctx s1,
|
adamc@45
|
473 fn s1' =>
|
adamc@45
|
474 S.map2 (sg (bind (ctx, Str (m, s1'))) s2,
|
adamc@45
|
475 fn s2' =>
|
adamc@45
|
476 (SgnFun (m, n, s1', s2'), loc)))
|
adamc@45
|
477 | SgnWhere (sgn, x, c) =>
|
adamc@45
|
478 S.bind2 (sg ctx sgn,
|
adamc@45
|
479 fn sgn' =>
|
adamc@45
|
480 S.map2 (con ctx c,
|
adamc@45
|
481 fn c' =>
|
adamc@45
|
482 (SgnWhere (sgn', x, c'), loc)))
|
adamc@64
|
483 | SgnProj _ => S.return2 sAll
|
adamc@38
|
484 in
|
adamc@38
|
485 sg
|
adamc@38
|
486 end
|
adamc@38
|
487
|
adamc@38
|
488 fun mapfold {kind, con, sgn_item, sgn} =
|
adamc@38
|
489 mapfoldB {kind = kind,
|
adamc@38
|
490 con = fn () => con,
|
adamc@38
|
491 sgn_item = fn () => sgn_item,
|
adamc@38
|
492 sgn = fn () => sgn,
|
adamc@38
|
493 bind = fn ((), _) => ()} ()
|
adamc@38
|
494
|
adamc@38
|
495 fun map {kind, con, sgn_item, sgn} s =
|
adamc@38
|
496 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@38
|
497 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@38
|
498 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
|
adamc@38
|
499 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
|
adamc@38
|
500 S.Return () => raise Fail "Expl_util.Sgn.map"
|
adamc@38
|
501 | S.Continue (s, ()) => s
|
adamc@38
|
502
|
adamc@38
|
503 end
|
adamc@38
|
504
|
adamc@38
|
505 end
|