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@436
|
823 | L.EFfi ("Basis", "show_time") =>
|
adamc@436
|
824 ((L'.EFfi ("Basis", "timeToString"), loc), fm)
|
adamc@286
|
825
|
adamc@290
|
826 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
|
adamc@290
|
827 let
|
adamc@290
|
828 val t = monoType env t
|
adamc@290
|
829 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@290
|
830 in
|
adamc@292
|
831 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
|
adamc@292
|
832 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
|
adamc@292
|
833 end
|
adamc@292
|
834 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
|
adamc@292
|
835 let
|
adamc@292
|
836 val t = monoType env t
|
adamc@292
|
837 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@292
|
838 in
|
adamc@292
|
839 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
|
adamc@292
|
840 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
|
adamc@290
|
841 end
|
adamc@290
|
842 | L.EFfi ("Basis", "read_int") =>
|
adamc@292
|
843 let
|
adamc@292
|
844 val t = (L'.TFfi ("Basis", "int"), loc)
|
adamc@292
|
845 in
|
adamc@292
|
846 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
|
adamc@292
|
847 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
|
adamc@292
|
848 loc),
|
adamc@292
|
849 fm)
|
adamc@292
|
850 end
|
adamc@290
|
851 | L.EFfi ("Basis", "read_float") =>
|
adamc@292
|
852 let
|
adamc@292
|
853 val t = (L'.TFfi ("Basis", "float"), loc)
|
adamc@292
|
854 in
|
adamc@292
|
855 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
|
adamc@292
|
856 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
|
adamc@292
|
857 loc),
|
adamc@292
|
858 fm)
|
adamc@292
|
859 end
|
adamc@290
|
860 | L.EFfi ("Basis", "read_string") =>
|
adamc@290
|
861 let
|
adamc@290
|
862 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@290
|
863 in
|
adamc@292
|
864 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
|
adamc@292
|
865 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
|
adamc@292
|
866 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
|
adamc@292
|
867 fm)
|
adamc@290
|
868 end
|
adamc@290
|
869 | L.EFfi ("Basis", "read_bool") =>
|
adamc@292
|
870 let
|
adamc@292
|
871 val t = (L'.TFfi ("Basis", "bool"), loc)
|
adamc@292
|
872 in
|
adamc@292
|
873 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
|
adamc@292
|
874 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
|
adamc@292
|
875 loc),
|
adamc@292
|
876 fm)
|
adamc@292
|
877 end
|
adamc@436
|
878 | L.EFfi ("Basis", "read_time") =>
|
adamc@436
|
879 let
|
adamc@436
|
880 val t = (L'.TFfi ("Basis", "time"), loc)
|
adamc@436
|
881 in
|
adamc@436
|
882 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToTime"), loc), readType' (t, loc)),
|
adamc@436
|
883 ("ReadError", (L'.EFfi ("Basis", "stringToTime_error"), loc), readErrType (t, loc))],
|
adamc@436
|
884 loc),
|
adamc@436
|
885 fm)
|
adamc@436
|
886 end
|
adamc@290
|
887
|
adamc@251
|
888 | L.ECApp ((L.EFfi ("Basis", "return"), _), t) =>
|
adamc@252
|
889 let
|
adamc@252
|
890 val t = monoType env t
|
adamc@252
|
891 in
|
adamc@252
|
892 ((L'.EAbs ("x", t,
|
adamc@252
|
893 (L'.TFun ((L'.TRecord [], loc), t), loc),
|
adamc@252
|
894 (L'.EAbs ("_", (L'.TRecord [], loc), t,
|
adamc@252
|
895 (L'.ERel 1, loc)), loc)), loc), fm)
|
adamc@252
|
896 end
|
adamc@251
|
897 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), t1), _), t2) =>
|
adamc@251
|
898 let
|
adamc@251
|
899 val t1 = monoType env t1
|
adamc@251
|
900 val t2 = monoType env t2
|
adamc@251
|
901 val un = (L'.TRecord [], loc)
|
adamc@252
|
902 val mt1 = (L'.TFun (un, t1), loc)
|
adamc@252
|
903 val mt2 = (L'.TFun (un, t2), loc)
|
adamc@251
|
904 in
|
adamc@252
|
905 ((L'.EAbs ("m1", mt1, (L'.TFun (mt1, (L'.TFun (mt2, (L'.TFun (un, un), loc)), loc)), loc),
|
adamc@252
|
906 (L'.EAbs ("m2", mt2, (L'.TFun (un, un), loc),
|
adamc@252
|
907 (L'.EAbs ("_", un, un,
|
adamc@252
|
908 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
|
adamc@252
|
909 (L'.ERecord [], loc)), loc),
|
adamc@252
|
910 (L'.EApp (
|
adamc@252
|
911 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
|
adamc@252
|
912 (L'.ERecord [], loc)),
|
adamc@252
|
913 loc)), loc)), loc)), loc)), loc),
|
adamc@251
|
914 fm)
|
adamc@251
|
915 end
|
adamc@251
|
916
|
adamc@307
|
917 | L.EFfiApp ("Basis", "dml", [e]) =>
|
adamc@307
|
918 let
|
adamc@307
|
919 val (e, fm) = monoExp (env, st, fm) e
|
adamc@307
|
920 val un = (L'.TRecord [], loc)
|
adamc@307
|
921 in
|
adamc@307
|
922 ((L'.EAbs ("_", un, un,
|
adamc@307
|
923 (L'.EDml (liftExpInExp 0 e), loc)), loc),
|
adamc@307
|
924 fm)
|
adamc@307
|
925 end
|
adamc@308
|
926
|
adamc@307
|
927 | L.ECApp ((L.EFfi ("Basis", "insert"), _), fields) =>
|
adamc@307
|
928 (case monoType env (L.TRecord fields, loc) of
|
adamc@307
|
929 (L'.TRecord fields, _) =>
|
adamc@307
|
930 let
|
adamc@307
|
931 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@307
|
932 val fields = map (fn (x, _) => (x, s)) fields
|
adamc@307
|
933 val rt = (L'.TRecord fields, loc)
|
adamc@307
|
934 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@307
|
935 in
|
adamc@307
|
936 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
|
adamc@307
|
937 (L'.EAbs ("fs", rt, s,
|
adamc@307
|
938 strcat loc [sc "INSERT INTO ",
|
adamc@307
|
939 (L'.ERel 1, loc),
|
adamc@307
|
940 sc " (",
|
adamc@311
|
941 strcatComma loc (map (fn (x, _) => sc ("uw_" ^ x)) fields),
|
adamc@307
|
942 sc ") VALUES (",
|
adamc@307
|
943 strcatComma loc (map (fn (x, _) =>
|
adamc@307
|
944 (L'.EField ((L'.ERel 0, loc),
|
adamc@307
|
945 x), loc)) fields),
|
adamc@307
|
946 sc ")"]), loc)), loc),
|
adamc@307
|
947 fm)
|
adamc@307
|
948 end
|
adamc@307
|
949 | _ => poly ())
|
adamc@307
|
950
|
adamc@403
|
951 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), changed) =>
|
adamc@308
|
952 (case monoType env (L.TRecord changed, loc) of
|
adamc@308
|
953 (L'.TRecord changed, _) =>
|
adamc@308
|
954 let
|
adamc@308
|
955 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@308
|
956 val changed = map (fn (x, _) => (x, s)) changed
|
adamc@308
|
957 val rt = (L'.TRecord changed, loc)
|
adamc@308
|
958 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@308
|
959 in
|
adamc@308
|
960 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@308
|
961 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
|
adamc@308
|
962 (L'.EAbs ("e", s, s,
|
adamc@308
|
963 strcat loc [sc "UPDATE ",
|
adamc@308
|
964 (L'.ERel 1, loc),
|
adamc@308
|
965 sc " AS T SET ",
|
adamc@308
|
966 strcatComma loc (map (fn (x, _) =>
|
adamc@311
|
967 strcat loc [sc ("uw_" ^ x
|
adamc@308
|
968 ^ " = "),
|
adamc@308
|
969 (L'.EField
|
adamc@308
|
970 ((L'.ERel 2,
|
adamc@308
|
971 loc),
|
adamc@308
|
972 x), loc)])
|
adamc@308
|
973 changed),
|
adamc@308
|
974 sc " WHERE ",
|
adamc@308
|
975 (L'.ERel 0, loc)]), loc)), loc)), loc),
|
adamc@308
|
976 fm)
|
adamc@308
|
977 end
|
adamc@308
|
978 | _ => poly ())
|
adamc@308
|
979
|
adamc@309
|
980 | L.ECApp ((L.EFfi ("Basis", "delete"), _), _) =>
|
adamc@309
|
981 let
|
adamc@309
|
982 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@309
|
983 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@309
|
984 in
|
adamc@309
|
985 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
|
adamc@309
|
986 (L'.EAbs ("e", s, s,
|
adamc@309
|
987 strcat loc [sc "DELETE FROM ",
|
adamc@309
|
988 (L'.ERel 1, loc),
|
adamc@309
|
989 sc " AS T WHERE ",
|
adamc@309
|
990 (L'.ERel 0, loc)]), loc)), loc),
|
adamc@309
|
991 fm)
|
adamc@309
|
992 end
|
adamc@309
|
993
|
adamc@252
|
994 | L.ECApp (
|
adamc@252
|
995 (L.ECApp (
|
adamc@252
|
996 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
|
adamc@252
|
997 exps), _),
|
adamc@252
|
998 state) =>
|
adamc@252
|
999 (case monoType env (L.TRecord exps, loc) of
|
adamc@252
|
1000 (L'.TRecord exps, _) =>
|
adamc@252
|
1001 let
|
adamc@252
|
1002 val tables = map (fn ((L.CName x, _), xts) =>
|
adamc@252
|
1003 (case monoType env (L.TRecord xts, loc) of
|
adamc@252
|
1004 (L'.TRecord xts, _) => SOME (x, xts)
|
adamc@252
|
1005 | _ => NONE)
|
adamc@252
|
1006 | _ => NONE) tables
|
adamc@252
|
1007 in
|
adamc@252
|
1008 if List.exists (fn x => x = NONE) tables then
|
adamc@252
|
1009 poly ()
|
adamc@252
|
1010 else
|
adamc@252
|
1011 let
|
adamc@252
|
1012 val tables = List.mapPartial (fn x => x) tables
|
adamc@252
|
1013 val state = monoType env state
|
adamc@252
|
1014 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
1015 val un = (L'.TRecord [], loc)
|
adamc@252
|
1016
|
adamc@252
|
1017 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
|
adamc@252
|
1018 val ft = (L'.TFun ((L'.TRecord rt, loc),
|
adamc@252
|
1019 (L'.TFun (state,
|
adamc@252
|
1020 (L'.TFun (un, state), loc)),
|
adamc@252
|
1021 loc)), loc)
|
adamc@252
|
1022
|
adamc@267
|
1023 val body' = (L'.EApp (
|
adamc@267
|
1024 (L'.EApp (
|
adamc@267
|
1025 (L'.EApp ((L'.ERel 4, loc),
|
adamc@267
|
1026 (L'.ERel 1, loc)), loc),
|
adamc@267
|
1027 (L'.ERel 0, loc)), loc),
|
adamc@267
|
1028 (L'.ERecord [], loc)), loc)
|
adamc@252
|
1029
|
adamc@252
|
1030 val body = (L'.EQuery {exps = exps,
|
adamc@252
|
1031 tables = tables,
|
adamc@252
|
1032 state = state,
|
adamc@252
|
1033 query = (L'.ERel 3, loc),
|
adamc@252
|
1034 body = body',
|
adamc@252
|
1035 initial = (L'.ERel 1, loc)},
|
adamc@252
|
1036 loc)
|
adamc@252
|
1037 in
|
adamc@252
|
1038 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
|
adamc@252
|
1039 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
|
adamc@252
|
1040 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
|
adamc@252
|
1041 (L'.EAbs ("_", un, state,
|
adamc@252
|
1042 body), loc)), loc)), loc)), loc), fm)
|
adamc@252
|
1043 end
|
adamc@252
|
1044 end
|
adamc@252
|
1045 | _ => poly ())
|
adamc@252
|
1046
|
adamc@252
|
1047 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
|
adamc@252
|
1048 let
|
adamc@252
|
1049 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@252
|
1050 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
1051 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
|
adamc@252
|
1052 in
|
adamc@252
|
1053 ((L'.EAbs ("r",
|
adamc@252
|
1054 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
|
adamc@252
|
1055 s,
|
adamc@252
|
1056 strcat loc [gf "Rows",
|
adamc@261
|
1057 (L'.ECase (gf "OrderBy",
|
adamc@261
|
1058 [((L'.PPrim (Prim.String ""), loc), sc ""),
|
adamc@261
|
1059 ((L'.PWild, loc),
|
adamc@261
|
1060 strcat loc [sc " ORDER BY ",
|
adamc@261
|
1061 gf "OrderBy"])],
|
adamc@261
|
1062 {disc = s, result = s}), loc),
|
adamc@252
|
1063 gf "Limit",
|
adamc@252
|
1064 gf "Offset"]), loc), fm)
|
adamc@252
|
1065 end
|
adamc@252
|
1066
|
adamc@252
|
1067 | L.ECApp (
|
adamc@252
|
1068 (L.ECApp (
|
adamc@252
|
1069 (L.ECApp (
|
adamc@252
|
1070 (L.ECApp (
|
adamc@252
|
1071 (L.EFfi ("Basis", "sql_query1"), _),
|
adamc@252
|
1072 (L.CRecord (_, tables), _)), _),
|
adamc@252
|
1073 (L.CRecord (_, grouped), _)), _),
|
adamc@252
|
1074 (L.CRecord (_, stables), _)), _),
|
adamc@252
|
1075 sexps) =>
|
adamc@252
|
1076 let
|
adamc@252
|
1077 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@252
|
1078 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
1079 val un = (L'.TRecord [], loc)
|
adamc@252
|
1080 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
|
adamc@252
|
1081
|
adamc@252
|
1082 fun doTables tables =
|
adamc@252
|
1083 let
|
adamc@252
|
1084 val tables = map (fn ((L.CName x, _), xts) =>
|
adamc@252
|
1085 (case monoType env (L.TRecord xts, loc) of
|
adamc@252
|
1086 (L'.TRecord xts, _) => SOME (x, xts)
|
adamc@252
|
1087 | _ => NONE)
|
adamc@252
|
1088 | _ => NONE) tables
|
adamc@252
|
1089 in
|
adamc@252
|
1090 if List.exists (fn x => x = NONE) tables then
|
adamc@252
|
1091 NONE
|
adamc@252
|
1092 else
|
adamc@260
|
1093 let
|
adamc@260
|
1094 val tables = List.mapPartial (fn x => x) tables
|
adamc@260
|
1095 val tables = ListMergeSort.sort
|
adamc@260
|
1096 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
|
adamc@260
|
1097 tables
|
adamc@260
|
1098 val tables = map (fn (x, xts) =>
|
adamc@260
|
1099 (x, ListMergeSort.sort
|
adamc@260
|
1100 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
|
adamc@260
|
1101 xts)) tables
|
adamc@260
|
1102 in
|
adamc@260
|
1103 SOME tables
|
adamc@260
|
1104 end
|
adamc@252
|
1105 end
|
adamc@252
|
1106 in
|
adamc@252
|
1107 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
|
adamc@252
|
1108 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
|
adamc@252
|
1109 ((L'.EAbs ("r",
|
adamc@252
|
1110 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
|
adamc@252
|
1111 ("Where", s),
|
adamc@252
|
1112 ("GroupBy", un),
|
adamc@252
|
1113 ("Having", s),
|
adamc@252
|
1114 ("SelectFields", un),
|
adamc@252
|
1115 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
|
adamc@252
|
1116 loc),
|
adamc@252
|
1117 s,
|
adamc@252
|
1118 strcat loc [sc "SELECT ",
|
adamc@261
|
1119 strcatComma loc (map (fn (x, t) =>
|
adamc@261
|
1120 strcat loc [
|
adamc@261
|
1121 (L'.EField (gf "SelectExps", x), loc),
|
adamc@261
|
1122 sc (" AS _" ^ x)
|
adamc@265
|
1123 ]) sexps
|
adamc@265
|
1124 @ map (fn (x, xts) =>
|
adamc@265
|
1125 strcatComma loc
|
adamc@265
|
1126 (map (fn (x', _) =>
|
adamc@311
|
1127 sc (x ^ ".uw_" ^ x'))
|
adamc@265
|
1128 xts)) stables),
|
adamc@252
|
1129 sc " FROM ",
|
adamc@252
|
1130 strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
|
adamc@253
|
1131 sc (" AS " ^ x)]) tables),
|
adamc@258
|
1132 (L'.ECase (gf "Where",
|
adamc@258
|
1133 [((L'.PPrim (Prim.String "TRUE"), loc),
|
adamc@258
|
1134 sc ""),
|
adamc@258
|
1135 ((L'.PWild, loc),
|
adamc@258
|
1136 strcat loc [sc " WHERE ", gf "Where"])],
|
adamc@258
|
1137 {disc = s,
|
adamc@258
|
1138 result = s}), loc),
|
adamc@258
|
1139
|
adamc@255
|
1140 if List.all (fn (x, xts) =>
|
adamc@255
|
1141 case List.find (fn (x', _) => x' = x) grouped of
|
adamc@255
|
1142 NONE => List.null xts
|
adamc@255
|
1143 | SOME (_, xts') =>
|
adamc@255
|
1144 List.all (fn (x, _) =>
|
adamc@255
|
1145 List.exists (fn (x', _) => x' = x)
|
adamc@255
|
1146 xts') xts) tables then
|
adamc@255
|
1147 sc ""
|
adamc@255
|
1148 else
|
adamc@255
|
1149 strcat loc [
|
adamc@255
|
1150 sc " GROUP BY ",
|
adamc@255
|
1151 strcatComma loc (map (fn (x, xts) =>
|
adamc@255
|
1152 strcatComma loc
|
adamc@255
|
1153 (map (fn (x', _) =>
|
adamc@311
|
1154 sc (x ^ ".uw_" ^ x'))
|
adamc@255
|
1155 xts)) grouped)
|
adamc@259
|
1156 ],
|
adamc@259
|
1157
|
adamc@259
|
1158 (L'.ECase (gf "Having",
|
adamc@259
|
1159 [((L'.PPrim (Prim.String "TRUE"), loc),
|
adamc@259
|
1160 sc ""),
|
adamc@259
|
1161 ((L'.PWild, loc),
|
adamc@259
|
1162 strcat loc [sc " HAVING ", gf "Having"])],
|
adamc@259
|
1163 {disc = s,
|
adamc@259
|
1164 result = s}), loc)
|
adamc@252
|
1165 ]), loc),
|
adamc@252
|
1166 fm)
|
adamc@252
|
1167 | _ => poly ()
|
adamc@252
|
1168 end
|
adamc@252
|
1169
|
adamc@252
|
1170 | L.ECApp (
|
adamc@252
|
1171 (L.ECApp (
|
adamc@252
|
1172 (L.ECApp (
|
adamc@252
|
1173 (L.ECApp (
|
adamc@252
|
1174 (L.EFfi ("Basis", "sql_inject"), _),
|
adamc@252
|
1175 _), _),
|
adamc@252
|
1176 _), _),
|
adamc@252
|
1177 _), _),
|
adamc@252
|
1178 t) =>
|
adamc@252
|
1179 let
|
adamc@252
|
1180 val t = monoType env t
|
adamc@252
|
1181 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@252
|
1182 in
|
adamc@252
|
1183 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
|
adamc@252
|
1184 (L'.ERel 0, loc)), loc), fm)
|
adamc@252
|
1185 end
|
adamc@252
|
1186
|
adamc@253
|
1187 | L.EFfi ("Basis", "sql_int") =>
|
adamc@253
|
1188 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
1189 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
1190 fm)
|
adamc@253
|
1191 | L.EFfi ("Basis", "sql_float") =>
|
adamc@253
|
1192 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
1193 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
1194 fm)
|
adamc@253
|
1195 | L.EFfi ("Basis", "sql_bool") =>
|
adamc@253
|
1196 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
1197 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
1198 fm)
|
adamc@253
|
1199 | L.EFfi ("Basis", "sql_string") =>
|
adamc@253
|
1200 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@253
|
1201 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
|
adamc@253
|
1202 fm)
|
adamc@253
|
1203
|
adamc@252
|
1204 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
|
adamc@252
|
1205 ((L'.ERecord [], loc), fm)
|
adamc@252
|
1206 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
|
adamc@252
|
1207 ((L'.ERecord [], loc), fm)
|
adamc@252
|
1208
|
adamc@252
|
1209 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
|
adamc@252
|
1210 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@261
|
1211 | L.ECApp (
|
adamc@261
|
1212 (L.ECApp (
|
adamc@261
|
1213 (L.ECApp (
|
adamc@261
|
1214 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
|
adamc@261
|
1215 _), _),
|
adamc@261
|
1216 _), _),
|
adamc@261
|
1217 _) =>
|
adamc@261
|
1218 let
|
adamc@261
|
1219 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@261
|
1220 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@261
|
1221 in
|
adamc@268
|
1222 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@268
|
1223 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
|
adamc@268
|
1224 (L'.EAbs ("e2", s, s,
|
adamc@268
|
1225 (L'.ECase ((L'.ERel 0, loc),
|
adamc@268
|
1226 [((L'.PPrim (Prim.String ""), loc),
|
adamc@268
|
1227 strcat loc [(L'.ERel 2, loc),
|
adamc@268
|
1228 (L'.ERel 1, loc)]),
|
adamc@268
|
1229 ((L'.PWild, loc),
|
adamc@268
|
1230 strcat loc [(L'.ERel 2, loc),
|
adamc@268
|
1231 (L'.ERel 1, loc),
|
adamc@268
|
1232 sc ", ",
|
adamc@268
|
1233 (L'.ERel 0, loc)])],
|
adamc@268
|
1234 {disc = s, result = s}), loc)), loc)), loc)), loc),
|
adamc@261
|
1235 fm)
|
adamc@261
|
1236 end
|
adamc@252
|
1237
|
adamc@252
|
1238 | L.EFfi ("Basis", "sql_no_limit") =>
|
adamc@252
|
1239 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@262
|
1240 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
|
adamc@262
|
1241 let
|
adamc@262
|
1242 val (e, fm) = monoExp (env, st, fm) e
|
adamc@262
|
1243 in
|
adamc@262
|
1244 (strcat loc [
|
adamc@262
|
1245 (L'.EPrim (Prim.String " LIMIT "), loc),
|
adamc@262
|
1246 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
|
adamc@262
|
1247 ],
|
adamc@262
|
1248 fm)
|
adamc@262
|
1249 end
|
adamc@262
|
1250
|
adamc@252
|
1251 | L.EFfi ("Basis", "sql_no_offset") =>
|
adamc@252
|
1252 ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@263
|
1253 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
|
adamc@263
|
1254 let
|
adamc@263
|
1255 val (e, fm) = monoExp (env, st, fm) e
|
adamc@263
|
1256 in
|
adamc@263
|
1257 (strcat loc [
|
adamc@263
|
1258 (L'.EPrim (Prim.String " OFFSET "), loc),
|
adamc@263
|
1259 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
|
adamc@263
|
1260 ],
|
adamc@263
|
1261 fm)
|
adamc@263
|
1262 end
|
adamc@253
|
1263
|
adamc@253
|
1264 | L.EFfi ("Basis", "sql_eq") =>
|
adamc@253
|
1265 ((L'.EPrim (Prim.String "="), loc), fm)
|
adamc@253
|
1266 | L.EFfi ("Basis", "sql_ne") =>
|
adamc@253
|
1267 ((L'.EPrim (Prim.String "<>"), loc), fm)
|
adamc@253
|
1268 | L.EFfi ("Basis", "sql_lt") =>
|
adamc@253
|
1269 ((L'.EPrim (Prim.String "<"), loc), fm)
|
adamc@253
|
1270 | L.EFfi ("Basis", "sql_le") =>
|
adamc@253
|
1271 ((L'.EPrim (Prim.String "<="), loc), fm)
|
adamc@253
|
1272 | L.EFfi ("Basis", "sql_gt") =>
|
adamc@253
|
1273 ((L'.EPrim (Prim.String ">"), loc), fm)
|
adamc@253
|
1274 | L.EFfi ("Basis", "sql_ge") =>
|
adamc@253
|
1275 ((L'.EPrim (Prim.String ">="), loc), fm)
|
adamc@253
|
1276
|
adamc@253
|
1277 | L.ECApp (
|
adamc@253
|
1278 (L.ECApp (
|
adamc@253
|
1279 (L.ECApp (
|
adamc@253
|
1280 (L.ECApp (
|
adamc@254
|
1281 (L.ECApp (
|
adamc@264
|
1282 (L.EFfi ("Basis", "sql_unary"), _),
|
adamc@264
|
1283 _), _),
|
adamc@264
|
1284 _), _),
|
adamc@264
|
1285 _), _),
|
adamc@264
|
1286 _), _),
|
adamc@264
|
1287 _) =>
|
adamc@264
|
1288 let
|
adamc@264
|
1289 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@264
|
1290 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@264
|
1291 in
|
adamc@264
|
1292 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@264
|
1293 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@264
|
1294 strcat loc [sc "(",
|
adamc@264
|
1295 (L'.ERel 1, loc),
|
adamc@264
|
1296 sc " ",
|
adamc@264
|
1297 (L'.ERel 0, loc),
|
adamc@264
|
1298 sc ")"]), loc)), loc),
|
adamc@264
|
1299 fm)
|
adamc@264
|
1300 end
|
adamc@264
|
1301 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
|
adamc@264
|
1302
|
adamc@264
|
1303 | L.ECApp (
|
adamc@264
|
1304 (L.ECApp (
|
adamc@264
|
1305 (L.ECApp (
|
adamc@264
|
1306 (L.ECApp (
|
adamc@264
|
1307 (L.ECApp (
|
adamc@254
|
1308 (L.ECApp (
|
adamc@254
|
1309 (L.EFfi ("Basis", "sql_binary"), _),
|
adamc@254
|
1310 _), _),
|
adamc@254
|
1311 _), _),
|
adamc@254
|
1312 _), _),
|
adamc@254
|
1313 _), _),
|
adamc@254
|
1314 _), _),
|
adamc@254
|
1315 _) =>
|
adamc@254
|
1316 let
|
adamc@254
|
1317 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@254
|
1318 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@254
|
1319 in
|
adamc@254
|
1320 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@254
|
1321 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@254
|
1322 (L'.EAbs ("e2", s, s,
|
adamc@254
|
1323 strcat loc [sc "(",
|
adamc@254
|
1324 (L'.ERel 1, loc),
|
adamc@254
|
1325 sc " ",
|
adamc@254
|
1326 (L'.ERel 2, loc),
|
adamc@254
|
1327 sc " ",
|
adamc@254
|
1328 (L'.ERel 0, loc),
|
adamc@254
|
1329 sc ")"]), loc)), loc)), loc),
|
adamc@254
|
1330 fm)
|
adamc@254
|
1331 end
|
adamc@254
|
1332 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
|
adamc@254
|
1333 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
|
adamc@254
|
1334
|
adamc@254
|
1335 | L.ECApp (
|
adamc@254
|
1336 (L.ECApp (
|
adamc@254
|
1337 (L.ECApp (
|
adamc@254
|
1338 (L.ECApp (
|
adamc@253
|
1339 (L.EFfi ("Basis", "sql_comparison"), _),
|
adamc@253
|
1340 _), _),
|
adamc@253
|
1341 _), _),
|
adamc@253
|
1342 _), _),
|
adamc@253
|
1343 _) =>
|
adamc@253
|
1344 let
|
adamc@253
|
1345 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@253
|
1346 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@253
|
1347 in
|
adamc@253
|
1348 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@253
|
1349 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@253
|
1350 (L'.EAbs ("e2", s, s,
|
adamc@254
|
1351 strcat loc [sc "(",
|
adamc@254
|
1352 (L'.ERel 1, loc),
|
adamc@253
|
1353 sc " ",
|
adamc@253
|
1354 (L'.ERel 2, loc),
|
adamc@253
|
1355 sc " ",
|
adamc@254
|
1356 (L'.ERel 0, loc),
|
adamc@254
|
1357 sc ")"]), loc)), loc)), loc),
|
adamc@253
|
1358 fm)
|
adamc@253
|
1359 end
|
adamc@253
|
1360
|
adamc@253
|
1361 | L.ECApp (
|
adamc@253
|
1362 (L.ECApp (
|
adamc@253
|
1363 (L.ECApp (
|
adamc@253
|
1364 (L.ECApp (
|
adamc@253
|
1365 (L.ECApp (
|
adamc@253
|
1366 (L.ECApp (
|
adamc@253
|
1367 (L.ECApp (
|
adamc@253
|
1368 (L.EFfi ("Basis", "sql_field"), _),
|
adamc@253
|
1369 _), _),
|
adamc@253
|
1370 _), _),
|
adamc@253
|
1371 _), _),
|
adamc@253
|
1372 _), _),
|
adamc@253
|
1373 _), _),
|
adamc@253
|
1374 (L.CName tab, _)), _),
|
adamc@311
|
1375 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
|
adamc@260
|
1376
|
adamc@260
|
1377 | L.ECApp (
|
adamc@260
|
1378 (L.ECApp (
|
adamc@260
|
1379 (L.ECApp (
|
adamc@260
|
1380 (L.ECApp (
|
adamc@261
|
1381 (L.ECApp (
|
adamc@261
|
1382 (L.EFfi ("Basis", "sql_exp"), _),
|
adamc@261
|
1383 _), _),
|
adamc@261
|
1384 _), _),
|
adamc@261
|
1385 _), _),
|
adamc@261
|
1386 _), _),
|
adamc@261
|
1387 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
|
adamc@261
|
1388
|
adamc@261
|
1389 | L.ECApp (
|
adamc@261
|
1390 (L.ECApp (
|
adamc@261
|
1391 (L.ECApp (
|
adamc@261
|
1392 (L.ECApp (
|
adamc@260
|
1393 (L.EFfi ("Basis", "sql_relop"), _),
|
adamc@260
|
1394 _), _),
|
adamc@260
|
1395 _), _),
|
adamc@260
|
1396 _), _),
|
adamc@260
|
1397 _) =>
|
adamc@260
|
1398 let
|
adamc@260
|
1399 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@260
|
1400 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@260
|
1401 in
|
adamc@260
|
1402 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@260
|
1403 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@260
|
1404 (L'.EAbs ("e2", s, s,
|
adamc@260
|
1405 strcat loc [sc "((",
|
adamc@260
|
1406 (L'.ERel 1, loc),
|
adamc@260
|
1407 sc ") ",
|
adamc@260
|
1408 (L'.ERel 2, loc),
|
adamc@260
|
1409 sc " (",
|
adamc@260
|
1410 (L'.ERel 0, loc),
|
adamc@260
|
1411 sc "))"]), loc)), loc)), loc),
|
adamc@260
|
1412 fm)
|
adamc@260
|
1413 end
|
adamc@260
|
1414
|
adamc@260
|
1415 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
|
adamc@260
|
1416 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
|
adamc@260
|
1417 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
|
adamc@260
|
1418
|
adamc@265
|
1419 | L.ECApp (
|
adamc@265
|
1420 (L.ECApp (
|
adamc@265
|
1421 (L.ECApp (
|
adamc@265
|
1422 (L.EFfi ("Basis", "sql_count"), _),
|
adamc@265
|
1423 _), _),
|
adamc@265
|
1424 _), _),
|
adamc@265
|
1425 _) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@265
|
1426 (L'.EPrim (Prim.String "COUNT(*)"), loc)), loc),
|
adamc@265
|
1427 fm)
|
adamc@266
|
1428
|
adamc@266
|
1429 | L.ECApp (
|
adamc@266
|
1430 (L.ECApp (
|
adamc@266
|
1431 (L.ECApp (
|
adamc@266
|
1432 (L.ECApp (
|
adamc@266
|
1433 (L.EFfi ("Basis", "sql_aggregate"), _),
|
adamc@266
|
1434 _), _),
|
adamc@266
|
1435 _), _),
|
adamc@266
|
1436 _), _),
|
adamc@266
|
1437 _) =>
|
adamc@266
|
1438 let
|
adamc@266
|
1439 val s = (L'.TFfi ("Basis", "string"), loc)
|
adamc@266
|
1440 fun sc s = (L'.EPrim (Prim.String s), loc)
|
adamc@266
|
1441 in
|
adamc@266
|
1442 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
|
adamc@266
|
1443 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
|
adamc@266
|
1444 strcat loc [(L'.ERel 1, loc),
|
adamc@266
|
1445 sc "(",
|
adamc@266
|
1446 (L'.ERel 0, loc),
|
adamc@266
|
1447 sc ")"]), loc)), loc),
|
adamc@266
|
1448 fm)
|
adamc@266
|
1449 end
|
adamc@266
|
1450
|
adamc@266
|
1451 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1452 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1453 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
|
adamc@266
|
1454 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1455 (L'.EPrim (Prim.String "AVG"), loc)), loc),
|
adamc@266
|
1456 fm)
|
adamc@266
|
1457 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
|
adamc@266
|
1458 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1459 (L'.EPrim (Prim.String "SUM"), loc)), loc),
|
adamc@266
|
1460 fm)
|
adamc@266
|
1461
|
adamc@266
|
1462 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1463 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1464 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
|
adamc@266
|
1465 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
|
adamc@266
|
1466 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1467 (L'.EPrim (Prim.String "MAX"), loc)), loc),
|
adamc@266
|
1468 fm)
|
adamc@266
|
1469 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
|
adamc@266
|
1470 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
|
adamc@266
|
1471 (L'.EPrim (Prim.String "MIN"), loc)), loc),
|
adamc@266
|
1472 fm)
|
adamc@266
|
1473
|
adamc@268
|
1474 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
|
adamc@268
|
1475 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
|
adamc@268
|
1476
|
adamc@338
|
1477 | L.EFfiApp ("Basis", "nextval", [e]) =>
|
adamc@338
|
1478 let
|
adamc@338
|
1479 val un = (L'.TRecord [], loc)
|
adamc@338
|
1480 val int = (L'.TFfi ("Basis", "int"), loc)
|
adamc@338
|
1481 val (e, fm) = monoExp (env, st, fm) e
|
adamc@338
|
1482 in
|
adamc@338
|
1483 ((L'.EAbs ("_", un, int,
|
adamc@338
|
1484 (L'.ENextval (liftExpInExp 0 e), loc)), loc),
|
adamc@338
|
1485 fm)
|
adamc@338
|
1486 end
|
adamc@338
|
1487
|
adamc@139
|
1488 | L.EApp (
|
adamc@139
|
1489 (L.ECApp (
|
adamc@141
|
1490 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
|
adamc@139
|
1491 _), _),
|
adamc@179
|
1492 se) =>
|
adamc@179
|
1493 let
|
adamc@179
|
1494 val (se, fm) = monoExp (env, st, fm) se
|
adamc@179
|
1495 in
|
adamc@179
|
1496 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
|
adamc@179
|
1497 end
|
adamc@179
|
1498
|
adamc@95
|
1499 | L.EApp (
|
adamc@95
|
1500 (L.EApp (
|
adamc@95
|
1501 (L.ECApp (
|
adamc@95
|
1502 (L.ECApp (
|
adamc@95
|
1503 (L.ECApp (
|
adamc@139
|
1504 (L.ECApp (
|
adamc@140
|
1505 (L.EFfi ("Basis", "join"),
|
adamc@139
|
1506 _), _), _),
|
adamc@139
|
1507 _), _),
|
adamc@95
|
1508 _), _),
|
adamc@95
|
1509 _), _),
|
adamc@95
|
1510 xml1), _),
|
adamc@179
|
1511 xml2) =>
|
adamc@179
|
1512 let
|
adamc@179
|
1513 val (xml1, fm) = monoExp (env, st, fm) xml1
|
adamc@179
|
1514 val (xml2, fm) = monoExp (env, st, fm) xml2
|
adamc@179
|
1515 in
|
adamc@179
|
1516 ((L'.EStrcat (xml1, xml2), loc), fm)
|
adamc@179
|
1517 end
|
adamc@95
|
1518
|
adamc@95
|
1519 | L.EApp (
|
adamc@95
|
1520 (L.EApp (
|
adamc@104
|
1521 (L.EApp (
|
adamc@95
|
1522 (L.ECApp (
|
adamc@104
|
1523 (L.ECApp (
|
adamc@104
|
1524 (L.ECApp (
|
adamc@104
|
1525 (L.ECApp (
|
adamc@139
|
1526 (L.ECApp (
|
adamc@139
|
1527 (L.ECApp (
|
adamc@139
|
1528 (L.ECApp (
|
adamc@139
|
1529 (L.ECApp (
|
adamc@139
|
1530 (L.EFfi ("Basis", "tag"),
|
adamc@139
|
1531 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@104
|
1532 attrs), _),
|
adamc@95
|
1533 tag), _),
|
adamc@95
|
1534 xml) =>
|
adamc@95
|
1535 let
|
adamc@140
|
1536 fun getTag' (e, _) =
|
adamc@140
|
1537 case e of
|
adamc@143
|
1538 L.EFfi ("Basis", tag) => (tag, [])
|
adamc@143
|
1539 | L.ECApp (e, t) => let
|
adamc@143
|
1540 val (tag, ts) = getTag' e
|
adamc@143
|
1541 in
|
adamc@143
|
1542 (tag, ts @ [t])
|
adamc@143
|
1543 end
|
adamc@140
|
1544 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@140
|
1545 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@143
|
1546 ("", []))
|
adamc@140
|
1547
|
adamc@95
|
1548 fun getTag (e, _) =
|
adamc@95
|
1549 case e of
|
adamc@143
|
1550 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
|
adamc@140
|
1551 | L.EApp (e, (L.ERecord [], _)) => getTag' e
|
adamc@95
|
1552 | _ => (E.errorAt loc "Non-constant XML tag";
|
adamc@95
|
1553 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
|
adamc@143
|
1554 ("", []))
|
adamc@95
|
1555
|
adamc@143
|
1556 val (tag, targs) = getTag tag
|
adamc@95
|
1557
|
adamc@179
|
1558 val (attrs, fm) = monoExp (env, st, fm) attrs
|
adamc@104
|
1559
|
adamc@143
|
1560 fun tagStart tag =
|
adamc@104
|
1561 case #1 attrs of
|
adamc@104
|
1562 L'.ERecord xes =>
|
adamc@104
|
1563 let
|
adamc@104
|
1564 fun lowercaseFirst "" = ""
|
adamc@143
|
1565 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
|
adamc@143
|
1566 ^ String.extract (s, 1, NONE)
|
adamc@104
|
1567
|
adamc@104
|
1568 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
|
adamc@104
|
1569 in
|
adamc@410
|
1570 foldl (fn (("Action", _, _), acc) => acc
|
adamc@410
|
1571 | ((x, e, t), (s, fm)) =>
|
adamc@347
|
1572 case t of
|
adamc@347
|
1573 (L'.TFfi ("Basis", "bool"), _) =>
|
adamc@347
|
1574 let
|
adamc@347
|
1575 val s' = " " ^ lowercaseFirst x
|
adamc@347
|
1576 in
|
adamc@347
|
1577 ((L'.ECase (e,
|
adamc@347
|
1578 [((L'.PCon (L'.Enum,
|
adamc@347
|
1579 L'.PConFfi {mod = "Basis",
|
adamc@347
|
1580 datatyp = "bool",
|
adamc@347
|
1581 con = "True",
|
adamc@347
|
1582 arg = NONE},
|
adamc@347
|
1583 NONE), loc),
|
adamc@347
|
1584 (L'.EStrcat (s,
|
adamc@347
|
1585 (L'.EPrim (Prim.String s'), loc)), loc)),
|
adamc@347
|
1586 ((L'.PCon (L'.Enum,
|
adamc@347
|
1587 L'.PConFfi {mod = "Basis",
|
adamc@347
|
1588 datatyp = "bool",
|
adamc@347
|
1589 con = "False",
|
adamc@347
|
1590 arg = NONE},
|
adamc@347
|
1591 NONE), loc),
|
adamc@347
|
1592 s)],
|
adamc@347
|
1593 {disc = (L'.TFfi ("Basis", "bool"), loc),
|
adamc@347
|
1594 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
|
adamc@347
|
1595 fm)
|
adamc@347
|
1596 end
|
adamc@347
|
1597 | _ =>
|
adamc@347
|
1598 let
|
adamc@347
|
1599 val fooify =
|
adamc@347
|
1600 case x of
|
adamc@347
|
1601 "Href" => urlifyExp
|
adamc@347
|
1602 | "Link" => urlifyExp
|
adamc@347
|
1603 | _ => attrifyExp
|
adamc@120
|
1604
|
adamc@347
|
1605 val xp = " " ^ lowercaseFirst x ^ "=\""
|
adamc@179
|
1606
|
adamc@347
|
1607 val (e, fm) = fooify env fm (e, t)
|
adamc@347
|
1608 in
|
adamc@347
|
1609 ((L'.EStrcat (s,
|
adamc@347
|
1610 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
|
adamc@347
|
1611 (L'.EStrcat (e,
|
adamc@347
|
1612 (L'.EPrim (Prim.String "\""),
|
adamc@347
|
1613 loc)),
|
adamc@347
|
1614 loc)),
|
adamc@347
|
1615 loc)), loc),
|
adamc@347
|
1616 fm)
|
adamc@347
|
1617 end)
|
adamc@179
|
1618 (s, fm) xes
|
adamc@104
|
1619 end
|
adamc@143
|
1620 | _ => raise Fail "Non-record attributes!"
|
adamc@104
|
1621
|
adamc@143
|
1622 fun input typ =
|
adamc@143
|
1623 case targs of
|
adamc@155
|
1624 [_, (L.CName name, _)] =>
|
adamc@179
|
1625 let
|
adamc@179
|
1626 val (ts, fm) = tagStart "input"
|
adamc@179
|
1627 in
|
adamc@179
|
1628 ((L'.EStrcat (ts,
|
adamc@179
|
1629 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
|
adamc@179
|
1630 loc)), loc), fm)
|
adamc@179
|
1631 end
|
adamc@143
|
1632 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1633 raise Fail "No name passed to input tag")
|
adamc@104
|
1634
|
adamc@152
|
1635 fun normal (tag, extra) =
|
adamc@143
|
1636 let
|
adamc@179
|
1637 val (tagStart, fm) = tagStart tag
|
adamc@152
|
1638 val tagStart = case extra of
|
adamc@152
|
1639 NONE => tagStart
|
adamc@152
|
1640 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
|
adamc@152
|
1641
|
adamc@143
|
1642 fun normal () =
|
adamc@179
|
1643 let
|
adamc@179
|
1644 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1645 in
|
adamc@179
|
1646 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
|
adamc@179
|
1647 (L'.EStrcat (xml,
|
adamc@179
|
1648 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
|
adamc@179
|
1649 loc)), loc)),
|
adamc@179
|
1650 loc),
|
adamc@179
|
1651 fm)
|
adamc@179
|
1652 end
|
adamc@143
|
1653 in
|
adamc@143
|
1654 case xml of
|
adamc@143
|
1655 (L.EApp ((L.ECApp (
|
adamc@143
|
1656 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
|
adamc@143
|
1657 _), _),
|
adamc@143
|
1658 _), _),
|
adamc@143
|
1659 (L.EPrim (Prim.String s), _)), _) =>
|
adamc@143
|
1660 if CharVector.all Char.isSpace s then
|
adamc@179
|
1661 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
|
adamc@143
|
1662 else
|
adamc@143
|
1663 normal ()
|
adamc@143
|
1664 | _ => normal ()
|
adamc@143
|
1665 end
|
adamc@152
|
1666 in
|
adamc@152
|
1667 case tag of
|
adamc@410
|
1668 "submit" => normal ("input type=\"submit\"", NONE)
|
adamc@152
|
1669
|
adamc@152
|
1670 | "textbox" =>
|
adamc@152
|
1671 (case targs of
|
adamc@152
|
1672 [_, (L.CName name, _)] =>
|
adamc@179
|
1673 let
|
adamc@179
|
1674 val (ts, fm) = tagStart "input"
|
adamc@179
|
1675 in
|
adamc@179
|
1676 ((L'.EStrcat (ts,
|
adamc@179
|
1677 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
|
adamc@179
|
1678 loc)), loc), fm)
|
adamc@179
|
1679 end
|
adamc@152
|
1680 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1681 raise Fail "No name passed to textarea tag"))
|
adamc@155
|
1682 | "password" => input "password"
|
adamc@361
|
1683 | "textarea" =>
|
adamc@152
|
1684 (case targs of
|
adamc@152
|
1685 [_, (L.CName name, _)] =>
|
adamc@179
|
1686 let
|
adamc@179
|
1687 val (ts, fm) = tagStart "textarea"
|
adamc@179
|
1688 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1689 in
|
adamc@179
|
1690 ((L'.EStrcat ((L'.EStrcat (ts,
|
adamc@179
|
1691 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
|
adamc@179
|
1692 (L'.EStrcat (xml,
|
adamc@179
|
1693 (L'.EPrim (Prim.String "</textarea>"),
|
adamc@179
|
1694 loc)), loc)),
|
adamc@179
|
1695 loc), fm)
|
adamc@179
|
1696 end
|
adamc@152
|
1697 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1698 raise Fail "No name passed to ltextarea tag"))
|
adamc@153
|
1699
|
adamc@190
|
1700 | "checkbox" => input "checkbox"
|
adamc@190
|
1701
|
adamc@153
|
1702 | "radio" =>
|
adamc@153
|
1703 (case targs of
|
adamc@153
|
1704 [_, (L.CName name, _)] =>
|
adamc@179
|
1705 monoExp (env, St.setRadioGroup (st, name), fm) xml
|
adamc@153
|
1706 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@153
|
1707 raise Fail "No name passed to radio tag"))
|
adamc@153
|
1708 | "radioOption" =>
|
adamc@153
|
1709 (case St.radioGroup st of
|
adamc@153
|
1710 NONE => raise Fail "No name for radioGroup"
|
adamc@153
|
1711 | SOME name =>
|
adamc@153
|
1712 normal ("input",
|
adamc@153
|
1713 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
|
adamc@152
|
1714
|
adamc@361
|
1715 | "select" =>
|
adamc@154
|
1716 (case targs of
|
adamc@154
|
1717 [_, (L.CName name, _)] =>
|
adamc@179
|
1718 let
|
adamc@179
|
1719 val (ts, fm) = tagStart "select"
|
adamc@179
|
1720 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@179
|
1721 in
|
adamc@179
|
1722 ((L'.EStrcat ((L'.EStrcat (ts,
|
adamc@179
|
1723 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
|
adamc@179
|
1724 (L'.EStrcat (xml,
|
adamc@179
|
1725 (L'.EPrim (Prim.String "</select>"),
|
adamc@179
|
1726 loc)), loc)),
|
adamc@179
|
1727 loc),
|
adamc@179
|
1728 fm)
|
adamc@179
|
1729 end
|
adamc@154
|
1730 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
|
adamc@154
|
1731 raise Fail "No name passed to lselect tag"))
|
adamc@154
|
1732
|
adamc@361
|
1733 | "option" => normal ("option", NONE)
|
adamc@154
|
1734
|
adamc@325
|
1735 | "tabl" => normal ("table", NONE)
|
adamc@152
|
1736 | _ => normal (tag, NONE)
|
adamc@95
|
1737 end
|
adamc@94
|
1738
|
adamc@141
|
1739 | L.EApp ((L.ECApp (
|
adamc@361
|
1740 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
|
adamc@141
|
1741 _), _),
|
adamc@141
|
1742 xml) =>
|
adamc@143
|
1743 let
|
adamc@143
|
1744 fun findSubmit (e, _) =
|
adamc@143
|
1745 case e of
|
adamc@143
|
1746 L.EApp (
|
adamc@143
|
1747 (L.EApp (
|
adamc@143
|
1748 (L.ECApp (
|
adamc@143
|
1749 (L.ECApp (
|
adamc@143
|
1750 (L.ECApp (
|
adamc@143
|
1751 (L.ECApp (
|
adamc@143
|
1752 (L.EFfi ("Basis", "join"),
|
adamc@143
|
1753 _), _), _),
|
adamc@143
|
1754 _), _),
|
adamc@143
|
1755 _), _),
|
adamc@143
|
1756 _), _),
|
adamc@143
|
1757 xml1), _),
|
adamc@143
|
1758 xml2) => (case findSubmit xml1 of
|
adamc@143
|
1759 Error => Error
|
adamc@143
|
1760 | NotFound => findSubmit xml2
|
adamc@143
|
1761 | Found e =>
|
adamc@143
|
1762 case findSubmit xml2 of
|
adamc@143
|
1763 NotFound => Found e
|
adamc@143
|
1764 | _ => Error)
|
adamc@143
|
1765 | L.EApp (
|
adamc@143
|
1766 (L.EApp (
|
adamc@143
|
1767 (L.EApp (
|
adamc@143
|
1768 (L.ECApp (
|
adamc@143
|
1769 (L.ECApp (
|
adamc@143
|
1770 (L.ECApp (
|
adamc@143
|
1771 (L.ECApp (
|
adamc@143
|
1772 (L.ECApp (
|
adamc@143
|
1773 (L.ECApp (
|
adamc@143
|
1774 (L.ECApp (
|
adamc@143
|
1775 (L.ECApp (
|
adamc@143
|
1776 (L.EFfi ("Basis", "tag"),
|
adamc@143
|
1777 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
|
adamc@143
|
1778 attrs), _),
|
adamc@143
|
1779 _), _),
|
adamc@143
|
1780 xml) =>
|
adamc@143
|
1781 (case #1 attrs of
|
adamc@143
|
1782 L.ERecord xes =>
|
adamc@143
|
1783 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
|
adamc@143
|
1784 | _ => NONE) xes of
|
adamc@143
|
1785 NONE => findSubmit xml
|
adamc@143
|
1786 | SOME et =>
|
adamc@143
|
1787 case findSubmit xml of
|
adamc@143
|
1788 NotFound => Found et
|
adamc@143
|
1789 | _ => Error)
|
adamc@143
|
1790 | _ => findSubmit xml)
|
adamc@143
|
1791 | _ => NotFound
|
adamc@143
|
1792
|
adamc@143
|
1793 val (action, actionT) = case findSubmit xml of
|
adamc@143
|
1794 NotFound => raise Fail "No submit found"
|
adamc@143
|
1795 | Error => raise Fail "Not ready for multi-submit lforms yet"
|
adamc@143
|
1796 | Found et => et
|
adamc@143
|
1797
|
adamc@143
|
1798 val actionT = monoType env actionT
|
adamc@179
|
1799 val (action, fm) = monoExp (env, st, fm) action
|
adamc@179
|
1800 val (action, fm) = urlifyExp env fm (action, actionT)
|
adamc@179
|
1801 val (xml, fm) = monoExp (env, st, fm) xml
|
adamc@143
|
1802 in
|
adamc@179
|
1803 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
|
adamc@179
|
1804 (L'.EStrcat (action,
|
adamc@179
|
1805 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
|
adamc@179
|
1806 (L'.EStrcat (xml,
|
adamc@179
|
1807 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
|
adamc@179
|
1808 fm)
|
adamc@143
|
1809 end
|
adamc@141
|
1810
|
adamc@148
|
1811 | L.EApp ((L.ECApp (
|
adamc@148
|
1812 (L.ECApp (
|
adamc@148
|
1813 (L.ECApp (
|
adamc@148
|
1814 (L.ECApp (
|
adamc@148
|
1815 (L.EFfi ("Basis", "useMore"), _), _), _),
|
adamc@148
|
1816 _), _),
|
adamc@148
|
1817 _), _),
|
adamc@148
|
1818 _), _),
|
adamc@179
|
1819 xml) => monoExp (env, st, fm) xml
|
adamc@148
|
1820
|
adamc@283
|
1821 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
|
adamc@283
|
1822 let
|
adamc@283
|
1823 val t = monoType env t
|
adamc@283
|
1824 in
|
adamc@283
|
1825 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
|
adamc@283
|
1826 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
|
adamc@283
|
1827 fm)
|
adamc@283
|
1828 end
|
adamc@283
|
1829
|
adamc@179
|
1830 | L.EApp (e1, e2) =>
|
adamc@179
|
1831 let
|
adamc@179
|
1832 val (e1, fm) = monoExp (env, st, fm) e1
|
adamc@179
|
1833 val (e2, fm) = monoExp (env, st, fm) e2
|
adamc@179
|
1834 in
|
adamc@179
|
1835 ((L'.EApp (e1, e2), loc), fm)
|
adamc@179
|
1836 end
|
adamc@26
|
1837 | L.EAbs (x, dom, ran, e) =>
|
adamc@179
|
1838 let
|
adamc@179
|
1839 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
|
adamc@179
|
1840 in
|
adamc@179
|
1841 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
|
adamc@179
|
1842 end
|
adamc@25
|
1843 | L.ECApp _ => poly ()
|
adamc@25
|
1844 | L.ECAbs _ => poly ()
|
adamc@25
|
1845
|
adamc@252
|
1846 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
|
adamc@252
|
1847 | L.EFfiApp (m, x, es) =>
|
adamc@252
|
1848 let
|
adamc@252
|
1849 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
|
adamc@252
|
1850 in
|
adamc@252
|
1851 ((L'.EFfiApp (m, x, es), loc), fm)
|
adamc@252
|
1852 end
|
adamc@252
|
1853
|
adamc@179
|
1854 | L.ERecord xes =>
|
adamc@179
|
1855 let
|
adamc@179
|
1856 val (xes, fm) = ListUtil.foldlMap
|
adamc@179
|
1857 (fn ((x, e, t), fm) =>
|
adamc@179
|
1858 let
|
adamc@179
|
1859 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1860 in
|
adamc@179
|
1861 ((monoName env x,
|
adamc@179
|
1862 e,
|
adamc@179
|
1863 monoType env t), fm)
|
adamc@179
|
1864 end) fm xes
|
adamc@179
|
1865 in
|
adamc@179
|
1866 ((L'.ERecord xes, loc), fm)
|
adamc@179
|
1867 end
|
adamc@179
|
1868 | L.EField (e, x, _) =>
|
adamc@179
|
1869 let
|
adamc@179
|
1870 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1871 in
|
adamc@179
|
1872 ((L'.EField (e, monoName env x), loc), fm)
|
adamc@179
|
1873 end
|
adamc@343
|
1874 | L.EWith _ => poly ()
|
adamc@149
|
1875 | L.ECut _ => poly ()
|
adamc@73
|
1876 | L.EFold _ => poly ()
|
adamc@177
|
1877
|
adamc@182
|
1878 | L.ECase (e, pes, {disc, result}) =>
|
adamc@179
|
1879 let
|
adamc@179
|
1880 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1881 val (pes, fm) = ListUtil.foldlMap
|
adamc@179
|
1882 (fn ((p, e), fm) =>
|
adamc@179
|
1883 let
|
adamc@179
|
1884 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1885 in
|
adamc@182
|
1886 ((monoPat env p, e), fm)
|
adamc@179
|
1887 end) fm pes
|
adamc@179
|
1888 in
|
adamc@182
|
1889 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
|
adamc@179
|
1890 end
|
adamc@177
|
1891
|
adamc@179
|
1892 | L.EWrite e =>
|
adamc@179
|
1893 let
|
adamc@179
|
1894 val (e, fm) = monoExp (env, st, fm) e
|
adamc@179
|
1895 in
|
adamc@252
|
1896 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
|
adamc@252
|
1897 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
|
adamc@179
|
1898 end
|
adamc@110
|
1899
|
adamc@179
|
1900 | L.EClosure (n, es) =>
|
adamc@179
|
1901 let
|
adamc@179
|
1902 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
|
adamc@179
|
1903 monoExp (env, st, fm) e)
|
adamc@179
|
1904 fm es
|
adamc@179
|
1905 in
|
adamc@179
|
1906 ((L'.EClosure (n, es), loc), fm)
|
adamc@179
|
1907 end
|
adamc@25
|
1908 end
|
adamc@25
|
1909
|
adamc@179
|
1910 fun monoDecl (env, fm) (all as (d, loc)) =
|
adamc@25
|
1911 let
|
adamc@25
|
1912 fun poly () =
|
adamc@25
|
1913 (E.errorAt loc "Unsupported declaration";
|
adamc@25
|
1914 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
|
adamc@25
|
1915 NONE)
|
adamc@25
|
1916 in
|
adamc@25
|
1917 case d of
|
adamc@25
|
1918 L.DCon _ => NONE
|
adamc@193
|
1919 | L.DDatatype (x, n, [], xncs) =>
|
adamc@193
|
1920 let
|
adamc@196
|
1921 val env' = Env.declBinds env all
|
adamc@196
|
1922 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
|
adamc@164
|
1923 in
|
adamc@273
|
1924 SOME (env', fm, [d])
|
adamc@193
|
1925 end
|
adamc@193
|
1926 | L.DDatatype _ => poly ()
|
adamc@179
|
1927 | L.DVal (x, n, t, e, s) =>
|
adamc@179
|
1928 let
|
adamc@179
|
1929 val (e, fm) = monoExp (env, St.empty, fm) e
|
adamc@179
|
1930 in
|
adamc@179
|
1931 SOME (Env.pushENamed env x n t NONE s,
|
adamc@179
|
1932 fm,
|
adamc@273
|
1933 [(L'.DVal (x, n, monoType env t, e, s), loc)])
|
adamc@179
|
1934 end
|
adamc@128
|
1935 | L.DValRec vis =>
|
adamc@128
|
1936 let
|
adamc@128
|
1937 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
|
adamc@179
|
1938
|
adamc@179
|
1939 val (vis, fm) = ListUtil.foldlMap
|
adamc@179
|
1940 (fn ((x, n, t, e, s), fm) =>
|
adamc@179
|
1941 let
|
adamc@179
|
1942 val (e, fm) = monoExp (env, St.empty, fm) e
|
adamc@179
|
1943 in
|
adamc@179
|
1944 ((x, n, monoType env t, e, s), fm)
|
adamc@179
|
1945 end)
|
adamc@179
|
1946 fm vis
|
adamc@128
|
1947 in
|
adamc@128
|
1948 SOME (env,
|
adamc@179
|
1949 fm,
|
adamc@273
|
1950 [(L'.DValRec vis, loc)])
|
adamc@128
|
1951 end
|
adamc@144
|
1952 | L.DExport (ek, n) =>
|
adamc@115
|
1953 let
|
adamc@120
|
1954 val (_, t, _, s) = Env.lookupENamed env n
|
adamc@120
|
1955
|
adamc@120
|
1956 fun unwind (t, _) =
|
adamc@120
|
1957 case t of
|
adamc@120
|
1958 L.TFun (dom, ran) => dom :: unwind ran
|
adamc@314
|
1959 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
|
adamc@314
|
1960 (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: unwind t
|
adamc@120
|
1961 | _ => []
|
adamc@120
|
1962
|
adamc@120
|
1963 val ts = map (monoType env) (unwind t)
|
adamc@115
|
1964 in
|
adamc@273
|
1965 SOME (env, fm, [(L'.DExport (ek, s, n, ts), loc)])
|
adamc@115
|
1966 end
|
adamc@273
|
1967 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
|
adamc@251
|
1968 let
|
adamc@251
|
1969 val t = (L.CFfi ("Basis", "string"), loc)
|
adamc@251
|
1970 val t' = (L'.TFfi ("Basis", "string"), loc)
|
adamc@311
|
1971 val s = "uw_" ^ s
|
adamc@251
|
1972 val e = (L'.EPrim (Prim.String s), loc)
|
adamc@273
|
1973
|
adamc@273
|
1974 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
|
adamc@251
|
1975 in
|
adamc@251
|
1976 SOME (Env.pushENamed env x n t NONE s,
|
adamc@251
|
1977 fm,
|
adamc@273
|
1978 [(L'.DTable (s, xts), loc),
|
adamc@273
|
1979 (L'.DVal (x, n, t', e, s), loc)])
|
adamc@251
|
1980 end
|
adamc@273
|
1981 | L.DTable _ => poly ()
|
adamc@338
|
1982 | L.DSequence (x, n, s) =>
|
adamc@338
|
1983 let
|
adamc@338
|
1984 val t = (L.CFfi ("Basis", "string"), loc)
|
adamc@338
|
1985 val t' = (L'.TFfi ("Basis", "string"), loc)
|
adamc@338
|
1986 val s = "uw_" ^ s
|
adamc@338
|
1987 val e = (L'.EPrim (Prim.String s), loc)
|
adamc@338
|
1988 in
|
adamc@338
|
1989 SOME (Env.pushENamed env x n t NONE s,
|
adamc@338
|
1990 fm,
|
adamc@338
|
1991 [(L'.DSequence s, loc),
|
adamc@338
|
1992 (L'.DVal (x, n, t', e, s), loc)])
|
adamc@338
|
1993 end
|
adamc@273
|
1994 | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)])
|
adamc@25
|
1995 end
|
adamc@25
|
1996
|
adamc@25
|
1997 fun monoize env ds =
|
adamc@25
|
1998 let
|
adamc@385
|
1999 val p = !urlPrefix
|
adamc@385
|
2000 val () =
|
adamc@385
|
2001 if p = "" then
|
adamc@385
|
2002 urlPrefix := "/"
|
adamc@385
|
2003 else if String.sub (p, size p - 1) <> #"/" then
|
adamc@385
|
2004 urlPrefix := p ^ "/"
|
adamc@385
|
2005 else
|
adamc@385
|
2006 ()
|
adamc@385
|
2007
|
adamc@179
|
2008 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
|
adamc@179
|
2009 case monoDecl (env, fm) d of
|
adamc@179
|
2010 NONE => (env, fm, ds)
|
adamc@273
|
2011 | SOME (env, fm, ds') =>
|
adamc@179
|
2012 (env,
|
adamc@179
|
2013 Fm.enter fm,
|
adamc@273
|
2014 ds' @ Fm.decls fm @ ds))
|
adamc@179
|
2015 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
|
adamc@25
|
2016 in
|
adamc@25
|
2017 rev ds
|
adamc@25
|
2018 end
|
adamc@25
|
2019
|
adamc@25
|
2020 end
|