annotate src/monoize.sml @ 1890:abb0e95dcaa1

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