adamc@25
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@25
|
2 * All rights reserved.
|
adamc@25
|
3 *
|
adamc@25
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@25
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@25
|
6 *
|
adamc@25
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@25
|
8 * this list of conditions and the following disclaimer.
|
adamc@25
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@25
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@25
|
11 * and/or other materials provided with the distribution.
|
adamc@25
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@25
|
13 * derived from this software without specific prior written permission.
|
adamc@25
|
14 *
|
adamc@25
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@25
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@25
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@25
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@25
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@25
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@25
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@25
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@25
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@25
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@25
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@25
|
26 *)
|
adamc@25
|
27
|
adamc@25
|
28 structure Monoize :> MONOIZE = struct
|
adamc@25
|
29
|
adamc@25
|
30 structure E = ErrorMsg
|
adamc@25
|
31 structure Env = CoreEnv
|
adamc@25
|
32
|
adamc@25
|
33 structure L = Core
|
adamc@25
|
34 structure L' = Mono
|
adamc@25
|
35
|
adamc@196
|
36 structure IM = IntBinaryMap
|
adamc@196
|
37
|
adamc@385
|
38 val urlPrefix = ref "/"
|
adamc@385
|
39
|
adamc@196
|
40 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
|
adamc@25
|
41
|
adamc@252
|
42 structure U = MonoUtil
|
adamc@252
|
43
|
adamc@252
|
44 val liftExpInExp =
|
adamc@252
|
45 U.Exp.mapB {typ = fn t => t,
|
adamc@252
|
46 exp = fn bound => fn e =>
|
adamc@252
|
47 case e of
|
adamc@252
|
48 L'.ERel xn =>
|
adamc@252
|
49 if xn < bound then
|
adamc@252
|
50 e
|
adamc@252
|
51 else
|
adamc@252
|
52 L'.ERel (xn + 1)
|
adamc@252
|
53 | _ => e,
|
adamc@252
|
54 bind = fn (bound, U.Exp.RelE _) => bound + 1
|
adamc@252
|
55 | (bound, _) => bound}
|
adamc@252
|
56
|
adamc@25
|
57 fun monoName env (all as (c, loc)) =
|
adamc@25
|
58 let
|
adamc@25
|
59 fun poly () =
|
adamc@25
|
60 (E.errorAt loc "Unsupported name constructor";
|
adamc@25
|
61 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
|
adamc@25
|
62 "")
|
adamc@25
|
63 in
|
adamc@25
|
64 case c of
|
adamc@25
|
65 L.CName s => s
|
adamc@25
|
66 | _ => poly ()
|
adamc@25
|
67 end
|
adamc@25
|
68
|
adamc@292
|
69 fun readType' (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
|
adamc@292
|
70 (L'.TOption t, loc)), loc)
|
adamc@292
|
71 fun readErrType (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
|
adamc@292
|
72 t), loc)
|
adamc@292
|
73 fun readType (t, loc) =
|
adamc@292
|
74 (L'.TRecord [("Read", readType' (t, loc)),
|
adamc@292
|
75 ("ReadError", readErrType (t, loc))],
|
adamc@292
|
76 loc)
|
adamc@292
|
77
|
adamc@196
|
78 fun monoType env =
|
adamc@25
|
79 let
|
adamc@196
|
80 fun mt env dtmap (all as (c, loc)) =
|
adamc@196
|
81 let
|
adamc@196
|
82 fun poly () =
|
adamc@196
|
83 (E.errorAt loc "Unsupported type constructor";
|
adamc@196
|
84 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
|
adamc@196
|
85 dummyTyp)
|
adamc@196
|
86 in
|
adamc@196
|
87 case c of
|
adamc@196
|
88 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
|
adamc@196
|
89 | L.TCFun _ => poly ()
|
adamc@196
|
90 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
|
adamc@196
|
91 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
|
adamc@196
|
92 | L.TRecord _ => poly ()
|
adamc@196
|
93
|
adamc@288
|
94 | L.CApp ((L.CFfi ("Basis", "option"), _), t) =>
|
adamc@288
|
95 (L'.TOption (mt env dtmap t), loc)
|
adamc@288
|
96
|
adamc@387
|
97 | L.CApp ((L.CFfi ("Basis", "eq"), _), t) =>
|
adamc@387
|
98 let
|
adamc@387
|
99 val t = mt env dtmap t
|
adamc@387
|
100 in
|
adamc@387
|
101 (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)
|
adamc@387
|
102 end
|
adamc@389
|
103 | L.CApp ((L.CFfi ("Basis", "num"), _), t) =>
|
adamc@389
|
104 let
|
adamc@389
|
105 val t = mt env dtmap t
|
adamc@389
|
106 in
|
adamc@417
|
107 (L'.TRecord [("Zero", t),
|
adamc@417
|
108 ("Neg", (L'.TFun (t, t), loc)),
|
adamc@389
|
109 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
110 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
111 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
112 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
113 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))],
|
adamc@389
|
114 loc)
|
adamc@389
|
115 end
|
adamc@391
|
116 | L.CApp ((L.CFfi ("Basis", "ord"), _), t) =>
|
adamc@391
|
117 let
|
adamc@391
|
118 val t = mt env dtmap t
|
adamc@391
|
119 in
|
adamc@391
|
120 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
|
adamc@391
|
121 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
|
adamc@391
|
122 loc)
|
adamc@391
|
123 end
|
adamc@286
|
124 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
|
adamc@286
|
125 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
|
adamc@290
|
126 | L.CApp ((L.CFfi ("Basis", "read"), _), t) =>
|
adamc@292
|
127 readType (mt env dtmap t, loc)
|
adamc@286
|
128
|
adamc@196
|
129 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
|
adamc@196
|
130 (L'.TFfi ("Basis", "string"), loc)
|
adamc@196
|
131 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
|
adamc@196
|
132 (L'.TFfi ("Basis", "string"), loc)
|
adamc@196
|
133
|
adamc@251
|
134 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
|
adamc@252
|
135 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
|
adamc@252
|
136 | L.CApp ((L.CFfi ("Basis", "sql_table"), _), _) =>
|
adamc@252
|
137 (L'.TFfi ("Basis", "string"), loc)
|
adamc@338
|
138 | L.CFfi ("Basis", "sql_sequence") =>
|
adamc@338
|
139 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
140 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
|
adamc@252
|
141 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
142 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
|
adamc@252
|
143 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
144 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
|
adamc@252
|
145 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
146
|
adamc@252
|
147 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
|
adamc@252
|
148 (L'.TRecord [], loc)
|
adamc@252
|
149 | L.CFfi ("Basis", "sql_relop") =>
|
adamc@252
|
150 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
151 | L.CFfi ("Basis", "sql_direction") =>
|
adamc@252
|
152 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
153 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
|
adamc@252
|
154 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
155 | L.CFfi ("Basis", "sql_limit") =>
|
adamc@252
|
156 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
157 | L.CFfi ("Basis", "sql_offset") =>
|
adamc@252
|
158 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
159
|
adamc@252
|
160 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
|
adamc@252
|
161 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
|
adamc@252
|
162 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
|
adamc@252
|
163 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
164 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
|
adamc@252
|
165 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
166 | L.CFfi ("Basis", "sql_comparison") =>
|
adamc@252
|
167 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
168 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
|
adamc@252
|
169 (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
170 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
|
adamc@252
|
171 (L'.TRecord [], loc)
|
adamc@252
|
172 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
|
adamc@252
|
173 (L'.TRecord [], loc)
|
adamc@251
|
174
|
adamc@196
|
175 | L.CRel _ => poly ()
|
adamc@196
|
176 | L.CNamed n =>
|
adamc@196
|
177 (case IM.find (dtmap, n) of
|
adamc@196
|
178 SOME r => (L'.TDatatype (n, r), loc)
|
adamc@196
|
179 | NONE =>
|
adamc@196
|
180 let
|
adamc@196
|
181 val r = ref (L'.Default, [])
|
adamc@196
|
182 val (_, xs, xncs) = Env.lookupDatatype env n
|
adamc@196
|
183
|
adamc@196
|
184 val dtmap' = IM.insert (dtmap, n, r)
|
adamc@196
|
185
|
adamc@196
|
186 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
|
adamc@196
|
187 in
|
adamc@196
|
188 case xs of
|
adamc@198
|
189 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
|
adamc@196
|
190 (L'.TDatatype (n, r), loc))
|
adamc@196
|
191 | _ => poly ()
|
adamc@196
|
192 end)
|
adamc@196
|
193 | L.CFfi mx => (L'.TFfi mx, loc)
|
adamc@196
|
194 | L.CApp _ => poly ()
|
adamc@196
|
195 | L.CAbs _ => poly ()
|
adamc@196
|
196
|
adamc@196
|
197 | L.CName _ => poly ()
|
adamc@196
|
198
|
adamc@196
|
199 | L.CRecord _ => poly ()
|
adamc@196
|
200 | L.CConcat _ => poly ()
|
adamc@196
|
201 | L.CFold _ => poly ()
|
adamc@196
|
202 | L.CUnit => poly ()
|
adamc@214
|
203
|
adamc@214
|
204 | L.CTuple _ => poly ()
|
adamc@214
|
205 | L.CProj _ => poly ()
|
adamc@196
|
206 end
|
adamc@25
|
207 in
|
adamc@196
|
208 mt env IM.empty
|
adamc@25
|
209 end
|
adamc@25
|
210
|
adamc@25
|
211 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
|
adamc@25
|
212
|
adamc@179
|
213 structure IM = IntBinaryMap
|
adamc@179
|
214
|
adamc@179
|
215 datatype foo_kind =
|
adamc@179
|
216 Attr
|
adamc@179
|
217 | Url
|
adamc@179
|
218
|
adamc@179
|
219 fun fk2s fk =
|
adamc@179
|
220 case fk of
|
adamc@179
|
221 Attr => "attr"
|
adamc@179
|
222 | Url => "url"
|
adamc@179
|
223
|
adamc@179
|
224 structure Fm :> sig
|
adamc@179
|
225 type t
|
adamc@179
|
226
|
adamc@179
|
227 val empty : int -> t
|
adamc@179
|
228
|
adamc@179
|
229 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
|
adamc@179
|
230 val enter : t -> t
|
adamc@179
|
231 val decls : t -> L'.decl list
|
adamc@179
|
232 end = struct
|
adamc@179
|
233
|
adamc@179
|
234 structure M = BinaryMapFn(struct
|
adamc@179
|
235 type ord_key = foo_kind
|
adamc@179
|
236 fun compare x =
|
adamc@179
|
237 case x of
|
adamc@179
|
238 (Attr, Attr) => EQUAL
|
adamc@179
|
239 | (Attr, _) => LESS
|
adamc@179
|
240 | (_, Attr) => GREATER
|
adamc@179
|
241
|
adamc@179
|
242 | (Url, Url) => EQUAL
|
adamc@179
|
243 end)
|
adamc@179
|
244
|
adamc@179
|
245 type t = {
|
adamc@179
|
246 count : int,
|
adamc@179
|
247 map : int IM.map M.map,
|
adamc@179
|
248 decls : L'.decl list
|
adamc@179
|
249 }
|
adamc@179
|
250
|
adamc@179
|
251 fun empty count = {
|
adamc@179
|
252 count = count,
|
adamc@179
|
253 map = M.empty,
|
adamc@179
|
254 decls = []
|
adamc@179
|
255 }
|
adamc@179
|
256
|
adamc@179
|
257 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
|
adamc@179
|
258 fun decls ({decls, ...} : t) = decls
|
adamc@179
|
259
|
adamc@179
|
260 fun lookup (t as {count, map, decls}) k n thunk =
|
adamc@120
|
261 let
|
adamc@179
|
262 val im = Option.getOpt (M.find (map, k), IM.empty)
|
adamc@179
|
263 in
|
adamc@179
|
264 case IM.find (im, n) of
|
adamc@179
|
265 NONE =>
|
adamc@179
|
266 let
|
adamc@179
|
267 val n' = count
|
adamc@179
|
268 val (d, {count, map, decls}) = thunk count {count = count + 1,
|
adamc@179
|
269 map = M.insert (map, k, IM.insert (im, n, n')),
|
adamc@179
|
270 decls = decls}
|
adamc@179
|
271 in
|
adamc@179
|
272 ({count = count,
|
adamc@179
|
273 map = map,
|
adamc@179
|
274 decls = d :: decls}, n')
|
adamc@179
|
275 end
|
adamc@179
|
276 | SOME n' => (t, n')
|
adamc@179
|
277 end
|
adamc@179
|
278
|
adamc@179
|
279 end
|
adamc@185
|
280
|
adamc@185
|
281
|
adamc@185
|
282 fun capitalize s =
|
adamc@185
|
283 if s = "" then
|
adamc@185
|
284 s
|
adamc@185
|
285 else
|
adamc@185
|
286 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@179
|
287
|
adamc@179
|
288 fun fooifyExp fk env =
|
adamc@179
|
289 let
|
adamc@179
|
290 fun fooify fm (e, tAll as (t, loc)) =
|
adamc@120
|
291 case #1 e of
|
adamc@120
|
292 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
|
adamc@120
|
293 let
|
adamc@120
|
294 val (_, _, _, s) = Env.lookupENamed env fnam
|
adamc@120
|
295 in
|
adamc@385
|
296 ((L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
|
adamc@120
|
297 end
|
adamc@120
|
298 | L'.EClosure (fnam, args) =>
|
adamc@120
|
299 let
|
adamc@120
|
300 val (_, ft, _, s) = Env.lookupENamed env fnam
|
adamc@120
|
301 val ft = monoType env ft
|
adamc@111
|
302
|
adamc@179
|
303 fun attrify (args, ft, e, fm) =
|
adamc@120
|
304 case (args, ft) of
|
adamc@179
|
305 ([], _) => (e, fm)
|
adamc@120
|
306 | (arg :: args, (L'.TFun (t, ft), _)) =>
|
adamc@179
|
307 let
|
adamc@179
|
308 val (arg', fm) = fooify fm (arg, t)
|
adamc@179
|
309 in
|
adamc@179
|
310 attrify (args, ft,
|
adamc@179
|
311 (L'.EStrcat (e,
|
adamc@179
|
312 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
|
adamc@179
|
313 arg'), loc)), loc),
|
adamc@179
|
314 fm)
|
adamc@179
|
315 end
|
adamc@120
|
316 | _ => (E.errorAt loc "Type mismatch encoding attribute";
|
adamc@179
|
317 (e, fm))
|
adamc@120
|
318 in
|
adamc@385
|
319 attrify (args, ft, (L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
|
adamc@120
|
320 end
|
adamc@120
|
321 | _ =>
|
adamc@120
|
322 case t of
|
adamc@185
|
323 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
|
adamc@200
|
324
|
adamc@179
|
325 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@200
|
326 | L'.TRecord ((x, t) :: xts) =>
|
adamc@200
|
327 let
|
adamc@200
|
328 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
|
adamc@200
|
329 in
|
adamc@200
|
330 foldl (fn ((x, t), (se, fm)) =>
|
adamc@200
|
331 let
|
adamc@200
|
332 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
|
adamc@200
|
333 in
|
adamc@200
|
334 ((L'.EStrcat (se,
|
adamc@200
|
335 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
|
adamc@200
|
336 se'), loc)), loc),
|
adamc@200
|
337 fm)
|
adamc@200
|
338 end) (se, fm) xts
|
adamc@200
|
339 end
|
adamc@111
|
340
|
adamc@196
|
341 | L'.TDatatype (i, ref (dk, _)) =>
|
adamc@179
|
342 let
|
adamc@179
|
343 fun makeDecl n fm =
|
adamc@179
|
344 let
|
adamc@193
|
345 val (x, _, xncs) = Env.lookupDatatype env i
|
adamc@179
|
346
|
adamc@179
|
347 val (branches, fm) =
|
adamc@179
|
348 ListUtil.foldlMap
|
adamc@179
|
349 (fn ((x, n, to), fm) =>
|
adamc@179
|
350 case to of
|
adamc@179
|
351 NONE =>
|
adamc@188
|
352 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
|
adamc@179
|
353 (L'.EPrim (Prim.String x), loc)),
|
adamc@179
|
354 fm)
|
adamc@179
|
355 | SOME t =>
|
adamc@179
|
356 let
|
adamc@182
|
357 val t = monoType env t
|
adamc@182
|
358 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
|
adamc@179
|
359 in
|
adamc@188
|
360 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
|
adamc@179
|
361 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
|
adamc@179
|
362 arg), loc)),
|
adamc@179
|
363 fm)
|
adamc@179
|
364 end)
|
adamc@179
|
365 fm xncs
|
adamc@179
|
366
|
adamc@179
|
367 val dom = tAll
|
adamc@179
|
368 val ran = (L'.TFfi ("Basis", "string"), loc)
|
adamc@179
|
369 in
|
adamc@179
|
370 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
|
adamc@179
|
371 n,
|
adamc@179
|
372 (L'.TFun (dom, ran), loc),
|
adamc@179
|
373 (L'.EAbs ("x",
|
adamc@179
|
374 dom,
|
adamc@179
|
375 ran,
|
adamc@179
|
376 (L'.ECase ((L'.ERel 0, loc),
|
adamc@179
|
377 branches,
|
adamc@182
|
378 {disc = dom,
|
adamc@182
|
379 result = ran}), loc)), loc),
|
adamc@179
|
380 "")], loc),
|
adamc@179
|
381 fm)
|
adamc@179
|
382 end
|
adamc@179
|
383
|
adamc@179
|
384 val (fm, n) = Fm.lookup fm fk i makeDecl
|
adamc@179
|
385 in
|
adamc@179
|
386 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
|
adamc@179
|
387 end
|
adamc@164
|
388
|
adamc@120
|
389 | _ => (E.errorAt loc "Don't know how to encode attribute type";
|
adamc@120
|
390 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
|
adamc@179
|
391 (dummyExp, fm))
|
adamc@120
|
392 in
|
adamc@120
|
393 fooify
|
adamc@120
|
394 end
|
adamc@120
|
395
|
adamc@179
|
396 val attrifyExp = fooifyExp Attr
|
adamc@179
|
397 val urlifyExp = fooifyExp Url
|
adamc@105
|
398
|
adamc@143
|
399 datatype 'a failable_search =
|
adamc@143
|
400 Found of 'a
|
adamc@143
|
401 | NotFound
|
adamc@143
|
402 | Error
|
adamc@143
|
403
|
adamc@153
|
404 structure St :> sig
|
adamc@153
|
405 type t
|
adamc@153
|
406
|
adamc@153
|
407 val empty : t
|
adamc@153
|
408
|
adamc@153
|
409 val radioGroup : t -> string option
|
adamc@153
|
410 val setRadioGroup : t * string -> t
|
adamc@153
|
411 end = struct
|
adamc@153
|
412
|
adamc@153
|
413 type t = {
|
adamc@153
|
414 radioGroup : string option
|
adamc@153
|
415 }
|
adamc@153
|
416
|
adamc@153
|
417 val empty = {radioGroup = NONE}
|
adamc@153
|
418
|
adamc@153
|
419 fun radioGroup (t : t) = #radioGroup t
|
adamc@153
|
420
|
adamc@153
|
421 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
|
adamc@153
|
422
|
adamc@153
|
423 end
|
adamc@153
|
424
|
adamc@186
|
425 fun monoPatCon env pc =
|
adamc@178
|
426 case pc of
|
adamc@178
|
427 L.PConVar n => L'.PConVar n
|
adamc@188
|
428 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
|
adamc@188
|
429 arg = Option.map (monoType env) arg}
|
adamc@178
|
430
|
adamc@193
|
431 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
|
adamc@193
|
432
|
adamc@193
|
433 fun monoPat env (all as (p, loc)) =
|
adamc@193
|
434 let
|
adamc@193
|
435 fun poly () =
|
adamc@193
|
436 (E.errorAt loc "Unsupported pattern";
|
adamc@193
|
437 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
|
adamc@193
|
438 dummyPat)
|
adamc@193
|
439 in
|
adamc@193
|
440 case p of
|
adamc@193
|
441 L.PWild => (L'.PWild, loc)
|
adamc@193
|
442 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
|
adamc@193
|
443 | L.PPrim p => (L'.PPrim p, loc)
|
adamc@193
|
444 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
|
adamc@288
|
445 | L.PCon (L.Option, _, [t], NONE) => (L'.PNone (monoType env t), loc)
|
adamc@288
|
446 | L.PCon (L.Option, _, [t], SOME p) => (L'.PSome (monoType env t, monoPat env p), loc)
|
adamc@193
|
447 | L.PCon _ => poly ()
|
adamc@193
|
448 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
|
adamc@193
|
449 end
|
adamc@178
|
450
|
adamc@252
|
451 fun strcat loc es =
|
adamc@252
|
452 case es of
|
adamc@252
|
453 [] => (L'.EPrim (Prim.String ""), loc)
|
adamc@252
|
454 | [e] => e
|
adamc@252
|
455 | _ =>
|
adamc@252
|
456 let
|
adamc@252
|
457 val e2 = List.last es
|
adamc@252
|
458 val es = List.take (es, length es - 1)
|
adamc@252
|
459 val e1 = List.last es
|
adamc@252
|
460 val es = List.take (es, length es - 1)
|
adamc@252
|
461 in
|
adamc@252
|
462 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
|
adamc@252
|
463 (L'.EStrcat (e1, e2), loc) es
|
adamc@252
|
464 end
|
adamc@252
|
465
|
adamc@252
|
466 fun strcatComma loc es =
|
adamc@252
|
467 case es of
|
adamc@252
|
468 [] => (L'.EPrim (Prim.String ""), loc)
|
adamc@252
|
469 | [e] => e
|
adamc@252
|
470 | _ =>
|
adamc@252
|
471 let
|
adamc@252
|
472 val e1 = List.last es
|
adamc@252
|
473 val es = List.take (es, length es - 1)
|
adamc@252
|
474 in
|
adamc@252
|
475 foldr (fn (e, e') =>
|
adamc@265
|
476 case (e, e') of
|
adamc@265
|
477 ((L'.EPrim (Prim.String ""), _), _) => e'
|
adamc@265
|
478 | (_, (L'.EPrim (Prim.String ""), _)) => e
|
adamc@252
|
479 | _ =>
|
adamc@252
|
480 (L'.EStrcat (e,
|
adamc@252
|
481 (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc))
|
adamc@252
|
482 e1 es
|
adamc@252
|
483 end
|
adamc@252
|
484
|
adamc@252
|
485 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
|
adamc@252
|
486
|
adamc@179
|
487 fun monoExp (env, st, fm) (all as (e, loc)) =
|
adamc@25
|
488 let
|
adamc@25
|
489 fun poly () =
|
adamc@25
|
490 (E.errorAt loc "Unsupported expression";
|
adamc@25
|
491 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
|
adamc@179
|
492 (dummyExp, fm))
|
adamc@389
|
493
|
adamc@389
|
494 fun numTy t =
|
adamc@417
|
495 (L'.TRecord [("Zero", t),
|
adamc@417
|
496 ("Neg", (L'.TFun (t, t), loc)),
|
adamc@389
|
497 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
498 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
499 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
500 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
501 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc)
|
adamc@417
|
502 fun numEx (t, zero, neg, plus, minus, times, dv, md) =
|
adamc@417
|
503 ((L'.ERecord [("Zero", (L'.EPrim zero, loc), t),
|
adamc@417
|
504 ("Neg", neg, (L'.TFun (t, t), loc)),
|
adamc@389
|
505 ("Plus", plus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
506 ("Minus", minus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
507 ("Times", times, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
508 ("Div", dv, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
|
adamc@389
|
509 ("Mod", md, (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc), fm)
|
adamc@391
|
510
|
adamc@391
|
511 fun ordTy t =
|
adamc@391
|
512 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
|
adamc@391
|
513 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))], loc)
|
adamc@391
|
514 fun ordEx (t, lt, le) =
|
adamc@391
|
515 ((L'.ERecord [("Lt", lt, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
|
adamc@391
|
516 ("Le", le, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
|
adamc@391
|
517 loc), fm)
|
adamc@25
|
518 in
|
adamc@25
|
519 case e of
|
adamc@179
|
520 L.EPrim p => ((L'.EPrim p, loc), fm)
|
adamc@179
|
521 | L.ERel n => ((L'.ERel n, loc), fm)
|
adamc@179
|
522 | L.ENamed n => ((L'.ENamed n, loc), fm)
|
adamc@193
|
523 | L.ECon (dk, pc, [], eo) =>
|
adamc@193
|
524 let
|
adamc@179
|
525 val (eo, fm) =
|
adamc@179
|
526 case eo of
|
adamc@179
|
527 NONE => (NONE, fm)
|
adamc@179
|
528 | SOME e =>
|
adamc@179
|
529 let
|
adamc@179
|
530 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
531 in
|
adamc@179
|
532 (SOME e, fm)
|
adamc@179
|
533 end
|
adamc@179
|
534 in
|
adamc@188
|
535 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
|
adamc@193
|
536 end
|
adamc@297
|
537 | L.ECon (L.Option, _, [t], NONE) =>
|
adamc@297
|
538 ((L'.ENone (monoType env t), loc), fm)
|
adamc@297
|
539 | L.ECon (L.Option, _, [t], SOME e) =>
|
adamc@297
|
540 let
|
adamc@297
|
541 val (e, fm) = monoExp (env, st, fm) e
|
adamc@297
|
542 in
|
adamc@297
|
543 ((L'.ESome (monoType env t, e), loc), fm)
|
adamc@297
|
544 end
|
adamc@193
|
545 | L.ECon _ => poly ()
|
adamc@94
|
546
|
adamc@387
|
547 | L.ECApp ((L.EFfi ("Basis", "eq"), _), t) =>
|
adamc@387
|
548 let
|
adamc@387
|
549 val t = monoType env t
|
adamc@387
|
550 val b = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@387
|
551 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
|
adamc@387
|
552 in
|
adamc@387
|
553 ((L'.EAbs ("f", dom, dom,
|
adamc@387
|
554 (L'.ERel 0, loc)), loc), fm)
|
adamc@387
|
555 end
|
adamc@387
|
556 | L.ECApp ((L.EFfi ("Basis", "ne"), _), t) =>
|
adamc@387
|
557 let
|
adamc@387
|
558 val t = monoType env t
|
adamc@387
|
559 val b = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@387
|
560 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
|
adamc@387
|
561 in
|
adamc@387
|
562 ((L'.EAbs ("f", dom, dom,
|
adamc@387
|
563 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
|
adamc@387
|
564 (L'.EAbs ("y", t, b,
|
adamc@387
|
565 (L'.EUnop ("!", (L'.EApp ((L'.EApp ((L'.ERel 2, loc),
|
adamc@387
|
566 (L'.ERel 1, loc)), loc),
|
adamc@387
|
567 (L'.ERel 0, loc)), loc)), loc)),
|
adamc@387
|
568 loc)),
|
adamc@387
|
569 loc)),
|
adamc@387
|
570 loc), fm)
|
adamc@387
|
571 end
|
adamc@387
|
572 | L.EFfi ("Basis", "eq_int") =>
|
adamc@387
|
573 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
|
adamc@387
|
574 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@387
|
575 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
|
adamc@387
|
576 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@387
|
577 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
|
adamc@387
|
578 fm)
|
adamc@394
|
579 | L.EFfi ("Basis", "eq_float") =>
|
adamc@394
|
580 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
|
adamc@394
|
581 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@394
|
582 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
|
adamc@394
|
583 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@394
|
584 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
|
adamc@394
|
585 fm)
|
adamc@388
|
586 | L.EFfi ("Basis", "eq_bool") =>
|
adamc@388
|
587 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
|
adamc@388
|
588 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@388
|
589 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
|
adamc@388
|
590 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@388
|
591 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
|
adamc@388
|
592 fm)
|
adamc@388
|
593 | L.EFfi ("Basis", "eq_string") =>
|
adamc@388
|
594 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
|
adamc@388
|
595 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@388
|
596 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
|
adamc@388
|
597 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@388
|
598 (L'.EBinop ("!strcmp", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
|
adamc@388
|
599 fm)
|
adamc@422
|
600 | L.ECApp ((L.EFfi ("Basis", "mkEq"), _), t) =>
|
adamc@422
|
601 let
|
adamc@422
|
602 val t = monoType env t
|
adamc@422
|
603 val b = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@422
|
604 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
|
adamc@422
|
605 in
|
adamc@422
|
606 ((L'.EAbs ("f", dom, dom,
|
adamc@422
|
607 (L'.ERel 0, loc)), loc), fm)
|
adamc@422
|
608 end
|
adamc@387
|
609
|
adamc@417
|
610 | L.ECApp ((L.EFfi ("Basis", "zero"), _), t) =>
|
adamc@417
|
611 let
|
adamc@417
|
612 val t = monoType env t
|
adamc@417
|
613 in
|
adamc@417
|
614 ((L'.EAbs ("r", numTy t, t,
|
adamc@417
|
615 (L'.EField ((L'.ERel 0, loc), "Zero"), loc)), loc), fm)
|
adamc@417
|
616 end
|
adamc@389
|
617 | L.ECApp ((L.EFfi ("Basis", "neg"), _), t) =>
|
adamc@389
|
618 let
|
adamc@389
|
619 val t = monoType env t
|
adamc@389
|
620 in
|
adamc@389
|
621 ((L'.EAbs ("r", numTy t, (L'.TFun (t, t), loc),
|
adamc@389
|
622 (L'.EField ((L'.ERel 0, loc), "Neg"), loc)), loc), fm)
|
adamc@389
|
623 end
|
adamc@389
|
624 | L.ECApp ((L.EFfi ("Basis", "plus"), _), t) =>
|
adamc@389
|
625 let
|
adamc@389
|
626 val t = monoType env t
|
adamc@389
|
627 in
|
adamc@389
|
628 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
|
adamc@389
|
629 (L'.EField ((L'.ERel 0, loc), "Plus"), loc)), loc), fm)
|
adamc@389
|
630 end
|
adamc@389
|
631 | L.ECApp ((L.EFfi ("Basis", "minus"), _), t) =>
|
adamc@389
|
632 let
|
adamc@389
|
633 val t = monoType env t
|
adamc@389
|
634 in
|
adamc@389
|
635 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
|
adamc@389
|
636 (L'.EField ((L'.ERel 0, loc), "Minus"), loc)), loc), fm)
|
adamc@389
|
637 end
|
adamc@389
|
638 | L.ECApp ((L.EFfi ("Basis", "times"), _), t) =>
|
adamc@389
|
639 let
|
adamc@389
|
640 val t = monoType env t
|
adamc@389
|
641 in
|
adamc@389
|
642 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
|
adamc@389
|
643 (L'.EField ((L'.ERel 0, loc), "Times"), loc)), loc), fm)
|
adamc@389
|
644 end
|
adamc@389
|
645 | L.ECApp ((L.EFfi ("Basis", "div"), _), t) =>
|
adamc@389
|
646 let
|
adamc@389
|
647 val t = monoType env t
|
adamc@389
|
648 in
|
adamc@389
|
649 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
|
adamc@389
|
650 (L'.EField ((L'.ERel 0, loc), "Div"), loc)), loc), fm)
|
adamc@389
|
651 end
|
adamc@389
|
652 | L.ECApp ((L.EFfi ("Basis", "mod"), _), t) =>
|
adamc@389
|
653 let
|
adamc@389
|
654 val t = monoType env t
|
adamc@389
|
655 in
|
adamc@389
|
656 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
|
adamc@389
|
657 (L'.EField ((L'.ERel 0, loc), "Mod"), loc)), loc), fm)
|
adamc@389
|
658 end
|
adamc@389
|
659 | L.EFfi ("Basis", "num_int") =>
|
adamc@389
|
660 let
|
adamc@389
|
661 fun intBin s =
|
adamc@389
|
662 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
|
adamc@389
|
663 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "int"), loc)), loc),
|
adamc@389
|
664 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
|
adamc@389
|
665 (L'.TFfi ("Basis", "int"), loc),
|
adamc@389
|
666 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
|
adamc@389
|
667 in
|
adamc@389
|
668 numEx ((L'.TFfi ("Basis", "int"), loc),
|
adamc@417
|
669 Prim.Int (Int64.fromInt 0),
|
adamc@389
|
670 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
|
adamc@389
|
671 (L'.TFfi ("Basis", "int"), loc),
|
adamc@389
|
672 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
|
adamc@389
|
673 intBin "+",
|
adamc@389
|
674 intBin "-",
|
adamc@389
|
675 intBin "*",
|
adamc@389
|
676 intBin "/",
|
adamc@389
|
677 intBin "%")
|
adamc@389
|
678 end
|
adamc@390
|
679 | L.EFfi ("Basis", "num_float") =>
|
adamc@390
|
680 let
|
adamc@390
|
681 fun floatBin s =
|
adamc@390
|
682 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
|
adamc@390
|
683 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "float"), loc)), loc),
|
adamc@390
|
684 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
|
adamc@390
|
685 (L'.TFfi ("Basis", "float"), loc),
|
adamc@390
|
686 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
|
adamc@390
|
687 in
|
adamc@390
|
688 numEx ((L'.TFfi ("Basis", "float"), loc),
|
adamc@417
|
689 Prim.Float 0.0,
|
adamc@390
|
690 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
|
adamc@390
|
691 (L'.TFfi ("Basis", "float"), loc),
|
adamc@390
|
692 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
|
adamc@390
|
693 floatBin "+",
|
adamc@390
|
694 floatBin "-",
|
adamc@390
|
695 floatBin "*",
|
adamc@390
|
696 floatBin "/",
|
adamc@390
|
697 floatBin "fmod")
|
adamc@390
|
698 end
|
adamc@391
|
699
|
adamc@391
|
700 | L.ECApp ((L.EFfi ("Basis", "lt"), _), t) =>
|
adamc@391
|
701 let
|
adamc@391
|
702 val t = monoType env t
|
adamc@391
|
703 in
|
adamc@391
|
704 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
|
adamc@391
|
705 (L'.EField ((L'.ERel 0, loc), "Lt"), loc)), loc), fm)
|
adamc@391
|
706 end
|
adamc@391
|
707 | L.ECApp ((L.EFfi ("Basis", "le"), _), t) =>
|
adamc@391
|
708 let
|
adamc@391
|
709 val t = monoType env t
|
adamc@391
|
710 in
|
adamc@391
|
711 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
|
adamc@391
|
712 (L'.EField ((L'.ERel 0, loc), "Le"), loc)), loc), fm)
|
adamc@391
|
713 end
|
adamc@392
|
714 | L.ECApp ((L.EFfi ("Basis", "gt"), _), t) =>
|
adamc@392
|
715 let
|
adamc@392
|
716 val t = monoType env t
|
adamc@392
|
717 val b = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@392
|
718 in
|
adamc@392
|
719 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
|
adamc@392
|
720 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
|
adamc@392
|
721 (L'.EAbs ("y", t, b,
|
adamc@392
|
722 (L'.EUnop ("!",
|
adamc@392
|
723 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
|
adamc@392
|
724 "Le"), loc),
|
adamc@392
|
725 (L'.ERel 1, loc)), loc),
|
adamc@392
|
726 (L'.ERel 0, loc)), loc)), loc)), loc)),
|
adamc@392
|
727 loc)),
|
adamc@392
|
728 loc), fm)
|
adamc@392
|
729 end
|
adamc@392
|
730 | L.ECApp ((L.EFfi ("Basis", "ge"), _), t) =>
|
adamc@392
|
731 let
|
adamc@392
|
732 val t = monoType env t
|
adamc@392
|
733 val b = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@392
|
734 in
|
adamc@392
|
735 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
|
adamc@392
|
736 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
|
adamc@392
|
737 (L'.EAbs ("y", t, b,
|
adamc@392
|
738 (L'.EUnop ("!",
|
adamc@392
|
739 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
|
adamc@392
|
740 "Lt"), loc),
|
adamc@392
|
741 (L'.ERel 1, loc)), loc),
|
adamc@392
|
742 (L'.ERel 0, loc)), loc)), loc)), loc)),
|
adamc@392
|
743 loc)),
|
adamc@392
|
744 loc), fm)
|
adamc@392
|
745 end
|
adamc@391
|
746 | L.EFfi ("Basis", "ord_int") =>
|
adamc@391
|
747 let
|
adamc@391
|
748 fun intBin s =
|
adamc@391
|
749 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
|
adamc@391
|
750 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@391
|
751 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
|
adamc@391
|
752 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@391
|
753 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
|
adamc@391
|
754 in
|
adamc@391
|
755 ordEx ((L'.TFfi ("Basis", "int"), loc),
|
adamc@391
|
756 intBin "<",
|
adamc@391
|
757 intBin "<=")
|
adamc@391
|
758 end
|
adamc@394
|
759 | L.EFfi ("Basis", "ord_float") =>
|
adamc@394
|
760 let
|
adamc@394
|
761 fun floatBin s =
|
adamc@394
|
762 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
|
adamc@394
|
763 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@394
|
764 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
|
adamc@394
|
765 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@394
|
766 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
|
adamc@394
|
767 in
|
adamc@394
|
768 ordEx ((L'.TFfi ("Basis", "float"), loc),
|
adamc@394
|
769 floatBin "<",
|
adamc@394
|
770 floatBin "<=")
|
adamc@394
|
771 end
|
adamc@394
|
772 | L.EFfi ("Basis", "ord_bool") =>
|
adamc@394
|
773 let
|
adamc@394
|
774 fun boolBin s =
|
adamc@394
|
775 (L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
|
adamc@394
|
776 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@394
|
777 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
|
adamc@394
|
778 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@394
|
779 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
|
adamc@394
|
780 in
|
adamc@394
|
781 ordEx ((L'.TFfi ("Basis", "bool"), loc),
|
adamc@394
|
782 boolBin "<",
|
adamc@394
|
783 boolBin "<=")
|
adamc@394
|
784 end
|
adamc@395
|
785 | L.EFfi ("Basis", "ord_string") =>
|
adamc@395
|
786 let
|
adamc@395
|
787 fun boolBin s =
|
adamc@395
|
788 (L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
|
adamc@395
|
789 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
|
adamc@395
|
790 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
|
adamc@395
|
791 (L'.TFfi ("Basis", "bool"), loc),
|
adamc@395
|
792 (L'.EBinop (s,
|
adamc@395
|
793 (L'.EBinop ("strcmp",
|
adamc@395
|
794 (L'.ERel 1, loc),
|
adamc@395
|
795 (L'.ERel 0, loc)), loc),
|
adamc@395
|
796 (L'.EPrim (Prim.Int (Int64.fromInt 0)), loc)), loc)), loc)), loc)
|
adamc@395
|
797 in
|
adamc@395
|
798 ordEx ((L'.TFfi ("Basis", "string"), loc),
|
adamc@395
|
799 boolBin "<",
|
adamc@395
|
800 boolBin "<=")
|
adamc@395
|
801 end
|
adamc@389
|
802
|
adamc@286
|
803 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
|
adamc@286
|
804 let
|
adamc@286
|
805 val t = monoType env t
|
adamc@286
|
806 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@286
|
807 in
|
adamc@286
|
808 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
|
adamc@286
|
809 (L'.ERel 0, loc)), loc), fm)
|
adamc@286
|
810 end
|
adamc@286
|
811 | L.EFfi ("Basis", "show_int") =>
|
adamc@286
|
812 ((L'.EFfi ("Basis", "intToString"), loc), fm)
|
adamc@286
|
813 | L.EFfi ("Basis", "show_float") =>
|
adamc@286
|
814 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
|
adamc@286
|
815 | L.EFfi ("Basis", "show_string") =>
|
adamc@286
|
816 let
|
adamc@286
|
817 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@286
|
818 in
|
adamc@286
|
819 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
|
adamc@286
|
820 end
|
adamc@286
|
821 | L.EFfi ("Basis", "show_bool") =>
|
adamc@286
|
822 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
|
adamc@286
|
823
|
adamc@290
|
824 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
|
adamc@290
|
825 let
|
adamc@290
|
826 val t = monoType env t
|
adamc@290
|
827 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@290
|
828 in
|
adamc@292
|
829 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
|
adamc@292
|
830 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
|
adamc@292
|
831 end
|
adamc@292
|
832 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
|
adamc@292
|
833 let
|
adamc@292
|
834 val t = monoType env t
|
adamc@292
|
835 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@292
|
836 in
|
adamc@292
|
837 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
|
adamc@292
|
838 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
|
adamc@290
|
839 end
|
adamc@290
|
840 | L.EFfi ("Basis", "read_int") =>
|
adamc@292
|
841 let
|
adamc@292
|
842 val t = (L'.TFfi ("Basis", "int"), loc)
|
adamc@292
|
843 in
|
adamc@292
|
844 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
|
adamc@292
|
845 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
|
adamc@292
|
846 loc),
|
adamc@292
|
847 fm)
|
adamc@292
|
848 end
|
adamc@290
|
849 | L.EFfi ("Basis", "read_float") =>
|
adamc@292
|
850 let
|
adamc@292
|
851 val t = (L'.TFfi ("Basis", "float"), loc)
|
adamc@292
|
852 in
|
adamc@292
|
853 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
|
adamc@292
|
854 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
|
adamc@292
|
855 loc),
|
adamc@292
|
856 fm)
|
adamc@292
|
857 end
|
adamc@290
|
858 | L.EFfi ("Basis", "read_string") =>
|
adamc@290
|
859 let
|
adamc@290
|
860 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@290
|
861 in
|
adamc@292
|
862 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
|
adamc@292
|
863 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
|
adamc@292
|
864 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
|
adamc@292
|
865 fm)
|
adamc@290
|
866 end
|
adamc@290
|
867 | L.EFfi ("Basis", "read_bool") =>
|
adamc@292
|
868 let
|
adamc@292
|
869 val t = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@292
|
870 in
|
adamc@292
|
871 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
|
adamc@292
|
872 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
|
adamc@292
|
873 loc),
|
adamc@292
|
874 fm)
|
adamc@292
|
875 end
|
adamc@290
|
876
|
adamc@251
|
877 | L.ECApp ((L.EFfi ("Basis", "return"), _), t) =>
|
adamc@252
|
878 let
|
adamc@252
|
879 val t = monoType env t
|
adamc@252
|
880 in
|
adamc@252
|
881 ((L'.EAbs ("x", t,
|
adamc@252
|
882 (L'.TFun ((L'.TRecord [], loc), t), loc),
|
adamc@252
|
883 (L'.EAbs ("_", (L'.TRecord [], loc), t,
|
adamc@252
|
884 (L'.ERel 1, loc)), loc)), loc), fm)
|
adamc@252
|
885 end
|
adamc@251
|
886 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), t1), _), t2) =>
|
adamc@251
|
887 let
|
adamc@251
|
888 val t1 = monoType env t1
|
adamc@251
|
889 val t2 = monoType env t2
|
adamc@251
|
890 val un = (L'.TRecord [], loc)
|
adamc@252
|
891 val mt1 = (L'.TFun (un, t1), loc)
|
adamc@252
|
892 val mt2 = (L'.TFun (un, t2), loc)
|
adamc@251
|
893 in
|
adamc@252
|
894 ((L'.EAbs ("m1", mt1, (L'.TFun (mt1, (L'.TFun (mt2, (L'.TFun (un, un), loc)), loc)), loc),
|
adamc@252
|
895 (L'.EAbs ("m2", mt2, (L'.TFun (un, un), loc),
|
adamc@252
|
896 (L'.EAbs ("_", un, un,
|
adamc@252
|
897 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
|
adamc@252
|
898 (L'.ERecord [], loc)), loc),
|
adamc@252
|
899 (L'.EApp (
|
adamc@252
|
900 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
|
adamc@252
|
901 (L'.ERecord [], loc)),
|
adamc@252
|
902 loc)), loc)), loc)), loc)), loc),
|
adamc@251
|
903 fm)
|
adamc@251
|
904 end
|
adamc@251
|
905
|
adamc@307
|
906 | L.EFfiApp ("Basis", "dml", [e]) =>
|
adamc@307
|
907 let
|
adamc@307
|
908 val (e, fm) = monoExp (env, st, fm) e
|
adamc@307
|
909 val un = (L'.TRecord [], loc)
|
adamc@307
|
910 in
|
adamc@307
|
911 ((L'.EAbs ("_", un, un,
|
adamc@307
|
912 (L'.EDml (liftExpInExp 0 e), loc)), loc),
|
adamc@307
|
913 fm)
|
adamc@307
|
914 end
|
adamc@308
|
915
|
adamc@307
|
916 | L.ECApp ((L.EFfi ("Basis", "insert"), _), fields) =>
|
adamc@307
|
917 (case monoType env (L.TRecord fields, loc) of
|
adamc@307
|
918 (L'.TRecord fields, _) =>
|
adamc@307
|
919 let
|
adamc@307
|
920 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@307
|
921 val fields = map (fn (x, _) => (x, s)) fields
|
adamc@307
|
922 val rt = (L'.TRecord fields, loc)
|
adamc@307
|
923 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@307
|
924 in
|
adamc@307
|
925 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
|
adamc@307
|
926 (L'.EAbs ("fs", rt, s,
|
adamc@307
|
927 strcat loc [sc "INSERT INTO ",
|
adamc@307
|
928 (L'.ERel 1, loc),
|
adamc@307
|
929 sc " (",
|
adamc@311
|
930 strcatComma loc (map (fn (x, _) => sc ("uw_" ^ x)) fields),
|
adamc@307
|
931 sc ") VALUES (",
|
adamc@307
|
932 strcatComma loc (map (fn (x, _) =>
|
adamc@307
|
933 (L'.EField ((L'.ERel 0, loc),
|
adamc@307
|
934 x), loc)) fields),
|
adamc@307
|
935 sc ")"]), loc)), loc),
|
adamc@307
|
936 fm)
|
adamc@307
|
937 end
|
adamc@307
|
938 | _ => poly ())
|
adamc@307
|
939
|
adamc@403
|
940 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), changed) =>
|
adamc@308
|
941 (case monoType env (L.TRecord changed, loc) of
|
adamc@308
|
942 (L'.TRecord changed, _) =>
|
adamc@308
|
943 let
|
adamc@308
|
944 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@308
|
945 val changed = map (fn (x, _) => (x, s)) changed
|
adamc@308
|
946 val rt = (L'.TRecord changed, loc)
|
adamc@308
|
947 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@308
|
948 in
|
adamc@308
|
949 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@308
|
950 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
|
adamc@308
|
951 (L'.EAbs ("e", s, s,
|
adamc@308
|
952 strcat loc [sc "UPDATE ",
|
adamc@308
|
953 (L'.ERel 1, loc),
|
adamc@308
|
954 sc " AS T SET ",
|
adamc@308
|
955 strcatComma loc (map (fn (x, _) =>
|
adamc@311
|
956 strcat loc [sc ("uw_" ^ x
|
adamc@308
|
957 ^ " = "),
|
adamc@308
|
958 (L'.EField
|
adamc@308
|
959 ((L'.ERel 2,
|
adamc@308
|
960 loc),
|
adamc@308
|
961 x), loc)])
|
adamc@308
|
962 changed),
|
adamc@308
|
963 sc " WHERE ",
|
adamc@308
|
964 (L'.ERel 0, loc)]), loc)), loc)), loc),
|
adamc@308
|
965 fm)
|
adamc@308
|
966 end
|
adamc@308
|
967 | _ => poly ())
|
adamc@308
|
968
|
adamc@309
|
969 | L.ECApp ((L.EFfi ("Basis", "delete"), _), _) =>
|
adamc@309
|
970 let
|
adamc@309
|
971 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@309
|
972 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@309
|
973 in
|
adamc@309
|
974 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
|
adamc@309
|
975 (L'.EAbs ("e", s, s,
|
adamc@309
|
976 strcat loc [sc "DELETE FROM ",
|
adamc@309
|
977 (L'.ERel 1, loc),
|
adamc@309
|
978 sc " AS T WHERE ",
|
adamc@309
|
979 (L'.ERel 0, loc)]), loc)), loc),
|
adamc@309
|
980 fm)
|
adamc@309
|
981 end
|
adamc@309
|
982
|
adamc@252
|
983 | L.ECApp (
|
adamc@252
|
984 (L.ECApp (
|
adamc@252
|
985 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
|
adamc@252
|
986 exps), _),
|
adamc@252
|
987 state) =>
|
adamc@252
|
988 (case monoType env (L.TRecord exps, loc) of
|
adamc@252
|
989 (L'.TRecord exps, _) =>
|
adamc@252
|
990 let
|
adamc@252
|
991 val tables = map (fn ((L.CName x, _), xts) =>
|
adamc@252
|
992 (case monoType env (L.TRecord xts, loc) of
|
adamc@252
|
993 (L'.TRecord xts, _) => SOME (x, xts)
|
adamc@252
|
994 | _ => NONE)
|
adamc@252
|
995 | _ => NONE) tables
|
adamc@252
|
996 in
|
adamc@252
|
997 if List.exists (fn x => x = NONE) tables then
|
adamc@252
|
998 poly ()
|
adamc@252
|
999 else
|
adamc@252
|
1000 let
|
adamc@252
|
1001 val tables = List.mapPartial (fn x => x) tables
|
adamc@252
|
1002 val state = monoType env state
|
adamc@252
|
1003 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
1004 val un = (L'.TRecord [], loc)
|
adamc@252
|
1005
|
adamc@252
|
1006 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
|
adamc@252
|
1007 val ft = (L'.TFun ((L'.TRecord rt, loc),
|
adamc@252
|
1008 (L'.TFun (state,
|
adamc@252
|
1009 (L'.TFun (un, state), loc)),
|
adamc@252
|
1010 loc)), loc)
|
adamc@252
|
1011
|
adamc@267
|
1012 val body' = (L'.EApp (
|
adamc@267
|
1013 (L'.EApp (
|
adamc@267
|
1014 (L'.EApp ((L'.ERel 4, loc),
|
adamc@267
|
1015 (L'.ERel 1, loc)), loc),
|
adamc@267
|
1016 (L'.ERel 0, loc)), loc),
|
adamc@267
|
1017 (L'.ERecord [], loc)), loc)
|
adamc@252
|
1018
|
adamc@252
|
1019 val body = (L'.EQuery {exps = exps,
|
adamc@252
|
1020 tables = tables,
|
adamc@252
|
1021 state = state,
|
adamc@252
|
1022 query = (L'.ERel 3, loc),
|
adamc@252
|
1023 body = body',
|
adamc@252
|
1024 initial = (L'.ERel 1, loc)},
|
adamc@252
|
1025 loc)
|
adamc@252
|
1026 in
|
adamc@252
|
1027 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
|
adamc@252
|
1028 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
|
adamc@252
|
1029 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
|
adamc@252
|
1030 (L'.EAbs ("_", un, state,
|
adamc@252
|
1031 body), loc)), loc)), loc)), loc), fm)
|
adamc@252
|
1032 end
|
adamc@252
|
1033 end
|
adamc@252
|
1034 | _ => poly ())
|
adamc@252
|
1035
|
adamc@252
|
1036 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
|
adamc@252
|
1037 let
|
adamc@252
|
1038 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@252
|
1039 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
1040 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
|
adamc@252
|
1041 in
|
adamc@252
|
1042 ((L'.EAbs ("r",
|
adamc@252
|
1043 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
|
adamc@252
|
1044 s,
|
adamc@252
|
1045 strcat loc [gf "Rows",
|
adamc@261
|
1046 (L'.ECase (gf "OrderBy",
|
adamc@261
|
1047 [((L'.PPrim (Prim.String ""), loc), sc ""),
|
adamc@261
|
1048 ((L'.PWild, loc),
|
adamc@261
|
1049 strcat loc [sc " ORDER BY ",
|
adamc@261
|
1050 gf "OrderBy"])],
|
adamc@261
|
1051 {disc = s, result = s}), loc),
|
adamc@252
|
1052 gf "Limit",
|
adamc@252
|
1053 gf "Offset"]), loc), fm)
|
adamc@252
|
1054 end
|
adamc@252
|
1055
|
adamc@252
|
1056 | L.ECApp (
|
adamc@252
|
1057 (L.ECApp (
|
adamc@252
|
1058 (L.ECApp (
|
adamc@252
|
1059 (L.ECApp (
|
adamc@252
|
1060 (L.EFfi ("Basis", "sql_query1"), _),
|
adamc@252
|
1061 (L.CRecord (_, tables), _)), _),
|
adamc@252
|
1062 (L.CRecord (_, grouped), _)), _),
|
adamc@252
|
1063 (L.CRecord (_, stables), _)), _),
|
adamc@252
|
1064 sexps) =>
|
adamc@252
|
1065 let
|
adamc@252
|
1066 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@252
|
1067 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
1068 val un = (L'.TRecord [], loc)
|
adamc@252
|
1069 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
|
adamc@252
|
1070
|
adamc@252
|
1071 fun doTables tables =
|
adamc@252
|
1072 let
|
adamc@252
|
1073 val tables = map (fn ((L.CName x, _), xts) =>
|
adamc@252
|
1074 (case monoType env (L.TRecord xts, loc) of
|
adamc@252
|
1075 (L'.TRecord xts, _) => SOME (x, xts)
|
adamc@252
|
1076 | _ => NONE)
|
adamc@252
|
1077 | _ => NONE) tables
|
adamc@252
|
1078 in
|
adamc@252
|
1079 if List.exists (fn x => x = NONE) tables then
|
adamc@252
|
1080 NONE
|
adamc@252
|
1081 else
|
adamc@260
|
1082 let
|
adamc@260
|
1083 val tables = List.mapPartial (fn x => x) tables
|
adamc@260
|
1084 val tables = ListMergeSort.sort
|
adamc@260
|
1085 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
|
adamc@260
|
1086 tables
|
adamc@260
|
1087 val tables = map (fn (x, xts) =>
|
adamc@260
|
1088 (x, ListMergeSort.sort
|
adamc@260
|
1089 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
|
adamc@260
|
1090 xts)) tables
|
adamc@260
|
1091 in
|
adamc@260
|
1092 SOME tables
|
adamc@260
|
1093 end
|
adamc@252
|
1094 end
|
adamc@252
|
1095 in
|
adamc@252
|
1096 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
|
adamc@252
|
1097 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
|
adamc@252
|
1098 ((L'.EAbs ("r",
|
adamc@252
|
1099 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
|
adamc@252
|
1100 ("Where", s),
|
adamc@252
|
1101 ("GroupBy", un),
|
adamc@252
|
1102 ("Having", s),
|
adamc@252
|
1103 ("SelectFields", un),
|
adamc@252
|
1104 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
|
adamc@252
|
1105 loc),
|
adamc@252
|
1106 s,
|
adamc@252
|
1107 strcat loc [sc "SELECT ",
|
adamc@261
|
1108 strcatComma loc (map (fn (x, t) =>
|
adamc@261
|
1109 strcat loc [
|
adamc@261
|
1110 (L'.EField (gf "SelectExps", x), loc),
|
adamc@261
|
1111 sc (" AS _" ^ x)
|
adamc@265
|
1112 ]) sexps
|
adamc@265
|
1113 @ map (fn (x, xts) =>
|
adamc@265
|
1114 strcatComma loc
|
adamc@265
|
1115 (map (fn (x', _) =>
|
adamc@311
|
1116 sc (x ^ ".uw_" ^ x'))
|
adamc@265
|
1117 xts)) stables),
|
adamc@252
|
1118 sc " FROM ",
|
adamc@252
|
1119 strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
|
adamc@253
|
1120 sc (" AS " ^ x)]) tables),
|
adamc@258
|
1121 (L'.ECase (gf "Where",
|
adamc@258
|
1122 [((L'.PPrim (Prim.String "TRUE"), loc),
|
adamc@258
|
1123 sc ""),
|
adamc@258
|
1124 ((L'.PWild, loc),
|
adamc@258
|
1125 strcat loc [sc " WHERE ", gf "Where"])],
|
adamc@258
|
1126 {disc = s,
|
adamc@258
|
1127 result = s}), loc),
|
adamc@258
|
1128
|
adamc@255
|
1129 if List.all (fn (x, xts) =>
|
adamc@255
|
1130 case List.find (fn (x', _) => x' = x) grouped of
|
adamc@255
|
1131 NONE => List.null xts
|
adamc@255
|
1132 | SOME (_, xts') =>
|
adamc@255
|
1133 List.all (fn (x, _) =>
|
adamc@255
|
1134 List.exists (fn (x', _) => x' = x)
|
adamc@255
|
1135 xts') xts) tables then
|
adamc@255
|
1136 sc ""
|
adamc@255
|
1137 else
|
adamc@255
|
1138 strcat loc [
|
adamc@255
|
1139 sc " GROUP BY ",
|
adamc@255
|
1140 strcatComma loc (map (fn (x, xts) =>
|
adamc@255
|
1141 strcatComma loc
|
adamc@255
|
1142 (map (fn (x', _) =>
|
adamc@311
|
1143 sc (x ^ ".uw_" ^ x'))
|
adamc@255
|
1144 xts)) grouped)
|
adamc@259
|
1145 ],
|
adamc@259
|
1146
|
adamc@259
|
1147 (L'.ECase (gf "Having",
|
adamc@259
|
1148 [((L'.PPrim (Prim.String "TRUE"), loc),
|
adamc@259
|
1149 sc ""),
|
adamc@259
|
1150 ((L'.PWild, loc),
|
adamc@259
|
1151 strcat loc [sc " HAVING ", gf "Having"])],
|
adamc@259
|
1152 {disc = s,
|
adamc@259
|
1153 result = s}), loc)
|
adamc@252
|
1154 ]), loc),
|
adamc@252
|
1155 fm)
|
adamc@252
|
1156 | _ => poly ()
|
adamc@252
|
1157 end
|
adamc@252
|
1158
|
adamc@252
|
1159 | L.ECApp (
|
adamc@252
|
1160 (L.ECApp (
|
adamc@252
|
1161 (L.ECApp (
|
adamc@252
|
1162 (L.ECApp (
|
adamc@252
|
1163 (L.EFfi ("Basis", "sql_inject"), _),
|
adamc@252
|
1164 _), _),
|
adamc@252
|
1165 _), _),
|
adamc@252
|
1166 _), _),
|
adamc@252
|
1167 t) =>
|
adamc@252
|
1168 let
|
adamc@252
|
1169 val t = monoType env t
|
adamc@252
|
1170 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
1171 in
|
adamc@252
|
1172 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
|
adamc@252
|
1173 (L'.ERel 0, loc)), loc), fm)
|
adamc@252
|
1174 end
|
adamc@252
|
1175
|
adamc@253
|
1176 | L.EFfi ("Basis", "sql_int") =>
|
adamc@253
|
1177 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
1178 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
1179 fm)
|
adamc@253
|
1180 | L.EFfi ("Basis", "sql_float") =>
|
adamc@253
|
1181 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
1182 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
1183 fm)
|
adamc@253
|
1184 | L.EFfi ("Basis", "sql_bool") =>
|
adamc@253
|
1185 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
1186 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
1187 fm)
|
adamc@253
|
1188 | L.EFfi ("Basis", "sql_string") =>
|
adamc@253
|
1189 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
1190 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
1191 fm)
|
adamc@253
|
1192
|
adamc@252
|
1193 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
|
adamc@252
|
1194 ((L'.ERecord [], loc), fm)
|
adamc@252
|
1195 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
|
adamc@252
|
1196 ((L'.ERecord [], loc), fm)
|
adamc@252
|
1197
|
adamc@252
|
1198 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
|
adamc@252
|
1199 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@261
|
1200 | L.ECApp (
|
adamc@261
|
1201 (L.ECApp (
|
adamc@261
|
1202 (L.ECApp (
|
adamc@261
|
1203 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
|
adamc@261
|
1204 _), _),
|
adamc@261
|
1205 _), _),
|
adamc@261
|
1206 _) =>
|
adamc@261
|
1207 let
|
adamc@261
|
1208 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@261
|
1209 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@261
|
1210 in
|
adamc@268
|
1211 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@268
|
1212 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
|
adamc@268
|
1213 (L'.EAbs ("e2", s, s,
|
adamc@268
|
1214 (L'.ECase ((L'.ERel 0, loc),
|
adamc@268
|
1215 [((L'.PPrim (Prim.String ""), loc),
|
adamc@268
|
1216 strcat loc [(L'.ERel 2, loc),
|
adamc@268
|
1217 (L'.ERel 1, loc)]),
|
adamc@268
|
1218 ((L'.PWild, loc),
|
adamc@268
|
1219 strcat loc [(L'.ERel 2, loc),
|
adamc@268
|
1220 (L'.ERel 1, loc),
|
adamc@268
|
1221 sc ", ",
|
adamc@268
|
1222 (L'.ERel 0, loc)])],
|
adamc@268
|
1223 {disc = s, result = s}), loc)), loc)), loc)), loc),
|
adamc@261
|
1224 fm)
|
adamc@261
|
1225 end
|
adamc@252
|
1226
|
adamc@252
|
1227 | L.EFfi ("Basis", "sql_no_limit") =>
|
adamc@252
|
1228 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@262
|
1229 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
|
adamc@262
|
1230 let
|
adamc@262
|
1231 val (e, fm) = monoExp (env, st, fm) e
|
adamc@262
|
1232 in
|
adamc@262
|
1233 (strcat loc [
|
adamc@262
|
1234 (L'.EPrim (Prim.String " LIMIT "), loc),
|
adamc@262
|
1235 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
|
adamc@262
|
1236 ],
|
adamc@262
|
1237 fm)
|
adamc@262
|
1238 end
|
adamc@262
|
1239
|
adamc@252
|
1240 | L.EFfi ("Basis", "sql_no_offset") =>
|
adamc@252
|
1241 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@263
|
1242 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
|
adamc@263
|
1243 let
|
adamc@263
|
1244 val (e, fm) = monoExp (env, st, fm) e
|
adamc@263
|
1245 in
|
adamc@263
|
1246 (strcat loc [
|
adamc@263
|
1247 (L'.EPrim (Prim.String " OFFSET "), loc),
|
adamc@263
|
1248 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
|
adamc@263
|
1249 ],
|
adamc@263
|
1250 fm)
|
adamc@263
|
1251 end
|
adamc@253
|
1252
|
adamc@253
|
1253 | L.EFfi ("Basis", "sql_eq") =>
|
adamc@253
|
1254 ((L'.EPrim (Prim.String "="), loc), fm)
|
adamc@253
|
1255 | L.EFfi ("Basis", "sql_ne") =>
|
adamc@253
|
1256 ((L'.EPrim (Prim.String "<>"), loc), fm)
|
adamc@253
|
1257 | L.EFfi ("Basis", "sql_lt") =>
|
adamc@253
|
1258 ((L'.EPrim (Prim.String "<"), loc), fm)
|
adamc@253
|
1259 | L.EFfi ("Basis", "sql_le") =>
|
adamc@253
|
1260 ((L'.EPrim (Prim.String "<="), loc), fm)
|
adamc@253
|
1261 | L.EFfi ("Basis", "sql_gt") =>
|
adamc@253
|
1262 ((L'.EPrim (Prim.String ">"), loc), fm)
|
adamc@253
|
1263 | L.EFfi ("Basis", "sql_ge") =>
|
adamc@253
|
1264 ((L'.EPrim (Prim.String ">="), loc), fm)
|
adamc@253
|
1265
|
adamc@253
|
1266 | L.ECApp (
|
adamc@253
|
1267 (L.ECApp (
|
adamc@253
|
1268 (L.ECApp (
|
adamc@253
|
1269 (L.ECApp (
|
adamc@254
|
1270 (L.ECApp (
|
adamc@264
|
1271 (L.EFfi ("Basis", "sql_unary"), _),
|
adamc@264
|
1272 _), _),
|
adamc@264
|
1273 _), _),
|
adamc@264
|
1274 _), _),
|
adamc@264
|
1275 _), _),
|
adamc@264
|
1276 _) =>
|
adamc@264
|
1277 let
|
adamc@264
|
1278 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@264
|
1279 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@264
|
1280 in
|
adamc@264
|
1281 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@264
|
1282 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@264
|
1283 strcat loc [sc "(",
|
adamc@264
|
1284 (L'.ERel 1, loc),
|
adamc@264
|
1285 sc " ",
|
adamc@264
|
1286 (L'.ERel 0, loc),
|
adamc@264
|
1287 sc ")"]), loc)), loc),
|
adamc@264
|
1288 fm)
|
adamc@264
|
1289 end
|
adamc@264
|
1290 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
|
adamc@264
|
1291
|
adamc@264
|
1292 | L.ECApp (
|
adamc@264
|
1293 (L.ECApp (
|
adamc@264
|
1294 (L.ECApp (
|
adamc@264
|
1295 (L.ECApp (
|
adamc@264
|
1296 (L.ECApp (
|
adamc@254
|
1297 (L.ECApp (
|
adamc@254
|
1298 (L.EFfi ("Basis", "sql_binary"), _),
|
adamc@254
|
1299 _), _),
|
adamc@254
|
1300 _), _),
|
adamc@254
|
1301 _), _),
|
adamc@254
|
1302 _), _),
|
adamc@254
|
1303 _), _),
|
adamc@254
|
1304 _) =>
|
adamc@254
|
1305 let
|
adamc@254
|
1306 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@254
|
1307 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@254
|
1308 in
|
adamc@254
|
1309 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@254
|
1310 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@254
|
1311 (L'.EAbs ("e2", s, s,
|
adamc@254
|
1312 strcat loc [sc "(",
|
adamc@254
|
1313 (L'.ERel 1, loc),
|
adamc@254
|
1314 sc " ",
|
adamc@254
|
1315 (L'.ERel 2, loc),
|
adamc@254
|
1316 sc " ",
|
adamc@254
|
1317 (L'.ERel 0, loc),
|
adamc@254
|
1318 sc ")"]), loc)), loc)), loc),
|
adamc@254
|
1319 fm)
|
adamc@254
|
1320 end
|
adamc@254
|
1321 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
|
adamc@254
|
1322 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
|
adamc@254
|
1323
|
adamc@254
|
1324 | L.ECApp (
|
adamc@254
|
1325 (L.ECApp (
|
adamc@254
|
1326 (L.ECApp (
|
adamc@254
|
1327 (L.ECApp (
|
adamc@253
|
1328 (L.EFfi ("Basis", "sql_comparison"), _),
|
adamc@253
|
1329 _), _),
|
adamc@253
|
1330 _), _),
|
adamc@253
|
1331 _), _),
|
adamc@253
|
1332 _) =>
|
adamc@253
|
1333 let
|
adamc@253
|
1334 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@253
|
1335 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@253
|
1336 in
|
adamc@253
|
1337 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@253
|
1338 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@253
|
1339 (L'.EAbs ("e2", s, s,
|
adamc@254
|
1340 strcat loc [sc "(",
|
adamc@254
|
1341 (L'.ERel 1, loc),
|
adamc@253
|
1342 sc " ",
|
adamc@253
|
1343 (L'.ERel 2, loc),
|
adamc@253
|
1344 sc " ",
|
adamc@254
|
1345 (L'.ERel 0, loc),
|
adamc@254
|
1346 sc ")"]), loc)), loc)), loc),
|
adamc@253
|
1347 fm)
|
adamc@253
|
1348 end
|
adamc@253
|
1349
|
adamc@253
|
1350 | L.ECApp (
|
adamc@253
|
1351 (L.ECApp (
|
adamc@253
|
1352 (L.ECApp (
|
adamc@253
|
1353 (L.ECApp (
|
adamc@253
|
1354 (L.ECApp (
|
adamc@253
|
1355 (L.ECApp (
|
adamc@253
|
1356 (L.ECApp (
|
adamc@253
|
1357 (L.EFfi ("Basis", "sql_field"), _),
|
adamc@253
|
1358 _), _),
|
adamc@253
|
1359 _), _),
|
adamc@253
|
1360 _), _),
|
adamc@253
|
1361 _), _),
|
adamc@253
|
1362 _), _),
|
adamc@253
|
1363 (L.CName tab, _)), _),
|
adamc@311
|
1364 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
|
adamc@260
|
1365
|
adamc@260
|
1366 | L.ECApp (
|
adamc@260
|
1367 (L.ECApp (
|
adamc@260
|
1368 (L.ECApp (
|
adamc@260
|
1369 (L.ECApp (
|
adamc@261
|
1370 (L.ECApp (
|
adamc@261
|
1371 (L.EFfi ("Basis", "sql_exp"), _),
|
adamc@261
|
1372 _), _),
|
adamc@261
|
1373 _), _),
|
adamc@261
|
1374 _), _),
|
adamc@261
|
1375 _), _),
|
adamc@261
|
1376 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
|
adamc@261
|
1377
|
adamc@261
|
1378 | L.ECApp (
|
adamc@261
|
1379 (L.ECApp (
|
adamc@261
|
1380 (L.ECApp (
|
adamc@261
|
1381 (L.ECApp (
|
adamc@260
|
1382 (L.EFfi ("Basis", "sql_relop"), _),
|
adamc@260
|
1383 _), _),
|
adamc@260
|
1384 _), _),
|
adamc@260
|
1385 _), _),
|
adamc@260
|
1386 _) =>
|
adamc@260
|
1387 let
|
adamc@260
|
1388 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@260
|
1389 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@260
|
1390 in
|
adamc@260
|
1391 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@260
|
1392 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@260
|
1393 (L'.EAbs ("e2", s, s,
|
adamc@260
|
1394 strcat loc [sc "((",
|
adamc@260
|
1395 (L'.ERel 1, loc),
|
adamc@260
|
1396 sc ") ",
|
adamc@260
|
1397 (L'.ERel 2, loc),
|
adamc@260
|
1398 sc " (",
|
adamc@260
|
1399 (L'.ERel 0, loc),
|
adamc@260
|
1400 sc "))"]), loc)), loc)), loc),
|
adamc@260
|
1401 fm)
|
adamc@260
|
1402 end
|
adamc@260
|
1403
|
adamc@260
|
1404 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
|
adamc@260
|
1405 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
|
adamc@260
|
1406 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
|
adamc@260
|
1407
|
adamc@265
|
1408 | L.ECApp (
|
adamc@265
|
1409 (L.ECApp (
|
adamc@265
|
1410 (L.ECApp (
|
adamc@265
|
1411 (L.EFfi ("Basis", "sql_count"), _),
|
adamc@265
|
1412 _), _),
|
adamc@265
|
1413 _), _),
|
adamc@265
|
1414 _) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@265
|
1415 (L'.EPrim (Prim.String "COUNT(*)"), loc)), loc),
|
adamc@265
|
1416 fm)
|
adamc@266
|
1417
|
adamc@266
|
1418 | L.ECApp (
|
adamc@266
|
1419 (L.ECApp (
|
adamc@266
|
1420 (L.ECApp (
|
adamc@266
|
1421 (L.ECApp (
|
adamc@266
|
1422 (L.EFfi ("Basis", "sql_aggregate"), _),
|
adamc@266
|
1423 _), _),
|
adamc@266
|
1424 _), _),
|
adamc@266
|
1425 _), _),
|
adamc@266
|
1426 _) =>
|
adamc@266
|
1427 let
|
adamc@266
|
1428 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@266
|
1429 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@266
|
1430 in
|
adamc@266
|
1431 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@266
|
1432 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@266
|
1433 strcat loc [(L'.ERel 1, loc),
|
adamc@266
|
1434 sc "(",
|
adamc@266
|
1435 (L'.ERel 0, loc),
|
adamc@266
|
1436 sc ")"]), loc)), loc),
|
adamc@266
|
1437 fm)
|
adamc@266
|
1438 end
|
adamc@266
|
1439
|
adamc@266
|
1440 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1441 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1442 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
|
adamc@266
|
1443 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1444 (L'.EPrim (Prim.String "AVG"), loc)), loc),
|
adamc@266
|
1445 fm)
|
adamc@266
|
1446 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
|
adamc@266
|
1447 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1448 (L'.EPrim (Prim.String "SUM"), loc)), loc),
|
adamc@266
|
1449 fm)
|
adamc@266
|
1450
|
adamc@266
|
1451 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1452 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1453 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1454 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
|
adamc@266
|
1455 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1456 (L'.EPrim (Prim.String "MAX"), loc)), loc),
|
adamc@266
|
1457 fm)
|
adamc@266
|
1458 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
|
adamc@266
|
1459 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1460 (L'.EPrim (Prim.String "MIN"), loc)), loc),
|
adamc@266
|
1461 fm)
|
adamc@266
|
1462
|
adamc@268
|
1463 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@268
|
1464 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
|
adamc@268
|
1465
|
adamc@338
|
1466 | L.EFfiApp ("Basis", "nextval", [e]) =>
|
adamc@338
|
1467 let
|
adamc@338
|
1468 val un = (L'.TRecord [], loc)
|
adamc@338
|
1469 val int = (L'.TFfi ("Basis", "int"), loc)
|
adamc@338
|
1470 val (e, fm) = monoExp (env, st, fm) e
|
adamc@338
|
1471 in
|
adamc@338
|
1472 ((L'.EAbs ("_", un, int,
|
adamc@338
|
1473 (L'.ENextval (liftExpInExp 0 e), loc)), loc),
|
adamc@338
|
1474 fm)
|
adamc@338
|
1475 end
|
adamc@338
|
1476
|
adamc@139
|
1477 | L.EApp (
|
adamc@139
|
1478 (L.ECApp (
|
adamc@141
|
1479 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
|
adamc@139
|
1480 _), _),
|
adamc@179
|
1481 se) =>
|
adamc@179
|
1482 let
|
adamc@179
|
1483 val (se, fm) = monoExp (env, st, fm) se
|
adamc@179
|
1484 in
|
adamc@179
|
1485 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
|
adamc@179
|
1486 end
|
adamc@179
|
1487
|
adamc@95
|
1488 | L.EApp (
|
adamc@95
|
1489 (L.EApp (
|
adamc@95
|
1490 (L.ECApp (
|
adamc@95
|
1491 (L.ECApp (
|
adamc@95
|
1492 (L.ECApp (
|
adamc@139
|
1493 (L.ECApp (
|
adamc@140
|
1494 (L.EFfi ("Basis", "join"),
|
adamc@139
|
1495 _), _), _),
|
adamc@139
|
1496 _), _),
|
adamc@95
|
1497 _), _),
|
adamc@95
|
1498 _), _),
|
adamc@95
|
1499 xml1), _),
|
adamc@179
|
1500 xml2) =>
|
adamc@179
|
1501 let
|
adamc@179
|
1502 val (xml1, fm) = monoExp (env, st, fm) xml1
|
adamc@179
|
1503 val (xml2, fm) = monoExp (env, st, fm) xml2
|
adamc@179
|
1504 in
|
adamc@179
|
1505 ((L'.EStrcat (xml1, xml2), loc), fm)
|
adamc@179
|
1506 end
|
adamc@95
|
1507
|
adamc@95
|
1508 | L.EApp (
|
adamc@95
|
1509 (L.EApp (
|
adamc@104
|
1510 (L.EApp (
|
adamc@95
|
1511 (L.ECApp (
|
adamc@104
|
1512 (L.ECApp (
|
adamc@104
|
1513 (L.ECApp (
|
adamc@104
|
1514 (L.ECApp (
|
adamc@139
|
1515 (L.ECApp (
|
adamc@139
|
1516 (L.ECApp (
|
adamc@139
|
1517 (L.ECApp (
|
adamc@139
|
1518 (L.ECApp (
|
adamc@139
|
1519 (L.EFfi ("Basis", "tag"),
|
adamc@139
|
1520 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@104
|
1521 attrs), _),
|
adamc@95
|
1522 tag), _),
|
adamc@95
|
1523 xml) =>
|
adamc@95
|
1524 let
|
adamc@140
|
1525 fun getTag' (e, _) =
|
adamc@140
|
1526 case e of
|
adamc@143
|
1527 L.EFfi ("Basis", tag) => (tag, [])
|
adamc@143
|
1528 | L.ECApp (e, t) => let
|
adamc@143
|
1529 val (tag, ts) = getTag' e
|
adamc@143
|
1530 in
|
adamc@143
|
1531 (tag, ts @ [t])
|
adamc@143
|
1532 end
|
adamc@140
|
1533 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@140
|
1534 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@143
|
1535 ("", []))
|
adamc@140
|
1536
|
adamc@95
|
1537 fun getTag (e, _) =
|
adamc@95
|
1538 case e of
|
adamc@143
|
1539 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
|
adamc@140
|
1540 | L.EApp (e, (L.ERecord [], _)) => getTag' e
|
adamc@95
|
1541 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@95
|
1542 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@143
|
1543 ("", []))
|
adamc@95
|
1544
|
adamc@143
|
1545 val (tag, targs) = getTag tag
|
adamc@95
|
1546
|
adamc@179
|
1547 val (attrs, fm) = monoExp (env, st, fm) attrs
|
adamc@104
|
1548
|
adamc@143
|
1549 fun tagStart tag =
|
adamc@104
|
1550 case #1 attrs of
|
adamc@104
|
1551 L'.ERecord xes =>
|
adamc@104
|
1552 let
|
adamc@104
|
1553 fun lowercaseFirst "" = ""
|
adamc@143
|
1554 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
|
adamc@143
|
1555 ^ String.extract (s, 1, NONE)
|
adamc@104
|
1556
|
adamc@104
|
1557 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
|
adamc@104
|
1558 in
|
adamc@410
|
1559 foldl (fn (("Action", _, _), acc) => acc
|
adamc@410
|
1560 | ((x, e, t), (s, fm)) =>
|
adamc@347
|
1561 case t of
|
adamc@347
|
1562 (L'.TFfi ("Basis", "bool"), _) =>
|
adamc@347
|
1563 let
|
adamc@347
|
1564 val s' = " " ^ lowercaseFirst x
|
adamc@347
|
1565 in
|
adamc@347
|
1566 ((L'.ECase (e,
|
adamc@347
|
1567 [((L'.PCon (L'.Enum,
|
adamc@347
|
1568 L'.PConFfi {mod = "Basis",
|
adamc@347
|
1569 datatyp = "bool",
|
adamc@347
|
1570 con = "True",
|
adamc@347
|
1571 arg = NONE},
|
adamc@347
|
1572 NONE), loc),
|
adamc@347
|
1573 (L'.EStrcat (s,
|
adamc@347
|
1574 (L'.EPrim (Prim.String s'), loc)), loc)),
|
adamc@347
|
1575 ((L'.PCon (L'.Enum,
|
adamc@347
|
1576 L'.PConFfi {mod = "Basis",
|
adamc@347
|
1577 datatyp = "bool",
|
adamc@347
|
1578 con = "False",
|
adamc@347
|
1579 arg = NONE},
|
adamc@347
|
1580 NONE), loc),
|
adamc@347
|
1581 s)],
|
adamc@347
|
1582 {disc = (L'.TFfi ("Basis", "bool"), loc),
|
adamc@347
|
1583 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
|
adamc@347
|
1584 fm)
|
adamc@347
|
1585 end
|
adamc@347
|
1586 | _ =>
|
adamc@347
|
1587 let
|
adamc@347
|
1588 val fooify =
|
adamc@347
|
1589 case x of
|
adamc@347
|
1590 "Href" => urlifyExp
|
adamc@347
|
1591 | "Link" => urlifyExp
|
adamc@347
|
1592 | _ => attrifyExp
|
adamc@120
|
1593
|
adamc@347
|
1594 val xp = " " ^ lowercaseFirst x ^ "=\""
|
adamc@179
|
1595
|
adamc@347
|
1596 val (e, fm) = fooify env fm (e, t)
|
adamc@347
|
1597 in
|
adamc@347
|
1598 ((L'.EStrcat (s,
|
adamc@347
|
1599 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
|
adamc@347
|
1600 (L'.EStrcat (e,
|
adamc@347
|
1601 (L'.EPrim (Prim.String "\""),
|
adamc@347
|
1602 loc)),
|
adamc@347
|
1603 loc)),
|
adamc@347
|
1604 loc)), loc),
|
adamc@347
|
1605 fm)
|
adamc@347
|
1606 end)
|
adamc@179
|
1607 (s, fm) xes
|
adamc@104
|
1608 end
|
adamc@143
|
1609 | _ => raise Fail "Non-record attributes!"
|
adamc@104
|
1610
|
adamc@143
|
1611 fun input typ =
|
adamc@143
|
1612 case targs of
|
adamc@155
|
1613 [_, (L.CName name, _)] =>
|
adamc@179
|
1614 let
|
adamc@179
|
1615 val (ts, fm) = tagStart "input"
|
adamc@179
|
1616 in
|
adamc@179
|
1617 ((L'.EStrcat (ts,
|
adamc@179
|
1618 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
|
adamc@179
|
1619 loc)), loc), fm)
|
adamc@179
|
1620 end
|
adamc@143
|
1621 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1622 raise Fail "No name passed to input tag")
|
adamc@104
|
1623
|
adamc@152
|
1624 fun normal (tag, extra) =
|
adamc@143
|
1625 let
|
adamc@179
|
1626 val (tagStart, fm) = tagStart tag
|
adamc@152
|
1627 val tagStart = case extra of
|
adamc@152
|
1628 NONE => tagStart
|
adamc@152
|
1629 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
|
adamc@152
|
1630
|
adamc@143
|
1631 fun normal () =
|
adamc@179
|
1632 let
|
adamc@179
|
1633 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1634 in
|
adamc@179
|
1635 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
|
adamc@179
|
1636 (L'.EStrcat (xml,
|
adamc@179
|
1637 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
|
adamc@179
|
1638 loc)), loc)),
|
adamc@179
|
1639 loc),
|
adamc@179
|
1640 fm)
|
adamc@179
|
1641 end
|
adamc@143
|
1642 in
|
adamc@143
|
1643 case xml of
|
adamc@143
|
1644 (L.EApp ((L.ECApp (
|
adamc@143
|
1645 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
|
adamc@143
|
1646 _), _),
|
adamc@143
|
1647 _), _),
|
adamc@143
|
1648 (L.EPrim (Prim.String s), _)), _) =>
|
adamc@143
|
1649 if CharVector.all Char.isSpace s then
|
adamc@179
|
1650 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
|
adamc@143
|
1651 else
|
adamc@143
|
1652 normal ()
|
adamc@143
|
1653 | _ => normal ()
|
adamc@143
|
1654 end
|
adamc@152
|
1655 in
|
adamc@152
|
1656 case tag of
|
adamc@410
|
1657 "submit" => normal ("input type=\"submit\"", NONE)
|
adamc@152
|
1658
|
adamc@152
|
1659 | "textbox" =>
|
adamc@152
|
1660 (case targs of
|
adamc@152
|
1661 [_, (L.CName name, _)] =>
|
adamc@179
|
1662 let
|
adamc@179
|
1663 val (ts, fm) = tagStart "input"
|
adamc@179
|
1664 in
|
adamc@179
|
1665 ((L'.EStrcat (ts,
|
adamc@179
|
1666 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
|
adamc@179
|
1667 loc)), loc), fm)
|
adamc@179
|
1668 end
|
adamc@152
|
1669 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1670 raise Fail "No name passed to textarea tag"))
|
adamc@155
|
1671 | "password" => input "password"
|
adamc@361
|
1672 | "textarea" =>
|
adamc@152
|
1673 (case targs of
|
adamc@152
|
1674 [_, (L.CName name, _)] =>
|
adamc@179
|
1675 let
|
adamc@179
|
1676 val (ts, fm) = tagStart "textarea"
|
adamc@179
|
1677 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1678 in
|
adamc@179
|
1679 ((L'.EStrcat ((L'.EStrcat (ts,
|
adamc@179
|
1680 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
|
adamc@179
|
1681 (L'.EStrcat (xml,
|
adamc@179
|
1682 (L'.EPrim (Prim.String "</textarea>"),
|
adamc@179
|
1683 loc)), loc)),
|
adamc@179
|
1684 loc), fm)
|
adamc@179
|
1685 end
|
adamc@152
|
1686 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1687 raise Fail "No name passed to ltextarea tag"))
|
adamc@153
|
1688
|
adamc@190
|
1689 | "checkbox" => input "checkbox"
|
adamc@190
|
1690
|
adamc@153
|
1691 | "radio" =>
|
adamc@153
|
1692 (case targs of
|
adamc@153
|
1693 [_, (L.CName name, _)] =>
|
adamc@179
|
1694 monoExp (env, St.setRadioGroup (st, name), fm) xml
|
adamc@153
|
1695 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1696 raise Fail "No name passed to radio tag"))
|
adamc@153
|
1697 | "radioOption" =>
|
adamc@153
|
1698 (case St.radioGroup st of
|
adamc@153
|
1699 NONE => raise Fail "No name for radioGroup"
|
adamc@153
|
1700 | SOME name =>
|
adamc@153
|
1701 normal ("input",
|
adamc@153
|
1702 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
|
adamc@152
|
1703
|
adamc@361
|
1704 | "select" =>
|
adamc@154
|
1705 (case targs of
|
adamc@154
|
1706 [_, (L.CName name, _)] =>
|
adamc@179
|
1707 let
|
adamc@179
|
1708 val (ts, fm) = tagStart "select"
|
adamc@179
|
1709 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1710 in
|
adamc@179
|
1711 ((L'.EStrcat ((L'.EStrcat (ts,
|
adamc@179
|
1712 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
|
adamc@179
|
1713 (L'.EStrcat (xml,
|
adamc@179
|
1714 (L'.EPrim (Prim.String "</select>"),
|
adamc@179
|
1715 loc)), loc)),
|
adamc@179
|
1716 loc),
|
adamc@179
|
1717 fm)
|
adamc@179
|
1718 end
|
adamc@154
|
1719 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@154
|
1720 raise Fail "No name passed to lselect tag"))
|
adamc@154
|
1721
|
adamc@361
|
1722 | "option" => normal ("option", NONE)
|
adamc@154
|
1723
|
adamc@325
|
1724 | "tabl" => normal ("table", NONE)
|
adamc@152
|
1725 | _ => normal (tag, NONE)
|
adamc@95
|
1726 end
|
adamc@94
|
1727
|
adamc@141
|
1728 | L.EApp ((L.ECApp (
|
adamc@361
|
1729 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
|
adamc@141
|
1730 _), _),
|
adamc@141
|
1731 xml) =>
|
adamc@143
|
1732 let
|
adamc@143
|
1733 fun findSubmit (e, _) =
|
adamc@143
|
1734 case e of
|
adamc@143
|
1735 L.EApp (
|
adamc@143
|
1736 (L.EApp (
|
adamc@143
|
1737 (L.ECApp (
|
adamc@143
|
1738 (L.ECApp (
|
adamc@143
|
1739 (L.ECApp (
|
adamc@143
|
1740 (L.ECApp (
|
adamc@143
|
1741 (L.EFfi ("Basis", "join"),
|
adamc@143
|
1742 _), _), _),
|
adamc@143
|
1743 _), _),
|
adamc@143
|
1744 _), _),
|
adamc@143
|
1745 _), _),
|
adamc@143
|
1746 xml1), _),
|
adamc@143
|
1747 xml2) => (case findSubmit xml1 of
|
adamc@143
|
1748 Error => Error
|
adamc@143
|
1749 | NotFound => findSubmit xml2
|
adamc@143
|
1750 | Found e =>
|
adamc@143
|
1751 case findSubmit xml2 of
|
adamc@143
|
1752 NotFound => Found e
|
adamc@143
|
1753 | _ => Error)
|
adamc@143
|
1754 | L.EApp (
|
adamc@143
|
1755 (L.EApp (
|
adamc@143
|
1756 (L.EApp (
|
adamc@143
|
1757 (L.ECApp (
|
adamc@143
|
1758 (L.ECApp (
|
adamc@143
|
1759 (L.ECApp (
|
adamc@143
|
1760 (L.ECApp (
|
adamc@143
|
1761 (L.ECApp (
|
adamc@143
|
1762 (L.ECApp (
|
adamc@143
|
1763 (L.ECApp (
|
adamc@143
|
1764 (L.ECApp (
|
adamc@143
|
1765 (L.EFfi ("Basis", "tag"),
|
adamc@143
|
1766 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@143
|
1767 attrs), _),
|
adamc@143
|
1768 _), _),
|
adamc@143
|
1769 xml) =>
|
adamc@143
|
1770 (case #1 attrs of
|
adamc@143
|
1771 L.ERecord xes =>
|
adamc@143
|
1772 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
|
adamc@143
|
1773 | _ => NONE) xes of
|
adamc@143
|
1774 NONE => findSubmit xml
|
adamc@143
|
1775 | SOME et =>
|
adamc@143
|
1776 case findSubmit xml of
|
adamc@143
|
1777 NotFound => Found et
|
adamc@143
|
1778 | _ => Error)
|
adamc@143
|
1779 | _ => findSubmit xml)
|
adamc@143
|
1780 | _ => NotFound
|
adamc@143
|
1781
|
adamc@143
|
1782 val (action, actionT) = case findSubmit xml of
|
adamc@143
|
1783 NotFound => raise Fail "No submit found"
|
adamc@143
|
1784 | Error => raise Fail "Not ready for multi-submit lforms yet"
|
adamc@143
|
1785 | Found et => et
|
adamc@143
|
1786
|
adamc@143
|
1787 val actionT = monoType env actionT
|
adamc@179
|
1788 val (action, fm) = monoExp (env, st, fm) action
|
adamc@179
|
1789 val (action, fm) = urlifyExp env fm (action, actionT)
|
adamc@179
|
1790 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@143
|
1791 in
|
adamc@179
|
1792 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
|
adamc@179
|
1793 (L'.EStrcat (action,
|
adamc@179
|
1794 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
|
adamc@179
|
1795 (L'.EStrcat (xml,
|
adamc@179
|
1796 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
|
adamc@179
|
1797 fm)
|
adamc@143
|
1798 end
|
adamc@141
|
1799
|
adamc@148
|
1800 | L.EApp ((L.ECApp (
|
adamc@148
|
1801 (L.ECApp (
|
adamc@148
|
1802 (L.ECApp (
|
adamc@148
|
1803 (L.ECApp (
|
adamc@148
|
1804 (L.EFfi ("Basis", "useMore"), _), _), _),
|
adamc@148
|
1805 _), _),
|
adamc@148
|
1806 _), _),
|
adamc@148
|
1807 _), _),
|
adamc@179
|
1808 xml) => monoExp (env, st, fm) xml
|
adamc@148
|
1809
|
adamc@283
|
1810 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
|
adamc@283
|
1811 let
|
adamc@283
|
1812 val t = monoType env t
|
adamc@283
|
1813 in
|
adamc@283
|
1814 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
|
adamc@283
|
1815 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
|
adamc@283
|
1816 fm)
|
adamc@283
|
1817 end
|
adamc@283
|
1818
|
adamc@179
|
1819 | L.EApp (e1, e2) =>
|
adamc@179
|
1820 let
|
adamc@179
|
1821 val (e1, fm) = monoExp (env, st, fm) e1
|
adamc@179
|
1822 val (e2, fm) = monoExp (env, st, fm) e2
|
adamc@179
|
1823 in
|
adamc@179
|
1824 ((L'.EApp (e1, e2), loc), fm)
|
adamc@179
|
1825 end
|
adamc@26
|
1826 | L.EAbs (x, dom, ran, e) =>
|
adamc@179
|
1827 let
|
adamc@179
|
1828 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
|
adamc@179
|
1829 in
|
adamc@179
|
1830 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
|
adamc@179
|
1831 end
|
adamc@25
|
1832 | L.ECApp _ => poly ()
|
adamc@25
|
1833 | L.ECAbs _ => poly ()
|
adamc@25
|
1834
|
adamc@252
|
1835 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
|
adamc@252
|
1836 | L.EFfiApp (m, x, es) =>
|
adamc@252
|
1837 let
|
adamc@252
|
1838 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
|
adamc@252
|
1839 in
|
adamc@252
|
1840 ((L'.EFfiApp (m, x, es), loc), fm)
|
adamc@252
|
1841 end
|
adamc@252
|
1842
|
adamc@179
|
1843 | L.ERecord xes =>
|
adamc@179
|
1844 let
|
adamc@179
|
1845 val (xes, fm) = ListUtil.foldlMap
|
adamc@179
|
1846 (fn ((x, e, t), fm) =>
|
adamc@179
|
1847 let
|
adamc@179
|
1848 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1849 in
|
adamc@179
|
1850 ((monoName env x,
|
adamc@179
|
1851 e,
|
adamc@179
|
1852 monoType env t), fm)
|
adamc@179
|
1853 end) fm xes
|
adamc@179
|
1854 in
|
adamc@179
|
1855 ((L'.ERecord xes, loc), fm)
|
adamc@179
|
1856 end
|
adamc@179
|
1857 | L.EField (e, x, _) =>
|
adamc@179
|
1858 let
|
adamc@179
|
1859 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1860 in
|
adamc@179
|
1861 ((L'.EField (e, monoName env x), loc), fm)
|
adamc@179
|
1862 end
|
adamc@343
|
1863 | L.EWith _ => poly ()
|
adamc@149
|
1864 | L.ECut _ => poly ()
|
adamc@73
|
1865 | L.EFold _ => poly ()
|
adamc@177
|
1866
|
adamc@182
|
1867 | L.ECase (e, pes, {disc, result}) =>
|
adamc@179
|
1868 let
|
adamc@179
|
1869 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1870 val (pes, fm) = ListUtil.foldlMap
|
adamc@179
|
1871 (fn ((p, e), fm) =>
|
adamc@179
|
1872 let
|
adamc@179
|
1873 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1874 in
|
adamc@182
|
1875 ((monoPat env p, e), fm)
|
adamc@179
|
1876 end) fm pes
|
adamc@179
|
1877 in
|
adamc@182
|
1878 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
|
adamc@179
|
1879 end
|
adamc@177
|
1880
|
adamc@179
|
1881 | L.EWrite e =>
|
adamc@179
|
1882 let
|
adamc@179
|
1883 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1884 in
|
adamc@252
|
1885 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
|
adamc@252
|
1886 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
|
adamc@179
|
1887 end
|
adamc@110
|
1888
|
adamc@179
|
1889 | L.EClosure (n, es) =>
|
adamc@179
|
1890 let
|
adamc@179
|
1891 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
|
adamc@179
|
1892 monoExp (env, st, fm) e)
|
adamc@179
|
1893 fm es
|
adamc@179
|
1894 in
|
adamc@179
|
1895 ((L'.EClosure (n, es), loc), fm)
|
adamc@179
|
1896 end
|
adamc@25
|
1897 end
|
adamc@25
|
1898
|
adamc@179
|
1899 fun monoDecl (env, fm) (all as (d, loc)) =
|
adamc@25
|
1900 let
|
adamc@25
|
1901 fun poly () =
|
adamc@25
|
1902 (E.errorAt loc "Unsupported declaration";
|
adamc@25
|
1903 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
|
adamc@25
|
1904 NONE)
|
adamc@25
|
1905 in
|
adamc@25
|
1906 case d of
|
adamc@25
|
1907 L.DCon _ => NONE
|
adamc@193
|
1908 | L.DDatatype (x, n, [], xncs) =>
|
adamc@193
|
1909 let
|
adamc@196
|
1910 val env' = Env.declBinds env all
|
adamc@196
|
1911 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
|
adamc@164
|
1912 in
|
adamc@273
|
1913 SOME (env', fm, [d])
|
adamc@193
|
1914 end
|
adamc@193
|
1915 | L.DDatatype _ => poly ()
|
adamc@179
|
1916 | L.DVal (x, n, t, e, s) =>
|
adamc@179
|
1917 let
|
adamc@179
|
1918 val (e, fm) = monoExp (env, St.empty, fm) e
|
adamc@179
|
1919 in
|
adamc@179
|
1920 SOME (Env.pushENamed env x n t NONE s,
|
adamc@179
|
1921 fm,
|
adamc@273
|
1922 [(L'.DVal (x, n, monoType env t, e, s), loc)])
|
adamc@179
|
1923 end
|
adamc@128
|
1924 | L.DValRec vis =>
|
adamc@128
|
1925 let
|
adamc@128
|
1926 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
|
adamc@179
|
1927
|
adamc@179
|
1928 val (vis, fm) = ListUtil.foldlMap
|
adamc@179
|
1929 (fn ((x, n, t, e, s), fm) =>
|
adamc@179
|
1930 let
|
adamc@179
|
1931 val (e, fm) = monoExp (env, St.empty, fm) e
|
adamc@179
|
1932 in
|
adamc@179
|
1933 ((x, n, monoType env t, e, s), fm)
|
adamc@179
|
1934 end)
|
adamc@179
|
1935 fm vis
|
adamc@128
|
1936 in
|
adamc@128
|
1937 SOME (env,
|
adamc@179
|
1938 fm,
|
adamc@273
|
1939 [(L'.DValRec vis, loc)])
|
adamc@128
|
1940 end
|
adamc@144
|
1941 | L.DExport (ek, n) =>
|
adamc@115
|
1942 let
|
adamc@120
|
1943 val (_, t, _, s) = Env.lookupENamed env n
|
adamc@120
|
1944
|
adamc@120
|
1945 fun unwind (t, _) =
|
adamc@120
|
1946 case t of
|
adamc@120
|
1947 L.TFun (dom, ran) => dom :: unwind ran
|
adamc@314
|
1948 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
|
adamc@314
|
1949 (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: unwind t
|
adamc@120
|
1950 | _ => []
|
adamc@120
|
1951
|
adamc@120
|
1952 val ts = map (monoType env) (unwind t)
|
adamc@115
|
1953 in
|
adamc@273
|
1954 SOME (env, fm, [(L'.DExport (ek, s, n, ts), loc)])
|
adamc@115
|
1955 end
|
adamc@273
|
1956 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
|
adamc@251
|
1957 let
|
adamc@251
|
1958 val t = (L.CFfi ("Basis", "string"), loc)
|
adamc@251
|
1959 val t' = (L'.TFfi ("Basis", "string"), loc)
|
adamc@311
|
1960 val s = "uw_" ^ s
|
adamc@251
|
1961 val e = (L'.EPrim (Prim.String s), loc)
|
adamc@273
|
1962
|
adamc@273
|
1963 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
|
adamc@251
|
1964 in
|
adamc@251
|
1965 SOME (Env.pushENamed env x n t NONE s,
|
adamc@251
|
1966 fm,
|
adamc@273
|
1967 [(L'.DTable (s, xts), loc),
|
adamc@273
|
1968 (L'.DVal (x, n, t', e, s), loc)])
|
adamc@251
|
1969 end
|
adamc@273
|
1970 | L.DTable _ => poly ()
|
adamc@338
|
1971 | L.DSequence (x, n, s) =>
|
adamc@338
|
1972 let
|
adamc@338
|
1973 val t = (L.CFfi ("Basis", "string"), loc)
|
adamc@338
|
1974 val t' = (L'.TFfi ("Basis", "string"), loc)
|
adamc@338
|
1975 val s = "uw_" ^ s
|
adamc@338
|
1976 val e = (L'.EPrim (Prim.String s), loc)
|
adamc@338
|
1977 in
|
adamc@338
|
1978 SOME (Env.pushENamed env x n t NONE s,
|
adamc@338
|
1979 fm,
|
adamc@338
|
1980 [(L'.DSequence s, loc),
|
adamc@338
|
1981 (L'.DVal (x, n, t', e, s), loc)])
|
adamc@338
|
1982 end
|
adamc@273
|
1983 | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)])
|
adamc@25
|
1984 end
|
adamc@25
|
1985
|
adamc@25
|
1986 fun monoize env ds =
|
adamc@25
|
1987 let
|
adamc@385
|
1988 val p = !urlPrefix
|
adamc@385
|
1989 val () =
|
adamc@385
|
1990 if p = "" then
|
adamc@385
|
1991 urlPrefix := "/"
|
adamc@385
|
1992 else if String.sub (p, size p - 1) <> #"/" then
|
adamc@385
|
1993 urlPrefix := p ^ "/"
|
adamc@385
|
1994 else
|
adamc@385
|
1995 ()
|
adamc@385
|
1996
|
adamc@179
|
1997 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
|
adamc@179
|
1998 case monoDecl (env, fm) d of
|
adamc@179
|
1999 NONE => (env, fm, ds)
|
adamc@273
|
2000 | SOME (env, fm, ds') =>
|
adamc@179
|
2001 (env,
|
adamc@179
|
2002 Fm.enter fm,
|
adamc@273
|
2003 ds' @ Fm.decls fm @ ds))
|
adamc@179
|
2004 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
|
adamc@25
|
2005 in
|
adamc@25
|
2006 rev ds
|
adamc@25
|
2007 end
|
adamc@25
|
2008
|
adamc@25
|
2009 end
|