|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Mathematical types |
| 4 | +
|
| 5 | +Author: Daniel Kroening, kroening@kroening.com |
| 6 | + Maria Svorenova, maria.svorenova@diffblue.com |
| 7 | +
|
| 8 | +\*******************************************************************/ |
| 9 | + |
| 10 | +/// \file |
| 11 | +/// 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 | +/// A type for mathematical functions (do not confuse with functions/methods |
| 57 | +/// in code) |
| 58 | +class mathematical_function_typet : public typet |
| 59 | +{ |
| 60 | +public: |
| 61 | + // the domain of the function is composed of zero, one, or |
| 62 | + // many variables |
| 63 | + class variablet : public irept |
| 64 | + { |
| 65 | + public: |
| 66 | + // the identifier is optional |
| 67 | + irep_idt get_identifier() const |
| 68 | + { |
| 69 | + return get(ID_identifier); |
| 70 | + } |
| 71 | + |
| 72 | + void set_identifier(const irep_idt &identifier) |
| 73 | + { |
| 74 | + return set(ID_identifier, identifier); |
| 75 | + } |
| 76 | + |
| 77 | + typet &type() |
| 78 | + { |
| 79 | + return static_cast<typet &>(add(ID_type)); |
| 80 | + } |
| 81 | + |
| 82 | + const typet &type() const |
| 83 | + { |
| 84 | + return static_cast<const typet &>(find(ID_type)); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + using domaint = std::vector<variablet>; |
| 89 | + |
| 90 | + mathematical_function_typet(const domaint &_domain, const typet &_codomain) |
| 91 | + : typet(ID_mathematical_function) |
| 92 | + { |
| 93 | + subtypes().resize(2); |
| 94 | + domain() = _domain; |
| 95 | + codomain() = _codomain; |
| 96 | + } |
| 97 | + |
| 98 | + domaint &domain() |
| 99 | + { |
| 100 | + return (domaint &)subtypes()[0].subtypes(); |
| 101 | + } |
| 102 | + |
| 103 | + const domaint &domain() const |
| 104 | + { |
| 105 | + return (const domaint &)subtypes()[0].subtypes(); |
| 106 | + } |
| 107 | + |
| 108 | + variablet &add_variable() |
| 109 | + { |
| 110 | + auto &d = domain(); |
| 111 | + d.push_back(variablet()); |
| 112 | + return d.back(); |
| 113 | + } |
| 114 | + |
| 115 | + /// Return the codomain, i.e., the set of values that the function maps to |
| 116 | + /// (the "target"). |
| 117 | + typet &codomain() |
| 118 | + { |
| 119 | + return subtypes()[1]; |
| 120 | + } |
| 121 | + |
| 122 | + /// \copydoc codomain() |
| 123 | + const typet &codomain() const |
| 124 | + { |
| 125 | + return subtypes()[1]; |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +/// Check whether a reference to a typet is a \ref mathematical_function_typet. |
| 130 | +/// \param type Source type. |
| 131 | +/// \return True if \param type is a \ref mathematical_function_typet. |
| 132 | +template <> |
| 133 | +inline bool can_cast_type<mathematical_function_typet>(const typet &type) |
| 134 | +{ |
| 135 | + return type.id() == ID_mathematical_function; |
| 136 | +} |
| 137 | + |
| 138 | +/// \brief Cast a typet to a \ref mathematical_function_typet |
| 139 | +/// |
| 140 | +/// This is an unchecked conversion. \a type must be known to be \ref |
| 141 | +/// mathematical_function_typet. Will fail with a precondition violation if type |
| 142 | +/// doesn't match. |
| 143 | +/// |
| 144 | +/// \param type Source type. |
| 145 | +/// \return Object of type \ref mathematical_function_typet. |
| 146 | +inline const mathematical_function_typet & |
| 147 | +to_mathematical_function_type(const typet &type) |
| 148 | +{ |
| 149 | + PRECONDITION(can_cast_type<mathematical_function_typet>(type)); |
| 150 | + return static_cast<const mathematical_function_typet &>(type); |
| 151 | +} |
| 152 | + |
| 153 | +/// \copydoc to_mathematical_function_type(const typet &) |
| 154 | +inline mathematical_function_typet &to_mathematical_function_type(typet &type) |
| 155 | +{ |
| 156 | + PRECONDITION(can_cast_type<mathematical_function_typet>(type)); |
| 157 | + return static_cast<mathematical_function_typet &>(type); |
| 158 | +} |
| 159 | + |
| 160 | +bool is_number(const typet &type); |
| 161 | + |
| 162 | +#endif // CPROVER_UTIL_MATHEMATICAL_TYPES_H |
0 commit comments