annotate src/monoize.sml @ 463:bb27c7efcd90

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