annotate src/monoize.sml @ 467:3f1b9231a37b

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