AFTD Auto-Formalizing Theoretical Domains

finite_two_appearances_system_of_distinct_representatives

theoremverified

For a finite indexed family of finite sets, if every set has at least two elements and every element occurs in at most two sets, then the family has distinct representatives.

Statement

theorem finite_two_appearances_system_of_distinct_representatives
    {ι α : Type*} [Fintype ι] [DecidableEq ι] [DecidableEq α]
    (sets : ι → Finset α)
    (hsize : ∀ i, 2 ≤ (sets i).card)
    (hfreq : ∀ a, (Finset.univ.filter fun i => a ∈ sets i).card ≤ 2) :
    ∃ f : ι → α, Function.Injective f ∧ ∀ i, f i ∈ sets i
Source
A direct Hall-theorem helper for the matching-cut injective port cover in OP-874.
Verified
27 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Used by 1

The proof, explained

By Hall's Marriage Theorem, the family of sets admits a system of distinct representatives if and only if every subcollection of sets J satisfies |J| ≤ |⋃_{i ∈ J} sets(i)|. Let N = ⋃_{i ∈ J} sets(i) be the union of these sets, and let d(l) be the number of sets in J containing element l. Double counting incidences between sets in J and elements in N shows that ∑_{i ∈ J} |sets(i)| = ∑_{l ∈ N} d(l). Since each set has size at least 2 and each element appears in at most 2 sets, we have 2|J| ≤ ∑_{i ∈ J} |sets(i)| = ∑_{l ∈ N} d(l) ≤ 2|N|, which implies |J| ≤ |N|.

  1. To show the existence of a system of distinct representatives, it suffices to verify Hall's marriage condition: |J| ≤ |⋃_{i ∈ J} sets(i)| for every subset of indices J.

    By Hall's Marriage Theorem (Finset.all_card_le_biUnion_card_iff_existsInjective'), an injective choice function exists if and only if every finite index subset J satisfies Hall's condition. Let J be an arbitrary index subset, let N = ⋃_{i ∈ J} sets(i) be the union of its sets, and for each element l, define the degree d(l) to be the number of indices i ∈ J such that l ∈ sets(i).

    lines 10–14

    uses Finset.all_card_le_biUnion_card_iff_existsInjective'

      classical
      apply (Finset.all_card_le_biUnion_card_iff_existsInjective' sets).mp
      intro J
      let N := J.biUnion sets
      let d : α → ℕ := fun l => (J.filter fun i => l ∈ sets i).card
  2. Each element l belongs to at most two sets indexed by J, that is, d(l) ≤ 2.

    The indices in J whose sets contain l form a subcollection of all indices whose sets contain l. By Finset.card_le_card, d(l) is bounded above by the total number of sets containing l across the entire family, which is assumed to be at most 2 (hfreq).

    lines 15–24

    uses Finset.card_le_card, Finset.mem_filter, Finset.mem_univ, true_and

      have hd2 : ∀ l, d l ≤ 2 := by
        intro l
        calc
          d l ≤ (univ.filter fun i => l ∈ sets i).card := by
            apply Finset.card_le_card
            intro i hi
            simp only [d,Finset.mem_filter] at hi
            simp only [Finset.mem_filter, Finset.mem_univ, true_and]
            exact hi.2
          _ ≤ 2 := hfreq l
  3. For each index i ∈ J, the elements in N that belong to sets(i) are precisely all elements of sets(i).

    Because i ∈ J, any element of sets(i) belongs to the union N = ⋃_{j ∈ J} sets(j) by Finset.mem_biUnion.mpr. Hence, intersecting N with sets(i) yields sets(i).

    lines 25–32

    uses Finset.mem_filter, And.right, Finset.mem_biUnion.mpr

      have hsets : ∀ i ∈ J, N.filter (fun l => l ∈ sets i) = sets i := by
        intro i hi
        ext l
        simp only [Finset.mem_filter]
        constructor
        · exact And.right
        · intro hl
          exact ⟨Finset.mem_biUnion.mpr ⟨i,hi,hl⟩,hl⟩
  4. The sum of the sizes of the sets indexed by J equals the sum of the degrees d(l) over all elements l ∈ N.

    Replacing each |sets(i)| with |{l ∈ N | l ∈ sets(i)}| via Finset.sum_congr, the sum ∑_{i ∈ J} |sets(i)| represents the sum of neighborhood sizes in the bipartite incidence graph. Double counting edges via Finset.sum_card_bipartiteAbove_eq_sum_card_bipartiteBelow swaps the order of summation to yield ∑_{l ∈ N} d(l).

    lines 33–42

    uses Finset.sum_congr, Finset.sum_card_bipartiteAbove_eq_sum_card_bipartiteBelow, Finset.bipartiteAbove, Finset.bipartiteBelow

      have hcount : (∑ i ∈ J, (sets i).card) = ∑ l ∈ N, d l := by
        calc
          _ = ∑ i ∈ J, (N.filter fun l => l ∈ sets i).card := by
            apply Finset.sum_congr rfl
            intro i hi
            rw [hsets i hi]
          _ = _ := by
            have h := @Finset.sum_card_bipartiteAbove_eq_sum_card_bipartiteBelow ι α
              (fun i l => l ∈ sets i) J N _
            simpa [Finset.bipartiteAbove, Finset.bipartiteBelow, d] using h
  5. The sum of the degrees over all elements in N is at most 2|N|.

    Since d(l) ≤ 2 for every element l, termwise comparison using Finset.sum_le_sum gives ∑_{l ∈ N} d(l) ≤ ∑_{l ∈ N} 2 = 2|N|.

    lines 43–46

    uses Finset.sum_le_sum, mul_comm

      have hsmall : (∑ l ∈ N, d l) ≤ 2 * N.card := by
        calc
          _ ≤ ∑ _l ∈ N, 2 := Finset.sum_le_sum (by intro l _; exact hd2 l)
          _ = _ := by simp [mul_comm]
  6. Twice the cardinality of J is at most the sum of the sizes of the sets indexed by J.

    Since every set in the family has size at least 2 (hsize), summing this inequality over all i ∈ J using Finset.sum_le_sum gives 2|J| = ∑_{i ∈ J} 2 ≤ ∑_{i ∈ J} |sets(i)|.

    lines 47–50

    uses mul_comm, Finset.sum_le_sum

      have hlarge : 2 * J.card ≤ ∑ i ∈ J, (sets i).card := by
        calc
          _ = ∑ _i ∈ J, 2 := by simp [mul_comm]
          _ ≤ _ := Finset.sum_le_sum (by intro i _; exact hsize i)
  7. Hall's marriage condition |J| ≤ |N| holds.

    Combining the inequalities 2|J| ≤ ∑_{i ∈ J} |sets(i)| = ∑_{l ∈ N} d(l) ≤ 2|N|, linear integer arithmetic (omega) deduces 2|J| ≤ 2|N|, which gives |J| ≤ |N|.

    lines 51–52

      change J.card ≤ N.card
      omega
Finset.all_card_le_biUnion_card_iff_existsInjective'
Hall's Marriage Theorem: an indexed family of finite sets admits an injective choice function (system of distinct representatives) if and only if every subcollection of indices s satisfies |s| ≤ |⋃_{i ∈ s} t(i)|.
Finset.card_le_card
If a finite set is a subset of another finite set, its cardinality is less than or equal to that of the other.
Finset.mem_filter
An element belongs to a filtered finite set if and only if it belongs to the base set and satisfies the filtering predicate.
Finset.mem_univ
Every element of a finite type belongs to the universal finite set Finset.univ.
true_and
The conjunction True ∧ p is equivalent to p.
And.right
From a conjunction a ∧ b, deduce the right conjunct b.
Finset.mem_biUnion.mpr
An element belongs to the indexed union ⋃_{i ∈ s} t(i) if there exists some index i ∈ s whose set contains it.
Finset.sum_congr
Two sums over identical finite sets are equal if their summands agree on every element of the index set.
Finset.sum_card_bipartiteAbove_eq_sum_card_bipartiteBelow
In a bipartite relation between two finite sets, the sum of out-degrees from the first set equals the sum of in-degrees to the second set (double counting edges).
Finset.bipartiteAbove
The subset of elements in the target set related to a given source element.
Finset.bipartiteBelow
The subset of elements in the source set related to a given target element.
Finset.sum_le_sum
If f(i) ≤ g(i) for all elements i in a finite set, then the sum of f over that set is at most the sum of g.
mul_comm
Multiplication is commutative (a * b = b * 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

open Finsetset_option maxHeartbeats 2000000theorem finite_two_appearances_system_of_distinct_representatives    {ι α : Type*} [Fintype ι] [DecidableEq ι] [DecidableEq α]    (sets : ι → Finset α)    (hsize : ∀ i, 2 ≤ (sets i).card)    (hfreq : ∀ a, (Finset.univ.filter fun i => a ∈ sets i).card ≤ 2) :    ∃ f : ι → α, Function.Injective f ∧ ∀ i, f i ∈ sets i := by  classical  apply (Finset.all_card_le_biUnion_card_iff_existsInjective' sets).mp  intro J  let N := J.biUnion sets  let d : α → ℕ := fun l => (J.filter fun i => l ∈ sets i).card  have hd2 : ∀ l, d l ≤ 2 := by    intro l    calc      d l ≤ (univ.filter fun i => l ∈ sets i).card := by        apply Finset.card_le_card        intro i hi        simp only [d,Finset.mem_filter] at hi        simp only [Finset.mem_filter, Finset.mem_univ, true_and]        exact hi.2      _ ≤ 2 := hfreq l  have hsets : ∀ i ∈ J, N.filter (fun l => l ∈ sets i) = sets i := by    intro i hi    ext l    simp only [Finset.mem_filter]    constructor    · exact And.right    · intro hl      exact ⟨Finset.mem_biUnion.mpr ⟨i,hi,hl⟩,hl⟩  have hcount : (∑ i ∈ J, (sets i).card) = ∑ l ∈ N, d l := by    calc      _ = ∑ i ∈ J, (N.filter fun l => l ∈ sets i).card := by        apply Finset.sum_congr rfl        intro i hi        rw [hsets i hi]      _ = _ := by        have h := @Finset.sum_card_bipartiteAbove_eq_sum_card_bipartiteBelow ι α          (fun i l => l ∈ sets i) J N _        simpa [Finset.bipartiteAbove, Finset.bipartiteBelow, d] using h  have hsmall : (∑ l ∈ N, d l) ≤ 2 * N.card := by    calc      _ ≤ ∑ _l ∈ N, 2 := Finset.sum_le_sum (by intro l _; exact hd2 l)      _ = _ := by simp [mul_comm]  have hlarge : 2 * J.card ≤ ∑ i ∈ J, (sets i).card := by    calc      _ = ∑ _i ∈ J, 2 := by simp [mul_comm]      _ ≤ _ := Finset.sum_le_sum (by intro i _; exact hsize i)  change J.card ≤ N.card  omega
See it in the graphDiscuss this resultChallenge it