annotate src/monoize.sml @ 1928:f1c211936557

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