annotate src/monoize.sml @ 394:9f6fe6f32961

ord_float and ord_bool
author Adam Chlipala <adamc@hcoop.net>
date Tue, 21 Oct 2008 11:19:17 -0400
parents a813476230e3
children 9f67aac13cf7
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@389 764
adamc@286 765 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
adamc@286 766 let
adamc@286 767 val t = monoType env t
adamc@286 768 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 769 in
adamc@286 770 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@286 771 (L'.ERel 0, loc)), loc), fm)
adamc@286 772 end
adamc@286 773 | L.EFfi ("Basis", "show_int") =>
adamc@286 774 ((L'.EFfi ("Basis", "intToString"), loc), fm)
adamc@286 775 | L.EFfi ("Basis", "show_float") =>
adamc@286 776 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
adamc@286 777 | L.EFfi ("Basis", "show_string") =>
adamc@286 778 let
adamc@286 779 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 780 in
adamc@286 781 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@286 782 end
adamc@286 783 | L.EFfi ("Basis", "show_bool") =>
adamc@286 784 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
adamc@286 785
adamc@290 786 | L.ECApp ((L.EFfi ("Basis", "read"), _), t) =>
adamc@290 787 let
adamc@290 788 val t = monoType env t
adamc@290 789 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 790 in
adamc@292 791 ((L'.EAbs ("f", readType (t, loc), readType' (t, loc),
adamc@292 792 (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm)
adamc@292 793 end
adamc@292 794 | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) =>
adamc@292 795 let
adamc@292 796 val t = monoType env t
adamc@292 797 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@292 798 in
adamc@292 799 ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc),
adamc@292 800 (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm)
adamc@290 801 end
adamc@290 802 | L.EFfi ("Basis", "read_int") =>
adamc@292 803 let
adamc@292 804 val t = (L'.TFfi ("Basis", "int"), loc)
adamc@292 805 in
adamc@292 806 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)),
adamc@292 807 ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))],
adamc@292 808 loc),
adamc@292 809 fm)
adamc@292 810 end
adamc@290 811 | L.EFfi ("Basis", "read_float") =>
adamc@292 812 let
adamc@292 813 val t = (L'.TFfi ("Basis", "float"), loc)
adamc@292 814 in
adamc@292 815 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)),
adamc@292 816 ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))],
adamc@292 817 loc),
adamc@292 818 fm)
adamc@292 819 end
adamc@290 820 | L.EFfi ("Basis", "read_string") =>
adamc@290 821 let
adamc@290 822 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@290 823 in
adamc@292 824 ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc),
adamc@292 825 (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)),
adamc@292 826 ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc),
adamc@292 827 fm)
adamc@290 828 end
adamc@290 829 | L.EFfi ("Basis", "read_bool") =>
adamc@292 830 let
adamc@292 831 val t = (L'.TFfi ("Basis", "bool"), loc)
adamc@292 832 in
adamc@292 833 ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)),
adamc@292 834 ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))],
adamc@292 835 loc),
adamc@292 836 fm)
adamc@292 837 end
adamc@290 838
adamc@251 839 | L.ECApp ((L.EFfi ("Basis", "return"), _), t) =>
adamc@252 840 let
adamc@252 841 val t = monoType env t
adamc@252 842 in
adamc@252 843 ((L'.EAbs ("x", t,
adamc@252 844 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@252 845 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@252 846 (L'.ERel 1, loc)), loc)), loc), fm)
adamc@252 847 end
adamc@251 848 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), t1), _), t2) =>
adamc@251 849 let
adamc@251 850 val t1 = monoType env t1
adamc@251 851 val t2 = monoType env t2
adamc@251 852 val un = (L'.TRecord [], loc)
adamc@252 853 val mt1 = (L'.TFun (un, t1), loc)
adamc@252 854 val mt2 = (L'.TFun (un, t2), loc)
adamc@251 855 in
adamc@252 856 ((L'.EAbs ("m1", mt1, (L'.TFun (mt1, (L'.TFun (mt2, (L'.TFun (un, un), loc)), loc)), loc),
adamc@252 857 (L'.EAbs ("m2", mt2, (L'.TFun (un, un), loc),
adamc@252 858 (L'.EAbs ("_", un, un,
adamc@252 859 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
adamc@252 860 (L'.ERecord [], loc)), loc),
adamc@252 861 (L'.EApp (
adamc@252 862 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
adamc@252 863 (L'.ERecord [], loc)),
adamc@252 864 loc)), loc)), loc)), loc)), loc),
adamc@251 865 fm)
adamc@251 866 end
adamc@251 867
adamc@307 868 | L.EFfiApp ("Basis", "dml", [e]) =>
adamc@307 869 let
adamc@307 870 val (e, fm) = monoExp (env, st, fm) e
adamc@307 871 val un = (L'.TRecord [], loc)
adamc@307 872 in
adamc@307 873 ((L'.EAbs ("_", un, un,
adamc@307 874 (L'.EDml (liftExpInExp 0 e), loc)), loc),
adamc@307 875 fm)
adamc@307 876 end
adamc@308 877
adamc@307 878 | L.ECApp ((L.EFfi ("Basis", "insert"), _), fields) =>
adamc@307 879 (case monoType env (L.TRecord fields, loc) of
adamc@307 880 (L'.TRecord fields, _) =>
adamc@307 881 let
adamc@307 882 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@307 883 val fields = map (fn (x, _) => (x, s)) fields
adamc@307 884 val rt = (L'.TRecord fields, loc)
adamc@307 885 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@307 886 in
adamc@307 887 ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc),
adamc@307 888 (L'.EAbs ("fs", rt, s,
adamc@307 889 strcat loc [sc "INSERT INTO ",
adamc@307 890 (L'.ERel 1, loc),
adamc@307 891 sc " (",
adamc@311 892 strcatComma loc (map (fn (x, _) => sc ("uw_" ^ x)) fields),
adamc@307 893 sc ") VALUES (",
adamc@307 894 strcatComma loc (map (fn (x, _) =>
adamc@307 895 (L'.EField ((L'.ERel 0, loc),
adamc@307 896 x), loc)) fields),
adamc@307 897 sc ")"]), loc)), loc),
adamc@307 898 fm)
adamc@307 899 end
adamc@307 900 | _ => poly ())
adamc@307 901
adamc@308 902 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), changed), _), _) =>
adamc@308 903 (case monoType env (L.TRecord changed, loc) of
adamc@308 904 (L'.TRecord changed, _) =>
adamc@308 905 let
adamc@308 906 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@308 907 val changed = map (fn (x, _) => (x, s)) changed
adamc@308 908 val rt = (L'.TRecord changed, loc)
adamc@308 909 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@308 910 in
adamc@308 911 ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@308 912 (L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@308 913 (L'.EAbs ("e", s, s,
adamc@308 914 strcat loc [sc "UPDATE ",
adamc@308 915 (L'.ERel 1, loc),
adamc@308 916 sc " AS T SET ",
adamc@308 917 strcatComma loc (map (fn (x, _) =>
adamc@311 918 strcat loc [sc ("uw_" ^ x
adamc@308 919 ^ " = "),
adamc@308 920 (L'.EField
adamc@308 921 ((L'.ERel 2,
adamc@308 922 loc),
adamc@308 923 x), loc)])
adamc@308 924 changed),
adamc@308 925 sc " WHERE ",
adamc@308 926 (L'.ERel 0, loc)]), loc)), loc)), loc),
adamc@308 927 fm)
adamc@308 928 end
adamc@308 929 | _ => poly ())
adamc@308 930
adamc@309 931 | L.ECApp ((L.EFfi ("Basis", "delete"), _), _) =>
adamc@309 932 let
adamc@309 933 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@309 934 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@309 935 in
adamc@309 936 ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc),
adamc@309 937 (L'.EAbs ("e", s, s,
adamc@309 938 strcat loc [sc "DELETE FROM ",
adamc@309 939 (L'.ERel 1, loc),
adamc@309 940 sc " AS T WHERE ",
adamc@309 941 (L'.ERel 0, loc)]), loc)), loc),
adamc@309 942 fm)
adamc@309 943 end
adamc@309 944
adamc@252 945 | L.ECApp (
adamc@252 946 (L.ECApp (
adamc@252 947 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 948 exps), _),
adamc@252 949 state) =>
adamc@252 950 (case monoType env (L.TRecord exps, loc) of
adamc@252 951 (L'.TRecord exps, _) =>
adamc@252 952 let
adamc@252 953 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 954 (case monoType env (L.TRecord xts, loc) of
adamc@252 955 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 956 | _ => NONE)
adamc@252 957 | _ => NONE) tables
adamc@252 958 in
adamc@252 959 if List.exists (fn x => x = NONE) tables then
adamc@252 960 poly ()
adamc@252 961 else
adamc@252 962 let
adamc@252 963 val tables = List.mapPartial (fn x => x) tables
adamc@252 964 val state = monoType env state
adamc@252 965 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 966 val un = (L'.TRecord [], loc)
adamc@252 967
adamc@252 968 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 969 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 970 (L'.TFun (state,
adamc@252 971 (L'.TFun (un, state), loc)),
adamc@252 972 loc)), loc)
adamc@252 973
adamc@267 974 val body' = (L'.EApp (
adamc@267 975 (L'.EApp (
adamc@267 976 (L'.EApp ((L'.ERel 4, loc),
adamc@267 977 (L'.ERel 1, loc)), loc),
adamc@267 978 (L'.ERel 0, loc)), loc),
adamc@267 979 (L'.ERecord [], loc)), loc)
adamc@252 980
adamc@252 981 val body = (L'.EQuery {exps = exps,
adamc@252 982 tables = tables,
adamc@252 983 state = state,
adamc@252 984 query = (L'.ERel 3, loc),
adamc@252 985 body = body',
adamc@252 986 initial = (L'.ERel 1, loc)},
adamc@252 987 loc)
adamc@252 988 in
adamc@252 989 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 990 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 991 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 992 (L'.EAbs ("_", un, state,
adamc@252 993 body), loc)), loc)), loc)), loc), fm)
adamc@252 994 end
adamc@252 995 end
adamc@252 996 | _ => poly ())
adamc@252 997
adamc@252 998 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 999 let
adamc@252 1000 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1001 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1002 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1003 in
adamc@252 1004 ((L'.EAbs ("r",
adamc@252 1005 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 1006 s,
adamc@252 1007 strcat loc [gf "Rows",
adamc@261 1008 (L'.ECase (gf "OrderBy",
adamc@261 1009 [((L'.PPrim (Prim.String ""), loc), sc ""),
adamc@261 1010 ((L'.PWild, loc),
adamc@261 1011 strcat loc [sc " ORDER BY ",
adamc@261 1012 gf "OrderBy"])],
adamc@261 1013 {disc = s, result = s}), loc),
adamc@252 1014 gf "Limit",
adamc@252 1015 gf "Offset"]), loc), fm)
adamc@252 1016 end
adamc@252 1017
adamc@252 1018 | L.ECApp (
adamc@252 1019 (L.ECApp (
adamc@252 1020 (L.ECApp (
adamc@252 1021 (L.ECApp (
adamc@252 1022 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 1023 (L.CRecord (_, tables), _)), _),
adamc@252 1024 (L.CRecord (_, grouped), _)), _),
adamc@252 1025 (L.CRecord (_, stables), _)), _),
adamc@252 1026 sexps) =>
adamc@252 1027 let
adamc@252 1028 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 1029 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1030 val un = (L'.TRecord [], loc)
adamc@252 1031 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 1032
adamc@252 1033 fun doTables tables =
adamc@252 1034 let
adamc@252 1035 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 1036 (case monoType env (L.TRecord xts, loc) of
adamc@252 1037 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 1038 | _ => NONE)
adamc@252 1039 | _ => NONE) tables
adamc@252 1040 in
adamc@252 1041 if List.exists (fn x => x = NONE) tables then
adamc@252 1042 NONE
adamc@252 1043 else
adamc@260 1044 let
adamc@260 1045 val tables = List.mapPartial (fn x => x) tables
adamc@260 1046 val tables = ListMergeSort.sort
adamc@260 1047 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1048 tables
adamc@260 1049 val tables = map (fn (x, xts) =>
adamc@260 1050 (x, ListMergeSort.sort
adamc@260 1051 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 1052 xts)) tables
adamc@260 1053 in
adamc@260 1054 SOME tables
adamc@260 1055 end
adamc@252 1056 end
adamc@252 1057 in
adamc@252 1058 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 1059 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@252 1060 ((L'.EAbs ("r",
adamc@252 1061 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
adamc@252 1062 ("Where", s),
adamc@252 1063 ("GroupBy", un),
adamc@252 1064 ("Having", s),
adamc@252 1065 ("SelectFields", un),
adamc@252 1066 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@252 1067 loc),
adamc@252 1068 s,
adamc@252 1069 strcat loc [sc "SELECT ",
adamc@261 1070 strcatComma loc (map (fn (x, t) =>
adamc@261 1071 strcat loc [
adamc@261 1072 (L'.EField (gf "SelectExps", x), loc),
adamc@261 1073 sc (" AS _" ^ x)
adamc@265 1074 ]) sexps
adamc@265 1075 @ map (fn (x, xts) =>
adamc@265 1076 strcatComma loc
adamc@265 1077 (map (fn (x', _) =>
adamc@311 1078 sc (x ^ ".uw_" ^ x'))
adamc@265 1079 xts)) stables),
adamc@252 1080 sc " FROM ",
adamc@252 1081 strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
adamc@253 1082 sc (" AS " ^ x)]) tables),
adamc@258 1083 (L'.ECase (gf "Where",
adamc@258 1084 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@258 1085 sc ""),
adamc@258 1086 ((L'.PWild, loc),
adamc@258 1087 strcat loc [sc " WHERE ", gf "Where"])],
adamc@258 1088 {disc = s,
adamc@258 1089 result = s}), loc),
adamc@258 1090
adamc@255 1091 if List.all (fn (x, xts) =>
adamc@255 1092 case List.find (fn (x', _) => x' = x) grouped of
adamc@255 1093 NONE => List.null xts
adamc@255 1094 | SOME (_, xts') =>
adamc@255 1095 List.all (fn (x, _) =>
adamc@255 1096 List.exists (fn (x', _) => x' = x)
adamc@255 1097 xts') xts) tables then
adamc@255 1098 sc ""
adamc@255 1099 else
adamc@255 1100 strcat loc [
adamc@255 1101 sc " GROUP BY ",
adamc@255 1102 strcatComma loc (map (fn (x, xts) =>
adamc@255 1103 strcatComma loc
adamc@255 1104 (map (fn (x', _) =>
adamc@311 1105 sc (x ^ ".uw_" ^ x'))
adamc@255 1106 xts)) grouped)
adamc@259 1107 ],
adamc@259 1108
adamc@259 1109 (L'.ECase (gf "Having",
adamc@259 1110 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@259 1111 sc ""),
adamc@259 1112 ((L'.PWild, loc),
adamc@259 1113 strcat loc [sc " HAVING ", gf "Having"])],
adamc@259 1114 {disc = s,
adamc@259 1115 result = s}), loc)
adamc@252 1116 ]), loc),
adamc@252 1117 fm)
adamc@252 1118 | _ => poly ()
adamc@252 1119 end
adamc@252 1120
adamc@252 1121 | L.ECApp (
adamc@252 1122 (L.ECApp (
adamc@252 1123 (L.ECApp (
adamc@252 1124 (L.ECApp (
adamc@252 1125 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 1126 _), _),
adamc@252 1127 _), _),
adamc@252 1128 _), _),
adamc@252 1129 t) =>
adamc@252 1130 let
adamc@252 1131 val t = monoType env t
adamc@252 1132 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 1133 in
adamc@252 1134 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 1135 (L'.ERel 0, loc)), loc), fm)
adamc@252 1136 end
adamc@252 1137
adamc@253 1138 | L.EFfi ("Basis", "sql_int") =>
adamc@253 1139 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1140 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1141 fm)
adamc@253 1142 | L.EFfi ("Basis", "sql_float") =>
adamc@253 1143 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1144 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1145 fm)
adamc@253 1146 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 1147 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1148 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1149 fm)
adamc@253 1150 | L.EFfi ("Basis", "sql_string") =>
adamc@253 1151 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 1152 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 1153 fm)
adamc@253 1154
adamc@252 1155 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 1156 ((L'.ERecord [], loc), fm)
adamc@252 1157 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 1158 ((L'.ERecord [], loc), fm)
adamc@252 1159
adamc@252 1160 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 1161 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@261 1162 | L.ECApp (
adamc@261 1163 (L.ECApp (
adamc@261 1164 (L.ECApp (
adamc@261 1165 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adamc@261 1166 _), _),
adamc@261 1167 _), _),
adamc@261 1168 _) =>
adamc@261 1169 let
adamc@261 1170 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 1171 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@261 1172 in
adamc@268 1173 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@268 1174 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adamc@268 1175 (L'.EAbs ("e2", s, s,
adamc@268 1176 (L'.ECase ((L'.ERel 0, loc),
adamc@268 1177 [((L'.PPrim (Prim.String ""), loc),
adamc@268 1178 strcat loc [(L'.ERel 2, loc),
adamc@268 1179 (L'.ERel 1, loc)]),
adamc@268 1180 ((L'.PWild, loc),
adamc@268 1181 strcat loc [(L'.ERel 2, loc),
adamc@268 1182 (L'.ERel 1, loc),
adamc@268 1183 sc ", ",
adamc@268 1184 (L'.ERel 0, loc)])],
adamc@268 1185 {disc = s, result = s}), loc)), loc)), loc)), loc),
adamc@261 1186 fm)
adamc@261 1187 end
adamc@252 1188
adamc@252 1189 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 1190 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@262 1191 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
adamc@262 1192 let
adamc@262 1193 val (e, fm) = monoExp (env, st, fm) e
adamc@262 1194 in
adamc@262 1195 (strcat loc [
adamc@262 1196 (L'.EPrim (Prim.String " LIMIT "), loc),
adamc@262 1197 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@262 1198 ],
adamc@262 1199 fm)
adamc@262 1200 end
adamc@262 1201
adamc@252 1202 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 1203 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@263 1204 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
adamc@263 1205 let
adamc@263 1206 val (e, fm) = monoExp (env, st, fm) e
adamc@263 1207 in
adamc@263 1208 (strcat loc [
adamc@263 1209 (L'.EPrim (Prim.String " OFFSET "), loc),
adamc@263 1210 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@263 1211 ],
adamc@263 1212 fm)
adamc@263 1213 end
adamc@253 1214
adamc@253 1215 | L.EFfi ("Basis", "sql_eq") =>
adamc@253 1216 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@253 1217 | L.EFfi ("Basis", "sql_ne") =>
adamc@253 1218 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@253 1219 | L.EFfi ("Basis", "sql_lt") =>
adamc@253 1220 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@253 1221 | L.EFfi ("Basis", "sql_le") =>
adamc@253 1222 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@253 1223 | L.EFfi ("Basis", "sql_gt") =>
adamc@253 1224 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@253 1225 | L.EFfi ("Basis", "sql_ge") =>
adamc@253 1226 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 1227
adamc@253 1228 | L.ECApp (
adamc@253 1229 (L.ECApp (
adamc@253 1230 (L.ECApp (
adamc@253 1231 (L.ECApp (
adamc@254 1232 (L.ECApp (
adamc@264 1233 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 1234 _), _),
adamc@264 1235 _), _),
adamc@264 1236 _), _),
adamc@264 1237 _), _),
adamc@264 1238 _) =>
adamc@264 1239 let
adamc@264 1240 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 1241 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 1242 in
adamc@264 1243 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 1244 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@264 1245 strcat loc [sc "(",
adamc@264 1246 (L'.ERel 1, loc),
adamc@264 1247 sc " ",
adamc@264 1248 (L'.ERel 0, loc),
adamc@264 1249 sc ")"]), loc)), loc),
adamc@264 1250 fm)
adamc@264 1251 end
adamc@264 1252 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@264 1253
adamc@264 1254 | L.ECApp (
adamc@264 1255 (L.ECApp (
adamc@264 1256 (L.ECApp (
adamc@264 1257 (L.ECApp (
adamc@264 1258 (L.ECApp (
adamc@254 1259 (L.ECApp (
adamc@254 1260 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 1261 _), _),
adamc@254 1262 _), _),
adamc@254 1263 _), _),
adamc@254 1264 _), _),
adamc@254 1265 _), _),
adamc@254 1266 _) =>
adamc@254 1267 let
adamc@254 1268 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 1269 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 1270 in
adamc@254 1271 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 1272 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 1273 (L'.EAbs ("e2", s, s,
adamc@254 1274 strcat loc [sc "(",
adamc@254 1275 (L'.ERel 1, loc),
adamc@254 1276 sc " ",
adamc@254 1277 (L'.ERel 2, loc),
adamc@254 1278 sc " ",
adamc@254 1279 (L'.ERel 0, loc),
adamc@254 1280 sc ")"]), loc)), loc)), loc),
adamc@254 1281 fm)
adamc@254 1282 end
adamc@254 1283 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 1284 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 1285
adamc@254 1286 | L.ECApp (
adamc@254 1287 (L.ECApp (
adamc@254 1288 (L.ECApp (
adamc@254 1289 (L.ECApp (
adamc@253 1290 (L.EFfi ("Basis", "sql_comparison"), _),
adamc@253 1291 _), _),
adamc@253 1292 _), _),
adamc@253 1293 _), _),
adamc@253 1294 _) =>
adamc@253 1295 let
adamc@253 1296 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@253 1297 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@253 1298 in
adamc@253 1299 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@253 1300 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@253 1301 (L'.EAbs ("e2", s, s,
adamc@254 1302 strcat loc [sc "(",
adamc@254 1303 (L'.ERel 1, loc),
adamc@253 1304 sc " ",
adamc@253 1305 (L'.ERel 2, loc),
adamc@253 1306 sc " ",
adamc@254 1307 (L'.ERel 0, loc),
adamc@254 1308 sc ")"]), loc)), loc)), loc),
adamc@253 1309 fm)
adamc@253 1310 end
adamc@253 1311
adamc@253 1312 | L.ECApp (
adamc@253 1313 (L.ECApp (
adamc@253 1314 (L.ECApp (
adamc@253 1315 (L.ECApp (
adamc@253 1316 (L.ECApp (
adamc@253 1317 (L.ECApp (
adamc@253 1318 (L.ECApp (
adamc@253 1319 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 1320 _), _),
adamc@253 1321 _), _),
adamc@253 1322 _), _),
adamc@253 1323 _), _),
adamc@253 1324 _), _),
adamc@253 1325 (L.CName tab, _)), _),
adamc@311 1326 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm)
adamc@260 1327
adamc@260 1328 | L.ECApp (
adamc@260 1329 (L.ECApp (
adamc@260 1330 (L.ECApp (
adamc@260 1331 (L.ECApp (
adamc@261 1332 (L.ECApp (
adamc@261 1333 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 1334 _), _),
adamc@261 1335 _), _),
adamc@261 1336 _), _),
adamc@261 1337 _), _),
adamc@261 1338 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 1339
adamc@261 1340 | L.ECApp (
adamc@261 1341 (L.ECApp (
adamc@261 1342 (L.ECApp (
adamc@261 1343 (L.ECApp (
adamc@260 1344 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 1345 _), _),
adamc@260 1346 _), _),
adamc@260 1347 _), _),
adamc@260 1348 _) =>
adamc@260 1349 let
adamc@260 1350 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 1351 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 1352 in
adamc@260 1353 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 1354 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 1355 (L'.EAbs ("e2", s, s,
adamc@260 1356 strcat loc [sc "((",
adamc@260 1357 (L'.ERel 1, loc),
adamc@260 1358 sc ") ",
adamc@260 1359 (L'.ERel 2, loc),
adamc@260 1360 sc " (",
adamc@260 1361 (L'.ERel 0, loc),
adamc@260 1362 sc "))"]), loc)), loc)), loc),
adamc@260 1363 fm)
adamc@260 1364 end
adamc@260 1365
adamc@260 1366 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 1367 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 1368 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 1369
adamc@265 1370 | L.ECApp (
adamc@265 1371 (L.ECApp (
adamc@265 1372 (L.ECApp (
adamc@265 1373 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 1374 _), _),
adamc@265 1375 _), _),
adamc@265 1376 _) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@265 1377 (L'.EPrim (Prim.String "COUNT(*)"), loc)), loc),
adamc@265 1378 fm)
adamc@266 1379
adamc@266 1380 | L.ECApp (
adamc@266 1381 (L.ECApp (
adamc@266 1382 (L.ECApp (
adamc@266 1383 (L.ECApp (
adamc@266 1384 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 1385 _), _),
adamc@266 1386 _), _),
adamc@266 1387 _), _),
adamc@266 1388 _) =>
adamc@266 1389 let
adamc@266 1390 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 1391 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 1392 in
adamc@266 1393 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 1394 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@266 1395 strcat loc [(L'.ERel 1, loc),
adamc@266 1396 sc "(",
adamc@266 1397 (L'.ERel 0, loc),
adamc@266 1398 sc ")"]), loc)), loc),
adamc@266 1399 fm)
adamc@266 1400 end
adamc@266 1401
adamc@266 1402 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1403 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1404 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 1405 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1406 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 1407 fm)
adamc@266 1408 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 1409 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1410 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 1411 fm)
adamc@266 1412
adamc@266 1413 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 1414 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 1415 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 1416 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 1417 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1418 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 1419 fm)
adamc@266 1420 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 1421 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 1422 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 1423 fm)
adamc@266 1424
adamc@268 1425 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 1426 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 1427
adamc@338 1428 | L.EFfiApp ("Basis", "nextval", [e]) =>
adamc@338 1429 let
adamc@338 1430 val un = (L'.TRecord [], loc)
adamc@338 1431 val int = (L'.TFfi ("Basis", "int"), loc)
adamc@338 1432 val (e, fm) = monoExp (env, st, fm) e
adamc@338 1433 in
adamc@338 1434 ((L'.EAbs ("_", un, int,
adamc@338 1435 (L'.ENextval (liftExpInExp 0 e), loc)), loc),
adamc@338 1436 fm)
adamc@338 1437 end
adamc@338 1438
adamc@139 1439 | L.EApp (
adamc@139 1440 (L.ECApp (
adamc@141 1441 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 1442 _), _),
adamc@179 1443 se) =>
adamc@179 1444 let
adamc@179 1445 val (se, fm) = monoExp (env, st, fm) se
adamc@179 1446 in
adamc@179 1447 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 1448 end
adamc@179 1449
adamc@95 1450 | L.EApp (
adamc@95 1451 (L.EApp (
adamc@95 1452 (L.ECApp (
adamc@95 1453 (L.ECApp (
adamc@95 1454 (L.ECApp (
adamc@139 1455 (L.ECApp (
adamc@140 1456 (L.EFfi ("Basis", "join"),
adamc@139 1457 _), _), _),
adamc@139 1458 _), _),
adamc@95 1459 _), _),
adamc@95 1460 _), _),
adamc@95 1461 xml1), _),
adamc@179 1462 xml2) =>
adamc@179 1463 let
adamc@179 1464 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 1465 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 1466 in
adamc@179 1467 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 1468 end
adamc@95 1469
adamc@95 1470 | L.EApp (
adamc@95 1471 (L.EApp (
adamc@104 1472 (L.EApp (
adamc@95 1473 (L.ECApp (
adamc@104 1474 (L.ECApp (
adamc@104 1475 (L.ECApp (
adamc@104 1476 (L.ECApp (
adamc@139 1477 (L.ECApp (
adamc@139 1478 (L.ECApp (
adamc@139 1479 (L.ECApp (
adamc@139 1480 (L.ECApp (
adamc@139 1481 (L.EFfi ("Basis", "tag"),
adamc@139 1482 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 1483 attrs), _),
adamc@95 1484 tag), _),
adamc@95 1485 xml) =>
adamc@95 1486 let
adamc@140 1487 fun getTag' (e, _) =
adamc@140 1488 case e of
adamc@143 1489 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 1490 | L.ECApp (e, t) => let
adamc@143 1491 val (tag, ts) = getTag' e
adamc@143 1492 in
adamc@143 1493 (tag, ts @ [t])
adamc@143 1494 end
adamc@140 1495 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 1496 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1497 ("", []))
adamc@140 1498
adamc@95 1499 fun getTag (e, _) =
adamc@95 1500 case e of
adamc@143 1501 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 1502 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 1503 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 1504 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1505 ("", []))
adamc@95 1506
adamc@143 1507 val (tag, targs) = getTag tag
adamc@95 1508
adamc@179 1509 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 1510
adamc@143 1511 fun tagStart tag =
adamc@104 1512 case #1 attrs of
adamc@104 1513 L'.ERecord xes =>
adamc@104 1514 let
adamc@104 1515 fun lowercaseFirst "" = ""
adamc@143 1516 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 1517 ^ String.extract (s, 1, NONE)
adamc@104 1518
adamc@104 1519 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 1520 in
adamc@179 1521 foldl (fn ((x, e, t), (s, fm)) =>
adamc@347 1522 case t of
adamc@347 1523 (L'.TFfi ("Basis", "bool"), _) =>
adamc@347 1524 let
adamc@347 1525 val s' = " " ^ lowercaseFirst x
adamc@347 1526 in
adamc@347 1527 ((L'.ECase (e,
adamc@347 1528 [((L'.PCon (L'.Enum,
adamc@347 1529 L'.PConFfi {mod = "Basis",
adamc@347 1530 datatyp = "bool",
adamc@347 1531 con = "True",
adamc@347 1532 arg = NONE},
adamc@347 1533 NONE), loc),
adamc@347 1534 (L'.EStrcat (s,
adamc@347 1535 (L'.EPrim (Prim.String s'), loc)), loc)),
adamc@347 1536 ((L'.PCon (L'.Enum,
adamc@347 1537 L'.PConFfi {mod = "Basis",
adamc@347 1538 datatyp = "bool",
adamc@347 1539 con = "False",
adamc@347 1540 arg = NONE},
adamc@347 1541 NONE), loc),
adamc@347 1542 s)],
adamc@347 1543 {disc = (L'.TFfi ("Basis", "bool"), loc),
adamc@347 1544 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
adamc@347 1545 fm)
adamc@347 1546 end
adamc@347 1547 | _ =>
adamc@347 1548 let
adamc@347 1549 val fooify =
adamc@347 1550 case x of
adamc@347 1551 "Href" => urlifyExp
adamc@347 1552 | "Link" => urlifyExp
adamc@347 1553 | "Action" => urlifyExp
adamc@347 1554 | _ => attrifyExp
adamc@120 1555
adamc@347 1556 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@179 1557
adamc@347 1558 val (e, fm) = fooify env fm (e, t)
adamc@347 1559 in
adamc@347 1560 ((L'.EStrcat (s,
adamc@347 1561 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@347 1562 (L'.EStrcat (e,
adamc@347 1563 (L'.EPrim (Prim.String "\""),
adamc@347 1564 loc)),
adamc@347 1565 loc)),
adamc@347 1566 loc)), loc),
adamc@347 1567 fm)
adamc@347 1568 end)
adamc@179 1569 (s, fm) xes
adamc@104 1570 end
adamc@143 1571 | _ => raise Fail "Non-record attributes!"
adamc@104 1572
adamc@143 1573 fun input typ =
adamc@143 1574 case targs of
adamc@155 1575 [_, (L.CName name, _)] =>
adamc@179 1576 let
adamc@179 1577 val (ts, fm) = tagStart "input"
adamc@179 1578 in
adamc@179 1579 ((L'.EStrcat (ts,
adamc@179 1580 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 1581 loc)), loc), fm)
adamc@179 1582 end
adamc@143 1583 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1584 raise Fail "No name passed to input tag")
adamc@104 1585
adamc@152 1586 fun normal (tag, extra) =
adamc@143 1587 let
adamc@179 1588 val (tagStart, fm) = tagStart tag
adamc@152 1589 val tagStart = case extra of
adamc@152 1590 NONE => tagStart
adamc@152 1591 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 1592
adamc@143 1593 fun normal () =
adamc@179 1594 let
adamc@179 1595 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1596 in
adamc@179 1597 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 1598 (L'.EStrcat (xml,
adamc@179 1599 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 1600 loc)), loc)),
adamc@179 1601 loc),
adamc@179 1602 fm)
adamc@179 1603 end
adamc@143 1604 in
adamc@143 1605 case xml of
adamc@143 1606 (L.EApp ((L.ECApp (
adamc@143 1607 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 1608 _), _),
adamc@143 1609 _), _),
adamc@143 1610 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 1611 if CharVector.all Char.isSpace s then
adamc@179 1612 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 1613 else
adamc@143 1614 normal ()
adamc@143 1615 | _ => normal ()
adamc@143 1616 end
adamc@152 1617 in
adamc@152 1618 case tag of
adamc@179 1619 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
adamc@152 1620
adamc@152 1621 | "textbox" =>
adamc@152 1622 (case targs of
adamc@152 1623 [_, (L.CName name, _)] =>
adamc@179 1624 let
adamc@179 1625 val (ts, fm) = tagStart "input"
adamc@179 1626 in
adamc@179 1627 ((L'.EStrcat (ts,
adamc@179 1628 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 1629 loc)), loc), fm)
adamc@179 1630 end
adamc@152 1631 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1632 raise Fail "No name passed to textarea tag"))
adamc@155 1633 | "password" => input "password"
adamc@361 1634 | "textarea" =>
adamc@152 1635 (case targs of
adamc@152 1636 [_, (L.CName name, _)] =>
adamc@179 1637 let
adamc@179 1638 val (ts, fm) = tagStart "textarea"
adamc@179 1639 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1640 in
adamc@179 1641 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1642 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1643 (L'.EStrcat (xml,
adamc@179 1644 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 1645 loc)), loc)),
adamc@179 1646 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 ltextarea tag"))
adamc@153 1650
adamc@190 1651 | "checkbox" => input "checkbox"
adamc@190 1652
adamc@153 1653 | "radio" =>
adamc@153 1654 (case targs of
adamc@153 1655 [_, (L.CName name, _)] =>
adamc@179 1656 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 1657 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1658 raise Fail "No name passed to radio tag"))
adamc@153 1659 | "radioOption" =>
adamc@153 1660 (case St.radioGroup st of
adamc@153 1661 NONE => raise Fail "No name for radioGroup"
adamc@153 1662 | SOME name =>
adamc@153 1663 normal ("input",
adamc@153 1664 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 1665
adamc@361 1666 | "select" =>
adamc@154 1667 (case targs of
adamc@154 1668 [_, (L.CName name, _)] =>
adamc@179 1669 let
adamc@179 1670 val (ts, fm) = tagStart "select"
adamc@179 1671 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1672 in
adamc@179 1673 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1674 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1675 (L'.EStrcat (xml,
adamc@179 1676 (L'.EPrim (Prim.String "</select>"),
adamc@179 1677 loc)), loc)),
adamc@179 1678 loc),
adamc@179 1679 fm)
adamc@179 1680 end
adamc@154 1681 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 1682 raise Fail "No name passed to lselect tag"))
adamc@154 1683
adamc@361 1684 | "option" => normal ("option", NONE)
adamc@154 1685
adamc@325 1686 | "tabl" => normal ("table", NONE)
adamc@152 1687 | _ => normal (tag, NONE)
adamc@95 1688 end
adamc@94 1689
adamc@141 1690 | L.EApp ((L.ECApp (
adamc@361 1691 (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _),
adamc@141 1692 _), _),
adamc@141 1693 xml) =>
adamc@143 1694 let
adamc@143 1695 fun findSubmit (e, _) =
adamc@143 1696 case e of
adamc@143 1697 L.EApp (
adamc@143 1698 (L.EApp (
adamc@143 1699 (L.ECApp (
adamc@143 1700 (L.ECApp (
adamc@143 1701 (L.ECApp (
adamc@143 1702 (L.ECApp (
adamc@143 1703 (L.EFfi ("Basis", "join"),
adamc@143 1704 _), _), _),
adamc@143 1705 _), _),
adamc@143 1706 _), _),
adamc@143 1707 _), _),
adamc@143 1708 xml1), _),
adamc@143 1709 xml2) => (case findSubmit xml1 of
adamc@143 1710 Error => Error
adamc@143 1711 | NotFound => findSubmit xml2
adamc@143 1712 | Found e =>
adamc@143 1713 case findSubmit xml2 of
adamc@143 1714 NotFound => Found e
adamc@143 1715 | _ => Error)
adamc@143 1716 | L.EApp (
adamc@143 1717 (L.EApp (
adamc@143 1718 (L.EApp (
adamc@143 1719 (L.ECApp (
adamc@143 1720 (L.ECApp (
adamc@143 1721 (L.ECApp (
adamc@143 1722 (L.ECApp (
adamc@143 1723 (L.ECApp (
adamc@143 1724 (L.ECApp (
adamc@143 1725 (L.ECApp (
adamc@143 1726 (L.ECApp (
adamc@143 1727 (L.EFfi ("Basis", "tag"),
adamc@143 1728 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 1729 attrs), _),
adamc@143 1730 _), _),
adamc@143 1731 xml) =>
adamc@143 1732 (case #1 attrs of
adamc@143 1733 L.ERecord xes =>
adamc@143 1734 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 1735 | _ => NONE) xes of
adamc@143 1736 NONE => findSubmit xml
adamc@143 1737 | SOME et =>
adamc@143 1738 case findSubmit xml of
adamc@143 1739 NotFound => Found et
adamc@143 1740 | _ => Error)
adamc@143 1741 | _ => findSubmit xml)
adamc@143 1742 | _ => NotFound
adamc@143 1743
adamc@143 1744 val (action, actionT) = case findSubmit xml of
adamc@143 1745 NotFound => raise Fail "No submit found"
adamc@143 1746 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 1747 | Found et => et
adamc@143 1748
adamc@143 1749 val actionT = monoType env actionT
adamc@179 1750 val (action, fm) = monoExp (env, st, fm) action
adamc@179 1751 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 1752 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 1753 in
adamc@179 1754 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 1755 (L'.EStrcat (action,
adamc@179 1756 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 1757 (L'.EStrcat (xml,
adamc@179 1758 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 1759 fm)
adamc@143 1760 end
adamc@141 1761
adamc@148 1762 | L.EApp ((L.ECApp (
adamc@148 1763 (L.ECApp (
adamc@148 1764 (L.ECApp (
adamc@148 1765 (L.ECApp (
adamc@148 1766 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 1767 _), _),
adamc@148 1768 _), _),
adamc@148 1769 _), _),
adamc@179 1770 xml) => monoExp (env, st, fm) xml
adamc@148 1771
adamc@283 1772 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 1773 let
adamc@283 1774 val t = monoType env t
adamc@283 1775 in
adamc@283 1776 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 1777 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 1778 fm)
adamc@283 1779 end
adamc@283 1780
adamc@179 1781 | L.EApp (e1, e2) =>
adamc@179 1782 let
adamc@179 1783 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 1784 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 1785 in
adamc@179 1786 ((L'.EApp (e1, e2), loc), fm)
adamc@179 1787 end
adamc@26 1788 | L.EAbs (x, dom, ran, e) =>
adamc@179 1789 let
adamc@179 1790 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 1791 in
adamc@179 1792 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 1793 end
adamc@25 1794 | L.ECApp _ => poly ()
adamc@25 1795 | L.ECAbs _ => poly ()
adamc@25 1796
adamc@252 1797 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 1798 | L.EFfiApp (m, x, es) =>
adamc@252 1799 let
adamc@252 1800 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 1801 in
adamc@252 1802 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 1803 end
adamc@252 1804
adamc@179 1805 | L.ERecord xes =>
adamc@179 1806 let
adamc@179 1807 val (xes, fm) = ListUtil.foldlMap
adamc@179 1808 (fn ((x, e, t), fm) =>
adamc@179 1809 let
adamc@179 1810 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1811 in
adamc@179 1812 ((monoName env x,
adamc@179 1813 e,
adamc@179 1814 monoType env t), fm)
adamc@179 1815 end) fm xes
adamc@179 1816 in
adamc@179 1817 ((L'.ERecord xes, loc), fm)
adamc@179 1818 end
adamc@179 1819 | L.EField (e, x, _) =>
adamc@179 1820 let
adamc@179 1821 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1822 in
adamc@179 1823 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 1824 end
adamc@343 1825 | L.EWith _ => poly ()
adamc@149 1826 | L.ECut _ => poly ()
adamc@73 1827 | L.EFold _ => poly ()
adamc@177 1828
adamc@182 1829 | L.ECase (e, pes, {disc, result}) =>
adamc@179 1830 let
adamc@179 1831 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1832 val (pes, fm) = ListUtil.foldlMap
adamc@179 1833 (fn ((p, e), fm) =>
adamc@179 1834 let
adamc@179 1835 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1836 in
adamc@182 1837 ((monoPat env p, e), fm)
adamc@179 1838 end) fm pes
adamc@179 1839 in
adamc@182 1840 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 1841 end
adamc@177 1842
adamc@179 1843 | L.EWrite e =>
adamc@179 1844 let
adamc@179 1845 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1846 in
adamc@252 1847 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 1848 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 1849 end
adamc@110 1850
adamc@179 1851 | L.EClosure (n, es) =>
adamc@179 1852 let
adamc@179 1853 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 1854 monoExp (env, st, fm) e)
adamc@179 1855 fm es
adamc@179 1856 in
adamc@179 1857 ((L'.EClosure (n, es), loc), fm)
adamc@179 1858 end
adamc@25 1859 end
adamc@25 1860
adamc@179 1861 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 1862 let
adamc@25 1863 fun poly () =
adamc@25 1864 (E.errorAt loc "Unsupported declaration";
adamc@25 1865 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 1866 NONE)
adamc@25 1867 in
adamc@25 1868 case d of
adamc@25 1869 L.DCon _ => NONE
adamc@193 1870 | L.DDatatype (x, n, [], xncs) =>
adamc@193 1871 let
adamc@196 1872 val env' = Env.declBinds env all
adamc@196 1873 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 1874 in
adamc@273 1875 SOME (env', fm, [d])
adamc@193 1876 end
adamc@193 1877 | L.DDatatype _ => poly ()
adamc@179 1878 | L.DVal (x, n, t, e, s) =>
adamc@179 1879 let
adamc@179 1880 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 1881 in
adamc@179 1882 SOME (Env.pushENamed env x n t NONE s,
adamc@179 1883 fm,
adamc@273 1884 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 1885 end
adamc@128 1886 | L.DValRec vis =>
adamc@128 1887 let
adamc@128 1888 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 1889
adamc@179 1890 val (vis, fm) = ListUtil.foldlMap
adamc@179 1891 (fn ((x, n, t, e, s), fm) =>
adamc@179 1892 let
adamc@179 1893 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 1894 in
adamc@179 1895 ((x, n, monoType env t, e, s), fm)
adamc@179 1896 end)
adamc@179 1897 fm vis
adamc@128 1898 in
adamc@128 1899 SOME (env,
adamc@179 1900 fm,
adamc@273 1901 [(L'.DValRec vis, loc)])
adamc@128 1902 end
adamc@144 1903 | L.DExport (ek, n) =>
adamc@115 1904 let
adamc@120 1905 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 1906
adamc@120 1907 fun unwind (t, _) =
adamc@120 1908 case t of
adamc@120 1909 L.TFun (dom, ran) => dom :: unwind ran
adamc@314 1910 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@314 1911 (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: unwind t
adamc@120 1912 | _ => []
adamc@120 1913
adamc@120 1914 val ts = map (monoType env) (unwind t)
adamc@115 1915 in
adamc@273 1916 SOME (env, fm, [(L'.DExport (ek, s, n, ts), loc)])
adamc@115 1917 end
adamc@273 1918 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
adamc@251 1919 let
adamc@251 1920 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 1921 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@311 1922 val s = "uw_" ^ s
adamc@251 1923 val e = (L'.EPrim (Prim.String s), loc)
adamc@273 1924
adamc@273 1925 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@251 1926 in
adamc@251 1927 SOME (Env.pushENamed env x n t NONE s,
adamc@251 1928 fm,
adamc@273 1929 [(L'.DTable (s, xts), loc),
adamc@273 1930 (L'.DVal (x, n, t', e, s), loc)])
adamc@251 1931 end
adamc@273 1932 | L.DTable _ => poly ()
adamc@338 1933 | L.DSequence (x, n, s) =>
adamc@338 1934 let
adamc@338 1935 val t = (L.CFfi ("Basis", "string"), loc)
adamc@338 1936 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@338 1937 val s = "uw_" ^ s
adamc@338 1938 val e = (L'.EPrim (Prim.String s), loc)
adamc@338 1939 in
adamc@338 1940 SOME (Env.pushENamed env x n t NONE s,
adamc@338 1941 fm,
adamc@338 1942 [(L'.DSequence s, loc),
adamc@338 1943 (L'.DVal (x, n, t', e, s), loc)])
adamc@338 1944 end
adamc@273 1945 | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)])
adamc@25 1946 end
adamc@25 1947
adamc@25 1948 fun monoize env ds =
adamc@25 1949 let
adamc@385 1950 val p = !urlPrefix
adamc@385 1951 val () =
adamc@385 1952 if p = "" then
adamc@385 1953 urlPrefix := "/"
adamc@385 1954 else if String.sub (p, size p - 1) <> #"/" then
adamc@385 1955 urlPrefix := p ^ "/"
adamc@385 1956 else
adamc@385 1957 ()
adamc@385 1958
adamc@179 1959 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 1960 case monoDecl (env, fm) d of
adamc@179 1961 NONE => (env, fm, ds)
adamc@273 1962 | SOME (env, fm, ds') =>
adamc@179 1963 (env,
adamc@179 1964 Fm.enter fm,
adamc@273 1965 ds' @ Fm.decls fm @ ds))
adamc@179 1966 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 1967 in
adamc@25 1968 rev ds
adamc@25 1969 end
adamc@25 1970
adamc@25 1971 end