annotate src/monoize.sml @ 707:d8217b4cb617

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