1-- Topological spaces: a topology on a set is a family of subsets closed2-- under arbitrary unions and binary intersections and containing the whole3-- set; closed sets, interior, and closure, with their basic laws4-- 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.Space10import Set1112predicate IsTopology (X T : Set) :=13 (T ⊆𝒫(X)) ∧ X ∈ T ∧14 (∀ S, S ⊆ T →⋃(S) ∈ T) ∧15 (∀ U ∈ T, ∀ V ∈ T, U ∩ V ∈ T)1617-- The members of T are the open sets; a closed set is a subset of X whose18-- complement in X is open.19predicate IsClosed (X T C : Set) := (C ⊆ X) ∧ complement(X, C) ∈ T2021theorem IsTopology.subset (X T U : Set)22requires topology : IsTopology(X, T)23requires hU : U ∈ T24shows U ⊆ X25proof26obtain⟨subsets, whole, unions, intersections⟩:= topology27have member := subsets with U, hU28exact powerset.membership_iff.forward with member2930theorem IsTopology.whole (X T : Set)31requires topology : IsTopology(X, T)32shows X ∈ T33proof34obtain⟨subsets, whole, unions, intersections⟩:= topology35exact whole3637theorem IsTopology.union (X T S : Set)38requires topology : IsTopology(X, T)39requires hS : S ⊆ T40shows⋃(S) ∈ T41proof42obtain⟨subsets, whole, unions, intersections⟩:= topology43exact unions with S, hS4445theorem IsTopology.intersection2 (X T U V : Set)46requires topology : IsTopology(X, T)47requires hU : U ∈ T48requires hV : V ∈ T49shows U ∩ V ∈ T50proof51obtain⟨subsets, whole, unions, intersections⟩:= topology52exact intersections with U, hU, V, hV5354theorem IsTopology.empty (X T : Set)55requires topology : IsTopology(X, T)56shows∅∈ T57proof58have inside := empty.subset at T59have member := IsTopology.union with topology, inside60 rwa union.empty at member6162theorem IsTopology.union2 (X T U V : Set)63requires topology : IsTopology(X, T)64requires hU : U ∈ T65requires hV : V ∈ T66shows U ∪ V ∈ T67proof68have inside : {U, V} ⊆ T69intros W hW70havesplit:= pair.membership.forward with hW71casessplitwith isU | isV72 . rw isU73exact hU74 . rw isV75exact hV76have member := IsTopology.union with topology, inside77 rwa union.pair at member7879-- Closed sets8081theorem IsClosed.whole (X T : Set)82requires topology : IsTopology(X, T)83shows IsClosed(X, T, X)84proof85constructor86 . exact Subset.reflexive at X87 . rw complement.self88exact IsTopology.empty with topology8990theorem IsClosed.empty (X T : Set)91requires topology : IsTopology(X, T)92shows IsClosed(X, T, ∅)93proof94constructor95 . exact empty.subset at X96 . rw complement.empty97exact IsTopology.whole with topology9899theorem IsClosed.complement_open (X T C : Set)100requires closed : IsClosed(X, T, C)101shows complement(X, C) ∈ T102proof103exact closed.right104105theorem Open.complement_closed (X T U : Set)106requires topology : IsTopology(X, T)107requires hU : U ∈ T108shows IsClosed(X, T, complement(X, U))109proof110have inside := IsTopology.subset with topology, hU111constructor112 . exact complement.subset at X, U113 . have double := complement.complement with inside114 rwa double115116theorem IsClosed.union2 (X T C D : Set)117requires topology : IsTopology(X, T)118requires hC : IsClosed(X, T, C)119requires hD : IsClosed(X, T, D)120shows IsClosed(X, T, C ∪ D)121proof122constructor123 . exact union2.subset with hC.left, hD.left124 . rw complement.union2_distrib125exact IsTopology.intersection2 with topology, hC.right, hD.right126127theorem IsClosed.intersection2 (X T C D : Set)128requires topology : IsTopology(X, T)129requires hC : IsClosed(X, T, C)130requires hD : IsClosed(X, T, D)131shows IsClosed(X, T, C ∩ D)132proof133constructor134 . exact Subset.transitive with (intersection.subset_left at C, D), hC.left135 . rw complement.intersection_distrib136exact IsTopology.union2 with topology, hC.right, hD.right137138theorem IsClosed.family_intersection (X T S : Set)139requires topology : IsTopology(X, T)140requires nonempty : Nonempty(S)141requires closed : ∀ C ∈ S, IsClosed(X, T, C)142shows IsClosed(X, T, ⋂(S))143proof144obtain⟨C, hC⟩:= nonempty145have inhabited : Nonempty(S) :=⟨C, hC⟩146constructor147 . have below := familyIntersection.subset_of_member with hC148have closedC := closed with C, hC149exact Subset.transitive with below, closedC.left150 . have demorgan := complements.intersection at X, S with inhabited151rw demorgan152apply IsTopology.union with topology153intros V hV154obtain⟨D, hD, shape⟩:= complements.membership.forward with hV155rw← shape156have closedD := closed with D, hD157have openD := closedD.right158unfold complement at openD159exact openD160161-- Interior: the largest open set inside A.162163def openSubsets (T A : Set) : Set:= { U ∈ T | U ⊆ A }164def interiorIn (T A : Set) : Set:=⋃(openSubsets(T, A))165166theorem interiorIn.open (X T A : Set)167requires topology : IsTopology(X, T)168shows interiorIn(T, A) ∈ T169proof170unfold interiorIn171apply IsTopology.union with topology172intros U hU173have parts := openSubsets.membership.forward with hU174exact parts.left175176theorem interiorIn.subset (T A : Set)177shows interiorIn(T, A) ⊆ A178proof179unfold interiorIn180apply union.subset181intros U hU182have parts := openSubsets.membership.forward with hU183exact parts.right184185theorem interiorIn.greatest (T A U : Set)186requires hU : U ∈ T187requires inside : U ⊆ A188shows U ⊆ interiorIn(T, A)189proof190have member : U ∈ openSubsets(T, A) := openSubsets.membership.backward with⟨hU, inside⟩191unfold interiorIn192exact union.subset_of_member with member193194theorem interiorIn.of_open (T U : Set)195requires hU : U ∈ T196shows interiorIn(T, U) = U197proof198apply Subset.antisymmetric199 . exact interiorIn.subset at T, U200 . exact interiorIn.greatest with hU, (Subset.reflexive at U)201202theorem interiorIn.monotone (X T A B : Set)203requires topology : IsTopology(X, T)204requires hAB : A ⊆ B205shows interiorIn(T, A) ⊆ interiorIn(T, B)206proof207have isOpen := interiorIn.open at X, T, A with topology208have inside := Subset.transitive with (interiorIn.subset at T, A), hAB209exact interiorIn.greatest with isOpen, inside210211theorem interiorIn.idempotent (X T A : Set)212requires topology : IsTopology(X, T)213shows interiorIn(T, interiorIn(T, A)) = interiorIn(T, A)214proof215exact interiorIn.of_open with (interiorIn.open at X, T, A with topology)216217theorem interiorIn.intersection2 (X T A B : Set)218requires topology : IsTopology(X, T)219shows interiorIn(T, A ∩ B) = interiorIn(T, A) ∩ interiorIn(T, B)220proof221apply Subset.antisymmetric222 . apply intersection.greatest223 . exact interiorIn.monotone with topology, (intersection.subset_left at A, B)224 . exact interiorIn.monotone with topology, (intersection.subset_right at A, B)225 . have openA := interiorIn.open at X, T, A with topology226have openB := interiorIn.open at X, T, B with topology227have isOpen := IsTopology.intersection2 with topology, openA, openB228have inside := intersection.monotone with (interiorIn.subset at T, A), (interiorIn.subset at T, B)229exact interiorIn.greatest with isOpen, inside230231theorem Open.iff_interior (X T U : Set)232requires topology : IsTopology(X, T)233shows U ∈ T ↔ interiorIn(T, U) = U234proof235constructor236 . intro hU237exact interiorIn.of_open with hU238 . intro equal239have isOpen := interiorIn.open at X, T, U with topology240 rwa equal at isOpen241242-- Closure: the smallest closed set around A.243244def closedSupersets (X T A : Set) : Set:= { C ∈𝒫(X) | IsClosed(X, T, C) ∧ A ⊆ C }245def closure (X T A : Set) : Set:= ⋂(closedSupersets(X, T, A))246247theorem closedSupersets.whole (X T A : Set)248requires topology : IsTopology(X, T)249requires hA : A ⊆ X250shows X ∈ closedSupersets(X, T, A)251proof252exact closedSupersets.membership.backward with⟨(powerset.membership_iff.backward with (Subset.reflexive at X)), (IsClosed.whole with topology), hA⟩253254theorem closure.closed (X T A : Set)255requires topology : IsTopology(X, T)256requires hA : A ⊆ X257shows IsClosed(X, T, closure(X, T, A))258proof259have whole := closedSupersets.whole with topology, hA260have inhabited : Nonempty(closedSupersets(X, T, A)) :=⟨X, whole⟩261unfold closure262apply IsClosed.family_intersection with topology, inhabited263intros C hC264have parts := closedSupersets.membership.forward with hC265have rest := parts.right266exact rest.left267268theorem closure.contains (X T A : Set)269requires topology : IsTopology(X, T)270requires hA : A ⊆ X271shows A ⊆ closure(X, T, A)272proof273have whole := closedSupersets.whole with topology, hA274have inhabited : Nonempty(closedSupersets(X, T, A)) :=⟨X, whole⟩275unfold closure276apply familyIntersection.greatest with inhabited277intros C hC278have parts := closedSupersets.membership.forward with hC279have rest := parts.right280exact rest.right281282theorem closure.least (X T A C : Set)283requires closed : IsClosed(X, T, C)284requires hAC : A ⊆ C285shows closure(X, T, A) ⊆ C286proof287have member : C ∈ closedSupersets(X, T, A)288exact closedSupersets.membership.backward with⟨(powerset.membership_iff.backward with closed.left), closed, hAC⟩289unfold closure290exact familyIntersection.subset_of_member with member291292theorem closure.subset (X T A : Set)293requires topology : IsTopology(X, T)294requires hA : A ⊆ X295shows closure(X, T, A) ⊆ X296proof297exact closure.least with (IsClosed.whole with topology), hA298299theorem closure.of_closed (X T C : Set)300requires topology : IsTopology(X, T)301requires closed : IsClosed(X, T, C)302shows closure(X, T, C) = C303proof304apply Subset.antisymmetric305 . exact closure.least with closed, (Subset.reflexive at C)306 . exact closure.contains with topology, closed.left307308theorem closure.monotone (X T A B : Set)309requires topology : IsTopology(X, T)310requires hB : B ⊆ X311requires hAB : A ⊆ B312shows closure(X, T, A) ⊆ closure(X, T, B)313proof314have inside := Subset.transitive with hAB, (closure.contains with topology, hB)315exact closure.least with inside316317theorem closure.idempotent (X T A : Set)318requires topology : IsTopology(X, T)319requires hA : A ⊆ X320shows closure(X, T, closure(X, T, A)) = closure(X, T, A)321proof322exact closure.of_closed with topology, (closure.closed with topology, hA)323324theorem closure.union2 (X T A B : Set)325requires topology : IsTopology(X, T)326requires hA : A ⊆ X327requires hB : B ⊆ X328shows closure(X, T, A ∪ B) = closure(X, T, A) ∪ closure(X, T, B)329proof330have hAB := union2.subset with hA, hB331apply Subset.antisymmetric332 . have closed := IsClosed.union2 with topology, (closure.closed with topology, hA), (closure.closed with topology, hB)333have inside := union2.monotone with (closure.contains with topology, hA), (closure.contains with topology, hB)334exact closure.least with closed, inside335 . apply union2.subset336 . exact closure.monotone with topology, hAB, (union2.subset_left at A, B)337 . exact closure.monotone with topology, hAB, (union2.subset_right at A, B)338339theorem IsClosed.iff_closure (X T C : Set)340requires topology : IsTopology(X, T)341requires hC : C ⊆ X342shows IsClosed(X, T, C) ↔ closure(X, T, C) = C343proof344constructor345 . intro closed346exact closure.of_closed with topology, closed347 . intro equal348have closed := closure.closed with topology, hC349 rwa equal at closed350351-- A point lies in the closure of A exactly when every open set around it352-- meets A.353theorem closure.membership (X T A x : Set)354requires topology : IsTopology(X, T)355requires hA : A ⊆ X356shows x ∈ closure(X, T, A) ↔ x ∈ X ∧ (∀ U ∈ T, x ∈ U → Nonempty(U ∩ A))357proof358constructor359 . intro hx360have inside := closure.subset with topology, hA361have inX := inside with x, hx362refine⟨inX, ?_⟩363intros U hU hxU364by_contra empty365have missing : A ⊆ complement(X, U)366intros a ha367apply complement.membership.backward368refine⟨(hA with a, ha), ?_⟩369intro inU370apply empty371witness a372exact intersection.membership.backward with⟨inU, ha⟩373have closed := Open.complement_closed with topology, hU374have below := closure.least with missing375have inside2 := below with x, hx376have parts := complement.membership.forward with inside2377exact parts.right with hxU378 . intro meets379obtain⟨inX, touches⟩:= meets380by_contra outside381have closed := closure.closed with topology, hA382have hU := IsClosed.complement_open with closed383have hxU : x ∈ complement(X, closure(X, T, A)) := complement.membership.backward with⟨inX, outside⟩384obtain⟨a, ha⟩:= touches with complement(X, closure(X, T, A)), hU, hxU385have parts := intersection.membership.forward with ha386have contains := closure.contains with topology, hA387have inClosure := contains with a, parts.right388have away := complement.membership.forward with parts.left389exact away.right with inClosure390391predicate IsDense (X T A : Set) := closure(X, T, A) = X392