annotate src/monoize.sml @ 757:fa2019a63ea4

Basis.list
author Adam Chlipala <adamc@hcoop.net>
date Thu, 30 Apr 2009 11:07:29 -0400
parents 8ce31c052dce
children 8323c1beef2e
rev   line source
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@735 37 structure IS = IntBinarySet
adamc@196 38
adamc@385 39 val urlPrefix = ref "/"
adamc@385 40
adamc@196 41 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
adamc@25 42
adamc@252 43 structure U = MonoUtil
adamc@252 44
adamc@252 45 val liftExpInExp =
adamc@252 46 U.Exp.mapB {typ = fn t => t,
adamc@252 47 exp = fn bound => fn e =>
adamc@252 48 case e of
adamc@252 49 L'.ERel xn =>
adamc@252 50 if xn < bound then
adamc@252 51 e
adamc@252 52 else
adamc@252 53 L'.ERel (xn + 1)
adamc@252 54 | _ => e,
adamc@252 55 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@252 56 | (bound, _) => bound}
adamc@252 57
adamc@25 58 fun monoName env (all as (c, loc)) =
adamc@25 59 let
adamc@25 60 fun poly () =
adamc@25 61 (E.errorAt loc "Unsupported name constructor";
adamc@25 62 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 63 "")
adamc@25 64 in
adamc@25 65 case c of
adamc@25 66 L.CName s => s
adamc@25 67 | _ => poly ()
adamc@25 68 end
adamc@25 69
adamc@292 70 fun readType' (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 71 (L'.TOption t, loc)), loc)
adamc@292 72 fun readErrType (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 73 t), loc)
adamc@292 74 fun readType (t, loc) =
adamc@292 75 (L'.TRecord [("Read", readType' (t, loc)),
adamc@292 76 ("ReadError", readErrType (t, loc))],
adamc@292 77 loc)
adamc@292 78
adamc@196 79 fun monoType env =
adamc@25 80 let
adamc@196 81 fun mt env dtmap (all as (c, loc)) =
adamc@196 82 let
adamc@196 83 fun poly () =
adamc@196 84 (E.errorAt loc "Unsupported type constructor";
adamc@196 85 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 86 dummyTyp)
adamc@196 87 in
adamc@196 88 case c of
adamc@196 89 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 90 | L.TCFun _ => poly ()
adamc@196 91 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@196 92 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
adamc@196 93 | L.TRecord _ => poly ()
adamc@196 94
adamc@288 95 | L.CApp ((L.CFfi ("Basis", "option"), _), t) =>
adamc@288 96 (L'.TOption (mt env dtmap t), loc)
adamc@757 97 | L.CApp ((L.CFfi ("Basis", "list"), _), t) =>
adamc@757 98 (L'.TList (mt env dtmap t), loc)
adamc@288 99
adamc@387 100 | L.CApp ((L.CFfi ("Basis", "eq"), _), t) =>
adamc@387 101 let
adamc@387 102 val t = mt env dtmap t
adamc@387 103 in
adamc@387 104 (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)
adamc@387 105 end
adamc@389 106 | L.CApp ((L.CFfi ("Basis", "num"), _), t) =>
adamc@389 107 let
adamc@389 108 val t = mt env dtmap t
adamc@389 109 in
adamc@417 110 (L'.TRecord [("Zero", t),
adamc@417 111 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 112 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 113 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 114 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 115 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 116 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))],
adamc@389 117 loc)
adamc@389 118 end
adamc@391 119 | L.CApp ((L.CFfi ("Basis", "ord"), _), t) =>
adamc@391 120 let
adamc@391 121 val t = mt env dtmap t
adamc@391 122 in
adamc@391 123 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 124 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 125 loc)
adamc@391 126 end
adamc@286 127 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
adamc@286 128 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@290 129 | L.CApp ((L.CFfi ("Basis", "read"), _), t) =>
adamc@292 130 readType (mt env dtmap t, loc)
adamc@286 131
adamc@717 132 | L.CFfi ("Basis", "url") => (L'.TFfi ("Basis", "string"), loc)
adamc@741 133 | L.CFfi ("Basis", "mimeType") => (L'.TFfi ("Basis", "string"), loc)
adamc@720 134 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 135 (L'.TFfi ("Basis", "string"), loc)
adamc@196 136 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 137 (L'.TFfi ("Basis", "string"), loc)
adamc@721 138 | L.CFfi ("Basis", "css_class") => (L'.TFfi ("Basis", "string"), loc)
adamc@196 139
adamc@251 140 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@252 141 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
adamc@565 142 | L.CApp ((L.CFfi ("Basis", "source"), _), t) =>
adamc@577 143 (L'.TSource, loc)
adamc@568 144 | L.CApp ((L.CFfi ("Basis", "signal"), _), t) =>
adamc@568 145 (L'.TSignal (mt env dtmap t), loc)
adamc@462 146 | L.CApp ((L.CFfi ("Basis", "http_cookie"), _), _) =>
adamc@462 147 (L'.TFfi ("Basis", "string"), loc)
adamc@705 148 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_table"), _), _), _), _) =>
adamc@252 149 (L'.TFfi ("Basis", "string"), loc)
adamc@338 150 | L.CFfi ("Basis", "sql_sequence") =>
adamc@338 151 (L'.TFfi ("Basis", "string"), loc)
adamc@252 152 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
adamc@252 153 (L'.TFfi ("Basis", "string"), loc)
adamc@252 154 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
adamc@252 155 (L'.TFfi ("Basis", "string"), loc)
adamc@748 156 | L.CApp ((L.CFfi ("Basis", "sql_from_items"), _), _) =>
adamc@748 157 (L'.TFfi ("Basis", "string"), loc)
adamc@252 158 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
adamc@252 159 (L'.TFfi ("Basis", "string"), loc)
adamc@707 160 | L.CApp ((L.CApp ((L.CFfi ("Basis", "primary_key"), _), _), _), _) =>
adamc@707 161 (L'.TFfi ("Basis", "string"), loc)
adamc@704 162 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_constraints"), _), _), _), _) =>
adamc@704 163 (L'.TFfi ("Basis", "sql_constraints"), loc)
adamc@705 164 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_constraint"), _), _), _), _) =>
adamc@704 165 (L'.TFfi ("Basis", "string"), loc)
adamc@712 166 | L.CApp ((L.CApp ((L.CFfi ("Basis", "linkable"), _), _), _), _) =>
adamc@712 167 (L'.TRecord [], loc)
adamc@709 168 | L.CApp ((L.CApp ((L.CFfi ("Basis", "matching"), _), _), _), _) =>
adamc@709 169 let
adamc@709 170 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 171 in
adamc@709 172 (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 173 end
adamc@709 174 | L.CApp ((L.CFfi ("Basis", "propagation_mode"), _), _) =>
adamc@709 175 (L'.TFfi ("Basis", "string"), loc)
adamc@252 176
adamc@252 177 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
adamc@252 178 (L'.TRecord [], loc)
adamc@252 179 | L.CFfi ("Basis", "sql_relop") =>
adamc@252 180 (L'.TFfi ("Basis", "string"), loc)
adamc@252 181 | L.CFfi ("Basis", "sql_direction") =>
adamc@252 182 (L'.TFfi ("Basis", "string"), loc)
adamc@252 183 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
adamc@252 184 (L'.TFfi ("Basis", "string"), loc)
adamc@252 185 | L.CFfi ("Basis", "sql_limit") =>
adamc@252 186 (L'.TFfi ("Basis", "string"), loc)
adamc@252 187 | L.CFfi ("Basis", "sql_offset") =>
adamc@252 188 (L'.TFfi ("Basis", "string"), loc)
adamc@753 189 | L.CApp ((L.CApp ((L.CFfi ("Basis", "fieldsOf"), _), _), _), _) =>
adamc@753 190 (L'.TRecord [], loc)
adamc@252 191
adamc@676 192 | L.CApp ((L.CFfi ("Basis", "sql_injectable_prim"), _), t) =>
adamc@676 193 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 194 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
adamc@252 195 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@750 196 | L.CApp ((L.CApp ((L.CFfi ("Basis", "nullify"), _), _), _), _) =>
adamc@750 197 (L'.TRecord [], loc)
adamc@252 198 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
adamc@252 199 (L'.TFfi ("Basis", "string"), loc)
adamc@252 200 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
adamc@252 201 (L'.TFfi ("Basis", "string"), loc)
adamc@252 202 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
adamc@252 203 (L'.TFfi ("Basis", "string"), loc)
adamc@252 204 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
adamc@252 205 (L'.TRecord [], loc)
adamc@252 206 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
adamc@252 207 (L'.TRecord [], loc)
adamc@559 208 | L.CApp ((L.CFfi ("Basis", "sql_arith"), _), _) =>
adamc@559 209 (L'.TRecord [], loc)
adamc@441 210 | L.CApp ((L.CFfi ("Basis", "sql_nfunc"), _), _) =>
adamc@441 211 (L'.TFfi ("Basis", "string"), loc)
adamc@746 212 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_ufunc"), _), _), _), _) =>
adamc@746 213 (L'.TFfi ("Basis", "string"), loc)
adamc@251 214
adamc@668 215 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@668 216 (L'.TFfi ("Basis", "channel"), loc)
adamc@668 217
adamc@196 218 | L.CRel _ => poly ()
adamc@196 219 | L.CNamed n =>
adamc@196 220 (case IM.find (dtmap, n) of
adamc@196 221 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 222 | NONE =>
adamc@196 223 let
adamc@196 224 val r = ref (L'.Default, [])
adamc@196 225 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 226
adamc@196 227 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 228
adamc@196 229 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 230 in
adamc@196 231 case xs of
adamc@198 232 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 233 (L'.TDatatype (n, r), loc))
adamc@196 234 | _ => poly ()
adamc@196 235 end)
adamc@196 236 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 237 | L.CApp _ => poly ()
adamc@196 238 | L.CAbs _ => poly ()
adamc@196 239
adamc@196 240 | L.CName _ => poly ()
adamc@196 241
adamc@196 242 | L.CRecord _ => poly ()
adamc@196 243 | L.CConcat _ => poly ()
adamc@621 244 | L.CMap _ => poly ()
adamc@196 245 | L.CUnit => poly ()
adamc@214 246
adamc@214 247 | L.CTuple _ => poly ()
adamc@214 248 | L.CProj _ => poly ()
adamc@626 249
adamc@626 250 | L.CKAbs _ => poly ()
adamc@626 251 | L.CKApp _ => poly ()
adamc@626 252 | L.TKFun _ => poly ()
adamc@196 253 end
adamc@25 254 in
adamc@196 255 mt env IM.empty
adamc@25 256 end
adamc@25 257
adamc@25 258 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 259
adamc@179 260 structure IM = IntBinaryMap
adamc@179 261
adamc@179 262 datatype foo_kind =
adamc@179 263 Attr
adamc@179 264 | Url
adamc@179 265
adamc@179 266 fun fk2s fk =
adamc@179 267 case fk of
adamc@179 268 Attr => "attr"
adamc@179 269 | Url => "url"
adamc@179 270
adamc@179 271 structure Fm :> sig
adamc@179 272 type t
adamc@179 273
adamc@179 274 val empty : int -> t
adamc@179 275
adamc@179 276 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@179 277 val enter : t -> t
adamc@179 278 val decls : t -> L'.decl list
adamc@683 279
adamc@683 280 val freshName : t -> int * t
adamc@179 281 end = struct
adamc@179 282
adamc@179 283 structure M = BinaryMapFn(struct
adamc@179 284 type ord_key = foo_kind
adamc@179 285 fun compare x =
adamc@179 286 case x of
adamc@179 287 (Attr, Attr) => EQUAL
adamc@179 288 | (Attr, _) => LESS
adamc@179 289 | (_, Attr) => GREATER
adamc@179 290
adamc@179 291 | (Url, Url) => EQUAL
adamc@179 292 end)
adamc@179 293
adamc@179 294 type t = {
adamc@179 295 count : int,
adamc@179 296 map : int IM.map M.map,
adamc@179 297 decls : L'.decl list
adamc@179 298 }
adamc@179 299
adamc@179 300 fun empty count = {
adamc@179 301 count = count,
adamc@179 302 map = M.empty,
adamc@179 303 decls = []
adamc@179 304 }
adamc@179 305
adamc@179 306 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
adamc@683 307 fun freshName {count, map, decls} = (count, {count = count + 1, map = map, decls = decls})
adamc@179 308 fun decls ({decls, ...} : t) = decls
adamc@179 309
adamc@179 310 fun lookup (t as {count, map, decls}) k n thunk =
adamc@120 311 let
adamc@179 312 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 313 in
adamc@179 314 case IM.find (im, n) of
adamc@179 315 NONE =>
adamc@179 316 let
adamc@179 317 val n' = count
adamc@179 318 val (d, {count, map, decls}) = thunk count {count = count + 1,
adamc@179 319 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@179 320 decls = decls}
adamc@179 321 in
adamc@179 322 ({count = count,
adamc@179 323 map = map,
adamc@179 324 decls = d :: decls}, n')
adamc@179 325 end
adamc@179 326 | SOME n' => (t, n')
adamc@179 327 end
adamc@179 328
adamc@179 329 end
adamc@185 330
adamc@185 331
adamc@185 332 fun capitalize s =
adamc@185 333 if s = "" then
adamc@185 334 s
adamc@185 335 else
adamc@185 336 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 337
adamc@179 338 fun fooifyExp fk env =
adamc@179 339 let
adamc@179 340 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 341 case #1 e of
adamc@120 342 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 343 let
adamc@120 344 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 345 in
adamc@385 346 ((L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
adamc@120 347 end
adamc@120 348 | L'.EClosure (fnam, args) =>
adamc@120 349 let
adamc@120 350 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 351 val ft = monoType env ft
adamc@111 352
adamc@179 353 fun attrify (args, ft, e, fm) =
adamc@120 354 case (args, ft) of
adamc@179 355 ([], _) => (e, fm)
adamc@120 356 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 357 let
adamc@179 358 val (arg', fm) = fooify fm (arg, t)
adamc@179 359 in
adamc@179 360 attrify (args, ft,
adamc@179 361 (L'.EStrcat (e,
adamc@179 362 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 363 arg'), loc)), loc),
adamc@179 364 fm)
adamc@179 365 end
adamc@120 366 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 367 (e, fm))
adamc@120 368 in
adamc@385 369 attrify (args, ft, (L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
adamc@120 370 end
adamc@120 371 | _ =>
adamc@120 372 case t of
adamc@185 373 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 374
adamc@179 375 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 376 | L'.TRecord ((x, t) :: xts) =>
adamc@200 377 let
adamc@200 378 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 379 in
adamc@200 380 foldl (fn ((x, t), (se, fm)) =>
adamc@200 381 let
adamc@200 382 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 383 in
adamc@200 384 ((L'.EStrcat (se,
adamc@200 385 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 386 se'), loc)), loc),
adamc@200 387 fm)
adamc@200 388 end) (se, fm) xts
adamc@200 389 end
adamc@111 390
adamc@196 391 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 392 let
adamc@179 393 fun makeDecl n fm =
adamc@179 394 let
adamc@193 395 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 396
adamc@179 397 val (branches, fm) =
adamc@179 398 ListUtil.foldlMap
adamc@179 399 (fn ((x, n, to), fm) =>
adamc@179 400 case to of
adamc@179 401 NONE =>
adamc@188 402 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 403 (L'.EPrim (Prim.String x), loc)),
adamc@179 404 fm)
adamc@179 405 | SOME t =>
adamc@179 406 let
adamc@182 407 val t = monoType env t
adamc@182 408 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 409 in
adamc@188 410 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 411 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 412 arg), loc)),
adamc@179 413 fm)
adamc@179 414 end)
adamc@179 415 fm xncs
adamc@179 416
adamc@179 417 val dom = tAll
adamc@179 418 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 419 in
adamc@179 420 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 421 n,
adamc@179 422 (L'.TFun (dom, ran), loc),
adamc@179 423 (L'.EAbs ("x",
adamc@179 424 dom,
adamc@179 425 ran,
adamc@179 426 (L'.ECase ((L'.ERel 0, loc),
adamc@179 427 branches,
adamc@182 428 {disc = dom,
adamc@182 429 result = ran}), loc)), loc),
adamc@179 430 "")], loc),
adamc@179 431 fm)
adamc@179 432 end
adamc@179 433
adamc@179 434 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 435 in
adamc@179 436 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 437 end
adamc@164 438
adamc@471 439 | L'.TOption t =>
adamc@471 440 let
adamc@471 441 val (body, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@471 442 in
adamc@471 443 ((L'.ECase (e,
adamc@471 444 [((L'.PNone t, loc),
adamc@471 445 (L'.EPrim (Prim.String "None"), loc)),
adamc@471 446
adamc@471 447 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc),
adamc@471 448 (L'.EStrcat ((L'.EPrim (Prim.String "Some/"), loc),
adamc@471 449 body), loc))],
adamc@471 450 {disc = tAll,
adamc@471 451 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@471 452 fm)
adamc@471 453 end
adamc@471 454
adamc@490 455 | _ => (E.errorAt loc "Don't know how to encode attribute/URL type";
adamc@120 456 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 457 (dummyExp, fm))
adamc@120 458 in
adamc@120 459 fooify
adamc@120 460 end
adamc@120 461
adamc@179 462 val attrifyExp = fooifyExp Attr
adamc@179 463 val urlifyExp = fooifyExp Url
adamc@105 464
adamc@143 465 datatype 'a failable_search =
adamc@143 466 Found of 'a
adamc@143 467 | NotFound
adamc@143 468 | Error
adamc@143 469
adamc@153 470 structure St :> sig
adamc@153 471 type t
adamc@153 472
adamc@153 473 val empty : t
adamc@153 474
adamc@153 475 val radioGroup : t -> string option
adamc@153 476 val setRadioGroup : t * string -> t
adamc@153 477 end = struct
adamc@153 478
adamc@153 479 type t = {
adamc@153 480 radioGroup : string option
adamc@153 481 }
adamc@153 482
adamc@153 483 val empty = {radioGroup = NONE}
adamc@153 484
adamc@153 485 fun radioGroup (t : t) = #radioGroup t
adamc@153 486
adamc@153 487 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 488
adamc@153 489 end
adamc@153 490
adamc@186 491 fun monoPatCon env pc =
adamc@178 492 case pc of
adamc@178 493 L.PConVar n => L'.PConVar n
adamc@188 494 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 495 arg = Option.map (monoType env) arg}
adamc@178 496
adamc@193 497 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 498
adamc@757 499
adamc@757 500 fun listify t = (L'.TRecord [("1", t), ("2", (L'.TList t, #2 t))], #2 t)
adamc@757 501
adamc@193 502 fun monoPat env (all as (p, loc)) =
adamc@193 503 let
adamc@193 504 fun poly () =
adamc@193 505 (E.errorAt loc "Unsupported pattern";
adamc@193 506 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 507 dummyPat)
adamc@193 508 in
adamc@193 509 case p of
adamc@193 510 L.PWild => (L'.PWild, loc)
adamc@193 511 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 512 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 513 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@757 514 | L.PCon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], NONE) =>
adamc@757 515 (L'.PNone (listify (monoType env t)), loc)
adamc@757 516 | L.PCon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], SOME p) =>
adamc@757 517 (L'.PSome (listify (monoType env t), monoPat env p), loc)
adamc@288 518 | L.PCon (L.Option, _, [t], NONE) => (L'.PNone (monoType env t), loc)
adamc@757 519 | L.PCon (L.Option, pc, [t], SOME p) => (L'.PSome (monoType env t, monoPat env p), loc)
adamc@193 520 | L.PCon _ => poly ()
adamc@193 521 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 522 end
adamc@178 523
adamc@252 524 fun strcat loc es =
adamc@252 525 case es of
adamc@252 526 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 527 | [e] => e
adamc@252 528 | _ =>
adamc@252 529 let
adamc@252 530 val e2 = List.last es
adamc@252 531 val es = List.take (es, length es - 1)
adamc@252 532 val e1 = List.last es
adamc@252 533 val es = List.take (es, length es - 1)
adamc@252 534 in
adamc@252 535 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
adamc@252 536 (L'.EStrcat (e1, e2), loc) es
adamc@252 537 end
adamc@252 538
adamc@252 539 fun strcatComma loc es =
adamc@252 540 case es of
adamc@252 541 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 542 | [e] => e
adamc@252 543 | _ =>
adamc@252 544 let
adamc@252 545 val e1 = List.last es
adamc@252 546 val es = List.take (es, length es - 1)
adamc@252 547 in
adamc@252 548 foldr (fn (e, e') =>
adamc@265 549 case (e, e') of
adamc@265 550 ((L'.EPrim (Prim.String ""), _), _) => e'
adamc@265 551 | (_, (L'.EPrim (Prim.String ""), _)) => e
adamc@252 552 | _ =>
adamc@252 553 (L'.EStrcat (e,
adamc@252 554 (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc))
adamc@252 555 e1 es
adamc@252 556 end
adamc@252 557
adamc@252 558 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
adamc@252 559
adamc@735 560 val readCookie = ref IS.empty
adamc@735 561
adamc@179 562 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 563 let
adamc@598 564 val strcat = strcat loc
adamc@598 565 val strcatComma = strcatComma loc
adamc@598 566 fun str s = (L'.EPrim (Prim.String s), loc)
adamc@598 567
adamc@25 568 fun poly () =
adamc@25 569 (E.errorAt loc "Unsupported expression";
adamc@25 570 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 571 (dummyExp, fm))
adamc@389 572
adamc@389 573 fun numTy t =
adamc@417 574 (L'.TRecord [("Zero", t),
adamc@417 575 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 576 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 577 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 578 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 579 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 580 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc)
adamc@417 581 fun numEx (t, zero, neg, plus, minus, times, dv, md) =
adamc@417 582 ((L'.ERecord [("Zero", (L'.EPrim zero, loc), t),
adamc@417 583 ("Neg", neg, (L'.TFun (t, t), loc)),
adamc@389 584 ("Plus", plus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 585 ("Minus", minus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 586 ("Times", times, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 587 ("Div", dv, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 588 ("Mod", md, (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc), fm)
adamc@391 589
adamc@391 590 fun ordTy t =
adamc@391 591 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 592 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))], loc)
adamc@391 593 fun ordEx (t, lt, le) =
adamc@391 594 ((L'.ERecord [("Lt", lt, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 595 ("Le", le, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 596 loc), fm)
adamc@750 597
adamc@750 598 fun outerRec xts =
adamc@750 599 (L'.TRecord (map (fn ((L.CName x, _), (L.CRecord (_, xts), _)) =>
adamc@750 600 (x, (L'.TRecord (map (fn (x', _) => (x, (L'.TRecord [], loc))) xts), loc))
adamc@750 601 | (x, all as (_, loc)) =>
adamc@750 602 (E.errorAt loc "Unsupported record field constructor";
adamc@750 603 Print.eprefaces' [("Name", CorePrint.p_con env x),
adamc@750 604 ("Constructor", CorePrint.p_con env all)];
adamc@750 605 ("", dummyTyp))) xts), loc)
adamc@25 606 in
adamc@25 607 case e of
adamc@179 608 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 609 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 610 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 611 | L.ECon (dk, pc, [], eo) =>
adamc@193 612 let
adamc@179 613 val (eo, fm) =
adamc@179 614 case eo of
adamc@179 615 NONE => (NONE, fm)
adamc@179 616 | SOME e =>
adamc@179 617 let
adamc@179 618 val (e, fm) = monoExp (env, st, fm) e
adamc@179 619 in
adamc@179 620 (SOME e, fm)
adamc@179 621 end
adamc@179 622 in
adamc@188 623 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 624 end
adamc@757 625 | L.ECon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], NONE) =>
adamc@757 626 ((L'.ENone (listify (monoType env t)), loc), fm)
adamc@757 627 | L.ECon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], SOME e) =>
adamc@757 628 let
adamc@757 629 val (e, fm) = monoExp (env, st, fm) e
adamc@757 630 in
adamc@757 631 ((L'.ESome (listify (monoType env t), e), loc), fm)
adamc@757 632 end
adamc@297 633 | L.ECon (L.Option, _, [t], NONE) =>
adamc@297 634 ((L'.ENone (monoType env t), loc), fm)
adamc@297 635 | L.ECon (L.Option, _, [t], SOME e) =>
adamc@297 636 let
adamc@297 637 val (e, fm) = monoExp (env, st, fm) e
adamc@297 638 in
adamc@297 639 ((L'.ESome (monoType env t, e), loc), fm)
adamc@297 640 end
adamc@193 641 | L.ECon _ => poly ()
adamc@94 642
adamc@387 643 | L.ECApp ((L.EFfi ("Basis", "eq"), _), t) =>
adamc@387 644 let
adamc@387 645 val t = monoType env t
adamc@387 646 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 647 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 648 in
adamc@387 649 ((L'.EAbs ("f", dom, dom,
adamc@387 650 (L'.ERel 0, loc)), loc), fm)
adamc@387 651 end
adamc@387 652 | L.ECApp ((L.EFfi ("Basis", "ne"), _), t) =>
adamc@387 653 let
adamc@387 654 val t = monoType env t
adamc@387 655 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 656 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 657 in
adamc@387 658 ((L'.EAbs ("f", dom, dom,
adamc@387 659 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@387 660 (L'.EAbs ("y", t, b,
adamc@387 661 (L'.EUnop ("!", (L'.EApp ((L'.EApp ((L'.ERel 2, loc),
adamc@387 662 (L'.ERel 1, loc)), loc),
adamc@387 663 (L'.ERel 0, loc)), loc)), loc)),
adamc@387 664 loc)),
adamc@387 665 loc)),
adamc@387 666 loc), fm)
adamc@387 667 end
adamc@387 668 | L.EFfi ("Basis", "eq_int") =>
adamc@387 669 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@387 670 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@387 671 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@387 672 (L'.TFfi ("Basis", "bool"), loc),
adamc@387 673 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@387 674 fm)
adamc@394 675 | L.EFfi ("Basis", "eq_float") =>
adamc@394 676 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@394 677 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 678 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@394 679 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 680 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@394 681 fm)
adamc@388 682 | L.EFfi ("Basis", "eq_bool") =>
adamc@388 683 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 684 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 685 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 686 (L'.TFfi ("Basis", "bool"), loc),
adamc@388 687 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 688 fm)
adamc@388 689 | L.EFfi ("Basis", "eq_string") =>
adamc@388 690 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@388 691 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 692 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@388 693 (L'.TFfi ("Basis", "bool"), loc),
adamc@388 694 (L'.EBinop ("!strcmp", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 695 fm)
adamc@437 696 | L.EFfi ("Basis", "eq_time") =>
adamc@437 697 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc),
adamc@437 698 (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@437 699 (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc),
adamc@437 700 (L'.TFfi ("Basis", "bool"), loc),
adamc@437 701 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@437 702 fm)
adamc@422 703 | L.ECApp ((L.EFfi ("Basis", "mkEq"), _), t) =>
adamc@422 704 let
adamc@422 705 val t = monoType env t
adamc@422 706 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@422 707 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@422 708 in
adamc@422 709 ((L'.EAbs ("f", dom, dom,
adamc@422 710 (L'.ERel 0, loc)), loc), fm)
adamc@422 711 end
adamc@387 712
adamc@417 713 | L.ECApp ((L.EFfi ("Basis", "zero"), _), t) =>
adamc@417 714 let
adamc@417 715 val t = monoType env t
adamc@417 716 in
adamc@417 717 ((L'.EAbs ("r", numTy t, t,
adamc@417 718 (L'.EField ((L'.ERel 0, loc), "Zero"), loc)), loc), fm)
adamc@417 719 end
adamc@389 720 | L.ECApp ((L.EFfi ("Basis", "neg"), _), t) =>
adamc@389 721 let
adamc@389 722 val t = monoType env t
adamc@389 723 in
adamc@389 724 ((L'.EAbs ("r", numTy t, (L'.TFun (t, t), loc),
adamc@389 725 (L'.EField ((L'.ERel 0, loc), "Neg"), loc)), loc), fm)
adamc@389 726 end
adamc@389 727 | L.ECApp ((L.EFfi ("Basis", "plus"), _), t) =>
adamc@389 728 let
adamc@389 729 val t = monoType env t
adamc@389 730 in
adamc@389 731 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 732 (L'.EField ((L'.ERel 0, loc), "Plus"), loc)), loc), fm)
adamc@389 733 end
adamc@389 734 | L.ECApp ((L.EFfi ("Basis", "minus"), _), t) =>
adamc@389 735 let
adamc@389 736 val t = monoType env t
adamc@389 737 in
adamc@389 738 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 739 (L'.EField ((L'.ERel 0, loc), "Minus"), loc)), loc), fm)
adamc@389 740 end
adamc@389 741 | L.ECApp ((L.EFfi ("Basis", "times"), _), t) =>
adamc@389 742 let
adamc@389 743 val t = monoType env t
adamc@389 744 in
adamc@389 745 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 746 (L'.EField ((L'.ERel 0, loc), "Times"), loc)), loc), fm)
adamc@389 747 end
adamc@389 748 | L.ECApp ((L.EFfi ("Basis", "div"), _), t) =>
adamc@389 749 let
adamc@389 750 val t = monoType env t
adamc@389 751 in
adamc@389 752 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 753 (L'.EField ((L'.ERel 0, loc), "Div"), loc)), loc), fm)
adamc@389 754 end
adamc@389 755 | L.ECApp ((L.EFfi ("Basis", "mod"), _), t) =>
adamc@389 756 let
adamc@389 757 val t = monoType env t
adamc@389 758 in
adamc@389 759 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 760 (L'.EField ((L'.ERel 0, loc), "Mod"), loc)), loc), fm)
adamc@389 761 end
adamc@389 762 | L.EFfi ("Basis", "num_int") =>
adamc@389 763 let
adamc@389 764 fun intBin s =
adamc@389 765 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 766 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "int"), loc)), loc),
adamc@389 767 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@389 768 (L'.TFfi ("Basis", "int"), loc),
adamc@389 769 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@389 770 in
adamc@389 771 numEx ((L'.TFfi ("Basis", "int"), loc),
adamc@417 772 Prim.Int (Int64.fromInt 0),
adamc@389 773 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 774 (L'.TFfi ("Basis", "int"), loc),
adamc@389 775 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@389 776 intBin "+",
adamc@389 777 intBin "-",
adamc@389 778 intBin "*",
adamc@389 779 intBin "/",
adamc@389 780 intBin "%")
adamc@389 781 end
adamc@390 782 | L.EFfi ("Basis", "num_float") =>
adamc@390 783 let
adamc@390 784 fun floatBin s =
adamc@390 785 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 786 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "float"), loc)), loc),
adamc@390 787 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@390 788 (L'.TFfi ("Basis", "float"), loc),
adamc@390 789 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@390 790 in
adamc@390 791 numEx ((L'.TFfi ("Basis", "float"), loc),
adamc@417 792 Prim.Float 0.0,
adamc@390 793 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 794 (L'.TFfi ("Basis", "float"), loc),
adamc@390 795 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@390 796 floatBin "+",
adamc@390 797 floatBin "-",
adamc@390 798 floatBin "*",
adamc@390 799 floatBin "/",
adamc@390 800 floatBin "fmod")
adamc@390 801 end
adamc@391 802
adamc@391 803 | L.ECApp ((L.EFfi ("Basis", "lt"), _), t) =>
adamc@391 804 let
adamc@391 805 val t = monoType env t
adamc@391 806 in
adamc@391 807 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
adamc@391 808 (L'.EField ((L'.ERel 0, loc), "Lt"), loc)), loc), fm)
adamc@391 809 end
adamc@391 810 | L.ECApp ((L.EFfi ("Basis", "le"), _), t) =>
adamc@391 811 let
adamc@391 812 val t = monoType env t
adamc@391 813 in
adamc@391 814 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
adamc@391 815 (L'.EField ((L'.ERel 0, loc), "Le"), loc)), loc), fm)
adamc@391 816 end
adamc@392 817 | L.ECApp ((L.EFfi ("Basis", "gt"), _), t) =>
adamc@392 818 let
adamc@392 819 val t = monoType env t
adamc@392 820 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@392 821 in
adamc@392 822 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
adamc@392 823 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@392 824 (L'.EAbs ("y", t, b,
adamc@392 825 (L'.EUnop ("!",
adamc@392 826 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
adamc@392 827 "Le"), loc),
adamc@392 828 (L'.ERel 1, loc)), loc),
adamc@392 829 (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@392 830 loc)),
adamc@392 831 loc), fm)
adamc@392 832 end
adamc@392 833 | L.ECApp ((L.EFfi ("Basis", "ge"), _), t) =>
adamc@392 834 let
adamc@392 835 val t = monoType env t
adamc@392 836 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@392 837 in
adamc@392 838 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
adamc@392 839 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@392 840 (L'.EAbs ("y", t, b,
adamc@392 841 (L'.EUnop ("!",
adamc@392 842 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
adamc@392 843 "Lt"), loc),
adamc@392 844 (L'.ERel 1, loc)), loc),
adamc@392 845 (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@392 846 loc)),
adamc@392 847 loc), fm)
adamc@392 848 end
adamc@391 849 | L.EFfi ("Basis", "ord_int") =>
adamc@391 850 let
adamc@391 851 fun intBin s =
adamc@391 852 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@391 853 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@391 854 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@391 855 (L'.TFfi ("Basis", "bool"), loc),
adamc@391 856 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@391 857 in
adamc@391 858 ordEx ((L'.TFfi ("Basis", "int"), loc),
adamc@391 859 intBin "<",
adamc@391 860 intBin "<=")
adamc@391 861 end
adamc@394 862 | L.EFfi ("Basis", "ord_float") =>
adamc@394 863 let
adamc@394 864 fun floatBin s =
adamc@394 865 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@394 866 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 867 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@394 868 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 869 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@394 870 in
adamc@394 871 ordEx ((L'.TFfi ("Basis", "float"), loc),
adamc@394 872 floatBin "<",
adamc@394 873 floatBin "<=")
adamc@394 874 end
adamc@394 875 | L.EFfi ("Basis", "ord_bool") =>
adamc@394 876 let
adamc@394 877 fun boolBin s =
adamc@394 878 (L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@394 879 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 880 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@394 881 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 882 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@394 883 in
adamc@394 884 ordEx ((L'.TFfi ("Basis", "bool"), loc),
adamc@394 885 boolBin "<",
adamc@394 886 boolBin "<=")
adamc@394 887 end
adamc@395 888 | L.EFfi ("Basis", "ord_string") =>
adamc@395 889 let
adamc@395 890 fun boolBin s =
adamc@395 891 (L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@395 892 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@395 893 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@395 894 (L'.TFfi ("Basis", "bool"), loc),
adamc@395 895 (L'.EBinop (s,
adamc@395 896 (L'.EBinop ("strcmp",
adamc@395 897 (L'.ERel 1, loc),
adamc@395 898 (L'.ERel 0, loc)), loc),
adamc@395 899 (L'.EPrim (Prim.Int (Int64.fromInt 0)), loc)), loc)), loc)), loc)
adamc@395 900 in
adamc@395 901 ordEx ((L'.TFfi ("Basis", "string"), loc),
adamc@395 902 boolBin "<",
adamc@395 903 boolBin "<=")
adamc@395 904 end
adamc@437 905 | L.EFfi ("Basis", "ord_time") =>
adamc@437 906 let
adamc@437 907 fun boolBin s =
adamc@437 908 (L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc),
adamc@437 909 (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@437 910 (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc),
adamc@437 911 (L'.TFfi ("Basis", "bool"), loc),
adamc@437 912 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@437 913 in
adamc@437 914 ordEx ((L'.TFfi ("Basis", "time"), loc),
adamc@437 915 boolBin "<",
adamc@437 916 boolBin "<=")
adamc@437 917 end
adamc@389 918
adamc@286 919 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
adamc@286 920 let
adamc@286 921 val t = monoType env t
adamc@286 922 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 923 in
adamc@286 924 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@286 925 (L'.ERel 0, loc)), loc), fm)
adamc@286 926 end
adamc@286 927 | L.EFfi ("Basis", "show_int") =>
adamc@286 928 ((L'.EFfi ("Basis", "intToString"), loc), fm)
adamc@286 929 | L.EFfi ("Basis", "show_float") =>
adamc@286 930 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
adamc@286 931 | L.EFfi ("Basis", "show_string") =>
adamc@286 932 let
adamc@286 933 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 934 in
adamc@286 935 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@286 936 end
adamc@286 937 | L.EFfi ("Basis", "show_bool") =>
adamc@286 938 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
adamc@436 939 | L.EFfi ("Basis", "show_time") =>
adamc@436 940 ((L'.EFfi ("Basis", "timeToString"), loc), fm)
adamc@727 941 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "show_xml"), _), _),_), _), _), _) =>
adamc@727 942 let
adamc@727 943 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@727 944 in
adamc@727 945 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@727 946 end
adamc@452 947 | L.ECApp ((L.EFfi ("Basis", "mkShow"), _), t) =>
adamc@452 948 let
adamc@452 949 val t = monoType env t
adamc@452 950 val b = (L'.TFfi ("Basis", "string"), loc)
adamc@452 951 val dom = (L'.TFun (t, b), loc)
adamc@452 952 in
adamc@452 953 ((L'.EAbs ("f", dom, dom,
adamc@452 954 (L'.ERel 0, loc)), loc), fm)
adamc@452 955 end
adamc@286 956
adamc@290 957 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
adamc@290 958 let
adamc@290 959 val t = monoType env t
adamc@290 960 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 961 in
adamc@292 962 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
adamc@292 963 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
adamc@292 964 end
adamc@292 965 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
adamc@292 966 let
adamc@292 967 val t = monoType env t
adamc@292 968 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@292 969 in
adamc@292 970 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
adamc@292 971 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
adamc@290 972 end
adamc@290 973 | L.EFfi ("Basis", "read_int") =>
adamc@292 974 let
adamc@292 975 val t = (L'.TFfi ("Basis", "int"), loc)
adamc@292 976 in
adamc@292 977 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
adamc@292 978 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
adamc@292 979 loc),
adamc@292 980 fm)
adamc@292 981 end
adamc@290 982 | L.EFfi ("Basis", "read_float") =>
adamc@292 983 let
adamc@292 984 val t = (L'.TFfi ("Basis", "float"), loc)
adamc@292 985 in
adamc@292 986 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
adamc@292 987 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
adamc@292 988 loc),
adamc@292 989 fm)
adamc@292 990 end
adamc@290 991 | L.EFfi ("Basis", "read_string") =>
adamc@290 992 let
adamc@290 993 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 994 in
adamc@292 995 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
adamc@292 996 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
adamc@292 997 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
adamc@292 998 fm)
adamc@290 999 end
adamc@290 1000 | L.EFfi ("Basis", "read_bool") =>
adamc@292 1001 let
adamc@292 1002 val t = (L'.TFfi ("Basis", "bool"), loc)
adamc@292 1003 in
adamc@292 1004 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
adamc@292 1005 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
adamc@292 1006 loc),
adamc@292 1007 fm)
adamc@292 1008 end
adamc@436 1009 | L.EFfi ("Basis", "read_time") =>
adamc@436 1010 let
adamc@436 1011 val t = (L'.TFfi ("Basis", "time"), loc)
adamc@436 1012 in
adamc@436 1013 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToTime"), loc), readType' (t, loc)),
adamc@436 1014 ("ReadError", (L'.EFfi ("Basis", "stringToTime_error"), loc), readErrType (t, loc))],
adamc@436 1015 loc),
adamc@436 1016 fm)
adamc@436 1017 end
adamc@290 1018
adamc@564 1019 | L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), _), _), t), _),
adamc@564 1020 (L.EFfi ("Basis", "transaction_monad"), _)) =>
adamc@252 1021 let
adamc@252 1022 val t = monoType env t
adamc@252 1023 in
adamc@252 1024 ((L'.EAbs ("x", t,
adamc@252 1025 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@252 1026 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@252 1027 (L'.ERel 1, loc)), loc)), loc), fm)
adamc@252 1028 end
adamc@564 1029 | L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _),
adamc@564 1030 (L.EFfi ("Basis", "transaction_monad"), _)) =>
adamc@251 1031 let
adamc@251 1032 val t1 = monoType env t1
adamc@251 1033 val t2 = monoType env t2
adamc@251 1034 val un = (L'.TRecord [], loc)
adamc@252 1035 val mt1 = (L'.TFun (un, t1), loc)
adamc@252 1036 val mt2 = (L'.TFun (un, t2), loc)
adamc@251 1037 in
adamc@572 1038 ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc)), loc),
adamc@572 1039 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc),
adamc@252 1040 (L'.EAbs ("_", un, un,
adamc@252 1041 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
adamc@252 1042 (L'.ERecord [], loc)), loc),
adamc@252 1043 (L'.EApp (
adamc@252 1044 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
adamc@252 1045 (L'.ERecord [], loc)),
adamc@252 1046 loc)), loc)), loc)), loc)), loc),
adamc@251 1047 fm)
adamc@251 1048 end
adamc@697 1049
adamc@670 1050 | L.EApp ((L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), _), _), t2), _),
adamc@670 1051 (L.EFfi ("Basis", "transaction_monad"), _)), _),
adamc@670 1052 (L.EApp ((L.ECApp ((L.EFfi ("Basis", "recv"), _), t1), _),
adamc@670 1053 ch), loc)) =>
adamc@670 1054 let
adamc@670 1055 val t1 = monoType env t1
adamc@670 1056 val t2 = monoType env t2
adamc@670 1057 val un = (L'.TRecord [], loc)
adamc@670 1058 val mt2 = (L'.TFun (un, t2), loc)
adamc@670 1059 val (ch, fm) = monoExp (env, st, fm) ch
adamc@670 1060 in
adamc@670 1061 ((L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc),
adamc@670 1062 (L'.EAbs ("_", un, un,
adamc@670 1063 (L'.ERecv (liftExpInExp 0 (liftExpInExp 0 ch),
adamc@670 1064 (L'.ERel 1, loc),
adamc@670 1065 t1), loc)), loc)), loc),
adamc@670 1066 fm)
adamc@670 1067 end
adamc@697 1068 | L.EFfiApp ("Basis", "recv", _) => poly ()
adamc@697 1069
adamc@695 1070 | L.EApp ((L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), _), _), t2), _),
adamc@695 1071 (L.EFfi ("Basis", "transaction_monad"), _)), _),
adamc@695 1072 (L.EAbs (_, _, _,
adamc@695 1073 (L.EFfiApp ("Basis", "sleep", [n]), _)), loc)) =>
adamc@695 1074 let
adamc@695 1075 val t2 = monoType env t2
adamc@695 1076 val un = (L'.TRecord [], loc)
adamc@695 1077 val mt2 = (L'.TFun (un, t2), loc)
adamc@695 1078 val (n, fm) = monoExp (env, st, fm) n
adamc@695 1079 in
adamc@695 1080 ((L'.EAbs ("m2", (L'.TFun (un, mt2), loc), (L'.TFun (un, un), loc),
adamc@695 1081 (L'.EAbs ("_", un, un,
adamc@697 1082 (L'.ESleep (liftExpInExp 0 n, (L'.EApp ((L'.ERel 1, loc),
adamc@695 1083 (L'.ERecord [], loc)), loc)),
adamc@695 1084 loc)), loc)), loc),
adamc@695 1085 fm)
adamc@695 1086 end
adamc@697 1087 | L.EFfiApp ("Basis", "sleep", _) => poly ()
adamc@251 1088
adamc@565 1089 | L.ECApp ((L.EFfi ("Basis", "source"), _), t) =>
adamc@565 1090 let
adamc@565 1091 val t = monoType env t
adamc@565 1092 in
adamc@577 1093 ((L'.EAbs ("x", t, (L'.TFun ((L'.TRecord [], loc), (L'.TSource, loc)), loc),
adamc@577 1094 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TSource, loc),
adamc@577 1095 (L'.EFfiApp ("Basis", "new_client_source",
adamc@590 1096 [(L'.EJavaScript (L'.Source t, (L'.ERel 1, loc), NONE), loc)]),
adamc@578 1097 loc)), loc)),
adamc@565 1098 loc),
adamc@565 1099 fm)
adamc@565 1100 end
adamc@575 1101 | L.ECApp ((L.EFfi ("Basis", "set"), _), t) =>
adamc@575 1102 let
adamc@575 1103 val t = monoType env t
adamc@575 1104 in
adamc@577 1105 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@575 1106 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@575 1107 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@575 1108 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@575 1109 (L'.EFfiApp ("Basis", "set_client_source",
adamc@577 1110 [(L'.ERel 2, loc),
adamc@590 1111 (L'.EJavaScript (L'.Source t,
adamc@590 1112 (L'.ERel 1, loc), NONE), loc)]),
adamc@575 1113 loc)), loc)), loc)), loc),
adamc@575 1114 fm)
adamc@575 1115 end
adamc@601 1116 | L.ECApp ((L.EFfi ("Basis", "get"), _), t) =>
adamc@601 1117 let
adamc@601 1118 val t = monoType env t
adamc@601 1119 in
adamc@601 1120 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@601 1121 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@601 1122 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@601 1123 (L'.EFfiApp ("Basis", "get_client_source",
adamc@601 1124 [(L'.ERel 1, loc)]),
adamc@601 1125 loc)), loc)), loc),
adamc@601 1126 fm)
adamc@601 1127 end
adamc@565 1128
adamc@694 1129 | L.EFfiApp ("Basis", "spawn", [e]) =>
adamc@694 1130 let
adamc@694 1131 val (e, fm) = monoExp (env, st, fm) e
adamc@694 1132 in
adamc@694 1133 ((L'.EApp (e, (L'.ERecord [], loc)), loc), fm)
adamc@694 1134 end
adamc@694 1135
adamc@568 1136 | L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), _), _), t), _),
adamc@568 1137 (L.EFfi ("Basis", "signal_monad"), _)) =>
adamc@568 1138 let
adamc@568 1139 val t = monoType env t
adamc@568 1140 in
adamc@568 1141 ((L'.EAbs ("x", t, (L'.TSignal t, loc),
adamc@568 1142 (L'.ESignalReturn (L'.ERel 0, loc), loc)), loc),
adamc@568 1143 fm)
adamc@568 1144 end
adamc@572 1145 | L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _),
adamc@572 1146 (L.EFfi ("Basis", "signal_monad"), _)) =>
adamc@572 1147 let
adamc@572 1148 val t1 = monoType env t1
adamc@572 1149 val t2 = monoType env t2
adamc@572 1150 val un = (L'.TRecord [], loc)
adamc@572 1151 val mt1 = (L'.TSignal t1, loc)
adamc@572 1152 val mt2 = (L'.TSignal t2, loc)
adamc@572 1153 in
adamc@572 1154 ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), mt2), loc),
adamc@572 1155 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), mt2,
adamc@572 1156 (L'.ESignalBind ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@572 1157 fm)
adamc@572 1158 end
adamc@574 1159 | L.ECApp ((L.EFfi ("Basis", "signal"), _), t) =>
adamc@574 1160 let
adamc@574 1161 val t = monoType env t
adamc@574 1162 in
adamc@574 1163 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TSignal t, loc),
adamc@574 1164 (L'.ESignalSource (L'.ERel 0, loc), loc)), loc),
adamc@574 1165 fm)
adamc@574 1166 end
adamc@568 1167
adamc@462 1168 | L.ECApp ((L.EFfi ("Basis", "getCookie"), _), t) =>
adamc@462 1169 let
adamc@462 1170 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1171 val un = (L'.TRecord [], loc)
adamc@462 1172 val t = monoType env t
adamc@462 1173 in
adamc@462 1174 ((L'.EAbs ("c", s, (L'.TFun (un, s), loc),
adamc@462 1175 (L'.EAbs ("_", un, s,
adamc@463 1176 (L'.EUnurlify ((L'.EFfiApp ("Basis", "get_cookie", [(L'.ERel 1, loc)]), loc),
adamc@463 1177 t),
adamc@463 1178 loc)), loc)), loc),
adamc@462 1179 fm)
adamc@462 1180 end
adamc@462 1181
adamc@462 1182 | L.ECApp ((L.EFfi ("Basis", "setCookie"), _), t) =>
adamc@462 1183 let
adamc@462 1184 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1185 val un = (L'.TRecord [], loc)
adamc@462 1186 val t = monoType env t
adamc@462 1187 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@462 1188 in
adamc@462 1189 ((L'.EAbs ("c", s, (L'.TFun (t, (L'.TFun (un, un), loc)), loc),
adamc@462 1190 (L'.EAbs ("v", t, (L'.TFun (un, un), loc),
adamc@462 1191 (L'.EAbs ("_", un, un,
adamc@466 1192 (L'.EFfiApp ("Basis", "set_cookie", [(L'.EPrim (Prim.String (!urlPrefix)),
adamc@466 1193 loc),
adamc@466 1194 (L'.ERel 2, loc),
adamc@466 1195 e]), loc)),
adamc@462 1196 loc)), loc)), loc),
adamc@462 1197 fm)
adamc@462 1198 end
adamc@462 1199
adamc@668 1200 | L.ECApp ((L.EFfi ("Basis", "channel"), _), t) =>
adamc@668 1201 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1202 (L'.EFfiApp ("Basis", "new_channel", [(L'.ERecord [], loc)]), loc)), loc),
adamc@668 1203 fm)
adamc@668 1204 | L.ECApp ((L.EFfi ("Basis", "send"), _), t) =>
adamc@668 1205 let
adamc@668 1206 val t = monoType env t
adamc@668 1207 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@668 1208 in
adamc@668 1209 ((L'.EAbs ("ch", (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1210 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@668 1211 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@668 1212 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@668 1213 (L'.EFfiApp ("Basis", "send",
adamc@668 1214 [(L'.ERel 2, loc),
adamc@668 1215 e]),
adamc@668 1216 loc)), loc)), loc)), loc),
adamc@668 1217 fm)
adamc@668 1218 end
adamc@668 1219
adamc@707 1220 | L.ECApp ((L.EFfi ("Basis", "no_primary_key"), _), _) =>
adamc@707 1221 ((L'.EPrim (Prim.String ""), loc),
adamc@707 1222 fm)
adamc@707 1223 | L.ECApp (
adamc@707 1224 (L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "primary_key"), _), _), _), t), _),
adamc@707 1225 nm), _),
adamc@707 1226 (L.CRecord (_, unique), _)) =>
adamc@707 1227 let
adamc@707 1228 val unique = (nm, t) :: unique
adamc@707 1229 val witnesses = (L'.TRecord (map (fn (nm, _) => (monoName env nm, (L'.TRecord [], loc))) unique), loc)
adamc@707 1230 in
adamc@707 1231 ((L'.EAbs ("_", witnesses, (L'.TFfi ("Basis", "string"), loc),
adamc@707 1232 (L'.EPrim (Prim.String
adamc@707 1233 (String.concatWith ", "
adamc@707 1234 (map (fn (x, _) => "uw_" ^ monoName env x) unique))),
adamc@707 1235 loc)), loc),
adamc@707 1236 fm)
adamc@707 1237 end
adamc@707 1238
adamc@704 1239 | L.ECApp ((L.EFfi ("Basis", "no_constraint"), _), _) =>
adamc@704 1240 ((L'.ERecord [], loc),
adamc@704 1241 fm)
adamc@705 1242 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "one_constraint"), _), _), _), _), _), (L.CName name, _)) =>
adamc@704 1243 ((L'.EAbs ("c",
adamc@704 1244 (L'.TFfi ("Basis", "string"), loc),
adamc@704 1245 (L'.TFfi ("Basis", "sql_constraints"), loc),
adamc@704 1246 (L'.ERecord [(name, (L'.ERel 0, loc), (L'.TFfi ("Basis", "string"), loc))], loc)), loc),
adamc@704 1247 fm)
adamc@705 1248 | L.ECApp (
adamc@705 1249 (L.ECApp (
adamc@705 1250 (L.ECApp (
adamc@705 1251 (L.EFfi ("Basis", "join_constraints"), _),
adamc@705 1252 _), _),
adamc@705 1253 _), _),
adamc@705 1254 _) =>
adamc@704 1255 let
adamc@704 1256 val constraints = (L'.TFfi ("Basis", "sql_constraints"), loc)
adamc@704 1257 in
adamc@704 1258 ((L'.EAbs ("cs1", constraints, (L'.TFun (constraints, constraints), loc),
adamc@704 1259 (L'.EAbs ("cs2", constraints, constraints,
adamc@704 1260 (L'.EStrcat ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@704 1261 fm)
adamc@704 1262 end
adamc@704 1263
adamc@705 1264 | L.ECApp (
adamc@705 1265 (L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "unique"), _), _), _), t), _),
adamc@705 1266 nm), _),
adamc@705 1267 (L.CRecord (_, unique), _)) =>
adamc@705 1268 let
adamc@705 1269 val unique = (nm, t) :: unique
adamc@705 1270 in
adamc@705 1271 ((L'.EPrim (Prim.String ("UNIQUE ("
adamc@705 1272 ^ String.concatWith ", " (map (fn (x, _) => "uw_" ^ monoName env x) unique)
adamc@705 1273 ^ ")")), loc),
adamc@705 1274 fm)
adamc@705 1275 end
adamc@704 1276
adamc@712 1277 | L.ECApp ((L.EFfi ("Basis", "linkable_same"), loc), _) =>
adamc@712 1278 ((L'.ERecord [], loc), fm)
adamc@712 1279 | L.ECApp ((L.EFfi ("Basis", "linkable_from_nullable"), loc), _) =>
adamc@712 1280 ((L'.ERecord [], loc), fm)
adamc@712 1281 | L.ECApp ((L.EFfi ("Basis", "linkable_to_nullable"), loc), _) =>
adamc@712 1282 ((L'.ERecord [], loc), fm)
adamc@712 1283
adamc@709 1284 | L.EFfi ("Basis", "mat_nil") =>
adamc@709 1285 let
adamc@709 1286 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1287 val stringE = (L'.EPrim (Prim.String ""), loc)
adamc@709 1288 in
adamc@709 1289 ((L'.ERecord [("1", stringE, string),
adamc@709 1290 ("2", stringE, string)], loc), fm)
adamc@709 1291 end
adamc@709 1292 | L.ECApp (
adamc@709 1293 (L.ECApp (
adamc@709 1294 (L.ECApp (
adamc@709 1295 (L.ECApp (
adamc@709 1296 (L.ECApp (
adamc@712 1297 (L.ECApp (
adamc@712 1298 (L.EFfi ("Basis", "mat_cons"), _),
adamc@712 1299 _), _),
adamc@709 1300 _), _),
adamc@709 1301 _), _),
adamc@709 1302 _), _),
adamc@709 1303 (L.CName nm1, _)), _),
adamc@709 1304 (L.CName nm2, _)) =>
adamc@709 1305 let
adamc@709 1306 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1307 val mat = (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 1308 in
adamc@712 1309 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun (mat, mat), loc),
adamc@712 1310 (L'.EAbs ("m", mat, mat,
adamc@712 1311 (L'.ECase ((L'.EField ((L'.ERel 0, loc), "1"), loc),
adamc@712 1312 [((L'.PPrim (Prim.String ""), loc),
adamc@712 1313 (L'.ERecord [("1", (L'.EPrim (Prim.String ("uw_" ^ nm1)),
adamc@712 1314 loc), string),
adamc@712 1315 ("2", (L'.EPrim (Prim.String ("uw_" ^ nm2)),
adamc@712 1316 loc), string)], loc)),
adamc@712 1317 ((L'.PWild, loc),
adamc@712 1318 (L'.ERecord [("1", (L'.EStrcat (
adamc@712 1319 (L'.EPrim (Prim.String ("uw_" ^ nm1 ^ ", ")),
adamc@712 1320 loc),
adamc@712 1321 (L'.EField ((L'.ERel 0, loc), "1"), loc)),
adamc@712 1322 loc), string),
adamc@712 1323 ("2", (L'.EStrcat (
adamc@712 1324 (L'.EPrim (Prim.String ("uw_" ^ nm2 ^ ", ")), loc),
adamc@712 1325 (L'.EField ((L'.ERel 0, loc), "2"), loc)),
adamc@712 1326 loc), string)],
adamc@712 1327 loc))],
adamc@712 1328 {disc = string,
adamc@712 1329 result = mat}), loc)), loc)), loc),
adamc@709 1330 fm)
adamc@709 1331 end
adamc@709 1332
adamc@709 1333 | L.ECApp ((L.EFfi ("Basis", "restrict"), _), _) => ((L'.EPrim (Prim.String "RESTRICT"), loc), fm)
adamc@709 1334 | L.ECApp ((L.EFfi ("Basis", "cascade"), _), _) => ((L'.EPrim (Prim.String "CASCADE"), loc), fm)
adamc@709 1335 | L.ECApp ((L.EFfi ("Basis", "no_action"), _), _) => ((L'.EPrim (Prim.String "NO ACTION"), loc), fm)
adamc@709 1336 | L.ECApp ((L.EFfi ("Basis", "set_null"), _), _) => ((L'.EPrim (Prim.String "SET NULL"), loc), fm)
adamc@709 1337
adamc@709 1338 | L.ECApp (
adamc@709 1339 (L.ECApp (
adamc@709 1340 (L.ECApp (
adamc@709 1341 (L.ECApp (
adamc@709 1342 (L.ECApp (
adamc@709 1343 (L.ECApp (
adamc@709 1344 (L.ECApp (
adamc@709 1345 (L.ECApp (
adamc@709 1346 (L.EFfi ("Basis", "foreign_key"), _),
adamc@709 1347 _), _),
adamc@709 1348 _), _),
adamc@709 1349 _), _),
adamc@709 1350 _), _),
adamc@709 1351 _), _),
adamc@709 1352 _), _),
adamc@709 1353 _), _),
adamc@709 1354 _) =>
adamc@709 1355 let
adamc@709 1356 val unit = (L'.TRecord [], loc)
adamc@709 1357 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1358 val mat = (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 1359 val recd = (L'.TRecord [("OnDelete", string),
adamc@709 1360 ("OnUpdate", string)], loc)
adamc@709 1361
adamc@709 1362 fun strcat [] = raise Fail "Monoize.strcat"
adamc@709 1363 | strcat [e] = e
adamc@709 1364 | strcat (e1 :: es) = (L'.EStrcat (e1, strcat es), loc)
adamc@709 1365
adamc@709 1366 fun prop (fd, kw) =
adamc@709 1367 (L'.ECase ((L'.EField ((L'.ERel 0, loc), fd), loc),
adamc@709 1368 [((L'.PPrim (Prim.String "NO ACTION"), loc),
adamc@709 1369 (L'.EPrim (Prim.String ""), loc)),
adamc@709 1370 ((L'.PWild, loc),
adamc@709 1371 strcat [(L'.EPrim (Prim.String (" ON " ^ kw ^ " ")), loc),
adamc@709 1372 (L'.EField ((L'.ERel 0, loc), fd), loc)])],
adamc@709 1373 {disc = string,
adamc@709 1374 result = string}), loc)
adamc@709 1375 in
adamc@709 1376 ((L'.EAbs ("m", mat, (L'.TFun (string, (L'.TFun (recd, string), loc)), loc),
adamc@709 1377 (L'.EAbs ("tab", string, (L'.TFun (recd, string), loc),
adamc@709 1378 (L'.EAbs ("pr", recd, string,
adamc@709 1379 strcat [(L'.EPrim (Prim.String "FOREIGN KEY ("), loc),
adamc@709 1380 (L'.EField ((L'.ERel 2, loc), "1"), loc),
adamc@709 1381 (L'.EPrim (Prim.String ") REFERENCES "), loc),
adamc@709 1382 (L'.ERel 1, loc),
adamc@709 1383 (L'.EPrim (Prim.String " ("), loc),
adamc@709 1384 (L'.EField ((L'.ERel 2, loc), "2"), loc),
adamc@709 1385 (L'.EPrim (Prim.String ")"), loc),
adamc@709 1386 prop ("OnDelete", "DELETE"),
adamc@709 1387 prop ("OnUpdate", "UPDATE")]), loc)), loc)), loc),
adamc@709 1388 fm)
adamc@709 1389 end
adamc@709 1390
adamc@714 1391 | L.ECApp ((L.EFfi ("Basis", "check"), _), _) =>
adamc@714 1392 let
adamc@714 1393 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@714 1394 in
adamc@714 1395 ((L'.EAbs ("e", string, string,
adamc@714 1396 (L'.EStrcat ((L'.EPrim (Prim.String "CHECK "), loc),
adamc@714 1397 (L'.EFfiApp ("Basis", "checkString",
adamc@714 1398 [(L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@714 1399 fm)
adamc@714 1400 end
adamc@714 1401
adamc@307 1402 | L.EFfiApp ("Basis", "dml", [e]) =>
adamc@307 1403 let
adamc@307 1404 val (e, fm) = monoExp (env, st, fm) e
adamc@307 1405 in
adamc@468 1406 ((L'.EDml e, loc),
adamc@307 1407 fm)
adamc@307 1408 end
adamc@308 1409
adamc@705 1410 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "insert"), _), fields), _), _) =>
adamc@307 1411 (case monoType env (L.TRecord fields, loc) of
adamc@307 1412 (L'.TRecord fields, _) =>
adamc@307 1413 let
adamc@307 1414 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@307 1415 val fields = map (fn (x, _) => (x, s)) fields
adamc@307 1416 val rt = (L'.TRecord fields, loc)
adamc@307 1417 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@307 1418 in
adamc@307 1419 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
adamc@307 1420 (L'.EAbs ("fs", rt, s,
adamc@598 1421 strcat [sc "INSERT INTO ",
adamc@598 1422 (L'.ERel 1, loc),
adamc@598 1423 sc " (",
adamc@598 1424 strcatComma (map (fn (x, _) => sc ("uw_" ^ x)) fields),
adamc@598 1425 sc ") VALUES (",
adamc@598 1426 strcatComma (map (fn (x, _) =>
adamc@598 1427 (L'.EField ((L'.ERel 0, loc),
adamc@598 1428 x), loc)) fields),
adamc@598 1429 sc ")"]), loc)), loc),
adamc@307 1430 fm)
adamc@307 1431 end
adamc@307 1432 | _ => poly ())
adamc@307 1433
adamc@705 1434 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), _), _), changed) =>
adamc@308 1435 (case monoType env (L.TRecord changed, loc) of
adamc@308 1436 (L'.TRecord changed, _) =>
adamc@308 1437 let
adamc@308 1438 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@308 1439 val changed = map (fn (x, _) => (x, s)) changed
adamc@308 1440 val rt = (L'.TRecord changed, loc)
adamc@308 1441 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@308 1442 in
adamc@308 1443 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@308 1444 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@308 1445 (L'.EAbs ("e", s, s,
adamc@598 1446 strcat [sc "UPDATE ",
adamc@598 1447 (L'.ERel 1, loc),
adamc@598 1448 sc " AS T SET ",
adamc@598 1449 strcatComma (map (fn (x, _) =>
adamc@598 1450 strcat [sc ("uw_" ^ x
adamc@598 1451 ^ " = "),
adamc@598 1452 (L'.EField
adamc@598 1453 ((L'.ERel 2,
adamc@598 1454 loc),
adamc@598 1455 x), loc)])
adamc@598 1456 changed),
adamc@598 1457 sc " WHERE ",
adamc@598 1458 (L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@308 1459 fm)
adamc@308 1460 end
adamc@308 1461 | _ => poly ())
adamc@308 1462
adamc@705 1463 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "delete"), _), _), _), _) =>
adamc@309 1464 let
adamc@309 1465 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@309 1466 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@309 1467 in
adamc@309 1468 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@309 1469 (L'.EAbs ("e", s, s,
adamc@598 1470 strcat [sc "DELETE FROM ",
adamc@598 1471 (L'.ERel 1, loc),
adamc@598 1472 sc " AS T WHERE ",
adamc@598 1473 (L'.ERel 0, loc)]), loc)), loc),
adamc@309 1474 fm)
adamc@309 1475 end
adamc@309 1476
adamc@252 1477 | L.ECApp (
adamc@252 1478 (L.ECApp (
adamc@252 1479 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 1480 exps), _),
adamc@252 1481 state) =>
adamc@252 1482 (case monoType env (L.TRecord exps, loc) of
adamc@252 1483 (L'.TRecord exps, _) =>
adamc@252 1484 let
adamc@252 1485 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1486 (case monoType env (L.TRecord xts, loc) of
adamc@252 1487 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1488 | _ => NONE)
adamc@252 1489 | _ => NONE) tables
adamc@252 1490 in
adamc@252 1491 if List.exists (fn x => x = NONE) tables then
adamc@252 1492 poly ()
adamc@252 1493 else
adamc@252 1494 let
adamc@252 1495 val tables = List.mapPartial (fn x => x) tables
adamc@252 1496 val state = monoType env state
adamc@252 1497 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1498 val un = (L'.TRecord [], loc)
adamc@252 1499
adamc@252 1500 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 1501 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 1502 (L'.TFun (state,
adamc@252 1503 (L'.TFun (un, state), loc)),
adamc@252 1504 loc)), loc)
adamc@252 1505
adamc@267 1506 val body' = (L'.EApp (
adamc@267 1507 (L'.EApp (
adamc@267 1508 (L'.EApp ((L'.ERel 4, loc),
adamc@267 1509 (L'.ERel 1, loc)), loc),
adamc@267 1510 (L'.ERel 0, loc)), loc),
adamc@267 1511 (L'.ERecord [], loc)), loc)
adamc@252 1512
adamc@252 1513 val body = (L'.EQuery {exps = exps,
adamc@252 1514 tables = tables,
adamc@252 1515 state = state,
adamc@252 1516 query = (L'.ERel 3, loc),
adamc@252 1517 body = body',
adamc@252 1518 initial = (L'.ERel 1, loc)},
adamc@252 1519 loc)
adamc@252 1520 in
adamc@252 1521 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 1522 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 1523 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 1524 (L'.EAbs ("_", un, state,
adamc@252 1525 body), loc)), loc)), loc)), loc), fm)
adamc@252 1526 end
adamc@252 1527 end
adamc@252 1528 | _ => poly ())
adamc@252 1529
adamc@252 1530 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 1531 let
adamc@252 1532 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1533 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1534 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1535 in
adamc@252 1536 ((L'.EAbs ("r",
adamc@252 1537 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 1538 s,
adamc@598 1539 strcat [gf "Rows",
adamc@598 1540 (L'.ECase (gf "OrderBy",
adamc@598 1541 [((L'.PPrim (Prim.String ""), loc), sc ""),
adamc@598 1542 ((L'.PWild, loc),
adamc@598 1543 strcat [sc " ORDER BY ",
adamc@598 1544 gf "OrderBy"])],
adamc@598 1545 {disc = s, result = s}), loc),
adamc@598 1546 gf "Limit",
adamc@598 1547 gf "Offset"]), loc), fm)
adamc@252 1548 end
adamc@252 1549
adamc@252 1550 | L.ECApp (
adamc@252 1551 (L.ECApp (
adamc@252 1552 (L.ECApp (
adamc@252 1553 (L.ECApp (
adamc@252 1554 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 1555 (L.CRecord (_, tables), _)), _),
adamc@252 1556 (L.CRecord (_, grouped), _)), _),
adamc@252 1557 (L.CRecord (_, stables), _)), _),
adamc@252 1558 sexps) =>
adamc@252 1559 let
adamc@252 1560 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1561 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1562 val un = (L'.TRecord [], loc)
adamc@252 1563 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1564
adamc@252 1565 fun doTables tables =
adamc@252 1566 let
adamc@252 1567 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1568 (case monoType env (L.TRecord xts, loc) of
adamc@252 1569 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1570 | _ => NONE)
adamc@252 1571 | _ => NONE) tables
adamc@252 1572 in
adamc@252 1573 if List.exists (fn x => x = NONE) tables then
adamc@252 1574 NONE
adamc@252 1575 else
adamc@260 1576 let
adamc@260 1577 val tables = List.mapPartial (fn x => x) tables
adamc@260 1578 val tables = ListMergeSort.sort
adamc@260 1579 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1580 tables
adamc@260 1581 val tables = map (fn (x, xts) =>
adamc@260 1582 (x, ListMergeSort.sort
adamc@260 1583 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1584 xts)) tables
adamc@260 1585 in
adamc@260 1586 SOME tables
adamc@260 1587 end
adamc@252 1588 end
adamc@252 1589 in
adamc@252 1590 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 1591 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@441 1592 let
adamc@441 1593 val sexps = ListMergeSort.sort
adamc@441 1594 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) sexps
adamc@441 1595 in
adamc@441 1596 ((L'.EAbs ("r",
adamc@748 1597 (L'.TRecord [("From", s),
adamc@441 1598 ("Where", s),
adamc@441 1599 ("GroupBy", un),
adamc@441 1600 ("Having", s),
adamc@441 1601 ("SelectFields", un),
adamc@441 1602 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@441 1603 loc),
adamc@441 1604 s,
adamc@598 1605 strcat [sc "SELECT ",
adamc@598 1606 strcatComma (map (fn (x, t) =>
adamc@598 1607 strcat [
adamc@598 1608 (L'.EField (gf "SelectExps", x), loc),
adamc@598 1609 sc (" AS _" ^ x)
adamc@598 1610 ]) sexps
adamc@598 1611 @ map (fn (x, xts) =>
adamc@598 1612 strcatComma
adamc@598 1613 (map (fn (x', _) =>
adamc@598 1614 sc (x ^ ".uw_" ^ x'))
adamc@598 1615 xts)) stables),
adamc@598 1616 sc " FROM ",
adamc@748 1617 gf "From",
adamc@598 1618 (L'.ECase (gf "Where",
adamc@598 1619 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@598 1620 sc ""),
adamc@598 1621 ((L'.PWild, loc),
adamc@598 1622 strcat [sc " WHERE ", gf "Where"])],
adamc@598 1623 {disc = s,
adamc@598 1624 result = s}), loc),
adamc@598 1625
adamc@598 1626 if List.all (fn (x, xts) =>
adamc@598 1627 case List.find (fn (x', _) => x' = x) grouped of
adamc@598 1628 NONE => List.null xts
adamc@598 1629 | SOME (_, xts') =>
adamc@598 1630 List.all (fn (x, _) =>
adamc@598 1631 List.exists (fn (x', _) => x' = x)
adamc@598 1632 xts') xts) tables then
adamc@598 1633 sc ""
adamc@598 1634 else
adamc@598 1635 strcat [
adamc@598 1636 sc " GROUP BY ",
adamc@598 1637 strcatComma (map (fn (x, xts) =>
adamc@598 1638 strcatComma
adamc@598 1639 (map (fn (x', _) =>
adamc@598 1640 sc (x ^ ".uw_" ^ x'))
adamc@598 1641 xts)) grouped)
adamc@598 1642 ],
adamc@259 1643
adamc@598 1644 (L'.ECase (gf "Having",
adamc@598 1645 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@598 1646 sc ""),
adamc@598 1647 ((L'.PWild, loc),
adamc@598 1648 strcat [sc " HAVING ", gf "Having"])],
adamc@598 1649 {disc = s,
adamc@598 1650 result = s}), loc)
adamc@441 1651 ]), loc),
adamc@441 1652 fm)
adamc@441 1653 end
adamc@252 1654 | _ => poly ()
adamc@252 1655 end
adamc@252 1656
adamc@252 1657 | L.ECApp (
adamc@252 1658 (L.ECApp (
adamc@252 1659 (L.ECApp (
adamc@252 1660 (L.ECApp (
adamc@252 1661 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 1662 _), _),
adamc@252 1663 _), _),
adamc@252 1664 _), _),
adamc@252 1665 t) =>
adamc@252 1666 let
adamc@252 1667 val t = monoType env t
adamc@252 1668 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1669 in
adamc@252 1670 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 1671 (L'.ERel 0, loc)), loc), fm)
adamc@252 1672 end
adamc@252 1673
adamc@253 1674 | L.EFfi ("Basis", "sql_int") =>
adamc@253 1675 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1676 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1677 fm)
adamc@253 1678 | L.EFfi ("Basis", "sql_float") =>
adamc@253 1679 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1680 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1681 fm)
adamc@253 1682 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 1683 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1684 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1685 fm)
adamc@253 1686 | L.EFfi ("Basis", "sql_string") =>
adamc@253 1687 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1688 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1689 fm)
adamc@439 1690 | L.EFfi ("Basis", "sql_time") =>
adamc@439 1691 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@439 1692 (L'.EFfiApp ("Basis", "sqlifyTime", [(L'.ERel 0, loc)]), loc)), loc),
adamc@439 1693 fm)
adamc@737 1694 | L.EFfi ("Basis", "sql_blob") =>
adamc@737 1695 ((L'.EAbs ("x", (L'.TFfi ("Basis", "blob"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@737 1696 (L'.EFfiApp ("Basis", "sqlifyBlob", [(L'.ERel 0, loc)]), loc)), loc),
adamc@737 1697 fm)
adamc@678 1698 | L.ECApp ((L.EFfi ("Basis", "sql_channel"), _), _) =>
adamc@678 1699 ((L'.EAbs ("x", (L'.TFfi ("Basis", "channel"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@678 1700 (L'.EFfiApp ("Basis", "sqlifyChannel", [(L'.ERel 0, loc)]), loc)), loc),
adamc@678 1701 fm)
adamc@682 1702 | L.EFfi ("Basis", "sql_client") =>
adamc@682 1703 ((L'.EAbs ("x", (L'.TFfi ("Basis", "client"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@682 1704 (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)), loc),
adamc@682 1705 fm)
adamc@676 1706 | L.ECApp ((L.EFfi ("Basis", "sql_prim"), _), t) =>
adamc@676 1707 let
adamc@676 1708 val t = monoType env t
adamc@676 1709 val tf = (L'.TFun (t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@676 1710 in
adamc@676 1711 ((L'.EAbs ("f", tf, tf, (L'.ERel 0, loc)), loc),
adamc@676 1712 fm)
adamc@676 1713 end
adamc@676 1714 | L.ECApp ((L.EFfi ("Basis", "sql_option_prim"), _), t) =>
adamc@676 1715 let
adamc@676 1716 val t = monoType env t
adamc@676 1717 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@676 1718 in
adamc@676 1719 ((L'.EAbs ("f",
adamc@676 1720 (L'.TFun (t, s), loc),
adamc@676 1721 (L'.TFun ((L'.TOption t, loc), s), loc),
adamc@676 1722 (L'.EAbs ("x",
adamc@676 1723 (L'.TOption t, loc),
adamc@676 1724 s,
adamc@676 1725 (L'.ECase ((L'.ERel 0, loc),
adamc@676 1726 [((L'.PNone t, loc),
adamc@676 1727 (L'.EPrim (Prim.String "NULL"), loc)),
adamc@676 1728 ((L'.PSome (t, (L'.PVar ("y", t), loc)), loc),
adamc@676 1729 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc))],
adamc@676 1730 {disc = (L'.TOption t, loc),
adamc@676 1731 result = s}), loc)), loc)), loc),
adamc@676 1732 fm)
adamc@676 1733 end
adamc@253 1734
adamc@750 1735 | L.ECApp ((L.EFfi ("Basis", "nullify_option"), _), _) =>
adamc@750 1736 ((L'.ERecord [], loc), fm)
adamc@750 1737 | L.ECApp ((L.EFfi ("Basis", "nullify_prim"), _), _) =>
adamc@750 1738 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@750 1739 (L'.ERecord [], loc)), loc),
adamc@750 1740 fm)
adamc@750 1741
adamc@252 1742 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 1743 ((L'.ERecord [], loc), fm)
adamc@252 1744 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 1745 ((L'.ERecord [], loc), fm)
adamc@252 1746
adamc@753 1747 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "fieldsOf_table"), _), _), _), _) =>
adamc@753 1748 ((L'.ERecord [], loc), fm)
adamc@753 1749 | L.ECApp ((L.EFfi ("Basis", "fieldsOf_view"), _), _) =>
adamc@753 1750 ((L'.ERecord [], loc), fm)
adamc@753 1751
adamc@753 1752 | L.ECApp ((L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_table"), _), _), _), _), _), _), _),
adamc@753 1753 (L.CName name, _)) =>
adamc@748 1754 let
adamc@748 1755 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@748 1756 in
adamc@748 1757 ((L'.EAbs ("tab", s, s,
adamc@748 1758 strcat [(L'.ERel 0, loc),
adamc@748 1759 (L'.EPrim (Prim.String (" AS " ^ name)), loc)]), loc),
adamc@748 1760 fm)
adamc@748 1761 end
adamc@748 1762 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_comma"), _), _), _), _) =>
adamc@748 1763 let
adamc@748 1764 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@748 1765 in
adamc@748 1766 ((L'.EAbs ("tab1", s, (L'.TFun (s, s), loc),
adamc@748 1767 (L'.EAbs ("tab2", s, s,
adamc@748 1768 strcat [(L'.ERel 1, loc),
adamc@748 1769 (L'.EPrim (Prim.String ", "), loc),
adamc@748 1770 (L'.ERel 0, loc)]), loc)), loc),
adamc@748 1771 fm)
adamc@748 1772 end
adamc@749 1773 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_inner_join"), _), _), _), _) =>
adamc@749 1774 let
adamc@749 1775 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@749 1776 in
adamc@749 1777 ((L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@749 1778 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@749 1779 (L'.EAbs ("on", s, s,
adamc@749 1780 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@749 1781 (L'.ERel 2, loc),
adamc@749 1782 (L'.EPrim (Prim.String " JOIN "), loc),
adamc@749 1783 (L'.ERel 1, loc),
adamc@749 1784 (L'.EPrim (Prim.String " ON "), loc),
adamc@749 1785 (L'.ERel 0, loc),
adamc@749 1786 (L'.EPrim (Prim.String ")"), loc)]), loc)), loc)), loc),
adamc@749 1787 fm)
adamc@749 1788 end
adamc@750 1789 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_left_join"), _), _), _), (L.CRecord (_, right), _)) =>
adamc@750 1790 let
adamc@750 1791 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@750 1792 in
adamc@750 1793 ((L'.EAbs ("_", outerRec right,
adamc@750 1794 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@750 1795 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@750 1796 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@750 1797 (L'.EAbs ("on", s, s,
adamc@750 1798 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@750 1799 (L'.ERel 2, loc),
adamc@750 1800 (L'.EPrim (Prim.String " LEFT JOIN "), loc),
adamc@750 1801 (L'.ERel 1, loc),
adamc@750 1802 (L'.EPrim (Prim.String " ON "), loc),
adamc@750 1803 (L'.ERel 0, loc),
adamc@750 1804 (L'.EPrim (Prim.String ")"), loc)]),
adamc@750 1805 loc)), loc)), loc)), loc),
adamc@750 1806 fm)
adamc@750 1807 end
adamc@751 1808 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_right_join"), _), (L.CRecord (_, left), _)), _), _) =>
adamc@751 1809 let
adamc@751 1810 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@751 1811 in
adamc@751 1812 ((L'.EAbs ("_", outerRec left,
adamc@751 1813 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@751 1814 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@751 1815 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@751 1816 (L'.EAbs ("on", s, s,
adamc@751 1817 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@751 1818 (L'.ERel 2, loc),
adamc@751 1819 (L'.EPrim (Prim.String " RIGHT JOIN "), loc),
adamc@751 1820 (L'.ERel 1, loc),
adamc@751 1821 (L'.EPrim (Prim.String " ON "), loc),
adamc@751 1822 (L'.ERel 0, loc),
adamc@751 1823 (L'.EPrim (Prim.String ")"), loc)]),
adamc@751 1824 loc)), loc)), loc)), loc),
adamc@751 1825 fm)
adamc@751 1826 end
adamc@751 1827 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_full_join"), _), (L.CRecord (_, left), _)), _),
adamc@751 1828 (L.CRecord (_, right), _)) =>
adamc@751 1829 let
adamc@751 1830 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@751 1831 in
adamc@751 1832 ((L'.EAbs ("_", outerRec (left @ right),
adamc@751 1833 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@751 1834 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@751 1835 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@751 1836 (L'.EAbs ("on", s, s,
adamc@751 1837 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@751 1838 (L'.ERel 2, loc),
adamc@751 1839 (L'.EPrim (Prim.String " FULL JOIN "), loc),
adamc@751 1840 (L'.ERel 1, loc),
adamc@751 1841 (L'.EPrim (Prim.String " ON "), loc),
adamc@751 1842 (L'.ERel 0, loc),
adamc@751 1843 (L'.EPrim (Prim.String ")"), loc)]),
adamc@751 1844 loc)), loc)), loc)), loc),
adamc@751 1845 fm)
adamc@751 1846 end
adamc@748 1847
adamc@252 1848 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 1849 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@261 1850 | L.ECApp (
adamc@261 1851 (L.ECApp (
adamc@261 1852 (L.ECApp (
adamc@261 1853 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adamc@261 1854 _), _),
adamc@261 1855 _), _),
adamc@261 1856 _) =>
adamc@261 1857 let
adamc@261 1858 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 1859 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@261 1860 in
adamc@268 1861 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@268 1862 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adamc@268 1863 (L'.EAbs ("e2", s, s,
adamc@268 1864 (L'.ECase ((L'.ERel 0, loc),
adamc@268 1865 [((L'.PPrim (Prim.String ""), loc),
adamc@598 1866 strcat [(L'.ERel 2, loc),
adamc@598 1867 (L'.ERel 1, loc)]),
adamc@268 1868 ((L'.PWild, loc),
adamc@598 1869 strcat [(L'.ERel 2, loc),
adamc@598 1870 (L'.ERel 1, loc),
adamc@598 1871 sc ", ",
adamc@598 1872 (L'.ERel 0, loc)])],
adamc@268 1873 {disc = s, result = s}), loc)), loc)), loc)), loc),
adamc@261 1874 fm)
adamc@261 1875 end
adamc@252 1876
adamc@252 1877 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 1878 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@262 1879 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
adamc@262 1880 let
adamc@262 1881 val (e, fm) = monoExp (env, st, fm) e
adamc@262 1882 in
adamc@598 1883 (strcat [
adamc@262 1884 (L'.EPrim (Prim.String " LIMIT "), loc),
adamc@262 1885 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@262 1886 ],
adamc@262 1887 fm)
adamc@262 1888 end
adamc@262 1889
adamc@252 1890 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 1891 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@263 1892 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
adamc@263 1893 let
adamc@263 1894 val (e, fm) = monoExp (env, st, fm) e
adamc@263 1895 in
adamc@598 1896 (strcat [
adamc@263 1897 (L'.EPrim (Prim.String " OFFSET "), loc),
adamc@263 1898 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@263 1899 ],
adamc@263 1900 fm)
adamc@263 1901 end
adamc@253 1902
adamc@559 1903 | L.ECApp ((L.EFfi ("Basis", "sql_eq"), _), _) =>
adamc@253 1904 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@559 1905 | L.ECApp ((L.EFfi ("Basis", "sql_ne"), _), _) =>
adamc@253 1906 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@559 1907 | L.ECApp ((L.EFfi ("Basis", "sql_lt"), _), _) =>
adamc@253 1908 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@559 1909 | L.ECApp ((L.EFfi ("Basis", "sql_le"), _), _) =>
adamc@253 1910 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@559 1911 | L.ECApp ((L.EFfi ("Basis", "sql_gt"), _), _) =>
adamc@253 1912 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@559 1913 | L.ECApp ((L.EFfi ("Basis", "sql_ge"), _), _) =>
adamc@253 1914 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 1915
adamc@559 1916 | L.ECApp ((L.EFfi ("Basis", "sql_plus"), _), _) =>
adamc@559 1917 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1918 (L'.EPrim (Prim.String "+"), loc)), loc), fm)
adamc@559 1919 | L.ECApp ((L.EFfi ("Basis", "sql_minus"), _), _) =>
adamc@559 1920 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1921 (L'.EPrim (Prim.String "-"), loc)), loc), fm)
adamc@559 1922 | L.ECApp ((L.EFfi ("Basis", "sql_times"), _), _) =>
adamc@559 1923 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1924 (L'.EPrim (Prim.String "*"), loc)), loc), fm)
adamc@559 1925 | L.ECApp ((L.EFfi ("Basis", "sql_div"), _), _) =>
adamc@559 1926 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1927 (L'.EPrim (Prim.String "/"), loc)), loc), fm)
adamc@559 1928 | L.EFfi ("Basis", "sql_mod") =>
adamc@559 1929 ((L'.EPrim (Prim.String "%"), loc), fm)
adamc@559 1930
adamc@253 1931 | L.ECApp (
adamc@253 1932 (L.ECApp (
adamc@253 1933 (L.ECApp (
adamc@253 1934 (L.ECApp (
adamc@254 1935 (L.ECApp (
adamc@264 1936 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 1937 _), _),
adamc@264 1938 _), _),
adamc@264 1939 _), _),
adamc@264 1940 _), _),
adamc@264 1941 _) =>
adamc@264 1942 let
adamc@264 1943 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 1944 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 1945 in
adamc@264 1946 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 1947 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@598 1948 strcat [sc "(",
adamc@598 1949 (L'.ERel 1, loc),
adamc@598 1950 sc " ",
adamc@598 1951 (L'.ERel 0, loc),
adamc@598 1952 sc ")"]), loc)), loc),
adamc@264 1953 fm)
adamc@264 1954 end
adamc@264 1955 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@559 1956 | L.ECApp ((L.EFfi ("Basis", "sql_neg"), _), _) =>
adamc@559 1957 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1958 (L'.EPrim (Prim.String "-"), loc)), loc), fm)
adamc@264 1959
adamc@264 1960 | L.ECApp (
adamc@264 1961 (L.ECApp (
adamc@264 1962 (L.ECApp (
adamc@264 1963 (L.ECApp (
adamc@264 1964 (L.ECApp (
adamc@254 1965 (L.ECApp (
adamc@254 1966 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 1967 _), _),
adamc@254 1968 _), _),
adamc@254 1969 _), _),
adamc@254 1970 _), _),
adamc@254 1971 _), _),
adamc@254 1972 _) =>
adamc@254 1973 let
adamc@254 1974 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 1975 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 1976 in
adamc@254 1977 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 1978 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 1979 (L'.EAbs ("e2", s, s,
adamc@598 1980 strcat [sc "(",
adamc@598 1981 (L'.ERel 1, loc),
adamc@598 1982 sc " ",
adamc@598 1983 (L'.ERel 2, loc),
adamc@598 1984 sc " ",
adamc@598 1985 (L'.ERel 0, loc),
adamc@598 1986 sc ")"]), loc)), loc)), loc),
adamc@254 1987 fm)
adamc@254 1988 end
adamc@254 1989 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 1990 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 1991
adamc@254 1992 | L.ECApp (
adamc@254 1993 (L.ECApp (
adamc@254 1994 (L.ECApp (
adamc@254 1995 (L.ECApp (
adamc@253 1996 (L.ECApp (
adamc@253 1997 (L.ECApp (
adamc@253 1998 (L.ECApp (
adamc@253 1999 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 2000 _), _),
adamc@253 2001 _), _),
adamc@253 2002 _), _),
adamc@253 2003 _), _),
adamc@253 2004 _), _),
adamc@253 2005 (L.CName tab, _)), _),
adamc@311 2006 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
adamc@260 2007
adamc@260 2008 | L.ECApp (
adamc@260 2009 (L.ECApp (
adamc@260 2010 (L.ECApp (
adamc@260 2011 (L.ECApp (
adamc@261 2012 (L.ECApp (
adamc@261 2013 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 2014 _), _),
adamc@261 2015 _), _),
adamc@261 2016 _), _),
adamc@261 2017 _), _),
adamc@261 2018 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 2019
adamc@261 2020 | L.ECApp (
adamc@261 2021 (L.ECApp (
adamc@261 2022 (L.ECApp (
adamc@261 2023 (L.ECApp (
adamc@260 2024 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 2025 _), _),
adamc@260 2026 _), _),
adamc@260 2027 _), _),
adamc@260 2028 _) =>
adamc@260 2029 let
adamc@260 2030 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 2031 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 2032 in
adamc@260 2033 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 2034 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 2035 (L'.EAbs ("e2", s, s,
adamc@598 2036 strcat [sc "((",
adamc@598 2037 (L'.ERel 1, loc),
adamc@598 2038 sc ") ",
adamc@598 2039 (L'.ERel 2, loc),
adamc@598 2040 sc " (",
adamc@598 2041 (L'.ERel 0, loc),
adamc@598 2042 sc "))"]), loc)), loc)), loc),
adamc@260 2043 fm)
adamc@260 2044 end
adamc@260 2045
adamc@260 2046 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 2047 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 2048 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 2049
adamc@265 2050 | L.ECApp (
adamc@265 2051 (L.ECApp (
adamc@265 2052 (L.ECApp (
adamc@265 2053 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 2054 _), _),
adamc@265 2055 _), _),
adamc@544 2056 _) => ((L'.EPrim (Prim.String "COUNT(*)"), loc),
adamc@265 2057 fm)
adamc@266 2058
adamc@266 2059 | L.ECApp (
adamc@266 2060 (L.ECApp (
adamc@266 2061 (L.ECApp (
adamc@266 2062 (L.ECApp (
adamc@266 2063 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 2064 _), _),
adamc@266 2065 _), _),
adamc@266 2066 _), _),
adamc@266 2067 _) =>
adamc@266 2068 let
adamc@266 2069 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 2070 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 2071 in
adamc@266 2072 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 2073 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@598 2074 strcat [(L'.ERel 1, loc),
adamc@598 2075 sc "(",
adamc@598 2076 (L'.ERel 0, loc),
adamc@598 2077 sc ")"]), loc)), loc),
adamc@266 2078 fm)
adamc@266 2079 end
adamc@266 2080
adamc@266 2081 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 2082 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 2083 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 2084 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2085 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 2086 fm)
adamc@266 2087 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 2088 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2089 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 2090 fm)
adamc@266 2091
adamc@559 2092 | L.EFfi ("Basis", "sql_arith_int") => ((L'.ERecord [], loc), fm)
adamc@559 2093 | L.EFfi ("Basis", "sql_arith_float") => ((L'.ERecord [], loc), fm)
adamc@559 2094
adamc@266 2095 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 2096 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 2097 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 2098 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 2099 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2100 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 2101 fm)
adamc@266 2102 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 2103 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2104 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 2105 fm)
adamc@266 2106
adamc@268 2107 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 2108 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 2109
adamc@441 2110 | L.ECApp (
adamc@441 2111 (L.ECApp (
adamc@441 2112 (L.ECApp (
adamc@441 2113 (L.ECApp (
adamc@441 2114 (L.EFfi ("Basis", "sql_nfunc"), _),
adamc@441 2115 _), _),
adamc@441 2116 _), _),
adamc@441 2117 _), _),
adamc@441 2118 _) =>
adamc@441 2119 let
adamc@441 2120 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@441 2121 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@441 2122 in
adamc@441 2123 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc),
adamc@441 2124 fm)
adamc@441 2125 end
adamc@441 2126 | L.EFfi ("Basis", "sql_current_timestamp") => ((L'.EPrim (Prim.String "CURRENT_TIMESTAMP"), loc), fm)
adamc@441 2127
adamc@746 2128 | L.ECApp (
adamc@746 2129 (L.ECApp (
adamc@746 2130 (L.ECApp (
adamc@746 2131 (L.ECApp (
adamc@746 2132 (L.ECApp (
adamc@746 2133 (L.EFfi ("Basis", "sql_ufunc"), _),
adamc@746 2134 _), _),
adamc@746 2135 _), _),
adamc@746 2136 _), _),
adamc@746 2137 _), _),
adamc@746 2138 _) =>
adamc@746 2139 let
adamc@746 2140 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@746 2141 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@746 2142 in
adamc@746 2143 ((L'.EAbs ("f", s, (L'.TFun (s, s), loc),
adamc@746 2144 (L'.EAbs ("x", s, s,
adamc@746 2145 strcat [(L'.ERel 1, loc),
adamc@746 2146 sc "(",
adamc@746 2147 (L'.ERel 0, loc),
adamc@746 2148 sc ")"]), loc)), loc),
adamc@746 2149 fm)
adamc@746 2150 end
adamc@746 2151 | L.EFfi ("Basis", "sql_octet_length") => ((L'.EPrim (Prim.String "octet_length"), loc), fm)
adamc@746 2152
adamc@470 2153 | (L.ECApp (
adamc@470 2154 (L.ECApp (
adamc@470 2155 (L.ECApp (
adamc@470 2156 (L.ECApp (
adamc@470 2157 (L.EFfi ("Basis", "sql_is_null"), _), _),
adamc@470 2158 _), _),
adamc@470 2159 _), _),
adamc@470 2160 _), _)) =>
adamc@470 2161 let
adamc@470 2162 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@470 2163 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@470 2164 in
adamc@470 2165 ((L'.EAbs ("s", s, s,
adamc@598 2166 strcat [sc "(",
adamc@598 2167 (L'.ERel 0, loc),
adamc@598 2168 sc " IS NULL)"]), loc),
adamc@470 2169 fm)
adamc@470 2170 end
adamc@470 2171
adamc@338 2172 | L.EFfiApp ("Basis", "nextval", [e]) =>
adamc@338 2173 let
adamc@338 2174 val (e, fm) = monoExp (env, st, fm) e
adamc@338 2175 in
adamc@465 2176 ((L'.ENextval e, loc), fm)
adamc@338 2177 end
adamc@338 2178
adamc@139 2179 | L.EApp (
adamc@139 2180 (L.ECApp (
adamc@720 2181 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 2182 _), _),
adamc@179 2183 se) =>
adamc@179 2184 let
adamc@179 2185 val (se, fm) = monoExp (env, st, fm) se
adamc@179 2186 in
adamc@179 2187 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 2188 end
adamc@179 2189
adamc@95 2190 | L.EApp (
adamc@95 2191 (L.EApp (
adamc@720 2192 (L.ECApp (
adamc@720 2193 (L.ECApp (
adamc@95 2194 (L.ECApp (
adamc@139 2195 (L.ECApp (
adamc@720 2196 (L.EFfi ("Basis", "join"),
adamc@720 2197 _), _), _),
adamc@139 2198 _), _),
adamc@720 2199 _), _),
adamc@720 2200 _), _),
adamc@720 2201 xml1), _),
adamc@720 2202 xml2) =>
adamc@179 2203 let
adamc@179 2204 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 2205 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 2206 in
adamc@179 2207 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 2208 end
adamc@95 2209
adamc@95 2210 | L.EApp (
adamc@95 2211 (L.EApp (
adamc@104 2212 (L.EApp (
adamc@721 2213 (L.EApp (
adamc@720 2214 (L.ECApp (
adamc@104 2215 (L.ECApp (
adamc@104 2216 (L.ECApp (
adamc@139 2217 (L.ECApp (
adamc@139 2218 (L.ECApp (
adamc@139 2219 (L.ECApp (
adamc@139 2220 (L.ECApp (
adamc@721 2221 (L.ECApp (
adamc@721 2222 (L.EFfi ("Basis", "tag"),
adamc@721 2223 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@721 2224 class), _),
adamc@720 2225 attrs), _),
adamc@720 2226 tag), _),
adamc@95 2227 xml) =>
adamc@95 2228 let
adamc@140 2229 fun getTag' (e, _) =
adamc@140 2230 case e of
adamc@143 2231 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 2232 | L.ECApp (e, t) => let
adamc@143 2233 val (tag, ts) = getTag' e
adamc@143 2234 in
adamc@143 2235 (tag, ts @ [t])
adamc@143 2236 end
adamc@140 2237 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 2238 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 2239 ("", []))
adamc@140 2240
adamc@95 2241 fun getTag (e, _) =
adamc@95 2242 case e of
adamc@143 2243 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 2244 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 2245 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 2246 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 2247 ("", []))
adamc@95 2248
adamc@143 2249 val (tag, targs) = getTag tag
adamc@95 2250
adamc@179 2251 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@598 2252 val attrs = case #1 attrs of
adamc@598 2253 L'.ERecord xes => xes
adamc@598 2254 | _ => raise Fail "Non-record attributes!"
adamc@104 2255
adamc@717 2256 val attrs =
adamc@717 2257 if List.exists (fn ("Link", _, _) => true
adamc@717 2258 | _ => false) attrs then
adamc@717 2259 List.filter (fn ("Href", _, _) => false
adamc@717 2260 | _ => true) attrs
adamc@717 2261 else
adamc@717 2262 attrs
adamc@717 2263
adamc@668 2264 fun findOnload (attrs, acc) =
adamc@668 2265 case attrs of
adamc@668 2266 [] => (NONE, acc)
adamc@668 2267 | ("Onload", e, _) :: rest => (SOME e, List.revAppend (acc, rest))
adamc@668 2268 | x :: rest => findOnload (rest, x :: acc)
adamc@679 2269
adamc@668 2270 val (onload, attrs) = findOnload (attrs, [])
adamc@668 2271
adamc@606 2272 fun lowercaseFirst "" = ""
adamc@606 2273 | lowercaseFirst s = String.str (Char.toLower (String.sub (s, 0)))
adamc@606 2274 ^ String.extract (s, 1, NONE)
adamc@606 2275
adamc@721 2276 val (class, fm) = monoExp (env, st, fm) class
adamc@721 2277
adamc@143 2278 fun tagStart tag =
adamc@598 2279 let
adamc@721 2280 val t = (L'.TFfi ("Basis", "string"), loc)
adamc@598 2281 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@721 2282
adamc@721 2283 val s = (L'.ECase (class,
adamc@721 2284 [((L'.PNone t, loc),
adamc@721 2285 s),
adamc@721 2286 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc),
adamc@721 2287 (L'.EStrcat (s,
adamc@721 2288 (L'.EStrcat ((L'.EPrim (Prim.String " class=\""), loc),
adamc@721 2289 (L'.EStrcat ((L'.ERel 0, loc),
adamc@721 2290 (L'.EPrim (Prim.String "\""), loc)),
adamc@721 2291 loc)), loc)), loc))],
adamc@721 2292 {disc = (L'.TOption t, loc),
adamc@721 2293 result = t}), loc)
adamc@598 2294 in
adamc@598 2295 foldl (fn (("Action", _, _), acc) => acc
adamc@598 2296 | (("Source", _, _), acc) => acc
adamc@598 2297 | ((x, e, t), (s, fm)) =>
adamc@598 2298 case t of
adamc@598 2299 (L'.TFfi ("Basis", "bool"), _) =>
adamc@598 2300 let
adamc@598 2301 val s' = " " ^ lowercaseFirst x
adamc@598 2302 in
adamc@598 2303 ((L'.ECase (e,
adamc@598 2304 [((L'.PCon (L'.Enum,
adamc@598 2305 L'.PConFfi {mod = "Basis",
adamc@598 2306 datatyp = "bool",
adamc@598 2307 con = "True",
adamc@598 2308 arg = NONE},
adamc@598 2309 NONE), loc),
adamc@598 2310 (L'.EStrcat (s,
adamc@598 2311 (L'.EPrim (Prim.String s'), loc)), loc)),
adamc@598 2312 ((L'.PCon (L'.Enum,
adamc@598 2313 L'.PConFfi {mod = "Basis",
adamc@598 2314 datatyp = "bool",
adamc@598 2315 con = "False",
adamc@598 2316 arg = NONE},
adamc@598 2317 NONE), loc),
adamc@598 2318 s)],
adamc@598 2319 {disc = (L'.TFfi ("Basis", "bool"), loc),
adamc@598 2320 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@598 2321 fm)
adamc@598 2322 end
adamc@598 2323 | (L'.TFun _, _) =>
adamc@598 2324 let
adamc@598 2325 val s' = " " ^ lowercaseFirst x ^ "='"
adamc@651 2326 val e = (L'.EApp (e, (L'.ERecord [], loc)), loc)
adamc@598 2327 in
adamc@598 2328 ((L'.EStrcat (s,
adamc@598 2329 (L'.EStrcat (
adamc@598 2330 (L'.EPrim (Prim.String s'), loc),
adamc@598 2331 (L'.EStrcat (
adamc@598 2332 (L'.EJavaScript (L'.Attribute, e, NONE), loc),
adamc@598 2333 (L'.EPrim (Prim.String "'"), loc)), loc)),
adamc@598 2334 loc)), loc),
adamc@598 2335 fm)
adamc@598 2336 end
adamc@598 2337 | _ =>
adamc@598 2338 let
adamc@598 2339 val fooify =
adamc@598 2340 case x of
adamc@717 2341 "Link" => urlifyExp
adamc@717 2342 | "Action" => urlifyExp
adamc@598 2343 | _ => attrifyExp
adamc@120 2344
adamc@724 2345 val x =
adamc@724 2346 case x of
adamc@724 2347 "Typ" => "Type"
adamc@724 2348 | _ => x
adamc@598 2349 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@179 2350
adamc@598 2351 val (e, fm) = fooify env fm (e, t)
adamc@598 2352 in
adamc@598 2353 ((L'.EStrcat (s,
adamc@598 2354 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@598 2355 (L'.EStrcat (e,
adamc@598 2356 (L'.EPrim (Prim.String "\""),
adamc@598 2357 loc)),
adamc@598 2358 loc)),
adamc@598 2359 loc)), loc),
adamc@598 2360 fm)
adamc@598 2361 end)
adamc@598 2362 (s, fm) attrs
adamc@598 2363 end
adamc@104 2364
adamc@143 2365 fun input typ =
adamc@143 2366 case targs of
adamc@155 2367 [_, (L.CName name, _)] =>
adamc@179 2368 let
adamc@179 2369 val (ts, fm) = tagStart "input"
adamc@179 2370 in
adamc@179 2371 ((L'.EStrcat (ts,
adamc@179 2372 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 2373 loc)), loc), fm)
adamc@179 2374 end
adamc@143 2375 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2376 raise Fail "No name passed to input tag")
adamc@104 2377
adamc@565 2378 fun normal (tag, extra, extraInner) =
adamc@143 2379 let
adamc@179 2380 val (tagStart, fm) = tagStart tag
adamc@152 2381 val tagStart = case extra of
adamc@152 2382 NONE => tagStart
adamc@152 2383 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 2384
adamc@695 2385 val xml = case extraInner of
adamc@695 2386 NONE => xml
adamc@695 2387 | SOME ei => (L.EFfiApp ("Basis", "strcat", [ei, xml]), loc)
adamc@695 2388
adamc@143 2389 fun normal () =
adamc@179 2390 let
adamc@179 2391 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2392 in
adamc@179 2393 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 2394 (L'.EStrcat (xml,
adamc@179 2395 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 2396 loc)), loc)),
adamc@179 2397 loc),
adamc@179 2398 fm)
adamc@179 2399 end
adamc@143 2400 in
adamc@143 2401 case xml of
adamc@143 2402 (L.EApp ((L.ECApp (
adamc@143 2403 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 2404 _), _),
adamc@143 2405 _), _),
adamc@143 2406 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 2407 if CharVector.all Char.isSpace s then
adamc@179 2408 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 2409 else
adamc@143 2410 normal ()
adamc@143 2411 | _ => normal ()
adamc@143 2412 end
adamc@606 2413
adamc@606 2414 fun setAttrs jexp =
adamc@606 2415 let
adamc@606 2416 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@606 2417
adamc@606 2418 val assgns = List.mapPartial
adamc@606 2419 (fn ("Source", _, _) => NONE
adamc@606 2420 | (x, e, _) =>
adamc@606 2421 SOME (strcat [str ("d." ^ lowercaseFirst x ^ "="),
adamc@606 2422 (L'.EJavaScript (L'.Script, e, NONE), loc),
adamc@606 2423 str ";"]))
adamc@606 2424 attrs
adamc@606 2425 in
adamc@606 2426 case assgns of
adamc@606 2427 [] => jexp
adamc@606 2428 | _ => strcat (str "var d="
adamc@606 2429 :: jexp
adamc@606 2430 :: str ";"
adamc@606 2431 :: assgns)
adamc@606 2432 end
adamc@152 2433 in
adamc@152 2434 case tag of
adamc@679 2435 "body" => let
adamc@668 2436 val onload = case onload of
adamc@668 2437 NONE => (L'.EPrim (Prim.String ""), loc)
adamc@668 2438 | SOME e =>
adamc@668 2439 let
adamc@668 2440 val e = (L'.EApp (e, (L'.ERecord [], loc)), loc)
adamc@668 2441 in
adamc@668 2442 (L'.EJavaScript (L'.Attribute, e, NONE), loc)
adamc@679 2443 end
adamc@668 2444 in
adamc@668 2445 normal ("body",
adamc@695 2446 SOME (L'.EFfiApp ("Basis", "maybe_onload",
adamc@695 2447 [(L'.EStrcat ((L'.EFfiApp ("Basis", "get_settings",
adamc@695 2448 [(L'.ERecord [], loc)]), loc),
adamc@695 2449 onload), loc)]),
adamc@695 2450 loc),
adamc@695 2451 SOME (L.EFfiApp ("Basis", "get_script", [(L.ERecord [], loc)]), loc))
adamc@668 2452 end
adamc@568 2453
adamc@568 2454 | "dyn" =>
adamc@598 2455 (case attrs of
adamc@598 2456 [("Signal", (L'.EApp ((L'.EAbs (_, _, _, (L'.ESignalReturn (L'.ERel 0, _), _)), _),
adamc@598 2457 e), _), _)] => (e, fm)
adamc@598 2458 | [("Signal", e, _)] =>
adamc@568 2459 ((L'.EStrcat
adamc@603 2460 ((L'.EPrim (Prim.String "<span><script type=\"text/javascript\">dyn("), loc),
adamc@578 2461 (L'.EStrcat ((L'.EJavaScript (L'.Script, e, NONE), loc),
adamc@603 2462 (L'.EPrim (Prim.String ")</script></span>"), loc)), loc)), loc),
adamc@568 2463 fm)
adamc@568 2464 | _ => raise Fail "Monoize: Bad dyn attributes")
adamc@565 2465
adamc@565 2466 | "submit" => normal ("input type=\"submit\"", NONE, NONE)
adamc@601 2467 | "button" => normal ("input type=\"submit\"", NONE, NONE)
adamc@152 2468
adamc@152 2469 | "textbox" =>
adamc@152 2470 (case targs of
adamc@152 2471 [_, (L.CName name, _)] =>
adamc@598 2472 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@598 2473 NONE =>
adamc@598 2474 let
adamc@598 2475 val (ts, fm) = tagStart "input"
adamc@598 2476 in
adamc@598 2477 ((L'.EStrcat (ts,
adamc@598 2478 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@598 2479 loc)), loc), fm)
adamc@598 2480 end
adamc@598 2481 | SOME (_, src, _) =>
adamc@646 2482 (strcat [str "<span><script type=\"text/javascript\">inp(\"input\",",
adamc@598 2483 (L'.EJavaScript (L'.Script, src, NONE), loc),
adamc@646 2484 str ")</script></span>"],
adamc@598 2485 fm))
adamc@152 2486 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@598 2487 raise Fail "No name passed to textbox tag"))
adamc@155 2488 | "password" => input "password"
adamc@361 2489 | "textarea" =>
adamc@152 2490 (case targs of
adamc@152 2491 [_, (L.CName name, _)] =>
adamc@179 2492 let
adamc@179 2493 val (ts, fm) = tagStart "textarea"
adamc@179 2494 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2495 in
adamc@179 2496 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 2497 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 2498 (L'.EStrcat (xml,
adamc@179 2499 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 2500 loc)), loc)),
adamc@179 2501 loc), fm)
adamc@179 2502 end
adamc@152 2503 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2504 raise Fail "No name passed to ltextarea tag"))
adamc@153 2505
adamc@190 2506 | "checkbox" => input "checkbox"
adamc@737 2507 | "upload" => input "file"
adamc@190 2508
adamc@153 2509 | "radio" =>
adamc@153 2510 (case targs of
adamc@153 2511 [_, (L.CName name, _)] =>
adamc@179 2512 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 2513 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2514 raise Fail "No name passed to radio tag"))
adamc@153 2515 | "radioOption" =>
adamc@153 2516 (case St.radioGroup st of
adamc@153 2517 NONE => raise Fail "No name for radioGroup"
adamc@153 2518 | SOME name =>
adamc@153 2519 normal ("input",
adamc@565 2520 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc),
adamc@565 2521 NONE))
adamc@152 2522
adamc@361 2523 | "select" =>
adamc@154 2524 (case targs of
adamc@154 2525 [_, (L.CName name, _)] =>
adamc@179 2526 let
adamc@179 2527 val (ts, fm) = tagStart "select"
adamc@179 2528 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2529 in
adamc@179 2530 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@598 2531 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")),
adamc@598 2532 loc)), loc),
adamc@179 2533 (L'.EStrcat (xml,
adamc@179 2534 (L'.EPrim (Prim.String "</select>"),
adamc@179 2535 loc)), loc)),
adamc@179 2536 loc),
adamc@179 2537 fm)
adamc@179 2538 end
adamc@154 2539 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 2540 raise Fail "No name passed to lselect tag"))
adamc@154 2541
adamc@601 2542 | "ctextbox" =>
adamc@601 2543 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@601 2544 NONE =>
adamc@601 2545 let
adamc@601 2546 val (ts, fm) = tagStart "input"
adamc@601 2547 in
adamc@601 2548 ((L'.EStrcat (ts,
adamc@601 2549 (L'.EPrim (Prim.String "/>"), loc)),
adamc@601 2550 loc), fm)
adamc@601 2551 end
adamc@601 2552 | SOME (_, src, _) =>
adamc@606 2553 let
adamc@606 2554 val sc = strcat [str "inp(\"input\",",
adamc@606 2555 (L'.EJavaScript (L'.Script, src, NONE), loc),
adamc@606 2556 str ")"]
adamc@606 2557 val sc = setAttrs sc
adamc@606 2558 in
adamc@646 2559 (strcat [str "<span><script type=\"text/javascript\">",
adamc@606 2560 sc,
adamc@646 2561 str "</script></span>"],
adamc@606 2562 fm)
adamc@606 2563 end)
adamc@154 2564
adamc@565 2565 | "tabl" => normal ("table", NONE, NONE)
adamc@565 2566 | _ => normal (tag, NONE, NONE)
adamc@95 2567 end
adamc@94 2568
adamc@141 2569 | L.EApp ((L.ECApp (
adamc@361 2570 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@734 2571 (L.CRecord (_, fields), _)), _),
adamc@141 2572 xml) =>
adamc@143 2573 let
adamc@143 2574 fun findSubmit (e, _) =
adamc@143 2575 case e of
adamc@143 2576 L.EApp (
adamc@143 2577 (L.EApp (
adamc@143 2578 (L.ECApp (
adamc@143 2579 (L.ECApp (
adamc@143 2580 (L.ECApp (
adamc@143 2581 (L.ECApp (
adamc@143 2582 (L.EFfi ("Basis", "join"),
adamc@143 2583 _), _), _),
adamc@143 2584 _), _),
adamc@143 2585 _), _),
adamc@143 2586 _), _),
adamc@143 2587 xml1), _),
adamc@143 2588 xml2) => (case findSubmit xml1 of
adamc@143 2589 Error => Error
adamc@143 2590 | NotFound => findSubmit xml2
adamc@143 2591 | Found e =>
adamc@143 2592 case findSubmit xml2 of
adamc@143 2593 NotFound => Found e
adamc@143 2594 | _ => Error)
adamc@143 2595 | L.EApp (
adamc@143 2596 (L.EApp (
adamc@143 2597 (L.EApp (
adamc@730 2598 (L.EApp (
adamc@143 2599 (L.ECApp (
adamc@143 2600 (L.ECApp (
adamc@143 2601 (L.ECApp (
adamc@143 2602 (L.ECApp (
adamc@143 2603 (L.ECApp (
adamc@143 2604 (L.ECApp (
adamc@143 2605 (L.ECApp (
adamc@730 2606 (L.ECApp (
adamc@730 2607 (L.EFfi ("Basis", "tag"),
adamc@730 2608 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@730 2609 _), _),
adamc@143 2610 attrs), _),
adamc@143 2611 _), _),
adamc@143 2612 xml) =>
adamc@143 2613 (case #1 attrs of
adamc@143 2614 L.ERecord xes =>
adamc@143 2615 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 2616 | _ => NONE) xes of
adamc@143 2617 NONE => findSubmit xml
adamc@143 2618 | SOME et =>
adamc@143 2619 case findSubmit xml of
adamc@143 2620 NotFound => Found et
adamc@143 2621 | _ => Error)
adamc@143 2622 | _ => findSubmit xml)
adamc@143 2623 | _ => NotFound
adamc@143 2624
adamc@735 2625 val (func, action, fm) = case findSubmit xml of
adamc@735 2626 NotFound => (0, (L'.EPrim (Prim.String ""), loc), fm)
adamc@143 2627 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@598 2628 | Found (action, actionT) =>
adamc@598 2629 let
adamc@735 2630 val func = case #1 action of
adamc@735 2631 L.EClosure (n, _) => n
adamc@735 2632 | _ => raise Fail "Monoize: Action is not a closure"
adamc@598 2633 val actionT = monoType env actionT
adamc@598 2634 val (action, fm) = monoExp (env, st, fm) action
adamc@598 2635 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@598 2636 in
adamc@735 2637 (func,
adamc@735 2638 (L'.EStrcat ((L'.EPrim (Prim.String " action=\""), loc),
adamc@598 2639 (L'.EStrcat (action,
adamc@598 2640 (L'.EPrim (Prim.String "\""), loc)), loc)), loc),
adamc@598 2641 fm)
adamc@598 2642 end
adamc@734 2643
adamc@737 2644 val hasUpload = CoreUtil.Exp.exists {kind = fn _ => false,
adamc@737 2645 con = fn _ => false,
adamc@737 2646 exp = fn e =>
adamc@737 2647 case e of
adamc@737 2648 L.EFfi ("Basis", "upload") => true
adamc@737 2649 | _ => false} xml
adamc@737 2650
adamc@179 2651 val (xml, fm) = monoExp (env, st, fm) xml
adamc@735 2652
adamc@735 2653 val xml =
adamc@735 2654 if IS.member (!readCookie, func) then
adamc@735 2655 let
adamc@735 2656 fun inFields s = List.exists (fn ((L.CName s', _), _) => s' = s
adamc@735 2657 | _ => true) fields
adamc@735 2658
adamc@735 2659 fun getSigName () =
adamc@735 2660 let
adamc@735 2661 fun getSigName' n =
adamc@735 2662 let
adamc@735 2663 val s = "Sig" ^ Int.toString n
adamc@735 2664 in
adamc@735 2665 if inFields s then
adamc@735 2666 getSigName' (n + 1)
adamc@735 2667 else
adamc@735 2668 s
adamc@735 2669 end
adamc@735 2670 in
adamc@735 2671 if inFields "Sig" then
adamc@735 2672 getSigName' 0
adamc@735 2673 else
adamc@735 2674 "Sig"
adamc@735 2675 end
adamc@735 2676
adamc@735 2677 val sigName = getSigName ()
adamc@735 2678 val sigSet = (L'.EFfiApp ("Basis", "sigString", [(L'.ERecord [], loc)]), loc)
adamc@735 2679 val sigSet = (L'.EStrcat ((L'.EPrim (Prim.String ("<input type=\"hidden\" name=\""
adamc@735 2680 ^ sigName
adamc@735 2681 ^ "\" value=\"")), loc),
adamc@735 2682 sigSet), loc)
adamc@735 2683 val sigSet = (L'.EStrcat (sigSet,
adamc@735 2684 (L'.EPrim (Prim.String "\">"), loc)), loc)
adamc@735 2685 in
adamc@735 2686 (L'.EStrcat (sigSet, xml), loc)
adamc@735 2687 end
adamc@735 2688 else
adamc@735 2689 xml
adamc@737 2690
adamc@737 2691 val action = if hasUpload then
adamc@737 2692 (L'.EStrcat (action,
adamc@737 2693 (L'.EPrim (Prim.String " enctype=\"multipart/form-data\""), loc)), loc)
adamc@737 2694 else
adamc@737 2695 action
adamc@737 2696
adamc@143 2697 in
adamc@730 2698 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form method=\"post\""), loc),
adamc@179 2699 (L'.EStrcat (action,
adamc@598 2700 (L'.EPrim (Prim.String ">"), loc)), loc)), loc),
adamc@179 2701 (L'.EStrcat (xml,
adamc@179 2702 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 2703 fm)
adamc@143 2704 end
adamc@141 2705
adamc@756 2706 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp (
adamc@756 2707 (L.EFfi ("Basis", "subform"), _), _), _), _),
adamc@756 2708 _), _), _), (L.CName nm, loc)) =>
adamc@756 2709 let
adamc@756 2710 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@756 2711 in
adamc@756 2712 ((L'.EAbs ("xml", s, s,
adamc@756 2713 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".b\" value=\""
adamc@756 2714 ^ nm ^ "\">")), loc),
adamc@756 2715 (L'.ERel 0, loc),
adamc@756 2716 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\">")), loc)]),
adamc@756 2717 loc),
adamc@756 2718 fm)
adamc@756 2719 end
adamc@756 2720
adamc@148 2721 | L.EApp ((L.ECApp (
adamc@148 2722 (L.ECApp (
adamc@148 2723 (L.ECApp (
adamc@148 2724 (L.ECApp (
adamc@148 2725 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 2726 _), _),
adamc@148 2727 _), _),
adamc@148 2728 _), _),
adamc@179 2729 xml) => monoExp (env, st, fm) xml
adamc@148 2730
adamc@283 2731 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 2732 let
adamc@283 2733 val t = monoType env t
adamc@283 2734 in
adamc@283 2735 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 2736 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 2737 fm)
adamc@283 2738 end
adamc@741 2739 | L.ECApp ((L.EFfi ("Basis", "returnBlob"), _), t) =>
adamc@741 2740 let
adamc@741 2741 val t = monoType env t
adamc@741 2742 val un = (L'.TRecord [], loc)
adamc@741 2743 in
adamc@741 2744 ((L'.EAbs ("b", (L'.TFfi ("Basis", "blob"), loc),
adamc@741 2745 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFun (un, t), loc)), loc),
adamc@741 2746 (L'.EAbs ("mt", (L'.TFfi ("Basis", "string"), loc), (L'.TFun (un, t), loc),
adamc@741 2747 (L'.EAbs ("_", un, t,
adamc@741 2748 (L'.EReturnBlob {blob = (L'.ERel 2, loc),
adamc@741 2749 mimeType = (L'.ERel 1, loc),
adamc@741 2750 t = t}, loc)), loc)), loc)), loc),
adamc@741 2751 fm)
adamc@741 2752 end
adamc@283 2753
adamc@179 2754 | L.EApp (e1, e2) =>
adamc@179 2755 let
adamc@179 2756 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 2757 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 2758 in
adamc@179 2759 ((L'.EApp (e1, e2), loc), fm)
adamc@179 2760 end
adamc@26 2761 | L.EAbs (x, dom, ran, e) =>
adamc@179 2762 let
adamc@179 2763 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 2764 in
adamc@179 2765 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 2766 end
adamc@25 2767 | L.ECApp _ => poly ()
adamc@25 2768 | L.ECAbs _ => poly ()
adamc@25 2769
adamc@252 2770 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 2771 | L.EFfiApp (m, x, es) =>
adamc@252 2772 let
adamc@252 2773 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 2774 in
adamc@252 2775 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 2776 end
adamc@252 2777
adamc@179 2778 | L.ERecord xes =>
adamc@179 2779 let
adamc@179 2780 val (xes, fm) = ListUtil.foldlMap
adamc@179 2781 (fn ((x, e, t), fm) =>
adamc@179 2782 let
adamc@179 2783 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2784 in
adamc@179 2785 ((monoName env x,
adamc@179 2786 e,
adamc@179 2787 monoType env t), fm)
adamc@179 2788 end) fm xes
adamc@179 2789 in
adamc@179 2790 ((L'.ERecord xes, loc), fm)
adamc@179 2791 end
adamc@179 2792 | L.EField (e, x, _) =>
adamc@179 2793 let
adamc@179 2794 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2795 in
adamc@179 2796 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 2797 end
adamc@445 2798 | L.EConcat _ => poly ()
adamc@149 2799 | L.ECut _ => poly ()
adamc@493 2800 | L.ECutMulti _ => poly ()
adamc@177 2801
adamc@182 2802 | L.ECase (e, pes, {disc, result}) =>
adamc@179 2803 let
adamc@179 2804 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2805 val (pes, fm) = ListUtil.foldlMap
adamc@179 2806 (fn ((p, e), fm) =>
adamc@179 2807 let
adamc@179 2808 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2809 in
adamc@182 2810 ((monoPat env p, e), fm)
adamc@179 2811 end) fm pes
adamc@179 2812 in
adamc@182 2813 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 2814 end
adamc@177 2815
adamc@179 2816 | L.EWrite e =>
adamc@179 2817 let
adamc@179 2818 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2819 in
adamc@252 2820 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 2821 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 2822 end
adamc@110 2823
adamc@179 2824 | L.EClosure (n, es) =>
adamc@179 2825 let
adamc@179 2826 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 2827 monoExp (env, st, fm) e)
adamc@179 2828 fm es
adamc@179 2829 in
adamc@179 2830 ((L'.EClosure (n, es), loc), fm)
adamc@179 2831 end
adamc@450 2832
adamc@450 2833 | L.ELet (x, t, e1, e2) =>
adamc@450 2834 let
adamc@450 2835 val t' = monoType env t
adamc@450 2836 val (e1, fm) = monoExp (env, st, fm) e1
adamc@450 2837 val (e2, fm) = monoExp (Env.pushERel env x t, st, fm) e2
adamc@450 2838 in
adamc@450 2839 ((L'.ELet (x, t', e1, e2), loc), fm)
adamc@450 2840 end
adamc@607 2841
adamc@609 2842 | L.EServerCall (n, es, ek, t) =>
adamc@608 2843 let
adamc@609 2844 val t = monoType env t
adamc@614 2845 val (_, ft, _, name) = Env.lookupENamed env n
adamc@608 2846 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@614 2847
adamc@614 2848 fun encodeArgs (es, ft, acc, fm) =
adamc@614 2849 case (es, ft) of
adamc@614 2850 ([], _) => (rev acc, fm)
adamc@614 2851 | (e :: es, (L.TFun (dom, ran), _)) =>
adamc@614 2852 let
adamc@614 2853 val (e, fm) = urlifyExp env fm (e, monoType env dom)
adamc@614 2854 in
adamc@614 2855 encodeArgs (es, ran, e
adamc@614 2856 :: (L'.EPrim (Prim.String "/"), loc)
adamc@614 2857 :: acc, fm)
adamc@614 2858 end
adamc@614 2859 | _ => raise Fail "Monoize: Not enough arguments visible in RPC function type"
adamc@614 2860
adamc@614 2861 val (call, fm) = encodeArgs (es, ft, [], fm)
adamc@614 2862 val call = foldl (fn (e, call) => (L'.EStrcat (call, e), loc))
adamc@614 2863 (L'.EPrim (Prim.String name), loc) call
adamc@614 2864
adamc@608 2865 val (ek, fm) = monoExp (env, st, fm) ek
adamc@609 2866
adamc@609 2867 val ekf = (L'.EAbs ("f",
adamc@609 2868 (L'.TFun (t,
adamc@609 2869 (L'.TFun ((L'.TRecord [], loc),
adamc@609 2870 (L'.TRecord [], loc)), loc)), loc),
adamc@609 2871 (L'.TFun (t,
adamc@609 2872 (L'.TRecord [], loc)), loc),
adamc@609 2873 (L'.EAbs ("x",
adamc@609 2874 t,
adamc@609 2875 (L'.TRecord [], loc),
adamc@609 2876 (L'.EApp ((L'.EApp ((L'.ERel 1, loc),
adamc@609 2877 (L'.ERel 0, loc)), loc),
adamc@609 2878 (L'.ERecord [], loc)), loc)), loc)), loc)
adamc@609 2879 val ek = (L'.EApp (ekf, ek), loc)
adamc@736 2880 val eff = if IS.member (!readCookie, n) then
adamc@736 2881 L'.ReadCookieWrite
adamc@736 2882 else
adamc@736 2883 L'.ReadOnly
adamc@736 2884 val e = (L'.EServerCall (call, ek, t, eff), loc)
adamc@651 2885 val e = liftExpInExp 0 e
adamc@651 2886 val unit = (L'.TRecord [], loc)
adamc@651 2887 val e = (L'.EAbs ("_", unit, unit, e), loc)
adamc@608 2888 in
adamc@651 2889 (e, fm)
adamc@608 2890 end
adamc@626 2891
adamc@626 2892 | L.EKAbs _ => poly ()
adamc@626 2893 | L.EKApp _ => poly ()
adamc@25 2894 end
adamc@25 2895
adamc@179 2896 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 2897 let
adamc@25 2898 fun poly () =
adamc@25 2899 (E.errorAt loc "Unsupported declaration";
adamc@25 2900 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 2901 NONE)
adamc@25 2902 in
adamc@25 2903 case d of
adamc@25 2904 L.DCon _ => NONE
adamc@193 2905 | L.DDatatype (x, n, [], xncs) =>
adamc@193 2906 let
adamc@196 2907 val env' = Env.declBinds env all
adamc@196 2908 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 2909 in
adamc@273 2910 SOME (env', fm, [d])
adamc@193 2911 end
adamc@757 2912 | L.DDatatype ("list", n, [_], [("Nil", _, NONE),
adamc@757 2913 ("Cons", _, SOME (L.TRecord (L.CRecord (_,
adamc@757 2914 [((L.CName "1", _),
adamc@757 2915 (L.CRel 0, _)),
adamc@757 2916 ((L.CName "2", _),
adamc@757 2917 (L.CApp ((L.CNamed n', _),
adamc@757 2918 (L.CRel 0, _)),
adamc@757 2919 _))]), _), _))]) =>
adamc@757 2920 if n = n' then
adamc@757 2921 NONE
adamc@757 2922 else
adamc@757 2923 poly ()
adamc@193 2924 | L.DDatatype _ => poly ()
adamc@179 2925 | L.DVal (x, n, t, e, s) =>
adamc@179 2926 let
adamc@179 2927 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 2928 in
adamc@179 2929 SOME (Env.pushENamed env x n t NONE s,
adamc@179 2930 fm,
adamc@273 2931 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 2932 end
adamc@128 2933 | L.DValRec vis =>
adamc@128 2934 let
adamc@128 2935 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 2936
adamc@179 2937 val (vis, fm) = ListUtil.foldlMap
adamc@179 2938 (fn ((x, n, t, e, s), fm) =>
adamc@179 2939 let
adamc@179 2940 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 2941 in
adamc@179 2942 ((x, n, monoType env t, e, s), fm)
adamc@179 2943 end)
adamc@179 2944 fm vis
adamc@128 2945 in
adamc@128 2946 SOME (env,
adamc@179 2947 fm,
adamc@273 2948 [(L'.DValRec vis, loc)])
adamc@128 2949 end
adamc@144 2950 | L.DExport (ek, n) =>
adamc@115 2951 let
adamc@120 2952 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 2953
adamc@609 2954 fun unwind (t, args) =
adamc@609 2955 case #1 t of
adamc@609 2956 L.TFun (dom, ran) => unwind (ran, dom :: args)
adamc@314 2957 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@609 2958 unwind (t, (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: args)
adamc@609 2959 | _ => (rev args, t)
adamc@120 2960
adamc@609 2961 val (ts, ran) = unwind (t, [])
adamc@609 2962 val ts = map (monoType env) ts
adamc@609 2963 val ran = monoType env ran
adamc@115 2964 in
adamc@609 2965 SOME (env, fm, [(L'.DExport (ek, s, n, ts, ran), loc)])
adamc@115 2966 end
adamc@707 2967 | L.DTable (x, n, (L.CRecord (_, xts), _), s, pe, _, ce, _) =>
adamc@251 2968 let
adamc@251 2969 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 2970 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 2971 val s = "uw_" ^ s
adamc@704 2972 val e_name = (L'.EPrim (Prim.String s), loc)
adamc@273 2973
adamc@273 2974 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@704 2975
adamc@707 2976 val (pe, fm) = monoExp (env, St.empty, fm) pe
adamc@707 2977 val (ce, fm) = monoExp (env, St.empty, fm) ce
adamc@251 2978 in
adamc@251 2979 SOME (Env.pushENamed env x n t NONE s,
adamc@251 2980 fm,
adamc@707 2981 [(L'.DTable (s, xts, pe, ce), loc),
adamc@704 2982 (L'.DVal (x, n, t', e_name, s), loc)])
adamc@251 2983 end
adamc@273 2984 | L.DTable _ => poly ()
adamc@754 2985 | L.DView (x, n, s, e, (L.CRecord (_, xts), _)) =>
adamc@754 2986 let
adamc@754 2987 val t = (L.CFfi ("Basis", "string"), loc)
adamc@754 2988 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@754 2989 val s = "uw_" ^ s
adamc@754 2990 val e_name = (L'.EPrim (Prim.String s), loc)
adamc@754 2991
adamc@754 2992 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@754 2993
adamc@754 2994 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@754 2995 val e = (L'.EFfiApp ("Basis", "viewify", [e]), loc)
adamc@754 2996 in
adamc@754 2997 SOME (Env.pushENamed env x n t NONE s,
adamc@754 2998 fm,
adamc@754 2999 [(L'.DView (s, xts, e), loc),
adamc@754 3000 (L'.DVal (x, n, t', e_name, s), loc)])
adamc@754 3001 end
adamc@754 3002 | L.DView _ => poly ()
adamc@338 3003 | L.DSequence (x, n, s) =>
adamc@338 3004 let
adamc@338 3005 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 3006 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 3007 val s = "uw_" ^ s
adamc@338 3008 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 3009 in
adamc@338 3010 SOME (Env.pushENamed env x n t NONE s,
adamc@338 3011 fm,
adamc@338 3012 [(L'.DSequence s, loc),
adamc@338 3013 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 3014 end
adamc@683 3015 | L.DDatabase _ => NONE
adamc@462 3016 | L.DCookie (x, n, t, s) =>
adamc@462 3017 let
adamc@462 3018 val t = (L.CFfi ("Basis", "string"), loc)
adamc@462 3019 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@462 3020 val e = (L'.EPrim (Prim.String s), loc)
adamc@462 3021 in
adamc@462 3022 SOME (Env.pushENamed env x n t NONE s,
adamc@462 3023 fm,
adamc@725 3024 [(L'.DCookie s, loc),
adamc@725 3025 (L'.DVal (x, n, t', e, s), loc)])
adamc@462 3026 end
adamc@720 3027 | L.DStyle (x, n, s) =>
adamc@718 3028 let
adamc@718 3029 val t = (L.CFfi ("Basis", "string"), loc)
adamc@718 3030 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@718 3031 val e = (L'.EPrim (Prim.String s), loc)
adamc@718 3032 in
adamc@718 3033 SOME (Env.pushENamed env x n t NONE s,
adamc@718 3034 fm,
adamc@720 3035 [(L'.DStyle s, loc),
adamc@718 3036 (L'.DVal (x, n, t', e, s), loc)])
adamc@718 3037 end
adamc@25 3038 end
adamc@25 3039
adamc@683 3040 datatype expungable = Client | Channel
adamc@683 3041
adamc@683 3042 fun monoize env file =
adamc@25 3043 let
adamc@385 3044 val p = !urlPrefix
adamc@385 3045 val () =
adamc@385 3046 if p = "" then
adamc@385 3047 urlPrefix := "/"
adamc@385 3048 else if String.sub (p, size p - 1) <> #"/" then
adamc@385 3049 urlPrefix := p ^ "/"
adamc@385 3050 else
adamc@385 3051 ()
adamc@385 3052
adamc@735 3053 (* Calculate which exported functions need cookie signature protection *)
adamc@735 3054 val rcook = foldl (fn ((d, _), rcook) =>
adamc@735 3055 case d of
adamc@735 3056 L.DExport (L.Action L.ReadCookieWrite, n) => IS.add (rcook, n)
adamc@735 3057 | L.DExport (L.Rpc L.ReadCookieWrite, n) => IS.add (rcook, n)
adamc@735 3058 | _ => rcook)
adamc@735 3059 IS.empty file
adamc@735 3060 val () = readCookie := rcook
adamc@735 3061
adamc@683 3062 val loc = E.dummySpan
adamc@683 3063 val client = (L'.TFfi ("Basis", "client"), loc)
adamc@683 3064 val unit = (L'.TRecord [], loc)
adamc@687 3065
adamc@687 3066 fun calcClientish xts =
adamc@687 3067 foldl (fn ((x : L.con, t : L.con), st as (nullable, notNullable)) =>
adamc@687 3068 case #1 x of
adamc@687 3069 L.CName x =>
adamc@687 3070 (case #1 t of
adamc@687 3071 L.CFfi ("Basis", "client") =>
adamc@687 3072 (nullable, (x, Client) :: notNullable)
adamc@687 3073 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@687 3074 (L.CFfi ("Basis", "client"), _)) =>
adamc@687 3075 ((x, Client) :: nullable, notNullable)
adamc@687 3076 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@687 3077 (nullable, (x, Channel) :: notNullable)
adamc@687 3078 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@687 3079 (L.CApp ((L.CFfi ("Basis", "channel"), _), _), _)) =>
adamc@687 3080 ((x, Channel) :: nullable, notNullable)
adamc@687 3081 | _ => st)
adamc@687 3082 | _ => st) ([], []) xts
adamc@687 3083
adamc@683 3084 fun expunger () =
adamc@683 3085 let
adamc@683 3086 val target = (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)
adamc@683 3087
adamc@683 3088 fun doTable (tab, xts, e) =
adamc@683 3089 case xts of
adamc@683 3090 L.CRecord (_, xts) =>
adamc@683 3091 let
adamc@687 3092 val (nullable, notNullable) = calcClientish xts
adamc@683 3093
adamc@684 3094 fun cond (x, v) =
adamc@684 3095 (L'.EStrcat ((L'.EPrim (Prim.String ("uw_" ^ x
adamc@684 3096 ^ (case v of
adamc@684 3097 Client => ""
adamc@684 3098 | Channel => " >> 32")
adamc@684 3099 ^ " = ")), loc),
adamc@684 3100 target), loc)
adamc@684 3101
adamc@684 3102 val e =
adamc@684 3103 foldl (fn ((x, v), e) =>
adamc@684 3104 (L'.ESeq (
adamc@684 3105 (L'.EDml (L'.EStrcat (
adamc@684 3106 (L'.EPrim (Prim.String ("UPDATE uw_"
adamc@684 3107 ^ tab
adamc@684 3108 ^ " SET uw_"
adamc@684 3109 ^ x
adamc@684 3110 ^ " = NULL WHERE ")), loc),
adamc@684 3111 cond (x, v)), loc), loc),
adamc@684 3112 e), loc))
adamc@684 3113 e nullable
adamc@684 3114
adamc@683 3115 val e =
adamc@683 3116 case notNullable of
adamc@683 3117 [] => e
adamc@683 3118 | eb :: ebs =>
adamc@684 3119 (L'.ESeq (
adamc@684 3120 (L'.EDml (foldl
adamc@684 3121 (fn (eb, s) =>
adamc@684 3122 (L'.EStrcat (s,
adamc@687 3123 (L'.EStrcat ((L'.EPrim (Prim.String " OR "),
adamc@684 3124 loc),
adamc@684 3125 cond eb), loc)), loc))
adamc@684 3126 (L'.EStrcat ((L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@684 3127 ^ tab
adamc@684 3128 ^ " WHERE ")), loc),
adamc@684 3129 cond eb), loc)
adamc@684 3130 ebs), loc),
adamc@684 3131 e), loc)
adamc@683 3132 in
adamc@683 3133 e
adamc@683 3134 end
adamc@683 3135 | _ => e
adamc@683 3136
adamc@683 3137 val e = (L'.ERecord [], loc)
adamc@683 3138 in
adamc@683 3139 foldl (fn ((d, _), e) =>
adamc@683 3140 case d of
adamc@707 3141 L.DTable (_, _, xts, tab, _, _, _, _) => doTable (tab, #1 xts, e)
adamc@683 3142 | _ => e) e file
adamc@683 3143 end
adamc@683 3144
adamc@687 3145 fun initializer () =
adamc@687 3146 let
adamc@687 3147 fun doTable (tab, xts, e) =
adamc@687 3148 case xts of
adamc@687 3149 L.CRecord (_, xts) =>
adamc@687 3150 let
adamc@687 3151 val (nullable, notNullable) = calcClientish xts
adamc@687 3152
adamc@687 3153 val e =
adamc@687 3154 case nullable of
adamc@687 3155 [] => e
adamc@687 3156 | (x, _) :: ebs =>
adamc@687 3157 (L'.ESeq (
adamc@687 3158 (L'.EDml (L'.EPrim (Prim.String
adamc@687 3159 (foldl (fn ((x, _), s) =>
adamc@687 3160 s ^ ", uw_" ^ x ^ " = NULL")
adamc@687 3161 ("UPDATE uw_"
adamc@687 3162 ^ tab
adamc@687 3163 ^ " SET uw_"
adamc@687 3164 ^ x
adamc@687 3165 ^ " = NULL")
adamc@687 3166 ebs)), loc), loc),
adamc@687 3167 e), loc)
adamc@687 3168
adamc@687 3169 val e =
adamc@687 3170 case notNullable of
adamc@687 3171 [] => e
adamc@687 3172 | eb :: ebs =>
adamc@687 3173 (L'.ESeq (
adamc@687 3174 (L'.EDml (L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@687 3175 ^ tab)), loc), loc),
adamc@687 3176 e), loc)
adamc@687 3177 in
adamc@687 3178 e
adamc@687 3179 end
adamc@687 3180 | _ => e
adamc@687 3181
adamc@687 3182 val e = (L'.ERecord [], loc)
adamc@687 3183 in
adamc@687 3184 foldl (fn ((d, _), e) =>
adamc@687 3185 case d of
adamc@707 3186 L.DTable (_, _, xts, tab, _, _, _, _) => doTable (tab, #1 xts, e)
adamc@687 3187 | _ => e) e file
adamc@687 3188 end
adamc@687 3189
adamc@179 3190 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@683 3191 case #1 d of
adamc@683 3192 L.DDatabase s =>
adamc@683 3193 let
adamc@687 3194 val (nExp, fm) = Fm.freshName fm
adamc@687 3195 val (nIni, fm) = Fm.freshName fm
adamc@687 3196
adamc@687 3197 val dExp = L'.DVal ("expunger",
adamc@687 3198 nExp,
adamc@687 3199 (L'.TFun (client, unit), loc),
adamc@687 3200 (L'.EAbs ("cli", client, unit, expunger ()), loc),
adamc@687 3201 "expunger")
adamc@687 3202 val dIni = L'.DVal ("initializer",
adamc@687 3203 nIni,
adamc@687 3204 (L'.TFun (unit, unit), loc),
adamc@687 3205 (L'.EAbs ("_", unit, unit, initializer ()), loc),
adamc@687 3206 "initializer")
adamc@683 3207 in
adamc@687 3208 (env, Fm.enter fm, (L'.DDatabase {name = s,
adamc@687 3209 expunge = nExp,
adamc@687 3210 initialize = nIni}, loc)
adamc@687 3211 :: (dExp, loc)
adamc@687 3212 :: (dIni, loc)
adamc@683 3213 :: ds)
adamc@683 3214 end
adamc@683 3215 | _ =>
adamc@683 3216 case monoDecl (env, fm) d of
adamc@683 3217 NONE => (env, fm, ds)
adamc@683 3218 | SOME (env, fm, ds') =>
adamc@683 3219 (env,
adamc@683 3220 Fm.enter fm,
adamc@683 3221 ds' @ Fm.decls fm @ ds))
adamc@683 3222 (env, Fm.empty (CoreUtil.File.maxName file + 1), []) file
adamc@25 3223 in
adamc@25 3224 rev ds
adamc@25 3225 end
adamc@25 3226
adamc@25 3227 end