carl  24.04
Computer ARithmetic Library
UEquality.h
Go to the documentation of this file.
1 /**
2  * @file UEquality.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  * @author Florian Corzilius <corzilius@cs.rwth-aachen.de>
5  * @since 2014-10-20
6  * @version 2014-10-22
7  */
8 
9 #pragma once
10 
11 #include "UVariable.h"
12 #include "UFInstance.h"
13 #include "UTerm.h"
14 
15 namespace carl
16 {
17  /**
18  * Implements an uninterpreted equality, that is an equality of either
19  * two uninterpreted function instances,
20  * two uninterpreted variables,
21  * or an uninterpreted function instance and an uninterpreted variable.
22  */
23  class UEquality {
24  private:
25  /// A flag indicating whether this equality shall hold (if being false) or its negation (if being true), hence the inequality, shall hold.
26  bool mNegated = false;
27  /// The left-hand side of this uninterpreted equality.
29  /// The right-hand side of this uninterpreted equality.
31 
32  public:
33  UEquality() = default;
34 
35  UEquality(const UEquality&) = default;
36  UEquality(UEquality&&) = default;
37 
38  UEquality& operator=(const UEquality&) = default;
39  UEquality& operator=(UEquality&&) = default;
40  /**
41  * Constructs an uninterpreted equality.
42  * @param negated true, if the negation of this equality shall hold, which means that it is actually an inequality.
43  * @param lhs An uninterpreted variable, which is going to be the left-hand side of this uninterpreted equality.
44  * @param rhs An uninterpreted variable, which is going to be the right-hand side of this uninterpreted equality.
45  */
46  UEquality(const UTerm& lhs, const UTerm& rhs, bool negated):
48  assert(mLhs.domain() == mRhs.domain());
49  if (mRhs < mLhs) std::swap(mLhs, mRhs);
50  }
51 
52  /**
53  * Copies the given uninterpreted equality.
54  * @param ueq The uninterpreted equality to copy.
55  * @param invert true, if the inverse of the given uninterpreted equality shall be constructed. (== -> != resp. != -> ==)
56  */
57  UEquality(const UEquality& ueq, bool invert):
58  mNegated(invert ? !ueq.mNegated : ueq.mNegated),
59  mLhs(ueq.mLhs),
60  mRhs(ueq.mRhs) {}
61 
62  /**
63  * @return true, if the negation of this equation shall hold, that is, it is actually an inequality.
64  */
65  bool negated() const {
66  return mNegated;
67  }
68 
69  /**
70  * @return The left-hand side of this equality.
71  */
72  const UTerm& lhs() const {
73  return mLhs;
74  }
75 
76  /**
77  * @return The right-hand side of this equality.
78  */
79  const UTerm& rhs() const {
80  return mRhs;
81  }
82 
83  /**
84  * @return An approximation of the complexity of this uninterpreted equality.
85  */
86  std::size_t complexity() const {
87  return 1 + mLhs.complexity() + mRhs.complexity();
88  }
89 
90  /*
91  * @return The negation of the uninterpreted equality.
92  */
93  UEquality negation() const {
94  return UEquality(lhs(), rhs(), !negated());
95  }
96 
97  void gatherVariables(carlVariables& vars) const {
98  mLhs.gatherVariables(vars);
99  mRhs.gatherVariables(vars);
100  }
101  void gatherUFs(std::set<UninterpretedFunction>& ufs) const {
102  mLhs.gatherUFs(ufs);
103  mRhs.gatherUFs(ufs);
104  }
105  void gatherUVariables(std::set<UVariable>& uvars) const;
106 
107  };
108 
109  /**
110  * @param lhs The left hand side.
111  * @param rhs The right hand side.
112  * @return true, if lhs and rhs are equal.
113  */
114  inline bool operator==(const UEquality& lhs, const UEquality& rhs) {
115  return std::forward_as_tuple(lhs.negated(), lhs.lhs(), lhs.rhs()) == std::forward_as_tuple(rhs.negated(), rhs.lhs(), rhs.rhs());
116  }
117 
118  /**
119  * @param lhs The left hand side.
120  * @param rhs The right hand side.
121  * @return true, if lhs and rhs are not equal.
122  */
123  inline bool operator!=(const UEquality& lhs, const UEquality& rhs) {
124  return !(lhs == rhs);
125  }
126 
127  /**
128  * @param lhs The left hand side.
129  * @param rhs The right hand side.
130  * @return true, if the left equality is less than the right one.
131  */
132  inline bool operator<(const UEquality& lhs, const UEquality& rhs) {
133  return std::forward_as_tuple(lhs.negated(), lhs.lhs(), lhs.rhs()) < std::forward_as_tuple(rhs.negated(), rhs.lhs(), rhs.rhs());
134  }
135 
136  /**
137  * Prints the given uninterpreted equality on the given output stream.
138  * @param os The output stream to print on.
139  * @param ueq The uninterpreted equality to print.
140  * @return The output stream after printing the given uninterpreted equality on it.
141  */
142  inline std::ostream& operator<<(std::ostream& os, const UEquality& ueq) {
143  return os << ueq.lhs() << (ueq.negated() ? " != " : " == ") << ueq.rhs();
144  }
145 } // end namespace carl
146 
147 namespace std
148 {
149  /**
150  * Implements std::hash for uninterpreted equalities.
151  */
152  template<>
153  struct hash<carl::UEquality> {
154  public:
155  /**
156  * @param ueq The uninterpreted equality to get the hash for.
157  * @return The hash of the given uninterpreted equality.
158  */
159  std::size_t operator()(const carl::UEquality& ueq) const {
160  return carl::hash_all(ueq.negated(), ueq.lhs(), ueq.rhs());
161  }
162  };
163 }
carl is the main namespace for the library.
void swap(Variable &lhs, Variable &rhs)
Definition: Variables.h:202
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)
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
std::size_t hash_all(Args &&... args)
Hashes an arbitrary number of values.
Definition: hash.h:71
Implements an uninterpreted equality, that is an equality of either two uninterpreted function instan...
Definition: UEquality.h:23
void gatherUVariables(std::set< UVariable > &uvars) const
Definition: UEquality.cpp:22
std::size_t complexity() const
Definition: UEquality.h:86
UEquality negation() const
Definition: UEquality.h:93
UTerm mRhs
The right-hand side of this uninterpreted equality.
Definition: UEquality.h:30
bool mNegated
A flag indicating whether this equality shall hold (if being false) or its negation (if being true),...
Definition: UEquality.h:26
void gatherVariables(carlVariables &vars) const
Definition: UEquality.h:97
const UTerm & rhs() const
Definition: UEquality.h:79
UEquality(const UTerm &lhs, const UTerm &rhs, bool negated)
Constructs an uninterpreted equality.
Definition: UEquality.h:46
UEquality()=default
UEquality(const UEquality &ueq, bool invert)
Copies the given uninterpreted equality.
Definition: UEquality.h:57
bool negated() const
Definition: UEquality.h:65
const UTerm & lhs() const
Definition: UEquality.h:72
UEquality & operator=(UEquality &&)=default
void gatherUFs(std::set< UninterpretedFunction > &ufs) const
Definition: UEquality.h:101
UEquality(UEquality &&)=default
UEquality & operator=(const UEquality &)=default
UEquality(const UEquality &)=default
UTerm mLhs
The left-hand side of this uninterpreted equality.
Definition: UEquality.h:28
std::size_t operator()(const carl::UEquality &ueq) const
Definition: UEquality.h:159
Implements an uninterpreted term, that is either an uninterpreted variable or an uninterpreted functi...
Definition: UTerm.h:22
void gatherUFs(std::set< UninterpretedFunction > &ufs) const
Definition: UTerm.cpp:37
Sort domain() const
Definition: UTerm.cpp:16
std::size_t complexity() const
Definition: UTerm.cpp:23
void gatherVariables(carlVariables &vars) const
Definition: UTerm.cpp:30