Documentation

CslibFrontier.Foundations.Data.List.Palindrome

Palindromic factors of lists #

Index characterizations of Mathlib's List.Palindrome, for whole lists (List.Palindrome.iff_getElem?) and for factors l.extract a b (List.palindrome_extract_iff), and the three facts about palindromic factors that center-based palindrome algorithms use: shrinking a palindromic factor symmetrically keeps it a palindrome (List.Palindrome.extract_of_le), a palindromic factor extends by one letter on each side iff those two letters agree (List.palindrome_extract_sub_one_add_one_iff), and palindromic factors inside a palindromic factor reflect to palindromic factors (List.Palindrome.extract_reflect).

List.palindromicFactors collects distinct palindromic words occurring as contiguous factors, including the empty word. Prepending a letter introduces at most one new palindrome.

Main results #

theorem List.Palindrome.iff_getElem? {α : Type u_1} {l : List α} :
l.Palindrome ↔ ∀ i < l.length, l[i]? = l[l.length - 1 - i]?

Index characterization of palindromes.

theorem List.palindrome_extract_iff {α : Type u_1} {l : List α} {a b : ℕ} (hb : b ≤ l.length) :
(l.extract a b).Palindrome ↔ ∀ (i : ℕ), a ≤ i → i < b → l[i]? = l[a + b - 1 - i]?

A factor l.extract a b with b ≤ l.length is a palindrome iff the letters at positions symmetric about the midpoint of [a, b) agree.

theorem List.Palindrome.extract_of_le {α : Type u_1} {l : List α} {a b a' b' : ℕ} (h : (l.extract a b).Palindrome) (hb : b ≤ l.length) (ha : a ≤ a') (hb' : b' ≤ b) (hsum : a' + b' = a + b) :

Shrinking a palindromic factor symmetrically about its center keeps it a palindrome.

theorem List.palindrome_extract_sub_one_add_one_iff {α : Type u_1} {l : List α} {a b : ℕ} (ha : 0 < a) (hab : a ≤ b) (hb : b < l.length) :
(l.extract (a - 1) (b + 1)).Palindrome ↔ (l.extract a b).Palindrome ∧ l[a - 1]? = l[b]?

A palindromic factor extends by one letter on each side iff those two letters agree.

theorem List.Palindrome.extract_reflect {α : Type u_1} {l : List α} {L R a b : ℕ} (hbig : (l.extract L R).Palindrome) (h : (l.extract a b).Palindrome) (hR : R ≤ l.length) (hL : L ≤ a) (hab : a ≤ b) (hbR : b ≤ R) :
(l.extract (L + R - b) (L + R - a)).Palindrome

Palindromes reflect inside a palindrome: if l.extract L R is a palindrome and L ≤ a ≤ b ≤ R, then the factor l.extract a b is a palindrome iff its mirror image l.extract (L + R - b) (L + R - a) is; this is the forward direction.

Counting distinct palindromic factors #

def List.palindromicFactors {α : Type u_1} [DecidableEq α] (w : List α) :

The distinct palindromic factors of a finite word, including the empty word.

@[simp]

Prepending a letter introduces at most one distinct palindrome. Any new factor is a prefix. If two palindromic prefixes have different lengths, reflection in the longer one places the shorter one in the old word.

A word of length n has at most n + 1 distinct palindromic factors, including the empty word. This is the Droubay–Justin–Pirillo bound as restated in Glen, Justin, Widmer and Zamboni, Proposition 2.1. The alphabet need not be finite.