AFTD Auto-Formalizing Theoretical Domains

hypercube_coordinate_square_count

theoremverifiedformalization

For n ≥ 2, the number of coordinate squares of the n-cube {0,1}^n is C(n,2)·2^(n-2), where a coordinate square is a vertex set of the form {u : u_k = v_k for all k ∉ {i, j}} for two distinct coordinates i ≠ j and a vertex v.

Statement

open Classical in
theorem hypercube_coordinate_square_count (n : ℕ) (hn : 2 ≤ n) :
    (Finset.univ.filter fun s : Finset (Fin n → Bool) =>
      ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,
        s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k).card =
      n.choose 2 * 2 ^ (n - 2)
Formalizes
arXiv:2611.27284 (The four-cycle extremal number of the seven-cube), Lemma 2.1, case k = 2 (number of coordinate 2-faces).
Verified
29 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Used by 1

Read back from the Lean

For any natural number $n \ge 2$, the number of subsets $s \subseteq (\text{Fin } n \to \text{Bool})$ for which there exist distinct indices $i, j : \text{Fin } n$ and a vector $v : \text{Fin } n \to \text{Bool}$ such that $s = \{u : \text{Fin } n \to \text{Bool} \mid \forall k : \text{Fin } n,\, k \neq i \to k \neq j \to u(k) = v(k)\}$ is equal to $\binom{n}{2} \cdot 2^{n - 2}$.

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

Each coordinate square in the $n$-cube is uniquely determined by a 2-element set $D$ of varying coordinates and a base vertex $w$ that is fixed to false on $D$ and specifies the coordinates outside $D$. We establish a bijection between the set of coordinate squares and this canonical index set of pairs $(D, w)$. The number of choices for the coordinate pair $D$ is $\binom{n}{2}$, and for each $D$, an auxiliary lemma shows that exactly $2^{n-2}$ vertices vanish on $D$. Summing over all choices of $D$ yields the total count $\binom{n}{2} 2^{n-2}$.

  1. The number of boolean functions on $\text{Fin } n$ that vanish on $D$ equals the number of subsets of the complement $D^c$.

    The exponent $n - |D|$ equals the cardinality of the complement $D^c$ (by Finset.card_compl and Fintype.card_fin), so $2^{n - |D|}$ is the cardinality of the powerset $\mathcal{P}(D^c)$ (by Finset.card_powerset). To prove the sets have equal size, Finset.card_bij' sets up a bijection between functions vanishing on $D$ and subsets of $D^c$, sending a function $w$ to its support $\{l \mid w(l) = \text{true}\}$ and a subset $T \subseteq D^c$ to its indicator function $\lambda l.\, \text{decide}(l \in T)$.

    lines 3–6

    uses Finset.card_compl, Fintype.card_fin, Finset.card_powerset, Finset.card_bij'

      classical
      rw [show n - D.card = (Dᶜ).card by rw [Finset.card_compl, Fintype.card_fin], ← Finset.card_powerset]
      refine Finset.card_bij' (fun w _ => Finset.univ.filter fun l => w l = true)
        (fun T _ => fun l => decide (l ∈ T)) ?_ ?_ ?_ ?_
  2. The support of any function vanishing on $D$ is a subset of $D^c$, and the indicator of any subset of $D^c$ vanishes on $D$.

    If $w(l) = \text{false}$ for all $l \in D$, then no element of the support $\{l \mid w(l) = \text{true}\}$ can belong to $D$, because $\text{false} \neq \text{true}$ (Bool.false_ne_true), so the support is contained in $D^c$ (Finset.mem_powerset). Conversely, for any $T \subseteq D^c$ and any $l \in D$, $l$ cannot belong to $T$ because $T \subseteq D^c$ and $D \cap D^c = \emptyset$ (Finset.mem_compl), so the indicator function evaluates to false on $D$ (decide_eq_false_iff_not).

    lines 7–22

    uses Finset.mem_powerset, Finset.mem_compl, Bool.false_ne_true, decide_eq_false_iff_not

      · intro w hw
        simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hw
        rw [Finset.mem_powerset]
        intro l hl
        simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hl
        rw [Finset.mem_compl]
        intro hlD
        rw [hw l hlD] at hl
        exact Bool.false_ne_true hl
      · intro T hT
        rw [Finset.mem_powerset] at hT
        simp only [Finset.mem_filter, Finset.mem_univ, true_and]
        intro l hl
        simp only [decide_eq_false_iff_not]
        intro hlT
        exact Finset.mem_compl.1 (hT hlT) hl
  3. The support map and the indicator map are two-sided inverses. automation

    Given any function $w$, the indicator of its support evaluated at coordinate $l$ evaluates to true if $w(l) = \text{true}$ and false if $w(l) = \text{false}$, recovering $w$. Given any subset $T \subseteq D^c$, the support of its indicator function is precisely the set of coordinates where the indicator is true, which is $T$.

    lines 23–28

      · intro w _
        funext l
        cases h : w l <;> simp [h]
      · intro T _
        ext l
        simp
  4. Define the index set $I$ of canonical pairs $(D, w)$ and the parametrization $\Phi$ of coordinate squares.

    We define $I$ as the dependent sum over all 2-element subsets $D \subseteq \text{Fin } n$ of the sets of functions $w : \text{Fin } n \to \text{Bool}$ that vanish on $D$. The map $\Phi$ takes a pair $\langle D, w \rangle$ to the coordinate square $\{u \mid \forall k \notin D,\, u(k) = w(k)\}$, which consists of all vertices agreeing with $w$ on all coordinates outside $D$.

    lines 36–40

      classical
      let I := (Finset.univ.powersetCard 2 : Finset (Finset (Fin n))).sigma fun D =>
        Finset.univ.filter fun w : Fin n → Bool => ∀ l ∈ D, w l = false
      let Φ : (Σ _ : Finset (Fin n), Fin n → Bool) → Finset (Fin n → Bool) := fun x =>
        Finset.univ.filter fun u => ∀ k, k ∉ x.1 → u k = x.2 k
  5. The square $\Phi(D, w)$ contains $w$ as well as any vector obtained by flipping a coordinate in $D$.

    For any coordinate $l \in D$, flipping the value of $w$ at $l$ does not alter its value at any $k \notin D$, since $k \neq l$. Thus the flipped vector still agrees with $w$ outside $D$ and belongs to $\Phi(D, w)$. The base vector $w$ itself belongs to $\Phi(D, w)$ trivially.

    lines 41–49

      have hflip_mem : ∀ (D : Finset (Fin n)) (w : Fin n → Bool) (l : Fin n), l ∈ D →
          (fun k => if k = l then !w k else w k) ∈ Φ ⟨D, w⟩ := by
        intro D w l hl
        simp only [Φ, Finset.mem_filter, Finset.mem_univ, true_and]
        intro k hk
        have : k ≠ l := fun h => hk (h ▸ hl)
        simp [this]
      have hself : ∀ (D : Finset (Fin n)) (w : Fin n → Bool), w ∈ Φ ⟨D, w⟩ := by
        intro D w; simp [Φ]
  6. If two canonical pairs determine the same square, their membership conditions are equivalent for every vertex.

    To show that $\Phi$ is injective on $I$, let $\langle D, w \rangle, \langle D', w' \rangle \in I$ satisfy $\Phi(D, w) = \Phi(D', w')$. By definition of $I$, $w$ vanishes on $D$ and $w'$ vanishes on $D'$. Since the squares are identical as sets, any vertex $u$ agrees with $w$ outside $D$ if and only if it agrees with $w'$ outside $D'$.

    lines 50–61

      have hinj : Set.InjOn Φ I := by
        rintro ⟨D, w⟩ hx ⟨D', w'⟩ hy hxy
        simp only [Φ] at hxy
        rw [Finset.mem_coe, Finset.mem_sigma] at hx hy
        have hw0 : ∀ l ∈ D, w l = false := by
          have := hx.2; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at this; exact this
        have hw0' : ∀ l ∈ D', w' l = false := by
          have := hy.2; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at this; exact this
        have hmem : ∀ u : Fin n → Bool, (∀ k, k ∉ D → u k = w k) ↔ (∀ k, k ∉ D' → u k = w' k) := by
          intro u
          have := Finset.ext_iff.1 hxy u
          simpa using this
  7. The coordinate set of varying directions is uniquely determined: $D = D'$.

    We show generally that if two such squares coincide, their varying coordinate sets satisfy $A \subseteq B$. If there were some coordinate $l \in A \setminus B$, then both the vector $a$ and the vector obtained by flipping $a$ at $l$ agree with $a$ outside $A$, hence both lie in the square. Because $l \notin B$, both vectors must agree with $b$ at coordinate $l$, forcing $a(l) = b(l) = \neg a(l)$, an impossibility. By mutual inclusion (Finset.Subset.antisymm), we obtain $D = D'$.

    lines 62–75

    uses if_pos, Finset.Subset.antisymm

        have hsub : ∀ {A B : Finset (Fin n)} {a b : Fin n → Bool},
            (∀ u : Fin n → Bool, (∀ k, k ∉ A → u k = a k) ↔ (∀ k, k ∉ B → u k = b k)) → A ⊆ B := by
          intro A B a b h l hlA
          by_contra hlB
          have h1 := (h a).1 (fun _ _ => rfl) l hlB
          have h2 := (h (fun k => if k = l then !a k else a k)).1 (by
            intro k hk
            have : k ≠ l := fun e => hk (e ▸ hlA)
            simp [this]) l hlB
          simp only [if_pos rfl] at h2
          rw [← h1] at h2
          cases a l <;> simp at h2
        have hDD : D = D' := Finset.Subset.antisymm (hsub hmem) (hsub (fun u => (hmem u).symm))
        subst hDD
  8. The base vertex is uniquely determined: $w = w'$, completing the injectivity of $\Phi$ on $I$.

    Having established $D = D'$, we compare $w(l)$ and $w'(l)$ coordinate by coordinate. On $l \in D$, both functions are false by the definition of $I$, so $w(l) = w'(l)$. On $l \notin D$, since $w \in \Phi(D, w) = \Phi(D, w')$, $w$ must agree with $w'$ at $l$. Thus $w = w'$, proving that $\Phi$ is injective on $I$.

    lines 76–82

        have hww : w = w' := by
          funext l
          by_cases hl : l ∈ D
          · rw [hw0 l hl, hw0' l hl]
          · exact (hmem w).1 (fun _ _ => rfl) l hl
        subst hww
        rfl
  9. Every coordinate square can be represented in canonical form as $\Phi(D, w)$ for some $\langle D, w \rangle \in I$.

    Given a coordinate square specified by distinct coordinates $i \neq j$ and a vertex $v$, let $D = \{i, j\}$ and let $w$ be the vertex that takes the value false on $\{i, j\}$ and agrees with $v$ elsewhere. Because $i \neq j$, $D$ has cardinality 2 (Finset.card_pair), so $\langle D, w \rangle \in I$ (using Finset.subset_univ). A vertex agrees with $v$ away from $i$ and $j$ if and only if it agrees with $w$ outside $D$, so the original square is exactly $\Phi(D, w)$.

    lines 83–103

    uses Finset.subset_univ, Finset.card_pair

      have himage : (Finset.univ.filter fun s : Finset (Fin n → Bool) =>
          ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,
            s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k) = I.image Φ := by
        ext s
        simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image]
        constructor
        · rintro ⟨i, j, hij, v, rfl⟩
          refine ⟨⟨{i, j}, fun k => if k ∈ ({i, j} : Finset (Fin n)) then false else v k⟩, ?_, ?_⟩
          · rw [Finset.mem_sigma, Finset.mem_powersetCard]
            refine ⟨⟨Finset.subset_univ _, Finset.card_pair hij⟩, ?_⟩
            simp only [Finset.mem_filter, Finset.mem_univ, true_and]
            intro l hl; simp [hl]
          · ext u
            simp only [Φ, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert,
              Finset.mem_singleton, not_or]
            constructor
            · intro h k hki hkj
              have := h k ⟨hki, hkj⟩
              simpa [hki, hkj] using this
            · rintro h k ⟨hki, hkj⟩
              simp [hki, hkj, h k hki hkj]
  10. Conversely, the image under $\Phi$ of every canonical pair $\langle D, w \rangle \in I$ is a coordinate square.

    For any $\langle D, w \rangle \in I$, the set $D$ has cardinality 2, so $D = \{i, j\}$ for two distinct coordinates $i \neq j$. The set $\Phi(D, w)$ consists of all vertices agreeing with $w$ on all coordinates other than $i$ and $j$, which is precisely the definition of a coordinate square.

    lines 104–113

        · rintro ⟨⟨D, w⟩, hx, rfl⟩
          rw [Finset.mem_sigma, Finset.mem_powersetCard] at hx
          obtain ⟨i, j, hij, rfl⟩ := Finset.card_eq_two.1 hx.1.2
          refine ⟨i, j, hij, w, ?_⟩
          ext u
          simp only [Φ, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert,
            Finset.mem_singleton, not_or]
          constructor
          · intro h k hki hkj; exact h k ⟨hki, hkj⟩
          · rintro h k ⟨hki, hkj⟩; exact h k hki hkj
  11. The number of coordinate squares is $\binom{n}{2} \cdot 2^{n-2}$.

    Because $\Phi$ maps $I$ injectively onto the set of coordinate squares (Finset.card_image_of_injOn), the number of squares equals $|I|$. By Finset.card_sigma, $|I|$ is the sum over all 2-element subsets $D$ of the number of vertices vanishing on $D$. By hcsq_card_zero_on, each of these fibers has cardinality $2^{n - 2}$. Finally, summing this constant over the $\binom{n}{2}$ subsets of size 2 (Finset.sum_const, Finset.card_powersetCard, Finset.card_univ, Fintype.card_fin, smul_eq_mul) gives $\binom{n}{2} \cdot 2^{n-2}$.

    lines 114–117

    uses Finset.card_image_of_injOn, Finset.card_sigma, Finset.sum_congr, hcsq_card_zero_on, Finset.sum_const, Finset.card_powersetCard, Finset.card_univ, Fintype.card_fin, smul_eq_mul

      rw [himage, Finset.card_image_of_injOn hinj, Finset.card_sigma]
      rw [Finset.sum_congr rfl (fun D hD => by
        rw [hcsq_card_zero_on n D, (Finset.mem_powersetCard.1 hD).2])]
      rw [Finset.sum_const, Finset.card_powersetCard, Finset.card_univ, Fintype.card_fin, smul_eq_mul]
Bool.false_ne_true
The boolean value false is not equal to true.
Finset.card_compl
The cardinality of the complement of a finset $s$ in a finite type $\alpha$ is $|\alpha| - |s|$.
Finset.card_bij'
Two finsets have the same cardinality if there exist maps between them that restrict to mutual inverses on the sets.
Finset.card_image_of_injOn
The image of a finset under a map that is injective on that finset has the same cardinality as the finset.
Finset.card_pair
The set $\{a, b\}$ of two distinct elements has cardinality 2.
Finset.card_powerset
The cardinality of the powerset of a finset $s$ is $2^{|s|}$.
Finset.card_powersetCard
The number of $k$-element subsets of a finset $s$ is $\binom{|s|}{k}$.
Finset.card_sigma
The cardinality of a dependent sum (sigma) of finsets is the sum over the base set of the cardinalities of the fibers.
Finset.card_univ
The cardinality of the universal finset of a finite type is the cardinality of the type.
Finset.mem_compl
An element belongs to the complement of a finset if and only if it does not belong to the finset.
Finset.mem_powerset
A finset belongs to the powerset of $t$ if and only if it is a subset of $t$.
Finset.Subset.antisymm
If two finsets are subsets of each other, they are equal.
Finset.subset_univ
Every finset in a finite type is a subset of the universal finset.
Finset.sum_congr
Two sums over the same finset are equal if their summands agree on every element.
Finset.sum_const
Summing a constant $b$ over a finset $s$ yields $|s| \cdot b$.
Fintype.card_fin
The number of elements in $\text{Fin } n$ is $n$.
decide_eq_false_iff_not
The boolean decision of a proposition $p$ is false if and only if $p$ does not hold.
hcsq_card_zero_on
The number of boolean functions on $\text{Fin } n$ that evaluate to false on all coordinates in $D$ is $2^{n - |D|}$.
if_pos
If condition $c$ holds, then `if c then t else e` reduces to $t$.
smul_eq_mul
Scalar multiplication of natural numbers coincides with ordinary multiplication.

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

theorem hcsq_card_zero_on (n : ℕ) (D : Finset (Fin n)) :    (Finset.univ.filter fun w : Fin n → Bool => ∀ l ∈ D, w l = false).card = 2 ^ (n - D.card) := by  classical  rw [show n - D.card = (Dᶜ).card by rw [Finset.card_compl, Fintype.card_fin], ← Finset.card_powerset]  refine Finset.card_bij' (fun w _ => Finset.univ.filter fun l => w l = true)    (fun T _ => fun l => decide (l ∈ T)) ?_ ?_ ?_ ?_  · intro w hw    simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hw    rw [Finset.mem_powerset]    intro l hl    simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hl    rw [Finset.mem_compl]    intro hlD    rw [hw l hlD] at hl    exact Bool.false_ne_true hl  · intro T hT    rw [Finset.mem_powerset] at hT    simp only [Finset.mem_filter, Finset.mem_univ, true_and]    intro l hl    simp only [decide_eq_false_iff_not]    intro hlT    exact Finset.mem_compl.1 (hT hlT) hl  · intro w _    funext l    cases h : w l <;> simp [h]  · intro T _    ext l    simpopen Classical intheorem hypercube_coordinate_square_count (n : ℕ) (hn : 2 ≤ n) :    (Finset.univ.filter fun s : Finset (Fin n → Bool) =>      ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,        s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k).card =      n.choose 2 * 2 ^ (n - 2) := by  classical  let I := (Finset.univ.powersetCard 2 : Finset (Finset (Fin n))).sigma fun D =>    Finset.univ.filter fun w : Fin n → Bool => ∀ l ∈ D, w l = false  let Φ : (Σ _ : Finset (Fin n), Fin n → Bool) → Finset (Fin n → Bool) := fun x =>    Finset.univ.filter fun u => ∀ k, k ∉ x.1 → u k = x.2 k  have hflip_mem : ∀ (D : Finset (Fin n)) (w : Fin n → Bool) (l : Fin n), l ∈ D →      (fun k => if k = l then !w k else w k) ∈ Φ ⟨D, w⟩ := by    intro D w l hl    simp only [Φ, Finset.mem_filter, Finset.mem_univ, true_and]    intro k hk    have : k ≠ l := fun h => hk (h ▸ hl)    simp [this]  have hself : ∀ (D : Finset (Fin n)) (w : Fin n → Bool), w ∈ Φ ⟨D, w⟩ := by    intro D w; simp [Φ]  have hinj : Set.InjOn Φ I := by    rintro ⟨D, w⟩ hx ⟨D', w'⟩ hy hxy    simp only [Φ] at hxy    rw [Finset.mem_coe, Finset.mem_sigma] at hx hy    have hw0 : ∀ l ∈ D, w l = false := by      have := hx.2; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at this; exact this    have hw0' : ∀ l ∈ D', w' l = false := by      have := hy.2; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at this; exact this    have hmem : ∀ u : Fin n → Bool, (∀ k, k ∉ D → u k = w k) ↔ (∀ k, k ∉ D' → u k = w' k) := by      intro u      have := Finset.ext_iff.1 hxy u      simpa using this    have hsub : ∀ {A B : Finset (Fin n)} {a b : Fin n → Bool},        (∀ u : Fin n → Bool, (∀ k, k ∉ A → u k = a k) ↔ (∀ k, k ∉ B → u k = b k)) → A ⊆ B := by      intro A B a b h l hlA      by_contra hlB      have h1 := (h a).1 (fun _ _ => rfl) l hlB      have h2 := (h (fun k => if k = l then !a k else a k)).1 (by        intro k hk        have : k ≠ l := fun e => hk (e ▸ hlA)        simp [this]) l hlB      simp only [if_pos rfl] at h2      rw [← h1] at h2      cases a l <;> simp at h2    have hDD : D = D' := Finset.Subset.antisymm (hsub hmem) (hsub (fun u => (hmem u).symm))    subst hDD    have hww : w = w' := by      funext l      by_cases hl : l ∈ D      · rw [hw0 l hl, hw0' l hl]      · exact (hmem w).1 (fun _ _ => rfl) l hl    subst hww    rfl  have himage : (Finset.univ.filter fun s : Finset (Fin n → Bool) =>      ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,        s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k) = I.image Φ := by    ext s    simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image]    constructor    · rintro ⟨i, j, hij, v, rfl⟩      refine ⟨⟨{i, j}, fun k => if k ∈ ({i, j} : Finset (Fin n)) then false else v k⟩, ?_, ?_⟩      · rw [Finset.mem_sigma, Finset.mem_powersetCard]        refine ⟨⟨Finset.subset_univ _, Finset.card_pair hij⟩, ?_⟩        simp only [Finset.mem_filter, Finset.mem_univ, true_and]        intro l hl; simp [hl]      · ext u        simp only [Φ, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert,          Finset.mem_singleton, not_or]        constructor        · intro h k hki hkj          have := h k ⟨hki, hkj⟩          simpa [hki, hkj] using this        · rintro h k ⟨hki, hkj⟩          simp [hki, hkj, h k hki hkj]    · rintro ⟨⟨D, w⟩, hx, rfl⟩      rw [Finset.mem_sigma, Finset.mem_powersetCard] at hx      obtain ⟨i, j, hij, rfl⟩ := Finset.card_eq_two.1 hx.1.2      refine ⟨i, j, hij, w, ?_⟩      ext u      simp only [Φ, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert,        Finset.mem_singleton, not_or]      constructor      · intro h k hki hkj; exact h k ⟨hki, hkj⟩      · rintro h k ⟨hki, hkj⟩; exact h k hki hkj  rw [himage, Finset.card_image_of_injOn hinj, Finset.card_sigma]  rw [Finset.sum_congr rfl (fun D hD => by    rw [hcsq_card_zero_on n D, (Finset.mem_powersetCard.1 hD).2])]  rw [Finset.sum_const, Finset.card_powersetCard, Finset.card_univ, Fintype.card_fin, smul_eq_mul]
See it in the graphDiscuss this resultChallenge it