annotate src/monoize.sml @ 2253:d665925acff8

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