Permutations of a list #
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]]
The r
argument to permutationsAux2
is the same as appending.
The ts
argument to permutationsAux2
can be folded into the f
argument.
The f
argument to permutationsAux2
when r = []
can be eliminated.
An expository lemma to show how all of ts
, r
, and f
can be eliminated from
permutationsAux2
.
(permutationsAux2 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 permutationsAux2 1 [] [] [2, 3, 4] id
-- [[1, 2, 3, 4], [2, 1, 3, 4], [2, 3, 1, 4]]