annotate src/monoize.sml @ 613:c5991cdb0c4b

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