annotate src/monoize.sml @ 1781:25824a0e8bf1

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