carl  24.04
Computer ARithmetic Library
UTerm.h
Go to the documentation of this file.
1 /**
2  * @file UTerm.h
3  * @author Rebecca Haehn <haehn@cs.rwth-aachen.de>
4  * @since 2018-08-28
5  * @version 2018-08-28
6  */
7 
8 #pragma once
9 
11 #include "UFInstance.h"
12 #include "UVariable.h"
13 
14 #include <iostream>
15 #include <variant>
16 
17 namespace carl
18 {
19  /**
20  * Implements an uninterpreted term, that is either an uninterpreted variable or an uninterpreted function instance.
21  */
22  class UTerm {
23  using Super = std::variant<UVariable, UFInstance>;
25 
26  public:
27  /**
28  * Default constructor.
29  */
30  UTerm() = default;
31 
32  UTerm(UVariable v): mTerm(v) {}
33  UTerm(UFInstance ufi): mTerm(ufi) {}
34 
35  /**
36  * Constructs an uninterpreted term.
37  * @param term
38  */
39  explicit UTerm(const Super& term): mTerm(term) {}
40 
41  const auto& asVariant() const {
42  return mTerm;
43  }
44 
45  /**
46  * @return true, if the stored term is a UVariable.
47  */
48  bool isUVariable() const {
49  return std::holds_alternative<UVariable>(mTerm);
50  }
51 
52  /**
53  * @return true, if the stored term is a UFInstance.
54  */
55  bool isUFInstance() const {
56  return std::holds_alternative<UFInstance>(mTerm);
57  }
58 
59  /**
60  * @return The stored term as UVariable.
61  */
63  assert(isUVariable());
64  return std::get<UVariable>(mTerm);
65  }
66  /**
67  * @return The stored term as UFInstance.
68  */
70  assert(isUFInstance());
71  return std::get<UFInstance>(mTerm);
72  }
73 
74  /**
75  * @return The domain of this uninterpreted term.
76  */
77  Sort domain() const;
78 
79  std::size_t complexity() const;
80 
81  void gatherVariables(carlVariables& vars) const;
82  void gatherUFs(std::set<UninterpretedFunction>& ufs) const;
83  };
84 
85  /**
86  * @param lhs The uninterpreted term to the left.
87  * @param rhs The uninterpreted term to the right.
88  * @return true, if the given uninterpreted terms are equal.
89  */
90  bool operator==(const UTerm& lhs, const UTerm& rhs);
91 
92  bool operator!=(const UTerm& lhs, const UTerm& rhs);
93 
94  /**
95  * @param lhs The uninterpreted term to the left.
96  * @param rhs The uninterpreted term to the right.
97  * @return true, if lhs is smaller than rhs.
98  */
99  bool operator<(const UTerm& lhs, const UTerm& rhs);
100 
101  /**
102  * Prints the given uninterpreted term on the given output stream.
103  * @param os The output stream to print on.
104  * @param ut The uninterpreted term to print.
105  * @return The output stream after printing the given uninterpreted term on it.
106  */
107  std::ostream& operator<<(std::ostream& os, const UTerm& ut);
108 } // end namespace carl
109 
110 namespace std
111 {
112  /**
113  * Implements std::hash for uninterpreted terms.
114  */
115  template<>
116  struct hash<carl::UTerm> {
117  /**
118  * @param ut The uninterpreted term to get the hash for.
119  * @return The hash of the given uninterpreted term.
120  */
121  std::size_t operator()(const carl::UTerm& ut) const {
123  [](carl::UVariable var) { return carl::hash_all(var); },
124  [](carl::UFInstance ufi) { return carl::hash_all(ufi); },
125  }, ut.asVariant());
126  }
127  };
128 }
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.
void visit(const Formula< Pol > &formula, Visitor func)
Recursively calls func on every subformula.
Definition: Visit.h:12
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 a sort (for defining types of variables and functions).
Definition: Sort.h:20
Implements an uninterpreted function instance.
Definition: UFInstance.h:25
Implements an uninterpreted term, that is either an uninterpreted variable or an uninterpreted functi...
Definition: UTerm.h:22
UTerm()=default
Default constructor.
std::variant< UVariable, UFInstance > Super
Definition: UTerm.h:23
void gatherUFs(std::set< UninterpretedFunction > &ufs) const
Definition: UTerm.cpp:37
UFInstance asUFInstance() const
Definition: UTerm.h:69
bool isUFInstance() const
Definition: UTerm.h:55
Sort domain() const
Definition: UTerm.cpp:16
const auto & asVariant() const
Definition: UTerm.h:41
UTerm(const Super &term)
Constructs an uninterpreted term.
Definition: UTerm.h:39
bool isUVariable() const
Definition: UTerm.h:48
std::size_t complexity() const
Definition: UTerm.cpp:23
UTerm(UVariable v)
Definition: UTerm.h:32
UVariable asUVariable() const
Definition: UTerm.h:62
UTerm(UFInstance ufi)
Definition: UTerm.h:33
Super mTerm
Definition: UTerm.h:24
void gatherVariables(carlVariables &vars) const
Definition: UTerm.cpp:30
std::size_t operator()(const carl::UTerm &ut) const
Definition: UTerm.h:121
Implements an uninterpreted variable.
Definition: UVariable.h:19