dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
symbolic_formula.h
1
2#pragma once
3
4#include <functional>
5#include <ostream>
6#include <set>
7#include <string>
8#include <utility>
9
10#include "dlinear/symbolic/hash.h"
11#include "dlinear/symbolic/symbolic_environment.h"
12#include "dlinear/symbolic/symbolic_expression.h"
13#include "dlinear/symbolic/symbolic_variable.h"
14#include "dlinear/symbolic/symbolic_variables.h"
15
16namespace dlinear::drake {
17namespace symbolic {
18
20enum class FormulaKind {
21 False,
22 True,
23 Var,
24 Eq,
25 Neq,
26 Gt,
27 Geq,
28 Lt,
29 Leq,
30 And,
31 Or,
32 Not,
33 Forall,
34};
35
36// Total ordering between FormulaKinds
37bool operator<(FormulaKind k1, FormulaKind k2);
38
39class FormulaCell; // In symbolic/symbolic_formula_cell.h
40class FormulaFalse; // In symbolic/symbolic_formula_cell.h
41class FormulaTrue; // In symbolic/symbolic_formula_cell.h
42class FormulaVar; // In symbolic/symbolic_formula_cell.h
43class RelationalFormulaCell; // In symbolic/symbolic_formula_cell.h
44class FormulaEq; // In symbolic/symbolic_formula_cell.h
45class FormulaNeq; // In symbolic/symbolic_formula_cell.h
46class FormulaGt; // In symbolic/symbolic_formula_cell.h
47class FormulaGeq; // In symbolic/symbolic_formula_cell.h
48class FormulaLt; // In symbolic/symbolic_formula_cell.h
49class FormulaLeq; // In symbolic/symbolic_formula_cell.h
50class NaryFormulaCell; // In symbolic/symbolic_formula_cell.h
51class FormulaNot; // In symbolic/symbolic_formula_cell.h
52class FormulaAnd; // In symbolic/symbolic_formula_cell.h
53class FormulaOr; // In symbolic/symbolic_formula_cell.h
54class FormulaForall; // In symbolic/symbolic_formula_cell.h
55
100class Formula {
101 public:
104 Formula(const Formula &);
105 Formula &operator=(const Formula &);
106 Formula(Formula &&) noexcept;
107 Formula &operator=(Formula &&) noexcept;
108 ~Formula();
109
113 explicit Formula(const Variable &var);
114
115 FormulaKind get_kind() const;
116 size_t get_hash() const;
117
121
123 bool EqualTo(const Formula &f) const;
124
125 bool IsFlattened() const;
126
149 bool Less(const Formula &f) const;
150
161 bool Evaluate(const Environment &env = Environment{}) const;
162
167 Formula Substitute(const Variable &var, const Expression &e) const;
168
173 Formula Substitute(const Variable &var, const Formula &f) const;
174
185 Formula Substitute(const ExpressionSubstitution &expr_subst,
186 const FormulaSubstitution &formula_subst) const;
187
194 Formula Substitute(const ExpressionSubstitution &expr_subst) const;
195
203 Formula Substitute(const FormulaSubstitution &formula_subst) const;
204
206 std::string to_string() const;
207
208 std::string to_smt2_string() const;
209
210 static Formula True();
211 static Formula False();
212
214 explicit operator bool() const { return Evaluate(); }
215
216 friend std::ostream &operator<<(std::ostream &os, const Formula &f);
217 friend void swap(Formula &a, Formula &b) { std::swap(a.ptr_, b.ptr_); }
218
219 friend bool is_false(const Formula &f);
220 friend bool is_true(const Formula &f);
221 friend bool is_variable(const Formula &f);
222 friend bool is_equal_to(const Formula &f);
223 friend bool is_not_equal_to(const Formula &f);
224 friend bool is_greater_than(const Formula &f);
226 friend bool is_less_than(const Formula &f);
227 friend bool is_less_than_or_equal_to(const Formula &f);
228 friend bool is_relational(const Formula &f);
229 friend bool is_conjunction(const Formula &f);
230 friend bool is_disjunction(const Formula &f);
231 friend bool is_negation(const Formula &f);
232 friend bool is_forall(const Formula &f);
233
234 // Note that the following cast functions are only for low-level operations
235 // and not exposed to the user of symbolic_formula.h. These functions are
236 // declared in symbolic_formula_cell.h header.
237 friend const FormulaFalse *to_false(const Formula &f);
238 friend const FormulaTrue *to_true(const Formula &f);
239 friend const FormulaVar *to_variable(const Formula &f);
240 friend const RelationalFormulaCell *to_relational(const Formula &f);
241 friend const FormulaEq *to_equal_to(const Formula &f);
242 friend const FormulaNeq *to_not_equal_to(const Formula &f);
243 friend const FormulaGt *to_greater_than(const Formula &f);
244 friend const FormulaGeq *to_greater_than_or_equal_to(const Formula &f);
245 friend const FormulaLt *to_less_than(const Formula &f);
246 friend const FormulaLeq *to_less_than_or_equal_to(const Formula &f);
247 friend const NaryFormulaCell *to_nary(const Formula &f);
248 friend NaryFormulaCell *to_nary(Formula &f);
249 friend const FormulaAnd *to_conjunction(const Formula &f);
250 friend const FormulaOr *to_disjunction(const Formula &f);
251 friend const FormulaNot *to_negation(const Formula &f);
252 friend const FormulaForall *to_forall(const Formula &f);
253
254 // Returns f1 = f1 && f2.
255 static Formula make_conjunction(Formula &f1, const Formula &f2);
256 // Returns f1 = f1 || f2.
257 static Formula make_disjunction(Formula &f1, const Formula &f2);
258
259 friend FormulaCell;
260 friend Formula forall(const Variables &vars, const Formula &f);
261 friend Formula make_conjunction(const std::set<Formula> &formulas);
262 friend Formula make_disjunction(const std::set<Formula> &formulas);
263 friend Formula operator!(const Formula &f);
264 friend Formula operator==(const Expression &e1, const Expression &e2);
265 friend Formula operator!=(const Expression &e1, const Expression &e2);
266 friend Formula operator<(const Expression &e1, const Expression &e2);
267 friend Formula operator<=(const Expression &e1, const Expression &e2);
268 friend Formula operator>(const Expression &e1, const Expression &e2);
269 friend Formula operator>=(const Expression &e1, const Expression &e2);
270 Formula &operator&=(const Formula &f2);
271
274 bool include_ite() const;
275
276 private:
277 explicit Formula(FormulaCell *ptr);
278
279 FormulaCell *ptr_;
280};
281
283Formula forall(const Variables &vars, const Formula &f);
284
295Formula make_conjunction(const std::set<Formula> &formulas);
296Formula operator&&(const Formula &f1, const Formula &f2);
297Formula operator&&(const Formula &f1, Formula &&f2);
298Formula operator&&(Formula &&f1, const Formula &f2);
299Formula operator&&(Formula &&f1, Formula &&f2);
300
301Formula operator&&(const Variable &v, const Formula &f);
302Formula operator&&(const Variable &v, Formula &&f);
303Formula operator&&(const Formula &f, const Variable &v);
304Formula operator&&(Formula &&f, const Variable &v);
305Formula operator&&(const Variable &v1, const Variable &v2);
306
317Formula make_disjunction(const std::set<Formula> &formulas);
318Formula operator||(const Formula &f1, const Formula &f2);
319Formula operator||(const Formula &f1, Formula &&f2);
320Formula operator||(Formula &&f1, const Formula &f2);
321Formula operator||(Formula &&f1, Formula &&f2);
322
323Formula operator||(const Variable &v, const Formula &f);
324Formula operator||(const Variable &v, Formula &&f);
325Formula operator||(const Formula &f, const Variable &v);
326Formula operator||(Formula &&f, const Variable &v);
327Formula operator||(const Variable &v1, const Variable &v2);
328
329Formula operator!(const Formula &f);
330Formula operator!(const Variable &v);
331
337Formula operator==(const Variable &v1, const Variable &v2);
338
340Formula operator==(const Expression &e1, const Expression &e2);
341
343Formula operator==(const Formula &f1, const Formula &f2);
344
346Formula operator==(const Variable &v, const Formula &f);
347
349Formula operator==(const Formula &f, const Variable &v);
350
356Formula operator!=(const Variable &v1, const Variable &v2);
357
359Formula operator!=(const Expression &e1, const Expression &e2);
360
362Formula operator!=(const Formula &f1, const Formula &f2);
363
365Formula operator!=(const Variable &v, const Formula &f);
366
368Formula operator!=(const Formula &f, const Variable &v);
369
370Formula operator<(const Expression &e1, const Expression &e2);
371Formula operator<=(const Expression &e1, const Expression &e2);
372Formula operator>(const Expression &e1, const Expression &e2);
373Formula operator>=(const Expression &e1, const Expression &e2);
374
375std::ostream &operator<<(std::ostream &os, const Formula &f);
376
378bool is_false(const Formula &f);
380bool is_true(const Formula &f);
382bool is_variable(const Formula &f);
384bool is_equal_to(const Formula &f);
386bool is_not_equal_to(const Formula &f);
388bool is_greater_than(const Formula &f);
390bool is_greater_than_or_equal_to(const Formula &f);
392bool is_less_than(const Formula &f);
394bool is_less_than_or_equal_to(const Formula &f);
396bool is_relational(const Formula &f);
398bool is_conjunction(const Formula &f);
400bool is_disjunction(const Formula &f);
402bool is_nary(const Formula &f);
404bool is_negation(const Formula &f);
406bool is_forall(const Formula &f);
407
411const Variable &get_variable(const Formula &f);
412
416const Expression &get_lhs_expression(const Formula &f);
417
421const Expression &get_rhs_expression(const Formula &f);
422
426const std::set<Formula> &get_operands(const Formula &f);
427
431const Formula &get_operand(const Formula &f);
432
436const Variables &get_quantified_variables(const Formula &f);
437
441const Formula &get_quantified_formula(const Formula &f);
442
443namespace detail {
446inline Formula logic_and(const Formula &f1, const Formula &f2) {
447 return f1 && f2;
448}
449} // namespace detail
450} // namespace symbolic
451
452} // namespace dlinear::drake
453
454
455namespace std {
456/* Provides std::less<dlinear::drake::symbolic::Formula>. */
457template<>
458struct less<dlinear::drake::symbolic::Formula> {
459 bool operator()(const dlinear::drake::symbolic::Formula &lhs,
460 const dlinear::drake::symbolic::Formula &rhs) const {
461 return lhs.Less(rhs);
462 }
463};
464
465/* Provides std::equal_to<dlinear::drake::symbolic::Formula>. */
466template<>
467struct equal_to<dlinear::drake::symbolic::Formula> {
468 bool operator()(const dlinear::drake::symbolic::Formula &lhs,
469 const dlinear::drake::symbolic::Formula &rhs) const {
470 return lhs.EqualTo(rhs);
471 }
472};
473
474template<>
475struct hash<dlinear::drake::symbolic::Formula> {
476 size_t operator()(const dlinear::drake::symbolic::Formula &f) const {
477 return f.get_hash();
478 }
479};
480
481} // namespace std
Represents a symbolic environment (mapping from a variable to a value).
Represents a symbolic form of an expression.
Represents a symbolic form of a first-order logic formula.
Formula Substitute(const Variable &var, const Expression &e) const
Returns a copy of this formula replacing all occurrences of var with e.
friend Formula make_disjunction(const std::set< Formula > &formulas)
Returns a disjunction of formulas.
friend bool is_forall(const Formula &f)
Checks if f is a Forall formula (∀).
friend bool is_greater_than(const Formula &f)
Checks if f is a formula representing greater-than (>).
bool Evaluate(const Environment &env=Environment{}) const
Evaluates under a given environment (by default, an empty environment).
bool EqualTo(const Formula &f) const
Checks structural equality.
friend bool is_disjunction(const Formula &f)
Checks if f is a disjunction (∨).
Formula Substitute(const FormulaSubstitution &formula_subst) const
Returns a copy of this formula replacing all occurrences of the variables in formula_subst with corre...
const Variables & GetFreeVariables() const
Gets free variables (unquantified variables).
friend bool is_conjunction(const Formula &f)
Checks if f is a conjunction (∧).
friend bool is_not_equal_to(const Formula &f)
Checks if f is a formula representing disequality (!=).
friend Formula forall(const Variables &vars, const Formula &f)
Returns a formula f, universally quantified by variables vars.
bool Less(const Formula &f) const
Checks lexicographical ordering between this and e.
Formula()
Default constructor.
friend bool is_less_than(const Formula &f)
Checks if f is a formula representing less-than (<).
Formula Substitute(const Variable &var, const Formula &f) const
Returns a copy of this formula replacing all occurrences of var with f.
friend bool is_equal_to(const Formula &f)
Checks if f is a formula representing equality (==).
std::string to_string() const
Returns string representation of Formula.
friend bool is_greater_than_or_equal_to(const Formula &f)
Checks if f is a formula representing greater-than-or-equal-to (>=).
friend bool is_relational(const Formula &f)
Checks if f is a relational formula ({==, !=, >, >=, <, <=}).
friend bool is_true(const Formula &f)
Checks if f is structurally equal to True formula.
Formula Substitute(const ExpressionSubstitution &expr_subst, const FormulaSubstitution &formula_subst) const
Returns a copy of this formula replacing all occurrences of the variables in expr_subst with correspo...
friend Formula operator!=(const Expression &e1, const Expression &e2)
Returns a formula representing e1 ≠ e2.
bool include_ite() const
Returns true if this symbolic formula includes an ITE (If-Then-Else) expression.
friend Formula make_conjunction(const std::set< Formula > &formulas)
Returns a conjunction of formulas.
friend Formula operator==(const Expression &e1, const Expression &e2)
Returns a formula representing (e1 = e2).
friend bool is_false(const Formula &f)
Checks if f is structurally equal to False formula.
friend bool is_variable(const Formula &f)
Checks if f is a variable formula.
friend bool is_less_than_or_equal_to(const Formula &f)
Checks if f is a formula representing less-than-or-equal-to (<=).
friend bool is_negation(const Formula &f)
Checks if f is a negation (¬).
Formula Substitute(const ExpressionSubstitution &expr_subst) const
Returns a copy of this formula replacing all occurrences of the variables in expr_subst with correspo...
Represents a symbolic variable.
Represents a set of variables.
Global namespace for the dlinear library.
Formula make_conjunction(const std::vector< Formula > &formulas)
Make conjunction of formulas.
Definition symbolic.cpp:485
LpColBound operator!(LpColBound bound)
Invert the bound with delta == 0.
Formula make_disjunction(const std::vector< Formula > &formulas)
Make disjunction of formulas.
Definition symbolic.cpp:493
STL namespace.