1-- Linear orders are distributive 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.LinearLattice8import Distributive910theorem IsLinearOrder.total (A r x y : Set)11requires h : IsLinearOrder(A, r)12requires hx : x ∈ A13requires hy : y ∈ A14shows relates(r, x, y) ∨ relates(r, y, x)15proof16rcases h with⟨partial, total⟩17exact total with x, hx, y, hy1819theorem IsLinearOrder.converse (A r : Set)20requires h : IsLinearOrder(A, r)21shows IsLinearOrder(A, converse(r, A, A))22proof23have order := IsLinearOrder.partial with h24constructor25 . exact IsPartialOrder.converse with order26 . intros x hx y hy27havecases:= IsLinearOrder.total with h, hx, hy28casescaseswith xy | yx29 . right30exact converse.relates with hx, hy, xy31 . left32exact converse.relates with hy, hx, yx3334-- In a linear order the join of two elements is the larger and the meet35-- is the smaller.3637theorem IsLinearOrder.lattice (A r : Set)38requires h : IsLinearOrder(A, r)39shows IsLattice(A, r)40proof41have order := IsLinearOrder.partial with h42have reflexive := IsPartialOrder.reflexive with order43apply IsLattice.of_pairs with order44 . intros x hx y hy45havecases:= IsLinearOrder.total with h, hx, hy46casescaseswith xy | yx47 . witness y48apply Supremum.of_pair with hx, hy, hy, xy, (reflexive with y, hy)49intros b hb xb yb50exact yb51 . witness x52apply Supremum.of_pair with hx, hy, hx, (reflexive with x, hx), yx53intros b hb xb yb54exact xb55 . intros x hx y hy56havecases:= IsLinearOrder.total with h, hx, hy57casescaseswith xy | yx58 . witness x59apply Infimum.of_pair with hx, hy, hx, (reflexive with x, hx), xy60intros b hb bx byBound61exact bx62 . witness y63apply Infimum.of_pair with hx, hy, hy, yx, (reflexive with y, hy)64intros b hb bx byBound65exact byBound6667theorem IsLinearOrder.join_cases (A r x y : Set)68requires h : IsLinearOrder(A, r)69requires hx : x ∈ A70requires hy : y ∈ A71shows join(A, r, x, y) = x ∨ join(A, r, x, y) = y72proof73have lattice := IsLinearOrder.lattice with h74havecases:= IsLinearOrder.total with h, hx, hy75casescaseswith xy | yx76 . right77exact join.of_le with lattice, hx, hy, xy78 . left79exact join.of_ge with lattice, hx, hy, yx8081theorem IsLinearOrder.meet_cases (A r x y : Set)82requires h : IsLinearOrder(A, r)83requires hx : x ∈ A84requires hy : y ∈ A85shows meet(A, r, x, y) = x ∨ meet(A, r, x, y) = y86proof87have lattice := IsLinearOrder.lattice with h88havecases:= IsLinearOrder.total with h, hx, hy89casescaseswith xy | yx90 . left91exact meet.of_le with lattice, hx, hy, xy92 . right93exact meet.of_ge with lattice, hx, hy, yx9495theorem IsLinearOrder.join_member_pair (A r x y : Set)96requires h : IsLinearOrder(A, r)97requires hx : x ∈ A98requires hy : y ∈ A99shows join(A, r, x, y) ∈ {x, y}100proof101havecases:= IsLinearOrder.join_cases with h, hx, hy102casescaseswithleft|right103 . rwleft104exact pair.left at x, y105 . rwright106exact pair.right at x, y107108theorem IsLinearOrder.meet_member_pair (A r x y : Set)109requires h : IsLinearOrder(A, r)110requires hx : x ∈ A111requires hy : y ∈ A112shows meet(A, r, x, y) ∈ {x, y}113proof114havecases:= IsLinearOrder.meet_cases with h, hx, hy115casescaseswithleft|right116 . rwleft117exact pair.left at x, y118 . rwright119exact pair.right at x, y120121-- Distributivity, first when the two joined elements are comparable in a122-- known direction, then in general.123124theorem IsLinearOrder.distributive_of_le (A r x y z : Set)125requires h : IsLinearOrder(A, r)126requires hx : x ∈ A127requires hy : y ∈ A128requires hz : z ∈ A129requires hyz : relates(r, y, z)130shows131 meet(A, r, x, join(A, r, y, z)) = join(A, r, meet(A, r, x, y), meet(A, r, x, z))132proof133have lattice := IsLinearOrder.lattice with h134have transitive := IsLattice.transitive with lattice135have joinValue := join.of_le with lattice, hy, hz, hyz136rw joinValue137have casesXY := IsLinearOrder.total with h, hx, hy138cases casesXY with xy | yx139 . have xz := transitive with x, hx, y, hy, z, hz, xy, hyz140have meetY := meet.of_le with lattice, hx, hy, xy141have meetZ := meet.of_le with lattice, hx, hz, xz142rw [meetY, meetZ]143 symm144exact join.idempotent with lattice, hx145 . have meetY := meet.of_ge with lattice, hx, hy, yx146rw meetY147have casesXZ := IsLinearOrder.total with h, hx, hz148cases casesXZ with xz | zx149 . have meetZ := meet.of_le with lattice, hx, hz, xz150rw meetZ151 symm152exact join.of_le with lattice, hy, hx, yx153 . have meetZ := meet.of_ge with lattice, hx, hz, zx154rw meetZ155 symm156exact join.of_le with lattice, hy, hz, hyz157158theorem IsLinearOrder.distributive (A r : Set)159requires h : IsLinearOrder(A, r)160shows IsDistributiveLattice(A, r)161proof162have lattice := IsLinearOrder.lattice with h163constructor164 . exact lattice165 . intros x hx y hy z hz166havecases:= IsLinearOrder.total with h, hy, hz167casescaseswith yz | zy168 . exact IsLinearOrder.distributive_of_le with h, hx, hy, hz, yz169 . have swapJoin := join.commutative with lattice, hy, hz170have xyA := meet.member with lattice, hx, hy171have xzA := meet.member with lattice, hx, hz172have swapMeets := join.commutative with lattice, xyA, xzA173rw [swapJoin, swapMeets]174exact IsLinearOrder.distributive_of_le with h, hx, hz, hy, zy175176theorem IsLinearOrder.modular (A r : Set)177requires h : IsLinearOrder(A, r)178shows IsModularLattice(A, r)179proof180exact IsDistributiveLattice.modular with (IsLinearOrder.distributive with h)181