1-- Lattices: ordered sets in which every pair has a join and a meet2-- 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.Lattice8import Bounds910predicate IsLattice (A r : Set) :=11 IsPartialOrder(A, r) ∧12 (∀ x ∈ A, ∀ y ∈ A,13 (∃ s, Supremum(A, r, {x, y}, s)) ∧ (∃ i, Infimum(A, r, {x, y}, i)))1415theorem IsLattice.order (A r : Set)16requires h : IsLattice(A, r)17shows IsPartialOrder(A, r)18proof19simp [IsLattice]2021theorem IsLattice.reflexive (A r : Set)22requires h : IsLattice(A, r)23shows ReflexiveOn(A, r)24proof25exact IsPartialOrder.reflexive with (IsLattice.order with h)2627theorem IsLattice.transitive (A r : Set)28requires h : IsLattice(A, r)29shows TransitiveOn(A, r)30proof31exact IsPartialOrder.transitive with (IsLattice.order with h)3233theorem IsLattice.antisymmetric (A r : Set)34requires h : IsLattice(A, r)35shows AntisymmetricOn(A, r)36proof37exact IsPartialOrder.antisymmetric with (IsLattice.order with h)3839theorem IsLattice.exists_supremum (A r x y : Set)40requires h : IsLattice(A, r)41requires hx : x ∈ A42requires hy : y ∈ A43shows∃ s, Supremum(A, r, {x, y}, s)44proof45rcases h with⟨order, pairs⟩46have both := pairs with x, hx, y, hy47exact both.left4849theorem IsLattice.exists_infimum (A r x y : Set)50requires h : IsLattice(A, r)51requires hx : x ∈ A52requires hy : y ∈ A53shows∃ i, Infimum(A, r, {x, y}, i)54proof55rcases h with⟨order, pairs⟩56have both := pairs with x, hx, y, hy57exact both.right5859-- The join of two elements is the supremum of the pair, and the meet is60-- its infimum. Both are determined uniquely by antisymmetry.6162defunique join (A r x y : Set) (z ∈ A) := Supremum(A, r, {x, y}, z)63requires lattice : IsLattice(A, r)64requires hx : x ∈ A65requires hy : y ∈ A66proof67obtain⟨s, hs⟩:= IsLattice.exists_supremum with lattice, hx, hy68witness s69refine⟨Supremum.member with hs, hs⟩70uniqueness71intros s sA first t tA second72exact Supremum.unique with (IsLattice.order with lattice), first, second7374defunique meet (A r x y : Set) (z ∈ A) := Infimum(A, r, {x, y}, z)75requires lattice : IsLattice(A, r)76requires hx : x ∈ A77requires hy : y ∈ A78proof79obtain⟨i, hi⟩:= IsLattice.exists_infimum with lattice, hx, hy80witness i81refine⟨Infimum.member with hi, hi⟩82uniqueness83intros i iA first j jA second84exact Infimum.unique with (IsLattice.order with lattice), first, second8586theorem join.member (A r x y : Set)87requires lattice : IsLattice(A, r)88requires hx : x ∈ A89requires hy : y ∈ A90shows join(A, r, x, y) ∈ A91proof92have selected := join.selected with lattice, hx, hy93exact selected.left9495theorem join.supremum (A r x y : Set)96requires lattice : IsLattice(A, r)97requires hx : x ∈ A98requires hy : y ∈ A99shows Supremum(A, r, {x, y}, join(A, r, x, y))100proof101have selected := join.selected with lattice, hx, hy102exact selected.right103104theorem join.upper_left (A r x y : Set)105requires lattice : IsLattice(A, r)106requires hx : x ∈ A107requires hy : y ∈ A108shows relates(r, x, join(A, r, x, y))109proof110exact Supremum.pair_left with (join.supremum with lattice, hx, hy)111112theorem join.upper_right (A r x y : Set)113requires lattice : IsLattice(A, r)114requires hx : x ∈ A115requires hy : y ∈ A116shows relates(r, y, join(A, r, x, y))117proof118exact Supremum.pair_right with (join.supremum with lattice, hx, hy)119120theorem join.least (A r x y b : Set)121requires lattice : IsLattice(A, r)122requires hx : x ∈ A123requires hy : y ∈ A124requires hb : b ∈ A125requires hxb : relates(r, x, b)126requires hyb : relates(r, y, b)127shows relates(r, join(A, r, x, y), b)128proof129have supremum := join.supremum with lattice, hx, hy130exact Supremum.pair_least with supremum, hb, hxb, hyb131132theorem join.of_supremum (A r x y z : Set)133requires lattice : IsLattice(A, r)134requires hx : x ∈ A135requires hy : y ∈ A136requires h : Supremum(A, r, {x, y}, z)137shows z = join(A, r, x, y)138proof139have order := IsLattice.order with lattice140have supremum := join.supremum with lattice, hx, hy141exact Supremum.unique with order, h, supremum142143theorem meet.member (A r x y : Set)144requires lattice : IsLattice(A, r)145requires hx : x ∈ A146requires hy : y ∈ A147shows meet(A, r, x, y) ∈ A148proof149have selected := meet.selected with lattice, hx, hy150exact selected.left151152theorem meet.infimum (A r x y : Set)153requires lattice : IsLattice(A, r)154requires hx : x ∈ A155requires hy : y ∈ A156shows Infimum(A, r, {x, y}, meet(A, r, x, y))157proof158have selected := meet.selected with lattice, hx, hy159exact selected.right160161theorem meet.lower_left (A r x y : Set)162requires lattice : IsLattice(A, r)163requires hx : x ∈ A164requires hy : y ∈ A165shows relates(r, meet(A, r, x, y), x)166proof167exact Infimum.pair_left with (meet.infimum with lattice, hx, hy)168169theorem meet.lower_right (A r x y : Set)170requires lattice : IsLattice(A, r)171requires hx : x ∈ A172requires hy : y ∈ A173shows relates(r, meet(A, r, x, y), y)174proof175exact Infimum.pair_right with (meet.infimum with lattice, hx, hy)176177theorem meet.greatest (A r x y b : Set)178requires lattice : IsLattice(A, r)179requires hx : x ∈ A180requires hy : y ∈ A181requires hb : b ∈ A182requires hbx : relates(r, b, x)183requires hby : relates(r, b, y)184shows relates(r, b, meet(A, r, x, y))185proof186have infimum := meet.infimum with lattice, hx, hy187exact Infimum.pair_greatest with infimum, hb, hbx, hby188189theorem meet.of_infimum (A r x y z : Set)190requires lattice : IsLattice(A, r)191requires hx : x ∈ A192requires hy : y ∈ A193requires h : Infimum(A, r, {x, y}, z)194shows z = meet(A, r, x, y)195proof196have order := IsLattice.order with lattice197have infimum := meet.infimum with lattice, hx, hy198exact Infimum.unique with order, h, infimum199200-- A lattice may be built from any choice of pairwise suprema and infima.201202theorem IsLattice.of_pairs (A r : Set)203requires order : IsPartialOrder(A, r)204requires suprema : ∀ x ∈ A, ∀ y ∈ A, ∃ s, Supremum(A, r, {x, y}, s)205requires infima : ∀ x ∈ A, ∀ y ∈ A, ∃ i, Infimum(A, r, {x, y}, i)206shows IsLattice(A, r)207proof208constructor209 . exact order210 . intros x hx y hy211have supremum := suprema with x, hx, y, hy212have infimum := infima with x, hx, y, hy213constructor214 . exact supremum215 . exact infimum216217-- The dual of a lattice is a lattice, with join and meet exchanged.218219theorem IsLattice.converse (A r : Set)220requires lattice : IsLattice(A, r)221shows IsLattice(A, converse(r, A, A))222proof223have order := IsLattice.order with lattice224apply IsLattice.of_pairs with (IsPartialOrder.converse with order)225 . intros x hx y hy226obtain⟨i, hi⟩:= IsLattice.exists_infimum with lattice, hx, hy227refine⟨i, Infimum.converse with hi⟩228 . intros x hx y hy229obtain⟨s, hs⟩:= IsLattice.exists_supremum with lattice, hx, hy230refine⟨s, Supremum.converse with hs⟩231232theorem join.converse (A r x y : Set)233requires lattice : IsLattice(A, r)234requires hx : x ∈ A235requires hy : y ∈ A236shows join(A, converse(r, A, A), x, y) = meet(A, r, x, y)237proof238have dual := IsLattice.converse with lattice239have infimum := meet.infimum with lattice, hx, hy240have supremum := Infimum.converse with infimum241 symm242exact join.of_supremum with dual, hx, hy, supremum243244theorem meet.converse (A r x y : Set)245requires lattice : IsLattice(A, r)246requires hx : x ∈ A247requires hy : y ∈ A248shows meet(A, converse(r, A, A), x, y) = join(A, r, x, y)249proof250have dual := IsLattice.converse with lattice251have supremum := join.supremum with lattice, hx, hy252have infimum := Supremum.converse with supremum253 symm254exact meet.of_infimum with dual, hx, hy, infimum255