Skip to content

Commit 3a53602

Browse files
author
svorenova
committed
Move mathematical types from std_types
1 parent f0dbb8b commit 3a53602

8 files changed

+233
-200
lines changed

src/util/c_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010
#ifndef CPROVER_UTIL_C_TYPES_H
1111
#define CPROVER_UTIL_C_TYPES_H
1212

13+
#include "mathematical_types.h"
1314
#include "std_types.h"
1415

1516
bitvector_typet index_type();

src/util/mathematical_types.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*******************************************************************\
2+
3+
Module: Pre-defined mathematical types
4+
5+
Author: Daniel Kroening, [email protected]
6+
Maria Svorenova, [email protected]
7+
8+
\*******************************************************************/
9+
10+
/// \file
11+
/// Pre-defined types
12+
13+
#include "mathematical_types.h"
14+
15+
/// Returns true if the type is a rational, real, integer, natural, complex,
16+
/// unsignedbv, signedbv, floatbv or fixedbv.
17+
bool is_number(const typet &type)
18+
{
19+
const irep_idt &id = type.id();
20+
return id == ID_rational || id == ID_real || id == ID_integer ||
21+
id == ID_natural || id == ID_complex || id == ID_unsignedbv ||
22+
id == ID_signedbv || id == ID_floatbv || id == ID_fixedbv;
23+
}

src/util/mathematical_types.h

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*******************************************************************\
2+
3+
Module: Pre-defined mathematical types
4+
5+
Author: Daniel Kroening, [email protected]
6+
Maria Svorenova, [email protected]
7+
8+
\*******************************************************************/
9+
10+
/// \file
11+
/// Pre-defined mathematical types
12+
13+
#ifndef CPROVER_UTIL_MATHEMATICAL_TYPES_H
14+
#define CPROVER_UTIL_MATHEMATICAL_TYPES_H
15+
16+
#include "invariant.h"
17+
#include "type.h"
18+
19+
/// Unbounded, signed integers (mathematical integers, not bitvectors)
20+
class integer_typet : public typet
21+
{
22+
public:
23+
integer_typet() : typet(ID_integer)
24+
{
25+
}
26+
};
27+
28+
/// Natural numbers including zero (mathematical integers, not bitvectors)
29+
class natural_typet : public typet
30+
{
31+
public:
32+
natural_typet() : typet(ID_natural)
33+
{
34+
}
35+
};
36+
37+
/// Unbounded, signed rational numbers
38+
class rational_typet : public typet
39+
{
40+
public:
41+
rational_typet() : typet(ID_rational)
42+
{
43+
}
44+
};
45+
46+
/// Unbounded, signed real numbers
47+
class real_typet : public typet
48+
{
49+
public:
50+
real_typet() : typet(ID_real)
51+
{
52+
}
53+
};
54+
55+
/// Complex numbers made of pair of given subtype
56+
class complex_typet : public type_with_subtypet
57+
{
58+
public:
59+
complex_typet() : type_with_subtypet(ID_complex)
60+
{
61+
}
62+
63+
explicit complex_typet(const typet &_subtype)
64+
: type_with_subtypet(ID_complex, _subtype)
65+
{
66+
}
67+
};
68+
69+
/// Check whether a reference to a typet is a \ref complex_typet.
70+
/// \param type Source type.
71+
/// \return True if \param type is a \ref complex_typet.
72+
template <>
73+
inline bool can_cast_type<complex_typet>(const typet &type)
74+
{
75+
return type.id() == ID_complex;
76+
}
77+
78+
/// \brief Cast a typet to a \ref complex_typet
79+
///
80+
/// This is an unchecked conversion. \a type must be known to be \ref
81+
/// complex_typet. Will fail with a precondition violation if type
82+
/// doesn't match.
83+
///
84+
/// \param type Source type.
85+
/// \return Object of type \ref complex_typet.
86+
inline const complex_typet &to_complex_type(const typet &type)
87+
{
88+
PRECONDITION(can_cast_type<complex_typet>(type));
89+
return static_cast<const complex_typet &>(type);
90+
}
91+
92+
/// \copydoc to_complex_type(const typet &)
93+
inline complex_typet &to_complex_type(typet &type)
94+
{
95+
PRECONDITION(can_cast_type<complex_typet>(type));
96+
return static_cast<complex_typet &>(type);
97+
}
98+
99+
/// A type for mathematical functions (do not confuse with functions/methods
100+
/// in code)
101+
class mathematical_function_typet : public typet
102+
{
103+
public:
104+
// the domain of the function is composed of zero, one, or
105+
// many variables
106+
class variablet : public irept
107+
{
108+
public:
109+
// the identifier is optional
110+
irep_idt get_identifier() const
111+
{
112+
return get(ID_identifier);
113+
}
114+
115+
void set_identifier(const irep_idt &identifier)
116+
{
117+
return set(ID_identifier, identifier);
118+
}
119+
120+
typet &type()
121+
{
122+
return static_cast<typet &>(add(ID_type));
123+
}
124+
125+
const typet &type() const
126+
{
127+
return static_cast<const typet &>(find(ID_type));
128+
}
129+
};
130+
131+
using domaint = std::vector<variablet>;
132+
133+
mathematical_function_typet(const domaint &_domain, const typet &_codomain)
134+
: typet(ID_mathematical_function)
135+
{
136+
subtypes().resize(2);
137+
domain() = _domain;
138+
codomain() = _codomain;
139+
}
140+
141+
domaint &domain()
142+
{
143+
return (domaint &)subtypes()[0].subtypes();
144+
}
145+
146+
const domaint &domain() const
147+
{
148+
return (const domaint &)subtypes()[0].subtypes();
149+
}
150+
151+
variablet &add_variable()
152+
{
153+
auto &d = domain();
154+
d.push_back(variablet());
155+
return d.back();
156+
}
157+
158+
/// Return the codomain, i.e., the set of values that the function maps to
159+
/// (the "target").
160+
typet &codomain()
161+
{
162+
return subtypes()[1];
163+
}
164+
165+
/// \copydoc codomain()
166+
const typet &codomain() const
167+
{
168+
return subtypes()[1];
169+
}
170+
};
171+
172+
/// Check whether a reference to a typet is a \ref mathematical_function_typet.
173+
/// \param type Source type.
174+
/// \return True if \param type is a \ref mathematical_function_typet.
175+
template <>
176+
inline bool can_cast_type<mathematical_function_typet>(const typet &type)
177+
{
178+
return type.id() == ID_mathematical_function;
179+
}
180+
181+
/// \brief Cast a typet to a \ref mathematical_function_typet
182+
///
183+
/// This is an unchecked conversion. \a type must be known to be \ref
184+
/// mathematical_function_typet. Will fail with a precondition violation if type
185+
/// doesn't match.
186+
///
187+
/// \param type Source type.
188+
/// \return Object of type \ref mathematical_function_typet.
189+
inline const mathematical_function_typet &
190+
to_mathematical_function_type(const typet &type)
191+
{
192+
PRECONDITION(can_cast_type<mathematical_function_typet>(type));
193+
return static_cast<const mathematical_function_typet &>(type);
194+
}
195+
196+
/// \copydoc to_mathematical_function_type(const typet &)
197+
inline mathematical_function_typet &to_mathematical_function_type(typet &type)
198+
{
199+
PRECONDITION(can_cast_type<mathematical_function_typet>(type));
200+
return static_cast<mathematical_function_typet &>(type);
201+
}
202+
203+
bool is_number(const typet &type);
204+
205+
#endif // CPROVER_UTIL_MATHEMATICAL_TYPES_H

src/util/simplify_expr_int.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include "fixedbv.h"
1919
#include "ieee_float.h"
2020
#include "invariant.h"
21+
#include "mathematical_types.h"
2122
#include "namespace.h"
2223
#include "pointer_offset_size.h"
2324
#include "rational.h"

src/util/std_expr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ Author: Daniel Kroening, [email protected]
1717
* \date Sun Jul 31 21:54:44 BST 2011
1818
*/
1919

20+
#include "expr_cast.h"
2021
#include "invariant.h"
22+
#include "mathematical_types.h"
2123
#include "std_types.h"
22-
#include "expr_cast.h"
23-
2424

2525
/*! \brief A transition system, consisting of
2626
state invariant, initial state predicate,

src/util/std_types.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,6 @@ constant_exprt unsignedbv_typet::largest_expr() const
197197
return from_integer(largest(), *this);
198198
}
199199

200-
/// Returns true if the type is a rational, real, integer, natural, complex,
201-
/// unsignedbv, signedbv, floatbv or fixedbv.
202-
bool is_number(const typet &type)
203-
{
204-
const irep_idt &id = type.id();
205-
return id == ID_rational || id == ID_real || id == ID_integer ||
206-
id == ID_natural || id == ID_complex || id == ID_unsignedbv ||
207-
id == ID_signedbv || id == ID_floatbv || id == ID_fixedbv;
208-
}
209-
210200
/// Identify whether a given type is constant itself or contains constant
211201
/// components.
212202
/// Examples include:

0 commit comments

Comments
 (0)