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 Corify :> CORIFY = struct
|
adamc@16
|
29
|
adamc@16
|
30 structure EM = ErrorMsg
|
adamc@39
|
31 structure L = Expl
|
adamc@16
|
32 structure L' = Core
|
adamc@16
|
33
|
adamc@39
|
34 structure IM = IntBinaryMap
|
adamc@39
|
35 structure SM = BinaryMapFn(struct
|
adamc@39
|
36 type ord_key = string
|
adamc@39
|
37 val compare = String.compare
|
adamc@39
|
38 end)
|
adamc@39
|
39
|
adamc@39
|
40 local
|
adamc@39
|
41 val count = ref 0
|
adamc@39
|
42 in
|
adamc@39
|
43
|
adamc@39
|
44 fun reset v = count := v
|
adamc@39
|
45
|
adamc@39
|
46 fun alloc () =
|
adamc@39
|
47 let
|
adamc@39
|
48 val r = !count
|
adamc@39
|
49 in
|
adamc@39
|
50 count := r + 1;
|
adamc@39
|
51 r
|
adamc@39
|
52 end
|
adamc@39
|
53
|
adamc@39
|
54 end
|
adamc@39
|
55
|
adamc@39
|
56 structure St : sig
|
adamc@39
|
57 type t
|
adamc@39
|
58
|
adamc@39
|
59 val empty : t
|
adamc@39
|
60
|
adamc@39
|
61 val enter : t -> t
|
adamc@39
|
62 val leave : t -> {outer : t, inner : t}
|
adamc@49
|
63 val ffi : string -> L'.con SM.map -> t
|
adamc@39
|
64
|
adamc@73
|
65 datatype core_con =
|
adamc@73
|
66 CNormal of int
|
adamc@73
|
67 | CFfi of string
|
adamc@73
|
68 val bindCon : t -> string -> int -> t * int
|
adamc@73
|
69 val lookupConById : t -> int -> int option
|
adamc@73
|
70 val lookupConByName : t -> string -> core_con
|
adamc@48
|
71
|
adamc@73
|
72 datatype core_val =
|
adamc@73
|
73 ENormal of int
|
adamc@73
|
74 | EFfi of string * L'.con
|
adamc@73
|
75 val bindVal : t -> string -> int -> t * int
|
adamc@73
|
76 val lookupValById : t -> int -> int option
|
adamc@73
|
77 val lookupValByName : t -> string -> core_val
|
adamc@39
|
78
|
adamc@39
|
79 val bindStr : t -> string -> int -> t -> t
|
adamc@39
|
80 val lookupStrById : t -> int -> t
|
adamc@39
|
81 val lookupStrByName : string * t -> t
|
adamc@46
|
82
|
adamc@46
|
83 val bindFunctor : t -> string -> int -> int -> L.str -> t
|
adamc@46
|
84 val lookupFunctorById : t -> int -> int * L.str
|
adamc@46
|
85 val lookupFunctorByName : string * t -> int * L.str
|
adamc@39
|
86 end = struct
|
adamc@39
|
87
|
adamc@48
|
88 datatype flattening =
|
adamc@73
|
89 FNormal of {cons : int SM.map,
|
adamc@73
|
90 vals : int SM.map,
|
adamc@48
|
91 strs : flattening SM.map,
|
adamc@48
|
92 funs : (int * L.str) SM.map}
|
adamc@73
|
93 | FFfi of {mod : string,
|
adamc@73
|
94 vals : L'.con SM.map}
|
adamc@39
|
95
|
adamc@39
|
96 type t = {
|
adamc@73
|
97 cons : int IM.map,
|
adamc@73
|
98 vals : int IM.map,
|
adamc@39
|
99 strs : flattening IM.map,
|
adamc@46
|
100 funs : (int * L.str) IM.map,
|
adamc@39
|
101 current : flattening,
|
adamc@39
|
102 nested : flattening list
|
adamc@39
|
103 }
|
adamc@39
|
104
|
adamc@39
|
105 val empty = {
|
adamc@73
|
106 cons = IM.empty,
|
adamc@73
|
107 vals = IM.empty,
|
adamc@39
|
108 strs = IM.empty,
|
adamc@46
|
109 funs = IM.empty,
|
adamc@73
|
110 current = FNormal { cons = SM.empty, vals = SM.empty, strs = SM.empty, funs = SM.empty },
|
adamc@39
|
111 nested = []
|
adamc@39
|
112 }
|
adamc@39
|
113
|
adamc@73
|
114 datatype core_con =
|
adamc@73
|
115 CNormal of int
|
adamc@73
|
116 | CFfi of string
|
adamc@48
|
117
|
adamc@73
|
118 datatype core_val =
|
adamc@73
|
119 ENormal of int
|
adamc@73
|
120 | EFfi of string * L'.con
|
adamc@73
|
121
|
adamc@73
|
122 fun bindCon {cons, vals, strs, funs, current, nested} s n =
|
adamc@39
|
123 let
|
adamc@39
|
124 val n' = alloc ()
|
adamc@39
|
125
|
adamc@39
|
126 val current =
|
adamc@48
|
127 case current of
|
adamc@48
|
128 FFfi _ => raise Fail "Binding inside FFfi"
|
adamc@73
|
129 | FNormal {cons, vals, strs, funs} =>
|
adamc@73
|
130 FNormal {cons = SM.insert (cons, s, n'),
|
adamc@73
|
131 vals = vals,
|
adamc@48
|
132 strs = strs,
|
adamc@48
|
133 funs = funs}
|
adamc@39
|
134 in
|
adamc@73
|
135 ({cons = IM.insert (cons, n, n'),
|
adamc@73
|
136 vals = vals,
|
adamc@39
|
137 strs = strs,
|
adamc@46
|
138 funs = funs,
|
adamc@39
|
139 current = current,
|
adamc@39
|
140 nested = nested},
|
adamc@39
|
141 n')
|
adamc@39
|
142 end
|
adamc@39
|
143
|
adamc@73
|
144 fun lookupConById ({cons, ...} : t) n = IM.find (cons, n)
|
adamc@39
|
145
|
adamc@73
|
146 fun lookupConByName ({current, ...} : t) x =
|
adamc@48
|
147 case current of
|
adamc@73
|
148 FFfi {mod = m, ...} => CFfi m
|
adamc@73
|
149 | FNormal {cons, ...} =>
|
adamc@73
|
150 case SM.find (cons, x) of
|
adamc@73
|
151 NONE => raise Fail "Corify.St.lookupConByName"
|
adamc@73
|
152 | SOME n => CNormal n
|
adamc@39
|
153
|
adamc@73
|
154 fun bindVal {cons, vals, strs, funs, current, nested} s n =
|
adamc@73
|
155 let
|
adamc@73
|
156 val n' = alloc ()
|
adamc@73
|
157
|
adamc@73
|
158 val current =
|
adamc@73
|
159 case current of
|
adamc@73
|
160 FFfi _ => raise Fail "Binding inside FFfi"
|
adamc@73
|
161 | FNormal {cons, vals, strs, funs} =>
|
adamc@73
|
162 FNormal {cons = cons,
|
adamc@73
|
163 vals = SM.insert (vals, s, n'),
|
adamc@73
|
164 strs = strs,
|
adamc@73
|
165 funs = funs}
|
adamc@73
|
166 in
|
adamc@73
|
167 ({cons = cons,
|
adamc@73
|
168 vals = IM.insert (vals, n, n'),
|
adamc@73
|
169 strs = strs,
|
adamc@73
|
170 funs = funs,
|
adamc@73
|
171 current = current,
|
adamc@73
|
172 nested = nested},
|
adamc@73
|
173 n')
|
adamc@73
|
174 end
|
adamc@73
|
175
|
adamc@73
|
176 fun lookupValById ({vals, ...} : t) n = IM.find (vals, n)
|
adamc@73
|
177
|
adamc@73
|
178 fun lookupValByName ({current, ...} : t) x =
|
adamc@73
|
179 case current of
|
adamc@73
|
180 FFfi {mod = m, vals, ...} =>
|
adamc@73
|
181 (case SM.find (vals, x) of
|
adamc@73
|
182 NONE => raise Fail "Corify.St.lookupValByName: no type for FFI val"
|
adamc@73
|
183 | SOME t => EFfi (m, t))
|
adamc@73
|
184 | FNormal {vals, ...} =>
|
adamc@73
|
185 case SM.find (vals, x) of
|
adamc@73
|
186 NONE => raise Fail "Corify.St.lookupValByName"
|
adamc@73
|
187 | SOME n => ENormal n
|
adamc@73
|
188
|
adamc@73
|
189 fun enter {cons, vals, strs, funs, current, nested} =
|
adamc@73
|
190 {cons = cons,
|
adamc@73
|
191 vals = vals,
|
adamc@39
|
192 strs = strs,
|
adamc@46
|
193 funs = funs,
|
adamc@73
|
194 current = FNormal {cons = SM.empty,
|
adamc@73
|
195 vals = SM.empty,
|
adamc@48
|
196 strs = SM.empty,
|
adamc@48
|
197 funs = SM.empty},
|
adamc@39
|
198 nested = current :: nested}
|
adamc@39
|
199
|
adamc@73
|
200 fun dummy f = {cons = IM.empty,
|
adamc@73
|
201 vals = IM.empty,
|
adamc@39
|
202 strs = IM.empty,
|
adamc@46
|
203 funs = IM.empty,
|
adamc@39
|
204 current = f,
|
adamc@39
|
205 nested = []}
|
adamc@39
|
206
|
adamc@73
|
207 fun leave {cons, vals, strs, funs, current, nested = m1 :: rest} =
|
adamc@73
|
208 {outer = {cons = cons,
|
adamc@73
|
209 vals = vals,
|
adamc@39
|
210 strs = strs,
|
adamc@46
|
211 funs = funs,
|
adamc@39
|
212 current = m1,
|
adamc@39
|
213 nested = rest},
|
adamc@39
|
214 inner = dummy current}
|
adamc@39
|
215 | leave _ = raise Fail "Corify.St.leave"
|
adamc@39
|
216
|
adamc@73
|
217 fun ffi m vals = dummy (FFfi {mod = m, vals = vals})
|
adamc@48
|
218
|
adamc@73
|
219 fun bindStr ({cons, vals, strs, funs,
|
adamc@73
|
220 current = FNormal {cons = mcons, vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
|
adamc@46
|
221 x n ({current = f, ...} : t) =
|
adamc@73
|
222 {cons = cons,
|
adamc@73
|
223 vals = vals,
|
adamc@39
|
224 strs = IM.insert (strs, n, f),
|
adamc@46
|
225 funs = funs,
|
adamc@73
|
226 current = FNormal {cons = mcons,
|
adamc@73
|
227 vals = mvals,
|
adamc@73
|
228 strs = SM.insert (mstrs, x, f),
|
adamc@73
|
229 funs = mfuns},
|
adamc@39
|
230 nested = nested}
|
adamc@48
|
231 | bindStr _ _ _ _ = raise Fail "Corify.St.bindStr"
|
adamc@39
|
232
|
adamc@39
|
233 fun lookupStrById ({strs, ...} : t) n =
|
adamc@39
|
234 case IM.find (strs, n) of
|
adamc@46
|
235 NONE => raise Fail "Corify.St.lookupStrById"
|
adamc@39
|
236 | SOME f => dummy f
|
adamc@39
|
237
|
adamc@48
|
238 fun lookupStrByName (m, {current = FNormal {strs, ...}, ...} : t) =
|
adamc@48
|
239 (case SM.find (strs, m) of
|
adamc@48
|
240 NONE => raise Fail "Corify.St.lookupStrByName"
|
adamc@48
|
241 | SOME f => dummy f)
|
adamc@48
|
242 | lookupStrByName _ = raise Fail "Corify.St.lookupStrByName"
|
adamc@39
|
243
|
adamc@73
|
244 fun bindFunctor ({cons, vals, strs, funs,
|
adamc@73
|
245 current = FNormal {cons = mcons, vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
|
adamc@46
|
246 x n na str =
|
adamc@73
|
247 {cons = cons,
|
adamc@73
|
248 vals = vals,
|
adamc@46
|
249 strs = strs,
|
adamc@46
|
250 funs = IM.insert (funs, n, (na, str)),
|
adamc@73
|
251 current = FNormal {cons = mcons,
|
adamc@73
|
252 vals = mvals,
|
adamc@48
|
253 strs = mstrs,
|
adamc@48
|
254 funs = SM.insert (mfuns, x, (na, str))},
|
adamc@46
|
255 nested = nested}
|
adamc@48
|
256 | bindFunctor _ _ _ _ _ = raise Fail "Corify.St.bindFunctor"
|
adamc@46
|
257
|
adamc@46
|
258 fun lookupFunctorById ({funs, ...} : t) n =
|
adamc@46
|
259 case IM.find (funs, n) of
|
adamc@46
|
260 NONE => raise Fail "Corify.St.lookupFunctorById"
|
adamc@46
|
261 | SOME v => v
|
adamc@46
|
262
|
adamc@48
|
263 fun lookupFunctorByName (m, {current = FNormal {funs, ...}, ...} : t) =
|
adamc@48
|
264 (case SM.find (funs, m) of
|
adamc@48
|
265 NONE => raise Fail "Corify.St.lookupFunctorByName"
|
adamc@48
|
266 | SOME v => v)
|
adamc@48
|
267 | lookupFunctorByName _ = raise Fail "Corify.St.lookupFunctorByName"
|
adamc@46
|
268
|
adamc@39
|
269 end
|
adamc@39
|
270
|
adamc@39
|
271
|
adamc@16
|
272 fun corifyKind (k, loc) =
|
adamc@16
|
273 case k of
|
adamc@16
|
274 L.KType => (L'.KType, loc)
|
adamc@16
|
275 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
|
adamc@16
|
276 | L.KName => (L'.KName, loc)
|
adamc@16
|
277 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
|
adamc@87
|
278 | L.KUnit => (L'.KUnit, loc)
|
adamc@16
|
279
|
adamc@39
|
280 fun corifyCon st (c, loc) =
|
adamc@16
|
281 case c of
|
adamc@39
|
282 L.TFun (t1, t2) => (L'.TFun (corifyCon st t1, corifyCon st t2), loc)
|
adamc@39
|
283 | L.TCFun (x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon st t), loc)
|
adamc@39
|
284 | L.TRecord c => (L'.TRecord (corifyCon st c), loc)
|
adamc@16
|
285
|
adamc@16
|
286 | L.CRel n => (L'.CRel n, loc)
|
adamc@39
|
287 | L.CNamed n =>
|
adamc@73
|
288 (case St.lookupConById st n of
|
adamc@39
|
289 NONE => (L'.CNamed n, loc)
|
adamc@39
|
290 | SOME n => (L'.CNamed n, loc))
|
adamc@39
|
291 | L.CModProj (m, ms, x) =>
|
adamc@39
|
292 let
|
adamc@39
|
293 val st = St.lookupStrById st m
|
adamc@39
|
294 val st = foldl St.lookupStrByName st ms
|
adamc@39
|
295 in
|
adamc@73
|
296 case St.lookupConByName st x of
|
adamc@73
|
297 St.CNormal n => (L'.CNamed n, loc)
|
adamc@73
|
298 | St.CFfi m => (L'.CFfi (m, x), loc)
|
adamc@39
|
299 end
|
adamc@34
|
300
|
adamc@39
|
301 | L.CApp (c1, c2) => (L'.CApp (corifyCon st c1, corifyCon st c2), loc)
|
adamc@39
|
302 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon st c), loc)
|
adamc@16
|
303
|
adamc@16
|
304 | L.CName s => (L'.CName s, loc)
|
adamc@16
|
305
|
adamc@39
|
306 | L.CRecord (k, xcs) =>
|
adamc@39
|
307 (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon st c1, corifyCon st c2)) xcs), loc)
|
adamc@39
|
308 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon st c1, corifyCon st c2), loc)
|
adamc@69
|
309 | L.CFold (k1, k2) => (L'.CFold (corifyKind k1, corifyKind k2), loc)
|
adamc@87
|
310 | L.CUnit => (L'.CUnit, loc)
|
adamc@16
|
311
|
adamc@39
|
312 fun corifyExp st (e, loc) =
|
adamc@16
|
313 case e of
|
adamc@16
|
314 L.EPrim p => (L'.EPrim p, loc)
|
adamc@16
|
315 | L.ERel n => (L'.ERel n, loc)
|
adamc@39
|
316 | L.ENamed n =>
|
adamc@73
|
317 (case St.lookupValById st n of
|
adamc@39
|
318 NONE => (L'.ENamed n, loc)
|
adamc@39
|
319 | SOME n => (L'.ENamed n, loc))
|
adamc@39
|
320 | L.EModProj (m, ms, x) =>
|
adamc@39
|
321 let
|
adamc@39
|
322 val st = St.lookupStrById st m
|
adamc@39
|
323 val st = foldl St.lookupStrByName st ms
|
adamc@39
|
324 in
|
adamc@73
|
325 case St.lookupValByName st x of
|
adamc@73
|
326 St.ENormal n => (L'.ENamed n, loc)
|
adamc@73
|
327 | St.EFfi (m, t) =>
|
adamc@49
|
328 case t of
|
adamc@50
|
329 (L'.TFun (dom as (L'.TRecord (L'.CRecord (_, []), _), _), ran), _) =>
|
adamc@50
|
330 (L'.EAbs ("arg", dom, ran, (L'.EFfiApp (m, x, []), loc)), loc)
|
adamc@50
|
331 | t as (L'.TFun _, _) =>
|
adamc@49
|
332 let
|
adamc@49
|
333 fun getArgs (all as (t, _), args) =
|
adamc@49
|
334 case t of
|
adamc@49
|
335 L'.TFun (dom, ran) => getArgs (ran, dom :: args)
|
adamc@49
|
336 | _ => (all, rev args)
|
adamc@49
|
337
|
adamc@49
|
338 val (result, args) = getArgs (t, [])
|
adamc@49
|
339
|
adamc@50
|
340 val (actuals, _) = foldr (fn (_, (actuals, n)) =>
|
adamc@50
|
341 ((L'.ERel n, loc) :: actuals,
|
adamc@50
|
342 n + 1)) ([], 0) args
|
adamc@50
|
343 val app = (L'.EFfiApp (m, x, actuals), loc)
|
adamc@49
|
344 val (abs, _, _) = foldr (fn (t, (abs, ran, n)) =>
|
adamc@49
|
345 ((L'.EAbs ("arg" ^ Int.toString n,
|
adamc@49
|
346 t,
|
adamc@49
|
347 ran,
|
adamc@49
|
348 abs), loc),
|
adamc@49
|
349 (L'.TFun (t, ran), loc),
|
adamc@49
|
350 n - 1)) (app, result, length args - 1) args
|
adamc@49
|
351 in
|
adamc@49
|
352 abs
|
adamc@49
|
353 end
|
adamc@49
|
354 | _ => (L'.EFfi (m, x), loc)
|
adamc@39
|
355 end
|
adamc@39
|
356 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
|
adamc@39
|
357 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
|
adamc@39
|
358 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
|
adamc@39
|
359 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
|
adamc@16
|
360
|
adamc@39
|
361 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) => (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
|
adamc@39
|
362 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
|
adamc@39
|
363 {field = corifyCon st field, rest = corifyCon st rest}), loc)
|
adamc@73
|
364 | L.EFold k => (L'.EFold (corifyKind k), loc)
|
adamc@16
|
365
|
adamc@39
|
366 fun corifyDecl ((d, loc : EM.span), st) =
|
adamc@39
|
367 case d of
|
adamc@39
|
368 L.DCon (x, n, k, c) =>
|
adamc@39
|
369 let
|
adamc@73
|
370 val (st, n) = St.bindCon st x n
|
adamc@39
|
371 in
|
adamc@39
|
372 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
|
adamc@39
|
373 end
|
adamc@39
|
374 | L.DVal (x, n, t, e) =>
|
adamc@39
|
375 let
|
adamc@73
|
376 val (st, n) = St.bindVal st x n
|
adamc@39
|
377 in
|
adamc@39
|
378 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e), loc)], st)
|
adamc@39
|
379 end
|
adamc@39
|
380
|
adamc@39
|
381 | L.DSgn _ => ([], st)
|
adamc@16
|
382
|
adamc@46
|
383 | L.DStr (x, n, _, (L.StrFun (_, na, _, _, str), _)) =>
|
adamc@46
|
384 ([], St.bindFunctor st x n na str)
|
adamc@46
|
385
|
adamc@39
|
386 | L.DStr (x, n, _, str) =>
|
adamc@39
|
387 let
|
adamc@39
|
388 val (ds, {inner, outer}) = corifyStr (str, st)
|
adamc@39
|
389 val st = St.bindStr outer x n inner
|
adamc@39
|
390 in
|
adamc@39
|
391 (ds, st)
|
adamc@39
|
392 end
|
adamc@16
|
393
|
adamc@49
|
394 | L.DFfiStr (m, n, (sgn, _)) =>
|
adamc@49
|
395 (case sgn of
|
adamc@49
|
396 L.SgnConst sgis =>
|
adamc@49
|
397 let
|
adamc@49
|
398 val (ds, cmap, st) =
|
adamc@49
|
399 foldl (fn ((sgi, _), (ds, cmap, st)) =>
|
adamc@49
|
400 case sgi of
|
adamc@49
|
401 L.SgiConAbs (x, n, k) =>
|
adamc@49
|
402 let
|
adamc@73
|
403 val (st, n') = St.bindCon st x n
|
adamc@49
|
404 in
|
adamc@49
|
405 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
|
adamc@49
|
406 cmap,
|
adamc@49
|
407 st)
|
adamc@49
|
408 end
|
adamc@49
|
409 | L.SgiCon (x, n, k, _) =>
|
adamc@49
|
410 let
|
adamc@73
|
411 val (st, n') = St.bindCon st x n
|
adamc@49
|
412 in
|
adamc@49
|
413 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
|
adamc@49
|
414 cmap,
|
adamc@49
|
415 st)
|
adamc@49
|
416 end
|
adamc@49
|
417
|
adamc@49
|
418 | L.SgiVal (x, _, c) =>
|
adamc@49
|
419 (ds,
|
adamc@49
|
420 SM.insert (cmap, x, corifyCon st c),
|
adamc@49
|
421 st)
|
adamc@49
|
422 | _ => (ds, cmap, st)) ([], SM.empty, st) sgis
|
adamc@49
|
423
|
adamc@49
|
424 val st = St.bindStr st m n (St.ffi m cmap)
|
adamc@49
|
425 in
|
adamc@49
|
426 (rev ds, st)
|
adamc@49
|
427 end
|
adamc@49
|
428 | _ => raise Fail "Non-const signature for FFI structure")
|
adamc@48
|
429
|
adamc@102
|
430 | L.DPage (c, e) =>
|
adamc@102
|
431 let
|
adamc@102
|
432 val c = corifyCon st c
|
adamc@102
|
433 val e = corifyExp st e
|
adamc@102
|
434
|
adamc@102
|
435 val dom = (L'.TRecord c, loc)
|
adamc@102
|
436 val ran = (L'.TRecord (L'.CRecord ((L'.KType, loc), []), loc), loc)
|
adamc@102
|
437 val e = (L'.EAbs ("vs", dom, ran,
|
adamc@102
|
438 (L'.EWrite (L'.EApp (e, (L'.ERel 0, loc)), loc), loc)), loc)
|
adamc@102
|
439
|
adamc@102
|
440 in
|
adamc@102
|
441 ([(L'.DPage (c, e), loc)], st)
|
adamc@102
|
442 end
|
adamc@48
|
443
|
adamc@39
|
444 and corifyStr ((str, _), st) =
|
adamc@39
|
445 case str of
|
adamc@39
|
446 L.StrConst ds =>
|
adamc@39
|
447 let
|
adamc@39
|
448 val st = St.enter st
|
adamc@39
|
449 val (ds, st) = ListUtil.foldlMapConcat corifyDecl st ds
|
adamc@39
|
450 in
|
adamc@39
|
451 (ds, St.leave st)
|
adamc@39
|
452 end
|
adamc@39
|
453 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
|
adamc@39
|
454 | L.StrProj (str, x) =>
|
adamc@39
|
455 let
|
adamc@39
|
456 val (ds, {inner, outer}) = corifyStr (str, st)
|
adamc@39
|
457 in
|
adamc@39
|
458 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
|
adamc@39
|
459 end
|
adamc@46
|
460 | L.StrFun _ => raise Fail "Corify of nested functor definition"
|
adamc@46
|
461 | L.StrApp (str1, str2) =>
|
adamc@46
|
462 let
|
adamc@46
|
463 fun unwind' (str, _) =
|
adamc@46
|
464 case str of
|
adamc@46
|
465 L.StrVar n => St.lookupStrById st n
|
adamc@46
|
466 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
|
adamc@46
|
467 | _ => raise Fail "Corify of fancy functor application [1]"
|
adamc@46
|
468
|
adamc@46
|
469 fun unwind (str, _) =
|
adamc@46
|
470 case str of
|
adamc@46
|
471 L.StrVar n => St.lookupFunctorById st n
|
adamc@46
|
472 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
|
adamc@46
|
473 | _ => raise Fail "Corify of fancy functor application [2]"
|
adamc@46
|
474
|
adamc@46
|
475 val (na, body) = unwind str1
|
adamc@46
|
476
|
adamc@46
|
477 val (ds1, {inner, outer}) = corifyStr (str2, st)
|
adamc@46
|
478 val (ds2, sts) = corifyStr (body, St.bindStr outer "ARG" na inner)
|
adamc@46
|
479 in
|
adamc@46
|
480 (ds1 @ ds2, sts)
|
adamc@46
|
481 end
|
adamc@31
|
482
|
adamc@39
|
483 fun maxName ds = foldl (fn ((d, _), n) =>
|
adamc@39
|
484 case d of
|
adamc@39
|
485 L.DCon (_, n', _, _) => Int.max (n, n')
|
adamc@39
|
486 | L.DVal (_, n', _ , _) => Int.max (n, n')
|
adamc@39
|
487 | L.DSgn (_, n', _) => Int.max (n, n')
|
adamc@48
|
488 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
|
adamc@100
|
489 | L.DFfiStr (_, n', _) => Int.max (n, n')
|
adamc@100
|
490 | L.DPage _ => n)
|
adamc@39
|
491 0 ds
|
adamc@39
|
492
|
adamc@39
|
493 and maxNameStr (str, _) =
|
adamc@39
|
494 case str of
|
adamc@39
|
495 L.StrConst ds => maxName ds
|
adamc@39
|
496 | L.StrVar n => n
|
adamc@39
|
497 | L.StrProj (str, _) => maxNameStr str
|
adamc@45
|
498 | L.StrFun (_, _, _, _, str) => maxNameStr str
|
adamc@45
|
499 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
|
adamc@39
|
500
|
adamc@39
|
501 fun corify ds =
|
adamc@39
|
502 let
|
adamc@39
|
503 val () = reset (maxName ds + 1)
|
adamc@39
|
504 val (ds, _) = ListUtil.foldlMapConcat corifyDecl St.empty ds
|
adamc@39
|
505 in
|
adamc@39
|
506 ds
|
adamc@39
|
507 end
|
adamc@16
|
508
|
adamc@16
|
509 end
|