The areas / V33. Data structures and computation
Mathlib.Data.List.Permutation
56 declarations and 1 deprecated aliases. Mathlib docs · Source
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]]
theorem List.permutationsAux2.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} {β : Type u_2} (motive : List α × List β → Sort u_3) (x : List α × List β)
(h_1 : (us : List α) → (zs : List β) → motive (us, zs)) (us : List α) (zs : List β),
x = (us, zs) →
(match x with
| (us, zs) => h_1 us zs) ≍
h_1 us zs
theorem List.permutationsAux2.match_3.congr_eq_1 E1 #
∀ {α : Type u_1} {β : Type u_3} (motive : List α → (List α → β) → Sort u_2) (x : List α) (x_1 : List α → β)
(h_1 : (x : List α → β) → motive [] x) (h_2 : (y : α) → (ys : List α) → (f : List α → β) → motive (y :: ys) f)
(x_2 : List α → β),
x = [] →
x_1 = x_2 →
(match x, x_1 with
| [], x => h_1 x
| y :: ys, f => h_2 y ys f) ≍
h_1 x_2
theorem List.permutationsAux.rec.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} (motive : List α → List α → Sort u_2) (x x_1 : List α) (h_1 : (is : List α) → motive [] is)
(h_2 : (t : α) → (ts is : List α) → motive (t :: ts) is) (t : α) (ts is : List α),
x = t :: ts →
x_1 = is →
(match x, x_1 with
| [], is => h_1 is
| t :: ts, is => h_2 t ts is) ≍
h_2 t ts is
theorem List.permutationsAux2.match_3.congr_eq_2 E1 #
∀ {α : Type u_1} {β : Type u_3} (motive : List α → (List α → β) → Sort u_2) (x : List α) (x_1 : List α → β)
(h_1 : (x : List α → β) → motive [] x) (h_2 : (y : α) → (ys : List α) → (f : List α → β) → motive (y :: ys) f) (y : α)
(ys : List α) (f : List α → β),
x = y :: ys →
x_1 = f →
(match x, x_1 with
| [], x => h_1 x
| y :: ys, f => h_2 y ys f) ≍
h_2 y ys f
theorem List.permutationsAux.rec.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} (motive : List α → List α → Sort u_2) (x x_1 : List α) (h_1 : (is : List α) → motive [] is)
(h_2 : (t : α) → (ts is : List α) → motive (t :: ts) is) (is : List α),
x = [] →
x_1 = is →
(match x, x_1 with
| [], is => h_1 is
| t :: ts, is => h_2 t ts is) ≍
h_1 is
theorem List.permutationsAux2_fst E1 #
∀ {α : Type u_1} {β : Type u_2} (t : α) (ts : List α) (r : List β) (ys : List α) (f : List α → β),
(List.permutationsAux2 t ts r ys f).1 = ys ++ ts
theorem List.permutationsAux2_snd_nil E0 #
∀ {α : Type u_1} {β : Type u_2} (t : α) (ts : List α) (r : List β) (f : List α → β),
(List.permutationsAux2 t ts r [] f).2 = r
theorem List.permutationsAux2_snd_cons E0 #
∀ {α : Type u_1} {β : Type u_2} (t : α) (ts : List α) (r : List β) (y : α) (ys : List α) (f : List α → β),
(List.permutationsAux2 t ts r (y :: ys) f).2 =
f (t :: y :: ys ++ ts) :: (List.permutationsAux2 t ts r ys fun x ↦ f (y :: x)).2
theorem List.permutationsAux2_append E1 #
∀ {α : Type u_1} {β : Type u_2} (t : α) (ts : List α) (r : List β) (ys : List α) (f : List α → β),
(List.permutationsAux2 t ts [] ys f).2 ++ r = (List.permutationsAux2 t ts r ys f).2
The r argument to permutationsAux2 is the same as appending.
theorem List.permutationsAux2_comp_append E1 #
∀ {α : Type u_1} {β : Type u_2} {t : α} {ts ys : List α} {r : List β} (f : List α → β),
(List.permutationsAux2 t [] r ys fun x ↦ f (x ++ ts)).2 = (List.permutationsAux2 t ts r ys f).2
The ts argument to permutationsAux2 can be folded into the f argument.
theorem List.map_permutationsAux2' E0 #
∀ {α : Type u_1} {β : Type u_2} {α' : Type u_3} {β' : Type u_4} (g : α → α') (g' : β → β') (t : α) (ts ys : List α)
(r : List β) (f : List α → β) (f' : List α' → β'),
(∀ (a : List α), g' (f a) = f' (List.map g a)) →
List.map g' (List.permutationsAux2 t ts r ys f).2 =
(List.permutationsAux2 (g t) (List.map g ts) (List.map g' r) (List.map g ys) f').2
theorem List.map_permutationsAux2 E0 #
∀ {α : Type u_1} {β : Type u_2} (t : α) (ts ys : List α) (f : List α → β),
List.map f (List.permutationsAux2 t ts [] ys id).2 = (List.permutationsAux2 t ts [] ys f).2
The f argument to permutationsAux2 when r = [] can be eliminated.
theorem List.permutationsAux2_snd_eq E0 #
∀ {α : Type u_1} {β : Type u_2} (t : α) (ts : List α) (r : List β) (ys : List α) (f : List α → β),
(List.permutationsAux2 t ts r ys f).2 = List.map (fun x ↦ f (x ++ ts)) (List.permutationsAux2 t [] [] ys id).2 ++ r
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]]
theorem List.map_map_permutationsAux2 E0 #
∀ {α : Type u_1} {α' : Type u_3} (g : α → α') (t : α) (ts ys : List α),
List.map (List.map g) (List.permutationsAux2 t ts [] ys id).2 =
(List.permutationsAux2 (g t) (List.map g ts) [] (List.map g ys) id).2
theorem List.map_map_permutations'Aux E0 #
∀ {α : Type u_1} {β : Type u_2} (f : α → β) (t : α) (ts : List α),
List.map (List.map f) (List.permutations'Aux t ts) = List.permutations'Aux (f t) (List.map f ts)
theorem List.permutations'Aux_eq_permutationsAux2 E1 #
∀ {α : Type u_1} (t : α) (ts : List α), List.permutations'Aux t ts = (List.permutationsAux2 t [] [ts ++ [t]] ts id).2
theorem List.mem_permutationsAux2 E0 #
∀ {α : Type u_1} {t : α} {ts ys l l' : List α},
l' ∈ (List.permutationsAux2 t ts [] ys fun x ↦ l ++ x).2 ↔
∃ l₁ l₂, l₂ ≠ [] ∧ ys = l₁ ++ l₂ ∧ l' = l ++ l₁ ++ t :: l₂ ++ ts
theorem List.mem_permutationsAux2' E0 #
∀ {α : Type u_1} {t : α} {ts ys l : List α},
l ∈ (List.permutationsAux2 t ts [] ys id).2 ↔ ∃ l₁ l₂, l₂ ≠ [] ∧ ys = l₁ ++ l₂ ∧ l = l₁ ++ t :: l₂ ++ ts
theorem List.length_permutationsAux2 E1 #
∀ {α : Type u_1} {β : Type u_2} (t : α) (ts ys : List α) (f : List α → β),
(List.permutationsAux2 t ts [] ys f).2.length = ys.length
theorem List.foldr_permutationsAux2 E1 #
∀ {α : Type u_1} (t : α) (ts : List α) (r L : List (List α)),
List.foldr (fun y r ↦ (List.permutationsAux2 t ts r y id).2) r L =
List.flatMap (fun y ↦ (List.permutationsAux2 t ts [] y id).2) L ++ r
theorem List.mem_foldr_permutationsAux2 E0 #
∀ {α : Type u_1} {t : α} {ts : List α} {r L : List (List α)} {l' : List α},
l' ∈ List.foldr (fun y r ↦ (List.permutationsAux2 t ts r y id).2) r L ↔
l' ∈ r ∨ ∃ l₁ l₂, l₁ ++ l₂ ∈ L ∧ l₂ ≠ [] ∧ l' = l₁ ++ t :: l₂ ++ ts
theorem List.length_foldr_permutationsAux2 E1 #
∀ {α : Type u_1} (t : α) (ts : List α) (r L : List (List α)),
(List.foldr (fun y r ↦ (List.permutationsAux2 t ts r y id).2) r L).length = (List.map List.length L).sum + r.length
theorem List.length_foldr_permutationsAux2' E1 #
∀ {α : Type u_1} (t : α) (ts : List α) (r L : List (List α)) (n : ℕ),
(∀ l ∈ L, l.length = n) →
(List.foldr (fun y r ↦ (List.permutationsAux2 t ts r y id).2) r L).length = n * L.length + r.length
theorem List.permutationsAux_nil E0 #
∀ {α : Type u_1} (is : List α), [].permutationsAux is = []
theorem List.permutationsAux_cons E0 #
∀ {α : Type u_1} (t : α) (ts is : List α),
(t :: ts).permutationsAux is =
List.foldr (fun y r ↦ (List.permutationsAux2 t ts r y id).2) (ts.permutationsAux (t :: is)) is.permutations
theorem List.permutations_nil E0 #
∀ {α : Type u_1}, [].permutations = [[]]
theorem List.map_permutationsAux E0 #
∀ {α : Type u_1} {β : Type u_2} (f : α → β) (ts is : List α),
List.map (List.map f) (ts.permutationsAux is) = (List.map f ts).permutationsAux (List.map f is)
theorem List.map_permutations E0 #
∀ {α : Type u_1} {β : Type u_2} (f : α → β) (ts : List α),
List.map (List.map f) ts.permutations = (List.map f ts).permutations
theorem List.map_permutations' E0 #
∀ {α : Type u_1} {β : Type u_2} (f : α → β) (ts : List α),
List.map (List.map f) ts.permutations' = (List.map f ts).permutations'
theorem List.permutationsAux_append E1 #
∀ {α : Type u_1} (is is' ts : List α),
(is ++ ts).permutationsAux is' =
List.map (fun x ↦ x ++ ts) (is.permutationsAux is') ++ ts.permutationsAux (is.reverse ++ is')
theorem List.permutations_append E1 #
∀ {α : Type u_1} (is ts : List α),
(is ++ ts).permutations = List.map (fun x ↦ x ++ ts) is.permutations ++ ts.permutationsAux is.reverse
theorem List.perm_of_mem_permutationsAux E0 #
∀ {α : Type u_1} {ts is l : List α}, l ∈ ts.permutationsAux is → l.Perm (ts ++ is)
theorem List.perm_of_mem_permutations E1 #
∀ {α : Type u_1} {l₁ l₂ : List α}, l₁ ∈ l₂.permutations → l₁.Perm l₂
theorem List.length_permutationsAux E0 #
∀ {α : Type u_1} (ts is : List α),
(ts.permutationsAux is).length + is.length.factorial = (ts.length + is.length).factorial
theorem List.length_permutations E1 #
∀ {α : Type u_1} (l : List α), l.permutations.length = l.length.factorial
theorem List.mem_permutationsAux_of_perm E0 #
∀ {α : Type u_1} {ts is l : List α},
l.Perm (is ++ ts) → (∃ is', ∃ (_ : is'.Perm is), l = is' ++ ts) ∨ l ∈ ts.permutationsAux is
theorem List.mem_permutations E0 #
∀ {α : Type u_1} {s t : List α}, s ∈ t.permutations ↔ s.Perm t
theorem List.perm_pair E1 #
∀ {α : Type u_1} {a b : α} {l : List α}, l.Perm [a, b] ↔ l = [a, b] ∨ l = [b, a]
A list is a permutation of the pair [a, b] if and only if it is equal to [a, b] or to
[b, a].
theorem List.pair_perm E1 #
∀ {α : Type u_1} {a b : α} {l : List α}, [a, b].Perm l ↔ l = [a, b] ∨ l = [b, a]
The pair [a, b] is a permutation of a list if and only if that list is equal to [a, b] or
to [b, a].
theorem List.perm_permutations'Aux_comm E1 #
∀ {α : Type u_1} (a b : α) (l : List α),
(List.flatMap (List.permutations'Aux b) (List.permutations'Aux a l)).Perm
(List.flatMap (List.permutations'Aux a) (List.permutations'Aux b l))
theorem List.Perm.permutations' E1 #
∀ {α : Type u_1} {s t : List α}, s.Perm t → s.permutations'.Perm t.permutations'
theorem List.permutations_perm_permutations' E1 #
∀ {α : Type u_1} (ts : List α), ts.permutations.Perm ts.permutations'
theorem List.mem_permutations' E0 #
∀ {α : Type u_1} {s t : List α}, s ∈ t.permutations' ↔ s.Perm t
theorem List.Perm.permutations E1 #
∀ {α : Type u_1} {s t : List α}, s.Perm t → s.permutations.Perm t.permutations
theorem List.perm_permutations_iff E0 #
∀ {α : Type u_1} {s t : List α}, s.permutations.Perm t.permutations ↔ s.Perm t
theorem List.perm_permutations'_iff E0 #
∀ {α : Type u_1} {s t : List α}, s.permutations'.Perm t.permutations' ↔ s.Perm t
theorem List.getElem_permutations'Aux E0 #
∀ {α : Type u_1} (s : List α) (x : α) (n : ℕ) (hn : n < (List.permutations'Aux x s).length),
(List.permutations'Aux x s)[n] = s.insertIdx n x
theorem List.get_permutations'Aux E0 #
∀ {α : Type u_1} (s : List α) (x : α) (n : ℕ) (hn : n < (List.permutations'Aux x s).length),
(List.permutations'Aux x s).get ⟨n, hn⟩ = s.insertIdx n x
theorem List.count_permutations'Aux_self E1 #
∀ {α : Type u_1} [inst : DecidableEq α] (l : List α) (x : α),
List.count (x :: l) (List.permutations'Aux x l) = (List.takeWhile (fun x_1 ↦ decide (x = x_1)) l).length + 1
theorem List.length_permutations'Aux E0 #
∀ {α : Type u_1} (s : List α) (x : α), (List.permutations'Aux x s).length = s.length + 1
theorem List.injective_permutations'Aux E0 #
∀ {α : Type u_1} (x : α), Function.Injective (List.permutations'Aux x)
theorem List.nodup_permutations'Aux_of_notMem E1 #
∀ {α : Type u_1} (s : List α), ∀ x ∉ s, (List.permutations'Aux x s).Nodup
theorem List.nodup_permutations'Aux_iff E0 #
∀ {α : Type u_1} {s : List α} {x : α}, (List.permutations'Aux x s).Nodup ↔ x ∉ s
theorem List.nodup_permutations E1 #
∀ {α : Type u_1} (s : List α), s.Nodup → s.permutations.Nodup
theorem List.permutations_take_two E1 #
∀ {α : Type u_1} (x y : α) (s : List α), List.take 2 (x :: y :: s).permutations = [x :: y :: s, y :: x :: s]
theorem List.nodup_permutations_iff E0 #
∀ {α : Type u_1} {s : List α}, s.permutations.Nodup ↔ s.Nodup
Deprecated aliases
| Name | New name | Since |
|---|---|---|
List.mem_permutations_of_perm_lemma | 2026-11-13 |