1-- Boolean algebras bundled as ordered sets, with the complement compl(P)2-- 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.BooleanAlgebra8import Boolean9import Linear1011-- A Boolean algebra is a bounded distributive lattice in which every12-- element has a complement; the complement is read back as compl(P)(a).13-- Only the bounded-lattice projection is an instance, so that Lattice(P)14-- has one derivation from a Boolean hypothesis.15predicate BooleanAlgebra (P : Set) :=16 DistributiveLattice(P) ∧ BoundedLattice(P) ∧17 (∀ a ∈ points(P), ∃ b, IsCompl(P, a, b))1819notationinfixl68"⊔"=> call2(sup(P), _, _) viahypothesis BooleanAlgebra(P) for P.points20notationinfixl69"⊓"=> call2(inf(P), _, _) viahypothesis BooleanAlgebra(P) for P.points21notationinfix50"≤"=> Le(P, _, _) viahypothesis BooleanAlgebra(P) for P.points2223theorem BooleanAlgebra.distributive_of (P : Set)24requires boolean : BooleanAlgebra(P)25shows DistributiveLattice(P)26proof27obtain⟨distributive, bounded, complemented⟩:= boolean28exact distributive2930instance BooleanAlgebra.bounded_of (P : Set)31requires boolean : BooleanAlgebra(P)32shows BoundedLattice(P)33proof34obtain⟨distributive, bounded, complemented⟩:= boolean35exact bounded3637theorem BooleanAlgebra.lattice_of (P : Set)38requires boolean : BooleanAlgebra(P)39shows Lattice(P)40proof41exact BoundedLattice.lattice_of with (BooleanAlgebra.bounded_of with boolean)4243theorem BooleanAlgebra.exists_isCompl (P a : Set)44requires boolean : BooleanAlgebra(P)45requires aIn : a ∈ points(P)46shows∃ b, IsCompl(P, a, b)47proof48obtain⟨distributive, bounded, complemented⟩:= boolean49exact complemented with a, aIn5051-- Bridges to the unbundled form IsBooleanAlgebra(A, r, bot, top).52instance BooleanAlgebra.unbundled (P : Set)53requires boolean : BooleanAlgebra(P)54shows IsBooleanAlgebra(points(P), le(P), bot(P), top(P))55proof56have distributive := BooleanAlgebra.distributive_of with boolean57have bounded := BooleanAlgebra.bounded_of with boolean58have both := BoundedLattice.bounded_distributive with distributive, bounded59constructor60 . exact both61 . intros a aIn62obtain⟨b, compl⟩:= BooleanAlgebra.exists_isCompl with boolean, aIn63witness b64exact IsCompl.unbundled with bounded, compl6566theorem BooleanAlgebra.mk (A r b t : Set)67requires boolean : IsBooleanAlgebra(A, r, b, t)68shows BooleanAlgebra(Order.mk(A, r))69proof70have boundedU := IsBooleanAlgebra.bounded with boolean71have bounded := BoundedLattice.mk with boundedU72have distributive := DistributiveLattice.mk with73 (IsBooleanAlgebra.distributive with boolean)74have botEq := bot.mk with boundedU75have topEq := top.mk with boundedU76refine⟨distributive, bounded, ?_⟩77intros a aIn78rw Order.mk.points at aIn79obtain⟨c, compl⟩:= IsBooleanAlgebra.exists_complement with boolean, aIn80witness c81have aIn2 : a ∈ points(Order.mk(A, r))82rw Order.mk.points83exact aIn84apply IsCompl.of_complementary with bounded, aIn285rw [Order.mk.points, Order.mk.le, botEq, topEq]86exact compl8788defgraph compl (P : Set) (a ∈ points(P)) : points(P) :=89 latticeComplement(points(P), le(P), bot(P), top(P), a)90requires boolean : BooleanAlgebra(P)91proof92exact latticeComplement.member with93 (BooleanAlgebra.unbundled with boolean), member9495-- The complement is written aᶜ, and the bounds ⊥ and ⊤, wherever a96-- hypothesis in scope supplies the Boolean algebra.97notationpostfix90"ᶜ"=>apply(compl(P), _) viahypothesis BooleanAlgebra(P) for P.points98notation constant "⊥"=> bot(P) viahypothesis BooleanAlgebra(P) for P.points99notation constant "⊤"=> top(P) viahypothesis BooleanAlgebra(P) for P.points100101section BooleanLaws102variable (P : Set)103hypothesis boolean : BooleanAlgebra(P)104105-- The bundled complement is the unbundled one.106theorem compl.complement (a ∈ points(P))107shows compl(P)(a) = latticeComplement(points(P), le(P), bot(P), top(P), a)108proof109exact compl.value with boolean, assumption110111instance compl.member (a ∈ points(P))112shows compl(P)(a) ∈ points(P)113proof114rw compl.complement115exact latticeComplement.member with116 (BooleanAlgebra.unbundled with boolean), assumption117118theorem compl.isCompl (a ∈ points(P))119shows IsCompl(P, a, compl(P)(a))120proof121have complementary := latticeComplement.complementary with122 (BooleanAlgebra.unbundled with boolean), assumption123rw compl.complement124exact IsCompl.of_complementary with125 (BooleanAlgebra.bounded_of with boolean), assumption126127theorem inf_compl_eq_bot (a ∈ points(P))128shows a ⊓ aᶜ =⊥129proof130rw [inf.meet, compl.complement]131exact latticeComplement.meet with132 (BooleanAlgebra.unbundled with boolean), assumption133134theorem sup_compl_eq_top (a ∈ points(P))135shows a ⊔ compl(P)(a) = top(P)136proof137rw [sup.join, compl.complement]138exact latticeComplement.join with139 (BooleanAlgebra.unbundled with boolean), assumption140141theorem compl_inf_eq_bot (a ∈ points(P))142shows compl(P)(a) ⊓ a = bot(P)143proof144rw [inf.meet, compl.complement]145exact latticeComplement.meet_left with146 (BooleanAlgebra.unbundled with boolean), assumption147148theorem compl_sup_eq_top (a ∈ points(P))149shows compl(P)(a) ⊔ a = top(P)150proof151rw [sup.join, compl.complement]152exact latticeComplement.join_left with153 (BooleanAlgebra.unbundled with boolean), assumption154155theorem IsCompl.compl_eq (b : Set) (a ∈ points(P))156requires compl : IsCompl(P, a, b)157shows b = compl(P)(a)158proof159have complementary := IsCompl.unbundled with160 (BooleanAlgebra.bounded_of with boolean), compl161rw compl.complement162exact latticeComplement.of_complementary with163 (BooleanAlgebra.unbundled with boolean), assumption, complementary164165theorem compl_compl (a ∈ points(P))166shows (aᶜ)ᶜ = a167proof168have complIn : compl(P)(a) ∈ points(P) := compl.member with*169rw [compl.complement, compl.complement]170exact latticeComplement.involutive with171 (BooleanAlgebra.unbundled with boolean), assumption172173theorem compl_bot174shows compl(P)(bot(P)) = top(P)175proof176have botIn : bot(P) ∈ points(P) := bot.member with*177rw compl.complement178exact latticeComplement.bottom with (BooleanAlgebra.unbundled with boolean)179180theorem compl_top181shows compl(P)(top(P)) = bot(P)182proof183have topIn : top(P) ∈ points(P) := top.member with*184rw compl.complement185exact latticeComplement.top with (BooleanAlgebra.unbundled with boolean)186187theorem compl_injective (a b ∈ points(P))188requires same : compl(P)(a) = compl(P)(b)189shows a = b190proof191rw [compl.complement, compl.complement] at same192exact latticeComplement.injective with193 (BooleanAlgebra.unbundled with boolean), *194195theorem compl_sup (a b ∈ points(P))196shows compl(P)(a ⊔ b) = compl(P)(a) ⊓ compl(P)(b)197proof198have supIn : a ⊔ b ∈ points(P) := sup.member with*199have complA : compl(P)(a) ∈ points(P) := compl.member with*200have complB : compl(P)(b) ∈ points(P) := compl.member with*201simp [sup.join, inf.meet, compl.complement]202exact latticeComplement.join_eq with203 (BooleanAlgebra.unbundled with boolean), *204205theorem compl_inf (a b ∈ points(P))206shows compl(P)(a ⊓ b) = compl(P)(a) ⊔ compl(P)(b)207proof208have infIn : a ⊓ b ∈ points(P) := inf.member with*209have complA : compl(P)(a) ∈ points(P) := compl.member with*210have complB : compl(P)(b) ∈ points(P) := compl.member with*211simp [sup.join, inf.meet, compl.complement]212exact latticeComplement.meet_eq with213 (BooleanAlgebra.unbundled with boolean), *214215theorem le_compl_iff_inf_eq_bot (a b ∈ points(P))216shows a ≤ compl(P)(b) ↔ a ⊓ b = bot(P)217proof218rw [inf.meet, compl.complement]219have characterization := latticeComplement.le_iff_meet_bottom at points(P),220 le(P), bot(P), top(P), a, b with (BooleanAlgebra.unbundled with boolean),221assumption, assumption222constructor223 . intro h224exact characterization.forward with (Le.relates with h)225 . intro h226exact Le.of_relates with (characterization.backward with h)227228theorem compl_le_compl (a b ∈ points(P))229requires hab : a ≤ b230shows compl(P)(b) ≤ compl(P)(a)231proof232rw [compl.complement, compl.complement]233unfold Le234exact latticeComplement.antitone with (BooleanAlgebra.unbundled with boolean),235assumption, assumption, (Le.relates with hab)236237theorem compl_le_compl_iff_le (a b ∈ points(P))238shows compl(P)(b) ≤ compl(P)(a) ↔ a ≤ b239proof240rw [compl.complement, compl.complement]241have characterization := latticeComplement.le_iff at points(P), le(P),242 bot(P), top(P), a, b with (BooleanAlgebra.unbundled with boolean),243assumption, assumption244constructor245 . intro h246exact Le.of_relates with (characterization.backward with (Le.relates with h))247 . intro h248exact Le.of_relates with (characterization.forward with (Le.relates with h))249250end BooleanLaws251252-- The powerset of a set is a Boolean algebra whose complement is the253-- relative complement.254instance powersetLattice.boolean (X : Set)255shows BooleanAlgebra(powersetLattice(X))256proof257unfold powersetLattice258exact BooleanAlgebra.mk with (powersetOrder.boolean at X)259260section PowersetBoolean261variable (X : Set)262hypothesis boolean : BooleanAlgebra(powersetLattice(X))263264theorem powersetLattice.compl (B ∈𝒫(X))265shows compl(powersetLattice(X))(B) = X ∖ B266proof267have complete : CompleteLattice(powersetLattice(X)) := powersetLattice.complete at X268have BIn : B ∈ points(powersetLattice(X)) := powersetLattice.member at X, B with269*270have botEq := powersetLattice.bot with complete271have topEq := powersetLattice.top with complete272rw [compl.complement, powersetLattice.points, powersetLattice.le, botEq, topEq]273exact powersetOrder.complement_value with*274275end PowersetBoolean276277-- The Boolean rewrite set: double complements, the complements of the278-- bounds, and the meet and join of an element with its complement.279tactic boolean_simp :=simp [compl_compl, compl_bot, compl_top, inf_compl_eq_bot, sup_compl_eq_top, compl_inf_eq_bot, compl_sup_eq_top]280