annotate src/monoize.sml @ 1477:d65387bdc557

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