[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"4mnvytpfiE":3},"# The Kochen–Specker theorem, in Lean\n\n**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.\n\nIn 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.\n\nThis repository proves it in Lean 4, for every d ≥ 3 and every `RCLike` field `𝕜` (so ℝ and ℂ):\n\n```lean\ntheorem KochenSpecker.kochen_specker {𝕜 : Type*} [RCLike 𝕜] {d : ℕ} (hd : 3 ≤ d) :\n    ¬ ∃ v : EuclideanSpace 𝕜 (Fin d) → Bool,\n      ∀ b : OrthonormalBasis (Fin d) 𝕜 (EuclideanSpace 𝕜 (Fin d)), ∃! i, v (b i) = true\n```\n\nThe 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`).\n\nEarlier 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.\n\n## The proof\n\n1. **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.\n2. **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`).\n3. **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`).\n\nI haven't read the three papers, only their bibliographic records; the proofs here don't follow their text.\n\n## Related work\n\n- **ℝ³ in Lean:** [guptbot/free_will_lean](https://github.com/guptbot/free_will_lean/blob/643be996b47d7ecae68e39627f24090adea36efa/FreeWillLean/KochenSpecker.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.\n- **ℂ⁴ in Lean:** [spitters/catcrypt-compiler](https://github.com/spitters/catcrypt-compiler/blob/56d69d0d4add3d289d755dec1861a3b98f56debf/CatCrypt/Quantum/KochenSpecker.lean) (November 2026) proves `kochenSpecker_complex`, the theorem in ℂ⁴ from Cabello's 18 vectors.\n- **ℝ³ in HOL Light:** John Harrison's [`KOCHEN_SPECKER_LEMMA`](https://github.com/jrh13/hol-light/blob/1a0c0e6b182cd9e271946ba8d78f7942619f4acb/Tutorial/Custom_tactics.ml) proves the theorem in ℝ³ from Peres's configuration with exact coordinates and an automated case analysis, the same architecture as dimension three here.\n- **Kochen–Specker sets in ℂ³:** [michaelkernaghan/contextuality](https://github.com/michaelkernaghan/contextuality/tree/ea5d9972a2e29b23cedfb4bd31446ad761aedd52), 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.\n- **My own [csd-lean4](https://github.com/zblore/csd-lean4/blob/dd25029e5cd706cfa7d229135a105feb220d6632/CsdLean4/Empirical/QM/Contextuality/KS18.lean)** 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.\n- [Qulinecier/KS_SAT](https://github.com/Qulinecier/KS_SAT/tree/144b039274c47babb2a6012039bf4d27048ae507) has lemmas about a SAT encoding of Kochen–Specker colourings in dimension three.\n- 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.\n\n## Files\n\n- `Challenge.lean` states the theorem, ending in `sorry`. It imports only Mathlib.\n- `KochenSpecker/Checker.lean`: the case-split checker and its soundness.\n- `KochenSpecker/Valuation.lean`: valuations, the basis lemmas, and the step from dimension d + 1 to d.\n- `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`.\n- `KochenSpecker/Cabello18.lean`: dimension four by parity.\n- `KochenSpecker/Main.lean`: the theorem; `Solution.lean` re-exports it.\n\n## Checks\n\n- `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.\n- `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`.\n- CI also rebuilds the library and `Solution` with warnings as errors, and regenerates the Peres data and requires it to match.\n- `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.\n\n## Building\n\nLean `v4.35.0-rc3` and Mathlib `5e0c4e5239cb0a2d86d68a884bf52cfd963fce22`.\n\n```\nlake exe cache get\nlake build\nlake env lean audit/Axioms.lean\nlake env lean audit/Control.lean\nbash scripts/assemble-peres33.sh && git diff --exit-code KochenSpecker/Peres33.lean\npython3 scripts/check-lean-sources.py\nruby scripts/validate-formalization.rb\n```\n\n## How it was made\n\nI drafted the Lean with Claude Code.\n\n## Licence\n\nApache 2.0.\n",1795981101073]