annotate src/monoize.sml @ 2029:dfa35ca83d92

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