annotate src/monoize.sml @ 287:3ed7a7c7b060

Ask GCC to strip executables
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 10:52:51 -0400
parents ffe5b01908ae
children 4260ad920c36
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@196 38 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
adamc@25 39
adamc@252 40 structure U = MonoUtil
adamc@252 41
adamc@252 42 val liftExpInExp =
adamc@252 43 U.Exp.mapB {typ = fn t => t,
adamc@252 44 exp = fn bound => fn e =>
adamc@252 45 case e of
adamc@252 46 L'.ERel xn =>
adamc@252 47 if xn < bound then
adamc@252 48 e
adamc@252 49 else
adamc@252 50 L'.ERel (xn + 1)
adamc@252 51 | _ => e,
adamc@252 52 bind = fn (bound, U.Exp.RelE _) => bound + 1
adamc@252 53 | (bound, _) => bound}
adamc@252 54
adamc@25 55 fun monoName env (all as (c, loc)) =
adamc@25 56 let
adamc@25 57 fun poly () =
adamc@25 58 (E.errorAt loc "Unsupported name constructor";
adamc@25 59 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 60 "")
adamc@25 61 in
adamc@25 62 case c of
adamc@25 63 L.CName s => s
adamc@25 64 | _ => poly ()
adamc@25 65 end
adamc@25 66
adamc@196 67 fun monoType env =
adamc@25 68 let
adamc@196 69 fun mt env dtmap (all as (c, loc)) =
adamc@196 70 let
adamc@196 71 fun poly () =
adamc@196 72 (E.errorAt loc "Unsupported type constructor";
adamc@196 73 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 74 dummyTyp)
adamc@196 75 in
adamc@196 76 case c of
adamc@196 77 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 78 | L.TCFun _ => poly ()
adamc@196 79 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@196 80 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
adamc@196 81 | L.TRecord _ => poly ()
adamc@196 82
adamc@286 83 | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
adamc@286 84 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@286 85
adamc@196 86 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 87 (L'.TFfi ("Basis", "string"), loc)
adamc@196 88 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 89 (L'.TFfi ("Basis", "string"), loc)
adamc@196 90
adamc@251 91 | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) =>
adamc@252 92 (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc)
adamc@252 93 | L.CApp ((L.CFfi ("Basis", "sql_table"), _), _) =>
adamc@252 94 (L'.TFfi ("Basis", "string"), loc)
adamc@252 95 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) =>
adamc@252 96 (L'.TFfi ("Basis", "string"), loc)
adamc@252 97 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) =>
adamc@252 98 (L'.TFfi ("Basis", "string"), loc)
adamc@252 99 | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) =>
adamc@252 100 (L'.TFfi ("Basis", "string"), loc)
adamc@252 101
adamc@252 102 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) =>
adamc@252 103 (L'.TRecord [], loc)
adamc@252 104 | L.CFfi ("Basis", "sql_relop") =>
adamc@252 105 (L'.TFfi ("Basis", "string"), loc)
adamc@252 106 | L.CFfi ("Basis", "sql_direction") =>
adamc@252 107 (L'.TFfi ("Basis", "string"), loc)
adamc@252 108 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) =>
adamc@252 109 (L'.TFfi ("Basis", "string"), loc)
adamc@252 110 | L.CFfi ("Basis", "sql_limit") =>
adamc@252 111 (L'.TFfi ("Basis", "string"), loc)
adamc@252 112 | L.CFfi ("Basis", "sql_offset") =>
adamc@252 113 (L'.TFfi ("Basis", "string"), loc)
adamc@252 114
adamc@252 115 | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) =>
adamc@252 116 (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
adamc@252 117 | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) =>
adamc@252 118 (L'.TFfi ("Basis", "string"), loc)
adamc@252 119 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) =>
adamc@252 120 (L'.TFfi ("Basis", "string"), loc)
adamc@252 121 | L.CFfi ("Basis", "sql_comparison") =>
adamc@252 122 (L'.TFfi ("Basis", "string"), loc)
adamc@252 123 | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) =>
adamc@252 124 (L'.TFfi ("Basis", "string"), loc)
adamc@252 125 | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) =>
adamc@252 126 (L'.TRecord [], loc)
adamc@252 127 | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) =>
adamc@252 128 (L'.TRecord [], loc)
adamc@251 129
adamc@196 130 | L.CRel _ => poly ()
adamc@196 131 | L.CNamed n =>
adamc@196 132 (case IM.find (dtmap, n) of
adamc@196 133 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 134 | NONE =>
adamc@196 135 let
adamc@196 136 val r = ref (L'.Default, [])
adamc@196 137 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 138
adamc@196 139 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 140
adamc@196 141 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 142 in
adamc@196 143 case xs of
adamc@198 144 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 145 (L'.TDatatype (n, r), loc))
adamc@196 146 | _ => poly ()
adamc@196 147 end)
adamc@196 148 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 149 | L.CApp _ => poly ()
adamc@196 150 | L.CAbs _ => poly ()
adamc@196 151
adamc@196 152 | L.CName _ => poly ()
adamc@196 153
adamc@196 154 | L.CRecord _ => poly ()
adamc@196 155 | L.CConcat _ => poly ()
adamc@196 156 | L.CFold _ => poly ()
adamc@196 157 | L.CUnit => poly ()
adamc@214 158
adamc@214 159 | L.CTuple _ => poly ()
adamc@214 160 | L.CProj _ => poly ()
adamc@196 161 end
adamc@25 162 in
adamc@196 163 mt env IM.empty
adamc@25 164 end
adamc@25 165
adamc@25 166 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 167
adamc@179 168 structure IM = IntBinaryMap
adamc@179 169
adamc@179 170 datatype foo_kind =
adamc@179 171 Attr
adamc@179 172 | Url
adamc@179 173
adamc@179 174 fun fk2s fk =
adamc@179 175 case fk of
adamc@179 176 Attr => "attr"
adamc@179 177 | Url => "url"
adamc@179 178
adamc@179 179 structure Fm :> sig
adamc@179 180 type t
adamc@179 181
adamc@179 182 val empty : int -> t
adamc@179 183
adamc@179 184 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@179 185 val enter : t -> t
adamc@179 186 val decls : t -> L'.decl list
adamc@179 187 end = struct
adamc@179 188
adamc@179 189 structure M = BinaryMapFn(struct
adamc@179 190 type ord_key = foo_kind
adamc@179 191 fun compare x =
adamc@179 192 case x of
adamc@179 193 (Attr, Attr) => EQUAL
adamc@179 194 | (Attr, _) => LESS
adamc@179 195 | (_, Attr) => GREATER
adamc@179 196
adamc@179 197 | (Url, Url) => EQUAL
adamc@179 198 end)
adamc@179 199
adamc@179 200 type t = {
adamc@179 201 count : int,
adamc@179 202 map : int IM.map M.map,
adamc@179 203 decls : L'.decl list
adamc@179 204 }
adamc@179 205
adamc@179 206 fun empty count = {
adamc@179 207 count = count,
adamc@179 208 map = M.empty,
adamc@179 209 decls = []
adamc@179 210 }
adamc@179 211
adamc@179 212 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
adamc@179 213 fun decls ({decls, ...} : t) = decls
adamc@179 214
adamc@179 215 fun lookup (t as {count, map, decls}) k n thunk =
adamc@120 216 let
adamc@179 217 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 218 in
adamc@179 219 case IM.find (im, n) of
adamc@179 220 NONE =>
adamc@179 221 let
adamc@179 222 val n' = count
adamc@179 223 val (d, {count, map, decls}) = thunk count {count = count + 1,
adamc@179 224 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@179 225 decls = decls}
adamc@179 226 in
adamc@179 227 ({count = count,
adamc@179 228 map = map,
adamc@179 229 decls = d :: decls}, n')
adamc@179 230 end
adamc@179 231 | SOME n' => (t, n')
adamc@179 232 end
adamc@179 233
adamc@179 234 end
adamc@185 235
adamc@185 236
adamc@185 237 fun capitalize s =
adamc@185 238 if s = "" then
adamc@185 239 s
adamc@185 240 else
adamc@185 241 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 242
adamc@179 243 fun fooifyExp fk env =
adamc@179 244 let
adamc@179 245 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 246 case #1 e of
adamc@120 247 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 248 let
adamc@120 249 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 250 in
adamc@183 251 ((L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
adamc@120 252 end
adamc@120 253 | L'.EClosure (fnam, args) =>
adamc@120 254 let
adamc@120 255 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 256 val ft = monoType env ft
adamc@111 257
adamc@179 258 fun attrify (args, ft, e, fm) =
adamc@120 259 case (args, ft) of
adamc@179 260 ([], _) => (e, fm)
adamc@120 261 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 262 let
adamc@179 263 val (arg', fm) = fooify fm (arg, t)
adamc@179 264 in
adamc@179 265 attrify (args, ft,
adamc@179 266 (L'.EStrcat (e,
adamc@179 267 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 268 arg'), loc)), loc),
adamc@179 269 fm)
adamc@179 270 end
adamc@120 271 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 272 (e, fm))
adamc@120 273 in
adamc@183 274 attrify (args, ft, (L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
adamc@120 275 end
adamc@120 276 | _ =>
adamc@120 277 case t of
adamc@185 278 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 279
adamc@179 280 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 281 | L'.TRecord ((x, t) :: xts) =>
adamc@200 282 let
adamc@200 283 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 284 in
adamc@200 285 foldl (fn ((x, t), (se, fm)) =>
adamc@200 286 let
adamc@200 287 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 288 in
adamc@200 289 ((L'.EStrcat (se,
adamc@200 290 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 291 se'), loc)), loc),
adamc@200 292 fm)
adamc@200 293 end) (se, fm) xts
adamc@200 294 end
adamc@111 295
adamc@196 296 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 297 let
adamc@179 298 fun makeDecl n fm =
adamc@179 299 let
adamc@193 300 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 301
adamc@179 302 val (branches, fm) =
adamc@179 303 ListUtil.foldlMap
adamc@179 304 (fn ((x, n, to), fm) =>
adamc@179 305 case to of
adamc@179 306 NONE =>
adamc@188 307 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 308 (L'.EPrim (Prim.String x), loc)),
adamc@179 309 fm)
adamc@179 310 | SOME t =>
adamc@179 311 let
adamc@182 312 val t = monoType env t
adamc@182 313 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 314 in
adamc@188 315 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 316 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 317 arg), loc)),
adamc@179 318 fm)
adamc@179 319 end)
adamc@179 320 fm xncs
adamc@179 321
adamc@179 322 val dom = tAll
adamc@179 323 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 324 in
adamc@179 325 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 326 n,
adamc@179 327 (L'.TFun (dom, ran), loc),
adamc@179 328 (L'.EAbs ("x",
adamc@179 329 dom,
adamc@179 330 ran,
adamc@179 331 (L'.ECase ((L'.ERel 0, loc),
adamc@179 332 branches,
adamc@182 333 {disc = dom,
adamc@182 334 result = ran}), loc)), loc),
adamc@179 335 "")], loc),
adamc@179 336 fm)
adamc@179 337 end
adamc@179 338
adamc@179 339 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 340 in
adamc@179 341 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 342 end
adamc@164 343
adamc@120 344 | _ => (E.errorAt loc "Don't know how to encode attribute type";
adamc@120 345 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 346 (dummyExp, fm))
adamc@120 347 in
adamc@120 348 fooify
adamc@120 349 end
adamc@120 350
adamc@179 351 val attrifyExp = fooifyExp Attr
adamc@179 352 val urlifyExp = fooifyExp Url
adamc@105 353
adamc@143 354 datatype 'a failable_search =
adamc@143 355 Found of 'a
adamc@143 356 | NotFound
adamc@143 357 | Error
adamc@143 358
adamc@153 359 structure St :> sig
adamc@153 360 type t
adamc@153 361
adamc@153 362 val empty : t
adamc@153 363
adamc@153 364 val radioGroup : t -> string option
adamc@153 365 val setRadioGroup : t * string -> t
adamc@153 366 end = struct
adamc@153 367
adamc@153 368 type t = {
adamc@153 369 radioGroup : string option
adamc@153 370 }
adamc@153 371
adamc@153 372 val empty = {radioGroup = NONE}
adamc@153 373
adamc@153 374 fun radioGroup (t : t) = #radioGroup t
adamc@153 375
adamc@153 376 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 377
adamc@153 378 end
adamc@153 379
adamc@186 380 fun monoPatCon env pc =
adamc@178 381 case pc of
adamc@178 382 L.PConVar n => L'.PConVar n
adamc@188 383 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 384 arg = Option.map (monoType env) arg}
adamc@178 385
adamc@193 386 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 387
adamc@193 388 fun monoPat env (all as (p, loc)) =
adamc@193 389 let
adamc@193 390 fun poly () =
adamc@193 391 (E.errorAt loc "Unsupported pattern";
adamc@193 392 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 393 dummyPat)
adamc@193 394 in
adamc@193 395 case p of
adamc@193 396 L.PWild => (L'.PWild, loc)
adamc@193 397 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 398 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 399 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@193 400 | L.PCon _ => poly ()
adamc@193 401 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 402 end
adamc@178 403
adamc@252 404 fun strcat loc es =
adamc@252 405 case es of
adamc@252 406 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 407 | [e] => e
adamc@252 408 | _ =>
adamc@252 409 let
adamc@252 410 val e2 = List.last es
adamc@252 411 val es = List.take (es, length es - 1)
adamc@252 412 val e1 = List.last es
adamc@252 413 val es = List.take (es, length es - 1)
adamc@252 414 in
adamc@252 415 foldr (fn (e, e') => (L'.EStrcat (e, e'), loc))
adamc@252 416 (L'.EStrcat (e1, e2), loc) es
adamc@252 417 end
adamc@252 418
adamc@252 419 fun strcatComma loc es =
adamc@252 420 case es of
adamc@252 421 [] => (L'.EPrim (Prim.String ""), loc)
adamc@252 422 | [e] => e
adamc@252 423 | _ =>
adamc@252 424 let
adamc@252 425 val e1 = List.last es
adamc@252 426 val es = List.take (es, length es - 1)
adamc@252 427 in
adamc@252 428 foldr (fn (e, e') =>
adamc@265 429 case (e, e') of
adamc@265 430 ((L'.EPrim (Prim.String ""), _), _) => e'
adamc@265 431 | (_, (L'.EPrim (Prim.String ""), _)) => e
adamc@252 432 | _ =>
adamc@252 433 (L'.EStrcat (e,
adamc@252 434 (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc))
adamc@252 435 e1 es
adamc@252 436 end
adamc@252 437
adamc@252 438 fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs)
adamc@252 439
adamc@179 440 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 441 let
adamc@25 442 fun poly () =
adamc@25 443 (E.errorAt loc "Unsupported expression";
adamc@25 444 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 445 (dummyExp, fm))
adamc@25 446 in
adamc@25 447 case e of
adamc@179 448 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 449 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 450 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 451 | L.ECon (dk, pc, [], eo) =>
adamc@193 452 let
adamc@179 453 val (eo, fm) =
adamc@179 454 case eo of
adamc@179 455 NONE => (NONE, fm)
adamc@179 456 | SOME e =>
adamc@179 457 let
adamc@179 458 val (e, fm) = monoExp (env, st, fm) e
adamc@179 459 in
adamc@179 460 (SOME e, fm)
adamc@179 461 end
adamc@179 462 in
adamc@188 463 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 464 end
adamc@193 465 | L.ECon _ => poly ()
adamc@94 466
adamc@286 467 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
adamc@286 468 let
adamc@286 469 val t = monoType env t
adamc@286 470 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 471 in
adamc@286 472 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@286 473 (L'.ERel 0, loc)), loc), fm)
adamc@286 474 end
adamc@286 475 | L.EFfi ("Basis", "show_int") =>
adamc@286 476 ((L'.EFfi ("Basis", "intToString"), loc), fm)
adamc@286 477 | L.EFfi ("Basis", "show_float") =>
adamc@286 478 ((L'.EFfi ("Basis", "floatToString"), loc), fm)
adamc@286 479 | L.EFfi ("Basis", "show_string") =>
adamc@286 480 let
adamc@286 481 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@286 482 in
adamc@286 483 ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
adamc@286 484 end
adamc@286 485 | L.EFfi ("Basis", "show_bool") =>
adamc@286 486 ((L'.EFfi ("Basis", "boolToString"), loc), fm)
adamc@286 487
adamc@251 488 | L.ECApp ((L.EFfi ("Basis", "return"), _), t) =>
adamc@252 489 let
adamc@252 490 val t = monoType env t
adamc@252 491 in
adamc@252 492 ((L'.EAbs ("x", t,
adamc@252 493 (L'.TFun ((L'.TRecord [], loc), t), loc),
adamc@252 494 (L'.EAbs ("_", (L'.TRecord [], loc), t,
adamc@252 495 (L'.ERel 1, loc)), loc)), loc), fm)
adamc@252 496 end
adamc@251 497 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), t1), _), t2) =>
adamc@251 498 let
adamc@251 499 val t1 = monoType env t1
adamc@251 500 val t2 = monoType env t2
adamc@251 501 val un = (L'.TRecord [], loc)
adamc@252 502 val mt1 = (L'.TFun (un, t1), loc)
adamc@252 503 val mt2 = (L'.TFun (un, t2), loc)
adamc@251 504 in
adamc@252 505 ((L'.EAbs ("m1", mt1, (L'.TFun (mt1, (L'.TFun (mt2, (L'.TFun (un, un), loc)), loc)), loc),
adamc@252 506 (L'.EAbs ("m2", mt2, (L'.TFun (un, un), loc),
adamc@252 507 (L'.EAbs ("_", un, un,
adamc@252 508 (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc),
adamc@252 509 (L'.ERecord [], loc)), loc),
adamc@252 510 (L'.EApp (
adamc@252 511 (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc),
adamc@252 512 (L'.ERecord [], loc)),
adamc@252 513 loc)), loc)), loc)), loc)), loc),
adamc@251 514 fm)
adamc@251 515 end
adamc@251 516
adamc@252 517 | L.ECApp (
adamc@252 518 (L.ECApp (
adamc@252 519 (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _),
adamc@252 520 exps), _),
adamc@252 521 state) =>
adamc@252 522 (case monoType env (L.TRecord exps, loc) of
adamc@252 523 (L'.TRecord exps, _) =>
adamc@252 524 let
adamc@252 525 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 526 (case monoType env (L.TRecord xts, loc) of
adamc@252 527 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 528 | _ => NONE)
adamc@252 529 | _ => NONE) tables
adamc@252 530 in
adamc@252 531 if List.exists (fn x => x = NONE) tables then
adamc@252 532 poly ()
adamc@252 533 else
adamc@252 534 let
adamc@252 535 val tables = List.mapPartial (fn x => x) tables
adamc@252 536 val state = monoType env state
adamc@252 537 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 538 val un = (L'.TRecord [], loc)
adamc@252 539
adamc@252 540 val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables
adamc@252 541 val ft = (L'.TFun ((L'.TRecord rt, loc),
adamc@252 542 (L'.TFun (state,
adamc@252 543 (L'.TFun (un, state), loc)),
adamc@252 544 loc)), loc)
adamc@252 545
adamc@267 546 val body' = (L'.EApp (
adamc@267 547 (L'.EApp (
adamc@267 548 (L'.EApp ((L'.ERel 4, loc),
adamc@267 549 (L'.ERel 1, loc)), loc),
adamc@267 550 (L'.ERel 0, loc)), loc),
adamc@267 551 (L'.ERecord [], loc)), loc)
adamc@252 552
adamc@252 553 val body = (L'.EQuery {exps = exps,
adamc@252 554 tables = tables,
adamc@252 555 state = state,
adamc@252 556 query = (L'.ERel 3, loc),
adamc@252 557 body = body',
adamc@252 558 initial = (L'.ERel 1, loc)},
adamc@252 559 loc)
adamc@252 560 in
adamc@252 561 ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc),
adamc@252 562 (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc),
adamc@252 563 (L'.EAbs ("i", state, (L'.TFun (un, state), loc),
adamc@252 564 (L'.EAbs ("_", un, state,
adamc@252 565 body), loc)), loc)), loc)), loc), fm)
adamc@252 566 end
adamc@252 567 end
adamc@252 568 | _ => poly ())
adamc@252 569
adamc@252 570 | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) =>
adamc@252 571 let
adamc@252 572 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 573 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 574 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 575 in
adamc@252 576 ((L'.EAbs ("r",
adamc@252 577 (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
adamc@252 578 s,
adamc@252 579 strcat loc [gf "Rows",
adamc@261 580 (L'.ECase (gf "OrderBy",
adamc@261 581 [((L'.PPrim (Prim.String ""), loc), sc ""),
adamc@261 582 ((L'.PWild, loc),
adamc@261 583 strcat loc [sc " ORDER BY ",
adamc@261 584 gf "OrderBy"])],
adamc@261 585 {disc = s, result = s}), loc),
adamc@252 586 gf "Limit",
adamc@252 587 gf "Offset"]), loc), fm)
adamc@252 588 end
adamc@252 589
adamc@252 590 | L.ECApp (
adamc@252 591 (L.ECApp (
adamc@252 592 (L.ECApp (
adamc@252 593 (L.ECApp (
adamc@252 594 (L.EFfi ("Basis", "sql_query1"), _),
adamc@252 595 (L.CRecord (_, tables), _)), _),
adamc@252 596 (L.CRecord (_, grouped), _)), _),
adamc@252 597 (L.CRecord (_, stables), _)), _),
adamc@252 598 sexps) =>
adamc@252 599 let
adamc@252 600 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@252 601 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 602 val un = (L'.TRecord [], loc)
adamc@252 603 fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc)
adamc@252 604
adamc@252 605 fun doTables tables =
adamc@252 606 let
adamc@252 607 val tables = map (fn ((L.CName x, _), xts) =>
adamc@252 608 (case monoType env (L.TRecord xts, loc) of
adamc@252 609 (L'.TRecord xts, _) => SOME (x, xts)
adamc@252 610 | _ => NONE)
adamc@252 611 | _ => NONE) tables
adamc@252 612 in
adamc@252 613 if List.exists (fn x => x = NONE) tables then
adamc@252 614 NONE
adamc@252 615 else
adamc@260 616 let
adamc@260 617 val tables = List.mapPartial (fn x => x) tables
adamc@260 618 val tables = ListMergeSort.sort
adamc@260 619 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 620 tables
adamc@260 621 val tables = map (fn (x, xts) =>
adamc@260 622 (x, ListMergeSort.sort
adamc@260 623 (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER)
adamc@260 624 xts)) tables
adamc@260 625 in
adamc@260 626 SOME tables
adamc@260 627 end
adamc@252 628 end
adamc@252 629 in
adamc@252 630 case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of
adamc@252 631 (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) =>
adamc@252 632 ((L'.EAbs ("r",
adamc@252 633 (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)),
adamc@252 634 ("Where", s),
adamc@252 635 ("GroupBy", un),
adamc@252 636 ("Having", s),
adamc@252 637 ("SelectFields", un),
adamc@252 638 ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))],
adamc@252 639 loc),
adamc@252 640 s,
adamc@252 641 strcat loc [sc "SELECT ",
adamc@261 642 strcatComma loc (map (fn (x, t) =>
adamc@261 643 strcat loc [
adamc@261 644 (L'.EField (gf "SelectExps", x), loc),
adamc@261 645 sc (" AS _" ^ x)
adamc@265 646 ]) sexps
adamc@265 647 @ map (fn (x, xts) =>
adamc@265 648 strcatComma loc
adamc@265 649 (map (fn (x', _) =>
adamc@277 650 sc (x ^ ".lw_" ^ x'))
adamc@265 651 xts)) stables),
adamc@252 652 sc " FROM ",
adamc@252 653 strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
adamc@253 654 sc (" AS " ^ x)]) tables),
adamc@258 655 (L'.ECase (gf "Where",
adamc@258 656 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@258 657 sc ""),
adamc@258 658 ((L'.PWild, loc),
adamc@258 659 strcat loc [sc " WHERE ", gf "Where"])],
adamc@258 660 {disc = s,
adamc@258 661 result = s}), loc),
adamc@258 662
adamc@255 663 if List.all (fn (x, xts) =>
adamc@255 664 case List.find (fn (x', _) => x' = x) grouped of
adamc@255 665 NONE => List.null xts
adamc@255 666 | SOME (_, xts') =>
adamc@255 667 List.all (fn (x, _) =>
adamc@255 668 List.exists (fn (x', _) => x' = x)
adamc@255 669 xts') xts) tables then
adamc@255 670 sc ""
adamc@255 671 else
adamc@255 672 strcat loc [
adamc@255 673 sc " GROUP BY ",
adamc@255 674 strcatComma loc (map (fn (x, xts) =>
adamc@255 675 strcatComma loc
adamc@255 676 (map (fn (x', _) =>
adamc@277 677 sc (x ^ ".lw_" ^ x'))
adamc@255 678 xts)) grouped)
adamc@259 679 ],
adamc@259 680
adamc@259 681 (L'.ECase (gf "Having",
adamc@259 682 [((L'.PPrim (Prim.String "TRUE"), loc),
adamc@259 683 sc ""),
adamc@259 684 ((L'.PWild, loc),
adamc@259 685 strcat loc [sc " HAVING ", gf "Having"])],
adamc@259 686 {disc = s,
adamc@259 687 result = s}), loc)
adamc@252 688 ]), loc),
adamc@252 689 fm)
adamc@252 690 | _ => poly ()
adamc@252 691 end
adamc@252 692
adamc@252 693 | L.ECApp (
adamc@252 694 (L.ECApp (
adamc@252 695 (L.ECApp (
adamc@252 696 (L.ECApp (
adamc@252 697 (L.EFfi ("Basis", "sql_inject"), _),
adamc@252 698 _), _),
adamc@252 699 _), _),
adamc@252 700 _), _),
adamc@252 701 t) =>
adamc@252 702 let
adamc@252 703 val t = monoType env t
adamc@252 704 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@252 705 in
adamc@252 706 ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
adamc@252 707 (L'.ERel 0, loc)), loc), fm)
adamc@252 708 end
adamc@252 709
adamc@253 710 | L.EFfi ("Basis", "sql_int") =>
adamc@253 711 ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 712 (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 713 fm)
adamc@253 714 | L.EFfi ("Basis", "sql_float") =>
adamc@253 715 ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 716 (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 717 fm)
adamc@253 718 | L.EFfi ("Basis", "sql_bool") =>
adamc@253 719 ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 720 (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 721 fm)
adamc@253 722 | L.EFfi ("Basis", "sql_string") =>
adamc@253 723 ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc),
adamc@253 724 (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc),
adamc@253 725 fm)
adamc@253 726
adamc@252 727 | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) =>
adamc@252 728 ((L'.ERecord [], loc), fm)
adamc@252 729 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
adamc@252 730 ((L'.ERecord [], loc), fm)
adamc@252 731
adamc@252 732 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
adamc@252 733 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@261 734 | L.ECApp (
adamc@261 735 (L.ECApp (
adamc@261 736 (L.ECApp (
adamc@261 737 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
adamc@261 738 _), _),
adamc@261 739 _), _),
adamc@261 740 _) =>
adamc@261 741 let
adamc@261 742 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@261 743 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@261 744 in
adamc@268 745 ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@268 746 (L'.EAbs ("d", s, (L'.TFun (s, s), loc),
adamc@268 747 (L'.EAbs ("e2", s, s,
adamc@268 748 (L'.ECase ((L'.ERel 0, loc),
adamc@268 749 [((L'.PPrim (Prim.String ""), loc),
adamc@268 750 strcat loc [(L'.ERel 2, loc),
adamc@268 751 (L'.ERel 1, loc)]),
adamc@268 752 ((L'.PWild, loc),
adamc@268 753 strcat loc [(L'.ERel 2, loc),
adamc@268 754 (L'.ERel 1, loc),
adamc@268 755 sc ", ",
adamc@268 756 (L'.ERel 0, loc)])],
adamc@268 757 {disc = s, result = s}), loc)), loc)), loc)), loc),
adamc@261 758 fm)
adamc@261 759 end
adamc@252 760
adamc@252 761 | L.EFfi ("Basis", "sql_no_limit") =>
adamc@252 762 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@262 763 | L.EFfiApp ("Basis", "sql_limit", [e]) =>
adamc@262 764 let
adamc@262 765 val (e, fm) = monoExp (env, st, fm) e
adamc@262 766 in
adamc@262 767 (strcat loc [
adamc@262 768 (L'.EPrim (Prim.String " LIMIT "), loc),
adamc@262 769 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@262 770 ],
adamc@262 771 fm)
adamc@262 772 end
adamc@262 773
adamc@252 774 | L.EFfi ("Basis", "sql_no_offset") =>
adamc@252 775 ((L'.EPrim (Prim.String ""), loc), fm)
adamc@263 776 | L.EFfiApp ("Basis", "sql_offset", [e]) =>
adamc@263 777 let
adamc@263 778 val (e, fm) = monoExp (env, st, fm) e
adamc@263 779 in
adamc@263 780 (strcat loc [
adamc@263 781 (L'.EPrim (Prim.String " OFFSET "), loc),
adamc@263 782 (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc)
adamc@263 783 ],
adamc@263 784 fm)
adamc@263 785 end
adamc@253 786
adamc@253 787 | L.EFfi ("Basis", "sql_eq") =>
adamc@253 788 ((L'.EPrim (Prim.String "="), loc), fm)
adamc@253 789 | L.EFfi ("Basis", "sql_ne") =>
adamc@253 790 ((L'.EPrim (Prim.String "<>"), loc), fm)
adamc@253 791 | L.EFfi ("Basis", "sql_lt") =>
adamc@253 792 ((L'.EPrim (Prim.String "<"), loc), fm)
adamc@253 793 | L.EFfi ("Basis", "sql_le") =>
adamc@253 794 ((L'.EPrim (Prim.String "<="), loc), fm)
adamc@253 795 | L.EFfi ("Basis", "sql_gt") =>
adamc@253 796 ((L'.EPrim (Prim.String ">"), loc), fm)
adamc@253 797 | L.EFfi ("Basis", "sql_ge") =>
adamc@253 798 ((L'.EPrim (Prim.String ">="), loc), fm)
adamc@253 799
adamc@253 800 | L.ECApp (
adamc@253 801 (L.ECApp (
adamc@253 802 (L.ECApp (
adamc@253 803 (L.ECApp (
adamc@254 804 (L.ECApp (
adamc@264 805 (L.EFfi ("Basis", "sql_unary"), _),
adamc@264 806 _), _),
adamc@264 807 _), _),
adamc@264 808 _), _),
adamc@264 809 _), _),
adamc@264 810 _) =>
adamc@264 811 let
adamc@264 812 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@264 813 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@264 814 in
adamc@264 815 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@264 816 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@264 817 strcat loc [sc "(",
adamc@264 818 (L'.ERel 1, loc),
adamc@264 819 sc " ",
adamc@264 820 (L'.ERel 0, loc),
adamc@264 821 sc ")"]), loc)), loc),
adamc@264 822 fm)
adamc@264 823 end
adamc@264 824 | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm)
adamc@264 825
adamc@264 826 | L.ECApp (
adamc@264 827 (L.ECApp (
adamc@264 828 (L.ECApp (
adamc@264 829 (L.ECApp (
adamc@264 830 (L.ECApp (
adamc@254 831 (L.ECApp (
adamc@254 832 (L.EFfi ("Basis", "sql_binary"), _),
adamc@254 833 _), _),
adamc@254 834 _), _),
adamc@254 835 _), _),
adamc@254 836 _), _),
adamc@254 837 _), _),
adamc@254 838 _) =>
adamc@254 839 let
adamc@254 840 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@254 841 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@254 842 in
adamc@254 843 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@254 844 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@254 845 (L'.EAbs ("e2", s, s,
adamc@254 846 strcat loc [sc "(",
adamc@254 847 (L'.ERel 1, loc),
adamc@254 848 sc " ",
adamc@254 849 (L'.ERel 2, loc),
adamc@254 850 sc " ",
adamc@254 851 (L'.ERel 0, loc),
adamc@254 852 sc ")"]), loc)), loc)), loc),
adamc@254 853 fm)
adamc@254 854 end
adamc@254 855 | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm)
adamc@254 856 | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm)
adamc@254 857
adamc@254 858 | L.ECApp (
adamc@254 859 (L.ECApp (
adamc@254 860 (L.ECApp (
adamc@254 861 (L.ECApp (
adamc@253 862 (L.EFfi ("Basis", "sql_comparison"), _),
adamc@253 863 _), _),
adamc@253 864 _), _),
adamc@253 865 _), _),
adamc@253 866 _) =>
adamc@253 867 let
adamc@253 868 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@253 869 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@253 870 in
adamc@253 871 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@253 872 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@253 873 (L'.EAbs ("e2", s, s,
adamc@254 874 strcat loc [sc "(",
adamc@254 875 (L'.ERel 1, loc),
adamc@253 876 sc " ",
adamc@253 877 (L'.ERel 2, loc),
adamc@253 878 sc " ",
adamc@254 879 (L'.ERel 0, loc),
adamc@254 880 sc ")"]), loc)), loc)), loc),
adamc@253 881 fm)
adamc@253 882 end
adamc@253 883
adamc@253 884 | L.ECApp (
adamc@253 885 (L.ECApp (
adamc@253 886 (L.ECApp (
adamc@253 887 (L.ECApp (
adamc@253 888 (L.ECApp (
adamc@253 889 (L.ECApp (
adamc@253 890 (L.ECApp (
adamc@253 891 (L.EFfi ("Basis", "sql_field"), _),
adamc@253 892 _), _),
adamc@253 893 _), _),
adamc@253 894 _), _),
adamc@253 895 _), _),
adamc@253 896 _), _),
adamc@253 897 (L.CName tab, _)), _),
adamc@277 898 (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".lw_" ^ field)), loc), fm)
adamc@260 899
adamc@260 900 | L.ECApp (
adamc@260 901 (L.ECApp (
adamc@260 902 (L.ECApp (
adamc@260 903 (L.ECApp (
adamc@261 904 (L.ECApp (
adamc@261 905 (L.EFfi ("Basis", "sql_exp"), _),
adamc@261 906 _), _),
adamc@261 907 _), _),
adamc@261 908 _), _),
adamc@261 909 _), _),
adamc@261 910 (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
adamc@261 911
adamc@261 912 | L.ECApp (
adamc@261 913 (L.ECApp (
adamc@261 914 (L.ECApp (
adamc@261 915 (L.ECApp (
adamc@260 916 (L.EFfi ("Basis", "sql_relop"), _),
adamc@260 917 _), _),
adamc@260 918 _), _),
adamc@260 919 _), _),
adamc@260 920 _) =>
adamc@260 921 let
adamc@260 922 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@260 923 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@260 924 in
adamc@260 925 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@260 926 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@260 927 (L'.EAbs ("e2", s, s,
adamc@260 928 strcat loc [sc "((",
adamc@260 929 (L'.ERel 1, loc),
adamc@260 930 sc ") ",
adamc@260 931 (L'.ERel 2, loc),
adamc@260 932 sc " (",
adamc@260 933 (L'.ERel 0, loc),
adamc@260 934 sc "))"]), loc)), loc)), loc),
adamc@260 935 fm)
adamc@260 936 end
adamc@260 937
adamc@260 938 | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm)
adamc@260 939 | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm)
adamc@260 940 | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm)
adamc@260 941
adamc@265 942 | L.ECApp (
adamc@265 943 (L.ECApp (
adamc@265 944 (L.ECApp (
adamc@265 945 (L.EFfi ("Basis", "sql_count"), _),
adamc@265 946 _), _),
adamc@265 947 _), _),
adamc@265 948 _) => ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@265 949 (L'.EPrim (Prim.String "COUNT(*)"), loc)), loc),
adamc@265 950 fm)
adamc@266 951
adamc@266 952 | L.ECApp (
adamc@266 953 (L.ECApp (
adamc@266 954 (L.ECApp (
adamc@266 955 (L.ECApp (
adamc@266 956 (L.EFfi ("Basis", "sql_aggregate"), _),
adamc@266 957 _), _),
adamc@266 958 _), _),
adamc@266 959 _), _),
adamc@266 960 _) =>
adamc@266 961 let
adamc@266 962 val s = (L'.TFfi ("Basis", "string"), loc)
adamc@266 963 fun sc s = (L'.EPrim (Prim.String s), loc)
adamc@266 964 in
adamc@266 965 ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc),
adamc@266 966 (L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
adamc@266 967 strcat loc [(L'.ERel 1, loc),
adamc@266 968 sc "(",
adamc@266 969 (L'.ERel 0, loc),
adamc@266 970 sc ")"]), loc)), loc),
adamc@266 971 fm)
adamc@266 972 end
adamc@266 973
adamc@266 974 | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm)
adamc@266 975 | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm)
adamc@266 976 | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) =>
adamc@266 977 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 978 (L'.EPrim (Prim.String "AVG"), loc)), loc),
adamc@266 979 fm)
adamc@266 980 | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) =>
adamc@266 981 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 982 (L'.EPrim (Prim.String "SUM"), loc)), loc),
adamc@266 983 fm)
adamc@266 984
adamc@266 985 | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm)
adamc@266 986 | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm)
adamc@266 987 | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm)
adamc@266 988 | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) =>
adamc@266 989 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 990 (L'.EPrim (Prim.String "MAX"), loc)), loc),
adamc@266 991 fm)
adamc@266 992 | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) =>
adamc@266 993 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc),
adamc@266 994 (L'.EPrim (Prim.String "MIN"), loc)), loc),
adamc@266 995 fm)
adamc@266 996
adamc@268 997 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@268 998 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
adamc@268 999
adamc@139 1000 | L.EApp (
adamc@139 1001 (L.ECApp (
adamc@141 1002 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 1003 _), _),
adamc@179 1004 se) =>
adamc@179 1005 let
adamc@179 1006 val (se, fm) = monoExp (env, st, fm) se
adamc@179 1007 in
adamc@179 1008 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 1009 end
adamc@179 1010
adamc@95 1011 | L.EApp (
adamc@95 1012 (L.EApp (
adamc@95 1013 (L.ECApp (
adamc@95 1014 (L.ECApp (
adamc@95 1015 (L.ECApp (
adamc@139 1016 (L.ECApp (
adamc@140 1017 (L.EFfi ("Basis", "join"),
adamc@139 1018 _), _), _),
adamc@139 1019 _), _),
adamc@95 1020 _), _),
adamc@95 1021 _), _),
adamc@95 1022 xml1), _),
adamc@179 1023 xml2) =>
adamc@179 1024 let
adamc@179 1025 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 1026 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 1027 in
adamc@179 1028 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 1029 end
adamc@95 1030
adamc@95 1031 | L.EApp (
adamc@95 1032 (L.EApp (
adamc@104 1033 (L.EApp (
adamc@95 1034 (L.ECApp (
adamc@104 1035 (L.ECApp (
adamc@104 1036 (L.ECApp (
adamc@104 1037 (L.ECApp (
adamc@139 1038 (L.ECApp (
adamc@139 1039 (L.ECApp (
adamc@139 1040 (L.ECApp (
adamc@139 1041 (L.ECApp (
adamc@139 1042 (L.EFfi ("Basis", "tag"),
adamc@139 1043 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 1044 attrs), _),
adamc@95 1045 tag), _),
adamc@95 1046 xml) =>
adamc@95 1047 let
adamc@140 1048 fun getTag' (e, _) =
adamc@140 1049 case e of
adamc@143 1050 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 1051 | L.ECApp (e, t) => let
adamc@143 1052 val (tag, ts) = getTag' e
adamc@143 1053 in
adamc@143 1054 (tag, ts @ [t])
adamc@143 1055 end
adamc@140 1056 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 1057 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1058 ("", []))
adamc@140 1059
adamc@95 1060 fun getTag (e, _) =
adamc@95 1061 case e of
adamc@143 1062 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 1063 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 1064 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 1065 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 1066 ("", []))
adamc@95 1067
adamc@143 1068 val (tag, targs) = getTag tag
adamc@95 1069
adamc@179 1070 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 1071
adamc@143 1072 fun tagStart tag =
adamc@104 1073 case #1 attrs of
adamc@104 1074 L'.ERecord xes =>
adamc@104 1075 let
adamc@104 1076 fun lowercaseFirst "" = ""
adamc@143 1077 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 1078 ^ String.extract (s, 1, NONE)
adamc@104 1079
adamc@104 1080 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 1081 in
adamc@179 1082 foldl (fn ((x, e, t), (s, fm)) =>
adamc@104 1083 let
adamc@104 1084 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@120 1085
adamc@120 1086 val fooify =
adamc@120 1087 case x of
adamc@185 1088 "Href" => urlifyExp
adamc@185 1089 | "Link" => urlifyExp
adamc@143 1090 | "Action" => urlifyExp
adamc@120 1091 | _ => attrifyExp
adamc@179 1092
adamc@179 1093 val (e, fm) = fooify env fm (e, t)
adamc@104 1094 in
adamc@179 1095 ((L'.EStrcat (s,
adamc@179 1096 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@179 1097 (L'.EStrcat (e,
adamc@179 1098 (L'.EPrim (Prim.String "\""),
adamc@179 1099 loc)),
adamc@179 1100 loc)),
adamc@179 1101 loc)), loc),
adamc@179 1102 fm)
adamc@104 1103 end)
adamc@179 1104 (s, fm) xes
adamc@104 1105 end
adamc@143 1106 | _ => raise Fail "Non-record attributes!"
adamc@104 1107
adamc@143 1108 fun input typ =
adamc@143 1109 case targs of
adamc@155 1110 [_, (L.CName name, _)] =>
adamc@179 1111 let
adamc@179 1112 val (ts, fm) = tagStart "input"
adamc@179 1113 in
adamc@179 1114 ((L'.EStrcat (ts,
adamc@179 1115 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 1116 loc)), loc), fm)
adamc@179 1117 end
adamc@143 1118 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1119 raise Fail "No name passed to input tag")
adamc@104 1120
adamc@152 1121 fun normal (tag, extra) =
adamc@143 1122 let
adamc@179 1123 val (tagStart, fm) = tagStart tag
adamc@152 1124 val tagStart = case extra of
adamc@152 1125 NONE => tagStart
adamc@152 1126 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 1127
adamc@143 1128 fun normal () =
adamc@179 1129 let
adamc@179 1130 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1131 in
adamc@179 1132 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 1133 (L'.EStrcat (xml,
adamc@179 1134 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 1135 loc)), loc)),
adamc@179 1136 loc),
adamc@179 1137 fm)
adamc@179 1138 end
adamc@143 1139 in
adamc@143 1140 case xml of
adamc@143 1141 (L.EApp ((L.ECApp (
adamc@143 1142 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 1143 _), _),
adamc@143 1144 _), _),
adamc@143 1145 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 1146 if CharVector.all Char.isSpace s then
adamc@179 1147 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 1148 else
adamc@143 1149 normal ()
adamc@143 1150 | _ => normal ()
adamc@143 1151 end
adamc@152 1152 in
adamc@152 1153 case tag of
adamc@179 1154 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
adamc@152 1155
adamc@152 1156 | "textbox" =>
adamc@152 1157 (case targs of
adamc@152 1158 [_, (L.CName name, _)] =>
adamc@179 1159 let
adamc@179 1160 val (ts, fm) = tagStart "input"
adamc@179 1161 in
adamc@179 1162 ((L'.EStrcat (ts,
adamc@179 1163 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 1164 loc)), loc), fm)
adamc@179 1165 end
adamc@152 1166 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1167 raise Fail "No name passed to textarea tag"))
adamc@155 1168 | "password" => input "password"
adamc@152 1169 | "ltextarea" =>
adamc@152 1170 (case targs of
adamc@152 1171 [_, (L.CName name, _)] =>
adamc@179 1172 let
adamc@179 1173 val (ts, fm) = tagStart "textarea"
adamc@179 1174 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1175 in
adamc@179 1176 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1177 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1178 (L'.EStrcat (xml,
adamc@179 1179 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 1180 loc)), loc)),
adamc@179 1181 loc), fm)
adamc@179 1182 end
adamc@152 1183 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1184 raise Fail "No name passed to ltextarea tag"))
adamc@153 1185
adamc@190 1186 | "checkbox" => input "checkbox"
adamc@190 1187
adamc@153 1188 | "radio" =>
adamc@153 1189 (case targs of
adamc@153 1190 [_, (L.CName name, _)] =>
adamc@179 1191 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 1192 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 1193 raise Fail "No name passed to radio tag"))
adamc@153 1194 | "radioOption" =>
adamc@153 1195 (case St.radioGroup st of
adamc@153 1196 NONE => raise Fail "No name for radioGroup"
adamc@153 1197 | SOME name =>
adamc@153 1198 normal ("input",
adamc@153 1199 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 1200
adamc@154 1201 | "lselect" =>
adamc@154 1202 (case targs of
adamc@154 1203 [_, (L.CName name, _)] =>
adamc@179 1204 let
adamc@179 1205 val (ts, fm) = tagStart "select"
adamc@179 1206 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 1207 in
adamc@179 1208 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 1209 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 1210 (L'.EStrcat (xml,
adamc@179 1211 (L'.EPrim (Prim.String "</select>"),
adamc@179 1212 loc)), loc)),
adamc@179 1213 loc),
adamc@179 1214 fm)
adamc@179 1215 end
adamc@154 1216 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 1217 raise Fail "No name passed to lselect tag"))
adamc@154 1218
adamc@154 1219 | "loption" => normal ("option", NONE)
adamc@154 1220
adamc@152 1221 | _ => normal (tag, NONE)
adamc@95 1222 end
adamc@94 1223
adamc@141 1224 | L.EApp ((L.ECApp (
adamc@141 1225 (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _),
adamc@141 1226 _), _),
adamc@141 1227 xml) =>
adamc@143 1228 let
adamc@143 1229 fun findSubmit (e, _) =
adamc@143 1230 case e of
adamc@143 1231 L.EApp (
adamc@143 1232 (L.EApp (
adamc@143 1233 (L.ECApp (
adamc@143 1234 (L.ECApp (
adamc@143 1235 (L.ECApp (
adamc@143 1236 (L.ECApp (
adamc@143 1237 (L.EFfi ("Basis", "join"),
adamc@143 1238 _), _), _),
adamc@143 1239 _), _),
adamc@143 1240 _), _),
adamc@143 1241 _), _),
adamc@143 1242 xml1), _),
adamc@143 1243 xml2) => (case findSubmit xml1 of
adamc@143 1244 Error => Error
adamc@143 1245 | NotFound => findSubmit xml2
adamc@143 1246 | Found e =>
adamc@143 1247 case findSubmit xml2 of
adamc@143 1248 NotFound => Found e
adamc@143 1249 | _ => Error)
adamc@143 1250 | L.EApp (
adamc@143 1251 (L.EApp (
adamc@143 1252 (L.EApp (
adamc@143 1253 (L.ECApp (
adamc@143 1254 (L.ECApp (
adamc@143 1255 (L.ECApp (
adamc@143 1256 (L.ECApp (
adamc@143 1257 (L.ECApp (
adamc@143 1258 (L.ECApp (
adamc@143 1259 (L.ECApp (
adamc@143 1260 (L.ECApp (
adamc@143 1261 (L.EFfi ("Basis", "tag"),
adamc@143 1262 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 1263 attrs), _),
adamc@143 1264 _), _),
adamc@143 1265 xml) =>
adamc@143 1266 (case #1 attrs of
adamc@143 1267 L.ERecord xes =>
adamc@143 1268 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 1269 | _ => NONE) xes of
adamc@143 1270 NONE => findSubmit xml
adamc@143 1271 | SOME et =>
adamc@143 1272 case findSubmit xml of
adamc@143 1273 NotFound => Found et
adamc@143 1274 | _ => Error)
adamc@143 1275 | _ => findSubmit xml)
adamc@143 1276 | _ => NotFound
adamc@143 1277
adamc@143 1278 val (action, actionT) = case findSubmit xml of
adamc@143 1279 NotFound => raise Fail "No submit found"
adamc@143 1280 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 1281 | Found et => et
adamc@143 1282
adamc@143 1283 val actionT = monoType env actionT
adamc@179 1284 val (action, fm) = monoExp (env, st, fm) action
adamc@179 1285 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 1286 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 1287 in
adamc@179 1288 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 1289 (L'.EStrcat (action,
adamc@179 1290 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 1291 (L'.EStrcat (xml,
adamc@179 1292 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 1293 fm)
adamc@143 1294 end
adamc@141 1295
adamc@148 1296 | L.EApp ((L.ECApp (
adamc@148 1297 (L.ECApp (
adamc@148 1298 (L.ECApp (
adamc@148 1299 (L.ECApp (
adamc@148 1300 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 1301 _), _),
adamc@148 1302 _), _),
adamc@148 1303 _), _),
adamc@179 1304 xml) => monoExp (env, st, fm) xml
adamc@148 1305
adamc@283 1306 | L.ECApp ((L.EFfi ("Basis", "error"), _), t) =>
adamc@283 1307 let
adamc@283 1308 val t = monoType env t
adamc@283 1309 in
adamc@283 1310 ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t,
adamc@283 1311 (L'.EError ((L'.ERel 0, loc), t), loc)), loc),
adamc@283 1312 fm)
adamc@283 1313 end
adamc@283 1314
adamc@179 1315 | L.EApp (e1, e2) =>
adamc@179 1316 let
adamc@179 1317 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 1318 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 1319 in
adamc@179 1320 ((L'.EApp (e1, e2), loc), fm)
adamc@179 1321 end
adamc@26 1322 | L.EAbs (x, dom, ran, e) =>
adamc@179 1323 let
adamc@179 1324 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 1325 in
adamc@179 1326 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 1327 end
adamc@25 1328 | L.ECApp _ => poly ()
adamc@25 1329 | L.ECAbs _ => poly ()
adamc@25 1330
adamc@252 1331 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@252 1332 | L.EFfiApp (m, x, es) =>
adamc@252 1333 let
adamc@252 1334 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@252 1335 in
adamc@252 1336 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@252 1337 end
adamc@252 1338
adamc@179 1339 | L.ERecord xes =>
adamc@179 1340 let
adamc@179 1341 val (xes, fm) = ListUtil.foldlMap
adamc@179 1342 (fn ((x, e, t), fm) =>
adamc@179 1343 let
adamc@179 1344 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1345 in
adamc@179 1346 ((monoName env x,
adamc@179 1347 e,
adamc@179 1348 monoType env t), fm)
adamc@179 1349 end) fm xes
adamc@179 1350 in
adamc@179 1351 ((L'.ERecord xes, loc), fm)
adamc@179 1352 end
adamc@179 1353 | L.EField (e, x, _) =>
adamc@179 1354 let
adamc@179 1355 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1356 in
adamc@179 1357 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 1358 end
adamc@149 1359 | L.ECut _ => poly ()
adamc@73 1360 | L.EFold _ => poly ()
adamc@177 1361
adamc@182 1362 | L.ECase (e, pes, {disc, result}) =>
adamc@179 1363 let
adamc@179 1364 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1365 val (pes, fm) = ListUtil.foldlMap
adamc@179 1366 (fn ((p, e), fm) =>
adamc@179 1367 let
adamc@179 1368 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1369 in
adamc@182 1370 ((monoPat env p, e), fm)
adamc@179 1371 end) fm pes
adamc@179 1372 in
adamc@182 1373 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 1374 end
adamc@177 1375
adamc@179 1376 | L.EWrite e =>
adamc@179 1377 let
adamc@179 1378 val (e, fm) = monoExp (env, st, fm) e
adamc@179 1379 in
adamc@252 1380 ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc),
adamc@252 1381 (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm)
adamc@179 1382 end
adamc@110 1383
adamc@179 1384 | L.EClosure (n, es) =>
adamc@179 1385 let
adamc@179 1386 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 1387 monoExp (env, st, fm) e)
adamc@179 1388 fm es
adamc@179 1389 in
adamc@179 1390 ((L'.EClosure (n, es), loc), fm)
adamc@179 1391 end
adamc@25 1392 end
adamc@25 1393
adamc@179 1394 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 1395 let
adamc@25 1396 fun poly () =
adamc@25 1397 (E.errorAt loc "Unsupported declaration";
adamc@25 1398 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 1399 NONE)
adamc@25 1400 in
adamc@25 1401 case d of
adamc@25 1402 L.DCon _ => NONE
adamc@193 1403 | L.DDatatype (x, n, [], xncs) =>
adamc@193 1404 let
adamc@196 1405 val env' = Env.declBinds env all
adamc@196 1406 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 1407 in
adamc@273 1408 SOME (env', fm, [d])
adamc@193 1409 end
adamc@193 1410 | L.DDatatype _ => poly ()
adamc@179 1411 | L.DVal (x, n, t, e, s) =>
adamc@179 1412 let
adamc@179 1413 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 1414 in
adamc@179 1415 SOME (Env.pushENamed env x n t NONE s,
adamc@179 1416 fm,
adamc@273 1417 [(L'.DVal (x, n, monoType env t, e, s), loc)])
adamc@179 1418 end
adamc@128 1419 | L.DValRec vis =>
adamc@128 1420 let
adamc@128 1421 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 1422
adamc@179 1423 val (vis, fm) = ListUtil.foldlMap
adamc@179 1424 (fn ((x, n, t, e, s), fm) =>
adamc@179 1425 let
adamc@179 1426 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 1427 in
adamc@179 1428 ((x, n, monoType env t, e, s), fm)
adamc@179 1429 end)
adamc@179 1430 fm vis
adamc@128 1431 in
adamc@128 1432 SOME (env,
adamc@179 1433 fm,
adamc@273 1434 [(L'.DValRec vis, loc)])
adamc@128 1435 end
adamc@144 1436 | L.DExport (ek, n) =>
adamc@115 1437 let
adamc@120 1438 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 1439
adamc@120 1440 fun unwind (t, _) =
adamc@120 1441 case t of
adamc@120 1442 L.TFun (dom, ran) => dom :: unwind ran
adamc@120 1443 | _ => []
adamc@120 1444
adamc@120 1445 val ts = map (monoType env) (unwind t)
adamc@115 1446 in
adamc@273 1447 SOME (env, fm, [(L'.DExport (ek, s, n, ts), loc)])
adamc@115 1448 end
adamc@273 1449 | L.DTable (x, n, (L.CRecord (_, xts), _), s) =>
adamc@251 1450 let
adamc@251 1451 val t = (L.CFfi ("Basis", "string"), loc)
adamc@251 1452 val t' = (L'.TFfi ("Basis", "string"), loc)
adamc@274 1453 val s = "lw_" ^ s
adamc@251 1454 val e = (L'.EPrim (Prim.String s), loc)
adamc@273 1455
adamc@273 1456 val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts
adamc@251 1457 in
adamc@251 1458 SOME (Env.pushENamed env x n t NONE s,
adamc@251 1459 fm,
adamc@273 1460 [(L'.DTable (s, xts), loc),
adamc@273 1461 (L'.DVal (x, n, t', e, s), loc)])
adamc@251 1462 end
adamc@273 1463 | L.DTable _ => poly ()
adamc@273 1464 | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)])
adamc@25 1465 end
adamc@25 1466
adamc@25 1467 fun monoize env ds =
adamc@25 1468 let
adamc@179 1469 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 1470 case monoDecl (env, fm) d of
adamc@179 1471 NONE => (env, fm, ds)
adamc@273 1472 | SOME (env, fm, ds') =>
adamc@179 1473 (env,
adamc@179 1474 Fm.enter fm,
adamc@273 1475 ds' @ Fm.decls fm @ ds))
adamc@179 1476 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 1477 in
adamc@25 1478 rev ds
adamc@25 1479 end
adamc@25 1480
adamc@25 1481 end