annotate src/monoize.sml @ 721:9864b64b1700

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