AFTD Auto-Formalizing Theoretical Domains

op861_boolean_matching_window_count

theoremverified

For a finite Boolean coordinate set, a fixed bit pattern, an integer offset, and an integer interval with ordered endpoints, the number of assignments whose offset plus number of matching bits lies in the interval is at most the interval length times the largest binomial coefficient for the number of coordinates.

Statement

theorem op861_boolean_matching_window_count (β : Type) [Fintype β] [DecidableEq β]
    (pattern : β → Bool) (lo hi offset : ℤ) (hwindow : lo ≤ hi) :
    ((Finset.univ : Finset (β → Bool)).filter (fun v =>
      lo ≤ offset + (((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card : ℤ) ∧
      offset + (((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card : ℤ) ≤ hi)).card ≤
      (hi - lo + 1).toNat * (Fintype.card β).choose (Fintype.card β / 2)
Source
Per-block window estimate for op861_disjoint_boolean_windows_count.
Verified
18 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 4
Used by 1

The proof, explained

The proof partitions the Boolean assignments satisfying the window condition according to their match count $s$, the number of coordinates where the assignment agrees with the given pattern. For each fixed count $s$, the set of matching assignments injects into the collection of size-$s$ subsets of coordinates, so each fiber has size at most $\binom{n}{s} \le \binom{n}{\lfloor n/2 \rfloor}$. The set of admissible values of $s$ embeds injectively into the integer interval $[lo - offset, hi - offset]$, which contains at most $(hi - lo + 1)_{+}$ integers. Summing the fiber bound over all admissible counts yields the upper bound $(hi - lo + 1)_{+} \binom{n}{\lfloor n/2 \rfloor}$.

  1. The set of assignments satisfying the window condition is contained in the union of fibers $\{v \mid m(v) = s\}$ over all match counts $s \in W$, where $W$ consists of counts $s \in \{0, \dots, n\}$ satisfying $lo \le offset + s \le hi$.

    Let $n = |\beta|$ be the number of coordinates, and define $m(v)$ as the number of coordinates where an assignment $v$ matches the pattern. Let $W$ be the set of counts $s \in \{0, \dots, n\}$ such that $lo \le offset + s \le hi$. For any assignment $v$ satisfying the window condition, the number of matching coordinates is at most the total number of coordinates $n$ (by `Finset.card_le_univ`), so $m(v) < n + 1$. Since $m(v)$ also satisfies the window bounds, $m(v) \in W$, placing $v$ in the union of fibers.

    lines 7–20

    uses Finset.card_le_univ, Finset.mem_biUnion, Finset.mem_filter, Finset.mem_range, Finset.mem_univ, true_and

      classical
      set n := Fintype.card β
      let m : (β → Bool) → ℕ := fun v => ((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card
      let W : Finset ℕ := (Finset.range (n + 1)).filter (fun s => lo ≤ offset + (s : ℤ) ∧ offset + (s : ℤ) ≤ hi)
      have hsub : ((Finset.univ : Finset (β → Bool)).filter (fun v =>
          lo ≤ offset + (((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card : ℤ) ∧
          offset + (((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card : ℤ) ≤ hi))
          ⊆ W.biUnion (fun s => (Finset.univ : Finset (β → Bool)).filter (fun v => m v = s)) := by
        intro v hv
        simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hv
        simp only [Finset.mem_biUnion, Finset.mem_filter, Finset.mem_univ, true_and, W, Finset.mem_range]
        refine ⟨m v, ⟨?_, hv⟩, rfl⟩
        have := Finset.card_le_univ ((Finset.univ : Finset β).filter (fun t => v t = pattern t))
        simp only [m]; omega
  2. For every match count $s$, the fiber $\{v \mid m(v) = s\}$ of assignments with exactly $s$ matches has cardinality at most $\binom{n}{\lfloor n/2 \rfloor}$.

    An assignment $v$ is uniquely determined by the set of coordinates where it matches the pattern: the map sending $v$ to $\{t \in \beta \mid v(t) = \text{pattern}(t)\}$ is injective on the fiber because knowing whether $v(t) = \text{pattern}(t)$ completely determines the Boolean value $v(t)$. This map sends assignments in the fiber $\{v \mid m(v) = s\}$ into size-$s$ subsets of $\beta$. By `Finset.card_le_card_of_injOn`, the fiber size is at most the number of such subsets, which is $\binom{n}{s}$ by `Finset.card_powersetCard` and `Finset.card_univ`. Finally, any binomial coefficient $\binom{n}{s}$ is at most the central coefficient $\binom{n}{\lfloor n/2 \rfloor}$ by `Nat.choose_le_middle`.

    lines 21–36

    uses Finset.card_le_card_of_injOn, Finset.card_powersetCard, Finset.card_univ, Finset.coe_filter, Finset.mem_coe, Finset.mem_filter, Finset.mem_powersetCard, Finset.mem_univ, Finset.subset_univ, Nat.choose_le_middle, Set.mem_setOf_eq, congrArg, eq_iff_iff, true_and

      have hfib : ∀ s, ((Finset.univ : Finset (β → Bool)).filter (fun v => m v = s)).card ≤ n.choose (n / 2) := by
        intro s
        calc _ ≤ ((Finset.univ : Finset β).powersetCard s).card := by
              apply Finset.card_le_card_of_injOn (fun v => (Finset.univ : Finset β).filter (fun t => v t = pattern t))
              · intro v hv
                simp only [Finset.coe_filter, Finset.mem_univ, true_and, Set.mem_setOf_eq] at hv
                simp only [Finset.mem_coe,
                  Finset.mem_powersetCard, Finset.subset_univ, true_and]
                exact hv
              · intro v _ w _ hvw
                funext t
                have := congrArg (fun S : Finset β => t ∈ S) hvw
                simp only [Finset.mem_filter, Finset.mem_univ, true_and, eq_iff_iff] at this
                cases hv : v t <;> cases hw : w t <;> cases hp : pattern t <;> simp_all
          _ = n.choose s := by rw [Finset.card_powersetCard, Finset.card_univ]
          _ ≤ n.choose (n / 2) := Nat.choose_le_middle s n
  3. The number of admissible match counts $|W|$ is at most $(hi - lo + 1)_{+}$.

    The natural embedding $s \mapsto (s : \mathbb{Z})$ is injective on $W$. For each $s \in W$, the condition $lo \le offset + s \le hi$ rearranges to $lo - offset \le s \le hi - offset$, so the image of $W$ lies in the closed integer interval $[lo - offset, hi - offset]$. By `Finset.card_le_card_of_injOn`, $|W| \le |[lo - offset, hi - offset]|$. By `Int.card_Icc`, the number of integers in this interval is $((hi - offset) - (lo - offset) + 1)_{+}$, which simplifies algebraically to $(hi - lo + 1)_{+}$.

    lines 37–49

    uses Finset.card_le_card_of_injOn, Finset.coe_Icc, Finset.coe_filter, Finset.mem_range, Int.card_Icc, Set.mem_Icc, Set.mem_setOf_eq

      have hW : W.card ≤ (hi - lo + 1).toNat := by
        have hinj : W.card ≤ (Finset.Icc (lo - offset) (hi - offset)).card := by
          apply Finset.card_le_card_of_injOn (fun s : ℕ => (s : ℤ))
          · intro s hs
            simp only [Finset.coe_filter, W, Finset.mem_range, Set.mem_setOf_eq] at hs
            simp only [Finset.coe_Icc, Set.mem_Icc]
            omega
          · intro a _ b _ h
            simpa using h
        rw [Int.card_Icc] at hinj
        have : hi - offset + 1 - (lo - offset) = hi - lo + 1 := by ring
        rw [this] at hinj
        exact hinj
  4. The total number of assignments satisfying the window condition is at most $(hi - lo + 1)_{+} \binom{n}{\lfloor n/2 \rfloor}$.

    By the subset inclusion established in Step 1, the number of satisfying assignments is at most the cardinality of the union of fibers (`Finset.card_le_card`), which is bounded by the sum of individual fiber sizes over $s \in W$ (`Finset.card_biUnion_le`). Replacing each fiber size with its upper bound $\binom{n}{\lfloor n/2 \rfloor}$ from Step 2 (`Finset.sum_le_sum`), this sum becomes $|W| \cdot \binom{n}{\lfloor n/2 \rfloor}$ (`Finset.sum_const`, `smul_eq_mul`). Finally, substituting the upper bound $|W| \le (hi - lo + 1)_{+}$ from Step 3 yields the desired bound (`Nat.mul_le_mul_right`).

    lines 50–56

    uses Finset.card_biUnion_le, Finset.card_le_card, Finset.sum_const, Finset.sum_le_sum, Nat.mul_le_mul_right, smul_eq_mul

      calc _ ≤ (W.biUnion (fun s => (Finset.univ : Finset (β → Bool)).filter (fun v => m v = s))).card :=
            Finset.card_le_card hsub
        _ ≤ ∑ s ∈ W, ((Finset.univ : Finset (β → Bool)).filter (fun v => m v = s)).card :=
            Finset.card_biUnion_le
        _ ≤ ∑ s ∈ W, n.choose (n / 2) := Finset.sum_le_sum fun s _ => hfib s
        _ = W.card * n.choose (n / 2) := by rw [Finset.sum_const, smul_eq_mul]
        _ ≤ _ := Nat.mul_le_mul_right _ hW
Finset.card_biUnion_le
The cardinality of a union of finite sets is at most the sum of their individual cardinalities.
Finset.card_le_card
If one finite set is a subset of another, its cardinality is at most that of the larger set.
Finset.card_le_card_of_injOn
If a function maps a finite set $s$ into a finite set $t$ and is injective on $s$, then $|s| \le |t|$.
Finset.card_le_univ
The cardinality of any finite subset of a finite type is at most the total number of elements in the type.
Finset.card_powersetCard
The number of size-$k$ subsets of a finite set $s$ is $\binom{|s|}{k}$.
Finset.card_univ
The cardinality of the universal finset of a finite type equals the total cardinality of the type.
Finset.coe_Icc
The coercion of a finite closed interval $[a, b]$ to a set equals the set-theoretic interval $\{x \mid a \le x \le b\}$.
Finset.coe_filter
The coercion of a filtered finset $\{x \in s \mid p(x)\}$ to a set equals the set of elements in $s$ satisfying $p$.
Finset.mem_biUnion
An element belongs to a finite union $\bigcup_{x \in s} t(x)$ if and only if it belongs to $t(x)$ for some $x \in s$.
Finset.mem_coe
Membership of an element in the set coercion of a finset is equivalent to membership in the finset.
Finset.mem_filter
An element belongs to a filtered finset $\{x \in s \mid p(x)\}$ if and only if it belongs to $s$ and satisfies $p$.
Finset.mem_powersetCard
A finite set $s$ belongs to the size-$k$ powerset of $t$ if and only if $s \subseteq t$ and $|s| = k$.
Finset.mem_range
A natural number $m$ belongs to `Finset.range n` if and only if $m < n$.
Finset.mem_univ
Every element of a finite type belongs to its universal finset.
Finset.subset_univ
Every finite subset of a finite type is contained in the universal finset.
Finset.sum_const
Summing a constant value over a finite set $s$ equals $|s|$ times that value.
Finset.sum_le_sum
If $f(x) \le g(x)$ for all elements $x$ in a finite set $s$, then $\sum_{x \in s} f(x) \le \sum_{x \in s} g(x)$.
Int.card_Icc
The number of integers in the closed interval $[a, b]$ is $(b - a + 1)_{+}$.
Nat.choose_le_middle
For any natural number $n$, the binomial coefficient $\binom{n}{r}$ is maximized at the central index $\lfloor n / 2 \rfloor$.
Nat.mul_le_mul_right
Multiplying both sides of an inequality $n \le m$ on the right by a natural number preserves the inequality: $n \cdot k \le m \cdot k$.
Set.mem_Icc
An element $x$ belongs to the set interval $[a, b]$ if and only if $a \le x$ and $x \le b$.
Set.mem_setOf_eq
Membership $x \in \{y \mid p(y)\}$ is definitionally $p(x)$.
congrArg
Applying a function to equal arguments produces equal results.
eq_iff_iff
Equality between two propositions is equivalent to logical equivalence.
smul_eq_mul
Scalar multiplication of numbers equals ordinary multiplication.
true_and
The conjunction $\text{True} \land p$ is logically equivalent to $p$.

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 op861_boolean_matching_window_count (β : Type) [Fintype β] [DecidableEq β]    (pattern : β → Bool) (lo hi offset : ℤ) (hwindow : lo ≤ hi) :    ((Finset.univ : Finset (β → Bool)).filter (fun v =>      lo ≤ offset + (((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card : ℤ) ∧      offset + (((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card : ℤ) ≤ hi)).card ≤      (hi - lo + 1).toNat * (Fintype.card β).choose (Fintype.card β / 2) := by  classical  set n := Fintype.card β  let m : (β → Bool) → ℕ := fun v => ((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card  let W : Finset ℕ := (Finset.range (n + 1)).filter (fun s => lo ≤ offset + (s : ℤ) ∧ offset + (s : ℤ) ≤ hi)  have hsub : ((Finset.univ : Finset (β → Bool)).filter (fun v =>      lo ≤ offset + (((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card : ℤ) ∧      offset + (((Finset.univ : Finset β).filter (fun t => v t = pattern t)).card : ℤ) ≤ hi))      ⊆ W.biUnion (fun s => (Finset.univ : Finset (β → Bool)).filter (fun v => m v = s)) := by    intro v hv    simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hv    simp only [Finset.mem_biUnion, Finset.mem_filter, Finset.mem_univ, true_and, W, Finset.mem_range]    refine ⟨m v, ⟨?_, hv⟩, rfl⟩    have := Finset.card_le_univ ((Finset.univ : Finset β).filter (fun t => v t = pattern t))    simp only [m]; omega  have hfib : ∀ s, ((Finset.univ : Finset (β → Bool)).filter (fun v => m v = s)).card ≤ n.choose (n / 2) := by    intro s    calc _ ≤ ((Finset.univ : Finset β).powersetCard s).card := by          apply Finset.card_le_card_of_injOn (fun v => (Finset.univ : Finset β).filter (fun t => v t = pattern t))          · intro v hv            simp only [Finset.coe_filter, Finset.mem_univ, true_and, Set.mem_setOf_eq] at hv            simp only [Finset.mem_coe,              Finset.mem_powersetCard, Finset.subset_univ, true_and]            exact hv          · intro v _ w _ hvw            funext t            have := congrArg (fun S : Finset β => t ∈ S) hvw            simp only [Finset.mem_filter, Finset.mem_univ, true_and, eq_iff_iff] at this            cases hv : v t <;> cases hw : w t <;> cases hp : pattern t <;> simp_all      _ = n.choose s := by rw [Finset.card_powersetCard, Finset.card_univ]      _ ≤ n.choose (n / 2) := Nat.choose_le_middle s n  have hW : W.card ≤ (hi - lo + 1).toNat := by    have hinj : W.card ≤ (Finset.Icc (lo - offset) (hi - offset)).card := by      apply Finset.card_le_card_of_injOn (fun s : ℕ => (s : ℤ))      · intro s hs        simp only [Finset.coe_filter, W, Finset.mem_range, Set.mem_setOf_eq] at hs        simp only [Finset.coe_Icc, Set.mem_Icc]        omega      · intro a _ b _ h        simpa using h    rw [Int.card_Icc] at hinj    have : hi - offset + 1 - (lo - offset) = hi - lo + 1 := by ring    rw [this] at hinj    exact hinj  calc _ ≤ (W.biUnion (fun s => (Finset.univ : Finset (β → Bool)).filter (fun v => m v = s))).card :=        Finset.card_le_card hsub    _ ≤ ∑ s ∈ W, ((Finset.univ : Finset (β → Bool)).filter (fun v => m v = s)).card :=        Finset.card_biUnion_le    _ ≤ ∑ s ∈ W, n.choose (n / 2) := Finset.sum_le_sum fun s _ => hfib s    _ = W.card * n.choose (n / 2) := by rw [Finset.sum_const, smul_eq_mul]    _ ≤ _ := Nat.mul_le_mul_right _ hW
See it in the graphDiscuss this resultChallenge it