adamc@482: (* Copyright (c) 2008, Adam Chlipala adamc@482: * All rights reserved. adamc@482: * adamc@482: * Redistribution and use in source and binary forms, with or without adamc@482: * modification, are permitted provided that the following conditions are met: adamc@482: * adamc@482: * - Redistributions of source code must retain the above copyright notice, adamc@482: * this list of conditions and the following disclaimer. adamc@482: * - Redistributions in binary form must reproduce the above copyright notice, adamc@482: * this list of conditions and the following disclaimer in the documentation adamc@482: * and/or other materials provided with the distribution. adamc@482: * - The names of contributors may not be used to endorse or promote products adamc@482: * derived from this software without specific prior written permission. adamc@482: * adamc@482: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@482: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@482: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@482: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@482: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@482: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@482: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@482: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@482: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@482: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@482: * POSSIBILITY OF SUCH DAMAGE. adamc@482: *) adamc@482: adamc@482: (* Simplify a Core program algebraically, without unfolding definitions *) adamc@482: adamc@482: structure ReduceLocal :> REDUCE_LOCAL = struct adamc@482: adamc@482: open Core adamc@482: adamc@482: structure E = CoreEnv adamc@482: structure U = CoreUtil adamc@482: adamc@482: val subExpInExp = E.subExpInExp adamc@482: adamc@482: fun default x = x adamc@482: adamc@482: fun exp (e : exp') = adamc@482: let adamc@482: (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp env (e, ErrorMsg.dummySpan))]*) adamc@482: adamc@482: val r = case e of adamc@482: EApp ((EAbs (x, t, _, e1), loc), e2) => adamc@482: ((*Print.prefaces "Substitute" [("x", Print.PD.string x), adamc@482: ("t", CorePrint.p_con CoreEnv.empty t)];*) adamc@482: #1 (reduceExp (subExpInExp (0, e2) e1))) adamc@482: adamc@482: | EField ((ERecord xes, _), (CName x, _), _) => adamc@482: (case List.find (fn ((CName x', _), _, _) => x' = x adamc@482: | _ => false) xes of adamc@482: SOME (_, (e, _), _) => e adamc@482: | NONE => e) adamc@482: adamc@482: | _ => e adamc@482: in adamc@482: (*Print.prefaces "exp'" [("e", CorePrint.p_exp env (e, ErrorMsg.dummySpan)), adamc@482: ("r", CorePrint.p_exp env (r, ErrorMsg.dummySpan))];*) adamc@482: adamc@482: r adamc@482: end adamc@482: adamc@482: and reduceExp e = U.Exp.map {kind = default, con = default, exp = exp} e adamc@482: adamc@482: val reduce = U.File.map {kind = default, con = default, exp = exp, decl = default} adamc@482: adamc@482: end