annotate src/monoize.sml @ 818:066493f7f008

Change List.mapM' to avoid leaving functions around
author Adam Chlipala <adamc@hcoop.net>
date Thu, 21 May 2009 11:45:04 -0400
parents 4585f744574a
children 91f465ded07e
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@815 1176 [(L'.EJavaScript (L'.Source t, (L'.ERel 1, loc)), 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@815 1192 (L'.ERel 1, loc)), 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@815 2413 (L'.EJavaScript (L'.Attribute, e), 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@815 2503 (L'.EJavaScript (L'.Script, e), loc),
adamc@800 2504 str ")"])
adamc@606 2505 | (x, e, _) =>
adamc@606 2506 SOME (strcat [str ("d." ^ lowercaseFirst x ^ "="),
adamc@815 2507 (L'.EJavaScript (L'.Script, e), 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@815 2527 (L'.EJavaScript (L'.Attribute, e), 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@818 2541 [("Signal", e, _)] =>
adamc@568 2542 ((L'.EStrcat
adamc@603 2543 ((L'.EPrim (Prim.String "<span><script type=\"text/javascript\">dyn("), loc),
adamc@815 2544 (L'.EStrcat ((L'.EJavaScript (L'.Script, e), loc),
adamc@603 2545 (L'.EPrim (Prim.String ")</script></span>"), loc)), loc)), loc),
adamc@568 2546 fm)
adamc@568 2547 | _ => raise Fail "Monoize: Bad dyn attributes")
adamc@565 2548
adamc@565 2549 | "submit" => normal ("input type=\"submit\"", NONE, NONE)
adamc@601 2550 | "button" => normal ("input type=\"submit\"", NONE, NONE)
adamc@761 2551 | "hidden" => input "hidden"
adamc@152 2552
adamc@152 2553 | "textbox" =>
adamc@152 2554 (case targs of
adamc@152 2555 [_, (L.CName name, _)] =>
adamc@598 2556 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@598 2557 NONE =>
adamc@598 2558 let
adamc@598 2559 val (ts, fm) = tagStart "input"
adamc@598 2560 in
adamc@598 2561 ((L'.EStrcat (ts,
adamc@802 2562 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\" />")),
adamc@598 2563 loc)), loc), fm)
adamc@598 2564 end
adamc@598 2565 | SOME (_, src, _) =>
adamc@816 2566 (strcat [str "<span><script type=\"text/javascript\">inp(",
adamc@815 2567 (L'.EJavaScript (L'.Script, src), loc),
adamc@816 2568 str ")</script></span>"],
adamc@598 2569 fm))
adamc@152 2570 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@598 2571 raise Fail "No name passed to textbox tag"))
adamc@155 2572 | "password" => input "password"
adamc@361 2573 | "textarea" =>
adamc@152 2574 (case targs of
adamc@152 2575 [_, (L.CName name, _)] =>
adamc@179 2576 let
adamc@179 2577 val (ts, fm) = tagStart "textarea"
adamc@179 2578 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2579 in
adamc@179 2580 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 2581 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 2582 (L'.EStrcat (xml,
adamc@179 2583 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 2584 loc)), loc)),
adamc@179 2585 loc), fm)
adamc@179 2586 end
adamc@152 2587 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2588 raise Fail "No name passed to ltextarea tag"))
adamc@153 2589
adamc@190 2590 | "checkbox" => input "checkbox"
adamc@737 2591 | "upload" => input "file"
adamc@190 2592
adamc@153 2593 | "radio" =>
adamc@153 2594 (case targs of
adamc@153 2595 [_, (L.CName name, _)] =>
adamc@179 2596 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 2597 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2598 raise Fail "No name passed to radio tag"))
adamc@153 2599 | "radioOption" =>
adamc@153 2600 (case St.radioGroup st of
adamc@153 2601 NONE => raise Fail "No name for radioGroup"
adamc@153 2602 | SOME name =>
adamc@153 2603 normal ("input",
adamc@565 2604 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc),
adamc@565 2605 NONE))
adamc@152 2606
adamc@361 2607 | "select" =>
adamc@154 2608 (case targs of
adamc@154 2609 [_, (L.CName name, _)] =>
adamc@179 2610 let
adamc@179 2611 val (ts, fm) = tagStart "select"
adamc@179 2612 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2613 in
adamc@179 2614 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@598 2615 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")),
adamc@598 2616 loc)), loc),
adamc@179 2617 (L'.EStrcat (xml,
adamc@179 2618 (L'.EPrim (Prim.String "</select>"),
adamc@179 2619 loc)), loc)),
adamc@179 2620 loc),
adamc@179 2621 fm)
adamc@179 2622 end
adamc@154 2623 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 2624 raise Fail "No name passed to lselect tag"))
adamc@154 2625
adamc@601 2626 | "ctextbox" =>
adamc@601 2627 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@601 2628 NONE =>
adamc@601 2629 let
adamc@601 2630 val (ts, fm) = tagStart "input"
adamc@601 2631 in
adamc@601 2632 ((L'.EStrcat (ts,
adamc@802 2633 (L'.EPrim (Prim.String " />"), loc)),
adamc@601 2634 loc), fm)
adamc@601 2635 end
adamc@601 2636 | SOME (_, src, _) =>
adamc@606 2637 let
adamc@816 2638 val sc = strcat [str "inp(",
adamc@815 2639 (L'.EJavaScript (L'.Script, src), loc),
adamc@816 2640 str ")"]
adamc@797 2641 val sc = setAttrs sc
adamc@797 2642 in
adamc@797 2643 (strcat [str "<span><script type=\"text/javascript\">",
adamc@797 2644 sc,
adamc@797 2645 str "</script></span>"],
adamc@797 2646 fm)
adamc@797 2647 end)
adamc@797 2648
adamc@817 2649 | "ccheckbox" =>
adamc@817 2650 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@817 2651 NONE =>
adamc@817 2652 let
adamc@817 2653 val (ts, fm) = tagStart "input type=\"checkbox\""
adamc@817 2654 in
adamc@817 2655 ((L'.EStrcat (ts,
adamc@817 2656 (L'.EPrim (Prim.String " />"), loc)),
adamc@817 2657 loc), fm)
adamc@817 2658 end
adamc@817 2659 | SOME (_, src, _) =>
adamc@817 2660 let
adamc@817 2661 val sc = strcat [str "chk(",
adamc@817 2662 (L'.EJavaScript (L'.Script, src), loc),
adamc@817 2663 str ")"]
adamc@817 2664 val sc = setAttrs sc
adamc@817 2665 in
adamc@817 2666 (strcat [str "<span><script type=\"text/javascript\">",
adamc@817 2667 sc,
adamc@817 2668 str "</script></span>"],
adamc@817 2669 fm)
adamc@817 2670 end)
adamc@817 2671
adamc@797 2672 | "cselect" =>
adamc@797 2673 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@797 2674 NONE =>
adamc@797 2675 let
adamc@797 2676 val (ts, fm) = tagStart "select"
adamc@797 2677 in
adamc@797 2678 ((L'.EStrcat (ts,
adamc@802 2679 (L'.EPrim (Prim.String " />"), loc)),
adamc@797 2680 loc), fm)
adamc@797 2681 end
adamc@797 2682 | SOME (_, src, _) =>
adamc@797 2683 let
adamc@797 2684 val (xml, fm) = monoExp (env, st, fm) xml
adamc@797 2685
adamc@816 2686 val sc = strcat [str "sel(",
adamc@815 2687 (L'.EJavaScript (L'.Script, src), loc),
adamc@797 2688 str ",",
adamc@815 2689 (L'.EJavaScript (L'.Script, xml), loc),
adamc@606 2690 str ")"]
adamc@606 2691 val sc = setAttrs sc
adamc@606 2692 in
adamc@646 2693 (strcat [str "<span><script type=\"text/javascript\">",
adamc@606 2694 sc,
adamc@646 2695 str "</script></span>"],
adamc@606 2696 fm)
adamc@606 2697 end)
adamc@154 2698
adamc@797 2699 | "coption" => normal ("option", NONE, NONE)
adamc@797 2700
adamc@565 2701 | "tabl" => normal ("table", NONE, NONE)
adamc@565 2702 | _ => normal (tag, NONE, NONE)
adamc@95 2703 end
adamc@94 2704
adamc@141 2705 | L.EApp ((L.ECApp (
adamc@361 2706 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@734 2707 (L.CRecord (_, fields), _)), _),
adamc@141 2708 xml) =>
adamc@143 2709 let
adamc@143 2710 fun findSubmit (e, _) =
adamc@143 2711 case e of
adamc@143 2712 L.EApp (
adamc@143 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.EFfi ("Basis", "join"),
adamc@143 2719 _), _), _),
adamc@143 2720 _), _),
adamc@143 2721 _), _),
adamc@143 2722 _), _),
adamc@143 2723 xml1), _),
adamc@143 2724 xml2) => (case findSubmit xml1 of
adamc@143 2725 Error => Error
adamc@143 2726 | NotFound => findSubmit xml2
adamc@143 2727 | Found e =>
adamc@143 2728 case findSubmit xml2 of
adamc@143 2729 NotFound => Found e
adamc@143 2730 | _ => Error)
adamc@143 2731 | L.EApp (
adamc@143 2732 (L.EApp (
adamc@143 2733 (L.EApp (
adamc@730 2734 (L.EApp (
adamc@143 2735 (L.ECApp (
adamc@143 2736 (L.ECApp (
adamc@143 2737 (L.ECApp (
adamc@143 2738 (L.ECApp (
adamc@143 2739 (L.ECApp (
adamc@143 2740 (L.ECApp (
adamc@143 2741 (L.ECApp (
adamc@730 2742 (L.ECApp (
adamc@730 2743 (L.EFfi ("Basis", "tag"),
adamc@730 2744 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@730 2745 _), _),
adamc@143 2746 attrs), _),
adamc@143 2747 _), _),
adamc@143 2748 xml) =>
adamc@143 2749 (case #1 attrs of
adamc@143 2750 L.ERecord xes =>
adamc@143 2751 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 2752 | _ => NONE) xes of
adamc@143 2753 NONE => findSubmit xml
adamc@143 2754 | SOME et =>
adamc@143 2755 case findSubmit xml of
adamc@143 2756 NotFound => Found et
adamc@143 2757 | _ => Error)
adamc@143 2758 | _ => findSubmit xml)
adamc@143 2759 | _ => NotFound
adamc@143 2760
adamc@735 2761 val (func, action, fm) = case findSubmit xml of
adamc@735 2762 NotFound => (0, (L'.EPrim (Prim.String ""), loc), fm)
adamc@143 2763 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@598 2764 | Found (action, actionT) =>
adamc@598 2765 let
adamc@735 2766 val func = case #1 action of
adamc@735 2767 L.EClosure (n, _) => n
adamc@735 2768 | _ => raise Fail "Monoize: Action is not a closure"
adamc@598 2769 val actionT = monoType env actionT
adamc@598 2770 val (action, fm) = monoExp (env, st, fm) action
adamc@598 2771 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@598 2772 in
adamc@735 2773 (func,
adamc@735 2774 (L'.EStrcat ((L'.EPrim (Prim.String " action=\""), loc),
adamc@598 2775 (L'.EStrcat (action,
adamc@598 2776 (L'.EPrim (Prim.String "\""), loc)), loc)), loc),
adamc@598 2777 fm)
adamc@598 2778 end
adamc@734 2779
adamc@737 2780 val hasUpload = CoreUtil.Exp.exists {kind = fn _ => false,
adamc@737 2781 con = fn _ => false,
adamc@737 2782 exp = fn e =>
adamc@737 2783 case e of
adamc@737 2784 L.EFfi ("Basis", "upload") => true
adamc@737 2785 | _ => false} xml
adamc@737 2786
adamc@179 2787 val (xml, fm) = monoExp (env, st, fm) xml
adamc@735 2788
adamc@735 2789 val xml =
adamc@735 2790 if IS.member (!readCookie, func) then
adamc@735 2791 let
adamc@735 2792 fun inFields s = List.exists (fn ((L.CName s', _), _) => s' = s
adamc@735 2793 | _ => true) fields
adamc@735 2794
adamc@735 2795 fun getSigName () =
adamc@735 2796 let
adamc@735 2797 fun getSigName' n =
adamc@735 2798 let
adamc@735 2799 val s = "Sig" ^ Int.toString n
adamc@735 2800 in
adamc@735 2801 if inFields s then
adamc@735 2802 getSigName' (n + 1)
adamc@735 2803 else
adamc@735 2804 s
adamc@735 2805 end
adamc@735 2806 in
adamc@735 2807 if inFields "Sig" then
adamc@735 2808 getSigName' 0
adamc@735 2809 else
adamc@735 2810 "Sig"
adamc@735 2811 end
adamc@735 2812
adamc@735 2813 val sigName = getSigName ()
adamc@735 2814 val sigSet = (L'.EFfiApp ("Basis", "sigString", [(L'.ERecord [], loc)]), loc)
adamc@735 2815 val sigSet = (L'.EStrcat ((L'.EPrim (Prim.String ("<input type=\"hidden\" name=\""
adamc@735 2816 ^ sigName
adamc@735 2817 ^ "\" value=\"")), loc),
adamc@735 2818 sigSet), loc)
adamc@735 2819 val sigSet = (L'.EStrcat (sigSet,
adamc@803 2820 (L'.EPrim (Prim.String "\" />"), loc)), loc)
adamc@735 2821 in
adamc@735 2822 (L'.EStrcat (sigSet, xml), loc)
adamc@735 2823 end
adamc@735 2824 else
adamc@735 2825 xml
adamc@737 2826
adamc@737 2827 val action = if hasUpload then
adamc@737 2828 (L'.EStrcat (action,
adamc@737 2829 (L'.EPrim (Prim.String " enctype=\"multipart/form-data\""), loc)), loc)
adamc@737 2830 else
adamc@737 2831 action
adamc@737 2832
adamc@143 2833 in
adamc@730 2834 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form method=\"post\""), loc),
adamc@179 2835 (L'.EStrcat (action,
adamc@598 2836 (L'.EPrim (Prim.String ">"), loc)), loc)), loc),
adamc@179 2837 (L'.EStrcat (xml,
adamc@179 2838 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 2839 fm)
adamc@143 2840 end
adamc@141 2841
adamc@756 2842 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp (
adamc@756 2843 (L.EFfi ("Basis", "subform"), _), _), _), _),
adamc@756 2844 _), _), _), (L.CName nm, loc)) =>
adamc@756 2845 let
adamc@756 2846 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@756 2847 in
adamc@756 2848 ((L'.EAbs ("xml", s, s,
adamc@756 2849 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".b\" value=\""
adamc@803 2850 ^ nm ^ "\" />")), loc),
adamc@756 2851 (L'.ERel 0, loc),
adamc@803 2852 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\" />")), loc)]),
adamc@756 2853 loc),
adamc@756 2854 fm)
adamc@756 2855 end
adamc@756 2856
adamc@758 2857 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp (
adamc@758 2858 (L.EFfi ("Basis", "subforms"), _), _), _), _),
adamc@758 2859 _), _), _), (L.CName nm, loc)) =>
adamc@758 2860 let
adamc@758 2861 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@758 2862 in
adamc@758 2863 ((L'.EAbs ("xml", s, s,
adamc@758 2864 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".s\" value=\""
adamc@803 2865 ^ nm ^ "\" />")), loc),
adamc@758 2866 (L'.ERel 0, loc),
adamc@803 2867 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\" />")), loc)]),
adamc@758 2868 loc),
adamc@758 2869 fm)
adamc@758 2870 end
adamc@758 2871
adamc@758 2872 | L.ECApp ((L.ECApp (
adamc@758 2873 (L.EFfi ("Basis", "entry"), _), _), _), _) =>
adamc@758 2874 let
adamc@758 2875 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@758 2876 in
adamc@758 2877 ((L'.EAbs ("xml", s, s,
adamc@803 2878 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".i\" value=\"1\" />")), loc),
adamc@758 2879 (L'.ERel 0, loc),
adamc@803 2880 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\" />")), loc)]),
adamc@758 2881 loc),
adamc@758 2882 fm)
adamc@758 2883 end
adamc@758 2884
adamc@148 2885 | L.EApp ((L.ECApp (
adamc@148 2886 (L.ECApp (
adamc@148 2887 (L.ECApp (
adamc@148 2888 (L.ECApp (
adamc@148 2889 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 2890 _), _),
adamc@148 2891 _), _),
adamc@148 2892 _), _),
adamc@179 2893 xml) => monoExp (env, st, fm) xml
adamc@148 2894
adamc@283 2895 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 2896 let
adamc@283 2897 val t = monoType env t
adamc@283 2898 in
adamc@283 2899 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 2900 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 2901 fm)
adamc@283 2902 end
adamc@741 2903 | L.ECApp ((L.EFfi ("Basis", "returnBlob"), _), t) =>
adamc@741 2904 let
adamc@741 2905 val t = monoType env t
adamc@741 2906 val un = (L'.TRecord [], loc)
adamc@741 2907 in
adamc@741 2908 ((L'.EAbs ("b", (L'.TFfi ("Basis", "blob"), loc),
adamc@741 2909 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFun (un, t), loc)), loc),
adamc@741 2910 (L'.EAbs ("mt", (L'.TFfi ("Basis", "string"), loc), (L'.TFun (un, t), loc),
adamc@741 2911 (L'.EAbs ("_", un, t,
adamc@741 2912 (L'.EReturnBlob {blob = (L'.ERel 2, loc),
adamc@741 2913 mimeType = (L'.ERel 1, loc),
adamc@741 2914 t = t}, loc)), loc)), loc)), loc),
adamc@741 2915 fm)
adamc@741 2916 end
adamc@283 2917
adamc@179 2918 | L.EApp (e1, e2) =>
adamc@179 2919 let
adamc@179 2920 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 2921 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 2922 in
adamc@179 2923 ((L'.EApp (e1, e2), loc), fm)
adamc@179 2924 end
adamc@26 2925 | L.EAbs (x, dom, ran, e) =>
adamc@179 2926 let
adamc@179 2927 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 2928 in
adamc@179 2929 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 2930 end
adamc@25 2931 | L.ECApp _ => poly ()
adamc@25 2932 | L.ECAbs _ => poly ()
adamc@25 2933
adamc@252 2934 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 2935 | L.EFfiApp (m, x, es) =>
adamc@252 2936 let
adamc@252 2937 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 2938 in
adamc@252 2939 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 2940 end
adamc@252 2941
adamc@179 2942 | L.ERecord xes =>
adamc@179 2943 let
adamc@179 2944 val (xes, fm) = ListUtil.foldlMap
adamc@179 2945 (fn ((x, e, t), fm) =>
adamc@179 2946 let
adamc@179 2947 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2948 in
adamc@179 2949 ((monoName env x,
adamc@179 2950 e,
adamc@179 2951 monoType env t), fm)
adamc@179 2952 end) fm xes
adamc@179 2953 in
adamc@179 2954 ((L'.ERecord xes, loc), fm)
adamc@179 2955 end
adamc@179 2956 | L.EField (e, x, _) =>
adamc@179 2957 let
adamc@179 2958 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2959 in
adamc@179 2960 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 2961 end
adamc@445 2962 | L.EConcat _ => poly ()
adamc@149 2963 | L.ECut _ => poly ()
adamc@493 2964 | L.ECutMulti _ => poly ()
adamc@177 2965
adamc@182 2966 | L.ECase (e, pes, {disc, result}) =>
adamc@179 2967 let
adamc@179 2968 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2969 val (pes, fm) = ListUtil.foldlMap
adamc@179 2970 (fn ((p, e), fm) =>
adamc@179 2971 let
adamc@179 2972 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2973 in
adamc@182 2974 ((monoPat env p, e), fm)
adamc@179 2975 end) fm pes
adamc@179 2976 in
adamc@182 2977 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 2978 end
adamc@177 2979
adamc@179 2980 | L.EWrite e =>
adamc@179 2981 let
adamc@179 2982 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2983 in
adamc@252 2984 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 2985 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 2986 end
adamc@110 2987
adamc@179 2988 | L.EClosure (n, es) =>
adamc@179 2989 let
adamc@179 2990 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 2991 monoExp (env, st, fm) e)
adamc@179 2992 fm es
adamc@179 2993 in
adamc@179 2994 ((L'.EClosure (n, es), loc), fm)
adamc@179 2995 end
adamc@450 2996
adamc@450 2997 | L.ELet (x, t, e1, e2) =>
adamc@450 2998 let
adamc@450 2999 val t' = monoType env t
adamc@450 3000 val (e1, fm) = monoExp (env, st, fm) e1
adamc@450 3001 val (e2, fm) = monoExp (Env.pushERel env x t, st, fm) e2
adamc@450 3002 in
adamc@450 3003 ((L'.ELet (x, t', e1, e2), loc), fm)
adamc@450 3004 end
adamc@607 3005
adamc@609 3006 | L.EServerCall (n, es, ek, t) =>
adamc@608 3007 let
adamc@609 3008 val t = monoType env t
adamc@614 3009 val (_, ft, _, name) = Env.lookupENamed env n
adamc@608 3010 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@614 3011
adamc@614 3012 fun encodeArgs (es, ft, acc, fm) =
adamc@614 3013 case (es, ft) of
adamc@614 3014 ([], _) => (rev acc, fm)
adamc@614 3015 | (e :: es, (L.TFun (dom, ran), _)) =>
adamc@614 3016 let
adamc@614 3017 val (e, fm) = urlifyExp env fm (e, monoType env dom)
adamc@614 3018 in
adamc@614 3019 encodeArgs (es, ran, e
adamc@614 3020 :: (L'.EPrim (Prim.String "/"), loc)
adamc@614 3021 :: acc, fm)
adamc@614 3022 end
adamc@614 3023 | _ => raise Fail "Monoize: Not enough arguments visible in RPC function type"
adamc@614 3024
adamc@614 3025 val (call, fm) = encodeArgs (es, ft, [], fm)
adamc@614 3026 val call = foldl (fn (e, call) => (L'.EStrcat (call, e), loc))
adamc@614 3027 (L'.EPrim (Prim.String name), loc) call
adamc@614 3028
adamc@608 3029 val (ek, fm) = monoExp (env, st, fm) ek
adamc@609 3030
adamc@609 3031 val ekf = (L'.EAbs ("f",
adamc@609 3032 (L'.TFun (t,
adamc@609 3033 (L'.TFun ((L'.TRecord [], loc),
adamc@609 3034 (L'.TRecord [], loc)), loc)), loc),
adamc@609 3035 (L'.TFun (t,
adamc@609 3036 (L'.TRecord [], loc)), loc),
adamc@609 3037 (L'.EAbs ("x",
adamc@609 3038 t,
adamc@609 3039 (L'.TRecord [], loc),
adamc@609 3040 (L'.EApp ((L'.EApp ((L'.ERel 1, loc),
adamc@609 3041 (L'.ERel 0, loc)), loc),
adamc@609 3042 (L'.ERecord [], loc)), loc)), loc)), loc)
adamc@609 3043 val ek = (L'.EApp (ekf, ek), loc)
adamc@736 3044 val eff = if IS.member (!readCookie, n) then
adamc@736 3045 L'.ReadCookieWrite
adamc@736 3046 else
adamc@736 3047 L'.ReadOnly
adamc@736 3048 val e = (L'.EServerCall (call, ek, t, eff), loc)
adamc@651 3049 val e = liftExpInExp 0 e
adamc@651 3050 val unit = (L'.TRecord [], loc)
adamc@651 3051 val e = (L'.EAbs ("_", unit, unit, e), loc)
adamc@608 3052 in
adamc@651 3053 (e, fm)
adamc@608 3054 end
adamc@626 3055
adamc@626 3056 | L.EKAbs _ => poly ()
adamc@626 3057 | L.EKApp _ => poly ()
adamc@25 3058 end
adamc@25 3059
adamc@179 3060 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 3061 let
adamc@25 3062 fun poly () =
adamc@25 3063 (E.errorAt loc "Unsupported declaration";
adamc@25 3064 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 3065 NONE)
adamc@25 3066 in
adamc@25 3067 case d of
adamc@25 3068 L.DCon _ => NONE
adamc@808 3069 | L.DDatatype [("list", n, [_], [("Nil", _, NONE),
adamc@808 3070 ("Cons", _, SOME (L.TRecord (L.CRecord (_,
adamc@808 3071 [((L.CName "1", _),
adamc@808 3072 (L.CRel 0, _)),
adamc@808 3073 ((L.CName "2", _),
adamc@808 3074 (L.CApp ((L.CNamed n', _),
adamc@808 3075 (L.CRel 0, _)),
adamc@808 3076 _))]), _), _))])] =>
adamc@757 3077 if n = n' then
adamc@757 3078 NONE
adamc@757 3079 else
adamc@757 3080 poly ()
adamc@808 3081 | L.DDatatype dts =>
adamc@808 3082 let
adamc@808 3083 val env' = Env.declBinds env all
adamc@808 3084 val dts = map (fn (x, n, [], xncs) =>
adamc@808 3085 (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs)
adamc@808 3086 | _ => (E.errorAt loc "Polymorphic datatype needed too late";
adamc@808 3087 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@808 3088 ("", 0, []))) dts
adamc@808 3089 val d = (L'.DDatatype dts, loc)
adamc@808 3090 in
adamc@808 3091 SOME (env', fm, [d])
adamc@808 3092 end
adamc@179 3093 | L.DVal (x, n, t, e, s) =>
adamc@179 3094 let
adamc@179 3095 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 3096 in
adamc@179 3097 SOME (Env.pushENamed env x n t NONE s,
adamc@179 3098 fm,
adamc@273 3099 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 3100 end
adamc@128 3101 | L.DValRec vis =>
adamc@128 3102 let
adamc@128 3103 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 3104
adamc@179 3105 val (vis, fm) = ListUtil.foldlMap
adamc@179 3106 (fn ((x, n, t, e, s), fm) =>
adamc@179 3107 let
adamc@179 3108 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 3109 in
adamc@179 3110 ((x, n, monoType env t, e, s), fm)
adamc@179 3111 end)
adamc@179 3112 fm vis
adamc@128 3113 in
adamc@128 3114 SOME (env,
adamc@179 3115 fm,
adamc@273 3116 [(L'.DValRec vis, loc)])
adamc@128 3117 end
adamc@144 3118 | L.DExport (ek, n) =>
adamc@115 3119 let
adamc@120 3120 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 3121
adamc@609 3122 fun unwind (t, args) =
adamc@609 3123 case #1 t of
adamc@609 3124 L.TFun (dom, ran) => unwind (ran, dom :: args)
adamc@314 3125 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@609 3126 unwind (t, (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: args)
adamc@609 3127 | _ => (rev args, t)
adamc@120 3128
adamc@609 3129 val (ts, ran) = unwind (t, [])
adamc@609 3130 val ts = map (monoType env) ts
adamc@609 3131 val ran = monoType env ran
adamc@115 3132 in
adamc@609 3133 SOME (env, fm, [(L'.DExport (ek, s, n, ts, ran), loc)])
adamc@115 3134 end
adamc@707 3135 | L.DTable (x, n, (L.CRecord (_, xts), _), s, pe, _, ce, _) =>
adamc@251 3136 let
adamc@251 3137 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 3138 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 3139 val s = "uw_" ^ s
adamc@704 3140 val e_name = (L'.EPrim (Prim.String s), loc)
adamc@273 3141
adamc@273 3142 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@704 3143
adamc@707 3144 val (pe, fm) = monoExp (env, St.empty, fm) pe
adamc@707 3145 val (ce, fm) = monoExp (env, St.empty, fm) ce
adamc@251 3146 in
adamc@251 3147 SOME (Env.pushENamed env x n t NONE s,
adamc@251 3148 fm,
adamc@707 3149 [(L'.DTable (s, xts, pe, ce), loc),
adamc@704 3150 (L'.DVal (x, n, t', e_name, s), loc)])
adamc@251 3151 end
adamc@273 3152 | L.DTable _ => poly ()
adamc@754 3153 | L.DView (x, n, s, e, (L.CRecord (_, xts), _)) =>
adamc@754 3154 let
adamc@754 3155 val t = (L.CFfi ("Basis", "string"), loc)
adamc@754 3156 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@754 3157 val s = "uw_" ^ s
adamc@754 3158 val e_name = (L'.EPrim (Prim.String s), loc)
adamc@754 3159
adamc@754 3160 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@754 3161
adamc@754 3162 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@754 3163 val e = (L'.EFfiApp ("Basis", "viewify", [e]), loc)
adamc@754 3164 in
adamc@754 3165 SOME (Env.pushENamed env x n t NONE s,
adamc@754 3166 fm,
adamc@754 3167 [(L'.DView (s, xts, e), loc),
adamc@754 3168 (L'.DVal (x, n, t', e_name, s), loc)])
adamc@754 3169 end
adamc@754 3170 | L.DView _ => poly ()
adamc@338 3171 | L.DSequence (x, n, s) =>
adamc@338 3172 let
adamc@338 3173 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 3174 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 3175 val s = "uw_" ^ s
adamc@338 3176 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 3177 in
adamc@338 3178 SOME (Env.pushENamed env x n t NONE s,
adamc@338 3179 fm,
adamc@338 3180 [(L'.DSequence s, loc),
adamc@338 3181 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 3182 end
adamc@683 3183 | L.DDatabase _ => NONE
adamc@462 3184 | L.DCookie (x, n, t, s) =>
adamc@462 3185 let
adamc@462 3186 val t = (L.CFfi ("Basis", "string"), loc)
adamc@462 3187 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@462 3188 val e = (L'.EPrim (Prim.String s), loc)
adamc@462 3189 in
adamc@462 3190 SOME (Env.pushENamed env x n t NONE s,
adamc@462 3191 fm,
adamc@725 3192 [(L'.DCookie s, loc),
adamc@725 3193 (L'.DVal (x, n, t', e, s), loc)])
adamc@462 3194 end
adamc@720 3195 | L.DStyle (x, n, s) =>
adamc@718 3196 let
adamc@718 3197 val t = (L.CFfi ("Basis", "string"), loc)
adamc@718 3198 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@718 3199 val e = (L'.EPrim (Prim.String s), loc)
adamc@718 3200 in
adamc@718 3201 SOME (Env.pushENamed env x n t NONE s,
adamc@718 3202 fm,
adamc@720 3203 [(L'.DStyle s, loc),
adamc@718 3204 (L'.DVal (x, n, t', e, s), loc)])
adamc@718 3205 end
adamc@25 3206 end
adamc@25 3207
adamc@683 3208 datatype expungable = Client | Channel
adamc@683 3209
adamc@683 3210 fun monoize env file =
adamc@25 3211 let
adamc@735 3212 (* Calculate which exported functions need cookie signature protection *)
adamc@735 3213 val rcook = foldl (fn ((d, _), rcook) =>
adamc@735 3214 case d of
adamc@735 3215 L.DExport (L.Action L.ReadCookieWrite, n) => IS.add (rcook, n)
adamc@735 3216 | L.DExport (L.Rpc L.ReadCookieWrite, n) => IS.add (rcook, n)
adamc@735 3217 | _ => rcook)
adamc@735 3218 IS.empty file
adamc@735 3219 val () = readCookie := rcook
adamc@735 3220
adamc@683 3221 val loc = E.dummySpan
adamc@683 3222 val client = (L'.TFfi ("Basis", "client"), loc)
adamc@683 3223 val unit = (L'.TRecord [], loc)
adamc@687 3224
adamc@687 3225 fun calcClientish xts =
adamc@687 3226 foldl (fn ((x : L.con, t : L.con), st as (nullable, notNullable)) =>
adamc@687 3227 case #1 x of
adamc@687 3228 L.CName x =>
adamc@687 3229 (case #1 t of
adamc@687 3230 L.CFfi ("Basis", "client") =>
adamc@687 3231 (nullable, (x, Client) :: notNullable)
adamc@687 3232 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@687 3233 (L.CFfi ("Basis", "client"), _)) =>
adamc@687 3234 ((x, Client) :: nullable, notNullable)
adamc@687 3235 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@687 3236 (nullable, (x, Channel) :: notNullable)
adamc@687 3237 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@687 3238 (L.CApp ((L.CFfi ("Basis", "channel"), _), _), _)) =>
adamc@687 3239 ((x, Channel) :: nullable, notNullable)
adamc@687 3240 | _ => st)
adamc@687 3241 | _ => st) ([], []) xts
adamc@687 3242
adamc@683 3243 fun expunger () =
adamc@683 3244 let
adamc@683 3245 val target = (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)
adamc@683 3246
adamc@683 3247 fun doTable (tab, xts, e) =
adamc@683 3248 case xts of
adamc@683 3249 L.CRecord (_, xts) =>
adamc@683 3250 let
adamc@687 3251 val (nullable, notNullable) = calcClientish xts
adamc@683 3252
adamc@684 3253 fun cond (x, v) =
adamc@684 3254 (L'.EStrcat ((L'.EPrim (Prim.String ("uw_" ^ x
adamc@684 3255 ^ (case v of
adamc@684 3256 Client => ""
adamc@684 3257 | Channel => " >> 32")
adamc@684 3258 ^ " = ")), loc),
adamc@684 3259 target), loc)
adamc@684 3260
adamc@684 3261 val e =
adamc@684 3262 foldl (fn ((x, v), e) =>
adamc@684 3263 (L'.ESeq (
adamc@684 3264 (L'.EDml (L'.EStrcat (
adamc@684 3265 (L'.EPrim (Prim.String ("UPDATE uw_"
adamc@684 3266 ^ tab
adamc@684 3267 ^ " SET uw_"
adamc@684 3268 ^ x
adamc@684 3269 ^ " = NULL WHERE ")), loc),
adamc@684 3270 cond (x, v)), loc), loc),
adamc@684 3271 e), loc))
adamc@684 3272 e nullable
adamc@684 3273
adamc@683 3274 val e =
adamc@683 3275 case notNullable of
adamc@683 3276 [] => e
adamc@683 3277 | eb :: ebs =>
adamc@684 3278 (L'.ESeq (
adamc@684 3279 (L'.EDml (foldl
adamc@684 3280 (fn (eb, s) =>
adamc@684 3281 (L'.EStrcat (s,
adamc@687 3282 (L'.EStrcat ((L'.EPrim (Prim.String " OR "),
adamc@684 3283 loc),
adamc@684 3284 cond eb), loc)), loc))
adamc@684 3285 (L'.EStrcat ((L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@684 3286 ^ tab
adamc@684 3287 ^ " WHERE ")), loc),
adamc@684 3288 cond eb), loc)
adamc@684 3289 ebs), loc),
adamc@684 3290 e), loc)
adamc@683 3291 in
adamc@683 3292 e
adamc@683 3293 end
adamc@683 3294 | _ => e
adamc@683 3295
adamc@683 3296 val e = (L'.ERecord [], loc)
adamc@683 3297 in
adamc@683 3298 foldl (fn ((d, _), e) =>
adamc@683 3299 case d of
adamc@707 3300 L.DTable (_, _, xts, tab, _, _, _, _) => doTable (tab, #1 xts, e)
adamc@683 3301 | _ => e) e file
adamc@683 3302 end
adamc@683 3303
adamc@687 3304 fun initializer () =
adamc@687 3305 let
adamc@687 3306 fun doTable (tab, xts, e) =
adamc@687 3307 case xts of
adamc@687 3308 L.CRecord (_, xts) =>
adamc@687 3309 let
adamc@687 3310 val (nullable, notNullable) = calcClientish xts
adamc@687 3311
adamc@687 3312 val e =
adamc@687 3313 case nullable of
adamc@687 3314 [] => e
adamc@687 3315 | (x, _) :: ebs =>
adamc@687 3316 (L'.ESeq (
adamc@687 3317 (L'.EDml (L'.EPrim (Prim.String
adamc@687 3318 (foldl (fn ((x, _), s) =>
adamc@687 3319 s ^ ", uw_" ^ x ^ " = NULL")
adamc@687 3320 ("UPDATE uw_"
adamc@687 3321 ^ tab
adamc@687 3322 ^ " SET uw_"
adamc@687 3323 ^ x
adamc@687 3324 ^ " = NULL")
adamc@687 3325 ebs)), loc), loc),
adamc@687 3326 e), loc)
adamc@687 3327
adamc@687 3328 val e =
adamc@687 3329 case notNullable of
adamc@687 3330 [] => e
adamc@687 3331 | eb :: ebs =>
adamc@687 3332 (L'.ESeq (
adamc@687 3333 (L'.EDml (L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@687 3334 ^ tab)), loc), loc),
adamc@687 3335 e), loc)
adamc@687 3336 in
adamc@687 3337 e
adamc@687 3338 end
adamc@687 3339 | _ => e
adamc@687 3340
adamc@687 3341 val e = (L'.ERecord [], loc)
adamc@687 3342 in
adamc@687 3343 foldl (fn ((d, _), e) =>
adamc@687 3344 case d of
adamc@707 3345 L.DTable (_, _, xts, tab, _, _, _, _) => doTable (tab, #1 xts, e)
adamc@687 3346 | _ => e) e file
adamc@687 3347 end
adamc@687 3348
adamc@179 3349 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@683 3350 case #1 d of
adamc@683 3351 L.DDatabase s =>
adamc@683 3352 let
adamc@687 3353 val (nExp, fm) = Fm.freshName fm
adamc@687 3354 val (nIni, fm) = Fm.freshName fm
adamc@687 3355
adamc@687 3356 val dExp = L'.DVal ("expunger",
adamc@687 3357 nExp,
adamc@687 3358 (L'.TFun (client, unit), loc),
adamc@687 3359 (L'.EAbs ("cli", client, unit, expunger ()), loc),
adamc@687 3360 "expunger")
adamc@687 3361 val dIni = L'.DVal ("initializer",
adamc@687 3362 nIni,
adamc@687 3363 (L'.TFun (unit, unit), loc),
adamc@687 3364 (L'.EAbs ("_", unit, unit, initializer ()), loc),
adamc@687 3365 "initializer")
adamc@683 3366 in
adamc@687 3367 (env, Fm.enter fm, (L'.DDatabase {name = s,
adamc@687 3368 expunge = nExp,
adamc@687 3369 initialize = nIni}, loc)
adamc@687 3370 :: (dExp, loc)
adamc@687 3371 :: (dIni, loc)
adamc@683 3372 :: ds)
adamc@683 3373 end
adamc@683 3374 | _ =>
adamc@683 3375 case monoDecl (env, fm) d of
adamc@683 3376 NONE => (env, fm, ds)
adamc@683 3377 | SOME (env, fm, ds') =>
adamc@683 3378 (env,
adamc@683 3379 Fm.enter fm,
adamc@683 3380 ds' @ Fm.decls fm @ ds))
adamc@683 3381 (env, Fm.empty (CoreUtil.File.maxName file + 1), []) file
adamc@25 3382 in
adamc@25 3383 rev ds
adamc@25 3384 end
adamc@25 3385
adamc@25 3386 end