annotate src/monoize.sml @ 279:8bb46d87b074

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