mCRL2
Loading...
Searching...
No Matches
enumerator_with_iterator.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
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/enumerator_with_iterator.h
10/// \brief add your file description here.
11
12#ifndef MCRL2_DATA_ENUMERATOR_WITH_ITERATOR_H
13#define MCRL2_DATA_ENUMERATOR_WITH_ITERATOR_H
14
15#include "mcrl2/atermpp/detail/aterm_configuration.h"
16#include "mcrl2/data/enumerator.h"
17
18namespace mcrl2::data
19{
20
22{
23 bool operator()(const data_expression& x) const
24 {
26 }
27};
28
30{
31 bool operator()(const data_expression& x) const
32 {
34 }
35};
36
37/// \brief An enumerator algorithm that generates solutions of a condition.
38template <typename Rewriter = data::rewriter, typename DataRewriter = data::rewriter>
40{
41 protected:
42 using super = enumerator_algorithm<Rewriter, DataRewriter>;
43 using super::r;
44 using super::R;
45 using super::dataspec;
46 using super::id_generator;
47 using super::m_max_count;
48 using super::rewrite;
49
50 /// \brief throw_exceptions If true, an exception is thrown when the enumeration is aborted.
52
53 template <typename EnumeratorListElement>
54 void cannot_enumerate(EnumeratorListElement& e, const std::string& msg) const
55 {
56 e.invalidate();
58 {
59 throw mcrl2::runtime_error(msg);
60 }
61 }
62
63 // add element without additional variables
64 template <typename EnumeratorListElement, IsSubstitution MutableSubstitution, typename Filter, typename Expression>
65 void add_element(enumerator_queue<EnumeratorListElement>& P,
66 MutableSubstitution& sigma,
67 Filter accept,
68 const data::variable_list& variables,
69 const Expression& phi,
70 const EnumeratorListElement& p,
71 const data::variable& v,
72 const data::data_expression& e) const
73 {
74 auto phi1 = rewrite(phi, sigma);
75 if (accept(phi1))
76 {
77 P.push_back(EnumeratorListElement(variables, phi1, p, v, e));
78 }
79 }
80
81 // add element with additional variables
82 template <typename EnumeratorListElement, IsSubstitution MutableSubstitution, typename Filter, typename Expression>
83 void add_element(enumerator_queue<EnumeratorListElement>& P,
84 MutableSubstitution& sigma,
85 Filter accept,
86 const data::variable_list& variables,
87 const data::variable_list& added_variables,
88 const Expression& phi,
89 const EnumeratorListElement& p,
90 const data::variable& v,
91 const data::data_expression& e) const
92 {
93 auto phi1 = rewrite(phi, sigma);
94 if (accept(phi1))
95 {
96 // Additional variables are put at the end of the list!
97 P.push_back(EnumeratorListElement(variables + added_variables, phi1, p, v, e));
98 }
99 }
100
101 // specialization for enumerator_list_element; in this case we are not interested in the substitutions,
102 // and this allows an optimization
103 template <IsSubstitution MutableSubstitution, typename Filter, typename Expression>
105 MutableSubstitution& sigma,
106 Filter accept,
107 const data::variable_list& variables,
108 const data::variable_list& added_variables,
109 const Expression& phi,
110 const enumerator_list_element<Expression>& p,
111 const data::variable& v,
112 const data::data_expression& e) const
113 {
114 auto phi1 = rewrite(phi, sigma);
115 if (accept(phi1))
116 {
117 if (phi1 == phi)
118 {
119 // Discard the added_variables, since we know they do not appear in phi1
120 P.push_back(enumerator_list_element<Expression>(variables, phi1, p, v, e));
121 }
122 else
123 {
124 // Additional variables are put at the end of the list!
125 P.push_back(enumerator_list_element<Expression>(variables + added_variables, phi1, p, v, e));
126 }
127 }
128 }
129
130 public:
132 const data::data_specification& dataspec,
133 const DataRewriter& r,
135 std::size_t max_count = (std::numeric_limits<std::size_t>::max)(),
136 bool throw_exceptions = false
137 )
138 : super(R, dataspec, r, id_generator, false, max_count), m_throw_exceptions(throw_exceptions)
139 {}
140
141 enumerator_algorithm_without_callback(const enumerator_algorithm<Rewriter, DataRewriter>&) = delete;
142
143 template <typename T>
145 {
146 bool operator()(const T&) { return false; }
147 };
148
149 /// \brief Enumerates the front element of the todo list P.
150 /// \param P The todo list of the algorithm.
151 /// \param sigma A mutable substitution that is applied by the rewriter.
152 /// \param accept Elements p for which accept(p) is false are discarded.
153 /// \pre !P.empty()
154 template <typename EnumeratorListElement, IsSubstitution MutableSubstitution, typename Filter>
155 void step(enumerator_queue<EnumeratorListElement>& P, MutableSubstitution& sigma, Filter accept) const
156 {
157 assert(!P.empty());
158
159 auto p = P.front();
160 const auto& v = p.variables();
161 const auto& phi = p.expression();
162 P.pop_front();
163
164 const auto& v1 = v.front();
165 const auto& v_tail = v.tail();
166 const auto& v1_sort = v1.sort();
167
168 if (data::is_function_sort(v1_sort))
169 {
170 const function_sort& function = atermpp::down_cast<function_sort>(v1_sort);
171 if (dataspec.is_certainly_finite(function))
172 {
173 data_expression_vector function_sorts;
174 variable_list function_parameter_list;
175 bool result = detail::compute_finite_function_sorts(function, id_generator, dataspec, r, function_sorts, function_parameter_list);
176 if (!result)
177 {
178 cannot_enumerate(p, "Sort " + data::pp(v1_sort) + " has too many elements to enumerate.");
179 }
180
181 data_expression sigma_v1 = sigma(v1);
182 for (const data_expression& f: function_sorts)
183 {
184 sigma[v1] = f;
185 add_element(P, sigma, accept, v_tail, phi, p, v1, f);
186 }
187 sigma[v1] = sigma_v1;
188 }
189 else
190 {
191 cannot_enumerate(p, "Cannot enumerate elements of function sort " + data::pp(v1_sort) + ".");
192 }
193 }
194 else if (sort_set::is_set(v1_sort))
195 {
196 const sort_expression& element_sort = atermpp::down_cast<container_sort>(v1_sort).element_sort();
197 if (dataspec.is_certainly_finite(element_sort))
198 {
199 const data_expression lambda_term = abstraction(lambda_binder(), { variable(id_generator(), element_sort) }, sort_bool::false_());
200 const variable fset_variable(id_generator(), sort_fset::fset(element_sort));
201 data_expression e = sort_set::constructor(element_sort, lambda_term, fset_variable);
202 data_expression sigma_v1 = sigma(v1);
203 sigma[v1] = e;
204 add_element(P, sigma, accept, v_tail, { fset_variable }, phi, p, v1, e);
205 sigma[v1] = sigma_v1;
206 }
207 else
208 {
209 cannot_enumerate(p, "Cannot enumerate elements of set sort " + data::pp(v1_sort) + ".");
210 return;
211 }
212 }
213 else if (sort_fset::is_fset(v1_sort))
214 {
215 const auto& fset = atermpp::down_cast<container_sort>(v1_sort);
216 if (dataspec.is_certainly_finite(fset.element_sort()))
217 {
218 data_expression_vector set_elements;
219 bool result = detail::compute_finite_set_elements(fset, dataspec, r, sigma, set_elements, id_generator);
220 if (!result)
221 {
222 cannot_enumerate(p, "Finite set sort " + data::pp(v1_sort) + " has too many elements to enumerate.");
223 }
224
225 data_expression sigma_v1 = sigma(v1);
226 for (const data_expression& e: set_elements)
227 {
228 sigma[v1] = e;
229 add_element(P, sigma, accept, v_tail, phi, p, v1, e);
230 }
231 sigma[v1] = sigma_v1;
232 }
233 else
234 {
235 cannot_enumerate(p, "Cannot enumerate elements of finite set sort " + data::pp(v1_sort) + ".");
236 return;
237 }
238 }
239 else if (sort_bag::is_bag(v1_sort))
240 {
241 cannot_enumerate(p, "Cannot enumerate elements of bag sort " + data::pp(v1_sort) + ".");
242 return;
243 }
244 else if (sort_fbag::is_fbag(v1_sort))
245 {
246 cannot_enumerate(p, "Cannot enumerate elements of finite bag sort " + data::pp(v1_sort) + ".");
247 return;
248 }
249 else
250 {
251 const function_symbol_vector& C = dataspec.constructors(v1_sort);
252 if (!C.empty())
253 {
254 for (const function_symbol& c: C)
255 {
256 if (data::is_function_sort(c.sort()))
257 {
258 auto const& domain = atermpp::down_cast<data::function_sort>(c.sort()).domain();
259 data::variable_list y(domain.begin(), domain.end(), [&](const data::sort_expression& s) { return data::variable(id_generator(), s); });
260 // TODO: We want to apply datar without the substitution sigma, but that is currently an inefficient operation of data::rewriter.
261 data_expression cy = r(application(c, y.begin(), y.end()), sigma);
262 sigma[v1] = cy;
263 add_element(P, sigma, accept, v_tail, y, phi, p, v1, cy);
264 sigma[v1] = v1;
265 }
266 else
267 {
268 // TODO: We want to apply r without the substitution sigma, but that is currently an inefficient operation of data::rewriter.
269 const auto e1 = r(c, sigma);
270 sigma[v1] = e1;
271 add_element(P, sigma, accept, v_tail, phi, p, v1, e1);
272 sigma[v1] = v1;
273 }
274 }
275 }
276 else
277 {
278 cannot_enumerate(p, "Cannot enumerate elements of sort " + data::pp(v1_sort) + " without constructors.");
279 return;
280 }
281 }
282 }
283
284 /// \brief Enumerates the front elements of the todo list P until a solution
285 /// has been found, or until P is empty.
286 /// \param P The todo list of the algorithm.
287 /// \param sigma A substitution.
288 /// \param accept Elements p for which accept(p) is false are discarded.
289 /// \return The number of elements that have been processed
290 /// \post Either P.empty() or P.front().is_solution()
291 template <typename EnumeratorListElement, IsSubstitution MutableSubstitution, typename Filter>
293 {
294 std::size_t count = 0;
295 while (!P.empty())
296 {
297 if (count++ >= m_max_count)
298 {
299 break;
300 }
301 if (P.front().is_solution())
302 {
303 break;
304 }
305 else
306 {
307 step(P, sigma, accept);
308 }
309 }
310 return count;
311 }
312
313 bool throw_exceptions() const
314 {
315 return m_throw_exceptions;
316 }
317};
318
319/// \brief An enumerator algorithm with an iterator interface.
320template <typename Rewriter = data::rewriter, typename EnumeratorListElement = enumerator_list_element_with_substitution<>, typename Filter = data::is_not_false, typename DataRewriter = data::rewriter, IsSubstitution MutableSubstitution = data::mutable_indexed_substitution<> >
322{
323 protected:
324 Filter m_accept;
325
326 public:
327 using super = enumerator_algorithm_without_callback<Rewriter, DataRewriter>;
328 using super::rewrite;
329
330 /// \brief A class to enumerate solutions for terms.
331 /// \details Solutions are presented as data_expression_lists of the same length as
332 /// the list of variables for which a solution is sought.
334 {
335 protected:
336 enumerator_algorithm_with_iterator<Rewriter, EnumeratorListElement, Filter, DataRewriter, MutableSubstitution>* E;
337 MutableSubstitution* sigma;
338 enumerator_queue<EnumeratorListElement>* P;
339 Filter accept;
341
342 static enumerator_queue<EnumeratorListElement>& default_deque()
343 {
344#ifdef MCRL2_ENABLE_MULTITHREADING
345 static_assert(mcrl2::utilities::detail::GlobalThreadSafe);
346 thread_local enumerator_queue<EnumeratorListElement> result; // Changed this static variable to thread local,
347 // as it could be the cause of a thread conflict.
348#else
349 static_assert(!mcrl2::utilities::detail::GlobalThreadSafe);
350 static enumerator_queue<EnumeratorListElement> result; // Changed this static variable to thread local,
351#endif
352 return result;
353 }
354
355 public:
356 iterator(enumerator_algorithm_with_iterator<Rewriter, EnumeratorListElement, Filter, DataRewriter, MutableSubstitution>* E_,
357 enumerator_queue<EnumeratorListElement>* P_,
358 MutableSubstitution* sigma_,
359 Filter accept_ = Filter()
360 )
361 : E(E_), sigma(sigma_), P(P_), accept(accept_), count(0)
362 {
363 count += E->next(*P, *sigma, accept);
364 }
365
366 explicit iterator(Filter accept_ = Filter())
367 : E(nullptr), sigma(nullptr), P(&default_deque()), accept(accept_), count(0)
368 { }
369
370 protected:
371 friend class boost::iterator_core_access;
372
374 {
375 assert(!P->empty());
376 P->pop_front();
377 count += E->next(*P, *sigma, accept);
378 if (count >= E->max_count())
379 {
380 if (E->throw_exceptions())
381 {
382 std::ostringstream out;
383 out << "enumeration was aborted, since it did not complete within " << E->max_count() << " iterations";
384 throw mcrl2::runtime_error(out.str());
385 }
386 else
387 {
388 P->clear();
389 return;
390 }
391 }
392 }
393
394 bool equal(iterator const& other) const
395 {
396 return P->size() == other.P->size();
397 }
398
399 const EnumeratorListElement& dereference() const
400 {
401 assert(!P->empty());
402 return P->front();
403 }
404 };
405
407 const Rewriter& R,
408 const data::data_specification& dataspec,
409 const DataRewriter& datar,
411 std::size_t max_count = (std::numeric_limits<std::size_t>::max)(),
412 bool throw_exceptions = false,
413 const Filter& f = Filter())
415 , m_accept(f)
416 {}
417
418 /// \brief Returns an iterator that enumerates solutions for variables that satisfy a condition
419 /// \param sigma A mutable substitution that is applied by the rewriter contained in E
420 /// \param P The condition that is solved, together with the list of variables
421 /// Otherwise an invalidated enumerator element is returned when it is dereferenced.
422 iterator begin(MutableSubstitution& sigma, enumerator_queue<EnumeratorListElement>& P)
423 {
424 assert(P.size() == 1);
425 auto& p = P.front();
426 p.expression() = rewrite(p.expression(), sigma);
427 if (m_accept(p.expression()))
428 {
429 return iterator(const_cast<enumerator_algorithm_with_iterator<Rewriter, EnumeratorListElement, Filter, DataRewriter, MutableSubstitution>*>(this), &P, &sigma, m_accept);
430 }
431 else
432 {
433 return end();
434 }
435 }
436
437 const iterator& end()
438 {
439#ifdef MCRL2_ENABLE_MULTITHREADING
440 static_assert(mcrl2::utilities::detail::GlobalThreadSafe);
441 thread_local iterator result(m_accept);
442#else
443 static_assert(!mcrl2::utilities::detail::GlobalThreadSafe);
444 static iterator result(m_accept);
445#endif
446 return result;
447 }
448};
449
450} // namespace mcrl2::data
451
452#endif // MCRL2_DATA_ENUMERATOR_WITH_ITERATOR_H
enumerator_algorithm_with_iterator< Rewriter, EnumeratorListElement, Filter, DataRewriter, MutableSubstitution > * E
static enumerator_queue< EnumeratorListElement > & default_deque()
iterator(enumerator_algorithm_with_iterator< Rewriter, EnumeratorListElement, Filter, DataRewriter, MutableSubstitution > *E_, enumerator_queue< EnumeratorListElement > *P_, MutableSubstitution *sigma_, Filter accept_=Filter())
An enumerator algorithm with an iterator interface.
iterator begin(MutableSubstitution &sigma, enumerator_queue< EnumeratorListElement > &P)
Returns an iterator that enumerates solutions for variables that satisfy a condition.
enumerator_algorithm_with_iterator(const Rewriter &R, const data::data_specification &dataspec, const DataRewriter &datar, enumerator_identifier_generator &id_generator, std::size_t max_count=(std::numeric_limits< std::size_t >::max)(), bool throw_exceptions=false, const Filter &f=Filter())
An enumerator algorithm that generates solutions of a condition.
void add_element(enumerator_queue< EnumeratorListElement > &P, MutableSubstitution &sigma, Filter accept, const data::variable_list &variables, const data::variable_list &added_variables, const Expression &phi, const EnumeratorListElement &p, const data::variable &v, const data::data_expression &e) const
void add_element(enumerator_queue< enumerator_list_element< Expression > > &P, MutableSubstitution &sigma, Filter accept, const data::variable_list &variables, const data::variable_list &added_variables, const Expression &phi, const enumerator_list_element< Expression > &p, const data::variable &v, const data::data_expression &e) const
std::size_t next(enumerator_queue< EnumeratorListElement > &P, MutableSubstitution &sigma, Filter accept) const
Enumerates the front elements of the todo list P until a solution has been found, or until P is empty...
enumerator_algorithm_without_callback(const enumerator_algorithm< Rewriter, DataRewriter > &)=delete
void step(enumerator_queue< EnumeratorListElement > &P, MutableSubstitution &sigma, Filter accept) const
Enumerates the front element of the todo list P.
bool m_throw_exceptions
throw_exceptions If true, an exception is thrown when the enumeration is aborted.
void add_element(enumerator_queue< EnumeratorListElement > &P, MutableSubstitution &sigma, Filter accept, const data::variable_list &variables, const Expression &phi, const EnumeratorListElement &p, const data::variable &v, const data::data_expression &e) const
enumerator_algorithm_without_callback(const Rewriter &R, const data::data_specification &dataspec, const DataRewriter &r, enumerator_identifier_generator &id_generator, std::size_t max_count=(std::numeric_limits< std::size_t >::max)(), bool throw_exceptions=false)
void cannot_enumerate(EnumeratorListElement &e, const std::string &msg) const
An enumerator algorithm that generates solutions of a condition.
Definition enumerator.h:599
The default element for the todo list of the enumerator.
Definition enumerator.h:229
Contains the enumerator queue.
Definition enumerator.h:499
\brief A function sort
\brief Binder for lambda abstraction
lambda_binder()
\brief Default constructor X3.
Rewriter that operates on data expressions.
Definition rewriter.h:84
\brief A sort expression
\brief A data variable
Definition variable.h:25
Namespace for system defined sort bag.
Definition bag1.h:35
bool is_bag(const sort_expression &e)
Recogniser for sort expression Bag(s)
Definition bag1.h:52
Namespace for system defined sort bool_.
Definition bool.h:29
bool is_false_function_symbol(const atermpp::aterm &e)
Recogniser for function false.
Definition bool.h:116
const function_symbol & false_()
Constructor for function symbol false.
Definition bool.h:106
bool is_true_function_symbol(const atermpp::aterm &e)
Recogniser for function true.
Definition bool.h:84
Namespace for system defined sort fbag.
Definition fbag1.h:34
bool is_fbag(const sort_expression &e)
Recogniser for sort expression FBag(s)
Definition fbag1.h:51
Namespace for system defined sort fset.
Definition fset1.h:32
bool is_fset(const sort_expression &e)
Recogniser for sort expression FSet(s)
Definition fset1.h:49
container_sort fset(const sort_expression &s)
Constructor for sort expression FSet(S)
Definition fset1.h:38
Namespace for system defined sort set_.
Definition set1.h:33
bool is_set(const sort_expression &e)
Recogniser for sort expression Set(s)
Definition set1.h:50
application constructor(const sort_expression &s, const data_expression &arg0, const data_expression &arg1)
Application of function symbol @set.
Definition set1.h:98
bool is_function_sort(const atermpp::aterm &x)
Returns true if the term t is a function sort.
bool operator()(const data_expression &x) const
bool operator()(const data_expression &x) const