Bytebook Compiler

Logic & Proofs

Logical Equivalences & Normal Forms

When two formulas mean the same thing: the laws of logic including De Morgan's, simplifying expressions step by step, and converting to disjunctive and conjunctive normal form.

4 min read1 runnable exampleInteractive4-question quiz

Two formulas are logically equivalent, written P ≡ Q, if they have the same truth value in every row of the truth table — equivalently, if P ↔ Q is a tautology.

The laws

Law Equivalence
Identity p ∧ T ≡ p, p ∨ F ≡ p
Domination p ∨ T ≡ T, p ∧ F ≡ F
Idempotent p ∨ p ≡ p, p ∧ p ≡ p
Double negation ¬(¬p) ≡ p
Commutative p ∨ q ≡ q ∨ p, p ∧ q ≡ q ∧ p
Associative (p ∨ q) ∨ r ≡ p ∨ (q ∨ r)
Distributive p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r); p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
De Morgan's ¬(p ∧ q) ≡ ¬p ∨ ¬q; ¬(p ∨ q) ≡ ¬p ∧ ¬q
Absorption p ∨ (p ∧ q) ≡ p; p ∧ (p ∨ q) ≡ p
Negation p ∨ ¬p ≡ T, p ∧ ¬p ≡ F

Equivalences involving implication:

Equivalence
p → q ≡ ¬p ∨ q implication as OR
p → q ≡ ¬q → ¬p contrapositive
¬(p → q) ≡ p ∧ ¬q negating an implication
p ↔ q ≡ (p → q) ∧ (q → p) biconditional
(p → q) ∧ (p → r) ≡ p → (q ∧ r)

Proving equivalences by the laws

Show ¬(p ∨ (¬p ∧ q)) ≡ ¬p ∧ ¬q:

Step Reason
¬(p ∨ (¬p ∧ q)) given
≡ ¬p ∧ ¬(¬p ∧ q) De Morgan
≡ ¬p ∧ (¬¬p ∨ ¬q) De Morgan
≡ ¬p ∧ (p ∨ ¬q) double negation
≡ (¬p ∧ p) ∨ (¬p ∧ ¬q) distributive
≡ F ∨ (¬p ∧ ¬q) negation law
≡ ¬p ∧ ¬q identity

Check any equivalence instantly — compare both columns:

Normal forms

Every formula can be rewritten in a standard shape:

  • Disjunctive normal form (DNF): an OR of AND-terms — a "sum of products". Example: (p ∧ ¬q) ∨ (¬p ∧ q).
  • Conjunctive normal form (CNF): an AND of OR-clauses — a "product of sums". Example: (p ∨ q) ∧ (¬p ∨ ¬q).

DNF from a truth table: for each row where the formula is T, write the AND of the literals that make that row (p if p is T, ¬p if p is F); OR them together.

CNF from a truth table: for each row where the formula is F, write the OR of the negated literals; AND them together.

Python
from itertools import product

def formula(p, q, r):
    return (p or q) and not r             # the function we want in normal form

names = "pqr"
minterms, maxterms = [], []
for row in product([True, False], repeat=3):
    lits = [n if v else "¬" + n for n, v in zip(names, row)]
    if formula(*row):
        minterms.append("(" + " ∧ ".join(lits) + ")")
    else:
        maxterms.append("(" + " ∨ ".join("¬" + n if v else n for n, v in zip(names, row)) + ")")
print("DNF:", " ∨ ".join(minterms))
print("CNF:", " ∧ ".join(maxterms))
Output
DNF: (p ∧ q ∧ ¬r) ∨ (p ∧ ¬q ∧ ¬r) ∨ (¬p ∧ q ∧ ¬r)
CNF: (¬p ∨ ¬q ∨ ¬r) ∧ (¬p ∨ q ∨ ¬r) ∧ (p ∨ ¬q ∨ ¬r) ∧ (p ∨ q ∨ ¬r) ∧ (p ∨ q ∨ r)

CNF matters in computer science because SAT solvers work on CNF formulas; DNF and CNF also correspond to sum-of-products and product-of-sums circuits in Boolean algebra.

Functionally complete sets

{¬, ∧, ∨} can express every Boolean function. So can {¬, ∧} (by De Morgan), and so can NAND alone or NOR alone — which is why chips can be built from a single gate type.

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    By De Morgan's law, ¬(p ∧ q) is equivalent to...

  2. 2

    Which is equivalent to p → q?

  3. 3

    What does the absorption law give for p ∧ (p ∨ q)?

  4. 4

    A formula written as an AND of OR-clauses is in...