1-- Inclusion–exclusion for two finite sets: the sizes of the union and the2-- intersection add up to the sizes of the two sets.3-- Part of Cumulus, the Grace17 mathematics library.4-- Copyright (c) 2026 Grant Passmore.5-- Released under the Apache License 2.0. See LICENSE in the repository root.6-- Authors: Grant Passmore78module Grace.Cumulus.Cardinal.InclusionExclusion9import NaturalSum10import Pigeonhole1112theorem cardinality.inclusion_exclusion (A B : Set)13requires finiteA : Finite(A)14requires finiteB : Finite(B)15shows cardinality(A ∪ B) + cardinality(A ∩ B) =16 cardinality(A) + cardinality(B)17proof18have restSubset := difference.subset_left at B, A with*19have finiteRest := Finite.subset with finiteB, restSubset20have meetSubset := intersection.subset_left at A, B with*21have finiteMeet := Finite.subset with finiteA, meetSubset22have splitUnion := difference.union2_self at A, B with*23have disjointRest : Disjoint(A, B ∖ A)24unfold Disjoint25ext x26simp27have unionCount := cardinality.union_disjoint with28 finiteA, finiteRest, disjointRest29rw splitUnion at unionCount30have splitB : (A ∩ B) ∪ (B ∖ A) = B31ext x32simp33have disjointMeet : Disjoint(A ∩ B, B ∖ A)34unfold Disjoint35ext x36simp37have meetCount := cardinality.union_disjoint with38 finiteMeet, finiteRest, disjointMeet39rw splitB at meetCount40have natA := cardinality.nat with finiteA41have natMeet := cardinality.nat with finiteMeet42have natRest := cardinality.nat with finiteRest43have regroup := add.associative at cardinality(B ∖ A), cardinality(A ∩ B),44 cardinality(A) with natA45have swap := add.commutative at cardinality(A ∩ B), cardinality(B ∖ A)46with natMeet, natRest47rw [unionCount, meetCount, regroup, swap]48rfl49