annotate src/monoize.sml @ 2252:e843a04499d4

Revert to revision 2222.
author Ziv Scully <ziv@mit.edu>
date Mon, 21 Sep 2015 10:16:55 -0400
parents 25874084bf1f
children d665925acff8
rev   line source
adam@2008 1 (* Copyright (c) 2008-2014, Adam Chlipala
adamc@25 2 * All rights reserved.
adamc@25 3 *
adamc@25 4 * Redistribution and use in source and binary forms, with or without
adamc@25 5 * modification, are permitted provided that the following conditions are met:
adamc@25 6 *
adamc@25 7 * - Redistributions of source code must retain the above copyright notice,
adamc@25 8 * this list of conditions and the following disclaimer.
adamc@25 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@25 10 * this list of conditions and the following disclaimer in the documentation
adamc@25 11 * and/or other materials provided with the distribution.
adamc@25 12 * - The names of contributors may not be used to endorse or promote products
adamc@25 13 * derived from this software without specific prior written permission.
adamc@25 14 *
adamc@25 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@25 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@25 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@25 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adam@1682 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@25 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@25 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@25 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@25 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@25 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@25 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@25 26 *)
adamc@25 27
adamc@25 28 structure Monoize :> MONOIZE = struct
adamc@25 29
adamc@25 30 structure E = ErrorMsg
adamc@25 31 structure Env = CoreEnv
adamc@25 32
adamc@25 33 structure L = Core
adamc@25 34 structure L' = Mono
adamc@25 35
adamc@196 36 structure IM = IntBinaryMap
adamc@735 37 structure IS = IntBinarySet
adamc@196 38
adam@1287 39 structure SK = struct
adam@1287 40 type ord_key = string
adam@1287 41 val compare = String.compare
adam@1287 42 end
adam@1287 43
adam@1287 44 structure SS = BinarySetFn(SK)
adam@1287 45 structure SM = BinaryMapFn(SK)
adam@1287 46
adam@1287 47 structure RM = BinaryMapFn(struct
adam@1287 48 type ord_key = (string * L'.typ) list
adam@1287 49 fun compare (r1, r2) = MonoUtil.Typ.compare ((L'.TRecord r1, E.dummySpan),
adam@1287 50 (L'.TRecord r2, E.dummySpan))
adamc@984 51 end)
adamc@984 52
adam@1287 53 val nextPvar = ref 0
adam@1287 54 val pvars = ref (RM.empty : (int * (string * int * L'.typ) list) RM.map)
adam@1713 55 val pvarDefs = ref ([] : (string * int * (string * int * L'.typ option) list) list)
adam@1288 56 val pvarOldDefs = ref ([] : (int * (string * int * L.con option) list) list)
adam@1287 57
adam@1287 58 fun choosePvar () =
adam@1287 59 let
adam@1287 60 val n = !nextPvar
adam@1287 61 in
adam@1287 62 nextPvar := n + 1;
adam@1287 63 n
adam@1287 64 end
adam@1287 65
adam@1288 66 fun pvar (r, r', loc) =
adam@1288 67 case RM.find (!pvars, r') of
adam@1287 68 NONE =>
adam@1287 69 let
adam@1287 70 val n = choosePvar ()
adam@1288 71 val fs = map (fn (x, t) => (x, choosePvar (), t)) r'
adam@1734 72 val r = ListMergeSort.sort (fn (((L.CName x, _), _), ((L.CName y, _), _)) => String.compare (x, y) = GREATER
adam@1734 73 | _ => raise Fail "Monoize: pvar, not CName") r
adam@1288 74 val (r, fs') = ListPair.foldr (fn ((_, t), (x, n, _), (r, fs')) =>
adam@1288 75 ((x, n, SOME t) :: r,
adam@1288 76 SM.insert (fs', x, n))) ([], SM.empty) (r, fs)
adam@1287 77 in
adam@1288 78 pvars := RM.insert (!pvars, r', (n, fs));
adam@1713 79 pvarDefs := ("$poly" ^ Int.toString n, n, map (fn (x, n, t) => (x, n, SOME t)) fs)
adam@1287 80 :: !pvarDefs;
adam@1288 81 pvarOldDefs := (n, r) :: !pvarOldDefs;
adam@1287 82 (n, fs)
adam@1287 83 end
adam@1287 84 | SOME v => v
adam@1287 85
adamc@984 86 val singletons = SS.addList (SS.empty,
adamc@984 87 ["link",
adamc@984 88 "br",
adamc@984 89 "p",
adamc@984 90 "hr",
adamc@984 91 "input",
adam@1841 92 "img",
adam@1841 93 "base",
adam@1841 94 "meta",
adam@1841 95 "param",
adam@1841 96 "area",
adam@1841 97 "col"])
adamc@984 98
adamc@196 99 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
adamc@25 100
adamc@252 101 structure U = MonoUtil
adamc@252 102
adamc@252 103 val liftExpInExp =
adamc@252 104 U.Exp.mapB {typ = fn t => t,
adamc@252 105 exp = fn bound => fn e =>
adamc@252 106 case e of
adamc@252 107 L'.ERel xn =>
adamc@252 108 if xn < bound then
adamc@252 109 e
adamc@252 110 else
adamc@252 111 L'.ERel (xn + 1)
adamc@252 112 | _ => e,
adamc@252 113 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@252 114 | (bound, _) => bound}
adamc@252 115
adamc@25 116 fun monoName env (all as (c, loc)) =
adamc@25 117 let
adamc@25 118 fun poly () =
adamc@25 119 (E.errorAt loc "Unsupported name constructor";
adamc@25 120 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 121 "")
adamc@25 122 in
adamc@25 123 case c of
adamc@25 124 L.CName s => s
adamc@25 125 | _ => poly ()
adamc@25 126 end
adamc@25 127
adamc@877 128 fun lowercaseFirst "" = ""
adamc@877 129 | lowercaseFirst s = String.str (Char.toLower (String.sub (s, 0)))
adamc@877 130 ^ String.extract (s, 1, NONE)
adamc@877 131
adamc@877 132 fun monoNameLc env c = lowercaseFirst (monoName env c)
adamc@877 133
adamc@292 134 fun readType' (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 135 (L'.TOption t, loc)), loc)
adamc@292 136 fun readErrType (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 137 t), loc)
adamc@292 138 fun readType (t, loc) =
adamc@292 139 (L'.TRecord [("Read", readType' (t, loc)),
adamc@292 140 ("ReadError", readErrType (t, loc))],
adamc@292 141 loc)
adamc@292 142
adamc@196 143 fun monoType env =
adamc@25 144 let
adamc@196 145 fun mt env dtmap (all as (c, loc)) =
adamc@196 146 let
adamc@196 147 fun poly () =
adamc@196 148 (E.errorAt loc "Unsupported type constructor";
adamc@196 149 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 150 dummyTyp)
adamc@196 151 in
adamc@196 152 case c of
adamc@196 153 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 154 | L.TCFun _ => poly ()
adamc@196 155 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@905 156 let
adamc@905 157 val xcs = map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs
adamc@905 158 val xcs = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) xcs
adamc@905 159 in
adamc@905 160 (L'.TRecord xcs, loc)
adamc@905 161 end
adamc@196 162 | L.TRecord _ => poly ()
adamc@196 163
adamc@288 164 | L.CApp ((L.CFfi ("Basis", "option"), _), t) =>
adamc@288 165 (L'.TOption (mt env dtmap t), loc)
adamc@757 166 | L.CApp ((L.CFfi ("Basis", "list"), _), t) =>
adamc@757 167 (L'.TList (mt env dtmap t), loc)
adamc@288 168
adam@1287 169 | L.CApp ((L.CFfi ("Basis", "variant"), _), (L.CRecord ((L.KType, _), xts), _)) =>
adam@1287 170 let
adam@1288 171 val xts' = map (fn (x, t) => (monoName env x, mt env dtmap t)) xts
adam@1288 172 val xts' = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) xts'
adam@1288 173 val (n, cs) = pvar (xts, xts', loc)
adam@1287 174 val cs = map (fn (x, n, t) => (x, n, SOME t)) cs
adam@1287 175 in
adam@1287 176 (L'.TDatatype (n, ref (ElabUtil.classifyDatatype cs, cs)), loc)
adam@1287 177 end
adam@1287 178
adamc@820 179 | L.CApp ((L.CFfi ("Basis", "monad"), _), _) =>
adamc@820 180 (L'.TRecord [], loc)
adamc@820 181
adamc@387 182 | L.CApp ((L.CFfi ("Basis", "eq"), _), t) =>
adamc@387 183 let
adamc@387 184 val t = mt env dtmap t
adamc@387 185 in
adamc@387 186 (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)
adamc@387 187 end
adamc@389 188 | L.CApp ((L.CFfi ("Basis", "num"), _), t) =>
adamc@389 189 let
adamc@389 190 val t = mt env dtmap t
adamc@389 191 in
adamc@417 192 (L'.TRecord [("Zero", t),
adamc@417 193 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 194 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 195 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 196 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 197 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
mad@1831 198 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adam@1832 199 ("Pow", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))],
adamc@389 200 loc)
adamc@389 201 end
adamc@391 202 | L.CApp ((L.CFfi ("Basis", "ord"), _), t) =>
adamc@391 203 let
adamc@391 204 val t = mt env dtmap t
adamc@391 205 in
adamc@391 206 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 207 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 208 loc)
adamc@391 209 end
adamc@286 210 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
adamc@286 211 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@290 212 | L.CApp ((L.CFfi ("Basis", "read"), _), t) =>
adamc@292 213 readType (mt env dtmap t, loc)
adamc@286 214
adamc@1176 215 | L.CFfi ("Basis", "unit") => (L'.TRecord [], loc)
adamc@1176 216 | L.CFfi ("Basis", "page") => (L'.TFfi ("Basis", "string"), loc)
adam@1921 217 | L.CFfi ("Basis", "xhead") => (L'.TFfi ("Basis", "string"), loc)
adamc@1176 218 | L.CFfi ("Basis", "xbody") => (L'.TFfi ("Basis", "string"), loc)
kkallio@1475 219 | L.CFfi ("Basis", "xtable") => (L'.TFfi ("Basis", "string"), loc)
adamc@1176 220 | L.CFfi ("Basis", "xtr") => (L'.TFfi ("Basis", "string"), loc)
adamc@1176 221 | L.CFfi ("Basis", "xform") => (L'.TFfi ("Basis", "string"), loc)
adamc@1176 222
adamc@717 223 | L.CFfi ("Basis", "url") => (L'.TFfi ("Basis", "string"), loc)
adamc@741 224 | L.CFfi ("Basis", "mimeType") => (L'.TFfi ("Basis", "string"), loc)
adamc@720 225 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 226 (L'.TFfi ("Basis", "string"), loc)
adamc@196 227 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 228 (L'.TFfi ("Basis", "string"), loc)
adamc@721 229 | L.CFfi ("Basis", "css_class") => (L'.TFfi ("Basis", "string"), loc)
adam@1750 230 | L.CFfi ("Basis", "css_value") => (L'.TFfi ("Basis", "string"), loc)
adam@1750 231 | L.CFfi ("Basis", "css_property") => (L'.TFfi ("Basis", "string"), loc)
adam@1750 232 | L.CFfi ("Basis", "css_style") => (L'.TFfi ("Basis", "string"), loc)
adam@1556 233 | L.CFfi ("Basis", "id") => (L'.TFfi ("Basis", "string"), loc)
adam@1799 234 | L.CFfi ("Basis", "requestHeader") => (L'.TFfi ("Basis", "string"), loc)
adam@1799 235 | L.CFfi ("Basis", "responseHeader") => (L'.TFfi ("Basis", "string"), loc)
adam@1799 236 | L.CFfi ("Basis", "envVar") => (L'.TFfi ("Basis", "string"), loc)
adam@2047 237 | L.CFfi ("Basis", "data_attr_kind") => (L'.TFfi ("Basis", "string"), loc)
adam@2008 238 | L.CFfi ("Basis", "data_attr") => (L'.TFfi ("Basis", "string"), loc)
adamc@196 239
adamc@1104 240 | L.CApp ((L.CFfi ("Basis", "serialized"), _), _) =>
adamc@1104 241 (L'.TFfi ("Basis", "string"), loc)
adamc@1104 242
adamc@251 243 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@252 244 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
adamc@565 245 | L.CApp ((L.CFfi ("Basis", "source"), _), t) =>
adamc@577 246 (L'.TSource, loc)
adamc@568 247 | L.CApp ((L.CFfi ("Basis", "signal"), _), t) =>
adamc@568 248 (L'.TSignal (mt env dtmap t), loc)
adamc@462 249 | L.CApp ((L.CFfi ("Basis", "http_cookie"), _), _) =>
adamc@462 250 (L'.TFfi ("Basis", "string"), loc)
adamc@705 251 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_table"), _), _), _), _) =>
adamc@252 252 (L'.TFfi ("Basis", "string"), loc)
adamc@823 253 | L.CApp ((L.CFfi ("Basis", "sql_view"), _), _) =>
adamc@823 254 (L'.TFfi ("Basis", "string"), loc)
adamc@338 255 | L.CFfi ("Basis", "sql_sequence") =>
adamc@338 256 (L'.TFfi ("Basis", "string"), loc)
adam@1394 257 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _), _), _), _), _) =>
adamc@252 258 (L'.TFfi ("Basis", "string"), loc)
adam@1394 259 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _), _), _), _), _) =>
adamc@252 260 (L'.TFfi ("Basis", "string"), loc)
adamc@1191 261 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_from_items"), _), _), _), _) =>
adamc@748 262 (L'.TFfi ("Basis", "string"), loc)
adam@1778 263 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
adam@1778 264 (L'.TFfi ("Basis", "string"), loc)
adam@1778 265 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_expw"), _), _), _), _), _), _), _), _) =>
adam@1778 266 (L'.TFfi ("Basis", "string"), loc)
adam@1778 267 | L.CApp ((L.CFfi ("Basis", "sql_window"), _), _) =>
adam@1778 268 (L'.TRecord [], loc)
adam@1778 269 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_window_function"), _), _), _), _), _), _), _), _) =>
adamc@252 270 (L'.TFfi ("Basis", "string"), loc)
adamc@707 271 | L.CApp ((L.CApp ((L.CFfi ("Basis", "primary_key"), _), _), _), _) =>
adamc@707 272 (L'.TFfi ("Basis", "string"), loc)
adamc@704 273 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_constraints"), _), _), _), _) =>
adamc@704 274 (L'.TFfi ("Basis", "sql_constraints"), loc)
adamc@705 275 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_constraint"), _), _), _), _) =>
adamc@704 276 (L'.TFfi ("Basis", "string"), loc)
adamc@712 277 | L.CApp ((L.CApp ((L.CFfi ("Basis", "linkable"), _), _), _), _) =>
adamc@712 278 (L'.TRecord [], loc)
adamc@709 279 | L.CApp ((L.CApp ((L.CFfi ("Basis", "matching"), _), _), _), _) =>
adamc@709 280 let
adamc@709 281 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 282 in
adamc@709 283 (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 284 end
adamc@709 285 | L.CApp ((L.CFfi ("Basis", "propagation_mode"), _), _) =>
adamc@709 286 (L'.TFfi ("Basis", "string"), loc)
adam@1870 287 | L.CFfi ("Basis", "dml") =>
adam@1870 288 (L'.TFfi ("Basis", "string"), loc)
adamc@252 289
adamc@252 290 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
adamc@252 291 (L'.TRecord [], loc)
adamc@252 292 | L.CFfi ("Basis", "sql_relop") =>
adamc@252 293 (L'.TFfi ("Basis", "string"), loc)
adamc@252 294 | L.CFfi ("Basis", "sql_direction") =>
adamc@252 295 (L'.TFfi ("Basis", "string"), loc)
adamc@252 296 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
adamc@252 297 (L'.TFfi ("Basis", "string"), loc)
adamc@252 298 | L.CFfi ("Basis", "sql_limit") =>
adamc@252 299 (L'.TFfi ("Basis", "string"), loc)
adamc@252 300 | L.CFfi ("Basis", "sql_offset") =>
adamc@252 301 (L'.TFfi ("Basis", "string"), loc)
adamc@753 302 | L.CApp ((L.CApp ((L.CFfi ("Basis", "fieldsOf"), _), _), _), _) =>
adamc@753 303 (L'.TRecord [], loc)
adamc@252 304
adamc@676 305 | L.CApp ((L.CFfi ("Basis", "sql_injectable_prim"), _), t) =>
adamc@676 306 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 307 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
adamc@252 308 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@750 309 | L.CApp ((L.CApp ((L.CFfi ("Basis", "nullify"), _), _), _), _) =>
adamc@750 310 (L'.TRecord [], loc)
adamc@252 311 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
adamc@252 312 (L'.TFfi ("Basis", "string"), loc)
adamc@252 313 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
adamc@252 314 (L'.TFfi ("Basis", "string"), loc)
adamc@1187 315 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), _), _), _) =>
adamc@252 316 (L'.TFfi ("Basis", "string"), loc)
adamc@252 317 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
adamc@252 318 (L'.TRecord [], loc)
adamc@252 319 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
adamc@252 320 (L'.TRecord [], loc)
adamc@559 321 | L.CApp ((L.CFfi ("Basis", "sql_arith"), _), _) =>
adamc@559 322 (L'.TRecord [], loc)
adamc@441 323 | L.CApp ((L.CFfi ("Basis", "sql_nfunc"), _), _) =>
adamc@441 324 (L'.TFfi ("Basis", "string"), loc)
adamc@746 325 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_ufunc"), _), _), _), _) =>
adamc@746 326 (L'.TFfi ("Basis", "string"), loc)
adam@1778 327 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_partition"), _), _), _), _), _), _) =>
adam@1778 328 (L'.TFfi ("Basis", "string"), loc)
adam@1778 329 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_window"), _), _), _), _), _), _), _), _) =>
adam@1778 330 (L'.TFfi ("Basis", "string"), loc)
adamc@251 331
adamc@668 332 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@668 333 (L'.TFfi ("Basis", "channel"), loc)
adamc@668 334
adamc@196 335 | L.CRel _ => poly ()
adamc@196 336 | L.CNamed n =>
adamc@196 337 (case IM.find (dtmap, n) of
adamc@196 338 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 339 | NONE =>
adamc@196 340 let
adamc@196 341 val r = ref (L'.Default, [])
adamc@196 342 val (_, xs, xncs) = Env.lookupDatatype env n
adam@1682 343
adamc@196 344 val dtmap' = IM.insert (dtmap, n, r)
adam@1682 345
adamc@196 346 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 347 in
adamc@196 348 case xs of
adamc@198 349 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 350 (L'.TDatatype (n, r), loc))
adamc@196 351 | _ => poly ()
adamc@196 352 end)
adamc@196 353 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 354 | L.CApp _ => poly ()
adamc@196 355 | L.CAbs _ => poly ()
adamc@196 356
adamc@196 357 | L.CName _ => poly ()
adamc@196 358
adamc@196 359 | L.CRecord _ => poly ()
adamc@196 360 | L.CConcat _ => poly ()
adamc@621 361 | L.CMap _ => poly ()
adamc@196 362 | L.CUnit => poly ()
adamc@214 363
adamc@214 364 | L.CTuple _ => poly ()
adamc@214 365 | L.CProj _ => poly ()
adamc@626 366
adamc@626 367 | L.CKAbs _ => poly ()
adamc@626 368 | L.CKApp _ => poly ()
adamc@626 369 | L.TKFun _ => poly ()
adamc@196 370 end
adamc@25 371 in
adamc@196 372 mt env IM.empty
adamc@25 373 end
adamc@25 374
adamc@25 375 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 376
adamc@179 377 structure IM = IntBinaryMap
adamc@179 378
adamc@179 379 datatype foo_kind =
adamc@179 380 Attr
adamc@179 381 | Url
adamc@179 382
adamc@179 383 fun fk2s fk =
adamc@179 384 case fk of
adamc@179 385 Attr => "attr"
adamc@179 386 | Url => "url"
adamc@179 387
adam@1730 388 type vr = string * int * L'.typ * L'.exp * string
adam@1730 389
adamc@179 390 structure Fm :> sig
adamc@179 391 type t
adamc@179 392
adamc@179 393 val empty : int -> t
adamc@179 394
adam@1730 395 val lookup : t -> foo_kind -> int -> (int -> t -> vr * t) -> t * int
adam@1730 396 val lookupList : t -> foo_kind -> L'.typ -> (int -> t -> vr * t) -> t * int
adamc@179 397 val enter : t -> t
adamc@179 398 val decls : t -> L'.decl list
adamc@683 399
adamc@683 400 val freshName : t -> int * t
adamc@179 401 end = struct
adamc@179 402
adamc@179 403 structure M = BinaryMapFn(struct
adamc@179 404 type ord_key = foo_kind
adamc@179 405 fun compare x =
adamc@179 406 case x of
adamc@179 407 (Attr, Attr) => EQUAL
adamc@179 408 | (Attr, _) => LESS
adamc@179 409 | (_, Attr) => GREATER
adamc@179 410
adamc@179 411 | (Url, Url) => EQUAL
adamc@179 412 end)
adamc@179 413
adamc@758 414 structure TM = BinaryMapFn(struct
adamc@758 415 type ord_key = L'.typ
adamc@758 416 val compare = MonoUtil.Typ.compare
adamc@758 417 end)
adamc@758 418
adamc@179 419 type t = {
adamc@179 420 count : int,
adamc@179 421 map : int IM.map M.map,
adamc@758 422 listMap : int TM.map M.map,
adam@1730 423 decls : vr list
adamc@179 424 }
adamc@179 425
adamc@179 426 fun empty count = {
adamc@179 427 count = count,
adamc@179 428 map = M.empty,
adamc@758 429 listMap = M.empty,
adamc@179 430 decls = []
adamc@179 431 }
adamc@179 432
adam@1287 433 fun chooseNext count =
adam@1287 434 let
adam@1287 435 val n = !nextPvar
adam@1287 436 in
adam@1287 437 if count < n then
adam@1287 438 (count, count+1)
adam@1287 439 else
adam@1287 440 (nextPvar := n + 1;
adam@1287 441 (n, n+1))
adam@1287 442 end
adam@1287 443
adamc@758 444 fun enter ({count, map, listMap, ...} : t) = {count = count, map = map, listMap = listMap, decls = []}
adam@1287 445 fun freshName {count, map, listMap, decls} =
adam@1287 446 let
adam@1287 447 val (next, count) = chooseNext count
adam@1287 448 in
adam@1287 449 (next, {count = count , map = map, listMap = listMap, decls = decls})
adam@1287 450 end
adam@1730 451 fun decls ({decls, ...} : t) =
adam@1730 452 case decls of
adam@1730 453 [] => []
adam@1730 454 | _ => [(L'.DValRec decls, ErrorMsg.dummySpan)]
adamc@179 455
adamc@758 456 fun lookup (t as {count, map, listMap, decls}) k n thunk =
adamc@120 457 let
adamc@179 458 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 459 in
adamc@179 460 case IM.find (im, n) of
adamc@179 461 NONE =>
adamc@179 462 let
adamc@179 463 val n' = count
adamc@758 464 val (d, {count, map, listMap, decls}) =
adamc@758 465 thunk count {count = count + 1,
adamc@758 466 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@758 467 listMap = listMap,
adamc@758 468 decls = decls}
adamc@179 469 in
adamc@179 470 ({count = count,
adamc@179 471 map = map,
adamc@758 472 listMap = listMap,
adamc@758 473 decls = d :: decls}, n')
adamc@758 474 end
adamc@758 475 | SOME n' => (t, n')
adamc@758 476 end
adamc@758 477
adamc@758 478 fun lookupList (t as {count, map, listMap, decls}) k tp thunk =
adamc@758 479 let
adamc@758 480 val tm = Option.getOpt (M.find (listMap, k), TM.empty)
adamc@758 481 in
adamc@758 482 case TM.find (tm, tp) of
adamc@758 483 NONE =>
adamc@758 484 let
adamc@758 485 val n' = count
adamc@758 486 val (d, {count, map, listMap, decls}) =
adamc@758 487 thunk count {count = count + 1,
adamc@758 488 map = map,
adamc@758 489 listMap = M.insert (listMap, k, TM.insert (tm, tp, n')),
adamc@758 490 decls = decls}
adamc@758 491 in
adamc@758 492 ({count = count,
adamc@758 493 map = map,
adamc@758 494 listMap = listMap,
adamc@179 495 decls = d :: decls}, n')
adamc@179 496 end
adamc@179 497 | SOME n' => (t, n')
adamc@179 498 end
adamc@179 499
adamc@179 500 end
adamc@185 501
adamc@185 502
adamc@185 503 fun capitalize s =
adamc@185 504 if s = "" then
adamc@185 505 s
adamc@185 506 else
adamc@185 507 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 508
adamc@179 509 fun fooifyExp fk env =
adamc@179 510 let
adamc@179 511 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 512 case #1 e of
adamc@120 513 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 514 let
adamc@120 515 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 516 in
adam@2048 517 ((L'.EPrim (Prim.String (Prim.Normal, Settings.getUrlPrefix () ^ s)), loc), fm)
adamc@120 518 end
adamc@120 519 | L'.EClosure (fnam, args) =>
adamc@120 520 let
adamc@120 521 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 522 val ft = monoType env ft
adamc@111 523
adamc@179 524 fun attrify (args, ft, e, fm) =
adamc@120 525 case (args, ft) of
adamc@179 526 ([], _) => (e, fm)
adamc@120 527 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 528 let
adamc@179 529 val (arg', fm) = fooify fm (arg, t)
adamc@179 530 in
adamc@179 531 attrify (args, ft,
adamc@179 532 (L'.EStrcat (e,
adam@2048 533 (L'.EStrcat ((L'.EPrim (Prim.String (Prim.Normal, "/")), loc),
adamc@179 534 arg'), loc)), loc),
adamc@179 535 fm)
adamc@179 536 end
adamc@120 537 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 538 (e, fm))
adamc@120 539 in
adam@2048 540 attrify (args, ft, (L'.EPrim (Prim.String (Prim.Normal, Settings.getUrlPrefix () ^ s)), loc), fm)
adamc@120 541 end
adamc@120 542 | _ =>
adamc@120 543 case t of
adam@2048 544 L'.TFfi ("Basis", "unit") => ((L'.EPrim (Prim.String (Prim.Normal, "")), loc), fm)
adam@1663 545 | L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [(e, tAll)]), loc), fm)
adamc@200 546
adam@2048 547 | L'.TRecord [] => ((L'.EPrim (Prim.String (Prim.Normal, "")), loc), fm)
adamc@200 548 | L'.TRecord ((x, t) :: xts) =>
adamc@200 549 let
adamc@200 550 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 551 in
adamc@200 552 foldl (fn ((x, t), (se, fm)) =>
adamc@200 553 let
adamc@200 554 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 555 in
adamc@200 556 ((L'.EStrcat (se,
adam@2048 557 (L'.EStrcat ((L'.EPrim (Prim.String (Prim.Normal, "/")), loc),
adamc@200 558 se'), loc)), loc),
adamc@200 559 fm)
adamc@200 560 end) (se, fm) xts
adamc@200 561 end
adamc@111 562
adamc@196 563 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 564 let
adamc@179 565 fun makeDecl n fm =
adamc@179 566 let
adam@1655 567 val (x, xncs) =
adam@1713 568 case ListUtil.search (fn (x, i', xncs) =>
adam@1655 569 if i' = i then
adam@1655 570 SOME (x, xncs)
adam@1655 571 else
adam@1713 572 NONE) (!pvarDefs) of
adam@1655 573 NONE =>
adam@1655 574 let
adam@1655 575 val (x, _, xncs) = Env.lookupDatatype env i
adam@1655 576 in
adam@1655 577 (x, map (fn (x, n, c) => (x, n, Option.map (monoType env) c)) xncs)
adam@1655 578 end
adam@1655 579 | SOME v => v
adamc@179 580
adamc@179 581 val (branches, fm) =
adamc@179 582 ListUtil.foldlMap
adamc@179 583 (fn ((x, n, to), fm) =>
adamc@179 584 case to of
adamc@179 585 NONE =>
adamc@188 586 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adam@2048 587 (L'.EPrim (Prim.String (Prim.Normal, x)), loc)),
adamc@179 588 fm)
adamc@179 589 | SOME t =>
adamc@179 590 let
adamc@182 591 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 592 in
adamc@188 593 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adam@2048 594 (L'.EStrcat ((L'.EPrim (Prim.String (Prim.Normal, x ^ "/")), loc),
adamc@179 595 arg), loc)),
adamc@179 596 fm)
adamc@179 597 end)
adamc@179 598 fm xncs
adamc@179 599
adamc@179 600 val dom = tAll
adamc@179 601 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 602 in
adam@1730 603 ((fk2s fk ^ "ify_" ^ x,
adam@1730 604 n,
adam@1730 605 (L'.TFun (dom, ran), loc),
adam@1730 606 (L'.EAbs ("x",
adam@1730 607 dom,
adam@1730 608 ran,
adam@1730 609 (L'.ECase ((L'.ERel 0, loc),
adam@1730 610 branches,
adam@1730 611 {disc = dom,
adam@1730 612 result = ran}), loc)), loc),
adam@1730 613 ""),
adamc@179 614 fm)
adam@1682 615 end
adamc@179 616
adamc@179 617 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 618 in
adamc@179 619 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 620 end
adamc@164 621
adamc@471 622 | L'.TOption t =>
adamc@471 623 let
adamc@471 624 val (body, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@471 625 in
adamc@471 626 ((L'.ECase (e,
adamc@471 627 [((L'.PNone t, loc),
adam@2048 628 (L'.EPrim (Prim.String (Prim.Normal, "None")), loc)),
adam@1682 629
adamc@471 630 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc),
adam@2048 631 (L'.EStrcat ((L'.EPrim (Prim.String (Prim.Normal, "Some/")), loc),
adamc@471 632 body), loc))],
adamc@471 633 {disc = tAll,
adamc@471 634 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@471 635 fm)
adamc@471 636 end
adamc@471 637
adamc@758 638 | L'.TList t =>
adamc@758 639 let
adamc@758 640 fun makeDecl n fm =
adamc@758 641 let
adamc@758 642 val rt = (L'.TRecord [("1", t), ("2", (L'.TList t, loc))], loc)
adamc@758 643 val (arg, fm) = fooify fm ((L'.ERel 0, loc), rt)
adamc@758 644
adamc@758 645 val branches = [((L'.PNone rt, loc),
adam@2048 646 (L'.EPrim (Prim.String (Prim.Normal, "Nil")), loc)),
adamc@758 647 ((L'.PSome (rt, (L'.PVar ("a", rt), loc)), loc),
adam@2048 648 (L'.EStrcat ((L'.EPrim (Prim.String (Prim.Normal, "Cons/")), loc),
adamc@758 649 arg), loc))]
adamc@758 650
adamc@758 651 val dom = tAll
adamc@758 652 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@758 653 in
adam@1730 654 ((fk2s fk ^ "ify_list",
adam@1730 655 n,
adam@1730 656 (L'.TFun (dom, ran), loc),
adam@1730 657 (L'.EAbs ("x",
adam@1730 658 dom,
adam@1730 659 ran,
adam@1730 660 (L'.ECase ((L'.ERel 0, loc),
adam@1730 661 branches,
adam@1730 662 {disc = dom,
adam@1730 663 result = ran}), loc)), loc),
adam@1730 664 ""),
adamc@758 665 fm)
adamc@758 666 end
adamc@758 667
adamc@758 668 val (fm, n) = Fm.lookupList fm fk t makeDecl
adamc@758 669 in
adamc@758 670 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@758 671 end
adamc@758 672
adamc@490 673 | _ => (E.errorAt loc "Don't know how to encode attribute/URL type";
adamc@120 674 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 675 (dummyExp, fm))
adamc@120 676 in
adamc@120 677 fooify
adamc@120 678 end
adamc@120 679
adamc@179 680 val attrifyExp = fooifyExp Attr
adamc@179 681 val urlifyExp = fooifyExp Url
adamc@105 682
ziv@2221 683 val urlifiedUnit =
ziv@2221 684 let
ziv@2221 685 val loc = ErrorMsg.dummySpan
ziv@2221 686 (* Urlifies [ERel 0] to match the [sqlcacheInfo] field of [EQuery]s. *)
ziv@2221 687 val (urlified, _) = urlifyExp CoreEnv.empty (Fm.empty 0)
ziv@2221 688 ((L'.ERel 0, loc), (L'.TRecord [], loc))
ziv@2221 689 in
ziv@2221 690 urlified
ziv@2221 691 end
ziv@2221 692
adamc@143 693 datatype 'a failable_search =
adamc@143 694 Found of 'a
adamc@143 695 | NotFound
adamc@143 696 | Error
adamc@143 697
adamc@153 698 structure St :> sig
adamc@153 699 type t
adamc@153 700
adamc@153 701 val empty : t
adamc@153 702
adamc@153 703 val radioGroup : t -> string option
adamc@153 704 val setRadioGroup : t * string -> t
adamc@153 705 end = struct
adamc@153 706
adamc@153 707 type t = {
adamc@153 708 radioGroup : string option
adamc@153 709 }
adamc@153 710
adamc@153 711 val empty = {radioGroup = NONE}
adamc@153 712
adamc@153 713 fun radioGroup (t : t) = #radioGroup t
adamc@153 714
adamc@153 715 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 716
adamc@153 717 end
adamc@153 718
adamc@186 719 fun monoPatCon env pc =
adamc@178 720 case pc of
adamc@178 721 L.PConVar n => L'.PConVar n
adamc@188 722 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 723 arg = Option.map (monoType env) arg}
adamc@178 724
adamc@193 725 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 726
adamc@757 727
adamc@757 728 fun listify t = (L'.TRecord [("1", t), ("2", (L'.TList t, #2 t))], #2 t)
adamc@757 729
adamc@193 730 fun monoPat env (all as (p, loc)) =
adamc@193 731 let
adamc@193 732 fun poly () =
adamc@193 733 (E.errorAt loc "Unsupported pattern";
adamc@193 734 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 735 dummyPat)
adamc@193 736 in
adamc@193 737 case p of
adamc@193 738 L.PWild => (L'.PWild, loc)
adamc@193 739 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 740 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 741 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@757 742 | L.PCon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], NONE) =>
adamc@757 743 (L'.PNone (listify (monoType env t)), loc)
adamc@757 744 | L.PCon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], SOME p) =>
adamc@757 745 (L'.PSome (listify (monoType env t), monoPat env p), loc)
adamc@288 746 | L.PCon (L.Option, _, [t], NONE) => (L'.PNone (monoType env t), loc)
adamc@757 747 | L.PCon (L.Option, pc, [t], SOME p) => (L'.PSome (monoType env t, monoPat env p), loc)
adamc@193 748 | L.PCon _ => poly ()
adamc@193 749 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 750 end
adamc@178 751
adamc@252 752 fun strcat loc es =
adamc@252 753 case es of
adam@2048 754 [] => (L'.EPrim (Prim.String (Prim.Normal, "")), loc)
adamc@252 755 | [e] => e
adamc@252 756 | _ =>
adamc@252 757 let
adamc@252 758 val e2 = List.last es
adamc@252 759 val es = List.take (es, length es - 1)
adamc@252 760 val e1 = List.last es
adamc@252 761 val es = List.take (es, length es - 1)
adamc@252 762 in
adamc@252 763 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
adamc@252 764 (L'.EStrcat (e1, e2), loc) es
adamc@252 765 end
adamc@252 766
adamc@252 767 fun strcatComma loc es =
adamc@252 768 case es of
adam@2048 769 [] => (L'.EPrim (Prim.String (Prim.Normal, "")), loc)
adamc@252 770 | [e] => e
adamc@252 771 | _ =>
adamc@252 772 let
adamc@252 773 val e1 = List.last es
adamc@252 774 val es = List.take (es, length es - 1)
adamc@252 775 in
adamc@252 776 foldr (fn (e, e') =>
adamc@265 777 case (e, e') of
adam@2048 778 ((L'.EPrim (Prim.String (_, "")), _), _) => e'
adam@2048 779 | (_, (L'.EPrim (Prim.String (_, "")), _)) => e
adamc@252 780 | _ =>
adamc@252 781 (L'.EStrcat (e,
adam@2048 782 (L'.EStrcat ((L'.EPrim (Prim.String (Prim.Normal, ", ")), loc), e'), loc)), loc))
adamc@252 783 e1 es
adamc@252 784 end
adamc@252 785
adamc@252 786 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
adamc@252 787
adamc@735 788 val readCookie = ref IS.empty
adamc@735 789
adamc@877 790 fun isBlobby (t : L.con) =
adamc@877 791 case #1 t of
adamc@877 792 L.CFfi ("Basis", "string") => true
adamc@877 793 | L.CFfi ("Basis", "blob") => true
adamc@877 794 | _ => false
adamc@877 795
adamc@179 796 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 797 let
adamc@598 798 val strcat = strcat loc
adamc@598 799 val strcatComma = strcatComma loc
adam@2048 800 fun str s = (L'.EPrim (Prim.String (Prim.Normal, s)), loc)
adam@2048 801 fun strH s = (L'.EPrim (Prim.String (Prim.Html, s)), loc)
adamc@598 802
adamc@25 803 fun poly () =
adamc@25 804 (E.errorAt loc "Unsupported expression";
adamc@25 805 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 806 (dummyExp, fm))
adamc@389 807
adamc@389 808 fun numTy t =
adamc@417 809 (L'.TRecord [("Zero", t),
adamc@417 810 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 811 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 812 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 813 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 814 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
mad@1831 815 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adam@1832 816 ("Pow", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc)
mad@1831 817 fun numEx (t, zero, neg, plus, minus, times, dv, md, ex) =
adamc@417 818 ((L'.ERecord [("Zero", (L'.EPrim zero, loc), t),
adamc@417 819 ("Neg", neg, (L'.TFun (t, t), loc)),
adamc@389 820 ("Plus", plus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 821 ("Minus", minus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 822 ("Times", times, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 823 ("Div", dv, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
mad@1831 824 ("Mod", md, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adam@1832 825 ("Pow", ex, (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc), fm)
adamc@391 826
adamc@391 827 fun ordTy t =
adamc@391 828 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 829 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))], loc)
adamc@391 830 fun ordEx (t, lt, le) =
adamc@391 831 ((L'.ERecord [("Lt", lt, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 832 ("Le", le, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 833 loc), fm)
adamc@750 834
adamc@750 835 fun outerRec xts =
adamc@750 836 (L'.TRecord (map (fn ((L.CName x, _), (L.CRecord (_, xts), _)) =>
adamc@750 837 (x, (L'.TRecord (map (fn (x', _) => (x, (L'.TRecord [], loc))) xts), loc))
adamc@750 838 | (x, all as (_, loc)) =>
adamc@750 839 (E.errorAt loc "Unsupported record field constructor";
adamc@750 840 Print.eprefaces' [("Name", CorePrint.p_con env x),
adamc@750 841 ("Constructor", CorePrint.p_con env all)];
adamc@750 842 ("", dummyTyp))) xts), loc)
adamc@25 843 in
adamc@25 844 case e of
adamc@179 845 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 846 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 847 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 848 | L.ECon (dk, pc, [], eo) =>
adamc@193 849 let
adamc@179 850 val (eo, fm) =
adamc@179 851 case eo of
adamc@179 852 NONE => (NONE, fm)
adamc@179 853 | SOME e =>
adamc@179 854 let
adamc@179 855 val (e, fm) = monoExp (env, st, fm) e
adamc@179 856 in
adamc@179 857 (SOME e, fm)
adamc@179 858 end
adamc@179 859 in
adamc@188 860 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 861 end
adamc@757 862 | L.ECon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], NONE) =>
adamc@757 863 ((L'.ENone (listify (monoType env t)), loc), fm)
adamc@757 864 | L.ECon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], SOME e) =>
adamc@757 865 let
adamc@757 866 val (e, fm) = monoExp (env, st, fm) e
adamc@757 867 in
adamc@757 868 ((L'.ESome (listify (monoType env t), e), loc), fm)
adamc@757 869 end
adamc@297 870 | L.ECon (L.Option, _, [t], NONE) =>
adamc@297 871 ((L'.ENone (monoType env t), loc), fm)
adamc@297 872 | L.ECon (L.Option, _, [t], SOME e) =>
adamc@297 873 let
adamc@297 874 val (e, fm) = monoExp (env, st, fm) e
adamc@297 875 in
adamc@297 876 ((L'.ESome (monoType env t, e), loc), fm)
adamc@297 877 end
adamc@193 878 | L.ECon _ => poly ()
adamc@94 879
adam@1287 880 | L.ECApp (
adam@1287 881 (L.ECApp (
adam@1288 882 (L.ECApp ((L.EFfi ("Basis", "make"), _), nmC as (L.CName nm, _)), _),
adam@1287 883 t), _),
adam@1287 884 (L.CRecord (_, xts), _)) =>
adam@1287 885 let
adam@1288 886 val t' = monoType env t
adam@1288 887 val xts' = map (fn (x, t) => (monoName env x, monoType env t)) xts
adam@1288 888 val xts' = (nm, t') :: xts'
adam@1288 889 val xts' = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) xts'
adam@1288 890 val (n, cs) = pvar ((nmC, t) :: xts, xts', loc)
adam@1287 891 val cs' = map (fn (x, n, t) => (x, n, SOME t)) cs
adam@1287 892 val cl = ElabUtil.classifyDatatype cs'
adam@1287 893 in
adam@1287 894 case List.find (fn (nm', _, _) => nm' = nm) cs of
adam@1287 895 NONE => raise Fail "Monoize: Polymorphic variant tag mismatch for 'make'"
adam@1288 896 | SOME (_, n', _) => ((L'.EAbs ("x", t', (L'.TDatatype (n, ref (cl, cs')), loc),
adam@1287 897 (L'.ECon (cl, L'.PConVar n', SOME (L'.ERel 0, loc)), loc)), loc),
adam@1287 898 fm)
adam@1287 899 end
adam@1287 900
adam@1287 901 | L.ECApp (
adam@1287 902 (L.ECApp ((L.EFfi ("Basis", "match"), _), (L.CRecord (_, xts), _)), _),
adam@1287 903 t) =>
adam@1287 904 let
adam@1287 905 val t = monoType env t
adam@1288 906 val xts' = map (fn (x, t) => (monoName env x, monoType env t)) xts
adam@1288 907 val xts' = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) xts'
adam@1288 908 val (n, cs) = pvar (xts, xts', loc)
adam@1287 909 val cs' = map (fn (x, n, t) => (x, n, SOME t)) cs
adam@1287 910 val cl = ElabUtil.classifyDatatype cs'
adam@1288 911 val fs = (L'.TRecord (map (fn (x, t') => (x, (L'.TFun (t', t), loc))) xts'), loc)
adam@1287 912 val dt = (L'.TDatatype (n, ref (cl, cs')), loc)
adam@1287 913 in
adam@1287 914 ((L'.EAbs ("v",
adam@1287 915 dt,
adam@1287 916 (L'.TFun (fs, t), loc),
adam@1287 917 (L'.EAbs ("fs", fs, t,
adam@1287 918 (L'.ECase ((L'.ERel 1, loc),
adam@1287 919 map (fn (x, n', t') =>
adam@1287 920 ((L'.PCon (cl, L'.PConVar n', SOME (L'.PVar ("x", t'), loc)), loc),
adam@1287 921 (L'.EApp ((L'.EField ((L'.ERel 1, loc), x), loc),
adam@1287 922 (L'.ERel 0, loc)), loc))) cs,
adam@1287 923 {disc = dt, result = t}), loc)), loc)), loc),
adam@1287 924 fm)
adam@1287 925 end
adam@1287 926
adamc@387 927 | L.ECApp ((L.EFfi ("Basis", "eq"), _), t) =>
adamc@387 928 let
adamc@387 929 val t = monoType env t
adamc@387 930 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 931 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 932 in
adamc@387 933 ((L'.EAbs ("f", dom, dom,
adamc@387 934 (L'.ERel 0, loc)), loc), fm)
adamc@387 935 end
adamc@387 936 | L.ECApp ((L.EFfi ("Basis", "ne"), _), t) =>
adamc@387 937 let
adamc@387 938 val t = monoType env t
adamc@387 939 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 940 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 941 in
adamc@387 942 ((L'.EAbs ("f", dom, dom,
adamc@387 943 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@387 944 (L'.EAbs ("y", t, b,
adamc@387 945 (L'.EUnop ("!", (L'.EApp ((L'.EApp ((L'.ERel 2, loc),
adamc@387 946 (L'.ERel 1, loc)), loc),
adamc@387 947 (L'.ERel 0, loc)), loc)), loc)),
adamc@387 948 loc)),
adamc@387 949 loc)),
adamc@387 950 loc), fm)
adamc@387 951 end
adamc@387 952 | L.EFfi ("Basis", "eq_int") =>
adamc@387 953 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@387 954 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@387 955 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@387 956 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 957 (L'.EBinop (L'.Int, "==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@387 958 fm)
adamc@394 959 | L.EFfi ("Basis", "eq_float") =>
adamc@394 960 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@394 961 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 962 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@394 963 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 964 (L'.EBinop (L'.NotInt, "==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@394 965 fm)
adamc@388 966 | L.EFfi ("Basis", "eq_bool") =>
adamc@388 967 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 968 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 969 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 970 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 971 (L'.EBinop (L'.NotInt, "==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 972 fm)
adamc@388 973 | L.EFfi ("Basis", "eq_string") =>
adamc@388 974 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@388 975 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 976 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@388 977 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 978 (L'.EBinop (L'.NotInt, "!strcmp", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 979 fm)
adamc@821 980 | L.EFfi ("Basis", "eq_char") =>
adamc@821 981 ((L'.EAbs ("x", (L'.TFfi ("Basis", "char"), loc),
adamc@821 982 (L'.TFun ((L'.TFfi ("Basis", "char"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@821 983 (L'.EAbs ("y", (L'.TFfi ("Basis", "char"), loc),
adamc@821 984 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 985 (L'.EBinop (L'.NotInt, "==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@821 986 fm)
adamc@437 987 | L.EFfi ("Basis", "eq_time") =>
adamc@437 988 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc),
adamc@437 989 (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@437 990 (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc),
adamc@437 991 (L'.TFfi ("Basis", "bool"), loc),
adam@1663 992 (L'.EFfiApp ("Basis", "eq_time", [((L'.ERel 1, loc), (L'.TFfi ("Basis", "time"), loc)),
adam@1663 993 ((L'.ERel 0, loc), (L'.TFfi ("Basis", "time"), loc))]), loc)), loc)), loc),
adamc@437 994 fm)
adamc@844 995
adamc@422 996 | L.ECApp ((L.EFfi ("Basis", "mkEq"), _), t) =>
adamc@422 997 let
adamc@422 998 val t = monoType env t
adamc@422 999 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@422 1000 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@422 1001 in
adamc@422 1002 ((L'.EAbs ("f", dom, dom,
adamc@422 1003 (L'.ERel 0, loc)), loc), fm)
adamc@422 1004 end
adamc@387 1005
adamc@417 1006 | L.ECApp ((L.EFfi ("Basis", "zero"), _), t) =>
adamc@417 1007 let
adamc@417 1008 val t = monoType env t
adamc@417 1009 in
adamc@417 1010 ((L'.EAbs ("r", numTy t, t,
adamc@417 1011 (L'.EField ((L'.ERel 0, loc), "Zero"), loc)), loc), fm)
adamc@417 1012 end
adamc@389 1013 | L.ECApp ((L.EFfi ("Basis", "neg"), _), t) =>
adamc@389 1014 let
adamc@389 1015 val t = monoType env t
adamc@389 1016 in
adamc@389 1017 ((L'.EAbs ("r", numTy t, (L'.TFun (t, t), loc),
adamc@389 1018 (L'.EField ((L'.ERel 0, loc), "Neg"), loc)), loc), fm)
adamc@389 1019 end
adamc@389 1020 | L.ECApp ((L.EFfi ("Basis", "plus"), _), t) =>
adamc@389 1021 let
adamc@389 1022 val t = monoType env t
adamc@389 1023 in
adamc@389 1024 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 1025 (L'.EField ((L'.ERel 0, loc), "Plus"), loc)), loc), fm)
adamc@389 1026 end
adamc@389 1027 | L.ECApp ((L.EFfi ("Basis", "minus"), _), t) =>
adamc@389 1028 let
adamc@389 1029 val t = monoType env t
adamc@389 1030 in
adamc@389 1031 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 1032 (L'.EField ((L'.ERel 0, loc), "Minus"), loc)), loc), fm)
adamc@389 1033 end
adamc@389 1034 | L.ECApp ((L.EFfi ("Basis", "times"), _), t) =>
adamc@389 1035 let
adamc@389 1036 val t = monoType env t
adamc@389 1037 in
adamc@389 1038 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 1039 (L'.EField ((L'.ERel 0, loc), "Times"), loc)), loc), fm)
adamc@389 1040 end
adamc@775 1041 | L.ECApp ((L.EFfi ("Basis", "divide"), _), t) =>
adamc@389 1042 let
adamc@389 1043 val t = monoType env t
adamc@389 1044 in
adamc@389 1045 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 1046 (L'.EField ((L'.ERel 0, loc), "Div"), loc)), loc), fm)
adamc@389 1047 end
adamc@389 1048 | L.ECApp ((L.EFfi ("Basis", "mod"), _), t) =>
adamc@389 1049 let
adamc@389 1050 val t = monoType env t
adamc@389 1051 in
adamc@389 1052 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 1053 (L'.EField ((L'.ERel 0, loc), "Mod"), loc)), loc), fm)
adamc@389 1054 end
adam@1832 1055 | L.ECApp ((L.EFfi ("Basis", "pow"), _), t) =>
mad@1831 1056 let
mad@1831 1057 val t = monoType env t
mad@1831 1058 in
mad@1831 1059 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adam@1832 1060 (L'.EField ((L'.ERel 0, loc), "Pow"), loc)), loc), fm)
mad@1831 1061 end
adamc@389 1062 | L.EFfi ("Basis", "num_int") =>
adamc@389 1063 let
adamc@389 1064 fun intBin s =
adamc@389 1065 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 1066 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "int"), loc)), loc),
adamc@389 1067 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@389 1068 (L'.TFfi ("Basis", "int"), loc),
adam@1360 1069 (L'.EBinop (L'.Int, s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@389 1070 in
adamc@389 1071 numEx ((L'.TFfi ("Basis", "int"), loc),
adamc@417 1072 Prim.Int (Int64.fromInt 0),
adamc@389 1073 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 1074 (L'.TFfi ("Basis", "int"), loc),
adamc@389 1075 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@389 1076 intBin "+",
adamc@389 1077 intBin "-",
adamc@389 1078 intBin "*",
adamc@389 1079 intBin "/",
mad@1831 1080 intBin "%",
mad@1831 1081 intBin "powl"
mad@1831 1082 )
adamc@389 1083 end
adamc@390 1084 | L.EFfi ("Basis", "num_float") =>
adamc@390 1085 let
adamc@390 1086 fun floatBin s =
adamc@390 1087 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 1088 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "float"), loc)), loc),
adamc@390 1089 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@390 1090 (L'.TFfi ("Basis", "float"), loc),
adam@1360 1091 (L'.EBinop (L'.NotInt, s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@390 1092 in
adamc@390 1093 numEx ((L'.TFfi ("Basis", "float"), loc),
adamc@417 1094 Prim.Float 0.0,
adamc@390 1095 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 1096 (L'.TFfi ("Basis", "float"), loc),
adamc@390 1097 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@390 1098 floatBin "+",
adamc@390 1099 floatBin "-",
adamc@390 1100 floatBin "*",
adam@1619 1101 floatBin "fdiv",
mad@1831 1102 floatBin "fmod",
mad@1831 1103 floatBin "powf"
mad@1831 1104 )
adamc@390 1105 end
adamc@391 1106
adamc@391 1107 | L.ECApp ((L.EFfi ("Basis", "lt"), _), t) =>
adamc@391 1108 let
adamc@391 1109 val t = monoType env t
adamc@391 1110 in
adamc@391 1111 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
adamc@391 1112 (L'.EField ((L'.ERel 0, loc), "Lt"), loc)), loc), fm)
adamc@391 1113 end
adamc@391 1114 | L.ECApp ((L.EFfi ("Basis", "le"), _), t) =>
adamc@391 1115 let
adamc@391 1116 val t = monoType env t
adamc@391 1117 in
adamc@391 1118 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
adamc@391 1119 (L'.EField ((L'.ERel 0, loc), "Le"), loc)), loc), fm)
adamc@391 1120 end
adamc@392 1121 | L.ECApp ((L.EFfi ("Basis", "gt"), _), t) =>
adamc@392 1122 let
adamc@392 1123 val t = monoType env t
adamc@392 1124 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@392 1125 in
adamc@392 1126 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
adamc@392 1127 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@392 1128 (L'.EAbs ("y", t, b,
adamc@392 1129 (L'.EUnop ("!",
adamc@392 1130 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
adamc@392 1131 "Le"), loc),
adamc@392 1132 (L'.ERel 1, loc)), loc),
adamc@392 1133 (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@392 1134 loc)),
adamc@392 1135 loc), fm)
adamc@392 1136 end
adamc@392 1137 | L.ECApp ((L.EFfi ("Basis", "ge"), _), t) =>
adamc@392 1138 let
adamc@392 1139 val t = monoType env t
adamc@392 1140 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@392 1141 in
adamc@392 1142 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
adamc@392 1143 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@392 1144 (L'.EAbs ("y", t, b,
adamc@392 1145 (L'.EUnop ("!",
adamc@392 1146 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
adamc@392 1147 "Lt"), loc),
adamc@392 1148 (L'.ERel 1, loc)), loc),
adamc@392 1149 (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@392 1150 loc)),
adamc@392 1151 loc), fm)
adamc@392 1152 end
adamc@391 1153 | L.EFfi ("Basis", "ord_int") =>
adamc@391 1154 let
adamc@391 1155 fun intBin s =
adamc@391 1156 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@391 1157 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@391 1158 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@391 1159 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 1160 (L'.EBinop (L'.Int, s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@391 1161 in
adamc@391 1162 ordEx ((L'.TFfi ("Basis", "int"), loc),
adamc@391 1163 intBin "<",
adamc@391 1164 intBin "<=")
adamc@391 1165 end
adamc@394 1166 | L.EFfi ("Basis", "ord_float") =>
adamc@394 1167 let
adamc@394 1168 fun floatBin s =
adamc@394 1169 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@394 1170 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 1171 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@394 1172 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 1173 (L'.EBinop (L'.NotInt, s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@394 1174 in
adamc@394 1175 ordEx ((L'.TFfi ("Basis", "float"), loc),
adamc@394 1176 floatBin "<",
adamc@394 1177 floatBin "<=")
adamc@394 1178 end
adamc@394 1179 | L.EFfi ("Basis", "ord_bool") =>
adamc@394 1180 let
adamc@394 1181 fun boolBin s =
adamc@394 1182 (L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@394 1183 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 1184 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@394 1185 (L'.TFfi ("Basis", "bool"), loc),
adam@1371 1186 (L'.EBinop (L'.NotInt, s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@394 1187 in
adamc@394 1188 ordEx ((L'.TFfi ("Basis", "bool"), loc),
adam@1371 1189 boolBin "<",
adam@1371 1190 boolBin "<=")
adamc@394 1191 end
adamc@395 1192 | L.EFfi ("Basis", "ord_string") =>
adamc@395 1193 let
adamc@395 1194 fun boolBin s =
adamc@395 1195 (L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@395 1196 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@395 1197 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@395 1198 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 1199 (L'.EBinop (L'.NotInt, s,
adam@1360 1200 (L'.EBinop (L'.NotInt, "strcmp",
adamc@395 1201 (L'.ERel 1, loc),
adamc@395 1202 (L'.ERel 0, loc)), loc),
adamc@395 1203 (L'.EPrim (Prim.Int (Int64.fromInt 0)), loc)), loc)), loc)), loc)
adamc@395 1204 in
adamc@395 1205 ordEx ((L'.TFfi ("Basis", "string"), loc),
adamc@395 1206 boolBin "<",
adamc@395 1207 boolBin "<=")
adamc@395 1208 end
adamc@821 1209 | L.EFfi ("Basis", "ord_char") =>
adamc@821 1210 let
adamc@821 1211 fun charBin s =
adamc@821 1212 (L'.EAbs ("x", (L'.TFfi ("Basis", "char"), loc),
adamc@821 1213 (L'.TFun ((L'.TFfi ("Basis", "char"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@821 1214 (L'.EAbs ("y", (L'.TFfi ("Basis", "char"), loc),
adamc@821 1215 (L'.TFfi ("Basis", "bool"), loc),
adam@1360 1216 (L'.EBinop (L'.NotInt, s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@821 1217 in
adamc@821 1218 ordEx ((L'.TFfi ("Basis", "char"), loc),
adamc@821 1219 charBin "<",
adamc@821 1220 charBin "<=")
adamc@821 1221 end
adamc@437 1222 | L.EFfi ("Basis", "ord_time") =>
adamc@437 1223 let
adamc@437 1224 fun boolBin s =
adamc@437 1225 (L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc),
adamc@437 1226 (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@437 1227 (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc),
adamc@437 1228 (L'.TFfi ("Basis", "bool"), loc),
adam@1663 1229 (L'.EFfiApp ("Basis", s, [((L'.ERel 1, loc), (L'.TFfi ("Basis", "time"), loc)),
adam@1663 1230 ((L'.ERel 0, loc), (L'.TFfi ("Basis", "time"), loc))]), loc)), loc)), loc)
adamc@437 1231 in
adamc@437 1232 ordEx ((L'.TFfi ("Basis", "time"), loc),
adam@1365 1233 boolBin "lt_time",
adam@1365 1234 boolBin "le_time")
adamc@437 1235 end
adamc@961 1236 | L.ECApp ((L.EFfi ("Basis", "mkOrd"), _), t) =>
adamc@961 1237 let
adamc@961 1238 val t = monoType env t
adamc@961 1239 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@961 1240 val dom = ordTy t
adamc@961 1241 in
adamc@961 1242 ((L'.EAbs ("f", dom, dom,
adamc@961 1243 (L'.ERel 0, loc)), loc), fm)
adamc@961 1244 end
adam@1682 1245
adamc@286 1246 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
adamc@286 1247 let
adamc@286 1248 val t = monoType env t
adamc@286 1249 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 1250 in
adamc@286 1251 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@286 1252 (L'.ERel 0, loc)), loc), fm)
adamc@286 1253 end
adamc@286 1254 | L.EFfi ("Basis", "show_int") =>
adamc@286 1255 ((L'.EFfi ("Basis", "intToString"), loc), fm)
adamc@286 1256 | L.EFfi ("Basis", "show_float") =>
adamc@286 1257 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
adamc@286 1258 | L.EFfi ("Basis", "show_string") =>
adamc@286 1259 let
adamc@286 1260 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 1261 in
adamc@286 1262 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@286 1263 end
adam@1370 1264 | L.EFfi ("Basis", "show_queryString") =>
adam@1370 1265 let
adam@1370 1266 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1370 1267 in
adam@1370 1268 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adam@1370 1269 end
adamc@1065 1270 | L.EFfi ("Basis", "show_url") =>
adamc@1065 1271 let
adamc@1065 1272 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@1065 1273 in
adamc@1065 1274 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@1065 1275 end
adam@1477 1276 | L.EFfi ("Basis", "show_css_class") =>
adam@1477 1277 let
adam@1477 1278 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1477 1279 in
adam@1477 1280 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adam@1477 1281 end
grrwlf@1929 1282 | L.EFfi ("Basis", "show_id") =>
grrwlf@1929 1283 let
grrwlf@1929 1284 val s = (L'.TFfi ("Basis", "string"), loc)
grrwlf@1929 1285 in
grrwlf@1929 1286 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
grrwlf@1929 1287 end
adamc@821 1288 | L.EFfi ("Basis", "show_char") =>
adamc@821 1289 ((L'.EFfi ("Basis", "charToString"), loc), fm)
adamc@286 1290 | L.EFfi ("Basis", "show_bool") =>
adamc@286 1291 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
adamc@436 1292 | L.EFfi ("Basis", "show_time") =>
adamc@436 1293 ((L'.EFfi ("Basis", "timeToString"), loc), fm)
adamc@727 1294 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "show_xml"), _), _),_), _), _), _) =>
adamc@727 1295 let
adamc@727 1296 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@727 1297 in
adamc@727 1298 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@727 1299 end
adam@1810 1300 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "show_sql_query"), _), _), _), _), _), _), _), _) =>
adam@1810 1301 let
adam@1810 1302 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1810 1303 in
adam@1810 1304 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adam@1810 1305 end
adamc@452 1306 | L.ECApp ((L.EFfi ("Basis", "mkShow"), _), t) =>
adamc@452 1307 let
adamc@452 1308 val t = monoType env t
adamc@452 1309 val b = (L'.TFfi ("Basis", "string"), loc)
adamc@452 1310 val dom = (L'.TFun (t, b), loc)
adamc@452 1311 in
adamc@452 1312 ((L'.EAbs ("f", dom, dom,
adamc@452 1313 (L'.ERel 0, loc)), loc), fm)
adamc@452 1314 end
adamc@286 1315
adamc@290 1316 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
adamc@290 1317 let
adamc@290 1318 val t = monoType env t
adamc@290 1319 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 1320 in
adamc@292 1321 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
adamc@292 1322 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
adamc@292 1323 end
adamc@292 1324 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
adamc@292 1325 let
adamc@292 1326 val t = monoType env t
adamc@292 1327 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@292 1328 in
adamc@292 1329 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
adamc@292 1330 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
adamc@290 1331 end
adamc@777 1332 | L.ECApp ((L.EFfi ("Basis", "mkRead"), _), t) =>
adamc@777 1333 let
adamc@777 1334 val t = monoType env t
adamc@777 1335 val b = (L'.TFfi ("Basis", "string"), loc)
adamc@777 1336 val b' = (L'.TOption b, loc)
adamc@777 1337 val dom = (L'.TFun (t, b), loc)
adamc@777 1338 val dom' = (L'.TFun (t, b'), loc)
adamc@777 1339 in
adamc@777 1340 ((L'.EAbs ("f", dom, (L'.TFun (dom', readType (t, loc)), loc),
adamc@777 1341 (L'.EAbs ("f'", dom', readType (t, loc),
adamc@777 1342 (L'.ERecord [("Read", (L'.ERel 0, loc), dom),
adamc@777 1343 ("ReadError", (L'.ERel 1, loc), dom')], loc)), loc)), loc),
adamc@777 1344 fm)
adamc@777 1345 end
adamc@290 1346 | L.EFfi ("Basis", "read_int") =>
adamc@292 1347 let
adamc@292 1348 val t = (L'.TFfi ("Basis", "int"), loc)
adamc@292 1349 in
adamc@292 1350 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
adamc@292 1351 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
adamc@292 1352 loc),
adamc@292 1353 fm)
adamc@292 1354 end
adamc@290 1355 | L.EFfi ("Basis", "read_float") =>
adamc@292 1356 let
adamc@292 1357 val t = (L'.TFfi ("Basis", "float"), loc)
adamc@292 1358 in
adamc@292 1359 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
adamc@292 1360 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
adamc@292 1361 loc),
adamc@292 1362 fm)
adamc@292 1363 end
adamc@290 1364 | L.EFfi ("Basis", "read_string") =>
adamc@290 1365 let
adamc@290 1366 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 1367 in
adamc@292 1368 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
adamc@292 1369 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
adamc@292 1370 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
adamc@292 1371 fm)
adamc@290 1372 end
adamc@821 1373 | L.EFfi ("Basis", "read_char") =>
adamc@821 1374 let
adamc@821 1375 val t = (L'.TFfi ("Basis", "char"), loc)
adamc@821 1376 in
adamc@821 1377 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToChar"), loc), readType' (t, loc)),
adamc@821 1378 ("ReadError", (L'.EFfi ("Basis", "stringToChar_error"), loc), readErrType (t, loc))],
adamc@821 1379 loc),
adamc@821 1380 fm)
adamc@821 1381 end
adamc@290 1382 | L.EFfi ("Basis", "read_bool") =>
adamc@292 1383 let
adamc@292 1384 val t = (L'.TFfi ("Basis", "bool"), loc)
adamc@292 1385 in
adamc@292 1386 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
adamc@292 1387 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
adamc@292 1388 loc),
adamc@292 1389 fm)
adamc@292 1390 end
adamc@436 1391 | L.EFfi ("Basis", "read_time") =>
adamc@436 1392 let
adamc@436 1393 val t = (L'.TFfi ("Basis", "time"), loc)
adamc@436 1394 in
adamc@436 1395 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToTime"), loc), readType' (t, loc)),
adamc@436 1396 ("ReadError", (L'.EFfi ("Basis", "stringToTime_error"), loc), readErrType (t, loc))],
adamc@436 1397 loc),
adamc@436 1398 fm)
adamc@436 1399 end
adamc@290 1400
adam@1544 1401 | L.ECApp ((L.EFfi ("Basis", "transaction_return"), _), t) =>
adamc@252 1402 let
adamc@252 1403 val t = monoType env t
adamc@252 1404 in
adam@1544 1405 ((L'.EAbs ("x", t,
adamc@820 1406 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@820 1407 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adam@1544 1408 (L'.ERel 1, loc)), loc)), loc),
adamc@820 1409 fm)
adamc@252 1410 end
adam@1544 1411 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "transaction_bind"), _), t1), _), t2) =>
adamc@251 1412 let
adamc@251 1413 val t1 = monoType env t1
adamc@251 1414 val t2 = monoType env t2
adamc@251 1415 val un = (L'.TRecord [], loc)
adamc@252 1416 val mt1 = (L'.TFun (un, t1), loc)
adamc@252 1417 val mt2 = (L'.TFun (un, t2), loc)
adamc@251 1418 in
adam@1544 1419 ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc)), loc),
adam@1544 1420 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc),
adam@1544 1421 (L'.EAbs ("_", un, un,
adam@1544 1422 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
adam@1544 1423 (L'.ERecord [], loc)), loc),
adam@1544 1424 (L'.EApp (
adam@1544 1425 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
adam@1544 1426 (L'.ERecord [], loc)),
adam@1544 1427 loc)), loc)), loc)), loc)), loc),
adamc@251 1428 fm)
adamc@251 1429 end
adamc@697 1430
adamc@1021 1431 | L.EApp ((L.ECApp ((L.EFfi ("Basis", "recv"), _), t1), _), ch) =>
adamc@670 1432 let
adamc@1021 1433 val un = (L'.TRecord [], loc)
adamc@670 1434 val t1 = monoType env t1
adamc@670 1435 val (ch, fm) = monoExp (env, st, fm) ch
adamc@670 1436 in
adamc@1021 1437 ((L'.EAbs ("_", un, un, (L'.ERecv (liftExpInExp 0 ch, t1), loc)), loc), fm)
adamc@670 1438 end
adamc@697 1439 | L.EFfiApp ("Basis", "recv", _) => poly ()
adamc@697 1440
adam@1663 1441 | L.EFfiApp ("Basis", "float", [(e, t)]) =>
adam@1571 1442 let
adam@1571 1443 val (e, fm) = monoExp (env, st, fm) e
adam@1571 1444 in
adam@1663 1445 ((L'.EFfiApp ("Basis", "floatFromInt", [(e, monoType env t)]), loc), fm)
adam@1571 1446 end
adam@1571 1447
adam@1663 1448 | L.EFfiApp ("Basis", "sleep", [(n, _)]) =>
adamc@695 1449 let
adamc@695 1450 val (n, fm) = monoExp (env, st, fm) n
adamc@695 1451 in
adamc@1021 1452 ((L'.ESleep n, loc), fm)
adamc@695 1453 end
adamc@697 1454 | L.EFfiApp ("Basis", "sleep", _) => poly ()
adamc@251 1455
adamc@565 1456 | L.ECApp ((L.EFfi ("Basis", "source"), _), t) =>
adamc@565 1457 let
adamc@565 1458 val t = monoType env t
adamc@565 1459 in
adamc@577 1460 ((L'.EAbs ("x", t, (L'.TFun ((L'.TRecord [], loc), (L'.TSource, loc)), loc),
adamc@577 1461 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TSource, loc),
adamc@577 1462 (L'.EFfiApp ("Basis", "new_client_source",
adam@1663 1463 [((L'.EJavaScript (L'.Source t, (L'.ERel 1, loc)), loc),
adam@1663 1464 (L'.TSource, loc))]),
adamc@578 1465 loc)), loc)),
adamc@565 1466 loc),
adamc@565 1467 fm)
adamc@565 1468 end
adamc@575 1469 | L.ECApp ((L.EFfi ("Basis", "set"), _), t) =>
adamc@575 1470 let
adamc@575 1471 val t = monoType env t
adamc@575 1472 in
adamc@577 1473 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@575 1474 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@575 1475 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@575 1476 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@575 1477 (L'.EFfiApp ("Basis", "set_client_source",
adam@1663 1478 [((L'.ERel 2, loc), (L'.TSource, loc)),
adam@1663 1479 ((L'.EJavaScript (L'.Source t,
adam@1663 1480 (L'.ERel 1, loc)), loc),
adam@1664 1481 (L'.TFfi ("Basis", "string"), loc))]),
adamc@575 1482 loc)), loc)), loc)), loc),
adamc@575 1483 fm)
adamc@575 1484 end
adamc@601 1485 | L.ECApp ((L.EFfi ("Basis", "get"), _), t) =>
adamc@601 1486 let
adamc@601 1487 val t = monoType env t
adamc@601 1488 in
adamc@601 1489 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@601 1490 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@601 1491 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@601 1492 (L'.EFfiApp ("Basis", "get_client_source",
adam@1663 1493 [((L'.ERel 1, loc), (L'.TSource, loc))]),
adamc@601 1494 loc)), loc)), loc),
adamc@601 1495 fm)
adamc@601 1496 end
adamc@841 1497 | L.ECApp ((L.EFfi ("Basis", "current"), _), t) =>
adamc@841 1498 let
adamc@841 1499 val t = monoType env t
adamc@841 1500 in
adamc@841 1501 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@841 1502 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@841 1503 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@841 1504 (L'.EFfiApp ("Basis", "current",
adam@1663 1505 [((L'.ERel 1, loc), (L'.TSource, loc))]),
adamc@841 1506 loc)), loc)), loc),
adamc@841 1507 fm)
adamc@841 1508 end
adamc@565 1509
adam@1663 1510 | L.EFfiApp ("Basis", "spawn", [(e, _)]) =>
adamc@694 1511 let
adamc@694 1512 val (e, fm) = monoExp (env, st, fm) e
adamc@694 1513 in
adamc@1021 1514 ((L'.ESpawn e, loc), fm)
adamc@694 1515 end
adamc@694 1516
adam@1544 1517 | L.ECApp ((L.EFfi ("Basis", "signal_return"), _), t) =>
adamc@568 1518 let
adamc@568 1519 val t = monoType env t
adamc@568 1520 in
adam@1544 1521 ((L'.EAbs ("x", t, (L'.TSignal t, loc),
adam@1544 1522 (L'.ESignalReturn (L'.ERel 0, loc), loc)), loc),
adamc@568 1523 fm)
adamc@568 1524 end
adam@1544 1525 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "signal_bind"), _), t1), _), t2) =>
adamc@572 1526 let
adamc@572 1527 val t1 = monoType env t1
adamc@572 1528 val t2 = monoType env t2
adamc@572 1529 val un = (L'.TRecord [], loc)
adamc@572 1530 val mt1 = (L'.TSignal t1, loc)
adamc@572 1531 val mt2 = (L'.TSignal t2, loc)
adamc@572 1532 in
adam@1544 1533 ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), mt2), loc),
adam@1544 1534 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), mt2,
adam@1544 1535 (L'.ESignalBind ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@572 1536 fm)
adamc@572 1537 end
adamc@574 1538 | L.ECApp ((L.EFfi ("Basis", "signal"), _), t) =>
adamc@574 1539 let
adamc@574 1540 val t = monoType env t
adamc@574 1541 in
adamc@574 1542 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TSignal t, loc),
adamc@574 1543 (L'.ESignalSource (L'.ERel 0, loc), loc)), loc),
adamc@574 1544 fm)
adamc@574 1545 end
adamc@568 1546
adamc@462 1547 | L.ECApp ((L.EFfi ("Basis", "getCookie"), _), t) =>
adamc@462 1548 let
adamc@462 1549 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1550 val un = (L'.TRecord [], loc)
adamc@462 1551 val t = monoType env t
adamc@462 1552 in
adamc@462 1553 ((L'.EAbs ("c", s, (L'.TFun (un, s), loc),
adamc@462 1554 (L'.EAbs ("_", un, s,
adam@1663 1555 (L'.EUnurlify ((L'.EFfiApp ("Basis", "get_cookie", [((L'.ERel 1, loc), s)]), loc),
adamc@1112 1556 t, true),
adamc@463 1557 loc)), loc)), loc),
adamc@462 1558 fm)
adamc@462 1559 end
adamc@462 1560
adamc@462 1561 | L.ECApp ((L.EFfi ("Basis", "setCookie"), _), t) =>
adamc@462 1562 let
adamc@462 1563 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1564 val un = (L'.TRecord [], loc)
adamc@462 1565 val t = monoType env t
adamc@1050 1566 val rt = (L'.TRecord [("Value", t),
adamc@1050 1567 ("Expires", (L'.TOption (L'.TFfi ("Basis", "time"),
adamc@1050 1568 loc), loc)),
adamc@1050 1569 ("Secure", (L'.TFfi ("Basis", "bool"), loc))], loc)
adamc@1050 1570
adamc@1050 1571 fun fd x = (L'.EField ((L'.ERel 1, loc), x), loc)
adamc@1050 1572 val (e, fm) = urlifyExp env fm (fd "Value", t)
adamc@462 1573 in
adamc@1050 1574 ((L'.EAbs ("c", s, (L'.TFun (rt, (L'.TFun (un, un), loc)), loc),
adamc@1050 1575 (L'.EAbs ("r", rt, (L'.TFun (un, un), loc),
adamc@462 1576 (L'.EAbs ("_", un, un,
adam@2048 1577 (L'.EFfiApp ("Basis", "set_cookie", [(str (Settings.getUrlPrefix ()), s),
adam@1663 1578 ((L'.ERel 2, loc), s),
adam@1663 1579 (e, s),
adam@1663 1580 (fd "Expires", (L'.TOption (L'.TFfi ("Basis", "time"), loc), loc)),
adam@1663 1581 (fd "Secure", (L'.TFfi ("Basis", "bool"), loc))])
adamc@1050 1582 , loc)), loc)), loc)), loc),
adamc@1050 1583 fm)
adamc@1050 1584 end
adamc@1050 1585
adamc@1050 1586 | L.ECApp ((L.EFfi ("Basis", "clearCookie"), _), t) =>
adamc@1050 1587 let
adamc@1050 1588 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@1050 1589 val un = (L'.TRecord [], loc)
adamc@1050 1590 in
adamc@1050 1591 ((L'.EAbs ("c", s, (L'.TFun (un, un), loc),
adamc@1050 1592 (L'.EAbs ("_", un, un,
adamc@1050 1593 (L'.EFfiApp ("Basis", "clear_cookie",
adam@2048 1594 [(str (Settings.getUrlPrefix ()), s),
adam@1663 1595 ((L'.ERel 1, loc), s)]),
adamc@462 1596 loc)), loc)), loc),
adamc@462 1597 fm)
adamc@1050 1598 end
adamc@462 1599
adamc@668 1600 | L.ECApp ((L.EFfi ("Basis", "channel"), _), t) =>
adamc@668 1601 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "channel"), loc),
adam@1663 1602 (L'.EFfiApp ("Basis", "new_channel", [((L'.ERecord [], loc), (L'.TRecord [], loc))]), loc)), loc),
adamc@668 1603 fm)
adamc@668 1604 | L.ECApp ((L.EFfi ("Basis", "send"), _), t) =>
adamc@668 1605 let
adamc@668 1606 val t = monoType env t
adamc@668 1607 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@668 1608 in
adamc@668 1609 ((L'.EAbs ("ch", (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1610 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@668 1611 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@668 1612 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@668 1613 (L'.EFfiApp ("Basis", "send",
adam@1663 1614 [((L'.ERel 2, loc), (L'.TFfi ("Basis", "channel"), loc)),
adam@1663 1615 (e, (L'.TFfi ("Basis", "string"), loc))]),
adamc@668 1616 loc)), loc)), loc)), loc),
adamc@668 1617 fm)
adamc@668 1618 end
adamc@668 1619
adamc@707 1620 | L.ECApp ((L.EFfi ("Basis", "no_primary_key"), _), _) =>
adam@2048 1621 (str "", fm)
adamc@707 1622 | L.ECApp (
adamc@707 1623 (L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "primary_key"), _), _), _), t), _),
adamc@707 1624 nm), _),
adamc@707 1625 (L.CRecord (_, unique), _)) =>
adamc@707 1626 let
adamc@707 1627 val unique = (nm, t) :: unique
adamc@707 1628 val witnesses = (L'.TRecord (map (fn (nm, _) => (monoName env nm, (L'.TRecord [], loc))) unique), loc)
adamc@707 1629 in
adamc@707 1630 ((L'.EAbs ("_", witnesses, (L'.TFfi ("Basis", "string"), loc),
adam@2048 1631 (str
adam@2048 1632 (String.concatWith ", "
adam@2048 1633 (map (fn (x, _) =>
adam@2048 1634 Settings.mangleSql (monoNameLc env x)
adam@2048 1635 ^ (if #textKeysNeedLengths (Settings.currentDbms ())
adam@2048 1636 andalso isBlobby t then
adam@2048 1637 "(767)"
adam@2048 1638 else
adam@2048 1639 "")) unique)))),
adam@2048 1640 loc),
adamc@707 1641 fm)
adamc@707 1642 end
adamc@707 1643
adamc@704 1644 | L.ECApp ((L.EFfi ("Basis", "no_constraint"), _), _) =>
adamc@704 1645 ((L'.ERecord [], loc),
adamc@704 1646 fm)
adamc@705 1647 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "one_constraint"), _), _), _), _), _), (L.CName name, _)) =>
adamc@704 1648 ((L'.EAbs ("c",
adamc@704 1649 (L'.TFfi ("Basis", "string"), loc),
adamc@704 1650 (L'.TFfi ("Basis", "sql_constraints"), loc),
adamc@704 1651 (L'.ERecord [(name, (L'.ERel 0, loc), (L'.TFfi ("Basis", "string"), loc))], loc)), loc),
adamc@704 1652 fm)
adamc@705 1653 | L.ECApp (
adamc@705 1654 (L.ECApp (
adamc@705 1655 (L.ECApp (
adamc@705 1656 (L.EFfi ("Basis", "join_constraints"), _),
adamc@705 1657 _), _),
adamc@705 1658 _), _),
adamc@705 1659 _) =>
adamc@704 1660 let
adamc@704 1661 val constraints = (L'.TFfi ("Basis", "sql_constraints"), loc)
adamc@704 1662 in
adamc@704 1663 ((L'.EAbs ("cs1", constraints, (L'.TFun (constraints, constraints), loc),
adamc@704 1664 (L'.EAbs ("cs2", constraints, constraints,
adamc@704 1665 (L'.EStrcat ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@704 1666 fm)
adamc@704 1667 end
adamc@704 1668
adamc@705 1669 | L.ECApp (
adamc@705 1670 (L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "unique"), _), _), _), t), _),
adamc@705 1671 nm), _),
adamc@705 1672 (L.CRecord (_, unique), _)) =>
adamc@705 1673 let
adamc@705 1674 val unique = (nm, t) :: unique
adamc@705 1675 in
adam@2048 1676 (str ("UNIQUE ("
adam@2048 1677 ^ String.concatWith ", "
adam@2048 1678 (map (fn (x, t) => Settings.mangleSql (monoNameLc env x)
adam@2048 1679 ^ (if #textKeysNeedLengths (Settings.currentDbms ())
adam@2048 1680 andalso isBlobby t then
adam@2048 1681 "(767)"
adam@2048 1682 else
adam@2048 1683 "")) unique)
adam@2048 1684 ^ ")"),
adamc@705 1685 fm)
adamc@705 1686 end
adamc@704 1687
adamc@712 1688 | L.ECApp ((L.EFfi ("Basis", "linkable_same"), loc), _) =>
adamc@712 1689 ((L'.ERecord [], loc), fm)
adamc@712 1690 | L.ECApp ((L.EFfi ("Basis", "linkable_from_nullable"), loc), _) =>
adamc@712 1691 ((L'.ERecord [], loc), fm)
adamc@712 1692 | L.ECApp ((L.EFfi ("Basis", "linkable_to_nullable"), loc), _) =>
adamc@712 1693 ((L'.ERecord [], loc), fm)
adamc@712 1694
adamc@709 1695 | L.EFfi ("Basis", "mat_nil") =>
adamc@709 1696 let
adamc@709 1697 val string = (L'.TFfi ("Basis", "string"), loc)
adam@2048 1698 val stringE = str ""
adamc@709 1699 in
adamc@709 1700 ((L'.ERecord [("1", stringE, string),
adamc@709 1701 ("2", stringE, string)], loc), fm)
adamc@709 1702 end
adamc@709 1703 | L.ECApp (
adamc@709 1704 (L.ECApp (
adamc@709 1705 (L.ECApp (
adamc@709 1706 (L.ECApp (
adamc@709 1707 (L.ECApp (
adamc@712 1708 (L.ECApp (
adamc@712 1709 (L.EFfi ("Basis", "mat_cons"), _),
adamc@712 1710 _), _),
adamc@709 1711 _), _),
adamc@709 1712 _), _),
adamc@709 1713 _), _),
adamc@709 1714 (L.CName nm1, _)), _),
adamc@709 1715 (L.CName nm2, _)) =>
adamc@709 1716 let
adamc@709 1717 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1718 val mat = (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 1719 in
adamc@712 1720 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun (mat, mat), loc),
adamc@712 1721 (L'.EAbs ("m", mat, mat,
adamc@712 1722 (L'.ECase ((L'.EField ((L'.ERel 0, loc), "1"), loc),
adam@2048 1723 [((L'.PPrim (Prim.String (Prim.Normal, "")), loc),
adam@2048 1724 (L'.ERecord [("1", str (Settings.mangleSql (lowercaseFirst nm1)),
adam@2048 1725 string),
adam@2048 1726 ("2", str (Settings.mangleSql (lowercaseFirst nm2)),
adam@2048 1727 string)], loc)),
adamc@712 1728 ((L'.PWild, loc),
adamc@712 1729 (L'.ERecord [("1", (L'.EStrcat (
adam@2048 1730 str (Settings.mangleSql (lowercaseFirst nm1)
adam@2048 1731 ^ ", "),
adamc@712 1732 (L'.EField ((L'.ERel 0, loc), "1"), loc)),
adamc@712 1733 loc), string),
adamc@712 1734 ("2", (L'.EStrcat (
adam@2048 1735 str (Settings.mangleSql (lowercaseFirst nm2)
adam@2048 1736 ^ ", "),
adamc@712 1737 (L'.EField ((L'.ERel 0, loc), "2"), loc)),
adamc@712 1738 loc), string)],
adamc@712 1739 loc))],
adamc@712 1740 {disc = string,
adamc@712 1741 result = mat}), loc)), loc)), loc),
adamc@709 1742 fm)
adamc@709 1743 end
adamc@709 1744
adam@2048 1745 | L.ECApp ((L.EFfi ("Basis", "restrict"), _), _) => (str "RESTRICT", fm)
adam@2048 1746 | L.ECApp ((L.EFfi ("Basis", "cascade"), _), _) => (str "CASCADE", fm)
adam@2048 1747 | L.ECApp ((L.EFfi ("Basis", "no_action"), _), _) => (str "NO ACTION", fm)
adam@2048 1748 | L.ECApp ((L.EFfi ("Basis", "set_null"), _), _) => (str "SET NULL", fm)
adamc@709 1749
adamc@709 1750 | L.ECApp (
adamc@709 1751 (L.ECApp (
adamc@709 1752 (L.ECApp (
adamc@709 1753 (L.ECApp (
adamc@709 1754 (L.ECApp (
adamc@709 1755 (L.ECApp (
adamc@709 1756 (L.ECApp (
adamc@709 1757 (L.ECApp (
adamc@709 1758 (L.EFfi ("Basis", "foreign_key"), _),
adamc@709 1759 _), _),
adamc@709 1760 _), _),
adamc@709 1761 _), _),
adamc@709 1762 _), _),
adamc@709 1763 _), _),
adamc@709 1764 _), _),
adamc@709 1765 _), _),
adamc@709 1766 _) =>
adamc@709 1767 let
adamc@709 1768 val unit = (L'.TRecord [], loc)
adamc@709 1769 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1770 val mat = (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 1771 val recd = (L'.TRecord [("OnDelete", string),
adamc@709 1772 ("OnUpdate", string)], loc)
adamc@709 1773
adamc@709 1774 fun strcat [] = raise Fail "Monoize.strcat"
adamc@709 1775 | strcat [e] = e
adamc@709 1776 | strcat (e1 :: es) = (L'.EStrcat (e1, strcat es), loc)
adamc@709 1777
adamc@709 1778 fun prop (fd, kw) =
adamc@709 1779 (L'.ECase ((L'.EField ((L'.ERel 0, loc), fd), loc),
adam@2048 1780 [((L'.PPrim (Prim.String (Prim.Normal, "NO ACTION")), loc),
adam@2048 1781 str ""),
adamc@709 1782 ((L'.PWild, loc),
adam@2048 1783 strcat [str (" ON " ^ kw ^ " "),
adamc@709 1784 (L'.EField ((L'.ERel 0, loc), fd), loc)])],
adamc@709 1785 {disc = string,
adamc@709 1786 result = string}), loc)
adamc@709 1787 in
adamc@709 1788 ((L'.EAbs ("m", mat, (L'.TFun (string, (L'.TFun (recd, string), loc)), loc),
adamc@709 1789 (L'.EAbs ("tab", string, (L'.TFun (recd, string), loc),
adamc@709 1790 (L'.EAbs ("pr", recd, string,
adam@2048 1791 strcat [str "FOREIGN KEY (",
adamc@709 1792 (L'.EField ((L'.ERel 2, loc), "1"), loc),
adam@2048 1793 str ") REFERENCES ",
adamc@709 1794 (L'.ERel 1, loc),
adam@2048 1795 str " (",
adamc@709 1796 (L'.EField ((L'.ERel 2, loc), "2"), loc),
adam@2048 1797 str ")",
adamc@709 1798 prop ("OnDelete", "DELETE"),
adamc@709 1799 prop ("OnUpdate", "UPDATE")]), loc)), loc)), loc),
adamc@709 1800 fm)
adamc@709 1801 end
adamc@709 1802
adamc@1072 1803 | L.ECApp (
adamc@1072 1804 (L.ECApp (
adamc@1072 1805 (L.ECApp (
adamc@1072 1806 (L.ECApp (
adamc@1072 1807 (L.ECApp (
adamc@1072 1808 (L.ECApp (
adamc@1072 1809 (L.ECApp (
adamc@1072 1810 (L.EFfi ("Basis", "sql_exp_weaken"), _),
adamc@1072 1811 _), _),
adamc@1072 1812 _), _),
adamc@1072 1813 _), _),
adamc@1072 1814 _), _),
adamc@1072 1815 _), _),
adamc@1072 1816 _), _),
adamc@1072 1817 _) =>
adamc@1072 1818 let
adamc@1072 1819 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@1072 1820 in
adamc@1072 1821 ((L'.EAbs ("e", string, string, (L'.ERel 0, loc)), loc),
adamc@1072 1822 fm)
adamc@1072 1823 end
adamc@1072 1824
adamc@714 1825 | L.ECApp ((L.EFfi ("Basis", "check"), _), _) =>
adamc@714 1826 let
adamc@714 1827 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@714 1828 in
adamc@714 1829 ((L'.EAbs ("e", string, string,
adam@2048 1830 (L'.EStrcat (str "CHECK ",
adamc@714 1831 (L'.EFfiApp ("Basis", "checkString",
adam@1663 1832 [((L'.ERel 0, loc), string)]), loc)), loc)), loc),
adamc@714 1833 fm)
adamc@714 1834 end
adamc@714 1835
adam@1663 1836 | L.EFfiApp ("Basis", "dml", [(e, _)]) =>
adamc@307 1837 let
adamc@307 1838 val (e, fm) = monoExp (env, st, fm) e
adamc@307 1839 in
adam@1293 1840 ((L'.EDml (e, L'.Error), loc),
adam@1293 1841 fm)
adam@1293 1842 end
adam@1293 1843
adam@1663 1844 | L.EFfiApp ("Basis", "tryDml", [(e, _)]) =>
adam@1293 1845 let
adam@1293 1846 val (e, fm) = monoExp (env, st, fm) e
adam@1293 1847 in
adam@1293 1848 ((L'.EDml (e, L'.None), loc),
adamc@307 1849 fm)
adamc@307 1850 end
adamc@308 1851
adamc@705 1852 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "insert"), _), fields), _), _) =>
adamc@307 1853 (case monoType env (L.TRecord fields, loc) of
adamc@307 1854 (L'.TRecord fields, _) =>
adamc@307 1855 let
adamc@307 1856 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@307 1857 val fields = map (fn (x, _) => (x, s)) fields
adamc@307 1858 val rt = (L'.TRecord fields, loc)
adamc@307 1859 in
adamc@307 1860 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
adamc@307 1861 (L'.EAbs ("fs", rt, s,
adam@2048 1862 strcat [str "INSERT INTO ",
adamc@598 1863 (L'.ERel 1, loc),
adam@2048 1864 str " (",
adam@2048 1865 strcatComma (map (fn (x, _) => str (Settings.mangleSql x)) fields),
adam@2048 1866 str ") VALUES (",
adamc@598 1867 strcatComma (map (fn (x, _) =>
adamc@598 1868 (L'.EField ((L'.ERel 0, loc),
adamc@598 1869 x), loc)) fields),
adam@2048 1870 str ")"]), loc)), loc),
adamc@307 1871 fm)
adamc@307 1872 end
adamc@307 1873 | _ => poly ())
adamc@307 1874
adamc@705 1875 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), _), _), changed) =>
adamc@308 1876 (case monoType env (L.TRecord changed, loc) of
adamc@308 1877 (L'.TRecord changed, _) =>
adamc@308 1878 let
adamc@308 1879 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@308 1880 val changed = map (fn (x, _) => (x, s)) changed
adamc@308 1881 val rt = (L'.TRecord changed, loc)
adamc@308 1882 in
adamc@308 1883 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@308 1884 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@308 1885 (L'.EAbs ("e", s, s,
adamc@886 1886 if #supportsUpdateAs (Settings.currentDbms ()) then
adam@2048 1887 strcat [str "UPDATE ",
adamc@886 1888 (L'.ERel 1, loc),
adam@2048 1889 str " AS T_T SET ",
adamc@886 1890 strcatComma (map (fn (x, _) =>
adam@2048 1891 strcat [str (Settings.mangleSql x
adamc@886 1892 ^ " = "),
adamc@886 1893 (L'.EField
adamc@886 1894 ((L'.ERel 2,
adamc@886 1895 loc),
adamc@886 1896 x), loc)])
adamc@886 1897 changed),
adam@2048 1898 str " WHERE ",
adamc@886 1899 (L'.ERel 0, loc)]
adamc@886 1900 else
adam@2048 1901 strcat [str "UPDATE ",
adamc@886 1902 (L'.ERel 1, loc),
adam@2048 1903 str " SET ",
adamc@886 1904 strcatComma (map (fn (x, _) =>
adam@2048 1905 strcat [str (Settings.mangleSql x
adamc@886 1906 ^ " = "),
adam@1466 1907 (L'.EFfiApp ("Basis", "unAs",
adam@1663 1908 [((L'.EField
adam@1663 1909 ((L'.ERel 2,
adam@1663 1910 loc),
adam@1663 1911 x), loc),
adam@1663 1912 s)]), loc)])
adamc@886 1913 changed),
adam@2048 1914 str " WHERE ",
adam@1663 1915 (L'.EFfiApp ("Basis", "unAs", [((L'.ERel 0, loc), s)]), loc)]),
adamc@886 1916 loc)), loc)), loc),
adamc@308 1917 fm)
adamc@308 1918 end
adamc@308 1919 | _ => poly ())
adamc@308 1920
adamc@705 1921 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "delete"), _), _), _), _) =>
adamc@309 1922 let
adamc@309 1923 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@309 1924 in
adamc@309 1925 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@309 1926 (L'.EAbs ("e", s, s,
adamc@874 1927 if #supportsDeleteAs (Settings.currentDbms ()) then
adam@2048 1928 strcat [str "DELETE FROM ",
adamc@874 1929 (L'.ERel 1, loc),
adam@2048 1930 str " AS T_T WHERE ",
adamc@874 1931 (L'.ERel 0, loc)]
adamc@874 1932 else
adam@2048 1933 strcat [str "DELETE FROM ",
adamc@874 1934 (L'.ERel 1, loc),
adam@2048 1935 str " WHERE ",
adam@1663 1936 (L'.EFfiApp ("Basis", "unAs", [((L'.ERel 0, loc), s)]), loc)]), loc)), loc),
adamc@309 1937 fm)
adamc@309 1938 end
adamc@309 1939
adamc@252 1940 | L.ECApp (
adamc@252 1941 (L.ECApp (
adamc@252 1942 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 1943 exps), _),
adamc@252 1944 state) =>
adamc@252 1945 (case monoType env (L.TRecord exps, loc) of
adamc@252 1946 (L'.TRecord exps, _) =>
adamc@252 1947 let
adamc@252 1948 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1949 (case monoType env (L.TRecord xts, loc) of
adamc@252 1950 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1951 | _ => NONE)
adamc@252 1952 | _ => NONE) tables
adamc@252 1953 in
adamc@252 1954 if List.exists (fn x => x = NONE) tables then
adamc@252 1955 poly ()
adamc@252 1956 else
adamc@252 1957 let
adamc@252 1958 val tables = List.mapPartial (fn x => x) tables
adamc@252 1959 val state = monoType env state
adamc@252 1960 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1961 val un = (L'.TRecord [], loc)
adamc@252 1962
adamc@252 1963 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 1964 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 1965 (L'.TFun (state,
adamc@252 1966 (L'.TFun (un, state), loc)),
adamc@252 1967 loc)), loc)
adamc@252 1968
adamc@267 1969 val body' = (L'.EApp (
adamc@267 1970 (L'.EApp (
adamc@267 1971 (L'.EApp ((L'.ERel 4, loc),
adamc@267 1972 (L'.ERel 1, loc)), loc),
adamc@267 1973 (L'.ERel 0, loc)), loc),
adamc@267 1974 (L'.ERecord [], loc)), loc)
ziv@2221 1975 val (urlifiedRel0, fm) = urlifyExp env fm ((L'.ERel 0, loc), state)
adamc@252 1976 val body = (L'.EQuery {exps = exps,
adamc@252 1977 tables = tables,
adamc@252 1978 state = state,
adamc@252 1979 query = (L'.ERel 3, loc),
adamc@252 1980 body = body',
ziv@2221 1981 initial = (L'.ERel 1, loc),
ziv@2221 1982 sqlcacheInfo = urlifiedRel0},
adamc@252 1983 loc)
adamc@252 1984 in
adamc@252 1985 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 1986 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 1987 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 1988 (L'.EAbs ("_", un, state,
adamc@252 1989 body), loc)), loc)), loc)), loc), fm)
adamc@252 1990 end
adamc@252 1991 end
adamc@252 1992 | _ => poly ())
adamc@252 1993
adam@1394 1994 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _), _), _), _), _) =>
adamc@252 1995 let
adamc@252 1996 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1997 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1998 in
adamc@252 1999 ((L'.EAbs ("r",
adamc@252 2000 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 2001 s,
adamc@598 2002 strcat [gf "Rows",
adamc@598 2003 (L'.ECase (gf "OrderBy",
adam@2048 2004 [((L'.PPrim (Prim.String (Prim.Normal, "")), loc), str ""),
adam@2084 2005 ((L'.PVar ("orderby", s), loc),
adam@2048 2006 strcat [str " ORDER BY ",
adam@2084 2007 (L'.ERel 0, loc)])],
adamc@598 2008 {disc = s, result = s}), loc),
adamc@598 2009 gf "Limit",
adamc@598 2010 gf "Offset"]), loc), fm)
adamc@252 2011 end
adamc@252 2012
adamc@252 2013 | L.ECApp (
adamc@252 2014 (L.ECApp (
adamc@252 2015 (L.ECApp (
adamc@252 2016 (L.ECApp (
adamc@1070 2017 (L.ECApp (
adamc@1191 2018 (L.ECApp (
adam@1394 2019 (L.ECApp (
adam@1394 2020 (L.EFfi ("Basis", "sql_query1"), _),
adam@1394 2021 _), _),
adamc@1191 2022 _), _),
adamc@1070 2023 (L.CRecord (_, tables), _)), _),
adamc@1070 2024 (L.CRecord (_, grouped), _)), _),
adamc@1070 2025 (L.CRecord (_, stables), _)), _),
adamc@1070 2026 sexps), _),
adamc@1070 2027 _) =>
adamc@252 2028 let
adamc@252 2029 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@993 2030 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@252 2031 val un = (L'.TRecord [], loc)
adamc@252 2032 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 2033
adamc@252 2034 fun doTables tables =
adamc@252 2035 let
adamc@252 2036 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 2037 (case monoType env (L.TRecord xts, loc) of
adamc@252 2038 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 2039 | _ => NONE)
adamc@252 2040 | _ => NONE) tables
adamc@252 2041 in
adamc@252 2042 if List.exists (fn x => x = NONE) tables then
adamc@252 2043 NONE
adamc@252 2044 else
adamc@260 2045 let
adamc@260 2046 val tables = List.mapPartial (fn x => x) tables
adamc@260 2047 val tables = ListMergeSort.sort
adamc@260 2048 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 2049 tables
adamc@260 2050 val tables = map (fn (x, xts) =>
adamc@260 2051 (x, ListMergeSort.sort
adamc@260 2052 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 2053 xts)) tables
adamc@260 2054 in
adamc@260 2055 SOME tables
adamc@260 2056 end
adamc@252 2057 end
adamc@252 2058 in
adamc@252 2059 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 2060 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@441 2061 let
adamc@441 2062 val sexps = ListMergeSort.sort
adamc@441 2063 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) sexps
adamc@441 2064 in
adamc@441 2065 ((L'.EAbs ("r",
adamc@993 2066 (L'.TRecord [("Distinct", b),
adamc@993 2067 ("From", s),
adamc@441 2068 ("Where", s),
adamc@441 2069 ("GroupBy", un),
adamc@441 2070 ("Having", s),
adamc@441 2071 ("SelectFields", un),
adamc@441 2072 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@441 2073 loc),
adamc@441 2074 s,
adam@2048 2075 strcat [str "SELECT ",
adamc@993 2076 (L'.ECase (gf "Distinct",
adamc@993 2077 [((L'.PCon (L'.Enum,
adamc@993 2078 L'.PConFfi {mod = "Basis",
adamc@993 2079 datatyp = "bool",
adamc@993 2080 con = "True",
adamc@993 2081 arg = NONE},
adamc@993 2082 NONE), loc),
adam@2048 2083 str "DISTINCT "),
adamc@993 2084 ((L'.PCon (L'.Enum,
adamc@993 2085 L'.PConFfi {mod = "Basis",
adamc@993 2086 datatyp = "bool",
adamc@993 2087 con = "False",
adamc@993 2088 arg = NONE},
adamc@993 2089 NONE), loc),
adam@2048 2090 str "")],
adamc@993 2091 {disc = b, result = s}), loc),
adamc@598 2092 strcatComma (map (fn (x, t) =>
adamc@598 2093 strcat [
adamc@598 2094 (L'.EField (gf "SelectExps", x), loc),
adam@2048 2095 str (" AS " ^ Settings.mangleSql x)
adamc@598 2096 ]) sexps
adamc@598 2097 @ map (fn (x, xts) =>
adamc@598 2098 strcatComma
adamc@598 2099 (map (fn (x', _) =>
adam@2048 2100 str ("T_" ^ x
adam@1953 2101 ^ "."
adam@1953 2102 ^ Settings.mangleSql x'))
adamc@598 2103 xts)) stables),
adamc@1195 2104 (L'.ECase (gf "From",
adam@2048 2105 [((L'.PPrim (Prim.String (Prim.Normal, "")), loc),
adam@2048 2106 str ""),
adamc@1195 2107 ((L'.PVar ("x", s), loc),
adam@2048 2108 strcat [str " FROM ",
adamc@1195 2109 (L'.ERel 0, loc)])],
adamc@1195 2110 {disc = s,
adamc@1195 2111 result = s}), loc),
adamc@598 2112 (L'.ECase (gf "Where",
adam@2048 2113 [((L'.PPrim (Prim.String (Prim.Normal, #trueString (Settings.currentDbms ()))),
adamc@1266 2114 loc),
adam@2048 2115 str ""),
adam@2084 2116 ((L'.PVar ("where", s), loc),
adam@2084 2117 strcat [str " WHERE ", (L'.ERel 0, loc)])],
adamc@598 2118 {disc = s,
adamc@598 2119 result = s}), loc),
adam@1682 2120
adamc@598 2121 if List.all (fn (x, xts) =>
adamc@598 2122 case List.find (fn (x', _) => x' = x) grouped of
adamc@598 2123 NONE => List.null xts
adamc@598 2124 | SOME (_, xts') =>
adamc@598 2125 List.all (fn (x, _) =>
adamc@598 2126 List.exists (fn (x', _) => x' = x)
adamc@598 2127 xts') xts) tables then
adam@2048 2128 str ""
adamc@598 2129 else
adamc@598 2130 strcat [
adam@2048 2131 str " GROUP BY ",
adamc@598 2132 strcatComma (map (fn (x, xts) =>
adamc@598 2133 strcatComma
adamc@598 2134 (map (fn (x', _) =>
adam@2048 2135 str ("T_" ^ x
adam@2004 2136 ^ "."
adam@1953 2137 ^ Settings.mangleSql x'))
adamc@598 2138 xts)) grouped)
adamc@598 2139 ],
adamc@259 2140
adamc@598 2141 (L'.ECase (gf "Having",
adamc@1014 2142 [((L'.PPrim (Prim.String
adam@2048 2143 (Prim.Normal, #trueString (Settings.currentDbms ()))), loc),
adam@2048 2144 str ""),
adam@2084 2145 ((L'.PVar ("having", s), loc),
adam@2084 2146 strcat [str " HAVING ", (L'.ERel 0, loc)])],
adamc@598 2147 {disc = s,
adamc@598 2148 result = s}), loc)
adamc@441 2149 ]), loc),
adamc@441 2150 fm)
adamc@441 2151 end
adamc@252 2152 | _ => poly ()
adamc@252 2153 end
adamc@252 2154
adamc@252 2155 | L.ECApp (
adamc@252 2156 (L.ECApp (
adamc@252 2157 (L.ECApp (
adamc@252 2158 (L.ECApp (
adam@1778 2159 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 2160 _), _),
adamc@252 2161 _), _),
adamc@252 2162 _), _),
adamc@252 2163 t) =>
adamc@252 2164 let
adamc@252 2165 val t = monoType env t
adamc@252 2166 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 2167 in
adamc@252 2168 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 2169 (L'.ERel 0, loc)), loc), fm)
adamc@252 2170 end
adamc@252 2171
adamc@253 2172 | L.EFfi ("Basis", "sql_int") =>
adamc@253 2173 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2174 (L'.EFfiApp ("Basis", "sqlifyInt", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "int"), loc))]), loc)), loc),
adamc@253 2175 fm)
adamc@253 2176 | L.EFfi ("Basis", "sql_float") =>
adamc@253 2177 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2178 (L'.EFfiApp ("Basis", "sqlifyFloat", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "float"), loc))]), loc)), loc),
adamc@253 2179 fm)
adamc@253 2180 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 2181 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2182 (L'.EFfiApp ("Basis", "sqlifyBool", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "bool"), loc))]), loc)), loc),
adamc@253 2183 fm)
adamc@253 2184 | L.EFfi ("Basis", "sql_string") =>
adamc@253 2185 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2186 (L'.EFfiApp ("Basis", "sqlifyString", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "string"), loc))]), loc)), loc),
adamc@253 2187 fm)
adamc@1011 2188 | L.EFfi ("Basis", "sql_char") =>
adamc@1011 2189 ((L'.EAbs ("x", (L'.TFfi ("Basis", "char"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2190 (L'.EFfiApp ("Basis", "sqlifyChar", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "char"), loc))]), loc)), loc),
adamc@1011 2191 fm)
adamc@439 2192 | L.EFfi ("Basis", "sql_time") =>
adamc@439 2193 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2194 (L'.EFfiApp ("Basis", "sqlifyTime", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "time"), loc))]), loc)), loc),
adamc@439 2195 fm)
adamc@737 2196 | L.EFfi ("Basis", "sql_blob") =>
adamc@737 2197 ((L'.EAbs ("x", (L'.TFfi ("Basis", "blob"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2198 (L'.EFfiApp ("Basis", "sqlifyBlob", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "blob"), loc))]), loc)), loc),
adamc@737 2199 fm)
adamc@678 2200 | L.ECApp ((L.EFfi ("Basis", "sql_channel"), _), _) =>
adamc@678 2201 ((L'.EAbs ("x", (L'.TFfi ("Basis", "channel"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2202 (L'.EFfiApp ("Basis", "sqlifyChannel", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "channel"), loc))]), loc)), loc),
adamc@678 2203 fm)
adamc@682 2204 | L.EFfi ("Basis", "sql_client") =>
adamc@682 2205 ((L'.EAbs ("x", (L'.TFfi ("Basis", "client"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2206 (L'.EFfiApp ("Basis", "sqlifyClient", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "client"), loc))]), loc)), loc),
adamc@682 2207 fm)
adamc@1104 2208 | L.ECApp ((L.EFfi ("Basis", "sql_serialized"), _), _) =>
adamc@1104 2209 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 2210 (L'.EFfiApp ("Basis", "sqlifyString", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "string"), loc))]), loc)), loc),
adamc@1104 2211 fm)
adam@2029 2212 | L.EFfi ("Basis", "sql_url") =>
adam@2029 2213 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@2029 2214 (L'.EFfiApp ("Basis", "sqlifyString", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "string"), loc))]), loc)), loc),
adam@2029 2215 fm)
adamc@676 2216 | L.ECApp ((L.EFfi ("Basis", "sql_prim"), _), t) =>
adamc@676 2217 let
adamc@676 2218 val t = monoType env t
adamc@676 2219 val tf = (L'.TFun (t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@676 2220 in
adamc@676 2221 ((L'.EAbs ("f", tf, tf, (L'.ERel 0, loc)), loc),
adamc@676 2222 fm)
adamc@676 2223 end
adamc@676 2224 | L.ECApp ((L.EFfi ("Basis", "sql_option_prim"), _), t) =>
adamc@676 2225 let
adamc@676 2226 val t = monoType env t
adamc@676 2227 val s = (L'.TFfi ("Basis", "string"), loc)
adam@2160 2228
adam@2160 2229 fun toSqlType (t : L'.typ) =
adam@2160 2230 case #1 t of
adam@2160 2231 L'.TFfi ("Basis", "int") => Settings.Int
adam@2160 2232 | L'.TFfi ("Basis", "float") => Settings.Float
adam@2160 2233 | L'.TFfi ("Basis", "string") => Settings.String
adam@2160 2234 | L'.TFfi ("Basis", "char") => Settings.Char
adam@2160 2235 | L'.TFfi ("Basis", "bool") => Settings.Bool
adam@2160 2236 | L'.TFfi ("Basis", "time") => Settings.Time
adam@2160 2237 | L'.TFfi ("Basis", "blob") => Settings.Blob
adam@2160 2238 | L'.TFfi ("Basis", "channel") => Settings.Channel
adam@2160 2239 | L'.TFfi ("Basis", "client") => Settings.Client
adam@2160 2240 | _ => raise Fail "Monoize/sql_option_prim: invalid SQL type"
adamc@676 2241 in
adamc@676 2242 ((L'.EAbs ("f",
adamc@676 2243 (L'.TFun (t, s), loc),
adamc@676 2244 (L'.TFun ((L'.TOption t, loc), s), loc),
adamc@676 2245 (L'.EAbs ("x",
adamc@676 2246 (L'.TOption t, loc),
adamc@676 2247 s,
adamc@676 2248 (L'.ECase ((L'.ERel 0, loc),
adamc@676 2249 [((L'.PNone t, loc),
adam@2160 2250 str (#p_cast (Settings.currentDbms ()) ("NULL", toSqlType t))),
adamc@676 2251 ((L'.PSome (t, (L'.PVar ("y", t), loc)), loc),
adamc@676 2252 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc))],
adamc@676 2253 {disc = (L'.TOption t, loc),
adamc@676 2254 result = s}), loc)), loc)), loc),
adamc@676 2255 fm)
adamc@676 2256 end
adamc@253 2257
adamc@750 2258 | L.ECApp ((L.EFfi ("Basis", "nullify_option"), _), _) =>
adamc@750 2259 ((L'.ERecord [], loc), fm)
adamc@750 2260 | L.ECApp ((L.EFfi ("Basis", "nullify_prim"), _), _) =>
adamc@750 2261 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@750 2262 (L'.ERecord [], loc)), loc),
adamc@750 2263 fm)
adamc@750 2264
adamc@252 2265 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 2266 ((L'.ERecord [], loc), fm)
adamc@252 2267 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 2268 ((L'.ERecord [], loc), fm)
adamc@1072 2269 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_subset_concat"),
adamc@1072 2270 _), _), _), _), _), _), _), _) =>
adamc@1072 2271 let
adam@1682 2272 val un = (L'.TRecord [], loc)
adamc@1072 2273 in
adamc@1072 2274 ((L'.EAbs ("_", un, (L'.TFun (un, un), loc),
adamc@1072 2275 (L'.EAbs ("_", un, un,
adamc@1072 2276 (L'.ERecord [], loc)), loc)), loc),
adamc@1072 2277 fm)
adamc@1072 2278 end
adamc@252 2279
adamc@753 2280 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "fieldsOf_table"), _), _), _), _) =>
adamc@753 2281 ((L'.ERecord [], loc), fm)
adamc@753 2282 | L.ECApp ((L.EFfi ("Basis", "fieldsOf_view"), _), _) =>
adamc@753 2283 ((L'.ERecord [], loc), fm)
adamc@753 2284
adamc@1195 2285 | L.ECApp ((L.EFfi ("Basis", "sql_from_nil"), _), _) =>
adam@2048 2286 (str "", fm)
adamc@1191 2287 | L.ECApp ((L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_table"), _), _),
adamc@1191 2288 _), _), _), _), _), _), _),
adamc@753 2289 (L.CName name, _)) =>
adamc@748 2290 let
adamc@748 2291 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@748 2292 in
adamc@748 2293 ((L'.EAbs ("tab", s, s,
adamc@748 2294 strcat [(L'.ERel 0, loc),
adam@2048 2295 str (" AS T_" ^ name)]), loc),
adamc@748 2296 fm)
adamc@748 2297 end
adamc@1192 2298 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_query"), _), _),
adamc@1192 2299 _), _), _),
adamc@1192 2300 (L.CName name, _)) =>
adamc@1192 2301 let
adamc@1192 2302 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@1192 2303 in
adamc@1192 2304 ((L'.EAbs ("q", s, s,
adam@2048 2305 strcat [str "(",
adamc@1192 2306 (L'.ERel 0, loc),
adam@2048 2307 str (") AS T_" ^ name)]), loc),
adamc@1192 2308 fm)
adamc@1192 2309 end
adamc@1191 2310 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_comma"), _), _), _), _), _), _) =>
adamc@748 2311 let
adamc@748 2312 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@748 2313 in
adamc@748 2314 ((L'.EAbs ("tab1", s, (L'.TFun (s, s), loc),
adamc@748 2315 (L'.EAbs ("tab2", s, s,
adamc@1195 2316 (L'.ECase ((L'.ERecord [("1", (L'.ERel 1, loc), s),
adamc@1195 2317 ("2", (L'.ERel 0, loc), s)], loc),
adam@2048 2318 [((L'.PRecord [("1", (L'.PPrim (Prim.String (Prim.Normal, "")), loc), s)], loc),
adamc@1195 2319 (L'.ERel 0, loc)),
adam@2048 2320 ((L'.PRecord [("2", (L'.PPrim (Prim.String (Prim.Normal, "")), loc), s)], loc),
adamc@1195 2321 (L'.ERel 1, loc)),
adamc@1195 2322 ((L'.PWild, loc),
adamc@1195 2323 strcat [(L'.ERel 1, loc),
adam@2048 2324 str ", ",
adamc@1195 2325 (L'.ERel 0, loc)])],
adamc@1195 2326 {disc = (L'.TRecord [("1", s), ("2", s)], loc),
adamc@1195 2327 result = s}), loc)), loc)), loc),
adamc@748 2328 fm)
adamc@748 2329 end
adamc@1191 2330 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_inner_join"), _), _), _), _), _), _) =>
adamc@749 2331 let
adamc@749 2332 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@749 2333 in
adamc@749 2334 ((L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@749 2335 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@749 2336 (L'.EAbs ("on", s, s,
adamc@1195 2337 (L'.ECase ((L'.ERecord [("1", (L'.ERel 2, loc), s),
adamc@1195 2338 ("2", (L'.ERel 1, loc), s)], loc),
adam@2048 2339 [((L'.PRecord [("1", (L'.PPrim (Prim.String (Prim.Normal, "")), loc), s)], loc),
adamc@1195 2340 (L'.ERel 1, loc)),
adam@2048 2341 ((L'.PRecord [("2", (L'.PPrim (Prim.String (Prim.Normal, "")), loc), s)], loc),
adamc@1195 2342 (L'.ERel 2, loc)),
adamc@1195 2343 ((L'.PWild, loc),
adamc@1266 2344 strcat ((if #nestedRelops
adamc@1266 2345 (Settings.currentDbms ()) then
adam@2048 2346 [str "("]
adamc@1266 2347 else
adamc@1266 2348 [])
adamc@1266 2349 @ [(L'.ERel 2, loc),
adam@2048 2350 str " JOIN ",
adamc@1266 2351 (L'.ERel 1, loc),
adam@2048 2352 str " ON ",
adamc@1266 2353 (L'.ERel 0, loc)]
adamc@1266 2354 @ (if #nestedRelops
adamc@1266 2355 (Settings.currentDbms ()) then
adam@2048 2356 [str ")"]
adamc@1266 2357 else
adamc@1266 2358 [])))],
adamc@1195 2359 {disc = (L'.TRecord [("1", s), ("2", s)], loc),
adamc@1195 2360 result = s}), loc)), loc)), loc)), loc),
adamc@749 2361 fm)
adamc@749 2362 end
adamc@1191 2363 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_left_join"), _), _), _), _), _),
adamc@1191 2364 (L.CRecord (_, right), _)) =>
adamc@750 2365 let
adamc@750 2366 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@750 2367 in
adamc@750 2368 ((L'.EAbs ("_", outerRec right,
adamc@750 2369 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@750 2370 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@750 2371 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@750 2372 (L'.EAbs ("on", s, s,
adamc@1195 2373 (L'.ECase ((L'.ERecord [("1", (L'.ERel 2, loc), s),
adamc@1195 2374 ("2", (L'.ERel 1, loc), s)], loc),
adam@2048 2375 [((L'.PRecord [("1", (L'.PPrim (Prim.String (Prim.Normal, "")),
adamc@1195 2376 loc), s)], loc),
adamc@1195 2377 (L'.ERel 1, loc)),
adam@2048 2378 ((L'.PRecord [("2", (L'.PPrim (Prim.String (Prim.Normal, "")),
adamc@1195 2379 loc), s)], loc),
adamc@1195 2380 (L'.ERel 2, loc)),
adamc@1195 2381 ((L'.PWild, loc),
adamc@1266 2382 strcat ((if #nestedRelops
adamc@1266 2383 (Settings.currentDbms ()) then
adam@2048 2384 [str "("]
adamc@1266 2385 else
adamc@1266 2386 [])
adamc@1266 2387 @ [(L'.ERel 2, loc),
adam@2048 2388 str " LEFT JOIN ",
adamc@1266 2389 (L'.ERel 1, loc),
adam@2048 2390 str " ON ",
adamc@1266 2391 (L'.ERel 0, loc)]
adamc@1266 2392 @ (if #nestedRelops
adamc@1266 2393 (Settings.currentDbms ()) then
adam@2048 2394 [str ")"]
adamc@1266 2395 else
adamc@1266 2396 [])))],
adamc@1195 2397 {disc = (L'.TRecord [("1", s), ("2", s)], loc),
adamc@1195 2398 result = s}), loc)), loc)), loc)), loc)), loc),
adamc@750 2399 fm)
adamc@750 2400 end
adamc@1191 2401 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_right_join"), _), (L.CRecord (_, left), _)),
adamc@1191 2402 _), _), _), _) =>
adamc@751 2403 let
adamc@751 2404 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@751 2405 in
adamc@751 2406 ((L'.EAbs ("_", outerRec left,
adamc@751 2407 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@751 2408 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@751 2409 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@751 2410 (L'.EAbs ("on", s, s,
adamc@1195 2411 (L'.ECase ((L'.ERecord [("1", (L'.ERel 2, loc), s),
adamc@1195 2412 ("2", (L'.ERel 1, loc), s)], loc),
adam@2048 2413 [((L'.PRecord [("1", (L'.PPrim (Prim.String (Prim.Normal, "")),
adamc@1195 2414 loc), s)], loc),
adamc@1195 2415 (L'.ERel 1, loc)),
adam@2048 2416 ((L'.PRecord [("2", (L'.PPrim (Prim.String (Prim.Normal, "")),
adamc@1195 2417 loc), s)], loc),
adamc@1195 2418 (L'.ERel 2, loc)),
adamc@1195 2419 ((L'.PWild, loc),
adamc@1266 2420 strcat ((if #nestedRelops
adamc@1266 2421 (Settings.currentDbms ()) then
adam@2048 2422 [str "("]
adamc@1266 2423 else
adamc@1266 2424 [])
adamc@1266 2425 @ [(L'.ERel 2, loc),
adam@2048 2426 str " RIGHT JOIN ",
adamc@1266 2427 (L'.ERel 1, loc),
adam@2048 2428 str " ON ",
adamc@1266 2429 (L'.ERel 0, loc)]
adamc@1266 2430 @ (if #nestedRelops
adamc@1266 2431 (Settings.currentDbms ()) then
adam@2048 2432 [str ")"]
adamc@1266 2433 else
adamc@1266 2434 [])))],
adamc@1195 2435 {disc = (L'.TRecord [("1", s), ("2", s)], loc),
adamc@1195 2436 result = s}), loc)), loc)), loc)), loc)), loc),
adamc@751 2437 fm)
adamc@751 2438 end
adamc@1191 2439 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_full_join"), _), (L.CRecord (_, left), _)), _),
adamc@1191 2440 (L.CRecord (_, right), _)), _), _) =>
adamc@751 2441 let
adamc@751 2442 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@751 2443 in
adamc@751 2444 ((L'.EAbs ("_", outerRec (left @ right),
adamc@751 2445 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@751 2446 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@751 2447 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@751 2448 (L'.EAbs ("on", s, s,
adamc@1195 2449 (L'.ECase ((L'.ERecord [("1", (L'.ERel 2, loc), s),
adamc@1195 2450 ("2", (L'.ERel 1, loc), s)], loc),
adam@2048 2451 [((L'.PRecord [("1", (L'.PPrim (Prim.String (Prim.Normal, "")),
adamc@1195 2452 loc), s)], loc),
adamc@1195 2453 (L'.ERel 1, loc)),
adam@2048 2454 ((L'.PRecord [("2", (L'.PPrim (Prim.String (Prim.Normal, "")),
adamc@1195 2455 loc), s)], loc),
adamc@1195 2456 (L'.ERel 2, loc)),
adamc@1195 2457 ((L'.PWild, loc),
adamc@1266 2458 strcat ((if #nestedRelops
adamc@1266 2459 (Settings.currentDbms ()) then
adam@2048 2460 [str "("]
adamc@1266 2461 else
adamc@1266 2462 [])
adamc@1266 2463 @ [(L'.ERel 2, loc),
adam@2048 2464 str " FULL JOIN ",
adamc@1266 2465 (L'.ERel 1, loc),
adam@2048 2466 str " ON ",
adamc@1266 2467 (L'.ERel 0, loc)]
adamc@1266 2468 @ (if #nestedRelops
adamc@1266 2469 (Settings.currentDbms ()) then
adam@2048 2470 [str ")"]
adamc@1266 2471 else
adamc@1266 2472 [])))],
adamc@1195 2473 {disc = (L'.TRecord [("1", s), ("2", s)], loc),
adamc@1195 2474 result = s}), loc)), loc)), loc)), loc)), loc),
adamc@751 2475 fm)
adamc@751 2476 end
adamc@748 2477
adamc@252 2478 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adam@2048 2479 (str "", fm)
adam@1682 2480 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_random"), _), _), _), _) =>
adam@2048 2481 (str (#randomFunction (Settings.currentDbms ()) ^ "()"), fm)
adamc@261 2482 | L.ECApp (
adamc@261 2483 (L.ECApp (
adamc@261 2484 (L.ECApp (
adam@1778 2485 (L.ECApp (
adam@1778 2486 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adam@1778 2487 _), _),
adamc@261 2488 _), _),
adamc@261 2489 _), _),
adamc@261 2490 _) =>
adamc@261 2491 let
adamc@261 2492 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 2493 in
adam@1778 2494 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adam@1778 2495 (L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adam@1778 2496 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adam@1778 2497 (L'.EAbs ("e2", s, s,
adam@1778 2498 (L'.ECase ((L'.ERel 0, loc),
adam@2048 2499 [((L'.PPrim (Prim.String (Prim.Normal, "")), loc),
adam@1778 2500 strcat [(L'.ERel 2, loc),
adam@1778 2501 (L'.ERel 1, loc)]),
adam@1778 2502 ((L'.PWild, loc),
adam@1778 2503 strcat [(L'.ERel 2, loc),
adam@1778 2504 (L'.ERel 1, loc),
adam@2048 2505 str ", ",
adam@1778 2506 (L'.ERel 0, loc)])],
adam@1778 2507 {disc = s, result = s}), loc)), loc)), loc)), loc)), loc),
adamc@261 2508 fm)
adamc@261 2509 end
adamc@252 2510
adamc@252 2511 | L.EFfi ("Basis", "sql_no_limit") =>
adam@2048 2512 (str "", fm)
adam@1663 2513 | L.EFfiApp ("Basis", "sql_limit", [(e, t)]) =>
adamc@262 2514 let
adamc@262 2515 val (e, fm) = monoExp (env, st, fm) e
adamc@262 2516 in
adamc@598 2517 (strcat [
adam@2048 2518 str " LIMIT ",
adam@1663 2519 (L'.EFfiApp ("Basis", "sqlifyInt", [(e, monoType env t)]), loc)
adamc@262 2520 ],
adamc@262 2521 fm)
adamc@262 2522 end
adamc@262 2523
adamc@252 2524 | L.EFfi ("Basis", "sql_no_offset") =>
adam@2048 2525 (str "", fm)
adam@1663 2526 | L.EFfiApp ("Basis", "sql_offset", [(e, t)]) =>
adamc@263 2527 let
adamc@263 2528 val (e, fm) = monoExp (env, st, fm) e
adamc@263 2529 in
adamc@598 2530 (strcat [
adam@2048 2531 str " OFFSET ",
adam@1663 2532 (L'.EFfiApp ("Basis", "sqlifyInt", [(e, monoType env t)]), loc)
adamc@263 2533 ],
adamc@263 2534 fm)
adamc@263 2535 end
adamc@253 2536
adamc@559 2537 | L.ECApp ((L.EFfi ("Basis", "sql_eq"), _), _) =>
adam@2048 2538 (str "=", fm)
adamc@559 2539 | L.ECApp ((L.EFfi ("Basis", "sql_ne"), _), _) =>
adam@2048 2540 (str "<>", fm)
adamc@559 2541 | L.ECApp ((L.EFfi ("Basis", "sql_lt"), _), _) =>
adam@2048 2542 (str "<", fm)
adamc@559 2543 | L.ECApp ((L.EFfi ("Basis", "sql_le"), _), _) =>
adam@2048 2544 (str "<=", fm)
adamc@559 2545 | L.ECApp ((L.EFfi ("Basis", "sql_gt"), _), _) =>
adam@2048 2546 (str ">", fm)
adamc@559 2547 | L.ECApp ((L.EFfi ("Basis", "sql_ge"), _), _) =>
adam@2048 2548 (str ">=", fm)
adamc@253 2549
adamc@559 2550 | L.ECApp ((L.EFfi ("Basis", "sql_plus"), _), _) =>
adamc@559 2551 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2552 str "+"), loc), fm)
adamc@559 2553 | L.ECApp ((L.EFfi ("Basis", "sql_minus"), _), _) =>
adamc@559 2554 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2555 str "-"), loc), fm)
adamc@559 2556 | L.ECApp ((L.EFfi ("Basis", "sql_times"), _), _) =>
adamc@559 2557 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2558 str "*"), loc), fm)
adamc@559 2559 | L.ECApp ((L.EFfi ("Basis", "sql_div"), _), _) =>
adamc@559 2560 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2561 str "/"), loc), fm)
adamc@559 2562 | L.EFfi ("Basis", "sql_mod") =>
adam@2048 2563 (str "%", fm)
adamc@559 2564
kkallio@1607 2565 | L.EFfi ("Basis", "sql_like") =>
adam@2048 2566 (str "LIKE", fm)
kkallio@1607 2567
adamc@253 2568 | L.ECApp (
adamc@253 2569 (L.ECApp (
adamc@253 2570 (L.ECApp (
adamc@253 2571 (L.ECApp (
adamc@254 2572 (L.ECApp (
adamc@264 2573 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 2574 _), _),
adamc@264 2575 _), _),
adamc@264 2576 _), _),
adamc@264 2577 _), _),
adamc@264 2578 _) =>
adamc@264 2579 let
adamc@264 2580 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 2581 in
adamc@264 2582 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 2583 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adam@2048 2584 strcat [str "(",
adamc@598 2585 (L'.ERel 1, loc),
adam@2048 2586 str " ",
adamc@598 2587 (L'.ERel 0, loc),
adam@2048 2588 str ")"]), loc)), loc),
adamc@264 2589 fm)
adamc@264 2590 end
adam@2048 2591 | L.EFfi ("Basis", "sql_not") => (str "NOT", fm)
adamc@559 2592 | L.ECApp ((L.EFfi ("Basis", "sql_neg"), _), _) =>
adamc@559 2593 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2594 str "-"), loc), fm)
adamc@264 2595
adamc@264 2596 | L.ECApp (
adamc@264 2597 (L.ECApp (
adamc@264 2598 (L.ECApp (
adamc@264 2599 (L.ECApp (
adamc@264 2600 (L.ECApp (
adamc@254 2601 (L.ECApp (
adam@1778 2602 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 2603 _), _),
adamc@254 2604 _), _),
adamc@254 2605 _), _),
adamc@254 2606 _), _),
adamc@254 2607 _), _),
adamc@254 2608 _) =>
adamc@254 2609 let
adamc@254 2610 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 2611 in
adamc@254 2612 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 2613 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 2614 (L'.EAbs ("e2", s, s,
adam@2048 2615 strcat [str "(",
adamc@598 2616 (L'.ERel 1, loc),
adam@2048 2617 str " ",
adamc@598 2618 (L'.ERel 2, loc),
adam@2048 2619 str " ",
adamc@598 2620 (L'.ERel 0, loc),
adam@2048 2621 str ")"]), loc)), loc)), loc),
adamc@254 2622 fm)
adamc@254 2623 end
adam@2048 2624 | L.EFfi ("Basis", "sql_and") => (str "AND", fm)
adam@2048 2625 | L.EFfi ("Basis", "sql_or") => (str "OR", fm)
adamc@254 2626
adamc@254 2627 | L.ECApp (
adamc@254 2628 (L.ECApp (
adamc@254 2629 (L.ECApp (
adamc@254 2630 (L.ECApp (
adamc@253 2631 (L.ECApp (
adamc@253 2632 (L.ECApp (
adamc@253 2633 (L.ECApp (
adam@1778 2634 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 2635 _), _),
adamc@253 2636 _), _),
adamc@253 2637 _), _),
adamc@253 2638 _), _),
adamc@253 2639 _), _),
adamc@253 2640 (L.CName tab, _)), _),
adam@2048 2641 (L.CName field, _)) => (str ("T_" ^ tab ^ "." ^ Settings.mangleSql (lowercaseFirst field)), fm)
adamc@260 2642
adamc@260 2643 | L.ECApp (
adamc@260 2644 (L.ECApp (
adamc@260 2645 (L.ECApp (
adamc@260 2646 (L.ECApp (
adamc@261 2647 (L.ECApp (
adam@1778 2648 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 2649 _), _),
adamc@261 2650 _), _),
adamc@261 2651 _), _),
adamc@261 2652 _), _),
adam@2048 2653 (L.CName nm, _)) => (str (Settings.mangleSql (lowercaseFirst nm)), fm)
adamc@261 2654
adamc@261 2655 | L.ECApp (
adamc@261 2656 (L.ECApp (
adamc@261 2657 (L.ECApp (
adamc@261 2658 (L.ECApp (
adamc@1191 2659 (L.ECApp (
adam@1416 2660 (L.ECApp (
adam@1416 2661 (L.EFfi ("Basis", "sql_relop"), _),
adam@1416 2662 _), _),
adamc@1191 2663 _), _),
adamc@260 2664 _), _),
adamc@260 2665 _), _),
adamc@260 2666 _), _),
adamc@260 2667 _) =>
adamc@260 2668 let
adamc@260 2669 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 2670 in
adamc@1196 2671 (if #nestedRelops (Settings.currentDbms ()) then
adam@1427 2672 (L'.EAbs ("c", s, (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adam@1427 2673 (L'.EAbs ("all", (L'.TFfi ("Basis", "bool"), loc), (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adam@1427 2674 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adam@1427 2675 (L'.EAbs ("e2", s, s,
adam@2048 2676 strcat [str "((",
adam@1427 2677 (L'.ERel 1, loc),
adam@2048 2678 str ") ",
adam@1427 2679 (L'.ERel 3, loc),
adam@1427 2680 (L'.ECase ((L'.ERel 2, loc),
adam@1427 2681 [((L'.PCon (L'.Enum, L'.PConFfi {mod = "Basis",
adam@1427 2682 datatyp = "bool",
adam@1427 2683 con = "True",
adam@1427 2684 arg = NONE}, NONE), loc),
adam@2048 2685 str " ALL"),
adam@1427 2686 ((L'.PWild, loc),
adam@2048 2687 str "")],
adam@1427 2688 {disc = (L'.TFfi ("Basis", "bool"), loc),
adam@1427 2689 result = s}), loc),
adam@2048 2690 str " (",
adam@1427 2691 (L'.ERel 0, loc),
adam@2048 2692 str "))"]), loc)), loc)), loc)), loc)
adamc@1196 2693 else
adam@1427 2694 (L'.EAbs ("c", s, (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adam@1427 2695 (L'.EAbs ("all", (L'.TFfi ("Basis", "bool"), loc), (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adam@1427 2696 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adam@1427 2697 (L'.EAbs ("e2", s, s,
adam@1427 2698 strcat [(L'.ERel 1, loc),
adam@2048 2699 str " ",
adam@1427 2700 (L'.ERel 3, loc),
adam@1427 2701 (L'.ECase ((L'.ERel 2, loc),
adam@1427 2702 [((L'.PCon (L'.Enum, L'.PConFfi {mod = "Basis",
adam@1427 2703 datatyp = "bool",
adam@1427 2704 con = "True",
adam@1427 2705 arg = NONE}, NONE), loc),
adam@2048 2706 str " ALL"),
adam@1427 2707 ((L'.PWild, loc),
adam@2048 2708 str "")],
adam@1427 2709 {disc = (L'.TFfi ("Basis", "bool"), loc),
adam@1427 2710 result = s}), loc),
adam@2048 2711 str " ",
adam@1427 2712 (L'.ERel 0, loc)]), loc)), loc)), loc)), loc),
adamc@260 2713 fm)
adamc@260 2714 end
adamc@1071 2715 | L.ECApp (
adamc@1071 2716 (L.ECApp (
adamc@1071 2717 (L.ECApp (
adamc@1191 2718 (L.ECApp (
adam@1394 2719 (L.ECApp (
adam@1394 2720 (L.EFfi ("Basis", "sql_forget_tables"), _),
adam@1394 2721 _), _),
adamc@1191 2722 _), _),
adamc@1071 2723 _), _),
adamc@1071 2724 _), _),
adamc@1071 2725 _) =>
adamc@1071 2726 let
adamc@1071 2727 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@1071 2728 in
adamc@1071 2729 ((L'.EAbs ("x", s, s, (L'.ERel 0, loc)), loc),
adamc@1071 2730 fm)
adamc@1071 2731 end
adamc@260 2732
adam@2048 2733 | L.EFfi ("Basis", "sql_union") => (str "UNION", fm)
adamc@1196 2734 | L.EFfi ("Basis", "sql_intersect") =>
adamc@1196 2735 (if #onlyUnion (Settings.currentDbms ()) then
adamc@1196 2736 ErrorMsg.errorAt loc "The DBMS you've selected doesn't support INTERSECT."
adamc@1196 2737 else
adamc@1196 2738 ();
adam@2048 2739 (str "INTERSECT", fm))
adamc@1196 2740 | L.EFfi ("Basis", "sql_except") =>
adamc@1196 2741 (if #onlyUnion (Settings.currentDbms ()) then
adamc@1196 2742 ErrorMsg.errorAt loc "The DBMS you've selected doesn't support EXCEPT."
adamc@1196 2743 else
adamc@1196 2744 ();
adam@2048 2745 (str "EXCEPT", fm))
adamc@260 2746
adamc@265 2747 | L.ECApp (
adamc@265 2748 (L.ECApp (
adamc@265 2749 (L.ECApp (
adam@1778 2750 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 2751 _), _),
adamc@265 2752 _), _),
adam@2048 2753 _) => (str "COUNT(*)", fm)
adamc@266 2754
adamc@266 2755 | L.ECApp (
adamc@266 2756 (L.ECApp (
adamc@266 2757 (L.ECApp (
adamc@266 2758 (L.ECApp (
adamc@1187 2759 (L.ECApp (
adam@1778 2760 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@1187 2761 _), _),
adamc@266 2762 _), _),
adamc@266 2763 _), _),
adamc@266 2764 _), _),
adamc@1168 2765 t) =>
adamc@266 2766 let
adamc@266 2767 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1357 2768
adam@1357 2769 val main = strcat [(L'.ERel 1, loc),
adam@2048 2770 str "(",
adam@1357 2771 (L'.ERel 0, loc),
adam@2048 2772 str ")"]
adamc@266 2773 in
adamc@266 2774 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adam@1778 2775 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), main), loc)), loc),
adamc@266 2776 fm)
adamc@266 2777 end
adamc@266 2778
adamc@1187 2779 | L.ECApp ((L.EFfi ("Basis", "sql_count_col"), _), _) =>
adam@2048 2780 (str "COUNT", fm)
adamc@1187 2781
adamc@266 2782 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 2783 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adam@1357 2784 | L.ECApp ((L.EFfi ("Basis", "sql_summable_option"), _), _) =>
adam@1357 2785 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adam@1357 2786 (L'.ERecord [], loc)), loc),
adam@1357 2787 fm)
adam@1777 2788 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adam@1777 2789 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2790 str "AVG"), loc),
adamc@266 2791 fm)
adam@1394 2792 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _), _), _) =>
adam@1394 2793 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun ((L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc)), loc),
adam@1394 2794 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2795 str "SUM"), loc)), loc),
adamc@266 2796 fm)
adamc@266 2797
adamc@559 2798 | L.EFfi ("Basis", "sql_arith_int") => ((L'.ERecord [], loc), fm)
adamc@559 2799 | L.EFfi ("Basis", "sql_arith_float") => ((L'.ERecord [], loc), fm)
adam@1427 2800 | L.ECApp ((L.EFfi ("Basis", "sql_arith_option"), _), _) =>
adam@1427 2801 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adam@1427 2802 (L'.ERecord [], loc)), loc),
adam@1427 2803 fm)
adamc@559 2804
adamc@266 2805 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 2806 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 2807 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adam@1357 2808 | L.ECApp ((L.EFfi ("Basis", "sql_maxable_option"), _), _) =>
adam@1357 2809 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adam@1357 2810 (L'.ERecord [], loc)), loc),
adam@1357 2811 fm)
adam@1394 2812 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _), _), _) =>
adam@1394 2813 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun ((L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc)), loc),
adam@1394 2814 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2815 str "MAX"), loc)), loc),
adamc@266 2816 fm)
adam@1394 2817 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _), _), _) =>
adam@1394 2818 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun ((L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc)), loc),
adam@1394 2819 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adam@2048 2820 str "MIN"), loc)), loc),
adamc@266 2821 fm)
adamc@266 2822
adam@2048 2823 | L.EFfi ("Basis", "sql_asc") => (str "", fm)
adam@2048 2824 | L.EFfi ("Basis", "sql_desc") => (str " DESC", fm)
adam@1777 2825 | L.ECApp (
adam@1777 2826 (L.ECApp (
adam@1777 2827 (L.ECApp (
adam@1777 2828 (L.ECApp (
adam@1778 2829 (L.EFfi ("Basis", "sql_nfunc"), _),
adam@1778 2830 _), _),
adam@1778 2831 _), _),
adam@1778 2832 _), _),
adam@1778 2833 _) =>
adam@1778 2834 let
adam@1778 2835 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1778 2836 in
adam@1778 2837 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc),
adam@1778 2838 fm)
adam@1778 2839 end
adam@1778 2840
adam@1778 2841 | L.EFfi ("Basis", "sql_window_normal") => ((L'.ERecord [], loc), fm)
adam@1778 2842 | L.EFfi ("Basis", "sql_window_fancy") => ((L'.ERecord [], loc), fm)
adam@1778 2843 | L.ECApp (
adam@1778 2844 (L.ECApp (
adam@1778 2845 (L.ECApp (
adam@1778 2846 (L.ECApp (
adam@1778 2847 (L.ECApp (
adam@1778 2848 (L.EFfi ("Basis", "sql_window"), _),
adam@1778 2849 _), _),
adam@1777 2850 _), _),
adam@1777 2851 _), _),
adam@1777 2852 _), _),
adam@1777 2853 _) =>
adam@1777 2854 let
adam@1777 2855 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1777 2856 in
adam@1778 2857 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun (s, s), loc),
adam@1778 2858 (L'.EAbs ("e", s, s,
adam@1778 2859 (L'.ERel 0, loc)), loc)), loc),
adam@1777 2860 fm)
adam@1777 2861 end
adam@1777 2862
adam@2048 2863 | L.EFfi ("Basis", "sql_current_timestamp") => (str "CURRENT_TIMESTAMP", fm)
adam@1776 2864
adam@1776 2865 | L.ECApp (
adam@1776 2866 (L.ECApp (
adam@1776 2867 (L.ECApp (
adam@1776 2868 (L.ECApp (
adam@1776 2869 (L.ECApp (
adam@1778 2870 (L.EFfi ("Basis", "sql_ufunc"), _),
adamc@746 2871 _), _),
adamc@746 2872 _), _),
adamc@746 2873 _), _),
adamc@746 2874 _), _),
adamc@746 2875 _) =>
adamc@746 2876 let
adamc@746 2877 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@746 2878 in
adamc@746 2879 ((L'.EAbs ("f", s, (L'.TFun (s, s), loc),
adamc@746 2880 (L'.EAbs ("x", s, s,
adamc@746 2881 strcat [(L'.ERel 1, loc),
adam@2048 2882 str "(",
adamc@746 2883 (L'.ERel 0, loc),
adam@2048 2884 str ")"]), loc)), loc),
adamc@746 2885 fm)
adamc@746 2886 end
adamc@890 2887 | L.EFfi ("Basis", "sql_octet_length") =>
adam@2048 2888 (str (if #supportsOctetLength (Settings.currentDbms ()) then
adam@2048 2889 "octet_length"
adam@2048 2890 else
adam@2048 2891 "length"), fm)
adam@1636 2892 | L.EFfi ("Basis", "sql_lower") =>
adam@2048 2893 (str "lower", fm)
adam@1636 2894 | L.EFfi ("Basis", "sql_upper") =>
adam@2048 2895 (str "upper", fm)
adamc@1207 2896 | L.ECApp ((L.EFfi ("Basis", "sql_known"), _), _) =>
adamc@1207 2897 ((L'.EFfi ("Basis", "sql_known"), loc), fm)
adamc@746 2898
adamc@470 2899 | (L.ECApp (
adamc@470 2900 (L.ECApp (
adamc@470 2901 (L.ECApp (
adamc@470 2902 (L.ECApp (
adam@1778 2903 (L.EFfi ("Basis", "sql_is_null"), _), _),
adamc@470 2904 _), _),
adamc@470 2905 _), _),
adamc@470 2906 _), _)) =>
adamc@470 2907 let
adamc@470 2908 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@470 2909 in
adamc@470 2910 ((L'.EAbs ("s", s, s,
adam@2048 2911 strcat [str "(",
adamc@598 2912 (L'.ERel 0, loc),
adam@2048 2913 str " IS NULL)"]), loc),
adamc@470 2914 fm)
adamc@470 2915 end
adamc@470 2916
kkallio@1572 2917 | (L.ECApp (
kkallio@1572 2918 (L.ECApp (
kkallio@1572 2919 (L.ECApp (
kkallio@1572 2920 (L.ECApp (
adam@1602 2921 (L.EFfi ("Basis", "sql_coalesce"), _), _),
adam@1602 2922 _), _),
adam@1602 2923 _), _),
adam@1602 2924 _), _)) =>
adam@1602 2925 let
adam@1602 2926 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1602 2927 in
adam@1602 2928 ((L'.EAbs ("x1", s, (L'.TFun (s, s), loc),
adam@1602 2929 (L'.EAbs ("x1", s, s,
adam@2048 2930 strcat [str "COALESCE(",
adam@1602 2931 (L'.ERel 1, loc),
adam@2048 2932 str ",",
adam@1602 2933 (L'.ERel 0, loc),
adam@2048 2934 str ")"]), loc)), loc),
adam@1602 2935 fm)
adam@1602 2936 end
adam@1602 2937
adam@1602 2938 | (L.ECApp (
adam@1602 2939 (L.ECApp (
adam@1602 2940 (L.ECApp (
adam@1602 2941 (L.ECApp (
adam@1778 2942 (L.EFfi ("Basis", "sql_if_then_else"), _), _),
kkallio@1572 2943 _), _),
kkallio@1572 2944 _), _),
kkallio@1572 2945 _), _)) =>
kkallio@1572 2946 let
kkallio@1572 2947 val s = (L'.TFfi ("Basis", "string"), loc)
kkallio@1572 2948 in
adam@1573 2949 ((L'.EAbs ("if", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
kkallio@1572 2950 (L'.EAbs ("then", s, (L'.TFun (s, s), loc),
adam@1573 2951 (L'.EAbs ("else", s, s,
adam@2048 2952 strcat [str "(CASE WHEN (",
kkallio@1572 2953 (L'.ERel 2, loc),
adam@2048 2954 str ") THEN (",
kkallio@1572 2955 (L'.ERel 1, loc),
adam@2048 2956 str ") ELSE (",
kkallio@1572 2957 (L'.ERel 0, loc),
adam@2048 2958 str ") END)"]), loc)), loc)), loc),
kkallio@1572 2959 fm)
kkallio@1572 2960 end
kkallio@1572 2961
adamc@1081 2962 | L.ECApp (
adamc@1081 2963 (L.ECApp (
adamc@1081 2964 (L.ECApp (
adamc@1081 2965 (L.ECApp (
adam@1778 2966 (L.EFfi ("Basis", "sql_nullable"), _),
adamc@1081 2967 _), _),
adamc@1081 2968 _), _),
adamc@1081 2969 _), _),
adamc@1081 2970 _) =>
adamc@1081 2971 let
adamc@1081 2972 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@1081 2973 in
adamc@1081 2974 ((L'.EAbs ("u", (L'.TRecord [], loc), (L'.TFun (s, s), loc),
adamc@1081 2975 (L'.EAbs ("x", s, s,
adamc@1081 2976 (L'.ERel 0, loc)), loc)), loc),
adamc@1081 2977 fm)
adamc@1081 2978 end
adam@1682 2979
adamc@1191 2980 | L.ECApp (
adamc@1191 2981 (L.ECApp (
adamc@1191 2982 (L.ECApp (
adamc@1191 2983 (L.ECApp (
adamc@1191 2984 (L.ECApp (
adam@1421 2985 (L.ECApp (
adam@1778 2986 (L.EFfi ("Basis", "sql_subquery"), _),
adam@1421 2987 _), _),
adamc@1191 2988 _), _),
adamc@1191 2989 _), _),
adamc@1191 2990 _), _),
adamc@1191 2991 _), _),
adamc@1191 2992 _) =>
adamc@1191 2993 let
adamc@1191 2994 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@1191 2995 in
adam@1421 2996 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun (s, s), loc),
adam@1421 2997 (L'.EAbs ("x", s, s,
adam@2048 2998 strcat [str "(",
adam@1421 2999 (L'.ERel 0, loc),
adam@2048 3000 str ")"]), loc)), loc),
adamc@1191 3001 fm)
adamc@1191 3002 end
adamc@1081 3003
adam@1778 3004 | L.ECApp (
adam@1778 3005 (L.ECApp (
adam@1778 3006 (L.ECApp (
adam@1778 3007 (L.EFfi ("Basis", "sql_no_partition"), _),
adam@1778 3008 _), _),
adam@1778 3009 _), _),
adam@2048 3010 _) => (str "", fm)
adam@1778 3011 | L.ECApp (
adam@1778 3012 (L.ECApp (
adam@1778 3013 (L.ECApp (
adam@1778 3014 (L.ECApp (
adam@1778 3015 (L.EFfi ("Basis", "sql_partition"), _),
adam@1778 3016 _), _),
adam@1778 3017 _), _),
adam@1778 3018 _), _),
adam@1778 3019 _) =>
adam@1778 3020 let
adam@1778 3021 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1778 3022 in
adam@2048 3023 ((L'.EAbs ("e", s, s, strcat [str "PARTITION BY ", (L'.ERel 0, loc)]), loc),
adam@1778 3024 fm)
adam@1778 3025 end
adam@1778 3026
adam@1778 3027 | L.ECApp (
adam@1778 3028 (L.ECApp (
adam@1778 3029 (L.ECApp (
adam@1778 3030 (L.ECApp (
adam@1778 3031 (L.EFfi ("Basis", "sql_window_function"), _),
adam@1778 3032 _), _),
adam@1778 3033 _), _),
adam@1778 3034 _), _),
adam@1778 3035 _) =>
adam@1778 3036 let
adam@1778 3037 val () = if #windowFunctions (Settings.currentDbms ()) then
adam@1778 3038 ()
adam@1778 3039 else
adam@1778 3040 ErrorMsg.errorAt loc "The DBMS you've selected doesn't support window functions."
adam@1778 3041
adam@1778 3042 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1778 3043
adam@1778 3044 val main = strcat [(L'.ERel 2, loc),
adam@2048 3045 str " OVER (",
adam@1778 3046 (L'.ERel 1, loc),
adam@1778 3047 (L'.ECase ((L'.ERel 0, loc),
adam@2048 3048 [((L'.PPrim (Prim.String (Prim.Normal, "")), loc),
adam@2048 3049 str ""),
adam@1778 3050 ((L'.PWild, loc),
adam@2048 3051 strcat [str " ORDER BY ",
adam@1778 3052 (L'.ERel 0, loc)])],
adam@1778 3053 {disc = s,
adam@1778 3054 result = s}), loc),
adam@2048 3055 str ")"]
adam@1778 3056 in
adam@1778 3057 ((L'.EAbs ("w", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adam@1778 3058 (L'.EAbs ("p", s, (L'.TFun (s, s), loc),
adam@1778 3059 (L'.EAbs ("o", s, s,
adam@1778 3060 main), loc)), loc)), loc),
adam@1778 3061 fm)
adam@1778 3062 end
adam@1778 3063
adam@1778 3064 | L.ECApp (
adam@1778 3065 (L.ECApp (
adam@1778 3066 (L.ECApp (
adam@1778 3067 (L.ECApp (
adam@1778 3068 (L.ECApp (
adam@1778 3069 (L.EFfi ("Basis", "sql_window_aggregate"), _),
adam@1778 3070 _), _),
adam@1778 3071 _), _),
adam@1778 3072 _), _),
adam@1778 3073 _), _),
adam@1778 3074 _) =>
adam@1778 3075 let
adam@1778 3076 val s = (L'.TFfi ("Basis", "string"), loc)
adam@1778 3077
adam@1778 3078 val main = strcat [(L'.ERel 1, loc),
adam@2048 3079 str "(",
adam@1778 3080 (L'.ERel 0, loc),
adam@2048 3081 str ")"]
adam@1778 3082 in
adam@1778 3083 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adam@1778 3084 (L'.EAbs ("e1", s, s, main), loc)), loc),
adam@1778 3085 fm)
adam@1778 3086 end
adam@1778 3087
adam@1778 3088 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_window_count"), _), _), _), _), _), _) =>
adam@2048 3089 (str "COUNT(*)", fm)
adam@1778 3090 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_rank"), _), _), _), _), _), _) =>
adam@2048 3091 (str "RANK()", fm)
adam@1778 3092
adam@1663 3093 | L.EFfiApp ("Basis", "nextval", [(e, _)]) =>
adamc@338 3094 let
adamc@338 3095 val (e, fm) = monoExp (env, st, fm) e
adamc@338 3096 in
adamc@465 3097 ((L'.ENextval e, loc), fm)
adamc@338 3098 end
adam@1663 3099 | L.EFfiApp ("Basis", "setval", [(e1, _), (e2, _)]) =>
adamc@1073 3100 let
adamc@1073 3101 val (e1, fm) = monoExp (env, st, fm) e1
adamc@1073 3102 val (e2, fm) = monoExp (env, st, fm) e2
adamc@1073 3103 in
adamc@1073 3104 ((L'.ESetval (e1, e2), loc), fm)
adamc@1073 3105 end
adamc@338 3106
adam@2048 3107 | L.EFfi ("Basis", "null") => (str "", fm)
adam@1567 3108
adam@1663 3109 | L.EFfiApp ("Basis", "classes", [(s1, _), (s2, _)]) =>
adam@1292 3110 let
adam@1292 3111 val (s1, fm) = monoExp (env, st, fm) s1
adam@1292 3112 val (s2, fm) = monoExp (env, st, fm) s2
adam@1292 3113 in
adam@2048 3114 ((L'.EStrcat (s1, (L'.EStrcat (str " ", s2), loc)), loc),
adam@1292 3115 fm)
adam@1292 3116 end
adam@1292 3117
adam@2048 3118 | L.EFfi ("Basis", "data_kind") => (str "data-", fm)
adam@2048 3119 | L.EFfi ("Basis", "aria_kind") => (str "aria-", fm)
adam@2047 3120
adam@2047 3121 | L.EFfiApp ("Basis", "data_attr", [(sk, _), (s1, _), (s2, _)]) =>
adam@2008 3122 let
adam@2047 3123 val (sk, fm) = monoExp (env, st, fm) sk
adam@2008 3124 val (s1, fm) = monoExp (env, st, fm) s1
adam@2008 3125 val (s2, fm) = monoExp (env, st, fm) s2
adam@2008 3126 in
adam@2047 3127 ((L'.EStrcat (sk,
adam@2008 3128 (L'.EStrcat ((L'.EFfiApp ("Basis", "blessData", [(s1, (L'.TFfi ("Basis", "string"), loc))]), loc),
adam@2048 3129 (L'.EStrcat (str "=\"",
adam@2008 3130 (L'.EStrcat ((L'.EFfiApp ("Basis", "attrifyString", [(s2, (L'.TFfi ("Basis", "string"), loc))]), loc),
adam@2048 3131 str "\""), loc)),
adam@2008 3132 loc)), loc)), loc),
adam@2008 3133 fm)
adam@2008 3134 end
adam@2008 3135
adam@2008 3136 | L.EFfiApp ("Basis", "data_attrs", [(s1, _), (s2, _)]) =>
adam@2008 3137 let
adam@2008 3138 val (s1, fm) = monoExp (env, st, fm) s1
adam@2008 3139 val (s2, fm) = monoExp (env, st, fm) s2
adam@2008 3140 in
adam@2048 3141 ((L'.EStrcat (s1, (L'.EStrcat (str " ", s2), loc)), loc),
adam@2008 3142 fm)
adam@2008 3143 end
adam@2008 3144
adam@1750 3145 | L.EFfiApp ("Basis", "css_url", [(s, _)]) =>
adam@1750 3146 let
adam@1750 3147 val (s, fm) = monoExp (env, st, fm) s
adam@1750 3148 in
adam@2048 3149 ((L'.EStrcat (str "url(",
adam@1750 3150 (L'.EStrcat ((L'.EFfiApp ("Basis", "css_url", [(s, (L'.TFfi ("Basis", "string"), loc))]), loc),
adam@2048 3151 str ")"), loc)), loc),
adam@1750 3152 fm)
adam@1750 3153 end
adam@1750 3154
adam@1750 3155 | L.EFfiApp ("Basis", "property", [(s, _)]) =>
adam@1750 3156 let
adam@1750 3157 val (s, fm) = monoExp (env, st, fm) s
adam@1750 3158 in
adam@1750 3159 ((L'.EStrcat ((L'.EFfiApp ("Basis", "property", [(s, (L'.TFfi ("Basis", "string"), loc))]), loc),
adam@2048 3160 str ":"), loc),
adam@1750 3161 fm)
adam@1750 3162 end
adam@1750 3163 | L.EFfiApp ("Basis", "value", [(s1, _), (s2, _)]) =>
adam@1750 3164 let
adam@1750 3165 val (s1, fm) = monoExp (env, st, fm) s1
adam@1750 3166 val (s2, fm) = monoExp (env, st, fm) s2
adam@1750 3167 in
adam@2048 3168 ((L'.EStrcat (s1, (L'.EStrcat (str " ", s2), loc)), loc),
adam@1750 3169 fm)
adam@1750 3170 end
adam@1750 3171
adam@2048 3172 | L.EFfi ("Basis", "noStyle") => (str "", fm)
adam@1750 3173 | L.EFfiApp ("Basis", "oneProperty", [(s1, _), (s2, _)]) =>
adam@1750 3174 let
adam@1750 3175 val (s1, fm) = monoExp (env, st, fm) s1
adam@1750 3176 val (s2, fm) = monoExp (env, st, fm) s2
adam@1750 3177 in
adam@2048 3178 ((L'.EStrcat (s1, (L'.EStrcat (s2, str ";"), loc)), loc),
adam@1750 3179 fm)
adam@1750 3180 end
adam@1750 3181
adamc@139 3182 | L.EApp (
adamc@139 3183 (L.ECApp (
adamc@720 3184 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 3185 _), _),
adamc@179 3186 se) =>
adamc@179 3187 let
adamc@179 3188 val (se, fm) = monoExp (env, st, fm) se
adamc@179 3189 in
adam@1663 3190 ((L'.EFfiApp ("Basis", "htmlifyString", [(se, (L'.TFfi ("Basis", "string"), loc))]), loc), fm)
adamc@179 3191 end
adam@1358 3192 | L.ECApp (
adam@1358 3193 (L.ECApp ((L.EFfi ("Basis", "cdataChar"), _), _), _),
adam@1358 3194 _) =>
adam@1358 3195 ((L'.EAbs ("ch", (L'.TFfi ("Basis", "char"), loc), (L'.TFfi ("Basis", "string"), loc),
adam@1663 3196 (L'.EFfiApp ("Basis", "htmlifySpecialChar", [((L'.ERel 0, loc), (L'.TFfi ("Basis", "char"), loc))]), loc)), loc), fm)
adamc@179 3197
adamc@95 3198 | L.EApp (
adamc@95 3199 (L.EApp (
adamc@720 3200 (L.ECApp (
adamc@720 3201 (L.ECApp (
adamc@95 3202 (L.ECApp (
adamc@139 3203 (L.ECApp (
adamc@720 3204 (L.EFfi ("Basis", "join"),
adamc@720 3205 _), _), _),
adamc@139 3206 _), _),
adamc@720 3207 _), _),
adamc@720 3208 _), _),
adamc@720 3209 xml1), _),
adamc@720 3210 xml2) =>
adamc@179 3211 let
adamc@179 3212 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 3213 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 3214 in
adamc@179 3215 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 3216 end
adamc@95 3217
adamc@95 3218 | L.EApp (
adamc@95 3219 (L.EApp (
adamc@104 3220 (L.EApp (
adamc@721 3221 (L.EApp (
adam@1643 3222 (L.EApp (
adam@1750 3223 (L.EApp (
adam@1751 3224 (L.EApp (
adam@1751 3225 (L.ECApp (
adamc@139 3226 (L.ECApp (
adamc@139 3227 (L.ECApp (
adamc@139 3228 (L.ECApp (
adamc@721 3229 (L.ECApp (
adam@1643 3230 (L.ECApp (
adam@1750 3231 (L.ECApp (
adam@1751 3232 (L.ECApp (
adam@1751 3233 (L.EFfi ("Basis", "tag"),
adam@2012 3234 _), (L.CRecord (_, attrsGiven), _)), _), _), _), ctxOuter), _), _), _), _), _), _), _), _), _), _), _),
adam@1751 3235 class), _),
adam@1751 3236 dynClass), _),
adam@1751 3237 style), _),
adam@1751 3238 dynStyle), _),
adamc@720 3239 attrs), _),
adamc@720 3240 tag), _),
adamc@95 3241 xml) =>
adamc@95 3242 let
adamc@140 3243 fun getTag' (e, _) =
adamc@140 3244 case e of
adam@1833 3245 L.EFfi (_, tag) => (tag, [])
adamc@143 3246 | L.ECApp (e, t) => let
adamc@143 3247 val (tag, ts) = getTag' e
adamc@143 3248 in
adamc@143 3249 (tag, ts @ [t])
adamc@143 3250 end
adamc@140 3251 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 3252 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 3253 ("", []))
adamc@140 3254
adamc@95 3255 fun getTag (e, _) =
adamc@95 3256 case e of
adam@1833 3257 L.EFfiApp (_, tag, [((L.ERecord [], _), _)]) => (tag, [])
adamc@140 3258 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 3259 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 3260 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 3261 ("", []))
adamc@95 3262
adamc@143 3263 val (tag, targs) = getTag tag
adamc@95 3264
adamc@179 3265 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@598 3266 val attrs = case #1 attrs of
adamc@598 3267 L'.ERecord xes => xes
adamc@1272 3268 | _ => map (fn ((L.CName x, _), t) => (x, (L'.EField (attrs, x), loc), monoType env t)
adamc@1272 3269 | (c, t) => (E.errorAt loc "Non-constant field name for HTML tag attribute";
adamc@1272 3270 Print.eprefaces' [("Name", CorePrint.p_con env c)];
adamc@1272 3271 ("", (L'.EField (attrs, ""), loc), monoType env t))) attrsGiven
adamc@104 3272
adamc@717 3273 val attrs =
adamc@717 3274 if List.exists (fn ("Link", _, _) => true
adamc@717 3275 | _ => false) attrs then
adamc@717 3276 List.filter (fn ("Href", _, _) => false
adamc@717 3277 | _ => true) attrs
adamc@717 3278 else
adamc@717 3279 attrs
adamc@717 3280
adamc@1042 3281 fun findOnload (attrs, onload, onunload, acc) =
adamc@668 3282 case attrs of
adamc@1042 3283 [] => (onload, onunload, acc)
adamc@1042 3284 | ("Onload", e, _) :: rest => findOnload (rest, SOME e, onunload, acc)
adamc@1042 3285 | ("Onunload", e, _) :: rest => findOnload (rest, onload, SOME e, acc)
adamc@1042 3286 | x :: rest => findOnload (rest, onload, onunload, x :: acc)
adam@1682 3287
vshabanoff@1711 3288 val (onload, onunload, attrs) =
vshabanoff@1711 3289 if tag = "body" then
vshabanoff@1711 3290 findOnload (attrs, NONE, NONE, [])
vshabanoff@1711 3291 else
vshabanoff@1711 3292 (NONE, NONE, attrs)
adamc@668 3293
adam@2121 3294 val (class, fm) = monoExp (env, st, fm) class
adam@2121 3295 val (dynClass, fm) = monoExp (env, st, fm) dynClass
adam@2121 3296 val (style, fm) = monoExp (env, st, fm) style
adam@2121 3297 val (dynStyle, fm) = monoExp (env, st, fm) dynStyle
adam@2121 3298
adam@2026 3299 (* Special case for <button value=""> *)
adam@2026 3300 val (attrs, extraString) = case tag of
adam@2026 3301 "button" =>
adam@2026 3302 (case List.partition (fn (x, _, _) => x = "Value") attrs of
adam@2026 3303 ([(_, value, _)], rest) =>
adam@2026 3304 (rest, SOME value)
adam@2026 3305 | _ => (attrs, NONE))
adam@2121 3306 | "body" =>
adam@2121 3307 (attrs,
adam@2121 3308 if (case (#1 dynClass, #1 dynStyle) of
adam@2121 3309 (L'.ESome _, _) => true
adam@2121 3310 | (_, L'.ESome _) => true
adam@2121 3311 | _ => false) then
adam@2121 3312 let
adam@2121 3313 fun jsify (e : L'.exp) =
adam@2121 3314 case #1 e of
adam@2121 3315 L'.ESome (_, ds) => strcat [str "execD(",
adam@2121 3316 (L'.EJavaScript (L'.Script, ds), loc),
adam@2121 3317 str ")"]
adam@2121 3318 | _ => str "null"
adam@2121 3319 in
adam@2121 3320 SOME (strcat [str "<script type=\"text/javascript\">bodyDynClass(",
adam@2121 3321 jsify dynClass,
adam@2121 3322 str ",",
adam@2121 3323 jsify dynStyle,
adam@2121 3324 str ")</script>"])
adam@2121 3325 end
adam@2121 3326 else
adam@2121 3327 NONE)
adam@2026 3328 | _ => (attrs, NONE)
ziv@2055 3329
adam@2026 3330
adam@2079 3331 val dynamics = ["dyn", "ctextbox", "cpassword", "ccheckbox", "cselect", "coption", "ctextarea", "active", "script", "cemail", "csearch", "curl", "ctel", "ccolor"]
adam@1651 3332
adam@1751 3333 fun isSome (e, _) =
adam@1751 3334 case e of
adam@1751 3335 L'.ESome _ => true
adam@1751 3336 | _ => false
adam@1751 3337
adam@1751 3338 val () = if isSome dynClass orelse isSome dynStyle then
adam@1751 3339 if List.exists (fn x => x = tag) dynamics then
adam@1751 3340 E.errorAt loc ("Dynamic tag <" ^ tag ^ "> cannot be combined with 'dynClass' or 'dynStyle' attribute; an additional <span> may be useful")
adam@1751 3341 else
adam@1751 3342 ()
adam@1751 3343 else
adam@1751 3344 ()
adam@1651 3345
adam@1479 3346 fun tagStart tag' =
adamc@598 3347 let
adamc@721 3348 val t = (L'.TFfi ("Basis", "string"), loc)
adam@2048 3349 val s = strH (String.concat ["<", tag'])
adamc@721 3350
adam@1817 3351 val s = (L'.EStrcat (s,
adam@1817 3352 (L'.ECase (class,
adam@2048 3353 [((L'.PPrim (Prim.String (Prim.Normal, "")), loc),
adam@2048 3354 strH ""),
adam@1817 3355 ((L'.PVar ("x", t), loc),
adam@2048 3356 (L'.EStrcat (strH " class=\"",
adam@1817 3357 (L'.EStrcat ((L'.ERel 0, loc),
adam@2048 3358 strH "\""),
adam@1817 3359 loc)), loc))],
adam@1817 3360 {disc = t,
adam@1817 3361 result = t}), loc)), loc)
adam@1817 3362
adam@1817 3363 val s = (L'.EStrcat (s,
adam@1817 3364 (L'.ECase (style,
adam@2048 3365 [((L'.PPrim (Prim.String (Prim.Normal, "")), loc),
adam@2048 3366 strH ""),
adam@1817 3367 ((L'.PVar ("x", t), loc),
adam@2048 3368 (L'.EStrcat (strH " style=\"",
adam@1817 3369 (L'.EStrcat ((L'.ERel 0, loc),
adam@2048 3370 strH "\""),
adam@1817 3371 loc)), loc))],
adam@1817 3372 {disc = t,
adam@1817 3373 result = t}), loc)), loc)
adam@1750 3374
adam@1479 3375 val (s, fm) = foldl (fn (("Action", _, _), acc) => acc
adam@1479 3376 | (("Source", _, _), acc) => acc
adam@2008 3377 | (("Data", e, _), (s, fm)) =>
adam@2008 3378 ((L'.EStrcat (s,
adam@2008 3379 (L'.EStrcat (
adam@2048 3380 strH " ",
adam@2008 3381 e), loc)), loc),
adam@2008 3382 fm)
adam@1479 3383 | ((x, e, t), (s, fm)) =>
adam@1479 3384 case t of
adam@1479 3385 (L'.TFfi ("Basis", "bool"), _) =>
adam@1479 3386 let
adam@1479 3387 val s' = " " ^ lowercaseFirst x
adam@1479 3388 in
adam@1479 3389 ((L'.ECase (e,
adam@1479 3390 [((L'.PCon (L'.Enum,
adam@1479 3391 L'.PConFfi {mod = "Basis",
adam@1479 3392 datatyp = "bool",
adam@1479 3393 con = "True",
adam@1479 3394 arg = NONE},
adam@1479 3395 NONE), loc),
adam@1479 3396 (L'.EStrcat (s,
adam@2048 3397 strH s'), loc)),
adam@1479 3398 ((L'.PCon (L'.Enum,
adam@1479 3399 L'.PConFfi {mod = "Basis",
adam@1479 3400 datatyp = "bool",
adam@1479 3401 con = "False",
adam@1479 3402 arg = NONE},
adam@1479 3403 NONE), loc),
adam@1479 3404 s)],
adam@1479 3405 {disc = (L'.TFfi ("Basis", "bool"), loc),
adam@1479 3406 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adam@1479 3407 fm)
adam@1479 3408 end
adam@1479 3409 | (L'.TFun (dom, _), _) =>
adam@1479 3410 let
vshabanoff@1712 3411 val e =
adam@1479 3412 case #1 dom of
vshabanoff@1712 3413 L'.TRecord [] => (L'.EApp (e, (L'.ERecord [], loc)), loc)
adam@1783 3414 | _ =>
adam@1783 3415 if String.isPrefix "Onkey" x then
adam@1783 3416 (L'.EApp ((L'.EApp (e, (L'.EFfiApp ("Basis", "keyEvent", []), loc)),
adam@1783 3417 loc), (L'.ERecord [], loc)), loc)
adam@1783 3418 else
adam@1783 3419 (L'.EApp ((L'.EApp (e, (L'.EFfiApp ("Basis", "mouseEvent", []), loc)),
adam@1783 3420 loc), (L'.ERecord [], loc)), loc)
adam@1783 3421
vshabanoff@1712 3422 val s' = " " ^ lowercaseFirst x ^ "='uw_event=event;exec("
adam@1479 3423 in
adam@1479 3424 ((L'.EStrcat (s,
adam@1479 3425 (L'.EStrcat (
adam@2048 3426 strH s',
adam@1479 3427 (L'.EStrcat (
adam@1479 3428 (L'.EJavaScript (L'.Attribute, e), loc),
julian@2145 3429 strH ")'"), loc)),
adam@1479 3430 loc)), loc),
adam@1479 3431 fm)
adam@1479 3432 end
adam@1479 3433 | _ =>
adam@1479 3434 let
adam@1479 3435 val fooify =
adam@1479 3436 case x of
adam@1479 3437 "Link" => urlifyExp
adam@1479 3438 | "Action" => urlifyExp
adam@1479 3439 | _ => attrifyExp
adam@1479 3440
adam@1479 3441 val x =
adam@1479 3442 case x of
adam@1479 3443 "Typ" => "Type"
adam@1840 3444 | "Nam" => "Name"
adam@1479 3445 | "Link" => "Href"
adam@1479 3446 | _ => x
adam@1840 3447
adam@1840 3448 val x = String.translate (fn #"_" => "-"
adam@1840 3449 | ch => String.str ch) x
adam@1840 3450
adam@1479 3451 val xp = " " ^ lowercaseFirst x ^ "=\""
adam@1479 3452
adam@1479 3453 val (e, fm) = fooify env fm (e, t)
adam@1479 3454 val e = case (tag, x) of
adam@2048 3455 ("coption", "Value") => (L'.EStrcat (strH "x", e), loc)
adam@1479 3456 | _ => e
adam@1479 3457 in
adam@1479 3458 ((L'.EStrcat (s,
adam@2048 3459 (L'.EStrcat (strH xp,
adam@1479 3460 (L'.EStrcat (e,
adam@2048 3461 strH "\""),
adam@1479 3462 loc)),
adam@1479 3463 loc)), loc),
adam@1479 3464 fm)
adam@1479 3465 end)
adam@1479 3466 (s, fm) attrs
adamc@598 3467 in
adam@1479 3468 (if tag = "coption" andalso List.all (fn ("Value", _, _) => false | _ => true) attrs then
adam@1479 3469 (L'.EStrcat (s,
adam@2048 3470 strH " value=\"\""), loc)
adam@1479 3471 else
adam@1479 3472 s,
adam@1479 3473 fm)
adamc@598 3474 end
adamc@104 3475
adamc@143 3476 fun input typ =
adamc@143 3477 case targs of
adamc@155 3478 [_, (L.CName name, _)] =>
adamc@179 3479 let
adamc@179 3480 val (ts, fm) = tagStart "input"
adamc@179 3481 in
adamc@179 3482 ((L'.EStrcat (ts,
adam@2048 3483 strH (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\" />")), loc), fm)
adamc@179 3484 end
adamc@143 3485 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 3486 raise Fail "No name passed to input tag")
adamc@104 3487
adam@1728 3488 fun normal (tag, extra) =
adamc@143 3489 let
adamc@179 3490 val (tagStart, fm) = tagStart tag
adamc@152 3491 val tagStart = case extra of
adamc@152 3492 NONE => tagStart
adamc@152 3493 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 3494
adam@2090 3495 val firstWord = Substring.string o #1 o Substring.splitl (fn ch => not (Char.isSpace ch)) o Substring.full
adam@2090 3496
adamc@143 3497 fun normal () =
adamc@179 3498 let
adamc@179 3499 val (xml, fm) = monoExp (env, st, fm) xml
adam@2026 3500
adam@2026 3501 val xml = case extraString of
adam@2026 3502 NONE => xml
adam@2026 3503 | SOME extra => (L'.EStrcat (extra, xml), loc)
adamc@179 3504 in
adam@2048 3505 ((L'.EStrcat ((L'.EStrcat (tagStart, strH ">"), loc),
adamc@179 3506 (L'.EStrcat (xml,
adam@2090 3507 strH (String.concat ["</", firstWord tag, ">"])), loc)),
adamc@179 3508 loc),
adamc@179 3509 fm)
adamc@179 3510 end
adamc@984 3511
adamc@984 3512 fun isSingleton () =
adamc@984 3513 let
adamc@984 3514 val (bef, aft) = Substring.splitl (not o Char.isSpace) (Substring.full tag)
adamc@984 3515 in
adamc@984 3516 SS.member (singletons, if Substring.isEmpty aft then
adamc@984 3517 tag
adamc@984 3518 else
adamc@984 3519 Substring.string bef)
adamc@984 3520 end
adamc@143 3521 in
adam@2026 3522 case (xml, extraString) of
adam@2026 3523 ((L.EApp ((L.ECApp (
adam@2026 3524 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adam@2026 3525 _), _),
adam@2026 3526 _), _),