mCRL2
Loading...
Searching...
No Matches
selection.h
Go to the documentation of this file.
1// Author(s): Jeroen van der Wulp
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/selection.h
10/// \brief Provides selection utility functionality
11
12#ifndef MCRL2_DATA_SELECTION_H
13#define MCRL2_DATA_SELECTION_H
14
15#include "mcrl2/data/data_specification.h"
16#include "mcrl2/data/find.h"
17#include "mcrl2/data/fset.h"
18
19
20
21namespace mcrl2::data
22{
23
24/** \brief Component for selecting a subset of equations that are actually used in an encompassing specification
25 *
26 * This component can be used with the constructor of data::rewriter
27 * derived classes to select a smaller set of equations that are used as
28 * rewrite rules. This limited set of rewrite rules should be enough for the
29 * purpose of rewriting objects that occur in the encompassing
30 * specification (the context).
31 *
32 * \note Use of this component can have a dramatic effect of rewriter
33 * initialisation time and overall performance.
34 **/
36{
37 private:
38
40
41 bool add_all;
42
44 {
45 m_used_symbols.insert(f);
46 }
47
48 template < typename Range >
49 void add_symbols(Range const& r)
50 {
51 m_used_symbols.insert(r.begin(), r.end());
52 }
53
54 protected:
56 {
57 // Always add if:Bool#Bool#Bool->Bool; This symbol is used in the prover.
59 // Always add and,or:Bool#Bool->Bool and not:Bool->Bool; This symbol is generated when eliminating quantifiers
63
64 // Add all constructors of all sorts as they may be used when enumerating over these sorts
65 std::set< sort_expression > sorts(specification.sorts().begin(),specification.sorts().end());
66 for (const sort_expression& sort: sorts)
67 {
68 add_symbols(specification.constructors(sort));
69 // Always add equality and inequality of each sort. The one point rewriter is using this for instance.
70 add_symbol(equal_to(sort));
71 add_symbol(not_equal_to(sort));
72
73 // Always add insert for an FSet(S) function, as it is used when enumerating the elements of an FSet.
74 if (sort_fset::is_fset(sort))
75 {
76 const container_sort &s = atermpp::down_cast<container_sort>(sort);
77 add_symbol(sort_fset::insert(s.element_sort()));
78 }
79
80 // Always add the if:Bool#S#S->S for every sort as enumerating elements over function sorts S1 #... # S # ... # Sn -> S
81 // rewriting over functions of this shape.
82 add_symbol(if_(sort));
83 }
84
85 std::map< data_equation, std::set< function_symbol > > symbols_for_equation;
86
87 for (const data_equation& equation: specification.equations())
88 {
89 std::set< function_symbol > used_symbols;
90
91 data::detail::make_find_function_symbols_traverser<data::data_expression_traverser>(std::inserter(used_symbols, used_symbols.end())).apply(equation.lhs());
92 symbols_for_equation[equation].swap(used_symbols);
93 }
94
95 /* Calculate the closure under the function symbols that can be introduced by applying equations, such that all
96 the function symbols that can occur during rewriting are present in the datatype */
97 std::vector<bool> equations_of_which_symbols_have_been_added(specification.equations().size(),false);
98 bool stable=false;
99 while (!stable)
100 {
101 stable=true;
102 std::size_t equation_count=0;
103 for (const data_equation& e: specification.equations())
104 {
105 if (!equations_of_which_symbols_have_been_added[equation_count] &&
106 std::includes(m_used_symbols.begin(), m_used_symbols.end(), symbols_for_equation[e].begin(), symbols_for_equation[e].end()))
107 {
108 add_function_symbols(e.rhs());
109 add_function_symbols(e.condition());
110 equations_of_which_symbols_have_been_added[equation_count]=true;
111 stable=false;
112 }
113 equation_count++;
114 }
115 }
116 }
117
118 public:
119
120 /// \brief Check whether the symbol is used.
121 bool operator()(const data::function_symbol& f) const
122 {
123 if (add_all)
124 {
125 return true;
126 }
127 return m_used_symbols.count(f)>0;
128 }
129
130 /// \brief Check whether data equation relates to used symbols, and therefore is important.
131 bool operator()(const data_equation& e) const
132 {
133 if (add_all)
134 {
135 return true;
136 }
137
138 std::set< function_symbol > used_symbols;
139
140 data::detail::make_find_function_symbols_traverser<data::data_expression_traverser>(std::inserter(used_symbols, used_symbols.end())).apply(e.lhs());
141
142 return std::includes(m_used_symbols.begin(), m_used_symbols.end(), used_symbols.begin(), used_symbols.end());
143 }
144
146 {
147 data::detail::make_find_function_symbols_traverser<data::data_expression_traverser>(std::inserter(m_used_symbols, m_used_symbols.end())).apply(t);
148 }
149
150 /// \brief default constructor
152 add_all(true)
153 {}
154
155 /// \brief context is a range of function symbols
156 template <typename Range>
157 used_data_equation_selector(data_specification const& data_spec, Range const& context):
158 add_all(false)
159 {
160 add_symbols(context);
162 }
163
165 const std::set<function_symbol>& function_symbols,
166 const std::set<data::variable>& global_variables,
167 const bool do_not_remove_function_symbols
168 ):add_all(do_not_remove_function_symbols)
169 {
170 // Compensate for symbols that could be used as part of an instantiation of free variables
171 for (const variable& global_variable: global_variables)
172 {
173 add_symbols(specification.constructors(global_variable.sort()));
174 add_symbols(specification.mappings(global_variable.sort()));
175 }
176 m_used_symbols.insert(function_symbols.begin(), function_symbols.end());
178 }
179
180 /// \brief select all equations
182 add_all(true)
183 {
184 /* add_symbols(specification.constructors());
185 add_symbols(specification.mappings());
186 add_data_specification_symbols(specification); */
187 }
188};
189
190} // namespace mcrl2::data
191
192
193
194#endif // MCRL2_DATA_SELECTION_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.
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
A strategy is a list of rules and the number of variables that occur in it.
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
\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
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
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)
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