annotate src/monoize.sml @ 2226:e10881cd92da

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