annotate src/monoize.sml @ 976:68eda5b0636d

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