annotate src/monoize.sml @ 441:c5335613f31e

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