# Documentation

Mathlib.Combinatorics.Composition

# Compositions #

A composition of a natural number n is a decomposition n = i₀ + ... + i_{k-1} of n into a sum of positive integers. Combinatorially, it corresponds to a decomposition of {0, ..., n-1} into non-empty blocks of consecutive integers, where the iⱼ are the lengths of the blocks. This notion is closely related to that of a partition of n, but in a composition of n the order of the iⱼs matters.

We implement two different structures covering these two viewpoints on compositions. The first one, made of a list of positive integers summing to n, is the main one and is called Composition n. The second one is useful for combinatorial arguments (for instance to show that the number of compositions of n is 2^(n-1)). It is given by a subset of {0, ..., n} containing 0 and n, where the elements of the subset (other than n) correspond to the leftmost points of each block. The main API is built on Composition n, and we provide an equivalence between the two types.

## Main functions #

• c : Composition n is a structure, made of a list of integers which are all positive and add up to n.
• composition_card states that the cardinality of Composition n is exactly 2^(n-1), which is proved by constructing an equiv with CompositionAsSet n (see below), which is itself in bijection with the subsets of Fin (n-1) (this holds even for n = 0, where - is nat subtraction).

Let c : Composition n be a composition of n. Then

• c.blocks is the list of blocks in c.

• c.length is the number of blocks in the composition.

• c.blocks_fun : Fin c.length → ℕ is the realization of c.blocks as a function on Fin c.length. This is the main object when using compositions to understand the composition of analytic functions.

• c.sizeUpTo : ℕ → ℕ is the sum of the size of the blocks up to i.;

• c.embedding i : Fin (c.blocks_fun i) → Fin n is the increasing embedding of the i-th block in Fin n;

• c.index j, for j : Fin n, is the index of the block containing j.

• Composition.ones n is the composition of n made of ones, i.e., [1, ..., 1].

• Composition.single n (hn : 0 < n) is the composition of n made of a single block of size n.

Compositions can also be used to split lists. Let l be a list of length n and c a composition of n.

• l.splitWrtComposition c is a list of lists, made of the slices of l corresponding to the blocks of c.
• join_splitWrtComposition states that splitting a list and then joining it gives back the original list.
• joinSplitWrtComposition_join states that joining a list of lists, and then splitting it back according to the right composition, gives back the original list of lists.

We turn to the second viewpoint on compositions, that we realize as a finset of Fin (n+1). c : CompositionAsSet n is a structure made of a finset of Fin (n+1) called c.boundaries and proofs that it contains 0 and n. (Taking a finset of Fin n containing 0 would not make sense in the edge case n = 0, while the previous description works in all cases). The elements of this set (other than n) correspond to leftmost points of blocks. Thus, there is an equiv between Composition n and CompositionAsSet n. We only construct basic API on CompositionAsSet (notably c.length and c.blocks) to be able to construct this equiv, called compositionEquiv n. Since there is a straightforward equiv between CompositionAsSet n and finsets of {1, ..., n-1} (obtained by removing 0 and n from a CompositionAsSet and called compositionAsSetEquiv n), we deduce that CompositionAsSet n and Composition n are both fintypes of cardinality 2^(n - 1) (see compositionAsSet_card and composition_card).

## Implementation details #

The main motivation for this structure and its API is in the construction of the composition of formal multilinear series, and the proof that the composition of analytic functions is analytic.

The representation of a composition as a list is very handy as lists are very flexible and already have a well-developed API.

## Tags #

Composition, partition

## References #

https://en.wikipedia.org/wiki/Composition_(combinatorics)

theorem Composition.ext {n : } (x : ) (y : ) (blocks : x.blocks = y.blocks) :
x = y
theorem Composition.ext_iff {n : } (x : ) (y : ) :
x = y x.blocks = y.blocks
structure Composition (n : ) :
• blocks :

List of positive integers summing to n

• blocks_pos : ∀ {i : }, i s.blocks0 < i

Proof of positivity for blocks

• blocks_sum : List.sum s.blocks = n

Proof that blocks sums to n

A composition of n is a list of positive integers summing to n.

Instances For
theorem CompositionAsSet.ext_iff {n : } (x : ) (y : ) :
x = y x.boundaries = y.boundaries
theorem CompositionAsSet.ext {n : } (x : ) (y : ) (boundaries : x.boundaries = y.boundaries) :
x = y
structure CompositionAsSet (n : ) :
• boundaries : Finset (Fin ())

Combinatorial viewpoint on a composition of n as consecutive integers {0, ..., n-1}

• zero_mem : 0 s.boundaries

Proof that 0 is a member of boundaries

• getLast_mem : s.boundaries

Last element of the composition

Combinatorial viewpoint on a composition of n, by seeing it as non-empty blocks of consecutive integers in {0, ..., n-1}. We register every block by its left end-point, yielding a finset containing 0. As this does not make sense for n = 0, we add n to this finset, and get a finset of {0, ..., n} containing 0 and n. This is the data in the structure CompositionAsSet n.

Instances For

### Compositions #

A composition of an integer n is a decomposition n = i₀ + ... + i_{k-1} of n into a sum of positive integers.

@[reducible]
def Composition.length {n : } (c : ) :

The length of a composition, i.e., the number of blocks in the composition.

Instances For
theorem Composition.blocks_length {n : } (c : ) :
List.length c.blocks =
def Composition.blocksFun {n : } (c : ) :

The blocks of a composition, seen as a function on Fin c.length. When composing analytic functions using compositions, this is the main player.

Instances For
theorem Composition.ofFn_blocksFun {n : } (c : ) :
= c.blocks
theorem Composition.sum_blocksFun {n : } (c : ) :
(Finset.sum Finset.univ fun i => ) = n
theorem Composition.blocksFun_mem_blocks {n : } (c : ) (i : ) :
c.blocks
@[simp]
theorem Composition.one_le_blocks {n : } (c : ) {i : } (h : i c.blocks) :
1 i
@[simp]
theorem Composition.one_le_blocks' {n : } (c : ) {i : } (h : ) :
1 List.nthLe c.blocks i h
@[simp]
theorem Composition.blocks_pos' {n : } (c : ) (i : ) (h : ) :
0 < List.nthLe c.blocks i h
theorem Composition.one_le_blocksFun {n : } (c : ) (i : ) :
theorem Composition.length_le {n : } (c : ) :
theorem Composition.length_pos_of_pos {n : } (c : ) (h : 0 < n) :
def Composition.sizeUpTo {n : } (c : ) (i : ) :

The sum of the sizes of the blocks in a composition up to i.

Instances For
@[simp]
theorem Composition.sizeUpTo_zero {n : } (c : ) :
theorem Composition.sizeUpTo_ofLength_le {n : } (c : ) (i : ) (h : ) :
@[simp]
theorem Composition.sizeUpTo_length {n : } (c : ) :
theorem Composition.sizeUpTo_le {n : } (c : ) (i : ) :
theorem Composition.sizeUpTo_succ {n : } (c : ) {i : } (h : ) :
Composition.sizeUpTo c (i + 1) = + List.nthLe c.blocks i h
theorem Composition.sizeUpTo_succ' {n : } (c : ) (i : ) :
theorem Composition.sizeUpTo_strict_mono {n : } (c : ) {i : } (h : ) :
def Composition.boundary {n : } (c : ) :
Fin () ↪o Fin (n + 1)

The i-th boundary of a composition, i.e., the leftmost point of the i-th block. We include a virtual point at the right of the last block, to make for a nice equiv with CompositionAsSet n.

Instances For
@[simp]
theorem Composition.boundary_zero {n : } (c : ) :
↑() 0 = 0
@[simp]
theorem Composition.boundary_last {n : } (c : ) :
↑() () =
def Composition.boundaries {n : } (c : ) :
Finset (Fin (n + 1))

The boundaries of a composition, i.e., the leftmost point of all the blocks. We include a virtual point at the right of the last block, to make for a nice equiv with CompositionAsSet n.

Instances For

To c : Composition n, one can associate a CompositionAsSet n by registering the leftmost point of each block, and adding a virtual point at the right of the last block.

Instances For

The canonical increasing bijection between Fin (c.length + 1) and c.boundaries is exactly c.boundary.

def Composition.embedding {n : } (c : ) (i : ) :
Fin () ↪o Fin n

Embedding the i-th block of a composition (identified with Fin (c.blocks_fun i)) into Fin n at the relevant position.

Instances For
@[simp]
theorem Composition.coe_embedding {n : } (c : ) (i : ) (j : Fin ()) :
↑(↑() j) = + j
theorem Composition.index_exists {n : } (c : ) {j : } (h : j < n) :
i, j <

index_exists asserts there is some i with j < c.size_up_to (i+1). In the next definition index we use nat.find to produce the minimal such index.

def Composition.index {n : } (c : ) (j : Fin n) :

c.index j is the index of the block in the composition c containing j.

Instances For
theorem Composition.lt_sizeUpTo_index_succ {n : } (c : ) (j : Fin n) :
j <
theorem Composition.sizeUpTo_index_le {n : } (c : ) (j : Fin n) :
j
def Composition.invEmbedding {n : } (c : ) (j : Fin n) :
Fin ()

Mapping an element j of Fin n to the element in the block containing it, identified with Fin (c.blocks_fun (c.index j)) through the canonical increasing bijection.

Instances For
@[simp]
theorem Composition.coe_invEmbedding {n : } (c : ) (j : Fin n) :
↑() = j -
theorem Composition.embedding_comp_inv {n : } (c : ) (j : Fin n) :
↑() () = j
theorem Composition.mem_range_embedding_iff {n : } (c : ) {j : Fin n} {i : } :
j j j <
theorem Composition.disjoint_range {n : } (c : ) {i₁ : } {i₂ : } (h : i₁ i₂) :
Disjoint (Set.range ↑()) (Set.range ↑())

The embeddings of different blocks of a composition are disjoint.

theorem Composition.mem_range_embedding {n : } (c : ) (j : Fin n) :
j Set.range ↑()
theorem Composition.mem_range_embedding_iff' {n : } (c : ) {j : Fin n} {i : } :
j i =
theorem Composition.index_embedding {n : } (c : ) (i : ) (j : Fin ()) :
Composition.index c (↑() j) = i
theorem Composition.invEmbedding_comp {n : } (c : ) (i : ) (j : Fin ()) :
↑(Composition.invEmbedding c (↑() j)) = j
def Composition.blocksFinEquiv {n : } (c : ) :
(i : ) × Fin () Fin n

Equivalence between the disjoint union of the blocks (each of them seen as Fin (c.blocks_fun i)) with Fin n.

Instances For
theorem Composition.blocksFun_congr {n₁ : } {n₂ : } (c₁ : ) (c₂ : ) (i₁ : ) (i₂ : ) (hn : n₁ = n₂) (hc : c₁.blocks = c₂.blocks) (hi : i₁ = i₂) :
=
theorem Composition.sigma_eq_iff_blocks_eq {c : (n : ) × } {c' : (n : ) × } :
c = c' c.snd.blocks = c'.snd.blocks

Two compositions (possibly of different integers) coincide if and only if they have the same sequence of blocks.

### The composition Composition.ones#

def Composition.ones (n : ) :

The composition made of blocks all of size 1.

Instances For
@[simp]
@[simp]
theorem Composition.ones_blocks (n : ) :
().blocks =
@[simp]
theorem Composition.ones_blocksFun (n : ) (i : ) :
@[simp]
theorem Composition.ones_sizeUpTo (n : ) (i : ) :
= min i n
@[simp]
theorem Composition.ones_embedding {n : } (i : ) (h : ) :
↑() { val := 0, isLt := h } = { val := i, isLt := (_ : i < n) }
theorem Composition.eq_ones_iff {n : } {c : } :
∀ (i : ), i c.blocksi = 1
theorem Composition.ne_ones_iff {n : } {c : } :
i, i c.blocks 1 < i

### The composition Composition.single#

def Composition.single (n : ) (h : 0 < n) :

The composition made of a single block of size n.

Instances For
@[simp]
theorem Composition.single_length {n : } (h : 0 < n) :
@[simp]
theorem Composition.single_blocks {n : } (h : 0 < n) :
().blocks = [n]
@[simp]
theorem Composition.single_blocksFun {n : } (h : 0 < n) (i : ) :
= n
@[simp]
theorem Composition.single_embedding {n : } (h : 0 < n) (i : Fin n) :
↑() i = i
theorem Composition.eq_single_iff_length {n : } (h : 0 < n) {c : } :
c =
theorem Composition.ne_single_iff {n : } (hn : 0 < n) {c : } :
c ∀ (i : ),

### Splitting a list #

Given a list of length n and a composition c of n, one can split l into c.length sublists of respective lengths c.blocks_fun 0, ..., c.blocks_fun (c.length-1). This is inverse to the join operation.

def List.splitWrtCompositionAux {α : Type u_1} :
List αList (List α)

Auxiliary for List.splitWrtComposition.

Equations
Instances For
def List.splitWrtComposition {n : } {α : Type u_1} (l : List α) (c : ) :
List (List α)

Given a list of length n and a composition [i₁, ..., iₖ] of n, split l into a list of k lists corresponding to the blocks of the composition, of respective lengths i₁, ..., iₖ. This makes sense mostly when n = l.length, but this is not necessary for the definition.

Instances For
theorem List.splitWrtCompositionAux_cons {α : Type u_1} (l : List α) (n : ) (ns : ) :
theorem List.length_splitWrtCompositionAux {α : Type u_1} (l : List α) (ns : ) :
@[simp]
theorem List.length_splitWrtComposition {n : } {α : Type u_1} (l : List α) (c : ) :

When one splits a list along a composition c, the number of sublists thus created is c.length.

theorem List.map_length_splitWrtCompositionAux {α : Type u_1} {ns : } {l : List α} :
List.sum ns List.map List.length () = ns
theorem List.map_length_splitWrtComposition {α : Type u_1} (l : List α) (c : ) :
List.map List.length () = c.blocks

When one splits a list along a composition c, the lengths of the sublists thus created are given by the block sizes in c.

theorem List.length_pos_of_mem_splitWrtComposition {α : Type u_1} {l : List α} {l' : List α} {c : } (h : ) :
0 <
theorem List.sum_take_map_length_splitWrtComposition {α : Type u_1} (l : List α) (c : ) (i : ) :
List.sum (List.take i (List.map List.length ())) =
theorem List.nthLe_splitWrtCompositionAux {α : Type u_1} (l : List α) (ns : ) {i : } (hi : ) :
theorem List.nthLe_splitWrtComposition {n : } {α : Type u_1} (l : List α) (c : ) {i : } (hi : ) :

The i-th sublist in the splitting of a list l along a composition c, is the slice of l between the indices c.sizeUpTo i and c.sizeUpTo (i+1), i.e., the indices in the i-th block of the composition.

theorem List.get_splitWrtComposition {n : } {α : Type u_1} (l : List α) (c : ) (i : ) :
theorem List.join_splitWrtCompositionAux {α : Type u_1} {ns : } {l : List α} :
List.sum ns =
@[simp]
theorem List.join_splitWrtComposition {α : Type u_1} (l : List α) (c : ) :

If one splits a list along a composition, and then joins the sublists, one gets back the original list.

@[simp]
theorem List.splitWrtComposition_join {α : Type u_1} (L : List (List α)) (c : ) (h : List.map List.length L = c.blocks) :

If one joins a list of lists and then splits the join along the right composition, one gets back the original list of lists.

### Compositions as sets #

Combinatorial viewpoints on compositions, seen as finite subsets of Fin (n+1) containing 0 and n, where the points of the set (other than n) correspond to the leftmost points of each block.

def compositionAsSetEquiv (n : ) :
Finset (Fin (n - 1))

Bijection between compositions of n and subsets of {0, ..., n-2}, defined by considering the restriction of the subset to {1, ..., n-1} and shifting to the left by one.

Instances For
theorem compositionAsSet_card (n : ) :
= 2 ^ (n - 1)
theorem CompositionAsSet.card_boundaries_pos {n : } (c : ) :
0 < Finset.card c.boundaries
def CompositionAsSet.length {n : } (c : ) :

Number of blocks in a CompositionAsSet.

Instances For
theorem CompositionAsSet.lt_length {n : } (c : ) (i : ) :
i + 1 < Finset.card c.boundaries
theorem CompositionAsSet.lt_length' {n : } (c : ) (i : ) :
i < Finset.card c.boundaries
def CompositionAsSet.boundary {n : } (c : ) :
Fin (Finset.card c.boundaries) ↪o Fin (n + 1)

Canonical increasing bijection from Fin c.boundaries.card to c.boundaries.

Instances For
@[simp]
theorem CompositionAsSet.boundary_zero {n : } (c : ) :
↑() { val := 0, isLt := (_ : 0 < Finset.card c.boundaries) } = 0
@[simp]
theorem CompositionAsSet.boundary_length {n : } (c : ) :
↑() { val := , isLt := (_ : < Finset.card c.boundaries) } =
def CompositionAsSet.blocksFun {n : } (c : ) (i : ) :

Size of the i-th block in a CompositionAsSet, seen as a function on Fin c.length.

Instances For
theorem CompositionAsSet.blocksFun_pos {n : } (c : ) (i : ) :
def CompositionAsSet.blocks {n : } (c : ) :

List of the sizes of the blocks in a CompositionAsSet.

Instances For
@[simp]
theorem CompositionAsSet.blocks_length {n : } (c : ) :
theorem CompositionAsSet.blocks_partial_sum {n : } (c : ) {i : } (h : i < Finset.card c.boundaries) :
= ↑(↑() { val := i, isLt := h })
theorem CompositionAsSet.mem_boundaries_iff_exists_blocks_sum_take_eq {n : } (c : ) {j : Fin (n + 1)} :
j c.boundaries i, i < Finset.card c.boundaries = j
theorem CompositionAsSet.blocks_sum {n : } (c : ) :

Associating a Composition n to a CompositionAsSet n, by registering the sizes of the blocks as a list of positive integers.

Instances For

### Equivalence between compositions and compositions as sets #

In this section, we explain how to go back and forth between a Composition and a CompositionAsSet, by showing that their blocks and length and boundaries correspond to each other, and construct an equivalence between them called compositionEquiv.

@[simp]
@[simp]
@[simp]
@[simp]
@[simp]
theorem CompositionAsSet.toComposition_boundaries {n : } (c : ) :
= c.boundaries
@[simp]
theorem Composition.toCompositionAsSet_boundaries {n : } (c : ) :
().boundaries =
def compositionEquiv (n : ) :

Equivalence between Composition n and CompositionAsSet n.

Instances For
instance compositionFintype (n : ) :
theorem composition_card (n : ) :
= 2 ^ (n - 1)