mCRL2
Loading...
Searching...
No Matches
rewrite.h
Go to the documentation of this file.
1// Author(s): Muck van Weerdenburg
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/detail/rewrite.h
10
11#ifndef MCRL2_DATA_DETAIL_REWRITE_H
12#define MCRL2_DATA_DETAIL_REWRITE_H
13
14#include "mcrl2/data/detail/enumerator_identifier_generator.h"
15#include "mcrl2/data/rewrite_strategy.h"
16#include "mcrl2/data/selection.h"
17#include "mcrl2/data/substitutions/mutable_indexed_substitution.h"
18
19
20
21namespace mcrl2::data::detail
22{
23
24/**
25 * \brief Rewriter interface class.
26 *
27 * This is the interface class for the rewriters. To create a specific
28 * rewriter, use createRewriter.
29 *
30 * Simple use of the rewriter would be as follows (with t a term in the mCRL2
31 * internal format):
32 *
33 * \code
34 * std::shared_ptr<Rewriter> r = createRewriter(equations);
35 * t = r->rewrite(t);
36 * \endcode
37 **/
39{
40 protected:
42
43 /** \brief The copy assignment operator is protected. Public copying is not allowed.
44 **/
45 Rewriter& operator=(const Rewriter& other) = default;
46
47 /** \brief The copy constructor operator is protected. Public copying is not allowed.
48 **/
49 Rewriter(const Rewriter& other) = default;
50
51 public:
53
55 /**
56 * \brief Constructor. Do not use directly; use createRewriter()
57 * function instead.
58 * \sa createRewriter()
59 **/
60 Rewriter(const data_specification& data_spec, const used_data_equation_selector& eq_selector):
63 {
64 }
65
66 /** \brief Destructor. */
67 virtual ~Rewriter() = default;
68
69 /** \brief The fresh name generator of the rewriter */
71 {
72 return m_generator;
73 }
74
75 /**
76 * \brief Get rewriter strategy that is used.
77 * \return Used rewriter strategy.
78 **/
79 virtual rewrite_strategy getStrategy() = 0;
80
81 /**
82 * \brief Rewrite an mCRL2 data term.
83 * \param Term The term to be rewritten. This term should be a data_term
84 * \return The normal form of Term.
85 **/
86 virtual data_expression rewrite(const data_expression& term, substitution_type& sigma) = 0;
87
88 /**
89 * \brief Rewrite an mCRL2 data term.
90 * \param Term The term to be rewritten. This term should be a data_term
91 * \return The normal form of Term.
92 **/
93 virtual void rewrite(data_expression& result, const data_expression& term, substitution_type& sigma) = 0;
94
95 /**
96 * \brief Provide the rewriter with a () operator, such that it can also
97 * rewrite terms using this operator.
98 **/
99
100 data_expression operator() (const data_expression& term, substitution_type& sigma)
101 {
102 return rewrite(term,sigma);
103 }
104
105 /**
106 * \brief Clone a rewriter.
107 * \return A (pointer to a) a clone of the rewriter.
108 **/
109 virtual std::shared_ptr<detail::Rewriter> clone() = 0;
110
111 public:
112 /* The functions below are public, because they are used in the compiling jitty rewriter */
114 const abstraction& t,
115 substitution_type& sigma,
116 bool t1_is_normal_form = false);
117
119 const variable_list& vl,
120 const data_expression& t1,
121 substitution_type& sigma,
122 bool t1_is_normal_form = false);
123
125 const abstraction& t,
126 substitution_type& sigma,
127 bool t1_is_normal_form = false);
128
130 const variable_list& vl,
131 const data_expression& t1,
132 substitution_type& sigma,
133 bool t1_is_normal_form = false);
134
135 // Rewrite a where expression where the subdataexpressions are in internal format.
136 // It yields a term without a where expression. The result is passed back in the variable result.
137 void rewrite_where(data_expression& result,
138 const where_clause& term,
139 substitution_type& sigma);
140
141 // Rewrite an expression with a lambda as outermost symbol. The expression is in internal format.
142 // Bound variables are replaced by new variables to avoid a clash with variables in the right hand sides
143 // of sigma.
144
146 const variable_list& vl,
147 const data_expression& body,
148 substitution_type& sigma,
149 bool body_in_normal_form);
150
151 /// Rewrite t, assuming that the headsymbol of t, which can be nested, is a lambda term.
153 const data_expression& t,
154 substitution_type& sigma,
155 bool arguments_are_in_normal_form = false);
156
158 const abstraction& lambda_term,
159 const application& t,
160 substitution_type& sigma,
161 bool arguments_are_in_normal_form = false);
162
163 virtual void thread_initialise()
164 {
165 }
166
167 protected:
168
170
172 const variable_list& vl,
173 const data_expression& t1,
174 bool t1_is_normal_form,
175 substitution_type& sigma,
176 const binder_type& binder,
177 data_expression (*lazy_op)(const data_expression&, const data_expression&),
178 const data_expression& identity_element,
179 const data_expression& absorbing_element);
180};
181
182/**
183 * \brief Create a rewriter.
184 * \param DataSpec A data specification.
185 * \param Strategy The rewrite strategy to be used by the rewriter.
186 * \return A (pointer to a) rewriter that uses the data specification DataSpec
187 * and strategy Strategy to rewrite.
188 **/
189std::shared_ptr<detail::Rewriter> createRewriter(const data_specification& DataSpec,
190 const used_data_equation_selector& equations_selector,
191 rewrite_strategy Strategy = jitty);
192
193/**
194 * \brief Check that an mCRL2 data equation is a valid rewrite rule. If not, an runtime_error is thrown indicating the problem.
195 * \param DataEqn The mCRL2 data equation to be checked.
196 * \throw std::runtime_error containing a reason why DataEqn is not a valid rewrite rule.
197 **/
198void CheckRewriteRule(const data_equation& data_eqn);
199
200/**
201 * \brief Check whether or not an mCRL2 data equation is a valid rewrite rule.
202 * \param DataEqn The mCRL2 data equation to be checked.
203 * \return Whether or not DataEqn is a valid rewrite rule.
204 **/
205bool isValidRewriteRule(const data_equation& data_eqn);
206
207
208// This function calculates the cumulated length of all
209// potential function arguments.
211{
212 sort_expression sort = op.sort();
213 std::size_t arity = 0;
214
215 while (is_function_sort(sort))
216 {
217 const function_sort fsort(sort);
218 const sort_expression_list& sort_dom = fsort.domain();
219 arity += sort_dom.size();
220 sort = fsort.codomain();
221 }
222 return arity;
223}
224
225// This function calculates the number of direct function arguments.
227{
228 sort_expression sort = op.sort();
229 if (is_function_sort(sort))
230 {
231 return atermpp::down_cast<function_sort>(sort).domain().size();
232 }
233 return 0;
234}
235
236} // namespace mcrl2::data::detail
237
238
239
240#endif // MCRL2_DATA_DETAIL_REWRITE_H
std::stack< std::reference_wrapper< _aterm > > m_todo
A reusable todo stack.
A unordered_map class in which aterms can be stored.
An abstraction expression.
Definition abstraction.h:23
const variable_list & variables() const
Definition abstraction.h:60
abstraction(const binder_type &binding_operator, const variable_list &variables, const data_expression &body)
Constructor.
Definition abstraction.h:39
const data_expression & body() const
Definition abstraction.h:65
const binder_type & binding_operator() const
Definition abstraction.h:55
\brief A data equation
data_expression & operator=(const data_expression &) noexcept=default
bool is_default_data_expression() const
A function to efficiently determine whether a data expression is made by the default constructor.
strategy create_a_rewriting_based_strategy(const function_symbol &f, const data_equation_list &rules1)
Definition strategy.cpp:44
strategy create_a_cpp_function_based_strategy(const function_symbol &f, const data_specification &data_spec)
Definition strategy.cpp:220
class rewrite_stack m_rewrite_stack
Definition jitty.h:84
void apply_cpp_code_to_higher_order_term(data_expression &result, const application &t, std::function< void(data_expression &, const data_expression &)> rewrite_cpp_code, ITERATOR begin, ITERATOR end, substitution_type &sigma)
void rewrite_aux_const_function_symbol(data_expression &result, const function_symbol &op, substitution_type &sigma)
Definition jitty.cpp:803
void thread_initialise() override
Definition jitty.h:131
rewrite_strategy getStrategy() override
Get rewriter strategy that is used.
Definition jitty.cpp:925
RewriterJitty(const data_specification &data_spec, const used_data_equation_selector &)
Definition jitty.cpp:169
void rewrite_aux(data_expression &result, const data_expression &term, substitution_type &sigma)
Rewrite a term with a given substitution and put the rewritten term in result.
Definition jitty.cpp:446
RewriterJitty(const RewriterJitty &other)=default
void add_normal_form_function(data_expression &t)
Definition jitty.cpp:92
strategy create_strategy(const function_symbol &f, const data_equation_list &rules1, const data_specification &data_spec)
Definition strategy.cpp:240
data_expression rewrite(const data_expression &term, substitution_type &sigma) override
Rewrite an mCRL2 data term.
Definition jitty.cpp:915
void apply_cpp_code_to_higher_order_term(data_expression &result, const application &t, const std::function< void(data_expression &, const data_expression &)> rewrite_cpp_code, ITERATOR begin, ITERATOR end, substitution_type &sigma)
Definition jitty.cpp:417
const function_symbol & this_term_is_in_normal_form()
Definition jitty.h:71
void subst_values(data_expression &result, const jitty_assignments_for_a_rewrite_rule &assignments, const data_expression &t, data::enumerator_identifier_generator &generator)
Definition jitty.cpp:224
RewriterJitty & operator=(const RewriterJitty &other)=delete
data_expression remove_normal_form_function(const data_expression &t)
Definition jitty.cpp:36
atermpp::detail::thread_aterm_pool * m_thread_aterm_pool
Definition jitty.h:91
void rebuild_strategy(const data_specification &data_spec, const mcrl2::data::used_data_equation_selector &equation_selector)
Definition jitty.cpp:147
void rewrite_aux_function_symbol(data_expression &result, const function_symbol &op, const application &term, substitution_type &sigma, std::size_t do_not_rewrite_first_arguments=0)
Definition jitty.cpp:583
std::vector< strategy > jitty_strat
Definition jitty.h:88
void rewrite(data_expression &result, const data_expression &term, substitution_type &sigma) override
Rewrite an mCRL2 data term.
Definition jitty.cpp:878
std::map< function_symbol, data_equation_list > jitty_eqns
Definition jitty.h:87
void make_jitty_strat_sufficiently_larger(std::size_t i)
Auxiliary function to take care that the array jitty_strat is sufficiently large to access element i.
Definition jitty.cpp:139
std::shared_ptr< detail::Rewriter > clone() override
Clone a rewriter.
Definition jitty.h:69
function_symbol this_term_is_in_normal_form_symbol
Definition jitty.h:81
std::vector< data_expression > rhs_for_constants_cache
Definition jitty.h:86
Rewriter interface class.
Definition rewrite.h:39
used_data_equation_selector data_equation_selector
Definition rewrite.h:54
mcrl2::data::data_specification m_data_specification_for_enumeration
Definition rewrite.h:169
virtual std::shared_ptr< detail::Rewriter > clone()=0
Clone a rewriter.
void rewrite_single_lambda(data_expression &result, const variable_list &vl, const data_expression &body, substitution_type &sigma, bool body_in_normal_form)
Definition rewrite.cpp:100
void rewrite_lambda_application(data_expression &result, const data_expression &t, substitution_type &sigma, bool arguments_are_in_normal_form=false)
Rewrite t, assuming that the headsymbol of t, which can be nested, is a lambda term.
Definition rewrite.cpp:205
void rewrite_where(data_expression &result, const where_clause &term, substitution_type &sigma)
Definition rewrite.cpp:61
virtual void rewrite(data_expression &result, const data_expression &term, substitution_type &sigma)=0
Rewrite an mCRL2 data term.
data_expression operator()(const data_expression &term, substitution_type &sigma)
Provide the rewriter with a () operator, such that it can also rewrite terms using this operator.
Definition rewrite.h:100
virtual ~Rewriter()=default
Destructor.
void existential_quantifier_enumeration(data_expression &result, const variable_list &vl, const data_expression &t1, substitution_type &sigma, bool t1_is_normal_form=false)
Definition rewrite.cpp:336
void existential_quantifier_enumeration(data_expression &result, const abstraction &t, substitution_type &sigma, bool t1_is_normal_form=false)
Definition rewrite.cpp:319
Rewriter(const Rewriter &other)=default
The copy constructor operator is protected. Public copying is not allowed.
void universal_quantifier_enumeration(data_expression &result, const variable_list &vl, const data_expression &t1, substitution_type &sigma, bool t1_is_normal_form=false)
Definition rewrite.cpp:362
virtual rewrite_strategy getStrategy()=0
Get rewriter strategy that is used.
void quantifier_enumeration(data_expression &result, const variable_list &vl, const data_expression &t1, bool t1_is_normal_form, substitution_type &sigma, const binder_type &binder, data_expression(*lazy_op)(const data_expression &, const data_expression &), const data_expression &identity_element, const data_expression &absorbing_element)
Definition rewrite.cpp:373
virtual data_expression rewrite(const data_expression &term, substitution_type &sigma)=0
Rewrite an mCRL2 data term.
void rewrite_lambda_application(data_expression &result, const abstraction &lambda_term, const application &t, substitution_type &sigma, bool arguments_are_in_normal_form=false)
Definition rewrite.cpp:230
void universal_quantifier_enumeration(data_expression &result, const abstraction &t, substitution_type &sigma, bool t1_is_normal_form=false)
Definition rewrite.cpp:348
data::enumerator_identifier_generator & identifier_generator()
The fresh name generator of the rewriter.
Definition rewrite.h:70
virtual void thread_initialise()
Definition rewrite.h:163
Rewriter(const data_specification &data_spec, const used_data_equation_selector &eq_selector)
Constructor. Do not use directly; use createRewriter() function instead.
Definition rewrite.h:60
Rewriter & operator=(const Rewriter &other)=default
The copy assignment operator is protected. Public copying is not allowed.
enumerator_identifier_generator m_generator
Definition rewrite.h:41
const std::set< std::size_t > & dependencies() const
Definition jitty.cpp:124
dependencies_rewrite_rule_pair(std::set< std::size_t > &dependencies, const data_equation &eq)
Definition jitty.cpp:120
mutable_indexed_substitution & m_sigma
Definition jitty.cpp:100
jitty_argument_rewriter(mutable_indexed_substitution<> &sigma, RewriterJitty &r)
Definition jitty.cpp:103
void operator()(data_expression &result, const data_expression &t)
Definition jitty.cpp:107
A strategy is a list of rules and the number of variables that occur in it.
\brief A function sort
const sort_expression & codomain() const
function_sort(const atermpp::aterm &term)
const sort_expression_list & domain() const
\brief A function symbol
const sort_expression & sort() const
\brief A sort expression
sort_expression & operator=(const sort_expression &) noexcept=default
Component for selecting a subset of equations that are actually used in an encompassing specification...
Definition selection.h:36
\brief A data variable
Definition variable.h:25
\brief A where expression
const data_expression & body() const
const assignment_expression_list & declarations() const
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:393
thread_aterm_pool & g_thread_term_pool()
A reference to the thread local term pool storage.
The main namespace for the aterm++ library.
void CheckRewriteRule(const data_equation &data_eqn)
Check that an mCRL2 data equation is a valid rewrite rule. If not, an runtime_error is thrown indicat...
Definition rewrite.cpp:582
data_expression remove_normal_form_function(const data_expression &t)
removes auxiliary expressions this_term_is_in_normal_form from data_expressions that are being rewrit...
bool isValidRewriteRule(const data_equation &data_eqn)
Check whether or not an mCRL2 data equation is a valid rewrite rule.
Definition rewrite.cpp:640
std::shared_ptr< detail::Rewriter > createRewriter(const data_specification &DataSpec, const used_data_equation_selector &equations_selector, rewrite_strategy Strategy=jitty)
Create a rewriter.
Definition rewrite.cpp:498
static bool match_jitty(const data_expression &t, const data_expression &p, jitty_assignments_for_a_rewrite_rule &assignments, const bool term_context_guarantees_normal_form)
Definition jitty.cpp:346
std::size_t getArity(const data::function_symbol &op)
Definition rewrite.h:210
std::size_t get_direct_arity(const data::function_symbol &op)
Definition rewrite.h:226
std::set< variable > bound_variables_in_substitution(const jitty_assignments_for_a_rewrite_rule &assignments)
Definition jitty.cpp:212
const data_expression & get_nested_head(const data_expression &t)
bool is_application(const data_expression &t)
Returns true if the term t is an application.
bool is_where_clause(const atermpp::aterm &x)
Returns true if the term t is a where clause.
bool is_abstraction(const atermpp::aterm &x)
Returns true if the term t is an abstraction.
bool is_exists_binder(const atermpp::aterm &x)
bool is_lambda_binder(const atermpp::aterm &x)
bool is_forall(const atermpp::aterm &x)
Returns true if the term t is a universal quantification.
bool is_function_symbol(const atermpp::aterm &x)
Returns true if the term t is a function symbol.
bool is_exists(const atermpp::aterm &x)
Returns true if the term t is an existential quantification.
bool is_function_sort(const atermpp::aterm &x)
Returns true if the term t is a function sort.
bool is_machine_number(const atermpp::aterm &x)
Returns true if the term t is a machine_number.
bool is_lambda(const atermpp::aterm &x)
Returns true if the term t is a lambda abstraction.
bool is_forall_binder(const atermpp::aterm &x)
bool is_variable(const atermpp::aterm &x)
Returns true if the term t is a variable.
jitty_variable_assignment_for_a_rewrite_rule * assignment
Definition jitty.h:38
jitty_assignments_for_a_rewrite_rule(jitty_variable_assignment_for_a_rewrite_rule *a)
Definition jitty.h:40
jitty_variable_assignment_for_a_rewrite_rule(const variable &m_var, const data_expression &m_term, bool m_nf)
Definition jitty.h:28