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 ElabUtil :> ELAB_UTIL = struct
|
adamc@2
|
29
|
adamc@2
|
30 open Elab
|
adamc@2
|
31
|
adamc@2
|
32 structure S = Search
|
adamc@2
|
33
|
adamc@2
|
34 structure Kind = struct
|
adamc@2
|
35
|
adamc@6
|
36 fun mapfold f =
|
adamc@2
|
37 let
|
adamc@2
|
38 fun mfk k acc =
|
adamc@2
|
39 S.bindP (mfk' k acc, f)
|
adamc@2
|
40
|
adamc@2
|
41 and mfk' (kAll as (k, loc)) =
|
adamc@2
|
42 case k of
|
adamc@2
|
43 KType => S.return2 kAll
|
adamc@2
|
44
|
adamc@2
|
45 | KArrow (k1, k2) =>
|
adamc@2
|
46 S.bind2 (mfk k1,
|
adamc@2
|
47 fn k1' =>
|
adamc@2
|
48 S.map2 (mfk k2,
|
adamc@2
|
49 fn k2' =>
|
adamc@2
|
50 (KArrow (k1', k2'), loc)))
|
adamc@2
|
51
|
adamc@2
|
52 | KName => S.return2 kAll
|
adamc@2
|
53
|
adamc@2
|
54 | KRecord k =>
|
adamc@2
|
55 S.map2 (mfk k,
|
adamc@2
|
56 fn k' =>
|
adamc@2
|
57 (KRecord k', loc))
|
adamc@2
|
58
|
adamc@82
|
59 | KUnit => S.return2 kAll
|
adamc@82
|
60
|
adamc@2
|
61 | KError => S.return2 kAll
|
adamc@2
|
62
|
adamc@76
|
63 | KUnif (_, _, ref (SOME k)) => mfk' k
|
adamc@2
|
64 | KUnif _ => S.return2 kAll
|
adamc@2
|
65 in
|
adamc@2
|
66 mfk
|
adamc@2
|
67 end
|
adamc@2
|
68
|
adamc@2
|
69 fun exists f k =
|
adamc@6
|
70 case mapfold (fn k => fn () =>
|
adamc@6
|
71 if f k then
|
adamc@6
|
72 S.Return ()
|
adamc@6
|
73 else
|
adamc@6
|
74 S.Continue (k, ())) k () of
|
adamc@6
|
75 S.Return _ => true
|
adamc@6
|
76 | S.Continue _ => false
|
adamc@6
|
77
|
adamc@6
|
78 end
|
adamc@6
|
79
|
adamc@6
|
80 structure Con = struct
|
adamc@6
|
81
|
adamc@11
|
82 datatype binder =
|
adamc@11
|
83 Rel of string * Elab.kind
|
adamc@11
|
84 | Named of string * Elab.kind
|
adamc@11
|
85
|
adamc@11
|
86 fun mapfoldB {kind = fk, con = fc, bind} =
|
adamc@6
|
87 let
|
adamc@6
|
88 val mfk = Kind.mapfold fk
|
adamc@6
|
89
|
adamc@11
|
90 fun mfc ctx c acc =
|
adamc@11
|
91 S.bindP (mfc' ctx c acc, fc ctx)
|
adamc@6
|
92
|
adamc@11
|
93 and mfc' ctx (cAll as (c, loc)) =
|
adamc@6
|
94 case c of
|
adamc@6
|
95 TFun (c1, c2) =>
|
adamc@11
|
96 S.bind2 (mfc ctx c1,
|
adamc@6
|
97 fn c1' =>
|
adamc@11
|
98 S.map2 (mfc ctx c2,
|
adamc@6
|
99 fn c2' =>
|
adamc@6
|
100 (TFun (c1', c2'), loc)))
|
adamc@6
|
101 | TCFun (e, x, k, c) =>
|
adamc@6
|
102 S.bind2 (mfk k,
|
adamc@6
|
103 fn k' =>
|
adamc@11
|
104 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@6
|
105 fn c' =>
|
adamc@6
|
106 (TCFun (e, x, k', c'), loc)))
|
adamc@85
|
107 | TDisjoint (c1, c2, c3) =>
|
adamc@85
|
108 S.bind2 (mfc ctx c1,
|
adamc@85
|
109 fn c1' =>
|
adamc@85
|
110 S.bind2 (mfc ctx c2,
|
adamc@85
|
111 fn c2' =>
|
adamc@85
|
112 S.map2 (mfc ctx c3,
|
adamc@85
|
113 fn c3' =>
|
adamc@85
|
114 (TDisjoint (c1', c2', c3'), loc))))
|
adamc@6
|
115 | TRecord c =>
|
adamc@11
|
116 S.map2 (mfc ctx c,
|
adamc@6
|
117 fn c' =>
|
adamc@6
|
118 (TRecord c', loc))
|
adamc@6
|
119
|
adamc@6
|
120 | CRel _ => S.return2 cAll
|
adamc@6
|
121 | CNamed _ => S.return2 cAll
|
adamc@34
|
122 | CModProj _ => S.return2 cAll
|
adamc@6
|
123 | CApp (c1, c2) =>
|
adamc@11
|
124 S.bind2 (mfc ctx c1,
|
adamc@6
|
125 fn c1' =>
|
adamc@11
|
126 S.map2 (mfc ctx c2,
|
adamc@6
|
127 fn c2' =>
|
adamc@6
|
128 (CApp (c1', c2'), loc)))
|
adamc@8
|
129 | CAbs (x, k, c) =>
|
adamc@6
|
130 S.bind2 (mfk k,
|
adamc@6
|
131 fn k' =>
|
adamc@11
|
132 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@6
|
133 fn c' =>
|
adamc@8
|
134 (CAbs (x, k', c'), loc)))
|
adamc@84
|
135 | CDisjoint (c1, c2, c3) =>
|
adamc@84
|
136 S.bind2 (mfc ctx c1,
|
adamc@84
|
137 fn c1' =>
|
adamc@84
|
138 S.bind2 (mfc ctx c2,
|
adamc@84
|
139 fn c2' =>
|
adamc@84
|
140 S.map2 (mfc ctx c3,
|
adamc@84
|
141 fn c3' =>
|
adamc@84
|
142 (CDisjoint (c1', c2', c3'), loc))))
|
adamc@6
|
143
|
adamc@6
|
144 | CName _ => S.return2 cAll
|
adamc@6
|
145
|
adamc@6
|
146 | CRecord (k, xcs) =>
|
adamc@6
|
147 S.bind2 (mfk k,
|
adamc@6
|
148 fn k' =>
|
adamc@6
|
149 S.map2 (ListUtil.mapfold (fn (x, c) =>
|
adamc@11
|
150 S.bind2 (mfc ctx x,
|
adamc@6
|
151 fn x' =>
|
adamc@11
|
152 S.map2 (mfc ctx c,
|
adamc@6
|
153 fn c' =>
|
adamc@6
|
154 (x', c'))))
|
adamc@6
|
155 xcs,
|
adamc@6
|
156 fn xcs' =>
|
adamc@6
|
157 (CRecord (k', xcs'), loc)))
|
adamc@6
|
158 | CConcat (c1, c2) =>
|
adamc@11
|
159 S.bind2 (mfc ctx c1,
|
adamc@6
|
160 fn c1' =>
|
adamc@11
|
161 S.map2 (mfc ctx c2,
|
adamc@6
|
162 fn c2' =>
|
adamc@6
|
163 (CConcat (c1', c2'), loc)))
|
adamc@67
|
164 | CFold (k1, k2) =>
|
adamc@67
|
165 S.bind2 (mfk k1,
|
adamc@67
|
166 fn k1' =>
|
adamc@67
|
167 S.map2 (mfk k2,
|
adamc@67
|
168 fn k2' =>
|
adamc@67
|
169 (CFold (k1', k2'), loc)))
|
adamc@6
|
170
|
adamc@82
|
171 | CUnit => S.return2 cAll
|
adamc@82
|
172
|
adamc@6
|
173 | CError => S.return2 cAll
|
adamc@76
|
174 | CUnif (_, _, _, ref (SOME c)) => mfc' ctx c
|
adamc@6
|
175 | CUnif _ => S.return2 cAll
|
adamc@6
|
176 in
|
adamc@6
|
177 mfc
|
adamc@6
|
178 end
|
adamc@6
|
179
|
adamc@11
|
180 fun mapfold {kind = fk, con = fc} =
|
adamc@11
|
181 mapfoldB {kind = fk,
|
adamc@11
|
182 con = fn () => fc,
|
adamc@11
|
183 bind = fn ((), _) => ()} ()
|
adamc@11
|
184
|
adamc@11
|
185 fun mapB {kind, con, bind} ctx c =
|
adamc@11
|
186 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@11
|
187 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@11
|
188 bind = bind} ctx c () of
|
adamc@11
|
189 S.Continue (c, ()) => c
|
adamc@34
|
190 | S.Return _ => raise Fail "ElabUtil.Con.mapB: Impossible"
|
adamc@34
|
191
|
adamc@34
|
192 fun map {kind, con} s =
|
adamc@34
|
193 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
194 con = fn c => fn () => S.Continue (con c, ())} s () of
|
adamc@34
|
195 S.Return () => raise Fail "ElabUtil.Con.map: Impossible"
|
adamc@34
|
196 | S.Continue (s, ()) => s
|
adamc@11
|
197
|
adamc@6
|
198 fun exists {kind, con} k =
|
adamc@6
|
199 case mapfold {kind = fn k => fn () =>
|
adamc@6
|
200 if kind k then
|
adamc@6
|
201 S.Return ()
|
adamc@6
|
202 else
|
adamc@6
|
203 S.Continue (k, ()),
|
adamc@6
|
204 con = fn c => fn () =>
|
adamc@6
|
205 if con c then
|
adamc@6
|
206 S.Return ()
|
adamc@6
|
207 else
|
adamc@6
|
208 S.Continue (c, ())} k () of
|
adamc@2
|
209 S.Return _ => true
|
adamc@2
|
210 | S.Continue _ => false
|
adamc@2
|
211
|
adamc@2
|
212 end
|
adamc@2
|
213
|
adamc@10
|
214 structure Exp = struct
|
adamc@10
|
215
|
adamc@11
|
216 datatype binder =
|
adamc@11
|
217 RelC of string * Elab.kind
|
adamc@11
|
218 | NamedC of string * Elab.kind
|
adamc@11
|
219 | RelE of string * Elab.con
|
adamc@11
|
220 | NamedE of string * Elab.con
|
adamc@11
|
221
|
adamc@11
|
222 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
|
adamc@10
|
223 let
|
adamc@10
|
224 val mfk = Kind.mapfold fk
|
adamc@10
|
225
|
adamc@11
|
226 fun bind' (ctx, b) =
|
adamc@11
|
227 let
|
adamc@11
|
228 val b' = case b of
|
adamc@11
|
229 Con.Rel x => RelC x
|
adamc@11
|
230 | Con.Named x => NamedC x
|
adamc@11
|
231 in
|
adamc@11
|
232 bind (ctx, b')
|
adamc@11
|
233 end
|
adamc@11
|
234 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@10
|
235
|
adamc@11
|
236 fun mfe ctx e acc =
|
adamc@11
|
237 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@11
|
238
|
adamc@11
|
239 and mfe' ctx (eAll as (e, loc)) =
|
adamc@10
|
240 case e of
|
adamc@14
|
241 EPrim _ => S.return2 eAll
|
adamc@14
|
242 | ERel _ => S.return2 eAll
|
adamc@10
|
243 | ENamed _ => S.return2 eAll
|
adamc@34
|
244 | EModProj _ => S.return2 eAll
|
adamc@10
|
245 | EApp (e1, e2) =>
|
adamc@11
|
246 S.bind2 (mfe ctx e1,
|
adamc@10
|
247 fn e1' =>
|
adamc@11
|
248 S.map2 (mfe ctx e2,
|
adamc@10
|
249 fn e2' =>
|
adamc@10
|
250 (EApp (e1', e2'), loc)))
|
adamc@26
|
251 | EAbs (x, dom, ran, e) =>
|
adamc@26
|
252 S.bind2 (mfc ctx dom,
|
adamc@26
|
253 fn dom' =>
|
adamc@26
|
254 S.bind2 (mfc ctx ran,
|
adamc@26
|
255 fn ran' =>
|
adamc@26
|
256 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
|
adamc@26
|
257 fn e' =>
|
adamc@26
|
258 (EAbs (x, dom', ran', e'), loc))))
|
adamc@26
|
259
|
adamc@10
|
260 | ECApp (e, c) =>
|
adamc@11
|
261 S.bind2 (mfe ctx e,
|
adamc@10
|
262 fn e' =>
|
adamc@11
|
263 S.map2 (mfc ctx c,
|
adamc@10
|
264 fn c' =>
|
adamc@10
|
265 (ECApp (e', c'), loc)))
|
adamc@10
|
266 | ECAbs (expl, x, k, e) =>
|
adamc@10
|
267 S.bind2 (mfk k,
|
adamc@10
|
268 fn k' =>
|
adamc@11
|
269 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
|
adamc@10
|
270 fn e' =>
|
adamc@10
|
271 (ECAbs (expl, x, k', e'), loc)))
|
adamc@10
|
272
|
adamc@12
|
273 | ERecord xes =>
|
adamc@29
|
274 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
|
adamc@12
|
275 S.bind2 (mfc ctx x,
|
adamc@12
|
276 fn x' =>
|
adamc@29
|
277 S.bind2 (mfe ctx e,
|
adamc@12
|
278 fn e' =>
|
adamc@29
|
279 S.map2 (mfc ctx t,
|
adamc@29
|
280 fn t' =>
|
adamc@29
|
281 (x', e', t')))))
|
adamc@12
|
282 xes,
|
adamc@12
|
283 fn xes' =>
|
adamc@12
|
284 (ERecord xes', loc))
|
adamc@12
|
285 | EField (e, c, {field, rest}) =>
|
adamc@12
|
286 S.bind2 (mfe ctx e,
|
adamc@12
|
287 fn e' =>
|
adamc@12
|
288 S.bind2 (mfc ctx c,
|
adamc@12
|
289 fn c' =>
|
adamc@12
|
290 S.bind2 (mfc ctx field,
|
adamc@12
|
291 fn field' =>
|
adamc@12
|
292 S.map2 (mfc ctx rest,
|
adamc@12
|
293 fn rest' =>
|
adamc@12
|
294 (EField (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@149
|
295 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
296 S.bind2 (mfe ctx e,
|
adamc@149
|
297 fn e' =>
|
adamc@149
|
298 S.bind2 (mfc ctx c,
|
adamc@149
|
299 fn c' =>
|
adamc@149
|
300 S.bind2 (mfc ctx field,
|
adamc@149
|
301 fn field' =>
|
adamc@149
|
302 S.map2 (mfc ctx rest,
|
adamc@149
|
303 fn rest' =>
|
adamc@149
|
304 (ECut (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@12
|
305
|
adamc@71
|
306 | EFold k =>
|
adamc@71
|
307 S.map2 (mfk k,
|
adamc@71
|
308 fn k' =>
|
adamc@71
|
309 (EFold k', loc))
|
adamc@71
|
310
|
adamc@182
|
311 | ECase (e, pes, {disc, result}) =>
|
adamc@171
|
312 S.bind2 (mfe ctx e,
|
adamc@171
|
313 fn e' =>
|
adamc@171
|
314 S.bind2 (ListUtil.mapfold (fn (p, e) =>
|
adamc@171
|
315 S.map2 (mfe ctx e,
|
adamc@171
|
316 fn e' => (p, e'))) pes,
|
adamc@171
|
317 fn pes' =>
|
adamc@182
|
318 S.bind2 (mfc ctx disc,
|
adamc@182
|
319 fn disc' =>
|
adamc@182
|
320 S.map2 (mfc ctx result,
|
adamc@182
|
321 fn result' =>
|
adamc@182
|
322 (ECase (e', pes', {disc = disc', result = result'}), loc)))))
|
adamc@171
|
323
|
adamc@10
|
324 | EError => S.return2 eAll
|
adamc@10
|
325 in
|
adamc@10
|
326 mfe
|
adamc@10
|
327 end
|
adamc@10
|
328
|
adamc@11
|
329 fun mapfold {kind = fk, con = fc, exp = fe} =
|
adamc@11
|
330 mapfoldB {kind = fk,
|
adamc@11
|
331 con = fn () => fc,
|
adamc@11
|
332 exp = fn () => fe,
|
adamc@11
|
333 bind = fn ((), _) => ()} ()
|
adamc@11
|
334
|
adamc@10
|
335 fun exists {kind, con, exp} k =
|
adamc@10
|
336 case mapfold {kind = fn k => fn () =>
|
adamc@10
|
337 if kind k then
|
adamc@10
|
338 S.Return ()
|
adamc@10
|
339 else
|
adamc@10
|
340 S.Continue (k, ()),
|
adamc@10
|
341 con = fn c => fn () =>
|
adamc@10
|
342 if con c then
|
adamc@10
|
343 S.Return ()
|
adamc@10
|
344 else
|
adamc@10
|
345 S.Continue (c, ()),
|
adamc@10
|
346 exp = fn e => fn () =>
|
adamc@10
|
347 if exp e then
|
adamc@10
|
348 S.Return ()
|
adamc@10
|
349 else
|
adamc@10
|
350 S.Continue (e, ())} k () of
|
adamc@10
|
351 S.Return _ => true
|
adamc@10
|
352 | S.Continue _ => false
|
adamc@10
|
353
|
adamc@10
|
354 end
|
adamc@10
|
355
|
adamc@34
|
356 structure Sgn = struct
|
adamc@34
|
357
|
adamc@34
|
358 datatype binder =
|
adamc@34
|
359 RelC of string * Elab.kind
|
adamc@34
|
360 | NamedC of string * Elab.kind
|
adamc@34
|
361 | Str of string * Elab.sgn
|
adamc@59
|
362 | Sgn of string * Elab.sgn
|
adamc@34
|
363
|
adamc@34
|
364 fun mapfoldB {kind, con, sgn_item, sgn, bind} =
|
adamc@34
|
365 let
|
adamc@34
|
366 fun bind' (ctx, b) =
|
adamc@34
|
367 let
|
adamc@34
|
368 val b' = case b of
|
adamc@34
|
369 Con.Rel x => RelC x
|
adamc@34
|
370 | Con.Named x => NamedC x
|
adamc@34
|
371 in
|
adamc@34
|
372 bind (ctx, b')
|
adamc@34
|
373 end
|
adamc@34
|
374 val con = Con.mapfoldB {kind = kind, con = con, bind = bind'}
|
adamc@34
|
375
|
adamc@34
|
376 val kind = Kind.mapfold kind
|
adamc@34
|
377
|
adamc@34
|
378 fun sgi ctx si acc =
|
adamc@34
|
379 S.bindP (sgi' ctx si acc, sgn_item ctx)
|
adamc@34
|
380
|
adamc@156
|
381 and sgi' ctx (siAll as (si, loc)) =
|
adamc@34
|
382 case si of
|
adamc@34
|
383 SgiConAbs (x, n, k) =>
|
adamc@34
|
384 S.map2 (kind k,
|
adamc@34
|
385 fn k' =>
|
adamc@34
|
386 (SgiConAbs (x, n, k'), loc))
|
adamc@34
|
387 | SgiCon (x, n, k, c) =>
|
adamc@34
|
388 S.bind2 (kind k,
|
adamc@34
|
389 fn k' =>
|
adamc@34
|
390 S.map2 (con ctx c,
|
adamc@34
|
391 fn c' =>
|
adamc@34
|
392 (SgiCon (x, n, k', c'), loc)))
|
adamc@156
|
393 | SgiDatatype (x, n, xncs) =>
|
adamc@156
|
394 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
395 case c of
|
adamc@156
|
396 NONE => S.return2 (x, n, c)
|
adamc@156
|
397 | SOME c =>
|
adamc@156
|
398 S.map2 (con ctx c,
|
adamc@156
|
399 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
400 fn xncs' =>
|
adamc@156
|
401 (SgiDatatype (x, n, xncs'), loc))
|
adamc@162
|
402 | SgiDatatypeImp (x, n, m1, ms, s, xncs) =>
|
adamc@162
|
403 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
404 case c of
|
adamc@162
|
405 NONE => S.return2 (x, n, c)
|
adamc@162
|
406 | SOME c =>
|
adamc@162
|
407 S.map2 (con ctx c,
|
adamc@162
|
408 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
409 fn xncs' =>
|
adamc@162
|
410 (SgiDatatypeImp (x, n, m1, ms, s, xncs'), loc))
|
adamc@34
|
411 | SgiVal (x, n, c) =>
|
adamc@34
|
412 S.map2 (con ctx c,
|
adamc@34
|
413 fn c' =>
|
adamc@34
|
414 (SgiVal (x, n, c'), loc))
|
adamc@34
|
415 | SgiStr (x, n, s) =>
|
adamc@34
|
416 S.map2 (sg ctx s,
|
adamc@34
|
417 fn s' =>
|
adamc@34
|
418 (SgiStr (x, n, s'), loc))
|
adamc@59
|
419 | SgiSgn (x, n, s) =>
|
adamc@59
|
420 S.map2 (sg ctx s,
|
adamc@59
|
421 fn s' =>
|
adamc@59
|
422 (SgiSgn (x, n, s'), loc))
|
adamc@88
|
423 | SgiConstraint (c1, c2) =>
|
adamc@88
|
424 S.bind2 (con ctx c1,
|
adamc@88
|
425 fn c1' =>
|
adamc@88
|
426 S.map2 (con ctx c2,
|
adamc@88
|
427 fn c2' =>
|
adamc@88
|
428 (SgiConstraint (c1', c2'), loc)))
|
adamc@34
|
429
|
adamc@34
|
430 and sg ctx s acc =
|
adamc@34
|
431 S.bindP (sg' ctx s acc, sgn ctx)
|
adamc@34
|
432
|
adamc@34
|
433 and sg' ctx (sAll as (s, loc)) =
|
adamc@34
|
434 case s of
|
adamc@34
|
435 SgnConst sgis =>
|
adamc@34
|
436 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
|
adamc@34
|
437 (case #1 si of
|
adamc@34
|
438 SgiConAbs (x, _, k) =>
|
adamc@34
|
439 bind (ctx, NamedC (x, k))
|
adamc@34
|
440 | SgiCon (x, _, k, _) =>
|
adamc@34
|
441 bind (ctx, NamedC (x, k))
|
adamc@156
|
442 | SgiDatatype (x, n, xncs) =>
|
adamc@156
|
443 bind (ctx, NamedC (x, (KType, loc)))
|
adamc@162
|
444 | SgiDatatypeImp (x, _, _, _, _, _) =>
|
adamc@156
|
445 bind (ctx, NamedC (x, (KType, loc)))
|
adamc@34
|
446 | SgiVal _ => ctx
|
adamc@34
|
447 | SgiStr (x, _, sgn) =>
|
adamc@59
|
448 bind (ctx, Str (x, sgn))
|
adamc@59
|
449 | SgiSgn (x, _, sgn) =>
|
adamc@88
|
450 bind (ctx, Sgn (x, sgn))
|
adamc@88
|
451 | SgiConstraint _ => ctx,
|
adamc@34
|
452 sgi ctx si)) ctx sgis,
|
adamc@34
|
453 fn sgis' =>
|
adamc@34
|
454 (SgnConst sgis', loc))
|
adamc@34
|
455
|
adamc@34
|
456 | SgnVar _ => S.return2 sAll
|
adamc@41
|
457 | SgnFun (m, n, s1, s2) =>
|
adamc@41
|
458 S.bind2 (sg ctx s1,
|
adamc@41
|
459 fn s1' =>
|
adamc@41
|
460 S.map2 (sg (bind (ctx, Str (m, s1'))) s2,
|
adamc@41
|
461 fn s2' =>
|
adamc@41
|
462 (SgnFun (m, n, s1', s2'), loc)))
|
adamc@59
|
463 | SgnProj _ => S.return2 sAll
|
adamc@42
|
464 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
465 S.bind2 (sg ctx sgn,
|
adamc@42
|
466 fn sgn' =>
|
adamc@42
|
467 S.map2 (con ctx c,
|
adamc@42
|
468 fn c' =>
|
adamc@42
|
469 (SgnWhere (sgn', x, c'), loc)))
|
adamc@34
|
470 | SgnError => S.return2 sAll
|
adamc@34
|
471 in
|
adamc@34
|
472 sg
|
adamc@34
|
473 end
|
adamc@34
|
474
|
adamc@34
|
475 fun mapfold {kind, con, sgn_item, sgn} =
|
adamc@34
|
476 mapfoldB {kind = kind,
|
adamc@34
|
477 con = fn () => con,
|
adamc@34
|
478 sgn_item = fn () => sgn_item,
|
adamc@34
|
479 sgn = fn () => sgn,
|
adamc@34
|
480 bind = fn ((), _) => ()} ()
|
adamc@34
|
481
|
adamc@34
|
482 fun map {kind, con, sgn_item, sgn} s =
|
adamc@34
|
483 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
484 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@34
|
485 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
|
adamc@34
|
486 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
|
adamc@34
|
487 S.Return () => raise Fail "Elab_util.Sgn.map"
|
adamc@34
|
488 | S.Continue (s, ()) => s
|
adamc@34
|
489
|
adamc@2
|
490 end
|
adamc@34
|
491
|
adamc@76
|
492 structure Decl = struct
|
adamc@76
|
493
|
adamc@76
|
494 datatype binder =
|
adamc@76
|
495 RelC of string * Elab.kind
|
adamc@76
|
496 | NamedC of string * Elab.kind
|
adamc@76
|
497 | RelE of string * Elab.con
|
adamc@76
|
498 | NamedE of string * Elab.con
|
adamc@76
|
499 | Str of string * Elab.sgn
|
adamc@76
|
500 | Sgn of string * Elab.sgn
|
adamc@76
|
501
|
adamc@76
|
502 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
|
adamc@76
|
503 let
|
adamc@76
|
504 val mfk = Kind.mapfold fk
|
adamc@76
|
505
|
adamc@76
|
506 fun bind' (ctx, b) =
|
adamc@76
|
507 let
|
adamc@76
|
508 val b' = case b of
|
adamc@76
|
509 Con.Rel x => RelC x
|
adamc@76
|
510 | Con.Named x => NamedC x
|
adamc@76
|
511 in
|
adamc@76
|
512 bind (ctx, b')
|
adamc@76
|
513 end
|
adamc@76
|
514 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@76
|
515
|
adamc@76
|
516 fun bind' (ctx, b) =
|
adamc@76
|
517 let
|
adamc@76
|
518 val b' = case b of
|
adamc@76
|
519 Exp.RelC x => RelC x
|
adamc@76
|
520 | Exp.NamedC x => NamedC x
|
adamc@76
|
521 | Exp.RelE x => RelE x
|
adamc@76
|
522 | Exp.NamedE x => NamedE x
|
adamc@76
|
523 in
|
adamc@76
|
524 bind (ctx, b')
|
adamc@76
|
525 end
|
adamc@76
|
526 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
|
adamc@76
|
527
|
adamc@76
|
528 fun bind' (ctx, b) =
|
adamc@76
|
529 let
|
adamc@76
|
530 val b' = case b of
|
adamc@76
|
531 Sgn.RelC x => RelC x
|
adamc@76
|
532 | Sgn.NamedC x => NamedC x
|
adamc@76
|
533 | Sgn.Sgn x => Sgn x
|
adamc@76
|
534 | Sgn.Str x => Str x
|
adamc@76
|
535 in
|
adamc@76
|
536 bind (ctx, b')
|
adamc@76
|
537 end
|
adamc@76
|
538 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
|
adamc@76
|
539
|
adamc@76
|
540 fun mfst ctx str acc =
|
adamc@76
|
541 S.bindP (mfst' ctx str acc, fst ctx)
|
adamc@76
|
542
|
adamc@76
|
543 and mfst' ctx (strAll as (str, loc)) =
|
adamc@76
|
544 case str of
|
adamc@76
|
545 StrConst ds =>
|
adamc@76
|
546 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
|
adamc@76
|
547 (case #1 d of
|
adamc@76
|
548 DCon (x, _, k, _) =>
|
adamc@76
|
549 bind (ctx, NamedC (x, k))
|
adamc@156
|
550 | DDatatype (x, n, xncs) =>
|
adamc@156
|
551 let
|
adamc@156
|
552 val ctx = bind (ctx, NamedC (x, (KType, loc)))
|
adamc@156
|
553 in
|
adamc@156
|
554 foldl (fn ((x, _, co), ctx) =>
|
adamc@156
|
555 let
|
adamc@156
|
556 val t =
|
adamc@156
|
557 case co of
|
adamc@156
|
558 NONE => CNamed n
|
adamc@156
|
559 | SOME t => TFun (t, (CNamed n, loc))
|
adamc@156
|
560 in
|
adamc@156
|
561 bind (ctx, NamedE (x, (t, loc)))
|
adamc@156
|
562 end)
|
adamc@156
|
563 ctx xncs
|
adamc@156
|
564 end
|
adamc@162
|
565 | DDatatypeImp (x, n, m, ms, x', _) =>
|
adamc@156
|
566 bind (ctx, NamedC (x, (KType, loc)))
|
adamc@76
|
567 | DVal (x, _, c, _) =>
|
adamc@76
|
568 bind (ctx, NamedE (x, c))
|
adamc@123
|
569 | DValRec vis =>
|
adamc@123
|
570 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
|
adamc@76
|
571 | DSgn (x, _, sgn) =>
|
adamc@76
|
572 bind (ctx, Sgn (x, sgn))
|
adamc@76
|
573 | DStr (x, _, sgn, _) =>
|
adamc@76
|
574 bind (ctx, Str (x, sgn))
|
adamc@76
|
575 | DFfiStr (x, _, sgn) =>
|
adamc@88
|
576 bind (ctx, Str (x, sgn))
|
adamc@100
|
577 | DConstraint _ => ctx
|
adamc@109
|
578 | DExport _ => ctx,
|
adamc@76
|
579 mfd ctx d)) ctx ds,
|
adamc@76
|
580 fn ds' => (StrConst ds', loc))
|
adamc@76
|
581 | StrVar _ => S.return2 strAll
|
adamc@76
|
582 | StrProj (str, x) =>
|
adamc@76
|
583 S.map2 (mfst ctx str,
|
adamc@76
|
584 fn str' =>
|
adamc@76
|
585 (StrProj (str', x), loc))
|
adamc@76
|
586 | StrFun (x, n, sgn1, sgn2, str) =>
|
adamc@76
|
587 S.bind2 (mfsg ctx sgn1,
|
adamc@76
|
588 fn sgn1' =>
|
adamc@76
|
589 S.bind2 (mfsg ctx sgn2,
|
adamc@76
|
590 fn sgn2' =>
|
adamc@76
|
591 S.map2 (mfst ctx str,
|
adamc@76
|
592 fn str' =>
|
adamc@76
|
593 (StrFun (x, n, sgn1', sgn2', str'), loc))))
|
adamc@76
|
594 | StrApp (str1, str2) =>
|
adamc@76
|
595 S.bind2 (mfst ctx str1,
|
adamc@76
|
596 fn str1' =>
|
adamc@76
|
597 S.map2 (mfst ctx str2,
|
adamc@76
|
598 fn str2' =>
|
adamc@76
|
599 (StrApp (str1', str2'), loc)))
|
adamc@76
|
600 | StrError => S.return2 strAll
|
adamc@76
|
601
|
adamc@76
|
602 and mfd ctx d acc =
|
adamc@76
|
603 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@76
|
604
|
adamc@76
|
605 and mfd' ctx (dAll as (d, loc)) =
|
adamc@76
|
606 case d of
|
adamc@76
|
607 DCon (x, n, k, c) =>
|
adamc@76
|
608 S.bind2 (mfk k,
|
adamc@76
|
609 fn k' =>
|
adamc@76
|
610 S.map2 (mfc ctx c,
|
adamc@76
|
611 fn c' =>
|
adamc@76
|
612 (DCon (x, n, k', c'), loc)))
|
adamc@156
|
613 | DDatatype (x, n, xncs) =>
|
adamc@156
|
614 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
615 case c of
|
adamc@156
|
616 NONE => S.return2 (x, n, c)
|
adamc@156
|
617 | SOME c =>
|
adamc@156
|
618 S.map2 (mfc ctx c,
|
adamc@156
|
619 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
620 fn xncs' =>
|
adamc@156
|
621 (DDatatype (x, n, xncs'), loc))
|
adamc@162
|
622 | DDatatypeImp (x, n, m1, ms, s, xncs) =>
|
adamc@162
|
623 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
624 case c of
|
adamc@162
|
625 NONE => S.return2 (x, n, c)
|
adamc@162
|
626 | SOME c =>
|
adamc@162
|
627 S.map2 (mfc ctx c,
|
adamc@162
|
628 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
629 fn xncs' =>
|
adamc@162
|
630 (DDatatypeImp (x, n, m1, ms, s, xncs'), loc))
|
adamc@123
|
631 | DVal vi =>
|
adamc@123
|
632 S.map2 (mfvi ctx vi,
|
adamc@123
|
633 fn vi' =>
|
adamc@123
|
634 (DVal vi', loc))
|
adamc@123
|
635 | DValRec vis =>
|
adamc@123
|
636 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@123
|
637 fn vis' =>
|
adamc@123
|
638 (DValRec vis', loc))
|
adamc@76
|
639 | DSgn (x, n, sgn) =>
|
adamc@76
|
640 S.map2 (mfsg ctx sgn,
|
adamc@76
|
641 fn sgn' =>
|
adamc@76
|
642 (DSgn (x, n, sgn'), loc))
|
adamc@76
|
643 | DStr (x, n, sgn, str) =>
|
adamc@76
|
644 S.bind2 (mfsg ctx sgn,
|
adamc@76
|
645 fn sgn' =>
|
adamc@76
|
646 S.map2 (mfst ctx str,
|
adamc@76
|
647 fn str' =>
|
adamc@76
|
648 (DStr (x, n, sgn', str'), loc)))
|
adamc@76
|
649 | DFfiStr (x, n, sgn) =>
|
adamc@76
|
650 S.map2 (mfsg ctx sgn,
|
adamc@76
|
651 fn sgn' =>
|
adamc@76
|
652 (DFfiStr (x, n, sgn'), loc))
|
adamc@88
|
653 | DConstraint (c1, c2) =>
|
adamc@88
|
654 S.bind2 (mfc ctx c1,
|
adamc@88
|
655 fn c1' =>
|
adamc@88
|
656 S.map2 (mfc ctx c2,
|
adamc@88
|
657 fn c2' =>
|
adamc@88
|
658 (DConstraint (c1', c2'), loc)))
|
adamc@109
|
659 | DExport (en, sgn, str) =>
|
adamc@109
|
660 S.bind2 (mfsg ctx sgn,
|
adamc@109
|
661 fn sgn' =>
|
adamc@109
|
662 S.map2 (mfst ctx str,
|
adamc@109
|
663 fn str' =>
|
adamc@109
|
664 (DExport (en, sgn', str'), loc)))
|
adamc@123
|
665
|
adamc@123
|
666 and mfvi ctx (x, n, c, e) =
|
adamc@123
|
667 S.bind2 (mfc ctx c,
|
adamc@123
|
668 fn c' =>
|
adamc@123
|
669 S.map2 (mfe ctx e,
|
adamc@123
|
670 fn e' =>
|
adamc@123
|
671 (x, n, c', e')))
|
adamc@76
|
672 in
|
adamc@76
|
673 mfd
|
adamc@76
|
674 end
|
adamc@76
|
675
|
adamc@76
|
676 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
|
adamc@76
|
677 mapfoldB {kind = kind,
|
adamc@76
|
678 con = fn () => con,
|
adamc@76
|
679 exp = fn () => exp,
|
adamc@76
|
680 sgn_item = fn () => sgn_item,
|
adamc@76
|
681 sgn = fn () => sgn,
|
adamc@76
|
682 str = fn () => str,
|
adamc@76
|
683 decl = fn () => decl,
|
adamc@76
|
684 bind = fn ((), _) => ()} ()
|
adamc@76
|
685
|
adamc@76
|
686 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
687 case mapfold {kind = fn k => fn () =>
|
adamc@76
|
688 if kind k then
|
adamc@76
|
689 S.Return ()
|
adamc@76
|
690 else
|
adamc@76
|
691 S.Continue (k, ()),
|
adamc@76
|
692 con = fn c => fn () =>
|
adamc@76
|
693 if con c then
|
adamc@76
|
694 S.Return ()
|
adamc@76
|
695 else
|
adamc@76
|
696 S.Continue (c, ()),
|
adamc@76
|
697 exp = fn e => fn () =>
|
adamc@76
|
698 if exp e then
|
adamc@76
|
699 S.Return ()
|
adamc@76
|
700 else
|
adamc@76
|
701 S.Continue (e, ()),
|
adamc@76
|
702 sgn_item = fn sgi => fn () =>
|
adamc@76
|
703 if sgn_item sgi then
|
adamc@76
|
704 S.Return ()
|
adamc@76
|
705 else
|
adamc@76
|
706 S.Continue (sgi, ()),
|
adamc@76
|
707 sgn = fn x => fn () =>
|
adamc@76
|
708 if sgn x then
|
adamc@76
|
709 S.Return ()
|
adamc@76
|
710 else
|
adamc@76
|
711 S.Continue (x, ()),
|
adamc@76
|
712 str = fn x => fn () =>
|
adamc@76
|
713 if str x then
|
adamc@76
|
714 S.Return ()
|
adamc@76
|
715 else
|
adamc@76
|
716 S.Continue (x, ()),
|
adamc@76
|
717 decl = fn x => fn () =>
|
adamc@76
|
718 if decl x then
|
adamc@76
|
719 S.Return ()
|
adamc@76
|
720 else
|
adamc@76
|
721 S.Continue (x, ())} k () of
|
adamc@76
|
722 S.Return _ => true
|
adamc@76
|
723 | S.Continue _ => false
|
adamc@76
|
724
|
adamc@76
|
725 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
726 case mapfold {kind = fn x => fn () =>
|
adamc@76
|
727 case kind x of
|
adamc@76
|
728 NONE => S.Continue (x, ())
|
adamc@76
|
729 | SOME v => S.Return v,
|
adamc@76
|
730
|
adamc@76
|
731 con = fn x => fn () =>
|
adamc@76
|
732 case con x of
|
adamc@76
|
733 NONE => S.Continue (x, ())
|
adamc@76
|
734 | SOME v => S.Return v,
|
adamc@76
|
735
|
adamc@76
|
736 exp = fn x => fn () =>
|
adamc@76
|
737 case exp x of
|
adamc@76
|
738 NONE => S.Continue (x, ())
|
adamc@76
|
739 | SOME v => S.Return v,
|
adamc@76
|
740
|
adamc@76
|
741 sgn_item = fn x => fn () =>
|
adamc@76
|
742 case sgn_item x of
|
adamc@76
|
743 NONE => S.Continue (x, ())
|
adamc@76
|
744 | SOME v => S.Return v,
|
adamc@76
|
745
|
adamc@76
|
746 sgn = fn x => fn () =>
|
adamc@76
|
747 case sgn x of
|
adamc@76
|
748 NONE => S.Continue (x, ())
|
adamc@76
|
749 | SOME v => S.Return v,
|
adamc@76
|
750
|
adamc@76
|
751 str = fn x => fn () =>
|
adamc@76
|
752 case str x of
|
adamc@76
|
753 NONE => S.Continue (x, ())
|
adamc@76
|
754 | SOME v => S.Return v,
|
adamc@76
|
755
|
adamc@76
|
756 decl = fn x => fn () =>
|
adamc@76
|
757 case decl x of
|
adamc@76
|
758 NONE => S.Continue (x, ())
|
adamc@76
|
759 | SOME v => S.Return v
|
adamc@76
|
760
|
adamc@76
|
761 } k () of
|
adamc@76
|
762 S.Return x => SOME x
|
adamc@76
|
763 | S.Continue _ => NONE
|
adamc@76
|
764
|
adamc@34
|
765 end
|
adamc@76
|
766
|
adamc@76
|
767 end
|