1-- Cardinality under bijections, insertion and removal of a point, the2-- empty set as the set of size zero, differences of finite sets, and3-- induction over finite subsets of a set (ZF).4-- 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.Cardinal.Counting10import Enumeration1112theorem cardinality.bijective (A B f : Set)13requires finite : Finite(A)14requires bijective : BijectiveBetween(f, A, B)15shows cardinality(A) = cardinality(B)16proof17obtain⟨natural, g, enumeration⟩:= cardinality.correct with finite18have inverse := converse.bijective with bijective19have composed := compose.bijective at g, converse(f, A, B), B, A, cardinality(A) with*20have other : HasCardinality(B, cardinality(A))21unfold HasCardinality22refine⟨natural, ?_⟩23unfold Equipotent24refine⟨compose(g, converse(f, A, B), B, A, cardinality(A)), composed⟩25 symm26exact cardinality.eq_of_has with other2728theorem cardinality.insert (A a : Set)29requires finite : Finite(A)30requires fresh : ¬(a ∈ A)31shows cardinality(insert(a, A)) = succ(cardinality(A))32proof33obtain⟨natural, f, enumeration⟩:= cardinality.correct with finite34have newIndex := membership.irreflexive at cardinality(A)35have extended := adjoinMap.bijective with*36have successor := natural.successor_as_insert with natural37rw← successor at extended38have next := Naturals.constructor.succ with natural39have cardinal : HasCardinality(insert(a, A), succ(cardinality(A)))40unfold HasCardinality41refine⟨next, ?_⟩42unfold Equipotent43refine⟨adjoinMap(f, a, cardinality(A)), extended⟩44exact cardinality.eq_of_has with cardinal4546theorem Finite.difference (A B : Set)47requires finite : Finite(A)48shows Finite(A ∖ B)49proof50exact Finite.subset with finite, (difference.subset_left at A, B)5152theorem cardinality.remove (A a : Set)53requires finite : Finite(A)54requires member : a ∈ A55shows cardinality(A) = succ(cardinality(A ∖ {a}))56proof57have smaller := Finite.difference at A, {a} with finite58have fresh : ¬(a ∈ A ∖ {a}) bysimp [difference.membership, singleton.membership]59have restored := insert.restore with member60have count := cardinality.insert with*61 rwa restored at count6263-- The same counts in the form n + 1, and with the point adjoined as a union.64theorem cardinality.insert_add_one (A a : Set)65requires finite : Finite(A)66requires fresh : ¬a ∈ A67shows cardinality(insert(a, A)) = cardinality(A) +168proof69have inserted := cardinality.insert with finite, fresh70have natA := HasCardinality.natural with (cardinality.correct with finite)71have one := add.right_one at cardinality(A)72rw [inserted, one]73rfl7475theorem cardinality.union_singleton (A a : Set)76requires finite : Finite(A)77requires fresh : ¬a ∈ A78shows cardinality(A ∪ {a}) = cardinality(A) +179proof80have asInsert : A ∪ {a} = insert(a, A)81unfold insert82exact union2.commutative at A, {a}83rw asInsert84exact cardinality.insert_add_one with finite, fresh8586theorem cardinality.remove_add_one (A a : Set)87requires finite : Finite(A)88requires member : a ∈ A89shows cardinality(A) = cardinality(A ∖ {a}) +190proof91have removed := cardinality.remove with finite, member92have restFinite := Finite.difference at A, {a} with finite93have restNat := HasCardinality.natural94with (cardinality.correct with restFinite)95have one := add.right_one at cardinality(A ∖ {a})96rw [removed, one]97rfl9899-- A finite set of size zero is empty.100theorem HasCardinality.empty_of_zero (A : Set)101requires counted : HasCardinality(A, 0)102shows A =∅103proof104rcases counted with⟨natural, f, bijective⟩105have function := Bijective.injective with bijective106have graph := Injective.function with function107have subset : A ⊆∅108intros a member109obtain⟨y, inZero, related⟩:=110 FunctionBetween.exists_value at f, A, 0, a with*111exfalso112rw nat.zero at inZero113exactset.empty with inZero114have reverse := empty.subset at A115exact Subset.antisymmetric with*116117theorem cardinality.eq_empty_of_zero (A : Set)118requires finite : Finite(A)119requires zero : cardinality(A) =0120shows A =∅121proof122have counted := cardinality.correct with finite123rw zero at counted124exact HasCardinality.empty_of_zero with counted125126-- A set of size k + 1 is a set of size k with one element put back.127theorem cardinality.exists_remove (F : Set) (k : Nat)128requires finite : Finite(F)129requires size : cardinality(F) = succ(k)130shows∃ e ∈ F, cardinality(F ∖ {e}) = k ∧ (F ∖ {e}) ∪ {e} = F131proof132have inhabited : ∃ e, e ∈ F133by_contra none134have empty : F =∅135ext y136constructor137 . intro hy138exfalso139exact none with⟨y, hy⟩140 . intro hy141exfalso142exactset.empty with hy143rw [empty, cardinality.empty] at size144exact Naturals.disjoint.zero.succ at k with size145obtain⟨e, he⟩:= inhabited146have removed := cardinality.remove with finite, he147rw size at removed148have restored : (F ∖ {e}) ∪ {e} = F149ext y150simp151refine⟨e, he, _, restored⟩152 symm153exact Naturals.injective.succ with removed154155-- Induction over the finite subsets of E, by the size of the subset: a156-- family S of sets containing ∅ and closed under adjoining a fresh point of157-- E to a finite member contains every finite subset of E.158theorem Finite.induction_of_size (E S : Set, n : Nat)159requires base : ∅∈ S160requires step : ∀ F ⊆ E, Finite(F) →∀ a ∈ E, a ∉ F → F ∈ S →161 F ∪ {a} ∈ S162shows∀ F ⊆ E, Finite(F) → cardinality(F) = n → F ∈ S163proof164induction n165| zero =>166intros F bound finite size167have empty : F =∅168ext y169constructor170 . intro hy171exfalso172have removed := cardinality.remove with finite, hy173rw size at removed174exact Naturals.disjoint.zero.succ at cardinality(F ∖ {y}) with removed175 . intro hy176exfalso177exactset.empty with hy178rw empty179exact base180| succ(k) using ih =>181intros F bound finite size182obtain⟨e, he, restSize, restored⟩:=183 cardinality.exists_remove with finite, size184have restSubset := difference.subset_left at F, {e}185have restBound := Subset.transitive with restSubset, bound186have restFinite := Finite.subset with finite, restSubset187have rest := ih with F ∖ {e}, restBound, restFinite, restSize188have fresh : e ∉ F ∖ {e} bysimp189have next := step with F ∖ {e}, restBound, restFinite, e,190 (bound with e, he), fresh, rest191rw restored at next192exact next193194theorem Finite.induction (E S : Set)195requires base : ∅∈ S196requires step : ∀ F ⊆ E, Finite(F) →∀ a ∈ E, a ∉ F → F ∈ S →197 F ∪ {a} ∈ S198shows∀ F ⊆ E, Finite(F) → F ∈ S199proof200intros F bound finite201have correct := cardinality.correct with finite202have natural := correct.left203have size : cardinality(F) = cardinality(F) byrfl204exact Finite.induction_of_size at E, S, cardinality(F)205with natural, base, step, F, bound, finite, size206