Hilbert's question
In the 1920s David Hilbert proposed a programme for the foundations of mathematics: formalise each branch of mathematics in a precise axiomatic system, in which proofs are finite strings checkable by rule; show that the system is complete (every statement of its language is proved or refuted); and prove its consistency by "finitary" means that nobody could doubt. If it succeeded, mathematical truth would coincide with provability in a fixed, mechanically checkable system, and that system would certify its own reliability.
In 1931 Kurt Gödel showed that both hopes fail for any system strong enough to do elementary arithmetic — and the reason is not a defect of a particular axiom system but a structural fact. A theory whose proofs can be checked mechanically can talk about its own proofs, because syntax can be coded in arithmetic. A diagonal argument, of the same shape as Cantor's, then produces a sentence that says, in effect, "I am not provable".
Today every proof assistant, including the Lean system used throughout this curriculum, is a mechanical proof checker of exactly the kind Hilbert envisaged. The incompleteness theorems tell us what such systems can never do: there will always be true arithmetical statements that a given system does not prove, and a consistent system cannot prove its own consistency.
The liar, tamed
The liar sentence "this sentence is false" is a paradox: it is true if and only if it is false. Gödel replaced truth by provability:
Now there is no paradox, only a dilemma. If proved , then would prove a sentence asserting its own unprovability — so (if proves only truths) would be both provable and false. Hence does not prove ; and that is exactly what says, so is true but unprovable in .
Two ingredients make this rigorous, and both are mathematics rather than philosophy:
- Arithmetisation. Formulas and proofs are finite strings, so they can be coded by numbers. " is the code of a proof of the formula with code " becomes an arithmetical relation between numbers, which the theory can express.
- Diagonalisation. A sentence cannot literally contain itself, but it can contain a description that evaluates to its own code — just as Cantor's diagonal sequence differs from the -th listed sequence at place .
The common shape of these diagonal arguments was isolated by F. William Lawvere (1969): if every map is of the form , then every map has a fixed point. In the diagram, and the dashed map is the "diagonal"; if then is a fixed point of .
Cantor's theorem is the case with = negation, which has no fixed point, so no surjection exists. Tarski's theorem is the case where is the set of truth values of sentences and is negation. Gödel's theorem is the case where negation is replaced by "not provable", which does have a fixed point — the Gödel sentence.
Theories, codes and representability
We work in first-order logic (see the completeness theorem) in the language of arithmetic. The numeral of is the term with copies of .
Definition— Robinson arithmetic and Peano arithmetic
Robinson arithmetic (R. M. Robinson, 1950) is the theory with the seven axioms
We write for . Peano arithmetic is together with the induction schema for every formula . has no induction at all; it cannot even prove .
Definition— consistent, complete, recursively axiomatised
A theory (a set of sentences, closed under consequence when convenient) is consistent if it proves no sentence together with its negation, and complete if for every sentence of its language, or . It is recursively axiomatised if it has a decidable set of axioms (after coding sentences by numbers, below). Then the set of theorems of is computably enumerable: list all finite sequences of formulas and keep those that are proofs.
Definition— Gödel numbering
Fix an injective coding of the symbols by positive integers, , and code a string by , where is the -th prime. A proof — a finite sequence of formulas — is coded by coding the sequence of the codes of its formulas the same way. The coding is injective and computable in both directions.
Definition— Δ₀ and Σ₁ formulas
A formula is if all its quantifiers are bounded, or ; it is if it has the form with in , and if it has the form . The truth of a sentence in can be decided by a finite search.
Definition— representability
A relation is represented in by a formula if for all : when holds, , and when it fails, . A function is represented by if for all : .
Three facts carry the technical weight. Their proofs are long but elementary; full treatments are in Smith (2013, chapters 11–20) and Boolos, Burgess and Jeffrey (2007, chapters 15–17).
Theorem— Fact A (Σ₁-completeness of Q)
Every sentence that is true in is provable in . Moreover, for each , and .
Theorem— Fact B (representability)
Every decidable relation and every computable total function on is representable in , hence in every consistent theory extending .
Theorem— Fact C (arithmetisation of proof)
If is recursively axiomatised, the relation " is the code of a -proof of the formula with code " is decidable (indeed primitive recursive, after replacing the axioms by an equivalent primitive recursive set — Craig's trick). Hence it is represented in by a formula that also defines it in and can be chosen to be . Then is a formula, true in of exactly the codes of theorems of .
Examples and non-examples
Example— Theories the theorems apply to
, , second-order arithmetic, and (which interprets by taking the finite von Neumann ordinals as numbers) are all recursively axiomatised and — we believe — consistent. Each is therefore incomplete, and none proves its own consistency. Adding the Gödel sentence as a new axiom does not help: is again recursively axiomatised and consistent, and has its own Gödel sentence.
Example— Complete theories: not enough arithmetic
- Presburger arithmetic, the theory of , is complete and decidable (Presburger, 1929, by quantifier elimination). Multiplication is not definable in it — the formal section cites Mathlib's proof of this.
- The theory of real closed fields — the first-order theory of — is complete and decidable (Tarski, published 1948 and 1951). The integers are not definable in by a first-order formula, so arithmetic cannot be interpreted.
- True arithmetic , the set of all sentences true in , is complete and consistent — but by the first incompleteness theorem it is not recursively axiomatisable (indeed, by Tarski's theorem it is not even arithmetically definable).
Example— A Gödel number
With symbol codes , and , the string "" has code , and "" has code . Any injective computable coding would serve; the experiment below uses a bijective base- coding instead.
The diagonal lemma and the first theorem
Throughout, is a recursively axiomatised theory extending .
Theorem— Diagonal lemma (Gödel 1931; Carnap 1934)
For every formula with one free variable there is a sentence such that
where abbreviates the numeral of the code of .
Proof
Let send the code of a formula to the code of the sentence , and every other number to . It is computable, so by Fact B some formula represents it in :
Put , let and let . By definition . Reasoning in ,
∎
Applying the lemma to gives a Gödel sentence with .
Theorem— First incompleteness theorem, Gödel's form
(a) If is consistent, then . (b) If moreover is -sound (every sentence it proves is true in ), then . Consequently is true in but not provable in .
Proof
(a) Suppose codes a -proof of . By Fact C, , so , and by the diagonal equivalence . Then is inconsistent.
(b) If , then , a sentence; by -soundness it is true, so , and is inconsistent, contradicting (a).
Finally, is true in , so the diagonal equivalence holds in ; since , the sentence is true in , hence so is .
∎
Gödel's original hypothesis for (b) was -consistency, which implies -soundness. J. Barkley Rosser (1936) removed it with a cleverer sentence: "if I am provable, there is a shorter proof of my negation".
Theorem— First incompleteness theorem, Rosser's form
If is a consistent, recursively axiomatised theory extending , then is incomplete: there is a sentence with and .
Proof
Let be the computable function , and write for the formula expressing this via a representing formula (Fact B). By the diagonal lemma there is a sentence with
. Suppose codes a proof of . As is consistent, no number codes a proof of , so by Fact C, for each . By Fact A, every is one of , so . Together with (take ) this gives , contradicting consistency.
. Suppose codes a proof of , so . As is consistent, , so for each . Now argue inside : let satisfy . By Fact A, or . The first case is impossible, since then is one of ; so , and witnesses . Hence , again contradicting consistency.
With more care in the choice of , the Rosser sentence can be taken to be (equivalent to) a sentence, like the Gödel sentence.
∎
The second theorem, and Tarski
Let be the sentence , with the standard provability formula of Fact C. When extends , it satisfies the Hilbert–Bernays–Löb derivability conditions: for all sentences ,
- D1 if then ;
- D2 ;
- D3 .
D1 is Fact A applied to a true sentence; D2 formalises modus ponens; D3, the hard one, is the formalisation inside of Fact A itself (it needs induction; see Boolos 1993, chapter 2).
Theorem— Second incompleteness theorem (Gödel 1931; Hilbert–Bernays 1939)
If is consistent, recursively axiomatised and extends , then .
Proof
Write for , for and for , so .
- From , by D1 and D2: .
- By D3: ; so .
- Since , D1 and D2 (twice) give .
- From 2 and 3: ; contrapositively , that is, .
If , then , contradicting part (a) of the first theorem.
∎
The proof shows more: , and in fact . Löb (1955) refined the argument to: if then — the second theorem is the case .
Theorem— Tarski's undefinability theorem (1933)
There is no formula of such that, for every sentence , . The set of codes of true arithmetical sentences is not arithmetically definable.
Proof
Apply the diagonal lemma to : some sentence satisfies , so this holds in . If defined truth, we would have .
∎
Since is definable, Tarski's theorem gives another proof that for a sound , provability and truth differ — without exhibiting the witness.
Incompleteness from the halting problem
A computability-theoretic route, due essentially to Turing (1936) and Kleene, makes incompleteness look inevitable. Recall from computability that the halting set is computably enumerable but not decidable, so its complement is not computably enumerable.
Theorem— Theorem (Turing–Kleene incompleteness)
Let be recursively axiomatised and sound (every theorem is true in ). For each program and input let be the natural sentence "the computation of on halts". Then there are such that is true but .
Proof
The set is computably enumerable (list the theorems of ). By soundness it is contained in the complement of . If it contained every pair outside it would equal the complement of , which would then be computably enumerable — so would be decidable, which it is not.
∎
This version needs soundness. Using a pair of computably inseparable c.e. sets instead of recovers Rosser's version, which needs only consistency. The theorem also says something Gödel's construction hides: the unprovable sentences include very concrete ones — "this particular program never halts". By the MRDP theorem (see the viewpoints) they include "this particular polynomial equation has no solution in natural numbers".
The diagonal argument in Lean
Mathlib does not contain Gödel's theorems for : Facts A–C are long and not yet formalised there. The Lean module therefore proves the logical skeleton of the theorems in full, with the arithmetical facts as explicit hypotheses, and proves one genuine incompleteness theorem about programs from Mathlib's computability library.
First, Lawvere's fixed-point theorem and Cantor's theorem as its corollary:
/-- **Lawvere's fixed-point theorem** (1969), the abstract core of the diagonal arguments of
Cantor, Russell, Gödel, Tarski and Turing. If `e : A → (A → B)` hits *every* function
`A → B`, then every map `f : B → B` has a fixed point.
Proof: the diagonal function `g a = f (e a a)` is `e a₀` for some `a₀`, and then
`e a₀ a₀ = g a₀ = f (e a₀ a₀)`. -/
theorem lawvere_fixed_point {A B : Type*} (e : A → A → B) (he : Function.Surjective e)
(f : B → B) : ∃ b, f b = b := by
obtain ⟨a₀, ha₀⟩ := he (fun a => f (e a a))
-- Evaluate the equation `e a₀ = g` at the diagonal point `a₀`.
exact ⟨e a₀ a₀, (congrFun ha₀ a₀).symm⟩/-- Cantor's theorem as a corollary: there is no surjection from `A` onto the predicates
`A → Prop`, because negation `¬ : Prop → Prop` has no fixed point (`P ↔ ¬P` is absurd).
Mathlib's version for sets is `Function.cantor_surjective`. -/
theorem cantor_from_lawvere {A : Type*} (e : A → A → Prop) : ¬ Function.Surjective e := by
intro he
obtain ⟨P, hP⟩ := lawvere_fixed_point e he Not
exact iff_not_self (Iff.of_eq hP.symm)Next, an abstract diagonalisable system in the sense of Smullyan (1992): formulas, sentences, Gödel numbers, substitution of numerals, negation, truth and provability. A set of numbers is expressible if some formula defines it.
/-- An abstract **diagonalisable system**, after R. M. Smullyan, *Gödel's Incompleteness
Theorems* (1992), chapter 1. It has formulas with one free variable, sentences, Gödel numbers
for both, the substitution `subst φ n = φ(n̄)` of a numeral, a negation, and two predicates on
sentences: truth (in the intended model) and provability (in the theory). The number
`diag (code ψ)` is the Gödel number of the *diagonalisation* `ψ(⌜ψ⌝)`. For first-order
arithmetic all of this is real: Gödel numbers are natural numbers and `diag` is primitive
recursive. -/
structure DiagSystem where
/-- Formulas with one free variable. -/
Formula : Type
/-- Sentences (no free variables). -/
Sentence : Type
/-- The Gödel number `⌜ψ⌝` of a formula. -/
code : Formula → ℕ
/-- The Gödel number of a sentence. -/
scode : Sentence → ℕ
/-- Different sentences have different Gödel numbers. -/
scode_injective : Function.Injective scode
/-- `subst ψ n` is the sentence `ψ(n̄)`. -/
subst : Formula → ℕ → Sentence
/-- Negation of sentences. -/
neg : Sentence → Sentence
/-- Truth in the intended model. -/
True : Sentence → Prop
/-- Provability in the theory. -/
Provable : Sentence → Prop
true_neg : ∀ s, True (neg s) ↔ ¬ True s
/-- The diagonal function on Gödel numbers. -/
diag : ℕ → ℕ
diag_code : ∀ ψ, diag (code ψ) = scode (subst ψ (code ψ))/-- A set `A` of natural numbers is **expressible** if some formula `ψ` defines it:
`ψ(n̄)` is true exactly when `n ∈ A`. -/
def Expressible (A : Set ℕ) : Prop :=
∃ ψ : S.Formula, ∀ n, S.True (S.subst ψ n) ↔ n ∈ AThe diagonal lemma is proved exactly as on paper, by a three-step calc chain. Its hypothesis hdiag — expressible sets are closed under taking preimages along diag — is the abstract counterpart of "the diagonal function is representable" (Fact B):
/-- **The diagonal lemma (semantic form).** Suppose that whenever `A` is expressible, so is
`diag⁻¹(A) = {n | diag n ∈ A}` (in arithmetic: substitution is definable). Then for every
expressible `A` there is a sentence `G` that is true iff its own Gödel number lies in `A`.
Proof: let `ψ` express `diag⁻¹(A)` and put `G = ψ(⌜ψ⌝)`. Then
`G` true ⟺ `⌜ψ⌝ ∈ diag⁻¹(A)` ⟺ `diag ⌜ψ⌝ ∈ A` ⟺ `⌜G⌝ ∈ A`. -/
theorem diagonal_lemma
(hdiag : ∀ A : Set ℕ, S.Expressible A → S.Expressible (S.diag ⁻¹' A))
{A : Set ℕ} (hA : S.Expressible A) : ∃ G : S.Sentence, S.True G ↔ S.scode G ∈ A := by
obtain ⟨ψ, hψ⟩ := hdiag A hA
refine ⟨S.subst ψ (S.code ψ), ?_⟩
calc S.True (S.subst ψ (S.code ψ)) ↔ S.code ψ ∈ S.diag ⁻¹' A := hψ (S.code ψ)
_ ↔ S.diag (S.code ψ) ∈ A := Iff.rfl
_ ↔ S.scode (S.subst ψ (S.code ψ)) ∈ A := by rw [S.diag_code ψ]From it, Tarski's theorem and the semantic first incompleteness theorem. Here hprov ("the provable codes form an expressible set") stands for Fact C, and sound for soundness. In the arithmetical setting all four hypotheses are theorems; here they are assumptions, and the formal content is that the conclusions follow from them by the diagonal argument and nothing else.
/-- **Tarski's undefinability of truth (abstract form).** If expressible sets are closed under
complements (negation is definable) and under `diag⁻¹` (substitution is definable), then the set
of Gödel numbers of *true* sentences is not expressible.
Proof: otherwise its complement is expressible, and the diagonal lemma gives a sentence `G`
that is true iff `⌜G⌝` is not the number of a true sentence, i.e. iff `G` is false. -/
theorem tarski_undefinability
(hcompl : ∀ A : Set ℕ, S.Expressible A → S.Expressible Aᶜ)
(hdiag : ∀ A : Set ℕ, S.Expressible A → S.Expressible (S.diag ⁻¹' A)) :
¬ S.Expressible (S.scode '' {s | S.True s}) := by
intro hT
obtain ⟨G, hG⟩ := S.diagonal_lemma hdiag (hcompl _ hT)
-- `⌜G⌝` is the code of a true sentence iff `G` is true, by injectivity of the coding.
have hcode : S.scode G ∈ S.scode '' {s | S.True s} ↔ S.True G :=
S.scode_injective.mem_set_image
have : S.True G ↔ ¬ S.True G := hG.trans (not_congr hcode)
exact iff_not_self this/-- **The first incompleteness theorem (abstract, semantic form).** Suppose the theory is
*sound* (it proves only true sentences), that the set of Gödel numbers of provable sentences is
expressible (Gödel's arithmetisation), and that expressible sets are closed under complements
and `diag⁻¹`. Then there is a sentence `G` that is true but unprovable, and whose negation is
unprovable too: the theory is incomplete.
Proof: the diagonal lemma applied to the non-provable codes gives `G` with
"`G` true ⟺ `G` unprovable". If `G` were provable it would be true, hence unprovable; so `G`
is unprovable, hence true. If `¬G` were provable it would be true, so `G` would be false. -/
theorem first_incompleteness
(sound : ∀ s, S.Provable s → S.True s)
(hprov : S.Expressible (S.scode '' {s | S.Provable s}))
(hcompl : ∀ A : Set ℕ, S.Expressible A → S.Expressible Aᶜ)
(hdiag : ∀ A : Set ℕ, S.Expressible A → S.Expressible (S.diag ⁻¹' A)) :
∃ G : S.Sentence, S.True G ∧ ¬ S.Provable G ∧ ¬ S.Provable (S.neg G) := by
obtain ⟨G, hG⟩ := S.diagonal_lemma hdiag (hcompl _ hprov)
have hcode : S.scode G ∈ S.scode '' {s | S.Provable s} ↔ S.Provable G :=
S.scode_injective.mem_set_image
-- The fixed-point property: `G` is true iff `G` is not provable.
have hfix : S.True G ↔ ¬ S.Provable G := hG.trans (not_congr hcode)
have hunprov : ¬ S.Provable G := fun hp => hfix.mp (sound G hp) hp
have htrue : S.True G := hfix.mpr hunprov
refine ⟨G, htrue, hunprov, fun hneg => ?_⟩
exact (S.true_neg G).mp (sound _ hneg) htrueThe computability route needs no hypotheses about arithmetic at all. Mathlib codes programs as Nat.Partrec.Code and proves the halting problem undecidable and its complement not semi-decidable. The theorem halting_incompleteness is the Turing–Kleene theorem above: any computably enumerable set of sentences that is sound for non-halting statements misses some true one.
/-- **Turing (1936):** the halting problem is undecidable — no computable procedure decides,
for every program `c`, whether `c` halts on input `n`. Here programs are Mathlib's codes
`Nat.Partrec.Code` for partial recursive functions; this applies
`ComputablePred.halting_problem`. -/
theorem halting_undecidable (n : ℕ) :
¬ ComputablePred fun c : Nat.Partrec.Code => (c.eval n).Dom :=
ComputablePred.halting_problem n/-- **Incompleteness from the halting problem** (after Turing 1936 and Kleene). Let `Provable`
be any set of (Gödel numbers of) sentences whose members can be computably listed — as the
theorems of every recursively axiomatised theory can — and let `nonHalt c` be a computable
assignment, to each program `c`, of a sentence meaning "`c` does not halt on input `n`". If the
theory is sound for these sentences, then some program never halts but the theory cannot prove
it. Proof: otherwise "`c` does not halt" would be semi-decidable — list the theorems and wait
for `nonHalt c` — and Mathlib's `ComputablePred.halting_problem_not_re` says it is not. -/
theorem halting_incompleteness (n : ℕ) (Provable : ℕ → Prop) (hre : REPred Provable)
(nonHalt : Nat.Partrec.Code → ℕ) (hc : Computable nonHalt)
(sound : ∀ c, Provable (nonHalt c) → ¬ (c.eval n).Dom) :
∃ c : Nat.Partrec.Code, ¬ (c.eval n).Dom ∧ ¬ Provable (nonHalt c) := by
by_contra h
push Not at h
-- `h : ∀ c, ¬ (c.eval n).Dom → Provable (nonHalt c)`: the theory would be complete for
-- non-halting, so non-halting would be semi-decidable.
have hcomp : REPred fun c => Provable (nonHalt c) := hre.comp hc
exact ComputablePred.halting_problem_not_re n (hcomp.of_eq fun c => ⟨sound c, h c⟩)Finally two library facts: formulas over a countable language can be numbered (the first step of arithmetisation), and multiplication is not definable in Presburger arithmetic — which is why Presburger arithmetic can be complete.
/-- The first step of arithmetisation: over a countable language, the first-order formulas
(with variables indexed by `ℕ`) form a countable set, so they can be numbered. This is the
instance `FirstOrder.Language.Formula`'s `Countable` from `Mathlib.ModelTheory.Encoding`. -/
theorem formulas_countable (L : FirstOrder.Language) [Countable L.Symbols] :
Countable (L.Formula ℕ) :=
inferInstance/-- Incompleteness needs multiplication. In the language `(0, 1, +)` of Presburger arithmetic
the graph of multiplication `{(a, b, c) | a = b * c}` is **not definable** in `ℕ`, even with
any set `A` of parameters — and Presburger arithmetic is complete and decidable. This applies
Mathlib's `FirstOrder.Language.presburger.mul_not_definable` (the definable sets are exactly the
semilinear ones). -/
theorem presburger_mul_not_definable (A : Set ℕ) :
¬ A.Definable FirstOrder.Language.presburger {v : Fin 3 → ℕ | v 0 = v 1 * v 2} :=
FirstOrder.Language.presburger.mul_not_definableWhat is not formalised here: the representability theorem (Fact B), the arithmetisation of -provability (Fact C), Rosser's theorem for consistent theories, and the second incompleteness theorem with its derivability conditions. Complete formalisations of the incompleteness theorems exist outside Mathlib — for example Shankar's in the Boyer–Moore prover (1986), O'Connor's in Coq (2005) and Paulson's in Isabelle/HOL (2015, including the second theorem), and there are Lean 4 developments outside Mathlib.
Computing a self-referential sentence
The first experiment Gödel-numbers strings, computes the function and checks the fixed-point identity for . With "" for a decidable property of codes, it evaluates and confirms . With = "is true", the liar sentence has no consistent truth value. Notice that does not contain its own code (the code is far longer than ): it contains a term that evaluates to its code.
"""The diagonal lemma, computed: sentences that talk about their own codes.
Formulas are strings over a small alphabet, and a bijective base-k numbering
turns every string into a natural number (its Goedel number) and back. The
function diag sends the code n of a formula phi(x) to the code of phi(n), the
result of substituting phi's own code, written as a numeral, for x.
For a template phi(x) = "P(diag(x))" the sentence G = phi(code of phi) says
"P holds of the sentence numbered diag(code of phi)" - and that sentence is G
itself. We check this identity exactly, and evaluate G when P is a decidable
property of codes: G then has a definite truth value, and G <-> P(code of G).
When P is a truth predicate, evaluating G requires evaluating G: for the liar
"~True(diag(x))" no truth value is consistent, which is the heart of Tarski's
theorem.
Numerals are written in decimal as an abbreviation of S...S0. Nothing here
decides provability, so this exhibits the mechanism of self-reference, not
the incompleteness theorem itself.
Runs unchanged in the browser (Pyodide) using only the standard library.
"""
import textwrap
#: The alphabet (all symbols distinct). Each formula is a string over it.
ALPHABET = "0123456789()~&|>=+*SAPdiagxyEvenLoTru"
BASE = len(ALPHABET)
def encode(text):
"""Bijective base-k Goedel number of a string (the empty string is 0)."""
code = 0
for ch in text:
index = ALPHABET.find(ch)
if index < 0:
raise ValueError(f"symbol {ch!r} is not in the alphabet")
code = code * BASE + index + 1
return code
def decode(code):
"""Inverse of encode: the string whose Goedel number is ``code``."""
if code < 0:
raise ValueError("codes are natural numbers")
chars = []
while code > 0:
code, digit = divmod(code - 1, BASE)
chars.append(ALPHABET[digit])
return "".join(reversed(chars))
def substitute(formula, n):
"""Replace the free variable x by the numeral of n (x is never bound here)."""
return formula.replace("x", str(n))
def diag(code):
"""Code of the diagonalisation phi(code of phi) of the formula with this code."""
return encode(substitute(decode(code), code))
def fixed_point(template):
"""The sentence G = phi(code of phi) for a template phi(x)."""
return substitute(template, encode(template))
#: Decidable properties of codes that sentences may mention.
PREDICATES = {
"Even": lambda code: code % 2 == 0,
"Long": lambda code: len(decode(code)) > 30,
}
def parse(sentence):
"""Split "P(diag(N))" or "~P(diag(N))" into (negated, P, diag(N))."""
negated = sentence.startswith("~")
body = sentence[1:] if negated else sentence
name, _, rest = body.partition("(")
if not (rest.startswith("diag(") and rest.endswith("))")):
raise ValueError(f"not of the form P(diag(N)): {sentence!r}")
return negated, name, diag(int(rest[len("diag("):-2]))
def evaluate(sentence):
"""Truth value of a sentence about a decidable property of a code."""
negated, name, target = parse(sentence)
value = PREDICATES[name](target)
return (not value) if negated else value
def self_reference_values(sentence):
"""Truth values v consistent with a sentence "[~]True(diag(N))" about itself.
If the sentence is about its own code, its value v must satisfy v = v
(unnegated: both values fit) or v = not v (negated: none fits).
"""
negated, name, target = parse(sentence)
if name != "True" or target != encode(sentence):
raise ValueError("expected a truth predicate applied to the sentence itself")
return [v for v in (True, False) if v == ((not v) if negated else v)]
def main():
phi = "~P(diag(x))"
g = fixed_point(phi)
n = encode(phi)
print("1. Goedel's sentence, reading P(m) as 'sentence m is provable'")
print(f" phi(x) = {phi}; its code n has {len(str(n))} digits")
print(f" G = phi(n) = ~P(diag({str(n)[:10]}...)), {len(g)} symbols")
print(f" diag(n) == code of G: {diag(n) == encode(g)}")
print(f" code of G has {len(str(encode(g)))} digits, so G does not "
f"contain it: {str(encode(g)) not in g}")
print()
print("2. Fixed points of decidable properties (Long = over 30 symbols)")
for template in ["Even(diag(x))", "~Even(diag(x))", "Long(diag(x))",
"~Long(diag(x))"]:
s = fixed_point(template)
negated, name, _ = parse(s)
value, own = evaluate(s), PREDICATES[name](encode(s))
holds = value == ((not own) if negated else own)
print(f" {template:<15} G: {str(value):<5} {name}(code G): "
f"{str(own):<5} equation holds: {holds}")
print()
print("3. A truth predicate inside the language")
for template in ["True(diag(x))", "~True(diag(x))"]:
values = self_reference_values(fixed_point(template))
print(f" {template:<15} consistent truth values for G: {values}")
print()
print(textwrap.fill(
"Epistemic status: exact computation. Part 1 verifies the identity "
"diag(code phi) = code G for one template; it holds for every template "
"by construction, which is the diagonal lemma's proof in miniature. Part 2 "
"checks G <-> P(code G) (or its negated form) for four fixed points. Part 3 "
"shows only that the liar has no consistent value under this evaluation; "
"that no arithmetical formula defines truth is Tarski's theorem, proved in "
"the lesson. Nothing here computes provability.",
width=78,
))
if __name__ == "__main__":
main()The second experiment makes the asymmetry between theorems and non-theorems tangible on a toy formal system, Hofstadter's MIU system. Its theorems can be listed mechanically, but no amount of listing shows that MU is not a theorem; an invariant (the number of I's is never divisible by 3) proves it. For a recursively axiomatised theory of arithmetic, the incompleteness theorems say that some truths of the second kind have no proof in the theory at all.
"""Theorems can be listed; non-theorems need an argument: the MIU system.
Hofstadter's MIU system (Goedel, Escher, Bach, 1979) is a tiny formal system.
Its only axiom is MI and its rules, for strings x, y over {I, U}, are
xI -> xIU, Mx -> Mxx, xIIIy -> xUy, xUUy -> xy.
Like the theorems of any recursively axiomatised theory, its theorems can be
listed mechanically: apply every rule to every theorem found so far. Here we
list all theorems reachable through strings of at most 16 symbols.
Is MU a theorem? The search never finds it - but a search can only ever say
"not yet". The answer comes from an argument outside the system: the number
of I's is never divisible by 3 (it starts at 1, and the rules keep it, double
it, or subtract 3). We check this invariant on every theorem found, and check
the known characterisation "Mx is a theorem iff the number of I's in x is not
divisible by 3" on all strings of at most 7 symbols.
Runs unchanged in the browser (Pyodide) using only the standard library.
"""
from collections import Counter
from itertools import product
import textwrap
AXIOM = "MI"
def successors(s):
"""All strings obtained from s by one application of one rule."""
out = set()
if s.endswith("I"):
out.add(s + "U")
out.add("M" + s[1:] * 2)
for i in range(1, len(s) - 2):
if s[i:i + 3] == "III":
out.add(s[:i] + "U" + s[i + 3:])
for i in range(1, len(s) - 1):
if s[i:i + 2] == "UU":
out.add(s[:i] + s[i + 2:])
return out
def theorems(max_len):
"""Theorems derivable with every intermediate string of length <= max_len."""
seen = {AXIOM}
frontier = [AXIOM]
while frontier:
new = []
for s in frontier:
for t in sorted(successors(s)):
if len(t) <= max_len and t not in seen:
seen.add(t)
new.append(t)
frontier = new
return seen
def invariant(s):
"""Number of I's modulo 3; it is never 0 for a theorem."""
return s.count("I") % 3
def characterisation_failures(found, max_len):
"""Strings Mx of length <= max_len where "#I not divisible by 3" and
"found as a theorem" disagree."""
bad = []
for n in range(max_len):
for tail in product("IU", repeat=n):
s = "M" + "".join(tail)
if (invariant(s) != 0) != (s in found):
bad.append(s)
return bad
def main():
found = theorems(16)
by_length = Counter(len(s) for s in found)
print("Theorems of MIU reachable through strings of <= 16 symbols")
print(f" total found: {len(found)}")
print(" by length: " + " ".join(f"{k}:{by_length[k]}" for k in range(2, 12)))
print(" shortest: " + ", ".join(sorted(found, key=lambda s: (len(s), s))[:6]))
print()
counts = Counter(invariant(s) for s in found)
print(f"Invariant (#I mod 3) over all theorems found: {dict(sorted(counts.items()))}")
print(f"MU found: {'MU' in found} (#I in MU mod 3 = {invariant('MU')})")
bad = characterisation_failures(found, 7)
print(f"Strings of <= 7 symbols where '#I mod 3 != 0' and 'theorem' disagree: "
f"{len(bad)}")
print()
print(textwrap.fill(
"Epistemic status: exhaustive within the stated bound. The search lists "
"theorems (it is a semi-decision procedure), but its failure to find MU "
"proves nothing; the invariant argument proves that MU is not a theorem. "
"The agreement for strings of at most 7 symbols is a finite check of a "
"characterisation that is proved in general by a separate argument.",
width=78,
))
if __name__ == "__main__":
main()Both are exact computations; neither computes provability in , which is undecidable.
Explore: the diagonal
Gödel's construction is a diagonal argument. In the widget, every list of binary sequences you write is defeated by the flipped diagonal. Change an entry on the diagonal and watch the new sequence change at exactly that place.
The diagonal machine
Here is a list of infinite 0–1 sequences (we can only show the start). The gold sequence d flips the diagonal.
| s1 | … | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| s2 | … | ||||||||||
| s3 | … | ||||||||||
| s4 | … | ||||||||||
| s5 | … | ||||||||||
| s6 | … | ||||||||||
| d | 1 | 0 | 1 | 0 | 0 | 1 | ? | ? | ? | ? | … |
Point at (or tab to) a row to see where d differs from it. Click any digit to change the list.
Add d to the list and a new d appears that is different again. No list — however long — can contain every sequence.
Compare with the diagonal lemma: the "list" is the list of formulas in one free variable, the entry in row and column is the sentence , and the diagonal is . Cantor flips the diagonal to escape the list; Gödel applies to the diagonal and finds that the result lies on the list — at the row of — which produces the fixed point.
Exercises
1. Gödel's incompleteness theorems
Warm-upHumanTo which of these theories does the first incompleteness theorem (Rosser's form) apply, assuming each is consistent? Choose all that apply.
2. Gödel's incompleteness theorems
Warm-upHumanCode a string by , with symbol codes , and . What is the Gödel number of the string ?
3. Gödel's incompleteness theorems
CoreHumanIn Gödel's form of the first theorem, part (a) shows and part (b) shows . Which hypothesis does part (b) need beyond those of part (a)?
4. Gödel's incompleteness theorems
CoreHumanAssume is consistent, and let where is the Gödel sentence of . Which statements are true? Choose all that apply.
5. Gödel's incompleteness theorems
StretchHumanLöb's theorem: if , then (for recursively axiomatised). Apply it with the sentence . What do you obtain?
6. Gödel's incompleteness theorems
CoreFormalIn the Lean file, which statement correctly formalises the abstract form of Tarski's theorem for a diagonalisable system S (with hcompl and hdiag as hypotheses)?
7. Gödel's incompleteness theorems
CoreFormalThe Lean theorem halting_incompleteness has hypotheses hre : REPred Provable, hc : Computable nonHalt and sound. Which hypothesis expresses that the theory is recursively axiomatised?
8. Gödel's incompleteness theorems
StretchComputationalWrite encode(codes), which sends a list of positive symbol codes to , and decode(n), which recovers the list (and [] from 1). Why is decode(encode(xs)) == xs guaranteed?
Common misconceptions
Misconception
"Gödel showed that some truths can never be known." The theorems concern provability in a fixed formal theory. The Gödel sentence of is provable in , and we know it is true (given that is consistent). What is impossible is a single consistent, recursively axiomatised theory that proves all arithmetical truths.
Misconception
"The theorems apply to every axiomatic system." They need two hypotheses: the theory is recursively axiomatised (true arithmetic is complete but not), and it interprets enough arithmetic (Presburger arithmetic and the theory of real closed fields are complete and decidable). Group theory is incomplete for a different and much simpler reason: it has non-isomorphic models.
Misconception
"The second theorem leaves the consistency of unsupported." It says only that cannot prove its own (standard) consistency statement. is provable in stronger systems — in , which has a model of , and in primitive recursive arithmetic plus induction up to (Gentzen 1936; see proof theory). Whether those proofs add confidence is a philosophical question; that they exist is a theorem.
Misconception
"Unprovable sentences are always artificial self-referential tricks." Paris and Harrington (1977) found a natural strengthening of the finite Ramsey theorem that is true but unprovable in ; Goodstein's theorem (see the proof theory lesson) is another. By the MRDP theorem, for each such some Diophantine equation has no solutions but cannot prove it.
Misconception
"Adding the Gödel sentence as an axiom completes the theory." is still consistent and recursively axiomatised, so it has its own undecided sentence. Incompleteness is not a gap that can be filled; it regenerates.
Many ways of seeing
Topology— The space of completions is a Cantor set
Let be the set of complete consistent theories extending (in the same language), with the topology whose basic open sets are . It is the Stone space of the Lindenbaum algebra of : compact, Hausdorff and totally disconnected, and second countable because there are countably many sentences. A point is isolated exactly when for some , that is, when is consistent and complete. For , Rosser's theorem says that no consistent is complete — so has no isolated points. By Brouwer's characterisation (1910), a non-empty compact metrisable totally disconnected space without isolated points is homeomorphic to the Cantor set. Incompleteness, topologically: the completions of form a Cantor set, and its Lindenbaum algebra is the countable atomless Boolean algebra. (Compare point-set topology.)
Computation— Incompleteness as a shadow of undecidability
From the computer scientist's side, a recursively axiomatised theory is a program that enumerates theorems. If it were complete and consistent, it would decide every sentence — and so (by Fact A) would decide the halting problem. Chaitin (1974) gave a quantitative version: for each consistent recursively axiomatised extending there is a constant such that proves no sentence of the form "the Kolmogorov complexity of the string exceeds ", although all but finitely many strings have complexity above . The proof is a formalised Berry paradox: a short program that searches 's theorems for such a statement would itself describe in fewer than bits.
Number theory— Unprovable Diophantine equations
The MRDP theorem (Matiyasevich 1970, completing work of Davis, Putnam and Julia Robinson) says that every computably enumerable set is Diophantine: there is a polynomial with integer coefficients such that if and only if has a solution in natural numbers. Taking to be the c.e. set of codes of theorems of , one obtains, for each consistent recursively axiomatised , a specific polynomial equation with no solution in such that cannot prove that it has none. Hilbert's tenth problem — an algorithm deciding solvability of Diophantine equations — is unsolvable for the same reason.
Combinatorics— Natural independence
Ramsey's theorem says that for all there is an such that every colouring of the -element subsets of with colours has a homogeneous set of size . Paris and Harrington (1977) added one condition — the homogeneous set must be relatively large, — and showed that the resulting statement, a sentence of arithmetic easily proved from the infinite Ramsey theorem, is unprovable in ; indeed it implies over . The function giving the least grows faster than every function proves total.
Category theory— Diagonal arguments as fixed points
Lawvere's theorem holds in any cartesian closed category: if is point-surjective, every endomorphism of has a fixed point. Cantor, Russell, Tarski, Gödel and Turing are all instances (Lawvere 1969; Yanofsky 2003 surveys them). Read contrapositively, it says that a "truth-value object" with a fixed-point-free endomorphism such as negation cannot be the target of a universal family — which is the categorical heart of Tarski's theorem. See categorical logic.
Logic— Provability logic
Treat as a modal operator. Over the derivability conditions and Löb's theorem become the axioms of the modal logic : and , with necessitation. Solovay (1976) proved that is exactly the set of modal principles that proves about its own provability predicate under all arithmetical interpretations. The second incompleteness theorem is the -theorem .
History and context
- Hilbert's programme took shape in the 1920s, with Paul Bernays, Wilhelm Ackermann and John von Neumann working on finitary consistency proofs.
- Kurt Gödel, then in Vienna, announced the first theorem at a conference in Königsberg in September 1930 and published both theorems in 1931 ("Über formal unentscheidbare Sätze der Principia Mathematica und verwandter Systeme I"). Von Neumann, on hearing the first theorem, independently found the second, but Gödel had already obtained it.
- Emil Post had reached related ideas in the early 1920s but did not publish them at the time; he later gave an account of this anticipation.
- Alfred Tarski proved the undefinability of truth in his 1933 monograph on the concept of truth (in Polish; German translation 1935).
- J. Barkley Rosser (1936) weakened -consistency to consistency. Alonzo Church and Alan Turing (1936) proved the undecidability of first-order logic, and Turing's halting problem gave incompleteness a computational form.
- Gerhard Gentzen (1936) proved the consistency of by transfinite induction up to . Hilbert and Bernays (1939) and Löb (1955) isolated the derivability conditions.
- Tarski, Mostowski and Raphael Robinson (1953) developed essential undecidability, with as a finitely axiomatised base. Yuri Matiyasevich (1970), building on Martin Davis, Hilary Putnam and Julia Robinson, settled Hilbert's tenth problem; Jeff Paris and Leo Harrington (1977) found the first natural mathematical statement independent of .
Further reading
- Peter Smith, An Introduction to Gödel's Theorems (2007; 2nd edition 2013) — the best first course, with full proofs of Facts A–C.
- George S. Boolos, John P. Burgess and Richard C. Jeffrey, Computability and Logic (5th edition, 2007) — the computability route and the representability theorem.
- Herbert B. Enderton, A Mathematical Introduction to Logic (1972; 2nd edition 2001), chapter 3.
- Raymond M. Smullyan, Gödel's Incompleteness Theorems (1992) — the abstract diagonal framework used in the Lean file.
- George Boolos, The Logic of Provability (1993) — derivability conditions, Löb's theorem and Solovay's theorem.
- Torkel Franzén, Gödel's Theorem: An Incomplete Guide to Its Use and Abuse (2005) — what the theorems do not say.
- Petr Hájek and Pavel Pudlák, Metamathematics of First-Order Arithmetic (1993) — the research-level treatment.
- Kurt Gödel's 1931 paper, in English in Jean van Heijenoort (ed.), From Frege to Gödel (1967).