annotate src/monoize.sml @ 683:9a2c18dab11d

Expunging non-nullable rows
author Adam Chlipala <adamc@hcoop.net>
date Sun, 29 Mar 2009 13:30:01 -0400
parents 5bbb542243e8
children f0224c7f12bb
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@196 37
adamc@385 38 val urlPrefix = ref "/"
adamc@385 39
adamc@196 40 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
adamc@25 41
adamc@252 42 structure U = MonoUtil
adamc@252 43
adamc@252 44 val liftExpInExp =
adamc@252 45 U.Exp.mapB {typ = fn t => t,
adamc@252 46 exp = fn bound => fn e =>
adamc@252 47 case e of
adamc@252 48 L'.ERel xn =>
adamc@252 49 if xn < bound then
adamc@252 50 e
adamc@252 51 else
adamc@252 52 L'.ERel (xn + 1)
adamc@252 53 | _ => e,
adamc@252 54 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@252 55 | (bound, _) => bound}
adamc@252 56
adamc@25 57 fun monoName env (all as (c, loc)) =
adamc@25 58 let
adamc@25 59 fun poly () =
adamc@25 60 (E.errorAt loc "Unsupported name constructor";
adamc@25 61 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 62 "")
adamc@25 63 in
adamc@25 64 case c of
adamc@25 65 L.CName s => s
adamc@25 66 | _ => poly ()
adamc@25 67 end
adamc@25 68
adamc@292 69 fun readType' (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 70 (L'.TOption t, loc)), loc)
adamc@292 71 fun readErrType (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 72 t), loc)
adamc@292 73 fun readType (t, loc) =
adamc@292 74 (L'.TRecord [("Read", readType' (t, loc)),
adamc@292 75 ("ReadError", readErrType (t, loc))],
adamc@292 76 loc)
adamc@292 77
adamc@196 78 fun monoType env =
adamc@25 79 let
adamc@196 80 fun mt env dtmap (all as (c, loc)) =
adamc@196 81 let
adamc@196 82 fun poly () =
adamc@196 83 (E.errorAt loc "Unsupported type constructor";
adamc@196 84 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 85 dummyTyp)
adamc@196 86 in
adamc@196 87 case c of
adamc@196 88 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 89 | L.TCFun _ => poly ()
adamc@196 90 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@196 91 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
adamc@196 92 | L.TRecord _ => poly ()
adamc@196 93
adamc@288 94 | L.CApp ((L.CFfi ("Basis", "option"), _), t) =>
adamc@288 95 (L'.TOption (mt env dtmap t), loc)
adamc@288 96
adamc@387 97 | L.CApp ((L.CFfi ("Basis", "eq"), _), t) =>
adamc@387 98 let
adamc@387 99 val t = mt env dtmap t
adamc@387 100 in
adamc@387 101 (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)
adamc@387 102 end
adamc@389 103 | L.CApp ((L.CFfi ("Basis", "num"), _), t) =>
adamc@389 104 let
adamc@389 105 val t = mt env dtmap t
adamc@389 106 in
adamc@417 107 (L'.TRecord [("Zero", t),
adamc@417 108 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 109 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 110 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 111 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 112 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 113 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))],
adamc@389 114 loc)
adamc@389 115 end
adamc@391 116 | L.CApp ((L.CFfi ("Basis", "ord"), _), t) =>
adamc@391 117 let
adamc@391 118 val t = mt env dtmap t
adamc@391 119 in
adamc@391 120 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 121 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 122 loc)
adamc@391 123 end
adamc@286 124 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
adamc@286 125 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@290 126 | L.CApp ((L.CFfi ("Basis", "read"), _), t) =>
adamc@292 127 readType (mt env dtmap t, loc)
adamc@286 128
adamc@196 129 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 130 (L'.TFfi ("Basis", "string"), loc)
adamc@196 131 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 132 (L'.TFfi ("Basis", "string"), loc)
adamc@196 133
adamc@251 134 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@252 135 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
adamc@565 136 | L.CApp ((L.CFfi ("Basis", "source"), _), t) =>
adamc@577 137 (L'.TSource, loc)
adamc@568 138 | L.CApp ((L.CFfi ("Basis", "signal"), _), t) =>
adamc@568 139 (L'.TSignal (mt env dtmap t), loc)
adamc@462 140 | L.CApp ((L.CFfi ("Basis", "http_cookie"), _), _) =>
adamc@462 141 (L'.TFfi ("Basis", "string"), loc)
adamc@252 142 | L.CApp ((L.CFfi ("Basis", "sql_table"), _), _) =>
adamc@252 143 (L'.TFfi ("Basis", "string"), loc)
adamc@338 144 | L.CFfi ("Basis", "sql_sequence") =>
adamc@338 145 (L'.TFfi ("Basis", "string"), loc)
adamc@252 146 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
adamc@252 147 (L'.TFfi ("Basis", "string"), loc)
adamc@252 148 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
adamc@252 149 (L'.TFfi ("Basis", "string"), loc)
adamc@252 150 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
adamc@252 151 (L'.TFfi ("Basis", "string"), loc)
adamc@252 152
adamc@252 153 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
adamc@252 154 (L'.TRecord [], loc)
adamc@252 155 | L.CFfi ("Basis", "sql_relop") =>
adamc@252 156 (L'.TFfi ("Basis", "string"), loc)
adamc@252 157 | L.CFfi ("Basis", "sql_direction") =>
adamc@252 158 (L'.TFfi ("Basis", "string"), loc)
adamc@252 159 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
adamc@252 160 (L'.TFfi ("Basis", "string"), loc)
adamc@252 161 | L.CFfi ("Basis", "sql_limit") =>
adamc@252 162 (L'.TFfi ("Basis", "string"), loc)
adamc@252 163 | L.CFfi ("Basis", "sql_offset") =>
adamc@252 164 (L'.TFfi ("Basis", "string"), loc)
adamc@252 165
adamc@676 166 | L.CApp ((L.CFfi ("Basis", "sql_injectable_prim"), _), t) =>
adamc@676 167 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 168 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
adamc@252 169 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 170 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
adamc@252 171 (L'.TFfi ("Basis", "string"), loc)
adamc@252 172 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
adamc@252 173 (L'.TFfi ("Basis", "string"), loc)
adamc@252 174 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
adamc@252 175 (L'.TFfi ("Basis", "string"), loc)
adamc@252 176 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
adamc@252 177 (L'.TRecord [], loc)
adamc@252 178 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
adamc@252 179 (L'.TRecord [], loc)
adamc@559 180 | L.CApp ((L.CFfi ("Basis", "sql_arith"), _), _) =>
adamc@559 181 (L'.TRecord [], loc)
adamc@441 182 | L.CApp ((L.CFfi ("Basis", "sql_nfunc"), _), _) =>
adamc@441 183 (L'.TFfi ("Basis", "string"), loc)
adamc@251 184
adamc@668 185 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@668 186 (L'.TFfi ("Basis", "channel"), loc)
adamc@668 187
adamc@196 188 | L.CRel _ => poly ()
adamc@196 189 | L.CNamed n =>
adamc@196 190 (case IM.find (dtmap, n) of
adamc@196 191 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 192 | NONE =>
adamc@196 193 let
adamc@196 194 val r = ref (L'.Default, [])
adamc@196 195 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 196
adamc@196 197 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 198
adamc@196 199 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 200 in
adamc@196 201 case xs of
adamc@198 202 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 203 (L'.TDatatype (n, r), loc))
adamc@196 204 | _ => poly ()
adamc@196 205 end)
adamc@196 206 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 207 | L.CApp _ => poly ()
adamc@196 208 | L.CAbs _ => poly ()
adamc@196 209
adamc@196 210 | L.CName _ => poly ()
adamc@196 211
adamc@196 212 | L.CRecord _ => poly ()
adamc@196 213 | L.CConcat _ => poly ()
adamc@621 214 | L.CMap _ => poly ()
adamc@196 215 | L.CUnit => poly ()
adamc@214 216
adamc@214 217 | L.CTuple _ => poly ()
adamc@214 218 | L.CProj _ => poly ()
adamc@626 219
adamc@626 220 | L.CKAbs _ => poly ()
adamc@626 221 | L.CKApp _ => poly ()
adamc@626 222 | L.TKFun _ => poly ()
adamc@196 223 end
adamc@25 224 in
adamc@196 225 mt env IM.empty
adamc@25 226 end
adamc@25 227
adamc@25 228 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 229
adamc@179 230 structure IM = IntBinaryMap
adamc@179 231
adamc@179 232 datatype foo_kind =
adamc@179 233 Attr
adamc@179 234 | Url
adamc@179 235
adamc@179 236 fun fk2s fk =
adamc@179 237 case fk of
adamc@179 238 Attr => "attr"
adamc@179 239 | Url => "url"
adamc@179 240
adamc@179 241 structure Fm :> sig
adamc@179 242 type t
adamc@179 243
adamc@179 244 val empty : int -> t
adamc@179 245
adamc@179 246 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@179 247 val enter : t -> t
adamc@179 248 val decls : t -> L'.decl list
adamc@683 249
adamc@683 250 val freshName : t -> int * t
adamc@179 251 end = struct
adamc@179 252
adamc@179 253 structure M = BinaryMapFn(struct
adamc@179 254 type ord_key = foo_kind
adamc@179 255 fun compare x =
adamc@179 256 case x of
adamc@179 257 (Attr, Attr) => EQUAL
adamc@179 258 | (Attr, _) => LESS
adamc@179 259 | (_, Attr) => GREATER
adamc@179 260
adamc@179 261 | (Url, Url) => EQUAL
adamc@179 262 end)
adamc@179 263
adamc@179 264 type t = {
adamc@179 265 count : int,
adamc@179 266 map : int IM.map M.map,
adamc@179 267 decls : L'.decl list
adamc@179 268 }
adamc@179 269
adamc@179 270 fun empty count = {
adamc@179 271 count = count,
adamc@179 272 map = M.empty,
adamc@179 273 decls = []
adamc@179 274 }
adamc@179 275
adamc@179 276 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
adamc@683 277 fun freshName {count, map, decls} = (count, {count = count + 1, map = map, decls = decls})
adamc@179 278 fun decls ({decls, ...} : t) = decls
adamc@179 279
adamc@179 280 fun lookup (t as {count, map, decls}) k n thunk =
adamc@120 281 let
adamc@179 282 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 283 in
adamc@179 284 case IM.find (im, n) of
adamc@179 285 NONE =>
adamc@179 286 let
adamc@179 287 val n' = count
adamc@179 288 val (d, {count, map, decls}) = thunk count {count = count + 1,
adamc@179 289 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@179 290 decls = decls}
adamc@179 291 in
adamc@179 292 ({count = count,
adamc@179 293 map = map,
adamc@179 294 decls = d :: decls}, n')
adamc@179 295 end
adamc@179 296 | SOME n' => (t, n')
adamc@179 297 end
adamc@179 298
adamc@179 299 end
adamc@185 300
adamc@185 301
adamc@185 302 fun capitalize s =
adamc@185 303 if s = "" then
adamc@185 304 s
adamc@185 305 else
adamc@185 306 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 307
adamc@179 308 fun fooifyExp fk env =
adamc@179 309 let
adamc@179 310 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 311 case #1 e of
adamc@120 312 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 313 let
adamc@120 314 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 315 in
adamc@385 316 ((L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
adamc@120 317 end
adamc@120 318 | L'.EClosure (fnam, args) =>
adamc@120 319 let
adamc@120 320 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 321 val ft = monoType env ft
adamc@111 322
adamc@179 323 fun attrify (args, ft, e, fm) =
adamc@120 324 case (args, ft) of
adamc@179 325 ([], _) => (e, fm)
adamc@120 326 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 327 let
adamc@179 328 val (arg', fm) = fooify fm (arg, t)
adamc@179 329 in
adamc@179 330 attrify (args, ft,
adamc@179 331 (L'.EStrcat (e,
adamc@179 332 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 333 arg'), loc)), loc),
adamc@179 334 fm)
adamc@179 335 end
adamc@120 336 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 337 (e, fm))
adamc@120 338 in
adamc@385 339 attrify (args, ft, (L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
adamc@120 340 end
adamc@120 341 | _ =>
adamc@120 342 case t of
adamc@185 343 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 344
adamc@179 345 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 346 | L'.TRecord ((x, t) :: xts) =>
adamc@200 347 let
adamc@200 348 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 349 in
adamc@200 350 foldl (fn ((x, t), (se, fm)) =>
adamc@200 351 let
adamc@200 352 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 353 in
adamc@200 354 ((L'.EStrcat (se,
adamc@200 355 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 356 se'), loc)), loc),
adamc@200 357 fm)
adamc@200 358 end) (se, fm) xts
adamc@200 359 end
adamc@111 360
adamc@196 361 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 362 let
adamc@179 363 fun makeDecl n fm =
adamc@179 364 let
adamc@193 365 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 366
adamc@179 367 val (branches, fm) =
adamc@179 368 ListUtil.foldlMap
adamc@179 369 (fn ((x, n, to), fm) =>
adamc@179 370 case to of
adamc@179 371 NONE =>
adamc@188 372 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 373 (L'.EPrim (Prim.String x), loc)),
adamc@179 374 fm)
adamc@179 375 | SOME t =>
adamc@179 376 let
adamc@182 377 val t = monoType env t
adamc@182 378 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 379 in
adamc@188 380 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 381 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 382 arg), loc)),
adamc@179 383 fm)
adamc@179 384 end)
adamc@179 385 fm xncs
adamc@179 386
adamc@179 387 val dom = tAll
adamc@179 388 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 389 in
adamc@179 390 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 391 n,
adamc@179 392 (L'.TFun (dom, ran), loc),
adamc@179 393 (L'.EAbs ("x",
adamc@179 394 dom,
adamc@179 395 ran,
adamc@179 396 (L'.ECase ((L'.ERel 0, loc),
adamc@179 397 branches,
adamc@182 398 {disc = dom,
adamc@182 399 result = ran}), loc)), loc),
adamc@179 400 "")], loc),
adamc@179 401 fm)
adamc@179 402 end
adamc@179 403
adamc@179 404 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 405 in
adamc@179 406 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 407 end
adamc@164 408
adamc@471 409 | L'.TOption t =>
adamc@471 410 let
adamc@471 411 val (body, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@471 412 in
adamc@471 413 ((L'.ECase (e,
adamc@471 414 [((L'.PNone t, loc),
adamc@471 415 (L'.EPrim (Prim.String "None"), loc)),
adamc@471 416
adamc@471 417 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc),
adamc@471 418 (L'.EStrcat ((L'.EPrim (Prim.String "Some/"), loc),
adamc@471 419 body), loc))],
adamc@471 420 {disc = tAll,
adamc@471 421 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@471 422 fm)
adamc@471 423 end
adamc@471 424
adamc@490 425 | _ => (E.errorAt loc "Don't know how to encode attribute/URL type";
adamc@120 426 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 427 (dummyExp, fm))
adamc@120 428 in
adamc@120 429 fooify
adamc@120 430 end
adamc@120 431
adamc@179 432 val attrifyExp = fooifyExp Attr
adamc@179 433 val urlifyExp = fooifyExp Url
adamc@105 434
adamc@143 435 datatype 'a failable_search =
adamc@143 436 Found of 'a
adamc@143 437 | NotFound
adamc@143 438 | Error
adamc@143 439
adamc@153 440 structure St :> sig
adamc@153 441 type t
adamc@153 442
adamc@153 443 val empty : t
adamc@153 444
adamc@153 445 val radioGroup : t -> string option
adamc@153 446 val setRadioGroup : t * string -> t
adamc@153 447 end = struct
adamc@153 448
adamc@153 449 type t = {
adamc@153 450 radioGroup : string option
adamc@153 451 }
adamc@153 452
adamc@153 453 val empty = {radioGroup = NONE}
adamc@153 454
adamc@153 455 fun radioGroup (t : t) = #radioGroup t
adamc@153 456
adamc@153 457 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 458
adamc@153 459 end
adamc@153 460
adamc@186 461 fun monoPatCon env pc =
adamc@178 462 case pc of
adamc@178 463 L.PConVar n => L'.PConVar n
adamc@188 464 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 465 arg = Option.map (monoType env) arg}
adamc@178 466
adamc@193 467 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 468
adamc@193 469 fun monoPat env (all as (p, loc)) =
adamc@193 470 let
adamc@193 471 fun poly () =
adamc@193 472 (E.errorAt loc "Unsupported pattern";
adamc@193 473 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 474 dummyPat)
adamc@193 475 in
adamc@193 476 case p of
adamc@193 477 L.PWild => (L'.PWild, loc)
adamc@193 478 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 479 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 480 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@288 481 | L.PCon (L.Option, _, [t], NONE) => (L'.PNone (monoType env t), loc)
adamc@288 482 | L.PCon (L.Option, _, [t], SOME p) => (L'.PSome (monoType env t, monoPat env p), loc)
adamc@193 483 | L.PCon _ => poly ()
adamc@193 484 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 485 end
adamc@178 486
adamc@252 487 fun strcat loc es =
adamc@252 488 case es of
adamc@252 489 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 490 | [e] => e
adamc@252 491 | _ =>
adamc@252 492 let
adamc@252 493 val e2 = List.last es
adamc@252 494 val es = List.take (es, length es - 1)
adamc@252 495 val e1 = List.last es
adamc@252 496 val es = List.take (es, length es - 1)
adamc@252 497 in
adamc@252 498 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
adamc@252 499 (L'.EStrcat (e1, e2), loc) es
adamc@252 500 end
adamc@252 501
adamc@252 502 fun strcatComma loc es =
adamc@252 503 case es of
adamc@252 504 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 505 | [e] => e
adamc@252 506 | _ =>
adamc@252 507 let
adamc@252 508 val e1 = List.last es
adamc@252 509 val es = List.take (es, length es - 1)
adamc@252 510 in
adamc@252 511 foldr (fn (e, e') =>
adamc@265 512 case (e, e') of
adamc@265 513 ((L'.EPrim (Prim.String ""), _), _) => e'
adamc@265 514 | (_, (L'.EPrim (Prim.String ""), _)) => e
adamc@252 515 | _ =>
adamc@252 516 (L'.EStrcat (e,
adamc@252 517 (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc))
adamc@252 518 e1 es
adamc@252 519 end
adamc@252 520
adamc@252 521 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
adamc@252 522
adamc@179 523 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 524 let
adamc@598 525 val strcat = strcat loc
adamc@598 526 val strcatComma = strcatComma loc
adamc@598 527 fun str s = (L'.EPrim (Prim.String s), loc)
adamc@598 528
adamc@25 529 fun poly () =
adamc@25 530 (E.errorAt loc "Unsupported expression";
adamc@25 531 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 532 (dummyExp, fm))
adamc@389 533
adamc@389 534 fun numTy t =
adamc@417 535 (L'.TRecord [("Zero", t),
adamc@417 536 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 537 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 538 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 539 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 540 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 541 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc)
adamc@417 542 fun numEx (t, zero, neg, plus, minus, times, dv, md) =
adamc@417 543 ((L'.ERecord [("Zero", (L'.EPrim zero, loc), t),
adamc@417 544 ("Neg", neg, (L'.TFun (t, t), loc)),
adamc@389 545 ("Plus", plus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 546 ("Minus", minus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 547 ("Times", times, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 548 ("Div", dv, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 549 ("Mod", md, (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc), fm)
adamc@391 550
adamc@391 551 fun ordTy t =
adamc@391 552 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 553 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))], loc)
adamc@391 554 fun ordEx (t, lt, le) =
adamc@391 555 ((L'.ERecord [("Lt", lt, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 556 ("Le", le, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 557 loc), fm)
adamc@25 558 in
adamc@25 559 case e of
adamc@179 560 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 561 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 562 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 563 | L.ECon (dk, pc, [], eo) =>
adamc@193 564 let
adamc@179 565 val (eo, fm) =
adamc@179 566 case eo of
adamc@179 567 NONE => (NONE, fm)
adamc@179 568 | SOME e =>
adamc@179 569 let
adamc@179 570 val (e, fm) = monoExp (env, st, fm) e
adamc@179 571 in
adamc@179 572 (SOME e, fm)
adamc@179 573 end
adamc@179 574 in
adamc@188 575 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 576 end
adamc@297 577 | L.ECon (L.Option, _, [t], NONE) =>
adamc@297 578 ((L'.ENone (monoType env t), loc), fm)
adamc@297 579 | L.ECon (L.Option, _, [t], SOME e) =>
adamc@297 580 let
adamc@297 581 val (e, fm) = monoExp (env, st, fm) e
adamc@297 582 in
adamc@297 583 ((L'.ESome (monoType env t, e), loc), fm)
adamc@297 584 end
adamc@193 585 | L.ECon _ => poly ()
adamc@94 586
adamc@387 587 | L.ECApp ((L.EFfi ("Basis", "eq"), _), t) =>
adamc@387 588 let
adamc@387 589 val t = monoType env t
adamc@387 590 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 591 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 592 in
adamc@387 593 ((L'.EAbs ("f", dom, dom,
adamc@387 594 (L'.ERel 0, loc)), loc), fm)
adamc@387 595 end
adamc@387 596 | L.ECApp ((L.EFfi ("Basis", "ne"), _), t) =>
adamc@387 597 let
adamc@387 598 val t = monoType env t
adamc@387 599 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 600 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 601 in
adamc@387 602 ((L'.EAbs ("f", dom, dom,
adamc@387 603 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@387 604 (L'.EAbs ("y", t, b,
adamc@387 605 (L'.EUnop ("!", (L'.EApp ((L'.EApp ((L'.ERel 2, loc),
adamc@387 606 (L'.ERel 1, loc)), loc),
adamc@387 607 (L'.ERel 0, loc)), loc)), loc)),
adamc@387 608 loc)),
adamc@387 609 loc)),
adamc@387 610 loc), fm)
adamc@387 611 end
adamc@387 612 | L.EFfi ("Basis", "eq_int") =>
adamc@387 613 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@387 614 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@387 615 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@387 616 (L'.TFfi ("Basis", "bool"), loc),
adamc@387 617 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@387 618 fm)
adamc@394 619 | L.EFfi ("Basis", "eq_float") =>
adamc@394 620 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@394 621 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 622 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@394 623 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 624 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@394 625 fm)
adamc@388 626 | L.EFfi ("Basis", "eq_bool") =>
adamc@388 627 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 628 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 629 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 630 (L'.TFfi ("Basis", "bool"), loc),
adamc@388 631 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 632 fm)
adamc@388 633 | L.EFfi ("Basis", "eq_string") =>
adamc@388 634 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@388 635 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 636 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@388 637 (L'.TFfi ("Basis", "bool"), loc),
adamc@388 638 (L'.EBinop ("!strcmp", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 639 fm)
adamc@437 640 | L.EFfi ("Basis", "eq_time") =>
adamc@437 641 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc),
adamc@437 642 (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@437 643 (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc),
adamc@437 644 (L'.TFfi ("Basis", "bool"), loc),
adamc@437 645 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@437 646 fm)
adamc@422 647 | L.ECApp ((L.EFfi ("Basis", "mkEq"), _), t) =>
adamc@422 648 let
adamc@422 649 val t = monoType env t
adamc@422 650 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@422 651 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@422 652 in
adamc@422 653 ((L'.EAbs ("f", dom, dom,
adamc@422 654 (L'.ERel 0, loc)), loc), fm)
adamc@422 655 end
adamc@387 656
adamc@417 657 | L.ECApp ((L.EFfi ("Basis", "zero"), _), t) =>
adamc@417 658 let
adamc@417 659 val t = monoType env t
adamc@417 660 in
adamc@417 661 ((L'.EAbs ("r", numTy t, t,
adamc@417 662 (L'.EField ((L'.ERel 0, loc), "Zero"), loc)), loc), fm)
adamc@417 663 end
adamc@389 664 | L.ECApp ((L.EFfi ("Basis", "neg"), _), t) =>
adamc@389 665 let
adamc@389 666 val t = monoType env t
adamc@389 667 in
adamc@389 668 ((L'.EAbs ("r", numTy t, (L'.TFun (t, t), loc),
adamc@389 669 (L'.EField ((L'.ERel 0, loc), "Neg"), loc)), loc), fm)
adamc@389 670 end
adamc@389 671 | L.ECApp ((L.EFfi ("Basis", "plus"), _), t) =>
adamc@389 672 let
adamc@389 673 val t = monoType env t
adamc@389 674 in
adamc@389 675 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 676 (L'.EField ((L'.ERel 0, loc), "Plus"), loc)), loc), fm)
adamc@389 677 end
adamc@389 678 | L.ECApp ((L.EFfi ("Basis", "minus"), _), t) =>
adamc@389 679 let
adamc@389 680 val t = monoType env t
adamc@389 681 in
adamc@389 682 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 683 (L'.EField ((L'.ERel 0, loc), "Minus"), loc)), loc), fm)
adamc@389 684 end
adamc@389 685 | L.ECApp ((L.EFfi ("Basis", "times"), _), t) =>
adamc@389 686 let
adamc@389 687 val t = monoType env t
adamc@389 688 in
adamc@389 689 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 690 (L'.EField ((L'.ERel 0, loc), "Times"), loc)), loc), fm)
adamc@389 691 end
adamc@389 692 | L.ECApp ((L.EFfi ("Basis", "div"), _), t) =>
adamc@389 693 let
adamc@389 694 val t = monoType env t
adamc@389 695 in
adamc@389 696 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 697 (L'.EField ((L'.ERel 0, loc), "Div"), loc)), loc), fm)
adamc@389 698 end
adamc@389 699 | L.ECApp ((L.EFfi ("Basis", "mod"), _), t) =>
adamc@389 700 let
adamc@389 701 val t = monoType env t
adamc@389 702 in
adamc@389 703 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 704 (L'.EField ((L'.ERel 0, loc), "Mod"), loc)), loc), fm)
adamc@389 705 end
adamc@389 706 | L.EFfi ("Basis", "num_int") =>
adamc@389 707 let
adamc@389 708 fun intBin s =
adamc@389 709 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 710 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "int"), loc)), loc),
adamc@389 711 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@389 712 (L'.TFfi ("Basis", "int"), loc),
adamc@389 713 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@389 714 in
adamc@389 715 numEx ((L'.TFfi ("Basis", "int"), loc),
adamc@417 716 Prim.Int (Int64.fromInt 0),
adamc@389 717 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 718 (L'.TFfi ("Basis", "int"), loc),
adamc@389 719 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@389 720 intBin "+",
adamc@389 721 intBin "-",
adamc@389 722 intBin "*",
adamc@389 723 intBin "/",
adamc@389 724 intBin "%")
adamc@389 725 end
adamc@390 726 | L.EFfi ("Basis", "num_float") =>
adamc@390 727 let
adamc@390 728 fun floatBin s =
adamc@390 729 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 730 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "float"), loc)), loc),
adamc@390 731 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@390 732 (L'.TFfi ("Basis", "float"), loc),
adamc@390 733 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@390 734 in
adamc@390 735 numEx ((L'.TFfi ("Basis", "float"), loc),
adamc@417 736 Prim.Float 0.0,
adamc@390 737 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 738 (L'.TFfi ("Basis", "float"), loc),
adamc@390 739 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@390 740 floatBin "+",
adamc@390 741 floatBin "-",
adamc@390 742 floatBin "*",
adamc@390 743 floatBin "/",
adamc@390 744 floatBin "fmod")
adamc@390 745 end
adamc@391 746
adamc@391 747 | L.ECApp ((L.EFfi ("Basis", "lt"), _), t) =>
adamc@391 748 let
adamc@391 749 val t = monoType env t
adamc@391 750 in
adamc@391 751 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
adamc@391 752 (L'.EField ((L'.ERel 0, loc), "Lt"), loc)), loc), fm)
adamc@391 753 end
adamc@391 754 | L.ECApp ((L.EFfi ("Basis", "le"), _), t) =>
adamc@391 755 let
adamc@391 756 val t = monoType env t
adamc@391 757 in
adamc@391 758 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
adamc@391 759 (L'.EField ((L'.ERel 0, loc), "Le"), loc)), loc), fm)
adamc@391 760 end
adamc@392 761 | L.ECApp ((L.EFfi ("Basis", "gt"), _), t) =>
adamc@392 762 let
adamc@392 763 val t = monoType env t
adamc@392 764 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@392 765 in
adamc@392 766 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
adamc@392 767 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@392 768 (L'.EAbs ("y", t, b,
adamc@392 769 (L'.EUnop ("!",
adamc@392 770 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
adamc@392 771 "Le"), loc),
adamc@392 772 (L'.ERel 1, loc)), loc),
adamc@392 773 (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@392 774 loc)),
adamc@392 775 loc), fm)
adamc@392 776 end
adamc@392 777 | L.ECApp ((L.EFfi ("Basis", "ge"), _), t) =>
adamc@392 778 let
adamc@392 779 val t = monoType env t
adamc@392 780 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@392 781 in
adamc@392 782 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
adamc@392 783 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@392 784 (L'.EAbs ("y", t, b,
adamc@392 785 (L'.EUnop ("!",
adamc@392 786 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
adamc@392 787 "Lt"), loc),
adamc@392 788 (L'.ERel 1, loc)), loc),
adamc@392 789 (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@392 790 loc)),
adamc@392 791 loc), fm)
adamc@392 792 end
adamc@391 793 | L.EFfi ("Basis", "ord_int") =>
adamc@391 794 let
adamc@391 795 fun intBin s =
adamc@391 796 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@391 797 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@391 798 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@391 799 (L'.TFfi ("Basis", "bool"), loc),
adamc@391 800 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@391 801 in
adamc@391 802 ordEx ((L'.TFfi ("Basis", "int"), loc),
adamc@391 803 intBin "<",
adamc@391 804 intBin "<=")
adamc@391 805 end
adamc@394 806 | L.EFfi ("Basis", "ord_float") =>
adamc@394 807 let
adamc@394 808 fun floatBin s =
adamc@394 809 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@394 810 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 811 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@394 812 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 813 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@394 814 in
adamc@394 815 ordEx ((L'.TFfi ("Basis", "float"), loc),
adamc@394 816 floatBin "<",
adamc@394 817 floatBin "<=")
adamc@394 818 end
adamc@394 819 | L.EFfi ("Basis", "ord_bool") =>
adamc@394 820 let
adamc@394 821 fun boolBin s =
adamc@394 822 (L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@394 823 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 824 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@394 825 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 826 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@394 827 in
adamc@394 828 ordEx ((L'.TFfi ("Basis", "bool"), loc),
adamc@394 829 boolBin "<",
adamc@394 830 boolBin "<=")
adamc@394 831 end
adamc@395 832 | L.EFfi ("Basis", "ord_string") =>
adamc@395 833 let
adamc@395 834 fun boolBin s =
adamc@395 835 (L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@395 836 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@395 837 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@395 838 (L'.TFfi ("Basis", "bool"), loc),
adamc@395 839 (L'.EBinop (s,
adamc@395 840 (L'.EBinop ("strcmp",
adamc@395 841 (L'.ERel 1, loc),
adamc@395 842 (L'.ERel 0, loc)), loc),
adamc@395 843 (L'.EPrim (Prim.Int (Int64.fromInt 0)), loc)), loc)), loc)), loc)
adamc@395 844 in
adamc@395 845 ordEx ((L'.TFfi ("Basis", "string"), loc),
adamc@395 846 boolBin "<",
adamc@395 847 boolBin "<=")
adamc@395 848 end
adamc@437 849 | L.EFfi ("Basis", "ord_time") =>
adamc@437 850 let
adamc@437 851 fun boolBin s =
adamc@437 852 (L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc),
adamc@437 853 (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@437 854 (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc),
adamc@437 855 (L'.TFfi ("Basis", "bool"), loc),
adamc@437 856 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@437 857 in
adamc@437 858 ordEx ((L'.TFfi ("Basis", "time"), loc),
adamc@437 859 boolBin "<",
adamc@437 860 boolBin "<=")
adamc@437 861 end
adamc@389 862
adamc@286 863 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
adamc@286 864 let
adamc@286 865 val t = monoType env t
adamc@286 866 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 867 in
adamc@286 868 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@286 869 (L'.ERel 0, loc)), loc), fm)
adamc@286 870 end
adamc@286 871 | L.EFfi ("Basis", "show_int") =>
adamc@286 872 ((L'.EFfi ("Basis", "intToString"), loc), fm)
adamc@286 873 | L.EFfi ("Basis", "show_float") =>
adamc@286 874 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
adamc@286 875 | L.EFfi ("Basis", "show_string") =>
adamc@286 876 let
adamc@286 877 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 878 in
adamc@286 879 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@286 880 end
adamc@286 881 | L.EFfi ("Basis", "show_bool") =>
adamc@286 882 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
adamc@436 883 | L.EFfi ("Basis", "show_time") =>
adamc@436 884 ((L'.EFfi ("Basis", "timeToString"), loc), fm)
adamc@452 885 | L.ECApp ((L.EFfi ("Basis", "mkShow"), _), t) =>
adamc@452 886 let
adamc@452 887 val t = monoType env t
adamc@452 888 val b = (L'.TFfi ("Basis", "string"), loc)
adamc@452 889 val dom = (L'.TFun (t, b), loc)
adamc@452 890 in
adamc@452 891 ((L'.EAbs ("f", dom, dom,
adamc@452 892 (L'.ERel 0, loc)), loc), fm)
adamc@452 893 end
adamc@286 894
adamc@290 895 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
adamc@290 896 let
adamc@290 897 val t = monoType env t
adamc@290 898 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 899 in
adamc@292 900 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
adamc@292 901 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
adamc@292 902 end
adamc@292 903 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
adamc@292 904 let
adamc@292 905 val t = monoType env t
adamc@292 906 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@292 907 in
adamc@292 908 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
adamc@292 909 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
adamc@290 910 end
adamc@290 911 | L.EFfi ("Basis", "read_int") =>
adamc@292 912 let
adamc@292 913 val t = (L'.TFfi ("Basis", "int"), loc)
adamc@292 914 in
adamc@292 915 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
adamc@292 916 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
adamc@292 917 loc),
adamc@292 918 fm)
adamc@292 919 end
adamc@290 920 | L.EFfi ("Basis", "read_float") =>
adamc@292 921 let
adamc@292 922 val t = (L'.TFfi ("Basis", "float"), loc)
adamc@292 923 in
adamc@292 924 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
adamc@292 925 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
adamc@292 926 loc),
adamc@292 927 fm)
adamc@292 928 end
adamc@290 929 | L.EFfi ("Basis", "read_string") =>
adamc@290 930 let
adamc@290 931 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 932 in
adamc@292 933 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
adamc@292 934 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
adamc@292 935 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
adamc@292 936 fm)
adamc@290 937 end
adamc@290 938 | L.EFfi ("Basis", "read_bool") =>
adamc@292 939 let
adamc@292 940 val t = (L'.TFfi ("Basis", "bool"), loc)
adamc@292 941 in
adamc@292 942 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
adamc@292 943 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
adamc@292 944 loc),
adamc@292 945 fm)
adamc@292 946 end
adamc@436 947 | L.EFfi ("Basis", "read_time") =>
adamc@436 948 let
adamc@436 949 val t = (L'.TFfi ("Basis", "time"), loc)
adamc@436 950 in
adamc@436 951 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToTime"), loc), readType' (t, loc)),
adamc@436 952 ("ReadError", (L'.EFfi ("Basis", "stringToTime_error"), loc), readErrType (t, loc))],
adamc@436 953 loc),
adamc@436 954 fm)
adamc@436 955 end
adamc@290 956
adamc@564 957 | L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), _), _), t), _),
adamc@564 958 (L.EFfi ("Basis", "transaction_monad"), _)) =>
adamc@252 959 let
adamc@252 960 val t = monoType env t
adamc@252 961 in
adamc@252 962 ((L'.EAbs ("x", t,
adamc@252 963 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@252 964 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@252 965 (L'.ERel 1, loc)), loc)), loc), fm)
adamc@252 966 end
adamc@564 967 | L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _),
adamc@564 968 (L.EFfi ("Basis", "transaction_monad"), _)) =>
adamc@251 969 let
adamc@251 970 val t1 = monoType env t1
adamc@251 971 val t2 = monoType env t2
adamc@251 972 val un = (L'.TRecord [], loc)
adamc@252 973 val mt1 = (L'.TFun (un, t1), loc)
adamc@252 974 val mt2 = (L'.TFun (un, t2), loc)
adamc@251 975 in
adamc@572 976 ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc)), loc),
adamc@572 977 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc),
adamc@252 978 (L'.EAbs ("_", un, un,
adamc@252 979 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
adamc@252 980 (L'.ERecord [], loc)), loc),
adamc@252 981 (L'.EApp (
adamc@252 982 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
adamc@252 983 (L'.ERecord [], loc)),
adamc@252 984 loc)), loc)), loc)), loc)), loc),
adamc@251 985 fm)
adamc@251 986 end
adamc@670 987 | L.EApp ((L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), _), _), t2), _),
adamc@670 988 (L.EFfi ("Basis", "transaction_monad"), _)), _),
adamc@670 989 (L.EApp ((L.ECApp ((L.EFfi ("Basis", "recv"), _), t1), _),
adamc@670 990 ch), loc)) =>
adamc@670 991 let
adamc@670 992 val t1 = monoType env t1
adamc@670 993 val t2 = monoType env t2
adamc@670 994 val un = (L'.TRecord [], loc)
adamc@670 995 val mt2 = (L'.TFun (un, t2), loc)
adamc@670 996 val (ch, fm) = monoExp (env, st, fm) ch
adamc@670 997 in
adamc@670 998 ((L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc),
adamc@670 999 (L'.EAbs ("_", un, un,
adamc@670 1000 (L'.ERecv (liftExpInExp 0 (liftExpInExp 0 ch),
adamc@670 1001 (L'.ERel 1, loc),
adamc@670 1002 t1), loc)), loc)), loc),
adamc@670 1003 fm)
adamc@670 1004 end
adamc@251 1005
adamc@565 1006 | L.ECApp ((L.EFfi ("Basis", "source"), _), t) =>
adamc@565 1007 let
adamc@565 1008 val t = monoType env t
adamc@565 1009 in
adamc@577 1010 ((L'.EAbs ("x", t, (L'.TFun ((L'.TRecord [], loc), (L'.TSource, loc)), loc),
adamc@577 1011 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TSource, loc),
adamc@577 1012 (L'.EFfiApp ("Basis", "new_client_source",
adamc@590 1013 [(L'.EJavaScript (L'.Source t, (L'.ERel 1, loc), NONE), loc)]),
adamc@578 1014 loc)), loc)),
adamc@565 1015 loc),
adamc@565 1016 fm)
adamc@565 1017 end
adamc@575 1018 | L.ECApp ((L.EFfi ("Basis", "set"), _), t) =>
adamc@575 1019 let
adamc@575 1020 val t = monoType env t
adamc@575 1021 in
adamc@577 1022 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@575 1023 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@575 1024 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@575 1025 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@575 1026 (L'.EFfiApp ("Basis", "set_client_source",
adamc@577 1027 [(L'.ERel 2, loc),
adamc@590 1028 (L'.EJavaScript (L'.Source t,
adamc@590 1029 (L'.ERel 1, loc), NONE), loc)]),
adamc@575 1030 loc)), loc)), loc)), loc),
adamc@575 1031 fm)
adamc@575 1032 end
adamc@601 1033 | L.ECApp ((L.EFfi ("Basis", "get"), _), t) =>
adamc@601 1034 let
adamc@601 1035 val t = monoType env t
adamc@601 1036 in
adamc@601 1037 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@601 1038 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@601 1039 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@601 1040 (L'.EFfiApp ("Basis", "get_client_source",
adamc@601 1041 [(L'.ERel 1, loc)]),
adamc@601 1042 loc)), loc)), loc),
adamc@601 1043 fm)
adamc@601 1044 end
adamc@565 1045
adamc@568 1046 | L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), _), _), t), _),
adamc@568 1047 (L.EFfi ("Basis", "signal_monad"), _)) =>
adamc@568 1048 let
adamc@568 1049 val t = monoType env t
adamc@568 1050 in
adamc@568 1051 ((L'.EAbs ("x", t, (L'.TSignal t, loc),
adamc@568 1052 (L'.ESignalReturn (L'.ERel 0, loc), loc)), loc),
adamc@568 1053 fm)
adamc@568 1054 end
adamc@572 1055 | L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _),
adamc@572 1056 (L.EFfi ("Basis", "signal_monad"), _)) =>
adamc@572 1057 let
adamc@572 1058 val t1 = monoType env t1
adamc@572 1059 val t2 = monoType env t2
adamc@572 1060 val un = (L'.TRecord [], loc)
adamc@572 1061 val mt1 = (L'.TSignal t1, loc)
adamc@572 1062 val mt2 = (L'.TSignal t2, loc)
adamc@572 1063 in
adamc@572 1064 ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), mt2), loc),
adamc@572 1065 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), mt2,
adamc@572 1066 (L'.ESignalBind ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@572 1067 fm)
adamc@572 1068 end
adamc@574 1069 | L.ECApp ((L.EFfi ("Basis", "signal"), _), t) =>
adamc@574 1070 let
adamc@574 1071 val t = monoType env t
adamc@574 1072 in
adamc@574 1073 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TSignal t, loc),
adamc@574 1074 (L'.ESignalSource (L'.ERel 0, loc), loc)), loc),
adamc@574 1075 fm)
adamc@574 1076 end
adamc@568 1077
adamc@462 1078 | L.ECApp ((L.EFfi ("Basis", "getCookie"), _), t) =>
adamc@462 1079 let
adamc@462 1080 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1081 val un = (L'.TRecord [], loc)
adamc@462 1082 val t = monoType env t
adamc@462 1083 in
adamc@462 1084 ((L'.EAbs ("c", s, (L'.TFun (un, s), loc),
adamc@462 1085 (L'.EAbs ("_", un, s,
adamc@463 1086 (L'.EUnurlify ((L'.EFfiApp ("Basis", "get_cookie", [(L'.ERel 1, loc)]), loc),
adamc@463 1087 t),
adamc@463 1088 loc)), loc)), loc),
adamc@462 1089 fm)
adamc@462 1090 end
adamc@462 1091
adamc@462 1092 | L.ECApp ((L.EFfi ("Basis", "setCookie"), _), t) =>
adamc@462 1093 let
adamc@462 1094 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1095 val un = (L'.TRecord [], loc)
adamc@462 1096 val t = monoType env t
adamc@462 1097 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@462 1098 in
adamc@462 1099 ((L'.EAbs ("c", s, (L'.TFun (t, (L'.TFun (un, un), loc)), loc),
adamc@462 1100 (L'.EAbs ("v", t, (L'.TFun (un, un), loc),
adamc@462 1101 (L'.EAbs ("_", un, un,
adamc@466 1102 (L'.EFfiApp ("Basis", "set_cookie", [(L'.EPrim (Prim.String (!urlPrefix)),
adamc@466 1103 loc),
adamc@466 1104 (L'.ERel 2, loc),
adamc@466 1105 e]), loc)),
adamc@462 1106 loc)), loc)), loc),
adamc@462 1107 fm)
adamc@462 1108 end
adamc@462 1109
adamc@668 1110 | L.ECApp ((L.EFfi ("Basis", "channel"), _), t) =>
adamc@668 1111 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1112 (L'.EFfiApp ("Basis", "new_channel", [(L'.ERecord [], loc)]), loc)), loc),
adamc@668 1113 fm)
adamc@668 1114 | L.ECApp ((L.EFfi ("Basis", "send"), _), t) =>
adamc@668 1115 let
adamc@668 1116 val t = monoType env t
adamc@668 1117 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@668 1118 in
adamc@668 1119 ((L'.EAbs ("ch", (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1120 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@668 1121 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@668 1122 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@668 1123 (L'.EFfiApp ("Basis", "send",
adamc@668 1124 [(L'.ERel 2, loc),
adamc@668 1125 e]),
adamc@668 1126 loc)), loc)), loc)), loc),
adamc@668 1127 fm)
adamc@668 1128 end
adamc@668 1129
adamc@307 1130 | L.EFfiApp ("Basis", "dml", [e]) =>
adamc@307 1131 let
adamc@307 1132 val (e, fm) = monoExp (env, st, fm) e
adamc@307 1133 in
adamc@468 1134 ((L'.EDml e, loc),
adamc@307 1135 fm)
adamc@307 1136 end
adamc@308 1137
adamc@307 1138 | L.ECApp ((L.EFfi ("Basis", "insert"), _), fields) =>
adamc@307 1139 (case monoType env (L.TRecord fields, loc) of
adamc@307 1140 (L'.TRecord fields, _) =>
adamc@307 1141 let
adamc@307 1142 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@307 1143 val fields = map (fn (x, _) => (x, s)) fields
adamc@307 1144 val rt = (L'.TRecord fields, loc)
adamc@307 1145 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@307 1146 in
adamc@307 1147 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
adamc@307 1148 (L'.EAbs ("fs", rt, s,
adamc@598 1149 strcat [sc "INSERT INTO ",
adamc@598 1150 (L'.ERel 1, loc),
adamc@598 1151 sc " (",
adamc@598 1152 strcatComma (map (fn (x, _) => sc ("uw_" ^ x)) fields),
adamc@598 1153 sc ") VALUES (",
adamc@598 1154 strcatComma (map (fn (x, _) =>
adamc@598 1155 (L'.EField ((L'.ERel 0, loc),
adamc@598 1156 x), loc)) fields),
adamc@598 1157 sc ")"]), loc)), loc),
adamc@307 1158 fm)
adamc@307 1159 end
adamc@307 1160 | _ => poly ())
adamc@307 1161
adamc@403 1162 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), changed) =>
adamc@308 1163 (case monoType env (L.TRecord changed, loc) of
adamc@308 1164 (L'.TRecord changed, _) =>
adamc@308 1165 let
adamc@308 1166 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@308 1167 val changed = map (fn (x, _) => (x, s)) changed
adamc@308 1168 val rt = (L'.TRecord changed, loc)
adamc@308 1169 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@308 1170 in
adamc@308 1171 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@308 1172 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@308 1173 (L'.EAbs ("e", s, s,
adamc@598 1174 strcat [sc "UPDATE ",
adamc@598 1175 (L'.ERel 1, loc),
adamc@598 1176 sc " AS T SET ",
adamc@598 1177 strcatComma (map (fn (x, _) =>
adamc@598 1178 strcat [sc ("uw_" ^ x
adamc@598 1179 ^ " = "),
adamc@598 1180 (L'.EField
adamc@598 1181 ((L'.ERel 2,
adamc@598 1182 loc),
adamc@598 1183 x), loc)])
adamc@598 1184 changed),
adamc@598 1185 sc " WHERE ",
adamc@598 1186 (L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@308 1187 fm)
adamc@308 1188 end
adamc@308 1189 | _ => poly ())
adamc@308 1190
adamc@309 1191 | L.ECApp ((L.EFfi ("Basis", "delete"), _), _) =>
adamc@309 1192 let
adamc@309 1193 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@309 1194 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@309 1195 in
adamc@309 1196 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@309 1197 (L'.EAbs ("e", s, s,
adamc@598 1198 strcat [sc "DELETE FROM ",
adamc@598 1199 (L'.ERel 1, loc),
adamc@598 1200 sc " AS T WHERE ",
adamc@598 1201 (L'.ERel 0, loc)]), loc)), loc),
adamc@309 1202 fm)
adamc@309 1203 end
adamc@309 1204
adamc@252 1205 | L.ECApp (
adamc@252 1206 (L.ECApp (
adamc@252 1207 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 1208 exps), _),
adamc@252 1209 state) =>
adamc@252 1210 (case monoType env (L.TRecord exps, loc) of
adamc@252 1211 (L'.TRecord exps, _) =>
adamc@252 1212 let
adamc@252 1213 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1214 (case monoType env (L.TRecord xts, loc) of
adamc@252 1215 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1216 | _ => NONE)
adamc@252 1217 | _ => NONE) tables
adamc@252 1218 in
adamc@252 1219 if List.exists (fn x => x = NONE) tables then
adamc@252 1220 poly ()
adamc@252 1221 else
adamc@252 1222 let
adamc@252 1223 val tables = List.mapPartial (fn x => x) tables
adamc@252 1224 val state = monoType env state
adamc@252 1225 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1226 val un = (L'.TRecord [], loc)
adamc@252 1227
adamc@252 1228 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 1229 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 1230 (L'.TFun (state,
adamc@252 1231 (L'.TFun (un, state), loc)),
adamc@252 1232 loc)), loc)
adamc@252 1233
adamc@267 1234 val body' = (L'.EApp (
adamc@267 1235 (L'.EApp (
adamc@267 1236 (L'.EApp ((L'.ERel 4, loc),
adamc@267 1237 (L'.ERel 1, loc)), loc),
adamc@267 1238 (L'.ERel 0, loc)), loc),
adamc@267 1239 (L'.ERecord [], loc)), loc)
adamc@252 1240
adamc@252 1241 val body = (L'.EQuery {exps = exps,
adamc@252 1242 tables = tables,
adamc@252 1243 state = state,
adamc@252 1244 query = (L'.ERel 3, loc),
adamc@252 1245 body = body',
adamc@252 1246 initial = (L'.ERel 1, loc)},
adamc@252 1247 loc)
adamc@252 1248 in
adamc@252 1249 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 1250 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 1251 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 1252 (L'.EAbs ("_", un, state,
adamc@252 1253 body), loc)), loc)), loc)), loc), fm)
adamc@252 1254 end
adamc@252 1255 end
adamc@252 1256 | _ => poly ())
adamc@252 1257
adamc@252 1258 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 1259 let
adamc@252 1260 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1261 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1262 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1263 in
adamc@252 1264 ((L'.EAbs ("r",
adamc@252 1265 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 1266 s,
adamc@598 1267 strcat [gf "Rows",
adamc@598 1268 (L'.ECase (gf "OrderBy",
adamc@598 1269 [((L'.PPrim (Prim.String ""), loc), sc ""),
adamc@598 1270 ((L'.PWild, loc),
adamc@598 1271 strcat [sc " ORDER BY ",
adamc@598 1272 gf "OrderBy"])],
adamc@598 1273 {disc = s, result = s}), loc),
adamc@598 1274 gf "Limit",
adamc@598 1275 gf "Offset"]), loc), fm)
adamc@252 1276 end
adamc@252 1277
adamc@252 1278 | L.ECApp (
adamc@252 1279 (L.ECApp (
adamc@252 1280 (L.ECApp (
adamc@252 1281 (L.ECApp (
adamc@252 1282 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 1283 (L.CRecord (_, tables), _)), _),
adamc@252 1284 (L.CRecord (_, grouped), _)), _),
adamc@252 1285 (L.CRecord (_, stables), _)), _),
adamc@252 1286 sexps) =>
adamc@252 1287 let
adamc@252 1288 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1289 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1290 val un = (L'.TRecord [], loc)
adamc@252 1291 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1292
adamc@252 1293 fun doTables tables =
adamc@252 1294 let
adamc@252 1295 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1296 (case monoType env (L.TRecord xts, loc) of
adamc@252 1297 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1298 | _ => NONE)
adamc@252 1299 | _ => NONE) tables
adamc@252 1300 in
adamc@252 1301 if List.exists (fn x => x = NONE) tables then
adamc@252 1302 NONE
adamc@252 1303 else
adamc@260 1304 let
adamc@260 1305 val tables = List.mapPartial (fn x => x) tables
adamc@260 1306 val tables = ListMergeSort.sort
adamc@260 1307 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1308 tables
adamc@260 1309 val tables = map (fn (x, xts) =>
adamc@260 1310 (x, ListMergeSort.sort
adamc@260 1311 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1312 xts)) tables
adamc@260 1313 in
adamc@260 1314 SOME tables
adamc@260 1315 end
adamc@252 1316 end
adamc@252 1317 in
adamc@252 1318 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 1319 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@441 1320 let
adamc@441 1321 val sexps = ListMergeSort.sort
adamc@441 1322 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) sexps
adamc@441 1323 in
adamc@441 1324 ((L'.EAbs ("r",
adamc@441 1325 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
adamc@441 1326 ("Where", s),
adamc@441 1327 ("GroupBy", un),
adamc@441 1328 ("Having", s),
adamc@441 1329 ("SelectFields", un),
adamc@441 1330 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@441 1331 loc),
adamc@441 1332 s,
adamc@598 1333 strcat [sc "SELECT ",
adamc@598 1334 strcatComma (map (fn (x, t) =>
adamc@598 1335 strcat [
adamc@598 1336 (L'.EField (gf "SelectExps", x), loc),
adamc@598 1337 sc (" AS _" ^ x)
adamc@598 1338 ]) sexps
adamc@598 1339 @ map (fn (x, xts) =>
adamc@598 1340 strcatComma
adamc@598 1341 (map (fn (x', _) =>
adamc@598 1342 sc (x ^ ".uw_" ^ x'))
adamc@598 1343 xts)) stables),
adamc@598 1344 sc " FROM ",
adamc@598 1345 strcatComma (map (fn (x, _) => strcat [(L'.EField (gf "From", x), loc),
adamc@598 1346 sc (" AS " ^ x)]) tables),
adamc@598 1347 (L'.ECase (gf "Where",
adamc@598 1348 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@598 1349 sc ""),
adamc@598 1350 ((L'.PWild, loc),
adamc@598 1351 strcat [sc " WHERE ", gf "Where"])],
adamc@598 1352 {disc = s,
adamc@598 1353 result = s}), loc),
adamc@598 1354
adamc@598 1355 if List.all (fn (x, xts) =>
adamc@598 1356 case List.find (fn (x', _) => x' = x) grouped of
adamc@598 1357 NONE => List.null xts
adamc@598 1358 | SOME (_, xts') =>
adamc@598 1359 List.all (fn (x, _) =>
adamc@598 1360 List.exists (fn (x', _) => x' = x)
adamc@598 1361 xts') xts) tables then
adamc@598 1362 sc ""
adamc@598 1363 else
adamc@598 1364 strcat [
adamc@598 1365 sc " GROUP BY ",
adamc@598 1366 strcatComma (map (fn (x, xts) =>
adamc@598 1367 strcatComma
adamc@598 1368 (map (fn (x', _) =>
adamc@598 1369 sc (x ^ ".uw_" ^ x'))
adamc@598 1370 xts)) grouped)
adamc@598 1371 ],
adamc@259 1372
adamc@598 1373 (L'.ECase (gf "Having",
adamc@598 1374 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@598 1375 sc ""),
adamc@598 1376 ((L'.PWild, loc),
adamc@598 1377 strcat [sc " HAVING ", gf "Having"])],
adamc@598 1378 {disc = s,
adamc@598 1379 result = s}), loc)
adamc@441 1380 ]), loc),
adamc@441 1381 fm)
adamc@441 1382 end
adamc@252 1383 | _ => poly ()
adamc@252 1384 end
adamc@252 1385
adamc@252 1386 | L.ECApp (
adamc@252 1387 (L.ECApp (
adamc@252 1388 (L.ECApp (
adamc@252 1389 (L.ECApp (
adamc@252 1390 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 1391 _), _),
adamc@252 1392 _), _),
adamc@252 1393 _), _),
adamc@252 1394 t) =>
adamc@252 1395 let
adamc@252 1396 val t = monoType env t
adamc@252 1397 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1398 in
adamc@252 1399 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 1400 (L'.ERel 0, loc)), loc), fm)
adamc@252 1401 end
adamc@252 1402
adamc@253 1403 | L.EFfi ("Basis", "sql_int") =>
adamc@253 1404 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1405 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1406 fm)
adamc@253 1407 | L.EFfi ("Basis", "sql_float") =>
adamc@253 1408 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1409 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1410 fm)
adamc@253 1411 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 1412 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1413 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1414 fm)
adamc@253 1415 | L.EFfi ("Basis", "sql_string") =>
adamc@253 1416 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1417 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1418 fm)
adamc@439 1419 | L.EFfi ("Basis", "sql_time") =>
adamc@439 1420 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@439 1421 (L'.EFfiApp ("Basis", "sqlifyTime", [(L'.ERel 0, loc)]), loc)), loc),
adamc@439 1422 fm)
adamc@678 1423 | L.ECApp ((L.EFfi ("Basis", "sql_channel"), _), _) =>
adamc@678 1424 ((L'.EAbs ("x", (L'.TFfi ("Basis", "channel"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@678 1425 (L'.EFfiApp ("Basis", "sqlifyChannel", [(L'.ERel 0, loc)]), loc)), loc),
adamc@678 1426 fm)
adamc@682 1427 | L.EFfi ("Basis", "sql_client") =>
adamc@682 1428 ((L'.EAbs ("x", (L'.TFfi ("Basis", "client"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@682 1429 (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)), loc),
adamc@682 1430 fm)
adamc@676 1431 | L.ECApp ((L.EFfi ("Basis", "sql_prim"), _), t) =>
adamc@676 1432 let
adamc@676 1433 val t = monoType env t
adamc@676 1434 val tf = (L'.TFun (t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@676 1435 in
adamc@676 1436 ((L'.EAbs ("f", tf, tf, (L'.ERel 0, loc)), loc),
adamc@676 1437 fm)
adamc@676 1438 end
adamc@676 1439 | L.ECApp ((L.EFfi ("Basis", "sql_option_prim"), _), t) =>
adamc@676 1440 let
adamc@676 1441 val t = monoType env t
adamc@676 1442 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@676 1443 in
adamc@676 1444 ((L'.EAbs ("f",
adamc@676 1445 (L'.TFun (t, s), loc),
adamc@676 1446 (L'.TFun ((L'.TOption t, loc), s), loc),
adamc@676 1447 (L'.EAbs ("x",
adamc@676 1448 (L'.TOption t, loc),
adamc@676 1449 s,
adamc@676 1450 (L'.ECase ((L'.ERel 0, loc),
adamc@676 1451 [((L'.PNone t, loc),
adamc@676 1452 (L'.EPrim (Prim.String "NULL"), loc)),
adamc@676 1453 ((L'.PSome (t, (L'.PVar ("y", t), loc)), loc),
adamc@676 1454 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc))],
adamc@676 1455 {disc = (L'.TOption t, loc),
adamc@676 1456 result = s}), loc)), loc)), loc),
adamc@676 1457 fm)
adamc@676 1458 end
adamc@253 1459
adamc@252 1460 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 1461 ((L'.ERecord [], loc), fm)
adamc@252 1462 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 1463 ((L'.ERecord [], loc), fm)
adamc@252 1464
adamc@252 1465 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 1466 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@261 1467 | L.ECApp (
adamc@261 1468 (L.ECApp (
adamc@261 1469 (L.ECApp (
adamc@261 1470 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adamc@261 1471 _), _),
adamc@261 1472 _), _),
adamc@261 1473 _) =>
adamc@261 1474 let
adamc@261 1475 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 1476 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@261 1477 in
adamc@268 1478 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@268 1479 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adamc@268 1480 (L'.EAbs ("e2", s, s,
adamc@268 1481 (L'.ECase ((L'.ERel 0, loc),
adamc@268 1482 [((L'.PPrim (Prim.String ""), loc),
adamc@598 1483 strcat [(L'.ERel 2, loc),
adamc@598 1484 (L'.ERel 1, loc)]),
adamc@268 1485 ((L'.PWild, loc),
adamc@598 1486 strcat [(L'.ERel 2, loc),
adamc@598 1487 (L'.ERel 1, loc),
adamc@598 1488 sc ", ",
adamc@598 1489 (L'.ERel 0, loc)])],
adamc@268 1490 {disc = s, result = s}), loc)), loc)), loc)), loc),
adamc@261 1491 fm)
adamc@261 1492 end
adamc@252 1493
adamc@252 1494 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 1495 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@262 1496 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
adamc@262 1497 let
adamc@262 1498 val (e, fm) = monoExp (env, st, fm) e
adamc@262 1499 in
adamc@598 1500 (strcat [
adamc@262 1501 (L'.EPrim (Prim.String " LIMIT "), loc),
adamc@262 1502 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@262 1503 ],
adamc@262 1504 fm)
adamc@262 1505 end
adamc@262 1506
adamc@252 1507 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 1508 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@263 1509 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
adamc@263 1510 let
adamc@263 1511 val (e, fm) = monoExp (env, st, fm) e
adamc@263 1512 in
adamc@598 1513 (strcat [
adamc@263 1514 (L'.EPrim (Prim.String " OFFSET "), loc),
adamc@263 1515 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@263 1516 ],
adamc@263 1517 fm)
adamc@263 1518 end
adamc@253 1519
adamc@559 1520 | L.ECApp ((L.EFfi ("Basis", "sql_eq"), _), _) =>
adamc@253 1521 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@559 1522 | L.ECApp ((L.EFfi ("Basis", "sql_ne"), _), _) =>
adamc@253 1523 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@559 1524 | L.ECApp ((L.EFfi ("Basis", "sql_lt"), _), _) =>
adamc@253 1525 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@559 1526 | L.ECApp ((L.EFfi ("Basis", "sql_le"), _), _) =>
adamc@253 1527 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@559 1528 | L.ECApp ((L.EFfi ("Basis", "sql_gt"), _), _) =>
adamc@253 1529 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@559 1530 | L.ECApp ((L.EFfi ("Basis", "sql_ge"), _), _) =>
adamc@253 1531 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 1532
adamc@559 1533 | L.ECApp ((L.EFfi ("Basis", "sql_plus"), _), _) =>
adamc@559 1534 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1535 (L'.EPrim (Prim.String "+"), loc)), loc), fm)
adamc@559 1536 | L.ECApp ((L.EFfi ("Basis", "sql_minus"), _), _) =>
adamc@559 1537 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1538 (L'.EPrim (Prim.String "-"), loc)), loc), fm)
adamc@559 1539 | L.ECApp ((L.EFfi ("Basis", "sql_times"), _), _) =>
adamc@559 1540 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1541 (L'.EPrim (Prim.String "*"), loc)), loc), fm)
adamc@559 1542 | L.ECApp ((L.EFfi ("Basis", "sql_div"), _), _) =>
adamc@559 1543 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1544 (L'.EPrim (Prim.String "/"), loc)), loc), fm)
adamc@559 1545 | L.EFfi ("Basis", "sql_mod") =>
adamc@559 1546 ((L'.EPrim (Prim.String "%"), loc), fm)
adamc@559 1547
adamc@253 1548 | L.ECApp (
adamc@253 1549 (L.ECApp (
adamc@253 1550 (L.ECApp (
adamc@253 1551 (L.ECApp (
adamc@254 1552 (L.ECApp (
adamc@264 1553 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 1554 _), _),
adamc@264 1555 _), _),
adamc@264 1556 _), _),
adamc@264 1557 _), _),
adamc@264 1558 _) =>
adamc@264 1559 let
adamc@264 1560 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 1561 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 1562 in
adamc@264 1563 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 1564 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@598 1565 strcat [sc "(",
adamc@598 1566 (L'.ERel 1, loc),
adamc@598 1567 sc " ",
adamc@598 1568 (L'.ERel 0, loc),
adamc@598 1569 sc ")"]), loc)), loc),
adamc@264 1570 fm)
adamc@264 1571 end
adamc@264 1572 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@559 1573 | L.ECApp ((L.EFfi ("Basis", "sql_neg"), _), _) =>
adamc@559 1574 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1575 (L'.EPrim (Prim.String "-"), loc)), loc), fm)
adamc@264 1576
adamc@264 1577 | L.ECApp (
adamc@264 1578 (L.ECApp (
adamc@264 1579 (L.ECApp (
adamc@264 1580 (L.ECApp (
adamc@264 1581 (L.ECApp (
adamc@254 1582 (L.ECApp (
adamc@254 1583 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 1584 _), _),
adamc@254 1585 _), _),
adamc@254 1586 _), _),
adamc@254 1587 _), _),
adamc@254 1588 _), _),
adamc@254 1589 _) =>
adamc@254 1590 let
adamc@254 1591 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 1592 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 1593 in
adamc@254 1594 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 1595 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 1596 (L'.EAbs ("e2", s, s,
adamc@598 1597 strcat [sc "(",
adamc@598 1598 (L'.ERel 1, loc),
adamc@598 1599 sc " ",
adamc@598 1600 (L'.ERel 2, loc),
adamc@598 1601 sc " ",
adamc@598 1602 (L'.ERel 0, loc),
adamc@598 1603 sc ")"]), loc)), loc)), loc),
adamc@254 1604 fm)
adamc@254 1605 end
adamc@254 1606 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 1607 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 1608
adamc@254 1609 | L.ECApp (
adamc@254 1610 (L.ECApp (
adamc@254 1611 (L.ECApp (
adamc@254 1612 (L.ECApp (
adamc@253 1613 (L.ECApp (
adamc@253 1614 (L.ECApp (
adamc@253 1615 (L.ECApp (
adamc@253 1616 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 1617 _), _),
adamc@253 1618 _), _),
adamc@253 1619 _), _),
adamc@253 1620 _), _),
adamc@253 1621 _), _),
adamc@253 1622 (L.CName tab, _)), _),
adamc@311 1623 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
adamc@260 1624
adamc@260 1625 | L.ECApp (
adamc@260 1626 (L.ECApp (
adamc@260 1627 (L.ECApp (
adamc@260 1628 (L.ECApp (
adamc@261 1629 (L.ECApp (
adamc@261 1630 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 1631 _), _),
adamc@261 1632 _), _),
adamc@261 1633 _), _),
adamc@261 1634 _), _),
adamc@261 1635 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 1636
adamc@261 1637 | L.ECApp (
adamc@261 1638 (L.ECApp (
adamc@261 1639 (L.ECApp (
adamc@261 1640 (L.ECApp (
adamc@260 1641 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 1642 _), _),
adamc@260 1643 _), _),
adamc@260 1644 _), _),
adamc@260 1645 _) =>
adamc@260 1646 let
adamc@260 1647 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 1648 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 1649 in
adamc@260 1650 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 1651 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 1652 (L'.EAbs ("e2", s, s,
adamc@598 1653 strcat [sc "((",
adamc@598 1654 (L'.ERel 1, loc),
adamc@598 1655 sc ") ",
adamc@598 1656 (L'.ERel 2, loc),
adamc@598 1657 sc " (",
adamc@598 1658 (L'.ERel 0, loc),
adamc@598 1659 sc "))"]), loc)), loc)), loc),
adamc@260 1660 fm)
adamc@260 1661 end
adamc@260 1662
adamc@260 1663 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 1664 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 1665 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 1666
adamc@265 1667 | L.ECApp (
adamc@265 1668 (L.ECApp (
adamc@265 1669 (L.ECApp (
adamc@265 1670 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 1671 _), _),
adamc@265 1672 _), _),
adamc@544 1673 _) => ((L'.EPrim (Prim.String "COUNT(*)"), loc),
adamc@265 1674 fm)
adamc@266 1675
adamc@266 1676 | L.ECApp (
adamc@266 1677 (L.ECApp (
adamc@266 1678 (L.ECApp (
adamc@266 1679 (L.ECApp (
adamc@266 1680 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 1681 _), _),
adamc@266 1682 _), _),
adamc@266 1683 _), _),
adamc@266 1684 _) =>
adamc@266 1685 let
adamc@266 1686 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 1687 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 1688 in
adamc@266 1689 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 1690 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@598 1691 strcat [(L'.ERel 1, loc),
adamc@598 1692 sc "(",
adamc@598 1693 (L'.ERel 0, loc),
adamc@598 1694 sc ")"]), loc)), loc),
adamc@266 1695 fm)
adamc@266 1696 end
adamc@266 1697
adamc@266 1698 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1699 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1700 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 1701 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1702 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 1703 fm)
adamc@266 1704 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 1705 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1706 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 1707 fm)
adamc@266 1708
adamc@559 1709 | L.EFfi ("Basis", "sql_arith_int") => ((L'.ERecord [], loc), fm)
adamc@559 1710 | L.EFfi ("Basis", "sql_arith_float") => ((L'.ERecord [], loc), fm)
adamc@559 1711
adamc@266 1712 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1713 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1714 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 1715 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 1716 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1717 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 1718 fm)
adamc@266 1719 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 1720 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1721 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 1722 fm)
adamc@266 1723
adamc@268 1724 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 1725 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 1726
adamc@441 1727 | L.ECApp (
adamc@441 1728 (L.ECApp (
adamc@441 1729 (L.ECApp (
adamc@441 1730 (L.ECApp (
adamc@441 1731 (L.EFfi ("Basis", "sql_nfunc"), _),
adamc@441 1732 _), _),
adamc@441 1733 _), _),
adamc@441 1734 _), _),
adamc@441 1735 _) =>
adamc@441 1736 let
adamc@441 1737 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@441 1738 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@441 1739 in
adamc@441 1740 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc),
adamc@441 1741 fm)
adamc@441 1742 end
adamc@441 1743 | L.EFfi ("Basis", "sql_current_timestamp") => ((L'.EPrim (Prim.String "CURRENT_TIMESTAMP"), loc), fm)
adamc@441 1744
adamc@470 1745 | (L.ECApp (
adamc@470 1746 (L.ECApp (
adamc@470 1747 (L.ECApp (
adamc@470 1748 (L.ECApp (
adamc@470 1749 (L.EFfi ("Basis", "sql_is_null"), _), _),
adamc@470 1750 _), _),
adamc@470 1751 _), _),
adamc@470 1752 _), _)) =>
adamc@470 1753 let
adamc@470 1754 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@470 1755 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@470 1756 in
adamc@470 1757 ((L'.EAbs ("s", s, s,
adamc@598 1758 strcat [sc "(",
adamc@598 1759 (L'.ERel 0, loc),
adamc@598 1760 sc " IS NULL)"]), loc),
adamc@470 1761 fm)
adamc@470 1762 end
adamc@470 1763
adamc@338 1764 | L.EFfiApp ("Basis", "nextval", [e]) =>
adamc@338 1765 let
adamc@338 1766 val (e, fm) = monoExp (env, st, fm) e
adamc@338 1767 in
adamc@465 1768 ((L'.ENextval e, loc), fm)
adamc@338 1769 end
adamc@338 1770
adamc@139 1771 | L.EApp (
adamc@139 1772 (L.ECApp (
adamc@141 1773 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 1774 _), _),
adamc@179 1775 se) =>
adamc@179 1776 let
adamc@179 1777 val (se, fm) = monoExp (env, st, fm) se
adamc@179 1778 in
adamc@179 1779 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 1780 end
adamc@179 1781
adamc@95 1782 | L.EApp (
adamc@95 1783 (L.EApp (
adamc@95 1784 (L.ECApp (
adamc@95 1785 (L.ECApp (
adamc@95 1786 (L.ECApp (
adamc@139 1787 (L.ECApp (
adamc@140 1788 (L.EFfi ("Basis", "join"),
adamc@139 1789 _), _), _),
adamc@139 1790 _), _),
adamc@95 1791 _), _),
adamc@95 1792 _), _),
adamc@95 1793 xml1), _),
adamc@179 1794 xml2) =>
adamc@179 1795 let
adamc@179 1796 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 1797 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 1798 in
adamc@179 1799 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 1800 end
adamc@95 1801
adamc@95 1802 | L.EApp (
adamc@95 1803 (L.EApp (
adamc@104 1804 (L.EApp (
adamc@95 1805 (L.ECApp (
adamc@104 1806 (L.ECApp (
adamc@104 1807 (L.ECApp (
adamc@104 1808 (L.ECApp (
adamc@139 1809 (L.ECApp (
adamc@139 1810 (L.ECApp (
adamc@139 1811 (L.ECApp (
adamc@139 1812 (L.ECApp (
adamc@139 1813 (L.EFfi ("Basis", "tag"),
adamc@139 1814 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 1815 attrs), _),
adamc@95 1816 tag), _),
adamc@95 1817 xml) =>
adamc@95 1818 let
adamc@140 1819 fun getTag' (e, _) =
adamc@140 1820 case e of
adamc@143 1821 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 1822 | L.ECApp (e, t) => let
adamc@143 1823 val (tag, ts) = getTag' e
adamc@143 1824 in
adamc@143 1825 (tag, ts @ [t])
adamc@143 1826 end
adamc@140 1827 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 1828 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1829 ("", []))
adamc@140 1830
adamc@95 1831 fun getTag (e, _) =
adamc@95 1832 case e of
adamc@143 1833 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 1834 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 1835 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 1836 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1837 ("", []))
adamc@95 1838
adamc@143 1839 val (tag, targs) = getTag tag
adamc@95 1840
adamc@179 1841 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@598 1842 val attrs = case #1 attrs of
adamc@598 1843 L'.ERecord xes => xes
adamc@598 1844 | _ => raise Fail "Non-record attributes!"
adamc@104 1845
adamc@668 1846 fun findOnload (attrs, acc) =
adamc@668 1847 case attrs of
adamc@668 1848 [] => (NONE, acc)
adamc@668 1849 | ("Onload", e, _) :: rest => (SOME e, List.revAppend (acc, rest))
adamc@668 1850 | x :: rest => findOnload (rest, x :: acc)
adamc@679 1851
adamc@668 1852 val (onload, attrs) = findOnload (attrs, [])
adamc@668 1853
adamc@606 1854 fun lowercaseFirst "" = ""
adamc@606 1855 | lowercaseFirst s = String.str (Char.toLower (String.sub (s, 0)))
adamc@606 1856 ^ String.extract (s, 1, NONE)
adamc@606 1857
adamc@143 1858 fun tagStart tag =
adamc@598 1859 let
adamc@598 1860 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@598 1861 in
adamc@598 1862 foldl (fn (("Action", _, _), acc) => acc
adamc@598 1863 | (("Source", _, _), acc) => acc
adamc@598 1864 | ((x, e, t), (s, fm)) =>
adamc@598 1865 case t of
adamc@598 1866 (L'.TFfi ("Basis", "bool"), _) =>
adamc@598 1867 let
adamc@598 1868 val s' = " " ^ lowercaseFirst x
adamc@598 1869 in
adamc@598 1870 ((L'.ECase (e,
adamc@598 1871 [((L'.PCon (L'.Enum,
adamc@598 1872 L'.PConFfi {mod = "Basis",
adamc@598 1873 datatyp = "bool",
adamc@598 1874 con = "True",
adamc@598 1875 arg = NONE},
adamc@598 1876 NONE), loc),
adamc@598 1877 (L'.EStrcat (s,
adamc@598 1878 (L'.EPrim (Prim.String s'), loc)), loc)),
adamc@598 1879 ((L'.PCon (L'.Enum,
adamc@598 1880 L'.PConFfi {mod = "Basis",
adamc@598 1881 datatyp = "bool",
adamc@598 1882 con = "False",
adamc@598 1883 arg = NONE},
adamc@598 1884 NONE), loc),
adamc@598 1885 s)],
adamc@598 1886 {disc = (L'.TFfi ("Basis", "bool"), loc),
adamc@598 1887 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@598 1888 fm)
adamc@598 1889 end
adamc@598 1890 | (L'.TFun _, _) =>
adamc@598 1891 let
adamc@598 1892 val s' = " " ^ lowercaseFirst x ^ "='"
adamc@651 1893 val e = (L'.EApp (e, (L'.ERecord [], loc)), loc)
adamc@598 1894 in
adamc@598 1895 ((L'.EStrcat (s,
adamc@598 1896 (L'.EStrcat (
adamc@598 1897 (L'.EPrim (Prim.String s'), loc),
adamc@598 1898 (L'.EStrcat (
adamc@598 1899 (L'.EJavaScript (L'.Attribute, e, NONE), loc),
adamc@598 1900 (L'.EPrim (Prim.String "'"), loc)), loc)),
adamc@598 1901 loc)), loc),
adamc@598 1902 fm)
adamc@598 1903 end
adamc@598 1904 | _ =>
adamc@598 1905 let
adamc@598 1906 val fooify =
adamc@598 1907 case x of
adamc@598 1908 "Href" => urlifyExp
adamc@598 1909 | "Link" => urlifyExp
adamc@598 1910 | _ => attrifyExp
adamc@120 1911
adamc@598 1912 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@179 1913
adamc@598 1914 val (e, fm) = fooify env fm (e, t)
adamc@598 1915 in
adamc@598 1916 ((L'.EStrcat (s,
adamc@598 1917 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@598 1918 (L'.EStrcat (e,
adamc@598 1919 (L'.EPrim (Prim.String "\""),
adamc@598 1920 loc)),
adamc@598 1921 loc)),
adamc@598 1922 loc)), loc),
adamc@598 1923 fm)
adamc@598 1924 end)
adamc@598 1925 (s, fm) attrs
adamc@598 1926 end
adamc@104 1927
adamc@143 1928 fun input typ =
adamc@143 1929 case targs of
adamc@155 1930 [_, (L.CName name, _)] =>
adamc@179 1931 let
adamc@179 1932 val (ts, fm) = tagStart "input"
adamc@179 1933 in
adamc@179 1934 ((L'.EStrcat (ts,
adamc@179 1935 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 1936 loc)), loc), fm)
adamc@179 1937 end
adamc@143 1938 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1939 raise Fail "No name passed to input tag")
adamc@104 1940
adamc@565 1941 fun normal (tag, extra, extraInner) =
adamc@143 1942 let
adamc@179 1943 val (tagStart, fm) = tagStart tag
adamc@152 1944 val tagStart = case extra of
adamc@152 1945 NONE => tagStart
adamc@152 1946 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 1947
adamc@143 1948 fun normal () =
adamc@179 1949 let
adamc@179 1950 val (xml, fm) = monoExp (env, st, fm) xml
adamc@565 1951 val xml = case extraInner of
adamc@679 1952 NONE => xml
adamc@679 1953 | SOME ei => (L'.EStrcat (ei, xml), loc)
adamc@179 1954 in
adamc@179 1955 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 1956 (L'.EStrcat (xml,
adamc@179 1957 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 1958 loc)), loc)),
adamc@179 1959 loc),
adamc@179 1960 fm)
adamc@179 1961 end
adamc@143 1962 in
adamc@143 1963 case xml of
adamc@143 1964 (L.EApp ((L.ECApp (
adamc@143 1965 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 1966 _), _),
adamc@143 1967 _), _),
adamc@143 1968 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 1969 if CharVector.all Char.isSpace s then
adamc@179 1970 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 1971 else
adamc@143 1972 normal ()
adamc@143 1973 | _ => normal ()
adamc@143 1974 end
adamc@606 1975
adamc@606 1976 fun setAttrs jexp =
adamc@606 1977 let
adamc@606 1978 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@606 1979
adamc@606 1980 val assgns = List.mapPartial
adamc@606 1981 (fn ("Source", _, _) => NONE
adamc@606 1982 | (x, e, _) =>
adamc@606 1983 SOME (strcat [str ("d." ^ lowercaseFirst x ^ "="),
adamc@606 1984 (L'.EJavaScript (L'.Script, e, NONE), loc),
adamc@606 1985 str ";"]))
adamc@606 1986 attrs
adamc@606 1987 in
adamc@606 1988 case assgns of
adamc@606 1989 [] => jexp
adamc@606 1990 | _ => strcat (str "var d="
adamc@606 1991 :: jexp
adamc@606 1992 :: str ";"
adamc@606 1993 :: assgns)
adamc@606 1994 end
adamc@152 1995 in
adamc@152 1996 case tag of
adamc@679 1997 "body" => let
adamc@668 1998 val onload = case onload of
adamc@668 1999 NONE => (L'.EPrim (Prim.String ""), loc)
adamc@668 2000 | SOME e =>
adamc@668 2001 let
adamc@668 2002 val e = (L'.EApp (e, (L'.ERecord [], loc)), loc)
adamc@668 2003 in
adamc@668 2004 (L'.EJavaScript (L'.Attribute, e, NONE), loc)
adamc@679 2005 end
adamc@668 2006 in
adamc@668 2007 normal ("body",
adamc@679 2008 SOME (L'.EFfiApp ("Basis", "get_settings", [onload]), loc),
adamc@668 2009 SOME (L'.EFfiApp ("Basis", "get_script", [(L'.ERecord [], loc)]), loc))
adamc@668 2010 end
adamc@568 2011
adamc@568 2012 | "dyn" =>
adamc@598 2013 (case attrs of
adamc@598 2014 [("Signal", (L'.EApp ((L'.EAbs (_, _, _, (L'.ESignalReturn (L'.ERel 0, _), _)), _),
adamc@598 2015 e), _), _)] => (e, fm)
adamc@598 2016 | [("Signal", e, _)] =>
adamc@568 2017 ((L'.EStrcat
adamc@603 2018 ((L'.EPrim (Prim.String "<span><script type=\"text/javascript\">dyn("), loc),
adamc@578 2019 (L'.EStrcat ((L'.EJavaScript (L'.Script, e, NONE), loc),
adamc@603 2020 (L'.EPrim (Prim.String ")</script></span>"), loc)), loc)), loc),
adamc@568 2021 fm)
adamc@568 2022 | _ => raise Fail "Monoize: Bad dyn attributes")
adamc@565 2023
adamc@565 2024 | "submit" => normal ("input type=\"submit\"", NONE, NONE)
adamc@601 2025 | "button" => normal ("input type=\"submit\"", NONE, NONE)
adamc@152 2026
adamc@152 2027 | "textbox" =>
adamc@152 2028 (case targs of
adamc@152 2029 [_, (L.CName name, _)] =>
adamc@598 2030 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@598 2031 NONE =>
adamc@598 2032 let
adamc@598 2033 val (ts, fm) = tagStart "input"
adamc@598 2034 in
adamc@598 2035 ((L'.EStrcat (ts,
adamc@598 2036 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@598 2037 loc)), loc), fm)
adamc@598 2038 end
adamc@598 2039 | SOME (_, src, _) =>
adamc@646 2040 (strcat [str "<span><script type=\"text/javascript\">inp(\"input\",",
adamc@598 2041 (L'.EJavaScript (L'.Script, src, NONE), loc),
adamc@646 2042 str ")</script></span>"],
adamc@598 2043 fm))
adamc@152 2044 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@598 2045 raise Fail "No name passed to textbox tag"))
adamc@155 2046 | "password" => input "password"
adamc@361 2047 | "textarea" =>
adamc@152 2048 (case targs of
adamc@152 2049 [_, (L.CName name, _)] =>
adamc@179 2050 let
adamc@179 2051 val (ts, fm) = tagStart "textarea"
adamc@179 2052 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2053 in
adamc@179 2054 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 2055 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 2056 (L'.EStrcat (xml,
adamc@179 2057 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 2058 loc)), loc)),
adamc@179 2059 loc), fm)
adamc@179 2060 end
adamc@152 2061 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2062 raise Fail "No name passed to ltextarea tag"))
adamc@153 2063
adamc@190 2064 | "checkbox" => input "checkbox"
adamc@190 2065
adamc@153 2066 | "radio" =>
adamc@153 2067 (case targs of
adamc@153 2068 [_, (L.CName name, _)] =>
adamc@179 2069 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 2070 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2071 raise Fail "No name passed to radio tag"))
adamc@153 2072 | "radioOption" =>
adamc@153 2073 (case St.radioGroup st of
adamc@153 2074 NONE => raise Fail "No name for radioGroup"
adamc@153 2075 | SOME name =>
adamc@153 2076 normal ("input",
adamc@565 2077 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc),
adamc@565 2078 NONE))
adamc@152 2079
adamc@361 2080 | "select" =>
adamc@154 2081 (case targs of
adamc@154 2082 [_, (L.CName name, _)] =>
adamc@179 2083 let
adamc@179 2084 val (ts, fm) = tagStart "select"
adamc@179 2085 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2086 in
adamc@179 2087 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@598 2088 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")),
adamc@598 2089 loc)), loc),
adamc@179 2090 (L'.EStrcat (xml,
adamc@179 2091 (L'.EPrim (Prim.String "</select>"),
adamc@179 2092 loc)), loc)),
adamc@179 2093 loc),
adamc@179 2094 fm)
adamc@179 2095 end
adamc@154 2096 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 2097 raise Fail "No name passed to lselect tag"))
adamc@154 2098
adamc@601 2099 | "ctextbox" =>
adamc@601 2100 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@601 2101 NONE =>
adamc@601 2102 let
adamc@601 2103 val (ts, fm) = tagStart "input"
adamc@601 2104 in
adamc@601 2105 ((L'.EStrcat (ts,
adamc@601 2106 (L'.EPrim (Prim.String "/>"), loc)),
adamc@601 2107 loc), fm)
adamc@601 2108 end
adamc@601 2109 | SOME (_, src, _) =>
adamc@606 2110 let
adamc@606 2111 val sc = strcat [str "inp(\"input\",",
adamc@606 2112 (L'.EJavaScript (L'.Script, src, NONE), loc),
adamc@606 2113 str ")"]
adamc@606 2114 val sc = setAttrs sc
adamc@606 2115 in
adamc@646 2116 (strcat [str "<span><script type=\"text/javascript\">",
adamc@606 2117 sc,
adamc@646 2118 str "</script></span>"],
adamc@606 2119 fm)
adamc@606 2120 end)
adamc@154 2121
adamc@565 2122 | "tabl" => normal ("table", NONE, NONE)
adamc@565 2123 | _ => normal (tag, NONE, NONE)
adamc@95 2124 end
adamc@94 2125
adamc@141 2126 | L.EApp ((L.ECApp (
adamc@361 2127 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@141 2128 _), _),
adamc@141 2129 xml) =>
adamc@143 2130 let
adamc@143 2131 fun findSubmit (e, _) =
adamc@143 2132 case e of
adamc@143 2133 L.EApp (
adamc@143 2134 (L.EApp (
adamc@143 2135 (L.ECApp (
adamc@143 2136 (L.ECApp (
adamc@143 2137 (L.ECApp (
adamc@143 2138 (L.ECApp (
adamc@143 2139 (L.EFfi ("Basis", "join"),
adamc@143 2140 _), _), _),
adamc@143 2141 _), _),
adamc@143 2142 _), _),
adamc@143 2143 _), _),
adamc@143 2144 xml1), _),
adamc@143 2145 xml2) => (case findSubmit xml1 of
adamc@143 2146 Error => Error
adamc@143 2147 | NotFound => findSubmit xml2
adamc@143 2148 | Found e =>
adamc@143 2149 case findSubmit xml2 of
adamc@143 2150 NotFound => Found e
adamc@143 2151 | _ => Error)
adamc@143 2152 | L.EApp (
adamc@143 2153 (L.EApp (
adamc@143 2154 (L.EApp (
adamc@143 2155 (L.ECApp (
adamc@143 2156 (L.ECApp (
adamc@143 2157 (L.ECApp (
adamc@143 2158 (L.ECApp (
adamc@143 2159 (L.ECApp (
adamc@143 2160 (L.ECApp (
adamc@143 2161 (L.ECApp (
adamc@143 2162 (L.ECApp (
adamc@143 2163 (L.EFfi ("Basis", "tag"),
adamc@143 2164 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 2165 attrs), _),
adamc@143 2166 _), _),
adamc@143 2167 xml) =>
adamc@143 2168 (case #1 attrs of
adamc@143 2169 L.ERecord xes =>
adamc@143 2170 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 2171 | _ => NONE) xes of
adamc@143 2172 NONE => findSubmit xml
adamc@143 2173 | SOME et =>
adamc@143 2174 case findSubmit xml of
adamc@143 2175 NotFound => Found et
adamc@143 2176 | _ => Error)
adamc@143 2177 | _ => findSubmit xml)
adamc@143 2178 | _ => NotFound
adamc@143 2179
adamc@598 2180 val (action, fm) = case findSubmit xml of
adamc@598 2181 NotFound => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@143 2182 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@598 2183 | Found (action, actionT) =>
adamc@598 2184 let
adamc@598 2185 val actionT = monoType env actionT
adamc@598 2186 val (action, fm) = monoExp (env, st, fm) action
adamc@598 2187 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@598 2188 in
adamc@598 2189 ((L'.EStrcat ((L'.EPrim (Prim.String " action=\""), loc),
adamc@598 2190 (L'.EStrcat (action,
adamc@598 2191 (L'.EPrim (Prim.String "\""), loc)), loc)), loc),
adamc@598 2192 fm)
adamc@598 2193 end
adamc@598 2194
adamc@179 2195 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 2196 in
adamc@598 2197 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form"), loc),
adamc@179 2198 (L'.EStrcat (action,
adamc@598 2199 (L'.EPrim (Prim.String ">"), loc)), loc)), loc),
adamc@179 2200 (L'.EStrcat (xml,
adamc@179 2201 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 2202 fm)
adamc@143 2203 end
adamc@141 2204
adamc@148 2205 | L.EApp ((L.ECApp (
adamc@148 2206 (L.ECApp (
adamc@148 2207 (L.ECApp (
adamc@148 2208 (L.ECApp (
adamc@148 2209 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 2210 _), _),
adamc@148 2211 _), _),
adamc@148 2212 _), _),
adamc@179 2213 xml) => monoExp (env, st, fm) xml
adamc@148 2214
adamc@283 2215 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 2216 let
adamc@283 2217 val t = monoType env t
adamc@283 2218 in
adamc@283 2219 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 2220 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 2221 fm)
adamc@283 2222 end
adamc@283 2223
adamc@179 2224 | L.EApp (e1, e2) =>
adamc@179 2225 let
adamc@179 2226 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 2227 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 2228 in
adamc@179 2229 ((L'.EApp (e1, e2), loc), fm)
adamc@179 2230 end
adamc@26 2231 | L.EAbs (x, dom, ran, e) =>
adamc@179 2232 let
adamc@179 2233 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 2234 in
adamc@179 2235 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 2236 end
adamc@25 2237 | L.ECApp _ => poly ()
adamc@25 2238 | L.ECAbs _ => poly ()
adamc@25 2239
adamc@252 2240 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 2241 | L.EFfiApp (m, x, es) =>
adamc@252 2242 let
adamc@252 2243 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 2244 in
adamc@252 2245 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 2246 end
adamc@252 2247
adamc@179 2248 | L.ERecord xes =>
adamc@179 2249 let
adamc@179 2250 val (xes, fm) = ListUtil.foldlMap
adamc@179 2251 (fn ((x, e, t), fm) =>
adamc@179 2252 let
adamc@179 2253 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2254 in
adamc@179 2255 ((monoName env x,
adamc@179 2256 e,
adamc@179 2257 monoType env t), fm)
adamc@179 2258 end) fm xes
adamc@179 2259 in
adamc@179 2260 ((L'.ERecord xes, loc), fm)
adamc@179 2261 end
adamc@179 2262 | L.EField (e, x, _) =>
adamc@179 2263 let
adamc@179 2264 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2265 in
adamc@179 2266 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 2267 end
adamc@445 2268 | L.EConcat _ => poly ()
adamc@149 2269 | L.ECut _ => poly ()
adamc@493 2270 | L.ECutMulti _ => poly ()
adamc@177 2271
adamc@182 2272 | L.ECase (e, pes, {disc, result}) =>
adamc@179 2273 let
adamc@179 2274 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2275 val (pes, fm) = ListUtil.foldlMap
adamc@179 2276 (fn ((p, e), fm) =>
adamc@179 2277 let
adamc@179 2278 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2279 in
adamc@182 2280 ((monoPat env p, e), fm)
adamc@179 2281 end) fm pes
adamc@179 2282 in
adamc@182 2283 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 2284 end
adamc@177 2285
adamc@179 2286 | L.EWrite e =>
adamc@179 2287 let
adamc@179 2288 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2289 in
adamc@252 2290 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 2291 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 2292 end
adamc@110 2293
adamc@179 2294 | L.EClosure (n, es) =>
adamc@179 2295 let
adamc@179 2296 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 2297 monoExp (env, st, fm) e)
adamc@179 2298 fm es
adamc@179 2299 in
adamc@179 2300 ((L'.EClosure (n, es), loc), fm)
adamc@179 2301 end
adamc@450 2302
adamc@450 2303 | L.ELet (x, t, e1, e2) =>
adamc@450 2304 let
adamc@450 2305 val t' = monoType env t
adamc@450 2306 val (e1, fm) = monoExp (env, st, fm) e1
adamc@450 2307 val (e2, fm) = monoExp (Env.pushERel env x t, st, fm) e2
adamc@450 2308 in
adamc@450 2309 ((L'.ELet (x, t', e1, e2), loc), fm)
adamc@450 2310 end
adamc@607 2311
adamc@609 2312 | L.EServerCall (n, es, ek, t) =>
adamc@608 2313 let
adamc@609 2314 val t = monoType env t
adamc@614 2315 val (_, ft, _, name) = Env.lookupENamed env n
adamc@608 2316 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@614 2317
adamc@614 2318 fun encodeArgs (es, ft, acc, fm) =
adamc@614 2319 case (es, ft) of
adamc@614 2320 ([], _) => (rev acc, fm)
adamc@614 2321 | (e :: es, (L.TFun (dom, ran), _)) =>
adamc@614 2322 let
adamc@614 2323 val (e, fm) = urlifyExp env fm (e, monoType env dom)
adamc@614 2324 in
adamc@614 2325 encodeArgs (es, ran, e
adamc@614 2326 :: (L'.EPrim (Prim.String "/"), loc)
adamc@614 2327 :: acc, fm)
adamc@614 2328 end
adamc@614 2329 | _ => raise Fail "Monoize: Not enough arguments visible in RPC function type"
adamc@614 2330
adamc@614 2331 val (call, fm) = encodeArgs (es, ft, [], fm)
adamc@614 2332 val call = foldl (fn (e, call) => (L'.EStrcat (call, e), loc))
adamc@614 2333 (L'.EPrim (Prim.String name), loc) call
adamc@614 2334
adamc@608 2335 val (ek, fm) = monoExp (env, st, fm) ek
adamc@609 2336
adamc@609 2337 val ekf = (L'.EAbs ("f",
adamc@609 2338 (L'.TFun (t,
adamc@609 2339 (L'.TFun ((L'.TRecord [], loc),
adamc@609 2340 (L'.TRecord [], loc)), loc)), loc),
adamc@609 2341 (L'.TFun (t,
adamc@609 2342 (L'.TRecord [], loc)), loc),
adamc@609 2343 (L'.EAbs ("x",
adamc@609 2344 t,
adamc@609 2345 (L'.TRecord [], loc),
adamc@609 2346 (L'.EApp ((L'.EApp ((L'.ERel 1, loc),
adamc@609 2347 (L'.ERel 0, loc)), loc),
adamc@609 2348 (L'.ERecord [], loc)), loc)), loc)), loc)
adamc@609 2349 val ek = (L'.EApp (ekf, ek), loc)
adamc@651 2350 val e = (L'.EServerCall (call, ek, t), loc)
adamc@651 2351 val e = liftExpInExp 0 e
adamc@651 2352 val unit = (L'.TRecord [], loc)
adamc@651 2353 val e = (L'.EAbs ("_", unit, unit, e), loc)
adamc@608 2354 in
adamc@651 2355 (e, fm)
adamc@608 2356 end
adamc@626 2357
adamc@626 2358 | L.EKAbs _ => poly ()
adamc@626 2359 | L.EKApp _ => poly ()
adamc@25 2360 end
adamc@25 2361
adamc@179 2362 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 2363 let
adamc@25 2364 fun poly () =
adamc@25 2365 (E.errorAt loc "Unsupported declaration";
adamc@25 2366 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 2367 NONE)
adamc@25 2368 in
adamc@25 2369 case d of
adamc@25 2370 L.DCon _ => NONE
adamc@193 2371 | L.DDatatype (x, n, [], xncs) =>
adamc@193 2372 let
adamc@196 2373 val env' = Env.declBinds env all
adamc@196 2374 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 2375 in
adamc@273 2376 SOME (env', fm, [d])
adamc@193 2377 end
adamc@193 2378 | L.DDatatype _ => poly ()
adamc@179 2379 | L.DVal (x, n, t, e, s) =>
adamc@179 2380 let
adamc@179 2381 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 2382 in
adamc@179 2383 SOME (Env.pushENamed env x n t NONE s,
adamc@179 2384 fm,
adamc@273 2385 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 2386 end
adamc@128 2387 | L.DValRec vis =>
adamc@128 2388 let
adamc@128 2389 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 2390
adamc@179 2391 val (vis, fm) = ListUtil.foldlMap
adamc@179 2392 (fn ((x, n, t, e, s), fm) =>
adamc@179 2393 let
adamc@179 2394 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 2395 in
adamc@179 2396 ((x, n, monoType env t, e, s), fm)
adamc@179 2397 end)
adamc@179 2398 fm vis
adamc@128 2399 in
adamc@128 2400 SOME (env,
adamc@179 2401 fm,
adamc@273 2402 [(L'.DValRec vis, loc)])
adamc@128 2403 end
adamc@144 2404 | L.DExport (ek, n) =>
adamc@115 2405 let
adamc@120 2406 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 2407
adamc@609 2408 fun unwind (t, args) =
adamc@609 2409 case #1 t of
adamc@609 2410 L.TFun (dom, ran) => unwind (ran, dom :: args)
adamc@314 2411 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@609 2412 unwind (t, (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: args)
adamc@609 2413 | _ => (rev args, t)
adamc@120 2414
adamc@609 2415 val (ts, ran) = unwind (t, [])
adamc@609 2416 val ts = map (monoType env) ts
adamc@609 2417 val ran = monoType env ran
adamc@115 2418 in
adamc@609 2419 SOME (env, fm, [(L'.DExport (ek, s, n, ts, ran), loc)])
adamc@115 2420 end
adamc@273 2421 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
adamc@251 2422 let
adamc@251 2423 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 2424 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 2425 val s = "uw_" ^ s
adamc@251 2426 val e = (L'.EPrim (Prim.String s), loc)
adamc@273 2427
adamc@273 2428 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@251 2429 in
adamc@251 2430 SOME (Env.pushENamed env x n t NONE s,
adamc@251 2431 fm,
adamc@273 2432 [(L'.DTable (s, xts), loc),
adamc@273 2433 (L'.DVal (x, n, t', e, s), loc)])
adamc@251 2434 end
adamc@273 2435 | L.DTable _ => poly ()
adamc@338 2436 | L.DSequence (x, n, s) =>
adamc@338 2437 let
adamc@338 2438 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 2439 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 2440 val s = "uw_" ^ s
adamc@338 2441 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 2442 in
adamc@338 2443 SOME (Env.pushENamed env x n t NONE s,
adamc@338 2444 fm,
adamc@338 2445 [(L'.DSequence s, loc),
adamc@338 2446 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 2447 end
adamc@683 2448 | L.DDatabase _ => NONE
adamc@462 2449 | L.DCookie (x, n, t, s) =>
adamc@462 2450 let
adamc@462 2451 val t = (L.CFfi ("Basis", "string"), loc)
adamc@462 2452 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@462 2453 val e = (L'.EPrim (Prim.String s), loc)
adamc@462 2454 in
adamc@462 2455 SOME (Env.pushENamed env x n t NONE s,
adamc@462 2456 fm,
adamc@462 2457 [(L'.DVal (x, n, t', e, s), loc)])
adamc@462 2458 end
adamc@25 2459 end
adamc@25 2460
adamc@683 2461 datatype expungable = Client | Channel
adamc@683 2462
adamc@683 2463 fun monoize env file =
adamc@25 2464 let
adamc@385 2465 val p = !urlPrefix
adamc@385 2466 val () =
adamc@385 2467 if p = "" then
adamc@385 2468 urlPrefix := "/"
adamc@385 2469 else if String.sub (p, size p - 1) <> #"/" then
adamc@385 2470 urlPrefix := p ^ "/"
adamc@385 2471 else
adamc@385 2472 ()
adamc@385 2473
adamc@683 2474 val loc = E.dummySpan
adamc@683 2475 val client = (L'.TFfi ("Basis", "client"), loc)
adamc@683 2476 val unit = (L'.TRecord [], loc)
adamc@683 2477 fun expunger () =
adamc@683 2478 let
adamc@683 2479 val target = (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)
adamc@683 2480
adamc@683 2481 fun doTable (tab, xts, e) =
adamc@683 2482 case xts of
adamc@683 2483 L.CRecord (_, xts) =>
adamc@683 2484 let
adamc@683 2485 val (nullable, notNullable) =
adamc@683 2486 foldl (fn ((x, t), st as (nullable, notNullable)) =>
adamc@683 2487 case #1 x of
adamc@683 2488 L.CName x =>
adamc@683 2489 (case #1 t of
adamc@683 2490 L.CFfi ("Basis", "client") =>
adamc@683 2491 (nullable, (x, Client) :: notNullable)
adamc@683 2492 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@683 2493 (L.CFfi ("Basis", "client"), _)) =>
adamc@683 2494 ((x, Client) :: nullable, notNullable)
adamc@683 2495 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@683 2496 (nullable, (x, Channel) :: notNullable)
adamc@683 2497 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@683 2498 (L.CApp ((L.CFfi ("Basis", "channel"), _), _), _)) =>
adamc@683 2499 ((x, Channel) :: nullable, notNullable)
adamc@683 2500 | _ => st)
adamc@683 2501 | _ => st) ([], []) xts
adamc@683 2502
adamc@683 2503 val e =
adamc@683 2504 case notNullable of
adamc@683 2505 [] => e
adamc@683 2506 | eb :: ebs =>
adamc@683 2507 let
adamc@683 2508 fun cond (x, v) =
adamc@683 2509 (L'.EStrcat ((L'.EPrim (Prim.String ("uw_" ^ x
adamc@683 2510 ^ (case v of
adamc@683 2511 Client => ""
adamc@683 2512 | Channel => " >> 32")
adamc@683 2513 ^ " = ")), loc),
adamc@683 2514 target), loc)
adamc@683 2515 in
adamc@683 2516 (L'.ESeq (
adamc@683 2517 (L'.EDml (foldl
adamc@683 2518 (fn (eb, s) =>
adamc@683 2519 (L'.EStrcat (s,
adamc@683 2520 (L'.EStrcat ((L'.EPrim (Prim.String " AND "),
adamc@683 2521 loc),
adamc@683 2522 cond eb), loc)), loc))
adamc@683 2523 (L'.EStrcat ((L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@683 2524 ^ tab
adamc@683 2525 ^ " WHERE ")), loc),
adamc@683 2526 cond eb), loc)
adamc@683 2527 ebs), loc),
adamc@683 2528 e), loc)
adamc@683 2529 end
adamc@683 2530 in
adamc@683 2531 e
adamc@683 2532 end
adamc@683 2533 | _ => e
adamc@683 2534
adamc@683 2535 val e = (L'.ERecord [], loc)
adamc@683 2536 in
adamc@683 2537 foldl (fn ((d, _), e) =>
adamc@683 2538 case d of
adamc@683 2539 L.DTable (_, _, xts, tab) => doTable (tab, #1 xts, e)
adamc@683 2540 | _ => e) e file
adamc@683 2541 end
adamc@683 2542
adamc@179 2543 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@683 2544 case #1 d of
adamc@683 2545 L.DDatabase s =>
adamc@683 2546 let
adamc@683 2547 val (n, fm) = Fm.freshName fm
adamc@683 2548
adamc@683 2549
adamc@683 2550 val d = L'.DVal ("expunger",
adamc@683 2551 n,
adamc@683 2552 (L'.TFun (client, unit), loc),
adamc@683 2553 (L'.EAbs ("cli", client, unit, expunger ()), loc),
adamc@683 2554 "expunger")
adamc@683 2555 in
adamc@683 2556 (env, Fm.enter fm, (L'.DDatabase (s, n), loc)
adamc@683 2557 :: (d, loc)
adamc@683 2558 :: ds)
adamc@683 2559 end
adamc@683 2560 | _ =>
adamc@683 2561 case monoDecl (env, fm) d of
adamc@683 2562 NONE => (env, fm, ds)
adamc@683 2563 | SOME (env, fm, ds') =>
adamc@683 2564 (env,
adamc@683 2565 Fm.enter fm,
adamc@683 2566 ds' @ Fm.decls fm @ ds))
adamc@683 2567 (env, Fm.empty (CoreUtil.File.maxName file + 1), []) file
adamc@25 2568 in
adamc@25 2569 rev ds
adamc@25 2570 end
adamc@25 2571
adamc@25 2572 end