annotate src/monoize.sml @ 769:efceae06df17

allow/deny working in Mono_opt
author Adam Chlipala <adamc@hcoop.net>
date Sat, 02 May 2009 13:37:52 -0400
parents 7f653298dd66
children a8bdd5a0d9b0
rev   line source
adamc@25 1 (* Copyright (c) 2008, Adam Chlipala
adamc@25 2 * All rights reserved.
adamc@25 3 *
adamc@25 4 * Redistribution and use in source and binary forms, with or without
adamc@25 5 * modification, are permitted provided that the following conditions are met:
adamc@25 6 *
adamc@25 7 * - Redistributions of source code must retain the above copyright notice,
adamc@25 8 * this list of conditions and the following disclaimer.
adamc@25 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@25 10 * this list of conditions and the following disclaimer in the documentation
adamc@25 11 * and/or other materials provided with the distribution.
adamc@25 12 * - The names of contributors may not be used to endorse or promote products
adamc@25 13 * derived from this software without specific prior written permission.
adamc@25 14 *
adamc@25 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@25 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@25 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@25 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@25 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@25 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@25 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@25 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@25 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@25 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@25 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@25 26 *)
adamc@25 27
adamc@25 28 structure Monoize :> MONOIZE = struct
adamc@25 29
adamc@25 30 structure E = ErrorMsg
adamc@25 31 structure Env = CoreEnv
adamc@25 32
adamc@25 33 structure L = Core
adamc@25 34 structure L' = Mono
adamc@25 35
adamc@196 36 structure IM = IntBinaryMap
adamc@735 37 structure IS = IntBinarySet
adamc@196 38
adamc@196 39 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
adamc@25 40
adamc@252 41 structure U = MonoUtil
adamc@252 42
adamc@252 43 val liftExpInExp =
adamc@252 44 U.Exp.mapB {typ = fn t => t,
adamc@252 45 exp = fn bound => fn e =>
adamc@252 46 case e of
adamc@252 47 L'.ERel xn =>
adamc@252 48 if xn < bound then
adamc@252 49 e
adamc@252 50 else
adamc@252 51 L'.ERel (xn + 1)
adamc@252 52 | _ => e,
adamc@252 53 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@252 54 | (bound, _) => bound}
adamc@252 55
adamc@25 56 fun monoName env (all as (c, loc)) =
adamc@25 57 let
adamc@25 58 fun poly () =
adamc@25 59 (E.errorAt loc "Unsupported name constructor";
adamc@25 60 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 61 "")
adamc@25 62 in
adamc@25 63 case c of
adamc@25 64 L.CName s => s
adamc@25 65 | _ => poly ()
adamc@25 66 end
adamc@25 67
adamc@292 68 fun readType' (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 69 (L'.TOption t, loc)), loc)
adamc@292 70 fun readErrType (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 71 t), loc)
adamc@292 72 fun readType (t, loc) =
adamc@292 73 (L'.TRecord [("Read", readType' (t, loc)),
adamc@292 74 ("ReadError", readErrType (t, loc))],
adamc@292 75 loc)
adamc@292 76
adamc@196 77 fun monoType env =
adamc@25 78 let
adamc@196 79 fun mt env dtmap (all as (c, loc)) =
adamc@196 80 let
adamc@196 81 fun poly () =
adamc@196 82 (E.errorAt loc "Unsupported type constructor";
adamc@196 83 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 84 dummyTyp)
adamc@196 85 in
adamc@196 86 case c of
adamc@196 87 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 88 | L.TCFun _ => poly ()
adamc@196 89 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@196 90 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
adamc@196 91 | L.TRecord _ => poly ()
adamc@196 92
adamc@288 93 | L.CApp ((L.CFfi ("Basis", "option"), _), t) =>
adamc@288 94 (L'.TOption (mt env dtmap t), loc)
adamc@757 95 | L.CApp ((L.CFfi ("Basis", "list"), _), t) =>
adamc@757 96 (L'.TList (mt env dtmap t), loc)
adamc@288 97
adamc@387 98 | L.CApp ((L.CFfi ("Basis", "eq"), _), t) =>
adamc@387 99 let
adamc@387 100 val t = mt env dtmap t
adamc@387 101 in
adamc@387 102 (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)
adamc@387 103 end
adamc@389 104 | L.CApp ((L.CFfi ("Basis", "num"), _), t) =>
adamc@389 105 let
adamc@389 106 val t = mt env dtmap t
adamc@389 107 in
adamc@417 108 (L'.TRecord [("Zero", t),
adamc@417 109 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 110 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 111 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 112 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 113 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 114 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))],
adamc@389 115 loc)
adamc@389 116 end
adamc@391 117 | L.CApp ((L.CFfi ("Basis", "ord"), _), t) =>
adamc@391 118 let
adamc@391 119 val t = mt env dtmap t
adamc@391 120 in
adamc@391 121 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 122 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 123 loc)
adamc@391 124 end
adamc@286 125 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
adamc@286 126 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@290 127 | L.CApp ((L.CFfi ("Basis", "read"), _), t) =>
adamc@292 128 readType (mt env dtmap t, loc)
adamc@286 129
adamc@717 130 | L.CFfi ("Basis", "url") => (L'.TFfi ("Basis", "string"), loc)
adamc@741 131 | L.CFfi ("Basis", "mimeType") => (L'.TFfi ("Basis", "string"), loc)
adamc@720 132 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 133 (L'.TFfi ("Basis", "string"), loc)
adamc@196 134 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 135 (L'.TFfi ("Basis", "string"), loc)
adamc@721 136 | L.CFfi ("Basis", "css_class") => (L'.TFfi ("Basis", "string"), loc)
adamc@196 137
adamc@251 138 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@252 139 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
adamc@565 140 | L.CApp ((L.CFfi ("Basis", "source"), _), t) =>
adamc@577 141 (L'.TSource, loc)
adamc@568 142 | L.CApp ((L.CFfi ("Basis", "signal"), _), t) =>
adamc@568 143 (L'.TSignal (mt env dtmap t), loc)
adamc@462 144 | L.CApp ((L.CFfi ("Basis", "http_cookie"), _), _) =>
adamc@462 145 (L'.TFfi ("Basis", "string"), loc)
adamc@705 146 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_table"), _), _), _), _) =>
adamc@252 147 (L'.TFfi ("Basis", "string"), loc)
adamc@338 148 | L.CFfi ("Basis", "sql_sequence") =>
adamc@338 149 (L'.TFfi ("Basis", "string"), loc)
adamc@252 150 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
adamc@252 151 (L'.TFfi ("Basis", "string"), loc)
adamc@252 152 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
adamc@252 153 (L'.TFfi ("Basis", "string"), loc)
adamc@748 154 | L.CApp ((L.CFfi ("Basis", "sql_from_items"), _), _) =>
adamc@748 155 (L'.TFfi ("Basis", "string"), loc)
adamc@252 156 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
adamc@252 157 (L'.TFfi ("Basis", "string"), loc)
adamc@707 158 | L.CApp ((L.CApp ((L.CFfi ("Basis", "primary_key"), _), _), _), _) =>
adamc@707 159 (L'.TFfi ("Basis", "string"), loc)
adamc@704 160 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_constraints"), _), _), _), _) =>
adamc@704 161 (L'.TFfi ("Basis", "sql_constraints"), loc)
adamc@705 162 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_constraint"), _), _), _), _) =>
adamc@704 163 (L'.TFfi ("Basis", "string"), loc)
adamc@712 164 | L.CApp ((L.CApp ((L.CFfi ("Basis", "linkable"), _), _), _), _) =>
adamc@712 165 (L'.TRecord [], loc)
adamc@709 166 | L.CApp ((L.CApp ((L.CFfi ("Basis", "matching"), _), _), _), _) =>
adamc@709 167 let
adamc@709 168 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 169 in
adamc@709 170 (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 171 end
adamc@709 172 | L.CApp ((L.CFfi ("Basis", "propagation_mode"), _), _) =>
adamc@709 173 (L'.TFfi ("Basis", "string"), loc)
adamc@252 174
adamc@252 175 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
adamc@252 176 (L'.TRecord [], loc)
adamc@252 177 | L.CFfi ("Basis", "sql_relop") =>
adamc@252 178 (L'.TFfi ("Basis", "string"), loc)
adamc@252 179 | L.CFfi ("Basis", "sql_direction") =>
adamc@252 180 (L'.TFfi ("Basis", "string"), loc)
adamc@252 181 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
adamc@252 182 (L'.TFfi ("Basis", "string"), loc)
adamc@252 183 | L.CFfi ("Basis", "sql_limit") =>
adamc@252 184 (L'.TFfi ("Basis", "string"), loc)
adamc@252 185 | L.CFfi ("Basis", "sql_offset") =>
adamc@252 186 (L'.TFfi ("Basis", "string"), loc)
adamc@753 187 | L.CApp ((L.CApp ((L.CFfi ("Basis", "fieldsOf"), _), _), _), _) =>
adamc@753 188 (L'.TRecord [], loc)
adamc@252 189
adamc@676 190 | L.CApp ((L.CFfi ("Basis", "sql_injectable_prim"), _), t) =>
adamc@676 191 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 192 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
adamc@252 193 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@750 194 | L.CApp ((L.CApp ((L.CFfi ("Basis", "nullify"), _), _), _), _) =>
adamc@750 195 (L'.TRecord [], loc)
adamc@252 196 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
adamc@252 197 (L'.TFfi ("Basis", "string"), loc)
adamc@252 198 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
adamc@252 199 (L'.TFfi ("Basis", "string"), loc)
adamc@252 200 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
adamc@252 201 (L'.TFfi ("Basis", "string"), loc)
adamc@252 202 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
adamc@252 203 (L'.TRecord [], loc)
adamc@252 204 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
adamc@252 205 (L'.TRecord [], loc)
adamc@559 206 | L.CApp ((L.CFfi ("Basis", "sql_arith"), _), _) =>
adamc@559 207 (L'.TRecord [], loc)
adamc@441 208 | L.CApp ((L.CFfi ("Basis", "sql_nfunc"), _), _) =>
adamc@441 209 (L'.TFfi ("Basis", "string"), loc)
adamc@746 210 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_ufunc"), _), _), _), _) =>
adamc@746 211 (L'.TFfi ("Basis", "string"), loc)
adamc@251 212
adamc@668 213 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@668 214 (L'.TFfi ("Basis", "channel"), loc)
adamc@668 215
adamc@196 216 | L.CRel _ => poly ()
adamc@196 217 | L.CNamed n =>
adamc@196 218 (case IM.find (dtmap, n) of
adamc@196 219 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 220 | NONE =>
adamc@196 221 let
adamc@196 222 val r = ref (L'.Default, [])
adamc@196 223 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 224
adamc@196 225 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 226
adamc@196 227 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 228 in
adamc@196 229 case xs of
adamc@198 230 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 231 (L'.TDatatype (n, r), loc))
adamc@196 232 | _ => poly ()
adamc@196 233 end)
adamc@196 234 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 235 | L.CApp _ => poly ()
adamc@196 236 | L.CAbs _ => poly ()
adamc@196 237
adamc@196 238 | L.CName _ => poly ()
adamc@196 239
adamc@196 240 | L.CRecord _ => poly ()
adamc@196 241 | L.CConcat _ => poly ()
adamc@621 242 | L.CMap _ => poly ()
adamc@196 243 | L.CUnit => poly ()
adamc@214 244
adamc@214 245 | L.CTuple _ => poly ()
adamc@214 246 | L.CProj _ => poly ()
adamc@626 247
adamc@626 248 | L.CKAbs _ => poly ()
adamc@626 249 | L.CKApp _ => poly ()
adamc@626 250 | L.TKFun _ => poly ()
adamc@196 251 end
adamc@25 252 in
adamc@196 253 mt env IM.empty
adamc@25 254 end
adamc@25 255
adamc@25 256 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 257
adamc@179 258 structure IM = IntBinaryMap
adamc@179 259
adamc@179 260 datatype foo_kind =
adamc@179 261 Attr
adamc@179 262 | Url
adamc@179 263
adamc@179 264 fun fk2s fk =
adamc@179 265 case fk of
adamc@179 266 Attr => "attr"
adamc@179 267 | Url => "url"
adamc@179 268
adamc@179 269 structure Fm :> sig
adamc@179 270 type t
adamc@179 271
adamc@179 272 val empty : int -> t
adamc@179 273
adamc@179 274 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@758 275 val lookupList : t -> foo_kind -> L'.typ -> (int -> t -> L'.decl * t) -> t * int
adamc@179 276 val enter : t -> t
adamc@179 277 val decls : t -> L'.decl list
adamc@683 278
adamc@683 279 val freshName : t -> int * t
adamc@179 280 end = struct
adamc@179 281
adamc@179 282 structure M = BinaryMapFn(struct
adamc@179 283 type ord_key = foo_kind
adamc@179 284 fun compare x =
adamc@179 285 case x of
adamc@179 286 (Attr, Attr) => EQUAL
adamc@179 287 | (Attr, _) => LESS
adamc@179 288 | (_, Attr) => GREATER
adamc@179 289
adamc@179 290 | (Url, Url) => EQUAL
adamc@179 291 end)
adamc@179 292
adamc@758 293 structure TM = BinaryMapFn(struct
adamc@758 294 type ord_key = L'.typ
adamc@758 295 val compare = MonoUtil.Typ.compare
adamc@758 296 end)
adamc@758 297
adamc@179 298 type t = {
adamc@179 299 count : int,
adamc@179 300 map : int IM.map M.map,
adamc@758 301 listMap : int TM.map M.map,
adamc@179 302 decls : L'.decl list
adamc@179 303 }
adamc@179 304
adamc@179 305 fun empty count = {
adamc@179 306 count = count,
adamc@179 307 map = M.empty,
adamc@758 308 listMap = M.empty,
adamc@179 309 decls = []
adamc@179 310 }
adamc@179 311
adamc@758 312 fun enter ({count, map, listMap, ...} : t) = {count = count, map = map, listMap = listMap, decls = []}
adamc@758 313 fun freshName {count, map, listMap, decls} = (count, {count = count + 1, map = map, listMap = listMap, decls = decls})
adamc@179 314 fun decls ({decls, ...} : t) = decls
adamc@179 315
adamc@758 316 fun lookup (t as {count, map, listMap, decls}) k n thunk =
adamc@120 317 let
adamc@179 318 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 319 in
adamc@179 320 case IM.find (im, n) of
adamc@179 321 NONE =>
adamc@179 322 let
adamc@179 323 val n' = count
adamc@758 324 val (d, {count, map, listMap, decls}) =
adamc@758 325 thunk count {count = count + 1,
adamc@758 326 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@758 327 listMap = listMap,
adamc@758 328 decls = decls}
adamc@179 329 in
adamc@179 330 ({count = count,
adamc@179 331 map = map,
adamc@758 332 listMap = listMap,
adamc@758 333 decls = d :: decls}, n')
adamc@758 334 end
adamc@758 335 | SOME n' => (t, n')
adamc@758 336 end
adamc@758 337
adamc@758 338 fun lookupList (t as {count, map, listMap, decls}) k tp thunk =
adamc@758 339 let
adamc@758 340 val tm = Option.getOpt (M.find (listMap, k), TM.empty)
adamc@758 341 in
adamc@758 342 case TM.find (tm, tp) of
adamc@758 343 NONE =>
adamc@758 344 let
adamc@758 345 val n' = count
adamc@758 346 val (d, {count, map, listMap, decls}) =
adamc@758 347 thunk count {count = count + 1,
adamc@758 348 map = map,
adamc@758 349 listMap = M.insert (listMap, k, TM.insert (tm, tp, n')),
adamc@758 350 decls = decls}
adamc@758 351 in
adamc@758 352 ({count = count,
adamc@758 353 map = map,
adamc@758 354 listMap = listMap,
adamc@179 355 decls = d :: decls}, n')
adamc@179 356 end
adamc@179 357 | SOME n' => (t, n')
adamc@179 358 end
adamc@179 359
adamc@179 360 end
adamc@185 361
adamc@185 362
adamc@185 363 fun capitalize s =
adamc@185 364 if s = "" then
adamc@185 365 s
adamc@185 366 else
adamc@185 367 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 368
adamc@179 369 fun fooifyExp fk env =
adamc@179 370 let
adamc@179 371 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 372 case #1 e of
adamc@120 373 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 374 let
adamc@120 375 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 376 in
adamc@764 377 ((L'.EPrim (Prim.String (Settings.getUrlPrefix () ^ s)), loc), fm)
adamc@120 378 end
adamc@120 379 | L'.EClosure (fnam, args) =>
adamc@120 380 let
adamc@120 381 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 382 val ft = monoType env ft
adamc@111 383
adamc@179 384 fun attrify (args, ft, e, fm) =
adamc@120 385 case (args, ft) of
adamc@179 386 ([], _) => (e, fm)
adamc@120 387 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 388 let
adamc@179 389 val (arg', fm) = fooify fm (arg, t)
adamc@179 390 in
adamc@179 391 attrify (args, ft,
adamc@179 392 (L'.EStrcat (e,
adamc@179 393 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 394 arg'), loc)), loc),
adamc@179 395 fm)
adamc@179 396 end
adamc@120 397 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 398 (e, fm))
adamc@120 399 in
adamc@764 400 attrify (args, ft, (L'.EPrim (Prim.String (Settings.getUrlPrefix () ^ s)), loc), fm)
adamc@120 401 end
adamc@120 402 | _ =>
adamc@120 403 case t of
adamc@185 404 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 405
adamc@179 406 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 407 | L'.TRecord ((x, t) :: xts) =>
adamc@200 408 let
adamc@200 409 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 410 in
adamc@200 411 foldl (fn ((x, t), (se, fm)) =>
adamc@200 412 let
adamc@200 413 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 414 in
adamc@200 415 ((L'.EStrcat (se,
adamc@200 416 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 417 se'), loc)), loc),
adamc@200 418 fm)
adamc@200 419 end) (se, fm) xts
adamc@200 420 end
adamc@111 421
adamc@196 422 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 423 let
adamc@179 424 fun makeDecl n fm =
adamc@179 425 let
adamc@193 426 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 427
adamc@179 428 val (branches, fm) =
adamc@179 429 ListUtil.foldlMap
adamc@179 430 (fn ((x, n, to), fm) =>
adamc@179 431 case to of
adamc@179 432 NONE =>
adamc@188 433 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 434 (L'.EPrim (Prim.String x), loc)),
adamc@179 435 fm)
adamc@179 436 | SOME t =>
adamc@179 437 let
adamc@182 438 val t = monoType env t
adamc@182 439 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 440 in
adamc@188 441 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 442 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 443 arg), loc)),
adamc@179 444 fm)
adamc@179 445 end)
adamc@179 446 fm xncs
adamc@179 447
adamc@179 448 val dom = tAll
adamc@179 449 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 450 in
adamc@179 451 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 452 n,
adamc@179 453 (L'.TFun (dom, ran), loc),
adamc@179 454 (L'.EAbs ("x",
adamc@179 455 dom,
adamc@179 456 ran,
adamc@179 457 (L'.ECase ((L'.ERel 0, loc),
adamc@179 458 branches,
adamc@182 459 {disc = dom,
adamc@182 460 result = ran}), loc)), loc),
adamc@179 461 "")], loc),
adamc@179 462 fm)
adamc@179 463 end
adamc@179 464
adamc@179 465 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 466 in
adamc@179 467 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 468 end
adamc@164 469
adamc@471 470 | L'.TOption t =>
adamc@471 471 let
adamc@471 472 val (body, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@471 473 in
adamc@471 474 ((L'.ECase (e,
adamc@471 475 [((L'.PNone t, loc),
adamc@471 476 (L'.EPrim (Prim.String "None"), loc)),
adamc@471 477
adamc@471 478 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc),
adamc@471 479 (L'.EStrcat ((L'.EPrim (Prim.String "Some/"), loc),
adamc@471 480 body), loc))],
adamc@471 481 {disc = tAll,
adamc@471 482 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@471 483 fm)
adamc@471 484 end
adamc@471 485
adamc@758 486 | L'.TList t =>
adamc@758 487 let
adamc@758 488 fun makeDecl n fm =
adamc@758 489 let
adamc@758 490 val rt = (L'.TRecord [("1", t), ("2", (L'.TList t, loc))], loc)
adamc@758 491 val (arg, fm) = fooify fm ((L'.ERel 0, loc), rt)
adamc@758 492
adamc@758 493 val branches = [((L'.PNone rt, loc),
adamc@758 494 (L'.EPrim (Prim.String "Nil"), loc)),
adamc@758 495 ((L'.PSome (rt, (L'.PVar ("a", rt), loc)), loc),
adamc@758 496 (L'.EStrcat ((L'.EPrim (Prim.String "Cons/"), loc),
adamc@758 497 arg), loc))]
adamc@758 498
adamc@758 499 val dom = tAll
adamc@758 500 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@758 501 in
adamc@758 502 ((L'.DValRec [(fk2s fk ^ "ify_list",
adamc@758 503 n,
adamc@758 504 (L'.TFun (dom, ran), loc),
adamc@758 505 (L'.EAbs ("x",
adamc@758 506 dom,
adamc@758 507 ran,
adamc@758 508 (L'.ECase ((L'.ERel 0, loc),
adamc@758 509 branches,
adamc@758 510 {disc = dom,
adamc@758 511 result = ran}), loc)), loc),
adamc@758 512 "")], loc),
adamc@758 513 fm)
adamc@758 514 end
adamc@758 515
adamc@758 516 val (fm, n) = Fm.lookupList fm fk t makeDecl
adamc@758 517 in
adamc@758 518 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@758 519 end
adamc@758 520
adamc@490 521 | _ => (E.errorAt loc "Don't know how to encode attribute/URL type";
adamc@120 522 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 523 (dummyExp, fm))
adamc@120 524 in
adamc@120 525 fooify
adamc@120 526 end
adamc@120 527
adamc@179 528 val attrifyExp = fooifyExp Attr
adamc@179 529 val urlifyExp = fooifyExp Url
adamc@105 530
adamc@143 531 datatype 'a failable_search =
adamc@143 532 Found of 'a
adamc@143 533 | NotFound
adamc@143 534 | Error
adamc@143 535
adamc@153 536 structure St :> sig
adamc@153 537 type t
adamc@153 538
adamc@153 539 val empty : t
adamc@153 540
adamc@153 541 val radioGroup : t -> string option
adamc@153 542 val setRadioGroup : t * string -> t
adamc@153 543 end = struct
adamc@153 544
adamc@153 545 type t = {
adamc@153 546 radioGroup : string option
adamc@153 547 }
adamc@153 548
adamc@153 549 val empty = {radioGroup = NONE}
adamc@153 550
adamc@153 551 fun radioGroup (t : t) = #radioGroup t
adamc@153 552
adamc@153 553 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 554
adamc@153 555 end
adamc@153 556
adamc@186 557 fun monoPatCon env pc =
adamc@178 558 case pc of
adamc@178 559 L.PConVar n => L'.PConVar n
adamc@188 560 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 561 arg = Option.map (monoType env) arg}
adamc@178 562
adamc@193 563 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 564
adamc@757 565
adamc@757 566 fun listify t = (L'.TRecord [("1", t), ("2", (L'.TList t, #2 t))], #2 t)
adamc@757 567
adamc@193 568 fun monoPat env (all as (p, loc)) =
adamc@193 569 let
adamc@193 570 fun poly () =
adamc@193 571 (E.errorAt loc "Unsupported pattern";
adamc@193 572 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 573 dummyPat)
adamc@193 574 in
adamc@193 575 case p of
adamc@193 576 L.PWild => (L'.PWild, loc)
adamc@193 577 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 578 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 579 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@757 580 | L.PCon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], NONE) =>
adamc@757 581 (L'.PNone (listify (monoType env t)), loc)
adamc@757 582 | L.PCon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], SOME p) =>
adamc@757 583 (L'.PSome (listify (monoType env t), monoPat env p), loc)
adamc@288 584 | L.PCon (L.Option, _, [t], NONE) => (L'.PNone (monoType env t), loc)
adamc@757 585 | L.PCon (L.Option, pc, [t], SOME p) => (L'.PSome (monoType env t, monoPat env p), loc)
adamc@193 586 | L.PCon _ => poly ()
adamc@193 587 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 588 end
adamc@178 589
adamc@252 590 fun strcat loc es =
adamc@252 591 case es of
adamc@252 592 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 593 | [e] => e
adamc@252 594 | _ =>
adamc@252 595 let
adamc@252 596 val e2 = List.last es
adamc@252 597 val es = List.take (es, length es - 1)
adamc@252 598 val e1 = List.last es
adamc@252 599 val es = List.take (es, length es - 1)
adamc@252 600 in
adamc@252 601 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
adamc@252 602 (L'.EStrcat (e1, e2), loc) es
adamc@252 603 end
adamc@252 604
adamc@252 605 fun strcatComma loc es =
adamc@252 606 case es of
adamc@252 607 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 608 | [e] => e
adamc@252 609 | _ =>
adamc@252 610 let
adamc@252 611 val e1 = List.last es
adamc@252 612 val es = List.take (es, length es - 1)
adamc@252 613 in
adamc@252 614 foldr (fn (e, e') =>
adamc@265 615 case (e, e') of
adamc@265 616 ((L'.EPrim (Prim.String ""), _), _) => e'
adamc@265 617 | (_, (L'.EPrim (Prim.String ""), _)) => e
adamc@252 618 | _ =>
adamc@252 619 (L'.EStrcat (e,
adamc@252 620 (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc))
adamc@252 621 e1 es
adamc@252 622 end
adamc@252 623
adamc@252 624 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
adamc@252 625
adamc@735 626 val readCookie = ref IS.empty
adamc@735 627
adamc@179 628 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 629 let
adamc@598 630 val strcat = strcat loc
adamc@598 631 val strcatComma = strcatComma loc
adamc@598 632 fun str s = (L'.EPrim (Prim.String s), loc)
adamc@598 633
adamc@25 634 fun poly () =
adamc@25 635 (E.errorAt loc "Unsupported expression";
adamc@25 636 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 637 (dummyExp, fm))
adamc@389 638
adamc@389 639 fun numTy t =
adamc@417 640 (L'.TRecord [("Zero", t),
adamc@417 641 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 642 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 643 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 644 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 645 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 646 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc)
adamc@417 647 fun numEx (t, zero, neg, plus, minus, times, dv, md) =
adamc@417 648 ((L'.ERecord [("Zero", (L'.EPrim zero, loc), t),
adamc@417 649 ("Neg", neg, (L'.TFun (t, t), loc)),
adamc@389 650 ("Plus", plus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 651 ("Minus", minus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 652 ("Times", times, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 653 ("Div", dv, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 654 ("Mod", md, (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc), fm)
adamc@391 655
adamc@391 656 fun ordTy t =
adamc@391 657 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 658 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))], loc)
adamc@391 659 fun ordEx (t, lt, le) =
adamc@391 660 ((L'.ERecord [("Lt", lt, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 661 ("Le", le, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 662 loc), fm)
adamc@750 663
adamc@750 664 fun outerRec xts =
adamc@750 665 (L'.TRecord (map (fn ((L.CName x, _), (L.CRecord (_, xts), _)) =>
adamc@750 666 (x, (L'.TRecord (map (fn (x', _) => (x, (L'.TRecord [], loc))) xts), loc))
adamc@750 667 | (x, all as (_, loc)) =>
adamc@750 668 (E.errorAt loc "Unsupported record field constructor";
adamc@750 669 Print.eprefaces' [("Name", CorePrint.p_con env x),
adamc@750 670 ("Constructor", CorePrint.p_con env all)];
adamc@750 671 ("", dummyTyp))) xts), loc)
adamc@25 672 in
adamc@25 673 case e of
adamc@179 674 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 675 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 676 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 677 | L.ECon (dk, pc, [], eo) =>
adamc@193 678 let
adamc@179 679 val (eo, fm) =
adamc@179 680 case eo of
adamc@179 681 NONE => (NONE, fm)
adamc@179 682 | SOME e =>
adamc@179 683 let
adamc@179 684 val (e, fm) = monoExp (env, st, fm) e
adamc@179 685 in
adamc@179 686 (SOME e, fm)
adamc@179 687 end
adamc@179 688 in
adamc@188 689 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 690 end
adamc@757 691 | L.ECon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], NONE) =>
adamc@757 692 ((L'.ENone (listify (monoType env t)), loc), fm)
adamc@757 693 | L.ECon (L.Option, L.PConFfi {mod = "Basis", datatyp = "list", ...}, [t], SOME e) =>
adamc@757 694 let
adamc@757 695 val (e, fm) = monoExp (env, st, fm) e
adamc@757 696 in
adamc@757 697 ((L'.ESome (listify (monoType env t), e), loc), fm)
adamc@757 698 end
adamc@297 699 | L.ECon (L.Option, _, [t], NONE) =>
adamc@297 700 ((L'.ENone (monoType env t), loc), fm)
adamc@297 701 | L.ECon (L.Option, _, [t], SOME e) =>
adamc@297 702 let
adamc@297 703 val (e, fm) = monoExp (env, st, fm) e
adamc@297 704 in
adamc@297 705 ((L'.ESome (monoType env t, e), loc), fm)
adamc@297 706 end
adamc@193 707 | L.ECon _ => poly ()
adamc@94 708
adamc@387 709 | L.ECApp ((L.EFfi ("Basis", "eq"), _), t) =>
adamc@387 710 let
adamc@387 711 val t = monoType env t
adamc@387 712 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 713 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 714 in
adamc@387 715 ((L'.EAbs ("f", dom, dom,
adamc@387 716 (L'.ERel 0, loc)), loc), fm)
adamc@387 717 end
adamc@387 718 | L.ECApp ((L.EFfi ("Basis", "ne"), _), t) =>
adamc@387 719 let
adamc@387 720 val t = monoType env t
adamc@387 721 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 722 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 723 in
adamc@387 724 ((L'.EAbs ("f", dom, dom,
adamc@387 725 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@387 726 (L'.EAbs ("y", t, b,
adamc@387 727 (L'.EUnop ("!", (L'.EApp ((L'.EApp ((L'.ERel 2, loc),
adamc@387 728 (L'.ERel 1, loc)), loc),
adamc@387 729 (L'.ERel 0, loc)), loc)), loc)),
adamc@387 730 loc)),
adamc@387 731 loc)),
adamc@387 732 loc), fm)
adamc@387 733 end
adamc@387 734 | L.EFfi ("Basis", "eq_int") =>
adamc@387 735 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@387 736 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@387 737 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@387 738 (L'.TFfi ("Basis", "bool"), loc),
adamc@387 739 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@387 740 fm)
adamc@394 741 | L.EFfi ("Basis", "eq_float") =>
adamc@394 742 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@394 743 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 744 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@394 745 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 746 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@394 747 fm)
adamc@388 748 | L.EFfi ("Basis", "eq_bool") =>
adamc@388 749 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 750 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 751 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 752 (L'.TFfi ("Basis", "bool"), loc),
adamc@388 753 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 754 fm)
adamc@388 755 | L.EFfi ("Basis", "eq_string") =>
adamc@388 756 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@388 757 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 758 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@388 759 (L'.TFfi ("Basis", "bool"), loc),
adamc@388 760 (L'.EBinop ("!strcmp", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 761 fm)
adamc@437 762 | L.EFfi ("Basis", "eq_time") =>
adamc@437 763 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc),
adamc@437 764 (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@437 765 (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc),
adamc@437 766 (L'.TFfi ("Basis", "bool"), loc),
adamc@437 767 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@437 768 fm)
adamc@422 769 | L.ECApp ((L.EFfi ("Basis", "mkEq"), _), t) =>
adamc@422 770 let
adamc@422 771 val t = monoType env t
adamc@422 772 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@422 773 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@422 774 in
adamc@422 775 ((L'.EAbs ("f", dom, dom,
adamc@422 776 (L'.ERel 0, loc)), loc), fm)
adamc@422 777 end
adamc@387 778
adamc@417 779 | L.ECApp ((L.EFfi ("Basis", "zero"), _), t) =>
adamc@417 780 let
adamc@417 781 val t = monoType env t
adamc@417 782 in
adamc@417 783 ((L'.EAbs ("r", numTy t, t,
adamc@417 784 (L'.EField ((L'.ERel 0, loc), "Zero"), loc)), loc), fm)
adamc@417 785 end
adamc@389 786 | L.ECApp ((L.EFfi ("Basis", "neg"), _), t) =>
adamc@389 787 let
adamc@389 788 val t = monoType env t
adamc@389 789 in
adamc@389 790 ((L'.EAbs ("r", numTy t, (L'.TFun (t, t), loc),
adamc@389 791 (L'.EField ((L'.ERel 0, loc), "Neg"), loc)), loc), fm)
adamc@389 792 end
adamc@389 793 | L.ECApp ((L.EFfi ("Basis", "plus"), _), t) =>
adamc@389 794 let
adamc@389 795 val t = monoType env t
adamc@389 796 in
adamc@389 797 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 798 (L'.EField ((L'.ERel 0, loc), "Plus"), loc)), loc), fm)
adamc@389 799 end
adamc@389 800 | L.ECApp ((L.EFfi ("Basis", "minus"), _), t) =>
adamc@389 801 let
adamc@389 802 val t = monoType env t
adamc@389 803 in
adamc@389 804 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 805 (L'.EField ((L'.ERel 0, loc), "Minus"), loc)), loc), fm)
adamc@389 806 end
adamc@389 807 | L.ECApp ((L.EFfi ("Basis", "times"), _), t) =>
adamc@389 808 let
adamc@389 809 val t = monoType env t
adamc@389 810 in
adamc@389 811 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 812 (L'.EField ((L'.ERel 0, loc), "Times"), loc)), loc), fm)
adamc@389 813 end
adamc@389 814 | L.ECApp ((L.EFfi ("Basis", "div"), _), t) =>
adamc@389 815 let
adamc@389 816 val t = monoType env t
adamc@389 817 in
adamc@389 818 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 819 (L'.EField ((L'.ERel 0, loc), "Div"), loc)), loc), fm)
adamc@389 820 end
adamc@389 821 | L.ECApp ((L.EFfi ("Basis", "mod"), _), t) =>
adamc@389 822 let
adamc@389 823 val t = monoType env t
adamc@389 824 in
adamc@389 825 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 826 (L'.EField ((L'.ERel 0, loc), "Mod"), loc)), loc), fm)
adamc@389 827 end
adamc@389 828 | L.EFfi ("Basis", "num_int") =>
adamc@389 829 let
adamc@389 830 fun intBin s =
adamc@389 831 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 832 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "int"), loc)), loc),
adamc@389 833 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@389 834 (L'.TFfi ("Basis", "int"), loc),
adamc@389 835 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@389 836 in
adamc@389 837 numEx ((L'.TFfi ("Basis", "int"), loc),
adamc@417 838 Prim.Int (Int64.fromInt 0),
adamc@389 839 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 840 (L'.TFfi ("Basis", "int"), loc),
adamc@389 841 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@389 842 intBin "+",
adamc@389 843 intBin "-",
adamc@389 844 intBin "*",
adamc@389 845 intBin "/",
adamc@389 846 intBin "%")
adamc@389 847 end
adamc@390 848 | L.EFfi ("Basis", "num_float") =>
adamc@390 849 let
adamc@390 850 fun floatBin s =
adamc@390 851 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 852 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "float"), loc)), loc),
adamc@390 853 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@390 854 (L'.TFfi ("Basis", "float"), loc),
adamc@390 855 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@390 856 in
adamc@390 857 numEx ((L'.TFfi ("Basis", "float"), loc),
adamc@417 858 Prim.Float 0.0,
adamc@390 859 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 860 (L'.TFfi ("Basis", "float"), loc),
adamc@390 861 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@390 862 floatBin "+",
adamc@390 863 floatBin "-",
adamc@390 864 floatBin "*",
adamc@390 865 floatBin "/",
adamc@390 866 floatBin "fmod")
adamc@390 867 end
adamc@391 868
adamc@391 869 | L.ECApp ((L.EFfi ("Basis", "lt"), _), t) =>
adamc@391 870 let
adamc@391 871 val t = monoType env t
adamc@391 872 in
adamc@391 873 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
adamc@391 874 (L'.EField ((L'.ERel 0, loc), "Lt"), loc)), loc), fm)
adamc@391 875 end
adamc@391 876 | L.ECApp ((L.EFfi ("Basis", "le"), _), t) =>
adamc@391 877 let
adamc@391 878 val t = monoType env t
adamc@391 879 in
adamc@391 880 ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc),
adamc@391 881 (L'.EField ((L'.ERel 0, loc), "Le"), loc)), loc), fm)
adamc@391 882 end
adamc@392 883 | L.ECApp ((L.EFfi ("Basis", "gt"), _), t) =>
adamc@392 884 let
adamc@392 885 val t = monoType env t
adamc@392 886 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@392 887 in
adamc@392 888 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
adamc@392 889 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@392 890 (L'.EAbs ("y", t, b,
adamc@392 891 (L'.EUnop ("!",
adamc@392 892 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
adamc@392 893 "Le"), loc),
adamc@392 894 (L'.ERel 1, loc)), loc),
adamc@392 895 (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@392 896 loc)),
adamc@392 897 loc), fm)
adamc@392 898 end
adamc@392 899 | L.ECApp ((L.EFfi ("Basis", "ge"), _), t) =>
adamc@392 900 let
adamc@392 901 val t = monoType env t
adamc@392 902 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@392 903 in
adamc@392 904 ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc),
adamc@392 905 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@392 906 (L'.EAbs ("y", t, b,
adamc@392 907 (L'.EUnop ("!",
adamc@392 908 (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc),
adamc@392 909 "Lt"), loc),
adamc@392 910 (L'.ERel 1, loc)), loc),
adamc@392 911 (L'.ERel 0, loc)), loc)), loc)), loc)),
adamc@392 912 loc)),
adamc@392 913 loc), fm)
adamc@392 914 end
adamc@391 915 | L.EFfi ("Basis", "ord_int") =>
adamc@391 916 let
adamc@391 917 fun intBin s =
adamc@391 918 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@391 919 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@391 920 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@391 921 (L'.TFfi ("Basis", "bool"), loc),
adamc@391 922 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@391 923 in
adamc@391 924 ordEx ((L'.TFfi ("Basis", "int"), loc),
adamc@391 925 intBin "<",
adamc@391 926 intBin "<=")
adamc@391 927 end
adamc@394 928 | L.EFfi ("Basis", "ord_float") =>
adamc@394 929 let
adamc@394 930 fun floatBin s =
adamc@394 931 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@394 932 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 933 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@394 934 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 935 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@394 936 in
adamc@394 937 ordEx ((L'.TFfi ("Basis", "float"), loc),
adamc@394 938 floatBin "<",
adamc@394 939 floatBin "<=")
adamc@394 940 end
adamc@394 941 | L.EFfi ("Basis", "ord_bool") =>
adamc@394 942 let
adamc@394 943 fun boolBin s =
adamc@394 944 (L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@394 945 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@394 946 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@394 947 (L'.TFfi ("Basis", "bool"), loc),
adamc@394 948 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@394 949 in
adamc@394 950 ordEx ((L'.TFfi ("Basis", "bool"), loc),
adamc@394 951 boolBin "<",
adamc@394 952 boolBin "<=")
adamc@394 953 end
adamc@395 954 | L.EFfi ("Basis", "ord_string") =>
adamc@395 955 let
adamc@395 956 fun boolBin s =
adamc@395 957 (L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@395 958 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@395 959 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@395 960 (L'.TFfi ("Basis", "bool"), loc),
adamc@395 961 (L'.EBinop (s,
adamc@395 962 (L'.EBinop ("strcmp",
adamc@395 963 (L'.ERel 1, loc),
adamc@395 964 (L'.ERel 0, loc)), loc),
adamc@395 965 (L'.EPrim (Prim.Int (Int64.fromInt 0)), loc)), loc)), loc)), loc)
adamc@395 966 in
adamc@395 967 ordEx ((L'.TFfi ("Basis", "string"), loc),
adamc@395 968 boolBin "<",
adamc@395 969 boolBin "<=")
adamc@395 970 end
adamc@437 971 | L.EFfi ("Basis", "ord_time") =>
adamc@437 972 let
adamc@437 973 fun boolBin s =
adamc@437 974 (L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc),
adamc@437 975 (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@437 976 (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc),
adamc@437 977 (L'.TFfi ("Basis", "bool"), loc),
adamc@437 978 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@437 979 in
adamc@437 980 ordEx ((L'.TFfi ("Basis", "time"), loc),
adamc@437 981 boolBin "<",
adamc@437 982 boolBin "<=")
adamc@437 983 end
adamc@389 984
adamc@286 985 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
adamc@286 986 let
adamc@286 987 val t = monoType env t
adamc@286 988 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 989 in
adamc@286 990 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@286 991 (L'.ERel 0, loc)), loc), fm)
adamc@286 992 end
adamc@286 993 | L.EFfi ("Basis", "show_int") =>
adamc@286 994 ((L'.EFfi ("Basis", "intToString"), loc), fm)
adamc@286 995 | L.EFfi ("Basis", "show_float") =>
adamc@286 996 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
adamc@286 997 | L.EFfi ("Basis", "show_string") =>
adamc@286 998 let
adamc@286 999 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 1000 in
adamc@286 1001 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@286 1002 end
adamc@286 1003 | L.EFfi ("Basis", "show_bool") =>
adamc@286 1004 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
adamc@436 1005 | L.EFfi ("Basis", "show_time") =>
adamc@436 1006 ((L'.EFfi ("Basis", "timeToString"), loc), fm)
adamc@727 1007 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "show_xml"), _), _),_), _), _), _) =>
adamc@727 1008 let
adamc@727 1009 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@727 1010 in
adamc@727 1011 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@727 1012 end
adamc@452 1013 | L.ECApp ((L.EFfi ("Basis", "mkShow"), _), t) =>
adamc@452 1014 let
adamc@452 1015 val t = monoType env t
adamc@452 1016 val b = (L'.TFfi ("Basis", "string"), loc)
adamc@452 1017 val dom = (L'.TFun (t, b), loc)
adamc@452 1018 in
adamc@452 1019 ((L'.EAbs ("f", dom, dom,
adamc@452 1020 (L'.ERel 0, loc)), loc), fm)
adamc@452 1021 end
adamc@286 1022
adamc@290 1023 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
adamc@290 1024 let
adamc@290 1025 val t = monoType env t
adamc@290 1026 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 1027 in
adamc@292 1028 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
adamc@292 1029 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
adamc@292 1030 end
adamc@292 1031 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
adamc@292 1032 let
adamc@292 1033 val t = monoType env t
adamc@292 1034 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@292 1035 in
adamc@292 1036 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
adamc@292 1037 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
adamc@290 1038 end
adamc@290 1039 | L.EFfi ("Basis", "read_int") =>
adamc@292 1040 let
adamc@292 1041 val t = (L'.TFfi ("Basis", "int"), loc)
adamc@292 1042 in
adamc@292 1043 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
adamc@292 1044 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
adamc@292 1045 loc),
adamc@292 1046 fm)
adamc@292 1047 end
adamc@290 1048 | L.EFfi ("Basis", "read_float") =>
adamc@292 1049 let
adamc@292 1050 val t = (L'.TFfi ("Basis", "float"), loc)
adamc@292 1051 in
adamc@292 1052 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
adamc@292 1053 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
adamc@292 1054 loc),
adamc@292 1055 fm)
adamc@292 1056 end
adamc@290 1057 | L.EFfi ("Basis", "read_string") =>
adamc@290 1058 let
adamc@290 1059 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 1060 in
adamc@292 1061 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
adamc@292 1062 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
adamc@292 1063 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
adamc@292 1064 fm)
adamc@290 1065 end
adamc@290 1066 | L.EFfi ("Basis", "read_bool") =>
adamc@292 1067 let
adamc@292 1068 val t = (L'.TFfi ("Basis", "bool"), loc)
adamc@292 1069 in
adamc@292 1070 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
adamc@292 1071 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
adamc@292 1072 loc),
adamc@292 1073 fm)
adamc@292 1074 end
adamc@436 1075 | L.EFfi ("Basis", "read_time") =>
adamc@436 1076 let
adamc@436 1077 val t = (L'.TFfi ("Basis", "time"), loc)
adamc@436 1078 in
adamc@436 1079 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToTime"), loc), readType' (t, loc)),
adamc@436 1080 ("ReadError", (L'.EFfi ("Basis", "stringToTime_error"), loc), readErrType (t, loc))],
adamc@436 1081 loc),
adamc@436 1082 fm)
adamc@436 1083 end
adamc@290 1084
adamc@564 1085 | L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), _), _), t), _),
adamc@564 1086 (L.EFfi ("Basis", "transaction_monad"), _)) =>
adamc@252 1087 let
adamc@252 1088 val t = monoType env t
adamc@252 1089 in
adamc@252 1090 ((L'.EAbs ("x", t,
adamc@252 1091 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@252 1092 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@252 1093 (L'.ERel 1, loc)), loc)), loc), fm)
adamc@252 1094 end
adamc@564 1095 | L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _),
adamc@564 1096 (L.EFfi ("Basis", "transaction_monad"), _)) =>
adamc@251 1097 let
adamc@251 1098 val t1 = monoType env t1
adamc@251 1099 val t2 = monoType env t2
adamc@251 1100 val un = (L'.TRecord [], loc)
adamc@252 1101 val mt1 = (L'.TFun (un, t1), loc)
adamc@252 1102 val mt2 = (L'.TFun (un, t2), loc)
adamc@251 1103 in
adamc@572 1104 ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc)), loc),
adamc@572 1105 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc),
adamc@252 1106 (L'.EAbs ("_", un, un,
adamc@252 1107 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
adamc@252 1108 (L'.ERecord [], loc)), loc),
adamc@252 1109 (L'.EApp (
adamc@252 1110 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
adamc@252 1111 (L'.ERecord [], loc)),
adamc@252 1112 loc)), loc)), loc)), loc)), loc),
adamc@251 1113 fm)
adamc@251 1114 end
adamc@697 1115
adamc@670 1116 | L.EApp ((L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), _), _), t2), _),
adamc@670 1117 (L.EFfi ("Basis", "transaction_monad"), _)), _),
adamc@670 1118 (L.EApp ((L.ECApp ((L.EFfi ("Basis", "recv"), _), t1), _),
adamc@670 1119 ch), loc)) =>
adamc@670 1120 let
adamc@670 1121 val t1 = monoType env t1
adamc@670 1122 val t2 = monoType env t2
adamc@670 1123 val un = (L'.TRecord [], loc)
adamc@670 1124 val mt2 = (L'.TFun (un, t2), loc)
adamc@670 1125 val (ch, fm) = monoExp (env, st, fm) ch
adamc@670 1126 in
adamc@670 1127 ((L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc),
adamc@670 1128 (L'.EAbs ("_", un, un,
adamc@670 1129 (L'.ERecv (liftExpInExp 0 (liftExpInExp 0 ch),
adamc@670 1130 (L'.ERel 1, loc),
adamc@670 1131 t1), loc)), loc)), loc),
adamc@670 1132 fm)
adamc@670 1133 end
adamc@697 1134 | L.EFfiApp ("Basis", "recv", _) => poly ()
adamc@697 1135
adamc@695 1136 | L.EApp ((L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), _), _), t2), _),
adamc@695 1137 (L.EFfi ("Basis", "transaction_monad"), _)), _),
adamc@695 1138 (L.EAbs (_, _, _,
adamc@695 1139 (L.EFfiApp ("Basis", "sleep", [n]), _)), loc)) =>
adamc@695 1140 let
adamc@695 1141 val t2 = monoType env t2
adamc@695 1142 val un = (L'.TRecord [], loc)
adamc@695 1143 val mt2 = (L'.TFun (un, t2), loc)
adamc@695 1144 val (n, fm) = monoExp (env, st, fm) n
adamc@695 1145 in
adamc@695 1146 ((L'.EAbs ("m2", (L'.TFun (un, mt2), loc), (L'.TFun (un, un), loc),
adamc@695 1147 (L'.EAbs ("_", un, un,
adamc@697 1148 (L'.ESleep (liftExpInExp 0 n, (L'.EApp ((L'.ERel 1, loc),
adamc@695 1149 (L'.ERecord [], loc)), loc)),
adamc@695 1150 loc)), loc)), loc),
adamc@695 1151 fm)
adamc@695 1152 end
adamc@697 1153 | L.EFfiApp ("Basis", "sleep", _) => poly ()
adamc@251 1154
adamc@565 1155 | L.ECApp ((L.EFfi ("Basis", "source"), _), t) =>
adamc@565 1156 let
adamc@565 1157 val t = monoType env t
adamc@565 1158 in
adamc@577 1159 ((L'.EAbs ("x", t, (L'.TFun ((L'.TRecord [], loc), (L'.TSource, loc)), loc),
adamc@577 1160 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TSource, loc),
adamc@577 1161 (L'.EFfiApp ("Basis", "new_client_source",
adamc@590 1162 [(L'.EJavaScript (L'.Source t, (L'.ERel 1, loc), NONE), loc)]),
adamc@578 1163 loc)), loc)),
adamc@565 1164 loc),
adamc@565 1165 fm)
adamc@565 1166 end
adamc@575 1167 | L.ECApp ((L.EFfi ("Basis", "set"), _), t) =>
adamc@575 1168 let
adamc@575 1169 val t = monoType env t
adamc@575 1170 in
adamc@577 1171 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@575 1172 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@575 1173 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@575 1174 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@575 1175 (L'.EFfiApp ("Basis", "set_client_source",
adamc@577 1176 [(L'.ERel 2, loc),
adamc@590 1177 (L'.EJavaScript (L'.Source t,
adamc@590 1178 (L'.ERel 1, loc), NONE), loc)]),
adamc@575 1179 loc)), loc)), loc)), loc),
adamc@575 1180 fm)
adamc@575 1181 end
adamc@601 1182 | L.ECApp ((L.EFfi ("Basis", "get"), _), t) =>
adamc@601 1183 let
adamc@601 1184 val t = monoType env t
adamc@601 1185 in
adamc@601 1186 ((L'.EAbs ("src", (L'.TSource, loc),
adamc@601 1187 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@601 1188 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@601 1189 (L'.EFfiApp ("Basis", "get_client_source",
adamc@601 1190 [(L'.ERel 1, loc)]),
adamc@601 1191 loc)), loc)), loc),
adamc@601 1192 fm)
adamc@601 1193 end
adamc@565 1194
adamc@694 1195 | L.EFfiApp ("Basis", "spawn", [e]) =>
adamc@694 1196 let
adamc@694 1197 val (e, fm) = monoExp (env, st, fm) e
adamc@694 1198 in
adamc@694 1199 ((L'.EApp (e, (L'.ERecord [], loc)), loc), fm)
adamc@694 1200 end
adamc@694 1201
adamc@568 1202 | L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), _), _), t), _),
adamc@568 1203 (L.EFfi ("Basis", "signal_monad"), _)) =>
adamc@568 1204 let
adamc@568 1205 val t = monoType env t
adamc@568 1206 in
adamc@568 1207 ((L'.EAbs ("x", t, (L'.TSignal t, loc),
adamc@568 1208 (L'.ESignalReturn (L'.ERel 0, loc), loc)), loc),
adamc@568 1209 fm)
adamc@568 1210 end
adamc@572 1211 | L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _),
adamc@572 1212 (L.EFfi ("Basis", "signal_monad"), _)) =>
adamc@572 1213 let
adamc@572 1214 val t1 = monoType env t1
adamc@572 1215 val t2 = monoType env t2
adamc@572 1216 val un = (L'.TRecord [], loc)
adamc@572 1217 val mt1 = (L'.TSignal t1, loc)
adamc@572 1218 val mt2 = (L'.TSignal t2, loc)
adamc@572 1219 in
adamc@572 1220 ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), mt2), loc),
adamc@572 1221 (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), mt2,
adamc@572 1222 (L'.ESignalBind ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@572 1223 fm)
adamc@572 1224 end
adamc@574 1225 | L.ECApp ((L.EFfi ("Basis", "signal"), _), t) =>
adamc@574 1226 let
adamc@574 1227 val t = monoType env t
adamc@574 1228 in
adamc@574 1229 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TSignal t, loc),
adamc@574 1230 (L'.ESignalSource (L'.ERel 0, loc), loc)), loc),
adamc@574 1231 fm)
adamc@574 1232 end
adamc@568 1233
adamc@462 1234 | L.ECApp ((L.EFfi ("Basis", "getCookie"), _), t) =>
adamc@462 1235 let
adamc@462 1236 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1237 val un = (L'.TRecord [], loc)
adamc@462 1238 val t = monoType env t
adamc@462 1239 in
adamc@462 1240 ((L'.EAbs ("c", s, (L'.TFun (un, s), loc),
adamc@462 1241 (L'.EAbs ("_", un, s,
adamc@463 1242 (L'.EUnurlify ((L'.EFfiApp ("Basis", "get_cookie", [(L'.ERel 1, loc)]), loc),
adamc@463 1243 t),
adamc@463 1244 loc)), loc)), loc),
adamc@462 1245 fm)
adamc@462 1246 end
adamc@462 1247
adamc@462 1248 | L.ECApp ((L.EFfi ("Basis", "setCookie"), _), t) =>
adamc@462 1249 let
adamc@462 1250 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 1251 val un = (L'.TRecord [], loc)
adamc@462 1252 val t = monoType env t
adamc@462 1253 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@462 1254 in
adamc@462 1255 ((L'.EAbs ("c", s, (L'.TFun (t, (L'.TFun (un, un), loc)), loc),
adamc@462 1256 (L'.EAbs ("v", t, (L'.TFun (un, un), loc),
adamc@462 1257 (L'.EAbs ("_", un, un,
adamc@764 1258 (L'.EFfiApp ("Basis", "set_cookie", [(L'.EPrim (Prim.String
adamc@764 1259 (Settings.getUrlPrefix ())),
adamc@466 1260 loc),
adamc@466 1261 (L'.ERel 2, loc),
adamc@466 1262 e]), loc)),
adamc@462 1263 loc)), loc)), loc),
adamc@462 1264 fm)
adamc@462 1265 end
adamc@462 1266
adamc@668 1267 | L.ECApp ((L.EFfi ("Basis", "channel"), _), t) =>
adamc@668 1268 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1269 (L'.EFfiApp ("Basis", "new_channel", [(L'.ERecord [], loc)]), loc)), loc),
adamc@668 1270 fm)
adamc@668 1271 | L.ECApp ((L.EFfi ("Basis", "send"), _), t) =>
adamc@668 1272 let
adamc@668 1273 val t = monoType env t
adamc@668 1274 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@668 1275 in
adamc@668 1276 ((L'.EAbs ("ch", (L'.TFfi ("Basis", "channel"), loc),
adamc@668 1277 (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc),
adamc@668 1278 (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc),
adamc@668 1279 (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@668 1280 (L'.EFfiApp ("Basis", "send",
adamc@668 1281 [(L'.ERel 2, loc),
adamc@668 1282 e]),
adamc@668 1283 loc)), loc)), loc)), loc),
adamc@668 1284 fm)
adamc@668 1285 end
adamc@668 1286
adamc@707 1287 | L.ECApp ((L.EFfi ("Basis", "no_primary_key"), _), _) =>
adamc@707 1288 ((L'.EPrim (Prim.String ""), loc),
adamc@707 1289 fm)
adamc@707 1290 | L.ECApp (
adamc@707 1291 (L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "primary_key"), _), _), _), t), _),
adamc@707 1292 nm), _),
adamc@707 1293 (L.CRecord (_, unique), _)) =>
adamc@707 1294 let
adamc@707 1295 val unique = (nm, t) :: unique
adamc@707 1296 val witnesses = (L'.TRecord (map (fn (nm, _) => (monoName env nm, (L'.TRecord [], loc))) unique), loc)
adamc@707 1297 in
adamc@707 1298 ((L'.EAbs ("_", witnesses, (L'.TFfi ("Basis", "string"), loc),
adamc@707 1299 (L'.EPrim (Prim.String
adamc@707 1300 (String.concatWith ", "
adamc@707 1301 (map (fn (x, _) => "uw_" ^ monoName env x) unique))),
adamc@707 1302 loc)), loc),
adamc@707 1303 fm)
adamc@707 1304 end
adamc@707 1305
adamc@704 1306 | L.ECApp ((L.EFfi ("Basis", "no_constraint"), _), _) =>
adamc@704 1307 ((L'.ERecord [], loc),
adamc@704 1308 fm)
adamc@705 1309 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "one_constraint"), _), _), _), _), _), (L.CName name, _)) =>
adamc@704 1310 ((L'.EAbs ("c",
adamc@704 1311 (L'.TFfi ("Basis", "string"), loc),
adamc@704 1312 (L'.TFfi ("Basis", "sql_constraints"), loc),
adamc@704 1313 (L'.ERecord [(name, (L'.ERel 0, loc), (L'.TFfi ("Basis", "string"), loc))], loc)), loc),
adamc@704 1314 fm)
adamc@705 1315 | L.ECApp (
adamc@705 1316 (L.ECApp (
adamc@705 1317 (L.ECApp (
adamc@705 1318 (L.EFfi ("Basis", "join_constraints"), _),
adamc@705 1319 _), _),
adamc@705 1320 _), _),
adamc@705 1321 _) =>
adamc@704 1322 let
adamc@704 1323 val constraints = (L'.TFfi ("Basis", "sql_constraints"), loc)
adamc@704 1324 in
adamc@704 1325 ((L'.EAbs ("cs1", constraints, (L'.TFun (constraints, constraints), loc),
adamc@704 1326 (L'.EAbs ("cs2", constraints, constraints,
adamc@704 1327 (L'.EStrcat ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@704 1328 fm)
adamc@704 1329 end
adamc@704 1330
adamc@705 1331 | L.ECApp (
adamc@705 1332 (L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "unique"), _), _), _), t), _),
adamc@705 1333 nm), _),
adamc@705 1334 (L.CRecord (_, unique), _)) =>
adamc@705 1335 let
adamc@705 1336 val unique = (nm, t) :: unique
adamc@705 1337 in
adamc@705 1338 ((L'.EPrim (Prim.String ("UNIQUE ("
adamc@705 1339 ^ String.concatWith ", " (map (fn (x, _) => "uw_" ^ monoName env x) unique)
adamc@705 1340 ^ ")")), loc),
adamc@705 1341 fm)
adamc@705 1342 end
adamc@704 1343
adamc@712 1344 | L.ECApp ((L.EFfi ("Basis", "linkable_same"), loc), _) =>
adamc@712 1345 ((L'.ERecord [], loc), fm)
adamc@712 1346 | L.ECApp ((L.EFfi ("Basis", "linkable_from_nullable"), loc), _) =>
adamc@712 1347 ((L'.ERecord [], loc), fm)
adamc@712 1348 | L.ECApp ((L.EFfi ("Basis", "linkable_to_nullable"), loc), _) =>
adamc@712 1349 ((L'.ERecord [], loc), fm)
adamc@712 1350
adamc@709 1351 | L.EFfi ("Basis", "mat_nil") =>
adamc@709 1352 let
adamc@709 1353 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1354 val stringE = (L'.EPrim (Prim.String ""), loc)
adamc@709 1355 in
adamc@709 1356 ((L'.ERecord [("1", stringE, string),
adamc@709 1357 ("2", stringE, string)], loc), fm)
adamc@709 1358 end
adamc@709 1359 | L.ECApp (
adamc@709 1360 (L.ECApp (
adamc@709 1361 (L.ECApp (
adamc@709 1362 (L.ECApp (
adamc@709 1363 (L.ECApp (
adamc@712 1364 (L.ECApp (
adamc@712 1365 (L.EFfi ("Basis", "mat_cons"), _),
adamc@712 1366 _), _),
adamc@709 1367 _), _),
adamc@709 1368 _), _),
adamc@709 1369 _), _),
adamc@709 1370 (L.CName nm1, _)), _),
adamc@709 1371 (L.CName nm2, _)) =>
adamc@709 1372 let
adamc@709 1373 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1374 val mat = (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 1375 in
adamc@712 1376 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFun (mat, mat), loc),
adamc@712 1377 (L'.EAbs ("m", mat, mat,
adamc@712 1378 (L'.ECase ((L'.EField ((L'.ERel 0, loc), "1"), loc),
adamc@712 1379 [((L'.PPrim (Prim.String ""), loc),
adamc@712 1380 (L'.ERecord [("1", (L'.EPrim (Prim.String ("uw_" ^ nm1)),
adamc@712 1381 loc), string),
adamc@712 1382 ("2", (L'.EPrim (Prim.String ("uw_" ^ nm2)),
adamc@712 1383 loc), string)], loc)),
adamc@712 1384 ((L'.PWild, loc),
adamc@712 1385 (L'.ERecord [("1", (L'.EStrcat (
adamc@712 1386 (L'.EPrim (Prim.String ("uw_" ^ nm1 ^ ", ")),
adamc@712 1387 loc),
adamc@712 1388 (L'.EField ((L'.ERel 0, loc), "1"), loc)),
adamc@712 1389 loc), string),
adamc@712 1390 ("2", (L'.EStrcat (
adamc@712 1391 (L'.EPrim (Prim.String ("uw_" ^ nm2 ^ ", ")), loc),
adamc@712 1392 (L'.EField ((L'.ERel 0, loc), "2"), loc)),
adamc@712 1393 loc), string)],
adamc@712 1394 loc))],
adamc@712 1395 {disc = string,
adamc@712 1396 result = mat}), loc)), loc)), loc),
adamc@709 1397 fm)
adamc@709 1398 end
adamc@709 1399
adamc@709 1400 | L.ECApp ((L.EFfi ("Basis", "restrict"), _), _) => ((L'.EPrim (Prim.String "RESTRICT"), loc), fm)
adamc@709 1401 | L.ECApp ((L.EFfi ("Basis", "cascade"), _), _) => ((L'.EPrim (Prim.String "CASCADE"), loc), fm)
adamc@709 1402 | L.ECApp ((L.EFfi ("Basis", "no_action"), _), _) => ((L'.EPrim (Prim.String "NO ACTION"), loc), fm)
adamc@709 1403 | L.ECApp ((L.EFfi ("Basis", "set_null"), _), _) => ((L'.EPrim (Prim.String "SET NULL"), loc), fm)
adamc@709 1404
adamc@709 1405 | L.ECApp (
adamc@709 1406 (L.ECApp (
adamc@709 1407 (L.ECApp (
adamc@709 1408 (L.ECApp (
adamc@709 1409 (L.ECApp (
adamc@709 1410 (L.ECApp (
adamc@709 1411 (L.ECApp (
adamc@709 1412 (L.ECApp (
adamc@709 1413 (L.EFfi ("Basis", "foreign_key"), _),
adamc@709 1414 _), _),
adamc@709 1415 _), _),
adamc@709 1416 _), _),
adamc@709 1417 _), _),
adamc@709 1418 _), _),
adamc@709 1419 _), _),
adamc@709 1420 _), _),
adamc@709 1421 _) =>
adamc@709 1422 let
adamc@709 1423 val unit = (L'.TRecord [], loc)
adamc@709 1424 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@709 1425 val mat = (L'.TRecord [("1", string), ("2", string)], loc)
adamc@709 1426 val recd = (L'.TRecord [("OnDelete", string),
adamc@709 1427 ("OnUpdate", string)], loc)
adamc@709 1428
adamc@709 1429 fun strcat [] = raise Fail "Monoize.strcat"
adamc@709 1430 | strcat [e] = e
adamc@709 1431 | strcat (e1 :: es) = (L'.EStrcat (e1, strcat es), loc)
adamc@709 1432
adamc@709 1433 fun prop (fd, kw) =
adamc@709 1434 (L'.ECase ((L'.EField ((L'.ERel 0, loc), fd), loc),
adamc@709 1435 [((L'.PPrim (Prim.String "NO ACTION"), loc),
adamc@709 1436 (L'.EPrim (Prim.String ""), loc)),
adamc@709 1437 ((L'.PWild, loc),
adamc@709 1438 strcat [(L'.EPrim (Prim.String (" ON " ^ kw ^ " ")), loc),
adamc@709 1439 (L'.EField ((L'.ERel 0, loc), fd), loc)])],
adamc@709 1440 {disc = string,
adamc@709 1441 result = string}), loc)
adamc@709 1442 in
adamc@709 1443 ((L'.EAbs ("m", mat, (L'.TFun (string, (L'.TFun (recd, string), loc)), loc),
adamc@709 1444 (L'.EAbs ("tab", string, (L'.TFun (recd, string), loc),
adamc@709 1445 (L'.EAbs ("pr", recd, string,
adamc@709 1446 strcat [(L'.EPrim (Prim.String "FOREIGN KEY ("), loc),
adamc@709 1447 (L'.EField ((L'.ERel 2, loc), "1"), loc),
adamc@709 1448 (L'.EPrim (Prim.String ") REFERENCES "), loc),
adamc@709 1449 (L'.ERel 1, loc),
adamc@709 1450 (L'.EPrim (Prim.String " ("), loc),
adamc@709 1451 (L'.EField ((L'.ERel 2, loc), "2"), loc),
adamc@709 1452 (L'.EPrim (Prim.String ")"), loc),
adamc@709 1453 prop ("OnDelete", "DELETE"),
adamc@709 1454 prop ("OnUpdate", "UPDATE")]), loc)), loc)), loc),
adamc@709 1455 fm)
adamc@709 1456 end
adamc@709 1457
adamc@714 1458 | L.ECApp ((L.EFfi ("Basis", "check"), _), _) =>
adamc@714 1459 let
adamc@714 1460 val string = (L'.TFfi ("Basis", "string"), loc)
adamc@714 1461 in
adamc@714 1462 ((L'.EAbs ("e", string, string,
adamc@714 1463 (L'.EStrcat ((L'.EPrim (Prim.String "CHECK "), loc),
adamc@714 1464 (L'.EFfiApp ("Basis", "checkString",
adamc@714 1465 [(L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@714 1466 fm)
adamc@714 1467 end
adamc@714 1468
adamc@307 1469 | L.EFfiApp ("Basis", "dml", [e]) =>
adamc@307 1470 let
adamc@307 1471 val (e, fm) = monoExp (env, st, fm) e
adamc@307 1472 in
adamc@468 1473 ((L'.EDml e, loc),
adamc@307 1474 fm)
adamc@307 1475 end
adamc@308 1476
adamc@705 1477 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "insert"), _), fields), _), _) =>
adamc@307 1478 (case monoType env (L.TRecord fields, loc) of
adamc@307 1479 (L'.TRecord fields, _) =>
adamc@307 1480 let
adamc@307 1481 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@307 1482 val fields = map (fn (x, _) => (x, s)) fields
adamc@307 1483 val rt = (L'.TRecord fields, loc)
adamc@307 1484 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@307 1485 in
adamc@307 1486 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
adamc@307 1487 (L'.EAbs ("fs", rt, s,
adamc@598 1488 strcat [sc "INSERT INTO ",
adamc@598 1489 (L'.ERel 1, loc),
adamc@598 1490 sc " (",
adamc@598 1491 strcatComma (map (fn (x, _) => sc ("uw_" ^ x)) fields),
adamc@598 1492 sc ") VALUES (",
adamc@598 1493 strcatComma (map (fn (x, _) =>
adamc@598 1494 (L'.EField ((L'.ERel 0, loc),
adamc@598 1495 x), loc)) fields),
adamc@598 1496 sc ")"]), loc)), loc),
adamc@307 1497 fm)
adamc@307 1498 end
adamc@307 1499 | _ => poly ())
adamc@307 1500
adamc@705 1501 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), _), _), changed) =>
adamc@308 1502 (case monoType env (L.TRecord changed, loc) of
adamc@308 1503 (L'.TRecord changed, _) =>
adamc@308 1504 let
adamc@308 1505 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@308 1506 val changed = map (fn (x, _) => (x, s)) changed
adamc@308 1507 val rt = (L'.TRecord changed, loc)
adamc@308 1508 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@308 1509 in
adamc@308 1510 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@308 1511 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@308 1512 (L'.EAbs ("e", s, s,
adamc@598 1513 strcat [sc "UPDATE ",
adamc@598 1514 (L'.ERel 1, loc),
adamc@598 1515 sc " AS T SET ",
adamc@598 1516 strcatComma (map (fn (x, _) =>
adamc@598 1517 strcat [sc ("uw_" ^ x
adamc@598 1518 ^ " = "),
adamc@598 1519 (L'.EField
adamc@598 1520 ((L'.ERel 2,
adamc@598 1521 loc),
adamc@598 1522 x), loc)])
adamc@598 1523 changed),
adamc@598 1524 sc " WHERE ",
adamc@598 1525 (L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@308 1526 fm)
adamc@308 1527 end
adamc@308 1528 | _ => poly ())
adamc@308 1529
adamc@705 1530 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "delete"), _), _), _), _) =>
adamc@309 1531 let
adamc@309 1532 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@309 1533 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@309 1534 in
adamc@309 1535 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@309 1536 (L'.EAbs ("e", s, s,
adamc@598 1537 strcat [sc "DELETE FROM ",
adamc@598 1538 (L'.ERel 1, loc),
adamc@598 1539 sc " AS T WHERE ",
adamc@598 1540 (L'.ERel 0, loc)]), loc)), loc),
adamc@309 1541 fm)
adamc@309 1542 end
adamc@309 1543
adamc@252 1544 | L.ECApp (
adamc@252 1545 (L.ECApp (
adamc@252 1546 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 1547 exps), _),
adamc@252 1548 state) =>
adamc@252 1549 (case monoType env (L.TRecord exps, loc) of
adamc@252 1550 (L'.TRecord exps, _) =>
adamc@252 1551 let
adamc@252 1552 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1553 (case monoType env (L.TRecord xts, loc) of
adamc@252 1554 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1555 | _ => NONE)
adamc@252 1556 | _ => NONE) tables
adamc@252 1557 in
adamc@252 1558 if List.exists (fn x => x = NONE) tables then
adamc@252 1559 poly ()
adamc@252 1560 else
adamc@252 1561 let
adamc@252 1562 val tables = List.mapPartial (fn x => x) tables
adamc@252 1563 val state = monoType env state
adamc@252 1564 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1565 val un = (L'.TRecord [], loc)
adamc@252 1566
adamc@252 1567 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 1568 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 1569 (L'.TFun (state,
adamc@252 1570 (L'.TFun (un, state), loc)),
adamc@252 1571 loc)), loc)
adamc@252 1572
adamc@267 1573 val body' = (L'.EApp (
adamc@267 1574 (L'.EApp (
adamc@267 1575 (L'.EApp ((L'.ERel 4, loc),
adamc@267 1576 (L'.ERel 1, loc)), loc),
adamc@267 1577 (L'.ERel 0, loc)), loc),
adamc@267 1578 (L'.ERecord [], loc)), loc)
adamc@252 1579
adamc@252 1580 val body = (L'.EQuery {exps = exps,
adamc@252 1581 tables = tables,
adamc@252 1582 state = state,
adamc@252 1583 query = (L'.ERel 3, loc),
adamc@252 1584 body = body',
adamc@252 1585 initial = (L'.ERel 1, loc)},
adamc@252 1586 loc)
adamc@252 1587 in
adamc@252 1588 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 1589 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 1590 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 1591 (L'.EAbs ("_", un, state,
adamc@252 1592 body), loc)), loc)), loc)), loc), fm)
adamc@252 1593 end
adamc@252 1594 end
adamc@252 1595 | _ => poly ())
adamc@252 1596
adamc@252 1597 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 1598 let
adamc@252 1599 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1600 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1601 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1602 in
adamc@252 1603 ((L'.EAbs ("r",
adamc@252 1604 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 1605 s,
adamc@598 1606 strcat [gf "Rows",
adamc@598 1607 (L'.ECase (gf "OrderBy",
adamc@598 1608 [((L'.PPrim (Prim.String ""), loc), sc ""),
adamc@598 1609 ((L'.PWild, loc),
adamc@598 1610 strcat [sc " ORDER BY ",
adamc@598 1611 gf "OrderBy"])],
adamc@598 1612 {disc = s, result = s}), loc),
adamc@598 1613 gf "Limit",
adamc@598 1614 gf "Offset"]), loc), fm)
adamc@252 1615 end
adamc@252 1616
adamc@252 1617 | L.ECApp (
adamc@252 1618 (L.ECApp (
adamc@252 1619 (L.ECApp (
adamc@252 1620 (L.ECApp (
adamc@252 1621 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 1622 (L.CRecord (_, tables), _)), _),
adamc@252 1623 (L.CRecord (_, grouped), _)), _),
adamc@252 1624 (L.CRecord (_, stables), _)), _),
adamc@252 1625 sexps) =>
adamc@252 1626 let
adamc@252 1627 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1628 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1629 val un = (L'.TRecord [], loc)
adamc@252 1630 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1631
adamc@252 1632 fun doTables tables =
adamc@252 1633 let
adamc@252 1634 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1635 (case monoType env (L.TRecord xts, loc) of
adamc@252 1636 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1637 | _ => NONE)
adamc@252 1638 | _ => NONE) tables
adamc@252 1639 in
adamc@252 1640 if List.exists (fn x => x = NONE) tables then
adamc@252 1641 NONE
adamc@252 1642 else
adamc@260 1643 let
adamc@260 1644 val tables = List.mapPartial (fn x => x) tables
adamc@260 1645 val tables = ListMergeSort.sort
adamc@260 1646 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1647 tables
adamc@260 1648 val tables = map (fn (x, xts) =>
adamc@260 1649 (x, ListMergeSort.sort
adamc@260 1650 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1651 xts)) tables
adamc@260 1652 in
adamc@260 1653 SOME tables
adamc@260 1654 end
adamc@252 1655 end
adamc@252 1656 in
adamc@252 1657 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 1658 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@441 1659 let
adamc@441 1660 val sexps = ListMergeSort.sort
adamc@441 1661 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) sexps
adamc@441 1662 in
adamc@441 1663 ((L'.EAbs ("r",
adamc@748 1664 (L'.TRecord [("From", s),
adamc@441 1665 ("Where", s),
adamc@441 1666 ("GroupBy", un),
adamc@441 1667 ("Having", s),
adamc@441 1668 ("SelectFields", un),
adamc@441 1669 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@441 1670 loc),
adamc@441 1671 s,
adamc@598 1672 strcat [sc "SELECT ",
adamc@598 1673 strcatComma (map (fn (x, t) =>
adamc@598 1674 strcat [
adamc@598 1675 (L'.EField (gf "SelectExps", x), loc),
adamc@598 1676 sc (" AS _" ^ x)
adamc@598 1677 ]) sexps
adamc@598 1678 @ map (fn (x, xts) =>
adamc@598 1679 strcatComma
adamc@598 1680 (map (fn (x', _) =>
adamc@598 1681 sc (x ^ ".uw_" ^ x'))
adamc@598 1682 xts)) stables),
adamc@598 1683 sc " FROM ",
adamc@748 1684 gf "From",
adamc@598 1685 (L'.ECase (gf "Where",
adamc@598 1686 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@598 1687 sc ""),
adamc@598 1688 ((L'.PWild, loc),
adamc@598 1689 strcat [sc " WHERE ", gf "Where"])],
adamc@598 1690 {disc = s,
adamc@598 1691 result = s}), loc),
adamc@598 1692
adamc@598 1693 if List.all (fn (x, xts) =>
adamc@598 1694 case List.find (fn (x', _) => x' = x) grouped of
adamc@598 1695 NONE => List.null xts
adamc@598 1696 | SOME (_, xts') =>
adamc@598 1697 List.all (fn (x, _) =>
adamc@598 1698 List.exists (fn (x', _) => x' = x)
adamc@598 1699 xts') xts) tables then
adamc@598 1700 sc ""
adamc@598 1701 else
adamc@598 1702 strcat [
adamc@598 1703 sc " GROUP BY ",
adamc@598 1704 strcatComma (map (fn (x, xts) =>
adamc@598 1705 strcatComma
adamc@598 1706 (map (fn (x', _) =>
adamc@598 1707 sc (x ^ ".uw_" ^ x'))
adamc@598 1708 xts)) grouped)
adamc@598 1709 ],
adamc@259 1710
adamc@598 1711 (L'.ECase (gf "Having",
adamc@598 1712 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@598 1713 sc ""),
adamc@598 1714 ((L'.PWild, loc),
adamc@598 1715 strcat [sc " HAVING ", gf "Having"])],
adamc@598 1716 {disc = s,
adamc@598 1717 result = s}), loc)
adamc@441 1718 ]), loc),
adamc@441 1719 fm)
adamc@441 1720 end
adamc@252 1721 | _ => poly ()
adamc@252 1722 end
adamc@252 1723
adamc@252 1724 | L.ECApp (
adamc@252 1725 (L.ECApp (
adamc@252 1726 (L.ECApp (
adamc@252 1727 (L.ECApp (
adamc@252 1728 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 1729 _), _),
adamc@252 1730 _), _),
adamc@252 1731 _), _),
adamc@252 1732 t) =>
adamc@252 1733 let
adamc@252 1734 val t = monoType env t
adamc@252 1735 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1736 in
adamc@252 1737 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 1738 (L'.ERel 0, loc)), loc), fm)
adamc@252 1739 end
adamc@252 1740
adamc@253 1741 | L.EFfi ("Basis", "sql_int") =>
adamc@253 1742 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1743 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1744 fm)
adamc@253 1745 | L.EFfi ("Basis", "sql_float") =>
adamc@253 1746 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1747 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1748 fm)
adamc@253 1749 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 1750 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1751 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1752 fm)
adamc@253 1753 | L.EFfi ("Basis", "sql_string") =>
adamc@253 1754 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1755 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1756 fm)
adamc@439 1757 | L.EFfi ("Basis", "sql_time") =>
adamc@439 1758 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@439 1759 (L'.EFfiApp ("Basis", "sqlifyTime", [(L'.ERel 0, loc)]), loc)), loc),
adamc@439 1760 fm)
adamc@737 1761 | L.EFfi ("Basis", "sql_blob") =>
adamc@737 1762 ((L'.EAbs ("x", (L'.TFfi ("Basis", "blob"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@737 1763 (L'.EFfiApp ("Basis", "sqlifyBlob", [(L'.ERel 0, loc)]), loc)), loc),
adamc@737 1764 fm)
adamc@678 1765 | L.ECApp ((L.EFfi ("Basis", "sql_channel"), _), _) =>
adamc@678 1766 ((L'.EAbs ("x", (L'.TFfi ("Basis", "channel"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@678 1767 (L'.EFfiApp ("Basis", "sqlifyChannel", [(L'.ERel 0, loc)]), loc)), loc),
adamc@678 1768 fm)
adamc@682 1769 | L.EFfi ("Basis", "sql_client") =>
adamc@682 1770 ((L'.EAbs ("x", (L'.TFfi ("Basis", "client"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@682 1771 (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)), loc),
adamc@682 1772 fm)
adamc@676 1773 | L.ECApp ((L.EFfi ("Basis", "sql_prim"), _), t) =>
adamc@676 1774 let
adamc@676 1775 val t = monoType env t
adamc@676 1776 val tf = (L'.TFun (t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@676 1777 in
adamc@676 1778 ((L'.EAbs ("f", tf, tf, (L'.ERel 0, loc)), loc),
adamc@676 1779 fm)
adamc@676 1780 end
adamc@676 1781 | L.ECApp ((L.EFfi ("Basis", "sql_option_prim"), _), t) =>
adamc@676 1782 let
adamc@676 1783 val t = monoType env t
adamc@676 1784 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@676 1785 in
adamc@676 1786 ((L'.EAbs ("f",
adamc@676 1787 (L'.TFun (t, s), loc),
adamc@676 1788 (L'.TFun ((L'.TOption t, loc), s), loc),
adamc@676 1789 (L'.EAbs ("x",
adamc@676 1790 (L'.TOption t, loc),
adamc@676 1791 s,
adamc@676 1792 (L'.ECase ((L'.ERel 0, loc),
adamc@676 1793 [((L'.PNone t, loc),
adamc@676 1794 (L'.EPrim (Prim.String "NULL"), loc)),
adamc@676 1795 ((L'.PSome (t, (L'.PVar ("y", t), loc)), loc),
adamc@676 1796 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc))],
adamc@676 1797 {disc = (L'.TOption t, loc),
adamc@676 1798 result = s}), loc)), loc)), loc),
adamc@676 1799 fm)
adamc@676 1800 end
adamc@253 1801
adamc@750 1802 | L.ECApp ((L.EFfi ("Basis", "nullify_option"), _), _) =>
adamc@750 1803 ((L'.ERecord [], loc), fm)
adamc@750 1804 | L.ECApp ((L.EFfi ("Basis", "nullify_prim"), _), _) =>
adamc@750 1805 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@750 1806 (L'.ERecord [], loc)), loc),
adamc@750 1807 fm)
adamc@750 1808
adamc@252 1809 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 1810 ((L'.ERecord [], loc), fm)
adamc@252 1811 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 1812 ((L'.ERecord [], loc), fm)
adamc@252 1813
adamc@753 1814 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "fieldsOf_table"), _), _), _), _) =>
adamc@753 1815 ((L'.ERecord [], loc), fm)
adamc@753 1816 | L.ECApp ((L.EFfi ("Basis", "fieldsOf_view"), _), _) =>
adamc@753 1817 ((L'.ERecord [], loc), fm)
adamc@753 1818
adamc@753 1819 | L.ECApp ((L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_table"), _), _), _), _), _), _), _),
adamc@753 1820 (L.CName name, _)) =>
adamc@748 1821 let
adamc@748 1822 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@748 1823 in
adamc@748 1824 ((L'.EAbs ("tab", s, s,
adamc@748 1825 strcat [(L'.ERel 0, loc),
adamc@748 1826 (L'.EPrim (Prim.String (" AS " ^ name)), loc)]), loc),
adamc@748 1827 fm)
adamc@748 1828 end
adamc@748 1829 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_from_comma"), _), _), _), _) =>
adamc@748 1830 let
adamc@748 1831 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@748 1832 in
adamc@748 1833 ((L'.EAbs ("tab1", s, (L'.TFun (s, s), loc),
adamc@748 1834 (L'.EAbs ("tab2", s, s,
adamc@748 1835 strcat [(L'.ERel 1, loc),
adamc@748 1836 (L'.EPrim (Prim.String ", "), loc),
adamc@748 1837 (L'.ERel 0, loc)]), loc)), loc),
adamc@748 1838 fm)
adamc@748 1839 end
adamc@749 1840 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_inner_join"), _), _), _), _) =>
adamc@749 1841 let
adamc@749 1842 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@749 1843 in
adamc@749 1844 ((L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@749 1845 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@749 1846 (L'.EAbs ("on", s, s,
adamc@749 1847 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@749 1848 (L'.ERel 2, loc),
adamc@749 1849 (L'.EPrim (Prim.String " JOIN "), loc),
adamc@749 1850 (L'.ERel 1, loc),
adamc@749 1851 (L'.EPrim (Prim.String " ON "), loc),
adamc@749 1852 (L'.ERel 0, loc),
adamc@749 1853 (L'.EPrim (Prim.String ")"), loc)]), loc)), loc)), loc),
adamc@749 1854 fm)
adamc@749 1855 end
adamc@750 1856 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_left_join"), _), _), _), (L.CRecord (_, right), _)) =>
adamc@750 1857 let
adamc@750 1858 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@750 1859 in
adamc@750 1860 ((L'.EAbs ("_", outerRec right,
adamc@750 1861 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@750 1862 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@750 1863 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@750 1864 (L'.EAbs ("on", s, s,
adamc@750 1865 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@750 1866 (L'.ERel 2, loc),
adamc@750 1867 (L'.EPrim (Prim.String " LEFT JOIN "), loc),
adamc@750 1868 (L'.ERel 1, loc),
adamc@750 1869 (L'.EPrim (Prim.String " ON "), loc),
adamc@750 1870 (L'.ERel 0, loc),
adamc@750 1871 (L'.EPrim (Prim.String ")"), loc)]),
adamc@750 1872 loc)), loc)), loc)), loc),
adamc@750 1873 fm)
adamc@750 1874 end
adamc@751 1875 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_right_join"), _), (L.CRecord (_, left), _)), _), _) =>
adamc@751 1876 let
adamc@751 1877 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@751 1878 in
adamc@751 1879 ((L'.EAbs ("_", outerRec left,
adamc@751 1880 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@751 1881 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@751 1882 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@751 1883 (L'.EAbs ("on", s, s,
adamc@751 1884 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@751 1885 (L'.ERel 2, loc),
adamc@751 1886 (L'.EPrim (Prim.String " RIGHT JOIN "), loc),
adamc@751 1887 (L'.ERel 1, loc),
adamc@751 1888 (L'.EPrim (Prim.String " ON "), loc),
adamc@751 1889 (L'.ERel 0, loc),
adamc@751 1890 (L'.EPrim (Prim.String ")"), loc)]),
adamc@751 1891 loc)), loc)), loc)), loc),
adamc@751 1892 fm)
adamc@751 1893 end
adamc@751 1894 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_full_join"), _), (L.CRecord (_, left), _)), _),
adamc@751 1895 (L.CRecord (_, right), _)) =>
adamc@751 1896 let
adamc@751 1897 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@751 1898 in
adamc@751 1899 ((L'.EAbs ("_", outerRec (left @ right),
adamc@751 1900 (L'.TFun (s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc)), loc),
adamc@751 1901 (L'.EAbs ("tab1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@751 1902 (L'.EAbs ("tab2", s, (L'.TFun (s, s), loc),
adamc@751 1903 (L'.EAbs ("on", s, s,
adamc@751 1904 strcat [(L'.EPrim (Prim.String "("), loc),
adamc@751 1905 (L'.ERel 2, loc),
adamc@751 1906 (L'.EPrim (Prim.String " FULL JOIN "), loc),
adamc@751 1907 (L'.ERel 1, loc),
adamc@751 1908 (L'.EPrim (Prim.String " ON "), loc),
adamc@751 1909 (L'.ERel 0, loc),
adamc@751 1910 (L'.EPrim (Prim.String ")"), loc)]),
adamc@751 1911 loc)), loc)), loc)), loc),
adamc@751 1912 fm)
adamc@751 1913 end
adamc@748 1914
adamc@252 1915 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 1916 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@261 1917 | L.ECApp (
adamc@261 1918 (L.ECApp (
adamc@261 1919 (L.ECApp (
adamc@261 1920 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adamc@261 1921 _), _),
adamc@261 1922 _), _),
adamc@261 1923 _) =>
adamc@261 1924 let
adamc@261 1925 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 1926 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@261 1927 in
adamc@268 1928 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@268 1929 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adamc@268 1930 (L'.EAbs ("e2", s, s,
adamc@268 1931 (L'.ECase ((L'.ERel 0, loc),
adamc@268 1932 [((L'.PPrim (Prim.String ""), loc),
adamc@598 1933 strcat [(L'.ERel 2, loc),
adamc@598 1934 (L'.ERel 1, loc)]),
adamc@268 1935 ((L'.PWild, loc),
adamc@598 1936 strcat [(L'.ERel 2, loc),
adamc@598 1937 (L'.ERel 1, loc),
adamc@598 1938 sc ", ",
adamc@598 1939 (L'.ERel 0, loc)])],
adamc@268 1940 {disc = s, result = s}), loc)), loc)), loc)), loc),
adamc@261 1941 fm)
adamc@261 1942 end
adamc@252 1943
adamc@252 1944 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 1945 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@262 1946 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
adamc@262 1947 let
adamc@262 1948 val (e, fm) = monoExp (env, st, fm) e
adamc@262 1949 in
adamc@598 1950 (strcat [
adamc@262 1951 (L'.EPrim (Prim.String " LIMIT "), loc),
adamc@262 1952 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@262 1953 ],
adamc@262 1954 fm)
adamc@262 1955 end
adamc@262 1956
adamc@252 1957 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 1958 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@263 1959 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
adamc@263 1960 let
adamc@263 1961 val (e, fm) = monoExp (env, st, fm) e
adamc@263 1962 in
adamc@598 1963 (strcat [
adamc@263 1964 (L'.EPrim (Prim.String " OFFSET "), loc),
adamc@263 1965 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@263 1966 ],
adamc@263 1967 fm)
adamc@263 1968 end
adamc@253 1969
adamc@559 1970 | L.ECApp ((L.EFfi ("Basis", "sql_eq"), _), _) =>
adamc@253 1971 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@559 1972 | L.ECApp ((L.EFfi ("Basis", "sql_ne"), _), _) =>
adamc@253 1973 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@559 1974 | L.ECApp ((L.EFfi ("Basis", "sql_lt"), _), _) =>
adamc@253 1975 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@559 1976 | L.ECApp ((L.EFfi ("Basis", "sql_le"), _), _) =>
adamc@253 1977 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@559 1978 | L.ECApp ((L.EFfi ("Basis", "sql_gt"), _), _) =>
adamc@253 1979 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@559 1980 | L.ECApp ((L.EFfi ("Basis", "sql_ge"), _), _) =>
adamc@253 1981 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 1982
adamc@559 1983 | L.ECApp ((L.EFfi ("Basis", "sql_plus"), _), _) =>
adamc@559 1984 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1985 (L'.EPrim (Prim.String "+"), loc)), loc), fm)
adamc@559 1986 | L.ECApp ((L.EFfi ("Basis", "sql_minus"), _), _) =>
adamc@559 1987 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1988 (L'.EPrim (Prim.String "-"), loc)), loc), fm)
adamc@559 1989 | L.ECApp ((L.EFfi ("Basis", "sql_times"), _), _) =>
adamc@559 1990 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1991 (L'.EPrim (Prim.String "*"), loc)), loc), fm)
adamc@559 1992 | L.ECApp ((L.EFfi ("Basis", "sql_div"), _), _) =>
adamc@559 1993 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 1994 (L'.EPrim (Prim.String "/"), loc)), loc), fm)
adamc@559 1995 | L.EFfi ("Basis", "sql_mod") =>
adamc@559 1996 ((L'.EPrim (Prim.String "%"), loc), fm)
adamc@559 1997
adamc@253 1998 | L.ECApp (
adamc@253 1999 (L.ECApp (
adamc@253 2000 (L.ECApp (
adamc@253 2001 (L.ECApp (
adamc@254 2002 (L.ECApp (
adamc@264 2003 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 2004 _), _),
adamc@264 2005 _), _),
adamc@264 2006 _), _),
adamc@264 2007 _), _),
adamc@264 2008 _) =>
adamc@264 2009 let
adamc@264 2010 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 2011 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 2012 in
adamc@264 2013 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 2014 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@598 2015 strcat [sc "(",
adamc@598 2016 (L'.ERel 1, loc),
adamc@598 2017 sc " ",
adamc@598 2018 (L'.ERel 0, loc),
adamc@598 2019 sc ")"]), loc)), loc),
adamc@264 2020 fm)
adamc@264 2021 end
adamc@264 2022 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@559 2023 | L.ECApp ((L.EFfi ("Basis", "sql_neg"), _), _) =>
adamc@559 2024 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@559 2025 (L'.EPrim (Prim.String "-"), loc)), loc), fm)
adamc@264 2026
adamc@264 2027 | L.ECApp (
adamc@264 2028 (L.ECApp (
adamc@264 2029 (L.ECApp (
adamc@264 2030 (L.ECApp (
adamc@264 2031 (L.ECApp (
adamc@254 2032 (L.ECApp (
adamc@254 2033 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 2034 _), _),
adamc@254 2035 _), _),
adamc@254 2036 _), _),
adamc@254 2037 _), _),
adamc@254 2038 _), _),
adamc@254 2039 _) =>
adamc@254 2040 let
adamc@254 2041 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 2042 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 2043 in
adamc@254 2044 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 2045 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 2046 (L'.EAbs ("e2", s, s,
adamc@598 2047 strcat [sc "(",
adamc@598 2048 (L'.ERel 1, loc),
adamc@598 2049 sc " ",
adamc@598 2050 (L'.ERel 2, loc),
adamc@598 2051 sc " ",
adamc@598 2052 (L'.ERel 0, loc),
adamc@598 2053 sc ")"]), loc)), loc)), loc),
adamc@254 2054 fm)
adamc@254 2055 end
adamc@254 2056 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 2057 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 2058
adamc@254 2059 | L.ECApp (
adamc@254 2060 (L.ECApp (
adamc@254 2061 (L.ECApp (
adamc@254 2062 (L.ECApp (
adamc@253 2063 (L.ECApp (
adamc@253 2064 (L.ECApp (
adamc@253 2065 (L.ECApp (
adamc@253 2066 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 2067 _), _),
adamc@253 2068 _), _),
adamc@253 2069 _), _),
adamc@253 2070 _), _),
adamc@253 2071 _), _),
adamc@253 2072 (L.CName tab, _)), _),
adamc@311 2073 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
adamc@260 2074
adamc@260 2075 | L.ECApp (
adamc@260 2076 (L.ECApp (
adamc@260 2077 (L.ECApp (
adamc@260 2078 (L.ECApp (
adamc@261 2079 (L.ECApp (
adamc@261 2080 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 2081 _), _),
adamc@261 2082 _), _),
adamc@261 2083 _), _),
adamc@261 2084 _), _),
adamc@261 2085 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 2086
adamc@261 2087 | L.ECApp (
adamc@261 2088 (L.ECApp (
adamc@261 2089 (L.ECApp (
adamc@261 2090 (L.ECApp (
adamc@260 2091 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 2092 _), _),
adamc@260 2093 _), _),
adamc@260 2094 _), _),
adamc@260 2095 _) =>
adamc@260 2096 let
adamc@260 2097 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 2098 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 2099 in
adamc@260 2100 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 2101 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 2102 (L'.EAbs ("e2", s, s,
adamc@598 2103 strcat [sc "((",
adamc@598 2104 (L'.ERel 1, loc),
adamc@598 2105 sc ") ",
adamc@598 2106 (L'.ERel 2, loc),
adamc@598 2107 sc " (",
adamc@598 2108 (L'.ERel 0, loc),
adamc@598 2109 sc "))"]), loc)), loc)), loc),
adamc@260 2110 fm)
adamc@260 2111 end
adamc@260 2112
adamc@260 2113 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 2114 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 2115 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 2116
adamc@265 2117 | L.ECApp (
adamc@265 2118 (L.ECApp (
adamc@265 2119 (L.ECApp (
adamc@265 2120 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 2121 _), _),
adamc@265 2122 _), _),
adamc@544 2123 _) => ((L'.EPrim (Prim.String "COUNT(*)"), loc),
adamc@265 2124 fm)
adamc@266 2125
adamc@266 2126 | L.ECApp (
adamc@266 2127 (L.ECApp (
adamc@266 2128 (L.ECApp (
adamc@266 2129 (L.ECApp (
adamc@266 2130 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 2131 _), _),
adamc@266 2132 _), _),
adamc@266 2133 _), _),
adamc@266 2134 _) =>
adamc@266 2135 let
adamc@266 2136 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 2137 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 2138 in
adamc@266 2139 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 2140 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@598 2141 strcat [(L'.ERel 1, loc),
adamc@598 2142 sc "(",
adamc@598 2143 (L'.ERel 0, loc),
adamc@598 2144 sc ")"]), loc)), loc),
adamc@266 2145 fm)
adamc@266 2146 end
adamc@266 2147
adamc@266 2148 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 2149 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 2150 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 2151 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2152 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 2153 fm)
adamc@266 2154 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 2155 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2156 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 2157 fm)
adamc@266 2158
adamc@559 2159 | L.EFfi ("Basis", "sql_arith_int") => ((L'.ERecord [], loc), fm)
adamc@559 2160 | L.EFfi ("Basis", "sql_arith_float") => ((L'.ERecord [], loc), fm)
adamc@559 2161
adamc@266 2162 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 2163 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 2164 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 2165 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 2166 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2167 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 2168 fm)
adamc@266 2169 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 2170 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 2171 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 2172 fm)
adamc@266 2173
adamc@268 2174 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 2175 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 2176
adamc@441 2177 | L.ECApp (
adamc@441 2178 (L.ECApp (
adamc@441 2179 (L.ECApp (
adamc@441 2180 (L.ECApp (
adamc@441 2181 (L.EFfi ("Basis", "sql_nfunc"), _),
adamc@441 2182 _), _),
adamc@441 2183 _), _),
adamc@441 2184 _), _),
adamc@441 2185 _) =>
adamc@441 2186 let
adamc@441 2187 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@441 2188 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@441 2189 in
adamc@441 2190 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc),
adamc@441 2191 fm)
adamc@441 2192 end
adamc@441 2193 | L.EFfi ("Basis", "sql_current_timestamp") => ((L'.EPrim (Prim.String "CURRENT_TIMESTAMP"), loc), fm)
adamc@441 2194
adamc@746 2195 | L.ECApp (
adamc@746 2196 (L.ECApp (
adamc@746 2197 (L.ECApp (
adamc@746 2198 (L.ECApp (
adamc@746 2199 (L.ECApp (
adamc@746 2200 (L.EFfi ("Basis", "sql_ufunc"), _),
adamc@746 2201 _), _),
adamc@746 2202 _), _),
adamc@746 2203 _), _),
adamc@746 2204 _), _),
adamc@746 2205 _) =>
adamc@746 2206 let
adamc@746 2207 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@746 2208 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@746 2209 in
adamc@746 2210 ((L'.EAbs ("f", s, (L'.TFun (s, s), loc),
adamc@746 2211 (L'.EAbs ("x", s, s,
adamc@746 2212 strcat [(L'.ERel 1, loc),
adamc@746 2213 sc "(",
adamc@746 2214 (L'.ERel 0, loc),
adamc@746 2215 sc ")"]), loc)), loc),
adamc@746 2216 fm)
adamc@746 2217 end
adamc@746 2218 | L.EFfi ("Basis", "sql_octet_length") => ((L'.EPrim (Prim.String "octet_length"), loc), fm)
adamc@746 2219
adamc@470 2220 | (L.ECApp (
adamc@470 2221 (L.ECApp (
adamc@470 2222 (L.ECApp (
adamc@470 2223 (L.ECApp (
adamc@470 2224 (L.EFfi ("Basis", "sql_is_null"), _), _),
adamc@470 2225 _), _),
adamc@470 2226 _), _),
adamc@470 2227 _), _)) =>
adamc@470 2228 let
adamc@470 2229 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@470 2230 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@470 2231 in
adamc@470 2232 ((L'.EAbs ("s", s, s,
adamc@598 2233 strcat [sc "(",
adamc@598 2234 (L'.ERel 0, loc),
adamc@598 2235 sc " IS NULL)"]), loc),
adamc@470 2236 fm)
adamc@470 2237 end
adamc@470 2238
adamc@338 2239 | L.EFfiApp ("Basis", "nextval", [e]) =>
adamc@338 2240 let
adamc@338 2241 val (e, fm) = monoExp (env, st, fm) e
adamc@338 2242 in
adamc@465 2243 ((L'.ENextval e, loc), fm)
adamc@338 2244 end
adamc@338 2245
adamc@139 2246 | L.EApp (
adamc@139 2247 (L.ECApp (
adamc@720 2248 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 2249 _), _),
adamc@179 2250 se) =>
adamc@179 2251 let
adamc@179 2252 val (se, fm) = monoExp (env, st, fm) se
adamc@179 2253 in
adamc@179 2254 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 2255 end
adamc@179 2256
adamc@95 2257 | L.EApp (
adamc@95 2258 (L.EApp (
adamc@720 2259 (L.ECApp (
adamc@720 2260 (L.ECApp (
adamc@95 2261 (L.ECApp (
adamc@139 2262 (L.ECApp (
adamc@720 2263 (L.EFfi ("Basis", "join"),
adamc@720 2264 _), _), _),
adamc@139 2265 _), _),
adamc@720 2266 _), _),
adamc@720 2267 _), _),
adamc@720 2268 xml1), _),
adamc@720 2269 xml2) =>
adamc@179 2270 let
adamc@179 2271 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 2272 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 2273 in
adamc@179 2274 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 2275 end
adamc@95 2276
adamc@95 2277 | L.EApp (
adamc@95 2278 (L.EApp (
adamc@104 2279 (L.EApp (
adamc@721 2280 (L.EApp (
adamc@720 2281 (L.ECApp (
adamc@104 2282 (L.ECApp (
adamc@104 2283 (L.ECApp (
adamc@139 2284 (L.ECApp (
adamc@139 2285 (L.ECApp (
adamc@139 2286 (L.ECApp (
adamc@139 2287 (L.ECApp (
adamc@721 2288 (L.ECApp (
adamc@721 2289 (L.EFfi ("Basis", "tag"),
adamc@721 2290 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@721 2291 class), _),
adamc@720 2292 attrs), _),
adamc@720 2293 tag), _),
adamc@95 2294 xml) =>
adamc@95 2295 let
adamc@140 2296 fun getTag' (e, _) =
adamc@140 2297 case e of
adamc@143 2298 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 2299 | L.ECApp (e, t) => let
adamc@143 2300 val (tag, ts) = getTag' e
adamc@143 2301 in
adamc@143 2302 (tag, ts @ [t])
adamc@143 2303 end
adamc@140 2304 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 2305 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 2306 ("", []))
adamc@140 2307
adamc@95 2308 fun getTag (e, _) =
adamc@95 2309 case e of
adamc@143 2310 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 2311 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 2312 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 2313 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 2314 ("", []))
adamc@95 2315
adamc@143 2316 val (tag, targs) = getTag tag
adamc@95 2317
adamc@179 2318 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@598 2319 val attrs = case #1 attrs of
adamc@598 2320 L'.ERecord xes => xes
adamc@598 2321 | _ => raise Fail "Non-record attributes!"
adamc@104 2322
adamc@717 2323 val attrs =
adamc@717 2324 if List.exists (fn ("Link", _, _) => true
adamc@717 2325 | _ => false) attrs then
adamc@717 2326 List.filter (fn ("Href", _, _) => false
adamc@717 2327 | _ => true) attrs
adamc@717 2328 else
adamc@717 2329 attrs
adamc@717 2330
adamc@668 2331 fun findOnload (attrs, acc) =
adamc@668 2332 case attrs of
adamc@668 2333 [] => (NONE, acc)
adamc@668 2334 | ("Onload", e, _) :: rest => (SOME e, List.revAppend (acc, rest))
adamc@668 2335 | x :: rest => findOnload (rest, x :: acc)
adamc@679 2336
adamc@668 2337 val (onload, attrs) = findOnload (attrs, [])
adamc@668 2338
adamc@606 2339 fun lowercaseFirst "" = ""
adamc@606 2340 | lowercaseFirst s = String.str (Char.toLower (String.sub (s, 0)))
adamc@606 2341 ^ String.extract (s, 1, NONE)
adamc@606 2342
adamc@721 2343 val (class, fm) = monoExp (env, st, fm) class
adamc@721 2344
adamc@143 2345 fun tagStart tag =
adamc@598 2346 let
adamc@721 2347 val t = (L'.TFfi ("Basis", "string"), loc)
adamc@598 2348 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@721 2349
adamc@721 2350 val s = (L'.ECase (class,
adamc@721 2351 [((L'.PNone t, loc),
adamc@721 2352 s),
adamc@721 2353 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc),
adamc@721 2354 (L'.EStrcat (s,
adamc@721 2355 (L'.EStrcat ((L'.EPrim (Prim.String " class=\""), loc),
adamc@721 2356 (L'.EStrcat ((L'.ERel 0, loc),
adamc@721 2357 (L'.EPrim (Prim.String "\""), loc)),
adamc@721 2358 loc)), loc)), loc))],
adamc@721 2359 {disc = (L'.TOption t, loc),
adamc@721 2360 result = t}), loc)
adamc@598 2361 in
adamc@598 2362 foldl (fn (("Action", _, _), acc) => acc
adamc@598 2363 | (("Source", _, _), acc) => acc
adamc@598 2364 | ((x, e, t), (s, fm)) =>
adamc@598 2365 case t of
adamc@598 2366 (L'.TFfi ("Basis", "bool"), _) =>
adamc@598 2367 let
adamc@598 2368 val s' = " " ^ lowercaseFirst x
adamc@598 2369 in
adamc@598 2370 ((L'.ECase (e,
adamc@598 2371 [((L'.PCon (L'.Enum,
adamc@598 2372 L'.PConFfi {mod = "Basis",
adamc@598 2373 datatyp = "bool",
adamc@598 2374 con = "True",
adamc@598 2375 arg = NONE},
adamc@598 2376 NONE), loc),
adamc@598 2377 (L'.EStrcat (s,
adamc@598 2378 (L'.EPrim (Prim.String s'), loc)), loc)),
adamc@598 2379 ((L'.PCon (L'.Enum,
adamc@598 2380 L'.PConFfi {mod = "Basis",
adamc@598 2381 datatyp = "bool",
adamc@598 2382 con = "False",
adamc@598 2383 arg = NONE},
adamc@598 2384 NONE), loc),
adamc@598 2385 s)],
adamc@598 2386 {disc = (L'.TFfi ("Basis", "bool"), loc),
adamc@598 2387 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@598 2388 fm)
adamc@598 2389 end
adamc@598 2390 | (L'.TFun _, _) =>
adamc@598 2391 let
adamc@598 2392 val s' = " " ^ lowercaseFirst x ^ "='"
adamc@651 2393 val e = (L'.EApp (e, (L'.ERecord [], loc)), loc)
adamc@598 2394 in
adamc@598 2395 ((L'.EStrcat (s,
adamc@598 2396 (L'.EStrcat (
adamc@598 2397 (L'.EPrim (Prim.String s'), loc),
adamc@598 2398 (L'.EStrcat (
adamc@598 2399 (L'.EJavaScript (L'.Attribute, e, NONE), loc),
adamc@598 2400 (L'.EPrim (Prim.String "'"), loc)), loc)),
adamc@598 2401 loc)), loc),
adamc@598 2402 fm)
adamc@598 2403 end
adamc@598 2404 | _ =>
adamc@598 2405 let
adamc@598 2406 val fooify =
adamc@598 2407 case x of
adamc@717 2408 "Link" => urlifyExp
adamc@717 2409 | "Action" => urlifyExp
adamc@598 2410 | _ => attrifyExp
adamc@120 2411
adamc@724 2412 val x =
adamc@724 2413 case x of
adamc@724 2414 "Typ" => "Type"
adamc@724 2415 | _ => x
adamc@598 2416 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@179 2417
adamc@598 2418 val (e, fm) = fooify env fm (e, t)
adamc@598 2419 in
adamc@598 2420 ((L'.EStrcat (s,
adamc@598 2421 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@598 2422 (L'.EStrcat (e,
adamc@598 2423 (L'.EPrim (Prim.String "\""),
adamc@598 2424 loc)),
adamc@598 2425 loc)),
adamc@598 2426 loc)), loc),
adamc@598 2427 fm)
adamc@598 2428 end)
adamc@598 2429 (s, fm) attrs
adamc@598 2430 end
adamc@104 2431
adamc@143 2432 fun input typ =
adamc@143 2433 case targs of
adamc@155 2434 [_, (L.CName name, _)] =>
adamc@179 2435 let
adamc@179 2436 val (ts, fm) = tagStart "input"
adamc@179 2437 in
adamc@179 2438 ((L'.EStrcat (ts,
adamc@179 2439 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 2440 loc)), loc), fm)
adamc@179 2441 end
adamc@143 2442 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2443 raise Fail "No name passed to input tag")
adamc@104 2444
adamc@565 2445 fun normal (tag, extra, extraInner) =
adamc@143 2446 let
adamc@179 2447 val (tagStart, fm) = tagStart tag
adamc@152 2448 val tagStart = case extra of
adamc@152 2449 NONE => tagStart
adamc@152 2450 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 2451
adamc@695 2452 val xml = case extraInner of
adamc@695 2453 NONE => xml
adamc@695 2454 | SOME ei => (L.EFfiApp ("Basis", "strcat", [ei, xml]), loc)
adamc@695 2455
adamc@143 2456 fun normal () =
adamc@179 2457 let
adamc@179 2458 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2459 in
adamc@179 2460 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 2461 (L'.EStrcat (xml,
adamc@179 2462 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 2463 loc)), loc)),
adamc@179 2464 loc),
adamc@179 2465 fm)
adamc@179 2466 end
adamc@143 2467 in
adamc@143 2468 case xml of
adamc@143 2469 (L.EApp ((L.ECApp (
adamc@143 2470 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 2471 _), _),
adamc@143 2472 _), _),
adamc@143 2473 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 2474 if CharVector.all Char.isSpace s then
adamc@179 2475 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 2476 else
adamc@143 2477 normal ()
adamc@143 2478 | _ => normal ()
adamc@143 2479 end
adamc@606 2480
adamc@606 2481 fun setAttrs jexp =
adamc@606 2482 let
adamc@606 2483 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@606 2484
adamc@606 2485 val assgns = List.mapPartial
adamc@606 2486 (fn ("Source", _, _) => NONE
adamc@606 2487 | (x, e, _) =>
adamc@606 2488 SOME (strcat [str ("d." ^ lowercaseFirst x ^ "="),
adamc@606 2489 (L'.EJavaScript (L'.Script, e, NONE), loc),
adamc@606 2490 str ";"]))
adamc@606 2491 attrs
adamc@606 2492 in
adamc@606 2493 case assgns of
adamc@606 2494 [] => jexp
adamc@606 2495 | _ => strcat (str "var d="
adamc@606 2496 :: jexp
adamc@606 2497 :: str ";"
adamc@606 2498 :: assgns)
adamc@606 2499 end
adamc@152 2500 in
adamc@152 2501 case tag of
adamc@679 2502 "body" => let
adamc@668 2503 val onload = case onload of
adamc@668 2504 NONE => (L'.EPrim (Prim.String ""), loc)
adamc@668 2505 | SOME e =>
adamc@668 2506 let
adamc@668 2507 val e = (L'.EApp (e, (L'.ERecord [], loc)), loc)
adamc@668 2508 in
adamc@668 2509 (L'.EJavaScript (L'.Attribute, e, NONE), loc)
adamc@679 2510 end
adamc@668 2511 in
adamc@668 2512 normal ("body",
adamc@695 2513 SOME (L'.EFfiApp ("Basis", "maybe_onload",
adamc@695 2514 [(L'.EStrcat ((L'.EFfiApp ("Basis", "get_settings",
adamc@695 2515 [(L'.ERecord [], loc)]), loc),
adamc@695 2516 onload), loc)]),
adamc@695 2517 loc),
adamc@695 2518 SOME (L.EFfiApp ("Basis", "get_script", [(L.ERecord [], loc)]), loc))
adamc@668 2519 end
adamc@568 2520
adamc@568 2521 | "dyn" =>
adamc@598 2522 (case attrs of
adamc@598 2523 [("Signal", (L'.EApp ((L'.EAbs (_, _, _, (L'.ESignalReturn (L'.ERel 0, _), _)), _),
adamc@598 2524 e), _), _)] => (e, fm)
adamc@598 2525 | [("Signal", e, _)] =>
adamc@568 2526 ((L'.EStrcat
adamc@603 2527 ((L'.EPrim (Prim.String "<span><script type=\"text/javascript\">dyn("), loc),
adamc@578 2528 (L'.EStrcat ((L'.EJavaScript (L'.Script, e, NONE), loc),
adamc@603 2529 (L'.EPrim (Prim.String ")</script></span>"), loc)), loc)), loc),
adamc@568 2530 fm)
adamc@568 2531 | _ => raise Fail "Monoize: Bad dyn attributes")
adamc@565 2532
adamc@565 2533 | "submit" => normal ("input type=\"submit\"", NONE, NONE)
adamc@601 2534 | "button" => normal ("input type=\"submit\"", NONE, NONE)
adamc@761 2535 | "hidden" => input "hidden"
adamc@152 2536
adamc@152 2537 | "textbox" =>
adamc@152 2538 (case targs of
adamc@152 2539 [_, (L.CName name, _)] =>
adamc@598 2540 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@598 2541 NONE =>
adamc@598 2542 let
adamc@598 2543 val (ts, fm) = tagStart "input"
adamc@598 2544 in
adamc@598 2545 ((L'.EStrcat (ts,
adamc@598 2546 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@598 2547 loc)), loc), fm)
adamc@598 2548 end
adamc@598 2549 | SOME (_, src, _) =>
adamc@646 2550 (strcat [str "<span><script type=\"text/javascript\">inp(\"input\",",
adamc@598 2551 (L'.EJavaScript (L'.Script, src, NONE), loc),
adamc@646 2552 str ")</script></span>"],
adamc@598 2553 fm))
adamc@152 2554 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@598 2555 raise Fail "No name passed to textbox tag"))
adamc@155 2556 | "password" => input "password"
adamc@361 2557 | "textarea" =>
adamc@152 2558 (case targs of
adamc@152 2559 [_, (L.CName name, _)] =>
adamc@179 2560 let
adamc@179 2561 val (ts, fm) = tagStart "textarea"
adamc@179 2562 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2563 in
adamc@179 2564 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 2565 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 2566 (L'.EStrcat (xml,
adamc@179 2567 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 2568 loc)), loc)),
adamc@179 2569 loc), fm)
adamc@179 2570 end
adamc@152 2571 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2572 raise Fail "No name passed to ltextarea tag"))
adamc@153 2573
adamc@190 2574 | "checkbox" => input "checkbox"
adamc@737 2575 | "upload" => input "file"
adamc@190 2576
adamc@153 2577 | "radio" =>
adamc@153 2578 (case targs of
adamc@153 2579 [_, (L.CName name, _)] =>
adamc@179 2580 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 2581 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 2582 raise Fail "No name passed to radio tag"))
adamc@153 2583 | "radioOption" =>
adamc@153 2584 (case St.radioGroup st of
adamc@153 2585 NONE => raise Fail "No name for radioGroup"
adamc@153 2586 | SOME name =>
adamc@153 2587 normal ("input",
adamc@565 2588 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc),
adamc@565 2589 NONE))
adamc@152 2590
adamc@361 2591 | "select" =>
adamc@154 2592 (case targs of
adamc@154 2593 [_, (L.CName name, _)] =>
adamc@179 2594 let
adamc@179 2595 val (ts, fm) = tagStart "select"
adamc@179 2596 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 2597 in
adamc@179 2598 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@598 2599 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")),
adamc@598 2600 loc)), loc),
adamc@179 2601 (L'.EStrcat (xml,
adamc@179 2602 (L'.EPrim (Prim.String "</select>"),
adamc@179 2603 loc)), loc)),
adamc@179 2604 loc),
adamc@179 2605 fm)
adamc@179 2606 end
adamc@154 2607 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 2608 raise Fail "No name passed to lselect tag"))
adamc@154 2609
adamc@601 2610 | "ctextbox" =>
adamc@601 2611 (case List.find (fn ("Source", _, _) => true | _ => false) attrs of
adamc@601 2612 NONE =>
adamc@601 2613 let
adamc@601 2614 val (ts, fm) = tagStart "input"
adamc@601 2615 in
adamc@601 2616 ((L'.EStrcat (ts,
adamc@601 2617 (L'.EPrim (Prim.String "/>"), loc)),
adamc@601 2618 loc), fm)
adamc@601 2619 end
adamc@601 2620 | SOME (_, src, _) =>
adamc@606 2621 let
adamc@606 2622 val sc = strcat [str "inp(\"input\",",
adamc@606 2623 (L'.EJavaScript (L'.Script, src, NONE), loc),
adamc@606 2624 str ")"]
adamc@606 2625 val sc = setAttrs sc
adamc@606 2626 in
adamc@646 2627 (strcat [str "<span><script type=\"text/javascript\">",
adamc@606 2628 sc,
adamc@646 2629 str "</script></span>"],
adamc@606 2630 fm)
adamc@606 2631 end)
adamc@154 2632
adamc@565 2633 | "tabl" => normal ("table", NONE, NONE)
adamc@565 2634 | _ => normal (tag, NONE, NONE)
adamc@95 2635 end
adamc@94 2636
adamc@141 2637 | L.EApp ((L.ECApp (
adamc@361 2638 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@734 2639 (L.CRecord (_, fields), _)), _),
adamc@141 2640 xml) =>
adamc@143 2641 let
adamc@143 2642 fun findSubmit (e, _) =
adamc@143 2643 case e of
adamc@143 2644 L.EApp (
adamc@143 2645 (L.EApp (
adamc@143 2646 (L.ECApp (
adamc@143 2647 (L.ECApp (
adamc@143 2648 (L.ECApp (
adamc@143 2649 (L.ECApp (
adamc@143 2650 (L.EFfi ("Basis", "join"),
adamc@143 2651 _), _), _),
adamc@143 2652 _), _),
adamc@143 2653 _), _),
adamc@143 2654 _), _),
adamc@143 2655 xml1), _),
adamc@143 2656 xml2) => (case findSubmit xml1 of
adamc@143 2657 Error => Error
adamc@143 2658 | NotFound => findSubmit xml2
adamc@143 2659 | Found e =>
adamc@143 2660 case findSubmit xml2 of
adamc@143 2661 NotFound => Found e
adamc@143 2662 | _ => Error)
adamc@143 2663 | L.EApp (
adamc@143 2664 (L.EApp (
adamc@143 2665 (L.EApp (
adamc@730 2666 (L.EApp (
adamc@143 2667 (L.ECApp (
adamc@143 2668 (L.ECApp (
adamc@143 2669 (L.ECApp (
adamc@143 2670 (L.ECApp (
adamc@143 2671 (L.ECApp (
adamc@143 2672 (L.ECApp (
adamc@143 2673 (L.ECApp (
adamc@730 2674 (L.ECApp (
adamc@730 2675 (L.EFfi ("Basis", "tag"),
adamc@730 2676 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@730 2677 _), _),
adamc@143 2678 attrs), _),
adamc@143 2679 _), _),
adamc@143 2680 xml) =>
adamc@143 2681 (case #1 attrs of
adamc@143 2682 L.ERecord xes =>
adamc@143 2683 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 2684 | _ => NONE) xes of
adamc@143 2685 NONE => findSubmit xml
adamc@143 2686 | SOME et =>
adamc@143 2687 case findSubmit xml of
adamc@143 2688 NotFound => Found et
adamc@143 2689 | _ => Error)
adamc@143 2690 | _ => findSubmit xml)
adamc@143 2691 | _ => NotFound
adamc@143 2692
adamc@735 2693 val (func, action, fm) = case findSubmit xml of
adamc@735 2694 NotFound => (0, (L'.EPrim (Prim.String ""), loc), fm)
adamc@143 2695 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@598 2696 | Found (action, actionT) =>
adamc@598 2697 let
adamc@735 2698 val func = case #1 action of
adamc@735 2699 L.EClosure (n, _) => n
adamc@735 2700 | _ => raise Fail "Monoize: Action is not a closure"
adamc@598 2701 val actionT = monoType env actionT
adamc@598 2702 val (action, fm) = monoExp (env, st, fm) action
adamc@598 2703 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@598 2704 in
adamc@735 2705 (func,
adamc@735 2706 (L'.EStrcat ((L'.EPrim (Prim.String " action=\""), loc),
adamc@598 2707 (L'.EStrcat (action,
adamc@598 2708 (L'.EPrim (Prim.String "\""), loc)), loc)), loc),
adamc@598 2709 fm)
adamc@598 2710 end
adamc@734 2711
adamc@737 2712 val hasUpload = CoreUtil.Exp.exists {kind = fn _ => false,
adamc@737 2713 con = fn _ => false,
adamc@737 2714 exp = fn e =>
adamc@737 2715 case e of
adamc@737 2716 L.EFfi ("Basis", "upload") => true
adamc@737 2717 | _ => false} xml
adamc@737 2718
adamc@179 2719 val (xml, fm) = monoExp (env, st, fm) xml
adamc@735 2720
adamc@735 2721 val xml =
adamc@735 2722 if IS.member (!readCookie, func) then
adamc@735 2723 let
adamc@735 2724 fun inFields s = List.exists (fn ((L.CName s', _), _) => s' = s
adamc@735 2725 | _ => true) fields
adamc@735 2726
adamc@735 2727 fun getSigName () =
adamc@735 2728 let
adamc@735 2729 fun getSigName' n =
adamc@735 2730 let
adamc@735 2731 val s = "Sig" ^ Int.toString n
adamc@735 2732 in
adamc@735 2733 if inFields s then
adamc@735 2734 getSigName' (n + 1)
adamc@735 2735 else
adamc@735 2736 s
adamc@735 2737 end
adamc@735 2738 in
adamc@735 2739 if inFields "Sig" then
adamc@735 2740 getSigName' 0
adamc@735 2741 else
adamc@735 2742 "Sig"
adamc@735 2743 end
adamc@735 2744
adamc@735 2745 val sigName = getSigName ()
adamc@735 2746 val sigSet = (L'.EFfiApp ("Basis", "sigString", [(L'.ERecord [], loc)]), loc)
adamc@735 2747 val sigSet = (L'.EStrcat ((L'.EPrim (Prim.String ("<input type=\"hidden\" name=\""
adamc@735 2748 ^ sigName
adamc@735 2749 ^ "\" value=\"")), loc),
adamc@735 2750 sigSet), loc)
adamc@735 2751 val sigSet = (L'.EStrcat (sigSet,
adamc@735 2752 (L'.EPrim (Prim.String "\">"), loc)), loc)
adamc@735 2753 in
adamc@735 2754 (L'.EStrcat (sigSet, xml), loc)
adamc@735 2755 end
adamc@735 2756 else
adamc@735 2757 xml
adamc@737 2758
adamc@737 2759 val action = if hasUpload then
adamc@737 2760 (L'.EStrcat (action,
adamc@737 2761 (L'.EPrim (Prim.String " enctype=\"multipart/form-data\""), loc)), loc)
adamc@737 2762 else
adamc@737 2763 action
adamc@737 2764
adamc@143 2765 in
adamc@730 2766 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form method=\"post\""), loc),
adamc@179 2767 (L'.EStrcat (action,
adamc@598 2768 (L'.EPrim (Prim.String ">"), loc)), loc)), loc),
adamc@179 2769 (L'.EStrcat (xml,
adamc@179 2770 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 2771 fm)
adamc@143 2772 end
adamc@141 2773
adamc@756 2774 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp (
adamc@756 2775 (L.EFfi ("Basis", "subform"), _), _), _), _),
adamc@756 2776 _), _), _), (L.CName nm, loc)) =>
adamc@756 2777 let
adamc@756 2778 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@756 2779 in
adamc@756 2780 ((L'.EAbs ("xml", s, s,
adamc@756 2781 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".b\" value=\""
adamc@756 2782 ^ nm ^ "\">")), loc),
adamc@756 2783 (L'.ERel 0, loc),
adamc@756 2784 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\">")), loc)]),
adamc@756 2785 loc),
adamc@756 2786 fm)
adamc@756 2787 end
adamc@756 2788
adamc@758 2789 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp (
adamc@758 2790 (L.EFfi ("Basis", "subforms"), _), _), _), _),
adamc@758 2791 _), _), _), (L.CName nm, loc)) =>
adamc@758 2792 let
adamc@758 2793 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@758 2794 in
adamc@758 2795 ((L'.EAbs ("xml", s, s,
adamc@758 2796 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".s\" value=\""
adamc@758 2797 ^ nm ^ "\">")), loc),
adamc@758 2798 (L'.ERel 0, loc),
adamc@758 2799 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\">")), loc)]),
adamc@758 2800 loc),
adamc@758 2801 fm)
adamc@758 2802 end
adamc@758 2803
adamc@758 2804 | L.ECApp ((L.ECApp (
adamc@758 2805 (L.EFfi ("Basis", "entry"), _), _), _), _) =>
adamc@758 2806 let
adamc@758 2807 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@758 2808 in
adamc@758 2809 ((L'.EAbs ("xml", s, s,
adamc@758 2810 strcat [(L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".i\" value=\"1\">")), loc),
adamc@758 2811 (L'.ERel 0, loc),
adamc@758 2812 (L'.EPrim (Prim.String ("<input type=\"hidden\" name=\".e\" value=\"1\">")), loc)]),
adamc@758 2813 loc),
adamc@758 2814 fm)
adamc@758 2815 end
adamc@758 2816
adamc@148 2817 | L.EApp ((L.ECApp (
adamc@148 2818 (L.ECApp (
adamc@148 2819 (L.ECApp (
adamc@148 2820 (L.ECApp (
adamc@148 2821 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 2822 _), _),
adamc@148 2823 _), _),
adamc@148 2824 _), _),
adamc@179 2825 xml) => monoExp (env, st, fm) xml
adamc@148 2826
adamc@283 2827 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 2828 let
adamc@283 2829 val t = monoType env t
adamc@283 2830 in
adamc@283 2831 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 2832 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 2833 fm)
adamc@283 2834 end
adamc@741 2835 | L.ECApp ((L.EFfi ("Basis", "returnBlob"), _), t) =>
adamc@741 2836 let
adamc@741 2837 val t = monoType env t
adamc@741 2838 val un = (L'.TRecord [], loc)
adamc@741 2839 in
adamc@741 2840 ((L'.EAbs ("b", (L'.TFfi ("Basis", "blob"), loc),
adamc@741 2841 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFun (un, t), loc)), loc),
adamc@741 2842 (L'.EAbs ("mt", (L'.TFfi ("Basis", "string"), loc), (L'.TFun (un, t), loc),
adamc@741 2843 (L'.EAbs ("_", un, t,
adamc@741 2844 (L'.EReturnBlob {blob = (L'.ERel 2, loc),
adamc@741 2845 mimeType = (L'.ERel 1, loc),
adamc@741 2846 t = t}, loc)), loc)), loc)), loc),
adamc@741 2847 fm)
adamc@741 2848 end
adamc@283 2849
adamc@179 2850 | L.EApp (e1, e2) =>
adamc@179 2851 let
adamc@179 2852 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 2853 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 2854 in
adamc@179 2855 ((L'.EApp (e1, e2), loc), fm)
adamc@179 2856 end
adamc@26 2857 | L.EAbs (x, dom, ran, e) =>
adamc@179 2858 let
adamc@179 2859 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 2860 in
adamc@179 2861 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 2862 end
adamc@25 2863 | L.ECApp _ => poly ()
adamc@25 2864 | L.ECAbs _ => poly ()
adamc@25 2865
adamc@252 2866 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 2867 | L.EFfiApp (m, x, es) =>
adamc@252 2868 let
adamc@252 2869 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 2870 in
adamc@252 2871 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 2872 end
adamc@252 2873
adamc@179 2874 | L.ERecord xes =>
adamc@179 2875 let
adamc@179 2876 val (xes, fm) = ListUtil.foldlMap
adamc@179 2877 (fn ((x, e, t), fm) =>
adamc@179 2878 let
adamc@179 2879 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2880 in
adamc@179 2881 ((monoName env x,
adamc@179 2882 e,
adamc@179 2883 monoType env t), fm)
adamc@179 2884 end) fm xes
adamc@179 2885 in
adamc@179 2886 ((L'.ERecord xes, loc), fm)
adamc@179 2887 end
adamc@179 2888 | L.EField (e, x, _) =>
adamc@179 2889 let
adamc@179 2890 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2891 in
adamc@179 2892 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 2893 end
adamc@445 2894 | L.EConcat _ => poly ()
adamc@149 2895 | L.ECut _ => poly ()
adamc@493 2896 | L.ECutMulti _ => poly ()
adamc@177 2897
adamc@182 2898 | L.ECase (e, pes, {disc, result}) =>
adamc@179 2899 let
adamc@179 2900 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2901 val (pes, fm) = ListUtil.foldlMap
adamc@179 2902 (fn ((p, e), fm) =>
adamc@179 2903 let
adamc@179 2904 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2905 in
adamc@182 2906 ((monoPat env p, e), fm)
adamc@179 2907 end) fm pes
adamc@179 2908 in
adamc@182 2909 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 2910 end
adamc@177 2911
adamc@179 2912 | L.EWrite e =>
adamc@179 2913 let
adamc@179 2914 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2915 in
adamc@252 2916 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 2917 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 2918 end
adamc@110 2919
adamc@179 2920 | L.EClosure (n, es) =>
adamc@179 2921 let
adamc@179 2922 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 2923 monoExp (env, st, fm) e)
adamc@179 2924 fm es
adamc@179 2925 in
adamc@179 2926 ((L'.EClosure (n, es), loc), fm)
adamc@179 2927 end
adamc@450 2928
adamc@450 2929 | L.ELet (x, t, e1, e2) =>
adamc@450 2930 let
adamc@450 2931 val t' = monoType env t
adamc@450 2932 val (e1, fm) = monoExp (env, st, fm) e1
adamc@450 2933 val (e2, fm) = monoExp (Env.pushERel env x t, st, fm) e2
adamc@450 2934 in
adamc@450 2935 ((L'.ELet (x, t', e1, e2), loc), fm)
adamc@450 2936 end
adamc@607 2937
adamc@609 2938 | L.EServerCall (n, es, ek, t) =>
adamc@608 2939 let
adamc@609 2940 val t = monoType env t
adamc@614 2941 val (_, ft, _, name) = Env.lookupENamed env n
adamc@608 2942 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@614 2943
adamc@614 2944 fun encodeArgs (es, ft, acc, fm) =
adamc@614 2945 case (es, ft) of
adamc@614 2946 ([], _) => (rev acc, fm)
adamc@614 2947 | (e :: es, (L.TFun (dom, ran), _)) =>
adamc@614 2948 let
adamc@614 2949 val (e, fm) = urlifyExp env fm (e, monoType env dom)
adamc@614 2950 in
adamc@614 2951 encodeArgs (es, ran, e
adamc@614 2952 :: (L'.EPrim (Prim.String "/"), loc)
adamc@614 2953 :: acc, fm)
adamc@614 2954 end
adamc@614 2955 | _ => raise Fail "Monoize: Not enough arguments visible in RPC function type"
adamc@614 2956
adamc@614 2957 val (call, fm) = encodeArgs (es, ft, [], fm)
adamc@614 2958 val call = foldl (fn (e, call) => (L'.EStrcat (call, e), loc))
adamc@614 2959 (L'.EPrim (Prim.String name), loc) call
adamc@614 2960
adamc@608 2961 val (ek, fm) = monoExp (env, st, fm) ek
adamc@609 2962
adamc@609 2963 val ekf = (L'.EAbs ("f",
adamc@609 2964 (L'.TFun (t,
adamc@609 2965 (L'.TFun ((L'.TRecord [], loc),
adamc@609 2966 (L'.TRecord [], loc)), loc)), loc),
adamc@609 2967 (L'.TFun (t,
adamc@609 2968 (L'.TRecord [], loc)), loc),
adamc@609 2969 (L'.EAbs ("x",
adamc@609 2970 t,
adamc@609 2971 (L'.TRecord [], loc),
adamc@609 2972 (L'.EApp ((L'.EApp ((L'.ERel 1, loc),
adamc@609 2973 (L'.ERel 0, loc)), loc),
adamc@609 2974 (L'.ERecord [], loc)), loc)), loc)), loc)
adamc@609 2975 val ek = (L'.EApp (ekf, ek), loc)
adamc@736 2976 val eff = if IS.member (!readCookie, n) then
adamc@736 2977 L'.ReadCookieWrite
adamc@736 2978 else
adamc@736 2979 L'.ReadOnly
adamc@736 2980 val e = (L'.EServerCall (call, ek, t, eff), loc)
adamc@651 2981 val e = liftExpInExp 0 e
adamc@651 2982 val unit = (L'.TRecord [], loc)
adamc@651 2983 val e = (L'.EAbs ("_", unit, unit, e), loc)
adamc@608 2984 in
adamc@651 2985 (e, fm)
adamc@608 2986 end
adamc@626 2987
adamc@626 2988 | L.EKAbs _ => poly ()
adamc@626 2989 | L.EKApp _ => poly ()
adamc@25 2990 end
adamc@25 2991
adamc@179 2992 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 2993 let
adamc@25 2994 fun poly () =
adamc@25 2995 (E.errorAt loc "Unsupported declaration";
adamc@25 2996 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 2997 NONE)
adamc@25 2998 in
adamc@25 2999 case d of
adamc@25 3000 L.DCon _ => NONE
adamc@193 3001 | L.DDatatype (x, n, [], xncs) =>
adamc@193 3002 let
adamc@196 3003 val env' = Env.declBinds env all
adamc@196 3004 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 3005 in
adamc@273 3006 SOME (env', fm, [d])
adamc@193 3007 end
adamc@757 3008 | L.DDatatype ("list", n, [_], [("Nil", _, NONE),
adamc@757 3009 ("Cons", _, SOME (L.TRecord (L.CRecord (_,
adamc@757 3010 [((L.CName "1", _),
adamc@757 3011 (L.CRel 0, _)),
adamc@757 3012 ((L.CName "2", _),
adamc@757 3013 (L.CApp ((L.CNamed n', _),
adamc@757 3014 (L.CRel 0, _)),
adamc@757 3015 _))]), _), _))]) =>
adamc@757 3016 if n = n' then
adamc@757 3017 NONE
adamc@757 3018 else
adamc@757 3019 poly ()
adamc@193 3020 | L.DDatatype _ => poly ()
adamc@179 3021 | L.DVal (x, n, t, e, s) =>
adamc@179 3022 let
adamc@179 3023 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 3024 in
adamc@179 3025 SOME (Env.pushENamed env x n t NONE s,
adamc@179 3026 fm,
adamc@273 3027 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 3028 end
adamc@128 3029 | L.DValRec vis =>
adamc@128 3030 let
adamc@128 3031 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 3032
adamc@179 3033 val (vis, fm) = ListUtil.foldlMap
adamc@179 3034 (fn ((x, n, t, e, s), fm) =>
adamc@179 3035 let
adamc@179 3036 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 3037 in
adamc@179 3038 ((x, n, monoType env t, e, s), fm)
adamc@179 3039 end)
adamc@179 3040 fm vis
adamc@128 3041 in
adamc@128 3042 SOME (env,
adamc@179 3043 fm,
adamc@273 3044 [(L'.DValRec vis, loc)])
adamc@128 3045 end
adamc@144 3046 | L.DExport (ek, n) =>
adamc@115 3047 let
adamc@120 3048 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 3049
adamc@609 3050 fun unwind (t, args) =
adamc@609 3051 case #1 t of
adamc@609 3052 L.TFun (dom, ran) => unwind (ran, dom :: args)
adamc@314 3053 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@609 3054 unwind (t, (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: args)
adamc@609 3055 | _ => (rev args, t)
adamc@120 3056
adamc@609 3057 val (ts, ran) = unwind (t, [])
adamc@609 3058 val ts = map (monoType env) ts
adamc@609 3059 val ran = monoType env ran
adamc@115 3060 in
adamc@609 3061 SOME (env, fm, [(L'.DExport (ek, s, n, ts, ran), loc)])
adamc@115 3062 end
adamc@707 3063 | L.DTable (x, n, (L.CRecord (_, xts), _), s, pe, _, ce, _) =>
adamc@251 3064 let
adamc@251 3065 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 3066 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 3067 val s = "uw_" ^ s
adamc@704 3068 val e_name = (L'.EPrim (Prim.String s), loc)
adamc@273 3069
adamc@273 3070 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@704 3071
adamc@707 3072 val (pe, fm) = monoExp (env, St.empty, fm) pe
adamc@707 3073 val (ce, fm) = monoExp (env, St.empty, fm) ce
adamc@251 3074 in
adamc@251 3075 SOME (Env.pushENamed env x n t NONE s,
adamc@251 3076 fm,
adamc@707 3077 [(L'.DTable (s, xts, pe, ce), loc),
adamc@704 3078 (L'.DVal (x, n, t', e_name, s), loc)])
adamc@251 3079 end
adamc@273 3080 | L.DTable _ => poly ()
adamc@754 3081 | L.DView (x, n, s, e, (L.CRecord (_, xts), _)) =>
adamc@754 3082 let
adamc@754 3083 val t = (L.CFfi ("Basis", "string"), loc)
adamc@754 3084 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@754 3085 val s = "uw_" ^ s
adamc@754 3086 val e_name = (L'.EPrim (Prim.String s), loc)
adamc@754 3087
adamc@754 3088 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@754 3089
adamc@754 3090 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@754 3091 val e = (L'.EFfiApp ("Basis", "viewify", [e]), loc)
adamc@754 3092 in
adamc@754 3093 SOME (Env.pushENamed env x n t NONE s,
adamc@754 3094 fm,
adamc@754 3095 [(L'.DView (s, xts, e), loc),
adamc@754 3096 (L'.DVal (x, n, t', e_name, s), loc)])
adamc@754 3097 end
adamc@754 3098 | L.DView _ => poly ()
adamc@338 3099 | L.DSequence (x, n, s) =>
adamc@338 3100 let
adamc@338 3101 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 3102 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 3103 val s = "uw_" ^ s
adamc@338 3104 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 3105 in
adamc@338 3106 SOME (Env.pushENamed env x n t NONE s,
adamc@338 3107 fm,
adamc@338 3108 [(L'.DSequence s, loc),
adamc@338 3109 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 3110 end
adamc@683 3111 | L.DDatabase _ => NONE
adamc@462 3112 | L.DCookie (x, n, t, s) =>
adamc@462 3113 let
adamc@462 3114 val t = (L.CFfi ("Basis", "string"), loc)
adamc@462 3115 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@462 3116 val e = (L'.EPrim (Prim.String s), loc)
adamc@462 3117 in
adamc@462 3118 SOME (Env.pushENamed env x n t NONE s,
adamc@462 3119 fm,
adamc@725 3120 [(L'.DCookie s, loc),
adamc@725 3121 (L'.DVal (x, n, t', e, s), loc)])
adamc@462 3122 end
adamc@720 3123 | L.DStyle (x, n, s) =>
adamc@718 3124 let
adamc@718 3125 val t = (L.CFfi ("Basis", "string"), loc)
adamc@718 3126 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@718 3127 val e = (L'.EPrim (Prim.String s), loc)
adamc@718 3128 in
adamc@718 3129 SOME (Env.pushENamed env x n t NONE s,
adamc@718 3130 fm,
adamc@720 3131 [(L'.DStyle s, loc),
adamc@718 3132 (L'.DVal (x, n, t', e, s), loc)])
adamc@718 3133 end
adamc@25 3134 end
adamc@25 3135
adamc@683 3136 datatype expungable = Client | Channel
adamc@683 3137
adamc@683 3138 fun monoize env file =
adamc@25 3139 let
adamc@764 3140
adamc@385 3141
adamc@735 3142 (* Calculate which exported functions need cookie signature protection *)
adamc@735 3143 val rcook = foldl (fn ((d, _), rcook) =>
adamc@735 3144 case d of
adamc@735 3145 L.DExport (L.Action L.ReadCookieWrite, n) => IS.add (rcook, n)
adamc@735 3146 | L.DExport (L.Rpc L.ReadCookieWrite, n) => IS.add (rcook, n)
adamc@735 3147 | _ => rcook)
adamc@735 3148 IS.empty file
adamc@735 3149 val () = readCookie := rcook
adamc@735 3150
adamc@683 3151 val loc = E.dummySpan
adamc@683 3152 val client = (L'.TFfi ("Basis", "client"), loc)
adamc@683 3153 val unit = (L'.TRecord [], loc)
adamc@687 3154
adamc@687 3155 fun calcClientish xts =
adamc@687 3156 foldl (fn ((x : L.con, t : L.con), st as (nullable, notNullable)) =>
adamc@687 3157 case #1 x of
adamc@687 3158 L.CName x =>
adamc@687 3159 (case #1 t of
adamc@687 3160 L.CFfi ("Basis", "client") =>
adamc@687 3161 (nullable, (x, Client) :: notNullable)
adamc@687 3162 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@687 3163 (L.CFfi ("Basis", "client"), _)) =>
adamc@687 3164 ((x, Client) :: nullable, notNullable)
adamc@687 3165 | L.CApp ((L.CFfi ("Basis", "channel"), _), _) =>
adamc@687 3166 (nullable, (x, Channel) :: notNullable)
adamc@687 3167 | L.CApp ((L.CFfi ("Basis", "option"), _),
adamc@687 3168 (L.CApp ((L.CFfi ("Basis", "channel"), _), _), _)) =>
adamc@687 3169 ((x, Channel) :: nullable, notNullable)
adamc@687 3170 | _ => st)
adamc@687 3171 | _ => st) ([], []) xts
adamc@687 3172
adamc@683 3173 fun expunger () =
adamc@683 3174 let
adamc@683 3175 val target = (L'.EFfiApp ("Basis", "sqlifyClient", [(L'.ERel 0, loc)]), loc)
adamc@683 3176
adamc@683 3177 fun doTable (tab, xts, e) =
adamc@683 3178 case xts of
adamc@683 3179 L.CRecord (_, xts) =>
adamc@683 3180 let
adamc@687 3181 val (nullable, notNullable) = calcClientish xts
adamc@683 3182
adamc@684 3183 fun cond (x, v) =
adamc@684 3184 (L'.EStrcat ((L'.EPrim (Prim.String ("uw_" ^ x
adamc@684 3185 ^ (case v of
adamc@684 3186 Client => ""
adamc@684 3187 | Channel => " >> 32")
adamc@684 3188 ^ " = ")), loc),
adamc@684 3189 target), loc)
adamc@684 3190
adamc@684 3191 val e =
adamc@684 3192 foldl (fn ((x, v), e) =>
adamc@684 3193 (L'.ESeq (
adamc@684 3194 (L'.EDml (L'.EStrcat (
adamc@684 3195 (L'.EPrim (Prim.String ("UPDATE uw_"
adamc@684 3196 ^ tab
adamc@684 3197 ^ " SET uw_"
adamc@684 3198 ^ x
adamc@684 3199 ^ " = NULL WHERE ")), loc),
adamc@684 3200 cond (x, v)), loc), loc),
adamc@684 3201 e), loc))
adamc@684 3202 e nullable
adamc@684 3203
adamc@683 3204 val e =
adamc@683 3205 case notNullable of
adamc@683 3206 [] => e
adamc@683 3207 | eb :: ebs =>
adamc@684 3208 (L'.ESeq (
adamc@684 3209 (L'.EDml (foldl
adamc@684 3210 (fn (eb, s) =>
adamc@684 3211 (L'.EStrcat (s,
adamc@687 3212 (L'.EStrcat ((L'.EPrim (Prim.String " OR "),
adamc@684 3213 loc),
adamc@684 3214 cond eb), loc)), loc))
adamc@684 3215 (L'.EStrcat ((L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@684 3216 ^ tab
adamc@684 3217 ^ " WHERE ")), loc),
adamc@684 3218 cond eb), loc)
adamc@684 3219 ebs), loc),
adamc@684 3220 e), loc)
adamc@683 3221 in
adamc@683 3222 e
adamc@683 3223 end
adamc@683 3224 | _ => e
adamc@683 3225
adamc@683 3226 val e = (L'.ERecord [], loc)
adamc@683 3227 in
adamc@683 3228 foldl (fn ((d, _), e) =>
adamc@683 3229 case d of
adamc@707 3230 L.DTable (_, _, xts, tab, _, _, _, _) => doTable (tab, #1 xts, e)
adamc@683 3231 | _ => e) e file
adamc@683 3232 end
adamc@683 3233
adamc@687 3234 fun initializer () =
adamc@687 3235 let
adamc@687 3236 fun doTable (tab, xts, e) =
adamc@687 3237 case xts of
adamc@687 3238 L.CRecord (_, xts) =>
adamc@687 3239 let
adamc@687 3240 val (nullable, notNullable) = calcClientish xts
adamc@687 3241
adamc@687 3242 val e =
adamc@687 3243 case nullable of
adamc@687 3244 [] => e
adamc@687 3245 | (x, _) :: ebs =>
adamc@687 3246 (L'.ESeq (
adamc@687 3247 (L'.EDml (L'.EPrim (Prim.String
adamc@687 3248 (foldl (fn ((x, _), s) =>
adamc@687 3249 s ^ ", uw_" ^ x ^ " = NULL")
adamc@687 3250 ("UPDATE uw_"
adamc@687 3251 ^ tab
adamc@687 3252 ^ " SET uw_"
adamc@687 3253 ^ x
adamc@687 3254 ^ " = NULL")
adamc@687 3255 ebs)), loc), loc),
adamc@687 3256 e), loc)
adamc@687 3257
adamc@687 3258 val e =
adamc@687 3259 case notNullable of
adamc@687 3260 [] => e
adamc@687 3261 | eb :: ebs =>
adamc@687 3262 (L'.ESeq (
adamc@687 3263 (L'.EDml (L'.EPrim (Prim.String ("DELETE FROM uw_"
adamc@687 3264 ^ tab)), loc), loc),
adamc@687 3265 e), loc)
adamc@687 3266 in
adamc@687 3267 e
adamc@687 3268 end
adamc@687 3269 | _ => e
adamc@687 3270
adamc@687 3271 val e = (L'.ERecord [], loc)
adamc@687 3272 in
adamc@687 3273 foldl (fn ((d, _), e) =>
adamc@687 3274 case d of
adamc@707 3275 L.DTable (_, _, xts, tab, _, _, _, _) => doTable (tab, #1 xts, e)
adamc@687 3276 | _ => e) e file
adamc@687 3277 end
adamc@687 3278
adamc@179 3279 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@683 3280 case #1 d of
adamc@683 3281 L.DDatabase s =>
adamc@683 3282 let
adamc@687 3283 val (nExp, fm) = Fm.freshName fm
adamc@687 3284 val (nIni, fm) = Fm.freshName fm
adamc@687 3285
adamc@687 3286 val dExp = L'.DVal ("expunger",
adamc@687 3287 nExp,
adamc@687 3288 (L'.TFun (client, unit), loc),
adamc@687 3289 (L'.EAbs ("cli", client, unit, expunger ()), loc),
adamc@687 3290 "expunger")
adamc@687 3291 val dIni = L'.DVal ("initializer",
adamc@687 3292 nIni,
adamc@687 3293 (L'.TFun (unit, unit), loc),
adamc@687 3294 (L'.EAbs ("_", unit, unit, initializer ()), loc),
adamc@687 3295 "initializer")
adamc@683 3296 in
adamc@687 3297 (env, Fm.enter fm, (L'.DDatabase {name = s,
adamc@687 3298 expunge = nExp,
adamc@687 3299 initialize = nIni}, loc)
adamc@687 3300 :: (dExp, loc)
adamc@687 3301 :: (dIni, loc)
adamc@683 3302 :: ds)
adamc@683 3303 end
adamc@683 3304 | _ =>
adamc@683 3305 case monoDecl (env, fm) d of
adamc@683 3306 NONE => (env, fm, ds)
adamc@683 3307 | SOME (env, fm, ds') =>
adamc@683 3308 (env,
adamc@683 3309 Fm.enter fm,
adamc@683 3310 ds' @ Fm.decls fm @ ds))
adamc@683 3311 (env, Fm.empty (CoreUtil.File.maxName file + 1), []) file
adamc@25 3312 in
adamc@25 3313 rev ds
adamc@25 3314 end
adamc@25 3315
adamc@25 3316 end