annotate src/monoize.sml @ 629:e68de2a5506b

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