Techniquecore
Unate recursive paradigm
Also called URP.
Solves an operation on a cover, such as tautology or complement, by Shannon splitting on binate variables until each part is unate and easy, then merging.
The unate recursive paradigm is a way to compute an operation on a cover, a list of cubes that stands for a sum of products, without building the truth table. Split the cover by the Shannon expansion on one variable, solve the operation on the two cofactors, and merge the two answers. Choose the variable so that the parts become unate fast, because on a unate cover the hard operations are easy: a unate cover is a tautology exactly when it holds the cube with no literal. Brayton, Hachtel, McMullen and Sangiovanni-Vincentelli (1984) built the operators of Espresso on this scheme: tautology, containment of a cube in a cover, complement, and others. The recursion is exponential in the worst case, since tautology of a cover is co-NP-complete, but on the covers met in logic design it stops early.
In plain words
You have a list of AND terms and want to know something about their OR, such as "is it always 1?". Pick one input and look at two cases: the input is 0, and the input is 1. In each case the list gets simpler, since some terms drop out and others lose a letter. Answer the question for each case, then put the two answers together.
Some lists are easy. If no input appears both plain and negated in the list, the OR is always 1 only when one term has no letters at all. So pick the input that most often appears both ways, and the easy case comes soon.
When it applies
- The function is given as a cover, and the truth table is too large to build.
- The operation can be computed from the two cofactors by a simple merge. Tautology is the AND of the two answers, and the complement is .
- The operation has a cheap answer on a unate cover, or on a cover with one cube, so the recursion can stop before the leaves are single points.
- The cover is not too far from unate. On a cover where every variable is binate in every part, the recursion is a full binary tree of depth .
The template
A cube is a string over , and a cover is a list of cubes. The cofactor of a cover by drops each cube with the literal of the other value at position , and sets position to in the cubes that are left. A cover is unate when no variable appears in it with both literals. The splitting rule takes the binate variable that is in the most cubes, then the one whose two literal counts are closest. On a unate cover it takes the variable in the most cubes.
The holes of the template are the leaf test, the splitting rule and the merge. Two instances fill them as follows.
TAUTOLOGY(F) ▷ is the OR of the cubes of F equal to 1?- 1
if F = ∅ then return false - 2
if F holds the cube - - ... - then return true - 3
if F is unate then return false - 4
if the minterm counts of the cubes of F add up to less than 2^n then return false - 5
x ← the splitting variable of F - 6
return TAUTOLOGY(F with x = 0) and TAUTOLOGY(F with x = 1) COMPLEMENT(F) ▷ a cover of the points where F is 0- 7
if F = ∅ then return { - - ... - } - 8
if F holds the cube - - ... - then return ∅ - 9
if F has one cube c then return one cube for each literal of c, with that literal negated - 10
x ← the splitting variable of F - 11
G0 ← COMPLEMENT(F with x = 0), G1 ← COMPLEMENT(F with x = 1) - 12
return the cubes in both G0 and G1 as they are, the other cubes of G0 with x̄, and the other cubes of G1 with x - 13
after removing each cube that lies inside another cube of the result
Why line L3 is right. Let be unate and without the cube of no literal. Build a point : if appears in only as , set ; if only as , set ; if not at all, set to any value. Every cube of has a literal, and makes that literal 0, so . So a unate cover is a tautology exactly when line L2 applies.
Why line L4 is right. A cube with dashes holds points. If the counts add up to less than , the cubes cannot hold all points, even with no overlap.
Why lines L6 and L12 are right. By the Shannon expansion, exactly when and , and . A cube that is in both and gives , so it needs no literal of . Line L13 only removes cubes inside other cubes, which does not change the function. Line L9 is De Morgan's law for one cube.
Why it stops. A cofactor by sets position to in every cube that is left. At line L5 the cover is binate, so appears in it. At line L10 the cover has at least two cubes and none is the cube of no literal, so the chosen appears in some cube. So each call reduces the number of positions that hold a literal in some cube, and the depth is at most .
Cost. Each call spends time on the cofactors, the counts and the tests. The recursion tree has at most leaves, and in the worst case it is that large: tautology of a cover is co-NP-complete. The leaf tests of lines L2 to L4 and L9 cut the tree, and the splitting rule is chosen to reach them early. The complement can also be exponential in its output alone: the cover has cubes, and its complement, the product , needs cubes in any sum of products.
Example. Take over . The variable is binate in two cubes, and is binate in all three, so line L5 picks . The cofactor by is , which is unate and has no universal cube, so line L3 returns false. So is not a tautology. Indeed, the point is 0 for all three cubes.
Classic examples
- Tautology and containment. A cube lies inside a cover exactly when the cofactor is a tautology. Espresso uses this test to find redundant cubes in its IRREDUNDANT step.
- Complement. Espresso computes the off-set of the function as the complement of the on-set plus the don't-care set, by the recursion of lines L7 to L13, and its EXPAND step checks each raise of a cube against it.
- Equivalence of two covers. Two covers and are equal when each cube of lies in and each cube of lies in . This is a small form of Boolean equivalence checking with tautology as the oracle.
- Building a BDD. A binary decision diagram is built by the same Shannon recursion, but it stores and shares the cofactors in place of throwing them away.
- Satisfiability of a product of sums. The dual question, whether a CNF has a satisfying point, is SAT. The complement of a cover is 0 everywhere exactly when the cover is a tautology, so DPLL search and the tautology recursion split on variables in the same way.
Variations
- Unate leaves for the complement. Brayton and coauthors give a separate method for the complement of a unate cover, in their section on unate complementation. The code of this page uses the general recursion down to one cube in place of it.
- Other operators. The same template computes the intersection of two covers, the cofactor by a cube, and the set of primes of a cover, each with its own leaf rule and merge.
- Component split. If the cubes of a cover fall into groups with no shared variable, the cover is a tautology exactly when one group is. Testing this before the split cuts the tree.
- Splitting rules. The rule can weigh the counts in other ways, or prefer variables in the largest cubes. All correct rules give the same answer, and they differ only in time.
- Multiple-valued variables. Espresso-MV applies the same recursion to variables with more than two values, where a split has one branch per value.
Pitfalls
- A unate function is not a unate cover. Line L3 tests the cover, not the function. The cover is a tautology, and its function, the constant 1, is unate, but no cube of the cover is the universal cube. A test of line L3 on the function would return false here, which is wrong. The rule holds for unate covers only.
- Splitting on an absent variable. If line L5 or line L10 picks a variable that appears in no cube, both cofactors equal and the recursion never stops.
- Losing the merge at line L12. Adding to every cube of and to every cube of is correct, but a cube in both halves then becomes the two cubes and . Neither lies inside the other, so line L13 does not remove them, and the result has more cubes and literals than needed.
- Counting minterms with overlap. Line L4 can only reject. A sum of or more does not mean a tautology, because cubes overlap.
- The don't-cares. A tautology test for a function with don't-cares must use the on-set plus the don't-care set as the cover, since the points of the don't-care set may be covered or not.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/unate-recursive-paradigm/. The authors are listed in prov/VCCME7.jsonl.
"""The unate recursive paradigm on covers of cubes: tautology, complement, containment.
A cube over n variables is a string over '0', '1' and '-'. A cover is
a list of cubes and stands for the OR of its cubes.
"""
Cube = str
Cover = list[Cube]
def universal(n: int) -> Cube:
return "-" * n
def cofactor(cover: Cover, i: int, b: str) -> Cover:
"""The cofactor of the cover by x_{i+1} = b, with b in '01'."""
out = []
for c in cover:
if c[i] == "-" or c[i] == b:
out.append(c[:i] + "-" + c[i + 1:])
return out
def cube_cofactor(cover: Cover, d: Cube) -> Cover:
"""The cofactor of the cover by the cube d (fix every literal of d)."""
out = []
for c in cover:
if any(x != "-" and y != "-" and x != y for x, y in zip(c, d)):
continue # c and d are disjoint
out.append("".join("-" if y != "-" else x for x, y in zip(c, d)))
return out
def column_counts(cover: Cover, n: int) -> list[tuple[int, int]]:
"""For each variable, the number of cubes with the literal x' and with the literal x."""
zeros = [0] * n
ones = [0] * n
for c in cover:
for i, ch in enumerate(c):
if ch == "0":
zeros[i] += 1
elif ch == "1":
ones[i] += 1
return list(zip(zeros, ones))
def is_unate(cover: Cover, n: int) -> bool:
"""True if no variable appears in both polarities in the cover."""
return all(z == 0 or o == 0 for z, o in column_counts(cover, n))
def splitting_variable(cover: Cover, n: int) -> int:
"""The most binate variable: in the most cubes, then the most balanced.
If the cover is unate, the variable in the most cubes. Returns -1 if
no variable appears.
"""
best, key = -1, None
for i, (z, o) in enumerate(column_counts(cover, n)):
if z + o == 0:
continue
k = (z > 0 and o > 0, z + o, -abs(z - o), -i)
if key is None or k > key:
best, key = i, k
return best
def tautology(cover: Cover, n: int) -> bool:
"""True if the cover is 1 on every point of {0,1}^n."""
if not cover: # @line L1
return False
if universal(n) in cover: # @line L2
return True
if is_unate(cover, n): # @line L3: a unate cover is a tautology only with the universal cube
return False
if sum(2 ** c.count("-") for c in cover) < 2 ** n: # @line L4: too few minterms
return False
i = splitting_variable(cover, n) # @line L5
return tautology(cofactor(cover, i, "0"), n) and tautology(cofactor(cover, i, "1"), n) # @line L6
def contains(cover: Cover, d: Cube) -> bool:
"""True if the cube d lies inside the cover."""
return tautology(cube_cofactor(cover, d), len(d))
def _single_cube_complement(c: Cube) -> Cover:
"""De Morgan: the complement of one cube, one cube per literal."""
n = len(c)
out = []
for i, ch in enumerate(c):
if ch != "-":
out.append("-" * i + ("1" if ch == "0" else "0") + "-" * (n - i - 1))
return out
def _drop_contained(cover: Cover) -> Cover:
"""Remove repeated cubes and cubes that lie in a single other cube."""
def inside(a: Cube, b: Cube) -> bool:
return all(y == "-" or x == y for x, y in zip(a, b))
out: Cover = []
for k, a in enumerate(cover):
if any(j != k and inside(a, b) and (a != b or j < k) for j, b in enumerate(cover)):
continue
out.append(a)
return out
def complement(cover: Cover, n: int) -> Cover:
"""A cover of the complement of the cover."""
if not cover: # @line L7
return [universal(n)]
if universal(n) in cover: # @line L8
return []
if len(cover) == 1: # @line L9
return _single_cube_complement(cover[0])
i = splitting_variable(cover, n) # @line L10
lo = complement(cofactor(cover, i, "0"), n) # @line L11
hi = complement(cofactor(cover, i, "1"), n)
both = set(lo) & set(hi) # @line L12: merge, a cube in both halves needs no literal
out = sorted(both)
out += [c[:i] + "0" + c[i + 1:] for c in lo if c not in both]
out += [c[:i] + "1" + c[i + 1:] for c in hi if c not in both]
return _drop_contained(out) # @line L13
test_unate_recursive_paradigm.py 91 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/unate-recursive-paradigm/. The authors are listed in prov/VCCME7.jsonl.
import itertools
import random
from unate_recursive_paradigm import (
cofactor,
complement,
contains,
is_unate,
splitting_variable,
tautology,
)
def points(cover: list[str], n: int) -> set[int]:
"""The truth table of a cover, as a set of minterms (x_1 is the high bit)."""
out = set()
for m in range(2 ** n):
bits = format(m, "b").zfill(n) if n else ""
if any(all(ch == "-" or ch == b for ch, b in zip(c, bits)) for c in cover):
out.add(m)
return out
def random_cover(rng: random.Random, n: int, k: int, p: float) -> list[str]:
return ["".join(rng.choice("01") if rng.random() < p else "-" for _ in range(n)) for _ in range(k)]
def test_small_cases() -> None:
assert tautology(["1-", "0-"], 2)
assert not tautology(["1-", "-1"], 2) # unate, no universal cube
assert tautology(["11", "0-", "-0"], 2)
assert not tautology([], 3)
assert tautology(["---"], 3)
assert complement([], 2) == ["--"]
assert complement(["--"], 2) == []
assert sorted(complement(["10-"], 3)) == ["-1-", "0--"]
def test_unate_rule() -> None:
assert is_unate(["1-0", "-10", "1--"], 3)
assert not is_unate(["1-", "0-"], 2)
assert cofactor(["1-0", "0-1", "--1"], 0, "1") == ["--0", "--1"]
# x3 is binate and in all three cubes; x1 is binate and in two.
assert splitting_variable(["1-0", "0-1", "--1"], 3) == 2
assert splitting_variable(["1--", "-1-"], 3) == 0 # unate: the first of the most used
def test_tautology_against_truth_table() -> None:
rng = random.Random(21)
for _ in range(1500):
n = rng.randint(1, 6)
cover = random_cover(rng, n, rng.randint(0, 12), rng.choice((0.3, 0.5, 0.7)))
assert tautology(cover, n) == (len(points(cover, n)) == 2 ** n)
def test_complement_against_truth_table() -> None:
rng = random.Random(22)
for _ in range(800):
n = rng.randint(1, 6)
cover = random_cover(rng, n, rng.randint(0, 10), rng.choice((0.3, 0.5, 0.7)))
comp = complement(cover, n)
assert points(comp, n) == set(range(2 ** n)) - points(cover, n)
assert len(set(comp)) == len(comp)
def test_complement_of_all_two_variable_covers() -> None:
cubes = ["".join(t) for t in itertools.product("01-", repeat=2)]
for k in range(4):
for cover in itertools.combinations(cubes, k):
comp = complement(list(cover), 2)
assert points(comp, 2) == set(range(4)) - points(list(cover), 2)
def test_contains_against_truth_table() -> None:
rng = random.Random(23)
for _ in range(800):
n = rng.randint(1, 6)
cover = random_cover(rng, n, rng.randint(0, 10), 0.5)
d = random_cover(rng, n, 1, 0.5)[0]
assert contains(cover, d) == (points([d], n) <= points(cover, n))
def test_exponential_complement() -> None:
# x1 y1 + x2 y2 + x3 y3 has a complement of 2^3 cubes, and none can be dropped.
cover = ["11----", "--11--", "----11"]
comp = complement(cover, 6)
assert len(comp) == 8
assert points(comp, 6) == set(range(64)) - points(cover, 6)
Exercises
- Prove that the complement of a unate cover is a unate function, with each variable in the opposite polarity.
- Show that the cover is a tautology by the recursion, and draw its recursion tree.
- Give a family of covers on which TAUTOLOGY, with the splitting rule of this page, builds a tree with leaves or more.
- Prove that the cube lies inside the cover exactly when the cofactor is a tautology.
- Write the merge for the intersection of two covers, and prove it correct by the Shannon expansion.
References
- Robert K. Brayton, Gary D. Hachtel, Curtis T. McMullen and Alberto L. Sangiovanni-Vincentelli, Logic Minimization Algorithms for VLSI Synthesis, Kluwer, 1984, Chapter 3, Decomposition and Unate Functions (Sections 3.1 Cofactors and Shannon Expansion, 3.3 Unate Functions, 3.4 Choice of Splitting Variable, 3.5 Unate Complementation), and Chapter 4, Sections 4.1 Complementation and 4.2 Tautology
- Giovanni De Micheli, Synthesis and Optimization of Digital Circuits, McGraw-Hill, 1994, Chapter 7, Two-Level Combinational Logic Optimization (the recursive operators on covers, tautology and complementation)
- Gary D. Hachtel and Fabio Somenzi, Logic Synthesis and Verification Algorithms, Kluwer, 1996, Chapter 5, Heuristic Minimization of Two-Level Circuits (the unate recursive paradigm and its operators)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag VCCME7 Edit this page History Report an error Markdown