annotate src/monoize.sml @ 676:e0c186464612

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