annotate src/monoize.sml @ 734:f2a2be93331c

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