Datasets:
Download lean_project/FormalConjecturesForMathlib/Combinatorics/Basic.lean from nielsr/formal-conjectures: direct link, hf CLI and curl.
- Browser
- Download file 13.5 kB
-
https://huggingface.co/datasets/nielsr/formal-conjectures/resolve/main/lean_project/FormalConjecturesForMathlib/Combinatorics/Basic.lean
- Command line
-
hf download hf://datasets/nielsr/formal-conjectures/lean_project/FormalConjecturesForMathlib/Combinatorics/Basic.lean
-
curl -L -o Basic.lean https://huggingface.co/datasets/nielsr/formal-conjectures/resolve/main/lean_project/FormalConjecturesForMathlib/Combinatorics/Basic.lean
13.5 kB
| /- | |
| Copyright 2025 The Formal Conjectures Authors. | |
| Licensed under the Apache License, Version 2.0 (the "License"); | |
| you may not use this file except in compliance with the License. | |
| You may obtain a copy of the License at | |
| https://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| -/ | |
| module | |
| public import FormalConjecturesForMathlib.Combinatorics.AP.Basic | |
| public import Mathlib.Algebra.Order.Sub.Unbundled.Basic | |
| public import Mathlib.Analysis.Normed.Field.Lemmas | |
| public import Mathlib.Order.CompletePartialOrder | |
| open Function Set | |
| open scoped Pointwise | |
| variable {α : Type*} [AddCommMonoid α] | |
| /-- | |
| A set $S$ is said to be product-free if the product set $S \cdot S$ is disjoint from $S$, | |
| i.e. if the equation $x \cdot y = z$ has no solution with $x, y, z \in S$. | |
| -/ | |
| A set $A$ is said to be sum-free if the sumset $A + A$ is disjoint from $A$, i.e. | |
| if the equation $a + b = c$ has no solution with $a, b, c \in A$. | |
| -/] | |
| def IsProductFree {M : Type*} [Mul M] (S : Set M) : Prop := Disjoint (S * S) S | |
| theorem isProductFree_iff {M : Type*} [Mul M] {S : Set M} : | |
| IsProductFree S ↔ ∀ x ∈ S, ∀ y ∈ S, x * y ∉ S := by | |
| simp [IsProductFree, Set.disjoint_left, Set.mem_mul] | |
| aesop | |
| /-- | |
| `allUniqueSums A` is the set of elements in `α` that can be written as the sum of exactly one | |
| unordered pair of elements from `A`. | |
| -/ | |
| def allUniqueSums (A : Set α) : Set α := | |
| { n | ∃ p : α × α, p.1 ∈ A ∧ p.2 ∈ A ∧ p.1 + p.2 = n ∧ | |
| ∀ a₁ ∈ A, ∀ a₂ ∈ A, a₁ + a₂ = n → (a₁ = p.1 ∧ a₂ = p.2) ∨ (a₁ = p.2 ∧ a₂ = p.1) } | |
| /-- | |
| A set `A` has no unique representation in its sumset `A + A` if for every pair of elements | |
| `a₁, a₂ ∈ A`, there exist another pair of elements `b₁, b₂ ∈ A` such that `a₁ + a₂ = b₁ + b₂` | |
| and `{a₁, a₂} ≠ {b₁, b₂}`. | |
| -/ | |
| def HasNoUniqueRepresentation {α : Type*} [AddCommMonoid α] (A : Finset α) : Prop := | |
| allUniqueSums (A : Set α) = ∅ | |
| /-- A set $A$ of natural numbers is said to have bounded gaps if there exists an integer $p$ such | |
| that $A ∩ [n, n + 1, ..., n + p]$ is nonempty for all $n$. -/ | |
| def IsSyndetic (A : Set ℕ) : Prop := ∃ p, ∀ n, (A ∩ .Icc n (n + p)).Nonempty | |
| /-- A Sidon set is a set, such that such that all pairwise sums of elements are distinct apart from | |
| coincidences forced by the commutativity of addition. -/ | |
| def IsSidon (A : Set α) : Prop := ∀ᵉ (i₁ ∈ A) (j₁ ∈ A) (i₂ ∈ A) (j₂ ∈ A), | |
| i₁ + i₂ = j₁ + j₂ → (i₁ = j₁ ∧ i₂ = j₂) ∨ (i₁ = j₂ ∧ i₂ = j₁) | |
| namespace Set | |
| lemma IsSidon.avoids_isAPOfLength_three {A : Set ℕ} (hA : IsSidon A) | |
| {Y : Set ℕ} (hY : Y.IsAPOfLength 3) : | |
| (A ∩ Y).ncard ≤ 2 := by | |
| simp [IsAPOfLength, IsAPOfLengthWith] at hY | |
| obtain ⟨hc, ⟨a, d, hY⟩⟩ := hY | |
| have hY_card : Y.ncard = 3 := by simp [ncard, hc] | |
| by_contra! h | |
| have hss : Y ⊆ A ∩ Y := by | |
| have hY_fin : Finite Y := finite_of_ncard_ne_zero (by linarith) | |
| rw [Set.eq_of_subset_of_ncard_le (Set.inter_subset_right) (by linarith)] | |
| have ha : a ∈ A := mem_of_mem_inter_left <| hss (hY ▸ ⟨0, by norm_num, by simp⟩) | |
| have ha₁ : a + d ∈ A := mem_of_mem_inter_left <| hss (hY ▸ ⟨1, by norm_num, by simp⟩) | |
| have ha₂ : a + 2 • d ∈ A := mem_of_mem_inter_left <| hss (hY ▸ ⟨2, by norm_num, by simp⟩) | |
| have := hA _ ha _ ha₁ _ ha₂ _ ha₁ (by simp; omega) | |
| simp at this | |
| simp [hY, this.1, ofPred_and] at hY_card | |
| linarith [ncard_singleton _ ▸ ncard_inter_le_ncard_right {a | ∃ x, x < 3} {a}] | |
| theorem IsSidon.subset {A B : Set α} (hB : IsSidon B) (hAB : A ⊆ B) : IsSidon A := | |
| fun _ _ _ _ _ _ _ _ _ ↦ hB _ (hAB ‹_›) _ (hAB ‹_›) _ (hAB ‹_›) _ (hAB ‹_›) ‹_› | |
| theorem IsSidon.insert {A : Set α} {m : α} [IsRightCancelAdd α] [IsLeftCancelAdd α] | |
| (hA : IsSidon A) : | |
| IsSidon (A ∪ {m}) ↔ (m ∈ A ∨ ∀ᵉ (a ∈ A) (b ∈ A), m + m ≠ a + b ∧ ∀ c ∈ A, m + a ≠ b + c) := by | |
| by_cases h_mem : m ∈ A | |
| · exact ⟨fun _ ↦ .inl h_mem, fun _ ↦ by rwa [union_singleton, insert_eq_of_mem h_mem]⟩ | |
| refine ⟨fun h ↦ .inr fun a ha b hb ↦ ⟨fun hc ↦ ?_, fun c hc h_contr ↦ ?_⟩, fun hm ↦ ?_⟩ | |
| · exact h m (by simp) a (by simp [ha]) m (by simp) b (by simp [hb]) hc | |
| |>.elim (fun _ ↦ by simp_all) (fun _ ↦ by simp_all) | |
| · exact h m (by simp) b (by simp [hb]) a (by simp [ha]) c (by simp [hc]) h_contr | |
| |>.elim (fun _ ↦ by simp_all) (fun _ ↦ by simp_all) | |
| · intro i₁ hi₁ | |
| rcases hi₁ with (hi₁ | hi₁) | |
| · intro j₁ hj₁ | |
| rcases hj₁ with (hj₁ | hj₁) | |
| · intro i₂ hi₂ | |
| rcases hi₂ with (hi₂ | hi₂) | |
| · intro j₂ hj₂ | |
| rcases hj₂ with (hj₂ | hj₂) | |
| · exact fun h ↦ hA i₁ hi₁ j₁ hj₁ i₂ hi₂ j₂ hj₂ h | |
| · simp_all | |
| exact fun h ↦ by cases (hm j₁ hj₁ i₁ hi₁).2 i₂ hi₂ (add_comm j₁ m ▸ h.symm) | |
| · simp_all | |
| exact fun a ha h ↦ by cases (hm i₁ hi₁ j₁ hj₁).2 a ha (add_comm i₁ m ▸ h) | |
| · simp_all | |
| refine ⟨fun b hb h ↦ .inr <| by simp_all [add_comm], fun b hb ↦ ⟨fun h ↦ ?_, ?_⟩⟩ | |
| · cases (hm i₁ hi₁ b hb).1 h.symm | |
| · exact fun c hc h ↦ by cases ((hm c hc i₁ hi₁).2 b hb) h.symm | |
| · simp_all | |
| exact fun _ _ _ _ _ ↦ by simp_all [add_comm] | |
| /-! | |
| Maximal Sidon sets in an interval. | |
| We follow the convention that `IsMaximalSidonSetIn A N` means `A ⊆ {1, …, N}` is Sidon and | |
| is inclusion-maximal among subsets of `Set.Icc 1 N` with the Sidon property. | |
| -/ | |
| /-- `IsMaximalSidonSetIn A N` means `A ⊆ {1, …, N}` is Sidon and cannot be extended within | |
| `{1, …, N}` while remaining Sidon. -/ | |
| def IsMaximalSidonSetIn (A : Set ℕ) (N : ℕ) : Prop := | |
| A ⊆ Set.Icc 1 N ∧ IsSidon A ∧ | |
| ∀ ⦃x : ℕ⦄, x ∈ Set.Icc 1 N → x ∉ A → ¬ IsSidon (A ∪ {x}) | |
| /-- Ordered-pair characterisation of the Sidon property: `A` is Sidon iff whenever `a₁ ≤ b₁`, | |
| `a₂ ≤ b₂` are elements of `A` with `a₁ + b₁ = a₂ + b₂`, then `a₁ = a₂` and `b₁ = b₂`. -/ | |
| theorem isSidon_iff_le [LinearOrder α] (A : Set α) : | |
| IsSidon A ↔ | |
| ∀ a₁ ∈ A, ∀ b₁ ∈ A, ∀ a₂ ∈ A, ∀ b₂ ∈ A, | |
| a₁ ≤ b₁ → a₂ ≤ b₂ → a₁ + b₁ = a₂ + b₂ → a₁ = a₂ ∧ b₁ = b₂ := by | |
| constructor | |
| · intro hS a₁ ha₁ b₁ hb₁ a₂ ha₂ b₂ hb₂ hab₁ hab₂ hsum | |
| rcases hS a₁ ha₁ a₂ ha₂ b₁ hb₁ b₂ hb₂ hsum with h | ⟨h₁, h₂⟩ | |
| · exact h | |
| · obtain rfl : a₁ = b₁ := le_antisymm hab₁ (h₂ ▸ h₁ ▸ hab₂) | |
| exact ⟨h₂, h₁⟩ | |
| · intro hS i₁ hi₁ j₁ hj₁ i₂ hi₂ j₂ hj₂ hsum | |
| rcases le_total i₁ i₂ with h₁ | h₁ <;> rcases le_total j₁ j₂ with h₂ | h₂ | |
| · exact .inl (hS i₁ hi₁ i₂ hi₂ j₁ hj₁ j₂ hj₂ h₁ h₂ hsum) | |
| · have := hS i₁ hi₁ i₂ hi₂ j₂ hj₂ j₁ hj₁ h₁ h₂ (hsum.trans (add_comm _ _)) | |
| exact .inr ⟨this.1, this.2⟩ | |
| · have := hS i₂ hi₂ i₁ hi₁ j₁ hj₁ j₂ hj₂ h₁ h₂ ((add_comm _ _).trans hsum) | |
| exact .inr ⟨this.2, this.1⟩ | |
| · have := hS i₂ hi₂ i₁ hi₁ j₂ hj₂ j₁ hj₁ h₁ h₂ | |
| ((add_comm _ _).trans (hsum.trans (add_comm _ _))) | |
| exact .inl ⟨this.2, this.1⟩ | |
| namespace IsMaximalSidonSetIn | |
| /-- If `A` is a maximal Sidon set in `{1, …, N}`, then `A ⊆ {1, …, N}`. -/ | |
| theorem subset {A : Set ℕ} {N : ℕ} (hA : IsMaximalSidonSetIn A N) : | |
| A ⊆ Set.Icc 1 N := hA.1 | |
| /-- If `A` is a maximal Sidon set in `{1, …, N}`, then `A` is Sidon. -/ | |
| theorem isSidon {A : Set ℕ} {N : ℕ} (hA : IsMaximalSidonSetIn A N) : IsSidon A := hA.2.1 | |
| /-- Maximality condition unpacked. -/ | |
| theorem maximal {A : Set ℕ} {N : ℕ} (hA : IsMaximalSidonSetIn A N) {x : ℕ} | |
| (hx : x ∈ Set.Icc 1 N) (hxA : x ∉ A) : ¬ IsSidon (A ∪ {x}) := hA.2.2 hx hxA | |
| end IsMaximalSidonSetIn | |
| end Set | |
| namespace Finset | |
| instance (A : Finset α) [DecidableEq α] : Decidable (IsSidon (A : Set α)) := by | |
| refine decidable_of_iff (∀ᵉ (i₁ ∈ A) (j₁ ∈ A) (i₂ ∈ A) (j₂ ∈ A), | |
| i₁ + i₂ = j₁ + j₂ → (i₁ = j₁ ∧ i₂ = j₂) ∨ (i₁ = j₂ ∧ i₂ = j₁)) ?_ | |
| rfl | |
| /-- In a Sidon set, a positive difference determines its endpoints: if `a₁ - b₁ = a₂ - b₂` with | |
| `b₁ < a₁` and `b₂ < a₂`, then `a₁ = a₂` and `b₁ = b₂`. -/ | |
| theorem IsSidon.eq_of_tsub {β : Type*} [AddCommMonoid β] [PartialOrder β] | |
| [ExistsAddOfLE β] [AddLeftMono β] [Sub β] [OrderedSub β] | |
| {A : Finset β} (hS : IsSidon (A : Set β)) | |
| {a₁ b₁ a₂ b₂ : β} (ha₁ : a₁ ∈ A) (hb₁ : b₁ ∈ A) (ha₂ : a₂ ∈ A) (hb₂ : b₂ ∈ A) | |
| (hlt₁ : b₁ < a₁) (hlt₂ : b₂ < a₂) (heq : a₁ - b₁ = a₂ - b₂) : | |
| a₁ = a₂ ∧ b₁ = b₂ := by | |
| have hsum : a₁ + b₂ = a₂ + b₁ := by | |
| calc | |
| a₁ + b₂ = (a₁ - b₁ + b₁) + b₂ := by rw [tsub_add_cancel_of_le hlt₁.le] | |
| _ = (a₂ - b₂ + b₂) + b₁ := by rw [heq]; ac_rfl | |
| _ = a₂ + b₁ := by rw [tsub_add_cancel_of_le hlt₂.le] | |
| rcases hS a₁ ha₁ a₂ ha₂ b₂ hb₂ b₁ hb₁ hsum with h | h | |
| · exact ⟨h.1, h.2.symm⟩ | |
| · exact (hlt₁.ne' h.1).elim | |
| /-- The maximum size of a Sidon set in the supplied `Finset`. -/ | |
| def maxSidonSubsetCard (A : Finset α) [DecidableEq α] : ℕ := | |
| (A.powerset.filter fun B : Finset α ↦ IsSidon (B : Set α)).sup Finset.card | |
| /-- If `A` is finite Sidon, then `A ∪ {s}` is also Sidon provided `s ≥ A.max + 1`. -/ | |
| theorem IsSidon.insert_ge_max' {A : Finset ℕ} (h : A.Nonempty) (hA : IsSidon (A : Set ℕ)) {s : ℕ} | |
| (hs : 2 * A.max' h + 1 ≤ s) : | |
| IsSidon (A ∪ {s}) := by | |
| have h₁ {a b c : ℕ} (ha : a ∈ A) (hb : b ∈ A) (hc : c ∈ A) : | |
| a + b < 2 * A.max' h + 1 + c := by linarith [A.le_max' _ ha, A.le_max' _ hb] | |
| have : s ∉ A := by | |
| exact mt (A.le_max' _) <| not_le.2 <| Finset.max'_lt_iff _ ‹_› |>.2 fun a ha ↦ by | |
| linarith [A.le_max' _ ha] | |
| exact (IsSidon.insert hA).2 <| by simpa [this] using fun a ha b hb ↦ | |
| ⟨by linarith [A.le_max' _ ha, A.le_max' _ hb], fun c hc ↦ by linarith [h₁ hc hb ha]⟩ | |
| theorem IsSidon.exists_insert {A : Finset ℕ} (h : A.Nonempty) (hA : IsSidon (A : Set ℕ)) : | |
| ∃ m ∉ A, IsSidon (A ∪ {m}) := by | |
| refine ⟨2 * A.max' h + 1, ?_, insert_ge_max' h hA le_rfl⟩ | |
| exact mt (A.le_max' _) <| not_le.2 <| Finset.max'_lt_iff _ ‹_› |>.2 fun a ha ↦ by | |
| linarith [A.le_max' _ ha] | |
| theorem IsSidon.exists_insert_ge {A : Finset ℕ} (h : A.Nonempty) (hA : IsSidon (A : Set ℕ)) (s : ℕ) : | |
| ∃ m ≥ s, m ∉ A ∧ IsSidon (A ∪ {m}) := by | |
| refine ⟨if s ≥ 2 * A.max' h + 1 then s else 2 * A.max' h + 1, ?_, ?_, ?_⟩ | |
| · split_ifs <;> linarith | |
| · split_ifs <;> | |
| exact mt (A.le_max' _) <| not_le.2 <| Finset.max'_lt_iff _ ‹_› |>.2 fun a ha ↦ by | |
| linarith [A.le_max' _ ha] | |
| · split_ifs with hs | |
| · exact insert_ge_max' h hA hs | |
| · exact insert_ge_max' h hA le_rfl | |
| /-- Given a finite Sidon set `A` and a lower bound `m`, `go` finds the smallest number `m' ≥ m` | |
| such that `A ∪ {m'}` is Sidon. If `A` is empty then this returns the value `m`. Note that | |
| the lower bound is required to avoid `0` being a contender in some cases. -/ | |
| def greedySidon.go (A : Finset ℕ) (hA : IsSidon (A : Set ℕ)) (m : ℕ) : | |
| {m' : ℕ // m' ≥ m ∧ m' ∉ A ∧ IsSidon (↑(A ∪ {m'}) : Set ℕ)} := | |
| if h : A.Nonempty then | |
| have : ∃ m', m' ≥ m ∧ m' ∉ A ∧ IsSidon (↑(A ∪ {m'}) : Set ℕ) := by | |
| simpa [and_assoc] using Finset.IsSidon.exists_insert_ge h hA m | |
| ⟨Nat.find this, Nat.find_spec this⟩ | |
| else ⟨m, by simp_all [IsSidon]⟩ | |
| /-- Main search loop for generating the greedy Sidon sequence. The return value for step `n` is the | |
| finite set of numbers generated so far, a proof that it is Sidon, and the greatest element of | |
| the finite set at that point. This is initialised at `{1}`, then `greedySidon.go` is | |
| called iteratively using the lower bound `max + 1` to find the next smallest Sidon preserving | |
| number. -/ | |
| def greedySidon.aux (n : ℕ) : ({A : Finset ℕ // IsSidon (A : Set ℕ)} × ℕ) := | |
| match n with | |
| | 0 => (⟨{1}, by simp [IsSidon]⟩, 1) | |
| | k + 1 => | |
| let (A, s) := greedySidon.aux k | |
| let s := if h : A.1.Nonempty then A.1.max' h + 1 else s | |
| let s' := greedySidon.go A.1 A.2 s | |
| (⟨A.1 ∪ {s'.1}, s'.2.2.2⟩, s'.1) | |
| /-- `greedySidon` is the sequence obtained by the initial set $\{1\}$ and iteratively obtaining | |
| the next smallest integer that preserves the Sidon property of the set. This gives the | |
| sequence `1, 2, 4, 8, 13, 21, 31, ...`. -/ | |
| def greedySidon (n : ℕ) : ℕ := greedySidon.aux n |>.2 | |
| /-- The greedy Sidon set in `{1, …, N}`: starting from `∅`, iterate through `1, …, N` and | |
| include `x` if and only if `A ∪ {x}` remains Sidon. | |
| Alternatively, this is precisely the set of elements in the greedy Sidon sequence that are `≤ N`. -/ | |
| def greedySidonBelow (N : ℕ) : Finset ℕ := | |
| (greedySidon.aux N).1.1.filter (· ≤ N) | |
| end Finset | |