Logical equivalence

  • A tautology is true in every row: p ∨ ¬p.
  • A contradiction is false in every row: p ∧ ¬p.
  • A contingency is neither.

Two formulas are logically equivalent, written A ≡ B, when they have the same value in every row, that is, when A ↔ B is a tautology. Equivalent formulas can replace each other anywhere, which is how programmers simplify conditions and how chip designers shrink circuits.

Checking by brute force

With few letters, a computer can simply try every row:

from itertools import product

def equivalent(f, g, letters):
    """Same value in every row? Returns (True, None) or (False, a row where they differ)."""
    for row in product([True, False], repeat=letters):
        if f(*row) != g(*row):
            return False, row
    return True, None

def implies(p, q):
    return (not p) or q

de_morgan = equivalent(lambda p, q: not (p and q), lambda p, q: (not p) or (not q), 2)
contrapositive = equivalent(lambda p, q: implies(p, q), lambda p, q: implies(not q, not p), 2)
converse = equivalent(lambda p, q: implies(p, q), lambda p, q: implies(q, p), 2)

print("De Morgan:", de_morgan)
print("contrapositive:", contrapositive)
print("converse:", converse)
Open in compiler

equivalent takes two functions and tries all 2n rows. It confirms De Morgan's law and the contrapositive, and finds the row where the converse fails: p false, q true.

The laws

LawEquivalence
Double negation¬¬p ≡ p
De Morgan's laws¬(p ∧ q) ≡ ¬p ∨ ¬q, and ¬(p ∨ q) ≡ ¬p ∧ ¬q
Commutativep ∧ q ≡ q ∧ p, and p ∨ q ≡ q ∨ p
Associative(p ∧ q) ∧ r ≡ p ∧ (q ∧ r), and the same for ∨
Distributivep ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r), and p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
Identityp ∧ T ≡ p, and p ∨ F ≡ p
Dominationp ∨ T ≡ T, and p ∧ F ≡ F
Absorptionp ∨ (p ∧ q) ≡ p, and p ∧ (p ∨ q) ≡ p
Implicationp → q ≡ ¬p ∨ q
Biconditionalp ↔ q ≡ (p → q) ∧ (q → p)

De Morgan's laws in words: "not (rainy and cold)" means "not rainy, or not cold". Programmers use them every day: not (x > 0 and x < 10) is x <= 0 or x >= 10.

Simplifying with the laws

¬(p ∨ (¬p ∧ q))
≡ ¬p ∧ ¬(¬p ∧ q)        De Morgan
≡ ¬p ∧ (¬¬p ∨ ¬q)       De Morgan
≡ ¬p ∧ (p ∨ ¬q)         double negation
≡ (¬p ∧ p) ∨ (¬p ∧ ¬q)  distributive
≡ F ∨ (¬p ∧ ¬q)         ¬p ∧ p is a contradiction
≡ ¬p ∧ ¬q               identity

Contrapositive, converse, inverse

For p → q:

NameFormEquivalent to p → q?
contrapositive¬q → ¬pyes
converseq → pno
inverse¬p → ¬qno (but it is equivalent to the converse)

"If it is a mango, it is a fruit" is true, and so is its contrapositive "if it is not a fruit, it is not a mango". Its converse "if it is a fruit, it is a mango" is false. Mixing up a statement and its converse is one of the most common errors in reasoning.

Quick check

1. What is ¬(p ∧ q) equivalent to?
2. Which is equivalent to 'if n is prime then n is odd'?
3. When are A and B logically equivalent?

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.