annotate src/monoize.sml @ 901:f17638dd3080

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