annotate src/monoize.sml @ 820:91f465ded07e

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