annotate src/monoize.sml @ 385:1195f6e4d208

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