annotate src/monoize.sml @ 390:519366a76603

num_float
author Adam Chlipala <adamc@hcoop.net>
date Tue, 21 Oct 2008 10:40:22 -0400
parents acaf9d19fbb7
children fe8f75f7e130
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@389 107 (L'.TRecord [("Neg", (L'.TFun (t, t), loc)),
adamc@389 108 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 109 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 110 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 111 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 112 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))],
adamc@389 113 loc)
adamc@389 114 end
adamc@286 115 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
adamc@286 116 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@290 117 | L.CApp ((L.CFfi ("Basis", "read"), _), t) =>
adamc@292 118 readType (mt env dtmap t, loc)
adamc@286 119
adamc@196 120 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 121 (L'.TFfi ("Basis", "string"), loc)
adamc@196 122 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 123 (L'.TFfi ("Basis", "string"), loc)
adamc@196 124
adamc@251 125 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@252 126 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
adamc@252 127 | L.CApp ((L.CFfi ("Basis", "sql_table"), _), _) =>
adamc@252 128 (L'.TFfi ("Basis", "string"), loc)
adamc@338 129 | L.CFfi ("Basis", "sql_sequence") =>
adamc@338 130 (L'.TFfi ("Basis", "string"), loc)
adamc@252 131 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
adamc@252 132 (L'.TFfi ("Basis", "string"), loc)
adamc@252 133 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
adamc@252 134 (L'.TFfi ("Basis", "string"), loc)
adamc@252 135 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
adamc@252 136 (L'.TFfi ("Basis", "string"), loc)
adamc@252 137
adamc@252 138 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
adamc@252 139 (L'.TRecord [], loc)
adamc@252 140 | L.CFfi ("Basis", "sql_relop") =>
adamc@252 141 (L'.TFfi ("Basis", "string"), loc)
adamc@252 142 | L.CFfi ("Basis", "sql_direction") =>
adamc@252 143 (L'.TFfi ("Basis", "string"), loc)
adamc@252 144 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
adamc@252 145 (L'.TFfi ("Basis", "string"), loc)
adamc@252 146 | L.CFfi ("Basis", "sql_limit") =>
adamc@252 147 (L'.TFfi ("Basis", "string"), loc)
adamc@252 148 | L.CFfi ("Basis", "sql_offset") =>
adamc@252 149 (L'.TFfi ("Basis", "string"), loc)
adamc@252 150
adamc@252 151 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
adamc@252 152 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 153 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
adamc@252 154 (L'.TFfi ("Basis", "string"), loc)
adamc@252 155 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
adamc@252 156 (L'.TFfi ("Basis", "string"), loc)
adamc@252 157 | L.CFfi ("Basis", "sql_comparison") =>
adamc@252 158 (L'.TFfi ("Basis", "string"), loc)
adamc@252 159 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
adamc@252 160 (L'.TFfi ("Basis", "string"), loc)
adamc@252 161 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
adamc@252 162 (L'.TRecord [], loc)
adamc@252 163 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
adamc@252 164 (L'.TRecord [], loc)
adamc@251 165
adamc@196 166 | L.CRel _ => poly ()
adamc@196 167 | L.CNamed n =>
adamc@196 168 (case IM.find (dtmap, n) of
adamc@196 169 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 170 | NONE =>
adamc@196 171 let
adamc@196 172 val r = ref (L'.Default, [])
adamc@196 173 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 174
adamc@196 175 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 176
adamc@196 177 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 178 in
adamc@196 179 case xs of
adamc@198 180 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 181 (L'.TDatatype (n, r), loc))
adamc@196 182 | _ => poly ()
adamc@196 183 end)
adamc@196 184 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 185 | L.CApp _ => poly ()
adamc@196 186 | L.CAbs _ => poly ()
adamc@196 187
adamc@196 188 | L.CName _ => poly ()
adamc@196 189
adamc@196 190 | L.CRecord _ => poly ()
adamc@196 191 | L.CConcat _ => poly ()
adamc@196 192 | L.CFold _ => poly ()
adamc@196 193 | L.CUnit => poly ()
adamc@214 194
adamc@214 195 | L.CTuple _ => poly ()
adamc@214 196 | L.CProj _ => poly ()
adamc@196 197 end
adamc@25 198 in
adamc@196 199 mt env IM.empty
adamc@25 200 end
adamc@25 201
adamc@25 202 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 203
adamc@179 204 structure IM = IntBinaryMap
adamc@179 205
adamc@179 206 datatype foo_kind =
adamc@179 207 Attr
adamc@179 208 | Url
adamc@179 209
adamc@179 210 fun fk2s fk =
adamc@179 211 case fk of
adamc@179 212 Attr => "attr"
adamc@179 213 | Url => "url"
adamc@179 214
adamc@179 215 structure Fm :> sig
adamc@179 216 type t
adamc@179 217
adamc@179 218 val empty : int -> t
adamc@179 219
adamc@179 220 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@179 221 val enter : t -> t
adamc@179 222 val decls : t -> L'.decl list
adamc@179 223 end = struct
adamc@179 224
adamc@179 225 structure M = BinaryMapFn(struct
adamc@179 226 type ord_key = foo_kind
adamc@179 227 fun compare x =
adamc@179 228 case x of
adamc@179 229 (Attr, Attr) => EQUAL
adamc@179 230 | (Attr, _) => LESS
adamc@179 231 | (_, Attr) => GREATER
adamc@179 232
adamc@179 233 | (Url, Url) => EQUAL
adamc@179 234 end)
adamc@179 235
adamc@179 236 type t = {
adamc@179 237 count : int,
adamc@179 238 map : int IM.map M.map,
adamc@179 239 decls : L'.decl list
adamc@179 240 }
adamc@179 241
adamc@179 242 fun empty count = {
adamc@179 243 count = count,
adamc@179 244 map = M.empty,
adamc@179 245 decls = []
adamc@179 246 }
adamc@179 247
adamc@179 248 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
adamc@179 249 fun decls ({decls, ...} : t) = decls
adamc@179 250
adamc@179 251 fun lookup (t as {count, map, decls}) k n thunk =
adamc@120 252 let
adamc@179 253 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 254 in
adamc@179 255 case IM.find (im, n) of
adamc@179 256 NONE =>
adamc@179 257 let
adamc@179 258 val n' = count
adamc@179 259 val (d, {count, map, decls}) = thunk count {count = count + 1,
adamc@179 260 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@179 261 decls = decls}
adamc@179 262 in
adamc@179 263 ({count = count,
adamc@179 264 map = map,
adamc@179 265 decls = d :: decls}, n')
adamc@179 266 end
adamc@179 267 | SOME n' => (t, n')
adamc@179 268 end
adamc@179 269
adamc@179 270 end
adamc@185 271
adamc@185 272
adamc@185 273 fun capitalize s =
adamc@185 274 if s = "" then
adamc@185 275 s
adamc@185 276 else
adamc@185 277 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 278
adamc@179 279 fun fooifyExp fk env =
adamc@179 280 let
adamc@179 281 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 282 case #1 e of
adamc@120 283 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 284 let
adamc@120 285 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 286 in
adamc@385 287 ((L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
adamc@120 288 end
adamc@120 289 | L'.EClosure (fnam, args) =>
adamc@120 290 let
adamc@120 291 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 292 val ft = monoType env ft
adamc@111 293
adamc@179 294 fun attrify (args, ft, e, fm) =
adamc@120 295 case (args, ft) of
adamc@179 296 ([], _) => (e, fm)
adamc@120 297 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 298 let
adamc@179 299 val (arg', fm) = fooify fm (arg, t)
adamc@179 300 in
adamc@179 301 attrify (args, ft,
adamc@179 302 (L'.EStrcat (e,
adamc@179 303 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 304 arg'), loc)), loc),
adamc@179 305 fm)
adamc@179 306 end
adamc@120 307 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 308 (e, fm))
adamc@120 309 in
adamc@385 310 attrify (args, ft, (L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm)
adamc@120 311 end
adamc@120 312 | _ =>
adamc@120 313 case t of
adamc@185 314 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 315
adamc@179 316 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 317 | L'.TRecord ((x, t) :: xts) =>
adamc@200 318 let
adamc@200 319 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 320 in
adamc@200 321 foldl (fn ((x, t), (se, fm)) =>
adamc@200 322 let
adamc@200 323 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 324 in
adamc@200 325 ((L'.EStrcat (se,
adamc@200 326 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 327 se'), loc)), loc),
adamc@200 328 fm)
adamc@200 329 end) (se, fm) xts
adamc@200 330 end
adamc@111 331
adamc@196 332 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 333 let
adamc@179 334 fun makeDecl n fm =
adamc@179 335 let
adamc@193 336 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 337
adamc@179 338 val (branches, fm) =
adamc@179 339 ListUtil.foldlMap
adamc@179 340 (fn ((x, n, to), fm) =>
adamc@179 341 case to of
adamc@179 342 NONE =>
adamc@188 343 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 344 (L'.EPrim (Prim.String x), loc)),
adamc@179 345 fm)
adamc@179 346 | SOME t =>
adamc@179 347 let
adamc@182 348 val t = monoType env t
adamc@182 349 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 350 in
adamc@188 351 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 352 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 353 arg), loc)),
adamc@179 354 fm)
adamc@179 355 end)
adamc@179 356 fm xncs
adamc@179 357
adamc@179 358 val dom = tAll
adamc@179 359 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 360 in
adamc@179 361 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 362 n,
adamc@179 363 (L'.TFun (dom, ran), loc),
adamc@179 364 (L'.EAbs ("x",
adamc@179 365 dom,
adamc@179 366 ran,
adamc@179 367 (L'.ECase ((L'.ERel 0, loc),
adamc@179 368 branches,
adamc@182 369 {disc = dom,
adamc@182 370 result = ran}), loc)), loc),
adamc@179 371 "")], loc),
adamc@179 372 fm)
adamc@179 373 end
adamc@179 374
adamc@179 375 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 376 in
adamc@179 377 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 378 end
adamc@164 379
adamc@120 380 | _ => (E.errorAt loc "Don't know how to encode attribute type";
adamc@120 381 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 382 (dummyExp, fm))
adamc@120 383 in
adamc@120 384 fooify
adamc@120 385 end
adamc@120 386
adamc@179 387 val attrifyExp = fooifyExp Attr
adamc@179 388 val urlifyExp = fooifyExp Url
adamc@105 389
adamc@143 390 datatype 'a failable_search =
adamc@143 391 Found of 'a
adamc@143 392 | NotFound
adamc@143 393 | Error
adamc@143 394
adamc@153 395 structure St :> sig
adamc@153 396 type t
adamc@153 397
adamc@153 398 val empty : t
adamc@153 399
adamc@153 400 val radioGroup : t -> string option
adamc@153 401 val setRadioGroup : t * string -> t
adamc@153 402 end = struct
adamc@153 403
adamc@153 404 type t = {
adamc@153 405 radioGroup : string option
adamc@153 406 }
adamc@153 407
adamc@153 408 val empty = {radioGroup = NONE}
adamc@153 409
adamc@153 410 fun radioGroup (t : t) = #radioGroup t
adamc@153 411
adamc@153 412 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 413
adamc@153 414 end
adamc@153 415
adamc@186 416 fun monoPatCon env pc =
adamc@178 417 case pc of
adamc@178 418 L.PConVar n => L'.PConVar n
adamc@188 419 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 420 arg = Option.map (monoType env) arg}
adamc@178 421
adamc@193 422 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 423
adamc@193 424 fun monoPat env (all as (p, loc)) =
adamc@193 425 let
adamc@193 426 fun poly () =
adamc@193 427 (E.errorAt loc "Unsupported pattern";
adamc@193 428 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 429 dummyPat)
adamc@193 430 in
adamc@193 431 case p of
adamc@193 432 L.PWild => (L'.PWild, loc)
adamc@193 433 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 434 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 435 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@288 436 | L.PCon (L.Option, _, [t], NONE) => (L'.PNone (monoType env t), loc)
adamc@288 437 | L.PCon (L.Option, _, [t], SOME p) => (L'.PSome (monoType env t, monoPat env p), loc)
adamc@193 438 | L.PCon _ => poly ()
adamc@193 439 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 440 end
adamc@178 441
adamc@252 442 fun strcat loc es =
adamc@252 443 case es of
adamc@252 444 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 445 | [e] => e
adamc@252 446 | _ =>
adamc@252 447 let
adamc@252 448 val e2 = List.last es
adamc@252 449 val es = List.take (es, length es - 1)
adamc@252 450 val e1 = List.last es
adamc@252 451 val es = List.take (es, length es - 1)
adamc@252 452 in
adamc@252 453 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
adamc@252 454 (L'.EStrcat (e1, e2), loc) es
adamc@252 455 end
adamc@252 456
adamc@252 457 fun strcatComma loc es =
adamc@252 458 case es of
adamc@252 459 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 460 | [e] => e
adamc@252 461 | _ =>
adamc@252 462 let
adamc@252 463 val e1 = List.last es
adamc@252 464 val es = List.take (es, length es - 1)
adamc@252 465 in
adamc@252 466 foldr (fn (e, e') =>
adamc@265 467 case (e, e') of
adamc@265 468 ((L'.EPrim (Prim.String ""), _), _) => e'
adamc@265 469 | (_, (L'.EPrim (Prim.String ""), _)) => e
adamc@252 470 | _ =>
adamc@252 471 (L'.EStrcat (e,
adamc@252 472 (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc))
adamc@252 473 e1 es
adamc@252 474 end
adamc@252 475
adamc@252 476 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
adamc@252 477
adamc@179 478 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 479 let
adamc@25 480 fun poly () =
adamc@25 481 (E.errorAt loc "Unsupported expression";
adamc@25 482 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 483 (dummyExp, fm))
adamc@389 484
adamc@389 485 fun numTy t =
adamc@389 486 (L'.TRecord [("Neg", (L'.TFun (t, t), loc)),
adamc@389 487 ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 488 ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 489 ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 490 ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 491 ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc)
adamc@389 492 fun numEx (t, neg, plus, minus, times, dv, md) =
adamc@389 493 ((L'.ERecord [("Neg", neg, (L'.TFun (t, t), loc)),
adamc@389 494 ("Plus", plus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 495 ("Minus", minus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 496 ("Times", times, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 497 ("Div", dv, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)),
adamc@389 498 ("Mod", md, (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc), fm)
adamc@25 499 in
adamc@25 500 case e of
adamc@179 501 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 502 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 503 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 504 | L.ECon (dk, pc, [], eo) =>
adamc@193 505 let
adamc@179 506 val (eo, fm) =
adamc@179 507 case eo of
adamc@179 508 NONE => (NONE, fm)
adamc@179 509 | SOME e =>
adamc@179 510 let
adamc@179 511 val (e, fm) = monoExp (env, st, fm) e
adamc@179 512 in
adamc@179 513 (SOME e, fm)
adamc@179 514 end
adamc@179 515 in
adamc@188 516 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 517 end
adamc@297 518 | L.ECon (L.Option, _, [t], NONE) =>
adamc@297 519 ((L'.ENone (monoType env t), loc), fm)
adamc@297 520 | L.ECon (L.Option, _, [t], SOME e) =>
adamc@297 521 let
adamc@297 522 val (e, fm) = monoExp (env, st, fm) e
adamc@297 523 in
adamc@297 524 ((L'.ESome (monoType env t, e), loc), fm)
adamc@297 525 end
adamc@193 526 | L.ECon _ => poly ()
adamc@94 527
adamc@387 528 | L.ECApp ((L.EFfi ("Basis", "eq"), _), t) =>
adamc@387 529 let
adamc@387 530 val t = monoType env t
adamc@387 531 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 532 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 533 in
adamc@387 534 ((L'.EAbs ("f", dom, dom,
adamc@387 535 (L'.ERel 0, loc)), loc), fm)
adamc@387 536 end
adamc@387 537 | L.ECApp ((L.EFfi ("Basis", "ne"), _), t) =>
adamc@387 538 let
adamc@387 539 val t = monoType env t
adamc@387 540 val b = (L'.TFfi ("Basis", "bool"), loc)
adamc@387 541 val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc)
adamc@387 542 in
adamc@387 543 ((L'.EAbs ("f", dom, dom,
adamc@387 544 (L'.EAbs ("x", t, (L'.TFun (t, b), loc),
adamc@387 545 (L'.EAbs ("y", t, b,
adamc@387 546 (L'.EUnop ("!", (L'.EApp ((L'.EApp ((L'.ERel 2, loc),
adamc@387 547 (L'.ERel 1, loc)), loc),
adamc@387 548 (L'.ERel 0, loc)), loc)), loc)),
adamc@387 549 loc)),
adamc@387 550 loc)),
adamc@387 551 loc), fm)
adamc@387 552 end
adamc@387 553 | L.EFfi ("Basis", "eq_int") =>
adamc@387 554 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@387 555 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@387 556 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@387 557 (L'.TFfi ("Basis", "bool"), loc),
adamc@387 558 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@387 559 fm)
adamc@388 560 | L.EFfi ("Basis", "eq_bool") =>
adamc@388 561 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 562 (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 563 (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc),
adamc@388 564 (L'.TFfi ("Basis", "bool"), loc),
adamc@388 565 (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 566 fm)
adamc@388 567 | L.EFfi ("Basis", "eq_string") =>
adamc@388 568 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc),
adamc@388 569 (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc),
adamc@388 570 (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc),
adamc@388 571 (L'.TFfi ("Basis", "bool"), loc),
adamc@388 572 (L'.EBinop ("!strcmp", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc),
adamc@388 573 fm)
adamc@387 574
adamc@389 575 | L.ECApp ((L.EFfi ("Basis", "neg"), _), t) =>
adamc@389 576 let
adamc@389 577 val t = monoType env t
adamc@389 578 in
adamc@389 579 ((L'.EAbs ("r", numTy t, (L'.TFun (t, t), loc),
adamc@389 580 (L'.EField ((L'.ERel 0, loc), "Neg"), loc)), loc), fm)
adamc@389 581 end
adamc@389 582 | L.ECApp ((L.EFfi ("Basis", "plus"), _), t) =>
adamc@389 583 let
adamc@389 584 val t = monoType env t
adamc@389 585 in
adamc@389 586 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 587 (L'.EField ((L'.ERel 0, loc), "Plus"), loc)), loc), fm)
adamc@389 588 end
adamc@389 589 | L.ECApp ((L.EFfi ("Basis", "minus"), _), t) =>
adamc@389 590 let
adamc@389 591 val t = monoType env t
adamc@389 592 in
adamc@389 593 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 594 (L'.EField ((L'.ERel 0, loc), "Minus"), loc)), loc), fm)
adamc@389 595 end
adamc@389 596 | L.ECApp ((L.EFfi ("Basis", "times"), _), t) =>
adamc@389 597 let
adamc@389 598 val t = monoType env t
adamc@389 599 in
adamc@389 600 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 601 (L'.EField ((L'.ERel 0, loc), "Times"), loc)), loc), fm)
adamc@389 602 end
adamc@389 603 | L.ECApp ((L.EFfi ("Basis", "div"), _), t) =>
adamc@389 604 let
adamc@389 605 val t = monoType env t
adamc@389 606 in
adamc@389 607 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 608 (L'.EField ((L'.ERel 0, loc), "Div"), loc)), loc), fm)
adamc@389 609 end
adamc@389 610 | L.ECApp ((L.EFfi ("Basis", "mod"), _), t) =>
adamc@389 611 let
adamc@389 612 val t = monoType env t
adamc@389 613 in
adamc@389 614 ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc),
adamc@389 615 (L'.EField ((L'.ERel 0, loc), "Mod"), loc)), loc), fm)
adamc@389 616 end
adamc@389 617 | L.EFfi ("Basis", "num_int") =>
adamc@389 618 let
adamc@389 619 fun intBin s =
adamc@389 620 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 621 (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "int"), loc)), loc),
adamc@389 622 (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc),
adamc@389 623 (L'.TFfi ("Basis", "int"), loc),
adamc@389 624 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@389 625 in
adamc@389 626 numEx ((L'.TFfi ("Basis", "int"), loc),
adamc@389 627 (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc),
adamc@389 628 (L'.TFfi ("Basis", "int"), loc),
adamc@389 629 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@389 630 intBin "+",
adamc@389 631 intBin "-",
adamc@389 632 intBin "*",
adamc@389 633 intBin "/",
adamc@389 634 intBin "%")
adamc@389 635 end
adamc@390 636 | L.EFfi ("Basis", "num_float") =>
adamc@390 637 let
adamc@390 638 fun floatBin s =
adamc@390 639 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 640 (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "float"), loc)), loc),
adamc@390 641 (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc),
adamc@390 642 (L'.TFfi ("Basis", "float"), loc),
adamc@390 643 (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc)
adamc@390 644 in
adamc@390 645 numEx ((L'.TFfi ("Basis", "float"), loc),
adamc@390 646 (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc),
adamc@390 647 (L'.TFfi ("Basis", "float"), loc),
adamc@390 648 (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc),
adamc@390 649 floatBin "+",
adamc@390 650 floatBin "-",
adamc@390 651 floatBin "*",
adamc@390 652 floatBin "/",
adamc@390 653 floatBin "fmod")
adamc@390 654 end
adamc@389 655
adamc@286 656 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
adamc@286 657 let
adamc@286 658 val t = monoType env t
adamc@286 659 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 660 in
adamc@286 661 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@286 662 (L'.ERel 0, loc)), loc), fm)
adamc@286 663 end
adamc@286 664 | L.EFfi ("Basis", "show_int") =>
adamc@286 665 ((L'.EFfi ("Basis", "intToString"), loc), fm)
adamc@286 666 | L.EFfi ("Basis", "show_float") =>
adamc@286 667 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
adamc@286 668 | L.EFfi ("Basis", "show_string") =>
adamc@286 669 let
adamc@286 670 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 671 in
adamc@286 672 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@286 673 end
adamc@286 674 | L.EFfi ("Basis", "show_bool") =>
adamc@286 675 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
adamc@286 676
adamc@290 677 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
adamc@290 678 let
adamc@290 679 val t = monoType env t
adamc@290 680 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 681 in
adamc@292 682 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
adamc@292 683 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
adamc@292 684 end
adamc@292 685 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
adamc@292 686 let
adamc@292 687 val t = monoType env t
adamc@292 688 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@292 689 in
adamc@292 690 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
adamc@292 691 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
adamc@290 692 end
adamc@290 693 | L.EFfi ("Basis", "read_int") =>
adamc@292 694 let
adamc@292 695 val t = (L'.TFfi ("Basis", "int"), loc)
adamc@292 696 in
adamc@292 697 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
adamc@292 698 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
adamc@292 699 loc),
adamc@292 700 fm)
adamc@292 701 end
adamc@290 702 | L.EFfi ("Basis", "read_float") =>
adamc@292 703 let
adamc@292 704 val t = (L'.TFfi ("Basis", "float"), loc)
adamc@292 705 in
adamc@292 706 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
adamc@292 707 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
adamc@292 708 loc),
adamc@292 709 fm)
adamc@292 710 end
adamc@290 711 | L.EFfi ("Basis", "read_string") =>
adamc@290 712 let
adamc@290 713 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 714 in
adamc@292 715 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
adamc@292 716 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
adamc@292 717 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
adamc@292 718 fm)
adamc@290 719 end
adamc@290 720 | L.EFfi ("Basis", "read_bool") =>
adamc@292 721 let
adamc@292 722 val t = (L'.TFfi ("Basis", "bool"), loc)
adamc@292 723 in
adamc@292 724 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
adamc@292 725 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
adamc@292 726 loc),
adamc@292 727 fm)
adamc@292 728 end
adamc@290 729
adamc@251 730 | L.ECApp ((L.EFfi ("Basis", "return"), _), t) =>
adamc@252 731 let
adamc@252 732 val t = monoType env t
adamc@252 733 in
adamc@252 734 ((L'.EAbs ("x", t,
adamc@252 735 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@252 736 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@252 737 (L'.ERel 1, loc)), loc)), loc), fm)
adamc@252 738 end
adamc@251 739 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), t1), _), t2) =>
adamc@251 740 let
adamc@251 741 val t1 = monoType env t1
adamc@251 742 val t2 = monoType env t2
adamc@251 743 val un = (L'.TRecord [], loc)
adamc@252 744 val mt1 = (L'.TFun (un, t1), loc)
adamc@252 745 val mt2 = (L'.TFun (un, t2), loc)
adamc@251 746 in
adamc@252 747 ((L'.EAbs ("m1", mt1, (L'.TFun (mt1, (L'.TFun (mt2, (L'.TFun (un, un), loc)), loc)), loc),
adamc@252 748 (L'.EAbs ("m2", mt2, (L'.TFun (un, un), loc),
adamc@252 749 (L'.EAbs ("_", un, un,
adamc@252 750 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
adamc@252 751 (L'.ERecord [], loc)), loc),
adamc@252 752 (L'.EApp (
adamc@252 753 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
adamc@252 754 (L'.ERecord [], loc)),
adamc@252 755 loc)), loc)), loc)), loc)), loc),
adamc@251 756 fm)
adamc@251 757 end
adamc@251 758
adamc@307 759 | L.EFfiApp ("Basis", "dml", [e]) =>
adamc@307 760 let
adamc@307 761 val (e, fm) = monoExp (env, st, fm) e
adamc@307 762 val un = (L'.TRecord [], loc)
adamc@307 763 in
adamc@307 764 ((L'.EAbs ("_", un, un,
adamc@307 765 (L'.EDml (liftExpInExp 0 e), loc)), loc),
adamc@307 766 fm)
adamc@307 767 end
adamc@308 768
adamc@307 769 | L.ECApp ((L.EFfi ("Basis", "insert"), _), fields) =>
adamc@307 770 (case monoType env (L.TRecord fields, loc) of
adamc@307 771 (L'.TRecord fields, _) =>
adamc@307 772 let
adamc@307 773 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@307 774 val fields = map (fn (x, _) => (x, s)) fields
adamc@307 775 val rt = (L'.TRecord fields, loc)
adamc@307 776 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@307 777 in
adamc@307 778 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
adamc@307 779 (L'.EAbs ("fs", rt, s,
adamc@307 780 strcat loc [sc "INSERT INTO ",
adamc@307 781 (L'.ERel 1, loc),
adamc@307 782 sc " (",
adamc@311 783 strcatComma loc (map (fn (x, _) => sc ("uw_" ^ x)) fields),
adamc@307 784 sc ") VALUES (",
adamc@307 785 strcatComma loc (map (fn (x, _) =>
adamc@307 786 (L'.EField ((L'.ERel 0, loc),
adamc@307 787 x), loc)) fields),
adamc@307 788 sc ")"]), loc)), loc),
adamc@307 789 fm)
adamc@307 790 end
adamc@307 791 | _ => poly ())
adamc@307 792
adamc@308 793 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), changed), _), _) =>
adamc@308 794 (case monoType env (L.TRecord changed, loc) of
adamc@308 795 (L'.TRecord changed, _) =>
adamc@308 796 let
adamc@308 797 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@308 798 val changed = map (fn (x, _) => (x, s)) changed
adamc@308 799 val rt = (L'.TRecord changed, loc)
adamc@308 800 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@308 801 in
adamc@308 802 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@308 803 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@308 804 (L'.EAbs ("e", s, s,
adamc@308 805 strcat loc [sc "UPDATE ",
adamc@308 806 (L'.ERel 1, loc),
adamc@308 807 sc " AS T SET ",
adamc@308 808 strcatComma loc (map (fn (x, _) =>
adamc@311 809 strcat loc [sc ("uw_" ^ x
adamc@308 810 ^ " = "),
adamc@308 811 (L'.EField
adamc@308 812 ((L'.ERel 2,
adamc@308 813 loc),
adamc@308 814 x), loc)])
adamc@308 815 changed),
adamc@308 816 sc " WHERE ",
adamc@308 817 (L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@308 818 fm)
adamc@308 819 end
adamc@308 820 | _ => poly ())
adamc@308 821
adamc@309 822 | L.ECApp ((L.EFfi ("Basis", "delete"), _), _) =>
adamc@309 823 let
adamc@309 824 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@309 825 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@309 826 in
adamc@309 827 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@309 828 (L'.EAbs ("e", s, s,
adamc@309 829 strcat loc [sc "DELETE FROM ",
adamc@309 830 (L'.ERel 1, loc),
adamc@309 831 sc " AS T WHERE ",
adamc@309 832 (L'.ERel 0, loc)]), loc)), loc),
adamc@309 833 fm)
adamc@309 834 end
adamc@309 835
adamc@252 836 | L.ECApp (
adamc@252 837 (L.ECApp (
adamc@252 838 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 839 exps), _),
adamc@252 840 state) =>
adamc@252 841 (case monoType env (L.TRecord exps, loc) of
adamc@252 842 (L'.TRecord exps, _) =>
adamc@252 843 let
adamc@252 844 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 845 (case monoType env (L.TRecord xts, loc) of
adamc@252 846 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 847 | _ => NONE)
adamc@252 848 | _ => NONE) tables
adamc@252 849 in
adamc@252 850 if List.exists (fn x => x = NONE) tables then
adamc@252 851 poly ()
adamc@252 852 else
adamc@252 853 let
adamc@252 854 val tables = List.mapPartial (fn x => x) tables
adamc@252 855 val state = monoType env state
adamc@252 856 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 857 val un = (L'.TRecord [], loc)
adamc@252 858
adamc@252 859 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 860 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 861 (L'.TFun (state,
adamc@252 862 (L'.TFun (un, state), loc)),
adamc@252 863 loc)), loc)
adamc@252 864
adamc@267 865 val body' = (L'.EApp (
adamc@267 866 (L'.EApp (
adamc@267 867 (L'.EApp ((L'.ERel 4, loc),
adamc@267 868 (L'.ERel 1, loc)), loc),
adamc@267 869 (L'.ERel 0, loc)), loc),
adamc@267 870 (L'.ERecord [], loc)), loc)
adamc@252 871
adamc@252 872 val body = (L'.EQuery {exps = exps,
adamc@252 873 tables = tables,
adamc@252 874 state = state,
adamc@252 875 query = (L'.ERel 3, loc),
adamc@252 876 body = body',
adamc@252 877 initial = (L'.ERel 1, loc)},
adamc@252 878 loc)
adamc@252 879 in
adamc@252 880 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 881 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 882 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 883 (L'.EAbs ("_", un, state,
adamc@252 884 body), loc)), loc)), loc)), loc), fm)
adamc@252 885 end
adamc@252 886 end
adamc@252 887 | _ => poly ())
adamc@252 888
adamc@252 889 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 890 let
adamc@252 891 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 892 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 893 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 894 in
adamc@252 895 ((L'.EAbs ("r",
adamc@252 896 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 897 s,
adamc@252 898 strcat loc [gf "Rows",
adamc@261 899 (L'.ECase (gf "OrderBy",
adamc@261 900 [((L'.PPrim (Prim.String ""), loc), sc ""),
adamc@261 901 ((L'.PWild, loc),
adamc@261 902 strcat loc [sc " ORDER BY ",
adamc@261 903 gf "OrderBy"])],
adamc@261 904 {disc = s, result = s}), loc),
adamc@252 905 gf "Limit",
adamc@252 906 gf "Offset"]), loc), fm)
adamc@252 907 end
adamc@252 908
adamc@252 909 | L.ECApp (
adamc@252 910 (L.ECApp (
adamc@252 911 (L.ECApp (
adamc@252 912 (L.ECApp (
adamc@252 913 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 914 (L.CRecord (_, tables), _)), _),
adamc@252 915 (L.CRecord (_, grouped), _)), _),
adamc@252 916 (L.CRecord (_, stables), _)), _),
adamc@252 917 sexps) =>
adamc@252 918 let
adamc@252 919 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 920 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 921 val un = (L'.TRecord [], loc)
adamc@252 922 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 923
adamc@252 924 fun doTables tables =
adamc@252 925 let
adamc@252 926 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 927 (case monoType env (L.TRecord xts, loc) of
adamc@252 928 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 929 | _ => NONE)
adamc@252 930 | _ => NONE) tables
adamc@252 931 in
adamc@252 932 if List.exists (fn x => x = NONE) tables then
adamc@252 933 NONE
adamc@252 934 else
adamc@260 935 let
adamc@260 936 val tables = List.mapPartial (fn x => x) tables
adamc@260 937 val tables = ListMergeSort.sort
adamc@260 938 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 939 tables
adamc@260 940 val tables = map (fn (x, xts) =>
adamc@260 941 (x, ListMergeSort.sort
adamc@260 942 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 943 xts)) tables
adamc@260 944 in
adamc@260 945 SOME tables
adamc@260 946 end
adamc@252 947 end
adamc@252 948 in
adamc@252 949 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 950 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@252 951 ((L'.EAbs ("r",
adamc@252 952 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
adamc@252 953 ("Where", s),
adamc@252 954 ("GroupBy", un),
adamc@252 955 ("Having", s),
adamc@252 956 ("SelectFields", un),
adamc@252 957 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@252 958 loc),
adamc@252 959 s,
adamc@252 960 strcat loc [sc "SELECT ",
adamc@261 961 strcatComma loc (map (fn (x, t) =>
adamc@261 962 strcat loc [
adamc@261 963 (L'.EField (gf "SelectExps", x), loc),
adamc@261 964 sc (" AS _" ^ x)
adamc@265 965 ]) sexps
adamc@265 966 @ map (fn (x, xts) =>
adamc@265 967 strcatComma loc
adamc@265 968 (map (fn (x', _) =>
adamc@311 969 sc (x ^ ".uw_" ^ x'))
adamc@265 970 xts)) stables),
adamc@252 971 sc " FROM ",
adamc@252 972 strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
adamc@253 973 sc (" AS " ^ x)]) tables),
adamc@258 974 (L'.ECase (gf "Where",
adamc@258 975 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@258 976 sc ""),
adamc@258 977 ((L'.PWild, loc),
adamc@258 978 strcat loc [sc " WHERE ", gf "Where"])],
adamc@258 979 {disc = s,
adamc@258 980 result = s}), loc),
adamc@258 981
adamc@255 982 if List.all (fn (x, xts) =>
adamc@255 983 case List.find (fn (x', _) => x' = x) grouped of
adamc@255 984 NONE => List.null xts
adamc@255 985 | SOME (_, xts') =>
adamc@255 986 List.all (fn (x, _) =>
adamc@255 987 List.exists (fn (x', _) => x' = x)
adamc@255 988 xts') xts) tables then
adamc@255 989 sc ""
adamc@255 990 else
adamc@255 991 strcat loc [
adamc@255 992 sc " GROUP BY ",
adamc@255 993 strcatComma loc (map (fn (x, xts) =>
adamc@255 994 strcatComma loc
adamc@255 995 (map (fn (x', _) =>
adamc@311 996 sc (x ^ ".uw_" ^ x'))
adamc@255 997 xts)) grouped)
adamc@259 998 ],
adamc@259 999
adamc@259 1000 (L'.ECase (gf "Having",
adamc@259 1001 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@259 1002 sc ""),
adamc@259 1003 ((L'.PWild, loc),
adamc@259 1004 strcat loc [sc " HAVING ", gf "Having"])],
adamc@259 1005 {disc = s,
adamc@259 1006 result = s}), loc)
adamc@252 1007 ]), loc),
adamc@252 1008 fm)
adamc@252 1009 | _ => poly ()
adamc@252 1010 end
adamc@252 1011
adamc@252 1012 | L.ECApp (
adamc@252 1013 (L.ECApp (
adamc@252 1014 (L.ECApp (
adamc@252 1015 (L.ECApp (
adamc@252 1016 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 1017 _), _),
adamc@252 1018 _), _),
adamc@252 1019 _), _),
adamc@252 1020 t) =>
adamc@252 1021 let
adamc@252 1022 val t = monoType env t
adamc@252 1023 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1024 in
adamc@252 1025 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 1026 (L'.ERel 0, loc)), loc), fm)
adamc@252 1027 end
adamc@252 1028
adamc@253 1029 | L.EFfi ("Basis", "sql_int") =>
adamc@253 1030 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1031 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1032 fm)
adamc@253 1033 | L.EFfi ("Basis", "sql_float") =>
adamc@253 1034 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1035 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1036 fm)
adamc@253 1037 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 1038 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1039 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1040 fm)
adamc@253 1041 | L.EFfi ("Basis", "sql_string") =>
adamc@253 1042 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1043 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1044 fm)
adamc@253 1045
adamc@252 1046 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 1047 ((L'.ERecord [], loc), fm)
adamc@252 1048 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 1049 ((L'.ERecord [], loc), fm)
adamc@252 1050
adamc@252 1051 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 1052 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@261 1053 | L.ECApp (
adamc@261 1054 (L.ECApp (
adamc@261 1055 (L.ECApp (
adamc@261 1056 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adamc@261 1057 _), _),
adamc@261 1058 _), _),
adamc@261 1059 _) =>
adamc@261 1060 let
adamc@261 1061 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 1062 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@261 1063 in
adamc@268 1064 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@268 1065 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adamc@268 1066 (L'.EAbs ("e2", s, s,
adamc@268 1067 (L'.ECase ((L'.ERel 0, loc),
adamc@268 1068 [((L'.PPrim (Prim.String ""), loc),
adamc@268 1069 strcat loc [(L'.ERel 2, loc),
adamc@268 1070 (L'.ERel 1, loc)]),
adamc@268 1071 ((L'.PWild, loc),
adamc@268 1072 strcat loc [(L'.ERel 2, loc),
adamc@268 1073 (L'.ERel 1, loc),
adamc@268 1074 sc ", ",
adamc@268 1075 (L'.ERel 0, loc)])],
adamc@268 1076 {disc = s, result = s}), loc)), loc)), loc)), loc),
adamc@261 1077 fm)
adamc@261 1078 end
adamc@252 1079
adamc@252 1080 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 1081 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@262 1082 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
adamc@262 1083 let
adamc@262 1084 val (e, fm) = monoExp (env, st, fm) e
adamc@262 1085 in
adamc@262 1086 (strcat loc [
adamc@262 1087 (L'.EPrim (Prim.String " LIMIT "), loc),
adamc@262 1088 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@262 1089 ],
adamc@262 1090 fm)
adamc@262 1091 end
adamc@262 1092
adamc@252 1093 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 1094 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@263 1095 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
adamc@263 1096 let
adamc@263 1097 val (e, fm) = monoExp (env, st, fm) e
adamc@263 1098 in
adamc@263 1099 (strcat loc [
adamc@263 1100 (L'.EPrim (Prim.String " OFFSET "), loc),
adamc@263 1101 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@263 1102 ],
adamc@263 1103 fm)
adamc@263 1104 end
adamc@253 1105
adamc@253 1106 | L.EFfi ("Basis", "sql_eq") =>
adamc@253 1107 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@253 1108 | L.EFfi ("Basis", "sql_ne") =>
adamc@253 1109 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@253 1110 | L.EFfi ("Basis", "sql_lt") =>
adamc@253 1111 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@253 1112 | L.EFfi ("Basis", "sql_le") =>
adamc@253 1113 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@253 1114 | L.EFfi ("Basis", "sql_gt") =>
adamc@253 1115 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@253 1116 | L.EFfi ("Basis", "sql_ge") =>
adamc@253 1117 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 1118
adamc@253 1119 | L.ECApp (
adamc@253 1120 (L.ECApp (
adamc@253 1121 (L.ECApp (
adamc@253 1122 (L.ECApp (
adamc@254 1123 (L.ECApp (
adamc@264 1124 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 1125 _), _),
adamc@264 1126 _), _),
adamc@264 1127 _), _),
adamc@264 1128 _), _),
adamc@264 1129 _) =>
adamc@264 1130 let
adamc@264 1131 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 1132 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 1133 in
adamc@264 1134 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 1135 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@264 1136 strcat loc [sc "(",
adamc@264 1137 (L'.ERel 1, loc),
adamc@264 1138 sc " ",
adamc@264 1139 (L'.ERel 0, loc),
adamc@264 1140 sc ")"]), loc)), loc),
adamc@264 1141 fm)
adamc@264 1142 end
adamc@264 1143 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@264 1144
adamc@264 1145 | L.ECApp (
adamc@264 1146 (L.ECApp (
adamc@264 1147 (L.ECApp (
adamc@264 1148 (L.ECApp (
adamc@264 1149 (L.ECApp (
adamc@254 1150 (L.ECApp (
adamc@254 1151 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 1152 _), _),
adamc@254 1153 _), _),
adamc@254 1154 _), _),
adamc@254 1155 _), _),
adamc@254 1156 _), _),
adamc@254 1157 _) =>
adamc@254 1158 let
adamc@254 1159 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 1160 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 1161 in
adamc@254 1162 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 1163 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 1164 (L'.EAbs ("e2", s, s,
adamc@254 1165 strcat loc [sc "(",
adamc@254 1166 (L'.ERel 1, loc),
adamc@254 1167 sc " ",
adamc@254 1168 (L'.ERel 2, loc),
adamc@254 1169 sc " ",
adamc@254 1170 (L'.ERel 0, loc),
adamc@254 1171 sc ")"]), loc)), loc)), loc),
adamc@254 1172 fm)
adamc@254 1173 end
adamc@254 1174 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 1175 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 1176
adamc@254 1177 | L.ECApp (
adamc@254 1178 (L.ECApp (
adamc@254 1179 (L.ECApp (
adamc@254 1180 (L.ECApp (
adamc@253 1181 (L.EFfi ("Basis", "sql_comparison"), _),
adamc@253 1182 _), _),
adamc@253 1183 _), _),
adamc@253 1184 _), _),
adamc@253 1185 _) =>
adamc@253 1186 let
adamc@253 1187 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@253 1188 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@253 1189 in
adamc@253 1190 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@253 1191 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@253 1192 (L'.EAbs ("e2", s, s,
adamc@254 1193 strcat loc [sc "(",
adamc@254 1194 (L'.ERel 1, loc),
adamc@253 1195 sc " ",
adamc@253 1196 (L'.ERel 2, loc),
adamc@253 1197 sc " ",
adamc@254 1198 (L'.ERel 0, loc),
adamc@254 1199 sc ")"]), loc)), loc)), loc),
adamc@253 1200 fm)
adamc@253 1201 end
adamc@253 1202
adamc@253 1203 | L.ECApp (
adamc@253 1204 (L.ECApp (
adamc@253 1205 (L.ECApp (
adamc@253 1206 (L.ECApp (
adamc@253 1207 (L.ECApp (
adamc@253 1208 (L.ECApp (
adamc@253 1209 (L.ECApp (
adamc@253 1210 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 1211 _), _),
adamc@253 1212 _), _),
adamc@253 1213 _), _),
adamc@253 1214 _), _),
adamc@253 1215 _), _),
adamc@253 1216 (L.CName tab, _)), _),
adamc@311 1217 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
adamc@260 1218
adamc@260 1219 | L.ECApp (
adamc@260 1220 (L.ECApp (
adamc@260 1221 (L.ECApp (
adamc@260 1222 (L.ECApp (
adamc@261 1223 (L.ECApp (
adamc@261 1224 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 1225 _), _),
adamc@261 1226 _), _),
adamc@261 1227 _), _),
adamc@261 1228 _), _),
adamc@261 1229 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 1230
adamc@261 1231 | L.ECApp (
adamc@261 1232 (L.ECApp (
adamc@261 1233 (L.ECApp (
adamc@261 1234 (L.ECApp (
adamc@260 1235 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 1236 _), _),
adamc@260 1237 _), _),
adamc@260 1238 _), _),
adamc@260 1239 _) =>
adamc@260 1240 let
adamc@260 1241 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 1242 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 1243 in
adamc@260 1244 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 1245 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 1246 (L'.EAbs ("e2", s, s,
adamc@260 1247 strcat loc [sc "((",
adamc@260 1248 (L'.ERel 1, loc),
adamc@260 1249 sc ") ",
adamc@260 1250 (L'.ERel 2, loc),
adamc@260 1251 sc " (",
adamc@260 1252 (L'.ERel 0, loc),
adamc@260 1253 sc "))"]), loc)), loc)), loc),
adamc@260 1254 fm)
adamc@260 1255 end
adamc@260 1256
adamc@260 1257 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 1258 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 1259 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 1260
adamc@265 1261 | L.ECApp (
adamc@265 1262 (L.ECApp (
adamc@265 1263 (L.ECApp (
adamc@265 1264 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 1265 _), _),
adamc@265 1266 _), _),
adamc@265 1267 _) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@265 1268 (L'.EPrim (Prim.String "COUNT(*)"), loc)), loc),
adamc@265 1269 fm)
adamc@266 1270
adamc@266 1271 | L.ECApp (
adamc@266 1272 (L.ECApp (
adamc@266 1273 (L.ECApp (
adamc@266 1274 (L.ECApp (
adamc@266 1275 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 1276 _), _),
adamc@266 1277 _), _),
adamc@266 1278 _), _),
adamc@266 1279 _) =>
adamc@266 1280 let
adamc@266 1281 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 1282 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 1283 in
adamc@266 1284 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 1285 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@266 1286 strcat loc [(L'.ERel 1, loc),
adamc@266 1287 sc "(",
adamc@266 1288 (L'.ERel 0, loc),
adamc@266 1289 sc ")"]), loc)), loc),
adamc@266 1290 fm)
adamc@266 1291 end
adamc@266 1292
adamc@266 1293 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1294 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1295 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 1296 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1297 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 1298 fm)
adamc@266 1299 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 1300 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1301 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 1302 fm)
adamc@266 1303
adamc@266 1304 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1305 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1306 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 1307 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 1308 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1309 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 1310 fm)
adamc@266 1311 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 1312 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1313 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 1314 fm)
adamc@266 1315
adamc@268 1316 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 1317 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 1318
adamc@338 1319 | L.EFfiApp ("Basis", "nextval", [e]) =>
adamc@338 1320 let
adamc@338 1321 val un = (L'.TRecord [], loc)
adamc@338 1322 val int = (L'.TFfi ("Basis", "int"), loc)
adamc@338 1323 val (e, fm) = monoExp (env, st, fm) e
adamc@338 1324 in
adamc@338 1325 ((L'.EAbs ("_", un, int,
adamc@338 1326 (L'.ENextval (liftExpInExp 0 e), loc)), loc),
adamc@338 1327 fm)
adamc@338 1328 end
adamc@338 1329
adamc@139 1330 | L.EApp (
adamc@139 1331 (L.ECApp (
adamc@141 1332 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 1333 _), _),
adamc@179 1334 se) =>
adamc@179 1335 let
adamc@179 1336 val (se, fm) = monoExp (env, st, fm) se
adamc@179 1337 in
adamc@179 1338 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 1339 end
adamc@179 1340
adamc@95 1341 | L.EApp (
adamc@95 1342 (L.EApp (
adamc@95 1343 (L.ECApp (
adamc@95 1344 (L.ECApp (
adamc@95 1345 (L.ECApp (
adamc@139 1346 (L.ECApp (
adamc@140 1347 (L.EFfi ("Basis", "join"),
adamc@139 1348 _), _), _),
adamc@139 1349 _), _),
adamc@95 1350 _), _),
adamc@95 1351 _), _),
adamc@95 1352 xml1), _),
adamc@179 1353 xml2) =>
adamc@179 1354 let
adamc@179 1355 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 1356 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 1357 in
adamc@179 1358 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 1359 end
adamc@95 1360
adamc@95 1361 | L.EApp (
adamc@95 1362 (L.EApp (
adamc@104 1363 (L.EApp (
adamc@95 1364 (L.ECApp (
adamc@104 1365 (L.ECApp (
adamc@104 1366 (L.ECApp (
adamc@104 1367 (L.ECApp (
adamc@139 1368 (L.ECApp (
adamc@139 1369 (L.ECApp (
adamc@139 1370 (L.ECApp (
adamc@139 1371 (L.ECApp (
adamc@139 1372 (L.EFfi ("Basis", "tag"),
adamc@139 1373 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 1374 attrs), _),
adamc@95 1375 tag), _),
adamc@95 1376 xml) =>
adamc@95 1377 let
adamc@140 1378 fun getTag' (e, _) =
adamc@140 1379 case e of
adamc@143 1380 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 1381 | L.ECApp (e, t) => let
adamc@143 1382 val (tag, ts) = getTag' e
adamc@143 1383 in
adamc@143 1384 (tag, ts @ [t])
adamc@143 1385 end
adamc@140 1386 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 1387 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1388 ("", []))
adamc@140 1389
adamc@95 1390 fun getTag (e, _) =
adamc@95 1391 case e of
adamc@143 1392 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 1393 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 1394 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 1395 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1396 ("", []))
adamc@95 1397
adamc@143 1398 val (tag, targs) = getTag tag
adamc@95 1399
adamc@179 1400 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 1401
adamc@143 1402 fun tagStart tag =
adamc@104 1403 case #1 attrs of
adamc@104 1404 L'.ERecord xes =>
adamc@104 1405 let
adamc@104 1406 fun lowercaseFirst "" = ""
adamc@143 1407 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 1408 ^ String.extract (s, 1, NONE)
adamc@104 1409
adamc@104 1410 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 1411 in
adamc@179 1412 foldl (fn ((x, e, t), (s, fm)) =>
adamc@347 1413 case t of
adamc@347 1414 (L'.TFfi ("Basis", "bool"), _) =>
adamc@347 1415 let
adamc@347 1416 val s' = " " ^ lowercaseFirst x
adamc@347 1417 in
adamc@347 1418 ((L'.ECase (e,
adamc@347 1419 [((L'.PCon (L'.Enum,
adamc@347 1420 L'.PConFfi {mod = "Basis",
adamc@347 1421 datatyp = "bool",
adamc@347 1422 con = "True",
adamc@347 1423 arg = NONE},
adamc@347 1424 NONE), loc),
adamc@347 1425 (L'.EStrcat (s,
adamc@347 1426 (L'.EPrim (Prim.String s'), loc)), loc)),
adamc@347 1427 ((L'.PCon (L'.Enum,
adamc@347 1428 L'.PConFfi {mod = "Basis",
adamc@347 1429 datatyp = "bool",
adamc@347 1430 con = "False",
adamc@347 1431 arg = NONE},
adamc@347 1432 NONE), loc),
adamc@347 1433 s)],
adamc@347 1434 {disc = (L'.TFfi ("Basis", "bool"), loc),
adamc@347 1435 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@347 1436 fm)
adamc@347 1437 end
adamc@347 1438 | _ =>
adamc@347 1439 let
adamc@347 1440 val fooify =
adamc@347 1441 case x of
adamc@347 1442 "Href" => urlifyExp
adamc@347 1443 | "Link" => urlifyExp
adamc@347 1444 | "Action" => urlifyExp
adamc@347 1445 | _ => attrifyExp
adamc@120 1446
adamc@347 1447 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@179 1448
adamc@347 1449 val (e, fm) = fooify env fm (e, t)
adamc@347 1450 in
adamc@347 1451 ((L'.EStrcat (s,
adamc@347 1452 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@347 1453 (L'.EStrcat (e,
adamc@347 1454 (L'.EPrim (Prim.String "\""),
adamc@347 1455 loc)),
adamc@347 1456 loc)),
adamc@347 1457 loc)), loc),
adamc@347 1458 fm)
adamc@347 1459 end)
adamc@179 1460 (s, fm) xes
adamc@104 1461 end
adamc@143 1462 | _ => raise Fail "Non-record attributes!"
adamc@104 1463
adamc@143 1464 fun input typ =
adamc@143 1465 case targs of
adamc@155 1466 [_, (L.CName name, _)] =>
adamc@179 1467 let
adamc@179 1468 val (ts, fm) = tagStart "input"
adamc@179 1469 in
adamc@179 1470 ((L'.EStrcat (ts,
adamc@179 1471 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 1472 loc)), loc), fm)
adamc@179 1473 end
adamc@143 1474 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1475 raise Fail "No name passed to input tag")
adamc@104 1476
adamc@152 1477 fun normal (tag, extra) =
adamc@143 1478 let
adamc@179 1479 val (tagStart, fm) = tagStart tag
adamc@152 1480 val tagStart = case extra of
adamc@152 1481 NONE => tagStart
adamc@152 1482 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 1483
adamc@143 1484 fun normal () =
adamc@179 1485 let
adamc@179 1486 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1487 in
adamc@179 1488 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 1489 (L'.EStrcat (xml,
adamc@179 1490 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 1491 loc)), loc)),
adamc@179 1492 loc),
adamc@179 1493 fm)
adamc@179 1494 end
adamc@143 1495 in
adamc@143 1496 case xml of
adamc@143 1497 (L.EApp ((L.ECApp (
adamc@143 1498 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 1499 _), _),
adamc@143 1500 _), _),
adamc@143 1501 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 1502 if CharVector.all Char.isSpace s then
adamc@179 1503 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 1504 else
adamc@143 1505 normal ()
adamc@143 1506 | _ => normal ()
adamc@143 1507 end
adamc@152 1508 in
adamc@152 1509 case tag of
adamc@179 1510 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
adamc@152 1511
adamc@152 1512 | "textbox" =>
adamc@152 1513 (case targs of
adamc@152 1514 [_, (L.CName name, _)] =>
adamc@179 1515 let
adamc@179 1516 val (ts, fm) = tagStart "input"
adamc@179 1517 in
adamc@179 1518 ((L'.EStrcat (ts,
adamc@179 1519 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 1520 loc)), loc), fm)
adamc@179 1521 end
adamc@152 1522 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1523 raise Fail "No name passed to textarea tag"))
adamc@155 1524 | "password" => input "password"
adamc@361 1525 | "textarea" =>
adamc@152 1526 (case targs of
adamc@152 1527 [_, (L.CName name, _)] =>
adamc@179 1528 let
adamc@179 1529 val (ts, fm) = tagStart "textarea"
adamc@179 1530 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1531 in
adamc@179 1532 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1533 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1534 (L'.EStrcat (xml,
adamc@179 1535 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 1536 loc)), loc)),
adamc@179 1537 loc), fm)
adamc@179 1538 end
adamc@152 1539 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1540 raise Fail "No name passed to ltextarea tag"))
adamc@153 1541
adamc@190 1542 | "checkbox" => input "checkbox"
adamc@190 1543
adamc@153 1544 | "radio" =>
adamc@153 1545 (case targs of
adamc@153 1546 [_, (L.CName name, _)] =>
adamc@179 1547 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 1548 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1549 raise Fail "No name passed to radio tag"))
adamc@153 1550 | "radioOption" =>
adamc@153 1551 (case St.radioGroup st of
adamc@153 1552 NONE => raise Fail "No name for radioGroup"
adamc@153 1553 | SOME name =>
adamc@153 1554 normal ("input",
adamc@153 1555 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 1556
adamc@361 1557 | "select" =>
adamc@154 1558 (case targs of
adamc@154 1559 [_, (L.CName name, _)] =>
adamc@179 1560 let
adamc@179 1561 val (ts, fm) = tagStart "select"
adamc@179 1562 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1563 in
adamc@179 1564 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1565 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1566 (L'.EStrcat (xml,
adamc@179 1567 (L'.EPrim (Prim.String "</select>"),
adamc@179 1568 loc)), loc)),
adamc@179 1569 loc),
adamc@179 1570 fm)
adamc@179 1571 end
adamc@154 1572 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 1573 raise Fail "No name passed to lselect tag"))
adamc@154 1574
adamc@361 1575 | "option" => normal ("option", NONE)
adamc@154 1576
adamc@325 1577 | "tabl" => normal ("table", NONE)
adamc@152 1578 | _ => normal (tag, NONE)
adamc@95 1579 end
adamc@94 1580
adamc@141 1581 | L.EApp ((L.ECApp (
adamc@361 1582 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@141 1583 _), _),
adamc@141 1584 xml) =>
adamc@143 1585 let
adamc@143 1586 fun findSubmit (e, _) =
adamc@143 1587 case e of
adamc@143 1588 L.EApp (
adamc@143 1589 (L.EApp (
adamc@143 1590 (L.ECApp (
adamc@143 1591 (L.ECApp (
adamc@143 1592 (L.ECApp (
adamc@143 1593 (L.ECApp (
adamc@143 1594 (L.EFfi ("Basis", "join"),
adamc@143 1595 _), _), _),
adamc@143 1596 _), _),
adamc@143 1597 _), _),
adamc@143 1598 _), _),
adamc@143 1599 xml1), _),
adamc@143 1600 xml2) => (case findSubmit xml1 of
adamc@143 1601 Error => Error
adamc@143 1602 | NotFound => findSubmit xml2
adamc@143 1603 | Found e =>
adamc@143 1604 case findSubmit xml2 of
adamc@143 1605 NotFound => Found e
adamc@143 1606 | _ => Error)
adamc@143 1607 | L.EApp (
adamc@143 1608 (L.EApp (
adamc@143 1609 (L.EApp (
adamc@143 1610 (L.ECApp (
adamc@143 1611 (L.ECApp (
adamc@143 1612 (L.ECApp (
adamc@143 1613 (L.ECApp (
adamc@143 1614 (L.ECApp (
adamc@143 1615 (L.ECApp (
adamc@143 1616 (L.ECApp (
adamc@143 1617 (L.ECApp (
adamc@143 1618 (L.EFfi ("Basis", "tag"),
adamc@143 1619 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 1620 attrs), _),
adamc@143 1621 _), _),
adamc@143 1622 xml) =>
adamc@143 1623 (case #1 attrs of
adamc@143 1624 L.ERecord xes =>
adamc@143 1625 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 1626 | _ => NONE) xes of
adamc@143 1627 NONE => findSubmit xml
adamc@143 1628 | SOME et =>
adamc@143 1629 case findSubmit xml of
adamc@143 1630 NotFound => Found et
adamc@143 1631 | _ => Error)
adamc@143 1632 | _ => findSubmit xml)
adamc@143 1633 | _ => NotFound
adamc@143 1634
adamc@143 1635 val (action, actionT) = case findSubmit xml of
adamc@143 1636 NotFound => raise Fail "No submit found"
adamc@143 1637 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 1638 | Found et => et
adamc@143 1639
adamc@143 1640 val actionT = monoType env actionT
adamc@179 1641 val (action, fm) = monoExp (env, st, fm) action
adamc@179 1642 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 1643 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 1644 in
adamc@179 1645 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 1646 (L'.EStrcat (action,
adamc@179 1647 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 1648 (L'.EStrcat (xml,
adamc@179 1649 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 1650 fm)
adamc@143 1651 end
adamc@141 1652
adamc@148 1653 | L.EApp ((L.ECApp (
adamc@148 1654 (L.ECApp (
adamc@148 1655 (L.ECApp (
adamc@148 1656 (L.ECApp (
adamc@148 1657 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 1658 _), _),
adamc@148 1659 _), _),
adamc@148 1660 _), _),
adamc@179 1661 xml) => monoExp (env, st, fm) xml
adamc@148 1662
adamc@283 1663 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 1664 let
adamc@283 1665 val t = monoType env t
adamc@283 1666 in
adamc@283 1667 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 1668 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 1669 fm)
adamc@283 1670 end
adamc@283 1671
adamc@179 1672 | L.EApp (e1, e2) =>
adamc@179 1673 let
adamc@179 1674 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 1675 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 1676 in
adamc@179 1677 ((L'.EApp (e1, e2), loc), fm)
adamc@179 1678 end
adamc@26 1679 | L.EAbs (x, dom, ran, e) =>
adamc@179 1680 let
adamc@179 1681 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 1682 in
adamc@179 1683 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 1684 end
adamc@25 1685 | L.ECApp _ => poly ()
adamc@25 1686 | L.ECAbs _ => poly ()
adamc@25 1687
adamc@252 1688 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 1689 | L.EFfiApp (m, x, es) =>
adamc@252 1690 let
adamc@252 1691 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 1692 in
adamc@252 1693 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 1694 end
adamc@252 1695
adamc@179 1696 | L.ERecord xes =>
adamc@179 1697 let
adamc@179 1698 val (xes, fm) = ListUtil.foldlMap
adamc@179 1699 (fn ((x, e, t), fm) =>
adamc@179 1700 let
adamc@179 1701 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1702 in
adamc@179 1703 ((monoName env x,
adamc@179 1704 e,
adamc@179 1705 monoType env t), fm)
adamc@179 1706 end) fm xes
adamc@179 1707 in
adamc@179 1708 ((L'.ERecord xes, loc), fm)
adamc@179 1709 end
adamc@179 1710 | L.EField (e, x, _) =>
adamc@179 1711 let
adamc@179 1712 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1713 in
adamc@179 1714 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 1715 end
adamc@343 1716 | L.EWith _ => poly ()
adamc@149 1717 | L.ECut _ => poly ()
adamc@73 1718 | L.EFold _ => poly ()
adamc@177 1719
adamc@182 1720 | L.ECase (e, pes, {disc, result}) =>
adamc@179 1721 let
adamc@179 1722 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1723 val (pes, fm) = ListUtil.foldlMap
adamc@179 1724 (fn ((p, e), fm) =>
adamc@179 1725 let
adamc@179 1726 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1727 in
adamc@182 1728 ((monoPat env p, e), fm)
adamc@179 1729 end) fm pes
adamc@179 1730 in
adamc@182 1731 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 1732 end
adamc@177 1733
adamc@179 1734 | L.EWrite e =>
adamc@179 1735 let
adamc@179 1736 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1737 in
adamc@252 1738 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 1739 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 1740 end
adamc@110 1741
adamc@179 1742 | L.EClosure (n, es) =>
adamc@179 1743 let
adamc@179 1744 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 1745 monoExp (env, st, fm) e)
adamc@179 1746 fm es
adamc@179 1747 in
adamc@179 1748 ((L'.EClosure (n, es), loc), fm)
adamc@179 1749 end
adamc@25 1750 end
adamc@25 1751
adamc@179 1752 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 1753 let
adamc@25 1754 fun poly () =
adamc@25 1755 (E.errorAt loc "Unsupported declaration";
adamc@25 1756 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 1757 NONE)
adamc@25 1758 in
adamc@25 1759 case d of
adamc@25 1760 L.DCon _ => NONE
adamc@193 1761 | L.DDatatype (x, n, [], xncs) =>
adamc@193 1762 let
adamc@196 1763 val env' = Env.declBinds env all
adamc@196 1764 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 1765 in
adamc@273 1766 SOME (env', fm, [d])
adamc@193 1767 end
adamc@193 1768 | L.DDatatype _ => poly ()
adamc@179 1769 | L.DVal (x, n, t, e, s) =>
adamc@179 1770 let
adamc@179 1771 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 1772 in
adamc@179 1773 SOME (Env.pushENamed env x n t NONE s,
adamc@179 1774 fm,
adamc@273 1775 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 1776 end
adamc@128 1777 | L.DValRec vis =>
adamc@128 1778 let
adamc@128 1779 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 1780
adamc@179 1781 val (vis, fm) = ListUtil.foldlMap
adamc@179 1782 (fn ((x, n, t, e, s), fm) =>
adamc@179 1783 let
adamc@179 1784 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 1785 in
adamc@179 1786 ((x, n, monoType env t, e, s), fm)
adamc@179 1787 end)
adamc@179 1788 fm vis
adamc@128 1789 in
adamc@128 1790 SOME (env,
adamc@179 1791 fm,
adamc@273 1792 [(L'.DValRec vis, loc)])
adamc@128 1793 end
adamc@144 1794 | L.DExport (ek, n) =>
adamc@115 1795 let
adamc@120 1796 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 1797
adamc@120 1798 fun unwind (t, _) =
adamc@120 1799 case t of
adamc@120 1800 L.TFun (dom, ran) => dom :: unwind ran
adamc@314 1801 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@314 1802 (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: unwind t
adamc@120 1803 | _ => []
adamc@120 1804
adamc@120 1805 val ts = map (monoType env) (unwind t)
adamc@115 1806 in
adamc@273 1807 SOME (env, fm, [(L'.DExport (ek, s, n, ts), loc)])
adamc@115 1808 end
adamc@273 1809 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
adamc@251 1810 let
adamc@251 1811 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 1812 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 1813 val s = "uw_" ^ s
adamc@251 1814 val e = (L'.EPrim (Prim.String s), loc)
adamc@273 1815
adamc@273 1816 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@251 1817 in
adamc@251 1818 SOME (Env.pushENamed env x n t NONE s,
adamc@251 1819 fm,
adamc@273 1820 [(L'.DTable (s, xts), loc),
adamc@273 1821 (L'.DVal (x, n, t', e, s), loc)])
adamc@251 1822 end
adamc@273 1823 | L.DTable _ => poly ()
adamc@338 1824 | L.DSequence (x, n, s) =>
adamc@338 1825 let
adamc@338 1826 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 1827 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 1828 val s = "uw_" ^ s
adamc@338 1829 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 1830 in
adamc@338 1831 SOME (Env.pushENamed env x n t NONE s,
adamc@338 1832 fm,
adamc@338 1833 [(L'.DSequence s, loc),
adamc@338 1834 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 1835 end
adamc@273 1836 | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)])
adamc@25 1837 end
adamc@25 1838
adamc@25 1839 fun monoize env ds =
adamc@25 1840 let
adamc@385 1841 val p = !urlPrefix
adamc@385 1842 val () =
adamc@385 1843 if p = "" then
adamc@385 1844 urlPrefix := "/"
adamc@385 1845 else if String.sub (p, size p - 1) <> #"/" then
adamc@385 1846 urlPrefix := p ^ "/"
adamc@385 1847 else
adamc@385 1848 ()
adamc@385 1849
adamc@179 1850 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 1851 case monoDecl (env, fm) d of
adamc@179 1852 NONE => (env, fm, ds)
adamc@273 1853 | SOME (env, fm, ds') =>
adamc@179 1854 (env,
adamc@179 1855 Fm.enter fm,
adamc@273 1856 ds' @ Fm.decls fm @ ds))
adamc@179 1857 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 1858 in
adamc@25 1859 rev ds
adamc@25 1860 end
adamc@25 1861
adamc@25 1862 end