annotate src/monoize.sml @ 417:e0e9e9eca1cb

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