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@34
|
439
|
adamc@34
|
440 and sg ctx s acc =
|
adamc@34
|
441 S.bindP (sg' ctx s acc, sgn ctx)
|
adamc@34
|
442
|
adamc@34
|
443 and sg' ctx (sAll as (s, loc)) =
|
adamc@34
|
444 case s of
|
adamc@34
|
445 SgnConst sgis =>
|
adamc@34
|
446 S.map2 (ListUtil.mapfoldB (fn (ctx, si) =>
|
adamc@34
|
447 (case #1 si of
|
adamc@34
|
448 SgiConAbs (x, _, k) =>
|
adamc@34
|
449 bind (ctx, NamedC (x, k))
|
adamc@34
|
450 | SgiCon (x, _, k, _) =>
|
adamc@34
|
451 bind (ctx, NamedC (x, k))
|
adamc@191
|
452 | SgiDatatype (x, n, _, xncs) =>
|
adamc@156
|
453 bind (ctx, NamedC (x, (KType, loc)))
|
adamc@191
|
454 | SgiDatatypeImp (x, _, _, _, _, _, _) =>
|
adamc@156
|
455 bind (ctx, NamedC (x, (KType, loc)))
|
adamc@34
|
456 | SgiVal _ => ctx
|
adamc@34
|
457 | SgiStr (x, _, sgn) =>
|
adamc@59
|
458 bind (ctx, Str (x, sgn))
|
adamc@59
|
459 | SgiSgn (x, _, sgn) =>
|
adamc@88
|
460 bind (ctx, Sgn (x, sgn))
|
adamc@88
|
461 | SgiConstraint _ => ctx,
|
adamc@34
|
462 sgi ctx si)) ctx sgis,
|
adamc@34
|
463 fn sgis' =>
|
adamc@34
|
464 (SgnConst sgis', loc))
|
adamc@34
|
465
|
adamc@34
|
466 | SgnVar _ => S.return2 sAll
|
adamc@41
|
467 | SgnFun (m, n, s1, s2) =>
|
adamc@41
|
468 S.bind2 (sg ctx s1,
|
adamc@41
|
469 fn s1' =>
|
adamc@41
|
470 S.map2 (sg (bind (ctx, Str (m, s1'))) s2,
|
adamc@41
|
471 fn s2' =>
|
adamc@41
|
472 (SgnFun (m, n, s1', s2'), loc)))
|
adamc@59
|
473 | SgnProj _ => S.return2 sAll
|
adamc@42
|
474 | SgnWhere (sgn, x, c) =>
|
adamc@42
|
475 S.bind2 (sg ctx sgn,
|
adamc@42
|
476 fn sgn' =>
|
adamc@42
|
477 S.map2 (con ctx c,
|
adamc@42
|
478 fn c' =>
|
adamc@42
|
479 (SgnWhere (sgn', x, c'), loc)))
|
adamc@34
|
480 | SgnError => S.return2 sAll
|
adamc@34
|
481 in
|
adamc@34
|
482 sg
|
adamc@34
|
483 end
|
adamc@34
|
484
|
adamc@34
|
485 fun mapfold {kind, con, sgn_item, sgn} =
|
adamc@34
|
486 mapfoldB {kind = kind,
|
adamc@34
|
487 con = fn () => con,
|
adamc@34
|
488 sgn_item = fn () => sgn_item,
|
adamc@34
|
489 sgn = fn () => sgn,
|
adamc@34
|
490 bind = fn ((), _) => ()} ()
|
adamc@34
|
491
|
adamc@34
|
492 fun map {kind, con, sgn_item, sgn} s =
|
adamc@34
|
493 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@34
|
494 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@34
|
495 sgn_item = fn si => fn () => S.Continue (sgn_item si, ()),
|
adamc@34
|
496 sgn = fn s => fn () => S.Continue (sgn s, ())} s () of
|
adamc@34
|
497 S.Return () => raise Fail "Elab_util.Sgn.map"
|
adamc@34
|
498 | S.Continue (s, ()) => s
|
adamc@34
|
499
|
adamc@2
|
500 end
|
adamc@34
|
501
|
adamc@76
|
502 structure Decl = struct
|
adamc@76
|
503
|
adamc@76
|
504 datatype binder =
|
adamc@76
|
505 RelC of string * Elab.kind
|
adamc@76
|
506 | NamedC of string * Elab.kind
|
adamc@76
|
507 | RelE of string * Elab.con
|
adamc@76
|
508 | NamedE of string * Elab.con
|
adamc@76
|
509 | Str of string * Elab.sgn
|
adamc@76
|
510 | Sgn of string * Elab.sgn
|
adamc@76
|
511
|
adamc@76
|
512 fun mapfoldB {kind = fk, con = fc, exp = fe, sgn_item = fsgi, sgn = fsg, str = fst, decl = fd, bind} =
|
adamc@76
|
513 let
|
adamc@76
|
514 val mfk = Kind.mapfold fk
|
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 Con.Rel x => RelC x
|
adamc@76
|
520 | Con.Named x => NamedC x
|
adamc@76
|
521 in
|
adamc@76
|
522 bind (ctx, b')
|
adamc@76
|
523 end
|
adamc@76
|
524 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@76
|
525
|
adamc@76
|
526 fun bind' (ctx, b) =
|
adamc@76
|
527 let
|
adamc@76
|
528 val b' = case b of
|
adamc@76
|
529 Exp.RelC x => RelC x
|
adamc@76
|
530 | Exp.NamedC x => NamedC x
|
adamc@76
|
531 | Exp.RelE x => RelE x
|
adamc@76
|
532 | Exp.NamedE x => NamedE x
|
adamc@76
|
533 in
|
adamc@76
|
534 bind (ctx, b')
|
adamc@76
|
535 end
|
adamc@76
|
536 val mfe = Exp.mapfoldB {kind = fk, con = fc, exp = fe, bind = bind'}
|
adamc@76
|
537
|
adamc@76
|
538 fun bind' (ctx, b) =
|
adamc@76
|
539 let
|
adamc@76
|
540 val b' = case b of
|
adamc@76
|
541 Sgn.RelC x => RelC x
|
adamc@76
|
542 | Sgn.NamedC x => NamedC x
|
adamc@76
|
543 | Sgn.Sgn x => Sgn x
|
adamc@76
|
544 | Sgn.Str x => Str x
|
adamc@76
|
545 in
|
adamc@76
|
546 bind (ctx, b')
|
adamc@76
|
547 end
|
adamc@76
|
548 val mfsg = Sgn.mapfoldB {kind = fk, con = fc, sgn_item = fsgi, sgn = fsg, bind = bind'}
|
adamc@76
|
549
|
adamc@76
|
550 fun mfst ctx str acc =
|
adamc@76
|
551 S.bindP (mfst' ctx str acc, fst ctx)
|
adamc@76
|
552
|
adamc@76
|
553 and mfst' ctx (strAll as (str, loc)) =
|
adamc@76
|
554 case str of
|
adamc@76
|
555 StrConst ds =>
|
adamc@76
|
556 S.map2 (ListUtil.mapfoldB (fn (ctx, d) =>
|
adamc@76
|
557 (case #1 d of
|
adamc@76
|
558 DCon (x, _, k, _) =>
|
adamc@76
|
559 bind (ctx, NamedC (x, k))
|
adamc@191
|
560 | DDatatype (x, n, xs, xncs) =>
|
adamc@156
|
561 let
|
adamc@156
|
562 val ctx = bind (ctx, NamedC (x, (KType, loc)))
|
adamc@156
|
563 in
|
adamc@156
|
564 foldl (fn ((x, _, co), ctx) =>
|
adamc@156
|
565 let
|
adamc@156
|
566 val t =
|
adamc@156
|
567 case co of
|
adamc@156
|
568 NONE => CNamed n
|
adamc@156
|
569 | SOME t => TFun (t, (CNamed n, loc))
|
adamc@191
|
570
|
adamc@191
|
571 val k = (KType, loc)
|
adamc@191
|
572 val t = (t, loc)
|
adamc@191
|
573 val t = foldr (fn (x, t) =>
|
adamc@191
|
574 (TCFun (Explicit,
|
adamc@191
|
575 x,
|
adamc@191
|
576 k,
|
adamc@191
|
577 t), loc))
|
adamc@191
|
578 t xs
|
adamc@156
|
579 in
|
adamc@191
|
580 bind (ctx, NamedE (x, t))
|
adamc@156
|
581 end)
|
adamc@156
|
582 ctx xncs
|
adamc@156
|
583 end
|
adamc@191
|
584 | DDatatypeImp (x, n, m, ms, x', _, _) =>
|
adamc@156
|
585 bind (ctx, NamedC (x, (KType, loc)))
|
adamc@76
|
586 | DVal (x, _, c, _) =>
|
adamc@76
|
587 bind (ctx, NamedE (x, c))
|
adamc@123
|
588 | DValRec vis =>
|
adamc@123
|
589 foldl (fn ((x, _, c, _), ctx) => bind (ctx, NamedE (x, c))) ctx vis
|
adamc@76
|
590 | DSgn (x, _, sgn) =>
|
adamc@76
|
591 bind (ctx, Sgn (x, sgn))
|
adamc@76
|
592 | DStr (x, _, sgn, _) =>
|
adamc@76
|
593 bind (ctx, Str (x, sgn))
|
adamc@76
|
594 | DFfiStr (x, _, sgn) =>
|
adamc@88
|
595 bind (ctx, Str (x, sgn))
|
adamc@100
|
596 | DConstraint _ => ctx
|
adamc@109
|
597 | DExport _ => ctx,
|
adamc@76
|
598 mfd ctx d)) ctx ds,
|
adamc@76
|
599 fn ds' => (StrConst ds', loc))
|
adamc@76
|
600 | StrVar _ => S.return2 strAll
|
adamc@76
|
601 | StrProj (str, x) =>
|
adamc@76
|
602 S.map2 (mfst ctx str,
|
adamc@76
|
603 fn str' =>
|
adamc@76
|
604 (StrProj (str', x), loc))
|
adamc@76
|
605 | StrFun (x, n, sgn1, sgn2, str) =>
|
adamc@76
|
606 S.bind2 (mfsg ctx sgn1,
|
adamc@76
|
607 fn sgn1' =>
|
adamc@76
|
608 S.bind2 (mfsg ctx sgn2,
|
adamc@76
|
609 fn sgn2' =>
|
adamc@76
|
610 S.map2 (mfst ctx str,
|
adamc@76
|
611 fn str' =>
|
adamc@76
|
612 (StrFun (x, n, sgn1', sgn2', str'), loc))))
|
adamc@76
|
613 | StrApp (str1, str2) =>
|
adamc@76
|
614 S.bind2 (mfst ctx str1,
|
adamc@76
|
615 fn str1' =>
|
adamc@76
|
616 S.map2 (mfst ctx str2,
|
adamc@76
|
617 fn str2' =>
|
adamc@76
|
618 (StrApp (str1', str2'), loc)))
|
adamc@76
|
619 | StrError => S.return2 strAll
|
adamc@76
|
620
|
adamc@76
|
621 and mfd ctx d acc =
|
adamc@76
|
622 S.bindP (mfd' ctx d acc, fd ctx)
|
adamc@76
|
623
|
adamc@76
|
624 and mfd' ctx (dAll as (d, loc)) =
|
adamc@76
|
625 case d of
|
adamc@76
|
626 DCon (x, n, k, c) =>
|
adamc@76
|
627 S.bind2 (mfk k,
|
adamc@76
|
628 fn k' =>
|
adamc@76
|
629 S.map2 (mfc ctx c,
|
adamc@76
|
630 fn c' =>
|
adamc@76
|
631 (DCon (x, n, k', c'), loc)))
|
adamc@191
|
632 | DDatatype (x, n, xs, xncs) =>
|
adamc@156
|
633 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@156
|
634 case c of
|
adamc@156
|
635 NONE => S.return2 (x, n, c)
|
adamc@156
|
636 | SOME c =>
|
adamc@156
|
637 S.map2 (mfc ctx c,
|
adamc@156
|
638 fn c' => (x, n, SOME c'))) xncs,
|
adamc@156
|
639 fn xncs' =>
|
adamc@191
|
640 (DDatatype (x, n, xs, xncs'), loc))
|
adamc@191
|
641 | DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@162
|
642 S.map2 (ListUtil.mapfold (fn (x, n, c) =>
|
adamc@162
|
643 case c of
|
adamc@162
|
644 NONE => S.return2 (x, n, c)
|
adamc@162
|
645 | SOME c =>
|
adamc@162
|
646 S.map2 (mfc ctx c,
|
adamc@162
|
647 fn c' => (x, n, SOME c'))) xncs,
|
adamc@162
|
648 fn xncs' =>
|
adamc@191
|
649 (DDatatypeImp (x, n, m1, ms, s, xs, xncs'), loc))
|
adamc@123
|
650 | DVal vi =>
|
adamc@123
|
651 S.map2 (mfvi ctx vi,
|
adamc@123
|
652 fn vi' =>
|
adamc@123
|
653 (DVal vi', loc))
|
adamc@123
|
654 | DValRec vis =>
|
adamc@123
|
655 S.map2 (ListUtil.mapfold (mfvi ctx) vis,
|
adamc@123
|
656 fn vis' =>
|
adamc@123
|
657 (DValRec vis', loc))
|
adamc@76
|
658 | DSgn (x, n, sgn) =>
|
adamc@76
|
659 S.map2 (mfsg ctx sgn,
|
adamc@76
|
660 fn sgn' =>
|
adamc@76
|
661 (DSgn (x, n, sgn'), loc))
|
adamc@76
|
662 | DStr (x, n, sgn, str) =>
|
adamc@76
|
663 S.bind2 (mfsg ctx sgn,
|
adamc@76
|
664 fn sgn' =>
|
adamc@76
|
665 S.map2 (mfst ctx str,
|
adamc@76
|
666 fn str' =>
|
adamc@76
|
667 (DStr (x, n, sgn', str'), loc)))
|
adamc@76
|
668 | DFfiStr (x, n, sgn) =>
|
adamc@76
|
669 S.map2 (mfsg ctx sgn,
|
adamc@76
|
670 fn sgn' =>
|
adamc@76
|
671 (DFfiStr (x, n, sgn'), loc))
|
adamc@88
|
672 | DConstraint (c1, c2) =>
|
adamc@88
|
673 S.bind2 (mfc ctx c1,
|
adamc@88
|
674 fn c1' =>
|
adamc@88
|
675 S.map2 (mfc ctx c2,
|
adamc@88
|
676 fn c2' =>
|
adamc@88
|
677 (DConstraint (c1', c2'), loc)))
|
adamc@109
|
678 | DExport (en, sgn, str) =>
|
adamc@109
|
679 S.bind2 (mfsg ctx sgn,
|
adamc@109
|
680 fn sgn' =>
|
adamc@109
|
681 S.map2 (mfst ctx str,
|
adamc@109
|
682 fn str' =>
|
adamc@109
|
683 (DExport (en, sgn', str'), loc)))
|
adamc@123
|
684
|
adamc@123
|
685 and mfvi ctx (x, n, c, e) =
|
adamc@123
|
686 S.bind2 (mfc ctx c,
|
adamc@123
|
687 fn c' =>
|
adamc@123
|
688 S.map2 (mfe ctx e,
|
adamc@123
|
689 fn e' =>
|
adamc@123
|
690 (x, n, c', e')))
|
adamc@76
|
691 in
|
adamc@76
|
692 mfd
|
adamc@76
|
693 end
|
adamc@76
|
694
|
adamc@76
|
695 fun mapfold {kind, con, exp, sgn_item, sgn, str, decl} =
|
adamc@76
|
696 mapfoldB {kind = kind,
|
adamc@76
|
697 con = fn () => con,
|
adamc@76
|
698 exp = fn () => exp,
|
adamc@76
|
699 sgn_item = fn () => sgn_item,
|
adamc@76
|
700 sgn = fn () => sgn,
|
adamc@76
|
701 str = fn () => str,
|
adamc@76
|
702 decl = fn () => decl,
|
adamc@76
|
703 bind = fn ((), _) => ()} ()
|
adamc@76
|
704
|
adamc@76
|
705 fun exists {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
706 case mapfold {kind = fn k => fn () =>
|
adamc@76
|
707 if kind k then
|
adamc@76
|
708 S.Return ()
|
adamc@76
|
709 else
|
adamc@76
|
710 S.Continue (k, ()),
|
adamc@76
|
711 con = fn c => fn () =>
|
adamc@76
|
712 if con c then
|
adamc@76
|
713 S.Return ()
|
adamc@76
|
714 else
|
adamc@76
|
715 S.Continue (c, ()),
|
adamc@76
|
716 exp = fn e => fn () =>
|
adamc@76
|
717 if exp e then
|
adamc@76
|
718 S.Return ()
|
adamc@76
|
719 else
|
adamc@76
|
720 S.Continue (e, ()),
|
adamc@76
|
721 sgn_item = fn sgi => fn () =>
|
adamc@76
|
722 if sgn_item sgi then
|
adamc@76
|
723 S.Return ()
|
adamc@76
|
724 else
|
adamc@76
|
725 S.Continue (sgi, ()),
|
adamc@76
|
726 sgn = fn x => fn () =>
|
adamc@76
|
727 if sgn x then
|
adamc@76
|
728 S.Return ()
|
adamc@76
|
729 else
|
adamc@76
|
730 S.Continue (x, ()),
|
adamc@76
|
731 str = fn x => fn () =>
|
adamc@76
|
732 if str x then
|
adamc@76
|
733 S.Return ()
|
adamc@76
|
734 else
|
adamc@76
|
735 S.Continue (x, ()),
|
adamc@76
|
736 decl = fn x => fn () =>
|
adamc@76
|
737 if decl x then
|
adamc@76
|
738 S.Return ()
|
adamc@76
|
739 else
|
adamc@76
|
740 S.Continue (x, ())} k () of
|
adamc@76
|
741 S.Return _ => true
|
adamc@76
|
742 | S.Continue _ => false
|
adamc@76
|
743
|
adamc@76
|
744 fun search {kind, con, exp, sgn_item, sgn, str, decl} k =
|
adamc@76
|
745 case mapfold {kind = fn x => fn () =>
|
adamc@76
|
746 case kind x of
|
adamc@76
|
747 NONE => S.Continue (x, ())
|
adamc@76
|
748 | SOME v => S.Return v,
|
adamc@76
|
749
|
adamc@76
|
750 con = fn x => fn () =>
|
adamc@76
|
751 case con x of
|
adamc@76
|
752 NONE => S.Continue (x, ())
|
adamc@76
|
753 | SOME v => S.Return v,
|
adamc@76
|
754
|
adamc@76
|
755 exp = fn x => fn () =>
|
adamc@76
|
756 case exp x of
|
adamc@76
|
757 NONE => S.Continue (x, ())
|
adamc@76
|
758 | SOME v => S.Return v,
|
adamc@76
|
759
|
adamc@76
|
760 sgn_item = fn x => fn () =>
|
adamc@76
|
761 case sgn_item x of
|
adamc@76
|
762 NONE => S.Continue (x, ())
|
adamc@76
|
763 | SOME v => S.Return v,
|
adamc@76
|
764
|
adamc@76
|
765 sgn = fn x => fn () =>
|
adamc@76
|
766 case sgn x of
|
adamc@76
|
767 NONE => S.Continue (x, ())
|
adamc@76
|
768 | SOME v => S.Return v,
|
adamc@76
|
769
|
adamc@76
|
770 str = fn x => fn () =>
|
adamc@76
|
771 case str x of
|
adamc@76
|
772 NONE => S.Continue (x, ())
|
adamc@76
|
773 | SOME v => S.Return v,
|
adamc@76
|
774
|
adamc@76
|
775 decl = fn x => fn () =>
|
adamc@76
|
776 case decl x of
|
adamc@76
|
777 NONE => S.Continue (x, ())
|
adamc@76
|
778 | SOME v => S.Return v
|
adamc@76
|
779
|
adamc@76
|
780 } k () of
|
adamc@76
|
781 S.Return x => SOME x
|
adamc@76
|
782 | S.Continue _ => NONE
|
adamc@76
|
783
|
adamc@34
|
784 end
|
adamc@76
|
785
|
adamc@76
|
786 end
|