annotate src/monoize.sml @ 1809:b8fa7441e637

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