1-- Permutations of a finite set: the bijections of a set onto itself form2-- a set of size n!, closed under composition and inversion and containing3-- the identity and every transposition4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.Combinatorics.Permutations10import Injections1112theorem permutations.eq_bijections (I : Set)13shows permutations(I) = bijections(I, I)14proof15ext p16constructor17 . intro member18exact bijections.of_bijective with (permutations.mem_iff.forward with member)19 . intro member20exact permutations.mem_iff.backward with (bijections.bijective with member)2122theorem permutations.of_bijective (I p : Set)23requires bijective : BijectiveBetween(p, I, I)24shows p ∈ permutations(I)25proof26exact permutations.mem_iff.backward with bijective2728theorem permutations.bijective (I p : Set)29requires member : p ∈ permutations(I)30shows BijectiveBetween(p, I, I)31proof32exact permutations.mem_iff.forward with member3334theorem permutations.function (I p : Set)35requires member : p ∈ permutations(I)36shows p : I → I37proof38have bijective := permutations.bijective with member39exact Injective.function with (Bijective.injective with bijective)4041-- A finite set with n elements has n! permutations.42theorem permutations.count (I : Set)43requires finite : Finite(I)44shows cardinality(permutations(I)) = cardinality(I)!45proof46rw permutations.eq_bijections47exact bijections.count_self with finite4849theorem permutations.natural_count (n : Nat)50shows cardinality(permutations(n)) = n!51proof52have count := permutations.count with (Finite.natural at n)53rw [count, cardinality.natural]54rfl5556theorem permutations.identity (I : Set)57shows identity(I) ∈ permutations(I)58proof59exact permutations.of_bijective with (identity.bijective at I)6061theorem permutations.nonempty (I : Set)62shows Nonempty(permutations(I))63proof64exact⟨identity(I), permutations.identity at I⟩6566theorem permutations.compose (I p q : Set)67requires hp : p ∈ permutations(I)68requires hq : q ∈ permutations(I)69shows compose(q, p, I, I, I) ∈ permutations(I)70proof71have pBijective := permutations.bijective with hp72have qBijective := permutations.bijective with hq73have composed := compose.bijective at q, p, I, I, I with74 pBijective, qBijective75exact permutations.of_bijective with composed7677theorem permutations.converse (I p : Set)78requires hp : p ∈ permutations(I)79shows converse(p, I, I) ∈ permutations(I)80proof81have bijective := permutations.bijective with hp82exact permutations.of_bijective with (converse.bijective with bijective)8384theorem permutations.transposition (I a b : Set)85requires ha : a ∈ I86requires hb : b ∈ I87shows transposition(I, a, b) ∈ permutations(I)88proof89have bijective := transposition.bijective at I, a, b with ha, hb90exact permutations.of_bijective with bijective9192theorem permutations.apply_closed (I p x : Set)93requires hp : p ∈ permutations(I)94requires hx : x ∈ I95shows p(x) ∈ I96proof97exact FunctionBetween.apply_closed with (permutations.function with hp), hx9899theorem permutations.apply_cancel (I p x y : Set)100requires hp : p ∈ permutations(I)101requires hx : x ∈ I102requires hy : y ∈ I103requires same : p(x) = p(y)104shows x = y105proof106have injective := Bijective.injective with (permutations.bijective with hp)107exact Injective.apply_cancel with injective, hx, hy, same108109theorem permutations.apply_surjective (I p y : Set)110requires hp : p ∈ permutations(I)111requires hy : y ∈ I112shows∃ x ∈ I, p(x) = y113proof114have bijective := permutations.bijective with hp115have surjective := Bijective.surjective with bijective116have function := permutations.function with hp117obtain⟨x, hx, related⟩:= surjective.right with y, hy118refine⟨x, hx, ?_⟩119exact FunctionBetween.apply_value with function, hx, related120121theorem permutations.image_full (I p : Set)122requires hp : p ∈ permutations(I)123shows functionImage(p, I, I) = I124proof125have bijective := permutations.bijective with hp126exact Surjective.image_full with (Bijective.surjective with bijective)127128theorem permutations.inverse_apply (I p x : Set)129requires hp : p ∈ permutations(I)130requires hx : x ∈ I131shows converse(p, I, I)(p(x)) = x132proof133exact Bijective.inverse_apply with (permutations.bijective with hp), hx134135theorem permutations.apply_inverse (I p y : Set)136requires hp : p ∈ permutations(I)137requires hy : y ∈ I138shows p(converse(p, I, I)(y)) = y139proof140exact Bijective.apply_inverse with (permutations.bijective with hp), hy141142theorem permutations.compose_identity_left (I p : Set)143requires hp : p ∈ permutations(I)144shows compose(identity(I), p, I, I, I) = p145proof146exact compose.identity_left with (permutations.function with hp)147148theorem permutations.compose_identity_right (I p : Set)149requires hp : p ∈ permutations(I)150shows compose(p, identity(I), I, I, I) = p151proof152exact compose.identity_right with (permutations.function with hp)153154theorem permutations.compose_associative (I p q r : Set)155requires hp : p ∈ permutations(I)156requires hq : q ∈ permutations(I)157requires hr : r ∈ permutations(I)158shows159 compose(r, compose(q, p, I, I, I), I, I, I) = compose(compose(r, q, I, I, I), p, I, I, I)160proof161have pFunction := permutations.function with hp162have qFunction := permutations.function with hq163have rFunction := permutations.function with hr164exact compose.associative at r, q, p, I, I, I, I with pFunction, qFunction, rFunction165166-- On a finite set an injective self-map is already a permutation.167theorem permutations.of_injective (I p : Set)168requires finite : Finite(I)169requires injective : InjectiveBetween(p, I, I)170shows p ∈ permutations(I)171proof172exact permutations.of_bijective with (Finite.self_injective_bijective with injective, finite)173174theorem permutations.of_surjective (I p : Set)175requires finite : Finite(I)176requires surjective : SurjectiveBetween(p, I, I)177shows p ∈ permutations(I)178proof179exact permutations.of_bijective with (Finite.self_surjective_bijective with surjective, finite)180181theorem permutations.eq_injections (I : Set)182requires finite : Finite(I)183shows permutations(I) = injections(I, I)184proof185have same : cardinality(I) = cardinality(I) byrfl186rw permutations.eq_bijections187exact bijections.eq_injections with finite, same188189-- A singleton has only the identity permutation.190theorem permutations.singleton (x : Set)191shows permutations({x}) = {identity({x})}192proof193have finite := Finite.singleton at x194have count := permutations.count with finite195rw [cardinality.singleton, factorial.one] at count196have finitePerms := permutations.finite with finite197have iff := cardinality.one_iff with finitePerms198obtain⟨y, shape⟩:= iff.forward with count199have member := permutations.identity at {x}200rw shape at member201have equal := singleton.membership.forward with member202rw [shape, equal]203rfl204205theorem permutations.two_count206shows cardinality(permutations(2)) =2207proof208have count := permutations.natural_count at2209rw count210compute211212theorem permutations.three_count213shows cardinality(permutations(3)) =6214proof215have count := permutations.natural_count at3216rw count217compute218