Logic & Proofs
The language of rigorous reasoning — propositional logic, truth tables, implication and its contrapositive, logical equivalences, predicates and quantifiers, and proof techniques (direct, contrapositive, contradiction, cases, induction, strong induction) with programming connections like loop invariants.
Every if condition, database query, type checker and correctness argument rests on logic. Proof techniques let you go beyond "it worked on my tests" to "it works for every input" — the basis of algorithm correctness, invariants and formal verification.
Propositional logic
A proposition is a statement that is either true or false: "7 is prime", "x > 3" (once x is fixed). Combine them with connectives:
| Connective | Symbol | Code | True when |
|---|---|---|---|
| Not | ¬p | !p | p is false |
| And | p ∧ q | p && q | both are true |
| Or | p ∨ q | p || q | at least one is true |
| Xor | p ⊕ q | p != q, ^ | exactly one is true |
| Implies | p → q | !p || q | p is false, or q is true |
| Iff | p ↔ q | p == q | both have the same value |
Implication
"If it rains, the ground is wet" (p → q) is false only when p is true and q is false. When p is false, the implication is vacuously true — the promise wasn't broken.
| p | q | p → q |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | T |
| F | F | T |
Related statements:
| Statement | Form | Equivalent to p → q? |
|---|---|---|
| Contrapositive | ¬q → ¬p | Yes — always |
| Converse | q → p | No |
| Inverse | ¬p → ¬q | No (it's the converse's contrapositive) |
"If a number is divisible by 4, it is even" is true; its converse "if even, then divisible by 4" is false (6).
Important equivalences
| Law | Equivalence |
|---|---|
| De Morgan | ¬(p ∧ q) ≡ ¬p ∨ ¬q, ¬(p ∨ q) ≡ ¬p ∧ ¬q |
| Double negation | ¬¬p ≡ p |
| Distributive | p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) |
| Implication | p → q ≡ ¬p ∨ q |
| Contrapositive | p → q ≡ ¬q → ¬p |
De Morgan in code — simplifying conditions:
// "not (logged in and admin)"
if (!(user.loggedIn && user.isAdmin)) deny();
// is the same as
if (!user.loggedIn || !user.isAdmin) deny();A formula that's always true is a tautology; always false, a contradiction; true for some assignment, satisfiable. Deciding satisfiability (SAT) is the original NP-complete problem (Complexity classes), yet modern SAT solvers handle millions of variables in practice.
Predicates and quantifiers
A predicate is a statement with variables: Prime(n), x < y. Quantifiers say how many values make it true:
| Quantifier | Symbol | Meaning | Code analogue |
|---|---|---|---|
| Universal | ∀x P(x) | P holds for every x | xs.every(P), all(...) |
| Existential | ∃x P(x) | P holds for at least one x | xs.some(P), any(...) |
Negating quantifiers flips them (De Morgan for quantifiers):
"Not every test passed" = "some test failed". To disprove a "for all" claim, one counterexample suffices.
Order matters with mixed quantifiers: ∀x ∃y (y > x) — "for every number there's a bigger one" — is true for integers; ∃y ∀x (y > x) — "there's a number bigger than all numbers" — is false. SQL's NOT EXISTS, ALL and ANY are quantifiers (SQL advanced).
Proof techniques
Direct proof
Assume the hypothesis, derive the conclusion step by step.
Claim: the sum of two even integers is even. Proof: let a = 2m and b = 2n. Then a + b = 2(m + n), which is even. ∎
Proof by contrapositive
Prove ¬q → ¬p instead of p → q, when it's easier.
Claim: if n² is even, then n is even. Proof (contrapositive): suppose n is odd, n = 2k + 1. Then n² = 4k² + 4k + 1 = 2(2k² + 2k) + 1, which is odd. ∎
Proof by contradiction
Assume the statement is false and derive something impossible.
Claim: there are infinitely many primes. Proof (Euclid): suppose there are finitely many, p₁, …, pₖ. Let N = p₁p₂⋯pₖ + 1. N leaves remainder 1 when divided by every pᵢ, so N's prime factors are not on the list — contradiction. ∎
The same technique proves the halting problem undecidable (Turing machines) and √2 irrational.
Proof by cases
Split into exhaustive cases and prove each. E.g. "n² + n is even": if n is even, n² + n = n(n + 1) has the even factor n; if odd, n + 1 is even. ∎
Mathematical induction
To prove P(n) for all n ≥ n₀:
- Base case: prove P(n₀).
- Inductive step: assume P(k) (the inductive hypothesis) and prove P(k + 1).
Like dominoes: the first falls, and each one knocks over the next.
Claim: 1 + 2 + ⋯ + n = n(n + 1)/2. Base: n = 1: 1 = 1·2/2. ✓ Step: assume 1 + ⋯ + k = k(k + 1)/2. Then 1 + ⋯ + k + (k + 1) = k(k + 1)/2 + (k + 1) = (k + 1)(k + 2)/2. ✓ ∎
Strong induction assumes P holds for all values up to k, not just k. E.g. every integer ≥ 2 is a product of primes: if k + 1 isn't prime, it's a × b with smaller a, b, which are products of primes by the strong hypothesis.
Structural induction proves properties of recursive structures (lists, trees, ASTs): prove it for the base constructors, then for each recursive constructor assuming it holds for the parts.
Proofs and programs
Induction and recursion are two sides of the same coin (Recursion): a recursive function is correct if its base case is correct and each recursive call, assumed correct on smaller input, combines into a correct result.
Loop invariants apply induction to loops — a property that is true before the loop and preserved by each iteration:
def array_max(a):
best = a[0]
for i in range(1, len(a)):
# Invariant: best == max(a[0..i-1])
if a[i] > best:
best = a[i]
# Invariant at exit (i == len(a)): best == max(a[0..n-1])
return bestShow the invariant holds initially (initialisation), that each iteration preserves it (maintenance), and that at exit it implies the result (termination) — exactly the structure of an induction proof. Binary search's invariant ("if the target exists, it's in [lo, hi]") is the key to writing it bug-free (Searching).
Formal tools take this further: type systems are logics (the Curry–Howard correspondence: types are propositions, programs are proofs); Dafny, TLA+, Coq/Rocq and Lean verify programs and distributed protocols mechanically.
- Propositions combine with ¬, ∧, ∨, →, ↔; p → q is false only when p is true and q is false.
- An implication equals its contrapositive, not its converse; De Morgan's laws simplify negations.
- ∀ and ∃ quantify predicates; negation swaps them; one counterexample disproves a ∀ claim.
- Proof techniques: direct, contrapositive, contradiction, cases, induction (weak, strong, structural).
- Induction mirrors recursion; loop invariants prove loops correct (initialisation, maintenance, termination).