annotate src/monoize.sml @ 392:a813476230e3

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