AFTD Auto-Formalizing Theoretical Domains

joint_perfect_code_min_dist

theoremverifiedformalization

Given implicit parameters n, q, c, and c', if C : Finset (Fin n → Fin q) is a (c, c') joint perfect code with 2 ≤ C.card, then code_min_dist C (2 * c + c' + 1), and if c' = 0 then 2 * c + c' + 1 is odd.

Statement

theorem joint_perfect_code_min_dist {n q c c' : ℕ}
    (C : Finset (Fin n → Fin q)) (hC : is_joint_perfect_code c c' C) (hcard : 2 ≤ C.card) :
    code_min_dist C (2 * c + c' + 1) ∧ (c' = 0 → Odd (2 * c + c' + 1))
Formalizes
arXiv:2611.18156, Proposition IV.2
Verified
23 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 7
  • 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…
  • is_joint_perfect_codeA code C ⊆ (Fin n → Fin q) is a (c, c') joint perfect code if it is a (c, c') joint error-correcting and detecting code and satisfies |C|…
  • joint_code_dist_geIf C ⊆ Fin n → Fin q is a (c, c') joint error-correcting and detecting code, then any two distinct codewords x₁, x₂ ∈ C have Hamming…
  • joint_perfect_code_partitionIf C ⊆ Fin n → Fin q is a (c, c') joint perfect code, then for any fixed codeword x₀ ∈ C, every word y : Fin n → Fin q either belongs to…
  • hamming_ballGiven implicit parameters n and q, the Hamming ball hamming_ball r x of radius r centered at x : Fin n → Fin q is the finite set…
  • is_joint_error_correcting_detectingGiven implicit parameters n and q, a code C : Finset (Fin n → Fin q) is a (c, c') joint error-correcting and detecting code if distinct…
  • hamming_ringGiven implicit parameters n and q, the (c, c') Hamming ring hamming_ring c c' x centered at x : Fin n → Fin q is the finite set (Finset)…

Read back from the Lean

Let $n, q, c, c'$ be natural numbers. Let $C$ be a finite set of words of length $n$ over an alphabet of size $q$ (functions Fin n → Fin q). Assume: 1. hC: $C$ is a "joint perfect code" with parameters $c, c'$, meaning: (a) For all distinct codewords $x_1, x_2 \in C$, the Hamming ball of radius $c$ around $x_1$ and the Hamming ball of radius $c$ around $x_2$ are disjoint; (b) For all codewords $x_1, x_2 \in C$, the Hamming ball of radius $c$ around $x_1$ and the Hamming ring with inner radius $c + 1$ and outer radius $c + c'$ around $x_2$ are disjoint; and (c) The size of $C$ times the volume of the Hamming ball of radius $c$ ($\sum_{i=0}^c \binom{n}{i}(q-1)^i$), plus the volume of a single Hamming ring from radius $c+1$ to $c+c'$ ($\sum_{j=c+1}^{c+c'} \binom{n}{j}(q-1)^j$), equals $q^n$. 2. hcard: $C$ contains at least 2 codewords ($2 \le |C|$). Then: 1. The minimum Hamming distance of $C$ is exactly $2c + c' + 1$ (there exist distinct $x, y \in C$ with Hamming distance $2c + c' + 1$, and all distinct $x, y \in C$ have Hamming distance at least $2c + c' + 1$); and 2. If $c' = 0$, then $2c + c' + 1$ is odd.

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 determine the minimum distance of a non-trivial (c, c')-joint perfect code C, we choose a codeword x₀ ∈ C and a closest distinct codeword x' ∈ C. The lower bound d(x₀, x') ≥ 2c + c' + 1 follows directly from the code's error-correction and detection capability. For the upper bound, assuming d(x₀, x') > 2c + c' + 1, we construct an auxiliary word y by moving c + c' + 1 steps from x₀ toward x' along their differing coordinates. This word y is at distance at least c + 1 from all codewords and at distance c + c' + 1 > c + c' from x₀, contradicting the sphere-and-ring partition property of joint perfect codes. Thus d(x₀, x') = 2c + c' + 1, which is the code's minimum distance, and this value is odd when c' = 0.

  1. The code C is a (c, c')-joint error-correcting and detecting code.

    A joint perfect code is defined as a joint error-correcting and detecting code satisfying a sphere-packing cardinality equality. We extract the error-correcting and detecting property directly from the first component of the assumption that C is joint perfect.

    line 5

      have hed : is_joint_error_correcting_detecting c c' C := hC.1
  2. There exist two distinct codewords x₀, x' ∈ C such that x' minimizes the Hamming distance to x₀ among all codewords in C \ {x₀}.

    Because |C| ≥ 2 > 1, the code contains at least two distinct codewords x₀ and x₁. Consequently, C \ {x₀} is nonempty, so by Finset.exists_min_image, the distance function z ↦ d(x₀, z) attains its minimum on C \ {x₀} at some codeword x' distinct from x₀.

    lines 6–10

    uses Finset.one_lt_card.mp, Finset.mem_erase.mpr, Finset.exists_min_image, Finset.mem_of_mem_erase, Finset.mem_erase.mp

      obtain ⟨x₀, hx₀, x₁, hx₁, hne⟩ := Finset.one_lt_card.mp (by omega : 1 < C.card)
      have hTne : (C.erase x₀).Nonempty := ⟨x₁, Finset.mem_erase.mpr ⟨hne.symm, hx₁⟩⟩
      obtain ⟨x', hx'T, hmin⟩ := Finset.exists_min_image (C.erase x₀) (fun z => hammingDist x₀ z) hTne
      have hx'C : x' ∈ C := Finset.mem_of_mem_erase hx'T
      have hx'ne : x' ≠ x₀ := (Finset.mem_erase.mp hx'T).1
  3. The distance between x₀ and x' satisfies d(x₀, x') ≥ 2c + c' + 1.

    By joint_code_dist_ge, any two distinct codewords in a (c, c')-joint error-correcting and detecting code have Hamming distance at least 2c + c' + 1. Applying this to the distinct codewords x₀ and x' gives the lower bound.

    line 11

    uses joint_code_dist_ge

      have hDge : 2 * c + c' + 1 ≤ hammingDist x₀ x' := joint_code_dist_ge C hed hx₀ hx'C hx'ne.symm
  4. Assuming for contradiction that d(x₀, x') > 2c + c' + 1, we construct an auxiliary word y by modifying x₀ toward x' on a subset S of c + c' + 1 differing coordinates.

    We assume for contradiction that d(x₀, x') > 2c + c' + 1. Let Dset be the set of coordinates where x₀ and x' differ, so |Dset| = d(x₀, x') > 2c + c' + 1 ≥ c + c' + 1. Using Finset.exists_subset_card_eq, we select a subset S ⊆ Dset of exact size c + c' + 1. We then define a word y by setting y(i) = x'(i) for i ∈ S and y(i) = x₀(i) for i ∉ S.

    lines 12–19

    uses Finset.exists_subset_card_eq

      have hDle : hammingDist x₀ x' ≤ 2 * c + c' + 1 := by
        by_contra hcon
        have hcon' : 2 * c + c' + 1 < hammingDist x₀ x' := by omega
        set Dset := Finset.univ.filter (fun i => x₀ i ≠ x' i) with hDsetdef
        have hDsetcard : Dset.card = hammingDist x₀ x' := rfl
        have hle : c + c' + 1 ≤ Dset.card := by omega
        obtain ⟨S, hSsub, hScard⟩ := Finset.exists_subset_card_eq (s := Dset) (n := c + c' + 1) hle
        set y : Fin n → Fin q := fun i => if i ∈ S then x' i else x₀ i with hydef
  5. The Hamming distance between x₀ and y is exactly c + c' + 1.

    The coordinates where x₀ and y differ are precisely the indices in S: for i ∉ S, y(i) = x₀(i), while for i ∈ S, y(i) = x'(i) ≠ x₀(i) because S ⊆ Dset. Therefore, the set of differing coordinates is S, whose cardinality is |S| = c + c' + 1.

    lines 20–32

    uses Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_filter.mp

        have hdy : hammingDist x₀ y = c + c' + 1 := by
          have hfilter : Finset.univ.filter (fun i => x₀ i ≠ y i) = S := by
            ext i
            simp only [Finset.mem_filter, Finset.mem_univ, true_and]
            constructor
            · intro hi
              by_contra hni
              exact hi (by simp [hydef, hni])
            · intro hi
              have hxi : x₀ i ≠ x' i := (Finset.mem_filter.mp (hSsub hi)).2
              simpa [hydef, hi] using hxi
          show (Finset.univ.filter (fun i => x₀ i ≠ y i)).card = c + c' + 1
          rw [hfilter, hScard]
  6. Every codeword x ∈ C is at Hamming distance at least c + 1 from y.

    If x = x₀, then d(x₀, y) = c + c' + 1 ≥ c + 1. If x ≠ x₀, then x ∈ C \ {x₀}, so d(x₀, x') ≤ d(x₀, x) by the minimality of x'. By the triangle inequality, d(x₀, x) ≤ d(x₀, y) + d(y, x) = c + c' + 1 + d(x, y). Since d(x₀, x) ≥ d(x₀, x') > 2c + c' + 1, linear arithmetic yields d(x, y) ≥ c + 1.

    lines 33–43

    uses Finset.mem_erase.mpr, hammingDist_triangle, hammingDist_comm

        have hfar : ∀ x ∈ C, c + 1 ≤ hammingDist x y := by
          intro x hxC
          by_cases hxx : x = x₀
          · subst hxx
            rw [hdy]; omega
          · have hxT : x ∈ C.erase x₀ := Finset.mem_erase.mpr ⟨hxx, hxC⟩
            have hDmin : hammingDist x₀ x' ≤ hammingDist x₀ x := hmin x hxT
            have htri : hammingDist x₀ x ≤ hammingDist x₀ y + hammingDist y x :=
              hammingDist_triangle x₀ y x
            rw [hdy, hammingDist_comm y x] at htri
            omega
  7. The distance between x₀ and x' satisfies d(x₀, x') ≤ 2c + c' + 1.

    By the partition theorem joint_perfect_code_partition, every word in the space must lie either in the closed ball B(x, c) for some codeword x ∈ C, or in the detection ring R(x₀, c, c') around x₀. If y ∈ B(x, c), then d(x, y) ≤ c, contradicting d(x, y) ≥ c + 1. If y ∈ R(x₀, c, c'), then d(x₀, y) ≤ c + c', contradicting d(x₀, y) = c + c' + 1. Both alternatives are impossible, refuting the assumption d(x₀, x') > 2c + c' + 1 and establishing d(x₀, x') ≤ 2c + c' + 1.

    lines 44–53

    uses joint_perfect_code_partition, Finset.mem_filter, Finset.mem_univ, true_and

        have hpart := joint_perfect_code_partition C hC hx₀ y
        rcases hpart with ⟨x, hxC, hball⟩ | hring
        · have hd : hammingDist x y ≤ c := by simpa [hamming_ball] using hball
          have := hfar x hxC
          omega
        · have hd2 : hammingDist x₀ y ≤ c + c' := by
            have h := hring
            simp only [hamming_ring, Finset.mem_filter, Finset.mem_univ, true_and] at h
            exact h.2
          omega
  8. The code C has minimum distance 2c + c' + 1.

    The definition code_min_dist requires exhibiting two distinct codewords at distance 2c + c' + 1 and showing that no distinct codewords have smaller distance. For the first condition, x₀, x' ∈ C are distinct and satisfy 2c + c' + 1 ≤ d(x₀, x') ≤ 2c + c' + 1, hence d(x₀, x') = 2c + c' + 1. For the second condition, any distinct x, y ∈ C satisfy d(x, y) ≥ 2c + c' + 1 by joint_code_dist_ge.

    lines 54–58

    uses joint_code_dist_ge

      refine ⟨?_, ?_⟩
      · refine ⟨⟨x₀, hx₀, x', hx'C, hx'ne.symm, ?_⟩, ?_⟩
        · omega
        · intro x hx y hy hxy
          exact joint_code_dist_ge C hed hx hy hxy
  9. If c' = 0, then the minimum distance 2c + c' + 1 is odd.

    When c' = 0, the minimum distance simplifies to 2c + 0 + 1 = 2c + 1, which is odd by definition with witness c.

    lines 59–61

      · intro hc'
        subst hc'
        exact ⟨c, by omega⟩
Finset.one_lt_card.mp
A finite set has cardinality greater than 1 if and only if it contains two distinct elements (forward implication).
Finset.mem_erase.mpr
An element a belongs to s \ {b} if a ≠ b and a ∈ s.
Finset.exists_min_image
A nonempty finite set contains an element that minimizes a given function into a linearly ordered codomain.
Finset.mem_of_mem_erase
If an element belongs to s \ {a}, then it belongs to s.
Finset.mem_erase.mp
If an element belongs to s \ {a}, then it is distinct from a and belongs to s.
joint_code_dist_ge
Distinct codewords in a (c, c')-joint error-correcting and detecting code have Hamming distance at least 2c + c' + 1.
Finset.exists_subset_card_eq
A finite set of cardinality at least k has a subset of cardinality exactly k.
Finset.mem_filter
An element belongs to a filtered finset if and only if it belongs to the base finset and satisfies the predicate.
Finset.mem_univ
Every element of a finite type belongs to the universal finset Finset.univ.
true_and
The logical identity simplifying True ∧ p to p.
Finset.mem_filter.mp
If an element belongs to a filtered finset, then it satisfies the predicate (forward direction).
hammingDist_triangle
Hamming distance satisfies the triangle inequality d(x, z) ≤ d(x, y) + d(y, z).
hammingDist_comm
Hamming distance is symmetric: d(x, y) = d(y, x).
joint_perfect_code_partition
In a (c, c')-joint perfect code, any word belongs either to the closed Hamming ball of radius c around some codeword or to the detection ring of radii between c + 1 and c + c' around x₀.

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

/-- A non-trivial (c, c') joint perfect code has minimum distance 2c + c' + 1; when c' = 0, this distance is odd. -/theorem joint_perfect_code_min_dist {n q c c' : ℕ}    (C : Finset (Fin n → Fin q)) (hC : is_joint_perfect_code c c' C) (hcard : 2 ≤ C.card) :    code_min_dist C (2 * c + c' + 1) ∧ (c' = 0 → Odd (2 * c + c' + 1)) := by  have hed : is_joint_error_correcting_detecting c c' C := hC.1  obtain ⟨x₀, hx₀, x₁, hx₁, hne⟩ := Finset.one_lt_card.mp (by omega : 1 < C.card)  have hTne : (C.erase x₀).Nonempty := ⟨x₁, Finset.mem_erase.mpr ⟨hne.symm, hx₁⟩⟩  obtain ⟨x', hx'T, hmin⟩ := Finset.exists_min_image (C.erase x₀) (fun z => hammingDist x₀ z) hTne  have hx'C : x' ∈ C := Finset.mem_of_mem_erase hx'T  have hx'ne : x' ≠ x₀ := (Finset.mem_erase.mp hx'T).1  have hDge : 2 * c + c' + 1 ≤ hammingDist x₀ x' := joint_code_dist_ge C hed hx₀ hx'C hx'ne.symm  have hDle : hammingDist x₀ x' ≤ 2 * c + c' + 1 := by    by_contra hcon    have hcon' : 2 * c + c' + 1 < hammingDist x₀ x' := by omega    set Dset := Finset.univ.filter (fun i => x₀ i ≠ x' i) with hDsetdef    have hDsetcard : Dset.card = hammingDist x₀ x' := rfl    have hle : c + c' + 1 ≤ Dset.card := by omega    obtain ⟨S, hSsub, hScard⟩ := Finset.exists_subset_card_eq (s := Dset) (n := c + c' + 1) hle    set y : Fin n → Fin q := fun i => if i ∈ S then x' i else x₀ i with hydef    have hdy : hammingDist x₀ y = c + c' + 1 := by      have hfilter : Finset.univ.filter (fun i => x₀ i ≠ y i) = S := by        ext i        simp only [Finset.mem_filter, Finset.mem_univ, true_and]        constructor        · intro hi          by_contra hni          exact hi (by simp [hydef, hni])        · intro hi          have hxi : x₀ i ≠ x' i := (Finset.mem_filter.mp (hSsub hi)).2          simpa [hydef, hi] using hxi      show (Finset.univ.filter (fun i => x₀ i ≠ y i)).card = c + c' + 1      rw [hfilter, hScard]    have hfar : ∀ x ∈ C, c + 1 ≤ hammingDist x y := by      intro x hxC      by_cases hxx : x = x₀      · subst hxx        rw [hdy]; omega      · have hxT : x ∈ C.erase x₀ := Finset.mem_erase.mpr ⟨hxx, hxC⟩        have hDmin : hammingDist x₀ x' ≤ hammingDist x₀ x := hmin x hxT        have htri : hammingDist x₀ x ≤ hammingDist x₀ y + hammingDist y x :=          hammingDist_triangle x₀ y x        rw [hdy, hammingDist_comm y x] at htri        omega    have hpart := joint_perfect_code_partition C hC hx₀ y    rcases hpart with ⟨x, hxC, hball⟩ | hring    · have hd : hammingDist x y ≤ c := by simpa [hamming_ball] using hball      have := hfar x hxC      omega    · have hd2 : hammingDist x₀ y ≤ c + c' := by        have h := hring        simp only [hamming_ring, Finset.mem_filter, Finset.mem_univ, true_and] at h        exact h.2      omega  refine ⟨?_, ?_⟩  · refine ⟨⟨x₀, hx₀, x', hx'C, hx'ne.symm, ?_⟩, ?_⟩    · omega    · intro x hx y hy hxy      exact joint_code_dist_ge C hed hx hy hxy  · intro hc'    subst hc'    exact ⟨c, by omega⟩
See it in the graphDiscuss this resultChallenge it