1-- Cardinality is monotone on finite sets, strictly so for proper subsets,2-- an image is no larger than its domain, and a function whose image is as3-- large as its finite domain is injective4-- 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.Pigeonhole10import NaturalSum1112theorem cardinality.nat (A : Set)13requires finite : Finite(A)14showsNat(cardinality(A))15proof16exact HasCardinality.natural with (cardinality.correct with finite)1718theorem cardinality.monotone (S T : Set)19requires finite : Finite(T)20requires subset : S ⊆ T21shows cardinality(S) ≤ cardinality(T)22proof23have finiteS := Finite.subset with finite, subset24have restSubset := difference.subset_left at T, S25have finiteRest := Finite.subset with finite, restSubset26have disjoint := Disjoint.symmetric with (difference.disjoint at T, S)27have restored : S ∪ (T ∖ S) = T28ext z29simp30have count := cardinality.union_disjoint with finiteS, finiteRest, disjoint31rw restored at count32have natS := cardinality.nat with finiteS33have natRest := cardinality.nat with finiteRest34rw count35arith3637theorem cardinality.strict (S T a : Set)38requires finite : Finite(T)39requires subset : S ⊆ T40requires member : a ∈ T41requires fresh : ¬a ∈ S42shows cardinality(S) < cardinality(T)43proof44have finiteS := Finite.subset with finite, subset45have inserted : insert(a, S) ⊆ T46intros z hz47havesplit:= insert.membership.forward with hz48casessplitwith same | inS49 . rw same50exact member51 . exact subset with z, inS52have count := cardinality.insert with finiteS, fresh53have mono := cardinality.monotone with finite, inserted54rw count at mono55have natS := cardinality.nat with finiteS56have natT := cardinality.nat with finite57arith5859theorem cardinality.eq_subset (S T : Set)60requires finite : Finite(T)61requires subset : S ⊆ T62requires same : cardinality(S) = cardinality(T)63shows S = T64proof65apply Subset.antisymmetric with subset66intros z hz67by_contra outside68have strict := cardinality.strict with finite, subset, hz, outside69have natS := cardinality.nat with (Finite.subset with finite, subset)70have natT := cardinality.nat with finite71rw same at strict72arith7374theorem cardinality.insert_le (A a : Set)75requires finite : Finite(A)76shows cardinality(insert(a, A)) ≤ succ(cardinality(A))77proof78have natA := cardinality.nat with finite79have alternatives : a ∈ A ∨¬a ∈ A bytauto80cases alternatives with inside | fresh81 . have same : insert(a, A) = A82ext z83constructor84 . intro hz85havesplit:= insert.membership.forward with hz86casessplitwith equal | inA87 . rw equal88exact inside89 . exact inA90 . intro hz91apply insert.membership.backward92right93exact hz94rw same95arith96 . have count := cardinality.insert with finite, fresh97rw count98arith99100theorem cardinality.image_bounded (f A B : Set) (k : Nat)101requires function : f : A → B102shows∀ S, S ⊆ A → HasCardinality(S, k) → cardinality(functionImage(f, S, B)) ≤ k103proof104induction k105| zero =>106intros S subset counted107have empty := HasCardinality.empty_of_zero at S with counted108rw [empty, functionImage.empty, ← nat.zero]109have zero := cardinality.natural at0110rw zero111arith112| succ(j) using ih =>113intros S subset counted114obtain⟨x, hx⟩:= HasCardinality.exists_member with counted115have finite := Finite.from_cardinality with counted116have count := cardinality.eq_of_has with counted117have removed := cardinality.remove with finite, hx118have restSubset := difference.subset_left at S, {x}119have smallerFinite := Finite.subset with finite, restSubset120have smallerNat := cardinality.nat with smallerFinite121have smallerCount : cardinality(S ∖ {x}) = j122rw count at removed123have forward : cardinality(S ∖ {x}) ≤ j byarith124have backward : j ≤ cardinality(S ∖ {x}) byarith125exact LessEqual.antisymmetric with forward, backward126have smallerHas : HasCardinality(S ∖ {x}, j)127rw← smallerCount128exact cardinality.correct with smallerFinite129have smallerSubset := Subset.transitive with restSubset, subset130have bound := ih with smallerSubset, smallerHas131have xInA := subset with x, hx132have image := functionImage.insert at f, A, B, x, S ∖ {x} with function, xInA133have restored := insert.restore with hx134rw restored at image135rw image136have finiteImage := Finite.function_image with function, smallerFinite, smallerSubset137have step := cardinality.insert_le at functionImage(f, S ∖ {x}, B), f(x) with finiteImage138have natImage := cardinality.nat with finiteImage139arith140141theorem cardinality.image_le (f A B S : Set)142requires function : f : A → B143requires subset : S ⊆ A144requires finite : Finite(S)145shows cardinality(functionImage(f, S, B)) ≤ cardinality(S)146proof147have counted := cardinality.correct with finite148have natS := cardinality.nat with finite149have all := cardinality.image_bounded with function150exact all with subset, counted151152-- A function whose image is as large as its finite domain is injective.153theorem cardinality.injective_of_image (f A B S x y : Set)154requires function : f : A → B155requires subset : S ⊆ A156requires finite : Finite(S)157requires full : cardinality(functionImage(f, S, B)) = cardinality(S)158requires hx : x ∈ S159requires hy : y ∈ S160requires same : f(x) = f(y)161shows x = y162proof163by_contra different164have restSubset := difference.subset_left at S, {x}165have smallerFinite := Finite.subset with finite, restSubset166have smallerSubset := Subset.transitive with restSubset, subset167have yRest : y ∈ S ∖ {x}168apply difference.singleton_membership.backward169constructor170 . exact hy171 . intro equal172apply different173 symm174exact equal175have inclusion : functionImage(f, S, B) ⊆ functionImage(f, S ∖ {x}, B)176intros z hz177obtain⟨zIn, w, hw, shape⟩:= functionImage.membership.forward with hz178apply functionImage.membership.backward179constructor180 . exact zIn181 . have alternatives : w = x ∨ w ≠ x182tauto183cases alternatives with wx | wnx184 . witness y185constructor186 . exact yRest187 . rw [shape, wx, same]188rfl189 . refine⟨w, (difference.singleton_membership.backward with⟨hw, wnx⟩), shape⟩190have finiteImage := Finite.function_image with function, finite, subset191have finiteSmallImage := Finite.function_image with function, smallerFinite, smallerSubset192have mono := cardinality.monotone with finiteSmallImage, inclusion193have le := cardinality.image_le with function, smallerSubset, smallerFinite194have removed := cardinality.remove with finite, hx195have natImage := cardinality.nat with finiteImage196have natSmallImage := cardinality.nat with finiteSmallImage197have natSmaller := cardinality.nat with smallerFinite198have natS := cardinality.nat with finite199arith200