Skip to content

Commit 3a29698

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

8 files changed

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