annotate src/monoize.sml @ 719:5c099b1308ae

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