annotate src/monoize.sml @ 697:755a71c99be5

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