carl  24.04
Computer ARithmetic Library
FormulaContent.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <iostream>
6 #include <variant>
7 
8 namespace carl {
9  // Forward declaration.
10  template<typename Pol>
11  class Formula;
12  // Forward declaration.
13  template<typename Pol>
14  class FormulaPool;
15 
16 
17  template<typename Poly>
18  using Formulas = std::vector<Formula<Poly>>;
19  template<typename Poly>
20  using FormulaSet = std::set<Formula<Poly>>;
21  template<typename Poly>
22  using FormulasMulti = std::multiset<Formula<Poly>>;
23 
24 
25  ///
26  /**
27  * Represent the type of a formula to allow faster/specialized processing.
28  *
29  * For each (supported) SMTLIB theory, we have
30  * - Constants
31  * - Variables
32  * - Functions
33  * - Additional functions (not specified, but used in the wild)
34  */
35  enum FormulaType {
36  // Generic
38 
39  // Core Theory
43  IFF,
44 
45  // Arithmetic Theory
49 
50  // Bitvector Theory
52 
53  // Uninterpreted Theory
54  UEQ
55  };
56 
57 
58 
59  /**
60  * @param _type The formula type to get the string representation for.
61  * @return The string representation of the given type.
62  */
63  inline std::string formulaTypeToString(FormulaType _type) {
64  switch (_type) {
65  case FormulaType::ITE: return "ite";
66  case FormulaType::EXISTS: return "exists";
67  case FormulaType::FORALL: return "forall";
68  case FormulaType::TRUE: return "true";
69  case FormulaType::FALSE: return "false";
70  case FormulaType::BOOL: return "bool";
71  case FormulaType::NOT: return "not";
72  case FormulaType::IMPLIES: return "=>";
73  case FormulaType::AND: return "and";
74  case FormulaType::OR: return "or";
75  case FormulaType::XOR: return "xor";
76  case FormulaType::IFF: return "=";
77  case FormulaType::CONSTRAINT: return "constraint";
78  case FormulaType::VARCOMPARE: return "varcompare";
79  case FormulaType::VARASSIGN: return "varassign";
80  case FormulaType::BITVECTOR: return "bv";
81  case FormulaType::UEQ: return "ueq";
82  default:
83  CARL_LOG_ERROR("carl.formula", "Unknown formula type " << unsigned(_type));
84  return "???";
85  }
86  }
87  inline std::ostream& operator<<(std::ostream& os, FormulaType t) {
88  return os << formulaTypeToString(t);
89  }
90 
91  /**
92  * Stores the variables and the formula bound by a quantifier.
93  */
94  template<typename Pol>
96  {
97  /// The quantified variables.
98  std::vector<carl::Variable> mVariables;
99  /// The formula bound by this quantifier.
101 
102  /**
103  * Constructs the content of a quantified formula.
104  * @param _vars The quantified variables.
105  * @param _formula The formula bound by this quantifier.
106  */
107  QuantifierContent( std::vector<carl::Variable>&& _vars, Formula<Pol>&& _formula ):
108  mVariables( std::move(_vars) ),
109  mFormula( std::move(_formula) )
110  {}
111 
112  /**
113  * Checks this content of a quantified formula and the given content of a quantified formula is equal.
114  * @param _qc The content of a quantified formula to check for equality.
115  * @return true, if this content of a quantified formula and the given content of a quantified formula is equal.
116  */
117  bool operator==(const QuantifierContent& _qc) const
118  {
119  return (mFormula == _qc.mFormula) && (mVariables == _qc.mVariables);
120  }
121  };
122 
123  template<typename Pol>
124  class FormulaContent : public boost::intrusive::unordered_set_base_hook<>
125  {
126  friend class Formula<Pol>;
127  friend class FormulaPool<Pol>;
128  template<typename P>
129  friend std::ostream& operator<<(std::ostream& os, const FormulaContent<P>& f);
130 
131  private:
132 
133  // Member.
134  /// The hash value.
135  size_t mHash = 0;
136  /// The unique id.
137  size_t mId = 0;
138  /// The activity for this formula, which means, how much is this formula involved in the solving procedure.
139  mutable double mActivity = 0.0;
140  /// The number of formulas existing with this content.
141  mutable size_t mUsages = 0;
142  /// The type of this formula.
144  /// The content of this formula.
145  std::variant<carl::Variable, // The variable, in case this formula wraps a variable.
146  Constraint<Pol>, // The constraint, in case this formula wraps a constraint.
147  VariableComparison<Pol>, // A constraint comparing a single variable with a value.
148  VariableAssignment<Pol>, // A constraint assigning a single variable to a value.
149  BVConstraint, // The bitvector constraint.
150  UEquality, // The uninterpreted equality, in case this formula wraps an uninterpreted equality.
151  Formula<Pol>, // The only sub-formula, in case this formula is an negation.
152  Formulas<Pol>, // The subformulas, in case this formula is a n-nary operation as AND, OR, IFF or XOR.
153  QuantifierContent<Pol>> mContent; // The quantifed variables and the bound formula, in case this formula is a quantified formula.
154  /// The negation
155  const FormulaContent<Pol> *mNegation = nullptr;
156  /// The propositions of this formula.
158  #ifdef THREAD_SAFE
159  /// Mutex for access to activity.
160  mutable std::mutex mActivityMutex;
161  /// Mutex for collecting the variables within this formula.
162  mutable std::mutex mVariablesMutex;
163  #endif
164  /// Container collecting the variables which occur in this formula.
165  mutable Variables* mpVariables = nullptr;
166 
167  FormulaContent() = delete;
168  FormulaContent(const FormulaContent&) = delete;
170 
171  /**
172  * Constructs the formula (true), if the given bool is true and the formula (false) otherwise.
173  * @param _true Specifies whether to create the formula (true) or (false).
174  * @param _id A unique id of the formula to create.
175  */
176  FormulaContent(FormulaType _type, std::size_t _id = 0);
177 
178  /**
179  * Constructs a formula being a variable.
180  * @param _variable
181  */
183 
184  /**
185  * Constructs a formula being a constraint.
186  * @param _constraint The pointer to the constraint.
187  */
189 
190  FormulaContent(VariableComparison<Pol>&& _variableComparison);
191 
192  FormulaContent(VariableAssignment<Pol>&& _variableAssignment);
193 
194  /**
195  * Constructs a formula being a bitvector constraint.
196  * @param _constraint The pointer to the constraint.
197  */
198  FormulaContent(BVConstraint&& _constraint);
199 
200  /**
201  * Constructs a formula being an uninterpreted equality.
202  * @param _ueq The pointer to the constraint.
203  */
205 
206  /**
207  * Constructs a formula of the given type with a single subformula. This is usually a negation.
208  */
209  FormulaContent(FormulaType _type, Formula<Pol>&& _subformula);
210 
211  FormulaContent(FormulaType _type, const std::initializer_list<Formula<Pol>>& _subformula);
212  FormulaContent(FormulaType _type, Formulas<Pol>&& _subformula);
213 
214 
215  /**
216  * Constructs a quantifier expression: (exists (vars) term) or (forall (vars) term)
217  * @param _type The type of the quantifier to construct.
218  * @param _vars The variables that are bound.
219  * @param _term The term in which the variables are bound.
220  */
221  FormulaContent(FormulaType _type, std::vector<carl::Variable>&& _vars, const Formula<Pol>& _term);
222 
223 
224  public:
225 
226  /**
227  * Destructor.
228  */
230  if( mpVariables != nullptr )
231  delete mpVariables;
232  }
233 
234  std::size_t hash() const {
235  return mHash;
236  }
237  std::size_t id() const {
238  return mId;
239  }
240 
241  bool is_nary() const
242  {
243  switch (mType) {
244  case FormulaType::TRUE: return false;
245  case FormulaType::FALSE: return false;
246  case FormulaType::BOOL: return false;
247  case FormulaType::NOT: return false;
248  case FormulaType::IMPLIES: return true;
249  case FormulaType::AND: return true;
250  case FormulaType::OR: return true;
251  case FormulaType::XOR: return true;
252  case FormulaType::IFF: return true;
253  case FormulaType::ITE: return true;
254  case FormulaType::EXISTS: return false;
255  case FormulaType::FORALL: return false;
256  case FormulaType::CONSTRAINT: return false;
257  case FormulaType::VARCOMPARE: return false;
258  case FormulaType::VARASSIGN: return false;
259  case FormulaType::BITVECTOR: return false;
260  case FormulaType::UEQ: return false;
261  }
262  return false;
263  }
264 
265  bool operator==(const FormulaContent& _content) const;
266  };
267  /**
268  * The output operator of a formula.
269  * @param os The stream to print on.
270  * @param f
271  */
272  template<typename Pol>
273  std::ostream& operator<<(std::ostream& os, const FormulaContent<Pol>& f) {
274  switch (f.mType) {
275  case FormulaType::FALSE:
276  return os << formulaTypeToString(f.mType);
277  case FormulaType::TRUE:
278  return os << formulaTypeToString(f.mType);
279  case FormulaType::BOOL:
280  return os << std::get<carl::Variable>(f.mContent);
282  return os << std::get<Constraint<Pol>>(f.mContent);
284  return os << std::get<VariableAssignment<Pol>>(f.mContent);
286  return os << std::get<VariableComparison<Pol>>(f.mContent);
288  return os << std::get<BVConstraint>(f.mContent);
289  case FormulaType::UEQ:
290  return os << std::get<UEquality>(f.mContent);
291  case FormulaType::NOT:
292  return os << "!(" << std::get<Formula<Pol>>(f.mContent) << ")";
293  case FormulaType::EXISTS:
294  os << "(exists";
295  for (auto v: std::get<QuantifierContent<Pol>>(f.mContent).mVariables) os << " " << v;
296  return os << ")(" << std::get<QuantifierContent<Pol>>(f.mContent).mFormula << ")";
297  case FormulaType::FORALL:
298  os << "(forall";
299  for (auto v: std::get<QuantifierContent<Pol>>(f.mContent).mVariables) os << " " << v;
300  return os << ")(" << std::get<QuantifierContent<Pol>>(f.mContent).mFormula << ")";
301  default:
302  assert(f.is_nary());
303  return os << "(" << carl::stream_joined(" " + formulaTypeToString(f.mType) + " ", std::get<Formulas<Pol>>(f.mContent)) << ")";
304  }
305  }
306 
307  template<typename Pol>
308  std::ostream& operator<<(std::ostream& os, const FormulaContent<Pol>* fc) {
309  assert(fc != nullptr);
310  return os << *fc;
311  }
312 
313 }
314 
315 #include "FormulaContent.tpp"
A small wrapper that configures logging for carl.
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
MultivariatePolynomial< Rational > Pol
Definition: HornerTest.cpp:17
carl is the main namespace for the library.
FormulaType
Represent the type of a formula to allow faster/specialized processing.
@ CONSTRAINT
@ BITVECTOR
@ VARCOMPARE
@ VARASSIGN
std::set< Formula< Poly > > FormulaSet
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
std::vector< Formula< Poly > > Formulas
std::string formulaTypeToString(FormulaType _type)
auto stream_joined(const std::string &glue, const T &v)
Allows to easily output some container with all elements separated by some string.
std::multiset< Formula< Poly > > FormulasMulti
std::set< Variable > Variables
Definition: Common.h:18
auto & get(const std::string &name)
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
Represent a sum type/variant of an (in)equality between a variable on the left-hand side and multivar...
Represent a polynomial (in)equality against zero.
Definition: Constraint.h:62
Represent an SMT formula, which can be an atom for some background theory or a boolean combination of...
Definition: Formula.h:47
Stores the variables and the formula bound by a quantifier.
Formula< Pol > mFormula
The formula bound by this quantifier.
QuantifierContent(std::vector< carl::Variable > &&_vars, Formula< Pol > &&_formula)
Constructs the content of a quantified formula.
bool operator==(const QuantifierContent &_qc) const
Checks this content of a quantified formula and the given content of a quantified formula is equal.
std::vector< carl::Variable > mVariables
The quantified variables.
FormulaContent(const FormulaContent &)=delete
~FormulaContent()
Destructor.
Variables * mpVariables
Container collecting the variables which occur in this formula.
bool operator==(const FormulaContent &_content) const
std::size_t hash() const
friend std::ostream & operator<<(std::ostream &os, const FormulaContent< P > &f)
double mActivity
The activity for this formula, which means, how much is this formula involved in the solving procedur...
FormulaContent(FormulaType _type, Formulas< Pol > &&_subformula)
FormulaContent(FormulaType _type, std::size_t _id=0)
Constructs the formula (true), if the given bool is true and the formula (false) otherwise.
FormulaContent(FormulaType _type, const std::initializer_list< Formula< Pol >> &_subformula)
std::size_t id() const
FormulaType mType
The type of this formula.
FormulaContent(FormulaContent &&o)
FormulaContent(VariableAssignment< Pol > &&_variableAssignment)
FormulaContent(FormulaType _type, std::vector< carl::Variable > &&_vars, const Formula< Pol > &_term)
Constructs a quantifier expression: (exists (vars) term) or (forall (vars) term)
size_t mUsages
The number of formulas existing with this content.
FormulaContent(FormulaType _type, Formula< Pol > &&_subformula)
Constructs a formula of the given type with a single subformula.
FormulaContent(UEquality &&_ueq)
Constructs a formula being an uninterpreted equality.
std::variant< carl::Variable, Constraint< Pol >, VariableComparison< Pol >, VariableAssignment< Pol >, BVConstraint, UEquality, Formula< Pol >, Formulas< Pol >, QuantifierContent< Pol > > mContent
The content of this formula.
const FormulaContent< Pol > * mNegation
The negation.
Condition mProperties
The propositions of this formula.
FormulaContent(Constraint< Pol > &&_constraint)
Constructs a formula being a constraint.
FormulaContent(Variable _variable)
Constructs a formula being a variable.
FormulaContent(BVConstraint &&_constraint)
Constructs a formula being a bitvector constraint.
size_t mId
The unique id.
FormulaContent(VariableComparison< Pol > &&_variableComparison)
size_t mHash
The hash value.
Implements an uninterpreted equality, that is an equality of either two uninterpreted function instan...
Definition: UEquality.h:23