adamc@133: (* Copyright (c) 2008, Adam Chlipala adamc@133: * All rights reserved. adamc@133: * adamc@133: * Redistribution and use in source and binary forms, with or without adamc@133: * modification, are permitted provided that the following conditions are met: adamc@133: * adamc@133: * - Redistributions of source code must retain the above copyright notice, adamc@133: * this list of conditions and the following disclaimer. adamc@133: * - Redistributions in binary form must reproduce the above copyright notice, adamc@133: * this list of conditions and the following disclaimer in the documentation adamc@133: * and/or other materials provided with the distribution. adamc@133: * - The names of contributors may not be used to endorse or promote products adamc@133: * derived from this software without specific prior written permission. adamc@133: * adamc@133: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@133: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@133: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@133: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@133: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@133: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@133: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@133: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@133: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@133: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@133: * POSSIBILITY OF SUCH DAMAGE. adamc@133: *) adamc@133: adamc@133: (* Simplify a Mono program algebraically *) adamc@133: adamc@133: structure MonoReduce :> MONO_REDUCE = struct adamc@133: adamc@133: open Mono adamc@133: adamc@133: structure E = MonoEnv adamc@133: structure U = MonoUtil adamc@133: adamc@133: val liftExpInExp = adamc@133: U.Exp.mapB {typ = fn t => t, adamc@133: exp = fn bound => fn e => adamc@133: case e of adamc@133: ERel xn => adamc@133: if xn < bound then adamc@133: e adamc@133: else adamc@133: ERel (xn + 1) adamc@133: | _ => e, adamc@133: bind = fn (bound, U.Exp.RelE _) => bound + 1 adamc@133: | (bound, _) => bound} adamc@133: adamc@133: val subExpInExp = adamc@133: U.Exp.mapB {typ = fn t => t, adamc@133: exp = fn (xn, rep) => fn e => adamc@133: case e of adamc@133: ERel xn' => adamc@133: (case Int.compare (xn', xn) of adamc@133: EQUAL => #1 rep adamc@133: | GREATER=> ERel (xn' - 1) adamc@133: | LESS => e) adamc@133: | _ => e, adamc@133: bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep) adamc@133: | (ctx, _) => ctx} adamc@133: adamc@133: fun bind (env, b) = adamc@133: case b of adamc@133: U.Decl.NamedT (x, n, co) => E.pushTNamed env x n co adamc@133: | U.Decl.RelE (x, t) => E.pushERel env x t adamc@133: | U.Decl.NamedE (x, n, t, eo, s) => E.pushENamed env x n t eo s adamc@133: adamc@133: fun typ c = c adamc@133: adamc@133: fun exp env e = adamc@133: case e of adamc@133: ENamed n => adamc@133: (case E.lookupENamed env n of adamc@133: (_, _, SOME e', _) => #1 e' adamc@133: | _ => e) adamc@133: adamc@133: | EApp ((EAbs (_, _, _, e1), loc), e2) => adamc@133: #1 (reduceExp env (subExpInExp (0, e2) e1)) adamc@133: adamc@133: | _ => e adamc@133: adamc@133: and reduceExp env = U.Exp.mapB {typ = typ, exp = exp, bind = bind} env adamc@133: adamc@133: fun decl env d = d adamc@133: adamc@133: val reduce = U.File.mapB {typ = typ, exp = exp, decl = decl, bind = bind} E.empty adamc@133: adamc@133: end