1-- The Ramsey chain: from an infinite set S and a colouring of its2-- (n + 1)-subsets, a descending sequence of infinite stages Sᵢ ⊆ S with3-- distinguished points aᵢ ∈ Sᵢ ∖ Sᵢ₊₁, where every n-subset T of Sᵢ₊₁4-- gives insert(aᵢ, T) one colour cᵢ. Each stage is chosen among the5-- homogeneous subsets that the Ramsey property for n provides.6-- Part of Cumulus, the Grace17 mathematics library.7-- Copyright (c) 2026 Grant Passmore.8-- Released under the Apache License 2.0. See LICENSE in the repository root.9-- Authors: Grant Passmore1011module Grace.Cumulus.Combinatorics.RamseyChain12import RamseyPigeonhole13import Set.Choice1415-- Every colouring of the n-subsets of an infinite set with colours in K has16-- an infinite homogeneous subset.17predicate RamseyProperty (n K : Set) :=18∀ S, ∀ C, Infinite(S) → FunctionBetween(C, subsetsOfSize(S, n), K) →19∃ H, H ⊆ S ∧ Infinite(H) ∧20∃ c ∈ K, ∀ U ∈ subsetsOfSize(H, n), C(U) = c2122-- The colouring of the n-subsets of Y obtained from a colouring C of the23-- (n + 1)-subsets of S by adjoining a point a of S outside Y.24defgraph inducedColouring (C S Y a K : Set, n : Nat) (T ∈ subsetsOfSize(Y, n)) : K :=25 C(insert(a, T))26requires colouring : C : subsetsOfSize(S, n +1) → K27requires subset : Y ⊆ S28requires ha : a ∈ S29requires fresh : ¬a ∈ Y30proof31have memberS := subsetsOfSize.member_of_subset with subset, member32have tSubset := subsetsOfSize.subset with member33have freshT : ¬a ∈ T34intro inT35exact fresh with (tSubset with a, inT)36have inserted := subsetsOfSize.insert_member with memberS, ha, freshT37exact FunctionBetween.apply_closed with colouring, inserted3839-- The infinite subsets of Y on whose n-subsets D is constant.40def homogeneousSubsets (D K Y : Set, n : Nat) : Set:=41 { Z ∈𝒫(Y) | Infinite(Z) ∧42∃ c ∈ K, ∀ U ∈ subsetsOfSize(Z, n), D(U) = c }4344-- A chosen infinite subset of Y homogeneous for the induced colouring.45def homogeneousChoice (C S Y a K : Set, n : Nat) : Set:=46 choose(homogeneousSubsets(inducedColouring(C, S, Y, a, K, n), K, Y, n))4748theorem homogeneousChoice.value (C S Y a K : Set, n : Nat)49shows homogeneousChoice(C, S, Y, a, K, n) =50 choose(homogeneousSubsets(inducedColouring(C, S, Y, a, K, n), K, Y, n))51proof52simp [homogeneousChoice]5354theorem homogeneousChoice.properties (C S Y a K : Set, n : Nat)55requires ramsey : RamseyProperty(n, K)56requires colouring : C : subsetsOfSize(S, n +1) → K57requires subset : Y ⊆ S58requires ha : a ∈ S59requires fresh : ¬a ∈ Y60requires infinite : Infinite(Y)61shows homogeneousChoice(C, S, Y, a, K, n) ⊆ Y ∧62 Infinite(homogeneousChoice(C, S, Y, a, K, n)) ∧63∃ c ∈ K,64∀ T ∈ subsetsOfSize(homogeneousChoice(C, S, Y, a, K, n), n),65 C(insert(a, T)) = c66proof67have induced := inducedColouring.function at C, S, Y, a, K, n with68 colouring, subset, ha, fresh69obtain⟨H, hH, hInfinite, c, hc, homogeneous⟩:=70 ramsey with Y, inducedColouring(C, S, Y, a, K, n), infinite, induced71have candidate : H ∈ homogeneousSubsets(inducedColouring(C, S, Y, a, K, n), K, Y, n)72have power := powerset.membership_iff.backward with hH73apply homogeneousSubsets.membership.backward74exact⟨power, hInfinite, c, hc, homogeneous⟩75have inhabited : Nonempty(homogeneousSubsets(inducedColouring(C, S, Y, a, K, n), K, Y, n)) :=76⟨H, candidate⟩77have selected := choose.member with inhabited78rw← homogeneousChoice.value at selected79obtain⟨power, chosenInfinite, d, hd, chosenHomogeneous⟩:=80 homogeneousSubsets.membership.forward with selected81have chosenSubset := powerset.membership_iff.forward with power82refine⟨chosenSubset, chosenInfinite, d, hd, ?_⟩83intros T hT84have memberY := subsetsOfSize.member_of_subset with chosenSubset, hT85have value := inducedColouring.value at C, S, Y, a, K, n, T with86 colouring, subset, ha, fresh, memberY87rw← value88exact chosenHomogeneous with T, hT8990-- One step of the chain: remove the chosen point of X and pass to a91-- homogeneous infinite subset of what remains.92def ramseyStep (C S K X : Set, n : Nat) : Set:=93 homogeneousChoice(C, S, X ∖ {choose(X)}, choose(X), K, n)9495theorem ramseyStep.value (C S K X : Set, n : Nat)96shows ramseyStep(C, S, K, X, n) =97 homogeneousChoice(C, S, X ∖ {choose(X)}, choose(X), K, n)98proof99simp [ramseyStep]100101theorem ramseyStep.properties (C S K X : Set, n : Nat)102requires ramsey : RamseyProperty(n, K)103requires colouring : C : subsetsOfSize(S, n +1) → K104requires subset : X ⊆ S105requires infinite : Infinite(X)106shows ramseyStep(C, S, K, X, n) ⊆ X ∖ {choose(X)} ∧107 Infinite(ramseyStep(C, S, K, X, n)) ∧108∃ c ∈ K, ∀ T ∈ subsetsOfSize(ramseyStep(C, S, K, X, n), n),109 C(insert(choose(X), T)) = c110proof111have chosen := choose.member with (Infinite.nonempty with infinite)112have inS := subset with choose(X), chosen113have restSubset : X ∖ {choose(X)} ⊆ S :=114 Subset.transitive with (difference.subset_left at X, {choose(X)}), subset115have restInfinite := Infinite.remove at X, choose(X) with infinite116have fresh : ¬choose(X) ∈ X ∖ {choose(X)} by117simp [difference.membership, singleton.membership]118have parts := homogeneousChoice.properties with119 ramsey, colouring, restSubset, inS, fresh, restInfinite120 simpa [ramseyStep.value] using parts121122-- The stages and their points.123124def ramseyStage (C S K : Set, n : Nat, i : Nat) →Set125| C, S, K, n, zero => S126| C, S, K, n, succ(k) => ramseyStep(C, S, K, ramseyStage(C, S, K, n, k), n)127128theorem ramseyStage.zero (C S K : Set, n : Nat)129shows ramseyStage(C, S, K, n, 0) = S130proof131simp [ramseyStage]132133theorem ramseyStage.succ (C S K : Set, n : Nat, k : Nat)134shows ramseyStage(C, S, K, n, k +1) =135 ramseyStep(C, S, K, ramseyStage(C, S, K, n, k), n)136proof137have one : k +1= succ(k) := add.right_one with*138rw one139simp [ramseyStage]140141def ramseyPoint (C S K : Set, n : Nat, i : Nat) : Set:=142 choose(ramseyStage(C, S, K, n, i))143144theorem ramseyPoint.value (C S K : Set, n : Nat, i : Nat)145shows ramseyPoint(C, S, K, n, i) = choose(ramseyStage(C, S, K, n, i))146proof147simp [ramseyPoint]148149section RamseyChain150variable (C S K : Set) (n : Nat)151hypothesis ramsey : RamseyProperty(n, K)152hypothesis colouring : FunctionBetween(C, subsetsOfSize(S, n +1), K)153hypothesis infinite : Infinite(S)154155theorem ramseyStage.invariant (i : Nat)156shows ramseyStage(C, S, K, n, i) ⊆ S ∧157 Infinite(ramseyStage(C, S, K, n, i))158proof159induction i160| zero =>161rw ramseyStage.zero162exact⟨Subset.reflexive at S, infinite⟩163| k +1using ih =>164rw ramseyStage.succ165obtain⟨subsetK, infiniteK⟩:= ih166obtain⟨next, nextInfinite, _⟩:= ramseyStep.properties at167 C, S, K, ramseyStage(C, S, K, n, k), n with168 ramsey, colouring, subsetK, infiniteK169have rest := difference.subset_left at170 ramseyStage(C, S, K, n, k), {choose(ramseyStage(C, S, K, n, k))}171have nextSubset : ramseyStep(C, S, K, ramseyStage(C, S, K, n, k), n) ⊆ S :=172 Subset.transitive with (Subset.transitive with next, rest), subsetK173exact⟨nextSubset, nextInfinite⟩174175theorem ramseyStage.subset (i : Nat)176shows ramseyStage(C, S, K, n, i) ⊆ S177proof178have parts := ramseyStage.invariant at C, S, K, n, i with*179exact parts.left180181theorem ramseyStage.infinite (i : Nat)182shows Infinite(ramseyStage(C, S, K, n, i))183proof184have parts := ramseyStage.invariant at C, S, K, n, i with*185exact parts.right186187theorem ramseyPoint.member (i : Nat)188shows ramseyPoint(C, S, K, n, i) ∈ ramseyStage(C, S, K, n, i)189proof190have stageInfinite := ramseyStage.infinite at C, S, K, n, i with*191rw ramseyPoint.value192exact choose.member with (Infinite.nonempty with stageInfinite)193194theorem ramseyPoint.in_set (i : Nat)195shows ramseyPoint(C, S, K, n, i) ∈ S196proof197have stageSubset := ramseyStage.subset at C, S, K, n, i with*198have member := ramseyPoint.member at C, S, K, n, i with*199exact stageSubset with ramseyPoint(C, S, K, n, i), member200201theorem ramseyStage.step (i : Nat)202shows ramseyStage(C, S, K, n, i +1) ⊆203 ramseyStage(C, S, K, n, i) ∖ {ramseyPoint(C, S, K, n, i)} ∧204∃ c ∈ K, ∀ T ∈ subsetsOfSize(ramseyStage(C, S, K, n, i +1), n),205 C(insert(ramseyPoint(C, S, K, n, i), T)) = c206proof207have stageSubset := ramseyStage.subset at C, S, K, n, i with*208have stageInfinite := ramseyStage.infinite at C, S, K, n, i with*209obtain⟨next, _, c, hc, homogeneous⟩:= ramseyStep.properties at210 C, S, K, ramseyStage(C, S, K, n, i), n with211 ramsey, colouring, stageSubset, stageInfinite212constructor213 . rw [ramseyStage.succ, ramseyPoint.value]214exact next215 . refine⟨c, hc, ?_⟩216intros T hT217rw ramseyStage.succ at hT218rw ramseyPoint.value219exact homogeneous with T, hT220221theorem ramseyStage.succ_subset (i : Nat)222shows ramseyStage(C, S, K, n, i +1) ⊆223 ramseyStage(C, S, K, n, i) ∖ {ramseyPoint(C, S, K, n, i)}224proof225have parts := ramseyStage.step at C, S, K, n, i with*226exact parts.left227228theorem ramseyStage.succ_homogeneous (i : Nat)229shows∃ c ∈ K, ∀ T ∈ subsetsOfSize(ramseyStage(C, S, K, n, i +1), n),230 C(insert(ramseyPoint(C, S, K, n, i), T)) = c231proof232have parts := ramseyStage.step at C, S, K, n, i with*233exact parts.right234235theorem ramseyStage.antitone (i d : Nat)236shows ramseyStage(C, S, K, n, i + d) ⊆ ramseyStage(C, S, K, n, i)237proof238induction d239| zero =>240have same : i +0= i byring241rw same242exact Subset.reflexive at ramseyStage(C, S, K, n, i)243| k +1using ih =>244have shape : i + (k +1) = (i + k) +1byring245rw shape246have step := ramseyStage.succ_subset at C, S, K, n, i + k with247 ramsey, colouring, infinite248have rest := difference.subset_left at249 ramseyStage(C, S, K, n, i + k), {ramseyPoint(C, S, K, n, i + k)}250exact Subset.transitive with (Subset.transitive with step, rest), ih251252theorem ramseyStage.monotone (i j : Nat)253requires le : i ≤ j254shows ramseyStage(C, S, K, n, j) ⊆ ramseyStage(C, S, K, n, i)255proof256obtain⟨d, dNat, shape⟩:= le257rw← shape258exact ramseyStage.antitone at C, S, K, n, i, d with*259260-- Later points lie in every earlier successor stage, so the points are261-- pairwise distinct.262theorem ramseyPoint.later_member (i j : Nat)263requires below : i < j264shows ramseyPoint(C, S, K, n, j) ∈ ramseyStage(C, S, K, n, i +1)265proof266have le := Less.gap.forward with below267have nested := ramseyStage.monotone at C, S, K, n, i +1, j with*268have member := ramseyPoint.member at C, S, K, n, j with*269exact nested with ramseyPoint(C, S, K, n, j), member270271theorem ramseyPoint.distinct (i j : Nat)272requires below : i < j273shows ramseyPoint(C, S, K, n, i) ≠ ramseyPoint(C, S, K, n, j)274proof275intro same276have later := ramseyPoint.later_member at C, S, K, n, i, j with*277have excluded := ramseyStage.succ_subset at C, S, K, n, i with*278have inRest := excluded with ramseyPoint(C, S, K, n, j), later279have parts := difference.singleton_membership.forward with inRest280apply parts.right281 symm282exact same283284theorem ramseyPoint.injective_on (i j : Nat)285requires same : ramseyPoint(C, S, K, n, i) = ramseyPoint(C, S, K, n, j)286shows i = j287proof288have alternatives := Less.trichotomy at i, j with*289rcases alternatives with below | equal | above290 . exfalso291have distinct := ramseyPoint.distinct at C, S, K, n, i, j with*292exact distinct with same293 . exact equal294 . exfalso295have distinct := ramseyPoint.distinct at C, S, K, n, j, i with*296apply distinct297 symm298exact same299300end RamseyChain301302-- The colour of stage i: the colour that every n-subset T of Sᵢ₊₁303-- gives to insert(aᵢ, T).304def ramseyColours (C S K : Set, n : Nat, i : Nat) : Set:=305 { c ∈ K |∀ T ∈ subsetsOfSize(ramseyStage(C, S, K, n, i +1), n),306 C(insert(ramseyPoint(C, S, K, n, i), T)) = c }307308def ramseyColour (C S K : Set, n : Nat, i : Nat) : Set:=309 choose(ramseyColours(C, S, K, n, i))310311section RamseyColour312variable (C S K : Set) (n : Nat)313hypothesis ramsey : RamseyProperty(n, K)314hypothesis colouring : FunctionBetween(C, subsetsOfSize(S, n +1), K)315hypothesis infinite : Infinite(S)316317theorem ramseyColour.properties (i : Nat)318shows ramseyColour(C, S, K, n, i) ∈ K ∧319∀ T ∈ subsetsOfSize(ramseyStage(C, S, K, n, i +1), n),320 C(insert(ramseyPoint(C, S, K, n, i), T)) = ramseyColour(C, S, K, n, i)321proof322obtain⟨c, hc, law⟩:=323 ramseyStage.succ_homogeneous at C, S, K, n, i with*324have candidate : c ∈ ramseyColours(C, S, K, n, i) :=325 ramseyColours.membership.backward with⟨hc, law⟩326have inhabited : Nonempty(ramseyColours(C, S, K, n, i)) :=⟨c, candidate⟩327have selected : ramseyColour(C, S, K, n, i) ∈ ramseyColours(C, S, K, n, i)328unfold ramseyColour329exact choose.member with inhabited330exact ramseyColours.membership.forward with selected331332theorem ramseyColour.member (i : Nat)333shows ramseyColour(C, S, K, n, i) ∈ K334proof335have parts := ramseyColour.properties at C, S, K, n, i with*336exact parts.left337338theorem ramseyColour.law (T : Set) (i : Nat)339requires hT : T ∈ subsetsOfSize(ramseyStage(C, S, K, n, i +1), n)340shows C(insert(ramseyPoint(C, S, K, n, i), T)) = ramseyColour(C, S, K, n, i)341proof342have parts := ramseyColour.properties at C, S, K, n, i with*343exact parts.right with T, hT344345end RamseyColour346