annotate src/monoize.sml @ 391:fe8f75f7e130

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