annotate src/monoize.sml @ 832:249740301a0a

Fix pathmap bug; expose lower-level heap manipulation from C
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 May 2009 13:43:02 -0400
parents 669ac5e9a69e
children 74e9e7642f08
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@565 1248
adamc@694 1249 | L.EFfiApp ("Basis", "spawn", [e]) =>
adamc@694 1250 let
adamc@694 1251 val (e, fm) = monoExp (env, st, fm) e
adamc@694 1252 in
adamc@694 1253 ((L'.EApp (e, (L'.ERecord [], loc)), loc), fm)
adamc@694 1254 end
adamc@694 1255
adamc@820 1256 | L.EFfi ("Basis", "signal_monad") => ((L'.ERecord [], loc), fm)
adamc@820 1257 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), (L.CFfi ("Basis", "signal"), _)), _), t) =>
adamc@568 1258 let
adamc@568 1259 val t = monoType env t
adamc@568 1260 in
adamc@820 1261 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun (t, (L'.TSignal t, loc)), loc),
adamc@820 1262 (L'.EAbs ("x", t, (L'.TSignal t, loc),
adamc@820 1263 (L'.ESignalReturn (L'.ERel 0, loc), loc)), loc)), loc),
adamc@568 1264 fm)
adamc@568 1265 end
adamc@820 1266 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), (L.CFfi ("Basis", "signal"), _)), _),
adamc@820 1267 t1), _), t2) =>
adamc@572 1268 let
adamc@572 1269 val t1 = monoType env t1
adamc@572 1270 val t2 = monoType env t2
adamc@572 1271 val un = (L'.TRecord [], loc)
adamc@572 1272 val mt1 = (L'.TSignal t1, loc)
adamc@572 1273 val mt2 = (L'.TSignal t2, loc)
adamc@572 1274 in
adamc@820 1275 ((L'.EAbs ("_", un, (L'.TFun (mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), mt2), loc)), loc),
adamc@820 1276 (L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), mt2), loc),
adamc@820 1277 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), mt2,
adamc@820 1278 (L'.ESignalBind ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@820 1279 loc),
adamc@572 1280 fm)
adamc@572 1281 end
adamc@574 1282 | L.ECApp ((L.EFfi ("Basis", "signal"), _), t) =>
adamc@574 1283 let
adamc@574 1284 val t = monoType env t
adamc@574 1285 in
adamc@574 1286 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TSignal t, loc),
adamc@574 1287 (L'.ESignalSource (L'.ERel 0, loc), loc)), loc),
adamc@574 1288 fm)
adamc@574 1289 end
adamc@568 1290
adamc@462 1291 | L.ECApp ((L.EFfi ("Basis", "getCookie"), _), t) =>
adamc@462 1292 let
adamc@462 1293 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1294 val un = (L'.TRecord [], loc)
adamc@462 1295 val t = monoType env t
adamc@462 1296 in
adamc@462 1297 ((L'.EAbs ("c", s, (L'.TFun (un, s), loc),
adamc@462 1298 (L'.EAbs ("_", un, s,
adamc@463 1299 (L'.EUnurlify ((L'.EFfiApp ("Basis", "get_cookie", [(L'.ERel 1, loc)]), loc),
adamc@463 1300 t),
adamc@463 1301 loc)), loc)), loc),
adamc@462 1302 fm)
adamc@462 1303 end
adamc@462 1304
adamc@462 1305 | L.ECApp ((L.EFfi ("Basis", "setCookie"), _), t) =>
adamc@462 1306 let
adamc@462 1307 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1308 val un = (L'.TRecord [], loc)
adamc@462 1309 val t = monoType env t
adamc@462 1310 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@462 1311 in
adamc@462 1312 ((L'.EAbs ("c", s, (L'.TFun (t, (L'.TFun (un, un), loc)), loc),
adamc@462 1313 (L'.EAbs ("v", t, (L'.TFun (un, un), loc),
adamc@462 1314 (L'.EAbs ("_", un, un,
adamc@764 1315 (L'.EFfiApp ("Basis", "set_cookie", [(L'.EPrim (Prim.String
adamc@764 1316 (Settings.getUrlPrefix ())),
adamc@466 1317 loc),
adamc@466 1318 (L'.ERel 2, loc),
adamc@466 1319 e]), loc)),
adamc@462 1320 loc)), loc)), loc),
adamc@462 1321 fm)
adamc@462 1322 end
adamc@462 1323
adamc@668 1324 | L.ECApp ((L.EFfi ("Basis", "channel"), _), t) =>
adamc@668 1325 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1326 (L'.EFfiApp ("Basis", "new_channel", [(L'.ERecord [], loc)]), loc)), loc),
adamc@668 1327 fm)
adamc@668 1328 | L.ECApp ((L.EFfi ("Basis", "send"), _), t) =>
adamc@668 1329 let
adamc@668 1330 val t = monoType env t
adamc@668 1331 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@668 1332 in
adamc@668 1333 ((L'.EAbs ("ch", (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1334 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@668 1335 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@668 1336 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@668 1337 (L'.EFfiApp ("Basis", "send",
adamc@668 1338 [(L'.ERel 2, loc),
adamc@668 1339 e]),
adamc@668 1340 loc)), loc)), loc)), loc),
adamc@668 1341 fm)
adamc@668 1342 end
adamc@668 1343
adamc@707 1344 | L.ECApp ((L.EFfi ("Basis", "no_primary_key"), _), _) =>
adamc@707 1345 ((L'.EPrim (Prim.String ""), loc),
adamc@707 1346 fm)
adamc@707 1347 | L.ECApp (
adamc@707 1348 (L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "primary_key"), _), _), _), t), _),
adamc@707 1349 nm), _),
adamc@707 1350 (L.CRecord (_, unique), _)) =>
adamc@707 1351 let
adamc@707 1352 val unique = (nm, t) :: unique
adamc@707 1353 val witnesses = (L'.TRecord (map (fn (nm, _) => (monoName env nm, (L'.TRecord [], loc))) unique), loc)
adamc@707 1354 in
adamc@707 1355 ((L'.EAbs ("_", witnesses, (L'.TFfi ("Basis", "string"), loc),
adamc@707 1356 (L'.EPrim (Prim.String
adamc@707 1357 (String.concatWith ", "
adamc@707 1358 (map (fn (x, _) => "uw_" ^ monoName env x) unique))),
adamc@707 1359 loc)), loc),
adamc@707 1360 fm)
adamc@707 1361 end
adamc@707 1362
adamc@704 1363 | L.ECApp ((L.EFfi ("Basis", "no_constraint"), _), _) =>
adamc@704 1364 ((L'.ERecord [], loc),
adamc@704 1365 fm)
adamc@705 1366 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "one_constraint"), _), _), _), _), _), (L.CName name, _)) =>
adamc@704 1367 ((L'.EAbs ("c",
adamc@704 1368 (L'.TFfi ("Basis", "string"), loc),
adamc@704 1369 (L'.TFfi ("Basis", "sql_constraints"), loc),
adamc@704 1370 (L'.ERecord [(name, (L'.ERel 0, loc), (L'.TFfi ("Basis", "string"), loc))], loc)), loc),
adamc@704 1371 fm)
adamc@705 1372 | L.ECApp (
adamc@705 1373 (L.ECApp (
adamc@705 1374 (L.ECApp (
adamc@705 1375 (L.EFfi ("Basis", "join_constraints"), _),
adamc@705 1376 _), _),
adamc@705 1377 _), _),
adamc@705 1378 _) =>
adamc@704 1379 let
adamc@704 1380 val constraints = (L'.TFfi ("Basis", "sql_constraints"), loc)
adamc@704 1381 in
adamc@704 1382 ((L'.EAbs ("cs1", constraints, (L'.TFun (constraints, constraints), loc),
adamc@704 1383 (L'.EAbs ("cs2", constraints, constraints,
adamc@704 1384 (L'.EStrcat ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@704 1385 fm)
adamc@704 1386 end
adamc@704 1387
adamc@705 1388 | L.ECApp (
adamc@705 1389 (L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "unique"), _), _), _), t), _),
adamc@705 1390 nm), _),
adamc@705 1391 (L.CRecord (_, unique), _)) =>
adamc@705 1392 let
adamc@705 1393 val unique = (nm, t) :: unique
adamc@705 1394 in
adamc@705 1395 ((L'.EPrim (Prim.String ("UNIQUE ("
adamc@705 1396 ^ String.concatWith ", " (map (fn (x, _) => "uw_" ^ monoName env x) unique)
adamc@705 1397 ^ ")")), loc),
adamc@705 1398 fm)
adamc@705 1399 end
adamc@704 1400
adamc@712 1401 | L.ECApp ((L.EFfi ("Basis", "linkable_same"), loc), _) =>
adamc@712 1402 ((L'.ERecord [], loc), fm)
adamc@712 1403 | L.ECApp ((L.EFfi ("Basis", "linkable_from_nullable"), loc), _) =>
adamc@712 1404 ((L'.ERecord [], loc), fm)
adamc@712 1405 | L.ECApp ((L.EFfi ("Basis", "linkable_to_nullable"), loc), _) =>
adamc@712 1406 ((L'.ERecord [], loc), fm)
adamc@712 1407
adamc@709 1408 | L.EFfi ("Basis", "mat_nil") =>
adamc@709 1409 let
adamc@709 1410 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1411 val stringE = (L'.EPrim (Prim.String ""), loc)
adamc@709 1412 in
adamc@709 1413 ((L'.ERecord [("1", stringE, string),
adamc@709 1414 ("2", stringE, string)], loc), fm)
adamc@709 1415 end
adamc@709 1416 | L.ECApp (
adamc@709 1417 (L.ECApp (
adamc@709 1418 (L.ECApp (
adamc@709 1419 (L.ECApp (
adamc@709 1420 (L.ECApp (
adamc@712 1421 (L.ECApp (
adamc@712 1422 (L.EFfi ("Basis", "mat_cons"), _),
adamc@712 1423 _), _),
adamc@709 1424 _), _),
adamc@709 1425 _), _),
adamc@709 1426 _), _),
adamc@709 1427 (L.CName nm1, _)), _),
adamc@709 1428 (L.CName nm2, _)) =>
adamc@709 1429 let
adamc@709 1430 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1431 val mat = (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 1432 in
adamc@712 1433 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun (mat, mat), loc),
adamc@712 1434 (L'.EAbs ("m", mat, mat,
adamc@712 1435 (L'.ECase ((L'.EField ((L'.ERel 0, loc), "1"), loc),
adamc@712 1436 [((L'.PPrim (Prim.String ""), loc),
adamc@712 1437 (L'.ERecord [("1", (L'.EPrim (Prim.String ("uw_" ^ nm1)),
adamc@712 1438 loc), string),
adamc@712 1439 ("2", (L'.EPrim (Prim.String ("uw_" ^ nm2)),
adamc@712 1440 loc), string)], loc)),
adamc@712 1441 ((L'.PWild, loc),
adamc@712 1442 (L'.ERecord [("1", (L'.EStrcat (
adamc@712 1443 (L'.EPrim (Prim.String ("uw_" ^ nm1 ^ ", ")),
adamc@712 1444 loc),
adamc@712 1445 (L'.EField ((L'.ERel 0, loc), "1"), loc)),
adamc@712 1446 loc), string),
adamc@712 1447 ("2", (L'.EStrcat (
adamc@712 1448 (L'.EPrim (Prim.String ("uw_" ^ nm2 ^ ", ")), loc),
adamc@712 1449 (L'.EField ((L'.ERel 0, loc), "2"), loc)),
adamc@712 1450 loc), string)],
adamc@712 1451 loc))],
adamc@712 1452 {disc = string,
adamc@712 1453 result = mat}), loc)), loc)), loc),
adamc@709 1454 fm)
adamc@709 1455 end
adamc@709 1456
adamc@709 1457 | L.ECApp ((L.EFfi ("Basis", "restrict"), _), _) => ((L'.EPrim (Prim.String "RESTRICT"), loc), fm)
adamc@709 1458 | L.ECApp ((L.EFfi ("Basis", "cascade"), _), _) => ((L'.EPrim (Prim.String "CASCADE"), loc), fm)
adamc@709 1459 | L.ECApp ((L.EFfi ("Basis", "no_action"), _), _) => ((L'.EPrim (Prim.String "NO ACTION"), loc), fm)
adamc@709 1460 | L.ECApp ((L.EFfi ("Basis", "set_null"), _), _) => ((L'.EPrim (Prim.String "SET NULL"), loc), fm)
adamc@709 1461
adamc@709 1462 | L.ECApp (
adamc@709 1463 (L.ECApp (
adamc@709 1464 (L.ECApp (
adamc@709 1465 (L.ECApp (
adamc@709 1466 (L.ECApp (
adamc@709 1467 (L.ECApp (
adamc@709 1468 (L.ECApp (
adamc@709 1469 (L.ECApp (
adamc@709 1470 (L.EFfi ("Basis", "foreign_key"), _),
adamc@709 1471 _), _),
adamc@709 1472 _), _),
adamc@709 1473 _), _),
adamc@709 1474 _), _),
adamc@709 1475 _), _),
adamc@709 1476 _), _),
adamc@709 1477 _), _),
adamc@709 1478 _) =>
adamc@709 1479 let
adamc@709 1480 val unit = (L'.TRecord [], loc)
adamc@709 1481 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1482 val mat = (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 1483 val recd = (L'.TRecord [("OnDelete", string),
adamc@709 1484 ("OnUpdate", string)], loc)
adamc@709 1485
adamc@709 1486 fun strcat [] = raise Fail "Monoize.strcat"
adamc@709 1487 | strcat [e] = e
adamc@709 1488 | strcat (e1 :: es) = (L'.EStrcat (e1, strcat es), loc)
adamc@709 1489
adamc@709 1490 fun prop (fd, kw) =
adamc@709 1491 (L'.ECase ((L'.EField ((L'.ERel 0, loc), fd), loc),
adamc@709 1492 [((L'.PPrim (Prim.String "NO ACTION"), loc),
adamc@709 1493 (L'.EPrim (Prim.String ""), loc)),
adamc@709 1494 ((L'.PWild, loc),
adamc@709 1495 strcat [(L'.EPrim (Prim.String (" ON " ^ kw ^ " ")), loc),
adamc@709 1496 (L'.EField ((L'.ERel 0, loc), fd), loc)])],
adamc@709 1497 {disc = string,
adamc@709 1498 result = string}), loc)
adamc@709 1499 in
adamc@709 1500 ((L'.EAbs ("m", mat, (L'.TFun (string, (L'.TFun (recd, string), loc)), loc),
adamc@709 1501 (L'.EAbs ("tab", string, (L'.TFun (recd, string), loc),
adamc@709 1502 (L'.EAbs ("pr", recd, string,
adamc@709 1503 strcat [(L'.EPrim (Prim.String "FOREIGN KEY ("), loc),
adamc@709 1504 (L'.EField ((L'.ERel 2, loc), "1"), loc),
adamc@709 1505 (L'.EPrim (Prim.String ") REFERENCES "), loc),
adamc@709 1506 (L'.ERel 1, loc),
adamc@709 1507 (L'.EPrim (Prim.String " ("), loc),
adamc@709 1508 (L'.EField ((L'.ERel 2, loc), "2"), loc),
adamc@709 1509 (L'.EPrim (Prim.String ")"), loc),
adamc@709 1510 prop ("OnDelete", "DELETE"),
adamc@709 1511 prop ("OnUpdate", "UPDATE")]), loc)), loc)), loc),
adamc@709 1512 fm)
adamc@709 1513 end
adamc@709 1514
adamc@714 1515 | L.ECApp ((L.EFfi ("Basis", "check"), _), _) =>
adamc@714 1516 let
adamc@714 1517 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@714 1518 in
adamc@714 1519 ((L'.EAbs ("e", string, string,
adamc@714 1520 (L'.EStrcat ((L'.EPrim (Prim.String "CHECK "), loc),
adamc@714 1521 (L'.EFfiApp ("Basis", "checkString",
adamc@714 1522 [(L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@714 1523 fm)
adamc@714 1524 end
adamc@714 1525
adamc@307 1526 | L.EFfiApp ("Basis", "dml", [e]) =>
adamc@307 1527 let
adamc@307 1528 val (e, fm) = monoExp (env, st, fm) e
adamc@307 1529 in
adamc@468 1530 ((L'.EDml e, loc),
adamc@307 1531 fm)
adamc@307 1532 end
adamc@308 1533
adamc@705 1534 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "insert"), _), fields), _), _) =>
adamc@307 1535 (case monoType env (L.TRecord fields, loc) of
adamc@307 1536 (L'.TRecord fields, _) =>
adamc@307 1537 let
adamc@307 1538 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@307 1539 val fields = map (fn (x, _) => (x, s)) fields
adamc@307 1540 val rt = (L'.TRecord fields, loc)
adamc@307 1541 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@307 1542 in
adamc@307 1543 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
adamc@307 1544 (L'.EAbs ("fs", rt, s,
adamc@598 1545 strcat [sc "INSERT INTO ",
adamc@598 1546 (L'.ERel 1, loc),
adamc@598 1547 sc " (",
adamc@598 1548 strcatComma (map (fn (x, _) => sc ("uw_" ^ x)) fields),
adamc@598 1549 sc ") VALUES (",
adamc@598 1550 strcatComma (map (fn (x, _) =>
adamc@598 1551 (L'.EField ((L'.ERel 0, loc),
adamc@598 1552 x), loc)) fields),
adamc@598 1553 sc ")"]), loc)), loc),
adamc@307 1554 fm)
adamc@307 1555 end
adamc@307 1556 | _ => poly ())
adamc@307 1557
adamc@705 1558 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), _), _), changed) =>
adamc@308 1559 (case monoType env (L.TRecord changed, loc) of
adamc@308 1560 (L'.TRecord changed, _) =>
adamc@308 1561 let
adamc@308 1562 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@308 1563 val changed = map (fn (x, _) => (x, s)) changed
adamc@308 1564 val rt = (L'.TRecord changed, loc)
adamc@308 1565 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@308 1566 in
adamc@308 1567 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@308 1568 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@308 1569 (L'.EAbs ("e", s, s,
adamc@598 1570 strcat [sc "UPDATE ",
adamc@598 1571 (L'.ERel 1, loc),
adamc@598 1572 sc " AS T SET ",
adamc@598 1573 strcatComma (map (fn (x, _) =>
adamc@598 1574 strcat [sc ("uw_" ^ x
adamc@598 1575 ^ " = "),
adamc@598 1576 (L'.EField
adamc@598 1577 ((L'.ERel 2,
adamc@598 1578 loc),
adamc@598 1579 x), loc)])
adamc@598 1580 changed),
adamc@598 1581 sc " WHERE ",
adamc@598 1582 (L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@308 1583 fm)
adamc@308 1584 end
adamc@308 1585 | _ => poly ())
adamc@308 1586
adamc@705 1587 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "delete"), _), _), _), _) =>
adamc@309 1588 let
adamc@309 1589 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@309 1590 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@309 1591 in
adamc@309 1592 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@309 1593 (L'.EAbs ("e", s, s,
adamc@598 1594 strcat [sc "DELETE FROM ",
adamc@598 1595 (L'.ERel 1, loc),
adamc@598 1596 sc " AS T WHERE ",
adamc@598 1597 (L'.ERel 0, loc)]), loc)), loc),
adamc@309 1598 fm)
adamc@309 1599 end
adamc@309 1600
adamc@252 1601 | L.ECApp (
adamc@252 1602 (L.ECApp (
adamc@252 1603 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 1604 exps), _),
adamc@252 1605 state) =>
adamc@252 1606 (case monoType env (L.TRecord exps, loc) of
adamc@252 1607 (L'.TRecord exps, _) =>
adamc@252 1608 let
adamc@252 1609 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1610 (case monoType env (L.TRecord xts, loc) of
adamc@252 1611 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1612 | _ => NONE)
adamc@252 1613 | _ => NONE) tables
adamc@252 1614 in
adamc@252 1615 if List.exists (fn x => x = NONE) tables then
adamc@252 1616 poly ()
adamc@252 1617 else
adamc@252 1618 let
adamc@252 1619 val tables = List.mapPartial (fn x => x) tables
adamc@252 1620 val state = monoType env state
adamc@252 1621 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1622 val un = (L'.TRecord [], loc)
adamc@252 1623
adamc@252 1624 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 1625 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 1626 (L'.TFun (state,
adamc@252 1627 (L'.TFun (un, state), loc)),
adamc@252 1628 loc)), loc)
adamc@252 1629
adamc@267 1630 val body' = (L'.EApp (
adamc@267 1631 (L'.EApp (
adamc@267 1632 (L'.EApp ((L'.ERel 4, loc),
adamc@267 1633 (L'.ERel 1, loc)), loc),
adamc@267 1634 (L'.ERel 0, loc)), loc),
adamc@267 1635 (L'.ERecord [], loc)), loc)
adamc@252 1636
adamc@252 1637 val body = (L'.EQuery {exps = exps,
adamc@252 1638 tables = tables,
adamc@252 1639 state = state,
adamc@252 1640 query = (L'.ERel 3, loc),
adamc@252 1641 body = body',
adamc@252 1642 initial = (L'.ERel 1, loc)},
adamc@252 1643 loc)
adamc@252 1644 in
adamc@252 1645 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 1646 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 1647 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 1648 (L'.EAbs ("_", un, state,
adamc@252 1649 body), loc)), loc)), loc)), loc), fm)
adamc@252 1650 end
adamc@252 1651 end
adamc@252 1652 | _ => poly ())
adamc@252 1653
adamc@252 1654 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 1655 let
adamc@252 1656 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1657 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1658 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1659 in
adamc@252 1660 ((L'.EAbs ("r",
adamc@252 1661 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 1662 s,
adamc@598 1663 strcat [gf "Rows",
adamc@598 1664 (L'.ECase (gf "OrderBy",
adamc@598 1665 [((L'.PPrim (Prim.String ""), loc), sc ""),
adamc@598 1666 ((L'.PWild, loc),
adamc@598 1667 strcat [sc " ORDER BY ",
adamc@598 1668 gf "OrderBy"])],
adamc@598 1669 {disc = s, result = s}), loc),
adamc@598 1670 gf "Limit",
adamc@598 1671 gf "Offset"]), loc), fm)
adamc@252 1672 end
adamc@252 1673
adamc@252 1674 | L.ECApp (
adamc@252 1675 (L.ECApp (
adamc@252 1676 (L.ECApp (
adamc@252 1677 (L.ECApp (
adamc@252 1678 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 1679 (L.CRecord (_, tables), _)), _),
adamc@252 1680 (L.CRecord (_, grouped), _)), _),
adamc@252 1681 (L.CRecord (_, stables), _)), _),
adamc@252 1682 sexps) =>
adamc@252 1683 let
adamc@252 1684 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1685 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1686 val un = (L'.TRecord [], loc)
adamc@252 1687 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1688
adamc@252 1689 fun doTables tables =
adamc@252 1690 let
adamc@252 1691 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1692 (case monoType env (L.TRecord xts, loc) of
adamc@252 1693 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1694 | _ => NONE)
adamc@252 1695 | _ => NONE) tables
adamc@252 1696 in
adamc@252 1697 if List.exists (fn x => x = NONE) tables then
adamc@252 1698 NONE
adamc@252 1699 else
adamc@260 1700 let
adamc@260 1701 val tables = List.mapPartial (fn x => x) tables
adamc@260 1702 val tables = ListMergeSort.sort
adamc@260 1703 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1704 tables
adamc@260 1705 val tables = map (fn (x, xts) =>
adamc@260 1706 (x, ListMergeSort.sort
adamc@260 1707 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1708 xts)) tables
adamc@260 1709 in
adamc@260 1710 SOME tables
adamc@260 1711 end
adamc@252 1712 end
adamc@252 1713 in
adamc@252 1714 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 1715 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@441 1716 let
adamc@441 1717 val sexps = ListMergeSort.sort
adamc@441 1718 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) sexps
adamc@441 1719 in
adamc@441 1720 ((L'.EAbs ("r",
adamc@748 1721 (L'.TRecord [("From", s),
adamc@441 1722 ("Where", s),
adamc@441 1723 ("GroupBy", un),
adamc@441 1724 ("Having", s),
adamc@441 1725 ("SelectFields", un),
adamc@441 1726 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@441 1727 loc),
adamc@441 1728 s,
adamc@598 1729 strcat [sc "SELECT ",
adamc@598 1730 strcatComma (map (fn (x, t) =>
adamc@598 1731 strcat [
adamc@598 1732 (L'.EField (gf "SelectExps", x), loc),
adamc@598 1733 sc (" AS _" ^ x)
adamc@598 1734 ]) sexps
adamc@598 1735 @ map (fn (x, xts) =>
adamc@598 1736 strcatComma
adamc@598 1737 (map (fn (x', _) =>
adamc@598 1738 sc (x ^ ".uw_" ^ x'))
adamc@598 1739 xts)) stables),
adamc@598 1740 sc " FROM ",
adamc@748 1741 gf "From",
adamc@598 1742 (L'.ECase (gf "Where",
adamc@598 1743 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@598 1744 sc ""),
adamc@598 1745 ((L'.PWild, loc),
adamc@598 1746 strcat [sc " WHERE ", gf "Where"])],
adamc@598 1747 {disc = s,
adamc@598 1748 result = s}), loc),
adamc@598 1749
adamc@598 1750 if List.all (fn (x, xts) =>
adamc@598 1751 case List.find (fn (x', _) => x' = x) grouped of
adamc@598 1752 NONE => List.null xts
adamc@598 1753 | SOME (_, xts') =>
adamc@598 1754 List.all (fn (x, _) =>
adamc@598 1755 List.exists (fn (x', _) => x' = x)
adamc@598 1756 xts') xts) tables then
adamc@598 1757 sc ""
adamc@598 1758 else
adamc@598 1759 strcat [
adamc@598 1760 sc " GROUP BY ",
adamc@598 1761 strcatComma (map (fn (x, xts) =>
adamc@598 1762 strcatComma
adamc@598 1763 (map (fn (x', _) =>
adamc@598 1764 sc (x ^ ".uw_" ^ x'))
adamc@598 1765 xts)) grouped)
adamc@598 1766 ],
adamc@259 1767
adamc@598 1768 (L'.ECase (gf "Having",
adamc@598 1769 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@598 1770 sc ""),
adamc@598 1771 ((L'.PWild, loc),
adamc@598 1772 strcat [sc " HAVING ", gf "Having"])],
adamc@598 1773 {disc = s,
adamc@598 1774 result = s}), loc)
adamc@441 1775 ]), loc),
adamc@441 1776 fm)
adamc@441 1777 end
adamc@252 1778 | _ => poly ()
adamc@252 1779 end
adamc@252 1780
adamc@252 1781 | L.ECApp (
adamc@252 1782 (L.ECApp (
adamc@252 1783 (L.ECApp (
adamc@252 1784 (L.ECApp (
adamc@252 1785 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 1786 _), _),
adamc@252 1787 _), _),
adamc@252 1788 _), _),
adamc@252 1789 t) =>
adamc@252 1790 let
adamc@252 1791 val t = monoType env t
adamc@252 1792 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1793 in
adamc@252 1794 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 1795 (L'.ERel 0, loc)), loc), fm)
adamc@252 1796 end
adamc@252 1797
adamc@253 1798 | L.EFfi ("Basis", "sql_int") =>
adamc@253 1799 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1800 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1801 fm)
adamc@253 1802 | L.EFfi ("Basis", "sql_float") =>
adamc@253 1803 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1804 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1805 fm)
adamc@253 1806 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 1807 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1808 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1809 fm)
adamc@253 1810 | L.EFfi ("Basis", "sql_string") =>
adamc@253 1811 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1812 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1813 fm)
adamc@439 1814 | L.EFfi ("Basis", "sql_time") =>
adamc@439 1815 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@439 1816 (L'.EFfiApp ("Basis", "sqlifyTime", [(L'.ERel 0, loc)]), loc)), loc),
adamc@439 1817 fm)
adamc@737 1818 | L.EFfi ("Basis", "sql_blob") =>
adamc@737 1819 ((L'.EAbs ("x", (L'.TFfi ("Basis", "blob"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@737 1820 (L'.EFfiApp ("Basis", "sqlifyBlob", [(L'.ERel 0, loc)]), loc)), loc),
adamc@737 1821 fm)
adamc@678 1822 | L.ECApp ((L.EFfi ("Basis", "sql_channel"), _), _) =>
adamc@678 1823 ((L'.EAbs ("x", (L'.TFfi ("Basis", "channel"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@678 1824 (L'.EFfiApp ("Basis", "sqlifyChannel", [(L'.ERel 0, loc)]), loc)), loc),
adamc@678 1825 fm)
adamc@682 1826 | L.EFfi ("Basis", "sql_client") =>
adamc@682 1827 ((L'.EAbs ("x", (L'.TFfi ("Basis", "client"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@682 1828 (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)), loc),
adamc@682 1829 fm)
adamc@676 1830 | L.ECApp ((L.EFfi ("Basis", "sql_prim"), _), t) =>
adamc@676 1831 let
adamc@676 1832 val t = monoType env t
adamc@676 1833 val tf = (L'.TFun (t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@676 1834 in
adamc@676 1835 ((L'.EAbs ("f", tf, tf, (L'.ERel 0, loc)), loc),
adamc@676 1836 fm)
adamc@676 1837 end
adamc@676 1838 | L.ECApp ((L.EFfi ("Basis", "sql_option_prim"), _), t) =>
adamc@676 1839 let
adamc@676 1840 val t = monoType env t
adamc@676 1841 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@676 1842 in
adamc@676 1843 ((L'.EAbs ("f",
adamc@676 1844 (L'.TFun (t, s), loc),
adamc@676 1845 (L'.TFun ((L'.TOption t, loc), s), loc),
adamc@676 1846 (L'.EAbs ("x",
adamc@676 1847 (L'.TOption t, loc),
adamc@676 1848 s,
adamc@676 1849 (L'.ECase ((L'.ERel 0, loc),
adamc@676 1850 [((L'.PNone t, loc),
adamc@676 1851 (L'.EPrim (Prim.String "NULL"), loc)),
adamc@676 1852 ((L'.PSome (t, (L'.PVar ("y", t), loc)), loc),
adamc@676 1853 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc))],
adamc@676 1854 {disc = (L'.TOption t, loc),
adamc@676 1855 result = s}), loc)), loc)), loc),
adamc@676 1856 fm)
adamc@676 1857 end
adamc@253 1858
adamc@750 1859 | L.ECApp ((L.EFfi ("Basis", "nullify_option"), _), _) =>
adamc@750 1860 ((L'.ERecord [], loc), fm)
adamc@750 1861 | L.ECApp ((L.EFfi ("Basis", "nullify_prim"), _), _) =>
adamc@750 1862 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@750 1863 (L'.ERecord [], loc)), loc),
adamc@750 1864 fm)
adamc@750 1865
adamc@252 1866 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 1867 ((L'.ERecord [], loc), fm)
adamc@252 1868 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 1869 ((L'.ERecord [], loc), fm)
adamc@252 1870
adamc@753 1871 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "fieldsOf_table"), _), _), _), _) =>
adamc@753 1872 ((L'.ERecord [], loc), fm)
adamc@753 1873 | L.ECApp ((L.EFfi ("Basis", "fieldsOf_view"), _), _) =>
adamc@753 1874 ((L'.ERecord [], loc), fm)
adamc@753 1875
adamc@753 1876 | L.ECApp ((L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_table"), _), _), _), _), _), _), _),
adamc@753 1877 (L.CName name, _)) =>
adamc@748 1878 let
adamc@748 1879 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@748 1880 in
adamc@748 1881 ((L'.EAbs ("tab", s, s,
adamc@748 1882 strcat [(L'.ERel 0, loc),
adamc@748 1883 (L'.EPrim (Prim.String (" AS " ^ name)), loc)]), loc),
adamc@748 1884 fm)
adamc@748 1885 end
adamc@748 1886 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_comma"), _), _), _), _) =>
adamc@748 1887 let
adamc@748 1888 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@748 1889 in
adamc@748 1890 ((L'.EAbs ("tab1", s, (L'.TFun (s, s), loc),
adamc@748 1891 (L'.EAbs ("tab2", s, s,
adamc@748 1892 strcat [(L'.ERel 1, loc),
adamc@748 1893 (L'.EPrim (Prim.String ", "), loc),
adamc@748 1894 (L'.ERel 0, loc)]), loc)), loc),
adamc@748 1895 fm)
adamc@748 1896 end
adamc@749 1897 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_inner_join"), _), _), _), _) =>
adamc@749 1898 let
adamc@749 1899 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@749 1900 in
adamc@749 1901 ((L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@749 1902 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@749 1903 (L'.EAbs ("on", s, s,
adamc@749 1904 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@749 1905 (L'.ERel 2, loc),
adamc@749 1906 (L'.EPrim (Prim.String " JOIN "), loc),
adamc@749 1907 (L'.ERel 1, loc),
adamc@749 1908 (L'.EPrim (Prim.String " ON "), loc),
adamc@749 1909 (L'.ERel 0, loc),
adamc@749 1910 (L'.EPrim (Prim.String ")"), loc)]), loc)), loc)), loc),
adamc@749 1911 fm)
adamc@749 1912 end
adamc@750 1913 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_left_join"), _), _), _), (L.CRecord (_, right), _)) =>
adamc@750 1914 let
adamc@750 1915 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@750 1916 in
adamc@750 1917 ((L'.EAbs ("_", outerRec right,
adamc@750 1918 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@750 1919 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@750 1920 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@750 1921 (L'.EAbs ("on", s, s,
adamc@750 1922 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@750 1923 (L'.ERel 2, loc),
adamc@750 1924 (L'.EPrim (Prim.String " LEFT JOIN "), loc),
adamc@750 1925 (L'.ERel 1, loc),
adamc@750 1926 (L'.EPrim (Prim.String " ON "), loc),
adamc@750 1927 (L'.ERel 0, loc),
adamc@750 1928 (L'.EPrim (Prim.String ")"), loc)]),
adamc@750 1929 loc)), loc)), loc)), loc),
adamc@750 1930 fm)
adamc@750 1931 end
adamc@751 1932 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_right_join"), _), (L.CRecord (_, left), _)), _), _) =>
adamc@751 1933 let
adamc@751 1934 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@751 1935 in
adamc@751 1936 ((L'.EAbs ("_", outerRec left,
adamc@751 1937 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@751 1938 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@751 1939 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@751 1940 (L'.EAbs ("on", s, s,
adamc@751 1941 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@751 1942 (L'.ERel 2, loc),
adamc@751 1943 (L'.EPrim (Prim.String " RIGHT JOIN "), loc),
adamc@751 1944 (L'.ERel 1, loc),
adamc@751 1945 (L'.EPrim (Prim.String " ON "), loc),
adamc@751 1946 (L'.ERel 0, loc),
adamc@751 1947 (L'.EPrim (Prim.String ")"), loc)]),
adamc@751 1948 loc)), loc)), loc)), loc),
adamc@751 1949 fm)
adamc@751 1950 end
adamc@751 1951 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_full_join"), _), (L.CRecord (_, left), _)), _),
adamc@751 1952 (L.CRecord (_, right), _)) =>
adamc@751 1953 let
adamc@751 1954 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@751 1955 in
adamc@751 1956 ((L'.EAbs ("_", outerRec (left @ right),
adamc@751 1957 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@751 1958 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@751 1959 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@751 1960 (L'.EAbs ("on", s, s,
adamc@751 1961 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@751 1962 (L'.ERel 2, loc),
adamc@751 1963 (L'.EPrim (Prim.String " FULL JOIN "), loc),
adamc@751 1964 (L'.ERel 1, loc),
adamc@751 1965 (L'.EPrim (Prim.String " ON "), loc),
adamc@751 1966 (L'.ERel 0, loc),
adamc@751 1967 (L'.EPrim (Prim.String ")"), loc)]),
adamc@751 1968 loc)), loc)), loc)), loc),
adamc@751 1969 fm)
adamc@751 1970 end
adamc@748 1971
adamc@252 1972 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 1973 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@261 1974 | L.ECApp (
adamc@261 1975 (L.ECApp (
adamc@261 1976 (L.ECApp (
adamc@261 1977 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adamc@261 1978 _), _),
adamc@261 1979 _), _),
adamc@261 1980 _) =>
adamc@261 1981 let
adamc@261 1982 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 1983 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@261 1984 in
adamc@268 1985 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@268 1986 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adamc@268 1987 (L'.EAbs ("e2", s, s,
adamc@268 1988 (L'.ECase ((L'.ERel 0, loc),
adamc@268 1989 [((L'.PPrim (Prim.String ""), loc),
adamc@598 1990 strcat [(L'.ERel 2, loc),
adamc@598 1991 (L'.ERel 1, loc)]),
adamc@268 1992 ((L'.PWild, loc),
adamc@598 1993 strcat [(L'.ERel 2, loc),
adamc@598 1994 (L'.ERel 1, loc),
adamc@598 1995 sc ", ",
adamc@598 1996 (L'.ERel 0, loc)])],
adamc@268 1997 {disc = s, result = s}), loc)), loc)), loc)), loc),
adamc@261 1998 fm)
adamc@261 1999 end
adamc@252 2000
adamc@252 2001 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 2002 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@262 2003 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
adamc@262 2004 let
adamc@262 2005 val (e, fm) = monoExp (env, st, fm) e
adamc@262 2006 in
adamc@598 2007 (strcat [
adamc@262 2008 (L'.EPrim (Prim.String " LIMIT "), loc),
adamc@262 2009 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@262 2010 ],
adamc@262 2011 fm)
adamc@262 2012 end
adamc@262 2013
adamc@252 2014 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 2015 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@263 2016 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
adamc@263 2017 let
adamc@263 2018 val (e, fm) = monoExp (env, st, fm) e
adamc@263 2019 in
adamc@598 2020 (strcat [
adamc@263 2021 (L'.EPrim (Prim.String " OFFSET "), loc),
adamc@263 2022 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@263 2023 ],
adamc@263 2024 fm)
adamc@263 2025 end
adamc@253 2026
adamc@559 2027 | L.ECApp ((L.EFfi ("Basis", "sql_eq"), _), _) =>
adamc@253 2028 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@559 2029 | L.ECApp ((L.EFfi ("Basis", "sql_ne"), _), _) =>
adamc@253 2030 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@559 2031 | L.ECApp ((L.EFfi ("Basis", "sql_lt"), _), _) =>
adamc@253 2032 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@559 2033 | L.ECApp ((L.EFfi ("Basis", "sql_le"), _), _) =>
adamc@253 2034 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@559 2035 | L.ECApp ((L.EFfi ("Basis", "sql_gt"), _), _) =>
adamc@253 2036 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@559 2037 | L.ECApp ((L.EFfi ("Basis", "sql_ge"), _), _) =>
adamc@253 2038 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 2039
adamc@559 2040 | L.ECApp ((L.EFfi ("Basis", "sql_plus"), _), _) =>
adamc@559 2041 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 2042 (L'.EPrim (Prim.String "+"), loc)), loc), fm)
adamc@559 2043 | L.ECApp ((L.EFfi ("Basis", "sql_minus"), _), _) =>
adamc@559 2044 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 2045 (L'.EPrim (Prim.String "-"), loc)), loc), fm)
adamc@559 2046 | L.ECApp ((L.EFfi ("Basis", "sql_times"), _), _) =>
adamc@559 2047 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 2048 (L'.EPrim (Prim.String "*"), loc)), loc), fm)
adamc@559 2049 | L.ECApp ((L.EFfi ("Basis", "sql_div"), _), _) =>
adamc@559 2050 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 2051 (L'.EPrim (Prim.String "/"), loc)), loc), fm)
adamc@559 2052 | L.EFfi ("Basis", "sql_mod") =>
adamc@559 2053 ((L'.EPrim (Prim.String "%"), loc), fm)
adamc@559 2054
adamc@253 2055 | L.ECApp (
adamc@253 2056 (L.ECApp (
adamc@253 2057 (L.ECApp (
adamc@253 2058 (L.ECApp (
adamc@254 2059 (L.ECApp (
adamc@264 2060 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 2061 _), _),
adamc@264 2062 _), _),
adamc@264 2063 _), _),
adamc@264 2064 _), _),
adamc@264 2065 _) =>
adamc@264 2066 let
adamc@264 2067 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 2068 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 2069 in
adamc@264 2070 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 2071 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@598 2072 strcat [sc "(",
adamc@598 2073 (L'.ERel 1, loc),
adamc@598 2074 sc " ",
adamc@598 2075 (L'.ERel 0, loc),
adamc@598 2076 sc ")"]), loc)), loc),
adamc@264 2077 fm)
adamc@264 2078 end
adamc@264 2079 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@559 2080 | L.ECApp ((L.EFfi ("Basis", "sql_neg"), _), _) =>
adamc@559 2081 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 2082 (L'.EPrim (Prim.String "-"), loc)), loc), fm)
adamc@264 2083
adamc@264 2084 | L.ECApp (
adamc@264 2085 (L.ECApp (
adamc@264 2086 (L.ECApp (
adamc@264 2087 (L.ECApp (
adamc@264 2088 (L.ECApp (
adamc@254 2089 (L.ECApp (
adamc@254 2090 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 2091 _), _),
adamc@254 2092 _), _),
adamc@254 2093 _), _),
adamc@254 2094 _), _),
adamc@254 2095 _), _),
adamc@254 2096 _) =>
adamc@254 2097 let
adamc@254 2098 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 2099 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 2100 in
adamc@254 2101 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 2102 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 2103 (L'.EAbs ("e2", s, s,
adamc@598 2104 strcat [sc "(",
adamc@598 2105 (L'.ERel 1, loc),
adamc@598 2106 sc " ",
adamc@598 2107 (L'.ERel 2, loc),
adamc@598 2108 sc " ",
adamc@598 2109 (L'.ERel 0, loc),
adamc@598 2110 sc ")"]), loc)), loc)), loc),
adamc@254 2111 fm)
adamc@254 2112 end
adamc@254 2113 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 2114 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 2115
adamc@254 2116 | L.ECApp (
adamc@254 2117 (L.ECApp (
adamc@254 2118 (L.ECApp (
adamc@254 2119 (L.ECApp (
adamc@253 2120 (L.ECApp (
adamc@253 2121 (L.ECApp (
adamc@253 2122 (L.ECApp (
adamc@253 2123 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 2124 _), _),
adamc@253 2125 _), _),
adamc@253 2126 _), _),
adamc@253 2127 _), _),
adamc@253 2128 _), _),
adamc@253 2129 (L.CName tab, _)), _),
adamc@311 2130 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
adamc@260 2131
adamc@260 2132 | L.ECApp (
adamc@260 2133 (L.ECApp (
adamc@260 2134 (L.ECApp (
adamc@260 2135 (L.ECApp (
adamc@261 2136 (L.ECApp (
adamc@261 2137 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 2138 _), _),
adamc@261 2139 _), _),
adamc@261 2140 _), _),
adamc@261 2141 _), _),
adamc@261 2142 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 2143
adamc@261 2144 | L.ECApp (
adamc@261 2145 (L.ECApp (
adamc@261 2146 (L.ECApp (
adamc@261 2147 (L.ECApp (
adamc@260 2148 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 2149 _), _),
adamc@260 2150 _), _),
adamc@260 2151 _), _),
adamc@260 2152 _) =>
adamc@260 2153 let
adamc@260 2154 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 2155 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 2156 in
adamc@260 2157 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 2158 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 2159 (L'.EAbs ("e2", s, s,
adamc@598 2160 strcat [sc "((",
adamc@598 2161 (L'.ERel 1, loc),
adamc@598 2162 sc ") ",
adamc@598 2163 (L'.ERel 2, loc),
adamc@598 2164 sc " (",
adamc@598 2165 (L'.ERel 0, loc),
adamc@598 2166 sc "))"]), loc)), loc)), loc),
adamc@260 2167 fm)
adamc@260 2168 end
adamc@260 2169
adamc@260 2170 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 2171 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 2172 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 2173
adamc@265 2174 | L.ECApp (
adamc@265 2175 (L.ECApp (
adamc@265 2176 (L.ECApp (
adamc@265 2177 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 2178 _), _),
adamc@265 2179 _), _),
adamc@544 2180 _) => ((L'.EPrim (Prim.String "COUNT(*)"), loc),
adamc@265 2181 fm)
adamc@266 2182
adamc@266 2183 | L.ECApp (
adamc@266 2184 (L.ECApp (
adamc@266 2185 (L.ECApp (
adamc@266 2186 (L.ECApp (
adamc@266 2187 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 2188 _), _),
adamc@266 2189 _), _),
adamc@266 2190 _), _),
adamc@266 2191 _) =>
adamc@266 2192 let
adamc@266 2193 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 2194 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 2195 in
adamc@266 2196 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 2197 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@598 2198 strcat [(L'.ERel 1, loc),
adamc@598 2199 sc "(",
adamc@598 2200 (L'.ERel 0, loc),
adamc@598 2201 sc ")"]), loc)), loc),
adamc@266 2202 fm)
adamc@266 2203 end
adamc@266 2204
adamc@266 2205 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 2206 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 2207 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 2208 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2209 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 2210 fm)
adamc@266 2211 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 2212 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2213 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 2214 fm)
adamc@266 2215
adamc@559 2216 | L.EFfi ("Basis", "sql_arith_int") => ((L'.ERecord [], loc), fm)
adamc@559 2217 | L.EFfi ("Basis", "sql_arith_float") => ((L'.ERecord [], loc), fm)
adamc@559 2218
adamc@266 2219 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 2220 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 2221 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 2222 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 2223 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2224 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 2225 fm)
adamc@266 2226 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 2227 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2228 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 2229 fm)
adamc@266 2230
adamc@268 2231 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 2232 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 2233
adamc@441 2234 | L.ECApp (
adamc@441 2235 (L.ECApp (
adamc@441 2236 (L.ECApp (
adamc@441 2237 (L.ECApp (
adamc@441 2238 (L.EFfi ("Basis", "sql_nfunc"), _),
adamc@441 2239 _), _),
adamc@441 2240 _), _),
adamc@441 2241 _), _),
adamc@441 2242 _) =>
adamc@441 2243 let
adamc@441 2244 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@441 2245 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@441 2246 in
adamc@441 2247 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc),
adamc@441 2248 fm)
adamc@441 2249 end
adamc@441 2250 | L.EFfi ("Basis", "sql_current_timestamp") => ((L'.EPrim (Prim.String "CURRENT_TIMESTAMP"), loc), fm)
adamc@441 2251
adamc@746 2252 | L.ECApp (
adamc@746 2253 (L.ECApp (
adamc@746 2254 (L.ECApp (
adamc@746 2255 (L.ECApp (
adamc@746 2256 (L.ECApp (
adamc@746 2257 (L.EFfi ("Basis", "sql_ufunc"), _),
adamc@746 2258 _), _),
adamc@746 2259 _), _),
adamc@746 2260 _), _),
adamc@746 2261 _), _),
adamc@746 2262 _) =>
adamc@746 2263 let
adamc@746 2264 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@746 2265 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@746 2266 in
adamc@746 2267 ((L'.EAbs ("f", s, (L'.TFun (s, s), loc),
adamc@746 2268 (L'.EAbs ("x", s, s,
adamc@746 2269 strcat [(L'.ERel 1, loc),
adamc@746 2270 sc "(",
adamc@746 2271 (L'.ERel 0, loc),
adamc@746 2272 sc ")"]), loc)), loc),
adamc@746 2273 fm)
adamc@746 2274 end
adamc@746 2275 | L.EFfi ("Basis", "sql_octet_length") => ((L'.EPrim (Prim.String "octet_length"), loc), fm)
adamc@746 2276
adamc@470 2277 | (L.ECApp (
adamc@470 2278 (L.ECApp (
adamc@470 2279 (L.ECApp (
adamc@470 2280 (L.ECApp (
adamc@470 2281 (L.EFfi ("Basis", "sql_is_null"), _), _),
adamc@470 2282 _), _),
adamc@470 2283 _), _),
adamc@470 2284 _), _)) =>
adamc@470 2285 let
adamc@470 2286 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@470 2287 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@470 2288 in
adamc@470 2289 ((L'.EAbs ("s", s, s,
adamc@598 2290 strcat [sc "(",
adamc@598 2291 (L'.ERel 0, loc),
adamc@598 2292 sc " IS NULL)"]), loc),
adamc@470 2293 fm)
adamc@470 2294 end
adamc@470 2295
adamc@338 2296 | L.EFfiApp ("Basis", "nextval", [e]) =>
adamc@338 2297 let
adamc@338 2298 val (e, fm) = monoExp (env, st, fm) e
adamc@338 2299 in
adamc@465 2300 ((L'.ENextval e, loc), fm)
adamc@338 2301 end
adamc@338 2302
adamc@139 2303 | L.EApp (
adamc@139 2304 (L.ECApp (
adamc@720 2305 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 2306 _), _),
adamc@179 2307 se) =>
adamc@179 2308 let
adamc@179 2309 val (se, fm) = monoExp (env, st, fm) se
adamc@179 2310 in
adamc@179 2311 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 2312 end
adamc@179 2313
adamc@95 2314 | L.EApp (
adamc@95 2315 (L.EApp (
adamc@720 2316 (L.ECApp (
adamc@720 2317 (L.ECApp (
adamc@95 2318 (L.ECApp (
adamc@139 2319 (L.ECApp (
adamc@720 2320 (L.EFfi ("Basis", "join"),
adamc@720 2321 _), _), _),
adamc@139 2322 _), _),
adamc@720 2323 _), _),
adamc@720 2324 _), _),
adamc@720 2325 xml1), _),
adamc@720 2326 xml2) =>
adamc@179 2327 let
adamc@179 2328 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 2329 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 2330 in
adamc@179 2331 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 2332 end
adamc@95 2333
adamc@95 2334 | L.EApp (
adamc@95 2335 (L.EApp (
adamc@104 2336 (L.EApp (
adamc@721 2337 (L.EApp (
adamc@720 2338 (L.ECApp (
adamc@104 2339 (L.ECApp (
adamc@104 2340 (L.ECApp (
adamc@139 2341 (L.ECApp (
adamc@139 2342 (L.ECApp (
adamc@139 2343 (L.ECApp (
adamc@139 2344 (L.ECApp (
adamc@721 2345 (L.ECApp (
adamc@721 2346 (L.EFfi ("Basis", "tag"),
adamc@721 2347 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@721 2348 class), _),
adamc@720 2349 attrs), _),
adamc@720 2350 tag), _),
adamc@95 2351 xml) =>
adamc@95 2352 let
adamc@140 2353 fun getTag' (e, _) =
adamc@140 2354 case e of
adamc@143 2355 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 2356 | L.ECApp (e, t) => let
adamc@143 2357 val (tag, ts) = getTag' e
adamc@143 2358 in
adamc@143 2359 (tag, ts @ [t])
adamc@143 2360 end
adamc@140 2361 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 2362 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 2363 ("", []))
adamc@140 2364
adamc@95 2365 fun getTag (e, _) =
adamc@95 2366 case e of
adamc@143 2367 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 2368 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 2369 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 2370 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 2371 ("", []))
adamc@95 2372
adamc@143 2373 val (tag, targs) = getTag tag
adamc@95 2374
adamc@179 2375 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@598 2376 val attrs = case #1 attrs of
adamc@598 2377 L'.ERecord xes => xes
adamc@598 2378 | _ => raise Fail "Non-record attributes!"
adamc@104 2379
adamc@717 2380 val attrs =
adamc@717 2381 if List.exists (fn ("Link", _, _) => true
adamc@717 2382 | _ => false) attrs then
adamc@717 2383 List.filter (fn ("Href", _, _) => false
adamc@717 2384 | _ => true) attrs
adamc@717 2385 else
adamc@717 2386 attrs
adamc@717 2387
adamc@668 2388 fun findOnload (attrs, acc) =
adamc@668 2389 case attrs of
adamc@668 2390 [] => (NONE, acc)
adamc@668 2391 | ("Onload", e, _) :: rest => (SOME e, List.revAppend (acc, rest))
adamc@668 2392 | x :: rest => findOnload (rest, x :: acc)
adamc@679 2393
adamc@668 2394 val (onload, attrs) = findOnload (attrs, [])
adamc@668 2395
adamc@606 2396 fun lowercaseFirst "" = ""
adamc@606 2397 | lowercaseFirst s = String.str (Char.toLower (String.sub (s, 0)))
adamc@606 2398 ^ String.extract (s, 1, NONE)
adamc@606 2399
adamc@721 2400 val (class, fm) = monoExp (env, st, fm) class
adamc@721 2401
adamc@143 2402 fun tagStart tag =
adamc@598 2403 let
adamc@721 2404 val t = (L'.TFfi ("Basis", "string"), loc)
adamc@598 2405 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@721 2406
adamc@721 2407 val s = (L'.ECase (class,
adamc@721 2408 [((L'.PNone t, loc),
adamc@721 2409 s),
adamc@721 2410 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc),
adamc@721 2411 (L'.EStrcat (s,
adamc@721 2412 (L'.EStrcat ((L'.EPrim (Prim.String " class=\""), loc),
adamc@721 2413 (L'.EStrcat ((L'.ERel 0, loc),
adamc@721 2414 (L'.EPrim (Prim.String "\""), loc)),
adamc@721 2415 loc)), loc)), loc))],
adamc@721 2416 {disc = (L'.TOption t, loc),
adamc@721 2417 result = t}), loc)
adamc@598 2418 in
adamc@598 2419 foldl (fn (("Action", _, _), acc) => acc
adamc@598 2420 | (("Source", _, _), acc) => acc
adamc@598 2421 | ((x, e, t), (s, fm)) =>
adamc@598 2422 case t of
adamc@598 2423 (L'.TFfi ("Basis", "bool"), _) =>
adamc@598 2424 let
adamc@598 2425 val s' = " " ^ lowercaseFirst x
adamc@598 2426 in
adamc@598 2427 ((L'.ECase (e,
adamc@598 2428 [((L'.PCon (L'.Enum,
adamc@598 2429 L'.PConFfi {mod = "Basis",
adamc@598 2430 datatyp = "bool",
adamc@598 2431 con = "True",
adamc@598 2432 arg = NONE},
adamc@598 2433 NONE), loc),
adamc@598 2434 (L'.EStrcat (s,
adamc@598 2435 (L'.EPrim (Prim.String s'), loc)), loc)),
adamc@598 2436 ((L'.PCon (L'.Enum,
adamc@598 2437 L'.PConFfi {mod = "Basis",
adamc@598 2438 datatyp = "bool",
adamc@598 2439 con = "False",
adamc@598 2440 arg = NONE},
adamc@598 2441 NONE), loc),
adamc@598 2442 s)],
adamc@598 2443 {disc = (L'.TFfi ("Basis", "bool"), loc),
adamc@598 2444 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@598 2445 fm)
adamc@598 2446 end
adamc@598 2447 | (L'.TFun _, _) =>
adamc@598 2448 let
adamc@598 2449 val s' = " " ^ lowercaseFirst x ^ "='"
adamc@651 2450 val e = (L'.EApp (e, (L'.ERecord [], loc)), loc)
adamc@598 2451 in
adamc@598 2452 ((L'.EStrcat (s,
adamc@598 2453 (L'.EStrcat (
adamc@598 2454 (L'.EPrim (Prim.String s'), loc),
adamc@598 2455 (L'.EStrcat (
adamc@815 2456 (L'.EJavaScript (L'.Attribute, e), loc),
adamc@598 2457 (L'.EPrim (Prim.String "'"), loc)), loc)),
adamc@598 2458 loc)), loc),
adamc@598 2459 fm)
adamc@598 2460 end
adamc@598 2461 | _ =>
adamc@598 2462 let
adamc@598 2463 val fooify =
adamc@598 2464 case x of
adamc@717 2465 "Link" => urlifyExp
adamc@717 2466 | "Action" => urlifyExp
adamc@598 2467 | _ => attrifyExp
adamc@120 2468
adamc@724 2469 val x =
adamc@724 2470 case x of
adamc@724 2471 "Typ" => "Type"
adamc@724 2472 | _ => x
adamc@598 2473 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@179 2474
adamc@598 2475 val (e, fm) = fooify env fm (e, t)
adamc@598 2476 in
adamc@598 2477 ((L'.EStrcat (s,
adamc@598 2478 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@598 2479 (L'.EStrcat (e,
adamc@598 2480 (L'.EPrim (Prim.String "\""),
adamc@598 2481 loc)),
adamc@598 2482 loc)),
adamc@598 2483 loc)), loc),
adamc@598 2484 fm)
adamc@598 2485 end)
adamc@598 2486 (s, fm) attrs
adamc@598 2487 end
adamc@104 2488
adamc@143 2489 fun input typ =
adamc@143 2490 case targs of
adamc@155 2491 [_, (L.CName name, _)] =>
adamc@179 2492 let
adamc@179 2493 val (ts, fm) = tagStart "input"
adamc@179 2494 in
adamc@179 2495 ((L'.EStrcat (ts,
adamc@802 2496 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\" />")),
adamc@179 2497 loc)), loc), fm)
adamc@179 2498 end
adamc@143 2499 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2500 raise Fail "No name passed to input tag")
adamc@104 2501
adamc@565 2502 fun normal (tag, extra, extraInner) =
adamc@143 2503 let
adamc@179 2504 val (tagStart, fm) = tagStart tag
adamc@152 2505 val tagStart = case extra of
adamc@152 2506 NONE => tagStart
adamc@152 2507 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 2508
adamc@695 2509 val xml = case extraInner of
adamc@695 2510 NONE => xml
adamc@695 2511 | SOME ei => (L.EFfiApp ("Basis", "strcat", [ei, xml]), loc)
adamc@695 2512
adamc@143 2513 fun normal () =
adamc@179 2514 let
adamc@179 2515 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2516 in
adamc@179 2517 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 2518 (L'.EStrcat (xml,
adamc@179 2519 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 2520 loc)), loc)),
adamc@179 2521 loc),
adamc@179 2522 fm)
adamc@179 2523 end
adamc@143 2524 in
adamc@143 2525 case xml of
adamc@143 2526 (L.EApp ((L.ECApp (
adamc@143 2527 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 2528 _), _),
adamc@143 2529 _), _),
adamc@143 2530 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 2531 if CharVector.all Char.isSpace s then
adamc@802 2532 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String " />"), loc)), loc), fm)
adamc@143 2533 else
adamc@143 2534 normal ()
adamc@143 2535 | _ => normal ()
adamc@143 2536 end
adamc@606 2537
adamc@606 2538 fun setAttrs jexp =
adamc@606 2539 let
adamc@606 2540 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@606 2541
adamc@606 2542 val assgns = List.mapPartial
adamc@606 2543 (fn ("Source", _, _) => NONE
adamc@800 2544 | ("Onchange", e, _) =>
adamc@800 2545 SOME (strcat [str "addOnChange(d,",
adamc@815 2546 (L'.EJavaScript (L'.Script, e), loc),
adamc@800 2547 str ")"])
adamc@606 2548 | (x, e, _) =>
adamc@606 2549 SOME (strcat [str ("d." ^ lowercaseFirst x ^ "="),
adamc@815 2550 (L'.EJavaScript (L'.Script, e), loc),
adamc@606 2551 str ";"]))
adamc@606 2552 attrs
adamc@606 2553 in
adamc@606 2554 case assgns of
adamc@606 2555 [] => jexp
adamc@606 2556 | _ => strcat (str "var d="
adamc@606 2557 :: jexp
adamc@606 2558 :: str ";"
adamc@606 2559 :: assgns)
adamc@606 2560 end
adamc@152 2561 in
adamc@152 2562 case tag of
adamc@679 2563 "body" => let
adamc@668 2564 val onload = case onload of
adamc@668 2565 NONE => (L'.EPrim (Prim.String ""), loc)
adamc@668 2566 | SOME e =>
adamc@668 2567 let
adamc@668 2568 val e = (L'.EApp (e, (L'.ERecord [], loc)), loc)
adamc@668 2569 in
adamc@815 2570 (L'.EJavaScript (L'.Attribute, e), loc)
adamc@679 2571 end
adamc@668 2572 in
adamc@668 2573 normal ("body",
adamc@695 2574 SOME (L'.EFfiApp ("Basis", "maybe_onload",
adamc@695 2575 [(L'.EStrcat ((L'.EFfiApp ("Basis", "get_settings",
adamc@695 2576 [(L'.ERecord [], loc)]), loc),
adamc@695 2577 onload), loc)]),
adamc@695 2578 loc),
adamc@695 2579 SOME (L.EFfiApp ("Basis", "get_script", [(L.ERecord [], loc)]), loc))
adamc@668 2580 end
adamc@568 2581
adamc@568 2582 | "dyn" =>
adamc@598 2583 (case attrs of
adamc@818 2584 [("Signal", e, _)] =>
adamc@568 2585 ((L'.EStrcat
adamc@603 2586 ((L'.EPrim (Prim.String "<span><script type=\"text/javascript\">dyn("), loc),
adamc@815 2587 (L'.EStrcat ((L'.EJavaScript (L'.Script, e), loc),
adamc@603 2588 (L'.EPrim (Prim.String ")</script></span>"), loc)), loc)), loc),
adamc@568 2589 fm)
adamc@568 2590 | _ => raise Fail "Monoize: Bad dyn attributes")
adamc@565 2591
adamc@565 2592 | "submit" => normal ("input type=\"submit\"", NONE, NONE)
adamc@601 2593 | "button" => normal ("input type=\"submit\"", NONE, NONE)
adamc@761 2594 | "hidden" => input "hidden"
adamc@152 2595
adamc@152 2596 | "textbox" =>
adamc@152 2597 (case targs of
adamc@152 2598 [_, (L.CName name, _)] =>
adamc@598 2599 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@598 2600 NONE =>
adamc@598 2601 let
adamc@598 2602 val (ts, fm) = tagStart "input"
adamc@598 2603 in
adamc@598 2604 ((L'.EStrcat (ts,
adamc@802 2605 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\" />")),
adamc@598 2606 loc)), loc), fm)
adamc@598 2607 end
adamc@598 2608 | SOME (_, src, _) =>
adamc@816 2609 (strcat [str "<span><script type=\"text/javascript\">inp(",
adamc@815 2610 (L'.EJavaScript (L'.Script, src), loc),
adamc@816 2611 str ")</script></span>"],
adamc@598 2612 fm))
adamc@152 2613 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@598 2614 raise Fail "No name passed to textbox tag"))
adamc@155 2615 | "password" => input "password"
adamc@361 2616 | "textarea" =>
adamc@152 2617 (case targs of
adamc@152 2618 [_, (L.CName name, _)] =>
adamc@179 2619 let
adamc@179 2620 val (ts, fm) = tagStart "textarea"
adamc@179 2621 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2622 in
adamc@179 2623 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 2624 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 2625 (L'.EStrcat (xml,
adamc@179 2626 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 2627 loc)), loc)),
adamc@179 2628 loc), fm)
adamc@179 2629 end
adamc@152 2630 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2631 raise Fail "No name passed to ltextarea tag"))
adamc@153 2632
adamc@190 2633 | "checkbox" => input "checkbox"
adamc@737 2634 | "upload" => input "file"
adamc@190 2635
adamc@153 2636 | "radio" =>
adamc@153 2637 (case targs of
adamc@153 2638 [_, (L.CName name, _)] =>
adamc@179 2639 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 2640 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2641 raise Fail "No name passed to radio tag"))
adamc@153 2642 | "radioOption" =>
adamc@153 2643 (case St.radioGroup st of
adamc@153 2644 NONE => raise Fail "No name for radioGroup"
adamc@153 2645 | SOME name =>
adamc@153 2646 normal ("input",
adamc@565 2647 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc),
adamc@565 2648 NONE))
adamc@152 2649
adamc@361 2650 | "select" =>
adamc@154 2651 (case targs of
adamc@154 2652 [_, (L.CName name, _)] =>
adamc@179 2653 let
adamc@179 2654 val (ts, fm) = tagStart "select"
adamc@179 2655 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2656 in
adamc@179 2657 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@598 2658 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")),
adamc@598 2659 loc)), loc),
adamc@179 2660 (L'.EStrcat (xml,
adamc@179 2661 (L'.EPrim (Prim.String "</select>"),
adamc@179 2662 loc)), loc)),
adamc@179 2663 loc),
adamc@179 2664 fm)
adamc@179 2665 end
adamc@154 2666 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 2667 raise Fail "No name passed to lselect tag"))
adamc@154 2668
adamc@601 2669 | "ctextbox" =>
adamc@601 2670 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@601 2671 NONE =>
adamc@601 2672 let
adamc@601 2673 val (ts, fm) = tagStart "input"
adamc@601 2674 in
adamc@601 2675 ((L'.EStrcat (ts,
adamc@802 2676 (L'.EPrim (Prim.String " />"), loc)),
adamc@601 2677 loc), fm)
adamc@601 2678 end
adamc@601 2679 | SOME (_, src, _) =>
adamc@606 2680 let
adamc@816 2681 val sc = strcat [str "inp(",
adamc@815 2682 (L'.EJavaScript (L'.Script, src), loc),
adamc@816 2683 str ")"]
adamc@797 2684 val sc = setAttrs sc
adamc@797 2685 in
adamc@797 2686 (strcat [str "<span><script type=\"text/javascript\">",
adamc@797 2687 sc,
adamc@797 2688 str "</script></span>"],
adamc@797 2689 fm)
adamc@797 2690 end)
adamc@797 2691
adamc@817 2692 | "ccheckbox" =>
adamc@817 2693 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@817 2694 NONE =>
adamc@817 2695 let
adamc@817 2696 val (ts, fm) = tagStart "input type=\"checkbox\""
adamc@817 2697 in
adamc@817 2698 ((L'.EStrcat (ts,
adamc@817 2699 (L'.EPrim (Prim.String " />"), loc)),
adamc@817 2700 loc), fm)
adamc@817 2701 end
adamc@817 2702 | SOME (_, src, _) =>
adamc@817 2703 let
adamc@817 2704 val sc = strcat [str "chk(",
adamc@817 2705 (L'.EJavaScript (L'.Script, src), loc),
adamc@817 2706 str ")"]
adamc@817 2707 val sc = setAttrs sc
adamc@817 2708 in
adamc@817 2709 (strcat [str "<span><script type=\"text/javascript\">",
adamc@817 2710 sc,
adamc@817 2711 str "</script></span>"],
adamc@817 2712 fm)
adamc@817 2713 end)
adamc@817 2714
adamc@797 2715 | "cselect" =>
adamc@797 2716 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@797 2717 NONE =>
adamc@797 2718 let
adamc@797 2719 val (ts, fm) = tagStart "select"
adamc@797 2720 in
adamc@797 2721 ((L'.EStrcat (ts,
adamc@802 2722 (L'.EPrim (Prim.String " />"), loc)),
adamc@797 2723 loc), fm)
adamc@797 2724 end
adamc@797 2725 | SOME (_, src, _) =>
adamc@797 2726 let
adamc@797 2727 val (xml, fm) = monoExp (env, st, fm) xml
adamc@797 2728
adamc@816 2729 val sc = strcat [str "sel(",
adamc@815 2730 (L'.EJavaScript (L'.Script, src), loc),
adamc@797 2731 str ",",
adamc@815 2732 (L'.EJavaScript (L'.Script, xml), loc),
adamc@606 2733 str ")"]
adamc@606 2734 val sc = setAttrs sc
adamc@606 2735 in
adamc@646 2736 (strcat [str "<span><script type=\"text/javascript\">",
adamc@606 2737 sc,
adamc@646 2738 str "</script></span>"],
adamc@606 2739 fm)
adamc@606 2740 end)
adamc@154 2741
adamc@797 2742 | "coption" => normal ("option", NONE, NONE)
adamc@797 2743
adamc@565 2744 | "tabl" => normal ("table", NONE, NONE)
adamc@565 2745 | _ => normal (tag, NONE, NONE)
adamc@95 2746 end
adamc@94 2747
adamc@141 2748 | L.EApp ((L.ECApp (
adamc@361 2749 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@734 2750 (L.CRecord (_, fields), _)), _),
adamc@141 2751 xml) =>
adamc@143 2752 let
adamc@143 2753 fun findSubmit (e, _) =
adamc@143 2754 case e of
adamc@143 2755 L.EApp (
adamc@143 2756 (L.EApp (
adamc@143 2757 (L.ECApp (
adamc@143 2758 (L.ECApp (
adamc@143 2759 (L.ECApp (
adamc@143 2760 (L.ECApp (
adamc@143 2761 (L.EFfi ("Basis", "join"),
adamc@143 2762 _), _), _),
adamc@143 2763 _), _),
adamc@143 2764 _), _),
adamc@143 2765 _), _),
adamc@143 2766 xml1), _),
adamc@143 2767 xml2) => (case findSubmit xml1 of
adamc@143 2768 Error => Error
adamc@143 2769 | NotFound => findSubmit xml2
adamc@143 2770 | Found e =>
adamc@143 2771 case findSubmit xml2 of
adamc@143 2772 NotFound => Found e
adamc@143 2773 | _ => Error)
adamc@143 2774 | L.EApp (
adamc@143 2775 (L.EApp (
adamc@143 2776 (L.EApp (
adamc@730 2777 (L.EApp (
adamc@143 2778 (L.ECApp (
adamc@143 2779 (L.ECApp (
adamc@143 2780 (L.ECApp (
adamc@143 2781 (L.ECApp (
adamc@143 2782 (L.ECApp (
adamc@143 2783 (L.ECApp (
adamc@143 2784 (L.ECApp (
adamc@730 2785 (L.ECApp (
adamc@730 2786 (L.EFfi ("Basis", "tag"),
adamc@730 2787 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@730 2788 _), _),
adamc@143 2789 attrs), _),
adamc@143 2790 _), _),
adamc@143 2791 xml) =>
adamc@143 2792 (case #1 attrs of
adamc@143 2793 L.ERecord xes =>
adamc@143 2794 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 2795 | _ => NONE) xes of
adamc@143 2796 NONE => findSubmit xml
adamc@143 2797 | SOME et =>
adamc@143 2798 case findSubmit xml of
adamc@143 2799 NotFound => Found et
adamc@143 2800 | _ => Error)
adamc@143 2801 | _ => findSubmit xml)
adamc@143 2802 | _ => NotFound
adamc@143 2803
adamc@735 2804 val (func, action, fm) = case findSubmit xml of
adamc@735 2805 NotFound => (0, (L'.EPrim (Prim.String ""), loc), fm)
adamc@143 2806 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@598 2807 | Found (action, actionT) =>
adamc@598 2808 let
adamc@735 2809 val func = case #1 action of
adamc@735 2810 L.EClosure (n, _) => n
adamc@735 2811 | _ => raise Fail "Monoize: Action is not a closure"
adamc@598 2812 val actionT = monoType env actionT
adamc@598 2813 val (action, fm) = monoExp (env, st, fm) action
adamc@598 2814 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@598 2815 in
adamc@735 2816 (func,
adamc@735 2817 (L'.EStrcat ((L'.EPrim (Prim.String " action=\""), loc),
adamc@598 2818 (L'.EStrcat (action,
adamc@598 2819 (L'.EPrim (Prim.String "\""), loc)), loc)), loc),
adamc@598 2820 fm)
adamc@598 2821 end
adamc@734 2822
adamc@737 2823 val hasUpload = CoreUtil.Exp.exists {kind = fn _ => false,
adamc@737 2824 con = fn _ => false,
adamc@737 2825 exp = fn e =>
adamc@737 2826 case e of
adamc@737 2827 L.EFfi ("Basis", "upload") => true
adamc@737 2828 | _ => false} xml
adamc@737 2829
adamc@179 2830 val (xml, fm) = monoExp (env, st, fm) xml
adamc@735 2831
adamc@735 2832 val xml =
adamc@735 2833 if IS.member (!readCookie, func) then
adamc@735 2834 let
adamc@735 2835 fun inFields s = List.exists (fn ((L.CName s', _), _) => s' = s
adamc@735 2836 | _ => true) fields
adamc@735 2837
adamc@735 2838 fun getSigName () =
adamc@735 2839 let
adamc@735 2840 fun getSigName' n =
adamc@735 2841 let
adamc@735 2842 val s = "Sig" ^ Int.toString n
adamc@735 2843 in
adamc@735 2844 if inFields s then
adamc@735 2845 getSigName' (n + 1)
adamc@735 2846 else
adamc@735 2847 s
adamc@735 2848 end
adamc@735 2849 in
adamc@735 2850 if inFields "Sig" then
adamc@735 2851 getSigName' 0
adamc@735 2852 else
adamc@735 2853 "Sig"
adamc@735 2854 end
adamc@735 2855
adamc@735 2856 val sigName = getSigName ()
adamc@735 2857 val sigSet = (L'.EFfiApp ("Basis", "sigString", [(L'.ERecord [], loc)]), loc)
adamc@735 2858 val sigSet = (L'.EStrcat ((L'.EPrim (Prim.String ("<input type=\"hidden\" name=\""
adamc@735 2859 ^ sigName
adamc@735 2860 ^ "\" value=\"")), loc),
adamc@735 2861 sigSet), loc)
adamc@735 2862 val sigSet = (L'.EStrcat (sigSet,
adamc@803 2863 (L'.EPrim (Prim.String "\" />"), loc)), loc)
adamc@735 2864 in
adamc@735 2865 (L'.EStrcat (sigSet, xml), loc)
adamc@735 2866 end
adamc@735 2867 else
adamc@735 2868 xml
adamc@737 2869
adamc@737 2870 val action = if hasUpload then
adamc@737 2871 (L'.EStrcat (action,
adamc@737 2872 (L'.EPrim (Prim.String " enctype=\"multipart/form-data\""), loc)), loc)
adamc@737 2873 else
adamc@737 2874 action
adamc@737 2875
adamc@143 2876 in
adamc@730 2877 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form method=\"post\""), loc),
adamc@179 2878 (L'.EStrcat (action,
adamc@598 2879 (L'.EPrim (Prim.String ">"), loc)), loc)), loc),
adamc@179 2880 (L'.EStrcat (xml,
adamc@179 2881 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 2882 fm)
adamc@143 2883 end
adamc@141 2884
adamc@756 2885 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp (
adamc@756 2886 (L.EFfi ("Basis", "subform"), _), _), _), _),
adamc@756 2887 _), _), _), (L.CName nm, loc)) =>
adamc@756 2888 let
adamc@756 2889 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@756 2890 in
adamc@756 2891 ((L'.EAbs ("xml", s, s,
adamc@756 2892 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".b\" value=\""
adamc@803 2893 ^ nm ^ "\" />")), loc),
adamc@756 2894 (L'.ERel 0, loc),
adamc@803 2895 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\" />")), loc)]),
adamc@756 2896 loc),
adamc@756 2897 fm)
adamc@756 2898 end
adamc@756 2899
adamc@758 2900 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp (
adamc@758 2901 (L.EFfi ("Basis", "subforms"), _), _), _), _),
adamc@758 2902 _), _), _), (L.CName nm, loc)) =>
adamc@758 2903 let
adamc@758 2904 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@758 2905 in
adamc@758 2906 ((L'.EAbs ("xml", s, s,
adamc@758 2907 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".s\" value=\""
adamc@803 2908 ^ nm ^ "\" />")), loc),
adamc@758 2909 (L'.ERel 0, loc),
adamc@803 2910 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\" />")), loc)]),
adamc@758 2911 loc),
adamc@758 2912 fm)
adamc@758 2913 end
adamc@758 2914
adamc@758 2915 | L.ECApp ((L.ECApp (
adamc@758 2916 (L.EFfi ("Basis", "entry"), _), _), _), _) =>
adamc@758 2917 let
adamc@758 2918 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@758 2919 in
adamc@758 2920 ((L'.EAbs ("xml", s, s,
adamc@803 2921 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".i\" value=\"1\" />")), loc),
adamc@758 2922 (L'.ERel 0, loc),
adamc@803 2923 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\" />")), loc)]),
adamc@758 2924 loc),
adamc@758 2925 fm)
adamc@758 2926 end
adamc@758 2927
adamc@148 2928 | L.EApp ((L.ECApp (
adamc@148 2929 (L.ECApp (
adamc@148 2930 (L.ECApp (
adamc@148 2931 (L.ECApp (
adamc@148 2932 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 2933 _), _),
adamc@148 2934 _), _),
adamc@148 2935 _), _),
adamc@179 2936 xml) => monoExp (env, st, fm) xml
adamc@148 2937
adamc@283 2938 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 2939 let
adamc@283 2940 val t = monoType env t
adamc@283 2941 in
adamc@283 2942 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 2943 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 2944 fm)
adamc@283 2945 end
adamc@741 2946 | L.ECApp ((L.EFfi ("Basis", "returnBlob"), _), t) =>
adamc@741 2947 let
adamc@741 2948 val t = monoType env t
adamc@741 2949 val un = (L'.TRecord [], loc)
adamc@741 2950 in
adamc@741 2951 ((L'.EAbs ("b", (L'.TFfi ("Basis", "blob"), loc),
adamc@741 2952 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFun (un, t), loc)), loc),
adamc@741 2953 (L'.EAbs ("mt", (L'.TFfi ("Basis", "string"), loc), (L'.TFun (un, t), loc),
adamc@741 2954 (L'.EAbs ("_", un, t,
adamc@741 2955 (L'.EReturnBlob {blob = (L'.ERel 2, loc),
adamc@741 2956 mimeType = (L'.ERel 1, loc),
adamc@741 2957 t = t}, loc)), loc)), loc)), loc),
adamc@741 2958 fm)
adamc@741 2959 end
adamc@283 2960
adamc@179 2961 | L.EApp (e1, e2) =>
adamc@179 2962 let
adamc@179 2963 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 2964 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 2965 in
adamc@179 2966 ((L'.EApp (e1, e2), loc), fm)
adamc@179 2967 end
adamc@26 2968 | L.EAbs (x, dom, ran, e) =>
adamc@179 2969 let
adamc@179 2970 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 2971 in
adamc@179 2972 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 2973 end
adamc@25 2974 | L.ECApp _ => poly ()
adamc@25 2975 | L.ECAbs _ => poly ()
adamc@25 2976
adamc@252 2977 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 2978 | L.EFfiApp (m, x, es) =>
adamc@252 2979 let
adamc@252 2980 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 2981 in
adamc@252 2982 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 2983 end
adamc@252 2984
adamc@179 2985 | L.ERecord xes =>
adamc@179 2986 let
adamc@179 2987 val (xes, fm) = ListUtil.foldlMap
adamc@179 2988 (fn ((x, e, t), fm) =>
adamc@179 2989 let
adamc@179 2990 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2991 in
adamc@179 2992 ((monoName env x,
adamc@179 2993 e,
adamc@179 2994 monoType env t), fm)
adamc@179 2995 end) fm xes
adamc@179 2996 in
adamc@179 2997 ((L'.ERecord xes, loc), fm)
adamc@179 2998 end
adamc@179 2999 | L.EField (e, x, _) =>
adamc@179 3000 let
adamc@179 3001 val (e, fm) = monoExp (env, st, fm) e
adamc@179 3002 in
adamc@179 3003 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 3004 end
adamc@445 3005 | L.EConcat _ => poly ()
adamc@149 3006 | L.ECut _ => poly ()
adamc@493 3007 | L.ECutMulti _ => poly ()
adamc@177 3008
adamc@182 3009 | L.ECase (e, pes, {disc, result}) =>
adamc@179 3010 let
adamc@179 3011 val (e, fm) = monoExp (env, st, fm) e
adamc@179 3012 val (pes, fm) = ListUtil.foldlMap
adamc@179 3013 (fn ((p, e), fm) =>
adamc@179 3014 let
adamc@179 3015 val (e, fm) = monoExp (env, st, fm) e
adamc@179 3016 in
adamc@182 3017 ((monoPat env p, e), fm)
adamc@179 3018 end) fm pes
adamc@179 3019 in
adamc@182 3020 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 3021 end
adamc@177 3022
adamc@179 3023 | L.EWrite e =>
adamc@179 3024 let
adamc@179 3025 val (e, fm) = monoExp (env, st, fm) e
adamc@179 3026 in
adamc@252 3027 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 3028 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 3029 end
adamc@110 3030
adamc@179 3031 | L.EClosure (n, es) =>
adamc@179 3032 let
adamc@179 3033 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 3034 monoExp (env, st, fm) e)
adamc@179 3035 fm es
adamc@179 3036 in
adamc@179 3037 ((L'.EClosure (n, es), loc), fm)
adamc@179 3038 end
adamc@450 3039
adamc@450 3040 | L.ELet (x, t, e1, e2) =>
adamc@450 3041 let
adamc@450 3042 val t' = monoType env t
adamc@450 3043 val (e1, fm) = monoExp (env, st, fm) e1
adamc@450 3044 val (e2, fm) = monoExp (Env.pushERel env x t, st, fm) e2
adamc@450 3045 in
adamc@450 3046 ((L'.ELet (x, t', e1, e2), loc), fm)
adamc@450 3047 end
adamc@607 3048
adamc@609 3049 | L.EServerCall (n, es, ek, t) =>
adamc@608 3050 let
adamc@609 3051 val t = monoType env t
adamc@614 3052 val (_, ft, _, name) = Env.lookupENamed env n
adamc@608 3053 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@614 3054
adamc@614 3055 fun encodeArgs (es, ft, acc, fm) =
adamc@614 3056 case (es, ft) of
adamc@614 3057 ([], _) => (rev acc, fm)
adamc@614 3058 | (e :: es, (L.TFun (dom, ran), _)) =>
adamc@614 3059 let
adamc@614 3060 val (e, fm) = urlifyExp env fm (e, monoType env dom)
adamc@614 3061 in
adamc@614 3062 encodeArgs (es, ran, e
adamc@614 3063 :: (L'.EPrim (Prim.String "/"), loc)
adamc@614 3064 :: acc, fm)
adamc@614 3065 end
adamc@614 3066 | _ => raise Fail "Monoize: Not enough arguments visible in RPC function type"
adamc@614 3067
adamc@614 3068 val (call, fm) = encodeArgs (es, ft, [], fm)
adamc@614 3069 val call = foldl (fn (e, call) => (L'.EStrcat (call, e), loc))
adamc@614 3070 (L'.EPrim (Prim.String name), loc) call
adamc@614 3071
adamc@608 3072 val (ek, fm) = monoExp (env, st, fm) ek
adamc@609 3073
adamc@609 3074 val ekf = (L'.EAbs ("f",
adamc@609 3075 (L'.TFun (t,
adamc@609 3076 (L'.TFun ((L'.TRecord [], loc),
adamc@609 3077 (L'.TRecord [], loc)), loc)), loc),
adamc@609 3078 (L'.TFun (t,
adamc@609 3079 (L'.TRecord [], loc)), loc),
adamc@609 3080 (L'.EAbs ("x",
adamc@609 3081 t,
adamc@609 3082 (L'.TRecord [], loc),
adamc@609 3083 (L'.EApp ((L'.EApp ((L'.ERel 1, loc),
adamc@609 3084 (L'.ERel 0, loc)), loc),
adamc@609 3085 (L'.ERecord [], loc)), loc)), loc)), loc)
adamc@609 3086 val ek = (L'.EApp (ekf, ek), loc)
adamc@736 3087 val eff = if IS.member (!readCookie, n) then
adamc@736 3088 L'.ReadCookieWrite
adamc@736 3089 else
adamc@736 3090 L'.ReadOnly
adamc@736 3091 val e = (L'.EServerCall (call, ek, t, eff), loc)
adamc@651 3092 val e = liftExpInExp 0 e
adamc@651 3093 val unit = (L'.TRecord [], loc)
adamc@651 3094 val e = (L'.EAbs ("_", unit, unit, e), loc)
adamc@608 3095 in
adamc@651 3096 (e, fm)
adamc@608 3097 end
adamc@626 3098
adamc@626 3099 | L.EKAbs _ => poly ()
adamc@626 3100 | L.EKApp _ => poly ()
adamc@25 3101 end
adamc@25 3102
adamc@179 3103 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 3104 let
adamc@25 3105 fun poly () =
adamc@25 3106 (E.errorAt loc "Unsupported declaration";
adamc@25 3107 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 3108 NONE)
adamc@25 3109 in
adamc@25 3110 case d of
adamc@25 3111 L.DCon _ => NONE
adamc@808 3112 | L.DDatatype [("list", n, [_], [("Nil", _, NONE),
adamc@808 3113 ("Cons", _, SOME (L.TRecord (L.CRecord (_,
adamc@808 3114 [((L.CName "1", _),
adamc@808 3115 (L.CRel 0, _)),
adamc@808 3116 ((L.CName "2", _),
adamc@808 3117 (L.CApp ((L.CNamed n', _),
adamc@808 3118 (L.CRel 0, _)),
adamc@808 3119 _))]), _), _))])] =>
adamc@757 3120 if n = n' then
adamc@757 3121 NONE
adamc@757 3122 else
adamc@757 3123 poly ()
adamc@808 3124 | L.DDatatype dts =>
adamc@808 3125 let
adamc@808 3126 val env' = Env.declBinds env all
adamc@808 3127 val dts = map (fn (x, n, [], xncs) =>
adamc@808 3128 (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs)
adamc@808 3129 | _ => (E.errorAt loc "Polymorphic datatype needed too late";
adamc@808 3130 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@808 3131 ("", 0, []))) dts
adamc@808 3132 val d = (L'.DDatatype dts, loc)
adamc@808 3133 in
adamc@808 3134 SOME (env', fm, [d])
adamc@808 3135 end
adamc@179 3136 | L.DVal (x, n, t, e, s) =>
adamc@179 3137 let
adamc@179 3138 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 3139 in
adamc@179 3140 SOME (Env.pushENamed env x n t NONE s,
adamc@179 3141 fm,
adamc@273 3142 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 3143 end
adamc@128 3144 | L.DValRec vis =>
adamc@128 3145 let
adamc@128 3146 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 3147
adamc@179 3148 val (vis, fm) = ListUtil.foldlMap
adamc@179 3149 (fn ((x, n, t, e, s), fm) =>
adamc@179 3150 let
adamc@179 3151 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 3152 in
adamc@179 3153 ((x, n, monoType env t, e, s), fm)
adamc@179 3154 end)
adamc@179 3155 fm vis
adamc@128 3156 in
adamc@128 3157 SOME (env,
adamc@179 3158 fm,
adamc@273 3159 [(L'.DValRec vis, loc)])
adamc@128 3160 end
adamc@144 3161 | L.DExport (ek, n) =>
adamc@115 3162 let
adamc@120 3163 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 3164
adamc@609 3165 fun unwind (t, args) =
adamc@609 3166 case #1 t of
adamc@609 3167 L.TFun (dom, ran) => unwind (ran, dom :: args)
adamc@314 3168 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@609 3169 unwind (t, (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: args)
adamc@609 3170 | _ => (rev args, t)
adamc@120 3171
adamc@609 3172 val (ts, ran) = unwind (t, [])
adamc@609 3173 val ts = map (monoType env) ts
adamc@609 3174 val ran = monoType env ran
adamc@115 3175 in
adamc@609 3176 SOME (env, fm, [(L'.DExport (ek, s, n, ts, ran), loc)])
adamc@115 3177 end
adamc@707 3178 | L.DTable (x, n, (L.CRecord (_, xts), _), s, pe, _, ce, _) =>
adamc@251 3179 let
adamc@251 3180 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 3181 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 3182 val s = "uw_" ^ s
adamc@704 3183 val e_name = (L'.EPrim (Prim.String s), loc)
adamc@273 3184
adamc@273 3185 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@704 3186
adamc@707 3187 val (pe, fm) = monoExp (env, St.empty, fm) pe
adamc@707 3188 val (ce, fm) = monoExp (env, St.empty, fm) ce
adamc@251 3189 in
adamc@251 3190 SOME (Env.pushENamed env x n t NONE s,
adamc@251 3191 fm,
adamc@707 3192 [(L'.DTable (s, xts, pe, ce), loc),
adamc@704 3193 (L'.DVal (x, n, t', e_name, s), loc)])
adamc@251 3194 end
adamc@273 3195 | L.DTable _ => poly ()
adamc@754 3196 | L.DView (x, n, s, e, (L.CRecord (_, xts), _)) =>
adamc@754 3197 let
adamc@754 3198 val t = (L.CFfi ("Basis", "string"), loc)
adamc@754 3199 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@754 3200 val s = "uw_" ^ s
adamc@754 3201 val e_name = (L'.EPrim (Prim.String s), loc)
adamc@754 3202
adamc@754 3203 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@754 3204
adamc@754 3205 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@754 3206 val e = (L'.EFfiApp ("Basis", "viewify", [e]), loc)
adamc@754 3207 in
adamc@754 3208 SOME (Env.pushENamed env x n t NONE s,
adamc@754 3209 fm,
adamc@754 3210 [(L'.DView (s, xts, e), loc),
adamc@754 3211 (L'.DVal (x, n, t', e_name, s), loc)])
adamc@754 3212 end
adamc@754 3213 | L.DView _ => poly ()
adamc@338 3214 | L.DSequence (x, n, s) =>
adamc@338 3215 let
adamc@338 3216 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 3217 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 3218 val s = "uw_" ^ s
adamc@338 3219 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 3220 in
adamc@338 3221 SOME (Env.pushENamed env x n t NONE s,
adamc@338 3222 fm,
adamc@338 3223 [(L'.DSequence s, loc),
adamc@338 3224 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 3225 end
adamc@683 3226 | L.DDatabase _ => NONE
adamc@462 3227 | L.DCookie (x, n, t, s) =>
adamc@462 3228 let
adamc@462 3229 val t = (L.CFfi ("Basis", "string"), loc)
adamc@462 3230 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@462 3231 val e = (L'.EPrim (Prim.String s), loc)
adamc@462 3232 in
adamc@462 3233 SOME (Env.pushENamed env x n t NONE s,
adamc@462 3234 fm,
adamc@725 3235 [(L'.DCookie s, loc),
adamc@725 3236 (L'.DVal (x, n, t', e, s), loc)])
adamc@462 3237 end
adamc@720 3238 | L.DStyle (x, n, s) =>
adamc@718 3239 let
adamc@718 3240 val t = (L.CFfi ("Basis", "string"), loc)
adamc@718 3241 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@718 3242 val e = (L'.EPrim (Prim.String s), loc)
adamc@718 3243 in
adamc@718 3244 SOME (Env.pushENamed env x n t NONE s,
adamc@718 3245 fm,
adamc@720 3246 [(L'.DStyle s, loc),
adamc@718 3247 (L'.DVal (x, n, t', e, s), loc)])
adamc@718 3248 end
adamc@25 3249 end
adamc@25 3250
adamc@683 3251 datatype expungable = Client | Channel
adamc@683 3252
adamc@683 3253 fun monoize env file =
adamc@25 3254 let
adamc@735 3255 (* Calculate which exported functions need cookie signature protection *)
adamc@735 3256 val rcook = foldl (fn ((d, _), rcook) =>
adamc@735 3257 case d of
adamc@735 3258 L.DExport (L.Action L.ReadCookieWrite, n) => IS.add (rcook, n)
adamc@735 3259 | L.DExport (L.Rpc L.ReadCookieWrite, n) => IS.add (rcook, n)
adamc@735 3260 | _ => rcook)
adamc@735 3261 IS.empty file
adamc@735 3262 val () = readCookie := rcook
adamc@735 3263
adamc@683 3264 val loc = E.dummySpan
adamc@683 3265 val client = (L'.TFfi ("Basis", "client"), loc)
adamc@683 3266 val unit = (L'.TRecord [], loc)
adamc@687 3267
adamc@687 3268 fun calcClientish xts =
adamc@687 3269 foldl (fn ((x : L.con, t : L.con), st as (nullable, notNullable)) =>
adamc@687 3270 case #1 x of
adamc@687 3271 L.CName x =>
adamc@687 3272 (case #1 t of
adamc@687 3273 L.CFfi ("Basis", "client") =>
adamc@687 3274 (nullable, (x, Client) :: notNullable)
adamc@687 3275 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@687 3276 (L.CFfi ("Basis", "client"), _)) =>
adamc@687 3277 ((x, Client) :: nullable, notNullable)
adamc@687 3278 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@687 3279 (nullable, (x, Channel) :: notNullable)
adamc@687 3280 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@687 3281 (L.CApp ((L.CFfi ("Basis", "channel"), _), _), _)) =>
adamc@687 3282 ((x, Channel) :: nullable, notNullable)
adamc@687 3283 | _ => st)
adamc@687 3284 | _ => st) ([], []) xts
adamc@687 3285
adamc@683 3286 fun expunger () =
adamc@683 3287 let
adamc@683 3288 val target = (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)
adamc@683 3289
adamc@683 3290 fun doTable (tab, xts, e) =
adamc@683 3291 case xts of
adamc@683 3292 L.CRecord (_, xts) =>
adamc@683 3293 let
adamc@687 3294 val (nullable, notNullable) = calcClientish xts
adamc@683 3295
adamc@684 3296 fun cond (x, v) =
adamc@684 3297 (L'.EStrcat ((L'.EPrim (Prim.String ("uw_" ^ x
adamc@684 3298 ^ (case v of
adamc@684 3299 Client => ""
adamc@684 3300 | Channel => " >> 32")
adamc@684 3301 ^ " = ")), loc),
adamc@684 3302 target), loc)
adamc@684 3303
adamc@684 3304 val e =
adamc@684 3305 foldl (fn ((x, v), e) =>
adamc@684 3306 (L'.ESeq (
adamc@684 3307 (L'.EDml (L'.EStrcat (
adamc@684 3308 (L'.EPrim (Prim.String ("UPDATE uw_"
adamc@684 3309 ^ tab
adamc@684 3310 ^ " SET uw_"
adamc@684 3311 ^ x
adamc@684 3312 ^ " = NULL WHERE ")), loc),
adamc@684 3313 cond (x, v)), loc), loc),
adamc@684 3314 e), loc))
adamc@684 3315 e nullable
adamc@684 3316
adamc@683 3317 val e =
adamc@683 3318 case notNullable of
adamc@683 3319 [] => e
adamc@683 3320 | eb :: ebs =>
adamc@684 3321 (L'.ESeq (
adamc@684 3322 (L'.EDml (foldl
adamc@684 3323 (fn (eb, s) =>
adamc@684 3324 (L'.EStrcat (s,
adamc@687 3325 (L'.EStrcat ((L'.EPrim (Prim.String " OR "),
adamc@684 3326 loc),
adamc@684 3327 cond eb), loc)), loc))
adamc@684 3328 (L'.EStrcat ((L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@684 3329 ^ tab
adamc@684 3330 ^ " WHERE ")), loc),
adamc@684 3331 cond eb), loc)
adamc@684 3332 ebs), loc),
adamc@684 3333 e), loc)
adamc@683 3334 in
adamc@683 3335 e
adamc@683 3336 end
adamc@683 3337 | _ => e
adamc@683 3338
adamc@683 3339 val e = (L'.ERecord [], loc)
adamc@683 3340 in
adamc@683 3341 foldl (fn ((d, _), e) =>
adamc@683 3342 case d of
adamc@707 3343 L.DTable (_, _, xts, tab, _, _, _, _) => doTable (tab, #1 xts, e)
adamc@683 3344 | _ => e) e file
adamc@683 3345 end
adamc@683 3346
adamc@687 3347 fun initializer () =
adamc@687 3348 let
adamc@687 3349 fun doTable (tab, xts, e) =
adamc@687 3350 case xts of
adamc@687 3351 L.CRecord (_, xts) =>
adamc@687 3352 let
adamc@687 3353 val (nullable, notNullable) = calcClientish xts
adamc@687 3354
adamc@687 3355 val e =
adamc@687 3356 case nullable of
adamc@687 3357 [] => e
adamc@687 3358 | (x, _) :: ebs =>
adamc@687 3359 (L'.ESeq (
adamc@687 3360 (L'.EDml (L'.EPrim (Prim.String
adamc@687 3361 (foldl (fn ((x, _), s) =>
adamc@687 3362 s ^ ", uw_" ^ x ^ " = NULL")
adamc@687 3363 ("UPDATE uw_"
adamc@687 3364 ^ tab
adamc@687 3365 ^ " SET uw_"
adamc@687 3366 ^ x
adamc@687 3367 ^ " = NULL")
adamc@687 3368 ebs)), loc), loc),
adamc@687 3369 e), loc)
adamc@687 3370
adamc@687 3371 val e =
adamc@687 3372 case notNullable of
adamc@687 3373 [] => e
adamc@687 3374 | eb :: ebs =>
adamc@687 3375 (L'.ESeq (
adamc@687 3376 (L'.EDml (L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@687 3377 ^ tab)), loc), loc),
adamc@687 3378 e), loc)
adamc@687 3379 in
adamc@687 3380 e
adamc@687 3381 end
adamc@687 3382 | _ => e
adamc@687 3383
adamc@687 3384 val e = (L'.ERecord [], loc)
adamc@687 3385 in
adamc@687 3386 foldl (fn ((d, _), e) =>
adamc@687 3387 case d of
adamc@707 3388 L.DTable (_, _, xts, tab, _, _, _, _) => doTable (tab, #1 xts, e)
adamc@687 3389 | _ => e) e file
adamc@687 3390 end
adamc@687 3391
adamc@179 3392 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@683 3393 case #1 d of
adamc@683 3394 L.DDatabase s =>
adamc@683 3395 let
adamc@687 3396 val (nExp, fm) = Fm.freshName fm
adamc@687 3397 val (nIni, fm) = Fm.freshName fm
adamc@687 3398
adamc@687 3399 val dExp = L'.DVal ("expunger",
adamc@687 3400 nExp,
adamc@687 3401 (L'.TFun (client, unit), loc),
adamc@687 3402 (L'.EAbs ("cli", client, unit, expunger ()), loc),
adamc@687 3403 "expunger")
adamc@687 3404 val dIni = L'.DVal ("initializer",
adamc@687 3405 nIni,
adamc@687 3406 (L'.TFun (unit, unit), loc),
adamc@687 3407 (L'.EAbs ("_", unit, unit, initializer ()), loc),
adamc@687 3408 "initializer")
adamc@683 3409 in
adamc@687 3410 (env, Fm.enter fm, (L'.DDatabase {name = s,
adamc@687 3411 expunge = nExp,
adamc@687 3412 initialize = nIni}, loc)
adamc@687 3413 :: (dExp, loc)
adamc@687 3414 :: (dIni, loc)
adamc@683 3415 :: ds)
adamc@683 3416 end
adamc@683 3417 | _ =>
adamc@683 3418 case monoDecl (env, fm) d of
adamc@683 3419 NONE => (env, fm, ds)
adamc@683 3420 | SOME (env, fm, ds') =>
adamc@683 3421 (env,
adamc@683 3422 Fm.enter fm,
adamc@683 3423 ds' @ Fm.decls fm @ ds))
adamc@683 3424 (env, Fm.empty (CoreUtil.File.maxName file + 1), []) file
adamc@25 3425 in
adamc@25 3426 rev ds
adamc@25 3427 end
adamc@25 3428
adamc@25 3429 end