annotate src/monoize.sml @ 559:5d494183ca89

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