1-- The Boolean algebra of sets: union, intersection, difference, and2-- symmetric difference under inclusion3-- 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.Set.Algebra9import Core1011def symmetricDifference (A B : Set) : Set:= (A ∖ B) ∪ (B ∖ A)12notationinfixl60"△"=> symmetricDifference1314theorem symmetricDifference.membership (x A B : Set)15shows x ∈ A △ B ↔¬(x ∈ A ↔ x ∈ B)16proof17simp [symmetricDifference]1819-- Inclusion2021theorem Subset.of_eq (A B : Set)22requires h : A = B23shows A ⊆ B24proof25rw h26simp2728theorem Subset.empty_iff (A : Set)29shows A ⊆∅↔ A =∅30proof31constructor32 . intro h33ext y34simp35 . intro h36rw h37simp3839theorem Subset.union2_iff (A B C : Set)40shows A ∪ B ⊆ C ↔ A ⊆ C ∧ B ⊆ C41proof42constructor43 . intro h44constructor45 . simp46 . simp47 . intro h48simp4950theorem Subset.intersection_iff (A B C : Set)51shows A ⊆ B ∩ C ↔ A ⊆ B ∧ A ⊆ C52proof53constructor54 . intro h55constructor56 . simp57 . simp58 . intro h59simp6061-- Union6263theorem union2.left_empty (A : Set)64shows∅∪ A = A65proof66ext y67simp6869theorem union2.right_empty (A : Set)70shows A ∪∅= A71proof72ext y73simp7475theorem union2.monotone (A B C D : Set)76requires hAC : A ⊆ C77requires hBD : B ⊆ D78shows A ∪ B ⊆ C ∪ D79proof80simp8182theorem union2.monotone_left (A B C : Set)83requires h : A ⊆ B84shows A ∪ C ⊆ B ∪ C85proof86simp8788theorem union2.monotone_right (A B C : Set)89requires h : A ⊆ B90shows C ∪ A ⊆ C ∪ B91proof92simp9394theorem union2.eq_right_of_subset (A B : Set)95requires h : A ⊆ B96shows A ∪ B = B97proof98ext y99simp100101theorem union2.eq_left_of_subset (A B : Set)102requires h : B ⊆ A103shows A ∪ B = A104proof105ext y106simp107108theorem union2.subset_iff_eq (A B : Set)109shows A ⊆ B ↔ A ∪ B = B110proof111constructor112 . intro h113ext y114simp115 . intros h x hx116have member : x ∈ A ∪ B bysimp [hx]117 rwa h at member118119theorem union2.left_commutative (A B C : Set)120shows A ∪ (B ∪ C) = B ∪ (A ∪ C)121proof122ext y123simp124125theorem union2.subset_of_subset_left (A B C : Set)126requires h : A ⊆ B127shows A ⊆ B ∪ C128proof129simp130131theorem union2.subset_of_subset_right (A B C : Set)132requires h : A ⊆ C133shows A ⊆ B ∪ C134proof135simp136137theorem union2.eq_empty_iff (A B : Set)138shows A ∪ B =∅↔ A =∅∧ B =∅139proof140constructor141 . intro h142constructor143 . ext y144constructor145 . intro hy146have member : y ∈ A ∪ B bysimp [hy]147 rwa h at member148 . intro hy149exact empty.subset at A with y, hy150 . ext y151constructor152 . intro hy153have member : y ∈ A ∪ B bysimp [hy]154 rwa h at member155 . intro hy156exact empty.subset at B with y, hy157 . intro h158rw [h.left, h.right]159exact union2.left_empty160161-- Intersection162163theorem intersection.left_empty (A : Set)164shows∅∩ A =∅165proof166ext y167simp168169theorem intersection.right_empty (A : Set)170shows A ∩∅=∅171proof172ext y173simp174175theorem intersection.associative (A B C : Set)176shows (A ∩ B) ∩ C = A ∩ (B ∩ C)177proof178ext y179simp180181theorem intersection.left_commutative (A B C : Set)182shows A ∩ (B ∩ C) = B ∩ (A ∩ C)183proof184ext y185simp186187theorem intersection.monotone (A B C D : Set)188requires hAC : A ⊆ C189requires hBD : B ⊆ D190shows A ∩ B ⊆ C ∩ D191proof192simp193194theorem intersection.monotone_left (A B C : Set)195requires h : A ⊆ B196shows A ∩ C ⊆ B ∩ C197proof198simp199200theorem intersection.monotone_right (A B C : Set)201requires h : A ⊆ B202shows C ∩ A ⊆ C ∩ B203proof204simp205206theorem intersection.eq_left_of_subset (A B : Set)207requires h : A ⊆ B208shows A ∩ B = A209proof210ext y211simp212213theorem intersection.eq_right_of_subset (A B : Set)214requires h : B ⊆ A215shows A ∩ B = B216proof217ext y218simp219220theorem intersection.subset_iff_eq (A B : Set)221shows A ⊆ B ↔ A ∩ B = A222proof223constructor224 . intro h225ext y226simp227 . intros h x hx228rw← h at hx229exact intersection.subset_right at A, B with x, hx230231theorem intersection.subset_union2 (A B : Set)232shows A ∩ B ⊆ A ∪ B233proof234simp235236-- Distributivity and absorption237238theorem intersection.union2_distrib (A B C : Set)239shows A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)240proof241ext y242simp243244theorem union2.intersection_distrib (A B C : Set)245shows A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)246proof247ext y248simp249250theorem intersection.union2_distrib_right (A B C : Set)251shows (A ∪ B) ∩ C = (A ∩ C) ∪ (B ∩ C)252proof253ext y254simp255256theorem union2.intersection_distrib_right (A B C : Set)257shows (A ∩ B) ∪ C = (A ∪ C) ∩ (B ∪ C)258proof259ext y260simp261262theorem union2.absorb (A B : Set)263shows A ∪ (A ∩ B) = A264proof265ext y266simp267268theorem intersection.absorb (A B : Set)269shows A ∩ (A ∪ B) = A270proof271ext y272simp273274-- Difference275276theorem difference.self (A : Set)277shows A ∖ A =∅278proof279ext y280simp281282theorem difference.empty (A : Set)283shows A ∖∅= A284proof285ext y286simp287288theorem difference.left_empty (A : Set)289shows∅∖ A =∅290proof291ext y292simp293294theorem difference.eq_empty_iff (A B : Set)295shows A ∖ B =∅↔ A ⊆ B296proof297constructor298 . intros h x hx299by_contra absent300have member : x ∈ A ∖ B bysimp [hx, absent]301rw h at member302 simpa using member303 . intro h304ext y305simp306307theorem difference.eq_self_iff_disjoint (A B : Set)308shows A ∖ B = A ↔ A ∩ B =∅309proof310constructor311 . intro h312ext y313constructor314 . intro hy315simp [intersection.membership] at hy316have inA := hy.left317rw← h at inA318simp [difference.membership] at inA319exfalso320exact inA.right with hy.right321 . intro hy322exact empty.subset at A ∩ B with y, hy323 . intro h324ext y325constructor326 . intro hy327simp [difference.membership] at hy328exact hy.left329 . intro hy330simp [difference.membership]331constructor332 . exact hy333 . intro hB334have member : y ∈ A ∩ B bysimp [hy, hB]335rw h at member336 simpa using member337338theorem difference.subset_of_subset (A B C : Set)339requires h : A ⊆ B340shows A ∖ C ⊆ B ∖ C341proof342simp343344theorem difference.antitone (A B C : Set)345requires h : B ⊆ C346shows A ∖ C ⊆ A ∖ B347proof348simp349350theorem difference.monotone (A B C D : Set)351requires hAC : A ⊆ C352requires hDB : D ⊆ B353shows A ∖ B ⊆ C ∖ D354proof355simp356357theorem difference.union2_of_subset (A B : Set)358requires h : A ⊆ B359shows A ∪ (B ∖ A) = B360proof361ext y362simp363364theorem difference.union2_self (A B : Set)365shows A ∪ (B ∖ A) = A ∪ B366proof367ext y368simp369370theorem difference.difference (A B : Set)371shows A ∖ (A ∖ B) = A ∩ B372proof373ext y374simp375376theorem difference.difference_right (A B C : Set)377shows (A ∖ B) ∖ C = A ∖ (B ∪ C)378proof379ext y380simp381382theorem difference.union2 (A B C : Set)383shows A ∖ (B ∪ C) = (A ∖ B) ∩ (A ∖ C)384proof385ext y386simp387388theorem difference.intersection (A B C : Set)389shows A ∖ (B ∩ C) = (A ∖ B) ∪ (A ∖ C)390proof391ext y392simp393394theorem difference.union2_left (A B C : Set)395shows (A ∪ B) ∖ C = (A ∖ C) ∪ (B ∖ C)396proof397ext y398simp399400theorem difference.intersection_left (A B C : Set)401shows (A ∩ B) ∖ C = (A ∖ C) ∩ (B ∖ C)402proof403ext y404simp405406theorem difference.intersection_right (A B C : Set)407shows A ∖ (B ∖ C) = (A ∖ B) ∪ (A ∩ C)408proof409ext y410simp411412theorem difference.of_intersection (A B : Set)413shows A ∖ (A ∩ B) = A ∖ B414proof415ext y416simp417418theorem difference.intersection_eq (A B : Set)419shows (A ∖ B) ∩ B =∅420proof421ext y422simp423424theorem difference.disjoint (A B : Set)425shows Disjoint(A ∖ B, B)426proof427simp [Disjoint, difference.intersection_eq]428429theorem difference.union2_intersection (A B : Set)430shows (A ∖ B) ∪ (A ∩ B) = A431proof432ext y433simp434435theorem difference.of_union2 (A B : Set)436shows (A ∪ B) ∖ B = A ∖ B437proof438ext y439simp440441theorem difference.subset_iff (A B C : Set)442shows A ∖ B ⊆ C ↔ A ⊆ B ∪ C443proof444constructor445 . intro h446simp447 . intros h x hx448simp [difference.membership] at hx449have inUnion : x ∈ B ∪ C := h with x, hx.left450simp [union2.membership] at inUnion451cases inUnion with inB | inC452 . exfalso453exact hx.right with inB454 . exact inC455456-- Disjointness457458theorem Disjoint.iff (A B : Set)459shows Disjoint(A, B) ↔∀ x ∈ A, ¬(x ∈ B)460proof461constructor462 . intros h x hx hB463have member : x ∈ A ∩ B bysimp [hx, hB]464simp [Disjoint] at h465rw h at member466 simpa using member467 . intro h468simp [Disjoint]469ext y470constructor471 . intro hy472simp [intersection.membership] at hy473exfalso474exact h with y, hy.left, hy.right475 . intro hy476exact empty.subset at A ∩ B with y, hy477478theorem Disjoint.empty_left (A : Set)479shows Disjoint(∅, A)480proof481simp [Disjoint, intersection.left_empty]482483theorem Disjoint.empty_right (A : Set)484shows Disjoint(A, ∅)485proof486simp [Disjoint, intersection.right_empty]487488theorem Disjoint.of_subset_left (A B C : Set)489requires h : Disjoint(B, C)490requires hAB : A ⊆ B491shows Disjoint(A, C)492proof493have absent := Disjoint.iff.forward with h494apply Disjoint.iff.backward495intros x hx496exact absent with x, (hAB with x, hx)497498theorem Disjoint.of_subset_right (A B C : Set)499requires h : Disjoint(A, C)500requires hBC : B ⊆ C501shows Disjoint(A, B)502proof503have absent := Disjoint.iff.forward with h504apply Disjoint.iff.backward505intros x hx hB506exact absent with x, hx, (hBC with x, hB)507508theorem Disjoint.union2_iff (A B C : Set)509shows Disjoint(A ∪ B, C) ↔ Disjoint(A, C) ∧ Disjoint(B, C)510proof511constructor512 . intro h513have absent := Disjoint.iff.forward with h514constructor515 . apply Disjoint.iff.backward516intros x hx517exact absent with x, (union2.subset_left at A, B with x, hx)518 . apply Disjoint.iff.backward519intros x hx520exact absent with x, (union2.subset_right at A, B with x, hx)521 . intro h522have absentA := Disjoint.iff.forward with h.left523have absentB := Disjoint.iff.forward with h.right524apply Disjoint.iff.backward525intros x hx526simp [union2.membership] at hx527cases hx with inA | inB528 . exact absentA with x, inA529 . exact absentB with x, inB530531theorem Disjoint.difference_left (A B : Set)532shows Disjoint(A ∖ B, B)533proof534exact difference.disjoint535536theorem Disjoint.difference_right (A B : Set)537shows Disjoint(B, A ∖ B)538proof539apply Disjoint.symmetric540exact difference.disjoint541542theorem Disjoint.subset_difference (A B : Set)543requires h : Disjoint(A, B)544shows A ⊆ A ∖ B545proof546have absent := Disjoint.iff.forward with h547intros x hx548simp [difference.membership]549550-- Symmetric difference551552theorem symmetricDifference.commutative (A B : Set)553shows A △ B = B △ A554proof555ext y556simp [symmetricDifference]557558theorem symmetricDifference.self (A : Set)559shows A △ A =∅560proof561ext y562simp [symmetricDifference]563564theorem symmetricDifference.empty (A : Set)565shows A △ ∅= A566proof567ext y568simp [symmetricDifference]569570theorem symmetricDifference.left_empty (A : Set)571shows∅ △ A = A572proof573ext y574simp [symmetricDifference]575576theorem symmetricDifference.eq_difference (A B : Set)577shows A △ B = (A ∪ B) ∖ (A ∩ B)578proof579ext y580simp [symmetricDifference]581582theorem symmetricDifference.associative (A B C : Set)583shows (A △ B) △ C = A △ (B △ C)584proof585ext y586simp [symmetricDifference]587588theorem symmetricDifference.union2 (A B : Set)589shows A ∪ B = (A △ B) ∪ (A ∩ B)590proof591ext y592simp [symmetricDifference]593594theorem symmetricDifference.intersection_distrib (A B C : Set)595shows A ∩ (B △ C) = (A ∩ B) △ (A ∩ C)596proof597ext y598simp [symmetricDifference]599600-- Intersection distributes over △ on the right as well.601theorem symmetricDifference.intersection_distrib_right (A B C : Set)602shows (A △ B) ∩ C = (A ∩ C) △ (B ∩ C)603proof604ext y605simp [symmetricDifference]606607theorem symmetricDifference.eq_empty_iff (A B : Set)608shows A △ B =∅↔ A = B609proof610constructor611 . intro h612ext y613by_contra different614have member : y ∈ A △ B bysimp [symmetricDifference.membership]615rw h at member616 simpa using member617 . intro h618rw h619exact symmetricDifference.self620621theorem symmetricDifference.subset (A B C : Set)622requires first : A ⊆ C623requires second : B ⊆ C624shows A △ B ⊆ C625proof626haveleft : A ∖ B ⊆ C :=627 Subset.transitive with (difference.subset_left at A, B), first628haveright : B ∖ A ⊆ C :=629 Subset.transitive with (difference.subset_left at B, A), second630unfold symmetricDifference631exact union2.subset withleft, right632