annotate src/monoize.sml @ 1777:59b07fdae1ff

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