mathlib documentation



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

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

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

We turn to the second viewpoint on compositions, that we realize as a finset of fin (n+1). c : composition_as_set 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 composition_as_set n. We only construct basic API on composition_as_set (notably c.length and c.blocks) to be able to construct this equiv, called composition_equiv n. Since there is a straightforward equiv between composition_as_set n and finsets of {1, ..., n-1} (obtained by removing 0 and n from a composition_as_set and called composition_as_set_equiv n), we deduce that composition_as_set n and composition n are both fintypes of cardinality 2^(n - 1) (see composition_as_set_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.


Composition, partition


theorem composition.ext_iff {n : } (x y : composition n) :
x = y x.blocks = y.blocks

structure composition  :
→ Type

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

theorem composition.ext {n : } (x y : composition n) :
x.blocks = y.blocksx = y

structure composition_as_set  :
→ Type

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 composition_as_set n.

theorem composition_as_set.ext {n : } (x y : composition_as_set n) :
x.boundaries = y.boundariesx = y


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

def composition.length {n : } :

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

def composition.blocks_fun {n : } (c : composition n) :
fin c.length

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

theorem composition.sum_blocks_fun {n : } (c : composition n) :
∑ (i : fin c.length), c.blocks_fun i = n

theorem composition.one_le_blocks {n : } (c : composition n) {i : } :
i c.blocks1 i

theorem composition.one_le_blocks' {n : } (c : composition n) {i : } (h : i < c.length) :
1 c.blocks.nth_le i h

theorem composition.blocks_pos' {n : } (c : composition n) (i : ) (h : i < c.length) :
0 < c.blocks.nth_le i h

theorem composition.length_le {n : } (c : composition n) :

theorem composition.length_pos_of_pos {n : } (c : composition n) :
0 < n0 < c.length

def composition.size_up_to {n : } :
composition n

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

theorem composition.size_up_to_zero {n : } (c : composition n) :

theorem composition.size_up_to_of_length_le {n : } (c : composition n) (i : ) :
c.length ic.size_up_to i = n


theorem composition.size_up_to_le {n : } (c : composition n) (i : ) :

theorem composition.size_up_to_succ {n : } (c : composition n) {i : } (h : i < c.length) :
c.size_up_to (i + 1) = c.size_up_to i + c.blocks.nth_le i h

theorem composition.size_up_to_strict_mono {n : } (c : composition n) {i : } :
i < c.lengthc.size_up_to i < c.size_up_to (i + 1)

def composition.boundary {n : } (c : composition n) :
fin (c.length + 1)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 composition_as_set n.

theorem composition.boundary_zero {n : } (c : composition n) :
c.boundary 0 = 0


def composition.boundaries {n : } :
composition nfinset (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 composition_as_set n.


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


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

def composition.embedding {n : } (c : composition n) (i : fin c.length) :
fin (c.blocks_fun i)fin n

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

theorem composition.index_exists {n : } (c : composition n) {j : } :
j < n(∃ (i : ), j < c.size_up_to i.succ i < c.length)

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

def composition.index {n : } (c : composition n) :
fin nfin c.length

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

theorem composition.size_up_to_index_le {n : } (c : composition n) (j : fin n) :

def composition.inv_embedding {n : } (c : composition n) (j : fin n) :
fin (c.blocks_fun (c.index j))

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.

theorem composition.embedding_comp_inv {n : } (c : composition n) (j : fin n) :
c.embedding (c.index j) (c.inv_embedding j) = j

theorem composition.disjoint_range {n : } (c : composition n) {i₁ i₂ : fin c.length} :
i₁ i₂disjoint (set.range (c.embedding i₁)) (set.range (c.embedding i₂))

The embeddings of different blocks of a composition are disjoint.

theorem composition.mem_range_embedding {n : } (c : composition n) (j : fin n) :

theorem composition.mem_range_embedding_iff' {n : } (c : composition n) {j : fin n} {i : fin c.length} :

theorem composition.index_embedding {n : } (c : composition n) (i : fin c.length) (j : fin (c.blocks_fun i)) :
c.index (c.embedding i j) = i

theorem composition.inv_embedding_comp {n : } (c : composition n) (i : fin c.length) (j : fin (c.blocks_fun i)) :

def composition.blocks_fin_equiv {n : } (c : composition n) :
(Σ (i : fin c.length), fin (c.blocks_fun i)) fin n

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

theorem composition.blocks_fun_congr {n₁ n₂ : } (c₁ : composition n₁) (c₂ : composition n₂) (i₁ : fin c₁.length) (i₂ : fin c₂.length) :
n₁ = n₂c₁.blocks = c₂.blocksi₁ = i₂c₁.blocks_fun i₁ = c₂.blocks_fun i₂

theorem composition.sigma_eq_iff_blocks_eq {c c' : Σ (n : ), composition 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 made of blocks all of size 1.




theorem composition.eq_ones_iff {n : } {c : composition n} :
c = composition.ones n ∀ (i : ), i c.blocksi = 1

theorem composition.ne_ones_iff {n : } {c : composition n} :
c composition.ones n ∃ (i : ) (H : i c.blocks), 1 < i

def composition.single (n : ) :
0 < ncomposition n

The composition made of a single block of size n.

theorem composition.single_length {n : } (h : 0 < n) :

theorem composition.single_blocks {n : } (h : 0 < n) :


theorem composition.single_embedding {n : } (h : 0 < n) (i : fin n) :

theorem composition.eq_single_iff {n : } {h : 0 < n} {c : composition n} :

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.split_wrt_composition_aux {α : Type u_1} :
list αlist list (list α)

Auxiliary for list.split_wrt_composition.

def list.split_wrt_composition {n : } {α : Type u_1} :
list αcomposition nlist (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.

theorem list.length_split_wrt_composition_aux {α : Type u_1} (l : list α) (ns : list ) :

theorem list.length_split_wrt_composition {n : } {α : Type u_1} (l : list α) (c : composition n) :

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

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_split_wrt_composition {α : Type u_1} {l l' : list α} {c : composition l.length} :

theorem list.nth_le_split_wrt_composition_aux {α : Type u_1} (l : list α) (ns : list ) {i : } (hi : i < (l.split_wrt_composition_aux ns).length) :

theorem list.nth_le_split_wrt_composition {n : } {α : Type u_1} (l : list α) (c : composition n) {i : } (hi : i < (l.split_wrt_composition c).length) :

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

theorem list.join_split_wrt_composition_aux {α : Type u_1} {ns : list } {l : list α} :

theorem list.join_split_wrt_composition {α : Type u_1} (l : list α) (c : composition l.length) :

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


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.

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.


Number of blocks in a composition_as_set.


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


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


List of the sizes of the blocks in a composition_as_set.


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


Equivalence between compositions and compositions as sets

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

theorem composition_card (n : ) :