annotate src/monoize.sml @ 888:ba3569f4fe89

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