annotate src/monoize.sml @ 753:d484df4e841a

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