annotate src/monoize.sml @ 679:44f23712020d

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