annotate src/monoize.sml @ 802:ef6de4075dc1

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