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@376
|
40 val restify = ref (fn s : string => s)
|
adamc@376
|
41
|
adamc@376
|
42 fun doRestify (mods, s) =
|
adamc@376
|
43 let
|
adamc@376
|
44 val s = if String.isPrefix "wrap_" s then
|
adamc@376
|
45 String.extract (s, 5, NONE)
|
adamc@376
|
46 else
|
adamc@376
|
47 s
|
adamc@376
|
48 in
|
adamc@376
|
49 !restify (String.concatWith "/" (rev (s :: mods)))
|
adamc@376
|
50 end
|
adamc@376
|
51
|
adamc@377
|
52 val relify = CharVector.map (fn #"/" => #"_"
|
adamc@377
|
53 | ch => ch)
|
adamc@377
|
54
|
adamc@39
|
55 local
|
adamc@39
|
56 val count = ref 0
|
adamc@39
|
57 in
|
adamc@39
|
58
|
adamc@39
|
59 fun reset v = count := v
|
adamc@39
|
60
|
adamc@39
|
61 fun alloc () =
|
adamc@39
|
62 let
|
adamc@39
|
63 val r = !count
|
adamc@39
|
64 in
|
adamc@39
|
65 count := r + 1;
|
adamc@39
|
66 r
|
adamc@39
|
67 end
|
adamc@39
|
68
|
adamc@39
|
69 end
|
adamc@39
|
70
|
adamc@39
|
71 structure St : sig
|
adamc@39
|
72 type t
|
adamc@39
|
73
|
adamc@39
|
74 val empty : t
|
adamc@39
|
75
|
adamc@146
|
76 val debug : t -> unit
|
adamc@146
|
77
|
adamc@376
|
78 val name : t -> string list
|
adamc@376
|
79
|
adamc@376
|
80 val enter : t * string list -> t
|
adamc@39
|
81 val leave : t -> {outer : t, inner : t}
|
adamc@192
|
82 val ffi : string -> L'.con SM.map -> (string * string list * L'.con option * L'.datatype_kind) SM.map -> t
|
adamc@39
|
83
|
adamc@249
|
84 val basisIs : t * int -> t
|
adamc@249
|
85 val lookupBasis : t -> int option
|
adamc@249
|
86
|
adamc@73
|
87 datatype core_con =
|
adamc@73
|
88 CNormal of int
|
adamc@73
|
89 | CFfi of string
|
adamc@73
|
90 val bindCon : t -> string -> int -> t * int
|
adamc@73
|
91 val lookupConById : t -> int -> int option
|
adamc@73
|
92 val lookupConByName : t -> string -> core_con
|
adamc@48
|
93
|
adamc@177
|
94 val bindConstructor : t -> string -> int -> L'.patCon -> t
|
adamc@186
|
95 val lookupConstructorByNameOpt : t -> string -> L'.patCon option
|
adamc@177
|
96 val lookupConstructorByName : t -> string -> L'.patCon
|
adamc@177
|
97 val lookupConstructorById : t -> int -> L'.patCon
|
adamc@249
|
98
|
adamc@249
|
99 datatype core_val =
|
adamc@249
|
100 ENormal of int
|
adamc@249
|
101 | EFfi of string * L'.con
|
adamc@249
|
102 val bindVal : t -> string -> int -> t * int
|
adamc@249
|
103 val bindConstructorVal : t -> string -> int -> t
|
adamc@249
|
104 val lookupValById : t -> int -> int option
|
adamc@249
|
105 val lookupValByName : t -> string -> core_val
|
adamc@177
|
106
|
adamc@249
|
107 val bindStr : t -> string -> int -> t -> t
|
adamc@249
|
108 val lookupStrById : t -> int -> t
|
adamc@249
|
109 val lookupStrByName : string * t -> t
|
adamc@339
|
110 val lookupStrByNameOpt : string * t -> t option
|
adamc@39
|
111
|
adamc@423
|
112 val bindFunctor : t -> string -> int -> string -> int -> L.str -> t
|
adamc@423
|
113 val lookupFunctorById : t -> int -> string * int * L.str
|
adamc@423
|
114 val lookupFunctorByName : string * t -> string * int * L.str
|
adamc@249
|
115 end = struct
|
adamc@46
|
116
|
adamc@249
|
117 datatype flattening =
|
adamc@376
|
118 FNormal of {name : string list,
|
adamc@376
|
119 cons : int SM.map,
|
adamc@249
|
120 constructors : L'.patCon SM.map,
|
adamc@249
|
121 vals : int SM.map,
|
adamc@249
|
122 strs : flattening SM.map,
|
adamc@423
|
123 funs : (string * int * L.str) SM.map}
|
adamc@249
|
124 | FFfi of {mod : string,
|
adamc@249
|
125 vals : L'.con SM.map,
|
adamc@249
|
126 constructors : (string * string list * L'.con option * L'.datatype_kind) SM.map}
|
adamc@39
|
127
|
adamc@249
|
128 type t = {
|
adamc@249
|
129 basis : int option,
|
adamc@249
|
130 cons : int IM.map,
|
adamc@249
|
131 constructors : L'.patCon IM.map,
|
adamc@249
|
132 vals : int IM.map,
|
adamc@249
|
133 strs : flattening IM.map,
|
adamc@423
|
134 funs : (string * int * L.str) IM.map,
|
adamc@249
|
135 current : flattening,
|
adamc@249
|
136 nested : flattening list
|
adamc@249
|
137 }
|
adamc@39
|
138
|
adamc@249
|
139 val empty = {
|
adamc@249
|
140 basis = NONE,
|
adamc@249
|
141 cons = IM.empty,
|
adamc@249
|
142 constructors = IM.empty,
|
adamc@249
|
143 vals = IM.empty,
|
adamc@249
|
144 strs = IM.empty,
|
adamc@249
|
145 funs = IM.empty,
|
adamc@376
|
146 current = FNormal { name = [], cons = SM.empty, constructors = SM.empty,
|
adamc@376
|
147 vals = SM.empty, strs = SM.empty, funs = SM.empty },
|
adamc@249
|
148 nested = []
|
adamc@249
|
149 }
|
adamc@39
|
150
|
adamc@376
|
151 fun debug ({current = FNormal {cons, constructors, vals, strs, funs, ...}, ...} : t) =
|
adamc@249
|
152 print ("cons: " ^ Int.toString (SM.numItems cons) ^ "; "
|
adamc@249
|
153 ^ "constructors: " ^ Int.toString (SM.numItems constructors) ^ "; "
|
adamc@249
|
154 ^ "vals: " ^ Int.toString (SM.numItems vals) ^ "; "
|
adamc@249
|
155 ^ "strs: " ^ Int.toString (SM.numItems strs) ^ "; "
|
adamc@249
|
156 ^ "funs: " ^ Int.toString (SM.numItems funs) ^ "\n")
|
adamc@249
|
157 | debug _ = print "Not normal!\n"
|
adamc@146
|
158
|
adamc@376
|
159 fun name ({current = FNormal {name, ...}, ...} : t) = name
|
adamc@376
|
160 | name {current = FFfi {mod = name, ...}, ...} = [name]
|
adamc@376
|
161
|
adamc@249
|
162 fun basisIs ({cons, constructors, vals, strs, funs, current, nested, ...} : t, basis) =
|
adamc@249
|
163 {basis = SOME basis,
|
adamc@249
|
164 cons = cons,
|
adamc@249
|
165 constructors = constructors,
|
adamc@249
|
166 vals = vals,
|
adamc@249
|
167 strs = strs,
|
adamc@249
|
168 funs = funs,
|
adamc@249
|
169 current = current,
|
adamc@249
|
170 nested = nested}
|
adamc@48
|
171
|
adamc@249
|
172 fun lookupBasis ({basis, ...} : t) = basis
|
adamc@73
|
173
|
adamc@249
|
174 datatype core_con =
|
adamc@249
|
175 CNormal of int
|
adamc@249
|
176 | CFfi of string
|
adamc@39
|
177
|
adamc@249
|
178 datatype core_val =
|
adamc@249
|
179 ENormal of int
|
adamc@249
|
180 | EFfi of string * L'.con
|
adamc@188
|
181
|
adamc@249
|
182 fun bindCon {basis, cons, constructors, vals, strs, funs, current, nested} s n =
|
adamc@249
|
183 let
|
adamc@249
|
184 val n' = alloc ()
|
adamc@188
|
185
|
adamc@249
|
186 val current =
|
adamc@249
|
187 case current of
|
adamc@249
|
188 FFfi _ => raise Fail "Binding inside FFfi"
|
adamc@376
|
189 | FNormal {name, cons, constructors, vals, strs, funs} =>
|
adamc@376
|
190 FNormal {name = name,
|
adamc@376
|
191 cons = SM.insert (cons, s, n'),
|
adamc@249
|
192 constructors = constructors,
|
adamc@249
|
193 vals = vals,
|
adamc@249
|
194 strs = strs,
|
adamc@249
|
195 funs = funs}
|
adamc@249
|
196 in
|
adamc@249
|
197 ({basis = basis,
|
adamc@249
|
198 cons = IM.insert (cons, n, n'),
|
adamc@177
|
199 constructors = constructors,
|
adamc@73
|
200 vals = vals,
|
adamc@39
|
201 strs = strs,
|
adamc@46
|
202 funs = funs,
|
adamc@39
|
203 current = current,
|
adamc@249
|
204 nested = nested},
|
adamc@249
|
205 n')
|
adamc@249
|
206 end
|
adamc@39
|
207
|
adamc@249
|
208 fun lookupConById ({cons, ...} : t) n = IM.find (cons, n)
|
adamc@39
|
209
|
adamc@249
|
210 fun lookupConByName ({current, ...} : t) x =
|
adamc@249
|
211 case current of
|
adamc@249
|
212 FFfi {mod = m, ...} => CFfi m
|
adamc@249
|
213 | FNormal {cons, ...} =>
|
adamc@249
|
214 case SM.find (cons, x) of
|
adamc@249
|
215 NONE => raise Fail "Corify.St.lookupConByName"
|
adamc@249
|
216 | SOME n => CNormal n
|
adamc@249
|
217
|
adamc@249
|
218 fun bindVal {basis, cons, constructors, vals, strs, funs, current, nested} s n =
|
adamc@249
|
219 let
|
adamc@249
|
220 val n' = alloc ()
|
adamc@249
|
221
|
adamc@249
|
222 val current =
|
adamc@249
|
223 case current of
|
adamc@249
|
224 FFfi _ => raise Fail "Binding inside FFfi"
|
adamc@376
|
225 | FNormal {name, cons, constructors, vals, strs, funs} =>
|
adamc@376
|
226 FNormal {name = name,
|
adamc@376
|
227 cons = cons,
|
adamc@249
|
228 constructors = constructors,
|
adamc@249
|
229 vals = SM.insert (vals, s, n'),
|
adamc@249
|
230 strs = strs,
|
adamc@249
|
231 funs = funs}
|
adamc@249
|
232 in
|
adamc@249
|
233 ({basis = basis,
|
adamc@249
|
234 cons = cons,
|
adamc@249
|
235 constructors = constructors,
|
adamc@249
|
236 vals = IM.insert (vals, n, n'),
|
adamc@249
|
237 strs = strs,
|
adamc@249
|
238 funs = funs,
|
adamc@249
|
239 current = current,
|
adamc@249
|
240 nested = nested},
|
adamc@249
|
241 n')
|
adamc@249
|
242 end
|
adamc@249
|
243
|
adamc@249
|
244 fun bindConstructorVal {basis, cons, constructors, vals, strs, funs, current, nested} s n =
|
adamc@249
|
245 let
|
adamc@249
|
246 val current =
|
adamc@249
|
247 case current of
|
adamc@249
|
248 FFfi _ => raise Fail "Binding inside FFfi"
|
adamc@376
|
249 | FNormal {name, cons, constructors, vals, strs, funs} =>
|
adamc@376
|
250 FNormal {name = name,
|
adamc@376
|
251 cons = cons,
|
adamc@249
|
252 constructors = constructors,
|
adamc@249
|
253 vals = SM.insert (vals, s, n),
|
adamc@249
|
254 strs = strs,
|
adamc@249
|
255 funs = funs}
|
adamc@249
|
256 in
|
adamc@249
|
257 {basis = basis,
|
adamc@249
|
258 cons = cons,
|
adamc@249
|
259 constructors = constructors,
|
adamc@249
|
260 vals = IM.insert (vals, n, n),
|
adamc@249
|
261 strs = strs,
|
adamc@249
|
262 funs = funs,
|
adamc@249
|
263 current = current,
|
adamc@249
|
264 nested = nested}
|
adamc@249
|
265 end
|
adamc@249
|
266
|
adamc@249
|
267
|
adamc@249
|
268 fun lookupValById ({vals, ...} : t) n = IM.find (vals, n)
|
adamc@249
|
269
|
adamc@249
|
270 fun lookupValByName ({current, ...} : t) x =
|
adamc@249
|
271 case current of
|
adamc@249
|
272 FFfi {mod = m, vals, ...} =>
|
adamc@249
|
273 (case SM.find (vals, x) of
|
adamc@249
|
274 NONE => raise Fail "Corify.St.lookupValByName: no type for FFI val"
|
adamc@249
|
275 | SOME t => EFfi (m, t))
|
adamc@249
|
276 | FNormal {vals, ...} =>
|
adamc@249
|
277 case SM.find (vals, x) of
|
adamc@249
|
278 NONE => raise Fail "Corify.St.lookupValByName"
|
adamc@249
|
279 | SOME n => ENormal n
|
adamc@249
|
280
|
adamc@249
|
281 fun bindConstructor {basis, cons, constructors, vals, strs, funs, current, nested} s n n' =
|
adamc@249
|
282 let
|
adamc@249
|
283 val current =
|
adamc@249
|
284 case current of
|
adamc@249
|
285 FFfi _ => raise Fail "Binding inside FFfi"
|
adamc@376
|
286 | FNormal {name, cons, constructors, vals, strs, funs} =>
|
adamc@376
|
287 FNormal {name = name,
|
adamc@376
|
288 cons = cons,
|
adamc@249
|
289 constructors = SM.insert (constructors, s, n'),
|
adamc@249
|
290 vals = vals,
|
adamc@249
|
291 strs = strs,
|
adamc@249
|
292 funs = funs}
|
adamc@249
|
293 in
|
adamc@249
|
294 {basis = basis,
|
adamc@249
|
295 cons = cons,
|
adamc@249
|
296 constructors = IM.insert (constructors, n, n'),
|
adamc@249
|
297 vals = vals,
|
adamc@249
|
298 strs = strs,
|
adamc@249
|
299 funs = funs,
|
adamc@249
|
300 current = current,
|
adamc@249
|
301 nested = nested}
|
adamc@249
|
302 end
|
adamc@249
|
303
|
adamc@249
|
304 fun lookupConstructorById ({constructors, ...} : t) n =
|
adamc@249
|
305 case IM.find (constructors, n) of
|
adamc@249
|
306 NONE => raise Fail "Corify.St.lookupConstructorById"
|
adamc@249
|
307 | SOME x => x
|
adamc@249
|
308
|
adamc@249
|
309 fun lookupConstructorByNameOpt ({current, ...} : t) x =
|
adamc@249
|
310 case current of
|
adamc@249
|
311 FFfi {mod = m, constructors, ...} =>
|
adamc@249
|
312 (case SM.find (constructors, x) of
|
adamc@188
|
313 NONE => NONE
|
adamc@249
|
314 | SOME (n, xs, to, dk) => SOME (L'.PConFfi {mod = m, datatyp = n, params = xs, con = x, arg = to, kind = dk}))
|
adamc@249
|
315 | FNormal {constructors, ...} =>
|
adamc@249
|
316 case SM.find (constructors, x) of
|
adamc@249
|
317 NONE => NONE
|
adamc@249
|
318 | SOME n => SOME n
|
adamc@39
|
319
|
adamc@249
|
320 fun lookupConstructorByName ({current, ...} : t) x =
|
adamc@249
|
321 case current of
|
adamc@249
|
322 FFfi {mod = m, constructors, ...} =>
|
adamc@249
|
323 (case SM.find (constructors, x) of
|
adamc@249
|
324 NONE => raise Fail "Corify.St.lookupConstructorByName [1]"
|
adamc@249
|
325 | SOME (n, xs, to, dk) => L'.PConFfi {mod = m, datatyp = n, params = xs, con = x, arg = to, kind = dk})
|
adamc@249
|
326 | FNormal {constructors, ...} =>
|
adamc@249
|
327 case SM.find (constructors, x) of
|
adamc@249
|
328 NONE => raise Fail "Corify.St.lookupConstructorByName [2]"
|
adamc@249
|
329 | SOME n => n
|
adamc@73
|
330
|
adamc@376
|
331 fun enter ({basis, cons, constructors, vals, strs, funs, current, nested}, name) =
|
adamc@249
|
332 {basis = basis,
|
adamc@249
|
333 cons = cons,
|
adamc@249
|
334 constructors = constructors,
|
adamc@249
|
335 vals = vals,
|
adamc@249
|
336 strs = strs,
|
adamc@249
|
337 funs = funs,
|
adamc@376
|
338 current = FNormal {name = name,
|
adamc@376
|
339 cons = SM.empty,
|
adamc@249
|
340 constructors = SM.empty,
|
adamc@249
|
341 vals = SM.empty,
|
adamc@249
|
342 strs = SM.empty,
|
adamc@249
|
343 funs = SM.empty},
|
adamc@249
|
344 nested = current :: nested}
|
adamc@73
|
345
|
adamc@249
|
346 fun dummy (b, f) = {basis = b,
|
adamc@249
|
347 cons = IM.empty,
|
adamc@249
|
348 constructors = IM.empty,
|
adamc@249
|
349 vals = IM.empty,
|
adamc@249
|
350 strs = IM.empty,
|
adamc@249
|
351 funs = IM.empty,
|
adamc@249
|
352 current = f,
|
adamc@249
|
353 nested = []}
|
adamc@163
|
354
|
adamc@249
|
355 fun leave {basis, cons, constructors, vals, strs, funs, current, nested = m1 :: rest} =
|
adamc@249
|
356 {outer = {basis = basis,
|
adamc@249
|
357 cons = cons,
|
adamc@249
|
358 constructors = constructors,
|
adamc@249
|
359 vals = vals,
|
adamc@249
|
360 strs = strs,
|
adamc@249
|
361 funs = funs,
|
adamc@249
|
362 current = m1,
|
adamc@249
|
363 nested = rest},
|
adamc@249
|
364 inner = dummy (basis, current)}
|
adamc@249
|
365 | leave _ = raise Fail "Corify.St.leave"
|
adamc@177
|
366
|
adamc@249
|
367 fun ffi m vals constructors = dummy (NONE, FFfi {mod = m, vals = vals, constructors = constructors})
|
adamc@73
|
368
|
adamc@249
|
369 fun bindStr ({basis, cons, constructors, vals, strs, funs,
|
adamc@376
|
370 current = FNormal {name, cons = mcons, constructors = mconstructors,
|
adamc@249
|
371 vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
|
adamc@249
|
372 x n ({current = f, ...} : t) =
|
adamc@249
|
373 {basis = basis,
|
adamc@249
|
374 cons = cons,
|
adamc@249
|
375 constructors = constructors,
|
adamc@249
|
376 vals = vals,
|
adamc@249
|
377 strs = IM.insert (strs, n, f),
|
adamc@249
|
378 funs = funs,
|
adamc@376
|
379 current = FNormal {name = name,
|
adamc@376
|
380 cons = mcons,
|
adamc@249
|
381 constructors = mconstructors,
|
adamc@249
|
382 vals = mvals,
|
adamc@249
|
383 strs = SM.insert (mstrs, x, f),
|
adamc@249
|
384 funs = mfuns},
|
adamc@249
|
385 nested = nested}
|
adamc@249
|
386 | bindStr _ _ _ _ = raise Fail "Corify.St.bindStr"
|
adamc@73
|
387
|
adamc@249
|
388 fun lookupStrById ({basis, strs, ...} : t) n =
|
adamc@249
|
389 case IM.find (strs, n) of
|
adamc@480
|
390 NONE => raise Fail ("Corify.St.lookupStrById(" ^ Int.toString n ^ ")")
|
adamc@249
|
391 | SOME f => dummy (basis, f)
|
adamc@177
|
392
|
adamc@249
|
393 fun lookupStrByName (m, {basis, current = FNormal {strs, ...}, ...} : t) =
|
adamc@249
|
394 (case SM.find (strs, m) of
|
adamc@339
|
395 NONE => raise Fail "Corify.St.lookupStrByName [1]"
|
adamc@249
|
396 | SOME f => dummy (basis, f))
|
adamc@339
|
397 | lookupStrByName _ = raise Fail "Corify.St.lookupStrByName [2]"
|
adamc@339
|
398
|
adamc@339
|
399 fun lookupStrByNameOpt (m, {basis, current = FNormal {strs, ...}, ...} : t) =
|
adamc@339
|
400 (case SM.find (strs, m) of
|
adamc@339
|
401 NONE => NONE
|
adamc@339
|
402 | SOME f => SOME (dummy (basis, f)))
|
adamc@339
|
403 | lookupStrByNameOpt _ = NONE
|
adamc@177
|
404
|
adamc@249
|
405 fun bindFunctor ({basis, cons, constructors, vals, strs, funs,
|
adamc@376
|
406 current = FNormal {name, cons = mcons, constructors = mconstructors,
|
adamc@249
|
407 vals = mvals, strs = mstrs, funs = mfuns}, nested} : t)
|
adamc@423
|
408 x n xa na str =
|
adamc@249
|
409 {basis = basis,
|
adamc@249
|
410 cons = cons,
|
adamc@249
|
411 constructors = constructors,
|
adamc@249
|
412 vals = vals,
|
adamc@249
|
413 strs = strs,
|
adamc@423
|
414 funs = IM.insert (funs, n, (xa, na, str)),
|
adamc@376
|
415 current = FNormal {name = name,
|
adamc@376
|
416 cons = mcons,
|
adamc@249
|
417 constructors = mconstructors,
|
adamc@249
|
418 vals = mvals,
|
adamc@249
|
419 strs = mstrs,
|
adamc@423
|
420 funs = SM.insert (mfuns, x, (xa, na, str))},
|
adamc@249
|
421 nested = nested}
|
adamc@423
|
422 | bindFunctor _ _ _ _ _ _ = raise Fail "Corify.St.bindFunctor"
|
adamc@186
|
423
|
adamc@249
|
424 fun lookupFunctorById ({funs, ...} : t) n =
|
adamc@249
|
425 case IM.find (funs, n) of
|
adamc@249
|
426 NONE => raise Fail "Corify.St.lookupFunctorById"
|
adamc@249
|
427 | SOME v => v
|
adamc@177
|
428
|
adamc@249
|
429 fun lookupFunctorByName (m, {current = FNormal {funs, ...}, ...} : t) =
|
adamc@249
|
430 (case SM.find (funs, m) of
|
adamc@339
|
431 NONE => raise Fail "Corify.St.lookupFunctorByName [1]"
|
adamc@249
|
432 | SOME v => v)
|
adamc@339
|
433 | lookupFunctorByName _ = raise Fail "Corify.St.lookupFunctorByName [2]"
|
adamc@39
|
434
|
adamc@249
|
435 end
|
adamc@39
|
436
|
adamc@39
|
437
|
adamc@249
|
438 fun corifyKind (k, loc) =
|
adamc@249
|
439 case k of
|
adamc@249
|
440 L.KType => (L'.KType, loc)
|
adamc@249
|
441 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
|
adamc@249
|
442 | L.KName => (L'.KName, loc)
|
adamc@249
|
443 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
|
adamc@249
|
444 | L.KUnit => (L'.KUnit, loc)
|
adamc@249
|
445 | L.KTuple ks => (L'.KTuple (map corifyKind ks), loc)
|
adamc@48
|
446
|
adamc@249
|
447 fun corifyCon st (c, loc) =
|
adamc@249
|
448 case c of
|
adamc@249
|
449 L.TFun (t1, t2) => (L'.TFun (corifyCon st t1, corifyCon st t2), loc)
|
adamc@249
|
450 | L.TCFun (x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon st t), loc)
|
adamc@249
|
451 | L.TRecord c => (L'.TRecord (corifyCon st c), loc)
|
adamc@39
|
452
|
adamc@249
|
453 | L.CRel n => (L'.CRel n, loc)
|
adamc@249
|
454 | L.CNamed n =>
|
adamc@249
|
455 (case St.lookupConById st n of
|
adamc@249
|
456 NONE => (L'.CNamed n, loc)
|
adamc@249
|
457 | SOME n => (L'.CNamed n, loc))
|
adamc@249
|
458 | L.CModProj (m, ms, x) =>
|
adamc@249
|
459 let
|
adamc@249
|
460 val st = St.lookupStrById st m
|
adamc@249
|
461 val st = foldl St.lookupStrByName st ms
|
adamc@249
|
462 in
|
adamc@249
|
463 case St.lookupConByName st x of
|
adamc@249
|
464 St.CNormal n => (L'.CNamed n, loc)
|
adamc@249
|
465 | St.CFfi m => (L'.CFfi (m, x), loc)
|
adamc@249
|
466 end
|
adamc@39
|
467
|
adamc@249
|
468 | L.CApp (c1, c2) => (L'.CApp (corifyCon st c1, corifyCon st c2), loc)
|
adamc@249
|
469 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon st c), loc)
|
adamc@39
|
470
|
adamc@249
|
471 | L.CName s => (L'.CName s, loc)
|
adamc@46
|
472
|
adamc@249
|
473 | L.CRecord (k, xcs) =>
|
adamc@249
|
474 (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon st c1, corifyCon st c2)) xcs), loc)
|
adamc@249
|
475 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon st c1, corifyCon st c2), loc)
|
adamc@621
|
476 | L.CMap (k1, k2) => (L'.CMap (corifyKind k1, corifyKind k2), loc)
|
adamc@249
|
477 | L.CUnit => (L'.CUnit, loc)
|
adamc@46
|
478
|
adamc@249
|
479 | L.CTuple cs => (L'.CTuple (map (corifyCon st) cs), loc)
|
adamc@249
|
480 | L.CProj (c, n) => (L'.CProj (corifyCon st c, n), loc)
|
adamc@213
|
481
|
adamc@249
|
482 fun corifyPatCon st pc =
|
adamc@249
|
483 case pc of
|
adamc@249
|
484 L.PConVar n => St.lookupConstructorById st n
|
adamc@249
|
485 | L.PConProj (m1, ms, x) =>
|
adamc@249
|
486 let
|
adamc@249
|
487 val st = St.lookupStrById st m1
|
adamc@249
|
488 val st = foldl St.lookupStrByName st ms
|
adamc@249
|
489 in
|
adamc@249
|
490 St.lookupConstructorByName st x
|
adamc@249
|
491 end
|
adamc@46
|
492
|
adamc@249
|
493 fun corifyPat st (p, loc) =
|
adamc@249
|
494 case p of
|
adamc@249
|
495 L.PWild => (L'.PWild, loc)
|
adamc@249
|
496 | L.PVar (x, t) => (L'.PVar (x, corifyCon st t), loc)
|
adamc@249
|
497 | L.PPrim p => (L'.PPrim p, loc)
|
adamc@249
|
498 | L.PCon (dk, pc, ts, po) => (L'.PCon (dk, corifyPatCon st pc, map (corifyCon st) ts,
|
adamc@249
|
499 Option.map (corifyPat st) po), loc)
|
adamc@249
|
500 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, corifyPat st p, corifyCon st t)) xps), loc)
|
adamc@39
|
501
|
adamc@249
|
502 fun corifyExp st (e, loc) =
|
adamc@249
|
503 case e of
|
adamc@249
|
504 L.EPrim p => (L'.EPrim p, loc)
|
adamc@249
|
505 | L.ERel n => (L'.ERel n, loc)
|
adamc@249
|
506 | L.ENamed n =>
|
adamc@249
|
507 (case St.lookupValById st n of
|
adamc@249
|
508 NONE => (L'.ENamed n, loc)
|
adamc@249
|
509 | SOME n => (L'.ENamed n, loc))
|
adamc@249
|
510 | L.EModProj (m, ms, x) =>
|
adamc@249
|
511 let
|
adamc@249
|
512 val st = St.lookupStrById st m
|
adamc@249
|
513 val st = foldl St.lookupStrByName st ms
|
adamc@249
|
514 in
|
adamc@249
|
515 case St.lookupConstructorByNameOpt st x of
|
adamc@249
|
516 SOME (pc as L'.PConFfi {mod = m, datatyp, params, arg, kind, ...}) =>
|
adamc@249
|
517 let
|
adamc@249
|
518 val args = ListUtil.mapi (fn (i, _) => (L'.CRel i, loc)) params
|
adamc@249
|
519 val e = case arg of
|
adamc@249
|
520 NONE => (L'.ECon (kind, pc, args, NONE), loc)
|
adamc@249
|
521 | SOME dom => (L'.EAbs ("x", dom, (L'.CFfi (m, datatyp), loc),
|
adamc@249
|
522 (L'.ECon (kind, pc, args, SOME (L'.ERel 0, loc)), loc)), loc)
|
adamc@192
|
523
|
adamc@249
|
524 val k = (L'.KType, loc)
|
adamc@249
|
525 in
|
adamc@249
|
526 foldr (fn (x, e) => (L'.ECAbs (x, k, e), loc)) e params
|
adamc@249
|
527 end
|
adamc@249
|
528 | _ =>
|
adamc@249
|
529 case St.lookupValByName st x of
|
adamc@249
|
530 St.ENormal n => (L'.ENamed n, loc)
|
adamc@249
|
531 | St.EFfi (m, t) =>
|
adamc@249
|
532 case t of
|
adamc@249
|
533 (L'.TFun (dom as (L'.TRecord (L'.CRecord (_, []), _), _), ran), _) =>
|
adamc@249
|
534 (L'.EAbs ("arg", dom, ran, (L'.EFfiApp (m, x, []), loc)), loc)
|
adamc@249
|
535 | t as (L'.TFun _, _) =>
|
adamc@249
|
536 let
|
adamc@249
|
537 fun getArgs (all as (t, _), args) =
|
adamc@249
|
538 case t of
|
adamc@249
|
539 L'.TFun (dom, ran) => getArgs (ran, dom :: args)
|
adamc@249
|
540 | _ => (all, rev args)
|
adamc@39
|
541
|
adamc@249
|
542 val (result, args) = getArgs (t, [])
|
adamc@456
|
543 val (isTransaction, result) =
|
adamc@456
|
544 case result of
|
adamc@456
|
545 (L'.CApp ((L'.CFfi ("Basis", "transaction"), _),
|
adamc@456
|
546 result), _) => (true, result)
|
adamc@456
|
547 | _ => (false, result)
|
adamc@16
|
548
|
adamc@456
|
549 fun makeApp n =
|
adamc@456
|
550 let
|
adamc@456
|
551 val (actuals, _) = foldr (fn (_, (actuals, n)) =>
|
adamc@456
|
552 ((L'.ERel n, loc) :: actuals,
|
adamc@456
|
553 n + 1)) ([], n) args
|
adamc@456
|
554 in
|
adamc@456
|
555 (L'.EFfiApp (m, x, actuals), loc)
|
adamc@456
|
556 end
|
adamc@456
|
557 val unit = (L'.TRecord (L'.CRecord ((L'.KType, loc), []), loc), loc)
|
adamc@456
|
558 val (result, app) =
|
adamc@456
|
559 if isTransaction then
|
adamc@456
|
560 ((L'.TFun (unit, result), loc),
|
adamc@456
|
561 (L'.EAbs ("_",
|
adamc@456
|
562 unit,
|
adamc@456
|
563 result,
|
adamc@456
|
564 makeApp 1), loc))
|
adamc@456
|
565 else
|
adamc@456
|
566 (result, makeApp 0)
|
adamc@456
|
567
|
adamc@249
|
568 val (abs, _, _) = foldr (fn (t, (abs, ran, n)) =>
|
adamc@249
|
569 ((L'.EAbs ("arg" ^ Int.toString n,
|
adamc@249
|
570 t,
|
adamc@249
|
571 ran,
|
adamc@249
|
572 abs), loc),
|
adamc@249
|
573 (L'.TFun (t, ran), loc),
|
adamc@249
|
574 n - 1)) (app, result, length args - 1) args
|
adamc@249
|
575 in
|
adamc@249
|
576 abs
|
adamc@249
|
577 end
|
adamc@249
|
578 | _ => (L'.EFfi (m, x), loc)
|
adamc@249
|
579 end
|
adamc@249
|
580 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
|
adamc@249
|
581 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
|
adamc@249
|
582 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
|
adamc@249
|
583 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
|
adamc@16
|
584
|
adamc@249
|
585 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) =>
|
adamc@249
|
586 (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
|
adamc@249
|
587 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
|
adamc@249
|
588 {field = corifyCon st field, rest = corifyCon st rest}), loc)
|
adamc@445
|
589 | L.EConcat (e1, c1, e2, c2) => (L'.EConcat (corifyExp st e1, corifyCon st c1, corifyExp st e2,
|
adamc@445
|
590 corifyCon st c2), loc)
|
adamc@249
|
591 | L.ECut (e1, c, {field, rest}) => (L'.ECut (corifyExp st e1, corifyCon st c,
|
adamc@249
|
592 {field = corifyCon st field, rest = corifyCon st rest}), loc)
|
adamc@493
|
593 | L.ECutMulti (e1, c, {rest}) => (L'.ECutMulti (corifyExp st e1, corifyCon st c,
|
adamc@493
|
594 {rest = corifyCon st rest}), loc)
|
adamc@34
|
595
|
adamc@249
|
596 | L.ECase (e, pes, {disc, result}) =>
|
adamc@249
|
597 (L'.ECase (corifyExp st e,
|
adamc@249
|
598 map (fn (p, e) => (corifyPat st p, corifyExp st e)) pes,
|
adamc@249
|
599 {disc = corifyCon st disc, result = corifyCon st result}),
|
adamc@249
|
600 loc)
|
adamc@16
|
601
|
adamc@249
|
602 | L.EWrite e => (L'.EWrite (corifyExp st e), loc)
|
adamc@16
|
603
|
adamc@450
|
604 | L.ELet (x, t, e1, e2) => (L'.ELet (x, corifyCon st t, corifyExp st e1, corifyExp st e2), loc)
|
adamc@450
|
605
|
adamc@480
|
606 fun corifyDecl mods (all as (d, loc : EM.span), st) =
|
adamc@249
|
607 case d of
|
adamc@249
|
608 L.DCon (x, n, k, c) =>
|
adamc@249
|
609 let
|
adamc@249
|
610 val (st, n) = St.bindCon st x n
|
adamc@249
|
611 in
|
adamc@249
|
612 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
|
adamc@249
|
613 end
|
adamc@249
|
614 | L.DDatatype (x, n, xs, xncs) =>
|
adamc@249
|
615 let
|
adamc@249
|
616 val (st, n) = St.bindCon st x n
|
adamc@249
|
617 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
|
adamc@249
|
618 let
|
adamc@249
|
619 val st = St.bindConstructor st x n (L'.PConVar n)
|
adamc@249
|
620 val st = St.bindConstructorVal st x n
|
adamc@249
|
621 val co = Option.map (corifyCon st) co
|
adamc@249
|
622 in
|
adamc@249
|
623 ((x, n, co), st)
|
adamc@249
|
624 end) st xncs
|
adamc@16
|
625
|
adamc@249
|
626 val dk = ElabUtil.classifyDatatype xncs
|
adamc@249
|
627 val t = (L'.CNamed n, loc)
|
adamc@249
|
628 val nxs = length xs - 1
|
adamc@249
|
629 val t = ListUtil.foldli (fn (i, _, t) => (L'.CApp (t, (L'.CRel (nxs - i), loc)), loc)) t xs
|
adamc@249
|
630 val k = (L'.KType, loc)
|
adamc@249
|
631 val dcons = map (fn (x, n, to) =>
|
adamc@249
|
632 let
|
adamc@249
|
633 val args = ListUtil.mapi (fn (i, _) => (L'.CRel (nxs - i), loc)) xs
|
adamc@249
|
634 val (e, t) =
|
adamc@249
|
635 case to of
|
adamc@249
|
636 NONE => ((L'.ECon (dk, L'.PConVar n, args, NONE), loc), t)
|
adamc@249
|
637 | SOME t' => ((L'.EAbs ("x", t', t,
|
adamc@249
|
638 (L'.ECon (dk, L'.PConVar n, args,
|
adamc@249
|
639 SOME (L'.ERel 0, loc)),
|
adamc@249
|
640 loc)),
|
adamc@249
|
641 loc),
|
adamc@249
|
642 (L'.TFun (t', t), loc))
|
adamc@192
|
643
|
adamc@249
|
644 val t = foldr (fn (x, t) => (L'.TCFun (x, k, t), loc)) t xs
|
adamc@249
|
645 val e = foldr (fn (x, e) => (L'.ECAbs (x, k, e), loc)) e xs
|
adamc@249
|
646 in
|
adamc@249
|
647 (L'.DVal (x, n, t, e, ""), loc)
|
adamc@249
|
648 end) xncs
|
adamc@249
|
649 in
|
adamc@249
|
650 ((L'.DDatatype (x, n, xs, xncs), loc) :: dcons, st)
|
adamc@249
|
651 end
|
adamc@249
|
652 | L.DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
|
adamc@249
|
653 let
|
adamc@249
|
654 val (st, n) = St.bindCon st x n
|
adamc@249
|
655 val c = corifyCon st (L.CModProj (m1, ms, s), loc)
|
adamc@177
|
656
|
adamc@249
|
657 val m = foldl (fn (x, m) => (L.StrProj (m, x), loc)) (L.StrVar m1, loc) ms
|
adamc@376
|
658 val (_, {inner, ...}) = corifyStr mods (m, st)
|
adamc@177
|
659
|
adamc@249
|
660 val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) =>
|
adamc@249
|
661 let
|
adamc@249
|
662 val n' = St.lookupConstructorByName inner x
|
adamc@249
|
663 val st = St.bindConstructor st x n n'
|
adamc@249
|
664 val (st, n) = St.bindVal st x n
|
adamc@249
|
665 val co = Option.map (corifyCon st) co
|
adamc@249
|
666 in
|
adamc@249
|
667 ((x, n, co), st)
|
adamc@249
|
668 end) st xncs
|
adamc@49
|
669
|
adamc@249
|
670 val nxs = length xs - 1
|
adamc@288
|
671 val cBase = c
|
adamc@249
|
672 val c = ListUtil.foldli (fn (i, _, c) => (L'.CApp (c, (L'.CRel (nxs - i), loc)), loc)) c xs
|
adamc@249
|
673 val k = (L'.KType, loc)
|
adamc@249
|
674 val k' = foldl (fn (_, k') => (L'.KArrow (k, k'), loc)) k xs
|
adamc@192
|
675
|
adamc@249
|
676 val cds = map (fn (x, n, co) =>
|
adamc@249
|
677 let
|
adamc@249
|
678 val t = case co of
|
adamc@249
|
679 NONE => c
|
adamc@249
|
680 | SOME t' => (L'.TFun (t', c), loc)
|
adamc@249
|
681 val e = corifyExp st (L.EModProj (m1, ms, x), loc)
|
adamc@192
|
682
|
adamc@249
|
683 val t = foldr (fn (x, t) => (L'.TCFun (x, k, t), loc)) t xs
|
adamc@249
|
684 in
|
adamc@249
|
685 (L'.DVal (x, n, t, e, x), loc)
|
adamc@249
|
686 end) xncs
|
adamc@249
|
687 in
|
adamc@288
|
688 ((L'.DCon (x, n, k', cBase), loc) :: cds, st)
|
adamc@249
|
689 end
|
adamc@249
|
690 | L.DVal (x, n, t, e) =>
|
adamc@249
|
691 let
|
adamc@249
|
692 val (st, n) = St.bindVal st x n
|
adamc@376
|
693 val s = doRestify (mods, x)
|
adamc@249
|
694 in
|
adamc@249
|
695 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e, s), loc)], st)
|
adamc@249
|
696 end
|
adamc@249
|
697 | L.DValRec vis =>
|
adamc@249
|
698 let
|
adamc@249
|
699 val (vis, st) = ListUtil.foldlMap
|
adamc@249
|
700 (fn ((x, n, t, e), st) =>
|
adamc@249
|
701 let
|
adamc@249
|
702 val (st, n) = St.bindVal st x n
|
adamc@249
|
703 in
|
adamc@249
|
704 ((x, n, t, e), st)
|
adamc@249
|
705 end)
|
adamc@249
|
706 st vis
|
adamc@16
|
707
|
adamc@249
|
708 val vis = map
|
adamc@249
|
709 (fn (x, n, t, e) =>
|
adamc@249
|
710 let
|
adamc@376
|
711 val s = doRestify (mods, x)
|
adamc@249
|
712 in
|
adamc@249
|
713 (x, n, corifyCon st t, corifyExp st e, s)
|
adamc@249
|
714 end)
|
adamc@249
|
715 vis
|
adamc@249
|
716 in
|
adamc@249
|
717 ([(L'.DValRec vis, loc)], st)
|
adamc@249
|
718 end
|
adamc@249
|
719 | L.DSgn _ => ([], st)
|
adamc@177
|
720
|
adamc@249
|
721 | L.DStr (x, n, _, (L.StrFun (xa, na, _, _, str), _)) =>
|
adamc@423
|
722 ([], St.bindFunctor st x n xa na str)
|
adamc@177
|
723
|
adamc@339
|
724 | L.DStr (x, n, _, (L.StrProj (str, x'), _)) =>
|
adamc@339
|
725 let
|
adamc@376
|
726 val (ds, {inner, outer}) = corifyStr mods (str, st)
|
adamc@339
|
727
|
adamc@339
|
728 val st = case St.lookupStrByNameOpt (x', inner) of
|
adamc@339
|
729 SOME st' => St.bindStr st x n st'
|
adamc@339
|
730 | NONE =>
|
adamc@339
|
731 let
|
adamc@423
|
732 val (x', n', str') = St.lookupFunctorByName (x', inner)
|
adamc@339
|
733 in
|
adamc@423
|
734 St.bindFunctor st x n x' n' str'
|
adamc@339
|
735 end
|
adamc@339
|
736 in
|
adamc@339
|
737 ([], st)
|
adamc@339
|
738 end
|
adamc@339
|
739
|
adamc@249
|
740 | L.DStr (x, n, _, str) =>
|
adamc@249
|
741 let
|
adamc@377
|
742 val mods' =
|
adamc@377
|
743 if x = "anon" then
|
adamc@377
|
744 mods
|
adamc@377
|
745 else
|
adamc@377
|
746 x :: mods
|
adamc@377
|
747
|
adamc@377
|
748 val (ds, {inner, outer}) = corifyStr mods' (str, st)
|
adamc@249
|
749 val st = St.bindStr outer x n inner
|
adamc@249
|
750 in
|
adamc@249
|
751 (ds, st)
|
adamc@249
|
752 end
|
adamc@16
|
753
|
adamc@249
|
754 | L.DFfiStr (m, n, (sgn, _)) =>
|
adamc@249
|
755 (case sgn of
|
adamc@249
|
756 L.SgnConst sgis =>
|
adamc@249
|
757 let
|
adamc@456
|
758 val (ds, cmap, conmap, st, _) =
|
adamc@456
|
759 foldl (fn ((sgi, _), (ds, cmap, conmap, st, trans)) =>
|
adamc@249
|
760 case sgi of
|
adamc@249
|
761 L.SgiConAbs (x, n, k) =>
|
adamc@249
|
762 let
|
adamc@249
|
763 val (st, n') = St.bindCon st x n
|
adamc@456
|
764
|
adamc@456
|
765 val trans =
|
adamc@456
|
766 if x = "transaction" then
|
adamc@456
|
767 SOME n
|
adamc@456
|
768 else
|
adamc@456
|
769 trans
|
adamc@249
|
770 in
|
adamc@249
|
771 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
|
adamc@249
|
772 cmap,
|
adamc@249
|
773 conmap,
|
adamc@456
|
774 st,
|
adamc@456
|
775 trans)
|
adamc@249
|
776 end
|
adamc@249
|
777 | L.SgiCon (x, n, k, _) =>
|
adamc@249
|
778 let
|
adamc@249
|
779 val (st, n') = St.bindCon st x n
|
adamc@249
|
780 in
|
adamc@249
|
781 ((L'.DCon (x, n', corifyKind k, (L'.CFfi (m, x), loc)), loc) :: ds,
|
adamc@249
|
782 cmap,
|
adamc@249
|
783 conmap,
|
adamc@456
|
784 st,
|
adamc@456
|
785 trans)
|
adamc@249
|
786 end
|
adamc@177
|
787
|
adamc@249
|
788 | L.SgiDatatype (x, n, xs, xnts) =>
|
adamc@249
|
789 let
|
adamc@249
|
790 val k = (L'.KType, loc)
|
adamc@249
|
791 val dk = ElabUtil.classifyDatatype xnts
|
adamc@249
|
792 val (st, n') = St.bindCon st x n
|
adamc@249
|
793 val (xnts, (ds', st, cmap, conmap)) =
|
adamc@249
|
794 ListUtil.foldlMap
|
adamc@249
|
795 (fn ((x', n, to), (ds', st, cmap, conmap)) =>
|
adamc@249
|
796 let
|
adamc@249
|
797 val dt = (L'.CNamed n', loc)
|
adamc@249
|
798 val args = ListUtil.mapi (fn (i, _) => (L'.CRel i, loc)) xs
|
adamc@163
|
799
|
adamc@249
|
800 val to = Option.map (corifyCon st) to
|
adamc@177
|
801
|
adamc@249
|
802 val pc = L'.PConFfi {mod = m,
|
adamc@249
|
803 datatyp = x,
|
adamc@249
|
804 params = xs,
|
adamc@249
|
805 con = x',
|
adamc@249
|
806 arg = to,
|
adamc@249
|
807 kind = dk}
|
adamc@130
|
808
|
adamc@249
|
809 fun wrapT t =
|
adamc@249
|
810 foldr (fn (x, t) => (L'.TCFun (x, k, t), loc)) t xs
|
adamc@249
|
811 fun wrapE e =
|
adamc@249
|
812 foldr (fn (x, e) => (L'.ECAbs (x, k, e), loc)) e xs
|
adamc@192
|
813
|
adamc@249
|
814 val (cmap, d) =
|
adamc@249
|
815 case to of
|
adamc@249
|
816 NONE => (SM.insert (cmap, x', wrapT dt),
|
adamc@249
|
817 (L'.DVal (x', n, wrapT dt,
|
adamc@249
|
818 wrapE
|
adamc@249
|
819 (L'.ECon (dk, pc, args, NONE),
|
adamc@249
|
820 loc),
|
adamc@249
|
821 ""), loc))
|
adamc@249
|
822 | SOME t =>
|
adamc@249
|
823 let
|
adamc@249
|
824 val tf = (L'.TFun (t, dt), loc)
|
adamc@249
|
825 val e = wrapE (L'.EAbs ("x", t, tf,
|
adamc@249
|
826 (L'.ECon (dk, pc, args,
|
adamc@249
|
827 SOME (L'.ERel 0,
|
adamc@249
|
828 loc)),
|
adamc@249
|
829 loc)), loc)
|
adamc@249
|
830 val d = (L'.DVal (x', n, wrapT tf,
|
adamc@249
|
831 e, ""), loc)
|
adamc@185
|
832 in
|
adamc@192
|
833 (SM.insert (cmap, x', wrapT tf), d)
|
adamc@185
|
834 end
|
adamc@185
|
835
|
adamc@186
|
836 val st = St.bindConstructor st x' n pc
|
adamc@186
|
837
|
adamc@192
|
838 val conmap = SM.insert (conmap, x', (x, xs, to, dk))
|
adamc@185
|
839 in
|
adamc@185
|
840 ((x', n, to),
|
adamc@186
|
841 (d :: ds', st, cmap, conmap))
|
adamc@186
|
842 end) ([], st, cmap, conmap) xnts
|
adamc@185
|
843 in
|
adamc@192
|
844 (ds' @ (L'.DDatatype (x, n', xs, xnts), loc) :: ds,
|
adamc@185
|
845 cmap,
|
adamc@185
|
846 conmap,
|
adamc@456
|
847 st,
|
adamc@456
|
848 trans)
|
adamc@192
|
849 end
|
adamc@49
|
850
|
adamc@49
|
851 | L.SgiVal (x, _, c) =>
|
adamc@456
|
852 let
|
adamc@456
|
853 val c =
|
adamc@456
|
854 case trans of
|
adamc@456
|
855 NONE => corifyCon st c
|
adamc@456
|
856 | SOME trans =>
|
adamc@456
|
857 let
|
adamc@456
|
858 fun transactify (all as (c, loc)) =
|
adamc@456
|
859 case c of
|
adamc@456
|
860 L.TFun (dom, ran) =>
|
adamc@456
|
861 (L'.TFun (corifyCon st dom, transactify ran), loc)
|
adamc@456
|
862 | L.CApp ((L.CNamed trans', _), t) =>
|
adamc@456
|
863 if trans' = trans then
|
adamc@456
|
864 (L'.CApp ((L'.CFfi (m, "transaction"), loc),
|
adamc@456
|
865 corifyCon st t), loc)
|
adamc@456
|
866 else
|
adamc@456
|
867 corifyCon st all
|
adamc@456
|
868 | _ => corifyCon st all
|
adamc@456
|
869 in
|
adamc@456
|
870 transactify c
|
adamc@456
|
871 end
|
adamc@456
|
872 in
|
adamc@456
|
873 (ds,
|
adamc@456
|
874 SM.insert (cmap, x, c),
|
adamc@456
|
875 conmap,
|
adamc@456
|
876 st,
|
adamc@456
|
877 trans)
|
adamc@456
|
878 end
|
adamc@456
|
879 | _ => (ds, cmap, conmap, st, trans))
|
adamc@456
|
880 ([], SM.empty, SM.empty, st, NONE) sgis
|
adamc@49
|
881
|
adamc@185
|
882 val st = St.bindStr st m n (St.ffi m cmap conmap)
|
adamc@49
|
883 in
|
adamc@249
|
884 (rev ds, St.basisIs (st, n))
|
adamc@49
|
885 end
|
adamc@49
|
886 | _ => raise Fail "Non-const signature for FFI structure")
|
adamc@48
|
887
|
adamc@109
|
888 | L.DExport (en, sgn, str) =>
|
adamc@109
|
889 (case #1 sgn of
|
adamc@109
|
890 L.SgnConst sgis =>
|
adamc@109
|
891 let
|
adamc@109
|
892 fun pathify (str, _) =
|
adamc@109
|
893 case str of
|
adamc@109
|
894 L.StrVar m => SOME (m, [])
|
adamc@109
|
895 | L.StrProj (str, s) =>
|
adamc@109
|
896 Option.map (fn (m, ms) => (m, ms @ [s])) (pathify str)
|
adamc@109
|
897 | _ => NONE
|
adamc@109
|
898 in
|
adamc@109
|
899 case pathify str of
|
adamc@109
|
900 NONE => (ErrorMsg.errorAt loc "Structure is too fancy to export";
|
adamc@109
|
901 ([], st))
|
adamc@109
|
902 | SOME (m, ms) =>
|
adamc@109
|
903 let
|
adamc@249
|
904 val basis_n = case St.lookupBasis st of
|
adamc@249
|
905 NONE => raise Fail "Corify: Don't know number of Basis"
|
adamc@249
|
906 | SOME n => n
|
adamc@249
|
907
|
adamc@109
|
908 fun wrapSgi ((sgi, _), (wds, eds)) =
|
adamc@109
|
909 case sgi of
|
adamc@109
|
910 L.SgiVal (s, _, t as (L.TFun (dom, ran), _)) =>
|
adamc@109
|
911 (case (#1 dom, #1 ran) of
|
adamc@249
|
912 (L.TRecord _,
|
adamc@249
|
913 L.CApp ((L.CModProj (basis, [], "transaction"), _),
|
adamc@249
|
914 ran' as
|
adamc@249
|
915 (L.CApp
|
adamc@249
|
916 ((L.CApp
|
adamc@249
|
917 ((L.CApp ((L.CModProj (basis', [], "xml"), _),
|
adamc@249
|
918 (L.CRecord (_, [((L.CName "Html", _),
|
adamc@249
|
919 _)]), _)), _), _),
|
adamc@249
|
920 _), _), _))) =>
|
adamc@109
|
921 let
|
adamc@109
|
922 val ran = (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc)
|
adamc@249
|
923 val ranT = (L.CApp ((L.CModProj (basis, [], "transaction"), loc),
|
adamc@249
|
924 ran), loc)
|
adamc@109
|
925 val e = (L.EModProj (m, ms, s), loc)
|
adamc@249
|
926
|
adamc@249
|
927 val ef = (L.EModProj (basis, [], "bind"), loc)
|
adamc@564
|
928 val ef = (L.ECApp (ef, (L.CModProj (basis, [], "transaction"), loc)), loc)
|
adamc@249
|
929 val ef = (L.ECApp (ef, ran'), loc)
|
adamc@251
|
930 val ef = (L.ECApp (ef, ran), loc)
|
adamc@564
|
931 val ef = (L.EApp (ef, (L.EModProj (basis, [], "transaction_monad"), loc)), loc)
|
adamc@249
|
932 val ef = (L.EApp (ef, (L.EApp (e, (L.ERel 0, loc)), loc)), loc)
|
adamc@249
|
933
|
adamc@249
|
934 val eat = (L.CApp ((L.CModProj (basis, [], "transaction"), loc),
|
adamc@249
|
935 ran), loc)
|
adamc@249
|
936 val ea = (L.EAbs ("p", ran', eat,
|
adamc@249
|
937 (L.EWrite (L.ERel 0, loc), loc)), loc)
|
adamc@249
|
938
|
adamc@249
|
939 val e = (L.EApp (ef, ea), loc)
|
adamc@249
|
940 val e = (L.EAbs ("vs", dom, ran, e), loc)
|
adamc@109
|
941 in
|
adamc@249
|
942 if basis = basis_n andalso basis' = basis_n then
|
adamc@249
|
943 ((L.DVal ("wrap_" ^ s, 0,
|
adamc@249
|
944 (L.TFun (dom, ranT), loc),
|
adamc@249
|
945 e), loc) :: wds,
|
adamc@249
|
946 (fn st =>
|
adamc@249
|
947 case #1 (corifyExp st (L.EModProj (en, [], "wrap_" ^ s), loc)) of
|
adamc@249
|
948 L'.ENamed n => (L'.DExport (L'.Link, n), loc)
|
adamc@249
|
949 | _ => raise Fail "Corify: Value to export didn't corify properly")
|
adamc@249
|
950 :: eds)
|
adamc@249
|
951 else
|
adamc@249
|
952 (wds, eds)
|
adamc@109
|
953 end
|
adamc@109
|
954 | _ => (wds, eds))
|
adamc@109
|
955 | _ => (wds, eds)
|
adamc@102
|
956
|
adamc@109
|
957 val (wds, eds) = foldl wrapSgi ([], []) sgis
|
adamc@109
|
958 val wrapper = (L.StrConst wds, loc)
|
adamc@376
|
959 val mst = St.lookupStrById st m
|
adamc@376
|
960 val (ds, {inner, outer}) = corifyStr (St.name mst) (wrapper, st)
|
adamc@109
|
961 val st = St.bindStr outer "wrapper" en inner
|
adamc@249
|
962
|
adamc@109
|
963 val ds = ds @ map (fn f => f st) eds
|
adamc@109
|
964 in
|
adamc@109
|
965 (ds, st)
|
adamc@109
|
966 end
|
adamc@109
|
967 end
|
adamc@109
|
968 | _ => raise Fail "Non-const signature for 'export'")
|
adamc@48
|
969
|
adamc@249
|
970 | L.DTable (_, x, n, c) =>
|
adamc@249
|
971 let
|
adamc@249
|
972 val (st, n) = St.bindVal st x n
|
adamc@377
|
973 val s = relify (doRestify (mods, x))
|
adamc@249
|
974 in
|
adamc@249
|
975 ([(L'.DTable (x, n, corifyCon st c, s), loc)], st)
|
adamc@249
|
976 end
|
adamc@338
|
977 | L.DSequence (_, x, n) =>
|
adamc@338
|
978 let
|
adamc@338
|
979 val (st, n) = St.bindVal st x n
|
adamc@377
|
980 val s = relify (doRestify (mods, x))
|
adamc@338
|
981 in
|
adamc@338
|
982 ([(L'.DSequence (x, n, s), loc)], st)
|
adamc@338
|
983 end
|
adamc@246
|
984
|
adamc@271
|
985 | L.DDatabase s => ([(L'.DDatabase s, loc)], st)
|
adamc@271
|
986
|
adamc@461
|
987 | L.DCookie (_, x, n, c) =>
|
adamc@461
|
988 let
|
adamc@461
|
989 val (st, n) = St.bindVal st x n
|
adamc@461
|
990 val s = doRestify (mods, x)
|
adamc@461
|
991 in
|
adamc@461
|
992 ([(L'.DCookie (x, n, corifyCon st c, s), loc)], st)
|
adamc@461
|
993 end
|
adamc@461
|
994
|
adamc@376
|
995 and corifyStr mods ((str, _), st) =
|
adamc@39
|
996 case str of
|
adamc@39
|
997 L.StrConst ds =>
|
adamc@39
|
998 let
|
adamc@376
|
999 val st = St.enter (st, mods)
|
adamc@376
|
1000 val (ds, st) = ListUtil.foldlMapConcat (corifyDecl mods) st ds
|
adamc@39
|
1001 in
|
adamc@39
|
1002 (ds, St.leave st)
|
adamc@39
|
1003 end
|
adamc@39
|
1004 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
|
adamc@39
|
1005 | L.StrProj (str, x) =>
|
adamc@39
|
1006 let
|
adamc@376
|
1007 val (ds, {inner, outer}) = corifyStr mods (str, st)
|
adamc@39
|
1008 in
|
adamc@39
|
1009 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
|
adamc@39
|
1010 end
|
adamc@46
|
1011 | L.StrFun _ => raise Fail "Corify of nested functor definition"
|
adamc@46
|
1012 | L.StrApp (str1, str2) =>
|
adamc@46
|
1013 let
|
adamc@46
|
1014 fun unwind' (str, _) =
|
adamc@46
|
1015 case str of
|
adamc@46
|
1016 L.StrVar n => St.lookupStrById st n
|
adamc@46
|
1017 | L.StrProj (str, x) => St.lookupStrByName (x, unwind' str)
|
adamc@46
|
1018 | _ => raise Fail "Corify of fancy functor application [1]"
|
adamc@46
|
1019
|
adamc@46
|
1020 fun unwind (str, _) =
|
adamc@46
|
1021 case str of
|
adamc@46
|
1022 L.StrVar n => St.lookupFunctorById st n
|
adamc@46
|
1023 | L.StrProj (str, x) => St.lookupFunctorByName (x, unwind' str)
|
adamc@46
|
1024 | _ => raise Fail "Corify of fancy functor application [2]"
|
adamc@46
|
1025
|
adamc@423
|
1026 val (xa, na, body) = unwind str1
|
adamc@46
|
1027
|
adamc@376
|
1028 val (ds1, {inner = inner', outer}) = corifyStr mods (str2, st)
|
adamc@376
|
1029
|
adamc@423
|
1030 val (ds2, {inner, outer}) = corifyStr mods (body, St.bindStr outer xa na inner')
|
adamc@46
|
1031 in
|
adamc@146
|
1032 (ds1 @ ds2, {inner = St.bindStr inner xa na inner', outer = outer})
|
adamc@46
|
1033 end
|
adamc@31
|
1034
|
adamc@39
|
1035 fun maxName ds = foldl (fn ((d, _), n) =>
|
adamc@39
|
1036 case d of
|
adamc@39
|
1037 L.DCon (_, n', _, _) => Int.max (n, n')
|
adamc@191
|
1038 | L.DDatatype (_, n', _, _) => Int.max (n, n')
|
adamc@191
|
1039 | L.DDatatypeImp (_, n', _, _, _, _, _) => Int.max (n, n')
|
adamc@124
|
1040 | L.DVal (_, n', _, _) => Int.max (n, n')
|
adamc@124
|
1041 | L.DValRec vis => foldl (fn ((_, n', _, _), n) => Int.max (n, n)) n vis
|
adamc@39
|
1042 | L.DSgn (_, n', _) => Int.max (n, n')
|
adamc@48
|
1043 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str))
|
adamc@100
|
1044 | L.DFfiStr (_, n', _) => Int.max (n, n')
|
adamc@246
|
1045 | L.DExport _ => n
|
adamc@271
|
1046 | L.DTable (_, _, n', _) => Int.max (n, n')
|
adamc@338
|
1047 | L.DSequence (_, _, n') => Int.max (n, n')
|
adamc@461
|
1048 | L.DDatabase _ => n
|
adamc@461
|
1049 | L.DCookie (_, _, n', _) => Int.max (n, n'))
|
adamc@249
|
1050 0 ds
|
adamc@39
|
1051
|
adamc@39
|
1052 and maxNameStr (str, _) =
|
adamc@39
|
1053 case str of
|
adamc@39
|
1054 L.StrConst ds => maxName ds
|
adamc@39
|
1055 | L.StrVar n => n
|
adamc@39
|
1056 | L.StrProj (str, _) => maxNameStr str
|
adamc@45
|
1057 | L.StrFun (_, _, _, _, str) => maxNameStr str
|
adamc@45
|
1058 | L.StrApp (str1, str2) => Int.max (maxNameStr str1, maxNameStr str2)
|
adamc@39
|
1059
|
adamc@39
|
1060 fun corify ds =
|
adamc@39
|
1061 let
|
adamc@39
|
1062 val () = reset (maxName ds + 1)
|
adamc@146
|
1063
|
adamc@376
|
1064 val (ds, _) = ListUtil.foldlMapConcat (corifyDecl []) St.empty ds
|
adamc@39
|
1065 in
|
adamc@39
|
1066 ds
|
adamc@39
|
1067 end
|
adamc@16
|
1068
|
adamc@16
|
1069 end
|