adamc@16
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@16
|
2 * All rights reserved.
|
adamc@16
|
3 *
|
adamc@16
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@16
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@16
|
6 *
|
adamc@16
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@16
|
8 * this list of conditions and the following disclaimer.
|
adamc@16
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@16
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@16
|
11 * and/or other materials provided with the distribution.
|
adamc@16
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@16
|
13 * derived from this software without specific prior written permission.
|
adamc@16
|
14 *
|
adamc@16
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@16
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@16
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@16
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@16
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@16
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@16
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@16
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@16
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@16
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@16
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@16
|
26 *)
|
adamc@16
|
27
|
adamc@16
|
28 structure CoreUtil :> CORE_UTIL = struct
|
adamc@16
|
29
|
adamc@16
|
30 open Core
|
adamc@16
|
31
|
adamc@16
|
32 structure S = Search
|
adamc@16
|
33
|
adamc@16
|
34 structure Kind = struct
|
adamc@16
|
35
|
adamc@16
|
36 fun mapfold f =
|
adamc@16
|
37 let
|
adamc@16
|
38 fun mfk k acc =
|
adamc@16
|
39 S.bindP (mfk' k acc, f)
|
adamc@16
|
40
|
adamc@16
|
41 and mfk' (kAll as (k, loc)) =
|
adamc@16
|
42 case k of
|
adamc@16
|
43 KType => S.return2 kAll
|
adamc@16
|
44
|
adamc@16
|
45 | KArrow (k1, k2) =>
|
adamc@16
|
46 S.bind2 (mfk k1,
|
adamc@16
|
47 fn k1' =>
|
adamc@16
|
48 S.map2 (mfk k2,
|
adamc@16
|
49 fn k2' =>
|
adamc@16
|
50 (KArrow (k1', k2'), loc)))
|
adamc@16
|
51
|
adamc@16
|
52 | KName => S.return2 kAll
|
adamc@16
|
53
|
adamc@16
|
54 | KRecord k =>
|
adamc@16
|
55 S.map2 (mfk k,
|
adamc@16
|
56 fn k' =>
|
adamc@16
|
57 (KRecord k', loc))
|
adamc@16
|
58 in
|
adamc@16
|
59 mfk
|
adamc@16
|
60 end
|
adamc@16
|
61
|
adamc@16
|
62 fun map f k =
|
adamc@16
|
63 case mapfold (fn k => fn () => S.Continue (f k, ())) k () of
|
adamc@16
|
64 S.Return () => raise Fail "Core_util.Kind.map"
|
adamc@16
|
65 | S.Continue (k, ()) => k
|
adamc@16
|
66
|
adamc@16
|
67 fun exists f k =
|
adamc@16
|
68 case mapfold (fn k => fn () =>
|
adamc@16
|
69 if f k then
|
adamc@16
|
70 S.Return ()
|
adamc@16
|
71 else
|
adamc@16
|
72 S.Continue (k, ())) k () of
|
adamc@16
|
73 S.Return _ => true
|
adamc@16
|
74 | S.Continue _ => false
|
adamc@16
|
75
|
adamc@16
|
76 end
|
adamc@16
|
77
|
adamc@16
|
78 structure Con = struct
|
adamc@16
|
79
|
adamc@16
|
80 datatype binder =
|
adamc@16
|
81 Rel of string * kind
|
adamc@16
|
82 | Named of string * kind
|
adamc@16
|
83
|
adamc@16
|
84 fun mapfoldB {kind = fk, con = fc, bind} =
|
adamc@16
|
85 let
|
adamc@16
|
86 val mfk = Kind.mapfold fk
|
adamc@16
|
87
|
adamc@16
|
88 fun mfc ctx c acc =
|
adamc@16
|
89 S.bindP (mfc' ctx c acc, fc ctx)
|
adamc@16
|
90
|
adamc@16
|
91 and mfc' ctx (cAll as (c, loc)) =
|
adamc@16
|
92 case c of
|
adamc@16
|
93 TFun (c1, c2) =>
|
adamc@16
|
94 S.bind2 (mfc ctx c1,
|
adamc@16
|
95 fn c1' =>
|
adamc@16
|
96 S.map2 (mfc ctx c2,
|
adamc@16
|
97 fn c2' =>
|
adamc@16
|
98 (TFun (c1', c2'), loc)))
|
adamc@16
|
99 | TCFun (x, k, c) =>
|
adamc@16
|
100 S.bind2 (mfk k,
|
adamc@16
|
101 fn k' =>
|
adamc@16
|
102 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@16
|
103 fn c' =>
|
adamc@16
|
104 (TCFun (x, k', c'), loc)))
|
adamc@16
|
105 | TRecord c =>
|
adamc@16
|
106 S.map2 (mfc ctx c,
|
adamc@16
|
107 fn c' =>
|
adamc@16
|
108 (TRecord c', loc))
|
adamc@16
|
109
|
adamc@16
|
110 | CRel _ => S.return2 cAll
|
adamc@16
|
111 | CNamed _ => S.return2 cAll
|
adamc@16
|
112 | CApp (c1, c2) =>
|
adamc@16
|
113 S.bind2 (mfc ctx c1,
|
adamc@16
|
114 fn c1' =>
|
adamc@16
|
115 S.map2 (mfc ctx c2,
|
adamc@16
|
116 fn c2' =>
|
adamc@16
|
117 (CApp (c1', c2'), loc)))
|
adamc@16
|
118 | CAbs (x, k, c) =>
|
adamc@16
|
119 S.bind2 (mfk k,
|
adamc@16
|
120 fn k' =>
|
adamc@16
|
121 S.map2 (mfc (bind (ctx, Rel (x, k))) c,
|
adamc@16
|
122 fn c' =>
|
adamc@16
|
123 (CAbs (x, k', c'), loc)))
|
adamc@16
|
124
|
adamc@16
|
125 | CName _ => S.return2 cAll
|
adamc@16
|
126
|
adamc@16
|
127 | CRecord (k, xcs) =>
|
adamc@16
|
128 S.bind2 (mfk k,
|
adamc@16
|
129 fn k' =>
|
adamc@16
|
130 S.map2 (ListUtil.mapfold (fn (x, c) =>
|
adamc@16
|
131 S.bind2 (mfc ctx x,
|
adamc@16
|
132 fn x' =>
|
adamc@16
|
133 S.map2 (mfc ctx c,
|
adamc@16
|
134 fn c' =>
|
adamc@16
|
135 (x', c'))))
|
adamc@16
|
136 xcs,
|
adamc@16
|
137 fn xcs' =>
|
adamc@16
|
138 (CRecord (k', xcs'), loc)))
|
adamc@16
|
139 | CConcat (c1, c2) =>
|
adamc@16
|
140 S.bind2 (mfc ctx c1,
|
adamc@16
|
141 fn c1' =>
|
adamc@16
|
142 S.map2 (mfc ctx c2,
|
adamc@16
|
143 fn c2' =>
|
adamc@16
|
144 (CConcat (c1', c2'), loc)))
|
adamc@16
|
145 in
|
adamc@16
|
146 mfc
|
adamc@16
|
147 end
|
adamc@16
|
148
|
adamc@16
|
149 fun mapfold {kind = fk, con = fc} =
|
adamc@16
|
150 mapfoldB {kind = fk,
|
adamc@16
|
151 con = fn () => fc,
|
adamc@16
|
152 bind = fn ((), _) => ()} ()
|
adamc@16
|
153
|
adamc@16
|
154 fun map {kind, con} c =
|
adamc@16
|
155 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@16
|
156 con = fn c => fn () => S.Continue (con c, ())} c () of
|
adamc@16
|
157 S.Return () => raise Fail "Core_util.Con.map"
|
adamc@16
|
158 | S.Continue (c, ()) => c
|
adamc@16
|
159
|
adamc@16
|
160 fun mapB {kind, con, bind} ctx c =
|
adamc@16
|
161 case mapfoldB {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@16
|
162 con = fn ctx => fn c => fn () => S.Continue (con ctx c, ()),
|
adamc@16
|
163 bind = bind} ctx c () of
|
adamc@16
|
164 S.Continue (c, ()) => c
|
adamc@16
|
165 | S.Return _ => raise Fail "Con.mapB: Impossible"
|
adamc@16
|
166
|
adamc@16
|
167 fun exists {kind, con} k =
|
adamc@16
|
168 case mapfold {kind = fn k => fn () =>
|
adamc@16
|
169 if kind k then
|
adamc@16
|
170 S.Return ()
|
adamc@16
|
171 else
|
adamc@16
|
172 S.Continue (k, ()),
|
adamc@16
|
173 con = fn c => fn () =>
|
adamc@16
|
174 if con c then
|
adamc@16
|
175 S.Return ()
|
adamc@16
|
176 else
|
adamc@16
|
177 S.Continue (c, ())} k () of
|
adamc@16
|
178 S.Return _ => true
|
adamc@16
|
179 | S.Continue _ => false
|
adamc@16
|
180
|
adamc@16
|
181 end
|
adamc@16
|
182
|
adamc@16
|
183 structure Exp = struct
|
adamc@16
|
184
|
adamc@16
|
185 datatype binder =
|
adamc@16
|
186 RelC of string * kind
|
adamc@16
|
187 | NamedC of string * kind
|
adamc@16
|
188 | RelE of string * con
|
adamc@16
|
189 | NamedE of string * con
|
adamc@16
|
190
|
adamc@16
|
191 fun mapfoldB {kind = fk, con = fc, exp = fe, bind} =
|
adamc@16
|
192 let
|
adamc@16
|
193 val mfk = Kind.mapfold fk
|
adamc@16
|
194
|
adamc@16
|
195 fun bind' (ctx, b) =
|
adamc@16
|
196 let
|
adamc@16
|
197 val b' = case b of
|
adamc@16
|
198 Con.Rel x => RelC x
|
adamc@16
|
199 | Con.Named x => NamedC x
|
adamc@16
|
200 in
|
adamc@16
|
201 bind (ctx, b')
|
adamc@16
|
202 end
|
adamc@16
|
203 val mfc = Con.mapfoldB {kind = fk, con = fc, bind = bind'}
|
adamc@16
|
204
|
adamc@16
|
205 fun mfe ctx e acc =
|
adamc@16
|
206 S.bindP (mfe' ctx e acc, fe ctx)
|
adamc@16
|
207
|
adamc@16
|
208 and mfe' ctx (eAll as (e, loc)) =
|
adamc@16
|
209 case e of
|
adamc@16
|
210 EPrim _ => S.return2 eAll
|
adamc@16
|
211 | ERel _ => S.return2 eAll
|
adamc@16
|
212 | ENamed _ => S.return2 eAll
|
adamc@16
|
213 | EApp (e1, e2) =>
|
adamc@16
|
214 S.bind2 (mfe ctx e1,
|
adamc@16
|
215 fn e1' =>
|
adamc@16
|
216 S.map2 (mfe ctx e2,
|
adamc@16
|
217 fn e2' =>
|
adamc@16
|
218 (EApp (e1', e2'), loc)))
|
adamc@16
|
219 | EAbs (x, t, e) =>
|
adamc@16
|
220 S.bind2 (mfc ctx t,
|
adamc@16
|
221 fn t' =>
|
adamc@16
|
222 S.map2 (mfe (bind (ctx, RelE (x, t))) e,
|
adamc@16
|
223 fn e' =>
|
adamc@16
|
224 (EAbs (x, t', e'), loc)))
|
adamc@16
|
225
|
adamc@16
|
226 | ECApp (e, c) =>
|
adamc@16
|
227 S.bind2 (mfe ctx e,
|
adamc@16
|
228 fn e' =>
|
adamc@16
|
229 S.map2 (mfc ctx c,
|
adamc@16
|
230 fn c' =>
|
adamc@16
|
231 (ECApp (e', c'), loc)))
|
adamc@16
|
232 | ECAbs (x, k, e) =>
|
adamc@16
|
233 S.bind2 (mfk k,
|
adamc@16
|
234 fn k' =>
|
adamc@16
|
235 S.map2 (mfe (bind (ctx, RelC (x, k))) e,
|
adamc@16
|
236 fn e' =>
|
adamc@16
|
237 (ECAbs (x, k', e'), loc)))
|
adamc@16
|
238
|
adamc@16
|
239 | ERecord xes =>
|
adamc@16
|
240 S.map2 (ListUtil.mapfold (fn (x, e) =>
|
adamc@16
|
241 S.bind2 (mfc ctx x,
|
adamc@16
|
242 fn x' =>
|
adamc@16
|
243 S.map2 (mfe ctx e,
|
adamc@16
|
244 fn e' =>
|
adamc@16
|
245 (x', e'))))
|
adamc@16
|
246 xes,
|
adamc@16
|
247 fn xes' =>
|
adamc@16
|
248 (ERecord xes', loc))
|
adamc@16
|
249 | EField (e, c, {field, rest}) =>
|
adamc@16
|
250 S.bind2 (mfe ctx e,
|
adamc@16
|
251 fn e' =>
|
adamc@16
|
252 S.bind2 (mfc ctx c,
|
adamc@16
|
253 fn c' =>
|
adamc@16
|
254 S.bind2 (mfc ctx field,
|
adamc@16
|
255 fn field' =>
|
adamc@16
|
256 S.map2 (mfc ctx rest,
|
adamc@16
|
257 fn rest' =>
|
adamc@16
|
258 (EField (e', c', {field = field', rest = rest'}), loc)))))
|
adamc@16
|
259 in
|
adamc@16
|
260 mfe
|
adamc@16
|
261 end
|
adamc@16
|
262
|
adamc@16
|
263 fun mapfold {kind = fk, con = fc, exp = fe} =
|
adamc@16
|
264 mapfoldB {kind = fk,
|
adamc@16
|
265 con = fn () => fc,
|
adamc@16
|
266 exp = fn () => fe,
|
adamc@16
|
267 bind = fn ((), _) => ()} ()
|
adamc@16
|
268
|
adamc@16
|
269 fun map {kind, con, exp} e =
|
adamc@16
|
270 case mapfold {kind = fn k => fn () => S.Continue (kind k, ()),
|
adamc@16
|
271 con = fn c => fn () => S.Continue (con c, ()),
|
adamc@16
|
272 exp = fn e => fn () => S.Continue (exp e, ())} e () of
|
adamc@16
|
273 S.Return () => raise Fail "Core_util.Exp.map"
|
adamc@16
|
274 | S.Continue (e, ()) => e
|
adamc@16
|
275
|
adamc@16
|
276 fun exists {kind, con, exp} k =
|
adamc@16
|
277 case mapfold {kind = fn k => fn () =>
|
adamc@16
|
278 if kind k then
|
adamc@16
|
279 S.Return ()
|
adamc@16
|
280 else
|
adamc@16
|
281 S.Continue (k, ()),
|
adamc@16
|
282 con = fn c => fn () =>
|
adamc@16
|
283 if con c then
|
adamc@16
|
284 S.Return ()
|
adamc@16
|
285 else
|
adamc@16
|
286 S.Continue (c, ()),
|
adamc@16
|
287 exp = fn e => fn () =>
|
adamc@16
|
288 if exp e then
|
adamc@16
|
289 S.Return ()
|
adamc@16
|
290 else
|
adamc@16
|
291 S.Continue (e, ())} k () of
|
adamc@16
|
292 S.Return _ => true
|
adamc@16
|
293 | S.Continue _ => false
|
adamc@16
|
294
|
adamc@16
|
295 end
|
adamc@16
|
296
|
adamc@16
|
297 end
|