annotate src/monoize.sml @ 1724:125f9b01fbf1

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