annotate src/monoize.sml @ 565:74800be65591

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