annotate src/monoize.sml @ 333:c655eddc3795

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