annotate src/monoize.sml @ 388:2e93d18daf44

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