carl  24.04
Computer ARithmetic Library
ModelVariable.h
Go to the documentation of this file.
1 #pragma once
2 
8 
9 #include <cassert>
10 #include <variant>
11 
12 namespace carl {
13 
14 /**
15  * Represent a sum type/variant over the different kinds of variables that
16  * exist in CARL to use them in a more uniform way,
17  * e.g. an (algebraic) "carl::Variable", an (uninterpreted) "carl::UVariable",
18  * an "carl::UninterpretedFunction" etc.
19  */
21  /**
22  * Base type for the content.
23  */
24  using Base = std::variant<Variable, BVVariable, UVariable, UninterpretedFunction>;
25 
27 
28 public:
29  friend bool operator==(const ModelVariable& lhs, const ModelVariable& rhs);
30  friend bool operator<(const ModelVariable& lhs, const ModelVariable& rhs);
31  friend std::ostream& operator<<(std::ostream& os, const ModelVariable& mv);
32  friend std::hash<ModelVariable>;
33 
34  /**
35  * Initialize the ModelVariable from some valid type of the underlying variant.
36  */
37  template<typename T, typename T2 = typename std::enable_if<convertible_to_variant<T, Base>::value, T>::type>
38  ModelVariable(const T& _t)
39  : mData(_t) {}
40 
41  /**
42  * @return true, if the stored value is a variable.
43  */
44  bool is_variable() const {
45  return std::holds_alternative<carl::Variable>(mData);
46  }
47 
48  /**
49  * @return true, if the stored value is a bitvector variable.
50  */
51  bool isBVVariable() const {
52  return std::holds_alternative<carl::BVVariable>(mData);
53  }
54 
55  /**
56  * @return true, if the stored value is an uninterpreted variable.
57  */
58  bool isUVariable() const {
59  return std::holds_alternative<carl::UVariable>(mData);
60  }
61 
62  /**
63  * @return true, if the stored value is a function.
64  */
65  bool isFunction() const {
66  return std::holds_alternative<carl::UninterpretedFunction>(mData);
67  }
68 
69  /**
70  * @return The stored value as a variable.
71  */
73  assert(is_variable());
74  return std::get<carl::Variable>(mData);
75  }
76 
77  /**
78  * @return The stored value as a bitvector variable.
79  */
80  const carl::BVVariable& asBVVariable() const {
81  assert(isBVVariable());
82  return std::get<carl::BVVariable>(mData);
83  }
84 
85  /**
86  * @return The stored value as an uninterpreted variable.
87  */
88  const carl::UVariable& asUVariable() const {
89  assert(isUVariable());
90  return std::get<carl::UVariable>(mData);
91  }
92 
93  /**
94  * @return The stored value as a function.
95  */
97  assert(isFunction());
98  return std::get<carl::UninterpretedFunction>(mData);
99  }
100 };
101 
102 /**
103  * Return true if lhs is equal to rhs.
104  */
105 inline bool operator==(const ModelVariable& lhs, const ModelVariable& rhs) {
106  return lhs.mData == rhs.mData;
107 }
108 
109 /**
110  * Return true if lhs is smaller than rhs.
111  */
112 inline bool operator<(const ModelVariable& lhs, const ModelVariable& rhs) {
113  return lhs.mData < rhs.mData;
114 }
115 
116 inline std::ostream& operator<<(std::ostream& os, const ModelVariable& mv) {
117  return os << mv.mData;
118 }
119 } // namespace carl
120 
121 namespace std {
122 template<>
123 struct hash<carl::ModelVariable> {
124  std::size_t operator()(const carl::ModelVariable& mv) const {
125  return std::hash<carl::ModelVariable::Base>()(mv.mData);
126  }
127 };
128 } // namespace std
carl is the main namespace for the library.
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
Represent a BitVector-Variable.
Definition: BVVariable.h:13
Represent a sum type/variant over the different kinds of variables that exist in CARL to use them in ...
Definition: ModelVariable.h:20
const carl::UVariable & asUVariable() const
Definition: ModelVariable.h:88
bool is_variable() const
Definition: ModelVariable.h:44
bool isFunction() const
Definition: ModelVariable.h:65
carl::Variable asVariable() const
Definition: ModelVariable.h:72
friend bool operator<(const ModelVariable &lhs, const ModelVariable &rhs)
Return true if lhs is smaller than rhs.
friend bool operator==(const ModelVariable &lhs, const ModelVariable &rhs)
Return true if lhs is equal to rhs.
ModelVariable(const T &_t)
Initialize the ModelVariable from some valid type of the underlying variant.
Definition: ModelVariable.h:38
const carl::UninterpretedFunction & asFunction() const
Definition: ModelVariable.h:96
bool isBVVariable() const
Definition: ModelVariable.h:51
bool isUVariable() const
Definition: ModelVariable.h:58
std::variant< Variable, BVVariable, UVariable, UninterpretedFunction > Base
Base type for the content.
Definition: ModelVariable.h:24
const carl::BVVariable & asBVVariable() const
Definition: ModelVariable.h:80
friend std::ostream & operator<<(std::ostream &os, const ModelVariable &mv)
std::size_t operator()(const carl::ModelVariable &mv) const
Implements an uninterpreted function.
Implements an uninterpreted variable.
Definition: UVariable.h:19