code_min_dist_pair
Given implicit parameters n and q, for any two words x, y : Fin n → Fin q with x ≠ y, the two-element code {x, y} : Finset (Fin n → Fin q) satisfies code_min_dist {x, y} (hammingDist x y).
Statement
lemma code_min_dist_pair {n q : ℕ} (x y : Fin n → Fin q) (hne : x ≠ y) :
code_min_dist {x, y} (hammingDist x y)- Source
- folklore
- Verified
- 23 Nov 2026
- Axioms
Classical.choiceQuot.soundpropext- Built on 1
- code_min_distGiven implicit parameters n and q, for a code C : Finset (Fin n → Fin q) and natural number d, code_min_dist C d holds if there exist…
Read back from the Lean
For all natural numbers $n$ and $q$, and all functions $x, y : \mathrm{Fin}\ n \to \mathrm{Fin}\ q$, if $x \neq y$, then the code $\{x, y\}$ has minimum distance $\mathrm{hammingDist}(x, y)$ (in the sense of code_min_dist).
Written by a model that saw only the Lean, never the English above. If the two disagree, that is worth a challenge.
The proof, explained
To show that the two-element code $\{x, y\}$ has minimum distance $\operatorname{hammingDist}(x, y)$, we must verify that this distance is attained by a pair of distinct codewords and that every pair of distinct codewords has distance at least $\operatorname{hammingDist}(x, y)$. The distance is attained directly by the pair $(x, y)$. For the lower bound, any two distinct codewords from $\{x, y\}$ must be either $(x, y)$ or $(y, x)$, and in both cases their Hamming distance equals $\operatorname{hammingDist}(x, y)$ by symmetry.
The distance $\operatorname{hammingDist}(x, y)$ is attained by two distinct codewords in $\{x, y\}$, leaving only to verify that no pair of distinct codewords has smaller distance.
Unfolding the definition of `code_min_dist` requires showing two things: that $\operatorname{hammingDist}(x, y)$ is realized by two distinct codewords in $\{x, y\}$, and that every pair of distinct codewords has distance at least $\operatorname{hammingDist}(x, y)$. We provide $x$ and $y$ as the witnesses: both belong to the set $\{x, y\}$, they are distinct by assumption ($x \neq y$), and their distance is trivially $\operatorname{hammingDist}(x, y)$.
dsimp [code_min_dist] refine ⟨⟨x, by simp, y, by simp, hne, rfl⟩, ?_⟩
For any pair of distinct codewords $a, b \in \{x, y\}$, both $a$ and $b$ must equal either $x$ or $y$.
We introduce arbitrary codewords $a, b \in \{x, y\}$ along with the hypothesis that $a \neq b$. Characterizing membership in the two-element set $\{x, y\}$ via `Finset.mem_insert` and `Finset.mem_singleton` decomposes the membership conditions into $a = x \lor a = y$ and $b = x \lor b = y$.
uses
Finset.mem_insert,Finset.mem_singletonintro a ha b hb hab simp only [Finset.mem_insert, Finset.mem_singleton] at ha hb
Every pair of distinct codewords $a, b \in \{x, y\}$ satisfies $\operatorname{hammingDist}(x, y) \le \operatorname{hammingDist}(a, b)$.
We perform a case split over the four possible combinations of values for $a$ and $b$: 1. If $a = x$ and $b = x$, or $a = y$ and $b = y$, substituting these equalities yields $a = b$, contradicting the assumption $a \neq b$. 2. If $a = x$ and $b = y$, the inequality reduces to $\operatorname{hammingDist}(x, y) \le \operatorname{hammingDist}(x, y)$, which holds by reflexivity (`le_rfl`). 3. If $a = y$ and $b = x$, the inequality is $\operatorname{hammingDist}(x, y) \le \operatorname{hammingDist}(y, x)$. Rewriting with the symmetry of the Hamming distance (`hammingDist_comm`) makes the two sides identical.
uses
le_rfl,hammingDist_commrcases ha with ha | ha <;> rcases hb with hb | hb · subst ha hb; contradiction · subst ha hb; exact le_rfl · subst ha hb; rw [hammingDist_comm] · subst ha hb; contradiction
Finset.mem_insert- An element belongs to `insert b s` if and only if it equals `b` or belongs to `s`.
Finset.mem_singleton- An element belongs to the singleton set `{a}` if and only if it equals `a`.
le_rfl- The preorder relation is reflexive: any element is less than or equal to itself ($a \le a$).
hammingDist_comm- The Hamming distance is symmetric: $\operatorname{hammingDist}(x, y) = \operatorname{hammingDist}(y, x)$.
- On line 11, rewriting by `hammingDist_comm` leaves the goal `hammingDist a b ≤ hammingDist a b`, which the `rw` tactic automatically closes using reflexivity.
Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.
Lean source view module on GitHub
Choose a tactic — a marked line — to see the goals Lean had before and after it, as in Lean’s infoview. Step with ← → or j k.
/-- A two-element code {x, y} with x ≠ y has minimum distance equal to the Hamming distance between x and y. -/lemma code_min_dist_pair {n q : ℕ} (x y : Fin n → Fin q) (hne : x ≠ y) : code_min_dist {x, y} (hammingDist x y) := by dsimp [code_min_dist] refine ⟨⟨x, by simp, y, by simp, hne, rfl⟩, ?_⟩ intro a ha b hb hab simp only [Finset.mem_insert, Finset.mem_singleton] at ha hb rcases ha with ha | ha <;> rcases hb with hb | hb · subst ha hb; contradiction · subst ha hb; exact le_rfl · subst ha hb; rw [hammingDist_comm] · subst ha hb; contradiction