mCRL2
Loading...
Searching...
No Matches
data_utility.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/detail/data_utility.h
10/// \brief Add your file description here.
11
12#ifndef MCRL2_DATA_DETAIL_DATA_UTILITY_H
13#define MCRL2_DATA_DETAIL_DATA_UTILITY_H
14
15#include "mcrl2/data/alias.h"
16#include "mcrl2/data/assignment.h"
17#include "mcrl2/data/detail/data_functional.h"
18#include "mcrl2/data/standard_utility.h"
19#include "mcrl2/utilities/detail/container_utility.h"
20
21namespace mcrl2::data::detail
22{
23
24/// \brief Returns the sorts of a sequence of parameters.
25template <typename Container>
26data::sort_expression_list parameter_sorts(const Container& parameters)
27{
28 data::sort_expression_list sorts;
29 for (auto const& param: parameters)
30 {
31 sorts.push_front(param.sort());
32 }
33 return atermpp::reverse(sorts);
34}
35
36/// \brief Returns true if the names of the given variables are unique.
37/// \param variables A sequence of data variables
38/// \return True if the names of the given variables are unique.
39template <class VariableContainer>
40inline
41bool unique_names(const VariableContainer& variables)
42{
43 std::set<core::identifier_string> variable_names;
44 for (auto const& var: variables)
45 {
46 variable_names.insert(var.name());
47 }
48 return variable_names.size() == variables.size();
49}
50
51/// \brief Returns true if the left hand sides of assignments are contained in variables.
52/// \param assignments A sequence of assignments to data variables
53/// \param variables A sequence of data variables
54/// \return True if the left hand sides of assignments are contained in variables.
55inline
56bool check_assignment_variables(assignment_list const& assignments, variable_list const& variables)
57{
58 using utilities::detail::contains;
59
60 std::set<variable> v(variables.begin(), variables.end());
61 for (const assignment& a: assignments)
62 {
63 if (!contains(v, a.lhs()))
64 {
65 return false;
66 }
67 }
68 return true;
69}
70
71/// \brief Returns true if the domain sorts and the codomain sort of the given sort s are contained in sorts.
72/// \param s A sort expression
73/// \param sorts A set of sort expressions
74/// \return True if the sort is contained in <tt>sorts</tt>
75template <typename SortContainer>
76inline bool check_sort(const sort_expression& s, const SortContainer& sorts)
77{
78 using utilities::detail::remove_if;
79
80 std::set<sort_expression> s_sorts = data::find_sort_expressions(s);
81 remove_if(s_sorts, [](const sort_expression& x) { return !is_function_sort(x); });
82 for (const sort_expression& sort: s_sorts)
83 {
84 if (std::find(sorts.begin(), sorts.end(), sort) == sorts.end())
85 {
86 // sort *i is not well-typed, a system defined sort or an alias
87 if (!(is_system_defined(sort)) && is_alias(sort))
88 {
89 alias sort_alias(sort);
90
91 if (std::find(sorts.begin(), sorts.end(), sort_alias.name()) == sorts.end())
92 {
93 // sort_alias.reference() is a basic, structured or container sort
94 const sort_expression& sort_reference(sort_alias.reference());
95
96 if (std::find(sorts.begin(), sorts.end(), sort_reference) == sorts.end())
97 {
98 // sort_reference is structured or container sort
99 if (is_structured_sort(sort_reference))
100 {
101 assert(false);
102 }
103 else if (is_container_sort(sort_reference))
104 {
105 if (std::find(sorts.begin(), sorts.end(), container_sort(sort_reference).element_sort()) == sorts.end())
106 {
107 return false;
108 }
109 }
110 else
111 {
112 assert(false);
113 }
114 }
115 }
116 }
117 }
118 }
119
120 return true;
121}
122
123/// \brief Returns true if the domain sorts and the range sort of the sorts in the sequence [first, last)
124/// are contained in sorts.
125/// \param first Start of a sequence of sorts
126/// \param last End of a sequence of sorts
127/// \param sorts A set of sort expressions
128/// \return True if the sequence of sorts is contained in <tt>sorts</tt>
129template <typename Iterator, typename SortContainer>
130bool check_sorts(Iterator first, Iterator last, const SortContainer& sorts)
131{
132 for (Iterator i = first; i != last; ++i)
133 {
134 if (!check_sort(*i, sorts))
135 {
136 return false;
137 }
138 }
139 return true;
140}
141
142/// \brief Returns true if the domain sorts and the range sort of the given variables are contained in sorts.
143/// \param variables A container with data variables
144/// \param sorts A set of sort expressions
145/// \return True if the domain sorts and the range sort of the given variables are contained in sorts.
146template <typename VariableContainer, typename SortContainer>
147bool check_variable_sorts(const VariableContainer& variables, const SortContainer& sorts)
148{
149 for (typename VariableContainer::const_iterator i = variables.begin(); i != variables.end(); ++i)
150 {
151 if (!check_sort(i->sort(), sorts))
152 {
153 return false;
154 }
155 }
156 return true;
157}
158
159/// \brief Returns true if names of the given variables are not contained in names.
160/// \param variables A sequence of data variables
161/// \param names A set of strings
162/// \return True if names of the given variables are not contained in names.
163inline
164bool check_variable_names(variable_list const& variables, const std::set<core::identifier_string>& names)
165{
166 using utilities::detail::contains;
167
168 for (const variable& v: variables)
169 {
170 if (contains(names, v.name()))
171 {
172 return false;
173 }
174 }
175 return true;
176}
177
178/// \brief Returns true if the domain sorts and range sort of the given functions are contained in sorts.
179/// \param range A sequence of data operations
180/// \param sorts A set of sort expressions
181/// \return True if the domain sorts and range sort of the given functions are contained in sorts.
182template < typename Container, typename SortContainer >
183inline
184bool check_data_spec_sorts(const Container& container, const SortContainer& sorts)
185{
186 for (typename Container::const_iterator i = container.begin(); i != container.end(); ++i)
187 {
188 if (!check_sort(i->sort(), sorts))
189 {
190 return false;
191 }
192 }
193 return true;
194}
195
196} // namespace mcrl2::data::detail
197
198#endif // MCRL2_DATA_DETAIL_DATA_UTILITY_H
A list of aterm objects.
Definition aterm_list.h:26
A unordered_map class in which aterms can be stored.
\brief A sort alias
Definition alias.h:23
\brief A basic sort
Definition basic_sort.h:25
basic_sort(const atermpp::aterm &term)
Definition basic_sort.h:34
\brief A container sort
const container_type & container_name() const
const sort_expression & element_sort() const
container_sort(const atermpp::aterm &term)
data_specification(const basic_sort_vector &sorts, const alias_vector &aliases, const function_symbol_vector &constructors, const function_symbol_vector &user_defined_mappings, const data_equation_vector &user_defined_equations)
Constructor from its members.
bool is_well_typed() const
Returns true if.
bool is_certainly_finite(const sort_expression &s) const
Checks whether a sort is certainly finite.
bool is_finite(const container_sort &s)
bool is_finite(const basic_sort &s)
bool is_finite_aux(const sort_expression &s)
bool is_finite(const sort_expression &s)
std::set< sort_expression > m_visiting
bool is_finite(const function_sort &s)
const data_specification & m_specification
finiteness_helper(const data_specification &specification)
bool is_finite(const structured_sort &s)
\brief Container type for finite sets
fset_container()
\brief Default constructor X3.
\brief A function sort
const sort_expression & codomain() const
function_sort(const atermpp::aterm &term)
const sort_expression_list & domain() const
\brief Container type for sets
set_container()
\brief Default constructor X3.
\brief A sort expression
sort_expression & operator=(const sort_expression &) noexcept=default
sort_expression(const sort_expression &) noexcept=default
Move semantics.
void add_system_defined_sort(const sort_expression &s)
Adds a sort to this specification, and marks it as system defined.
void sorts_are_not_necessarily_normalised_anymore() const
void import_system_defined_sort(const sort_expression &sort)
Adds the system defined sorts in a sequence. The second argument is used to check which sorts are add...
structured_sort(const atermpp::aterm &term)
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:435
aterm identity(const aterm &x)
The default transformer that maps each term to itself.
Definition aterm_io.h:26
std::string file_source(const std::string &filename)
Definition load_aterm.h:24
atermpp::aterm load_aterm(std::istream &stream, bool binary=true, const std::string &format="aterm", const std::string &source="", atermpp::aterm_transformer transformer=atermpp::identity)
Attempts to read an aterm from a stream.
Definition load_aterm.h:43
bool check_variable_sorts(const VariableContainer &variables, const SortContainer &sorts)
Returns true if the domain sorts and the range sort of the given variables are contained in sorts.
bool check_variable_names(variable_list const &variables, const std::set< core::identifier_string > &names)
Returns true if names of the given variables are not contained in names.
bool check_assignment_variables(assignment_list const &assignments, variable_list const &variables)
Returns true if the left hand sides of assignments are contained in variables.
bool check_sort(const sort_expression &s, const SortContainer &sorts)
Returns true if the domain sorts and the codomain sort of the given sort s are contained in sorts.
bool check_data_spec_sorts(const Container &container, const SortContainer &sorts)
Returns true if the domain sorts and range sort of the given functions are contained in sorts.
data::sort_expression_list parameter_sorts(const Container &parameters)
Returns the sorts of a sequence of parameters.
bool check_sorts(Iterator first, Iterator last, const SortContainer &sorts)
Returns true if the domain sorts and the range sort of the sorts in the sequence [first,...
bool unique_names(const VariableContainer &variables)
Returns true if the names of the given variables are unique.
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
const basic_sort & bool_()
Constructor for sort expression Bool.
Definition bool.h:41
Namespace for system defined sort fbag.
Definition fbag1.h:34
container_sort fbag(const sort_expression &s)
Constructor for sort expression FBag(S)
Definition fbag1.h:40
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 int_.
const basic_sort & int_()
Constructor for sort expression Int.
Definition int1.h:44
Namespace for system defined sort list.
Definition list1.h:33
bool is_list(const sort_expression &e)
Recogniser for sort expression List(s)
Definition list1.h:50
Namespace for system defined sort nat.
const basic_sort & nat()
Constructor for sort expression Nat.
Definition nat1.h:43
const basic_sort & natpair()
Constructor for sort expression @NatPair.
Definition nat1.h:72
Namespace for system defined sort pos.
const basic_sort & pos()
Constructor for sort expression Pos.
Definition pos1.h:42
Namespace for system defined sort real_.
const basic_sort & real_()
Constructor for sort expression Real.
Definition real1.h:45
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
container_sort set_(const sort_expression &s)
Constructor for sort expression Set(S)
Definition set1.h:39
bool is_structured_sort(const atermpp::aterm &x)
Returns true if the term t is a structured sort.
static sort_expression find_normal_form(const sort_expression &e, const std::multimap< sort_expression, sort_expression > &map1, std::set< sort_expression > sorts_already_seen=std::set< sort_expression >())
bool is_untyped_possible_sorts(const atermpp::aterm &x)
Returns true if the term t is an expression for multiple possible sorts.
bool is_untyped_sort(const atermpp::aterm &x)
Returns true if the term t is the unknown sort.
bool is_container_sort(const atermpp::aterm &x)
Returns true if the term t is a container sort.
bool is_basic_sort(const atermpp::aterm &x)
Returns true if the term t is a basic sort.
bool is_function_sort(const atermpp::aterm &x)
Returns true if the term t is a function sort.