open import 1Lab.Prelude

open import Algebra.Semilattice

open import Cat.Functor.Equivalence
open import Cat.Functor.Base
open import Cat.Prelude
open import Cat.Thin

module Algebra.Lattice where

LatticesšŸ”—

A lattice (A,āˆ§,āˆØ)(A, \land, \lor) is a pair of semilattices (A,āˆ§)(A, \land) and (A,āˆØ)(A, \lor) which ā€œfit togetherā€ with equations specifying that āˆ§\land and āˆØ\lor are duals, called absorption laws.

record is-lattice (_āˆ§_ : A ā†’ A ā†’ A) (_āˆØ_ : A ā†’ A ā†’ A) : Type (level-of A) where
  field
    has-meets : is-semilattice _āˆ§_
    has-joins : is-semilattice _āˆØ_
We rename the fields of has-meets and has-joins so they refer to the operator in their name, and hide anything extra from the hierarchy.
  open is-semilattice has-meets public
    renaming ( associative to āˆ§-associative
             ; commutative to āˆ§-commutative
             ; idempotent to āˆ§-idempotent
             )
    hiding ( has-is-magma ; has-is-semigroup )

  open is-semilattice has-joins public
    renaming ( associative to āˆØ-associative
             ; commutative to āˆØ-commutative
             ; idempotent to āˆØ-idempotent
             )
    hiding ( underlying-set ; has-is-magma ; has-is-set ; magma-hlevel )
  field
    āˆ§-absorbs-āˆØ : āˆ€ {x y} ā†’ (x āˆ§ (x āˆØ y)) ā‰” x
    āˆØ-absorbs-āˆ§ : āˆ€ {x y} ā†’ (x āˆØ (x āˆ§ y)) ā‰” x

A lattice structure equips a type AA with two binary operators, the meet āˆ§\land and join āˆØ\lor, such that (A,āˆ§,āˆØ)(A, \land, \lor) is a lattice. Since being a semilattice includes being a set, this means that being a lattice is a property of (A,āˆ§,āˆØ)(A, \land, \lor):

private unquoteDecl eqv = declare-record-iso eqv (quote is-lattice)

instance
  H-Level-is-lattice : āˆ€ {M J : A ā†’ A ā†’ A} {n} ā†’ H-Level (is-lattice M J) (suc n)
  H-Level-is-lattice = prop-instance Ī» x ā†’
    let open is-lattice x in is-hlevelā‰ƒ 1 (Isoā†’Equiv eqv eā»Ā¹) (hlevel 1) x

record Lattice-on (A : Type ā„“) : Type ā„“ where
  field
    _Lāˆ§_ : A ā†’ A ā†’ A
    _LāˆØ_ : A ā†’ A ā†’ A

  infixr 40 _Lāˆ§_
  infixr 30 _LāˆØ_

  field
    has-is-lattice : is-lattice _Lāˆ§_ _LāˆØ_

  open is-lattice has-is-lattice public

  Lattice-onā†’is-meet-semi : is-semilattice _Lāˆ§_
  Lattice-onā†’is-meet-semi = has-meets

  Lattice-onā†’is-join-semi : is-semilattice _LāˆØ_
  Lattice-onā†’is-join-semi = has-joins

open Lattice-on using (Lattice-onā†’is-meet-semi ; Lattice-onā†’is-join-semi) public

Lattice : āˆ€ ā„“ ā†’ Type (lsuc ā„“)
Lattice ā„“ = Ī£ (Lattice-on {ā„“ = ā„“})

Since the absorption laws are property, not structure, a lattice homomorphism turns out to be a function which is homomorphic for both semilattice structures, i.e.Ā one that independently preserves meets and joins.

record Latticeā†’ (A B : Lattice ā„“) (f : A .fst ā†’ B .fst) : Type ā„“ where
  private
    module A = Lattice-on (A .snd)
    module B = Lattice-on (B .snd)

  field
    pres-āˆ§ : āˆ€ x y ā†’ f (x A.Lāˆ§ y) ā‰” f x B.Lāˆ§ f y
    pres-āˆØ : āˆ€ x y ā†’ f (x A.LāˆØ y) ā‰” f x B.LāˆØ f y

Latticeā‰ƒ : (A B : Lattice ā„“) (f : A .fst ā‰ƒ B .fst) ā†’ Type ā„“
Latticeā‰ƒ A B = Latticeā†’ A B āˆ˜ fst

Using the automated machinery for deriving is-univalent proofs, we get that identification of lattices is the same thing as lattice isomorphism.

Lattice-univalent : āˆ€ {ā„“} ā†’ is-univalent (HomTā†’Str (Latticeā‰ƒ {ā„“ = ā„“}))
Lattice-univalent {ā„“ = ā„“} =
  Derive-univalent-record (record-desc (Lattice-on {ā„“ = ā„“}) Latticeā‰ƒ
    (record:
      field[ Lattice-on._Lāˆ§_ by Latticeā†’.pres-āˆ§ ]
      field[ Lattice-on._LāˆØ_ by Latticeā†’.pres-āˆØ ]
      axiom[ Lattice-on.has-is-lattice by (Ī» _ ā†’ hlevel 1) ]))

Order-theoreticallyšŸ”—

We already know that a given semilattice structure can induce one of two posets, depending on whether the semilattice operator is being considered as equipping the poset with meets or joins. Weā€™d then expect that a lattice, having two semi-lattices, would have four poset structures. However, there are only two, which we call the ā€œcovariantā€ and ā€œcontravariantā€ orderings.

Latticeā†’covariant-on : Lattice-on A ā†’ Poset (level-of A) (level-of A)
Latticeā†’covariant-on lat = Semilattice-onā†’Meet-on (Lattice-onā†’is-meet-semi lat)

Latticeā†’contravariant-on : Lattice-on A ā†’ Poset (level-of A) (level-of A)
Latticeā†’contravariant-on lat = Semilattice-onā†’Join-on (Lattice-onā†’is-meet-semi lat)

Above, the ā€œcovariant orderā€ is obtaining by considering the (A,āˆ§)(A, \land) semilattice as inducing meets on the poset (hence the operator being called āˆ§\land). It can also be obtained in a dual way, by considering that (A,āˆØ)(A, \lor) induces joins on the poset. By the absorption laws, these constructions give rise to the same poset; We start by defining a monotone map (that is, a Functor) between the two possibilities:

covariant-order-map
  : (l : Lattice-on A)
  ā†’ Monotone-map
      (Semilattice-onā†’Meet-on (Lattice-onā†’is-meet-semi l))
      (Semilattice-onā†’Join-on (Lattice-onā†’is-join-semi l))
covariant-order-map {A = A} l = F where
  open Lattice-on l
    hiding (Lattice-onā†’is-join-semi ; Lattice-onā†’is-meet-semi)

  F : Monotone-map (Semilattice-onā†’Meet-on (Lattice-onā†’is-meet-semi l))
                   (Semilattice-onā†’Join-on (Lattice-onā†’is-join-semi l))
  F .Fā‚€ = id
  F .Fā‚ {x} {y} p = q where abstract
    q : y ā‰” x LāˆØ y
    q =
      y             ā‰”āŸØ sym āˆØ-absorbs-āˆ§ āŸ©ā‰”
      y LāˆØ (y Lāˆ§ x) ā‰”āŸØ apā‚‚ _LāˆØ_ refl āˆ§-commutative āŸ©ā‰”
      y LāˆØ (x Lāˆ§ y) ā‰”āŸØ apā‚‚ _LāˆØ_ refl (sym p) āŸ©ā‰”
      y LāˆØ x        ā‰”āŸØ āˆØ-commutative āŸ©ā‰”
      x LāˆØ y        āˆŽ
  F .F-id = has-is-set _ _ _ _
  F .F-āˆ˜ _ _ = has-is-set _ _ _ _

We now show that this functor is an equivalence: It is fully faithful and split essentially surjective.

covariant-order-map-is-equivalence
  : (l : Lattice-on A) ā†’ is-equivalence (covariant-order-map l)
covariant-order-map-is-equivalence l =
  ff+split-esoā†’is-equivalence ff eso
  where
    open Lattice-on l hiding (Lattice-onā†’is-join-semi)
    import
      Cat.Reasoning
        (Semilattice-onā†’Join-on (Lattice-onā†’is-join-semi l) .Poset.underlying)
      as D

A tiny calculation shows that this functor is fully faithful, and essential surjectivity is immediate:

    ff : is-fully-faithful (covariant-order-map l)
    ff {x} {y} .is-eqv p .centre .fst =
      x             ā‰”āŸØ sym āˆ§-absorbs-āˆØ āŸ©ā‰”
      x Lāˆ§ (x LāˆØ y) ā‰”āŸØ apā‚‚ _Lāˆ§_ refl (sym p) āŸ©ā‰”
      x Lāˆ§ y        āˆŽ
    ff .is-eqv y .centre .snd = has-is-set _ _ _ _
    ff .is-eqv y .paths x =
      Ī£-path (has-is-set _ _ _ _)
             (is-propā†’is-set (has-is-set _ _) _ _ _ _)

    eso : is-split-eso (covariant-order-map l)
    eso y .fst = y
    eso y .snd =
      D.make-iso (sym āˆØ-idempotent) (sym āˆØ-idempotent)
        (has-is-set _ _ _ _)
        (has-is-set _ _ _ _)