AFTD Auto-Formalizing Theoretical Domains

joint_perfect_code_partition

theoremverified

If 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 the radius-c Hamming ball around some codeword x ∈ C or belongs to the (c, c') Hamming ring centered at x₀.

Statement

lemma joint_perfect_code_partition {n q c c' : ℕ}
    (C : Finset (Fin n → Fin q)) (hC : is_joint_perfect_code c c' C)
    {x₀ : Fin n → Fin q} (hx₀ : x₀ ∈ C) (y : Fin n → Fin q) :
    (∃ x ∈ C, y ∈ hamming_ball c x) ∨ y ∈ hamming_ring c c' x₀
Source
arXiv:2611.18156, Lemma IV.1
Verified
23 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 6
  • 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|…
  • 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…
  • 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)…
  • 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…
  • card_hamming_ringFor natural numbers n, q, c, c' with 2 ≤ q and c + c' ≤ n, and any x : Fin n → Fin q, the cardinality of the (c, c') Hamming ring centered…
  • card_hamming_ballFor natural numbers n, q, r with 2 ≤ q and r ≤ n, and any x : Fin n → Fin q, the cardinality of the Hamming ball of radius r centered at x…
Used by 1
  • joint_perfect_code_min_distGiven 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…

Read back from the Lean

Let $n, q, c, c' \in \mathbb{N}$. Let $C$ be a finite set of words of length $n$ over an alphabet of size $q$ (i.e., elements of Fin n → Fin q). Assume that $C$ is a joint perfect code with parameters $c$ and $c'$ (is_joint_perfect_code c c' C). Then, for any codeword $x₀ \in C$ and for any word $y : \text{Fin } n \to \text{Fin } q$, either there exists some codeword $x \in C$ such that $y$ is in the Hamming ball of radius $c$ centered at $x$, or $y$ is in the Hamming ring with inner radius $c + 1$ and outer radius $c + c'$ centered at $x₀$.

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

A (c, c') joint perfect code C partitions the space of words into radius-c Hamming balls around codewords and a (c, c') Hamming ring around any chosen codeword x₀ ∈ C. After dispensing with the degenerate cases n ≤ c and q ≤ 1 where the balls already cover the whole space, the argument splits on whether c + c' ≤ n. In the main case c + c' ≤ n, the volume-counting formula in the definition of a joint perfect code ensures that the disjoint union of the balls and the ring has cardinality exactly qⁿ, hence covers the entire space. In the complementary case n < c + c', the ring around x₀ coincides with the complement of the ball B_c(x₀), and a counting bound forces C = {x₀}, so every word trivially lies either in B_c(x₀) or in its complement ring.

  1. We extract the geometric packing and volume-counting hypotheses defining the joint perfect code C.

    A (c, c') joint perfect code is by definition a joint error-correcting and detecting code satisfying the sphere-packing condition. We unpack this into three hypotheses: pairwise disjointness of radius-c balls around distinct codewords (hdisj), disjointness of every radius-c codeword ball from the (c, c') ring around any codeword (hdisj_ring), and the cardinality equation equating the sum of the volumes of |C| balls and one ring to the full space size qⁿ (hcount).

    lines 6–8

      classical
      obtain ⟨hECD, hcount⟩ := hC
      obtain ⟨hdisj, hdisj_ring⟩ := hECD
  2. If n ≤ c, every word y belongs to the radius-c Hamming ball around x₀.

    The Hamming distance between any two words of length n is at most n (by hammingDist_le_card_fintype). When n ≤ c, we have d(x₀, y) ≤ n ≤ c by transitivity (le_trans), so y belongs to the ball B_c(x₀) around the codeword x₀ ∈ C, satisfying the first alternative of the disjunction via Or.inl.

    lines 9–12

    uses Or.inl, Finset.mem_filter, Finset.mem_univ, true_and, le_trans, hammingDist_le_card_fintype

      by_cases hcn : n ≤ c
      · refine Or.inl ⟨x₀, hx₀, ?_⟩
        simp only [hamming_ball, Finset.mem_filter, Finset.mem_univ, true_and]
        exact le_trans (by simpa using hammingDist_le_card_fintype (x := x₀) (y := y)) hcn
  3. If q ≤ 1, every word y belongs to the radius-c Hamming ball around x₀.

    When q ≤ 1, the alphabet Fin q has cardinality at most 1, making it a subsingleton (Fintype.card_le_one_iff_subsingleton). The entire word space (Fin n → Fin q) therefore has at most one element, forcing y = x₀ (Subsingleton.elim). Their Hamming distance is 0 (hammingDist_eq_zero.mpr), which is at most c (Nat.zero_le), so y ∈ B_c(x₀).

    lines 13–21

    uses Fintype.card_le_one_iff_subsingleton, Fintype.card_fin, Or.inl, Finset.mem_filter, Finset.mem_univ, true_and, hammingDist_eq_zero.mpr, Subsingleton.elim, Nat.zero_le

      by_cases hq1 : q ≤ 1
      · have hsub : Subsingleton (Fin q) := by
          rw [← Fintype.card_le_one_iff_subsingleton, Fintype.card_fin]
          exact hq1
        haveI := hsub
        refine Or.inl ⟨x₀, hx₀, ?_⟩
        simp only [hamming_ball, Finset.mem_filter, Finset.mem_univ, true_and]
        rw [hammingDist_eq_zero.mpr (Subsingleton.elim x₀ y)]
        exact Nat.zero_le c
  4. We establish the non-trivial parameter bounds c < n, c ≤ n, and 2 ≤ q.

    Having eliminated the degenerate cases n ≤ c and q ≤ 1, their negations provide c < n (and hence c ≤ n by le_of_lt) and q > 1 (and hence 2 ≤ q by integer arithmetic).

    lines 22–24

    uses not_le.mp, le_of_lt

      have hcn' : c < n := not_le.mp hcn
      have hq2 : 2 ≤ q := by have := not_le.mp hq1; omega
      have hc_le : c ≤ n := le_of_lt hcn'
  5. Assuming c + c' ≤ n, the union of balls ⋃_{x ∈ C} B_c(x) and the ring around x₀ are disjoint, with their cardinalities given by standard binomial coefficient formulas.

    Because 2 ≤ q, c ≤ n, and c + c' ≤ n, the library theorems card_hamming_ball and card_hamming_ring express the size of each ball B_c(x) and the ring around x₀ as sums of binomial terms. Since the codeword balls are pairwise disjoint, the size of their union is |C| times the single-ball volume (Finset.card_biUnion). Moreover, each ball B_c(x) is disjoint from the ring around x₀ by hdisj_ring, so the entire union of balls is disjoint from the ring (Finset.disjoint_biUnion_left).

    lines 25–38

    uses card_hamming_ball, card_hamming_ring, Finset.card_biUnion, Finset.sum_congr, Finset.sum_const, Nat.nsmul_eq_mul, Finset.disjoint_biUnion_left

      by_cases hcc' : c + c' ≤ n
      · have hball_c : ∀ x : Fin n → Fin q,
            (hamming_ball c x).card = ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i :=
          fun x => card_hamming_ball n q c hq2 hc_le x
        have hring : (hamming_ring c c' x₀).card =
            ∑ j ∈ Finset.Icc (c + 1) (c + c'), n.choose j * (q - 1) ^ j :=
          card_hamming_ring n q c c' hq2 hcc' x₀
        have hcardA : (C.biUnion (fun x => hamming_ball c x)).card =
            C.card * ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by
          rw [Finset.card_biUnion hdisj]
          rw [Finset.sum_congr rfl (fun x _ => hball_c x), Finset.sum_const, Nat.nsmul_eq_mul]
        have hdisjAB : Disjoint (C.biUnion (fun x => hamming_ball c x)) (hamming_ring c c' x₀) := by
          rw [Finset.disjoint_biUnion_left]
          exact fun x hx => hdisj_ring x hx x₀ hx₀
  6. When c + c' ≤ n, the union of all codeword balls and the ring around x₀ is the entire space, so y belongs to one of them.

    Because the union of codeword balls and the ring around x₀ are disjoint, the cardinality of their union is the sum of |C| ball volumes and one ring volume (Finset.card_union_of_disjoint). By the joint perfect code condition hcount, this sum equals qⁿ, the cardinality of the full space of words. Any subset of a finite type with the same cardinality must be the entire universal finset (Finset.eq_univ_of_card). Therefore, y belongs to this union, meaning y is in B_c(x) for some codeword x ∈ C or y is in the ring around x₀.

    lines 39–48

    uses Finset.card_union_of_disjoint, Finset.eq_univ_of_card, Finset.mem_univ, Finset.mem_union, Finset.mem_biUnion

        have hUcard : (C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀).card =
            Fintype.card (Fin n → Fin q) := by
          rw [Finset.card_union_of_disjoint hdisjAB, hcardA, hring, hcount]
          simp
        have hUuniv : C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀ = Finset.univ :=
          Finset.eq_univ_of_card _ hUcard
        have hy : y ∈ C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀ := by
          rw [hUuniv]; exact Finset.mem_univ y
        rw [Finset.mem_union, Finset.mem_biUnion] at hy
        exact hy
  7. When n < c + c', the ring around x₀ is the complete complement of the ball B_c(x₀), with cardinality qⁿ - |B_c(x₀)|.

    The distance from x₀ to any word z is at most n. Since n < c + c', the upper distance bound d(x₀, z) ≤ c + c' holds automatically for all words. Thus, z belongs to the ring if and only if c + 1 ≤ d(x₀, z), which is equivalent to z ∉ B_c(x₀). Hence the ring is precisely the set difference Finset.univ \ B_c(x₀), and its cardinality is qⁿ - |B_c(x₀)| (Finset.card_sdiff).

    lines 49–66

    uses not_le.mp, card_hamming_ball, hammingDist_le_card_fintype, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_sdiff, Finset.card_sdiff, Finset.inter_univ

      · have hcc'' : n < c + c' := not_le.mp hcc'
        have hball_c : ∀ x : Fin n → Fin q,
            (hamming_ball c x).card = ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i :=
          fun x => card_hamming_ball n q c hq2 hc_le x
        have hdist_le : ∀ z : Fin n → Fin q, hammingDist x₀ z ≤ n :=
          fun z => by simpa using hammingDist_le_card_fintype (x := x₀) (y := z)
        have hring_eq : hamming_ring c c' x₀ = Finset.univ \ hamming_ball c x₀ := by
          ext z
          have hd : hammingDist x₀ z ≤ n := hdist_le z
          simp only [hamming_ring, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_sdiff,
            hamming_ball]
          constructor
          · rintro ⟨h1, -⟩; omega
          · intro h; omega
        have hcardB : (hamming_ring c c' x₀).card =
            q ^ n - ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by
          rw [hring_eq, Finset.card_sdiff, Finset.inter_univ, hball_c x₀]
          simp
  8. The combined cardinality of the disjoint balls and the ring around x₀ satisfies |C| · |B_c| + (qⁿ - |B_c|) ≤ qⁿ.

    The ball B_c(x₀) is nonempty since it contains x₀, so its volume |B_c| is strictly positive and at most qⁿ. The union of the pairwise disjoint codeword balls and the ring around x₀ is a disjoint union of size |C| · |B_c| + (qⁿ - |B_c|). Because this union is a subset of the full space of qⁿ words, its cardinality cannot exceed qⁿ.

    lines 67–90

    uses Finset.card_pos.mpr, Finset.card_le_card, Finset.subset_univ, Finset.card_biUnion, Finset.sum_congr, Finset.sum_const, Nat.nsmul_eq_mul, Finset.disjoint_biUnion_left, Finset.card_union_of_disjoint

        have hball_pos : 0 < ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by
          rw [← hball_c x₀]
          exact Finset.card_pos.mpr ⟨x₀, by simp [hamming_ball]⟩
        have hSle : ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i ≤ q ^ n := by
          rw [← hball_c x₀]
          calc (hamming_ball c x₀).card ≤ Finset.univ.card := Finset.card_le_card (Finset.subset_univ _)
            _ = q ^ n := by simp
        have hcardA : (C.biUnion (fun x => hamming_ball c x)).card =
            C.card * ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by
          rw [Finset.card_biUnion hdisj]
          rw [Finset.sum_congr rfl (fun x _ => hball_c x), Finset.sum_const, Nat.nsmul_eq_mul]
        have hdisjAB : Disjoint (C.biUnion (fun x => hamming_ball c x)) (hamming_ring c c' x₀) := by
          rw [Finset.disjoint_biUnion_left]
          exact fun x hx => hdisj_ring x hx x₀ hx₀
        have hle : C.card * (∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i)
            + (q ^ n - ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i) ≤ q ^ n := by
          have h1 : (C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀).card =
              C.card * (∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i)
                + (q ^ n - ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i) := by
            rw [Finset.card_union_of_disjoint hdisjAB, hcardA, hcardB]
          rw [← h1]
          calc (C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀).card
              ≤ Finset.univ.card := Finset.card_le_card (Finset.subset_univ _)
            _ = q ^ n := by simp
  9. When n < c + c', the code has cardinality 1, so C = {x₀}.

    Adding |B_c| to both sides of |C| · |B_c| + (qⁿ - |B_c|) ≤ qⁿ cancels the subtraction to give |C| · |B_c| + qⁿ ≤ qⁿ + |B_c|, which simplifies to |C| · |B_c| ≤ |B_c| = 1 · |B_c|. Since |B_c| > 0, we deduce |C| ≤ 1 (Nat.le_of_mul_le_mul_right). Since x₀ ∈ C, |C| is also positive, forcing |C| = 1. A finset of size 1 containing x₀ must be the singleton {x₀} (Finset.card_eq_one.mp).

    lines 91–108

    uses Nat.one_mul, Nat.add_le_add_right, Nat.add_assoc, Nat.sub_add_cancel, Nat.add_comm, Nat.add_le_add_iff_right, Nat.le_of_mul_le_mul_right, Finset.card_pos.mpr, Finset.card_eq_one.mp

        have hCcard_le : C.card ≤ 1 := by
          have hXle : C.card * (∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i) ≤
              1 * ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by
            rw [Nat.one_mul]
            have h2 : C.card * (∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i)
                  + (q ^ n - ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i)
                  + ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i ≤ q ^ n + _ :=
              Nat.add_le_add_right hle _
            rw [Nat.add_assoc, Nat.sub_add_cancel hSle, Nat.add_comm (q ^ n)] at h2
            exact (Nat.add_le_add_iff_right).mp h2
          exact Nat.le_of_mul_le_mul_right hXle hball_pos
        have hk : C.card = 1 := by
          have hpos : 0 < C.card := Finset.card_pos.mpr ⟨x₀, hx₀⟩
          omega
        have hCsing : C = {x₀} := by
          obtain ⟨x, hx⟩ := Finset.card_eq_one.mp hk
          have hxx : x₀ = x := by simpa [hx] using hx₀
          rw [hx, ← hxx]
  10. When n < c + c', every word y belongs either to B_c(x₀) or to the ring around x₀.

    We split on whether d(x₀, y) ≤ c. If so, y ∈ B_c(x₀) with codeword x₀ ∈ C = {x₀}. If not, then c + 1 ≤ d(x₀, y), and since d(x₀, y) ≤ n < c + c', y satisfies both ring bounds and thus belongs to the ring around x₀.

    lines 109–116

    uses Or.inl, Or.inr, Finset.mem_filter, Finset.mem_univ, true_and

        by_cases hyc : hammingDist x₀ y ≤ c
        · refine Or.inl ⟨x₀, ?_, ?_⟩
          · rw [hCsing]; simp
          · simpa [hamming_ball] using hyc
        · refine Or.inr ?_
          simp only [hamming_ring, Finset.mem_filter, Finset.mem_univ, true_and]
          have hd : hammingDist x₀ y ≤ n := hdist_le y
          omega
Or.inl
from a proof of A, deduce A ∨ B
Or.inr
from a proof of B, deduce A ∨ B
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 its universal finset Finset.univ
true_and
the conjunction of True and p is equivalent to p
le_trans
transitivity of inequality: if a ≤ b and b ≤ c, then a ≤ c
hammingDist_le_card_fintype
the Hamming distance between two vectors is bounded by the cardinality of their index type
Fintype.card_le_one_iff_subsingleton
a finite type has cardinality at most 1 if and only if it is a subsingleton
Fintype.card_fin
the cardinality of Fin n is n
hammingDist_eq_zero.mpr
if two words are equal, their Hamming distance is 0
Subsingleton.elim
any two elements of a subsingleton type are equal
Nat.zero_le
zero is less than or equal to any natural number
not_le.mp
if a is not less than or equal to b, then b < a for natural numbers
le_of_lt
a strict inequality a < b implies a ≤ b
card_hamming_ball
the cardinality of a radius-r Hamming ball in (Fin q)ⁿ (with 2 ≤ q and r ≤ n) is ∑_{i=0}^r (n choose i) * (q - 1)^i
card_hamming_ring
the cardinality of a (c, c') Hamming ring in (Fin q)ⁿ (with 2 ≤ q and c + c' ≤ n) is ∑_{j=c+1}^{c+c'} (n choose j) * (q - 1)^j
Finset.card_biUnion
the cardinality of a pairwise disjoint union of finsets is the sum of their cardinalities
Finset.sum_congr
sums over identical finsets of pointwise equal functions are equal
Finset.sum_const
the sum of a constant over a finset is the size of the finset times that constant
Nat.nsmul_eq_mul
natural number scalar multiplication coincides with standard multiplication
Finset.disjoint_biUnion_left
an indexed union of finsets is disjoint from a set if and only if each component finset is disjoint from that set
Finset.card_union_of_disjoint
the cardinality of the union of two disjoint finsets is the sum of their cardinalities
Finset.eq_univ_of_card
a subset of a finite type whose cardinality equals the type's total cardinality is the full universal set
Finset.mem_union
an element belongs to the union of two finsets if and only if it belongs to at least one of them
Finset.mem_biUnion
an element belongs to an indexed union of finsets if and only if it belongs to some member finset
Finset.mem_sdiff
an element belongs to s \ t if and only if it belongs to s and does not belong to t
Finset.card_sdiff
the cardinality of s \ t is |s| - |t ∩ s|
Finset.inter_univ
the intersection of any finset with the universal finset is itself
Finset.card_pos.mpr
a finset has strictly positive cardinality if and only if it is nonempty
Finset.card_le_card
if s ⊆ t, then |s| ≤ |t|
Finset.subset_univ
every finset is a subset of Finset.univ
Nat.one_mul
1 * n = n for any natural number n
Nat.add_le_add_right
adding a natural number k to both sides preserves inequality: n ≤ m → n + k ≤ m + k
Nat.add_assoc
addition of natural numbers is associative: (n + m) + k = n + (m + k)
Nat.sub_add_cancel
if m ≤ n, then (n - m) + m = n
Nat.add_comm
addition of natural numbers is commutative: n + m = m + n
Nat.add_le_add_iff_right
m + n ≤ k + n if and only if m ≤ k for natural numbers
Nat.le_of_mul_le_mul_right
if a * c ≤ b * c and c > 0, then a ≤ b for natural numbers
Finset.card_eq_one.mp
a finset has cardinality 1 if and only if it is a singleton {a}

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 (c, c') joint perfect code partitions the space into radius-c balls around each codeword and the (c, c') ring around any fixed codeword. -/lemma joint_perfect_code_partition {n q c c' : ℕ}    (C : Finset (Fin n → Fin q)) (hC : is_joint_perfect_code c c' C)    {x₀ : Fin n → Fin q} (hx₀ : x₀ ∈ C) (y : Fin n → Fin q) :    (∃ x ∈ C, y ∈ hamming_ball c x) ∨ y ∈ hamming_ring c c' x₀ := by  classical  obtain ⟨hECD, hcount⟩ := hC  obtain ⟨hdisj, hdisj_ring⟩ := hECD  by_cases hcn : n ≤ c  · refine Or.inl ⟨x₀, hx₀, ?_⟩    simp only [hamming_ball, Finset.mem_filter, Finset.mem_univ, true_and]    exact le_trans (by simpa using hammingDist_le_card_fintype (x := x₀) (y := y)) hcn  by_cases hq1 : q ≤ 1  · have hsub : Subsingleton (Fin q) := by      rw [← Fintype.card_le_one_iff_subsingleton, Fintype.card_fin]      exact hq1    haveI := hsub    refine Or.inl ⟨x₀, hx₀, ?_⟩    simp only [hamming_ball, Finset.mem_filter, Finset.mem_univ, true_and]    rw [hammingDist_eq_zero.mpr (Subsingleton.elim x₀ y)]    exact Nat.zero_le c  have hcn' : c < n := not_le.mp hcn  have hq2 : 2 ≤ q := by have := not_le.mp hq1; omega  have hc_le : c ≤ n := le_of_lt hcn'  by_cases hcc' : c + c' ≤ n  · have hball_c : ∀ x : Fin n → Fin q,        (hamming_ball c x).card = ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i :=      fun x => card_hamming_ball n q c hq2 hc_le x    have hring : (hamming_ring c c' x₀).card =        ∑ j ∈ Finset.Icc (c + 1) (c + c'), n.choose j * (q - 1) ^ j :=      card_hamming_ring n q c c' hq2 hcc' x₀    have hcardA : (C.biUnion (fun x => hamming_ball c x)).card =        C.card * ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by      rw [Finset.card_biUnion hdisj]      rw [Finset.sum_congr rfl (fun x _ => hball_c x), Finset.sum_const, Nat.nsmul_eq_mul]    have hdisjAB : Disjoint (C.biUnion (fun x => hamming_ball c x)) (hamming_ring c c' x₀) := by      rw [Finset.disjoint_biUnion_left]      exact fun x hx => hdisj_ring x hx x₀ hx₀    have hUcard : (C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀).card =        Fintype.card (Fin n → Fin q) := by      rw [Finset.card_union_of_disjoint hdisjAB, hcardA, hring, hcount]      simp    have hUuniv : C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀ = Finset.univ :=      Finset.eq_univ_of_card _ hUcard    have hy : y ∈ C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀ := by      rw [hUuniv]; exact Finset.mem_univ y    rw [Finset.mem_union, Finset.mem_biUnion] at hy    exact hy  · have hcc'' : n < c + c' := not_le.mp hcc'    have hball_c : ∀ x : Fin n → Fin q,        (hamming_ball c x).card = ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i :=      fun x => card_hamming_ball n q c hq2 hc_le x    have hdist_le : ∀ z : Fin n → Fin q, hammingDist x₀ z ≤ n :=      fun z => by simpa using hammingDist_le_card_fintype (x := x₀) (y := z)    have hring_eq : hamming_ring c c' x₀ = Finset.univ \ hamming_ball c x₀ := by      ext z      have hd : hammingDist x₀ z ≤ n := hdist_le z      simp only [hamming_ring, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_sdiff,        hamming_ball]      constructor      · rintro ⟨h1, -⟩; omega      · intro h; omega    have hcardB : (hamming_ring c c' x₀).card =        q ^ n - ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by      rw [hring_eq, Finset.card_sdiff, Finset.inter_univ, hball_c x₀]      simp    have hball_pos : 0 < ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by      rw [← hball_c x₀]      exact Finset.card_pos.mpr ⟨x₀, by simp [hamming_ball]⟩    have hSle : ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i ≤ q ^ n := by      rw [← hball_c x₀]      calc (hamming_ball c x₀).card ≤ Finset.univ.card := Finset.card_le_card (Finset.subset_univ _)        _ = q ^ n := by simp    have hcardA : (C.biUnion (fun x => hamming_ball c x)).card =        C.card * ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by      rw [Finset.card_biUnion hdisj]      rw [Finset.sum_congr rfl (fun x _ => hball_c x), Finset.sum_const, Nat.nsmul_eq_mul]    have hdisjAB : Disjoint (C.biUnion (fun x => hamming_ball c x)) (hamming_ring c c' x₀) := by      rw [Finset.disjoint_biUnion_left]      exact fun x hx => hdisj_ring x hx x₀ hx₀    have hle : C.card * (∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i)        + (q ^ n - ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i) ≤ q ^ n := by      have h1 : (C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀).card =          C.card * (∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i)            + (q ^ n - ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i) := by        rw [Finset.card_union_of_disjoint hdisjAB, hcardA, hcardB]      rw [← h1]      calc (C.biUnion (fun x => hamming_ball c x) ∪ hamming_ring c c' x₀).card          ≤ Finset.univ.card := Finset.card_le_card (Finset.subset_univ _)        _ = q ^ n := by simp    have hCcard_le : C.card ≤ 1 := by      have hXle : C.card * (∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i) ≤          1 * ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i := by        rw [Nat.one_mul]        have h2 : C.card * (∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i)              + (q ^ n - ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i)              + ∑ i ∈ Finset.range (c + 1), n.choose i * (q - 1) ^ i ≤ q ^ n + _ :=          Nat.add_le_add_right hle _        rw [Nat.add_assoc, Nat.sub_add_cancel hSle, Nat.add_comm (q ^ n)] at h2        exact (Nat.add_le_add_iff_right).mp h2      exact Nat.le_of_mul_le_mul_right hXle hball_pos    have hk : C.card = 1 := by      have hpos : 0 < C.card := Finset.card_pos.mpr ⟨x₀, hx₀⟩      omega    have hCsing : C = {x₀} := by      obtain ⟨x, hx⟩ := Finset.card_eq_one.mp hk      have hxx : x₀ = x := by simpa [hx] using hx₀      rw [hx, ← hxx]    by_cases hyc : hammingDist x₀ y ≤ c    · refine Or.inl ⟨x₀, ?_, ?_⟩      · rw [hCsing]; simp      · simpa [hamming_ball] using hyc    · refine Or.inr ?_      simp only [hamming_ring, Finset.mem_filter, Finset.mem_univ, true_and]      have hd : hammingDist x₀ y ≤ n := hdist_le y      omega
See it in the graphDiscuss this resultChallenge it