Permutations of a list #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
In this file we prove properties about list.permutations
, a list of all permutations of a list. It
is defined in data.list.defs
.
Order of the permutations #
Designed for performance, the order in which the permutations appear in list.permutations
is
rather intricate and not very amenable to induction. That's why we also provide list.permutations'
as a less efficient but more straightforward way of listing permutations.
list.permutations
#
TODO. In the meantime, you can try decrypting the docstrings.
list.permutations'
#
The list of partitions is built by recursion. The permutations of []
are [[]]
. Then, the
permutations of a :: l
are obtained by taking all permutations of l
in order and adding a
in
all positions. Hence, to build [0, 1, 2, 3].permutations'
, it does
[[]]
[[3]]
[[2, 3], [3, 2]]]
[[1, 2, 3], [2, 1, 3], [2, 3, 1], [1, 3, 2], [3, 1, 2], [3, 2, 1]]
[[0, 1, 2, 3], [1, 0, 2, 3], [1, 2, 0, 3], [1, 2, 3, 0],
[0, 2, 1, 3], [2, 0, 1, 3], [2, 1, 0, 3], [2, 1, 3, 0],
[0, 2, 3, 1], [2, 0, 3, 1], [2, 3, 0, 1], [2, 3, 1, 0],
[0, 1, 3, 2], [1, 0, 3, 2], [1, 3, 0, 2], [1, 3, 2, 0],
[0, 3, 1, 2], [3, 0, 1, 2], [3, 1, 0, 2], [3, 1, 2, 0],
[0, 3, 2, 1], [3, 0, 2, 1], [3, 2, 0, 1], [3, 2, 1, 0]]
TODO #
Show that l.nodup → l.permutations.nodup
. See data.fintype.list
.
The r
argument to permutations_aux2
is the same as appending.
The ts
argument to permutations_aux2
can be folded into the f
argument.
An expository lemma to show how all of ts
, r
, and f
can be eliminated from
permutations_aux2
.
(permutations_aux2 t [] [] ys id).2
, which appears on the RHS, is a list whose elements are
produced by inserting t
into every non-terminal position of ys
in order. As an example:
#eval permutations_aux2 1 [] [] [2, 3, 4] id
-- [[1, 2, 3, 4], [2, 1, 3, 4], [2, 3, 1, 4]]