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@168: U.Decl.Datatype (x, n, xncs) => E.pushDatatype env x n xncs adamc@183: | U.Decl.RelE (x, t) => E.pushERel env x t NONE 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@183: fun match (env, p : pat, e : exp) = adamc@183: case (#1 p, #1 e) of adamc@183: (PWild, _) => SOME env adamc@183: | (PVar (x, t), _) => SOME (E.pushERel env x t (SOME e)) adamc@183: adamc@183: | (PPrim p, EPrim p') => adamc@183: if Prim.equal (p, p') then adamc@183: SOME env adamc@183: else adamc@183: NONE adamc@183: adamc@183: | (PCon (PConVar n1, NONE), ECon (n2, NONE)) => adamc@183: if n1 = n2 then adamc@183: SOME env adamc@183: else adamc@183: NONE adamc@183: adamc@183: | (PCon (PConVar n1, SOME p), ECon (n2, SOME e)) => adamc@183: if n1 = n2 then adamc@183: match (env, p, e) adamc@183: else adamc@183: NONE adamc@183: adamc@183: | (PRecord xps, ERecord xes) => adamc@183: let adamc@183: fun consider (xps, env) = adamc@183: case xps of adamc@183: [] => SOME env adamc@183: | (x, p, _) :: rest => adamc@183: case List.find (fn (x', _, _) => x' = x) xes of adamc@183: NONE => NONE adamc@183: | SOME (_, e, _) => adamc@183: case match (env, p, e) of adamc@183: NONE => NONE adamc@183: | SOME env => consider (rest, env) adamc@183: in adamc@183: consider (xps, env) adamc@183: end adamc@183: adamc@183: | _ => NONE adamc@183: adamc@133: fun exp env e = adamc@133: case e of adamc@183: ERel n => adamc@183: (case E.lookupERel env n of adamc@183: (_, _, SOME e') => #1 e' adamc@183: | _ => e) adamc@183: | 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@183: | ECase (disc, pes, t) => adamc@183: (case ListUtil.search (fn (p, body) => adamc@183: case match (env, p, disc) of adamc@183: NONE => NONE adamc@183: | SOME env => SOME (#1 (reduceExp env body))) pes of adamc@183: NONE => e adamc@183: | SOME e' => e') adamc@183: 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