Martin Escardo, November-December 2019 \begin{code} {-# OPTIONS --safe --without-K #-} module Fin.Pigeonhole where open import Factorial.Swap open import Fin.Bishop open import Fin.Choice open import Fin.Embeddings open import Fin.Order open import Fin.Topology open import Fin.Type open import MLTT.Plus-Properties open import MLTT.Spartan open import Naturals.Order open import Notation.Order open import NotionsOfDecidability.Complemented open import NotionsOfDecidability.Decidable open import UF.DiscreteAndSeparated open import UF.Equiv open import UF.FunExt open import UF.LeftCancellable open import UF.PropTrunc \end{code} Recall that X β£ Y is the type of left cancellable maps from X to Y, which should not be confused with the type X βͺ Y of embeddings of X into Y. However, for types that are sets, like Fin n, there is no difference between the embedding property and left cancellability. \begin{code} +π-cancel-lemma : {X Y : π€ Μ } β (π : X + π β£ Y + π) β β π β π οΌ π β X β£ Y +π-cancel-lemma {π€} {X} {Y} (f , l) p = g , m where g : X β Y g x = prβ (inl-preservation {π€} {π€} {π€} {π€} f p l x) a : (x : X) β f (suc x) οΌ suc (g x) a x = prβ (inl-preservation f p l x) m : left-cancellable g m {x} {x'} p = q where r = f (suc x) οΌβ¨ a x β© suc (g x) οΌβ¨ ap suc p β© suc (g x') οΌβ¨ (a x')β»ΒΉ β© f (suc x') β q : x οΌ x' q = inl-lc (l r) +π-cancel : {X Y : π€ Μ } β is-discrete Y β X + π β£ Y + π β X β£ Y +π-cancel {π€} {X} {Y} i (f , e) = a where h : Y + π β Y + π h = swap (f π) π (+-is-discrete i π-is-discrete (f π)) new-point-is-isolated d : left-cancellable h d = equivs-are-lc h (swap-is-equiv (f π) π (+-is-discrete i π-is-discrete (f π)) new-point-is-isolated) f' : X + π β Y + π f' = h β f e' : left-cancellable f' e' = left-cancellable-closed-under-β f h e d p : f' π οΌ π p = swap-equationβ (f π) π (+-is-discrete i π-is-discrete (f π)) new-point-is-isolated a : X β£ Y a = +π-cancel-lemma (f' , e') p \end{code} In set theory, natural numbers are defined as certain sets, and their order relation is inherited from the ordering of sets defined by the existence of injections, or left-cancellable maps. Here, in type theory, we have defined m β€ n by induction on m and n, in the style of Peano Arithmetic, but we can prove that this relation is characterized by this injection property: \begin{code} β£-gives-β€ : (m n : β) β (Fin m β£ Fin n) β m β€ n β£-gives-β€ 0 n e = zero-least n β£-gives-β€ (succ m) 0 (f , i) = π-elim (f π) β£-gives-β€ (succ m) (succ n) e = β£-gives-β€ m n (+π-cancel Fin-is-discrete e) canonical-Fin-inclusion : (m n : β) β m β€ n β (Fin m β Fin n) canonical-Fin-inclusion 0 n l = unique-from-π canonical-Fin-inclusion (succ m) 0 l = π-elim l canonical-Fin-inclusion (succ m) (succ n) l = +functor IH unique-to-π where IH : Fin m β Fin n IH = canonical-Fin-inclusion m n l canonical-Fin-inclusion-lc : (m n : β) (l : m β€ n) β left-cancellable (canonical-Fin-inclusion m n l) canonical-Fin-inclusion-lc 0 n l {x} {y} p = π-elim x canonical-Fin-inclusion-lc (succ m) 0 l {x} {y} p = π-elim l canonical-Fin-inclusion-lc (succ m) (succ n) l {suc x} {suc y} p = Ξ³ where IH : canonical-Fin-inclusion m n l x οΌ canonical-Fin-inclusion m n l y β x οΌ y IH = canonical-Fin-inclusion-lc m n l Ξ³ : suc x οΌ suc y Ξ³ = ap suc (IH (inl-lc p)) canonical-Fin-inclusion-lc (succ m) (succ n) l {π} {π} p = refl β€-gives-β£ : (m n : β) β m β€ n β (Fin m β£ Fin n) β€-gives-β£ m n l = canonical-Fin-inclusion m n l , canonical-Fin-inclusion-lc m n l \end{code} An equivalent, shorter construction: \begin{code} β€-gives-β£' : (m n : β) β m β€ n β (Fin m β£ Fin n) β€-gives-β£' 0 n l = unique-from-π , (Ξ» {x} {x'} p β π-elim x) β€-gives-β£' (succ m) 0 l = π-elim l β€-gives-β£' (succ m) (succ n) l = g , j where IH : Fin m β£ Fin n IH = β€-gives-β£' m n l f : Fin m β Fin n f = prβ IH i : left-cancellable f i = prβ IH g : Fin (succ m) β Fin (succ n) g = +functor f unique-to-π j : left-cancellable g j {suc x} {suc x'} p = ap suc (i (inl-lc p)) j {suc x} {π} p = π-elim (+disjoint p) j {π} {suc y} p = π-elim (+disjoint' p) j {π} {π} p = refl \end{code} Added 9th December 2019. A version of the pigeonhole principle, which uses (one direction of) the above characterization of the relation m β€ n as the existence of an injection Fin m β Fin n: \begin{code} _has-a-repetition : {X : π€ Μ } {Y : π₯ Μ } β (X β Y) β π€ β π₯ Μ f has-a-repetition = Ξ£ x κ domain f , Ξ£ x' κ domain f , (x β x') Γ (f x οΌ f x') pigeonhole-principle : (m n : β) (f : Fin m β Fin n) β m > n β f has-a-repetition pigeonhole-principle m n f g = Ξ³ where a : Β¬ (Ξ£ f κ (Fin m β Fin n), left-cancellable f) a = contrapositive (β£-gives-β€ m n) (less-not-bigger-or-equal n m g) b : Β¬ left-cancellable f b l = a (f , l) c : Β¬ ((i j : Fin m) β f i οΌ f j β i οΌ j) c Ο = b (Ξ» {i} {j} β Ο i j) d : ¬¬ (f has-a-repetition) d Ο = c Ξ΄ where Ξ΅ : (i j : Fin m) β f i οΌ f j β Β¬ (i β j) Ξ΅ i j p Ξ½ = Ο (i , j , Ξ½ , p) Ξ΄ : (i j : Fin m) β f i οΌ f j β i οΌ j Ξ΄ i j p = ¬¬-elim (Fin-is-discrete i j) (Ξ΅ i j p) \end{code} A classical proof ends at this point. For a constructive proof, we need more steps. \begin{code} u : (i j : Fin m) β is-decidable ((i β j) Γ (f i οΌ f j)) u i j = Γ-preserves-decidability (Β¬-preserves-decidability (Fin-is-discrete i j)) (Fin-is-discrete (f i) (f j)) v : (i : Fin m) β is-decidable (Ξ£ j κ Fin m , (i β j) Γ (f i οΌ f j)) v i = Fin-Compact _ (u i) w : is-decidable (f has-a-repetition) w = Fin-Compact _ v Ξ³ : f has-a-repetition Ξ³ = ¬¬-elim w d \end{code} This, of course, doesn't give the most efficient algorithm, but it does give an algorithm for computing an argument of the function whose value is repeated. Example. The pigeonhole principle holds for finite types in the following form: \begin{code} module _ (pt : propositional-truncations-exist) where open PropositionalTruncation pt open finiteness pt finite-pigeonhole-principle : {X : π€ Μ } {Y : π₯ Μ } (f : X β Y) (Ο : is-finite X) (Ο : is-finite Y) β cardinality X Ο > cardinality Y Ο β β₯ f has-a-repetition β₯ finite-pigeonhole-principle {π€} {π₯} {X} {Y} f (m , s) (n , t) b = Ξ³ where h : Fin m β X β Y β Fin n β f has-a-repetition h (g , d) (h , e) = r r' where f' : Fin m β Fin n f' = h β f β g r' : f' has-a-repetition r' = pigeonhole-principle m n f' b r : f' has-a-repetition β f has-a-repetition r (i , j , u , p) = g i , g j , u' , p' where u' : g i β g j u' = contrapositive (equivs-are-lc g d) u p' : f (g i) οΌ f (g j) p' = equivs-are-lc h e p Ξ³ : β₯ f has-a-repetition β₯ Ξ³ = β₯β₯-functorβ h (β₯β₯-functor β-sym s) t \end{code} We now assume function extensionality for a while: \begin{code} module _ (fe : FunExt) where \end{code} We now consider further variations of the finite pigeonhole principle. \begin{code} repeated-values : {X : π€ Μ } {Y : π₯ Μ } β (X β Y) β X β π€ β π₯ Μ repeated-values f = Ξ» x β Ξ£ x' κ domain f , (x β x') Γ (f x οΌ f x') repetitions-complemented : {m : β} {Y : π₯ Μ } (f : Fin m β Y) β is-finite Y β is-complemented (repeated-values f) repetitions-complemented {π₯} {m} {Y} f (n , t) i = Fin-Compact (Ξ» j β (i β j) Γ (f i οΌ f j)) (Ξ» j β Γ-preserves-decidability (Β¬-preserves-decidability (Fin-is-discrete i j)) (finite-types-are-discrete pt fe (n , t) (f i) (f j))) finite-pigeonhole-principle' : {m : β} {Y : π₯ Μ } (f : Fin m β Y) (Ο : is-finite Y) β m > cardinality Y Ο β f has-a-repetition finite-pigeonhole-principle' {π₯} {m} {Y} f (n , t) b = Ξ³ where h : Y β Fin n β f has-a-repetition h (h , e) = r r' where f' : Fin m β Fin n f' = h β f r' : f' has-a-repetition r' = pigeonhole-principle m n f' b r : f' has-a-repetition β f has-a-repetition r (i , j , u , p) = i , j , u , equivs-are-lc h e p Ξ³' : β₯ f has-a-repetition β₯ Ξ³' = β₯β₯-functor h t A : Fin m β π₯ Μ A i = Ξ£ j κ Fin m , (i β j) Γ (f i οΌ f j) Ξ³ : f has-a-repetition Ξ³ = Fin-Ξ£-from-β pt fe {m} A (repetitions-complemented f (n , t)) Ξ³' \end{code} We can easily derive the construction finite-pigeonhole-principle from finite-pigeonhole-principle', but at the expense of function extensionality, which was not needed in our original construction. Further versions of the pigeonhole principle are the following. \begin{code} finite-pigeonhole-principle'' : {m : β} {Y : π₯ Μ } (f : Fin m β Y) (Ο : is-finite Y) β m > cardinality Y Ο β Ξ£-min Ξ»(i : Fin m) β repeated-values f i finite-pigeonhole-principle'' {π₯} {m} {Y} f Ο g = Ξ£-gives-Ξ£-min (repeated-values f) (repetitions-complemented f Ο) (finite-pigeonhole-principle' f Ο g) β-finite-pigeonhole-principle : {Y : π₯ Μ } (f : β β Y) β is-finite Y β f has-a-repetition β-finite-pigeonhole-principle {π₯} {Y} f (m , t) = r r' where f' : Fin (succ m) β Y f' i = f (β¦_β§ i) r' : f' has-a-repetition r' = finite-pigeonhole-principle' f'(m , t) (<-succ m) r : f' has-a-repetition β f has-a-repetition r (i , j , u , p) = β¦_β§ i , β¦_β§ j , contrapositive (β¦_β§-lc (succ m)) u , p \end{code} Added 13th December 2019. A well-known application of the pigeonhole principle is that every element has a (least) finite order in a finite group. This holds more generally for any finite type equipped with a left-cancellable binary operation _Β·_ and a distinguished element e, with the same construction. \begin{code} module _ {X : π€ Μ } (Ο : is-finite X) (_Β·_ : X β X β X) (lc : (x : X) β left-cancellable (x Β·_)) (e : X) where _β_ : X β β β X x β 0 = e x β (succ n) = x Β· (x β n) infixl 3 _β_ finite-order : (x : X) β Ξ£ k κ β , x β (succ k) οΌ e finite-order x = c a where a : Ξ£ m κ β , Ξ£ n κ β , (m β n) Γ (x β m οΌ x β n) a = β-finite-pigeonhole-principle (x β_) Ο b : (m : β) (n : β) β m β n β x β m οΌ x β n β Ξ£ k κ β , x β (succ k) οΌ e b 0 0 Ξ½ p = π-elim (Ξ½ refl) b 0 (succ n) Ξ½ p = n , (p β»ΒΉ) b (succ m) 0 Ξ½ p = m , p b (succ m) (succ n) Ξ½ p = b m n (Ξ» (q : m οΌ n) β Ξ½ (ap succ q)) (lc x p) c : type-of a β Ξ£ k κ β , x β (succ k) οΌ e c (m , n , Ξ½ , p) = b m n Ξ½ p \end{code} And of course then there is a least such k, by bounded minimization, because finite types are discrete: \begin{code} least-finite-order : (x : X) β Σμ Ξ»(k : β) β x β (succ k) οΌ e least-finite-order x = least-from-given A Ξ³ (finite-order x) where A : β β π€ Μ A n = x β (succ n) οΌ e Ξ³ : (n : β) β is-decidable (x β succ n οΌ e) Ξ³ n = finite-types-are-discrete pt fe Ο (x β succ n) e \end{code} Remark: the given construction finite-order already produces the least order, but it seems slightly more difficult to prove this than just compute the least order from any order. If we were interested in the efficiency of our constructions (Agda constructions are functional programs!), we would have to consider this.