annotate src/monoize.sml @ 462:21bb5bbba2e9

Setting a cookie
author Adam Chlipala <adamc@hcoop.net>
date Thu, 06 Nov 2008 11:29:16 -0500
parents 222cbc1da232
children bb27c7efcd90
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@462 958 (L'.EPrim (Prim.String "Cookie!"), loc)), loc)), loc),
adamc@462 959 fm)
adamc@462 960 end
adamc@462 961
adamc@462 962 | L.ECApp ((L.EFfi ("Basis", "setCookie"), _), t) =>
adamc@462 963 let
adamc@462 964 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@462 965 val un = (L'.TRecord [], loc)
adamc@462 966 val t = monoType env t
adamc@462 967 val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t)
adamc@462 968 in
adamc@462 969 ((L'.EAbs ("c", s, (L'.TFun (t, (L'.TFun (un, un), loc)), loc),
adamc@462 970 (L'.EAbs ("v", t, (L'.TFun (un, un), loc),
adamc@462 971 (L'.EAbs ("_", un, un,
adamc@462 972 (L'.EFfiApp ("Basis", "set_cookie", [(L'.ERel 2, loc), e]), loc)),
adamc@462 973 loc)), loc)), loc),
adamc@462 974 fm)
adamc@462 975 end
adamc@462 976
adamc@307 977 | L.EFfiApp ("Basis", "dml", [e]) =>
adamc@307 978 let
adamc@307 979 val (e, fm) = monoExp (env, st, fm) e
adamc@307 980 val un = (L'.TRecord [], loc)
adamc@307 981 in
adamc@307 982 ((L'.EAbs ("_", un, un,
adamc@307 983 (L'.EDml (liftExpInExp 0 e), loc)), loc),
adamc@307 984 fm)
adamc@307 985 end
adamc@308 986
adamc@307 987 | L.ECApp ((L.EFfi ("Basis", "insert"), _), fields) =>
adamc@307 988 (case monoType env (L.TRecord fields, loc) of
adamc@307 989 (L'.TRecord fields, _) =>
adamc@307 990 let
adamc@307 991 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@307 992 val fields = map (fn (x, _) => (x, s)) fields
adamc@307 993 val rt = (L'.TRecord fields, loc)
adamc@307 994 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@307 995 in
adamc@307 996 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
adamc@307 997 (L'.EAbs ("fs", rt, s,
adamc@307 998 strcat loc [sc "INSERT INTO ",
adamc@307 999 (L'.ERel 1, loc),
adamc@307 1000 sc " (",
adamc@311 1001 strcatComma loc (map (fn (x, _) => sc ("uw_" ^ x)) fields),
adamc@307 1002 sc ") VALUES (",
adamc@307 1003 strcatComma loc (map (fn (x, _) =>
adamc@307 1004 (L'.EField ((L'.ERel 0, loc),
adamc@307 1005 x), loc)) fields),
adamc@307 1006 sc ")"]), loc)), loc),
adamc@307 1007 fm)
adamc@307 1008 end
adamc@307 1009 | _ => poly ())
adamc@307 1010
adamc@403 1011 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), changed) =>
adamc@308 1012 (case monoType env (L.TRecord changed, loc) of
adamc@308 1013 (L'.TRecord changed, _) =>
adamc@308 1014 let
adamc@308 1015 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@308 1016 val changed = map (fn (x, _) => (x, s)) changed
adamc@308 1017 val rt = (L'.TRecord changed, loc)
adamc@308 1018 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@308 1019 in
adamc@308 1020 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@308 1021 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@308 1022 (L'.EAbs ("e", s, s,
adamc@308 1023 strcat loc [sc "UPDATE ",
adamc@308 1024 (L'.ERel 1, loc),
adamc@308 1025 sc " AS T SET ",
adamc@308 1026 strcatComma loc (map (fn (x, _) =>
adamc@311 1027 strcat loc [sc ("uw_" ^ x
adamc@308 1028 ^ " = "),
adamc@308 1029 (L'.EField
adamc@308 1030 ((L'.ERel 2,
adamc@308 1031 loc),
adamc@308 1032 x), loc)])
adamc@308 1033 changed),
adamc@308 1034 sc " WHERE ",
adamc@308 1035 (L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@308 1036 fm)
adamc@308 1037 end
adamc@308 1038 | _ => poly ())
adamc@308 1039
adamc@309 1040 | L.ECApp ((L.EFfi ("Basis", "delete"), _), _) =>
adamc@309 1041 let
adamc@309 1042 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@309 1043 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@309 1044 in
adamc@309 1045 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@309 1046 (L'.EAbs ("e", s, s,
adamc@309 1047 strcat loc [sc "DELETE FROM ",
adamc@309 1048 (L'.ERel 1, loc),
adamc@309 1049 sc " AS T WHERE ",
adamc@309 1050 (L'.ERel 0, loc)]), loc)), loc),
adamc@309 1051 fm)
adamc@309 1052 end
adamc@309 1053
adamc@252 1054 | L.ECApp (
adamc@252 1055 (L.ECApp (
adamc@252 1056 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 1057 exps), _),
adamc@252 1058 state) =>
adamc@252 1059 (case monoType env (L.TRecord exps, loc) of
adamc@252 1060 (L'.TRecord exps, _) =>
adamc@252 1061 let
adamc@252 1062 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1063 (case monoType env (L.TRecord xts, loc) of
adamc@252 1064 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1065 | _ => NONE)
adamc@252 1066 | _ => NONE) tables
adamc@252 1067 in
adamc@252 1068 if List.exists (fn x => x = NONE) tables then
adamc@252 1069 poly ()
adamc@252 1070 else
adamc@252 1071 let
adamc@252 1072 val tables = List.mapPartial (fn x => x) tables
adamc@252 1073 val state = monoType env state
adamc@252 1074 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1075 val un = (L'.TRecord [], loc)
adamc@252 1076
adamc@252 1077 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 1078 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 1079 (L'.TFun (state,
adamc@252 1080 (L'.TFun (un, state), loc)),
adamc@252 1081 loc)), loc)
adamc@252 1082
adamc@267 1083 val body' = (L'.EApp (
adamc@267 1084 (L'.EApp (
adamc@267 1085 (L'.EApp ((L'.ERel 4, loc),
adamc@267 1086 (L'.ERel 1, loc)), loc),
adamc@267 1087 (L'.ERel 0, loc)), loc),
adamc@267 1088 (L'.ERecord [], loc)), loc)
adamc@252 1089
adamc@252 1090 val body = (L'.EQuery {exps = exps,
adamc@252 1091 tables = tables,
adamc@252 1092 state = state,
adamc@252 1093 query = (L'.ERel 3, loc),
adamc@252 1094 body = body',
adamc@252 1095 initial = (L'.ERel 1, loc)},
adamc@252 1096 loc)
adamc@252 1097 in
adamc@252 1098 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 1099 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 1100 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 1101 (L'.EAbs ("_", un, state,
adamc@252 1102 body), loc)), loc)), loc)), loc), fm)
adamc@252 1103 end
adamc@252 1104 end
adamc@252 1105 | _ => poly ())
adamc@252 1106
adamc@252 1107 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 1108 let
adamc@252 1109 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1110 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1111 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1112 in
adamc@252 1113 ((L'.EAbs ("r",
adamc@252 1114 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 1115 s,
adamc@252 1116 strcat loc [gf "Rows",
adamc@261 1117 (L'.ECase (gf "OrderBy",
adamc@261 1118 [((L'.PPrim (Prim.String ""), loc), sc ""),
adamc@261 1119 ((L'.PWild, loc),
adamc@261 1120 strcat loc [sc " ORDER BY ",
adamc@261 1121 gf "OrderBy"])],
adamc@261 1122 {disc = s, result = s}), loc),
adamc@252 1123 gf "Limit",
adamc@252 1124 gf "Offset"]), loc), fm)
adamc@252 1125 end
adamc@252 1126
adamc@252 1127 | L.ECApp (
adamc@252 1128 (L.ECApp (
adamc@252 1129 (L.ECApp (
adamc@252 1130 (L.ECApp (
adamc@252 1131 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 1132 (L.CRecord (_, tables), _)), _),
adamc@252 1133 (L.CRecord (_, grouped), _)), _),
adamc@252 1134 (L.CRecord (_, stables), _)), _),
adamc@252 1135 sexps) =>
adamc@252 1136 let
adamc@252 1137 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1138 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1139 val un = (L'.TRecord [], loc)
adamc@252 1140 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1141
adamc@252 1142 fun doTables tables =
adamc@252 1143 let
adamc@252 1144 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1145 (case monoType env (L.TRecord xts, loc) of
adamc@252 1146 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1147 | _ => NONE)
adamc@252 1148 | _ => NONE) tables
adamc@252 1149 in
adamc@252 1150 if List.exists (fn x => x = NONE) tables then
adamc@252 1151 NONE
adamc@252 1152 else
adamc@260 1153 let
adamc@260 1154 val tables = List.mapPartial (fn x => x) tables
adamc@260 1155 val tables = ListMergeSort.sort
adamc@260 1156 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1157 tables
adamc@260 1158 val tables = map (fn (x, xts) =>
adamc@260 1159 (x, ListMergeSort.sort
adamc@260 1160 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1161 xts)) tables
adamc@260 1162 in
adamc@260 1163 SOME tables
adamc@260 1164 end
adamc@252 1165 end
adamc@252 1166 in
adamc@252 1167 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 1168 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@441 1169 let
adamc@441 1170 val sexps = ListMergeSort.sort
adamc@441 1171 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) sexps
adamc@441 1172 in
adamc@441 1173 ((L'.EAbs ("r",
adamc@441 1174 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
adamc@441 1175 ("Where", s),
adamc@441 1176 ("GroupBy", un),
adamc@441 1177 ("Having", s),
adamc@441 1178 ("SelectFields", un),
adamc@441 1179 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@441 1180 loc),
adamc@441 1181 s,
adamc@441 1182 strcat loc [sc "SELECT ",
adamc@441 1183 strcatComma loc (map (fn (x, t) =>
adamc@441 1184 strcat loc [
adamc@441 1185 (L'.EField (gf "SelectExps", x), loc),
adamc@441 1186 sc (" AS _" ^ x)
adamc@265 1187 ]) sexps
adamc@441 1188 @ map (fn (x, xts) =>
adamc@441 1189 strcatComma loc
adamc@441 1190 (map (fn (x', _) =>
adamc@441 1191 sc (x ^ ".uw_" ^ x'))
adamc@441 1192 xts)) stables),
adamc@441 1193 sc " FROM ",
adamc@441 1194 strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
adamc@441 1195 sc (" AS " ^ x)]) tables),
adamc@441 1196 (L'.ECase (gf "Where",
adamc@441 1197 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@441 1198 sc ""),
adamc@441 1199 ((L'.PWild, loc),
adamc@441 1200 strcat loc [sc " WHERE ", gf "Where"])],
adamc@441 1201 {disc = s,
adamc@441 1202 result = s}), loc),
adamc@441 1203
adamc@441 1204 if List.all (fn (x, xts) =>
adamc@441 1205 case List.find (fn (x', _) => x' = x) grouped of
adamc@441 1206 NONE => List.null xts
adamc@441 1207 | SOME (_, xts') =>
adamc@441 1208 List.all (fn (x, _) =>
adamc@441 1209 List.exists (fn (x', _) => x' = x)
adamc@441 1210 xts') xts) tables then
adamc@441 1211 sc ""
adamc@441 1212 else
adamc@441 1213 strcat loc [
adamc@441 1214 sc " GROUP BY ",
adamc@441 1215 strcatComma loc (map (fn (x, xts) =>
adamc@441 1216 strcatComma loc
adamc@441 1217 (map (fn (x', _) =>
adamc@441 1218 sc (x ^ ".uw_" ^ x'))
adamc@441 1219 xts)) grouped)
adamc@441 1220 ],
adamc@259 1221
adamc@441 1222 (L'.ECase (gf "Having",
adamc@441 1223 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@441 1224 sc ""),
adamc@441 1225 ((L'.PWild, loc),
adamc@441 1226 strcat loc [sc " HAVING ", gf "Having"])],
adamc@441 1227 {disc = s,
adamc@441 1228 result = s}), loc)
adamc@441 1229 ]), loc),
adamc@441 1230 fm)
adamc@441 1231 end
adamc@252 1232 | _ => poly ()
adamc@252 1233 end
adamc@252 1234
adamc@252 1235 | L.ECApp (
adamc@252 1236 (L.ECApp (
adamc@252 1237 (L.ECApp (
adamc@252 1238 (L.ECApp (
adamc@252 1239 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 1240 _), _),
adamc@252 1241 _), _),
adamc@252 1242 _), _),
adamc@252 1243 t) =>
adamc@252 1244 let
adamc@252 1245 val t = monoType env t
adamc@252 1246 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1247 in
adamc@252 1248 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 1249 (L'.ERel 0, loc)), loc), fm)
adamc@252 1250 end
adamc@252 1251
adamc@253 1252 | L.EFfi ("Basis", "sql_int") =>
adamc@253 1253 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1254 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1255 fm)
adamc@253 1256 | L.EFfi ("Basis", "sql_float") =>
adamc@253 1257 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1258 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1259 fm)
adamc@253 1260 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 1261 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1262 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1263 fm)
adamc@253 1264 | L.EFfi ("Basis", "sql_string") =>
adamc@253 1265 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1266 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1267 fm)
adamc@439 1268 | L.EFfi ("Basis", "sql_time") =>
adamc@439 1269 ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@439 1270 (L'.EFfiApp ("Basis", "sqlifyTime", [(L'.ERel 0, loc)]), loc)), loc),
adamc@439 1271 fm)
adamc@253 1272
adamc@252 1273 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 1274 ((L'.ERecord [], loc), fm)
adamc@252 1275 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 1276 ((L'.ERecord [], loc), fm)
adamc@252 1277
adamc@252 1278 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 1279 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@261 1280 | L.ECApp (
adamc@261 1281 (L.ECApp (
adamc@261 1282 (L.ECApp (
adamc@261 1283 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adamc@261 1284 _), _),
adamc@261 1285 _), _),
adamc@261 1286 _) =>
adamc@261 1287 let
adamc@261 1288 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 1289 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@261 1290 in
adamc@268 1291 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@268 1292 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adamc@268 1293 (L'.EAbs ("e2", s, s,
adamc@268 1294 (L'.ECase ((L'.ERel 0, loc),
adamc@268 1295 [((L'.PPrim (Prim.String ""), loc),
adamc@268 1296 strcat loc [(L'.ERel 2, loc),
adamc@268 1297 (L'.ERel 1, loc)]),
adamc@268 1298 ((L'.PWild, loc),
adamc@268 1299 strcat loc [(L'.ERel 2, loc),
adamc@268 1300 (L'.ERel 1, loc),
adamc@268 1301 sc ", ",
adamc@268 1302 (L'.ERel 0, loc)])],
adamc@268 1303 {disc = s, result = s}), loc)), loc)), loc)), loc),
adamc@261 1304 fm)
adamc@261 1305 end
adamc@252 1306
adamc@252 1307 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 1308 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@262 1309 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
adamc@262 1310 let
adamc@262 1311 val (e, fm) = monoExp (env, st, fm) e
adamc@262 1312 in
adamc@262 1313 (strcat loc [
adamc@262 1314 (L'.EPrim (Prim.String " LIMIT "), loc),
adamc@262 1315 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@262 1316 ],
adamc@262 1317 fm)
adamc@262 1318 end
adamc@262 1319
adamc@252 1320 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 1321 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@263 1322 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
adamc@263 1323 let
adamc@263 1324 val (e, fm) = monoExp (env, st, fm) e
adamc@263 1325 in
adamc@263 1326 (strcat loc [
adamc@263 1327 (L'.EPrim (Prim.String " OFFSET "), loc),
adamc@263 1328 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@263 1329 ],
adamc@263 1330 fm)
adamc@263 1331 end
adamc@253 1332
adamc@253 1333 | L.EFfi ("Basis", "sql_eq") =>
adamc@253 1334 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@253 1335 | L.EFfi ("Basis", "sql_ne") =>
adamc@253 1336 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@253 1337 | L.EFfi ("Basis", "sql_lt") =>
adamc@253 1338 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@253 1339 | L.EFfi ("Basis", "sql_le") =>
adamc@253 1340 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@253 1341 | L.EFfi ("Basis", "sql_gt") =>
adamc@253 1342 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@253 1343 | L.EFfi ("Basis", "sql_ge") =>
adamc@253 1344 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 1345
adamc@253 1346 | L.ECApp (
adamc@253 1347 (L.ECApp (
adamc@253 1348 (L.ECApp (
adamc@253 1349 (L.ECApp (
adamc@254 1350 (L.ECApp (
adamc@264 1351 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 1352 _), _),
adamc@264 1353 _), _),
adamc@264 1354 _), _),
adamc@264 1355 _), _),
adamc@264 1356 _) =>
adamc@264 1357 let
adamc@264 1358 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 1359 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 1360 in
adamc@264 1361 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 1362 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@264 1363 strcat loc [sc "(",
adamc@264 1364 (L'.ERel 1, loc),
adamc@264 1365 sc " ",
adamc@264 1366 (L'.ERel 0, loc),
adamc@264 1367 sc ")"]), loc)), loc),
adamc@264 1368 fm)
adamc@264 1369 end
adamc@264 1370 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@264 1371
adamc@264 1372 | L.ECApp (
adamc@264 1373 (L.ECApp (
adamc@264 1374 (L.ECApp (
adamc@264 1375 (L.ECApp (
adamc@264 1376 (L.ECApp (
adamc@254 1377 (L.ECApp (
adamc@254 1378 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 1379 _), _),
adamc@254 1380 _), _),
adamc@254 1381 _), _),
adamc@254 1382 _), _),
adamc@254 1383 _), _),
adamc@254 1384 _) =>
adamc@254 1385 let
adamc@254 1386 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 1387 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 1388 in
adamc@254 1389 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 1390 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 1391 (L'.EAbs ("e2", s, s,
adamc@254 1392 strcat loc [sc "(",
adamc@254 1393 (L'.ERel 1, loc),
adamc@254 1394 sc " ",
adamc@254 1395 (L'.ERel 2, loc),
adamc@254 1396 sc " ",
adamc@254 1397 (L'.ERel 0, loc),
adamc@254 1398 sc ")"]), loc)), loc)), loc),
adamc@254 1399 fm)
adamc@254 1400 end
adamc@254 1401 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 1402 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 1403
adamc@254 1404 | L.ECApp (
adamc@254 1405 (L.ECApp (
adamc@254 1406 (L.ECApp (
adamc@254 1407 (L.ECApp (
adamc@253 1408 (L.EFfi ("Basis", "sql_comparison"), _),
adamc@253 1409 _), _),
adamc@253 1410 _), _),
adamc@253 1411 _), _),
adamc@253 1412 _) =>
adamc@253 1413 let
adamc@253 1414 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@253 1415 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@253 1416 in
adamc@253 1417 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@253 1418 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@253 1419 (L'.EAbs ("e2", s, s,
adamc@254 1420 strcat loc [sc "(",
adamc@254 1421 (L'.ERel 1, loc),
adamc@253 1422 sc " ",
adamc@253 1423 (L'.ERel 2, loc),
adamc@253 1424 sc " ",
adamc@254 1425 (L'.ERel 0, loc),
adamc@254 1426 sc ")"]), loc)), loc)), loc),
adamc@253 1427 fm)
adamc@253 1428 end
adamc@253 1429
adamc@253 1430 | L.ECApp (
adamc@253 1431 (L.ECApp (
adamc@253 1432 (L.ECApp (
adamc@253 1433 (L.ECApp (
adamc@253 1434 (L.ECApp (
adamc@253 1435 (L.ECApp (
adamc@253 1436 (L.ECApp (
adamc@253 1437 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 1438 _), _),
adamc@253 1439 _), _),
adamc@253 1440 _), _),
adamc@253 1441 _), _),
adamc@253 1442 _), _),
adamc@253 1443 (L.CName tab, _)), _),
adamc@311 1444 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
adamc@260 1445
adamc@260 1446 | L.ECApp (
adamc@260 1447 (L.ECApp (
adamc@260 1448 (L.ECApp (
adamc@260 1449 (L.ECApp (
adamc@261 1450 (L.ECApp (
adamc@261 1451 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 1452 _), _),
adamc@261 1453 _), _),
adamc@261 1454 _), _),
adamc@261 1455 _), _),
adamc@261 1456 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 1457
adamc@261 1458 | L.ECApp (
adamc@261 1459 (L.ECApp (
adamc@261 1460 (L.ECApp (
adamc@261 1461 (L.ECApp (
adamc@260 1462 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 1463 _), _),
adamc@260 1464 _), _),
adamc@260 1465 _), _),
adamc@260 1466 _) =>
adamc@260 1467 let
adamc@260 1468 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 1469 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 1470 in
adamc@260 1471 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 1472 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 1473 (L'.EAbs ("e2", s, s,
adamc@260 1474 strcat loc [sc "((",
adamc@260 1475 (L'.ERel 1, loc),
adamc@260 1476 sc ") ",
adamc@260 1477 (L'.ERel 2, loc),
adamc@260 1478 sc " (",
adamc@260 1479 (L'.ERel 0, loc),
adamc@260 1480 sc "))"]), loc)), loc)), loc),
adamc@260 1481 fm)
adamc@260 1482 end
adamc@260 1483
adamc@260 1484 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 1485 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 1486 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 1487
adamc@265 1488 | L.ECApp (
adamc@265 1489 (L.ECApp (
adamc@265 1490 (L.ECApp (
adamc@265 1491 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 1492 _), _),
adamc@265 1493 _), _),
adamc@265 1494 _) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@265 1495 (L'.EPrim (Prim.String "COUNT(*)"), loc)), loc),
adamc@265 1496 fm)
adamc@266 1497
adamc@266 1498 | L.ECApp (
adamc@266 1499 (L.ECApp (
adamc@266 1500 (L.ECApp (
adamc@266 1501 (L.ECApp (
adamc@266 1502 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 1503 _), _),
adamc@266 1504 _), _),
adamc@266 1505 _), _),
adamc@266 1506 _) =>
adamc@266 1507 let
adamc@266 1508 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 1509 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 1510 in
adamc@266 1511 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 1512 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@266 1513 strcat loc [(L'.ERel 1, loc),
adamc@266 1514 sc "(",
adamc@266 1515 (L'.ERel 0, loc),
adamc@266 1516 sc ")"]), loc)), loc),
adamc@266 1517 fm)
adamc@266 1518 end
adamc@266 1519
adamc@266 1520 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1521 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1522 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 1523 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1524 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 1525 fm)
adamc@266 1526 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 1527 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1528 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 1529 fm)
adamc@266 1530
adamc@266 1531 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1532 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1533 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 1534 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 1535 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1536 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 1537 fm)
adamc@266 1538 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 1539 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1540 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 1541 fm)
adamc@266 1542
adamc@268 1543 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 1544 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 1545
adamc@441 1546 | L.ECApp (
adamc@441 1547 (L.ECApp (
adamc@441 1548 (L.ECApp (
adamc@441 1549 (L.ECApp (
adamc@441 1550 (L.EFfi ("Basis", "sql_nfunc"), _),
adamc@441 1551 _), _),
adamc@441 1552 _), _),
adamc@441 1553 _), _),
adamc@441 1554 _) =>
adamc@441 1555 let
adamc@441 1556 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@441 1557 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@441 1558 in
adamc@441 1559 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc),
adamc@441 1560 fm)
adamc@441 1561 end
adamc@441 1562 | L.EFfi ("Basis", "sql_current_timestamp") => ((L'.EPrim (Prim.String "CURRENT_TIMESTAMP"), loc), fm)
adamc@441 1563
adamc@338 1564 | L.EFfiApp ("Basis", "nextval", [e]) =>
adamc@338 1565 let
adamc@338 1566 val un = (L'.TRecord [], loc)
adamc@338 1567 val int = (L'.TFfi ("Basis", "int"), loc)
adamc@338 1568 val (e, fm) = monoExp (env, st, fm) e
adamc@338 1569 in
adamc@338 1570 ((L'.EAbs ("_", un, int,
adamc@338 1571 (L'.ENextval (liftExpInExp 0 e), loc)), loc),
adamc@338 1572 fm)
adamc@338 1573 end
adamc@338 1574
adamc@139 1575 | L.EApp (
adamc@139 1576 (L.ECApp (
adamc@141 1577 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 1578 _), _),
adamc@179 1579 se) =>
adamc@179 1580 let
adamc@179 1581 val (se, fm) = monoExp (env, st, fm) se
adamc@179 1582 in
adamc@179 1583 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 1584 end
adamc@179 1585
adamc@95 1586 | L.EApp (
adamc@95 1587 (L.EApp (
adamc@95 1588 (L.ECApp (
adamc@95 1589 (L.ECApp (
adamc@95 1590 (L.ECApp (
adamc@139 1591 (L.ECApp (
adamc@140 1592 (L.EFfi ("Basis", "join"),
adamc@139 1593 _), _), _),
adamc@139 1594 _), _),
adamc@95 1595 _), _),
adamc@95 1596 _), _),
adamc@95 1597 xml1), _),
adamc@179 1598 xml2) =>
adamc@179 1599 let
adamc@179 1600 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 1601 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 1602 in
adamc@179 1603 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 1604 end
adamc@95 1605
adamc@95 1606 | L.EApp (
adamc@95 1607 (L.EApp (
adamc@104 1608 (L.EApp (
adamc@95 1609 (L.ECApp (
adamc@104 1610 (L.ECApp (
adamc@104 1611 (L.ECApp (
adamc@104 1612 (L.ECApp (
adamc@139 1613 (L.ECApp (
adamc@139 1614 (L.ECApp (
adamc@139 1615 (L.ECApp (
adamc@139 1616 (L.ECApp (
adamc@139 1617 (L.EFfi ("Basis", "tag"),
adamc@139 1618 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 1619 attrs), _),
adamc@95 1620 tag), _),
adamc@95 1621 xml) =>
adamc@95 1622 let
adamc@140 1623 fun getTag' (e, _) =
adamc@140 1624 case e of
adamc@143 1625 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 1626 | L.ECApp (e, t) => let
adamc@143 1627 val (tag, ts) = getTag' e
adamc@143 1628 in
adamc@143 1629 (tag, ts @ [t])
adamc@143 1630 end
adamc@140 1631 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 1632 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1633 ("", []))
adamc@140 1634
adamc@95 1635 fun getTag (e, _) =
adamc@95 1636 case e of
adamc@143 1637 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 1638 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 1639 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 1640 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1641 ("", []))
adamc@95 1642
adamc@143 1643 val (tag, targs) = getTag tag
adamc@95 1644
adamc@179 1645 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 1646
adamc@143 1647 fun tagStart tag =
adamc@104 1648 case #1 attrs of
adamc@104 1649 L'.ERecord xes =>
adamc@104 1650 let
adamc@104 1651 fun lowercaseFirst "" = ""
adamc@143 1652 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 1653 ^ String.extract (s, 1, NONE)
adamc@104 1654
adamc@104 1655 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 1656 in
adamc@410 1657 foldl (fn (("Action", _, _), acc) => acc
adamc@410 1658 | ((x, e, t), (s, fm)) =>
adamc@347 1659 case t of
adamc@347 1660 (L'.TFfi ("Basis", "bool"), _) =>
adamc@347 1661 let
adamc@347 1662 val s' = " " ^ lowercaseFirst x
adamc@347 1663 in
adamc@347 1664 ((L'.ECase (e,
adamc@347 1665 [((L'.PCon (L'.Enum,
adamc@347 1666 L'.PConFfi {mod = "Basis",
adamc@347 1667 datatyp = "bool",
adamc@347 1668 con = "True",
adamc@347 1669 arg = NONE},
adamc@347 1670 NONE), loc),
adamc@347 1671 (L'.EStrcat (s,
adamc@347 1672 (L'.EPrim (Prim.String s'), loc)), loc)),
adamc@347 1673 ((L'.PCon (L'.Enum,
adamc@347 1674 L'.PConFfi {mod = "Basis",
adamc@347 1675 datatyp = "bool",
adamc@347 1676 con = "False",
adamc@347 1677 arg = NONE},
adamc@347 1678 NONE), loc),
adamc@347 1679 s)],
adamc@347 1680 {disc = (L'.TFfi ("Basis", "bool"), loc),
adamc@347 1681 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@347 1682 fm)
adamc@347 1683 end
adamc@347 1684 | _ =>
adamc@347 1685 let
adamc@347 1686 val fooify =
adamc@347 1687 case x of
adamc@347 1688 "Href" => urlifyExp
adamc@347 1689 | "Link" => urlifyExp
adamc@347 1690 | _ => attrifyExp
adamc@120 1691
adamc@347 1692 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@179 1693
adamc@347 1694 val (e, fm) = fooify env fm (e, t)
adamc@347 1695 in
adamc@347 1696 ((L'.EStrcat (s,
adamc@347 1697 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@347 1698 (L'.EStrcat (e,
adamc@347 1699 (L'.EPrim (Prim.String "\""),
adamc@347 1700 loc)),
adamc@347 1701 loc)),
adamc@347 1702 loc)), loc),
adamc@347 1703 fm)
adamc@347 1704 end)
adamc@179 1705 (s, fm) xes
adamc@104 1706 end
adamc@143 1707 | _ => raise Fail "Non-record attributes!"
adamc@104 1708
adamc@143 1709 fun input typ =
adamc@143 1710 case targs of
adamc@155 1711 [_, (L.CName name, _)] =>
adamc@179 1712 let
adamc@179 1713 val (ts, fm) = tagStart "input"
adamc@179 1714 in
adamc@179 1715 ((L'.EStrcat (ts,
adamc@179 1716 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 1717 loc)), loc), fm)
adamc@179 1718 end
adamc@143 1719 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1720 raise Fail "No name passed to input tag")
adamc@104 1721
adamc@152 1722 fun normal (tag, extra) =
adamc@143 1723 let
adamc@179 1724 val (tagStart, fm) = tagStart tag
adamc@152 1725 val tagStart = case extra of
adamc@152 1726 NONE => tagStart
adamc@152 1727 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 1728
adamc@143 1729 fun normal () =
adamc@179 1730 let
adamc@179 1731 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1732 in
adamc@179 1733 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 1734 (L'.EStrcat (xml,
adamc@179 1735 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 1736 loc)), loc)),
adamc@179 1737 loc),
adamc@179 1738 fm)
adamc@179 1739 end
adamc@143 1740 in
adamc@143 1741 case xml of
adamc@143 1742 (L.EApp ((L.ECApp (
adamc@143 1743 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 1744 _), _),
adamc@143 1745 _), _),
adamc@143 1746 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 1747 if CharVector.all Char.isSpace s then
adamc@179 1748 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 1749 else
adamc@143 1750 normal ()
adamc@143 1751 | _ => normal ()
adamc@143 1752 end
adamc@152 1753 in
adamc@152 1754 case tag of
adamc@410 1755 "submit" => normal ("input type=\"submit\"", NONE)
adamc@152 1756
adamc@152 1757 | "textbox" =>
adamc@152 1758 (case targs of
adamc@152 1759 [_, (L.CName name, _)] =>
adamc@179 1760 let
adamc@179 1761 val (ts, fm) = tagStart "input"
adamc@179 1762 in
adamc@179 1763 ((L'.EStrcat (ts,
adamc@179 1764 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 1765 loc)), loc), fm)
adamc@179 1766 end
adamc@152 1767 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1768 raise Fail "No name passed to textarea tag"))
adamc@155 1769 | "password" => input "password"
adamc@361 1770 | "textarea" =>
adamc@152 1771 (case targs of
adamc@152 1772 [_, (L.CName name, _)] =>
adamc@179 1773 let
adamc@179 1774 val (ts, fm) = tagStart "textarea"
adamc@179 1775 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1776 in
adamc@179 1777 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1778 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1779 (L'.EStrcat (xml,
adamc@179 1780 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 1781 loc)), loc)),
adamc@179 1782 loc), fm)
adamc@179 1783 end
adamc@152 1784 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1785 raise Fail "No name passed to ltextarea tag"))
adamc@153 1786
adamc@190 1787 | "checkbox" => input "checkbox"
adamc@190 1788
adamc@153 1789 | "radio" =>
adamc@153 1790 (case targs of
adamc@153 1791 [_, (L.CName name, _)] =>
adamc@179 1792 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 1793 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1794 raise Fail "No name passed to radio tag"))
adamc@153 1795 | "radioOption" =>
adamc@153 1796 (case St.radioGroup st of
adamc@153 1797 NONE => raise Fail "No name for radioGroup"
adamc@153 1798 | SOME name =>
adamc@153 1799 normal ("input",
adamc@153 1800 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 1801
adamc@361 1802 | "select" =>
adamc@154 1803 (case targs of
adamc@154 1804 [_, (L.CName name, _)] =>
adamc@179 1805 let
adamc@179 1806 val (ts, fm) = tagStart "select"
adamc@179 1807 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1808 in
adamc@179 1809 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1810 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1811 (L'.EStrcat (xml,
adamc@179 1812 (L'.EPrim (Prim.String "</select>"),
adamc@179 1813 loc)), loc)),
adamc@179 1814 loc),
adamc@179 1815 fm)
adamc@179 1816 end
adamc@154 1817 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 1818 raise Fail "No name passed to lselect tag"))
adamc@154 1819
adamc@361 1820 | "option" => normal ("option", NONE)
adamc@154 1821
adamc@325 1822 | "tabl" => normal ("table", NONE)
adamc@152 1823 | _ => normal (tag, NONE)
adamc@95 1824 end
adamc@94 1825
adamc@141 1826 | L.EApp ((L.ECApp (
adamc@361 1827 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@141 1828 _), _),
adamc@141 1829 xml) =>
adamc@143 1830 let
adamc@143 1831 fun findSubmit (e, _) =
adamc@143 1832 case e of
adamc@143 1833 L.EApp (
adamc@143 1834 (L.EApp (
adamc@143 1835 (L.ECApp (
adamc@143 1836 (L.ECApp (
adamc@143 1837 (L.ECApp (
adamc@143 1838 (L.ECApp (
adamc@143 1839 (L.EFfi ("Basis", "join"),
adamc@143 1840 _), _), _),
adamc@143 1841 _), _),
adamc@143 1842 _), _),
adamc@143 1843 _), _),
adamc@143 1844 xml1), _),
adamc@143 1845 xml2) => (case findSubmit xml1 of
adamc@143 1846 Error => Error
adamc@143 1847 | NotFound => findSubmit xml2
adamc@143 1848 | Found e =>
adamc@143 1849 case findSubmit xml2 of
adamc@143 1850 NotFound => Found e
adamc@143 1851 | _ => Error)
adamc@143 1852 | L.EApp (
adamc@143 1853 (L.EApp (
adamc@143 1854 (L.EApp (
adamc@143 1855 (L.ECApp (
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.EFfi ("Basis", "tag"),
adamc@143 1864 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 1865 attrs), _),
adamc@143 1866 _), _),
adamc@143 1867 xml) =>
adamc@143 1868 (case #1 attrs of
adamc@143 1869 L.ERecord xes =>
adamc@143 1870 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 1871 | _ => NONE) xes of
adamc@143 1872 NONE => findSubmit xml
adamc@143 1873 | SOME et =>
adamc@143 1874 case findSubmit xml of
adamc@143 1875 NotFound => Found et
adamc@143 1876 | _ => Error)
adamc@143 1877 | _ => findSubmit xml)
adamc@143 1878 | _ => NotFound
adamc@143 1879
adamc@143 1880 val (action, actionT) = case findSubmit xml of
adamc@143 1881 NotFound => raise Fail "No submit found"
adamc@143 1882 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 1883 | Found et => et
adamc@143 1884
adamc@143 1885 val actionT = monoType env actionT
adamc@179 1886 val (action, fm) = monoExp (env, st, fm) action
adamc@179 1887 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 1888 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 1889 in
adamc@179 1890 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 1891 (L'.EStrcat (action,
adamc@179 1892 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 1893 (L'.EStrcat (xml,
adamc@179 1894 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 1895 fm)
adamc@143 1896 end
adamc@141 1897
adamc@148 1898 | L.EApp ((L.ECApp (
adamc@148 1899 (L.ECApp (
adamc@148 1900 (L.ECApp (
adamc@148 1901 (L.ECApp (
adamc@148 1902 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 1903 _), _),
adamc@148 1904 _), _),
adamc@148 1905 _), _),
adamc@179 1906 xml) => monoExp (env, st, fm) xml
adamc@148 1907
adamc@283 1908 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 1909 let
adamc@283 1910 val t = monoType env t
adamc@283 1911 in
adamc@283 1912 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 1913 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 1914 fm)
adamc@283 1915 end
adamc@283 1916
adamc@179 1917 | L.EApp (e1, e2) =>
adamc@179 1918 let
adamc@179 1919 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 1920 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 1921 in
adamc@179 1922 ((L'.EApp (e1, e2), loc), fm)
adamc@179 1923 end
adamc@26 1924 | L.EAbs (x, dom, ran, e) =>
adamc@179 1925 let
adamc@179 1926 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 1927 in
adamc@179 1928 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 1929 end
adamc@25 1930 | L.ECApp _ => poly ()
adamc@25 1931 | L.ECAbs _ => poly ()
adamc@25 1932
adamc@252 1933 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 1934 | L.EFfiApp (m, x, es) =>
adamc@252 1935 let
adamc@252 1936 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 1937 in
adamc@252 1938 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 1939 end
adamc@252 1940
adamc@179 1941 | L.ERecord xes =>
adamc@179 1942 let
adamc@179 1943 val (xes, fm) = ListUtil.foldlMap
adamc@179 1944 (fn ((x, e, t), fm) =>
adamc@179 1945 let
adamc@179 1946 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1947 in
adamc@179 1948 ((monoName env x,
adamc@179 1949 e,
adamc@179 1950 monoType env t), fm)
adamc@179 1951 end) fm xes
adamc@179 1952 in
adamc@179 1953 ((L'.ERecord xes, loc), fm)
adamc@179 1954 end
adamc@179 1955 | L.EField (e, x, _) =>
adamc@179 1956 let
adamc@179 1957 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1958 in
adamc@179 1959 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 1960 end
adamc@445 1961 | L.EConcat _ => poly ()
adamc@149 1962 | L.ECut _ => poly ()
adamc@73 1963 | L.EFold _ => poly ()
adamc@177 1964
adamc@182 1965 | L.ECase (e, pes, {disc, result}) =>
adamc@179 1966 let
adamc@179 1967 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1968 val (pes, fm) = ListUtil.foldlMap
adamc@179 1969 (fn ((p, e), fm) =>
adamc@179 1970 let
adamc@179 1971 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1972 in
adamc@182 1973 ((monoPat env p, e), fm)
adamc@179 1974 end) fm pes
adamc@179 1975 in
adamc@182 1976 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 1977 end
adamc@177 1978
adamc@179 1979 | L.EWrite e =>
adamc@179 1980 let
adamc@179 1981 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1982 in
adamc@252 1983 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 1984 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 1985 end
adamc@110 1986
adamc@179 1987 | L.EClosure (n, es) =>
adamc@179 1988 let
adamc@179 1989 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 1990 monoExp (env, st, fm) e)
adamc@179 1991 fm es
adamc@179 1992 in
adamc@179 1993 ((L'.EClosure (n, es), loc), fm)
adamc@179 1994 end
adamc@450 1995
adamc@450 1996 | L.ELet (x, t, e1, e2) =>
adamc@450 1997 let
adamc@450 1998 val t' = monoType env t
adamc@450 1999 val (e1, fm) = monoExp (env, st, fm) e1
adamc@450 2000 val (e2, fm) = monoExp (Env.pushERel env x t, st, fm) e2
adamc@450 2001 in
adamc@450 2002 ((L'.ELet (x, t', e1, e2), loc), fm)
adamc@450 2003 end
adamc@25 2004 end
adamc@25 2005
adamc@179 2006 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 2007 let
adamc@25 2008 fun poly () =
adamc@25 2009 (E.errorAt loc "Unsupported declaration";
adamc@25 2010 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 2011 NONE)
adamc@25 2012 in
adamc@25 2013 case d of
adamc@25 2014 L.DCon _ => NONE
adamc@193 2015 | L.DDatatype (x, n, [], xncs) =>
adamc@193 2016 let
adamc@196 2017 val env' = Env.declBinds env all
adamc@196 2018 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 2019 in
adamc@273 2020 SOME (env', fm, [d])
adamc@193 2021 end
adamc@193 2022 | L.DDatatype _ => poly ()
adamc@179 2023 | L.DVal (x, n, t, e, s) =>
adamc@179 2024 let
adamc@179 2025 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 2026 in
adamc@179 2027 SOME (Env.pushENamed env x n t NONE s,
adamc@179 2028 fm,
adamc@273 2029 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 2030 end
adamc@128 2031 | L.DValRec vis =>
adamc@128 2032 let
adamc@128 2033 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 2034
adamc@179 2035 val (vis, fm) = ListUtil.foldlMap
adamc@179 2036 (fn ((x, n, t, e, s), fm) =>
adamc@179 2037 let
adamc@179 2038 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 2039 in
adamc@179 2040 ((x, n, monoType env t, e, s), fm)
adamc@179 2041 end)
adamc@179 2042 fm vis
adamc@128 2043 in
adamc@128 2044 SOME (env,
adamc@179 2045 fm,
adamc@273 2046 [(L'.DValRec vis, loc)])
adamc@128 2047 end
adamc@144 2048 | L.DExport (ek, n) =>
adamc@115 2049 let
adamc@120 2050 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 2051
adamc@120 2052 fun unwind (t, _) =
adamc@120 2053 case t of
adamc@120 2054 L.TFun (dom, ran) => dom :: unwind ran
adamc@314 2055 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@314 2056 (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: unwind t
adamc@120 2057 | _ => []
adamc@120 2058
adamc@120 2059 val ts = map (monoType env) (unwind t)
adamc@115 2060 in
adamc@273 2061 SOME (env, fm, [(L'.DExport (ek, s, n, ts), loc)])
adamc@115 2062 end
adamc@273 2063 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
adamc@251 2064 let
adamc@251 2065 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 2066 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 2067 val s = "uw_" ^ s
adamc@251 2068 val e = (L'.EPrim (Prim.String s), loc)
adamc@273 2069
adamc@273 2070 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@251 2071 in
adamc@251 2072 SOME (Env.pushENamed env x n t NONE s,
adamc@251 2073 fm,
adamc@273 2074 [(L'.DTable (s, xts), loc),
adamc@273 2075 (L'.DVal (x, n, t', e, s), loc)])
adamc@251 2076 end
adamc@273 2077 | L.DTable _ => poly ()
adamc@338 2078 | L.DSequence (x, n, s) =>
adamc@338 2079 let
adamc@338 2080 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 2081 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 2082 val s = "uw_" ^ s
adamc@338 2083 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 2084 in
adamc@338 2085 SOME (Env.pushENamed env x n t NONE s,
adamc@338 2086 fm,
adamc@338 2087 [(L'.DSequence s, loc),
adamc@338 2088 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 2089 end
adamc@273 2090 | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)])
adamc@462 2091 | L.DCookie (x, n, t, s) =>
adamc@462 2092 let
adamc@462 2093 val t = (L.CFfi ("Basis", "string"), loc)
adamc@462 2094 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@462 2095 val e = (L'.EPrim (Prim.String s), loc)
adamc@462 2096 in
adamc@462 2097 SOME (Env.pushENamed env x n t NONE s,
adamc@462 2098 fm,
adamc@462 2099 [(L'.DVal (x, n, t', e, s), loc)])
adamc@462 2100 end
adamc@25 2101 end
adamc@25 2102
adamc@25 2103 fun monoize env ds =
adamc@25 2104 let
adamc@385 2105 val p = !urlPrefix
adamc@385 2106 val () =
adamc@385 2107 if p = "" then
adamc@385 2108 urlPrefix := "/"
adamc@385 2109 else if String.sub (p, size p - 1) <> #"/" then
adamc@385 2110 urlPrefix := p ^ "/"
adamc@385 2111 else
adamc@385 2112 ()
adamc@385 2113
adamc@179 2114 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 2115 case monoDecl (env, fm) d of
adamc@179 2116 NONE => (env, fm, ds)
adamc@273 2117 | SOME (env, fm, ds') =>
adamc@179 2118 (env,
adamc@179 2119 Fm.enter fm,
adamc@273 2120 ds' @ Fm.decls fm @ ds))
adamc@179 2121 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 2122 in
adamc@25 2123 rev ds
adamc@25 2124 end
adamc@25 2125
adamc@25 2126 end