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