annotate src/monoize.sml @ 1773:b5f5e8d439c7

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