annotate src/monoize.sml @ 178:eb3f9913bf31

First part of getting cases through monoize
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 09:26:49 -0400
parents 5d030ee143e2
children 3bbed533fbd2
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@168 36 val dummyTyp = (L'.TDatatype (0, []), E.dummySpan)
adamc@25 37
adamc@25 38 fun monoName env (all as (c, loc)) =
adamc@25 39 let
adamc@25 40 fun poly () =
adamc@25 41 (E.errorAt loc "Unsupported name constructor";
adamc@25 42 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 43 "")
adamc@25 44 in
adamc@25 45 case c of
adamc@25 46 L.CName s => s
adamc@25 47 | _ => poly ()
adamc@25 48 end
adamc@25 49
adamc@25 50 fun monoType env (all as (c, loc)) =
adamc@25 51 let
adamc@25 52 fun poly () =
adamc@25 53 (E.errorAt loc "Unsupported type constructor";
adamc@25 54 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 55 dummyTyp)
adamc@25 56 in
adamc@25 57 case c of
adamc@25 58 L.TFun (c1, c2) => (L'.TFun (monoType env c1, monoType env c2), loc)
adamc@25 59 | L.TCFun _ => poly ()
adamc@25 60 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@25 61 (L'.TRecord (map (fn (x, t) => (monoName env x, monoType env t)) xcs), loc)
adamc@25 62 | L.TRecord _ => poly ()
adamc@25 63
adamc@143 64 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@143 65 (L'.TFfi ("Basis", "string"), loc)
adamc@94 66
adamc@25 67 | L.CRel _ => poly ()
adamc@168 68 | L.CNamed n =>
adamc@168 69 let
adamc@168 70 val (_, xncs) = Env.lookupDatatype env n
adamc@168 71
adamc@168 72 val xncs = map (fn (x, n, to) => (x, n, Option.map (monoType env) to)) xncs
adamc@168 73 in
adamc@168 74 (L'.TDatatype (n, xncs), loc)
adamc@168 75 end
adamc@51 76 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@25 77 | L.CApp _ => poly ()
adamc@25 78 | L.CAbs _ => poly ()
adamc@25 79
adamc@25 80 | L.CName _ => poly ()
adamc@25 81
adamc@25 82 | L.CRecord _ => poly ()
adamc@25 83 | L.CConcat _ => poly ()
adamc@69 84 | L.CFold _ => poly ()
adamc@87 85 | L.CUnit => poly ()
adamc@25 86 end
adamc@25 87
adamc@25 88 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 89
adamc@120 90 fun fooifyExp name env =
adamc@120 91 let
adamc@120 92 fun fooify (e, tAll as (t, loc)) =
adamc@120 93 case #1 e of
adamc@120 94 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 95 let
adamc@120 96 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 97 in
adamc@120 98 (L'.EPrim (Prim.String s), loc)
adamc@120 99 end
adamc@120 100 | L'.EClosure (fnam, args) =>
adamc@120 101 let
adamc@120 102 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 103 val ft = monoType env ft
adamc@111 104
adamc@120 105 fun attrify (args, ft, e) =
adamc@120 106 case (args, ft) of
adamc@120 107 ([], _) => e
adamc@120 108 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@121 109 attrify (args, ft,
adamc@121 110 (L'.EStrcat (e,
adamc@121 111 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@121 112 fooify (arg, t)), loc)), loc))
adamc@120 113 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@120 114 e)
adamc@120 115 in
adamc@120 116 attrify (args, ft, (L'.EPrim (Prim.String s), loc))
adamc@120 117 end
adamc@120 118 | _ =>
adamc@120 119 case t of
adamc@120 120 L'.TFfi ("Basis", "string") => (L'.EFfiApp ("Basis", name ^ "ifyString", [e]), loc)
adamc@120 121 | L'.TFfi ("Basis", "int") => (L'.EFfiApp ("Basis", name ^ "ifyInt", [e]), loc)
adamc@120 122 | L'.TFfi ("Basis", "float") => (L'.EFfiApp ("Basis", name ^ "ifyFloat", [e]), loc)
adamc@120 123 | L'.TRecord [] => (L'.EPrim (Prim.String ""), loc)
adamc@111 124
adamc@168 125 | L'.TDatatype _ => (L'.EPrim (Prim.String "A"), loc)
adamc@164 126
adamc@120 127 | _ => (E.errorAt loc "Don't know how to encode attribute type";
adamc@120 128 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@120 129 dummyExp)
adamc@120 130 in
adamc@120 131 fooify
adamc@120 132 end
adamc@120 133
adamc@120 134 val attrifyExp = fooifyExp "attr"
adamc@120 135 val urlifyExp = fooifyExp "url"
adamc@105 136
adamc@143 137 datatype 'a failable_search =
adamc@143 138 Found of 'a
adamc@143 139 | NotFound
adamc@143 140 | Error
adamc@143 141
adamc@153 142 structure St :> sig
adamc@153 143 type t
adamc@153 144
adamc@153 145 val empty : t
adamc@153 146
adamc@153 147 val radioGroup : t -> string option
adamc@153 148 val setRadioGroup : t * string -> t
adamc@153 149 end = struct
adamc@153 150
adamc@153 151 type t = {
adamc@153 152 radioGroup : string option
adamc@153 153 }
adamc@153 154
adamc@153 155 val empty = {radioGroup = NONE}
adamc@153 156
adamc@153 157 fun radioGroup (t : t) = #radioGroup t
adamc@153 158
adamc@153 159 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 160
adamc@153 161 end
adamc@153 162
adamc@178 163 fun monoPatCon pc =
adamc@178 164 case pc of
adamc@178 165 L.PConVar n => L'.PConVar n
adamc@178 166 | L.PConFfi mx => L'.PConFfi mx
adamc@178 167
adamc@178 168 fun monoPat (p, loc) =
adamc@178 169 case p of
adamc@178 170 L.PWild => (L'.PWild, loc)
adamc@178 171 | L.PVar x => (L'.PVar x, loc)
adamc@178 172 | L.PPrim p => (L'.PPrim p, loc)
adamc@178 173 | L.PCon (pc, po) => (L'.PCon (monoPatCon pc, Option.map monoPat po), loc)
adamc@178 174 | L.PRecord xps => (L'.PRecord (map (fn (x, p) => (x, monoPat p)) xps), loc)
adamc@178 175
adamc@153 176 fun monoExp (env, st) (all as (e, loc)) =
adamc@25 177 let
adamc@25 178 fun poly () =
adamc@25 179 (E.errorAt loc "Unsupported expression";
adamc@25 180 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@25 181 dummyExp)
adamc@25 182 in
adamc@25 183 case e of
adamc@25 184 L.EPrim p => (L'.EPrim p, loc)
adamc@25 185 | L.ERel n => (L'.ERel n, loc)
adamc@25 186 | L.ENamed n => (L'.ENamed n, loc)
adamc@178 187 | L.ECon (n, eo) => (L'.ECon (n, Option.map (monoExp (env, st)) eo), loc)
adamc@51 188 | L.EFfi mx => (L'.EFfi mx, loc)
adamc@153 189 | L.EFfiApp (m, x, es) => (L'.EFfiApp (m, x, map (monoExp (env, st)) es), loc)
adamc@94 190
adamc@139 191 | L.EApp (
adamc@139 192 (L.ECApp (
adamc@141 193 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 194 _), _),
adamc@153 195 se) => (L'.EFfiApp ("Basis", "htmlifyString", [monoExp (env, st) se]), loc)
adamc@95 196 | L.EApp (
adamc@95 197 (L.EApp (
adamc@95 198 (L.ECApp (
adamc@95 199 (L.ECApp (
adamc@95 200 (L.ECApp (
adamc@139 201 (L.ECApp (
adamc@140 202 (L.EFfi ("Basis", "join"),
adamc@139 203 _), _), _),
adamc@139 204 _), _),
adamc@95 205 _), _),
adamc@95 206 _), _),
adamc@95 207 xml1), _),
adamc@153 208 xml2) => (L'.EStrcat (monoExp (env, st) xml1, monoExp (env, st) xml2), loc)
adamc@95 209
adamc@95 210 | L.EApp (
adamc@95 211 (L.EApp (
adamc@104 212 (L.EApp (
adamc@95 213 (L.ECApp (
adamc@104 214 (L.ECApp (
adamc@104 215 (L.ECApp (
adamc@104 216 (L.ECApp (
adamc@139 217 (L.ECApp (
adamc@139 218 (L.ECApp (
adamc@139 219 (L.ECApp (
adamc@139 220 (L.ECApp (
adamc@139 221 (L.EFfi ("Basis", "tag"),
adamc@139 222 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 223 attrs), _),
adamc@95 224 tag), _),
adamc@95 225 xml) =>
adamc@95 226 let
adamc@140 227 fun getTag' (e, _) =
adamc@140 228 case e of
adamc@143 229 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 230 | L.ECApp (e, t) => let
adamc@143 231 val (tag, ts) = getTag' e
adamc@143 232 in
adamc@143 233 (tag, ts @ [t])
adamc@143 234 end
adamc@140 235 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 236 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 237 ("", []))
adamc@140 238
adamc@95 239 fun getTag (e, _) =
adamc@95 240 case e of
adamc@143 241 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 242 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 243 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 244 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 245 ("", []))
adamc@95 246
adamc@143 247 val (tag, targs) = getTag tag
adamc@95 248
adamc@153 249 val attrs = monoExp (env, st) attrs
adamc@104 250
adamc@143 251 fun tagStart tag =
adamc@104 252 case #1 attrs of
adamc@104 253 L'.ERecord xes =>
adamc@104 254 let
adamc@104 255 fun lowercaseFirst "" = ""
adamc@143 256 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 257 ^ String.extract (s, 1, NONE)
adamc@104 258
adamc@104 259 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 260 in
adamc@105 261 foldl (fn ((x, e, t), s) =>
adamc@104 262 let
adamc@104 263 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@120 264
adamc@120 265 val fooify =
adamc@120 266 case x of
adamc@120 267 "Link" => urlifyExp
adamc@143 268 | "Action" => urlifyExp
adamc@120 269 | _ => attrifyExp
adamc@104 270 in
adamc@104 271 (L'.EStrcat (s,
adamc@104 272 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@120 273 (L'.EStrcat (fooify env (e, t),
adamc@143 274 (L'.EPrim (Prim.String "\""),
adamc@143 275 loc)),
adamc@104 276 loc)),
adamc@104 277 loc)), loc)
adamc@104 278 end)
adamc@143 279 s xes
adamc@104 280 end
adamc@143 281 | _ => raise Fail "Non-record attributes!"
adamc@104 282
adamc@143 283 fun input typ =
adamc@143 284 case targs of
adamc@155 285 [_, (L.CName name, _)] =>
adamc@143 286 (L'.EStrcat (tagStart "input",
adamc@143 287 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@143 288 loc)), loc)
adamc@143 289 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 290 raise Fail "No name passed to input tag")
adamc@104 291
adamc@152 292 fun normal (tag, extra) =
adamc@143 293 let
adamc@143 294 val tagStart = tagStart tag
adamc@152 295 val tagStart = case extra of
adamc@152 296 NONE => tagStart
adamc@152 297 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 298
adamc@143 299 fun normal () =
adamc@143 300 (L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@153 301 (L'.EStrcat (monoExp (env, st) xml,
adamc@143 302 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@143 303 loc)), loc)),
adamc@143 304 loc)
adamc@143 305 in
adamc@143 306 case xml of
adamc@143 307 (L.EApp ((L.ECApp (
adamc@143 308 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 309 _), _),
adamc@143 310 _), _),
adamc@143 311 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 312 if CharVector.all Char.isSpace s then
adamc@143 313 (L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc)
adamc@143 314 else
adamc@143 315 normal ()
adamc@143 316 | _ => normal ()
adamc@143 317 end
adamc@152 318 in
adamc@152 319 case tag of
adamc@152 320 "submit" => (L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc)
adamc@152 321
adamc@152 322 | "textbox" =>
adamc@152 323 (case targs of
adamc@152 324 [_, (L.CName name, _)] =>
adamc@152 325 (L'.EStrcat (tagStart "input",
adamc@152 326 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@152 327 loc)), loc)
adamc@152 328 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 329 raise Fail "No name passed to textarea tag"))
adamc@155 330 | "password" => input "password"
adamc@152 331 | "ltextarea" =>
adamc@152 332 (case targs of
adamc@152 333 [_, (L.CName name, _)] =>
adamc@152 334 (L'.EStrcat ((L'.EStrcat (tagStart "textarea",
adamc@152 335 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@153 336 (L'.EStrcat (monoExp (env, st) xml,
adamc@152 337 (L'.EPrim (Prim.String "</textarea>"),
adamc@152 338 loc)), loc)),
adamc@152 339 loc)
adamc@152 340 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 341 raise Fail "No name passed to ltextarea tag"))
adamc@153 342
adamc@153 343 | "radio" =>
adamc@153 344 (case targs of
adamc@153 345 [_, (L.CName name, _)] =>
adamc@153 346 monoExp (env, St.setRadioGroup (st, name)) xml
adamc@153 347 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 348 raise Fail "No name passed to radio tag"))
adamc@153 349 | "radioOption" =>
adamc@153 350 (case St.radioGroup st of
adamc@153 351 NONE => raise Fail "No name for radioGroup"
adamc@153 352 | SOME name =>
adamc@153 353 normal ("input",
adamc@153 354 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 355
adamc@154 356 | "lselect" =>
adamc@154 357 (case targs of
adamc@154 358 [_, (L.CName name, _)] =>
adamc@154 359 (L'.EStrcat ((L'.EStrcat (tagStart "select",
adamc@154 360 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@154 361 (L'.EStrcat (monoExp (env, st) xml,
adamc@154 362 (L'.EPrim (Prim.String "</select>"),
adamc@154 363 loc)), loc)),
adamc@154 364 loc)
adamc@154 365 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 366 raise Fail "No name passed to lselect tag"))
adamc@154 367
adamc@154 368 | "loption" => normal ("option", NONE)
adamc@154 369
adamc@152 370 | _ => normal (tag, NONE)
adamc@95 371 end
adamc@94 372
adamc@141 373 | L.EApp ((L.ECApp (
adamc@141 374 (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _),
adamc@141 375 _), _),
adamc@141 376 xml) =>
adamc@143 377 let
adamc@143 378 fun findSubmit (e, _) =
adamc@143 379 case e of
adamc@143 380 L.EApp (
adamc@143 381 (L.EApp (
adamc@143 382 (L.ECApp (
adamc@143 383 (L.ECApp (
adamc@143 384 (L.ECApp (
adamc@143 385 (L.ECApp (
adamc@143 386 (L.EFfi ("Basis", "join"),
adamc@143 387 _), _), _),
adamc@143 388 _), _),
adamc@143 389 _), _),
adamc@143 390 _), _),
adamc@143 391 xml1), _),
adamc@143 392 xml2) => (case findSubmit xml1 of
adamc@143 393 Error => Error
adamc@143 394 | NotFound => findSubmit xml2
adamc@143 395 | Found e =>
adamc@143 396 case findSubmit xml2 of
adamc@143 397 NotFound => Found e
adamc@143 398 | _ => Error)
adamc@143 399 | L.EApp (
adamc@143 400 (L.EApp (
adamc@143 401 (L.EApp (
adamc@143 402 (L.ECApp (
adamc@143 403 (L.ECApp (
adamc@143 404 (L.ECApp (
adamc@143 405 (L.ECApp (
adamc@143 406 (L.ECApp (
adamc@143 407 (L.ECApp (
adamc@143 408 (L.ECApp (
adamc@143 409 (L.ECApp (
adamc@143 410 (L.EFfi ("Basis", "tag"),
adamc@143 411 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 412 attrs), _),
adamc@143 413 _), _),
adamc@143 414 xml) =>
adamc@143 415 (case #1 attrs of
adamc@143 416 L.ERecord xes =>
adamc@143 417 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 418 | _ => NONE) xes of
adamc@143 419 NONE => findSubmit xml
adamc@143 420 | SOME et =>
adamc@143 421 case findSubmit xml of
adamc@143 422 NotFound => Found et
adamc@143 423 | _ => Error)
adamc@143 424 | _ => findSubmit xml)
adamc@143 425 | _ => NotFound
adamc@143 426
adamc@143 427 val (action, actionT) = case findSubmit xml of
adamc@143 428 NotFound => raise Fail "No submit found"
adamc@143 429 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 430 | Found et => et
adamc@143 431
adamc@143 432 val actionT = monoType env actionT
adamc@153 433 val action = monoExp (env, st) action
adamc@143 434 in
adamc@143 435 (L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@143 436 (L'.EStrcat (urlifyExp env (action, actionT),
adamc@143 437 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@153 438 (L'.EStrcat (monoExp (env, st) xml,
adamc@143 439 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc)
adamc@143 440 end
adamc@141 441
adamc@148 442 | L.EApp ((L.ECApp (
adamc@148 443 (L.ECApp (
adamc@148 444 (L.ECApp (
adamc@148 445 (L.ECApp (
adamc@148 446 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 447 _), _),
adamc@148 448 _), _),
adamc@148 449 _), _),
adamc@153 450 xml) => monoExp (env, st) xml
adamc@148 451
adamc@148 452
adamc@153 453 | L.EApp (e1, e2) => (L'.EApp (monoExp (env, st) e1, monoExp (env, st) e2), loc)
adamc@26 454 | L.EAbs (x, dom, ran, e) =>
adamc@153 455 (L'.EAbs (x, monoType env dom, monoType env ran, monoExp (Env.pushERel env x dom, st) e), loc)
adamc@25 456 | L.ECApp _ => poly ()
adamc@25 457 | L.ECAbs _ => poly ()
adamc@25 458
adamc@153 459 | L.ERecord xes => (L'.ERecord (map (fn (x, e, t) => (monoName env x,
adamc@153 460 monoExp (env, st) e,
adamc@153 461 monoType env t)) xes), loc)
adamc@153 462 | L.EField (e, x, _) => (L'.EField (monoExp (env, st) e, monoName env x), loc)
adamc@149 463 | L.ECut _ => poly ()
adamc@73 464 | L.EFold _ => poly ()
adamc@177 465
adamc@178 466 | L.ECase (e, pes, t) => (L'.ECase (monoExp (env, st) e,
adamc@178 467 map (fn (p, e) => (monoPat p, monoExp (env, st) e)) pes,
adamc@178 468 monoType env t), loc)
adamc@177 469
adamc@153 470 | L.EWrite e => (L'.EWrite (monoExp (env, st) e), loc)
adamc@110 471
adamc@153 472 | L.EClosure (n, es) => (L'.EClosure (n, map (monoExp (env, st)) es), loc)
adamc@25 473 end
adamc@25 474
adamc@25 475 fun monoDecl env (all as (d, loc)) =
adamc@25 476 let
adamc@25 477 fun poly () =
adamc@25 478 (E.errorAt loc "Unsupported declaration";
adamc@25 479 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 480 NONE)
adamc@25 481 in
adamc@25 482 case d of
adamc@25 483 L.DCon _ => NONE
adamc@164 484 | L.DDatatype (x, n, xncs) =>
adamc@164 485 let
adamc@164 486 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env) to)) xncs), loc)
adamc@164 487 in
adamc@164 488 SOME (Env.declBinds env all, d)
adamc@164 489 end
adamc@109 490 | L.DVal (x, n, t, e, s) => SOME (Env.pushENamed env x n t (SOME e) s,
adamc@153 491 (L'.DVal (x, n, monoType env t, monoExp (env, St.empty) e, s), loc))
adamc@128 492 | L.DValRec vis =>
adamc@128 493 let
adamc@128 494 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@128 495 in
adamc@128 496 SOME (env,
adamc@153 497 (L'.DValRec (map (fn (x, n, t, e, s) => (x, n, monoType env t,
adamc@153 498 monoExp (env, St.empty) e, s)) vis), loc))
adamc@128 499 end
adamc@144 500 | L.DExport (ek, n) =>
adamc@115 501 let
adamc@120 502 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 503
adamc@120 504 fun unwind (t, _) =
adamc@120 505 case t of
adamc@120 506 L.TFun (dom, ran) => dom :: unwind ran
adamc@120 507 | _ => []
adamc@120 508
adamc@120 509 val ts = map (monoType env) (unwind t)
adamc@115 510 in
adamc@144 511 SOME (env, (L'.DExport (ek, s, n, ts), loc))
adamc@115 512 end
adamc@25 513 end
adamc@25 514
adamc@25 515 fun monoize env ds =
adamc@25 516 let
adamc@25 517 val (_, ds) = List.foldl (fn (d, (env, ds)) =>
adamc@25 518 case monoDecl env d of
adamc@25 519 NONE => (env, ds)
adamc@25 520 | SOME (env, d) => (env, d :: ds)) (env, []) ds
adamc@25 521 in
adamc@25 522 rev ds
adamc@25 523 end
adamc@25 524
adamc@25 525 end