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