annotate src/monoize.sml @ 437:1a4c1b5f4d8f

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