annotate src/monoize.sml @ 1014:ea9f03ac2710

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