annotate src/monoize.sml @ 241:052126db06e7

Shorthand for multi-binding exp 'fn'
author Adam Chlipala <adamc@hcoop.net>
date Thu, 28 Aug 2008 13:57:12 -0400
parents 766b5475477f
children 5c50b17f5e4a
rev   line source
adamc@25 1 (* Copyright (c) 2008, Adam Chlipala
adamc@25 2 * All rights reserved.
adamc@25 3 *
adamc@25 4 * Redistribution and use in source and binary forms, with or without
adamc@25 5 * modification, are permitted provided that the following conditions are met:
adamc@25 6 *
adamc@25 7 * - Redistributions of source code must retain the above copyright notice,
adamc@25 8 * this list of conditions and the following disclaimer.
adamc@25 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@25 10 * this list of conditions and the following disclaimer in the documentation
adamc@25 11 * and/or other materials provided with the distribution.
adamc@25 12 * - The names of contributors may not be used to endorse or promote products
adamc@25 13 * derived from this software without specific prior written permission.
adamc@25 14 *
adamc@25 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@25 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@25 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@25 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@25 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@25 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@25 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@25 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@25 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@25 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@25 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@25 26 *)
adamc@25 27
adamc@25 28 structure Monoize :> MONOIZE = struct
adamc@25 29
adamc@25 30 structure E = ErrorMsg
adamc@25 31 structure Env = CoreEnv
adamc@25 32
adamc@25 33 structure L = Core
adamc@25 34 structure L' = Mono
adamc@25 35
adamc@196 36 structure IM = IntBinaryMap
adamc@196 37
adamc@196 38 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
adamc@25 39
adamc@25 40 fun monoName env (all as (c, loc)) =
adamc@25 41 let
adamc@25 42 fun poly () =
adamc@25 43 (E.errorAt loc "Unsupported name constructor";
adamc@25 44 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 45 "")
adamc@25 46 in
adamc@25 47 case c of
adamc@25 48 L.CName s => s
adamc@25 49 | _ => poly ()
adamc@25 50 end
adamc@25 51
adamc@196 52 fun monoType env =
adamc@25 53 let
adamc@196 54 fun mt env dtmap (all as (c, loc)) =
adamc@196 55 let
adamc@196 56 fun poly () =
adamc@196 57 (E.errorAt loc "Unsupported type constructor";
adamc@196 58 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 59 dummyTyp)
adamc@196 60 in
adamc@196 61 case c of
adamc@196 62 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 63 | L.TCFun _ => poly ()
adamc@196 64 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@196 65 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
adamc@196 66 | L.TRecord _ => poly ()
adamc@196 67
adamc@196 68 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 69 (L'.TFfi ("Basis", "string"), loc)
adamc@196 70 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 71 (L'.TFfi ("Basis", "string"), loc)
adamc@196 72
adamc@196 73 | L.CRel _ => poly ()
adamc@196 74 | L.CNamed n =>
adamc@196 75 (case IM.find (dtmap, n) of
adamc@196 76 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 77 | NONE =>
adamc@196 78 let
adamc@196 79 val r = ref (L'.Default, [])
adamc@196 80 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 81
adamc@196 82 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 83
adamc@196 84 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 85 in
adamc@196 86 case xs of
adamc@198 87 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 88 (L'.TDatatype (n, r), loc))
adamc@196 89 | _ => poly ()
adamc@196 90 end)
adamc@196 91 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 92 | L.CApp _ => poly ()
adamc@196 93 | L.CAbs _ => poly ()
adamc@196 94
adamc@196 95 | L.CName _ => poly ()
adamc@196 96
adamc@196 97 | L.CRecord _ => poly ()
adamc@196 98 | L.CConcat _ => poly ()
adamc@196 99 | L.CFold _ => poly ()
adamc@196 100 | L.CUnit => poly ()
adamc@214 101
adamc@214 102 | L.CTuple _ => poly ()
adamc@214 103 | L.CProj _ => poly ()
adamc@196 104 end
adamc@25 105 in
adamc@196 106 mt env IM.empty
adamc@25 107 end
adamc@25 108
adamc@25 109 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 110
adamc@179 111 structure IM = IntBinaryMap
adamc@179 112
adamc@179 113 datatype foo_kind =
adamc@179 114 Attr
adamc@179 115 | Url
adamc@179 116
adamc@179 117 fun fk2s fk =
adamc@179 118 case fk of
adamc@179 119 Attr => "attr"
adamc@179 120 | Url => "url"
adamc@179 121
adamc@179 122 structure Fm :> sig
adamc@179 123 type t
adamc@179 124
adamc@179 125 val empty : int -> t
adamc@179 126
adamc@179 127 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@179 128 val enter : t -> t
adamc@179 129 val decls : t -> L'.decl list
adamc@179 130 end = struct
adamc@179 131
adamc@179 132 structure M = BinaryMapFn(struct
adamc@179 133 type ord_key = foo_kind
adamc@179 134 fun compare x =
adamc@179 135 case x of
adamc@179 136 (Attr, Attr) => EQUAL
adamc@179 137 | (Attr, _) => LESS
adamc@179 138 | (_, Attr) => GREATER
adamc@179 139
adamc@179 140 | (Url, Url) => EQUAL
adamc@179 141 end)
adamc@179 142
adamc@179 143 type t = {
adamc@179 144 count : int,
adamc@179 145 map : int IM.map M.map,
adamc@179 146 decls : L'.decl list
adamc@179 147 }
adamc@179 148
adamc@179 149 fun empty count = {
adamc@179 150 count = count,
adamc@179 151 map = M.empty,
adamc@179 152 decls = []
adamc@179 153 }
adamc@179 154
adamc@179 155 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
adamc@179 156 fun decls ({decls, ...} : t) = decls
adamc@179 157
adamc@179 158 fun lookup (t as {count, map, decls}) k n thunk =
adamc@120 159 let
adamc@179 160 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 161 in
adamc@179 162 case IM.find (im, n) of
adamc@179 163 NONE =>
adamc@179 164 let
adamc@179 165 val n' = count
adamc@179 166 val (d, {count, map, decls}) = thunk count {count = count + 1,
adamc@179 167 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@179 168 decls = decls}
adamc@179 169 in
adamc@179 170 ({count = count,
adamc@179 171 map = map,
adamc@179 172 decls = d :: decls}, n')
adamc@179 173 end
adamc@179 174 | SOME n' => (t, n')
adamc@179 175 end
adamc@179 176
adamc@179 177 end
adamc@185 178
adamc@185 179
adamc@185 180 fun capitalize s =
adamc@185 181 if s = "" then
adamc@185 182 s
adamc@185 183 else
adamc@185 184 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 185
adamc@179 186 fun fooifyExp fk env =
adamc@179 187 let
adamc@179 188 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 189 case #1 e of
adamc@120 190 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 191 let
adamc@120 192 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 193 in
adamc@183 194 ((L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
adamc@120 195 end
adamc@120 196 | L'.EClosure (fnam, args) =>
adamc@120 197 let
adamc@120 198 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 199 val ft = monoType env ft
adamc@111 200
adamc@179 201 fun attrify (args, ft, e, fm) =
adamc@120 202 case (args, ft) of
adamc@179 203 ([], _) => (e, fm)
adamc@120 204 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 205 let
adamc@179 206 val (arg', fm) = fooify fm (arg, t)
adamc@179 207 in
adamc@179 208 attrify (args, ft,
adamc@179 209 (L'.EStrcat (e,
adamc@179 210 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 211 arg'), loc)), loc),
adamc@179 212 fm)
adamc@179 213 end
adamc@120 214 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 215 (e, fm))
adamc@120 216 in
adamc@183 217 attrify (args, ft, (L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
adamc@120 218 end
adamc@120 219 | _ =>
adamc@120 220 case t of
adamc@185 221 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 222
adamc@179 223 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 224 | L'.TRecord ((x, t) :: xts) =>
adamc@200 225 let
adamc@200 226 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 227 in
adamc@200 228 foldl (fn ((x, t), (se, fm)) =>
adamc@200 229 let
adamc@200 230 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 231 in
adamc@200 232 ((L'.EStrcat (se,
adamc@200 233 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 234 se'), loc)), loc),
adamc@200 235 fm)
adamc@200 236 end) (se, fm) xts
adamc@200 237 end
adamc@111 238
adamc@196 239 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 240 let
adamc@179 241 fun makeDecl n fm =
adamc@179 242 let
adamc@193 243 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 244
adamc@179 245 val (branches, fm) =
adamc@179 246 ListUtil.foldlMap
adamc@179 247 (fn ((x, n, to), fm) =>
adamc@179 248 case to of
adamc@179 249 NONE =>
adamc@188 250 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 251 (L'.EPrim (Prim.String x), loc)),
adamc@179 252 fm)
adamc@179 253 | SOME t =>
adamc@179 254 let
adamc@182 255 val t = monoType env t
adamc@182 256 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 257 in
adamc@188 258 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 259 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 260 arg), loc)),
adamc@179 261 fm)
adamc@179 262 end)
adamc@179 263 fm xncs
adamc@179 264
adamc@179 265 val dom = tAll
adamc@179 266 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 267 in
adamc@179 268 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 269 n,
adamc@179 270 (L'.TFun (dom, ran), loc),
adamc@179 271 (L'.EAbs ("x",
adamc@179 272 dom,
adamc@179 273 ran,
adamc@179 274 (L'.ECase ((L'.ERel 0, loc),
adamc@179 275 branches,
adamc@182 276 {disc = dom,
adamc@182 277 result = ran}), loc)), loc),
adamc@179 278 "")], loc),
adamc@179 279 fm)
adamc@179 280 end
adamc@179 281
adamc@179 282 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 283 in
adamc@179 284 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 285 end
adamc@164 286
adamc@120 287 | _ => (E.errorAt loc "Don't know how to encode attribute type";
adamc@120 288 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 289 (dummyExp, fm))
adamc@120 290 in
adamc@120 291 fooify
adamc@120 292 end
adamc@120 293
adamc@179 294 val attrifyExp = fooifyExp Attr
adamc@179 295 val urlifyExp = fooifyExp Url
adamc@105 296
adamc@143 297 datatype 'a failable_search =
adamc@143 298 Found of 'a
adamc@143 299 | NotFound
adamc@143 300 | Error
adamc@143 301
adamc@153 302 structure St :> sig
adamc@153 303 type t
adamc@153 304
adamc@153 305 val empty : t
adamc@153 306
adamc@153 307 val radioGroup : t -> string option
adamc@153 308 val setRadioGroup : t * string -> t
adamc@153 309 end = struct
adamc@153 310
adamc@153 311 type t = {
adamc@153 312 radioGroup : string option
adamc@153 313 }
adamc@153 314
adamc@153 315 val empty = {radioGroup = NONE}
adamc@153 316
adamc@153 317 fun radioGroup (t : t) = #radioGroup t
adamc@153 318
adamc@153 319 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 320
adamc@153 321 end
adamc@153 322
adamc@186 323 fun monoPatCon env pc =
adamc@178 324 case pc of
adamc@178 325 L.PConVar n => L'.PConVar n
adamc@188 326 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 327 arg = Option.map (monoType env) arg}
adamc@178 328
adamc@193 329 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 330
adamc@193 331 fun monoPat env (all as (p, loc)) =
adamc@193 332 let
adamc@193 333 fun poly () =
adamc@193 334 (E.errorAt loc "Unsupported pattern";
adamc@193 335 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 336 dummyPat)
adamc@193 337 in
adamc@193 338 case p of
adamc@193 339 L.PWild => (L'.PWild, loc)
adamc@193 340 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 341 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 342 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@193 343 | L.PCon _ => poly ()
adamc@193 344 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 345 end
adamc@178 346
adamc@179 347 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 348 let
adamc@25 349 fun poly () =
adamc@25 350 (E.errorAt loc "Unsupported expression";
adamc@25 351 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 352 (dummyExp, fm))
adamc@25 353 in
adamc@25 354 case e of
adamc@179 355 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 356 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 357 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 358 | L.ECon (dk, pc, [], eo) =>
adamc@193 359 let
adamc@179 360 val (eo, fm) =
adamc@179 361 case eo of
adamc@179 362 NONE => (NONE, fm)
adamc@179 363 | SOME e =>
adamc@179 364 let
adamc@179 365 val (e, fm) = monoExp (env, st, fm) e
adamc@179 366 in
adamc@179 367 (SOME e, fm)
adamc@179 368 end
adamc@179 369 in
adamc@188 370 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 371 end
adamc@193 372 | L.ECon _ => poly ()
adamc@179 373 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@179 374 | L.EFfiApp (m, x, es) =>
adamc@179 375 let
adamc@179 376 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@179 377 in
adamc@179 378 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@179 379 end
adamc@94 380
adamc@139 381 | L.EApp (
adamc@139 382 (L.ECApp (
adamc@141 383 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 384 _), _),
adamc@179 385 se) =>
adamc@179 386 let
adamc@179 387 val (se, fm) = monoExp (env, st, fm) se
adamc@179 388 in
adamc@179 389 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 390 end
adamc@179 391
adamc@95 392 | L.EApp (
adamc@95 393 (L.EApp (
adamc@95 394 (L.ECApp (
adamc@95 395 (L.ECApp (
adamc@95 396 (L.ECApp (
adamc@139 397 (L.ECApp (
adamc@140 398 (L.EFfi ("Basis", "join"),
adamc@139 399 _), _), _),
adamc@139 400 _), _),
adamc@95 401 _), _),
adamc@95 402 _), _),
adamc@95 403 xml1), _),
adamc@179 404 xml2) =>
adamc@179 405 let
adamc@179 406 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 407 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 408 in
adamc@179 409 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 410 end
adamc@95 411
adamc@95 412 | L.EApp (
adamc@95 413 (L.EApp (
adamc@104 414 (L.EApp (
adamc@95 415 (L.ECApp (
adamc@104 416 (L.ECApp (
adamc@104 417 (L.ECApp (
adamc@104 418 (L.ECApp (
adamc@139 419 (L.ECApp (
adamc@139 420 (L.ECApp (
adamc@139 421 (L.ECApp (
adamc@139 422 (L.ECApp (
adamc@139 423 (L.EFfi ("Basis", "tag"),
adamc@139 424 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 425 attrs), _),
adamc@95 426 tag), _),
adamc@95 427 xml) =>
adamc@95 428 let
adamc@140 429 fun getTag' (e, _) =
adamc@140 430 case e of
adamc@143 431 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 432 | L.ECApp (e, t) => let
adamc@143 433 val (tag, ts) = getTag' e
adamc@143 434 in
adamc@143 435 (tag, ts @ [t])
adamc@143 436 end
adamc@140 437 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 438 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 439 ("", []))
adamc@140 440
adamc@95 441 fun getTag (e, _) =
adamc@95 442 case e of
adamc@143 443 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 444 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 445 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 446 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 447 ("", []))
adamc@95 448
adamc@143 449 val (tag, targs) = getTag tag
adamc@95 450
adamc@179 451 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 452
adamc@143 453 fun tagStart tag =
adamc@104 454 case #1 attrs of
adamc@104 455 L'.ERecord xes =>
adamc@104 456 let
adamc@104 457 fun lowercaseFirst "" = ""
adamc@143 458 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 459 ^ String.extract (s, 1, NONE)
adamc@104 460
adamc@104 461 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 462 in
adamc@179 463 foldl (fn ((x, e, t), (s, fm)) =>
adamc@104 464 let
adamc@104 465 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@120 466
adamc@120 467 val fooify =
adamc@120 468 case x of
adamc@185 469 "Href" => urlifyExp
adamc@185 470 | "Link" => urlifyExp
adamc@143 471 | "Action" => urlifyExp
adamc@120 472 | _ => attrifyExp
adamc@179 473
adamc@179 474 val (e, fm) = fooify env fm (e, t)
adamc@104 475 in
adamc@179 476 ((L'.EStrcat (s,
adamc@179 477 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@179 478 (L'.EStrcat (e,
adamc@179 479 (L'.EPrim (Prim.String "\""),
adamc@179 480 loc)),
adamc@179 481 loc)),
adamc@179 482 loc)), loc),
adamc@179 483 fm)
adamc@104 484 end)
adamc@179 485 (s, fm) xes
adamc@104 486 end
adamc@143 487 | _ => raise Fail "Non-record attributes!"
adamc@104 488
adamc@143 489 fun input typ =
adamc@143 490 case targs of
adamc@155 491 [_, (L.CName name, _)] =>
adamc@179 492 let
adamc@179 493 val (ts, fm) = tagStart "input"
adamc@179 494 in
adamc@179 495 ((L'.EStrcat (ts,
adamc@179 496 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 497 loc)), loc), fm)
adamc@179 498 end
adamc@143 499 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 500 raise Fail "No name passed to input tag")
adamc@104 501
adamc@152 502 fun normal (tag, extra) =
adamc@143 503 let
adamc@179 504 val (tagStart, fm) = tagStart tag
adamc@152 505 val tagStart = case extra of
adamc@152 506 NONE => tagStart
adamc@152 507 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 508
adamc@143 509 fun normal () =
adamc@179 510 let
adamc@179 511 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 512 in
adamc@179 513 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 514 (L'.EStrcat (xml,
adamc@179 515 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 516 loc)), loc)),
adamc@179 517 loc),
adamc@179 518 fm)
adamc@179 519 end
adamc@143 520 in
adamc@143 521 case xml of
adamc@143 522 (L.EApp ((L.ECApp (
adamc@143 523 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 524 _), _),
adamc@143 525 _), _),
adamc@143 526 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 527 if CharVector.all Char.isSpace s then
adamc@179 528 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 529 else
adamc@143 530 normal ()
adamc@143 531 | _ => normal ()
adamc@143 532 end
adamc@152 533 in
adamc@152 534 case tag of
adamc@179 535 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
adamc@152 536
adamc@152 537 | "textbox" =>
adamc@152 538 (case targs of
adamc@152 539 [_, (L.CName name, _)] =>
adamc@179 540 let
adamc@179 541 val (ts, fm) = tagStart "input"
adamc@179 542 in
adamc@179 543 ((L'.EStrcat (ts,
adamc@179 544 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 545 loc)), loc), fm)
adamc@179 546 end
adamc@152 547 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 548 raise Fail "No name passed to textarea tag"))
adamc@155 549 | "password" => input "password"
adamc@152 550 | "ltextarea" =>
adamc@152 551 (case targs of
adamc@152 552 [_, (L.CName name, _)] =>
adamc@179 553 let
adamc@179 554 val (ts, fm) = tagStart "textarea"
adamc@179 555 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 556 in
adamc@179 557 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 558 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 559 (L'.EStrcat (xml,
adamc@179 560 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 561 loc)), loc)),
adamc@179 562 loc), fm)
adamc@179 563 end
adamc@152 564 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 565 raise Fail "No name passed to ltextarea tag"))
adamc@153 566
adamc@190 567 | "checkbox" => input "checkbox"
adamc@190 568
adamc@153 569 | "radio" =>
adamc@153 570 (case targs of
adamc@153 571 [_, (L.CName name, _)] =>
adamc@179 572 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 573 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 574 raise Fail "No name passed to radio tag"))
adamc@153 575 | "radioOption" =>
adamc@153 576 (case St.radioGroup st of
adamc@153 577 NONE => raise Fail "No name for radioGroup"
adamc@153 578 | SOME name =>
adamc@153 579 normal ("input",
adamc@153 580 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 581
adamc@154 582 | "lselect" =>
adamc@154 583 (case targs of
adamc@154 584 [_, (L.CName name, _)] =>
adamc@179 585 let
adamc@179 586 val (ts, fm) = tagStart "select"
adamc@179 587 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 588 in
adamc@179 589 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 590 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 591 (L'.EStrcat (xml,
adamc@179 592 (L'.EPrim (Prim.String "</select>"),
adamc@179 593 loc)), loc)),
adamc@179 594 loc),
adamc@179 595 fm)
adamc@179 596 end
adamc@154 597 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 598 raise Fail "No name passed to lselect tag"))
adamc@154 599
adamc@154 600 | "loption" => normal ("option", NONE)
adamc@154 601
adamc@152 602 | _ => normal (tag, NONE)
adamc@95 603 end
adamc@94 604
adamc@141 605 | L.EApp ((L.ECApp (
adamc@141 606 (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _),
adamc@141 607 _), _),
adamc@141 608 xml) =>
adamc@143 609 let
adamc@143 610 fun findSubmit (e, _) =
adamc@143 611 case e of
adamc@143 612 L.EApp (
adamc@143 613 (L.EApp (
adamc@143 614 (L.ECApp (
adamc@143 615 (L.ECApp (
adamc@143 616 (L.ECApp (
adamc@143 617 (L.ECApp (
adamc@143 618 (L.EFfi ("Basis", "join"),
adamc@143 619 _), _), _),
adamc@143 620 _), _),
adamc@143 621 _), _),
adamc@143 622 _), _),
adamc@143 623 xml1), _),
adamc@143 624 xml2) => (case findSubmit xml1 of
adamc@143 625 Error => Error
adamc@143 626 | NotFound => findSubmit xml2
adamc@143 627 | Found e =>
adamc@143 628 case findSubmit xml2 of
adamc@143 629 NotFound => Found e
adamc@143 630 | _ => Error)
adamc@143 631 | L.EApp (
adamc@143 632 (L.EApp (
adamc@143 633 (L.EApp (
adamc@143 634 (L.ECApp (
adamc@143 635 (L.ECApp (
adamc@143 636 (L.ECApp (
adamc@143 637 (L.ECApp (
adamc@143 638 (L.ECApp (
adamc@143 639 (L.ECApp (
adamc@143 640 (L.ECApp (
adamc@143 641 (L.ECApp (
adamc@143 642 (L.EFfi ("Basis", "tag"),
adamc@143 643 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 644 attrs), _),
adamc@143 645 _), _),
adamc@143 646 xml) =>
adamc@143 647 (case #1 attrs of
adamc@143 648 L.ERecord xes =>
adamc@143 649 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 650 | _ => NONE) xes of
adamc@143 651 NONE => findSubmit xml
adamc@143 652 | SOME et =>
adamc@143 653 case findSubmit xml of
adamc@143 654 NotFound => Found et
adamc@143 655 | _ => Error)
adamc@143 656 | _ => findSubmit xml)
adamc@143 657 | _ => NotFound
adamc@143 658
adamc@143 659 val (action, actionT) = case findSubmit xml of
adamc@143 660 NotFound => raise Fail "No submit found"
adamc@143 661 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 662 | Found et => et
adamc@143 663
adamc@143 664 val actionT = monoType env actionT
adamc@179 665 val (action, fm) = monoExp (env, st, fm) action
adamc@179 666 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 667 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 668 in
adamc@179 669 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 670 (L'.EStrcat (action,
adamc@179 671 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 672 (L'.EStrcat (xml,
adamc@179 673 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 674 fm)
adamc@143 675 end
adamc@141 676
adamc@148 677 | L.EApp ((L.ECApp (
adamc@148 678 (L.ECApp (
adamc@148 679 (L.ECApp (
adamc@148 680 (L.ECApp (
adamc@148 681 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 682 _), _),
adamc@148 683 _), _),
adamc@148 684 _), _),
adamc@179 685 xml) => monoExp (env, st, fm) xml
adamc@148 686
adamc@179 687 | L.EApp (e1, e2) =>
adamc@179 688 let
adamc@179 689 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 690 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 691 in
adamc@179 692 ((L'.EApp (e1, e2), loc), fm)
adamc@179 693 end
adamc@26 694 | L.EAbs (x, dom, ran, e) =>
adamc@179 695 let
adamc@179 696 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 697 in
adamc@179 698 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 699 end
adamc@25 700 | L.ECApp _ => poly ()
adamc@25 701 | L.ECAbs _ => poly ()
adamc@25 702
adamc@179 703 | L.ERecord xes =>
adamc@179 704 let
adamc@179 705 val (xes, fm) = ListUtil.foldlMap
adamc@179 706 (fn ((x, e, t), fm) =>
adamc@179 707 let
adamc@179 708 val (e, fm) = monoExp (env, st, fm) e
adamc@179 709 in
adamc@179 710 ((monoName env x,
adamc@179 711 e,
adamc@179 712 monoType env t), fm)
adamc@179 713 end) fm xes
adamc@179 714 in
adamc@179 715 ((L'.ERecord xes, loc), fm)
adamc@179 716 end
adamc@179 717 | L.EField (e, x, _) =>
adamc@179 718 let
adamc@179 719 val (e, fm) = monoExp (env, st, fm) e
adamc@179 720 in
adamc@179 721 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 722 end
adamc@149 723 | L.ECut _ => poly ()
adamc@73 724 | L.EFold _ => poly ()
adamc@177 725
adamc@182 726 | L.ECase (e, pes, {disc, result}) =>
adamc@179 727 let
adamc@179 728 val (e, fm) = monoExp (env, st, fm) e
adamc@179 729 val (pes, fm) = ListUtil.foldlMap
adamc@179 730 (fn ((p, e), fm) =>
adamc@179 731 let
adamc@179 732 val (e, fm) = monoExp (env, st, fm) e
adamc@179 733 in
adamc@182 734 ((monoPat env p, e), fm)
adamc@179 735 end) fm pes
adamc@179 736 in
adamc@182 737 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 738 end
adamc@177 739
adamc@179 740 | L.EWrite e =>
adamc@179 741 let
adamc@179 742 val (e, fm) = monoExp (env, st, fm) e
adamc@179 743 in
adamc@179 744 ((L'.EWrite e, loc), fm)
adamc@179 745 end
adamc@110 746
adamc@179 747 | L.EClosure (n, es) =>
adamc@179 748 let
adamc@179 749 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 750 monoExp (env, st, fm) e)
adamc@179 751 fm es
adamc@179 752 in
adamc@179 753 ((L'.EClosure (n, es), loc), fm)
adamc@179 754 end
adamc@25 755 end
adamc@25 756
adamc@179 757 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 758 let
adamc@25 759 fun poly () =
adamc@25 760 (E.errorAt loc "Unsupported declaration";
adamc@25 761 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 762 NONE)
adamc@25 763 in
adamc@25 764 case d of
adamc@25 765 L.DCon _ => NONE
adamc@193 766 | L.DDatatype (x, n, [], xncs) =>
adamc@193 767 let
adamc@196 768 val env' = Env.declBinds env all
adamc@196 769 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 770 in
adamc@196 771 SOME (env', fm, d)
adamc@193 772 end
adamc@193 773 | L.DDatatype _ => poly ()
adamc@179 774 | L.DVal (x, n, t, e, s) =>
adamc@179 775 let
adamc@179 776 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 777 in
adamc@179 778 SOME (Env.pushENamed env x n t NONE s,
adamc@179 779 fm,
adamc@179 780 (L'.DVal (x, n, monoType env t, e, s), loc))
adamc@179 781 end
adamc@128 782 | L.DValRec vis =>
adamc@128 783 let
adamc@128 784 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 785
adamc@179 786 val (vis, fm) = ListUtil.foldlMap
adamc@179 787 (fn ((x, n, t, e, s), fm) =>
adamc@179 788 let
adamc@179 789 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 790 in
adamc@179 791 ((x, n, monoType env t, e, s), fm)
adamc@179 792 end)
adamc@179 793 fm vis
adamc@128 794 in
adamc@128 795 SOME (env,
adamc@179 796 fm,
adamc@179 797 (L'.DValRec vis, loc))
adamc@128 798 end
adamc@144 799 | L.DExport (ek, n) =>
adamc@115 800 let
adamc@120 801 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 802
adamc@120 803 fun unwind (t, _) =
adamc@120 804 case t of
adamc@120 805 L.TFun (dom, ran) => dom :: unwind ran
adamc@120 806 | _ => []
adamc@120 807
adamc@120 808 val ts = map (monoType env) (unwind t)
adamc@115 809 in
adamc@179 810 SOME (env, fm, (L'.DExport (ek, s, n, ts), loc))
adamc@115 811 end
adamc@25 812 end
adamc@25 813
adamc@25 814 fun monoize env ds =
adamc@25 815 let
adamc@179 816 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 817 case monoDecl (env, fm) d of
adamc@179 818 NONE => (env, fm, ds)
adamc@179 819 | SOME (env, fm, d) =>
adamc@179 820 (env,
adamc@179 821 Fm.enter fm,
adamc@179 822 d :: Fm.decls fm @ ds))
adamc@179 823 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 824 in
adamc@25 825 rev ds
adamc@25 826 end
adamc@25 827
adamc@25 828 end