Drake
Formula Class Reference

Represents a symbolic form of a first-order logic formula. More...

#include <drake/common/symbolic_formula.h>

Public Member Functions

 Formula ()
 Default constructor. More...
 
 Formula (bool value)
 Constructs from a bool. More...
 
 Formula (std::shared_ptr< const FormulaCell > ptr)
 
 Formula (const Variable &var)
 Constructs a formula from var. More...
 
FormulaKind get_kind () const
 
Variables GetFreeVariables () const
 Gets free variables (unquantified variables). More...
 
bool EqualTo (const Formula &f) const
 Checks structural equality. More...
 
bool Less (const Formula &f) const
 Checks lexicographical ordering between this and e. More...
 
bool Evaluate (const Environment &env=Environment{}) const
 Evaluates under a given environment (by default, an empty environment). More...
 
Formula Substitute (const Variable &var, const Expression &e) const
 Returns a copy of this formula replacing all occurrences of var with e. More...
 
Formula Substitute (const Substitution &s) const
 Returns a copy of this formula replacing all occurrences of the variables in s with corresponding expressions in s. More...
 
std::string to_string () const
 Returns string representation of Formula. More...
 
 operator bool () const
 Conversion to bool. More...
 
Implements CopyConstructible, CopyAssignable, MoveConstructible, MoveAssignable
 Formula (const Formula &)=default
 
Formulaoperator= (const Formula &)=default
 
 Formula (Formula &&)=default
 
Formulaoperator= (Formula &&)=default
 

Static Public Member Functions

static Formula True ()
 
static Formula False ()
 

Friends

template<class HashAlgorithm >
void hash_append (HashAlgorithm &hasher, const Formula &item) noexcept
 Implements the hash_append generic hashing concept. More...
 
std::ostream & operator<< (std::ostream &os, const Formula &f)
 
void swap (Formula &a, Formula &b)
 
bool is_false (const Formula &f)
 Checks if f is structurally equal to False formula. More...
 
bool is_true (const Formula &f)
 Checks if f is structurally equal to True formula. More...
 
bool is_variable (const Formula &f)
 Checks if f is a variable formula. More...
 
bool is_equal_to (const Formula &f)
 Checks if f is a formula representing equality (==). More...
 
bool is_not_equal_to (const Formula &f)
 Checks if f is a formula representing disequality (!=). More...
 
bool is_greater_than (const Formula &f)
 Checks if f is a formula representing greater-than (>). More...
 
bool is_greater_than_or_equal_to (const Formula &f)
 Checks if f is a formula representing greater-than-or-equal-to (>=). More...
 
bool is_less_than (const Formula &f)
 Checks if f is a formula representing less-than (<). More...
 
bool is_less_than_or_equal_to (const Formula &f)
 Checks if f is a formula representing less-than-or-equal-to (<=). More...
 
bool is_relational (const Formula &f)
 Checks if f is a relational formula ({==, !=, >, >=, <, <=}). More...
 
bool is_conjunction (const Formula &f)
 Checks if f is a conjunction (∧). More...
 
bool is_disjunction (const Formula &f)
 Checks if f is a disjunction (∨). More...
 
bool is_negation (const Formula &f)
 Checks if f is a negation (¬). More...
 
bool is_forall (const Formula &f)
 Checks if f is a Forall formula (∀). More...
 
bool is_isnan (const Formula &f)
 Checks if f is an isnan formula. More...
 
bool is_positive_semidefinite (const Formula &f)
 Checks if f is a positive-semidefinite formula. More...
 
std::shared_ptr< const FormulaFalseto_false (const Formula &f)
 Casts f to shared_ptr<const FormulaFalse>. More...
 
std::shared_ptr< const FormulaTrueto_true (const Formula &f)
 Casts f to shared_ptr<const FormulaTrue>. More...
 
std::shared_ptr< const FormulaVarto_variable (const Formula &f)
 Casts f to shared_ptr<const FormulaVar>. More...
 
std::shared_ptr< const RelationalFormulaCellto_relational (const Formula &f)
 Casts f to shared_ptr<const RelationalFormulaCell>. More...
 
std::shared_ptr< const FormulaEqto_equal_to (const Formula &f)
 Casts f to shared_ptr<const FormulaEq>. More...
 
std::shared_ptr< const FormulaNeqto_not_equal_to (const Formula &f)
 Casts f to shared_ptr<const FormulaNeq>. More...
 
std::shared_ptr< const FormulaGtto_greater_than (const Formula &f)
 Casts f to shared_ptr<const FormulaGt>. More...
 
std::shared_ptr< const FormulaGeqto_greater_than_or_equal_to (const Formula &f)
 Casts f to shared_ptr<const FormulaGeq>. More...
 
std::shared_ptr< const FormulaLtto_less_than (const Formula &f)
 Casts f to shared_ptr<const FormulaLt>. More...
 
std::shared_ptr< const FormulaLeqto_less_than_or_equal_to (const Formula &f)
 Casts f to shared_ptr<const FormulaLeq>. More...
 
std::shared_ptr< const NaryFormulaCellto_nary (const Formula &f)
 Casts f to shared_ptr<const NaryFormulaCell>. More...
 
std::shared_ptr< const FormulaAndto_conjunction (const Formula &f)
 Casts f to shared_ptr<const FormulaAnd>. More...
 
std::shared_ptr< const FormulaOrto_disjunction (const Formula &f)
 Casts f to shared_ptr<const FormulaOr>. More...
 
std::shared_ptr< const FormulaNotto_negation (const Formula &f)
 Casts f to shared_ptr<const FormulaNot>. More...
 
std::shared_ptr< const FormulaForallto_forall (const Formula &f)
 Casts f to shared_ptr<const FormulaForall>. More...
 
std::shared_ptr< const FormulaIsnanto_isnan (const Formula &f)
 Casts f to shared_ptr<const FormulaIsnan>. More...
 
std::shared_ptr< const FormulaPositiveSemidefiniteto_positive_semidefinite (const Formula &f)
 Casts f to shared_ptr<const FormulaPositiveSemidefinite>. More...
 

Detailed Description

Represents a symbolic form of a first-order logic formula.

It has the following grammar:

    F := ⊥ | ⊤ | Var | E = E | E ≠ E | E > E | E ≥ E | E < E | E ≤ E
       | E ∧ ... ∧ E | E ∨ ... ∨ E | ¬F | ∀ x₁, ..., xn. F

In the implementation, Formula is a simple wrapper including a shared pointer to FormulaCell class which is a super-class of different kinds of symbolic formulas (i.e. FormulaAnd, FormulaOr, FormulaEq). Note that it includes a shared pointer, not a unique pointer, to allow sharing sub-expressions.

Note
The sharing of sub-expressions is not yet implemented.

The following simple simplifications are implemented:

    E1 = E2        ->  True    (if E1 and E2 are structurally equal)
    E1 ≠ E2        ->  False   (if E1 and E2 are structurally equal)
    E1 > E2        ->  False   (if E1 and E2 are structurally equal)
    E1 ≥ E2        ->  True    (if E1 and E2 are structurally equal)
    E1 < E2        ->  False   (if E1 and E2 are structurally equal)
    E1 ≤ E2        ->  True    (if E1 and E2 are structurally equal)
    F1 ∧ F2        ->  False   (if either F1 or F2 is False)
    F1 ∨ F2        ->  True    (if either F1 or F2 is True)
    ¬(¬(F))        ->  F

We flatten nested conjunctions (or disjunctions) at the construction. A conjunction (resp. disjunction) takes a set of conjuncts (resp. disjuncts). Note that any duplicated conjunct/disjunct is removed. For example, both of f1 && (f2 && f1) and (f1 && f2) && f1 are flattened to f1 && f2 && f1 and simplified into f1 && f2. As a result, the two are identified as the same formula.

Note
Formula class has an explicit conversion operator to bool. It evaluates a symbolic formula under an empty environment. If a symbolic formula includes variables, the conversion operator throws an exception. This operator is only intended for third-party code doing things like (imag(SymbolicExpression(0)) == SymbolicExpression(0)) { ... }; that we found in Eigen3 codebase. In general, a user of this class should explicitly call Evaluate from within Drake for readability.

Constructor & Destructor Documentation

◆ Formula() [1/6]

Formula ( const Formula )
default

◆ Formula() [2/6]

Formula ( Formula &&  )
default

◆ Formula() [3/6]

Formula ( )
inline

Default constructor.

Sets the value to Formula::False, to be consistent with value-initialized bools.

◆ Formula() [4/6]

Formula ( bool  value)
inlineexplicit

Constructs from a bool.

This overload is also used by Eigen when EIGEN_INITIALIZE_MATRICES_BY_ZERO is enabled.

◆ Formula() [5/6]

Formula ( std::shared_ptr< const FormulaCell ptr)
explicit

◆ Formula() [6/6]

Formula ( const Variable var)
explicit

Constructs a formula from var.

Precondition
var is of BOOLEAN type and not a dummy variable.

Member Function Documentation

◆ EqualTo()

bool EqualTo ( const Formula f) const

Checks structural equality.

◆ Evaluate()

bool Evaluate ( const Environment env = Environment{}) const

Evaluates under a given environment (by default, an empty environment).

Exceptions
std::runtime_errorif a variable v is needed for an evaluation but not provided by env.

◆ False()

Formula False ( )
static

◆ get_kind()

FormulaKind get_kind ( ) const

◆ GetFreeVariables()

Variables GetFreeVariables ( ) const

Gets free variables (unquantified variables).

◆ Less()

bool Less ( const Formula f) const

Checks lexicographical ordering between this and e.

If the two formulas f1 and f2 have different kinds k1 and k2 respectively, f1.Less(f2) is equal to k1 < k2. If f1 and f2 are expressions of the same kind, we check the ordering between f1 and f2 by comparing their elements lexicographically.

For example, in case of And, let f1 and f2 be

f1 = f_1,1 ∧ ... ∧ f_1,n
f2 = f_2,1 ∧ ... ∧ f_2,m

f1.Less(f2) is true if there exists an index i (<= n, m) such that for all j < i, we have

¬(f_1_j.Less(f_2_j)) ∧ ¬(f_2_j.Less(f_1_j))

and f_1_i.Less(f_2_i) holds.

This function is used as a compare function in std::map<symbolic::Formula> and std::set<symbolic::Formula> via std::less<symbolic::Formula>.

◆ operator bool()

operator bool ( ) const
inlineexplicit

Conversion to bool.

◆ operator=() [1/2]

Formula& operator= ( Formula &&  )
default

◆ operator=() [2/2]

Formula& operator= ( const Formula )
default

◆ Substitute() [1/2]

Formula Substitute ( const Variable var,
const Expression e 
) const

Returns a copy of this formula replacing all occurrences of var with e.

Exceptions
std::runtime_errorif NaN is detected during substitution.

◆ Substitute() [2/2]

Formula Substitute ( const Substitution s) const

Returns a copy of this formula replacing all occurrences of the variables in s with corresponding expressions in s.

Note that the substitutions occur simultaneously. For example, (x / y > 0).Substitute({{x, y}, {y, x}}) gets (y / x > 0).

Exceptions
std::runtime_errorif NaN is detected during substitution.

◆ to_string()

string to_string ( ) const

Returns string representation of Formula.

◆ True()

Formula True ( )
static

Friends And Related Function Documentation

◆ hash_append

void hash_append ( HashAlgorithm &  hasher,
const Formula item 
)
friend

Implements the hash_append generic hashing concept.

◆ is_conjunction

bool is_conjunction ( const Formula f)
friend

Checks if f is a conjunction (∧).

◆ is_disjunction

bool is_disjunction ( const Formula f)
friend

Checks if f is a disjunction (∨).

◆ is_equal_to

bool is_equal_to ( const Formula f)
friend

Checks if f is a formula representing equality (==).

◆ is_false

bool is_false ( const Formula f)
friend

Checks if f is structurally equal to False formula.

◆ is_forall

bool is_forall ( const Formula f)
friend

Checks if f is a Forall formula (∀).

◆ is_greater_than

bool is_greater_than ( const Formula f)
friend

Checks if f is a formula representing greater-than (>).

◆ is_greater_than_or_equal_to

bool is_greater_than_or_equal_to ( const Formula f)
friend

Checks if f is a formula representing greater-than-or-equal-to (>=).

◆ is_isnan

bool is_isnan ( const Formula f)
friend

Checks if f is an isnan formula.

◆ is_less_than

bool is_less_than ( const Formula f)
friend

Checks if f is a formula representing less-than (<).

◆ is_less_than_or_equal_to

bool is_less_than_or_equal_to ( const Formula f)
friend

Checks if f is a formula representing less-than-or-equal-to (<=).

◆ is_negation

bool is_negation ( const Formula f)
friend

Checks if f is a negation (¬).

◆ is_not_equal_to

bool is_not_equal_to ( const Formula f)
friend

Checks if f is a formula representing disequality (!=).

◆ is_positive_semidefinite

bool is_positive_semidefinite ( const Formula f)
friend

Checks if f is a positive-semidefinite formula.

◆ is_relational

bool is_relational ( const Formula f)
friend

Checks if f is a relational formula ({==, !=, >, >=, <, <=}).

◆ is_true

bool is_true ( const Formula f)
friend

Checks if f is structurally equal to True formula.

◆ is_variable

bool is_variable ( const Formula f)
friend

Checks if f is a variable formula.

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Formula f 
)
friend

◆ swap

void swap ( Formula a,
Formula b 
)
friend

◆ to_conjunction

std::shared_ptr<const FormulaAnd> to_conjunction ( const Formula f)
friend

Casts f to shared_ptr<const FormulaAnd>.

Precondition
is_conjunction(f) is true.

◆ to_disjunction

std::shared_ptr<const FormulaOr> to_disjunction ( const Formula f)
friend

Casts f to shared_ptr<const FormulaOr>.

Precondition
is_disjunction(f) is true.

◆ to_equal_to

std::shared_ptr<const FormulaEq> to_equal_to ( const Formula f)
friend

Casts f to shared_ptr<const FormulaEq>.

Precondition
is_equal_to(f) is true.

◆ to_false

std::shared_ptr<const FormulaFalse> to_false ( const Formula f)
friend

Casts f to shared_ptr<const FormulaFalse>.

Precondition
is_false(f) is true.

◆ to_forall

std::shared_ptr<const FormulaForall> to_forall ( const Formula f)
friend

Casts f to shared_ptr<const FormulaForall>.

Precondition
is_forall(f) is true.

◆ to_greater_than

std::shared_ptr<const FormulaGt> to_greater_than ( const Formula f)
friend

Casts f to shared_ptr<const FormulaGt>.

Precondition
is_greater_than(f) is true.

◆ to_greater_than_or_equal_to

std::shared_ptr<const FormulaGeq> to_greater_than_or_equal_to ( const Formula f)
friend

Casts f to shared_ptr<const FormulaGeq>.

Precondition
is_greater_than_or_equal_to(f) is true.

◆ to_isnan

std::shared_ptr<const FormulaIsnan> to_isnan ( const Formula f)
friend

Casts f to shared_ptr<const FormulaIsnan>.

Precondition
is_isnan(f) is true.

◆ to_less_than

std::shared_ptr<const FormulaLt> to_less_than ( const Formula f)
friend

Casts f to shared_ptr<const FormulaLt>.

Precondition
is_less_than(f) is true.

◆ to_less_than_or_equal_to

std::shared_ptr<const FormulaLeq> to_less_than_or_equal_to ( const Formula f)
friend

Casts f to shared_ptr<const FormulaLeq>.

Precondition
is_less_than_or_equal_to(f) is true.

◆ to_nary

std::shared_ptr<const NaryFormulaCell> to_nary ( const Formula f)
friend

Casts f to shared_ptr<const NaryFormulaCell>.

Precondition
is_nary(f) is true.

◆ to_negation

std::shared_ptr<const FormulaNot> to_negation ( const Formula f)
friend

Casts f to shared_ptr<const FormulaNot>.

Precondition
is_negation(f) is true.

◆ to_not_equal_to

std::shared_ptr<const FormulaNeq> to_not_equal_to ( const Formula f)
friend

Casts f to shared_ptr<const FormulaNeq>.

Precondition
is_not_equal_to(f) is true.

◆ to_positive_semidefinite

std::shared_ptr<const FormulaPositiveSemidefinite> to_positive_semidefinite ( const Formula f)
friend

Casts f to shared_ptr<const FormulaPositiveSemidefinite>.

Precondition
is_positive_semidefinite(f) is true.

◆ to_relational

std::shared_ptr<const RelationalFormulaCell> to_relational ( const Formula f)
friend

Casts f to shared_ptr<const RelationalFormulaCell>.

Precondition
is_relational(f) is true.

◆ to_true

std::shared_ptr<const FormulaTrue> to_true ( const Formula f)
friend

Casts f to shared_ptr<const FormulaTrue>.

Precondition
is_true(f) is true.

◆ to_variable

std::shared_ptr<const FormulaVar> to_variable ( const Formula f)
friend

Casts f to shared_ptr<const FormulaVar>.

Precondition
is_variable(f) is true.

The documentation for this class was generated from the following files: