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