annotate src/monoize.sml @ 1173:983d9b38abc7

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