annotate src/monoize.sml @ 670:f73913d97a40

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