mCRL2
Loading...
Searching...
No Matches
unify_parameters.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/unify_parameters.h
10/// \brief add your file description here.
11
12#ifndef MCRL2_PBES_UNIFY_PARAMETERS_H
13#define MCRL2_PBES_UNIFY_PARAMETERS_H
14
15#include "mcrl2/data/data_expression.h"
16#include "mcrl2/data/default_expression_generator.h"
17#include "mcrl2/pbes/pbes_expression.h"
18#include "mcrl2/pbes/replace.h"
19#include "mcrl2/pbes/srf_pbes.h"
20#include "mcrl2/pbes/detail/pbes_remove_counterexample_info.h"
21#include <optional>
22
23namespace mcrl2::pbes_system {
24
26{
27 // maps propositional variable names to the corresponding parameters
29
30 // generates default expressions for sorts
32
33 // maps parameters to their new positions
35
36 // store the 'missing' parameters for each parameter list
38
39 // the new list of parameters
40 data::variable_list parameters;
41
42 // reuse this vector for constructing new parameters
44
45 /// Indicates that instantiations of parameters are reset to a default value.
46 bool m_reset;
47
48 data::variable_list compute_parameters()
49 {
50 std::vector<data::variable> parameter_vector;
51 for (const auto& p: propositional_variable_parameters)
52 {
53 const data::variable_list& eqn_parameters = p.second;
54 for (const data::variable& v: eqn_parameters)
55 {
56 auto i = parameter_positions.find(v);
57 if (i == parameter_positions.end())
58 {
59 parameter_positions[v] = parameter_vector.size();
60 parameter_vector.push_back(v);
61 }
62 }
63 }
64 return data::variable_list(parameter_vector.begin(), parameter_vector.end());
65 }
66
68 const std::map<core::identifier_string, data::variable_list>& propositional_variable_parameters_,
69 const data::data_specification& dataspec,
70 bool reset
71 )
73 {
74 using utilities::detail::contains;
75
76 parameters = compute_parameters();
77 tmp_parameters.resize(parameters.size());
78
79 // Compute missing parameters for each equation
80 for (const auto& p: propositional_variable_parameters_)
81 {
82 const data::variable_list& eqn_parameters = p.second;
83 auto i = missing_parameters.find(eqn_parameters);
84 if (i != missing_parameters.end())
85 {
86 continue;
87 }
88 std::set<data::variable> eqn_parameter_set(eqn_parameters.begin(), eqn_parameters.end());
89 std::vector<data::variable> missing;
90 for (const data::variable& v: parameters)
91 {
92 if (!contains(eqn_parameter_set, v))
93 {
94 missing.push_back(v);
95 }
96 }
97 missing_parameters[eqn_parameters] = missing;
98 }
99 }
100
101 // If `reset` is true then the newly introduced parameters will be reset to a default value instead of copying the value.
103 {
105 {
106 return x;
107 }
108
109 const data::variable_list& variables = propositional_variable_parameters.at(x.name());
110 const data::data_expression_list& values = x.parameters();
111 auto i = variables.begin();
112 auto j = values.begin();
113 for (; i != variables.end(); ++i, ++j)
114 {
115 std::size_t pos = parameter_positions[*i];
116 tmp_parameters[pos] = *j;
117 }
118
119 for (const data::variable& v: missing_parameters[variables])
120 {
121 std::size_t pos = parameter_positions[v];
122
123 if (m_reset)
124 {
125 tmp_parameters[pos] = generator(v.sort());
126 }
127 else
128 {
129 tmp_parameters[pos] = v;
130 }
131 }
132
133 return propositional_variable_instantiation(x.name(), data::data_expression_list(tmp_parameters.begin(), tmp_parameters.end()));
134 };
135};
136
137/// Unify all parameters of the equations, optionally ignoring the equations
138/// related to counter example information. Finally, if reset is true, reset the
139/// newly introduced parameters to a default value.
140inline
141void unify_parameters(pbes& p, bool ignore_ce_equations, bool reset)
142{
143 std::map<core::identifier_string, data::variable_list> propositional_variable_parameters;
144 for (const pbes_equation& eqn: p.equations())
145 {
146 // Ignore the counter example equations for the list of parameters.
147 if (!ignore_ce_equations || !detail::is_counter_example_equation(eqn))
148 {
149 propositional_variable_parameters[eqn.variable().name()] = eqn.variable().parameters();
150 }
151 }
152
153 unify_parameters_replace_function replace(propositional_variable_parameters, p.data(), reset);
154 unify_parameters_replace_function replace_reset(propositional_variable_parameters, p.data(), true);
155
156 // update the right hand sides of the equations
157 replace_propositional_variables(p, replace);
158
159 // update the initial state
161
162 // update the left hand sides of the equations
163 for (pbes_equation& eqn: p.equations())
164 {
165 // Do not replace the counter example equations
166 if (!detail::is_counter_example_equation(eqn))
167 {
168 propositional_variable& X = eqn.variable();
169 X = propositional_variable(X.name(), replace.parameters);
170 }
171 }
172}
173
174/// See `unify_parameters(pbes&, bool, bool)` for a description of the parameters.
175template<bool allow_ce>
176inline
177void unify_parameters(detail::pre_srf_pbes<allow_ce>& p, bool ignore_ce_equations, bool reset)
178{
179 std::map<core::identifier_string, data::variable_list> propositional_variable_parameters;
180 for (const auto& eqn: p.equations())
181 {
182 if (!ignore_ce_equations || !detail::is_counter_example_equation(eqn.to_pbes()))
183 {
184 propositional_variable_parameters[eqn.variable().name()] = eqn.variable().parameters();
185 }
186 }
187
188 unify_parameters_replace_function replace(propositional_variable_parameters, p.data(), reset);
189 unify_parameters_replace_function replace_reset(propositional_variable_parameters, p.data(), true);
190
191 std::size_t N = p.equations().size();
192 const auto& false_summand = p.equations()[N - 2].summands().front();
193 const auto& true_summand = p.equations()[N - 1].summands().front();
194
195 // update the equations
196 for (auto& eqn: p.equations())
197 {
198 // Do not replace the counter example equations
199 if (!ignore_ce_equations || !detail::is_counter_example_equation(eqn.to_pbes()))
200 {
201 for (auto& summand: eqn.summands())
202 {
203 summand.variable() = replace(summand.variable());
204 }
205 propositional_variable& X = eqn.variable();
206 X = propositional_variable(X.name(), replace.parameters);
207 }
208 else
209 {
210 // For counter example equations it is important to unify the X_true and X_false, for which we introduce default values because counter example
211 // equations do not have unified parameters.
212 for (auto& summand: eqn.summands())
213 {
214 if (summand.variable() == false_summand.variable() || summand.variable() == true_summand.variable())
215 {
216 summand.variable() = replace_reset(summand.variable());
217 }
218 }
219 }
220 }
221
222 // update the initial state
223 p.initial_state() = replace_reset(p.initial_state());
224}
225
226/// \returns true iff all PBES equations have the same parameter list.
227inline
229{
230 std::optional<data::variable_list> parameters;
231 for (const auto& equation : pbes.equations())
232 {
233 if (!parameters.has_value())
234 {
235 parameters = equation.variable().parameters();
236 }
237 else
238 {
239 if (parameters.value() != equation.variable().parameters())
240 {
241 return false;
242 }
243 }
244 }
245
246 return true;
247}
248
249} // namespace mcrl2::pbes_system
250
251
252
253#endif // MCRL2_PBES_UNIFY_PARAMETERS_H
Expression generator that caches values.
parameterized boolean equation system
Definition pbes.h:54
propositional_variable_instantiation & initial_state()
Returns the initial state.
Definition pbes.h:195
\brief A propositional variable instantiation
const data::data_expression_list & parameters() const
propositional_variable_instantiation & operator=(propositional_variable_instantiation &&) noexcept=default
\brief A propositional variable declaration
const core::identifier_string & name() const
propositional_variable & operator=(propositional_variable &&) noexcept=default
bool is_counter_example_equation(const pbes_equation &equation)
Guesses if the PBES equation is a counter example equation.
bool is_counter_example_instantiation(const propositional_variable_instantiation &inst)
Guesses if the PBES variable instantiation is for counter example equation.
bool has_unified_parameters(const pbes &pbes)
void unify_parameters(pbes &p, bool ignore_ce_equations, bool reset)
void unify_parameters(detail::pre_srf_pbes< allow_ce > &p, bool ignore_ce_equations, bool reset)
See unify_parameters(pbes&, bool, bool) for a description of the parameters.
propositional_variable_instantiation operator()(const propositional_variable_instantiation &x) const
std::vector< data::data_expression > tmp_parameters
unify_parameters_replace_function(const std::map< core::identifier_string, data::variable_list > &propositional_variable_parameters_, const data::data_specification &dataspec, bool reset)
bool m_reset
Indicates that instantiations of parameters are reset to a default value.
const std::map< core::identifier_string, data::variable_list > & propositional_variable_parameters
std::map< data::variable, std::size_t > parameter_positions