annotate src/monoize.sml @ 466:1626dcba13ee

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