The Kochen–Specker theorem, in Lean
Theorem (Kochen and Specker, "The Problem of Hidden Variables in Quantum Mechanics", J. Math. Mech. 17 (1967), 59–87). In a Hilbert space of dimension d ≥ 3, real or complex, there is no way to assign true or false to every vector so that each orthonormal basis has exactly one true vector.
In quantum terms: there is no non-contextual assignment of definite outcomes to all yes/no measurements, consistent with the rule that exactly one outcome of each complete measurement occurs. Kochen and Specker state the theorem for quantum observables and their functional relations; the form here is the standard equivalent for rank-one projections. The theorem is on Mathlib's list of 1000+ theorems (docs/1000.yaml, Wikidata Q528955), with no formalization recorded there.
This repository proves it in Lean 4, for every d ≥ 3 and every RCLike field 𝕜 (so ℝ and ℂ):
theorem KochenSpecker.kochen_specker {𝕜 : Type*} [RCLike 𝕜] {d : ℕ} (hd : 3 ≤ d) :
¬ ∃ v : EuclideanSpace 𝕜 (Fin d) → Bool,
∀ b : OrthonormalBasis (Fin d) 𝕜 (EuclideanSpace 𝕜 (Fin d)), ∃! i, v (b i) = true
The map v is defined on all vectors, and nothing is assumed about its values off unit vectors or under phases. A valuation on rays or projections, the usual textbook form, gives such a map, so this form is at least as strong. In ℝ² such a map exists (audit/Control.lean, two_dim_valuation), so the hypothesis d ≥ 3 is needed. The library also proves the theorem for every inner product space of dimension d ≥ 3 (KochenSpecker.not_isValuation).
Earlier formal proofs cover ℝ³ (in Lean and in HOL Light) and ℂ⁴ (in Lean); see Related work. What is new here is the statement for every dimension d ≥ 3, over ℝ and ℂ at once, the step between dimensions, and the parity route for d ≥ 4 in any such space.
The proof
- Dimension three: Peres's 33 rays (A. Peres, "Two simple proofs of the Kochen-Specker theorem", J. Phys. A 24 (1991) L175). These are the coordinate permutations and sign changes of (0, 0, 1), (0, 1, 1), (0, 1, √2) and (1, 1, √2), one vector per ray. Coordinates are kept exactly as integer pairs (a, b) meaning a + b√2, so orthogonality is an integer computation. The rays have 72 orthogonal pairs and 16 orthogonal triads. A valuation would colour them: each triad is, once normalised, an orthonormal basis (exactly one true), and each pair extends to one (at most one true).
scripts/peres33.pyfinds a case-split refutation of every colouring, an 895-node tree, andKochenSpecker/Checker.leanchecks it in the Lean kernel withdecide, using a checker proved sound there. Nothing computed outside Lean is trusted, and CI regenerates the data and fails if it differs from the committed file. - Every larger dimension. A valuation
vin dimension d + 1 is false on some unit vector u (any orthonormal basis has at least two vectors, only one of them true). An orthonormal basis of u⊥ together with u is an orthonormal basis of the whole space, sovrestricted to u⊥ is a valuation in dimension d. Hence the theorem in dimension d gives it in dimension d + 1 (KochenSpecker/Valuation.lean). - A second proof for d ≥ 4, without computer search: Cabello's 18 vectors (A. Cabello, J. M. Estebaranz and G. García-Alcaine, "Bell-Kochen-Specker theorem: A proof with 18 vectors", Phys. Lett. A 212 (1996) 183). Eighteen integer vectors in ℝ⁴ form 9 orthogonal bases, and every vector lies in exactly two of them. A valuation gives each basis exactly one true, nine in all, but each true vector is counted twice, an even number (
KochenSpecker/Cabello18.lean,kochen_specker_of_four_le).
I haven't read the three papers, only their bibliographic records; the proofs here don't follow their text.
Related work
- ℝ³ in Lean: guptbot/free_will_lean (December 2024, Lean 4 and Mathlib) proves
FreeWillLean.kochen_speckerfor all unit vectors of ℝ³, by a hand-written sequence of assignments, as groundwork for the Free Will Theorem. - ℂ⁴ in Lean: spitters/catcrypt-compiler (November 2026) proves
kochenSpecker_complex, the theorem in ℂ⁴ from Cabello's 18 vectors. - ℝ³ in HOL Light: John Harrison's
KOCHEN_SPECKER_LEMMAproves the theorem in ℝ³ from Peres's configuration with exact coordinates and an automated case analysis, the same architecture as dimension three here. - Kochen–Specker sets in ℂ³: michaelkernaghan/contextuality, the Lean code for the paper "The Algebraic Landscape of Kochen–Specker Sets in Dimension Three" (arXiv:2603.16988; the repository's current version is 11, and versions 1–9 are withdrawn). Its
lean/project (Mathlib) proves that named ray sets in ℂ³ admit no Kochen–Specker colouring, minimum sizes of such sets in coordinate pools, andsix_divides_iff_ks_uncolorable: the pool of 0 and the n-th roots of unity is uncolourable exactly when 6 divides n. Itslean-ks/project (core Lean) checks uncolourability certificates for Peres's 33 and Conway and Kochen's 31 at the level of labels. The checker here uses the same method aslean-ks/, a case-split tree over a list-based partial assignment checked withdecide, which I read before writing mine. Mine was written separately (explicit pair and triad lists, a single conflict-detecting leaf, exactly-one triads), and no file was copied. That repository states no open-source licence. - My own csd-lean4 has Cabello's parity count and the orthogonality of the 18 vectors in ℝ⁴, inside a larger project. The parity argument here is a standalone rewrite.
- Qulinecier/KS_SAT has lemmas about a SAT encoding of Kochen–Specker colourings in dimension three.
- I found no statement of the theorem for every dimension over ℝ and ℂ in Mathlib, Physlib, QICLean, Lean-QIT, Lean-QuantumInfo, Formal Conjectures or the repositories above, and nothing on Kochen–Specker in the Palomar registry, on 24 November 2026. That isn't proof that none exists.
Files
Challenge.leanstates the theorem, ending insorry. It imports only Mathlib.KochenSpecker/Checker.lean: the case-split checker and its soundness.KochenSpecker/Valuation.lean: valuations, the basis lemmas, and the step from dimension d + 1 to d.KochenSpecker/Peres33.lean: dimension three. Its data block is generated fromKochenSpecker/Peres33.lean.inbyscripts/assemble-peres33.sh, which runsscripts/peres33.py.KochenSpecker/Cabello18.lean: dimension four by parity.KochenSpecker/Main.lean: the theorem;Solution.leanre-exports it.
Checks
audit/Axioms.leanuses#guard_msgsto require that the theorem, its general form, both base cases and the second route use onlypropext,Classical.choiceandQuot.sound. The dimension-three certificate is checked withdecide, notnative_decide, so it adds no axiom.audit/Control.leanstates its controls as named theorems and checks each one's axioms. It shows that ℝ² has a valuation, so d ≥ 3 is needed; that the theorem applies in ℝ³ and in ℂ³; and that the checker rejects a refutation with no case split. Last, a canary confirms that the axiom audit detectssorry.- CI also rebuilds the library and
Solutionwith warnings as errors, and regenerates the Peres data and requires it to match. scripts/verify-comparator.shrunslake comparatorin CI after a push, sandboxed on Linux. It checks the Challenge against the proof and replays the proof through Lean's kernel, nanoda and con-ron. I have run it locally on Windows, unsandboxed (--inadvisably-no-sandbox, with full paths to the nanoda and con-ron binaries in a copy ofcomparator.json), on 24 November 2026: it ended "Your solution is okay!", with con-ron, nanoda and Lean's kernel all accepting the proof. An unsandboxed run is not the trusted check; the sandboxed Linux run in CI is.
Building
Lean v4.35.0-rc3 and Mathlib 5e0c4e5239cb0a2d86d68a884bf52cfd963fce22.
lake exe cache get
lake build
lake env lean audit/Axioms.lean
lake env lean audit/Control.lean
bash scripts/assemble-peres33.sh && git diff --exit-code KochenSpecker/Peres33.lean
python3 scripts/check-lean-sources.py
ruby scripts/validate-formalization.rb
How it was made
I drafted the Lean with Claude Code.
Licence
Apache 2.0.