mCRL2
Loading...
Searching...
No Matches
suminst.h
Go to the documentation of this file.
1// Author(s): Jeroen Keiren
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 suminst.h
10/// \brief Instantiate summation variables.
11
12#ifndef MCRL2_LPS_SUMINST_H
13#define MCRL2_LPS_SUMINST_H
14
15#include "mcrl2/atermpp/set_operations.h"
16
17#include "mcrl2/data/enumerator.h"
18
19#include "mcrl2/lps/detail/lps_algorithm.h"
20
21namespace mcrl2::lps
22{
23
24/// \brief Return a set with all finite sorts in data specification s.
25inline
27{
28 const std::set<data::sort_expression>& sorts = s.sorts();
29 std::set<data::sort_expression> result;
30
31 for(const data::sort_expression& sort : sorts)
32 {
33 if(s.is_certainly_finite(sort))
34 {
35 result.insert(sort);
36 }
37 }
38 return result;
39}
40
41template<typename DataRewriter, typename Specification>
42class suminst_algorithm: public detail::lps_algorithm<Specification>
43{
44 using super = detail::lps_algorithm<Specification>;
46 using process_type = typename Specification::process_type;
47 using action_summand_type = typename process_type::action_summand_type;
49 using super::m_spec;
50
51 protected:
52 /// Sorts to be instantiated
54
55 /// Only instantiate tau summands
57
58 /// Rewriter
59 DataRewriter m_rewriter;
62
63 /// Statistiscs for verbose output
67
68 template <typename SummandType, typename Container>
70 {
71 using namespace data;
72 std::size_t nr_summands = 0; // Counter for the number of new summands, used for verbose output
73 std::deque< variable > variables; // The variables we need to consider in instantiating
74
75 // partition such that variables with finite sort precede those that do not
76 for (const variable& v: s.summation_variables())
77 {
78 if(m_sorts.find(v.sort()) != m_sorts.end())
79 {
80 if (m_spec.data().is_certainly_finite(v.sort()))
81 {
82 variables.push_front(v);
83 }
84 else
85 {
86 variables.push_back(v);
87 }
88 }
89 }
90
91 if (variables.empty())
92 {
93 // Nothing to be done, return original summand
94 result.push_back(s);
95 nr_summands = 1;
96 }
97 else
98 {
99 // List of variables with the instantiated variables removed (can be done upfront, which is more efficient,
100 // because we only need to calculate it once.
101 const variable_list vl(variables.begin(),variables.end());
102 variable_list new_summation_variables = term_list_difference(s.summation_variables(), vl);
103
104 try
105 {
106 mCRL2log(log::log_level_t::debug) << "enumerating variables " << vl << " in condition: " << data::pp(s.condition()) << std::endl;
107 data::mutable_indexed_substitution<> local_sigma;
108 m_enumerator.enumerate(enumerator_element(vl, s.condition()),
109 local_sigma,
110 [&](const enumerator_element& p)
111 {
112 mutable_indexed_substitution<> sigma;
113 p.add_assignments(vl, sigma, m_rewriter);
114 mCRL2log(log::log_level_t::debug) << "substitutions: " << sigma << std::endl;
115 SummandType t(s);
116 t.summation_variables() = new_summation_variables;
117 lps::rewrite(t, m_rewriter, sigma);
118 result.push_back(t);
119 ++nr_summands;
120 return false;
121 },
122 sort_bool::is_false_function_symbol
123 );
124 }
125 catch (mcrl2::runtime_error const& e)
126 {
127 // If an error occurs in enumerating, remove all summands that
128 // have been added to result thus far, and re-add the original.
129 // This prevents problems e.g. in case of a sort without constructors.
130 mCRL2log(log::log_level_t::debug) << "An error occurred in enumeration, removing already added summands, and keeping the original" << std::endl;
131 mCRL2log(log::log_level_t::debug) << e.what() << std::endl;
132
133 result.resize(result.size() - nr_summands);
134 result.push_back(s);
135 nr_summands = 1;
136 }
137 }
138 return nr_summands;
139 }
140
141 bool must_instantiate(const action_summand_type& summand)
142 {
143 return !m_tau_summands_only || summand.is_tau();
144 }
145
147 {
148 return !m_tau_summands_only;
149 }
150
151 template <typename SummandListType, typename Container>
152 void run(const SummandListType& list, Container& result)
153 {
154 for (typename SummandListType::const_iterator i = list.begin(); i != list.end(); ++i)
155 {
156 if (must_instantiate(*i))
157 {
158 std::size_t newsummands = instantiate_summand(*i, result);
159 if (newsummands > 0)
160 {
161 m_added += newsummands - 1;
162 }
163 else
164 {
165 ++m_deleted;
166 }
167 }
168 else
169 {
170 result.push_back(*i);
171 }
172 ++m_processed;
173 mCRL2log(log::log_level_t::status) << "Replaced " << m_processed << " summands by " << (m_processed + m_added - m_deleted)
174 << " summands (" << m_deleted << " were deleted)" << std::endl;
175 }
176 }
177
178 public:
179 suminst_algorithm(Specification& spec,
180 DataRewriter& r,
181 std::set<data::sort_expression> sorts = std::set<data::sort_expression>(),
182 bool tau_summands_only = false)
183 : detail::lps_algorithm<Specification>(spec),
184 m_sorts(sorts),
185 m_tau_summands_only(tau_summands_only),
186 m_rewriter(r),
188 {
189 if(sorts.empty())
190 {
191 mCRL2log(log::log_level_t::info) << "an empty set of sorts to be unfolded was provided; defaulting to all finite sorts" << std::endl;
192 m_sorts = finite_sorts(spec.data());
193 }
194 }
195
196 void run()
197 {
198 action_summand_vector_type action_summands;
199 deadlock_summand_vector deadlock_summands;
200 m_added = 0;
201 m_deleted = 0;
202 m_processed = 0;
203 run(m_spec.process().action_summands(), action_summands);
204 run(m_spec.process().deadlock_summands(), deadlock_summands);
205 m_spec.process().action_summands().swap(action_summands);
206 m_spec.process().deadlock_summands().swap(deadlock_summands);
207 mCRL2log(log::log_level_t::status) << std::endl;
208 }
209
210}; // suminst_algorithm
211
212} // namespace mcrl2::lps
213
214#endif // MCRL2_LPS_SUMINST_H
\brief A data variable
Definition variable.h:25
const sort_expression & sort() const
Definition variable.h:40
LPS summand containing a deadlock.
Algorithm class for algorithms on linear process specifications. It can be instantiated with lps::spe...
DataRewriter m_rewriter
Rewriter.
Definition suminst.h:59
void run(const SummandListType &list, Container &result)
Definition suminst.h:152
std::set< data::sort_expression > m_sorts
Sorts to be instantiated.
Definition suminst.h:53
data::enumerator_identifier_generator m_id_generator
Definition suminst.h:61
data::enumerator_algorithm m_enumerator
Definition suminst.h:60
std::size_t instantiate_summand(const SummandType &s, Container &result)
Definition suminst.h:69
std::size_t m_processed
Statistiscs for verbose output.
Definition suminst.h:64
bool must_instantiate(const action_summand_type &summand)
Definition suminst.h:141
bool must_instantiate(const deadlock_summand &)
Definition suminst.h:146
suminst_algorithm(Specification &spec, DataRewriter &r, std::set< data::sort_expression > sorts=std::set< data::sort_expression >(), bool tau_summands_only=false)
Definition suminst.h:179
bool m_tau_summands_only
Only instantiate tau summands.
Definition suminst.h:56
Standard exception class for reporting runtime errors.
Definition exception.h:27
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:435
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
The main namespace for the LPS library.
Definition constelm.h:18
std::set< data::sort_expression > finite_sorts(const data::data_specification &s)
Return a set with all finite sorts in data specification s.
Definition suminst.h:26