1-- The Cantor–Bendixson theorem: in a space with a countable base, a2-- closed set is the disjoint union of its perfect kernel and a countable3-- set of removed points, and its Cantor–Bendixson rank is countable4-- 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.Topology.CantorBendixson.Theorem10import Stabilise1112-- A perfect set is closed and equal to its derived set: closed with no13-- isolated points.14predicate Perfect (X T P : Set) := IsClosed(X, T, P) ∧ derivedSet(X, T, P) = P1516theorem Perfect.closed (X T P : Set)17requires perfect : Perfect(X, T, P)18shows IsClosed(X, T, P)19proof20exact perfect.left2122theorem Perfect.no_isolated_point (X T P x : Set)23requires topology : IsTopology(X, T)24requires perfect : Perfect(X, T, P)25requires hx : x ∈ P26shows¬IsolatedPoint(X, T, P, x)27proof28obtain⟨closed, fixed⟩:= perfect29intro isolated30have removed := derivedSet.removed_iff.backward with31 topology, closed.left, isolated32have parts := difference.membership.forward with removed33apply parts.right34rw fixed35exact parts.left3637-- The perfect kernel is perfect: it is a stage, hence closed, and its38-- derived set is the next stage, which is the same set.39theorem cantorBendixson.kernel_perfect (X T B A : Set)40requires topology : IsTopology(X, T)41requires basis : Basis(X, T, B)42requires closed : IsClosed(X, T, A)43requires countable : Countable(B)44shows Perfect(X, T, perfectKernel(X, T, A))45proof46have same : perfectKernel(X, T, A) =47 derived(X, T, A, cantorBendixsonRank(X, T, A))48unfold perfectKernel49rfl50have hRank := cantorBendixsonRank.ordinal with51 topology, basis, closed, countable52have fixed := cantorBendixsonRank.fixed with53 topology, basis, closed, countable54rw derived.next at fixed55rw same at fixed56rw same57constructor58 . exact derived.closed with topology, closed, hRank59 . exact fixed6061theorem cantorBendixson.kernel_subset (X T B A : Set)62requires topology : IsTopology(X, T)63requires basis : Basis(X, T, B)64requires closed : IsClosed(X, T, A)65requires countable : Countable(B)66shows perfectKernel(X, T, A) ⊆ A67proof68have hRank := cantorBendixsonRank.ordinal with69 topology, basis, closed, countable70unfold perfectKernel71exact derived.subset with topology, closed, hRank7273-- The points removed below stage ρ, each related to the basic sets that74-- witness its removal at the stage where it leaves.75def pointDrops (X T A B ρ : Set) : Set:=76 { p ∈ (A ∖ derived(X, T, A, ρ)) × B |77∃ x ∈ A ∖ derived(X, T, A, ρ), ∃ W ∈ B, p = (x ↦ W) ∧78 (∃ β ∈ ρ, W ∩ derived(X, T, A, β) = {x}) }7980theorem pointDrops.relates (X T A B ρ x W : Set)81shows relates(pointDrops(X, T, A, B, ρ), x, W) ↔82 x ∈ A ∖ derived(X, T, A, ρ) ∧ W ∈ B ∧83 (∃ β ∈ ρ, W ∩ derived(X, T, A, β) = {x})84proof85constructor86 . intro edge87unfold relates at edge88have parts := pointDrops.membership.forward with edge89obtain⟨x2, hx2, W2, hW2, shape, witnessed⟩:= parts.right90obtain⟨rfl, rfl⟩:= orderedPair.injective with shape91exact⟨hx2, hW2, witnessed⟩92 . intro h93obtain⟨hx, hW, witnessed⟩:= h94unfold relates95apply pointDrops.membership.backward96constructor97 . exact product.pair with hx, hW98 . exact⟨x, hx, W, hW, rfl, witnessed⟩99100-- Every removed point has a witness: it leaves at a successor stage below101-- ρ, where a basic set isolates it.102theorem pointDrops.offered (X T A B ρ : Set)103requires topology : IsTopology(X, T)104requires basis : Basis(X, T, B)105requires closed : IsClosed(X, T, A)106requires hRho : ρ ∈Ordinal107shows∀ x ∈ A ∖ derived(X, T, A, ρ), ∃ W ∈ B,108 relates(pointDrops(X, T, A, B, ρ), x, W)109proof110intros x hx111have parts := difference.membership.forward with hx112obtain⟨β, hβ, inBeta, away⟩:= derived.removed_stage with113 topology, closed, hRho, parts.left, parts.right114have hBeta :=Ordinal.member with hRho, hβ115obtain⟨W, hW, trace⟩:= derived.removed_witness with116 topology, basis, closed, hBeta, inBeta, away117witness W118constructor119 . exact hW120 . apply pointDrops.relates.backward121exact⟨hx, hW, β, hβ, trace⟩122123-- A basic set witnesses the removal of at most one point.124theorem pointDrops.separated (X T A B ρ : Set)125requires topology : IsTopology(X, T)126requires basis : Basis(X, T, B)127requires closed : IsClosed(X, T, A)128requires hRho : ρ ∈Ordinal129shows∀ x ∈ A ∖ derived(X, T, A, ρ), ∀ y ∈ A ∖ derived(X, T, A, ρ),130∀ W ∈ B, relates(pointDrops(X, T, A, B, ρ), x, W) →131 relates(pointDrops(X, T, A, B, ρ), y, W) → x = y132proof133intros x hx y hy W hW first second134obtain⟨hx2, hW2, β, hβ, traceX⟩:= pointDrops.relates.forward with135 first136obtain⟨hy2, hW3, δ, hδ, traceY⟩:= pointDrops.relates.forward with137 second138have hBeta :=Ordinal.member with hRho, hβ139have hDelta :=Ordinal.member with hRho, hδ140have both := derived.witness_injective with141 topology, basis, closed, hBeta, hDelta, hW, traceX, traceY142exact both.right143144-- The points removed below any stage are countable: each selects the145-- witness of least code, injectively into the base.146theorem derived.removed_countable (X T A B ρ : Set)147requires topology : IsTopology(X, T)148requires basis : Basis(X, T, B)149requires closed : IsClosed(X, T, A)150requires countable : Countable(B)151requires hRho : ρ ∈Ordinal152shows Countable(A ∖ derived(X, T, A, ρ))153proof154obtain⟨g, code⟩:= countable155have offered := pointDrops.offered with topology, basis, closed, hRho156have separated := pointDrops.separated with topology, basis, closed, hRho157exact leastCoded.countable with code, offered, separated158159theorem cantorBendixson.scattered_countable (X T B A : Set)160requires topology : IsTopology(X, T)161requires basis : Basis(X, T, B)162requires closed : IsClosed(X, T, A)163requires countable : Countable(B)164shows Countable(A ∖ perfectKernel(X, T, A))165proof166have hRank := cantorBendixsonRank.ordinal with167 topology, basis, closed, countable168unfold perfectKernel169exact derived.removed_countable with170 topology, basis, closed, countable, hRank171172-- The rank is countable: every stage below it drops, so the stages below173-- the rank select witnesses injectively into the base.174theorem cantorBendixson.rank_countable (X T B A : Set)175requires topology : IsTopology(X, T)176requires basis : Basis(X, T, B)177requires closed : IsClosed(X, T, A)178requires countable : Countable(B)179shows Countable(cantorBendixsonRank(X, T, A))180proof181obtain⟨g, code⟩:= countable182have countableB : Countable(B) :=⟨g, code⟩183have hRank := cantorBendixsonRank.ordinal with184 topology, basis, closed, countableB185have ordinals : ∀ γ ∈ cantorBendixsonRank(X, T, A), γ ∈Ordinal186intros γ hγ187exactOrdinal.member with hRank, hγ188have offered : ∀ γ ∈ cantorBendixsonRank(X, T, A), ∃ W ∈ B,189 relates(stageDrops(X, T, A, B, cantorBendixsonRank(X, T, A)), γ, W)190intros γ hγ191have hGamma := ordinals with γ, hγ192have strict : derived(X, T, A, next(γ)) ≠ derived(X, T, A, γ)193intro stable194have below := cantorBendixsonRank.least with195 topology, basis, closed, countableB, hGamma, stable196exactOrdinal.not_member_of_subset with below, hγ197obtain⟨W, hW, x, trace⟩:= stageDrops.offered with198 topology, basis, closed, hGamma, strict199witness W200constructor201 . exact hW202 . apply stageDrops.relates.backward203exact⟨hγ, hW, x, trace⟩204have separated := stageDrops.separated with topology, basis, closed, ordinals205exact leastCoded.countable with code, offered, separated206207-- Cantor–Bendixson: a closed set in a space with a countable base is the208-- disjoint union of its perfect kernel and a countable set.209theorem cantorBendixson (X T B A : Set)210requires topology : IsTopology(X, T)211requires basis : Basis(X, T, B)212requires closed : IsClosed(X, T, A)213requires countable : Countable(B)214shows Perfect(X, T, perfectKernel(X, T, A)) ∧215 perfectKernel(X, T, A) ⊆ A ∧ Countable(A ∖ perfectKernel(X, T, A))216proof217refine⟨?_, ?_, ?_⟩218 . exact cantorBendixson.kernel_perfect with topology, basis, closed, countable219 . exact cantorBendixson.kernel_subset with topology, basis, closed, countable220 . exact cantorBendixson.scattered_countable with221 topology, basis, closed, countable222