annotate src/monoize.sml @ 740:b302b6e35f93

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