1-- Boolean algebras: complemented distributive bounded lattices2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Lattice.Boolean8import Distributive910predicate IsBooleanAlgebra (A r bot top : Set) :=11 BoundedDistributiveLattice(A, r, bot, top) ∧12 (∀ x ∈ A, ∃ y, Complementary(A, r, bot, top, x, y))1314theorem IsBooleanAlgebra.bounded_distributive (A r bot top : Set)15requires h : IsBooleanAlgebra(A, r, bot, top)16shows BoundedDistributiveLattice(A, r, bot, top)17proof18simp [IsBooleanAlgebra]1920theorem IsBooleanAlgebra.distributive (A r bot top : Set)21requires h : IsBooleanAlgebra(A, r, bot, top)22shows IsDistributiveLattice(A, r)23proof24exact BoundedDistributiveLattice.distributive with25 (IsBooleanAlgebra.bounded_distributive with h)2627theorem IsBooleanAlgebra.bounded (A r bot top : Set)28requires h : IsBooleanAlgebra(A, r, bot, top)29shows IsBoundedLattice(A, r, bot, top)30proof31exact BoundedDistributiveLattice.bounded with32 (IsBooleanAlgebra.bounded_distributive with h)3334theorem IsBooleanAlgebra.lattice (A r bot top : Set)35requires h : IsBooleanAlgebra(A, r, bot, top)36shows IsLattice(A, r)37proof38exact IsBoundedLattice.lattice with (IsBooleanAlgebra.bounded with h)3940theorem IsBooleanAlgebra.exists_complement (A r bot top x : Set)41requires h : IsBooleanAlgebra(A, r, bot, top)42requires hx : x ∈ A43shows∃ y, Complementary(A, r, bot, top, x, y)44proof45rcases h with⟨bounded, complemented⟩46exact complemented with x, hx4748-- The complement of an element is its unique complementary element.4950defunique latticeComplement (A r bot top x : Set) (y ∈ A) :=51 Complementary(A, r, bot, top, x, y)52requires boolean : IsBooleanAlgebra(A, r, bot, top)53requires hx : x ∈ A54proof55obtain⟨y, hy⟩:= IsBooleanAlgebra.exists_complement with boolean, hx56witness y57refine⟨Complementary.member with hy, hy⟩58uniqueness59intros y yA first z zA second60have bounded := IsBooleanAlgebra.bounded_distributive with boolean61exact Complementary.unique with bounded, hx, first, second6263theorem latticeComplement.member (A r bot top x : Set)64requires boolean : IsBooleanAlgebra(A, r, bot, top)65requires hx : x ∈ A66shows latticeComplement(A, r, bot, top, x) ∈ A67proof68have selected := latticeComplement.selected with boolean, hx69exact selected.left7071theorem latticeComplement.complementary (A r bot top x : Set)72requires boolean : IsBooleanAlgebra(A, r, bot, top)73requires hx : x ∈ A74shows Complementary(A, r, bot, top, x, latticeComplement(A, r, bot, top, x))75proof76have selected := latticeComplement.selected with boolean, hx77exact selected.right7879theorem latticeComplement.meet (A r bot top x : Set)80requires boolean : IsBooleanAlgebra(A, r, bot, top)81requires hx : x ∈ A82shows meet(A, r, x, latticeComplement(A, r, bot, top, x)) = bot83proof84exact Complementary.meet with (latticeComplement.complementary with boolean,85 hx)8687theorem latticeComplement.join (A r bot top x : Set)88requires boolean : IsBooleanAlgebra(A, r, bot, top)89requires hx : x ∈ A90shows join(A, r, x, latticeComplement(A, r, bot, top, x)) = top91proof92exact Complementary.join with (latticeComplement.complementary with boolean,93 hx)9495theorem latticeComplement.meet_left (A r bot top x : Set)96requires boolean : IsBooleanAlgebra(A, r, bot, top)97requires hx : x ∈ A98shows meet(A, r, latticeComplement(A, r, bot, top, x), x) = bot99proof100have lattice := IsBooleanAlgebra.lattice with boolean101have swap := meet.commutative with lattice,102 (latticeComplement.member with boolean, hx), hx103rw swap104exact latticeComplement.meet with boolean, hx105106theorem latticeComplement.join_left (A r bot top x : Set)107requires boolean : IsBooleanAlgebra(A, r, bot, top)108requires hx : x ∈ A109shows join(A, r, latticeComplement(A, r, bot, top, x), x) = top110proof111have lattice := IsBooleanAlgebra.lattice with boolean112have swap := join.commutative with lattice,113 (latticeComplement.member with boolean, hx), hx114rw swap115exact latticeComplement.join with boolean, hx116117theorem latticeComplement.of_complementary (A r bot top x y : Set)118requires boolean : IsBooleanAlgebra(A, r, bot, top)119requires hx : x ∈ A120requires h : Complementary(A, r, bot, top, x, y)121shows y = latticeComplement(A, r, bot, top, x)122proof123have bounded := IsBooleanAlgebra.bounded_distributive with boolean124have selected := latticeComplement.complementary with boolean, hx125exact Complementary.unique with bounded, hx, h, selected126127theorem latticeComplement.involutive (A r bot top x : Set)128requires boolean : IsBooleanAlgebra(A, r, bot, top)129requires hx : x ∈ A130shows131 latticeComplement(A, r, bot, top, latticeComplement(A, r, bot, top, x)) = x132proof133have bounded := IsBooleanAlgebra.bounded with boolean134have complementA := latticeComplement.member with boolean, hx135have selected := latticeComplement.complementary with boolean, hx136have symmetric := Complementary.symmetric with bounded, hx, selected137 symm138exact latticeComplement.of_complementary with boolean, complementA, symmetric139140theorem latticeComplement.bottom (A r bot top : Set)141requires boolean : IsBooleanAlgebra(A, r, bot, top)142shows latticeComplement(A, r, bot, top, bot) = top143proof144have bounded := IsBooleanAlgebra.bounded with boolean145have botA := IsBoundedLattice.bottom_member with bounded146 symm147exact latticeComplement.of_complementary with boolean, botA,148 (Complementary.bottom_top with bounded)149150theorem latticeComplement.top (A r bot top : Set)151requires boolean : IsBooleanAlgebra(A, r, bot, top)152shows latticeComplement(A, r, bot, top, top) = bot153proof154have bounded := IsBooleanAlgebra.bounded with boolean155have topA := IsBoundedLattice.top_member with bounded156 symm157exact latticeComplement.of_complementary with boolean, topA,158 (Complementary.top_bottom with bounded)159160theorem latticeComplement.injective (A r bot top x y : Set)161requires boolean : IsBooleanAlgebra(A, r, bot, top)162requires hx : x ∈ A163requires hy : y ∈ A164requires same :165 latticeComplement(A, r, bot, top, x) = latticeComplement(A, r, bot, top, y)166shows x = y167proof168have twiceX := latticeComplement.involutive with boolean, hx169have twiceY := latticeComplement.involutive with boolean, hy170rw← twiceX171rw← twiceY172rw same173rfl174175-- De Morgan's laws176177theorem latticeComplement.join_eq (A r bot top x y : Set)178requires boolean : IsBooleanAlgebra(A, r, bot, top)179requires hx : x ∈ A180requires hy : y ∈ A181shows latticeComplement(A, r, bot, top, join(A, r, x, y)) =182 meet(A, r, latticeComplement(A, r, bot, top, x), latticeComplement(A, r, bot, top, y))183proof184have distributive := IsBooleanAlgebra.distributive with boolean185have bounded := IsBooleanAlgebra.bounded with boolean186have lattice := IsBooleanAlgebra.lattice with boolean187have xA := latticeComplement.member with boolean, hx188have yA := latticeComplement.member with boolean, hy189have joinA := join.member with lattice, hx, hy190have meetA := meet.member with lattice, xA, yA191have meetBottom : meet(A, r, join(A, r, x, y), meet(A, r,192 latticeComplement(A, r, bot, top, x), latticeComplement(A, r, bot, top,193 y))) = bot194have expand := IsDistributiveLattice.meet_join_right with distributive, hx,195 hy, meetA196have regroupX := meet.associative with lattice, hx, xA, yA197have swapInner := meet.commutative with lattice, xA, yA198have regroupY := meet.associative with lattice, hy, yA, xA199have cancelX := latticeComplement.meet with boolean, hx200have cancelY := latticeComplement.meet with boolean, hy201have absorbX := meet.bottom_left with bounded, yA202have absorbY := meet.bottom_left with bounded, xA203have botA := IsBoundedLattice.bottom_member with bounded204rw [expand, ← regroupX, cancelX, absorbX, swapInner, ← regroupY, cancelY,205 absorbY]206exact join.idempotent with lattice, botA207have joinTop : join(A, r, join(A, r, x, y), meet(A, r, latticeComplement(A,208 r, bot, top, x), latticeComplement(A, r, bot, top, y))) = top209have expand := IsDistributiveLattice.join_meet with distributive, joinA, xA,210 yA211have swapOuter := join.commutative with lattice, hx, hy212have regroupX := join.associative with lattice, hy, hx, xA213have cancelX := latticeComplement.join with boolean, hx214have absorbX := join.top_right with bounded, hy215have regroupY := join.associative with lattice, hx, hy, yA216have cancelY := latticeComplement.join with boolean, hy217have absorbY := join.top_right with bounded, hx218have topA := IsBoundedLattice.top_member with bounded219rw expand220rw [regroupY, cancelY, absorbY]221rw [swapOuter, regroupX, cancelX, absorbX]222exact meet.idempotent with lattice, topA223have complementary : Complementary(A, r, bot, top, join(A, r, x, y), meet(A,224 r, latticeComplement(A, r, bot, top, x), latticeComplement(A, r, bot,225 top, y)))226refine⟨meetA, meetBottom, joinTop⟩227 symm228exact latticeComplement.of_complementary with boolean, joinA, complementary229230theorem latticeComplement.meet_eq (A r bot top x y : Set)231requires boolean : IsBooleanAlgebra(A, r, bot, top)232requires hx : x ∈ A233requires hy : y ∈ A234shows latticeComplement(A, r, bot, top, meet(A, r, x, y)) =235 join(A, r, latticeComplement(A, r, bot, top, x), latticeComplement(A, r, bot, top, y))236proof237have lattice := IsBooleanAlgebra.lattice with boolean238have xA := latticeComplement.member with boolean, hx239have yA := latticeComplement.member with boolean, hy240have joinA := join.member with lattice, xA, yA241have deMorgan := latticeComplement.join_eq with boolean, xA, yA242have twiceX := latticeComplement.involutive with boolean, hx243have twiceY := latticeComplement.involutive with boolean, hy244rw [twiceX, twiceY] at deMorgan245rw← deMorgan246exact latticeComplement.involutive with boolean, joinA247248-- Order and complements249250theorem latticeComplement.le_iff_meet_bottom (A r bot top a b : Set)251requires boolean : IsBooleanAlgebra(A, r, bot, top)252requires ha : a ∈ A253requires hb : b ∈ A254shows255 relates(r, a, latticeComplement(A, r, bot, top, b)) ↔ meet(A, r, a, b) = bot256proof257have distributive := IsBooleanAlgebra.distributive with boolean258have bounded := IsBooleanAlgebra.bounded with boolean259have lattice := IsBooleanAlgebra.lattice with boolean260have reflexive := IsLattice.reflexive with lattice261have bA := latticeComplement.member with boolean, hb262have abA := meet.member with lattice, ha, hb263constructor264 . intro below265have bounded2 := meet.monotone with lattice, ha, hb, bA, hb, below,266 (reflexive with b, hb)267have cancel := latticeComplement.meet_left with boolean, hb268rw cancel at bounded2269exact IsBoundedLattice.le_bottom_iff.forward with bounded, abA, bounded2270 . intro disjoint271have unit := meet.top_right with bounded, ha272have cover := latticeComplement.join with boolean, hb273have expand := IsDistributiveLattice.meet_join with distributive, ha, hb, bA274have abBotA := meet.member with lattice, ha, bA275have absorb := join.bottom_left with bounded, abBotA276 symm at absorb277 symm at expand278have equation : meet(A, r, a, latticeComplement(A, r, bot, top, b)) = a279calc280 meet(A, r, a, latticeComplement(A, r, bot, top, b)) = join(A, r, bot, meet(A, r, a, latticeComplement(A, r, bot, top, b))) :=281 absorb282 _ = join(A, r, meet(A, r, a, b), meet(A, r, a, latticeComplement(A, r, bot, top, b))) :=283bysimp [disjoint]284 _ = meet(A, r, a, join(A, r, b, latticeComplement(A, r, bot, top, b))) := expand285 _ = meet(A, r, a, top) :=bysimp [cover]286 _ = a := unit287exact meet.eq_left_iff.backward with lattice, ha, bA, equation288289theorem latticeComplement.antitone (A r bot top x y : Set)290requires boolean : IsBooleanAlgebra(A, r, bot, top)291requires hx : x ∈ A292requires hy : y ∈ A293requires hxy : relates(r, x, y)294shows295 relates(r, latticeComplement(A, r, bot, top, y), latticeComplement(A, r, bot, top, x))296proof297have bounded := IsBooleanAlgebra.bounded with boolean298have lattice := IsBooleanAlgebra.lattice with boolean299have reflexive := IsLattice.reflexive with lattice300have yA := latticeComplement.member with boolean, hy301have yxA := meet.member with lattice, yA, hx302have below := meet.monotone with lattice, yA, hx, yA, hy,303 (reflexive with latticeComplement(A, r, bot, top, y), yA), hxy304have cancel := latticeComplement.meet_left with boolean, hy305rw cancel at below306have disjoint := IsBoundedLattice.le_bottom_iff.forward with bounded, yxA,307 below308exact latticeComplement.le_iff_meet_bottom.backward with boolean, yA, hx,309 disjoint310311theorem latticeComplement.le_iff (A r bot top x y : Set)312requires boolean : IsBooleanAlgebra(A, r, bot, top)313requires hx : x ∈ A314requires hy : y ∈ A315shows relates(r, x, y) ↔316 relates(r, latticeComplement(A, r, bot, top, y), latticeComplement(A, r, bot, top, x))317proof318constructor319 . intro hxy320exact latticeComplement.antitone with boolean, hx, hy, hxy321 . intro reversed322have xA := latticeComplement.member with boolean, hx323have yA := latticeComplement.member with boolean, hy324have twice := latticeComplement.antitone with boolean, yA, xA, reversed325have twiceX := latticeComplement.involutive with boolean, hx326have twiceY := latticeComplement.involutive with boolean, hy327rw [twiceX, twiceY] at twice328exact twice329330theorem IsBooleanAlgebra.converse (A r bot top : Set)331requires boolean : IsBooleanAlgebra(A, r, bot, top)332shows IsBooleanAlgebra(A, converse(r, A, A), top, bot)333proof334have bounded := IsBooleanAlgebra.bounded with boolean335have distributive := IsBooleanAlgebra.bounded_distributive with boolean336constructor337 . exact BoundedDistributiveLattice.converse with distributive338 . intros x hx339have selected := latticeComplement.complementary with boolean, hx340refine⟨latticeComplement(A, r, bot, top, x),341 (Complementary.converse with bounded, hx, selected)⟩342343-- The powerset of X is a Boolean algebra whose complements are relative344-- complements in X.345346theorem powersetOrder.complementary (X B : Set)347requires hB : B ∈𝒫(X)348shows Complementary(𝒫(X), powersetOrder(X), ∅, X, B, X ∖ B)349proof350have BX := powerset.membership_iff.forward with hB351have differenceIn : X ∖ B ∈𝒫(X) :=352 powerset.membership_iff.backward with (difference.subset_left at X, B)353have meetValue := powersetOrder.meet with hB, differenceIn354have joinValue := powersetOrder.join with hB, differenceIn355refine⟨differenceIn, ?_, ?_⟩356 . rw meetValue357have swap := intersection.commutative at B, X ∖ B358rw swap359exact difference.intersection_eq at X, B360 . rw joinValue361exact difference.union2_of_subset with BX362363theorem powersetOrder.boolean (X : Set)364shows IsBooleanAlgebra(𝒫(X), powersetOrder(X), ∅, X)365proof366constructor367 . exact powersetOrder.bounded_distributive at X368 . intros B hB369refine⟨X ∖ B, powersetOrder.complementary with hB⟩370371theorem powersetOrder.complement_value (X B : Set)372requires hB : B ∈𝒫(X)373shows latticeComplement(𝒫(X), powersetOrder(X), ∅, X, B) = X ∖ B374proof375have boolean := powersetOrder.boolean at X376 symm377exact latticeComplement.of_complementary with boolean, hB,378 (powersetOrder.complementary with hB)379