annotate src/monoize.sml @ 164:6847741e1f5f

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