1-- Bases of a topology: every open set is a union of basis sets, the2-- topology generated by a family satisfying the basis axioms, closure,3-- interior, and continuity tested on a basis, bases of subspaces and4-- products, and the open balls as a basis of the real line5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.Topology.Basis11import Separation12import Real1314predicate Basis (X T B : Set) :=15 B ⊆ T ∧ (∀ U ∈ T, ∀ x ∈ U, ∃ W ∈ B, x ∈ W ∧ W ⊆ U)1617theorem Basis.subset (X T B : Set)18requires basis : Basis(X, T, B)19shows B ⊆ T20proof21exact basis.left2223theorem Basis.refine (X T B U x : Set)24requires basis : Basis(X, T, B)25requires hU : U ∈ T26requires hx : x ∈ U27shows∃ W ∈ B, x ∈ W ∧ W ⊆ U28proof29exact basis.right with U, hU, x, hx3031theorem Basis.self (X T : Set)32shows Basis(X, T, T)33proof34constructor35 . exact Subset.reflexive at T36 . intros U hU x hx37exact⟨U, hU, hx, (Subset.reflexive at U)⟩3839-- The basis sets inside U.4041def basisBelow (B U : Set) : Set:= { W ∈ B | W ⊆ U }4243theorem basisBelow.union (B U : Set)44requires refines : ∀ x ∈ U, ∃ W ∈ B, x ∈ W ∧ W ⊆ U45shows U =⋃(basisBelow(B, U))46proof47apply Subset.antisymmetric48 . intros x hx49obtain⟨W, hW, hxW, below⟩:= refines with x, hx50have found : W ∈ basisBelow(B, U) := basisBelow.membership.backward with⟨hW, below⟩51exact union.contains with found, hxW52 . apply union.subset53intros W hW54have parts := basisBelow.membership.forward with hW55exact parts.right5657theorem basisBelow.open (X T B U : Set)58requires topology : IsTopology(X, T)59requires inside : B ⊆ T60shows⋃(basisBelow(B, U)) ∈ T61proof62apply IsTopology.union with topology63intros W hW64have parts := basisBelow.membership.forward with hW65exact inside with W, parts.left6667-- Every open set is the union of the basis sets inside it.68theorem Basis.open_union (X T B U : Set)69requires basis : Basis(X, T, B)70requires hU : U ∈ T71shows U =⋃(basisBelow(B, U))72proof73apply basisBelow.union74intros x hx75exact basis.right with U, hU, x, hx7677theorem Basis.open_iff (X T B U : Set)78requires topology : IsTopology(X, T)79requires basis : Basis(X, T, B)80shows U ∈ T ↔ U ⊆ X ∧ (∀ x ∈ U, ∃ W ∈ B, x ∈ W ∧ W ⊆ U)81proof82constructor83 . intro hU84constructor85 . exact IsTopology.subset with topology, hU86 . intros x hx87exact basis.right with U, hU, x, hx88 . intro h89obtain⟨inside, refines⟩:= h90have equal := basisBelow.union with refines91rw equal92exact basisBelow.open with topology, basis.left9394theorem Basis.covers (X T B : Set)95requires topology : IsTopology(X, T)96requires basis : Basis(X, T, B)97shows⋃(B) = X98proof99apply Subset.antisymmetric100 . apply union.subset101intros W hW102exact IsTopology.subset with topology, (basis.left with W, hW)103 . intros x hx104obtain⟨W, hW, hxW, inside⟩:= basis.right with X, (IsTopology.whole with topology), x, hx105exact union.contains with hW, hxW106107theorem Basis.meet (X T B W1 W2 x : Set)108requires topology : IsTopology(X, T)109requires basis : Basis(X, T, B)110requires hW1 : W1 ∈ B111requires hW2 : W2 ∈ B112requires hx : x ∈ W1 ∩ W2113shows∃ W ∈ B, x ∈ W ∧ W ⊆ W1 ∩ W2114proof115have openMeet := IsTopology.intersection2 with topology, (basis.left with W1, hW1), (basis.left with W2, hW2)116exact basis.right with W1 ∩ W2, openMeet, x, hx117118-- The basis axioms: a family of subsets of X that covers X and refines119-- pairwise intersections; such a family generates a topology.120121predicate BasisAxioms (X B : Set) :=122 B ⊆𝒫(X) ∧ (∀ x ∈ X, ∃ W ∈ B, x ∈ W) ∧123 (∀ W1 ∈ B, ∀ W2 ∈ B, ∀ x ∈ W1 ∩ W2, ∃ W ∈ B, x ∈ W ∧ W ⊆ W1 ∩ W2)124125theorem Basis.axioms (X T B : Set)126requires topology : IsTopology(X, T)127requires basis : Basis(X, T, B)128shows BasisAxioms(X, B)129proof130refine⟨?_, ?_, ?_⟩131 . intros W hW132apply powerset.membership_iff.backward133exact IsTopology.subset with topology, (basis.left with W, hW)134 . intros x hx135obtain⟨W, hW, hxW, inside⟩:= basis.right with X, (IsTopology.whole with topology), x, hx136exact⟨W, hW, hxW⟩137 . intros W1 hW1 W2 hW2 x hx138exact Basis.meet with topology, basis, hW1, hW2, hx139140def generatedTopology (X B : Set) : Set:=141 { U ∈𝒫(X) |∀ x ∈ U, ∃ W ∈ B, x ∈ W ∧ W ⊆ U }142143theorem generatedTopology.contains (X B : Set)144requires axioms : BasisAxioms(X, B)145shows B ⊆ generatedTopology(X, B)146proof147intros W hW148apply generatedTopology.membership.backward149refine⟨(axioms.left with W, hW), ?_⟩150intros x hx151exact⟨W, hW, hx, (Subset.reflexive at W)⟩152153theorem generatedTopology.topology (X B : Set)154requires axioms : BasisAxioms(X, B)155shows IsTopology(X, generatedTopology(X, B))156proof157obtain⟨inside, covers, meets⟩:= axioms158refine⟨?_, ?_, ?_, ?_⟩159 . intros U hU160have parts := generatedTopology.membership.forward with hU161exact parts.left162 . apply generatedTopology.membership.backward163refine⟨(powerset.self at X), ?_⟩164intros x hx165obtain⟨W, hW, hxW⟩:= covers with x, hx166have below := powerset.membership_iff.forward with (inside with W, hW)167exact⟨W, hW, hxW, below⟩168 . intros S hS169apply generatedTopology.membership.backward170constructor171 . apply powerset.membership_iff.backward172apply union.subset173intros U hU174have parts := generatedTopology.membership.forward with (hS with U, hU)175exact powerset.membership_iff.forward with parts.left176 . intros x hx177obtain⟨U, hU, hxU⟩:= union.membership.forward with hx178have parts := generatedTopology.membership.forward with (hS with U, hU)179obtain⟨W, hW, hxW, below⟩:= parts.right with x, hxU180refine⟨W, hW, hxW, ?_⟩181exact Subset.transitive with below, (union.subset_of_member with hU)182 . intros U hU V hV183have partsU := generatedTopology.membership.forward with hU184have partsV := generatedTopology.membership.forward with hV185apply generatedTopology.membership.backward186constructor187 . apply powerset.membership_iff.backward188have insideU := powerset.membership_iff.forward with partsU.left189exact Subset.transitive with (intersection.subset_left at U, V), insideU190 . intros x hx191have both := intersection.membership.forward with hx192obtain⟨W1, hW1, hxW1, belowU⟩:= partsU.right with x, both.left193obtain⟨W2, hW2, hxW2, belowV⟩:= partsV.right with x, both.right194have inMeet : x ∈ W1 ∩ W2 := intersection.membership.backward with⟨hxW1, hxW2⟩195obtain⟨W, hW, hxW, below⟩:= meets with W1, hW1, W2, hW2, x, inMeet196refine⟨W, hW, hxW, ?_⟩197exact Subset.transitive with below, (intersection.monotone with belowU, belowV)198199theorem generatedTopology.basis (X B : Set)200requires axioms : BasisAxioms(X, B)201shows Basis(X, generatedTopology(X, B), B)202proof203constructor204 . exact generatedTopology.contains with axioms205 . intros U hU x hx206have parts := generatedTopology.membership.forward with hU207exact parts.right with x, hx208209-- The generated topology is the coarsest topology containing B.210theorem generatedTopology.coarsest (X T B : Set)211requires topology : IsTopology(X, T)212requires inside : B ⊆ T213shows generatedTopology(X, B) ⊆ T214proof215intros U hU216have parts := generatedTopology.membership.forward with hU217have equal := basisBelow.union with parts.right218rw equal219exact basisBelow.open with topology, inside220221theorem Basis.generated (X T B : Set)222requires topology : IsTopology(X, T)223requires basis : Basis(X, T, B)224shows T = generatedTopology(X, B)225proof226apply Subset.antisymmetric227 . intros U hU228apply generatedTopology.membership.backward229constructor230 . exact powerset.membership_iff.backward with (IsTopology.subset with topology, hU)231 . intros x hx232exact basis.right with U, hU, x, hx233 . exact generatedTopology.coarsest with topology, basis.left234235-- Two bases of the same space determine each other's topology.236theorem Basis.unique_topology (X T T2 B : Set)237requires topology : IsTopology(X, T)238requires topology2 : IsTopology(X, T2)239requires basis : Basis(X, T, B)240requires basis2 : Basis(X, T2, B)241shows T = T2242proof243have first := Basis.generated with topology, basis244have second := Basis.generated with topology2, basis2245rw [first, second]246rfl247248-- Closure, interior, and continuity are tested on a basis.249250theorem Basis.closure_membership (X T B A x : Set)251requires topology : IsTopology(X, T)252requires basis : Basis(X, T, B)253requires hA : A ⊆ X254shows x ∈ closure(X, T, A) ↔ x ∈ X ∧ (∀ W ∈ B, x ∈ W → Nonempty(W ∩ A))255proof256have characterization := closure.membership at X, T, A, x with topology, hA257constructor258 . intro hx259have parts := characterization.forward with hx260refine⟨parts.left, ?_⟩261intros W hW hxW262exact parts.right with W, (basis.left with W, hW), hxW263 . intro h264obtain⟨hxX, meets⟩:= h265apply characterization.backward266refine⟨hxX, ?_⟩267intros U hU hxU268obtain⟨W, hW, hxW, below⟩:= basis.right with U, hU, x, hxU269obtain⟨a, ha⟩:= meets with W, hW, hxW270have inner := intersection.membership.forward with ha271witness a272exact intersection.membership.backward with⟨(below with a, inner.left), inner.right⟩273274theorem Basis.interior_membership (X T B A x : Set)275requires basis : Basis(X, T, B)276shows x ∈ interiorIn(T, A) ↔∃ W ∈ B, x ∈ W ∧ W ⊆ A277proof278have characterization := interiorIn.membership at T, A, x279constructor280 . intro hx281obtain⟨U, hU, hxU, below⟩:= characterization.forward with hx282obtain⟨W, hW, hxW, belowU⟩:= basis.right with U, hU, x, hxU283exact⟨W, hW, hxW, (Subset.transitive with belowU, below)⟩284 . intro h285obtain⟨W, hW, hxW, below⟩:= h286apply characterization.backward287exact⟨W, (basis.left with W, hW), hxW, below⟩288289theorem ContinuousMap.of_basis (f X T Y S B : Set)290requires topology : IsTopology(X, T)291requires basis : Basis(Y, S, B)292requires function : f : X → Y293requires pulledOpen : ∀ W ∈ B, functionPreimage(f, X, W) ∈ T294shows ContinuousMap(f, X, T, Y, S)295proof296constructor297 . exact function298 . intros V hV299have equal : functionPreimage(f, X, V) =⋃(preimages(f, X, basisBelow(B, V)))300ext x301constructor302 . intro hx303have parts := functionPreimage.membership.forward with hx304obtain⟨W, hW, inW, below⟩:= basis.right with V, hV, f(x), parts.right305have found : W ∈ basisBelow(B, V) := basisBelow.membership.backward with⟨hW, below⟩306have pulled : functionPreimage(f, X, W) ∈ preimages(f, X, basisBelow(B, V)) := preimages.membership.backward with⟨W, found, rfl⟩307have inPreimage : x ∈ functionPreimage(f, X, W) := functionPreimage.membership.backward with⟨parts.left, inW⟩308exact union.contains with pulled, inPreimage309 . intro hx310obtain⟨P, hP, hxP⟩:= union.membership.forward with hx311obtain⟨W, hW, shape⟩:= preimages.membership.forward with hP312rw← shape at hxP313have parts := functionPreimage.membership.forward with hxP314have belowParts := basisBelow.membership.forward with hW315apply functionPreimage.membership.backward316exact⟨parts.left, (belowParts.right with f(x), parts.right)⟩317rw equal318apply IsTopology.union with topology319intros P hP320obtain⟨W, hW, shape⟩:= preimages.membership.forward with hP321rw← shape322have belowParts := basisBelow.membership.forward with hW323exact pulledOpen with W, belowParts.left324325-- Bases of subspaces and products.326327theorem Basis.subspace (X T B A : Set)328requires basis : Basis(X, T, B)329requires hA : A ⊆ X330shows Basis(A, subspaceTopology(T, A), subspaceTopology(B, A))331proof332constructor333 . intros W hW334obtain⟨W0, hW0, shape⟩:= subspaceTopology.membership.forward with hW335apply subspaceTopology.membership.backward336exact⟨W0, (basis.left with W0, hW0), shape⟩337 . intros V hV x hx338obtain⟨U, hU, shape⟩:= subspaceTopology.membership.forward with hV339rw← shape at hx340have parts := intersection.membership.forward with hx341obtain⟨W, hW, hxW, below⟩:= basis.right with U, hU, x, parts.left342have traced : W ∩ A ∈ subspaceTopology(B, A) := subspaceTopology.membership.backward with⟨W, hW, rfl⟩343refine⟨W ∩ A, traced, (intersection.membership.backward with⟨hxW, parts.right⟩), ?_⟩344rw← shape345exact intersection.monotone_left with below346347def boxes (X T Y S : Set) : Set:=348 { W ∈𝒫(X × Y) |∃ U ∈ T, ∃ V ∈ S, W = U × V }349350theorem Basis.product (X T Y S : Set)351requires topologyX : IsTopology(X, T)352requires topologyY : IsTopology(Y, S)353shows Basis(X × Y, productTopology(X, T, Y, S), boxes(X, T, Y, S))354proof355constructor356 . intros W hW357have parts := boxes.membership.forward with hW358obtain⟨U, hU, V, hV, shape⟩:= parts.right359rw shape360exact productTopology.box with topologyX, topologyY, hU, hV361 . intros W hW p hp362have parts := productTopology.membership.forward with hW363obtain⟨U, hU, V, hV, inBox, below⟩:= parts.right with p, hp364have inside := product.monotone with (IsTopology.subset with topologyX, hU), (IsTopology.subset with topologyY, hV)365have found : U × V ∈ boxes(X, T, Y, S)366apply boxes.membership.backward367exact⟨(powerset.membership_iff.backward with inside), U, hU, V, hV, rfl⟩368exact⟨U × V, found, inBox, below⟩369370-- The open balls form a basis of the real line.371372def openBalls : Set:=373 { W ∈𝒫(ℝ) |∃ C ∈ ℝ, ∃ E ∈ ℝ, 0 < E ∧ W = openBall(C, E) }374375theorem openBall.member_balls (C E : Real)376requires positive : 0 < E377shows openBall(C, E) ∈ openBalls378proof379have realC : C ∈ ℝ bytypecheck380have realE : E ∈ ℝ bytypecheck381apply openBalls.membership.backward382exact⟨(powerset.membership_iff.backward with (openBall.subset_reals at C, E)), C, realC, E, realE, positive, rfl⟩383384theorem realTopology.basis385shows Basis(ℝ, realTopology, openBalls)386proof387constructor388 . intros W hW389have parts := openBalls.membership.forward with hW390obtain⟨C, hC, E, hE, positive, shape⟩:= parts.right391rw shape392exact openBall.open at C, E393 . intros U hU x hx394have parts := realTopology.membership.forward with hU395have inside := powerset.membership_iff.forward with parts.left396have hxR := inside with x, hx397obtain⟨E, hE, positive, below⟩:= parts.right with x, hx398have center := openBall.center at x, E with positive399have found := openBall.member_balls at x, E with positive400exact⟨openBall(x, E), found, center, below⟩401