annotate src/monoize.sml @ 841:44c2c089ca15

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