Line data Source code
1 : // Author(s): Wieger Wesselink 2 : // Copyright: see the accompanying file COPYING or copy at 3 : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING 4 : // 5 : // Distributed under the Boost Software License, Version 1.0. 6 : // (See accompanying file LICENSE_1_0.txt or copy at 7 : // http://www.boost.org/LICENSE_1_0.txt) 8 : // 9 : /// \file mcrl2/data/replace_constants_by_variables.h 10 : /// \brief add your file description here. 11 : 12 : #ifndef MCRL2_DATA_REPLACE_CONSTANTS_BY_VARIABLES_H 13 : #define MCRL2_DATA_REPLACE_CONSTANTS_BY_VARIABLES_H 14 : 15 : #include "mcrl2/data/rewriter.h" 16 : #include "mcrl2/data/builder.h" 17 : 18 : namespace mcrl2 { 19 : 20 : namespace data { 21 : 22 : namespace detail { 23 : 24 : /// \brief Replace each constant data application c by a fresh variable v, and add extend the substitution sigma 25 : /// with the assignment v := r(c). This can be used in rewriting, to avoid that c is rewritten by the rewriter 26 : /// multiple times. 27 : template <template <class> class Builder> 28 : struct replace_constants_by_variables_builder: public Builder<replace_constants_by_variables_builder<Builder>> 29 : { 30 : typedef Builder<replace_constants_by_variables_builder<Builder>> super; 31 : using super::apply; 32 : using super::update; 33 : 34 : data::set_identifier_generator id_generator; 35 : std::unordered_map<data::data_expression, data::variable> substitutions; 36 : const data::rewriter& r; 37 : data::mutable_indexed_substitution<>& sigma; 38 : 39 0 : bool is_constant(const data::data_expression& x) const 40 : { 41 0 : return data::find_free_variables(x).empty(); 42 : } 43 : 44 0 : replace_constants_by_variables_builder(const data::rewriter& r_, data::mutable_indexed_substitution<>& sigma_) 45 0 : : r(r_), sigma(sigma_) 46 0 : {} 47 : 48 : template <class T> 49 0 : void apply(T& result, const data::application& x) 50 : { 51 0 : auto i = substitutions.find(x); 52 0 : if (i != substitutions.end()) 53 : { 54 0 : result = i->second; 55 : } 56 0 : else if (is_constant(x)) 57 : { 58 0 : data::variable v(id_generator("@rewr_var"), x.sort()); 59 0 : substitutions[x] = v; 60 0 : sigma[v] = r(x, sigma); 61 0 : result = v; 62 0 : } 63 : else 64 : { 65 0 : super::apply(result, x); 66 : } 67 0 : } 68 : }; 69 : 70 : } // namespace detail 71 : 72 : } // namespace data 73 : 74 : } // namespace mcrl2 75 : 76 : #endif // MCRL2_DATA_REPLACE_CONSTANTS_BY_VARIABLES_H