annotate src/monoize.sml @ 254:f8d9395575ec

Monoized binary operators
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 Aug 2008 14:33:22 -0400
parents 7f6620853c36
children 69d337f186eb
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@196 67 fun monoType env =
adamc@25 68 let
adamc@196 69 fun mt env dtmap (all as (c, loc)) =
adamc@196 70 let
adamc@196 71 fun poly () =
adamc@196 72 (E.errorAt loc "Unsupported type constructor";
adamc@196 73 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 74 dummyTyp)
adamc@196 75 in
adamc@196 76 case c of
adamc@196 77 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 78 | L.TCFun _ => poly ()
adamc@196 79 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@196 80 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
adamc@196 81 | L.TRecord _ => poly ()
adamc@196 82
adamc@196 83 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 84 (L'.TFfi ("Basis", "string"), loc)
adamc@196 85 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 86 (L'.TFfi ("Basis", "string"), loc)
adamc@196 87
adamc@251 88 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@252 89 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
adamc@252 90 | L.CApp ((L.CFfi ("Basis", "sql_table"), _), _) =>
adamc@252 91 (L'.TFfi ("Basis", "string"), loc)
adamc@252 92 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
adamc@252 93 (L'.TFfi ("Basis", "string"), loc)
adamc@252 94 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
adamc@252 95 (L'.TFfi ("Basis", "string"), loc)
adamc@252 96 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
adamc@252 97 (L'.TFfi ("Basis", "string"), loc)
adamc@252 98
adamc@252 99 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
adamc@252 100 (L'.TRecord [], loc)
adamc@252 101 | L.CFfi ("Basis", "sql_relop") =>
adamc@252 102 (L'.TFfi ("Basis", "string"), loc)
adamc@252 103 | L.CFfi ("Basis", "sql_direction") =>
adamc@252 104 (L'.TFfi ("Basis", "string"), loc)
adamc@252 105 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
adamc@252 106 (L'.TFfi ("Basis", "string"), loc)
adamc@252 107 | L.CFfi ("Basis", "sql_limit") =>
adamc@252 108 (L'.TFfi ("Basis", "string"), loc)
adamc@252 109 | L.CFfi ("Basis", "sql_offset") =>
adamc@252 110 (L'.TFfi ("Basis", "string"), loc)
adamc@252 111
adamc@252 112 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
adamc@252 113 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 114 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
adamc@252 115 (L'.TFfi ("Basis", "string"), loc)
adamc@252 116 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
adamc@252 117 (L'.TFfi ("Basis", "string"), loc)
adamc@252 118 | L.CFfi ("Basis", "sql_comparison") =>
adamc@252 119 (L'.TFfi ("Basis", "string"), loc)
adamc@252 120 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
adamc@252 121 (L'.TFfi ("Basis", "string"), loc)
adamc@252 122 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
adamc@252 123 (L'.TRecord [], loc)
adamc@252 124 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
adamc@252 125 (L'.TRecord [], loc)
adamc@251 126
adamc@196 127 | L.CRel _ => poly ()
adamc@196 128 | L.CNamed n =>
adamc@196 129 (case IM.find (dtmap, n) of
adamc@196 130 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 131 | NONE =>
adamc@196 132 let
adamc@196 133 val r = ref (L'.Default, [])
adamc@196 134 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 135
adamc@196 136 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 137
adamc@196 138 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 139 in
adamc@196 140 case xs of
adamc@198 141 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 142 (L'.TDatatype (n, r), loc))
adamc@196 143 | _ => poly ()
adamc@196 144 end)
adamc@196 145 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 146 | L.CApp _ => poly ()
adamc@196 147 | L.CAbs _ => poly ()
adamc@196 148
adamc@196 149 | L.CName _ => poly ()
adamc@196 150
adamc@196 151 | L.CRecord _ => poly ()
adamc@196 152 | L.CConcat _ => poly ()
adamc@196 153 | L.CFold _ => poly ()
adamc@196 154 | L.CUnit => poly ()
adamc@214 155
adamc@214 156 | L.CTuple _ => poly ()
adamc@214 157 | L.CProj _ => poly ()
adamc@196 158 end
adamc@25 159 in
adamc@196 160 mt env IM.empty
adamc@25 161 end
adamc@25 162
adamc@25 163 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 164
adamc@179 165 structure IM = IntBinaryMap
adamc@179 166
adamc@179 167 datatype foo_kind =
adamc@179 168 Attr
adamc@179 169 | Url
adamc@179 170
adamc@179 171 fun fk2s fk =
adamc@179 172 case fk of
adamc@179 173 Attr => "attr"
adamc@179 174 | Url => "url"
adamc@179 175
adamc@179 176 structure Fm :> sig
adamc@179 177 type t
adamc@179 178
adamc@179 179 val empty : int -> t
adamc@179 180
adamc@179 181 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@179 182 val enter : t -> t
adamc@179 183 val decls : t -> L'.decl list
adamc@179 184 end = struct
adamc@179 185
adamc@179 186 structure M = BinaryMapFn(struct
adamc@179 187 type ord_key = foo_kind
adamc@179 188 fun compare x =
adamc@179 189 case x of
adamc@179 190 (Attr, Attr) => EQUAL
adamc@179 191 | (Attr, _) => LESS
adamc@179 192 | (_, Attr) => GREATER
adamc@179 193
adamc@179 194 | (Url, Url) => EQUAL
adamc@179 195 end)
adamc@179 196
adamc@179 197 type t = {
adamc@179 198 count : int,
adamc@179 199 map : int IM.map M.map,
adamc@179 200 decls : L'.decl list
adamc@179 201 }
adamc@179 202
adamc@179 203 fun empty count = {
adamc@179 204 count = count,
adamc@179 205 map = M.empty,
adamc@179 206 decls = []
adamc@179 207 }
adamc@179 208
adamc@179 209 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
adamc@179 210 fun decls ({decls, ...} : t) = decls
adamc@179 211
adamc@179 212 fun lookup (t as {count, map, decls}) k n thunk =
adamc@120 213 let
adamc@179 214 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 215 in
adamc@179 216 case IM.find (im, n) of
adamc@179 217 NONE =>
adamc@179 218 let
adamc@179 219 val n' = count
adamc@179 220 val (d, {count, map, decls}) = thunk count {count = count + 1,
adamc@179 221 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@179 222 decls = decls}
adamc@179 223 in
adamc@179 224 ({count = count,
adamc@179 225 map = map,
adamc@179 226 decls = d :: decls}, n')
adamc@179 227 end
adamc@179 228 | SOME n' => (t, n')
adamc@179 229 end
adamc@179 230
adamc@179 231 end
adamc@185 232
adamc@185 233
adamc@185 234 fun capitalize s =
adamc@185 235 if s = "" then
adamc@185 236 s
adamc@185 237 else
adamc@185 238 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 239
adamc@179 240 fun fooifyExp fk env =
adamc@179 241 let
adamc@179 242 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 243 case #1 e of
adamc@120 244 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 245 let
adamc@120 246 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 247 in
adamc@183 248 ((L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
adamc@120 249 end
adamc@120 250 | L'.EClosure (fnam, args) =>
adamc@120 251 let
adamc@120 252 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 253 val ft = monoType env ft
adamc@111 254
adamc@179 255 fun attrify (args, ft, e, fm) =
adamc@120 256 case (args, ft) of
adamc@179 257 ([], _) => (e, fm)
adamc@120 258 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 259 let
adamc@179 260 val (arg', fm) = fooify fm (arg, t)
adamc@179 261 in
adamc@179 262 attrify (args, ft,
adamc@179 263 (L'.EStrcat (e,
adamc@179 264 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 265 arg'), loc)), loc),
adamc@179 266 fm)
adamc@179 267 end
adamc@120 268 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 269 (e, fm))
adamc@120 270 in
adamc@183 271 attrify (args, ft, (L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
adamc@120 272 end
adamc@120 273 | _ =>
adamc@120 274 case t of
adamc@185 275 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 276
adamc@179 277 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 278 | L'.TRecord ((x, t) :: xts) =>
adamc@200 279 let
adamc@200 280 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 281 in
adamc@200 282 foldl (fn ((x, t), (se, fm)) =>
adamc@200 283 let
adamc@200 284 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 285 in
adamc@200 286 ((L'.EStrcat (se,
adamc@200 287 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 288 se'), loc)), loc),
adamc@200 289 fm)
adamc@200 290 end) (se, fm) xts
adamc@200 291 end
adamc@111 292
adamc@196 293 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 294 let
adamc@179 295 fun makeDecl n fm =
adamc@179 296 let
adamc@193 297 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 298
adamc@179 299 val (branches, fm) =
adamc@179 300 ListUtil.foldlMap
adamc@179 301 (fn ((x, n, to), fm) =>
adamc@179 302 case to of
adamc@179 303 NONE =>
adamc@188 304 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 305 (L'.EPrim (Prim.String x), loc)),
adamc@179 306 fm)
adamc@179 307 | SOME t =>
adamc@179 308 let
adamc@182 309 val t = monoType env t
adamc@182 310 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 311 in
adamc@188 312 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 313 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 314 arg), loc)),
adamc@179 315 fm)
adamc@179 316 end)
adamc@179 317 fm xncs
adamc@179 318
adamc@179 319 val dom = tAll
adamc@179 320 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 321 in
adamc@179 322 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 323 n,
adamc@179 324 (L'.TFun (dom, ran), loc),
adamc@179 325 (L'.EAbs ("x",
adamc@179 326 dom,
adamc@179 327 ran,
adamc@179 328 (L'.ECase ((L'.ERel 0, loc),
adamc@179 329 branches,
adamc@182 330 {disc = dom,
adamc@182 331 result = ran}), loc)), loc),
adamc@179 332 "")], loc),
adamc@179 333 fm)
adamc@179 334 end
adamc@179 335
adamc@179 336 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 337 in
adamc@179 338 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 339 end
adamc@164 340
adamc@120 341 | _ => (E.errorAt loc "Don't know how to encode attribute type";
adamc@120 342 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 343 (dummyExp, fm))
adamc@120 344 in
adamc@120 345 fooify
adamc@120 346 end
adamc@120 347
adamc@179 348 val attrifyExp = fooifyExp Attr
adamc@179 349 val urlifyExp = fooifyExp Url
adamc@105 350
adamc@143 351 datatype 'a failable_search =
adamc@143 352 Found of 'a
adamc@143 353 | NotFound
adamc@143 354 | Error
adamc@143 355
adamc@153 356 structure St :> sig
adamc@153 357 type t
adamc@153 358
adamc@153 359 val empty : t
adamc@153 360
adamc@153 361 val radioGroup : t -> string option
adamc@153 362 val setRadioGroup : t * string -> t
adamc@153 363 end = struct
adamc@153 364
adamc@153 365 type t = {
adamc@153 366 radioGroup : string option
adamc@153 367 }
adamc@153 368
adamc@153 369 val empty = {radioGroup = NONE}
adamc@153 370
adamc@153 371 fun radioGroup (t : t) = #radioGroup t
adamc@153 372
adamc@153 373 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 374
adamc@153 375 end
adamc@153 376
adamc@186 377 fun monoPatCon env pc =
adamc@178 378 case pc of
adamc@178 379 L.PConVar n => L'.PConVar n
adamc@188 380 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 381 arg = Option.map (monoType env) arg}
adamc@178 382
adamc@193 383 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 384
adamc@193 385 fun monoPat env (all as (p, loc)) =
adamc@193 386 let
adamc@193 387 fun poly () =
adamc@193 388 (E.errorAt loc "Unsupported pattern";
adamc@193 389 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 390 dummyPat)
adamc@193 391 in
adamc@193 392 case p of
adamc@193 393 L.PWild => (L'.PWild, loc)
adamc@193 394 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 395 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 396 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@193 397 | L.PCon _ => poly ()
adamc@193 398 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 399 end
adamc@178 400
adamc@252 401 fun strcat loc es =
adamc@252 402 case es of
adamc@252 403 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 404 | [e] => e
adamc@252 405 | _ =>
adamc@252 406 let
adamc@252 407 val e2 = List.last es
adamc@252 408 val es = List.take (es, length es - 1)
adamc@252 409 val e1 = List.last es
adamc@252 410 val es = List.take (es, length es - 1)
adamc@252 411 in
adamc@252 412 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
adamc@252 413 (L'.EStrcat (e1, e2), loc) es
adamc@252 414 end
adamc@252 415
adamc@252 416 fun strcatComma loc es =
adamc@252 417 case es of
adamc@252 418 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 419 | [e] => e
adamc@252 420 | _ =>
adamc@252 421 let
adamc@252 422 val e1 = List.last es
adamc@252 423 val es = List.take (es, length es - 1)
adamc@252 424 in
adamc@252 425 foldr (fn (e, e') =>
adamc@252 426 case e of
adamc@252 427 (L'.EPrim (Prim.String ""), _) => e'
adamc@252 428 | _ =>
adamc@252 429 (L'.EStrcat (e,
adamc@252 430 (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc))
adamc@252 431 e1 es
adamc@252 432 end
adamc@252 433
adamc@252 434 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
adamc@252 435
adamc@179 436 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 437 let
adamc@25 438 fun poly () =
adamc@25 439 (E.errorAt loc "Unsupported expression";
adamc@25 440 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 441 (dummyExp, fm))
adamc@25 442 in
adamc@25 443 case e of
adamc@179 444 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 445 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 446 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 447 | L.ECon (dk, pc, [], eo) =>
adamc@193 448 let
adamc@179 449 val (eo, fm) =
adamc@179 450 case eo of
adamc@179 451 NONE => (NONE, fm)
adamc@179 452 | SOME e =>
adamc@179 453 let
adamc@179 454 val (e, fm) = monoExp (env, st, fm) e
adamc@179 455 in
adamc@179 456 (SOME e, fm)
adamc@179 457 end
adamc@179 458 in
adamc@188 459 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 460 end
adamc@193 461 | L.ECon _ => poly ()
adamc@94 462
adamc@251 463 | L.ECApp ((L.EFfi ("Basis", "return"), _), t) =>
adamc@252 464 let
adamc@252 465 val t = monoType env t
adamc@252 466 in
adamc@252 467 ((L'.EAbs ("x", t,
adamc@252 468 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@252 469 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@252 470 (L'.ERel 1, loc)), loc)), loc), fm)
adamc@252 471 end
adamc@251 472 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), t1), _), t2) =>
adamc@251 473 let
adamc@251 474 val t1 = monoType env t1
adamc@251 475 val t2 = monoType env t2
adamc@251 476 val un = (L'.TRecord [], loc)
adamc@252 477 val mt1 = (L'.TFun (un, t1), loc)
adamc@252 478 val mt2 = (L'.TFun (un, t2), loc)
adamc@251 479 in
adamc@252 480 ((L'.EAbs ("m1", mt1, (L'.TFun (mt1, (L'.TFun (mt2, (L'.TFun (un, un), loc)), loc)), loc),
adamc@252 481 (L'.EAbs ("m2", mt2, (L'.TFun (un, un), loc),
adamc@252 482 (L'.EAbs ("_", un, un,
adamc@252 483 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
adamc@252 484 (L'.ERecord [], loc)), loc),
adamc@252 485 (L'.EApp (
adamc@252 486 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
adamc@252 487 (L'.ERecord [], loc)),
adamc@252 488 loc)), loc)), loc)), loc)), loc),
adamc@251 489 fm)
adamc@251 490 end
adamc@251 491
adamc@252 492 | L.ECApp (
adamc@252 493 (L.ECApp (
adamc@252 494 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 495 exps), _),
adamc@252 496 state) =>
adamc@252 497 (case monoType env (L.TRecord exps, loc) of
adamc@252 498 (L'.TRecord exps, _) =>
adamc@252 499 let
adamc@252 500 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 501 (case monoType env (L.TRecord xts, loc) of
adamc@252 502 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 503 | _ => NONE)
adamc@252 504 | _ => NONE) tables
adamc@252 505 in
adamc@252 506 if List.exists (fn x => x = NONE) tables then
adamc@252 507 poly ()
adamc@252 508 else
adamc@252 509 let
adamc@252 510 val tables = List.mapPartial (fn x => x) tables
adamc@252 511 val state = monoType env state
adamc@252 512 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 513 val un = (L'.TRecord [], loc)
adamc@252 514
adamc@252 515 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 516 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 517 (L'.TFun (state,
adamc@252 518 (L'.TFun (un, state), loc)),
adamc@252 519 loc)), loc)
adamc@252 520
adamc@252 521 val body' = (L'.EAbs ("r", (L'.TRecord rt, loc),
adamc@252 522 (L'.TFun (state, state), loc),
adamc@252 523 (L'.EAbs ("acc", state, state,
adamc@252 524 (L'.EApp (
adamc@252 525 (L'.EApp (
adamc@252 526 (L'.EApp ((L'.ERel 4, loc),
adamc@252 527 (L'.ERel 1, loc)), loc),
adamc@252 528 (L'.ERel 0, loc)), loc),
adamc@252 529 (L'.ERecord [], loc)), loc)), loc)), loc)
adamc@252 530
adamc@252 531 val body = (L'.EQuery {exps = exps,
adamc@252 532 tables = tables,
adamc@252 533 state = state,
adamc@252 534 query = (L'.ERel 3, loc),
adamc@252 535 body = body',
adamc@252 536 initial = (L'.ERel 1, loc)},
adamc@252 537 loc)
adamc@252 538 in
adamc@252 539 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 540 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 541 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 542 (L'.EAbs ("_", un, state,
adamc@252 543 body), loc)), loc)), loc)), loc), fm)
adamc@252 544 end
adamc@252 545 end
adamc@252 546 | _ => poly ())
adamc@252 547
adamc@252 548 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 549 let
adamc@252 550 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 551 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 552 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 553 in
adamc@252 554 ((L'.EAbs ("r",
adamc@252 555 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 556 s,
adamc@252 557 strcat loc [gf "Rows",
adamc@252 558 gf "OrderBy",
adamc@252 559 gf "Limit",
adamc@252 560 gf "Offset"]), loc), fm)
adamc@252 561 end
adamc@252 562
adamc@252 563 | L.ECApp (
adamc@252 564 (L.ECApp (
adamc@252 565 (L.ECApp (
adamc@252 566 (L.ECApp (
adamc@252 567 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 568 (L.CRecord (_, tables), _)), _),
adamc@252 569 (L.CRecord (_, grouped), _)), _),
adamc@252 570 (L.CRecord (_, stables), _)), _),
adamc@252 571 sexps) =>
adamc@252 572 let
adamc@252 573 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 574 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 575 val un = (L'.TRecord [], loc)
adamc@252 576 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 577
adamc@252 578 fun doTables tables =
adamc@252 579 let
adamc@252 580 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 581 (case monoType env (L.TRecord xts, loc) of
adamc@252 582 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 583 | _ => NONE)
adamc@252 584 | _ => NONE) tables
adamc@252 585 in
adamc@252 586 if List.exists (fn x => x = NONE) tables then
adamc@252 587 NONE
adamc@252 588 else
adamc@252 589 SOME (List.mapPartial (fn x => x) tables)
adamc@252 590 end
adamc@252 591 in
adamc@252 592 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 593 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@252 594 ((L'.EAbs ("r",
adamc@252 595 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
adamc@252 596 ("Where", s),
adamc@252 597 ("GroupBy", un),
adamc@252 598 ("Having", s),
adamc@252 599 ("SelectFields", un),
adamc@252 600 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@252 601 loc),
adamc@252 602 s,
adamc@252 603 strcat loc [sc "SELECT ",
adamc@252 604 strcatR loc (gf "SelectExps") sexps,
adamc@252 605 case sexps of
adamc@252 606 [] => sc ""
adamc@252 607 | _ => sc ", ",
adamc@252 608 strcatComma loc (map (fn (x, xts) =>
adamc@252 609 strcatComma loc
adamc@252 610 (map (fn (x', _) =>
adamc@252 611 sc (x ^ "." ^ x'))
adamc@252 612 xts)) stables),
adamc@252 613 sc " FROM ",
adamc@252 614 strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
adamc@253 615 sc (" AS " ^ x)]) tables),
adamc@253 616 sc " WHERE ",
adamc@253 617 gf "Where"
adamc@252 618 ]), loc),
adamc@252 619 fm)
adamc@252 620 | _ => poly ()
adamc@252 621 end
adamc@252 622
adamc@252 623 | L.ECApp (
adamc@252 624 (L.ECApp (
adamc@252 625 (L.ECApp (
adamc@252 626 (L.ECApp (
adamc@252 627 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 628 _), _),
adamc@252 629 _), _),
adamc@252 630 _), _),
adamc@252 631 t) =>
adamc@252 632 let
adamc@252 633 val t = monoType env t
adamc@252 634 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 635 in
adamc@252 636 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 637 (L'.ERel 0, loc)), loc), fm)
adamc@252 638 end
adamc@252 639
adamc@253 640 | L.EFfi ("Basis", "sql_int") =>
adamc@253 641 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 642 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 643 fm)
adamc@253 644 | L.EFfi ("Basis", "sql_float") =>
adamc@253 645 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 646 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 647 fm)
adamc@253 648 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 649 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 650 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 651 fm)
adamc@253 652 | L.EFfi ("Basis", "sql_string") =>
adamc@253 653 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 654 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 655 fm)
adamc@253 656
adamc@252 657 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 658 ((L'.ERecord [], loc), fm)
adamc@252 659 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 660 ((L'.ERecord [], loc), fm)
adamc@252 661
adamc@252 662 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 663 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@252 664
adamc@252 665 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 666 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@252 667 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 668 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@253 669
adamc@253 670 | L.EFfi ("Basis", "sql_eq") =>
adamc@253 671 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@253 672 | L.EFfi ("Basis", "sql_ne") =>
adamc@253 673 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@253 674 | L.EFfi ("Basis", "sql_lt") =>
adamc@253 675 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@253 676 | L.EFfi ("Basis", "sql_le") =>
adamc@253 677 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@253 678 | L.EFfi ("Basis", "sql_gt") =>
adamc@253 679 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@253 680 | L.EFfi ("Basis", "sql_ge") =>
adamc@253 681 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 682
adamc@253 683 | L.ECApp (
adamc@253 684 (L.ECApp (
adamc@253 685 (L.ECApp (
adamc@253 686 (L.ECApp (
adamc@254 687 (L.ECApp (
adamc@254 688 (L.ECApp (
adamc@254 689 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 690 _), _),
adamc@254 691 _), _),
adamc@254 692 _), _),
adamc@254 693 _), _),
adamc@254 694 _), _),
adamc@254 695 _) =>
adamc@254 696 let
adamc@254 697 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 698 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 699 in
adamc@254 700 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 701 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 702 (L'.EAbs ("e2", s, s,
adamc@254 703 strcat loc [sc "(",
adamc@254 704 (L'.ERel 1, loc),
adamc@254 705 sc " ",
adamc@254 706 (L'.ERel 2, loc),
adamc@254 707 sc " ",
adamc@254 708 (L'.ERel 0, loc),
adamc@254 709 sc ")"]), loc)), loc)), loc),
adamc@254 710 fm)
adamc@254 711 end
adamc@254 712 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 713 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 714
adamc@254 715 | L.ECApp (
adamc@254 716 (L.ECApp (
adamc@254 717 (L.ECApp (
adamc@254 718 (L.ECApp (
adamc@253 719 (L.EFfi ("Basis", "sql_comparison"), _),
adamc@253 720 _), _),
adamc@253 721 _), _),
adamc@253 722 _), _),
adamc@253 723 _) =>
adamc@253 724 let
adamc@253 725 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@253 726 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@253 727 in
adamc@253 728 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@253 729 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@253 730 (L'.EAbs ("e2", s, s,
adamc@254 731 strcat loc [sc "(",
adamc@254 732 (L'.ERel 1, loc),
adamc@253 733 sc " ",
adamc@253 734 (L'.ERel 2, loc),
adamc@253 735 sc " ",
adamc@254 736 (L'.ERel 0, loc),
adamc@254 737 sc ")"]), loc)), loc)), loc),
adamc@253 738 fm)
adamc@253 739 end
adamc@253 740
adamc@253 741 | L.ECApp (
adamc@253 742 (L.ECApp (
adamc@253 743 (L.ECApp (
adamc@253 744 (L.ECApp (
adamc@253 745 (L.ECApp (
adamc@253 746 (L.ECApp (
adamc@253 747 (L.ECApp (
adamc@253 748 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 749 _), _),
adamc@253 750 _), _),
adamc@253 751 _), _),
adamc@253 752 _), _),
adamc@253 753 _), _),
adamc@253 754 (L.CName tab, _)), _),
adamc@253 755 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ "." ^ field)), loc), fm)
adamc@252 756
adamc@139 757 | L.EApp (
adamc@139 758 (L.ECApp (
adamc@141 759 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 760 _), _),
adamc@179 761 se) =>
adamc@179 762 let
adamc@179 763 val (se, fm) = monoExp (env, st, fm) se
adamc@179 764 in
adamc@179 765 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 766 end
adamc@179 767
adamc@95 768 | L.EApp (
adamc@95 769 (L.EApp (
adamc@95 770 (L.ECApp (
adamc@95 771 (L.ECApp (
adamc@95 772 (L.ECApp (
adamc@139 773 (L.ECApp (
adamc@140 774 (L.EFfi ("Basis", "join"),
adamc@139 775 _), _), _),
adamc@139 776 _), _),
adamc@95 777 _), _),
adamc@95 778 _), _),
adamc@95 779 xml1), _),
adamc@179 780 xml2) =>
adamc@179 781 let
adamc@179 782 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 783 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 784 in
adamc@179 785 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 786 end
adamc@95 787
adamc@95 788 | L.EApp (
adamc@95 789 (L.EApp (
adamc@104 790 (L.EApp (
adamc@95 791 (L.ECApp (
adamc@104 792 (L.ECApp (
adamc@104 793 (L.ECApp (
adamc@104 794 (L.ECApp (
adamc@139 795 (L.ECApp (
adamc@139 796 (L.ECApp (
adamc@139 797 (L.ECApp (
adamc@139 798 (L.ECApp (
adamc@139 799 (L.EFfi ("Basis", "tag"),
adamc@139 800 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 801 attrs), _),
adamc@95 802 tag), _),
adamc@95 803 xml) =>
adamc@95 804 let
adamc@140 805 fun getTag' (e, _) =
adamc@140 806 case e of
adamc@143 807 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 808 | L.ECApp (e, t) => let
adamc@143 809 val (tag, ts) = getTag' e
adamc@143 810 in
adamc@143 811 (tag, ts @ [t])
adamc@143 812 end
adamc@140 813 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 814 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 815 ("", []))
adamc@140 816
adamc@95 817 fun getTag (e, _) =
adamc@95 818 case e of
adamc@143 819 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 820 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 821 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 822 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 823 ("", []))
adamc@95 824
adamc@143 825 val (tag, targs) = getTag tag
adamc@95 826
adamc@179 827 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 828
adamc@143 829 fun tagStart tag =
adamc@104 830 case #1 attrs of
adamc@104 831 L'.ERecord xes =>
adamc@104 832 let
adamc@104 833 fun lowercaseFirst "" = ""
adamc@143 834 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 835 ^ String.extract (s, 1, NONE)
adamc@104 836
adamc@104 837 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 838 in
adamc@179 839 foldl (fn ((x, e, t), (s, fm)) =>
adamc@104 840 let
adamc@104 841 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@120 842
adamc@120 843 val fooify =
adamc@120 844 case x of
adamc@185 845 "Href" => urlifyExp
adamc@185 846 | "Link" => urlifyExp
adamc@143 847 | "Action" => urlifyExp
adamc@120 848 | _ => attrifyExp
adamc@179 849
adamc@179 850 val (e, fm) = fooify env fm (e, t)
adamc@104 851 in
adamc@179 852 ((L'.EStrcat (s,
adamc@179 853 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@179 854 (L'.EStrcat (e,
adamc@179 855 (L'.EPrim (Prim.String "\""),
adamc@179 856 loc)),
adamc@179 857 loc)),
adamc@179 858 loc)), loc),
adamc@179 859 fm)
adamc@104 860 end)
adamc@179 861 (s, fm) xes
adamc@104 862 end
adamc@143 863 | _ => raise Fail "Non-record attributes!"
adamc@104 864
adamc@143 865 fun input typ =
adamc@143 866 case targs of
adamc@155 867 [_, (L.CName name, _)] =>
adamc@179 868 let
adamc@179 869 val (ts, fm) = tagStart "input"
adamc@179 870 in
adamc@179 871 ((L'.EStrcat (ts,
adamc@179 872 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 873 loc)), loc), fm)
adamc@179 874 end
adamc@143 875 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 876 raise Fail "No name passed to input tag")
adamc@104 877
adamc@152 878 fun normal (tag, extra) =
adamc@143 879 let
adamc@179 880 val (tagStart, fm) = tagStart tag
adamc@152 881 val tagStart = case extra of
adamc@152 882 NONE => tagStart
adamc@152 883 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 884
adamc@143 885 fun normal () =
adamc@179 886 let
adamc@179 887 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 888 in
adamc@179 889 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 890 (L'.EStrcat (xml,
adamc@179 891 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 892 loc)), loc)),
adamc@179 893 loc),
adamc@179 894 fm)
adamc@179 895 end
adamc@143 896 in
adamc@143 897 case xml of
adamc@143 898 (L.EApp ((L.ECApp (
adamc@143 899 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 900 _), _),
adamc@143 901 _), _),
adamc@143 902 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 903 if CharVector.all Char.isSpace s then
adamc@179 904 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 905 else
adamc@143 906 normal ()
adamc@143 907 | _ => normal ()
adamc@143 908 end
adamc@152 909 in
adamc@152 910 case tag of
adamc@179 911 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
adamc@152 912
adamc@152 913 | "textbox" =>
adamc@152 914 (case targs of
adamc@152 915 [_, (L.CName name, _)] =>
adamc@179 916 let
adamc@179 917 val (ts, fm) = tagStart "input"
adamc@179 918 in
adamc@179 919 ((L'.EStrcat (ts,
adamc@179 920 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 921 loc)), loc), fm)
adamc@179 922 end
adamc@152 923 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 924 raise Fail "No name passed to textarea tag"))
adamc@155 925 | "password" => input "password"
adamc@152 926 | "ltextarea" =>
adamc@152 927 (case targs of
adamc@152 928 [_, (L.CName name, _)] =>
adamc@179 929 let
adamc@179 930 val (ts, fm) = tagStart "textarea"
adamc@179 931 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 932 in
adamc@179 933 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 934 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 935 (L'.EStrcat (xml,
adamc@179 936 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 937 loc)), loc)),
adamc@179 938 loc), fm)
adamc@179 939 end
adamc@152 940 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 941 raise Fail "No name passed to ltextarea tag"))
adamc@153 942
adamc@190 943 | "checkbox" => input "checkbox"
adamc@190 944
adamc@153 945 | "radio" =>
adamc@153 946 (case targs of
adamc@153 947 [_, (L.CName name, _)] =>
adamc@179 948 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 949 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 950 raise Fail "No name passed to radio tag"))
adamc@153 951 | "radioOption" =>
adamc@153 952 (case St.radioGroup st of
adamc@153 953 NONE => raise Fail "No name for radioGroup"
adamc@153 954 | SOME name =>
adamc@153 955 normal ("input",
adamc@153 956 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 957
adamc@154 958 | "lselect" =>
adamc@154 959 (case targs of
adamc@154 960 [_, (L.CName name, _)] =>
adamc@179 961 let
adamc@179 962 val (ts, fm) = tagStart "select"
adamc@179 963 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 964 in
adamc@179 965 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 966 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 967 (L'.EStrcat (xml,
adamc@179 968 (L'.EPrim (Prim.String "</select>"),
adamc@179 969 loc)), loc)),
adamc@179 970 loc),
adamc@179 971 fm)
adamc@179 972 end
adamc@154 973 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 974 raise Fail "No name passed to lselect tag"))
adamc@154 975
adamc@154 976 | "loption" => normal ("option", NONE)
adamc@154 977
adamc@152 978 | _ => normal (tag, NONE)
adamc@95 979 end
adamc@94 980
adamc@141 981 | L.EApp ((L.ECApp (
adamc@141 982 (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _),
adamc@141 983 _), _),
adamc@141 984 xml) =>
adamc@143 985 let
adamc@143 986 fun findSubmit (e, _) =
adamc@143 987 case e of
adamc@143 988 L.EApp (
adamc@143 989 (L.EApp (
adamc@143 990 (L.ECApp (
adamc@143 991 (L.ECApp (
adamc@143 992 (L.ECApp (
adamc@143 993 (L.ECApp (
adamc@143 994 (L.EFfi ("Basis", "join"),
adamc@143 995 _), _), _),
adamc@143 996 _), _),
adamc@143 997 _), _),
adamc@143 998 _), _),
adamc@143 999 xml1), _),
adamc@143 1000 xml2) => (case findSubmit xml1 of
adamc@143 1001 Error => Error
adamc@143 1002 | NotFound => findSubmit xml2
adamc@143 1003 | Found e =>
adamc@143 1004 case findSubmit xml2 of
adamc@143 1005 NotFound => Found e
adamc@143 1006 | _ => Error)
adamc@143 1007 | L.EApp (
adamc@143 1008 (L.EApp (
adamc@143 1009 (L.EApp (
adamc@143 1010 (L.ECApp (
adamc@143 1011 (L.ECApp (
adamc@143 1012 (L.ECApp (
adamc@143 1013 (L.ECApp (
adamc@143 1014 (L.ECApp (
adamc@143 1015 (L.ECApp (
adamc@143 1016 (L.ECApp (
adamc@143 1017 (L.ECApp (
adamc@143 1018 (L.EFfi ("Basis", "tag"),
adamc@143 1019 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 1020 attrs), _),
adamc@143 1021 _), _),
adamc@143 1022 xml) =>
adamc@143 1023 (case #1 attrs of
adamc@143 1024 L.ERecord xes =>
adamc@143 1025 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 1026 | _ => NONE) xes of
adamc@143 1027 NONE => findSubmit xml
adamc@143 1028 | SOME et =>
adamc@143 1029 case findSubmit xml of
adamc@143 1030 NotFound => Found et
adamc@143 1031 | _ => Error)
adamc@143 1032 | _ => findSubmit xml)
adamc@143 1033 | _ => NotFound
adamc@143 1034
adamc@143 1035 val (action, actionT) = case findSubmit xml of
adamc@143 1036 NotFound => raise Fail "No submit found"
adamc@143 1037 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 1038 | Found et => et
adamc@143 1039
adamc@143 1040 val actionT = monoType env actionT
adamc@179 1041 val (action, fm) = monoExp (env, st, fm) action
adamc@179 1042 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 1043 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 1044 in
adamc@179 1045 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 1046 (L'.EStrcat (action,
adamc@179 1047 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 1048 (L'.EStrcat (xml,
adamc@179 1049 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 1050 fm)
adamc@143 1051 end
adamc@141 1052
adamc@148 1053 | L.EApp ((L.ECApp (
adamc@148 1054 (L.ECApp (
adamc@148 1055 (L.ECApp (
adamc@148 1056 (L.ECApp (
adamc@148 1057 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 1058 _), _),
adamc@148 1059 _), _),
adamc@148 1060 _), _),
adamc@179 1061 xml) => monoExp (env, st, fm) xml
adamc@148 1062
adamc@179 1063 | L.EApp (e1, e2) =>
adamc@179 1064 let
adamc@179 1065 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 1066 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 1067 in
adamc@179 1068 ((L'.EApp (e1, e2), loc), fm)
adamc@179 1069 end
adamc@26 1070 | L.EAbs (x, dom, ran, e) =>
adamc@179 1071 let
adamc@179 1072 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 1073 in
adamc@179 1074 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 1075 end
adamc@25 1076 | L.ECApp _ => poly ()
adamc@25 1077 | L.ECAbs _ => poly ()
adamc@25 1078
adamc@252 1079 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 1080 | L.EFfiApp (m, x, es) =>
adamc@252 1081 let
adamc@252 1082 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 1083 in
adamc@252 1084 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 1085 end
adamc@252 1086
adamc@179 1087 | L.ERecord xes =>
adamc@179 1088 let
adamc@179 1089 val (xes, fm) = ListUtil.foldlMap
adamc@179 1090 (fn ((x, e, t), fm) =>
adamc@179 1091 let
adamc@179 1092 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1093 in
adamc@179 1094 ((monoName env x,
adamc@179 1095 e,
adamc@179 1096 monoType env t), fm)
adamc@179 1097 end) fm xes
adamc@179 1098 in
adamc@179 1099 ((L'.ERecord xes, loc), fm)
adamc@179 1100 end
adamc@179 1101 | L.EField (e, x, _) =>
adamc@179 1102 let
adamc@179 1103 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1104 in
adamc@179 1105 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 1106 end
adamc@149 1107 | L.ECut _ => poly ()
adamc@73 1108 | L.EFold _ => poly ()
adamc@177 1109
adamc@182 1110 | L.ECase (e, pes, {disc, result}) =>
adamc@179 1111 let
adamc@179 1112 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1113 val (pes, fm) = ListUtil.foldlMap
adamc@179 1114 (fn ((p, e), fm) =>
adamc@179 1115 let
adamc@179 1116 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1117 in
adamc@182 1118 ((monoPat env p, e), fm)
adamc@179 1119 end) fm pes
adamc@179 1120 in
adamc@182 1121 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 1122 end
adamc@177 1123
adamc@179 1124 | L.EWrite e =>
adamc@179 1125 let
adamc@179 1126 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1127 in
adamc@252 1128 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 1129 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 1130 end
adamc@110 1131
adamc@179 1132 | L.EClosure (n, es) =>
adamc@179 1133 let
adamc@179 1134 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 1135 monoExp (env, st, fm) e)
adamc@179 1136 fm es
adamc@179 1137 in
adamc@179 1138 ((L'.EClosure (n, es), loc), fm)
adamc@179 1139 end
adamc@25 1140 end
adamc@25 1141
adamc@179 1142 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 1143 let
adamc@25 1144 fun poly () =
adamc@25 1145 (E.errorAt loc "Unsupported declaration";
adamc@25 1146 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 1147 NONE)
adamc@25 1148 in
adamc@25 1149 case d of
adamc@25 1150 L.DCon _ => NONE
adamc@193 1151 | L.DDatatype (x, n, [], xncs) =>
adamc@193 1152 let
adamc@196 1153 val env' = Env.declBinds env all
adamc@196 1154 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 1155 in
adamc@196 1156 SOME (env', fm, d)
adamc@193 1157 end
adamc@193 1158 | L.DDatatype _ => poly ()
adamc@179 1159 | L.DVal (x, n, t, e, s) =>
adamc@179 1160 let
adamc@179 1161 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 1162 in
adamc@179 1163 SOME (Env.pushENamed env x n t NONE s,
adamc@179 1164 fm,
adamc@179 1165 (L'.DVal (x, n, monoType env t, e, s), loc))
adamc@179 1166 end
adamc@128 1167 | L.DValRec vis =>
adamc@128 1168 let
adamc@128 1169 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 1170
adamc@179 1171 val (vis, fm) = ListUtil.foldlMap
adamc@179 1172 (fn ((x, n, t, e, s), fm) =>
adamc@179 1173 let
adamc@179 1174 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 1175 in
adamc@179 1176 ((x, n, monoType env t, e, s), fm)
adamc@179 1177 end)
adamc@179 1178 fm vis
adamc@128 1179 in
adamc@128 1180 SOME (env,
adamc@179 1181 fm,
adamc@179 1182 (L'.DValRec vis, loc))
adamc@128 1183 end
adamc@144 1184 | L.DExport (ek, n) =>
adamc@115 1185 let
adamc@120 1186 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 1187
adamc@120 1188 fun unwind (t, _) =
adamc@120 1189 case t of
adamc@120 1190 L.TFun (dom, ran) => dom :: unwind ran
adamc@120 1191 | _ => []
adamc@120 1192
adamc@120 1193 val ts = map (monoType env) (unwind t)
adamc@115 1194 in
adamc@179 1195 SOME (env, fm, (L'.DExport (ek, s, n, ts), loc))
adamc@115 1196 end
adamc@251 1197 | L.DTable (x, n, _, s) =>
adamc@251 1198 let
adamc@251 1199 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 1200 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@251 1201 val e = (L'.EPrim (Prim.String s), loc)
adamc@251 1202 in
adamc@251 1203 SOME (Env.pushENamed env x n t NONE s,
adamc@251 1204 fm,
adamc@251 1205 (L'.DVal (x, n, t', e, s), loc))
adamc@251 1206 end
adamc@25 1207 end
adamc@25 1208
adamc@25 1209 fun monoize env ds =
adamc@25 1210 let
adamc@179 1211 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 1212 case monoDecl (env, fm) d of
adamc@179 1213 NONE => (env, fm, ds)
adamc@179 1214 | SOME (env, fm, d) =>
adamc@179 1215 (env,
adamc@179 1216 Fm.enter fm,
adamc@179 1217 d :: Fm.decls fm @ ds))
adamc@179 1218 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 1219 in
adamc@25 1220 rev ds
adamc@25 1221 end
adamc@25 1222
adamc@25 1223 end