pyeda.boolalg.expr — Expressions

The pyeda.boolalg.expr module implements Boolean functions represented as expressions.

Interface Functions:

Interface Classes:

Interface Functions

pyeda.boolalg.expr.exprvar(name, index=None)[source]

Return a unique Expression variable.

A Boolean variable is an abstract numerical quantity that may assume any value in the set \(B = \{0, 1\}\). The exprvar function returns a unique Boolean variable instance represented by an expression. Variable instances may be used to symbolically construct larger expressions.

A variable is defined by one or more names, and zero or more indices. Multiple names establish hierarchical namespaces, and multiple indices group several related variables. If the name parameter is a single str, it will be converted to (name, ). The index parameter is optional; when empty, it will be converted to an empty tuple (). If the index parameter is a single int, it will be converted to (index, ).

Given identical names and indices, the exprvar function will always return the same variable:

>>> exprvar('a', 0) is exprvar('a', 0)

To create several single-letter variables:

>>> a, b, c, d = map(exprvar, "abcd")

To create variables with multiple names (inner-most first):

>>> fifo_push = exprvar(('push', 'fifo'))
>>> fifo_pop = exprvar(('pop', 'fifo'))

See also

For creating arrays of variables with incremental indices, we recommend using the pyeda.boolalg.bfarray.exprvars() function.

pyeda.boolalg.expr.expr(obj, simplify=True)[source]

Convert an arbitrary object into an Expression.


Convert an abstract syntax tree to an Expression.


Convert an expression into an equivalent DIMACS CNF.


Convert an untyped point to an Expression point.

Logic Factory Functions

Primary Operators

pyeda.boolalg.expr.Not(arg, simplify=True)[source]

Factory function for Boolean NOT expression.

pyeda.boolalg.expr.Or(*args, simplify=True)[source]

Factory function for Boolean OR expression.

pyeda.boolalg.expr.And(*args, simplify=True)[source]

Factory function for Boolean AND expression.

Secondary Operators

pyeda.boolalg.expr.Nor(*args, simplify=True)[source]

Alias for Not(Or(...))

pyeda.boolalg.expr.Nand(*args, simplify=True)[source]

Alias for Not(And(...))

pyeda.boolalg.expr.Xor(*args, simplify=True)[source]

Factory function for Boolean XOR expression.

pyeda.boolalg.expr.Xnor(*args, simplify=True)[source]

Factory function for Boolean XNOR expression.

pyeda.boolalg.expr.Equal(*args, simplify=True)[source]

Factory function for Boolean EQUAL expression.

pyeda.boolalg.expr.Unequal(*args, simplify=True)[source]

Factory function for Boolean UNEQUAL expression.

pyeda.boolalg.expr.Implies(p, q, simplify=True)[source]

Factory function for Boolean implication expression.

pyeda.boolalg.expr.ITE(s, d1, d0, simplify=True)[source]

Factory function for Boolean If-Then-Else expression.

High Order Operators

pyeda.boolalg.expr.OneHot0(*args, simplify=True, conj=True)[source]
Return an expression that means:
At most one input variable is true.
pyeda.boolalg.expr.OneHot(*args, simplify=True, conj=True)[source]
Return an expression that means:
Exactly one input variable is true.
pyeda.boolalg.expr.Majority(*args, simplify=True, conj=False)[source]
Return an expression that means:
The majority of the input variables are true.
pyeda.boolalg.expr.AchillesHeel(*args, simplify=True)[source]

Return the Achille’s Heel function, defined as \(\prod_{i=0}^{n/2-1}{X_{2i} + X_{2i+1}}\).

pyeda.boolalg.expr.Mux(fs, sel, simplify=True)[source]

Return an expression that multiplexes a sequence of input functions over a sequence of select functions.

Interface Classes

Expression Tree Nodes

class pyeda.boolalg.expr.Expression[source]

Boolean function represented by a logic expression


Return a string representation using Unicode symbols.


Return a string representation using Latex.


Return an inverted expression.


Return a simplified expression.


Return True if the expression is simplified.


Return a factored expression.

A factored expression is one and only one of the following:

  • A literal.
  • A disjunction / conjunction of factored expressions.


conj : bool
Always choose a conjunctive form when there’s a choice

Return the depth of the expression tree.

Expression depth is defined recursively:

  1. A leaf node (constant or literal) has zero depth.
  2. A branch node (operator) has depth equal to the maximum depth of its children (arguments) plus one.

Return the expression converted to an abstract syntax tree.

expand(vs=None, conj=False)[source]

Return the Shannon expansion with respect to a list of variables.


Return a factored, flattened expression.

Use the distributive law to flatten all nested expressions:

\[a + (b \cdot c) = (a + b) \cdot (a + c)\]\[a \cdot (b + c) = (a \cdot b) + (a \cdot c)\]
op : ExprOr or ExprAnd
The operator you want to flatten. For example, if you want to produce an ExprOr expression, flatten all the nested ExprAnds.

Return the expression in disjunctive normal form.


Return the expression in canonical disjunctive normal form.


Return the expression in conjunctive normal form.


Return the expression in canonical conjunctive normal form.


Return the DNF expression as a cover of cubes.


Return the DNF/CNF expression after absorption.

\[a + (a \cdot b) = a\]\[a \cdot (a + b) = a\]

Reduce the DNF/CNF expression to a canonical form.


Return a compact encoding for input variables.


Encode as a compact DNF.


Encode as a compact CNF.


Convert the expression to Tseitin’s encoding.


Return a DNF that contains all prime implicants.


Return True if this expression is equivalent to other.


Convert to DOT language representation.

class pyeda.boolalg.expr.ExprConstant[source]

Expression constant

class pyeda.boolalg.expr.ExprLiteral[source]

An instance of a variable or of its complement

class pyeda.boolalg.expr.ExprVariable(bvar)[source]

Expression variable

class pyeda.boolalg.expr.ExprComplement(exprvar)[source]

Expression complement

class pyeda.boolalg.expr.ExprNot(arg)[source]

Expression NOT operator

class pyeda.boolalg.expr.ExprOrAnd(*args)[source]

Base class for Expression OR/AND expressions

class pyeda.boolalg.expr.ExprOr(*args)[source]

Expression OR operator

class pyeda.boolalg.expr.ExprAnd(*args)[source]

Expression AND operator

class pyeda.boolalg.expr.ExprNorNand(*args)[source]

Base class for Expression NOR/NAND expressions

class pyeda.boolalg.expr.ExprNor(*args)[source]

Expression NOR operator

class pyeda.boolalg.expr.ExprNand(*args)[source]

Expression NAND operator

class pyeda.boolalg.expr.ExprExclusive(*args)[source]

Expression exclusive (XOR, XNOR) operator

class pyeda.boolalg.expr.ExprXor(*args)[source]

Expression Exclusive OR (XOR) operator

class pyeda.boolalg.expr.ExprXnor(*args)[source]

Expression Exclusive NOR (XNOR) operator

class pyeda.boolalg.expr.ExprEqualBase(*args)[source]

Expression equality (EQUAL, UNEQUAL) operators

class pyeda.boolalg.expr.ExprEqual(*args)[source]

Expression EQUAL operator

class pyeda.boolalg.expr.ExprUnequal(*args)[source]

Expression UNEQUAL operator

class pyeda.boolalg.expr.ExprImplies(p, q)[source]

Expression implication operator

class pyeda.boolalg.expr.ExprITE(s, d1, d0)[source]

Expression if-then-else ternary operator

Normal Forms

class pyeda.boolalg.expr.NormalForm(nvars, clauses)[source]

Normal form expression

class pyeda.boolalg.expr.DisjNormalForm(nvars, clauses)[source]

Disjunctive normal form expression

class pyeda.boolalg.expr.ConjNormalForm(nvars, clauses)[source]

Conjunctive normal form expression

class pyeda.boolalg.expr.DimacsCNF(nvars, clauses)[source]

Wrapper class for a DIMACS CNF representation