TExcelTAD
Home › Discrete Mathematics › Equivalence, Validity and Normal Forms

Equivalence, Validity and Normal Forms

Two formulas are equivalent (F≡GF \equiv G) 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 P∨¬PP \lor \neg P
satisfiable true for at least one assignment P∧¬QP \land \neg Q
unsatisfiable (a contradiction) true for no assignment P∧¬PP \land \neg P
F≡GF \equiv G same truth value everywhere P→Q≡¬P∨QP \to Q \equiv \neg P \lor Q

They are linked: FF is valid exactly when ¬F\neg F is unsatisfiable, and F≡GF \equiv G exactly when F↔GF \leftrightarrow G is valid. So one tool, a satisfiability checker, answers all three questions.

The key equivalences

Law Equivalence
double negation ¬¬P≡P\neg\neg P \equiv P
De Morgan ¬(P∧Q)≡¬P∨¬Q\neg(P \land Q) \equiv \neg P \lor \neg Q, ¬(P∨Q)≡¬P∧¬Q\quad \neg(P \lor Q) \equiv \neg P \land \neg Q
distributive P∧(Q∨R)≡(P∧Q)∨(P∧R)P \land (Q \lor R) \equiv (P \land Q) \lor (P \land R), P∨(Q∧R)≡(P∨Q)∧(P∨R)\quad P \lor (Q \land R) \equiv (P \lor Q) \land (P \lor R)
implication P→Q≡¬P∨QP \to Q \equiv \neg P \lor Q
contrapositive P→Q≡¬Q→¬PP \to Q \equiv \neg Q \to \neg P
absorption P∨(P∧Q)≡PP \lor (P \land Q) \equiv P, P∧(P∨Q)≡P\quad P \land (P \lor Q) \equiv P
identity, domination P∧T≡PP \land T \equiv P, P∨T≡T\quad P \lor T \equiv T

Each can be checked with a truth table. A brute-force equivalence checker:

python · try it yourself
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 P→Q→RP \to Q \to R.

Simplifying with algebra

Equivalences can be applied like algebra rules. Simplify ¬(P∨¬Q)∨Q\neg(P \lor \neg Q) \lor Q:

¬(P∨¬Q)∨Q≡(¬P∧¬¬Q)∨QDe Morgan≡(¬P∧Q)∨Qdouble negation≡Qabsorption\begin{aligned} \neg(P \lor \neg Q) \lor Q &\equiv (\neg P \land \neg\neg Q) \lor Q && \text{De Morgan} \\ &\equiv (\neg P \land Q) \lor Q && \text{double negation} \\ &\equiv Q && \text{absorption} \end{aligned}

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. (P∧¬Q)∨(¬P∧Q)(P \land \neg Q) \lor (\neg P \land Q).
  • Conjunctive normal form (CNF): an AND of ORs of literals (clauses), e.g. (P∨Q)∧(¬P∨¬Q)(P \lor Q) \land (\neg P \lor \neg Q).

A DNF can be read off a truth table: take one AND-term for each true row. For P⊕QP \oplus Q the true rows are (T, F) and (F, T), giving (P∧¬Q)∨(¬P∧Q)(P \land \neg Q) \lor (\neg P \land Q). A CNF comes from the false rows: each false row is ruled out by one clause. SymPy converts and simplifies:

python · try it yourself
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 P∨RP \lor R: if PP is true one of the first two terms holds, and if PP is false the formula reduces to RR.

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) (P→Q)∨(Q→P)(P \to Q) \lor (Q \to P); (b) (P∨Q)∧¬P∧¬Q(P \lor Q) \land \neg P \land \neg Q; (c) P→(P∧Q)P \to (P \land Q).

Answer

(a) valid; (b) unsatisfiable; (c) satisfiable but not valid.

Solution

(a) If QQ is true, P→QP \to Q is true; if QQ is false, Q→PQ \to P is true. So it is always true. (b) ¬P∧¬Q\neg P \land \neg Q forces both false, and then P∨QP \lor Q is false. (c) True when PP is false; false when PP is true and QQ false.

python · try it yourself
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 ¬(¬P∧Q)∧Q\neg(\neg P \land Q) \land Q using the laws.

Answer

P∧QP \land Q.

Solution
¬(¬P∧Q)∧Q≡(P∨¬Q)∧QDe Morgan, double negation≡(P∧Q)∨(¬Q∧Q)distributive≡(P∧Q)∨F≡P∧Q\begin{aligned} \neg(\neg P \land Q) \land Q &\equiv (P \lor \neg Q) \land Q && \text{De Morgan, double negation} \\ &\equiv (P \land Q) \lor (\neg Q \land Q) && \text{distributive} \\ &\equiv (P \land Q) \lor F \equiv P \land Q \end{aligned}

Exercise 3: DNF from a table

A formula M(A,B,C)M(A, B, C) is true exactly when at least two of its inputs are true (the majority function). Write its DNF.

Answer

(A∧B)∨(A∧C)∨(B∧C)(A \land B) \lor (A \land C) \lor (B \land C).

Solution

Reading off the four true rows gives (A∧B∧C)∨(A∧B∧¬C)∨(A∧¬B∧C)∨(¬A∧B∧C)(A \land B \land C) \lor (A \land B \land \neg C) \lor (A \land \neg B \land C) \lor (\neg A \land B \land C). Each pair-term absorbs the rows where that pair is true, so the short form (A∧B)∨(A∧C)∨(B∧C)(A \land B) \lor (A \land C) \lor (B \land C) is equivalent; a truth table confirms it.

Test yourself

By De Morgan's law, ¬(P∨Q)\neg(P \lor Q) is equivalent to…

  1. ¬P∨¬Q\neg P \lor \neg Q
  2. ¬P∧¬Q\neg P \land \neg Q
  3. P∧QP \land Q
  4. ¬P→Q\neg P \to Q

FF is valid if and only if…

  1. FF is satisfiable
  2. ¬F\neg F is unsatisfiable
  3. ¬F\neg F is valid
  4. FF has a CNF

Which formula is in CNF?

  1. (P∧Q)∨R(P \land Q) \lor R
  2. (P∨¬Q)∧(Q∨R)(P \lor \neg Q) \land (Q \lor R)
  3. P→QP \to Q
  4. ¬(P∧Q)\neg(P \land Q)