Documentation

CslibFrontier.Foundations.Combinatorics.Words.Avoidability

Word Pattern Reflection, Avoidability, Free Sets, and Zimin Words #

A finite pattern u : List A is reflected by a word v : List B (Cslib.Word.Reflects v u) if there is a map φ : A → List B sending every letter occurring in u to a nonempty word over B such that u.flatMap φ occurs as a contiguous factor of v. Following Bean, Ehrenfeucht, and McNulty [BEMcN79], a pattern w : List A is unavoidable (Cslib.Word.IsUnavoidable) if for every finite alphabet B, only finitely many words over B fail to reflect w; otherwise w is avoidable (Cslib.Word.IsAvoidable).

This module develops the foundational algebra of pattern reflection, avoidability of repeated adjacent letters via Thue's ternary square-free words [Thu06], letter deletion and identification, free letters and free sets via alternating 2-factor chains [BeanEhrenfeuchtMcNulty1979, Definition 3.18], the Bean–Ehrenfeucht–McNulty telescoping morphism lift across free-set deletion [BeanEhrenfeuchtMcNulty1979, Lemma 3.20], canonical Zimin words Z_k (Cslib.Word.ziminWord) [Zim84], the equivalence between free-set reducibility and reflection in Z_k [Sapir2014, Section 2.5], and the unavoidability of every Z_k under standard Lean axioms.

Main definitions #

Main results #

def Cslib.Word.Reflects {A : Type u_1} {B : Type u_2} (v : List B) (u : List A) :

A word v : List B reflects a pattern u : List A if there is a map φ : A → List B sending every letter occurring in u to a nonempty word over B such that u.flatMap φ is a contiguous factor (infix) of v.

theorem Cslib.Word.reflects_iff {A : Type u_1} {B : Type u_2} {v : List B} {u : List A} :
Reflects v u ↔ ∃ (φ : A → List B), (∀ a ∈ u, φ a ≠ []) ∧ List.flatMap φ u <:+: v

Unfolded characterization of Reflects.

theorem Cslib.Word.reflects_iff_exists_forall_ne_nil {A : Type u_1} {B : Type u_2} [Nonempty B] {v : List B} {u : List A} :
Reflects v u ↔ ∃ (φ : A → List B), (∀ (a : A), φ a ≠ []) ∧ List.flatMap φ u <:+: v

Over a nonempty target alphabet B, Reflects v u is equivalent to existence of a globally non-erasing morphism φ : A → List B (∀ a, φ a ≠ []) with u.flatMap φ <:+: v.

@[simp]
theorem Cslib.Word.reflects_nil {A : Type u_1} {B : Type u_2} (v : List B) :

Every word reflects the empty pattern.

theorem Cslib.Word.Reflects.refl {A : Type u_1} (w : List A) :

Reflection is reflexive.

theorem Cslib.Word.Reflects.of_infix {A : Type u_1} {B : Type u_2} {u : List A} {v₁ v₂ : List B} (h₁ : Reflects v₁ u) (h₂ : v₁ <:+: v₂) :
Reflects v₂ u

Reflection is preserved when extending the host word by factor containment.

theorem Cslib.Word.Reflects.of_isInfix {A : Type u_1} {u w : List A} (h : u <:+: w) :

Every contiguous factor of w is reflected by w.

theorem Cslib.Word.Reflects.trans {A : Type u_1} {B : Type u_2} {C : Type u_3} {u : List A} {v : List B} {w : List C} (h₁ : Reflects v u) (h₂ : Reflects w v) :

Reflection is transitive across morphisms.

theorem Cslib.Word.reflects_map {A : Type u_1} {B : Type u_2} (f : A → B) (w : List A) :

Any letter-wise image w.map f reflects w.

theorem Cslib.Word.Reflects.of_injective_map {A : Type u_1} {B : Type u_2} {f : A → B} (hf : Function.Injective f) (w : List A) :

An injective letter-wise renaming reflects in both directions: w reflects w.map f.

theorem Cslib.Word.Reflects.length_le {A : Type u_1} {B : Type u_2} {v : List B} {u : List A} (h : Reflects v u) :

Any word reflecting u has length at least u.length.

def Cslib.Word.IsUnavoidableFor {A : Type u_1} {B : Type u_2} (L : Set (List B)) (w : List A) :

A pattern w : List A is unavoidable for a language L : Set (List B) if all but finitely many words in L reflect w.

theorem Cslib.Word.isUnavoidableFor_iff {A : Type u_1} {B : Type u_2} {L : Set (List B)} {w : List A} :

Unfolded characterization of IsUnavoidableFor.

def Cslib.Word.IsUnavoidable {A : Type u_1} (w : List A) :

A pattern w : List A is unavoidable if for every finite alphabet Fin k, all but finitely many words over Fin k reflect w.

Unfolded characterization of IsUnavoidable.

def Cslib.Word.IsAvoidable {A : Type u_1} (w : List A) :

A pattern w : List A is avoidable if it is not unavoidable.

Unfolded characterization of IsAvoidable.

An unavoidable pattern is unavoidable for Set.univ : Set (List B) over any finite alphabet B.

theorem Cslib.Word.IsUnavoidable.isUnavoidableFor {A : Type u_1} {B : Type u_2} [Finite B] {w : List A} (hw : IsUnavoidable w) (L : Set (List B)) :

Over any finite alphabet B and any language L : Set (List B), an unavoidable pattern is unavoidable for L.

theorem Cslib.Word.isAvoidable_of_infinite_not_reflects {A : Type u_1} {B : Type u_2} [Finite B] {L : Set (List B)} {w : List A} (hL : L.Infinite) (hav : ∀ v ∈ L, ¬Reflects v w) :

Existence of an infinite language over a finite alphabet avoiding w implies IsAvoidable w.

theorem Cslib.Word.isUnavoidableFor_univ_iff_exists_length {A : Type u_1} {B : Type u_2} [Finite B] {w : List A} :
IsUnavoidableFor Set.univ w ↔ ∃ (N : ℕ), ∀ (v : List B), N ≤ v.length → Reflects v w

Over any finite alphabet B, w is unavoidable for Set.univ : Set (List B) if and only if there exists a threshold length N such that every word over B of length at least N reflects w.

theorem Cslib.Word.IsUnavoidable.exists_length_forall_reflects {A : Type u_1} {B : Type u_2} [Finite B] {w : List A} (hw : IsUnavoidable w) :
∃ (N : ℕ), ∀ (v : List B), N ≤ v.length → Reflects v w

Over any finite alphabet B, an unavoidable pattern is reflected by every sufficiently long word over B.

@[simp]

The empty word is unavoidable.

@[simp]

Every single-letter pattern is unavoidable.

theorem Cslib.Word.Reflects.isUnavoidable {A : Type u_1} {B : Type u_2} {u : List A} {v : List B} (huv : Reflects v u) (hv : IsUnavoidable v) :

If v reflects u and v is unavoidable, then u is unavoidable.

theorem Cslib.Word.isUnavoidable_map_iff {A : Type u_1} {B : Type u_2} {f : A → B} (hf : Function.Injective f) {w : List A} :

Injective letter renaming preserves unavoidability in both directions.

theorem Cslib.Word.isAvoidable_of_pair_infix {A : Type u_1} {w : List A} {x : A} (hx : [x, x] <:+: w) :

Any pattern containing an adjacent repeated letter [x, x] is avoidable (via Thue's ternary square-free words).

noncomputable def Cslib.Word.deleteLetters {A : Type u_1} (X : Set A) (w : List A) :

Delete all occurrences of letters belonging to X : Set A from w : List A (w^X).

noncomputable def Cslib.Word.deleteLetter {A : Type u_1} (x : A) (w : List A) :

Delete all occurrences of a single letter x : A from w : List A (w^x).

Singleton-set characterization of deleteLetter.

noncomputable def Cslib.Word.identifyLetters {A : Type u_1} (x y : A) (w : List A) :

Replace every occurrence of x in w by y (letter identification).

@[simp]

Deleting any letter set from [] yields [].

@[simp]
theorem Cslib.Word.deleteLetters_append {A : Type u_1} (X : Set A) (u v : List A) :

deleteLetters distributes over word concatenation.

theorem Cslib.Word.deleteLetters_cons_of_mem {A : Type u_1} {X : Set A} {a : A} (ha : a ∈ X) (w : List A) :

deleteLetters X drops a head letter belonging to X.

theorem Cslib.Word.deleteLetters_cons_of_not_mem {A : Type u_1} {X : Set A} {a : A} (ha : a ∉ X) (w : List A) :

deleteLetters X retains a head letter outside X.

@[simp]
theorem Cslib.Word.mem_deleteLetters {A : Type u_1} {X : Set A} {w : List A} {a : A} :
a ∈ deleteLetters X w ↔ a ∈ w ∧ a ∉ X

Membership in deleteLetters X w holds iff the letter lies in w and outside X.

theorem Cslib.Word.length_deleteLetters_le {A : Type u_1} (X : Set A) (w : List A) :

Deleting letters does not increase word length.

theorem Cslib.Word.deleteLetters_eq_filter {A : Type u_1} (X : Set A) [DecidablePred fun (x : A) => x ∈ X] (w : List A) :
deleteLetters X w = List.filter (fun (a : A) => decide ¬decide (a ∈ X) = true) w

Computable List.filter characterization of deleteLetters under [DecidablePred (· ∈ X)].

theorem Cslib.Word.deleteLetters_deleteLetters {A : Type u_1} (X Y : Set A) (w : List A) :

Deleting X then Y equals deleting X ∪ Y.

theorem Cslib.Word.deleteLetters_eq_self {A : Type u_1} {X : Set A} {w : List A} (h : ∀ a ∈ w, a ∉ X) :

If no letter of X occurs in w, deleteLetters X w = w.

theorem Cslib.Word.IsInfix.deleteLetters {A : Type u_1} {u v : List A} (h : u <:+: v) (X : Set A) :

Factor containment is preserved under deleteLetters.

@[simp]
theorem Cslib.Word.deleteLetter_nil {A : Type u_1} (x : A) :

Deleting x from [] yields [].

@[simp]
theorem Cslib.Word.deleteLetter_append {A : Type u_1} (x : A) (u v : List A) :

deleteLetter distributes over word concatenation.

theorem Cslib.Word.deleteLetter_cons_eq {A : Type u_1} (x : A) (w : List A) :

deleteLetter x drops a matching head letter x.

theorem Cslib.Word.deleteLetter_cons_ne {A : Type u_1} {x a : A} (hne : a ≠ x) (w : List A) :

deleteLetter x retains any head letter a ≠ x.

@[simp]
theorem Cslib.Word.mem_deleteLetter {A : Type u_1} {x a : A} {w : List A} :
a ∈ deleteLetter x w ↔ a ∈ w ∧ a ≠ x

Membership in deleteLetter x w holds iff the letter lies in w and differs from x.

theorem Cslib.Word.deleteLetter_eq_filter {A : Type u_1} [DecidableEq A] (x : A) (w : List A) :
deleteLetter x w = List.filter (fun (x_1 : A) => decide (x_1 ≠ x)) w

Computable List.filter characterization of deleteLetter under [DecidableEq A].

@[simp]
theorem Cslib.Word.identifyLetters_nil {A : Type u_1} (x y : A) :

Letter identification on [] yields [].

@[simp]
theorem Cslib.Word.identifyLetters_append {A : Type u_1} (x y : A) (u v : List A) :

identifyLetters distributes over word concatenation.

theorem Cslib.Word.identifyLetters_cons_eq {A : Type u_1} (x y : A) (w : List A) :

identifyLetters x y replaces a head x by y.

theorem Cslib.Word.identifyLetters_cons_ne {A : Type u_1} {x y a : A} (hne : a ≠ x) (w : List A) :

identifyLetters x y leaves any head a ≠ x unchanged.

theorem Cslib.Word.identifyLetters_eq_map {A : Type u_1} [DecidableEq A] (x y : A) (w : List A) :
identifyLetters x y w = List.map (fun (a : A) => if a = x then y else a) w

Computable List.map characterization of identifyLetters under [DecidableEq A].

theorem Cslib.Word.reflects_identifyLetters {A : Type u_1} (x y : A) (w : List A) :

identifyLetters x y w always reflects w.

def Cslib.Word.HasAlternatingChain {A : Type u_1} (w : List A) (x y : A) :

Existence of an alternating 2-factor chain x a₁, b₁ a₁, b₁ a₂, …, b_{n-1} a_n, b_n a_n, b_n y (n > 0) among the contiguous 2-factors of w.

def Cslib.Word.IsFreeLetter {A : Type u_1} (w : List A) (x : A) :

A letter x : A is free for w : List A if x occurs in w and has no alternating 2-factor chain from x back to x in w.

theorem Cslib.Word.isFreeLetter_iff {A : Type u_1} {w : List A} {x : A} :

Unfolded characterization of IsFreeLetter.

def Cslib.Word.IsFreeSet {A : Type u_1} (w : List A) (X : Set A) :

A set of letters X : Set A is free for w : List A if no pair x, y ∈ X is linked by an alternating 2-factor chain in w.

theorem Cslib.Word.isFreeSet_iff {A : Type u_1} {w : List A} {X : Set A} :
IsFreeSet w X ↔ ∀ x ∈ X, ∀ y ∈ X, ¬HasAlternatingChain w x y

Unfolded characterization of IsFreeSet.

A letter x occurring in w is free if and only if {x} is a free set for w.

theorem Cslib.Word.IsFreeSet.isFreeLetter {A : Type u_1} {w : List A} {X : Set A} (hX : IsFreeSet w X) {x : A} (hxX : x ∈ X) (hxw : x ∈ w) :

Every letter of a free set X that occurs in w is a free letter for w.

theorem Cslib.Word.IsFreeSet.mono {A : Type u_1} {w : List A} {X Y : Set A} (hY : IsFreeSet w Y) (hXY : X ⊆ Y) :

Every subset of a free set is a free set.

theorem Cslib.Word.hasAlternatingChain_of_pair_infix {A : Type u_1} {w : List A} {x y : A} (hxy : [x, y] <:+: w) :

Any direct 2-factor [x, y] <:+: w yields a length-1 alternating chain from x to y.

theorem Cslib.Word.IsFreeSet.not_pair_infix {A : Type u_1} {w : List A} {X : Set A} (hX : IsFreeSet w X) {x y : A} (hx : x ∈ X) (hy : y ∈ X) :
¬[x, y] <:+: w

No two letters of a free set can occur adjacently in w.

def Cslib.Word.SameRightNeighbor {A : Type u_1} (w : List A) (u v : A) :

Shared-right-neighbor step ∃ c, [u, c] <:+: w ∧ [v, c] <:+: w on left components of 2-factors of w.

theorem Cslib.Word.sameRightNeighbor_iff {A : Type u_1} {w : List A} {u v : A} :
SameRightNeighbor w u v ↔ ∃ (c : A), [u, c] <:+: w ∧ [v, c] <:+: w

Unfolded characterization of SameRightNeighbor.

Graph-reachability characterization of HasAlternatingChain: an alternating 2-factor chain from x to y exists if and only if some b reachable from x via SameRightNeighbor w steps satisfies [b, y] <:+: w.

theorem Cslib.Word.flatMap_telescoping {A : Type u_1} {B : Type u_2} {δR δL ψ φ₀ : A → List B} (v : List A) (hv : v ≠ []) :
(∀ z ∈ v, δR z ++ ψ z = φ₀ z ++ δL z) → (∀ (y z : A), [y, z] <:+: v → δL y = δR z) → δR (v.head hv) ++ List.flatMap ψ v = List.flatMap φ₀ v ++ δL (v.getLast hv)

Boundary-cancelling telescoping identity for word morphisms along any nonempty word v.

theorem Cslib.Word.reflects_append_singleton_of_isFreeSet {A : Type u_1} {B : Type u_2} {w : List A} {X : Set A} (hX : IsFreeSet w X) (a : B) {φ : A → List B} (hφ : ∀ z ∈ w, z ∉ X → ∃ (u : List B), u ≠ [] ∧ φ z = a :: u) :
∃ (ψ : A → List B), (∀ z ∈ w, ψ z ≠ []) ∧ List.flatMap ψ w <:+: List.flatMap φ (deleteLetters X w) ++ [a]

Telescoping Block-Morphism Lemma: if X is a free set for w : List A and every letter z ∈ w outside X maps under φ to a word a :: u z starting with a : B of length at least 2 (u z ≠ []), then there exists a morphism ψ : A → List B sending every letter of w to a nonempty word such that w.flatMap ψ <:+: (deleteLetters X w).flatMap φ ++ [a].

Canonical Zimin words ziminWord k : List ℕ (Z_0 = [], Z_{k+1} = Z_k ++ [k] ++ Z_k).

@[simp]

The 0-th Zimin word is empty.

Recursive unfolding ziminWord (k + 1) = ziminWord k ++ [k] ++ ziminWord k.

@[simp]

Length of the k-th Zimin word is 2 ^ k - 1.

theorem Cslib.Word.mem_ziminWord_lt {k m : ℕ} (hm : m ∈ ziminWord k) :
m < k

Every letter occurring in ziminWord k is strictly less than k.

theorem Cslib.Word.ziminWord_succ_eq_flatMap (k : ℕ) :
ziminWord (k + 1) = List.flatMap (fun (m : ℕ) => [0, m + 1]) (ziminWord k) ++ [0]

Interleaved 0-block expansion ziminWord (k + 1) = (ziminWord k).flatMap (fun m ↦ [0, m + 1]) ++ [0].

theorem Cslib.Word.IsFreeSet.reflects_ziminWord_succ {A : Type u_1} {w : List A} {X : Set A} (hX : IsFreeSet w X) {k : ℕ} (hk : Reflects (ziminWord k) (deleteLetters X w)) :
Reflects (ziminWord (k + 1)) w

If X is a free set for w and ziminWord k reflects deleteLetters X w, then ziminWord (k + 1) reflects w.

inductive Cslib.Word.IsFreeSetReducible {A : Type u_1} :
List A → Prop

Reducibility of a pattern to [] by iteratively deleting free sets ([Zim84]; [Sapir2014, Section 2.5]).

inductive Cslib.Word.IsBEMReducible {A : Type u_1} :
List A → Prop

Reducibility of a pattern to [] by iteratively deleting a free letter or identifying two distinct letters occurring in the pattern ([BeanEhrenfeuchtMcNulty1979, Section 3, p. 284]).

theorem Cslib.Word.pair_getLast_head_infix {A : Type u_1} {u v : List A} (hu : u ≠ []) (hv : v ≠ []) :
[u.getLast hu, v.head hv] <:+: u ++ v

The last letter of a nonempty word u followed by the first letter of a nonempty word v forms a contiguous 2-factor of u ++ v.

Free-set reducibility is equivalent to reflection in some Zimin word ziminWord k.

Every canonical Zimin word ziminWord k is unavoidable.