[−][src]Enum boolean_expression::Expr
An Expr
is a simple Boolean logic expression. It may contain terminals
(i.e., free variables), constants, and the following fundamental operations:
AND, OR, NOT.
Variants
A terminal (free variable). This expression node represents a value that is not known until evaluation time.
Const(bool)
A boolean constant: true or false.
The logical complement of the contained expression argument.
The logical AND of the two expression arguments.
The logical OR of the two expression arguments.
Implementations
impl<T> Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
pub fn is_terminal(&self) -> bool
[src]
Returns true
if this Expr
is a terminal.
pub fn is_const(&self) -> bool
[src]
Returns true
if this Expr
is a constant.
pub fn is_not(&self) -> bool
[src]
Returns true
if this Expr
is a NOT node.
pub fn is_and(&self) -> bool
[src]
Returns true
if this Expr
is an AND node.
pub fn is_or(&self) -> bool
[src]
Returns true
if this Expr
is an OR node.
pub fn not(e: Expr<T>) -> Expr<T>
[src]
Builds a NOT node around an argument, consuming the argument expression.
pub fn and(e1: Expr<T>, e2: Expr<T>) -> Expr<T>
[src]
Builds an AND node around two arguments, consuming the argument expressions.
pub fn or(e1: Expr<T>, e2: Expr<T>) -> Expr<T>
[src]
Builds an OR node around two arguments, consuming the argument expressions.
pub fn evaluate(&self, vals: &HashMap<T, bool>) -> bool
[src]
Evaluates the expression with a particular set of terminal assignments.
If any terminals are not assigned, they default to false
.
pub fn simplify_via_laws(self) -> Expr<T>
[src]
Simplify an expression in a relatively cheap way using well-known logic identities.
This function performs certain reductions using DeMorgan's Law,
distribution of ANDs over ORs, and certain identities involving
constants, to simplify an expression. The result will always be in
sum-of-products form: nodes will always appear in order (from
expression tree root to leaves) OR -> AND -> NOT
. In other words,
AND
nodes may only have NOT
nodes (or terminals or constants) as
children, and NOT
nodes may only have terminals or constants as
children.
This function explicitly does not perform any sort of minterm reduction.
The terms may thus be redundant (i.e., And(a, b)
may appear twice), and
combinable terms may exist (i.e., And(a, b)
and And(a, Not(b))
may
appear in the OR
'd list of terms, where these could be combined to
simply a
but are not). For example:
use boolean_expression::Expr; // This simplifies using DeMorgan's Law: let expr = Expr::not(Expr::or(Expr::Terminal(0), Expr::Terminal(1))); let simplified = expr.simplify_via_laws(); assert_eq!(simplified, Expr::and(Expr::not(Expr::Terminal(0)), Expr::not(Expr::Terminal(1)))); // This doesn't simplify, though: let expr = Expr::or( Expr::and(Expr::Terminal(0), Expr::Terminal(1)), Expr::and(Expr::Terminal(0), Expr::not(Expr::Terminal(1)))); let simplified = expr.clone().simplify_via_laws(); assert_eq!(simplified, expr);
For better (but more expensive) simplification, see simplify_via_bdd()
.
pub fn simplify_via_bdd(self) -> Expr<T>
[src]
Simplify an expression via a roundtrip through a BDD
. This procedure
is more effective than Expr::simplify_via_laws()
, but more expensive.
This roundtrip will implicitly simplify an arbitrarily complicated
function (by construction, as the BDD is built), and then find a
quasi-minimal set of terms using cubelist-based reduction. For example:
use boolean_expression::Expr; // `simplify_via_laws()` cannot combine these terms, but // `simplify_via_bdd()` will: let expr = Expr::or( Expr::and(Expr::Terminal(0), Expr::Terminal(1)), Expr::and(Expr::Terminal(0), Expr::not(Expr::Terminal(1)))); let simplified = expr.simplify_via_bdd(); assert_eq!(simplified, Expr::Terminal(0));
pub fn map<F, R>(&self, f: F) -> Expr<R> where
F: Fn(&T) -> R,
R: Clone + Debug + Eq + Hash,
[src]
F: Fn(&T) -> R,
R: Clone + Debug + Eq + Hash,
Map terminal values using the specified mapping function.
Trait Implementations
impl<T> BitAnd<Expr<T>> for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
type Output = Self
The resulting type after applying the &
operator.
fn bitand(self, rhs: Expr<T>) -> Self::Output
[src]
impl<T> BitAndAssign<Expr<T>> for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
fn bitand_assign(&mut self, rhs: Expr<T>)
[src]
impl<T> BitOr<Expr<T>> for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
type Output = Self
The resulting type after applying the |
operator.
fn bitor(self, rhs: Expr<T>) -> Self::Output
[src]
impl<T> BitOrAssign<Expr<T>> for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
fn bitor_assign(&mut self, rhs: Expr<T>)
[src]
impl<T: Clone> Clone for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
impl<T: Debug> Debug for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
impl<T: Eq> Eq for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
impl<T: Hash> Hash for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T> Not for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
type Output = Self
The resulting type after applying the !
operator.
fn not(self) -> Self::Output
[src]
impl<T: PartialEq> PartialEq<Expr<T>> for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
impl<T> StructuralEq for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
impl<T> StructuralPartialEq for Expr<T> where
T: Clone + Debug + Eq + Hash,
[src]
T: Clone + Debug + Eq + Hash,
Auto Trait Implementations
impl<T> RefUnwindSafe for Expr<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for Expr<T> where
T: Send,
T: Send,
impl<T> Sync for Expr<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Expr<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for Expr<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,