|
| 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 |
0 commit comments