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

  1. 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.py finds a case-split refutation of every colouring, an 895-node tree, and KochenSpecker/Checker.lean checks it in the Lean kernel with decide, 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.
  2. Every larger dimension. A valuation v in 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, so v restricted to u⊥ is a valuation in dimension d. Hence the theorem in dimension d gives it in dimension d + 1 (KochenSpecker/Valuation.lean).
  3. 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_specker for 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_LEMMA proves 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, and six_divides_iff_ks_uncolorable: the pool of 0 and the n-th roots of unity is uncolourable exactly when 6 divides n. Its lean-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 as lean-ks/, a case-split tree over a list-based partial assignment checked with decide, 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.lean states the theorem, ending in sorry. 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 from KochenSpecker/Peres33.lean.in by scripts/assemble-peres33.sh, which runs scripts/peres33.py.
  • KochenSpecker/Cabello18.lean: dimension four by parity.
  • KochenSpecker/Main.lean: the theorem; Solution.lean re-exports it.

Checks

  • audit/Axioms.lean uses #guard_msgs to require that the theorem, its general form, both base cases and the second route use only propext, Classical.choice and Quot.sound. The dimension-three certificate is checked with decide, not native_decide, so it adds no axiom.
  • audit/Control.lean states 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 detects sorry.
  • CI also rebuilds the library and Solution with warnings as errors, and regenerates the Peres data and requires it to match.
  • scripts/verify-comparator.sh runs lake comparator in 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 of comparator.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.