annotate src/monoize.sml @ 288:4260ad920c36

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