1-- The infinite Ramsey theorem: for every n, every infinite set S, and every2-- colouring of the n-subsets of S with finitely many colours, some infinite3-- H ⊆ S has all its n-subsets of one colour. The chain of stages supplies4-- points a₀, a₁, … with colours c₀, c₁, …; one colour occurs5-- infinitely often, and the points of that colour form H: an6-- (n + 1)-subset of H with least index j is insert(aⱼ, T) for an7-- n-subset T of Sⱼ₊₁.8-- Part of Cumulus, the Grace17 mathematics library.9-- Copyright (c) 2026 Grant Passmore.10-- Released under the Apache License 2.0. See LICENSE in the repository root.11-- Authors: Grant Passmore1213module Grace.Cumulus.Combinatorics.RamseyInfinite14import RamseyChain1516-- The colours and the points of the chain, as graphs on ℕ.1718defgraph ramseyColourMap (C S K : Set, n : Nat) (i ∈ ℕ) : K :=19 ramseyColour(C, S, K, n, i)20requires ramsey : RamseyProperty(n, K)21requires colouring : C : subsetsOfSize(S, n +1) → K22requires infinite : Infinite(S)23proof24have iNat := nat.omega.backward with member25exact ramseyColour.member at C, S, K, n, i with*2627defgraph ramseyPointMap (C S K : Set, n : Nat) (i ∈ ℕ) : S :=28 ramseyPoint(C, S, K, n, i)29requires ramsey : RamseyProperty(n, K)30requires colouring : C : subsetsOfSize(S, n +1) → K31requires infinite : Infinite(S)32proof33have iNat := nat.omega.backward with member34exact ramseyPoint.in_set at C, S, K, n, i with*3536-- The stages whose colour is c, and the points of those stages.3738def ramseyIndices (C S K c : Set, n : Nat) : Set:=39 functionPreimage(ramseyColourMap(C, S, K, n), ℕ, {c})4041theorem ramseyIndices.value (C S K c : Set, n : Nat)42shows ramseyIndices(C, S, K, c, n) =43 functionPreimage(ramseyColourMap(C, S, K, n), ℕ, {c})44proof45simp [ramseyIndices]4647def ramseyHomogeneous (C S K c : Set, n : Nat) : Set:=48 functionImage(ramseyPointMap(C, S, K, n), ramseyIndices(C, S, K, c, n), S)4950theorem ramseyHomogeneous.value (C S K c : Set, n : Nat)51shows ramseyHomogeneous(C, S, K, c, n) =52 functionImage(ramseyPointMap(C, S, K, n), ramseyIndices(C, S, K, c, n), S)53proof54simp [ramseyHomogeneous]5556-- The indices of colour c whose points lie in U.57def ramseyIndicesOf (C S K c U : Set, n : Nat) : Set:=58 { i ∈ ramseyIndices(C, S, K, c, n) | ramseyPoint(C, S, K, n, i) ∈ U }5960section RamseyInfinite61variable (C S K : Set) (n : Nat)62hypothesis ramsey : RamseyProperty(n, K)63hypothesis colouring : FunctionBetween(C, subsetsOfSize(S, n +1), K)64hypothesis infinite : Infinite(S)6566theorem ramseyIndices.subset (c : Set)67shows ramseyIndices(C, S, K, c, n) ⊆ ℕ68proof69rw ramseyIndices.value70exact functionPreimage.subset at ramseyColourMap(C, S, K, n), ℕ, {c}7172theorem ramseyIndices.membership (c i : Set)73shows i ∈ ramseyIndices(C, S, K, c, n) ↔74 i ∈ ℕ ∧ ramseyColour(C, S, K, n, i) = c75proof76rw ramseyIndices.value77constructor78 . intro h79obtain⟨hi, valueC⟩:= functionPreimage.point_membership.forward with h80have value := ramseyColourMap.value at C, S, K, n, i with*81rw value at valueC82exact⟨hi, valueC⟩83 . intro h84obtain⟨hi, valueC⟩:= h85have value := ramseyColourMap.value at C, S, K, n, i with*86apply functionPreimage.point_membership.backward87rw value88exact⟨hi, valueC⟩8990theorem ramseyHomogeneous.subset (c : Set)91shows ramseyHomogeneous(C, S, K, c, n) ⊆ S92proof93rw ramseyHomogeneous.value94exact functionImage.subset at95 ramseyPointMap(C, S, K, n), ramseyIndices(C, S, K, c, n), S9697theorem ramseyHomogeneous.membership (c x : Set)98shows x ∈ ramseyHomogeneous(C, S, K, c, n) ↔99∃ i ∈ ramseyIndices(C, S, K, c, n), x = ramseyPoint(C, S, K, n, i)100proof101have indices := ramseyIndices.subset at C, S, K, n, c with*102rw ramseyHomogeneous.value103constructor104 . intro h105obtain⟨_, i, hi, shape⟩:= functionImage.membership.forward with h106have hiN := indices with i, hi107have value := ramseyPointMap.value at C, S, K, n, i with*108rw value at shape109exact⟨i, hi, shape⟩110 . intro h111obtain⟨i, hi, shape⟩:= h112have hiN := indices with i, hi113have iNat := nat.omega.backward with hiN114have value := ramseyPointMap.value at C, S, K, n, i with*115have inS := ramseyPoint.in_set at C, S, K, n, i with*116apply functionImage.membership.backward117constructor118 . rw shape119exact inS120 . refine⟨i, hi, ?_⟩121rw [shape, value]122rfl123124-- The points are pairwise distinct, so H is as large as its index set.125theorem ramseyPointMap.injective126shows InjectiveBetween(ramseyPointMap(C, S, K, n), ℕ, S)127proof128have graph := ramseyPointMap.function at C, S, K, n with*129apply Injective.of_pointwise with graph130intros i hi j hj same131have iNat := nat.omega.backward with hi132have jNat := nat.omega.backward with hj133have valueI := ramseyPointMap.value at C, S, K, n, i with*134have valueJ := ramseyPointMap.value at C, S, K, n, j with*135rw [valueI, valueJ] at same136exact ramseyPoint.injective_on at C, S, K, n, i, j with*137138theorem ramseyHomogeneous.infinite (c : Set)139requires indicesInfinite : Infinite(ramseyIndices(C, S, K, c, n))140shows Infinite(ramseyHomogeneous(C, S, K, c, n))141proof142have injective := ramseyPointMap.injective at C, S, K, n with*143have graph := ramseyPointMap.function at C, S, K, n with*144have indices := ramseyIndices.subset at C, S, K, n, c with*145have restricted := restrict.injective with injective, indices146have imageInfinite := Infinite.injective_image with147 restricted, indicesInfinite148have sameImage := functionImage.restrict with graph, indices149rw sameImage at imageInfinite150rw ramseyHomogeneous.value151exact imageInfinite152153-- An (n + 1)-subset U of H whose least index is j is insert(aⱼ, T) for the154-- n-subset T = U ∖ {aⱼ} of Sⱼ₊₁, so its colour is cⱼ = c.155theorem ramseyHomogeneous.colour_of_least (c U : Set) (j : Nat)156requires hU : U ∈ subsetsOfSize(ramseyHomogeneous(C, S, K, c, n), n +1)157requires jIndex : j ∈ ramseyIndices(C, S, K, c, n)158requires jIn : ramseyPoint(C, S, K, n, j) ∈ U159requires least : ∀ i ∈ ramseyIndices(C, S, K, c, n),160 ramseyPoint(C, S, K, n, i) ∈ U → j ≤ i161shows C(U) = c162proof163have subsetU := subsetsOfSize.subset with hU164have indices := ramseyIndices.subset at C, S, K, n, c with*165have jParts := ramseyIndices.membership.forward with jIndex166have restSubset : U ∖ {ramseyPoint(C, S, K, n, j)} ⊆ ramseyStage(C, S, K, n, j +1)167intros x hx168obtain⟨xIn, xNe⟩:= difference.singleton_membership.forward with hx169have xH := subsetU with x, xIn170obtain⟨i, hi, shape⟩:= ramseyHomogeneous.membership.forward with xH171have hiN := indices with i, hi172have iNat := nat.omega.backward with hiN173have pointIn : ramseyPoint(C, S, K, n, i) ∈ U174rw← shape175exact xIn176have jLe := least with i, hi, pointIn177have jNe : j ≠ i178intro same179apply xNe180cc181have jLt := Less.of_LessEqual_ne with jLe, jNe182have later := ramseyPoint.later_member at C, S, K, n, j, i with*183rw shape184exact later185have counted := subsetsOfSize.counted with hU186have one : n +1= succ(n) := add.right_one with*187rw one at counted188have restCounted := HasCardinality.remove with counted, jIn189have restIn := subsetsOfSize.of_subset with restSubset, restCounted190have law := ramseyColour.law at191 C, S, K, n, U ∖ {ramseyPoint(C, S, K, n, j)}, j with*192have restored := insert.restore with jIn193rw restored at law194rw law195exact jParts.right196197theorem ramseyHomogeneous.homogeneous (c U : Set)198requires hU : U ∈ subsetsOfSize(ramseyHomogeneous(C, S, K, c, n), n +1)199shows C(U) = c200proof201have subsetU := subsetsOfSize.subset with hU202have indices := ramseyIndices.subset at C, S, K, n, c with*203obtain⟨u, hu⟩:= subsetsOfSize.exists_member with hU204have uH := subsetU with u, hu205obtain⟨i0, hi0, shape0⟩:= ramseyHomogeneous.membership.forward with uH206have pointIn0 : ramseyPoint(C, S, K, n, i0) ∈ U207rw← shape0208exact hu209have candidate : i0 ∈ ramseyIndicesOf(C, S, K, c, U, n) :=210 ramseyIndicesOf.membership.backward with⟨hi0, pointIn0⟩211have inhabited : Nonempty(ramseyIndicesOf(C, S, K, c, U, n)) :=212⟨i0, candidate⟩213have naturals : ramseyIndicesOf(C, S, K, c, U, n) ⊆ ℕ214intros i hi215have parts := ramseyIndicesOf.membership.forward with hi216exact indices with i, parts.left217have jMember := leastNatural.member with inhabited, naturals218obtain⟨jIndex, jIn⟩:= ramseyIndicesOf.membership.forward with jMember219have jN := indices with220 leastNatural(ramseyIndicesOf(C, S, K, c, U, n)), jIndex221have jNat := nat.omega.backward with jN222have least : ∀ i ∈ ramseyIndices(C, S, K, c, n),223 ramseyPoint(C, S, K, n, i) ∈ U →224 leastNatural(ramseyIndicesOf(C, S, K, c, U, n)) ≤ i225intros i hi pointIn226have iNat := nat.omega.backward with (indices with i, hi)227have member : i ∈ ramseyIndicesOf(C, S, K, c, U, n) :=228 ramseyIndicesOf.membership.backward with⟨hi, pointIn⟩229exact leastNatural.least with inhabited, naturals, member230exact ramseyHomogeneous.colour_of_least at231 C, S, K, n, c, U, leastNatural(ramseyIndicesOf(C, S, K, c, U, n)) with*232233end RamseyInfinite234235-- The induction on n.236237theorem RamseyProperty.zero (K : Set)238shows RamseyProperty(0, K)239proof240intros S C infinite colouring241have emptyIn : ∅∈ subsetsOfSize(S, 0)242rw subsetsOfSize.zero243exact singleton.self at∅244have value := FunctionBetween.apply_closed with colouring, emptyIn245refine⟨S, Subset.reflexive at S, infinite, C(∅), value, ?_⟩246intros U hU247rw subsetsOfSize.zero at hU248have equal := singleton.membership.forward with hU249rw equal250rfl251252theorem RamseyProperty.succ (K : Set) (n : Nat)253requires finite : Finite(K)254requires ramsey : RamseyProperty(n, K)255shows RamseyProperty(n +1, K)256proof257intros S C infinite colouring258have colourGraph := ramseyColourMap.function at C, S, K, n with*259have omegaInfinite : Infinite(ℕ) := omega.infinite260obtain⟨c, hc, indicesInfinite⟩:=261 pigeonhole.infinite_fiber with colourGraph, omegaInfinite, finite262rw← ramseyIndices.value at indicesInfinite263have subset := ramseyHomogeneous.subset at C, S, K, n, c with*264have hInfinite := ramseyHomogeneous.infinite at C, S, K, n, c with*265refine⟨ramseyHomogeneous(C, S, K, c, n), subset, hInfinite, c, hc, ?_⟩266intros U hU267exact ramseyHomogeneous.homogeneous at C, S, K, n, c, U with*268269theorem RamseyProperty.all (K : Set) (n : Nat)270requires finite : Finite(K)271shows RamseyProperty(n, K)272proof273induction n274| zero =>exact RamseyProperty.zero at K275| k +1using ih =>exact RamseyProperty.succ at K, k with finite, ih276277-- The infinite Ramsey theorem, for any finite set of colours.278theorem ramsey.infinite_colours (n : Nat) (S C K : Set)279requires infinite : Infinite(S)280requires finite : Finite(K)281requires colouring : C : subsetsOfSize(S, n) → K282shows∃ H, H ⊆ S ∧ Infinite(H) ∧283∃ c ∈ K, ∀ U ∈ subsetsOfSize(H, n), C(U) = c284proof285have ramsey := RamseyProperty.all at K, n with finite286exact ramsey with S, C, infinite, colouring287288-- The infinite Ramsey theorem for two colours.289theorem ramsey.infinite (n : Nat) (S C : Set)290requires infinite : Infinite(S)291requires colouring : C : subsetsOfSize(S, n) →2292shows∃ H, H ⊆ S ∧ Infinite(H) ∧293∃ c ∈2, ∀ U ∈ subsetsOfSize(H, n), C(U) = c294proof295have finite := Finite.natural at2296exact ramsey.infinite_colours with infinite, finite, colouring297298-- The infinite Ramsey theorem for k colours.299theorem ramsey.infinite_numeral (n k : Nat) (S C : Set)300requires infinite : Infinite(S)301requires colouring : C : subsetsOfSize(S, n) → k302shows∃ H, H ⊆ S ∧ Infinite(H) ∧303∃ c ∈ k, ∀ U ∈ subsetsOfSize(H, n), C(U) = c304proof305have finite := Finite.natural at k306exact ramsey.infinite_colours with infinite, finite, colouring307