1-- Ordered sets bundled as one set, with the order written ≤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.Order8import Bounds910-- An ordered set is a structure of its points and its order relation,11-- Order.mk(A, r). The projections read the two back: P.points is the12-- underlying set and P.le the relation on it, so that every order structure13-- is a property of the one set P. The order structures below are separate14-- predicates over the same data, so Order carries no laws of its own.15structure Order16| points le : Set1718-- The projection laws of a constructed order as one rewrite set.19tactic order_projections :=simp [Order.mk.points, Order.mk.le]2021-- The order structures, as properties of the bundled set. Each is the22-- unbundled predicate at the projections, so a hypothesis Preorder(P)23-- opens to IsPreorder(points(P), le(P)).24predicate Preorder (P : Set) := IsPreorder(points(P), le(P))25predicate PartialOrder (P : Set) := IsPartialOrder(points(P), le(P))26predicate LinearOrder (P : Set) := IsLinearOrder(points(P), le(P))2728-- The order of P as a predicate of its own, written a ≤ b wherever a29-- hypothesis in scope supplies the ordered set; the operands' membership in30-- points(P) selects it when several are in scope. A fact a ≤ b opens to31-- relates(le(P), a, b) wherever local evidence is consulted.32predicate Le (P a b : Set) := relates(le(P), a, b)3334notationinfix50"≤"=> Le(P, _, _) viahypothesis Preorder(P) for P.points35notationinfix50"≤"=> Le(P, _, _) viahypothesis PartialOrder(P) for P.points36notationinfix50"≤"=> Le(P, _, _) viahypothesis LinearOrder(P) for P.points3738theorem Le.relates (P a b : Set)39requires h : Le(P, a, b)40shows relates(le(P), a, b)41proof42simp [Le]4344theorem Le.of_relates (P a b : Set)45requires h : relates(le(P), a, b)46shows Le(P, a, b)47proof48simp [Le]4950theorem Le.iff (P a b : Set)51shows Le(P, a, b) ↔ relates(le(P), a, b)52proof53simp [Le]5455-- Bridges to the unbundled predicates: a structure on (A, r) is one on56-- Order.mk(A, r), and the instances let `with *` read the unbundled form57-- off a bundled hypothesis.58instance Preorder.unbundled (P : Set)59requires order : Preorder(P)60shows IsPreorder(points(P), le(P))61proof62simp [Preorder]6364instance PartialOrder.unbundled (P : Set)65requires order : PartialOrder(P)66shows IsPartialOrder(points(P), le(P))67proof68simp [PartialOrder]6970instance LinearOrder.unbundled (P : Set)71requires order : LinearOrder(P)72shows IsLinearOrder(points(P), le(P))73proof74simp [LinearOrder]7576theorem Preorder.mk (A r : Set)77requires order : IsPreorder(A, r)78shows Preorder(Order.mk(A, r))79proof80unfold Preorder81rw [Order.mk.points, Order.mk.le]82exact order8384theorem PartialOrder.mk (A r : Set)85requires order : IsPartialOrder(A, r)86shows PartialOrder(Order.mk(A, r))87proof88unfold PartialOrder89rw [Order.mk.points, Order.mk.le]90exact order9192theorem LinearOrder.mk (A r : Set)93requires order : IsLinearOrder(A, r)94shows LinearOrder(Order.mk(A, r))95proof96unfold LinearOrder97rw [Order.mk.points, Order.mk.le]98exact order99100instance PartialOrder.preorder_of (P : Set)101requires order : PartialOrder(P)102shows Preorder(P)103proof104unfold Preorder105exact IsPartialOrder.preorder with (PartialOrder.unbundled with order)106107theorem LinearOrder.partial_order (P : Set)108requires order : LinearOrder(P)109shows PartialOrder(P)110proof111unfold PartialOrder112exact IsLinearOrder.partial with (LinearOrder.unbundled with order)113114-- The order relation lives on the points.115theorem IsPreorder.relation (P : Set)116requires order : Preorder(P)117shows RelationBetween(le(P), points(P), points(P))118proof119obtain⟨relation, rest⟩:= Preorder.unbundled with order120exact relation121122section PreorderLaws123variable (P : Set)124hypothesis order : Preorder(P)125126theorem le_refl (a ∈ points(P))127shows a ≤ a128proof129obtain⟨relation, reflexive, transitive⟩:= Preorder.unbundled with order130unfold Le131exact reflexive with a, assumption132133theorem le_trans (a b c ∈ points(P))134requires hab : a ≤ b135requires hbc : b ≤ c136shows a ≤ c137proof138obtain⟨relation, reflexive, transitive⟩:= Preorder.unbundled with order139unfold Le140exact transitive with a, assumption, b, assumption, c, assumption, hab, hbc141142end PreorderLaws143144section PartialOrderLaws145variable (P : Set)146hypothesis order : PartialOrder(P)147148theorem le_antisymm (a b ∈ points(P))149requires hab : a ≤ b150requires hba : b ≤ a151shows a = b152proof153have antisymmetric := IsPartialOrder.antisymmetric with (PartialOrder.unbundled with order)154exact antisymmetric with a, assumption, b, assumption, hab, hba155156theorem le_antisymm_iff (a b ∈ points(P))157shows a = b ↔ (a ≤ b ∧ b ≤ a)158proof159constructor160 . intro same161rw same162have reflexive : b ≤ b := le_refl with*163exact⟨reflexive, reflexive⟩164 . intro both165obtain⟨hab, hba⟩:= both166exact le_antisymm with*167168end PartialOrderLaws169170section LinearOrderLaws171variable (P : Set)172hypothesis order : LinearOrder(P)173174theorem le_total (a b ∈ points(P))175shows a ≤ b ∨ b ≤ a176proof177obtain⟨partial, total⟩:= LinearOrder.unbundled with order178have both := total with a, assumption, b, assumption179cases both with ab | ba180 . left181exact Le.of_relates with ab182 . right183exact Le.of_relates with ba184185end LinearOrderLaws186187-- Bounds of a subset of a bundled order.188def upperBounds (P S : Set) : Set:= { b ∈ points(P) |∀ x ∈ S, relates(le(P), x, b) }189def lowerBounds (P S : Set) : Set:= { b ∈ points(P) |∀ x ∈ S, relates(le(P), b, x) }190predicate IsLUB (P S s : Set) := Supremum(points(P), le(P), S, s)191predicate IsGLB (P S i : Set) := Infimum(points(P), le(P), S, i)192predicate IsLeast (P S a : Set) := Least(points(P), le(P), S, a)193predicate IsGreatest (P S a : Set) := Greatest(points(P), le(P), S, a)194195theorem upperBounds.of_upper_bound (P S b : Set)196requires bound : UpperBound(points(P), le(P), S, b)197shows b ∈ upperBounds(P, S)198proof199obtain⟨bIn, subset, above⟩:= bound200apply upperBounds.membership.backward201exact⟨bIn, above⟩202203theorem lowerBounds.of_lower_bound (P S b : Set)204requires bound : LowerBound(points(P), le(P), S, b)205shows b ∈ lowerBounds(P, S)206proof207obtain⟨bIn, subset, below⟩:= bound208apply lowerBounds.membership.backward209exact⟨bIn, below⟩210211section BoundLaws212variable (P : Set)213hypothesis order : PartialOrder(P)214215theorem IsLUB.unique (S s t : Set)216requires first : IsLUB(P, S, s)217requires second : IsLUB(P, S, t)218shows s = t219proof220unfold IsLUB at first221unfold IsLUB at second222exact Supremum.unique with*223224theorem IsGLB.unique (S i j : Set)225requires first : IsGLB(P, S, i)226requires second : IsGLB(P, S, j)227shows i = j228proof229unfold IsGLB at first230unfold IsGLB at second231exact Infimum.unique with*232233theorem IsLUB.member (S s : Set)234requires lub : IsLUB(P, S, s)235shows s ∈ points(P)236proof237unfold IsLUB at lub238exact Supremum.member with lub239240theorem IsGLB.member (S i : Set)241requires glb : IsGLB(P, S, i)242shows i ∈ points(P)243proof244unfold IsGLB at glb245exact Infimum.member with glb246247theorem IsLUB.le_of_mem (S s x : Set)248requires lub : IsLUB(P, S, s)249requires xIn : x ∈ S250shows x ≤ s251proof252unfold IsLUB at lub253unfold Le254exact Supremum.above with lub, xIn255256theorem IsGLB.le_of_mem (S i x : Set)257requires glb : IsGLB(P, S, i)258requires xIn : x ∈ S259shows i ≤ x260proof261unfold IsGLB at glb262unfold Le263exact Infimum.below with glb, xIn264265theorem IsLUB.le_of_upper (S s b : Set)266requires lub : IsLUB(P, S, s)267requires bound : b ∈ upperBounds(P, S)268shows s ≤ b269proof270obtain⟨upper, least⟩:= lub271obtain⟨bIn, above⟩:= upperBounds.membership.forward with bound272have subset := UpperBound.subset with upper273unfold Le274exact least with b, bIn, ⟨bIn, subset, above⟩275276theorem IsGLB.ge_of_lower (S i b : Set)277requires glb : IsGLB(P, S, i)278requires bound : b ∈ lowerBounds(P, S)279shows b ≤ i280proof281obtain⟨lower, greatest⟩:= glb282obtain⟨bIn, below⟩:= lowerBounds.membership.forward with bound283have subset := LowerBound.subset with lower284unfold Le285exact greatest with b, bIn, ⟨bIn, subset, below⟩286287theorem IsGreatest.isLUB (S a : Set)288requires greatest : IsGreatest(P, S, a)289shows IsLUB(P, S, a)290proof291unfold IsGreatest at greatest292unfold IsLUB293exact Greatest.supremum with*294295theorem IsLeast.isGLB (S a : Set)296requires least : IsLeast(P, S, a)297shows IsGLB(P, S, a)298proof299unfold IsLeast at least300unfold IsGLB301exact Least.infimum with*302303end BoundLaws304305-- The dual of a bundled order reverses the relation; suprema and infima306-- exchange.307def dual (P : Set) : Set:= Order.mk(points(P), converse(le(P), points(P), points(P)))308309theorem dual.points (P : Set)310shows points(dual(P)) = points(P)311proof312simp [dual, Order.mk.points]313314theorem dual.le (P : Set)315shows le(dual(P)) = converse(le(P), points(P), points(P))316proof317simp [dual, Order.mk.le]318319theorem PartialOrder.dual (P : Set)320requires order : PartialOrder(P)321shows PartialOrder(dual(P))322proof323unfold dual324apply PartialOrder.mk325exact IsPartialOrder.converse with (PartialOrder.unbundled with order)326327theorem IsLUB.dual (P S s : Set)328requires order : PartialOrder(P)329requires lub : IsLUB(P, S, s)330shows IsGLB(dual(P), S, s)331proof332unfold IsLUB at lub333unfold IsGLB334rw [dual.points, dual.le]335exact Supremum.converse with*336337theorem IsGLB.dual (P S i : Set)338requires order : PartialOrder(P)339requires glb : IsGLB(P, S, i)340shows IsLUB(dual(P), S, i)341proof342unfold IsGLB at glb343unfold IsLUB344rw [dual.points, dual.le]345exact Infimum.converse with*346