annotate src/monoize.sml @ 452:222cbc1da232

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