Equivalence, Validity and Normal Forms
Two formulas are equivalent () when they have the same truth value for every assignment of their variables. Equivalences let you simplify conditions in code, and they are the algebra behind circuit design. This lesson also introduces valid and satisfiable formulas and the two standard shapes of formulas, DNF and CNF.
Valid, satisfiable, equivalent
| Term | Meaning | Example |
|---|---|---|
| valid (a tautology) | true for every assignment | |
| satisfiable | true for at least one assignment | |
| unsatisfiable (a contradiction) | true for no assignment | |
| same truth value everywhere |
They are linked: is valid exactly when is unsatisfiable, and exactly when is valid. So one tool, a satisfiability checker, answers all three questions.
The key equivalences
| Law | Equivalence |
|---|---|
| double negation | |
| De Morgan | , |
| distributive | , |
| implication | |
| contrapositive | |
| absorption | , |
| identity, domination | , |
Each can be checked with a truth table. A brute-force equivalence checker:
from itertools import product
def equivalent(f, g, n):
return all(f(*v) == g(*v) for v in product([False, True], repeat=n))
print("De Morgan:", equivalent(lambda p, q: not (p and q), lambda p, q: (not p) or (not q), 2))
print("distributive:", equivalent(lambda p, q, r: p or (q and r), lambda p, q, r: (p or q) and (p or r), 3))
print("absorption:", equivalent(lambda p, q: p or (p and q), lambda p, q: p, 2))
print("(P->Q)->R vs P->(Q->R):", equivalent(lambda p, q, r: (not ((not p) or q)) or r,
lambda p, q, r: (not p) or ((not q) or r), 3))De Morgan: True
distributive: True
absorption: True
(P->Q)->R vs P->(Q->R): False
The last line shows that implication is not associative: you must bracket .
Simplifying with algebra
Equivalences can be applied like algebra rules. Simplify :
Normal forms
Every formula is equivalent to one in each of two standard shapes:
- Disjunctive normal form (DNF): an OR of ANDs of literals (a literal is a variable or its negation), e.g. .
- Conjunctive normal form (CNF): an AND of ORs of literals (clauses), e.g. .
A DNF can be read off a truth table: take one AND-term for each true row. For the true rows are (T, F) and (F, T), giving . A CNF comes from the false rows: each false row is ruled out by one clause. SymPy converts and simplifies:
from sympy import symbols, Xor
from sympy.logic.boolalg import to_dnf, to_cnf, simplify_logic
P, Q, R = symbols("P Q R")
print("DNF of P xor Q:", to_dnf(Xor(P, Q), simplify=True))
print("CNF of P xor Q:", to_cnf(Xor(P, Q), simplify=True))
print("simplify ~(P | ~Q) | Q:", simplify_logic(~(P | ~Q) | Q))
print("simplify (P & Q) | (P & ~Q) | (~P & R):", simplify_logic((P & Q) | (P & ~Q) | (~P & R)))DNF of P xor Q: (P & ~Q) | (Q & ~P)
CNF of P xor Q: (P | Q) & (~P | ~Q)
simplify ~(P | ~Q) | Q: Q
simplify (P & Q) | (P & ~Q) | (~P & R): P | R
The last formula simplifies to : if is true one of the first two terms holds, and if is false the formula reduces to .
In software
Refactoring tools and linters apply De Morgan to clean up conditions: if not (user and user.active) becomes if not user or not user.active. CNF is the input format of SAT solvers (next lesson); DNF is how a truth table becomes a circuit made of AND gates feeding an OR gate (Boolean Logic in Circuits and Code).
Exercises
Exercise 1: Valid, satisfiable or neither?
Classify: (a) ; (b) ; (c) .
Answer
(a) valid; (b) unsatisfiable; (c) satisfiable but not valid.
Solution
(a) If is true, is true; if is false, is true. So it is always true. (b) forces both false, and then is false. (c) True when is false; false when is true and false.
from itertools import product
forms = {
"a": lambda p, q: ((not p) or q) or ((not q) or p),
"b": lambda p, q: (p or q) and not p and not q,
"c": lambda p, q: (not p) or (p and q),
}
for k, f in forms.items():
vals = [f(p, q) for p, q in product([False, True], repeat=2)]
print(k, "valid" if all(vals) else "satisfiable" if any(vals) else "unsatisfiable")a valid
b unsatisfiable
c satisfiable
Exercise 2: Simplify by hand
Simplify using the laws.
Answer
.
Solution
Exercise 3: DNF from a table
A formula is true exactly when at least two of its inputs are true (the majority function). Write its DNF.
Answer
.
Solution
Reading off the four true rows gives . Each pair-term absorbs the rows where that pair is true, so the short form is equivalent; a truth table confirms it.
Test yourself
By De Morgan's law, is equivalent to…
is valid if and only if…
- is satisfiable
- is unsatisfiable
- is valid
- has a CNF
Which formula is in CNF?