annotate src/monoize.sml @ 400:e756d3a47726

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