annotate src/monoize.sml @ 292:6e665c7c96f6

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