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
19namespace mcrl2
20{
21namespace data
22{
23namespace detail
24{
25
26/**
27 * \brief Rewriter interface class.
28 *
29 * This is the interface class for the rewriters. To create a specific
30 * rewriter, use createRewriter.
31 *
32 * Simple use of the rewriter would be as follow (with t a term in the mCRL2
33 * internal format):
34 *
35 * \code
36 * Rewriter *r = createRewriter(equations);
37 * t = r->rewrite(t);
38 * delete r;
39 * \endcode
40 **/
42{
43 protected:
45
46 /** \brief The copy assignment operator is protected. Public copying is not allowed.
47 **/
48 Rewriter& operator=(const Rewriter& other) = default;
49
50 /** \brief The copy constructor operator is protected. Public copying is not allowed.
51 **/
52 Rewriter(const Rewriter& other) = default;
53
54 public:
56
58 /**
59 * \brief Constructor. Do not use directly; use createRewriter()
60 * function instead.
61 * \sa createRewriter()
62 **/
63 Rewriter(const data_specification& data_spec, const used_data_equation_selector& eq_selector):
66 {
67 }
68
69 /** \brief Destructor. */
70 virtual ~Rewriter()
71 {
72 }
73
74 /** \brief The fresh name generator of the rewriter */
76 {
77 return m_generator;
78 }
79
80 /**
81 * \brief Get rewriter strategy that is used.
82 * \return Used rewriter strategy.
83 **/
85
86 /**
87 * \brief Rewrite an mCRL2 data term.
88 * \param Term The term to be rewritten. This term should be a data_term
89 * \return The normal form of Term.
90 **/
91 virtual data_expression rewrite(const data_expression& term, substitution_type& sigma) = 0;
92
93 /**
94 * \brief Rewrite an mCRL2 data term.
95 * \param Term The term to be rewritten. This term should be a data_term
96 * \return The normal form of Term.
97 **/
98 virtual void rewrite(data_expression& result, const data_expression& term, substitution_type& sigma) = 0;
99
100 /**
101 * \brief Provide the rewriter with a () operator, such that it can also
102 * rewrite terms using this operator.
103 **/
104
106 {
107 return rewrite(term,sigma);
108 }
109
110 /**
111 * \brief Clone a rewriter.
112 * \return A (pointer to a) a clone of the rewriter.
113 **/
114 virtual std::shared_ptr<detail::Rewriter> clone() = 0;
115
116 public:
117 /* The functions below are public, because they are used in the compiling jitty rewriter */
119 data_expression& result,
120 const abstraction& t,
121 substitution_type& sigma);
123 data_expression& result,
124 const variable_list& vl,
125 const data_expression& t1,
126 const bool t1_is_normal_form,
127 substitution_type& sigma);
128
130 data_expression& result,
131 const abstraction& t,
132 substitution_type& sigma);
134 data_expression& result,
135 const variable_list& vl,
136 const data_expression& t1,
137 const bool t1_is_normal_form,
138 substitution_type& sigma);
139
140 /* The functions below exist temporarily in the transformation of the jittyc rewriter to a rewrite_stack */
141 /* They ought to be removed. */
142 data_expression existential_quantifier_enumeration( // TODO: THIS SHOULD BE REMOVED IN DUE TIME.
143 const abstraction& t,
144 substitution_type& sigma)
145 {
146 data_expression result;
148 return result;
149 }
150 data_expression existential_quantifier_enumeration( // TODO: THIS SHOULD BE REMOVED IN DUE TIME.
151 const variable_list& vl,
152 const data_expression& t1,
153 const bool t1_is_normal_form,
154 substitution_type& sigma)
155 {
156 data_expression result;
157 existential_quantifier_enumeration(result, vl, t1, t1_is_normal_form, sigma);
158 return result;
159 }
160
161 data_expression universal_quantifier_enumeration( // TODO: THIS SHOULD BE REMOVED IN DUE TIME.
162 const abstraction& t,
163 substitution_type& sigma)
164 {
165 data_expression result;
167 return result;
168 }
169
170 data_expression universal_quantifier_enumeration( // TODO: THIS SHOULD BE REMOVED IN DUE TIME.
171 const variable_list& vl,
172 const data_expression& t1,
173 const bool t1_is_normal_form,
174 substitution_type& sigma)
175 {
176 data_expression result;
177 universal_quantifier_enumeration(result, vl, t1, t1_is_normal_form, sigma);
178 return result;
179 }
180
181 // Rewrite a where expression where the subdataexpressions are in internal format.
182 // It yields a term without a where expression. The result is passed back in the variable result.
183 void rewrite_where(data_expression& result,
184 const where_clause& term,
185 substitution_type& sigma);
186
187 data_expression rewrite_where(const where_clause& term, // TODO: THIS SHOULD BE REMOVED IN DUE TIME.
188 substitution_type& sigma);
189
190 // Rewrite an expression with a lambda as outermost symbol. The expression is in internal format.
191 // Bound variables are replaced by new variables to avoid a clash with variables in the right hand sides
192 // of sigma.
193
195 data_expression& result,
196 const variable_list& vl,
197 const data_expression& body,
198 const bool body_in_normal_form,
199 substitution_type& sigma);
200
201 data_expression rewrite_single_lambda( // TODO: THIS SHOULD BE REMOVED IN DUE TIME.
202 const variable_list& vl,
203 const data_expression& body,
204 const bool body_in_normal_form,
205 substitution_type& sigma)
206 {
207 data_expression result;
208 rewrite_single_lambda(result, vl, body, body_in_normal_form, sigma);
209 return result;
210 }
211
212 /// Rewrite t, assuming that the headsymbol of t, which can be nested, is a lambda term.
214 data_expression& result,
215 const data_expression& t,
216 substitution_type& sigma);
217
219 const data_expression& t,
220 substitution_type& sigma);
221
223 data_expression& result,
224 const abstraction& lambda_term,
225 const application& t,
226 substitution_type& sigma);
227
228 virtual void thread_initialise()
229 {
230 }
231
232 protected:
233
235
237 data_expression& result,
238 const variable_list& vl,
239 const data_expression& t1,
240 const bool t1_is_normal_form,
241 substitution_type& sigma,
242 const binder_type& binder,
243 data_expression (*lazy_op)(const data_expression&, const data_expression&),
244 const data_expression& identity_element,
245 const data_expression& absorbing_element);
246
247};
248
249/**
250 * \brief Create a rewriter.
251 * \param DataSpec A data specification.
252 * \param Strategy The rewrite strategy to be used by the rewriter.
253 * \return A (pointer to a) rewriter that uses the data specification DataSpec
254 * and strategy Strategy to rewrite.
255 **/
256std::shared_ptr<detail::Rewriter> createRewriter(
257 const data_specification& DataSpec,
258 const used_data_equation_selector& equations_selector,
259 const rewrite_strategy Strategy = jitty);
260
261/**
262 * \brief Check that an mCRL2 data equation is a valid rewrite rule. If not, an runtime_error is thrown indicating the problem.
263 * \param DataEqn The mCRL2 data equation to be checked.
264 * \throw std::runtime_error containing a reason why DataEqn is not a valid rewrite rule.
265 **/
266void CheckRewriteRule(const data_equation& data_eqn);
267
268/**
269 * \brief Check whether or not an mCRL2 data equation is a valid rewrite rule.
270 * \param DataEqn The mCRL2 data equation to be checked.
271 * \return Whether or not DataEqn is a valid rewrite rule.
272 **/
273bool isValidRewriteRule(const data_equation& data_eqn);
274
275
276// This function calculates the cumulated length of all
277// potential function arguments.
278inline std::size_t getArity(const data::function_symbol& op)
279{
280 sort_expression sort = op.sort();
281 std::size_t arity = 0;
282
283 while (is_function_sort(sort))
284 {
285 const function_sort fsort(sort);
286 const sort_expression_list& sort_dom = fsort.domain();
287 arity += sort_dom.size();
288 sort = fsort.codomain();
289 }
290 return arity;
291}
292
293// This function calculates the number of direct function arguments.
294inline std::size_t get_direct_arity(const data::function_symbol& op)
295{
296 sort_expression sort = op.sort();
297 if (is_function_sort(sort))
298 {
299 return atermpp::down_cast<function_sort>(sort).domain().size();
300 }
301 return 0;
302}
303
304} // namespace detail
305} // namespace data
306} // namespace mcrl2
307
308#endif
aterm_core & assign(const aterm_core &other, detail::thread_aterm_pool &pool) noexcept
Assignment operator, to be used if busy and forbidden flags are explicitly available.
size_type size() const
Returns the number of arguments of this term.
Definition aterm.h:151
bool operator==(const unprotected_aterm_core &t) const
Comparison operator.
Definition aterm_core.h:83
bool operator!=(const unprotected_aterm_core &t) const
Inequality operator on two unprotected aterms.
Definition aterm_core.h:92
An abstraction expression.
Definition abstraction.h:26
const variable_list & variables() const
Definition abstraction.h:63
abstraction(const binder_type &binding_operator, const variable_list &variables, const data_expression &body)
Constructor.
Definition abstraction.h:42
const data_expression & body() const
Definition abstraction.h:68
const binder_type & binding_operator() const
Definition abstraction.h:58
\brief A data equation
data_equation(const data_equation &) noexcept=default
Move semantics.
data_expression & operator=(const data_expression &) noexcept=default
data_expression & operator=(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.
const function_symbol_vector & constructors() const
Gets all constructors including those that are system defined.
const function_symbol_vector & mappings() const
Gets all mappings in this specification including those that are system defined.
std::shared_ptr< detail::Rewriter > clone()
Clone a rewriter.
Definition jitty.h:72
strategy create_a_rewriting_based_strategy(const function_symbol &f, const data_equation_list &rules1)
Definition strategy.cpp:46
void rewrite(data_expression &result, const data_expression &term, substitution_type &sigma)
Rewrite an mCRL2 data term.
Definition jitty.cpp:893
rewrite_strategy getStrategy()
Get rewriter strategy that is used.
Definition jitty.cpp:940
strategy create_a_cpp_function_based_strategy(const function_symbol &f, const data_specification &data_spec)
Definition strategy.cpp:222
class rewrite_stack m_rewrite_stack
Definition jitty.h:90
void rewrite_aux_const_function_symbol(data_expression &result, const function_symbol &op, substitution_type &sigma)
Definition jitty.cpp:817
data_expression rewrite(const data_expression &term, substitution_type &sigma)
Rewrite an mCRL2 data term.
Definition jitty.cpp:930
RewriterJitty(const data_specification &data_spec, const used_data_equation_selector &)
Definition jitty.cpp:166
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:456
RewriterJitty(const RewriterJitty &other)=default
strategy create_strategy(const function_symbol &f, const data_equation_list &rules1, const data_specification &data_spec)
Definition strategy.cpp:242
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:427
const function_symbol & this_term_is_in_normal_form()
Definition jitty.h:77
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
void rewrite_aux_function_symbol(data_expression &result, const function_symbol &op, const application &term, substitution_type &sigma)
Definition jitty.cpp:592
data_expression remove_normal_form_function(const data_expression &t)
Definition jitty.cpp:38
atermpp::detail::thread_aterm_pool * m_thread_aterm_pool
Definition jitty.h:96
void rebuild_strategy(const data_specification &data_spec, const mcrl2::data::used_data_equation_selector &equation_selector)
Definition jitty.cpp:144
std::vector< strategy > jitty_strat
Definition jitty.h:94
std::map< function_symbol, data_equation_list > jitty_eqns
Definition jitty.h:93
function_symbol this_term_is_in_normal_form_symbol
Definition jitty.h:87
void make_jitty_strat_sufficiently_larger(const std::size_t i)
Auxiliary function to take care that the array jitty_strat is sufficiently large to access element i.
Definition jitty.cpp:136
std::vector< data_expression > rhs_for_constants_cache
Definition jitty.h:92
Rewriter interface class.
Definition rewrite.h:42
used_data_equation_selector data_equation_selector
Definition rewrite.h:57
mcrl2::data::data_specification m_data_specification_for_enumeration
Definition rewrite.h:234
virtual std::shared_ptr< detail::Rewriter > clone()=0
Clone a rewriter.
data_expression universal_quantifier_enumeration(const abstraction &t, substitution_type &sigma)
Definition rewrite.h:161
void rewrite_lambda_application(data_expression &result, const data_expression &t, substitution_type &sigma)
Rewrite t, assuming that the headsymbol of t, which can be nested, is a lambda term.
Definition rewrite.cpp:214
void rewrite_lambda_application(data_expression &result, const abstraction &lambda_term, const application &t, substitution_type &sigma)
Definition rewrite.cpp:256
void rewrite_where(data_expression &result, const where_clause &term, substitution_type &sigma)
Definition rewrite.cpp:70
void existential_quantifier_enumeration(data_expression &result, const abstraction &t, substitution_type &sigma)
Definition rewrite.cpp:334
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:105
void existential_quantifier_enumeration(data_expression &result, const variable_list &vl, const data_expression &t1, const bool t1_is_normal_form, substitution_type &sigma)
Definition rewrite.cpp:350
data_expression existential_quantifier_enumeration(const abstraction &t, substitution_type &sigma)
Definition rewrite.h:142
void universal_quantifier_enumeration(data_expression &result, const abstraction &t, substitution_type &sigma)
Definition rewrite.cpp:362
data_expression universal_quantifier_enumeration(const variable_list &vl, const data_expression &t1, const bool t1_is_normal_form, substitution_type &sigma)
Definition rewrite.h:170
Rewriter(const Rewriter &other)=default
The copy constructor operator is protected. Public copying is not allowed.
virtual rewrite_strategy getStrategy()=0
Get rewriter strategy that is used.
mutable_indexed_substitution substitution_type
Definition rewrite.h:55
virtual data_expression rewrite(const data_expression &term, substitution_type &sigma)=0
Rewrite an mCRL2 data term.
data_expression existential_quantifier_enumeration(const variable_list &vl, const data_expression &t1, const bool t1_is_normal_form, substitution_type &sigma)
Definition rewrite.h:150
data_expression rewrite_single_lambda(const variable_list &vl, const data_expression &body, const bool body_in_normal_form, substitution_type &sigma)
Definition rewrite.h:201
data_expression rewrite_where(const where_clause &term, substitution_type &sigma)
Definition rewrite.cpp:61
data_expression rewrite_lambda_application(const data_expression &t, substitution_type &sigma)
Definition rewrite.cpp:241
virtual ~Rewriter()
Destructor.
Definition rewrite.h:70
data::enumerator_identifier_generator & identifier_generator()
The fresh name generator of the rewriter.
Definition rewrite.h:75
void rewrite_single_lambda(data_expression &result, const variable_list &vl, const data_expression &body, const bool body_in_normal_form, substitution_type &sigma)
Definition rewrite.cpp:109
virtual void thread_initialise()
Definition rewrite.h:228
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:63
Rewriter & operator=(const Rewriter &other)=default
The copy assignment operator is protected. Public copying is not allowed.
void universal_quantifier_enumeration(data_expression &result, const variable_list &vl, const data_expression &t1, const bool t1_is_normal_form, substitution_type &sigma)
Definition rewrite.cpp:375
enumerator_identifier_generator m_generator
Definition rewrite.h:44
void quantifier_enumeration(data_expression &result, const variable_list &vl, const data_expression &t1, const 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:386
const std::set< std::size_t > & dependencies() const
Definition jitty.cpp:121
dependencies_rewrite_rule_pair(std::set< std::size_t > &dependencies, const data_equation &eq)
Definition jitty.cpp:117
mutable_indexed_substitution & m_sigma
Definition jitty.cpp:97
jitty_argument_rewriter(mutable_indexed_substitution<> &sigma, RewriterJitty &r)
Definition jitty.cpp:100
void operator()(data_expression &result, const data_expression &t)
Definition jitty.cpp:104
void increase(std::size_t distance)
data_expression & element(std::size_t pos, std::size_t frame_size)
void decrease(std::size_t distance)
A strategy is a list of rules and the number of variables that occur in it.
std::size_t number_of_variables() const
Provides the maximal number of variables used in the rewrite rules making up this strategy.
\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
function_symbol(const std::string &name, const sort_expression &sort)
Constructor.
\brief A sort expression
sort_expression & operator=(const sort_expression &) noexcept=default
\brief Unknown sort expression
untyped_sort()
\brief Default constructor X3.
Component for selecting a subset of equations that are actually used in an encompassing specification...
Definition selection.h:37
\brief A data variable
Definition variable.h:28
\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:391
thread_aterm_pool & g_thread_term_pool()
A reference to the thread local term pool storage.
The main namespace for the aterm++ library.
Definition algorithm.h:21
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:597
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:655
std::size_t recursive_number_of_args(const data_expression &t)
const data_expression & get_argument_of_higher_order_term(const application &t, std::size_t i)
std::shared_ptr< detail::Rewriter > createRewriter(const data_specification &DataSpec, const used_data_equation_selector &equations_selector, const rewrite_strategy Strategy=jitty)
Create a rewriter.
Definition rewrite.cpp:513
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:355
std::size_t getArity(const data::function_symbol &op)
Definition rewrite.h:278
std::size_t get_direct_arity(const data::function_symbol &op)
Definition rewrite.h:294
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)
Namespace for all data library functionality.
Definition data.cpp:22
atermpp::term_list< assignment_expression > assignment_expression_list
\brief list of assignment_expressions
Definition assignment.h:50
std::vector< assignment > assignment_vector
\brief vector of assignments
Definition assignment.h:149
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.
std::vector< variable > variable_vector
\brief vector of variables
Definition variable.h:89
atermpp::term_list< sort_expression > sort_expression_list
\brief list of sort_expressions
rewrite_strategy
The strategy of the rewriter.
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.
atermpp::term_list< data_equation > data_equation_list
\brief list of data_equations
atermpp::term_list< variable > variable_list
\brief list of variables
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.
atermpp::term_list< assignment > assignment_list
\brief list of assignments
Definition assignment.h:146
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)
std::vector< function_symbol > function_symbol_vector
\brief vector of function_symbols
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:40
jitty_assignments_for_a_rewrite_rule(jitty_variable_assignment_for_a_rewrite_rule *a)
Definition jitty.h:42
jitty_variable_assignment_for_a_rewrite_rule(const variable &m_var, const data_expression &m_term, bool m_nf)
Definition jitty.h:30
std::size_t operator()(const atermpp::detail::reference_aterm< T > &t) const