annotate src/monoize.sml @ 1734:d2b3fada532e

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