mCRL2
Loading...
Searching...
No Matches
strategy_rule.h
Go to the documentation of this file.
1// Author(s): Jan Friso Groote
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 strategy_rule.h
10
11#ifndef MCRL2_DATA_DETAIL_REWRITE_STRATEGY_RULE_H
12#define MCRL2_DATA_DETAIL_REWRITE_STRATEGY_RULE_H
13
14#include "mcrl2/data/data_equation.h"
15
16
17
18namespace mcrl2::data::detail
19{
20
21/// \brief Is either a rewrite rule to be matched or an index that should be rewritten.
23{
24 protected:
25 // Only one of the fields rewrite_rule, rewrite_index or cpp_function will be used
26 // at any given time. As this hardly requires a lot of memory, we do not optimise
27 // this using for instance a union type.
30 size_t m_rewrite_index = 0UL;
32
33 public:
34 strategy_rule(const std::size_t n)
35 : m_strategy_element_type(strategy_element_type::rewrite_index_type),
37 {}
38
39 strategy_rule(const std::function<void(data_expression&, const data_expression&)> f)
40 : m_strategy_element_type(strategy_element_type::cpp_function_type),
42 {}
43
45 : m_strategy_element_type(strategy_element_type::data_equation_type),
47 {}
48
49 bool is_rewrite_index() const
50 {
51 return m_strategy_element_type==strategy_element_type::rewrite_index_type;
52 }
53
54 bool is_cpp_code() const
55 {
56
57 return m_strategy_element_type==strategy_element_type::cpp_function_type;
58 }
59
60 bool is_equation() const
61 {
62 return m_strategy_element_type==strategy_element_type::data_equation_type;
63 }
64
65 const data_equation& equation() const
66 {
67 assert(is_equation());
69 return m_rewrite_rule;
70 }
71
73 {
74 assert(is_rewrite_index());
75 return m_rewrite_index;
76 }
77
79 {
80 assert(is_cpp_code());
81 return m_cpp_function;
82 }
83};
84
85/// A strategy is a list of rules and the number of variables that occur in it.
87{
88 protected:
91
92 public:
93 /// \brief Default constructor.
94 strategy(size_t n, const std::vector<strategy_rule>& r)
96 m_rules(r)
97 {}
98
99 /// \brief Default constructor.
102 {}
103
104 /// \brief Provides the maximal number of variables used in the rewrite rules making up this strategy.
106 {
107 return m_number_of_variables;
108 }
109
110 /// \brief Yield the rules of the strategy.
111 const std::vector<strategy_rule>& rules() const
112 {
113 return m_rules;
114 }
115
116};
117
118/// \brief Creates a strategy for given set of rewrite rules with head symbol f.
119strategy create_strategy(data_equation_list rules);
120
121} // namespace mcrl2::data::detail
122
123
124
125#endif // MCRL2_DATA_DETAIL_REWRITE_STRATEGY_RULE_H
std::stack< std::reference_wrapper< _aterm > > m_todo
A reusable todo stack.
Generates unique function symbols with a given prefix.
std::size_t m_initial_index
Cache the value that is set in the constructor.
void clear()
Restores the index back to the value that was initially assigned in the constructor.
std::size_t m_index
A reference to the index as present in the function symbol generator.
std::string m_string_buffer
A local string cache to prevent allocating new strings for every function symbol generated.
function_symbol operator()(std::size_t arity=0)
Generates a unique function symbol with the given prefix followed by a number.
function_symbol operator()(const std::string &prefix)
Generate a unique function symbol, based on the prefix, followed by a number.
function_symbol_generator(const std::string &prefix)
Constructor.
std::shared_ptr< std::size_t > m_central_index
The address of the central index for this prefix.
A unordered_map class in which aterms can be stored.
A vector class in which aterms can be stored.
Definition vector.h:40
const value_type * data() const noexcept
Definition vector.h:200
std::size_t size() const
Definition vector.h:348
reference back()
Definition vector.h:196
iterator insert(const_iterator pos, InputIt first, InputIt last)
Definition vector.h:273
size_type max_size() const noexcept
Definition vector.h:225
const_iterator end() const noexcept
Definition vector.h:207
iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition vector.h:280
iterator insert(const_iterator pos, T &&value)
Definition vector.h:260
void mark(term_mark_stack &todo) const
Marks all stored terms during garbage collection; used when this container is an element of another p...
Definition vector.h:357
const_reference at(size_type pos) const
Definition vector.h:188
void swap(vector &other) noexcept
Definition vector.h:342
auto operator<=>(const vector &other) const
Definition vector.h:367
const_iterator cbegin() const noexcept
Definition vector.h:204
super m_container
Definition vector.h:44
iterator emplace(const_iterator pos, Args &&... args)
Definition vector.h:287
reference front()
Definition vector.h:193
size_type capacity() const noexcept
Definition vector.h:236
const_reference front() const
Definition vector.h:194
void push_back(const T &value)
Definition vector.h:305
iterator insert(const_iterator pos, size_type count, const T &value)
Definition vector.h:266
vector(const vector &x, const allocator_type &alloc)
Constructor.
Definition vector.h:113
void push_back(T &&value)
Definition vector.h:311
allocator_type get_allocator() const noexcept
Definition vector.h:163
iterator begin() noexcept
Definition vector.h:202
iterator erase(const_iterator first, const_iterator last)
Definition vector.h:299
vector(const allocator_type &alloc)
Constructor.
Definition vector.h:83
~vector()=default
Standard destructor.
void resize(size_type count, const value_type &value)
Definition vector.h:336
static auto acquire_guard()
Acquires the lock that structural modifications must hold: exclusive when ThreadSafe,...
Definition vector.h:49
vector(InputIterator first, InputIterator last, const allocator_type &alloc=allocator_type())
Constructor.
Definition vector.h:101
const_reverse_iterator rend() const noexcept
Definition vector.h:215
void shrink_to_fit()
Definition vector.h:241
const_reverse_iterator crbegin() const noexcept
Definition vector.h:212
const_reference back() const
Definition vector.h:197
reference operator[](size_type pos)
Definition vector.h:190
const_reverse_iterator rbegin() const noexcept
Definition vector.h:211
value_type * data() noexcept
Definition vector.h:199
reverse_iterator rbegin() noexcept
Definition vector.h:210
detail::generic_aterm_container< super > container_wrapper
Definition vector.h:45
reverse_iterator rend() noexcept
Definition vector.h:214
void assign(std::initializer_list< value_type > ilist)
Definition vector.h:181
iterator end() noexcept
Definition vector.h:206
void assign(size_type count, const value_type &value)
Definition vector.h:168
vector(vector &&x, const allocator_type &alloc)
Constructor.
Definition vector.h:126
vector(size_type n, const allocator_type &alloc=allocator_type())
Constructor.
Definition vector.h:89
iterator erase(const_iterator pos)
Definition vector.h:293
void reserve(size_type new_cap)
Definition vector.h:230
iterator insert(const_iterator pos, const T &value)
Definition vector.h:253
vector(const vector &x)
Constructor.
Definition vector.h:107
void resize(size_type count)
Definition vector.h:330
const_iterator cend() const noexcept
Definition vector.h:208
const_reverse_iterator crend() const noexcept
Definition vector.h:216
bool empty() const noexcept
Definition vector.h:218
reference at(size_type pos)
Definition vector.h:187
vector & operator=(vector &&x) noexcept
Move assignment operator.
Definition vector.h:149
void assign(InputIt first, InputIt last)
Definition vector.h:175
void clear() noexcept
Definition vector.h:247
vector(vector &&x) noexcept
Constructor.
Definition vector.h:119
bool operator==(const vector &other) const
Definition vector.h:365
vector & operator=(const vector &x)
Assignment operator.
Definition vector.h:138
const_iterator begin() const noexcept
Definition vector.h:203
void pop_back()
Definition vector.h:324
reference emplace_back(Args &&... args)
Definition vector.h:318
vector()
Default constructor.
Definition vector.h:77
const_reference operator[](size_type pos) const
Definition vector.h:191
vector(size_type n, const value_type &val, const allocator_type &alloc=allocator_type())
Definition vector.h:94
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:170
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:101
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:164
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
const data_expression & get_element(std::size_t pos, std::size_t frame_size) const
void increase(std::size_t distance)
void set_element(std::size_t pos, std::size_t frame_size, const data_expression &d)
data_expression & element(std::size_t pos, std::size_t frame_size)
void decrease(std::size_t distance)
atermpp::vector< data_expression >::const_iterator stack_iterator(std::size_t pos, std::size_t frame_size) const
Is either a rewrite rule to be matched or an index that should be rewritten.
std::function< void(data_expression &, const data_expression &)> m_cpp_function
std::function< void(data_expression &, const data_expression &)> rewrite_cpp_code() const
const data_equation & equation() const
strategy_rule(const data_equation &eq)
strategy_rule(const std::size_t n)
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.
const std::vector< strategy_rule > & rules() const
Yield the rules of the strategy.
strategy(size_t n, const std::vector< strategy_rule > &r)
Default constructor.
strategy()
Default constructor.
std::vector< strategy_rule > m_rules
core::identifier_string operator()()
Generates a unique function symbol with the given prefix followed by a number.
enumerator_identifier_generator(const std::string &prefix="x_")
Constructor.
core::identifier_string operator()(const std::string &prefix)
Generates a unique constant function symbol with the given prefix followed by a number.
\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
const expression_type & operator()(const variable_type &v) const
Application operator; applies substitution to v.
mutable_indexed_substitution(const VARIABLES &vars, const EXPRESSIONS &exps)
mutable_indexed_substitution< VariableType, ExpressionType > clone()
Create a clone of the rewriter in which the underlying rewriter is copied, and not passed as a shared...
void apply(const variable_type &v, expression_type &target, atermpp::detail::thread_aterm_pool &thread_aterm_pool)
Application operator; applies substitution to v.
bool size()
Returns the number of assigned variables in the substitution.
bool operator==(const Substitution &) const
Compare substitutions.
const std::multiset< variable_type > & variables_occurring_in_right_hand_sides() const
Provides a multiset containing the variables in the rhs.
void apply(const variable_type &v, ResultType &target) const
Application operator; applies substitution to v.
bool empty()
Returns true if the substitution is empty.
assignment operator[](variable_type const &v)
Index operator.
substitution_type m_substitution
Internal storage for substitutions. Required to be a container with random access through [] operator...
std::string to_string() const
string representation of the substitution. N.B. This is an expensive operation!
bool variable_occurs_in_a_rhs(const variable &v)
Checks whether a variable v occurs in one of the rhs's of the current substitution.
mutable_indexed_substitution(const substitution_type &substitution, const bool variables_in_rhs_set_is_defined, const std::multiset< variable_type > &variables_in_rhs)
\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
used_data_equation_selector(const data_specification &specification, const std::set< function_symbol > &function_symbols, const std::set< data::variable > &global_variables, const bool do_not_remove_function_symbols)
Definition selection.h:164
void add_data_specification_symbols(const data_specification &specification)
Definition selection.h:55
used_data_equation_selector()
default constructor
Definition selection.h:151
used_data_equation_selector(const data_specification &)
select all equations
Definition selection.h:181
void add_function_symbols(const data_expression &t)
Definition selection.h:145
used_data_equation_selector(data_specification const &data_spec, Range const &context)
context is a range of function symbols
Definition selection.h:157
void add_symbol(const function_symbol &f)
Definition selection.h:43
bool operator()(const data_equation &e) const
Check whether data equation relates to used symbols, and therefore is important.
Definition selection.h:131
bool operator()(const data::function_symbol &f) const
Check whether the symbol is used.
Definition selection.h:121
std::set< function_symbol > m_used_symbols
Definition selection.h:39
\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:435
thread_aterm_pool & g_thread_term_pool()
A reference to the thread local term pool storage.
The main namespace for the aterm++ library.
static std::size_t & generator_sequence_number()
static std::mutex & function_symbol_generator_mutex()
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
std::shared_ptr< detail::Rewriter > createRewriter(const data_specification &DataSpec, const used_data_equation_selector &equations_selector, rewrite_strategy Strategy=data::rewrite_strategy::jitty)
Create a rewriter.
Definition rewrite.cpp:498
strategy create_strategy(data_equation_list rules)
Creates a strategy for given set of rewrite rules with head symbol f.
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
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:211
std::size_t get_direct_arity(const data::function_symbol &op)
Definition rewrite.h:227
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 system defined sort bool_.
Definition bool.h:29
const basic_sort & bool_()
Constructor for sort expression Bool.
Definition bool.h:41
const function_symbol & and_()
Constructor for function symbol &&.
Definition bool.h:232
const function_symbol & or_()
Constructor for function symbol ||.
Definition bool.h:296
const function_symbol & not_()
Constructor for function symbol !.
Definition bool.h:170
std::multiset< variable > substitution_variables(const mutable_indexed_substitution< VariableType, ExpressionType > &sigma)
bool is_data_equation(const atermpp::aterm &t)
Recognizer function.
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::ostream & operator<<(std::ostream &out, const mutable_indexed_substitution< VariableType, ExpressionType > &sigma)
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)
function_symbol if_(const sort_expression &s)
Constructor for function symbol if.
Definition standard.h:205
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
Wrapper class for internal storage and substitution updates using operator()
mutable_indexed_substitution< VariableType, ExpressionType > & m_super
assignment(const variable_type &v, mutable_indexed_substitution< VariableType, ExpressionType > &super)
Constructor.
assignment & operator=(const expression_type &e)
Actual assignment.