The areas / V30. Combinatorics
Mathlib.Combinatorics.Enumerative.DyckWord
97 declarations. Mathlib docs · Source
Dyck words
A Dyck word is a sequence consisting of an equal number n of symbols of two types such that
for all prefixes one symbol occurs at least as many times as the other.
If the symbols are ( and ) the latter restriction is equivalent to balanced brackets;
if they are U = (1, 1) and D = (1, -1) the sequence is a lattice path from (0, 0) to (0, 2n)
and the restriction requires the path to never go below the x-axis.
This file defines Dyck words and constructs their bijection with rooted binary trees,
one consequence being that the number of Dyck words with length 2 * n is catalan n.
Main definitions
DyckWord: a list ofUs andDs with as manyUs asDs and with every prefix having at least as manyUs asDs.DyckWord.semilength: semilength (half the length) of a Dyck word.DyckWord.firstReturn: for a nonempty word, the index of theDmatching the initialU.
Main results
DyckWord.equivTree: equivalence between Dyck words and rooted binary trees. See the docstrings ofDyckWord.toTreeandDyckWord.ofTreefor details.DyckWord.equivTreesOfNumNodesEq: equivalence between Dyck words of length2 * nand rooted binary trees withninternal nodes.DyckWord.card_dyckWord_semilength_eq_catalan: there arecatalan nDyck words of length2 * nor semilengthn.
Implementation notes
While any two-valued type could have been used for DyckStep, a new enumerated type is used here
to emphasise that the definition of a Dyck word does not depend on that underlying type.
theorem DyckWord.mk.inj E1 #
∀ {toList : List DyckStep} {count_U_eq_count_D : List.count DyckStep.U toList = List.count DyckStep.D toList}
{count_D_le_count_U :
∀ (i : ℕ), List.count DyckStep.D (List.take i toList) ≤ List.count DyckStep.U (List.take i toList)}
{toList_1 : List DyckStep} {count_U_eq_count_D_1 : List.count DyckStep.U toList_1 = List.count DyckStep.D toList_1}
{count_D_le_count_U_1 :
∀ (i : ℕ), List.count DyckStep.D (List.take i toList_1) ≤ List.count DyckStep.U (List.take i toList_1)},
{ toList := toList, count_U_eq_count_D := count_U_eq_count_D, count_D_le_count_U := count_D_le_count_U } =
{ toList := toList_1, count_U_eq_count_D := count_U_eq_count_D_1, count_D_le_count_U := count_D_le_count_U_1 } →
toList = toList_1
theorem DyckWord.mk.congr_simp E1 #
∀ (toList toList_1 : List DyckStep) (e_toList : toList = toList_1)
(count_U_eq_count_D : List.count DyckStep.U toList = List.count DyckStep.D toList)
(count_D_le_count_U :
∀ (i : ℕ), List.count DyckStep.D (List.take i toList) ≤ List.count DyckStep.U (List.take i toList)),
{ toList := toList, count_U_eq_count_D := count_U_eq_count_D, count_D_le_count_U := count_D_le_count_U } =
{ toList := toList_1, count_U_eq_count_D := ⋯, count_D_le_count_U := ⋯ }
abbrev DyckStep.ofNat E1 #
ℕ → DyckStep
def DyckWord.ctorIdx E1 #
DyckWord → ℕ
theorem DyckWord.ofTree.match_1.congr_eq_1 E1 #
∀ (motive : BinaryTree Unit → Sort u_1) (x : BinaryTree Unit) (h_1 : Unit → motive BinaryTree.nil)
(h_2 : (value : Unit) → (l r : BinaryTree Unit) → motive (BinaryTree.node value l r)),
x = BinaryTree.nil →
(match x with
| BinaryTree.nil => h_1 ()
| BinaryTree.node value l r => h_2 value l r) ≍
h_1 ()
def instInhabitedDyckStep.default E1 #
DyckStep
theorem DyckWord.denest.congr_simp E1 #
∀ (p p_1 : DyckWord) (e_p : p = p_1) (hn : p.IsNested), p.denest hn = p_1.denest ⋯
theorem DyckWord.ofTree.match_1.congr_eq_2 E1 #
∀ (motive : BinaryTree Unit → Sort u_1) (x : BinaryTree Unit) (h_1 : Unit → motive BinaryTree.nil)
(h_2 : (value : Unit) → (l r : BinaryTree Unit) → motive (BinaryTree.node value l r)) (value : Unit)
(l r : BinaryTree Unit),
x = BinaryTree.node value l r →
(match x with
| BinaryTree.nil => h_1 ()
| BinaryTree.node value l r => h_2 value l r) ≍
h_2 value l r
def DyckStep.ctorIdx E1 #
DyckStep → ℕ
theorem DyckWord.mk.injEq E0 #
∀ (toList : List DyckStep) (count_U_eq_count_D : List.count DyckStep.U toList = List.count DyckStep.D toList)
(count_D_le_count_U :
∀ (i : ℕ), List.count DyckStep.D (List.take i toList) ≤ List.count DyckStep.U (List.take i toList))
(toList_1 : List DyckStep) (count_U_eq_count_D_1 : List.count DyckStep.U toList_1 = List.count DyckStep.D toList_1)
(count_D_le_count_U_1 :
∀ (i : ℕ), List.count DyckStep.D (List.take i toList_1) ≤ List.count DyckStep.U (List.take i toList_1)),
({ toList := toList, count_U_eq_count_D := count_U_eq_count_D, count_D_le_count_U := count_D_le_count_U } =
{ toList := toList_1, count_U_eq_count_D := count_U_eq_count_D_1, count_D_le_count_U := count_D_le_count_U_1 }) =
(toList = toList_1)
theorem DyckStep.ofNat_ctorIdx E1 #
∀ (x : DyckStep), DyckStep.ofNat x.ctorIdx = x
theorem DyckWord.drop.congr_simp E1 #
∀ (p p_1 : DyckWord) (e_p : p = p_1) (i i_1 : ℕ) (e_i : i = i_1)
(hi : List.count DyckStep.U (List.take i ↑p) = List.count DyckStep.D (List.take i ↑p)), p.drop i hi = p_1.drop i_1 ⋯
abbrev DyckStep.ctorElimType E1 #
{motive : DyckStep → Sort u} → ℕ → Sort (max 1 u)
theorem DyckWord.take.congr_simp E1 #
∀ (p p_1 : DyckWord) (e_p : p = p_1) (i i_1 : ℕ) (e_i : i = i_1)
(hi : List.count DyckStep.U (List.take i ↑p) = List.count DyckStep.D (List.take i ↑p)), p.take i hi = p_1.take i_1 ⋯
inductive DyckStep E1 #
Type
A DyckStep is either U or D, corresponding to ( and ) respectively.
ctor DyckStep.U E0 #
DyckStep
ctor DyckStep.D E0 #
DyckStep
instance instDecidableEqDyckStep E0 #
DecidableEq DyckStep
instance instInhabitedDyckStep E0 #
Inhabited DyckStep
theorem DyckStep.dichotomy E1 #
∀ (s : DyckStep), s = DyckStep.U ∨ s = DyckStep.D
Named in analogy to Bool.dichotomy.
structure DyckWord E3 #
Type
A Dyck word is a list of DyckSteps with as many Us as Ds and with every prefix having
at least as many Us as Ds.
theorem DyckWord.ext_iff E0 #
∀ {x y : DyckWord}, x = y ↔ ↑x = ↑y
theorem DyckWord.ext E1 #
∀ {x y : DyckWord}, ↑x = ↑y → x = y
ctor DyckWord.mk E0 #
(toList : List DyckStep) →
List.count DyckStep.U toList = List.count DyckStep.D toList →
(∀ (i : ℕ), List.count DyckStep.D (List.take i toList) ≤ List.count DyckStep.U (List.take i toList)) → DyckWord
abbrev DyckWord.toList E1 #
DyckWord → List DyckStep
The underlying list
theorem DyckWord.count_U_eq_count_D E1 #
∀ (self : DyckWord), List.count DyckStep.U ↑self = List.count DyckStep.D ↑self
There are as many Us as Ds
theorem DyckWord.count_D_le_count_U E1 #
∀ (self : DyckWord) (i : ℕ), List.count DyckStep.D (List.take i ↑self) ≤ List.count DyckStep.U (List.take i ↑self)
Each prefix has at least as many Us as Ds
instance instDecidableEqDyckWord E0 #
DecidableEq DyckWord
def instDecidableEqDyckWord.decEq E1 #
(x x_1 : DyckWord) → Decidable (x = x_1)
instance instCoeDyckWordListDyckStep E0 #
Coe DyckWord (List DyckStep)
instance instAddDyckWord E0 #
Add DyckWord
instance instZeroDyckWord E0 #
Zero DyckWord
instance instAddCancelMonoidDyckWord E0 #
AddCancelMonoid DyckWord
Dyck words form an additive cancellative monoid under concatenation, with the empty word as 0.
theorem DyckWord.toList_eq_nil E1 #
∀ {p : DyckWord}, ↑p = [] ↔ p = 0
theorem DyckWord.toList_ne_nil E1 #
∀ {p : DyckWord}, ↑p ≠ [] ↔ p ≠ 0
instance DyckWord.instUniqueAddUnits E0 #
Unique (AddUnits DyckWord)
The only Dyck word that is an additive unit is the empty word.
theorem DyckWord.head_eq_U E1 #
∀ (p : DyckWord) (h : ↑p ≠ []), (↑p).head h = DyckStep.U
The first element of a nonempty Dyck word is U.
theorem DyckWord.getLast_eq_D E1 #
∀ (p : DyckWord) (h : ↑p ≠ []), (↑p).getLast h = DyckStep.D
The last element of a nonempty Dyck word is D.
theorem DyckWord.cons_tail_dropLast_concat E1 #
∀ {p : DyckWord}, p ≠ 0 → DyckStep.U :: (↑p).dropLast.tail ++ [DyckStep.D] = ↑p
def DyckWord.take E1 #
(p : DyckWord) → (i : ℕ) → List.count DyckStep.U (List.take i ↑p) = List.count DyckStep.D (List.take i ↑p) → DyckWord
Prefix of a Dyck word as a Dyck word, given that the count of Us and Ds in it are equal.
def DyckWord.drop E1 #
(p : DyckWord) → (i : ℕ) → List.count DyckStep.U (List.take i ↑p) = List.count DyckStep.D (List.take i ↑p) → DyckWord
Suffix of a Dyck word as a Dyck word, given that the count of Us and Ds in the prefix
are equal.
def DyckWord.nest E1 #
DyckWord → DyckWord
Nest p in one pair of brackets, i.e. x becomes (x).
theorem DyckWord.nest_ne_zero E0 #
∀ {p : DyckWord}, p.nest ≠ 0
def DyckWord.IsNested E1 #
DyckWord → Prop
A property stating that p is nonempty and strictly positive in its interior,
i.e. is of the form (x) with x a Dyck word.
theorem DyckWord.IsNested.nest E1 #
∀ {p : DyckWord}, p.nest.IsNested
def DyckWord.denest E1 #
(p : DyckWord) → p.IsNested → DyckWord
Denest p, i.e. (x) becomes x, given that p.IsNested.
theorem DyckWord.nest_denest E1 #
∀ (p : DyckWord) (hn : p.IsNested), (p.denest hn).nest = p
theorem DyckWord.denest_nest E1 #
∀ (p : DyckWord), p.nest.denest ⋯ = p
def DyckWord.semilength E3 #
DyckWord → ℕ
The semilength of a Dyck word is half of the number of DyckSteps in it, or equivalently
its number of Us.
theorem DyckWord.semilength_zero E0 #
DyckWord.semilength 0 = 0
theorem DyckWord.semilength_add E0 #
∀ {p q : DyckWord}, (p + q).semilength = p.semilength + q.semilength
theorem DyckWord.semilength_nest E0 #
∀ {p : DyckWord}, p.nest.semilength = p.semilength + 1
theorem DyckWord.semilength_eq_count_D E1 #
∀ {p : DyckWord}, p.semilength = List.count DyckStep.D ↑p
theorem DyckWord.two_mul_semilength_eq_length E0 #
∀ {p : DyckWord}, 2 * p.semilength = (↑p).length
def DyckWord.firstReturn E3 #
DyckWord → ℕ
p.firstReturn is 0 if p = 0 and the index of the D matching the initial U otherwise.
theorem DyckWord.firstReturn_zero E0 #
DyckWord.firstReturn 0 = 0
theorem DyckWord.firstReturn_pos E1 #
∀ {p : DyckWord}, p ≠ 0 → 0 < p.firstReturn
theorem DyckWord.firstReturn_lt_length E1 #
∀ {p : DyckWord}, p ≠ 0 → p.firstReturn < (↑p).length
theorem DyckWord.count_take_firstReturn_add_one E1 #
∀ {p : DyckWord},
p ≠ 0 →
List.count DyckStep.U (List.take (p.firstReturn + 1) ↑p) = List.count DyckStep.D (List.take (p.firstReturn + 1) ↑p)
theorem DyckWord.count_D_lt_count_U_of_lt_firstReturn E1 #
∀ {p : DyckWord} {i : ℕ},
i < p.firstReturn → List.count DyckStep.D (List.take (i + 1) ↑p) < List.count DyckStep.U (List.take (i + 1) ↑p)
theorem DyckWord.firstReturn_add E0 #
∀ {p q : DyckWord}, (p + q).firstReturn = if p = 0 then q.firstReturn else p.firstReturn
theorem DyckWord.firstReturn_nest E0 #
∀ {p : DyckWord}, p.nest.firstReturn = (↑p).length + 1
def DyckWord.insidePart E1 #
DyckWord → DyckWord
The left part of the Dyck word decomposition,
inside the U, D pair that firstReturn refers to. insidePart 0 = 0.
def DyckWord.outsidePart E1 #
DyckWord → DyckWord
The right part of the Dyck word decomposition,
outside the U, D pair that firstReturn refers to. outsidePart 0 = 0.
theorem DyckWord.insidePart_zero E0 #
DyckWord.insidePart 0 = 0
theorem DyckWord.outsidePart_zero E0 #
DyckWord.outsidePart 0 = 0
theorem DyckWord.insidePart_add E0 #
∀ {p q : DyckWord}, p ≠ 0 → (p + q).insidePart = p.insidePart
theorem DyckWord.outsidePart_add E0 #
∀ {p q : DyckWord}, p ≠ 0 → (p + q).outsidePart = p.outsidePart + q
theorem DyckWord.insidePart_nest E0 #
∀ {p : DyckWord}, p.nest.insidePart = p
theorem DyckWord.outsidePart_nest E0 #
∀ {p : DyckWord}, p.nest.outsidePart = 0
theorem DyckWord.nest_insidePart_add_outsidePart E0 #
∀ {p : DyckWord}, p ≠ 0 → p.insidePart.nest + p.outsidePart = p
theorem DyckWord.semilength_insidePart_add_semilength_outsidePart_add_one E1 #
∀ {p : DyckWord}, p ≠ 0 → p.insidePart.semilength + p.outsidePart.semilength + 1 = p.semilength
theorem DyckWord.semilength_insidePart_lt E1 #
∀ {p : DyckWord}, p ≠ 0 → p.insidePart.semilength < p.semilength
theorem DyckWord.semilength_outsidePart_lt E1 #
∀ {p : DyckWord}, p ≠ 0 → p.outsidePart.semilength < p.semilength
instance DyckWord.instPreorder E0 #
Preorder DyckWord
theorem DyckWord.le_add_self E1 #
∀ (p q : DyckWord), q ≤ p + q
theorem DyckWord.zero_le E1 #
∀ (p : DyckWord), 0 ≤ p
theorem DyckWord.infix_of_le E1 #
∀ {p q : DyckWord}, p ≤ q → ↑p <:+: ↑q
theorem DyckWord.le_of_suffix E1 #
∀ {p q : DyckWord}, ↑p <:+ ↑q → p ≤ q
instance DyckWord.instPartialOrder E0 #
PartialOrder DyckWord
Partial order on Dyck words: p ≤ q if a (possibly empty) sequence of
insidePart and outsidePart operations can turn q into p.
theorem DyckWord.pos_iff_ne_zero E1 #
∀ {p : DyckWord}, 0 < p ↔ p ≠ 0
theorem DyckWord.monotone_semilength E1 #
Monotone DyckWord.semilength
theorem DyckWord.strictMono_semilength E1 #
StrictMono DyckWord.semilength
def DyckWord.toTree E3 #
DyckWord → BinaryTree Unit
Convert a Dyck word to a binary rooted tree.
f(0) = nil. For a nonzero word find the D that matches the initial U,
which has index p.firstReturn, then let x be everything strictly between said U and D,
and y be everything strictly after said D. p = x.nest + y with x, y (possibly empty)
Dyck words. f(p) = f(x) △ f(y), where △ (defined in Mathlib/Data/Tree/Basic.lean) joins two
subtrees to a new root node.
def DyckWord.ofTree E3 #
BinaryTree Unit → DyckWord
Convert a binary rooted tree to a Dyck word.
g(nil) = 0. A nonempty tree with left subtree l and right subtree r
is sent to g(l).nest + g(r).
theorem DyckWord.ofTree_toTree E1 #
∀ (p : DyckWord), DyckWord.ofTree p.toTree = p
theorem DyckWord.toTree_ofTree E1 #
∀ (t : BinaryTree Unit), (DyckWord.ofTree t).toTree = t
def DyckWord.equivTree E3 #
DyckWord ≃ BinaryTree Unit
Equivalence between Dyck words and rooted binary trees.
theorem DyckWord.equivTree_symm_apply E0 #
∀ (a : BinaryTree Unit), DyckWord.equivTree.symm a = DyckWord.ofTree a
theorem DyckWord.equivTree_apply E0 #
∀ (p : DyckWord), DyckWord.equivTree p = p.toTree
theorem DyckWord.numNodes_toTree E0 #
∀ (p : DyckWord), p.toTree.numNodes = p.semilength
def DyckWord.equivTreesOfNumNodesEq E3 #
(n : ℕ) → { p // p.semilength = n } ≃ ↥(BinaryTree.treesOfNumNodesEq n)
Equivalence between Dyck words of semilength n and rooted binary trees with
n internal nodes.
theorem DyckWord.equivTreesOfNumNodesEq_symm_apply_coe E0 #
∀ (n : ℕ) (b : ↥(BinaryTree.treesOfNumNodesEq n)), ↑((DyckWord.equivTreesOfNumNodesEq n).symm b) = DyckWord.ofTree ↑b
theorem DyckWord.equivTreesOfNumNodesEq_apply_coe E0 #
∀ (n : ℕ) (a : { a // (fun p ↦ p.semilength = n) a }), ↑((DyckWord.equivTreesOfNumNodesEq n) a) = (↑a).toTree
instance DyckWord.instFintypeSubtypeEqNatSemilength E0 #
{n : ℕ} → Fintype { p // p.semilength = n }
theorem DyckWord.card_dyckWord_semilength_eq_catalan E3 #
∀ (n : ℕ), Fintype.card { p // p.semilength = n } = catalan n
There are catalan n Dyck words of semilength n (or length 2 * n).
def Mathlib.Meta.Positivity.evalDyckWordFirstReturn E1 #
Mathlib.Meta.Positivity.PositivityExt
Extension for the positivity tactic: p.firstReturn is positive if p is nonzero.