annotate src/monoize.sml @ 1817:148203744882

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