mCRL2
Loading...
Searching...
No Matches
pbesinst_algorithm.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/pbes/pbesinst_algorithm.h
10/// \brief Algorithm for instantiating a PBES.
11
12#ifndef MCRL2_PBES_PBESINST_ALGORITHM_H
13#define MCRL2_PBES_PBESINST_ALGORITHM_H
14
15#include "mcrl2/pbes/detail/bes_equation_limit.h"
16#include "mcrl2/pbes/detail/instantiate_global_variables.h"
17#include "mcrl2/pbes/rewriters/enumerate_quantifiers_rewriter.h"
18#include "mcrl2/pbes/rewriters/one_point_rule_rewriter.h"
19#include "mcrl2/pbes/rewriters/simplify_quantifiers_rewriter.h"
20
21
22
23namespace mcrl2::pbes_system
24{
25
26/// \brief Creates a substitution function for the pbesinst rewriter.
27/// \param v A sequence of data variables
28/// \param e A sequence of data expressions
29/// \param sigma The substitution that maps the i-th element of \p v to the i-th element of \p e
30inline
31void make_pbesinst_substitution(const data::variable_list& v, const data::data_expression_list& e, data::rewriter::substitution_type& sigma)
32{
33 assert(v.size() == e.size());
34 data::variable_list::iterator i = v.begin();
35 data::data_expression_list::iterator j = e.begin();
36 for (; i != v.end(); ++i, ++j)
37 {
38 sigma[*i] = *j;
39 }
40}
41
42inline
44{
45 return pbes_system::find_free_variables(x).empty();
46}
47
48/// \brief Creates a unique name for a propositional variable instantiation. The
49/// propositional variable instantiation must be closed.
50/// Originally implemented by Alexander van Dam.
51/// \return A name that uniquely corresponds to the propositional variable.
53{
54 core::identifier_string operator()(const propositional_variable_instantiation& Ye) const
55 {
56 assert(pbesinst_is_constant(Ye));
57 std::string name = Ye.name();
58 for (const data::data_expression& exp: Ye.parameters())
59 {
60 if (is_function_symbol(exp) && exp.sort() != data::sort_pos::pos() && exp.sort() != data::sort_nat::nat())
61 {
62 // This case is dealt with separately, as it occurs often.
63 // The use of pp as in the next case is correct for this case also, but very time consuming.
64 // The exception to this rule is constants @c1 of sort Pos, @c0 of sort Nat.
65 name += "@";
66 name += atermpp::down_cast<data::function_symbol>(exp).name();
67 }
68 else if (is_function_symbol(exp) || is_application(exp) || is_abstraction(exp))
69 {
70 name += "@";
71 name += data::pp(exp);
72 }
73 else
74 {
75 throw mcrl2::runtime_error(std::string("pbesinst_rename_long: could not rename the variable ") + pbes_system::pp(Ye) + " " + data::pp(exp));
76 }
77 }
78 return name;
79 }
80};
81
82/// \brief Creates a unique name for a propositional variable instantiation. The
83/// propositional variable instantiation must be closed.
84/// Originally implemented by Alexander van Dam.
85/// \return A name that uniquely corresponds to the propositional variable.
87{
89 {
91 {
92 return Ye;
93 }
94 return propositional_variable_instantiation(pbesinst_rename_long()(Ye), data::data_expression_list());
95 }
96};
97
98/// \brief Algorithm class for the pbesinst instantiation algorithm.
100{
101 protected:
102 /// \brief Data rewriter.
104
105 /// \brief The rewriter.
107
108 /// \brief The number of generated equations.
110
111 /// \brief Propositional variable instantiations that need to be handled.
113
114 /// \brief Propositional variable instantiations that have been handled.
116
117 /// \brief Data structure for storing the result. E[i] corresponds to the equations
118 /// generated from the i-th PBES equation.
120
121 /// \brief The initial value.
123
124 /// \brief A lookup map for PBES equations.
126
127 /// \brief Print the equations to standard out.
129
130 /// \brief Prints a log message for every 1000-th equation
132 {
133 if (size > 0 && size % 1000 == 0)
134 {
135 std::ostringstream out;
136 out << "Generated " << size << " BES equations" << std::endl;
137 return out.str();
138 }
139 return "";
140 }
141
142 // renames propositional variables in x
144 {
145 return replace_propositional_variables(x, pbesinst_rename());
146 }
147
148 public:
149
150 /// \brief Constructor.
151 /// \param data_spec A data specification.
152 /// \param rewrite_strategy A strategy for the data rewriter.
153 /// \param print_equations If true, the generated equations are printed.
154 explicit pbesinst_algorithm(data::data_specification const& data_spec,
155 data::rewriter::strategy rewrite_strategy = data::jitty,
156 bool print_equations = false)
158 R(datar, data_spec),
159
160 m_print_equations(print_equations)
161 {}
162
163 /// \brief Runs the algorithm. The result is obtained by calling the function \p get_result.
164 /// \param p A PBES.
165 void run(pbes& p)
166 {
167 using utilities::detail::pick_element;
168 using utilities::detail::contains;
169
170 pbes_system::detail::instantiate_global_variables(p);
171
172 // simplify all right hand sides of p
173 //
174 // NOTE: This is not just an optimization. There are certain PBES
175 // equations for which applying enumerate_quantifiers_rewriter directly
176 // won't terminate, like:
177 //
178 // forall m: Nat . exists k: Nat . val(m == k)
179 pbes_system::one_point_rule_rewriter one_point_rule_rewriter;
180 pbes_system::simplify_quantifiers_data_rewriter<mcrl2::data::rewriter> simplify_rewriter(datar);
181 for (pbes_equation& eqn: p.equations())
182 {
183 eqn.formula() = one_point_rule_rewriter(simplify_rewriter(eqn.formula()));
184 }
185
186 // initialize equation_index and E
187 int eqn_index = 0;
188 auto const& equations = p.equations();
189 for (const pbes_equation& eqn : equations)
190 {
191 equation_index[eqn.variable().name()] = eqn_index++;
192 E.emplace_back();
193 }
194 init = atermpp::down_cast<propositional_variable_instantiation>(R(p.initial_state()));
195 todo.insert(init);
196 while (!todo.empty())
197 {
198 auto const& X_e = pick_element(todo);
199 done.insert(X_e);
200 int index = equation_index[X_e.name()];
201 const pbes_equation& eqn = p.equations()[index];
202 data::rewriter::substitution_type sigma;
203 make_pbesinst_substitution(eqn.variable().parameters(), X_e.parameters(), sigma);
204 auto const& phi = eqn.formula();
205 pbes_expression psi_e = R(phi, sigma);
206 R.clear_identifier_generator();
207 for (const propositional_variable_instantiation& v: find_propositional_variable_instantiations(psi_e))
208 {
209 if (!contains(done, v))
210 {
211 todo.insert(v);
212 }
213 }
214 pbes_equation new_eqn(eqn.symbol(), propositional_variable(pbesinst_rename()(X_e).name(), data::variable_list()), rho(psi_e));
215 if (m_print_equations)
216 {
217 mCRL2log(log::info) << eqn.symbol() << " " << X_e << " = " << psi_e << std::endl;
218 }
219 E[index].push_back(new_eqn);
220 mCRL2log(log::verbose) << print_equation_count(++m_equation_count);
221 detail::check_bes_equation_limit(m_equation_count);
222 }
223 }
224
225 /// \brief Returns the computed bes in pbes format
226 /// \return The computed bes in pbes format
228 {
229 pbes result;
230 for (const std::vector<pbes_equation>& equations: E)
231 {
232 result.equations().insert(result.equations().end(), equations.begin(), equations.end());
233 }
235 return result;
236 }
237
238 /// \brief Returns the flag for printing the generated bes equations
239 /// \return The flag for printing the generated bes equations
241 {
242 return m_print_equations;
243 }
244
246 {
247 return R;
248 }
249};
250
251} // namespace mcrl2::pbes_system
252
253
254
255#endif // MCRL2_PBES_PBESINST_ALGORITHM_H
Rewriter that operates on data expressions.
Definition rewriter.h:84
A rewriter that applies one point rule quantifier elimination to a PBES.
parameterized boolean equation system
Definition pbes.h:54
propositional_variable_instantiation & initial_state()
Returns the initial state.
Definition pbes.h:195
Algorithm class for the pbesinst instantiation algorithm.
std::map< core::identifier_string, int > equation_index
A lookup map for PBES equations.
propositional_variable_instantiation init
The initial value.
enumerate_quantifiers_rewriter & rewriter()
void run(pbes &p)
Runs the algorithm. The result is obtained by calling the function get_result.
bool m_print_equations
Print the equations to standard out.
std::set< propositional_variable_instantiation > done
Propositional variable instantiations that have been handled.
std::size_t m_equation_count
The number of generated equations.
std::string print_equation_count(std::size_t size) const
Prints a log message for every 1000-th equation.
pbes_expression rho(const pbes_expression &x) const
bool & print_equations()
Returns the flag for printing the generated bes equations.
enumerate_quantifiers_rewriter R
The rewriter.
pbes get_result()
Returns the computed bes in pbes format.
std::set< propositional_variable_instantiation > todo
Propositional variable instantiations that need to be handled.
pbesinst_algorithm(data::data_specification const &data_spec, data::rewriter::strategy rewrite_strategy=data::jitty, bool print_equations=false)
Constructor.
\brief A propositional variable instantiation
propositional_variable_instantiation(const core::identifier_string &name, const data::data_expression_list &parameters)
Constructor.
propositional_variable_instantiation & operator=(propositional_variable_instantiation &&) noexcept=default
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:393
void make_pbesinst_substitution(const data::variable_list &v, const data::data_expression_list &e, data::rewriter::substitution_type &sigma)
Creates a substitution function for the pbesinst rewriter.
bool pbesinst_is_constant(const pbes_expression &x)
Creates a unique name for a propositional variable instantiation. The propositional variable instanti...
core::identifier_string operator()(const propositional_variable_instantiation &Ye) const
Creates a unique name for a propositional variable instantiation. The propositional variable instanti...
propositional_variable_instantiation operator()(const propositional_variable_instantiation &Ye) const
A rewriter that simplifies boolean expressions and quantifiers, and rewrites data expressions.