annotate src/monoize.sml @ 878:a8952047e1d3

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