annotate src/monoize.sml @ 564:803b2f3bb86b

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