annotate src/monoize.sml @ 543:c01415a171ed

Start of sql_exp
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Dec 2008 09:19:53 -0500
parents ae03d09043c1
children 4154b4dc62c6
rev   line source
adamc@25 1 (* Copyright (c) 2008, Adam Chlipala
adamc@25 2 * All rights reserved.
adamc@25 3 *
adamc@25 4 * Redistribution and use in source and binary forms, with or without
adamc@25 5 * modification, are permitted provided that the following conditions are met:
adamc@25 6 *
adamc@25 7 * - Redistributions of source code must retain the above copyright notice,
adamc@25 8 * this list of conditions and the following disclaimer.
adamc@25 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@25 10 * this list of conditions and the following disclaimer in the documentation
adamc@25 11 * and/or other materials provided with the distribution.
adamc@25 12 * - The names of contributors may not be used to endorse or promote products
adamc@25 13 * derived from this software without specific prior written permission.
adamc@25 14 *
adamc@25 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@25 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@25 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@25 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@25 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@25 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@25 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@25 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@25 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@25 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@25 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@25 26 *)
adamc@25 27
adamc@25 28 structure Monoize :> MONOIZE = struct
adamc@25 29
adamc@25 30 structure E = ErrorMsg
adamc@25 31 structure Env = CoreEnv
adamc@25 32
adamc@25 33 structure L = Core
adamc@25 34 structure L' = Mono
adamc@25 35
adamc@196 36 structure IM = IntBinaryMap
adamc@196 37
adamc@385 38 val urlPrefix = ref "/"
adamc@385 39
adamc@196 40 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
adamc@25 41
adamc@252 42 structure U = MonoUtil
adamc@252 43
adamc@252 44 val liftExpInExp =
adamc@252 45 U.Exp.mapB {typ = fn t => t,
adamc@252 46 exp = fn bound => fn e =>
adamc@252 47 case e of
adamc@252 48 L'.ERel xn =>
adamc@252 49 if xn < bound then
adamc@252 50 e
adamc@252 51 else
adamc@252 52 L'.ERel (xn + 1)
adamc@252 53 | _ => e,
adamc@252 54 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@252 55 | (bound, _) => bound}
adamc@252 56
adamc@25 57 fun monoName env (all as (c, loc)) =
adamc@25 58 let
adamc@25 59 fun poly () =
adamc@25 60 (E.errorAt loc "Unsupported name constructor";
adamc@25 61 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 62 "")
adamc@25 63 in
adamc@25 64 case c of
adamc@25 65 L.CName s => s
adamc@25 66 | _ => poly ()
adamc@25 67 end
adamc@25 68
adamc@292 69 fun readType' (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 70 (L'.TOption t, loc)), loc)
adamc@292 71 fun readErrType (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc),
adamc@292 72 t), loc)
adamc@292 73 fun readType (t, loc) =
adamc@292 74 (L'.TRecord [("Read", readType' (t, loc)),
adamc@292 75 ("ReadError", readErrType (t, loc))],
adamc@292 76 loc)
adamc@292 77
adamc@196 78 fun monoType env =
adamc@25 79 let
adamc@196 80 fun mt env dtmap (all as (c, loc)) =
adamc@196 81 let
adamc@196 82 fun poly () =
adamc@196 83 (E.errorAt loc "Unsupported type constructor";
adamc@196 84 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 85 dummyTyp)
adamc@196 86 in
adamc@196 87 case c of
adamc@196 88 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 89 | L.TCFun _ => poly ()
adamc@196 90 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@196 91 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
adamc@196 92 | L.TRecord _ => poly ()
adamc@196 93
adamc@288 94 | L.CApp ((L.CFfi ("Basis", "option"), _), t) =>
adamc@288 95 (L'.TOption (mt env dtmap t), loc)
adamc@288 96
adamc@387 97 | L.CApp ((L.CFfi ("Basis", "eq"), _), t) =>
adamc@387 98 let
adamc@387 99 val t = mt env dtmap t
adamc@387 100 in
adamc@387 101 (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)
adamc@387 102 end
adamc@389 103 | L.CApp ((L.CFfi ("Basis", "num"), _), t) =>
adamc@389 104 let
adamc@389 105 val t = mt env dtmap t
adamc@389 106 in
adamc@417 107 (L'.TRecord [("Zero", t),
adamc@417 108 ("Neg", (L'.TFun (t, t), loc)),
adamc@389 109 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 110 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 111 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 112 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 113 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))],
adamc@389 114 loc)
adamc@389 115 end
adamc@391 116 | L.CApp ((L.CFfi ("Basis", "ord"), _), t) =>
adamc@391 117 let
adamc@391 118 val t = mt env dtmap t
adamc@391 119 in
adamc@391 120 (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)),
adamc@391 121 ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))],
adamc@391 122 loc)
adamc@391 123 end
adamc@286 124 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
adamc@286 125 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@290 126 | L.CApp ((L.CFfi ("Basis", "read"), _), t) =>
adamc@292 127 readType (mt env dtmap t, loc)
adamc@286 128
adamc@196 129 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 130 (L'.TFfi ("Basis", "string"), loc)
adamc@196 131 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 132 (L'.TFfi ("Basis", "string"), loc)
adamc@196 133
adamc@251 134 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@252 135 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
adamc@462 136 | L.CApp ((L.CFfi ("Basis", "http_cookie"), _), _) =>
adamc@462 137 (L'.TFfi ("Basis", "string"), loc)
adamc@252 138 | L.CApp ((L.CFfi ("Basis", "sql_table"), _), _) =>
adamc@252 139 (L'.TFfi ("Basis", "string"), loc)
adamc@338 140 | L.CFfi ("Basis", "sql_sequence") =>
adamc@338 141 (L'.TFfi ("Basis", "string"), loc)
adamc@252 142 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
adamc@252 143 (L'.TFfi ("Basis", "string"), loc)
adamc@252 144 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
adamc@252 145 (L'.TFfi ("Basis", "string"), loc)
adamc@252 146 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
adamc@252 147 (L'.TFfi ("Basis", "string"), loc)
adamc@252 148
adamc@252 149 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
adamc@252 150 (L'.TRecord [], loc)
adamc@252 151 | L.CFfi ("Basis", "sql_relop") =>
adamc@252 152 (L'.TFfi ("Basis", "string"), loc)
adamc@252 153 | L.CFfi ("Basis", "sql_direction") =>
adamc@252 154 (L'.TFfi ("Basis", "string"), loc)
adamc@252 155 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
adamc@252 156 (L'.TFfi ("Basis", "string"), loc)
adamc@252 157 | L.CFfi ("Basis", "sql_limit") =>
adamc@252 158 (L'.TFfi ("Basis", "string"), loc)
adamc@252 159 | L.CFfi ("Basis", "sql_offset") =>
adamc@252 160 (L'.TFfi ("Basis", "string"), loc)
adamc@252 161
adamc@252 162 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
adamc@252 163 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 164 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
adamc@252 165 (L'.TFfi ("Basis", "string"), loc)
adamc@252 166 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
adamc@252 167 (L'.TFfi ("Basis", "string"), loc)
adamc@252 168 | L.CFfi ("Basis", "sql_comparison") =>
adamc@252 169 (L'.TFfi ("Basis", "string"), loc)
adamc@252 170 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
adamc@252 171 (L'.TFfi ("Basis", "string"), loc)
adamc@252 172 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
adamc@252 173 (L'.TRecord [], loc)
adamc@252 174 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
adamc@252 175 (L'.TRecord [], loc)
adamc@441 176 | L.CApp ((L.CFfi ("Basis", "sql_nfunc"), _), _) =>
adamc@441 177 (L'.TFfi ("Basis", "string"), loc)
adamc@251 178
adamc@196 179 | L.CRel _ => poly ()
adamc@196 180 | L.CNamed n =>
adamc@196 181 (case IM.find (dtmap, n) of
adamc@196 182 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 183 | NONE =>
adamc@196 184 let
adamc@196 185 val r = ref (L'.Default, [])
adamc@196 186 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 187
adamc@196 188 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 189
adamc@196 190 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 191 in
adamc@196 192 case xs of
adamc@198 193 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 194 (L'.TDatatype (n, r), loc))
adamc@196 195 | _ => poly ()
adamc@196 196 end)
adamc@196 197 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 198 | L.CApp _ => poly ()
adamc@196 199 | L.CAbs _ => poly ()
adamc@196 200
adamc@196 201 | L.CName _ => poly ()
adamc@196 202
adamc@196 203 | L.CRecord _ => poly ()
adamc@196 204 | L.CConcat _ => poly ()
adamc@196 205 | L.CFold _ => poly ()
adamc@196 206 | L.CUnit => poly ()
adamc@214 207
adamc@214 208 | L.CTuple _ => poly ()
adamc@214 209 | L.CProj _ => poly ()
adamc@196 210 end
adamc@25 211 in
adamc@196 212 mt env IM.empty
adamc@25 213 end
adamc@25 214
adamc@25 215 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 216
adamc@179 217 structure IM = IntBinaryMap
adamc@179 218
adamc@179 219 datatype foo_kind =
adamc@179 220 Attr
adamc@179 221 | Url
adamc@179 222
adamc@179 223 fun fk2s fk =
adamc@179 224 case fk of
adamc@179 225 Attr => "attr"
adamc@179 226 | Url => "url"
adamc@179 227
adamc@179 228 structure Fm :> sig
adamc@179 229 type t
adamc@179 230
adamc@179 231 val empty : int -> t
adamc@179 232
adamc@179 233 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@179 234 val enter : t -> t
adamc@179 235 val decls : t -> L'.decl list
adamc@179 236 end = struct
adamc@179 237
adamc@179 238 structure M = BinaryMapFn(struct
adamc@179 239 type ord_key = foo_kind
adamc@179 240 fun compare x =
adamc@179 241 case x of
adamc@179 242 (Attr, Attr) => EQUAL
adamc@179 243 | (Attr, _) => LESS
adamc@179 244 | (_, Attr) => GREATER
adamc@179 245
adamc@179 246 | (Url, Url) => EQUAL
adamc@179 247 end)
adamc@179 248
adamc@179 249 type t = {
adamc@179 250 count : int,
adamc@179 251 map : int IM.map M.map,
adamc@179 252 decls : L'.decl list
adamc@179 253 }
adamc@179 254
adamc@179 255 fun empty count = {
adamc@179 256 count = count,
adamc@179 257 map = M.empty,
adamc@179 258 decls = []
adamc@179 259 }
adamc@179 260
adamc@179 261 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
adamc@179 262 fun decls ({decls, ...} : t) = decls
adamc@179 263
adamc@179 264 fun lookup (t as {count, map, decls}) k n thunk =
adamc@120 265 let
adamc@179 266 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 267 in
adamc@179 268 case IM.find (im, n) of
adamc@179 269 NONE =>
adamc@179 270 let
adamc@179 271 val n' = count
adamc@179 272 val (d, {count, map, decls}) = thunk count {count = count + 1,
adamc@179 273 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@179 274 decls = decls}
adamc@179 275 in
adamc@179 276 ({count = count,
adamc@179 277 map = map,
adamc@179 278 decls = d :: decls}, n')
adamc@179 279 end
adamc@179 280 | SOME n' => (t, n')
adamc@179 281 end
adamc@179 282
adamc@179 283 end
adamc@185 284
adamc@185 285
adamc@185 286 fun capitalize s =
adamc@185 287 if s = "" then
adamc@185 288 s
adamc@185 289 else
adamc@185 290 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 291
adamc@179 292 fun fooifyExp fk env =
adamc@179 293 let
adamc@179 294 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 295 case #1 e of
adamc@120 296 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 297 let
adamc@120 298 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 299 in
adamc@385 300 ((L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
adamc@120 301 end
adamc@120 302 | L'.EClosure (fnam, args) =>
adamc@120 303 let
adamc@120 304 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 305 val ft = monoType env ft
adamc@111 306
adamc@179 307 fun attrify (args, ft, e, fm) =
adamc@120 308 case (args, ft) of
adamc@179 309 ([], _) => (e, fm)
adamc@120 310 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 311 let
adamc@179 312 val (arg', fm) = fooify fm (arg, t)
adamc@179 313 in
adamc@179 314 attrify (args, ft,
adamc@179 315 (L'.EStrcat (e,
adamc@179 316 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 317 arg'), loc)), loc),
adamc@179 318 fm)
adamc@179 319 end
adamc@120 320 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 321 (e, fm))
adamc@120 322 in
adamc@385 323 attrify (args, ft, (L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
adamc@120 324 end
adamc@120 325 | _ =>
adamc@120 326 case t of
adamc@185 327 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 328
adamc@179 329 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 330 | L'.TRecord ((x, t) :: xts) =>
adamc@200 331 let
adamc@200 332 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 333 in
adamc@200 334 foldl (fn ((x, t), (se, fm)) =>
adamc@200 335 let
adamc@200 336 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 337 in
adamc@200 338 ((L'.EStrcat (se,
adamc@200 339 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 340 se'), loc)), loc),
adamc@200 341 fm)
adamc@200 342 end) (se, fm) xts
adamc@200 343 end
adamc@111 344
adamc@196 345 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 346 let
adamc@179 347 fun makeDecl n fm =
adamc@179 348 let
adamc@193 349 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 350
adamc@179 351 val (branches, fm) =
adamc@179 352 ListUtil.foldlMap
adamc@179 353 (fn ((x, n, to), fm) =>
adamc@179 354 case to of
adamc@179 355 NONE =>
adamc@188 356 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 357 (L'.EPrim (Prim.String x), loc)),
adamc@179 358 fm)
adamc@179 359 | SOME t =>
adamc@179 360 let
adamc@182 361 val t = monoType env t
adamc@182 362 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 363 in
adamc@188 364 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 365 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 366 arg), loc)),
adamc@179 367 fm)
adamc@179 368 end)
adamc@179 369 fm xncs
adamc@179 370
adamc@179 371 val dom = tAll
adamc@179 372 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 373 in
adamc@179 374 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 375 n,
adamc@179 376 (L'.TFun (dom, ran), loc),
adamc@179 377 (L'.EAbs ("x",
adamc@179 378 dom,
adamc@179 379 ran,
adamc@179 380 (L'.ECase ((L'.ERel 0, loc),
adamc@179 381 branches,
adamc@182 382 {disc = dom,
adamc@182 383 result = ran}), loc)), loc),
adamc@179 384 "")], loc),
adamc@179 385 fm)
adamc@179 386 end
adamc@179 387
adamc@179 388 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 389 in
adamc@179 390 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 391 end
adamc@164 392
adamc@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@253 1372 | L.EFfi ("Basis", "sql_eq") =>
adamc@253 1373 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@253 1374 | L.EFfi ("Basis", "sql_ne") =>
adamc@253 1375 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@253 1376 | L.EFfi ("Basis", "sql_lt") =>
adamc@253 1377 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@253 1378 | L.EFfi ("Basis", "sql_le") =>
adamc@253 1379 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@253 1380 | L.EFfi ("Basis", "sql_gt") =>
adamc@253 1381 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@253 1382 | L.EFfi ("Basis", "sql_ge") =>
adamc@253 1383 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 1384
adamc@253 1385 | L.ECApp (
adamc@253 1386 (L.ECApp (
adamc@253 1387 (L.ECApp (
adamc@253 1388 (L.ECApp (
adamc@254 1389 (L.ECApp (
adamc@264 1390 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 1391 _), _),
adamc@264 1392 _), _),
adamc@264 1393 _), _),
adamc@264 1394 _), _),
adamc@264 1395 _) =>
adamc@264 1396 let
adamc@264 1397 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 1398 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 1399 in
adamc@264 1400 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 1401 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@264 1402 strcat loc [sc "(",
adamc@264 1403 (L'.ERel 1, loc),
adamc@264 1404 sc " ",
adamc@264 1405 (L'.ERel 0, loc),
adamc@264 1406 sc ")"]), loc)), loc),
adamc@264 1407 fm)
adamc@264 1408 end
adamc@264 1409 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@264 1410
adamc@264 1411 | L.ECApp (
adamc@264 1412 (L.ECApp (
adamc@264 1413 (L.ECApp (
adamc@264 1414 (L.ECApp (
adamc@264 1415 (L.ECApp (
adamc@254 1416 (L.ECApp (
adamc@254 1417 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 1418 _), _),
adamc@254 1419 _), _),
adamc@254 1420 _), _),
adamc@254 1421 _), _),
adamc@254 1422 _), _),
adamc@254 1423 _) =>
adamc@254 1424 let
adamc@254 1425 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 1426 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 1427 in
adamc@254 1428 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 1429 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 1430 (L'.EAbs ("e2", s, s,
adamc@254 1431 strcat loc [sc "(",
adamc@254 1432 (L'.ERel 1, loc),
adamc@254 1433 sc " ",
adamc@254 1434 (L'.ERel 2, loc),
adamc@254 1435 sc " ",
adamc@254 1436 (L'.ERel 0, loc),
adamc@254 1437 sc ")"]), loc)), loc)), loc),
adamc@254 1438 fm)
adamc@254 1439 end
adamc@254 1440 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 1441 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 1442
adamc@254 1443 | L.ECApp (
adamc@254 1444 (L.ECApp (
adamc@254 1445 (L.ECApp (
adamc@254 1446 (L.ECApp (
adamc@253 1447 (L.EFfi ("Basis", "sql_comparison"), _),
adamc@253 1448 _), _),
adamc@253 1449 _), _),
adamc@253 1450 _), _),
adamc@253 1451 _) =>
adamc@253 1452 let
adamc@253 1453 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@253 1454 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@253 1455 in
adamc@253 1456 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@253 1457 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@253 1458 (L'.EAbs ("e2", s, s,
adamc@254 1459 strcat loc [sc "(",
adamc@254 1460 (L'.ERel 1, loc),
adamc@253 1461 sc " ",
adamc@253 1462 (L'.ERel 2, loc),
adamc@253 1463 sc " ",
adamc@254 1464 (L'.ERel 0, loc),
adamc@254 1465 sc ")"]), loc)), loc)), loc),
adamc@253 1466 fm)
adamc@253 1467 end
adamc@253 1468
adamc@253 1469 | L.ECApp (
adamc@253 1470 (L.ECApp (
adamc@253 1471 (L.ECApp (
adamc@253 1472 (L.ECApp (
adamc@253 1473 (L.ECApp (
adamc@253 1474 (L.ECApp (
adamc@253 1475 (L.ECApp (
adamc@253 1476 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 1477 _), _),
adamc@253 1478 _), _),
adamc@253 1479 _), _),
adamc@253 1480 _), _),
adamc@253 1481 _), _),
adamc@253 1482 (L.CName tab, _)), _),
adamc@311 1483 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
adamc@260 1484
adamc@260 1485 | L.ECApp (
adamc@260 1486 (L.ECApp (
adamc@260 1487 (L.ECApp (
adamc@260 1488 (L.ECApp (
adamc@261 1489 (L.ECApp (
adamc@261 1490 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 1491 _), _),
adamc@261 1492 _), _),
adamc@261 1493 _), _),
adamc@261 1494 _), _),
adamc@261 1495 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 1496
adamc@261 1497 | L.ECApp (
adamc@261 1498 (L.ECApp (
adamc@261 1499 (L.ECApp (
adamc@261 1500 (L.ECApp (
adamc@260 1501 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 1502 _), _),
adamc@260 1503 _), _),
adamc@260 1504 _), _),
adamc@260 1505 _) =>
adamc@260 1506 let
adamc@260 1507 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 1508 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 1509 in
adamc@260 1510 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 1511 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 1512 (L'.EAbs ("e2", s, s,
adamc@260 1513 strcat loc [sc "((",
adamc@260 1514 (L'.ERel 1, loc),
adamc@260 1515 sc ") ",
adamc@260 1516 (L'.ERel 2, loc),
adamc@260 1517 sc " (",
adamc@260 1518 (L'.ERel 0, loc),
adamc@260 1519 sc "))"]), loc)), loc)), loc),
adamc@260 1520 fm)
adamc@260 1521 end
adamc@260 1522
adamc@260 1523 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 1524 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 1525 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 1526
adamc@265 1527 | L.ECApp (
adamc@265 1528 (L.ECApp (
adamc@265 1529 (L.ECApp (
adamc@265 1530 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 1531 _), _),
adamc@265 1532 _), _),
adamc@265 1533 _) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@265 1534 (L'.EPrim (Prim.String "COUNT(*)"), loc)), loc),
adamc@265 1535 fm)
adamc@266 1536
adamc@266 1537 | L.ECApp (
adamc@266 1538 (L.ECApp (
adamc@266 1539 (L.ECApp (
adamc@266 1540 (L.ECApp (
adamc@266 1541 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 1542 _), _),
adamc@266 1543 _), _),
adamc@266 1544 _), _),
adamc@266 1545 _) =>
adamc@266 1546 let
adamc@266 1547 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 1548 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 1549 in
adamc@266 1550 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 1551 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@266 1552 strcat loc [(L'.ERel 1, loc),
adamc@266 1553 sc "(",
adamc@266 1554 (L'.ERel 0, loc),
adamc@266 1555 sc ")"]), loc)), loc),
adamc@266 1556 fm)
adamc@266 1557 end
adamc@266 1558
adamc@266 1559 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1560 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1561 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 1562 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1563 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 1564 fm)
adamc@266 1565 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 1566 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1567 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 1568 fm)
adamc@266 1569
adamc@266 1570 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1571 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1572 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 1573 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 1574 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1575 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 1576 fm)
adamc@266 1577 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 1578 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1579 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 1580 fm)
adamc@266 1581
adamc@268 1582 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 1583 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 1584
adamc@441 1585 | L.ECApp (
adamc@441 1586 (L.ECApp (
adamc@441 1587 (L.ECApp (
adamc@441 1588 (L.ECApp (
adamc@441 1589 (L.EFfi ("Basis", "sql_nfunc"), _),
adamc@441 1590 _), _),
adamc@441 1591 _), _),
adamc@441 1592 _), _),
adamc@441 1593 _) =>
adamc@441 1594 let
adamc@441 1595 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@441 1596 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@441 1597 in
adamc@441 1598 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc),
adamc@441 1599 fm)
adamc@441 1600 end
adamc@441 1601 | L.EFfi ("Basis", "sql_current_timestamp") => ((L'.EPrim (Prim.String "CURRENT_TIMESTAMP"), loc), fm)
adamc@441 1602
adamc@470 1603 | (L.ECApp (
adamc@470 1604 (L.ECApp (
adamc@470 1605 (L.ECApp (
adamc@470 1606 (L.ECApp (
adamc@470 1607 (L.EFfi ("Basis", "sql_is_null"), _), _),
adamc@470 1608 _), _),
adamc@470 1609 _), _),
adamc@470 1610 _), _)) =>
adamc@470 1611 let
adamc@470 1612 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@470 1613 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@470 1614 in
adamc@470 1615 ((L'.EAbs ("s", s, s,
adamc@470 1616 strcat loc [sc "(",
adamc@470 1617 (L'.ERel 0, loc),
adamc@470 1618 sc " IS NULL)"]), loc),
adamc@470 1619 fm)
adamc@470 1620 end
adamc@470 1621
adamc@338 1622 | L.EFfiApp ("Basis", "nextval", [e]) =>
adamc@338 1623 let
adamc@338 1624 val (e, fm) = monoExp (env, st, fm) e
adamc@338 1625 in
adamc@465 1626 ((L'.ENextval e, loc), fm)
adamc@338 1627 end
adamc@338 1628
adamc@139 1629 | L.EApp (
adamc@139 1630 (L.ECApp (
adamc@141 1631 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 1632 _), _),
adamc@179 1633 se) =>
adamc@179 1634 let
adamc@179 1635 val (se, fm) = monoExp (env, st, fm) se
adamc@179 1636 in
adamc@179 1637 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 1638 end
adamc@179 1639
adamc@95 1640 | L.EApp (
adamc@95 1641 (L.EApp (
adamc@95 1642 (L.ECApp (
adamc@95 1643 (L.ECApp (
adamc@95 1644 (L.ECApp (
adamc@139 1645 (L.ECApp (
adamc@140 1646 (L.EFfi ("Basis", "join"),
adamc@139 1647 _), _), _),
adamc@139 1648 _), _),
adamc@95 1649 _), _),
adamc@95 1650 _), _),
adamc@95 1651 xml1), _),
adamc@179 1652 xml2) =>
adamc@179 1653 let
adamc@179 1654 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 1655 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 1656 in
adamc@179 1657 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 1658 end
adamc@95 1659
adamc@95 1660 | L.EApp (
adamc@95 1661 (L.EApp (
adamc@104 1662 (L.EApp (
adamc@95 1663 (L.ECApp (
adamc@104 1664 (L.ECApp (
adamc@104 1665 (L.ECApp (
adamc@104 1666 (L.ECApp (
adamc@139 1667 (L.ECApp (
adamc@139 1668 (L.ECApp (
adamc@139 1669 (L.ECApp (
adamc@139 1670 (L.ECApp (
adamc@139 1671 (L.EFfi ("Basis", "tag"),
adamc@139 1672 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 1673 attrs), _),
adamc@95 1674 tag), _),
adamc@95 1675 xml) =>
adamc@95 1676 let
adamc@140 1677 fun getTag' (e, _) =
adamc@140 1678 case e of
adamc@143 1679 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 1680 | L.ECApp (e, t) => let
adamc@143 1681 val (tag, ts) = getTag' e
adamc@143 1682 in
adamc@143 1683 (tag, ts @ [t])
adamc@143 1684 end
adamc@140 1685 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 1686 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1687 ("", []))
adamc@140 1688
adamc@95 1689 fun getTag (e, _) =
adamc@95 1690 case e of
adamc@143 1691 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 1692 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 1693 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 1694 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1695 ("", []))
adamc@95 1696
adamc@143 1697 val (tag, targs) = getTag tag
adamc@95 1698
adamc@179 1699 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 1700
adamc@143 1701 fun tagStart tag =
adamc@104 1702 case #1 attrs of
adamc@104 1703 L'.ERecord xes =>
adamc@104 1704 let
adamc@104 1705 fun lowercaseFirst "" = ""
adamc@143 1706 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 1707 ^ String.extract (s, 1, NONE)
adamc@104 1708
adamc@104 1709 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 1710 in
adamc@410 1711 foldl (fn (("Action", _, _), acc) => acc
adamc@410 1712 | ((x, e, t), (s, fm)) =>
adamc@347 1713 case t of
adamc@347 1714 (L'.TFfi ("Basis", "bool"), _) =>
adamc@347 1715 let
adamc@347 1716 val s' = " " ^ lowercaseFirst x
adamc@347 1717 in
adamc@347 1718 ((L'.ECase (e,
adamc@347 1719 [((L'.PCon (L'.Enum,
adamc@347 1720 L'.PConFfi {mod = "Basis",
adamc@347 1721 datatyp = "bool",
adamc@347 1722 con = "True",
adamc@347 1723 arg = NONE},
adamc@347 1724 NONE), loc),
adamc@347 1725 (L'.EStrcat (s,
adamc@347 1726 (L'.EPrim (Prim.String s'), loc)), loc)),
adamc@347 1727 ((L'.PCon (L'.Enum,
adamc@347 1728 L'.PConFfi {mod = "Basis",
adamc@347 1729 datatyp = "bool",
adamc@347 1730 con = "False",
adamc@347 1731 arg = NONE},
adamc@347 1732 NONE), loc),
adamc@347 1733 s)],
adamc@347 1734 {disc = (L'.TFfi ("Basis", "bool"), loc),
adamc@347 1735 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@347 1736 fm)
adamc@347 1737 end
adamc@347 1738 | _ =>
adamc@347 1739 let
adamc@347 1740 val fooify =
adamc@347 1741 case x of
adamc@347 1742 "Href" => urlifyExp
adamc@347 1743 | "Link" => urlifyExp
adamc@347 1744 | _ => attrifyExp
adamc@120 1745
adamc@347 1746 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@179 1747
adamc@347 1748 val (e, fm) = fooify env fm (e, t)
adamc@347 1749 in
adamc@347 1750 ((L'.EStrcat (s,
adamc@347 1751 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@347 1752 (L'.EStrcat (e,
adamc@347 1753 (L'.EPrim (Prim.String "\""),
adamc@347 1754 loc)),
adamc@347 1755 loc)),
adamc@347 1756 loc)), loc),
adamc@347 1757 fm)
adamc@347 1758 end)
adamc@179 1759 (s, fm) xes
adamc@104 1760 end
adamc@143 1761 | _ => raise Fail "Non-record attributes!"
adamc@104 1762
adamc@143 1763 fun input typ =
adamc@143 1764 case targs of
adamc@155 1765 [_, (L.CName name, _)] =>
adamc@179 1766 let
adamc@179 1767 val (ts, fm) = tagStart "input"
adamc@179 1768 in
adamc@179 1769 ((L'.EStrcat (ts,
adamc@179 1770 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 1771 loc)), loc), fm)
adamc@179 1772 end
adamc@143 1773 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1774 raise Fail "No name passed to input tag")
adamc@104 1775
adamc@152 1776 fun normal (tag, extra) =
adamc@143 1777 let
adamc@179 1778 val (tagStart, fm) = tagStart tag
adamc@152 1779 val tagStart = case extra of
adamc@152 1780 NONE => tagStart
adamc@152 1781 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 1782
adamc@143 1783 fun normal () =
adamc@179 1784 let
adamc@179 1785 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1786 in
adamc@179 1787 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 1788 (L'.EStrcat (xml,
adamc@179 1789 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 1790 loc)), loc)),
adamc@179 1791 loc),
adamc@179 1792 fm)
adamc@179 1793 end
adamc@143 1794 in
adamc@143 1795 case xml of
adamc@143 1796 (L.EApp ((L.ECApp (
adamc@143 1797 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 1798 _), _),
adamc@143 1799 _), _),
adamc@143 1800 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 1801 if CharVector.all Char.isSpace s then
adamc@179 1802 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 1803 else
adamc@143 1804 normal ()
adamc@143 1805 | _ => normal ()
adamc@143 1806 end
adamc@152 1807 in
adamc@152 1808 case tag of
adamc@410 1809 "submit" => normal ("input type=\"submit\"", NONE)
adamc@152 1810
adamc@152 1811 | "textbox" =>
adamc@152 1812 (case targs of
adamc@152 1813 [_, (L.CName name, _)] =>
adamc@179 1814 let
adamc@179 1815 val (ts, fm) = tagStart "input"
adamc@179 1816 in
adamc@179 1817 ((L'.EStrcat (ts,
adamc@179 1818 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 1819 loc)), loc), fm)
adamc@179 1820 end
adamc@152 1821 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1822 raise Fail "No name passed to textarea tag"))
adamc@155 1823 | "password" => input "password"
adamc@361 1824 | "textarea" =>
adamc@152 1825 (case targs of
adamc@152 1826 [_, (L.CName name, _)] =>
adamc@179 1827 let
adamc@179 1828 val (ts, fm) = tagStart "textarea"
adamc@179 1829 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1830 in
adamc@179 1831 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1832 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1833 (L'.EStrcat (xml,
adamc@179 1834 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 1835 loc)), loc)),
adamc@179 1836 loc), fm)
adamc@179 1837 end
adamc@152 1838 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1839 raise Fail "No name passed to ltextarea tag"))
adamc@153 1840
adamc@190 1841 | "checkbox" => input "checkbox"
adamc@190 1842
adamc@153 1843 | "radio" =>
adamc@153 1844 (case targs of
adamc@153 1845 [_, (L.CName name, _)] =>
adamc@179 1846 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 1847 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1848 raise Fail "No name passed to radio tag"))
adamc@153 1849 | "radioOption" =>
adamc@153 1850 (case St.radioGroup st of
adamc@153 1851 NONE => raise Fail "No name for radioGroup"
adamc@153 1852 | SOME name =>
adamc@153 1853 normal ("input",
adamc@153 1854 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 1855
adamc@361 1856 | "select" =>
adamc@154 1857 (case targs of
adamc@154 1858 [_, (L.CName name, _)] =>
adamc@179 1859 let
adamc@179 1860 val (ts, fm) = tagStart "select"
adamc@179 1861 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1862 in
adamc@179 1863 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1864 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1865 (L'.EStrcat (xml,
adamc@179 1866 (L'.EPrim (Prim.String "</select>"),
adamc@179 1867 loc)), loc)),
adamc@179 1868 loc),
adamc@179 1869 fm)
adamc@179 1870 end
adamc@154 1871 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 1872 raise Fail "No name passed to lselect tag"))
adamc@154 1873
adamc@361 1874 | "option" => normal ("option", NONE)
adamc@154 1875
adamc@325 1876 | "tabl" => normal ("table", NONE)
adamc@152 1877 | _ => normal (tag, NONE)
adamc@95 1878 end
adamc@94 1879
adamc@141 1880 | L.EApp ((L.ECApp (
adamc@361 1881 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@141 1882 _), _),
adamc@141 1883 xml) =>
adamc@143 1884 let
adamc@143 1885 fun findSubmit (e, _) =
adamc@143 1886 case e of
adamc@143 1887 L.EApp (
adamc@143 1888 (L.EApp (
adamc@143 1889 (L.ECApp (
adamc@143 1890 (L.ECApp (
adamc@143 1891 (L.ECApp (
adamc@143 1892 (L.ECApp (
adamc@143 1893 (L.EFfi ("Basis", "join"),
adamc@143 1894 _), _), _),
adamc@143 1895 _), _),
adamc@143 1896 _), _),
adamc@143 1897 _), _),
adamc@143 1898 xml1), _),
adamc@143 1899 xml2) => (case findSubmit xml1 of
adamc@143 1900 Error => Error
adamc@143 1901 | NotFound => findSubmit xml2
adamc@143 1902 | Found e =>
adamc@143 1903 case findSubmit xml2 of
adamc@143 1904 NotFound => Found e
adamc@143 1905 | _ => Error)
adamc@143 1906 | L.EApp (
adamc@143 1907 (L.EApp (
adamc@143 1908 (L.EApp (
adamc@143 1909 (L.ECApp (
adamc@143 1910 (L.ECApp (
adamc@143 1911 (L.ECApp (
adamc@143 1912 (L.ECApp (
adamc@143 1913 (L.ECApp (
adamc@143 1914 (L.ECApp (
adamc@143 1915 (L.ECApp (
adamc@143 1916 (L.ECApp (
adamc@143 1917 (L.EFfi ("Basis", "tag"),
adamc@143 1918 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 1919 attrs), _),
adamc@143 1920 _), _),
adamc@143 1921 xml) =>
adamc@143 1922 (case #1 attrs of
adamc@143 1923 L.ERecord xes =>
adamc@143 1924 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 1925 | _ => NONE) xes of
adamc@143 1926 NONE => findSubmit xml
adamc@143 1927 | SOME et =>
adamc@143 1928 case findSubmit xml of
adamc@143 1929 NotFound => Found et
adamc@143 1930 | _ => Error)
adamc@143 1931 | _ => findSubmit xml)
adamc@143 1932 | _ => NotFound
adamc@143 1933
adamc@143 1934 val (action, actionT) = case findSubmit xml of
adamc@143 1935 NotFound => raise Fail "No submit found"
adamc@143 1936 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 1937 | Found et => et
adamc@143 1938
adamc@143 1939 val actionT = monoType env actionT
adamc@179 1940 val (action, fm) = monoExp (env, st, fm) action
adamc@179 1941 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 1942 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 1943 in
adamc@179 1944 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 1945 (L'.EStrcat (action,
adamc@179 1946 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 1947 (L'.EStrcat (xml,
adamc@179 1948 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 1949 fm)
adamc@143 1950 end
adamc@141 1951
adamc@148 1952 | L.EApp ((L.ECApp (
adamc@148 1953 (L.ECApp (
adamc@148 1954 (L.ECApp (
adamc@148 1955 (L.ECApp (
adamc@148 1956 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 1957 _), _),
adamc@148 1958 _), _),
adamc@148 1959 _), _),
adamc@179 1960 xml) => monoExp (env, st, fm) xml
adamc@148 1961
adamc@283 1962 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 1963 let
adamc@283 1964 val t = monoType env t
adamc@283 1965 in
adamc@283 1966 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 1967 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 1968 fm)
adamc@283 1969 end
adamc@283 1970
adamc@179 1971 | L.EApp (e1, e2) =>
adamc@179 1972 let
adamc@179 1973 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 1974 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 1975 in
adamc@179 1976 ((L'.EApp (e1, e2), loc), fm)
adamc@179 1977 end
adamc@26 1978 | L.EAbs (x, dom, ran, e) =>
adamc@179 1979 let
adamc@179 1980 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 1981 in
adamc@179 1982 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 1983 end
adamc@25 1984 | L.ECApp _ => poly ()
adamc@25 1985 | L.ECAbs _ => poly ()
adamc@25 1986
adamc@252 1987 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 1988 | L.EFfiApp (m, x, es) =>
adamc@252 1989 let
adamc@252 1990 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 1991 in
adamc@252 1992 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 1993 end
adamc@252 1994
adamc@179 1995 | L.ERecord xes =>
adamc@179 1996 let
adamc@179 1997 val (xes, fm) = ListUtil.foldlMap
adamc@179 1998 (fn ((x, e, t), fm) =>
adamc@179 1999 let
adamc@179 2000 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2001 in
adamc@179 2002 ((monoName env x,
adamc@179 2003 e,
adamc@179 2004 monoType env t), fm)
adamc@179 2005 end) fm xes
adamc@179 2006 in
adamc@179 2007 ((L'.ERecord xes, loc), fm)
adamc@179 2008 end
adamc@179 2009 | L.EField (e, x, _) =>
adamc@179 2010 let
adamc@179 2011 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2012 in
adamc@179 2013 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 2014 end
adamc@445 2015 | L.EConcat _ => poly ()
adamc@149 2016 | L.ECut _ => poly ()
adamc@493 2017 | L.ECutMulti _ => poly ()
adamc@73 2018 | L.EFold _ => poly ()
adamc@177 2019
adamc@182 2020 | L.ECase (e, pes, {disc, result}) =>
adamc@179 2021 let
adamc@179 2022 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2023 val (pes, fm) = ListUtil.foldlMap
adamc@179 2024 (fn ((p, e), fm) =>
adamc@179 2025 let
adamc@179 2026 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2027 in
adamc@182 2028 ((monoPat env p, e), fm)
adamc@179 2029 end) fm pes
adamc@179 2030 in
adamc@182 2031 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 2032 end
adamc@177 2033
adamc@179 2034 | L.EWrite e =>
adamc@179 2035 let
adamc@179 2036 val (e, fm) = monoExp (env, st, fm) e
adamc@179 2037 in
adamc@252 2038 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 2039 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 2040 end
adamc@110 2041
adamc@179 2042 | L.EClosure (n, es) =>
adamc@179 2043 let
adamc@179 2044 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 2045 monoExp (env, st, fm) e)
adamc@179 2046 fm es
adamc@179 2047 in
adamc@179 2048 ((L'.EClosure (n, es), loc), fm)
adamc@179 2049 end
adamc@450 2050
adamc@450 2051 | L.ELet (x, t, e1, e2) =>
adamc@450 2052 let
adamc@450 2053 val t' = monoType env t
adamc@450 2054 val (e1, fm) = monoExp (env, st, fm) e1
adamc@450 2055 val (e2, fm) = monoExp (Env.pushERel env x t, st, fm) e2
adamc@450 2056 in
adamc@450 2057 ((L'.ELet (x, t', e1, e2), loc), fm)
adamc@450 2058 end
adamc@25 2059 end
adamc@25 2060
adamc@179 2061 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 2062 let
adamc@25 2063 fun poly () =
adamc@25 2064 (E.errorAt loc "Unsupported declaration";
adamc@25 2065 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 2066 NONE)
adamc@25 2067 in
adamc@25 2068 case d of
adamc@25 2069 L.DCon _ => NONE
adamc@193 2070 | L.DDatatype (x, n, [], xncs) =>
adamc@193 2071 let
adamc@196 2072 val env' = Env.declBinds env all
adamc@196 2073 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 2074 in
adamc@273 2075 SOME (env', fm, [d])
adamc@193 2076 end
adamc@193 2077 | L.DDatatype _ => poly ()
adamc@179 2078 | L.DVal (x, n, t, e, s) =>
adamc@179 2079 let
adamc@179 2080 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 2081 in
adamc@179 2082 SOME (Env.pushENamed env x n t NONE s,
adamc@179 2083 fm,
adamc@273 2084 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 2085 end
adamc@128 2086 | L.DValRec vis =>
adamc@128 2087 let
adamc@128 2088 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 2089
adamc@179 2090 val (vis, fm) = ListUtil.foldlMap
adamc@179 2091 (fn ((x, n, t, e, s), fm) =>
adamc@179 2092 let
adamc@179 2093 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 2094 in
adamc@179 2095 ((x, n, monoType env t, e, s), fm)
adamc@179 2096 end)
adamc@179 2097 fm vis
adamc@128 2098 in
adamc@128 2099 SOME (env,
adamc@179 2100 fm,
adamc@273 2101 [(L'.DValRec vis, loc)])
adamc@128 2102 end
adamc@144 2103 | L.DExport (ek, n) =>
adamc@115 2104 let
adamc@120 2105 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 2106
adamc@120 2107 fun unwind (t, _) =
adamc@120 2108 case t of
adamc@120 2109 L.TFun (dom, ran) => dom :: unwind ran
adamc@314 2110 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@314 2111 (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: unwind t
adamc@120 2112 | _ => []
adamc@120 2113
adamc@120 2114 val ts = map (monoType env) (unwind t)
adamc@115 2115 in
adamc@273 2116 SOME (env, fm, [(L'.DExport (ek, s, n, ts), loc)])
adamc@115 2117 end
adamc@273 2118 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
adamc@251 2119 let
adamc@251 2120 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 2121 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 2122 val s = "uw_" ^ s
adamc@251 2123 val e = (L'.EPrim (Prim.String s), loc)
adamc@273 2124
adamc@273 2125 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@251 2126 in
adamc@251 2127 SOME (Env.pushENamed env x n t NONE s,
adamc@251 2128 fm,
adamc@273 2129 [(L'.DTable (s, xts), loc),
adamc@273 2130 (L'.DVal (x, n, t', e, s), loc)])
adamc@251 2131 end
adamc@273 2132 | L.DTable _ => poly ()
adamc@338 2133 | L.DSequence (x, n, s) =>
adamc@338 2134 let
adamc@338 2135 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 2136 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 2137 val s = "uw_" ^ s
adamc@338 2138 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 2139 in
adamc@338 2140 SOME (Env.pushENamed env x n t NONE s,
adamc@338 2141 fm,
adamc@338 2142 [(L'.DSequence s, loc),
adamc@338 2143 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 2144 end
adamc@273 2145 | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)])
adamc@462 2146 | L.DCookie (x, n, t, s) =>
adamc@462 2147 let
adamc@462 2148 val t = (L.CFfi ("Basis", "string"), loc)
adamc@462 2149 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@462 2150 val e = (L'.EPrim (Prim.String s), loc)
adamc@462 2151 in
adamc@462 2152 SOME (Env.pushENamed env x n t NONE s,
adamc@462 2153 fm,
adamc@462 2154 [(L'.DVal (x, n, t', e, s), loc)])
adamc@462 2155 end
adamc@25 2156 end
adamc@25 2157
adamc@25 2158 fun monoize env ds =
adamc@25 2159 let
adamc@385 2160 val p = !urlPrefix
adamc@385 2161 val () =
adamc@385 2162 if p = "" then
adamc@385 2163 urlPrefix := "/"
adamc@385 2164 else if String.sub (p, size p - 1) <> #"/" then
adamc@385 2165 urlPrefix := p ^ "/"
adamc@385 2166 else
adamc@385 2167 ()
adamc@385 2168
adamc@179 2169 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 2170 case monoDecl (env, fm) d of
adamc@179 2171 NONE => (env, fm, ds)
adamc@273 2172 | SOME (env, fm, ds') =>
adamc@179 2173 (env,
adamc@179 2174 Fm.enter fm,
adamc@273 2175 ds' @ Fm.decls fm @ ds))
adamc@179 2176 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 2177 in
adamc@25 2178 rev ds
adamc@25 2179 end
adamc@25 2180
adamc@25 2181 end