annotate src/monoize.sml @ 389:acaf9d19fbb7

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