Enum boolean_expression::Expr[][src]

pub enum Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
{ Terminal(T), Const(bool), Not(Box<Expr<T>>), And(Box<Expr<T>>, Box<Expr<T>>), Or(Box<Expr<T>>, Box<Expr<T>>), }

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.

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.

Methods

impl<T> Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
[src]

Returns true if this Expr is a terminal.

Returns true if this Expr is a constant.

Returns true if this Expr is a NOT node.

Returns true if this Expr is an AND node.

Returns true if this Expr is an OR node.

Builds a NOT node around an argument, consuming the argument expression.

Builds an AND node around two arguments, consuming the argument expressions.

Builds an OR node around two arguments, consuming the argument expressions.

Evaluates the expression with a particular set of terminal assignments. If any terminals are not assigned, they default to false.

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().

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));

Map terminal values using the specified mapping function.

Trait Implementations

impl<T: Clone> Clone for Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T: Debug> Debug for Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
[src]

Formats the value using the given formatter. Read more

impl<T: PartialEq> PartialEq for Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<T: Eq> Eq for Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
[src]

impl<T: PartialOrd> PartialOrd for Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<T: Ord> Ord for Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl<T: Hash> Hash for Expr<T> where
    T: Clone + Debug + Eq + Ord + Hash
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

Auto Trait Implementations

impl<T> Send for Expr<T> where
    T: Send

impl<T> Sync for Expr<T> where
    T: Sync