Post · 11 min read· pp. 597–605

Order theory for computer scientists

Haskell code

The header for the Haskell code in this file is:

text
import Data.Map as Map hiding (map)
import Data.Set as Set hiding (map)

type ℙ a = Set.Set a

type k :-> v = Map.Map k v

And, the ghc extensions used are:

text
 -XTypeOperators -XTypeSynonymInstances \
 -XParallelListComp -XFlexibleInstances

Preorders

A binary relation ⊑\sqsubseteq on a set XX is a preorder iff the relation ⊑\sqsubseteq is both reflexive and transitive.

A binary relation RR is reflexive if xRxx \mathrel{R} x for any xx.

A binary relation RR is transitive if aRba \mathrel{R} b and bRcb \mathrel{R} c implies aRca \mathrel{R} c.

Equivalence relations

An equivalence relation ≡\equiv is a preorder that is also symmetric.

A binary relation RR is symmetric if aRba \mathrel{R} b implies bRab \mathrel{R} a.

An equivalence relation ≡\equiv divides a set SS into equivalence classes.

The equivalence class of the element xx is [x]≡[x]_\equiv:

[x]≡={y∣x≡y}.[x]_\equiv = \{ y \mathrel{|} x \equiv y \} \text.

The set of all equivalence classes on XX under the relation ≡\equiv is the set X/≡X/\equiv.

Equivalence relations from preorders

Every preorder ⊑\sqsubseteq induces a natural equivalence relation ≡⊑\equiv_{\sqsubseteq}:

a≡⊑b iff a⊑b and b⊑a.a \mathrel{\equiv_{\sqsubseteq}} b \text{ iff } a \sqsubseteq b \text{ and } b \sqsubseteq a \text.

Partial orders

A partial order is a preorder that is antisymmetric.

A partially ordered set or poset is a set XX equipped with a partial order ⊑\sqsubseteq, often described as the pair (X,⊑)(X,\sqsubseteq).

A binary relation RR is antisymmetric if aRba \mathrel{R} b and bRab \mathrel{R} a implies a=ba = b.

In Haskell, we can define a partial order through type classes:

text
class PartialOrder t where
 (⊑) :: t -> t -> Bool

Total orders

A total order ≤\leq is a binary relation that is total, transitive and antisymmetric.

A binary relation RR is total if for any two values aa and bb, aRba \mathrel{R} b or bRab \mathrel{R} a.

Totality implies reflexivity, which means that every total order is also a partial order.

Meet semilattices

A meet semilattice is a poset for which any two elements aa and bb have a greatest lower bound, denoted a⊓ba \sqcap b.

The greatest lower bound of aa and bb is the largest element that is still less than both of them.

In a lattice, the greatest lower bound must be unique.

The greatest lower bound of aa and bb is also called the meet or infimum of aa and bb.

The greatest lower bound has the following properties:

  • (a⊓b)⊑a(a \sqcap b) \sqsubseteq a.
  • (a⊓b)⊑b(a \sqcap b) \sqsubseteq b.
  • c⊑ac \sqsubseteq a and c⊑bc \sqsubseteq b implies c⊑(a⊓b)c \sqsubseteq (a \sqcap b).

Join semilattices

A join semilattice is a poset for which any two elements aa and bb have a least upper bound, denoted a⊔ba \sqcup b.

The least upper bound of aa and bb is the smallest element that is still greater than both.

In a lattice, the least upper bound must be unique.

The least upper bound of aa and bb is also called the join or supremum of aa and bb.

The least upper bound has the following properties:

  • a⊑(a⊔b)a \sqsubseteq (a \sqcup b).
  • b⊑(a⊔b)b \sqsubseteq (a \sqcup b).
  • a⊑ca \sqsubseteq c and b⊑cb \sqsubseteq c implies (a⊔b)⊑c(a \sqcup b) \sqsubseteq c.

Lattices

If a poset is both a meet semilattice and a join semilattice, then the poset is also a lattice.

In Haskell, a lattice is a partial order with meet and join defined:

text
class PartialOrder t => Lattice t where
 (⊔) :: t -> t -> t
 (⊓) :: t -> t -> t

Bounded lattices

A lattice (L,⊑)(L,\sqsubseteq) is bounded if there exists a maximum element (top or ⊤\top) and a minimum element (bottom or ⊥\bot) in the set LL.

For any element xx in the lattice, it must be the case that:

  • x⊑⊤x \sqsubseteq \top and
  • ⊥⊑x\bot \sqsubseteq x.

In Haskell, the type class for a bounded lattice defines a top and a bottom:

text
class Lattice t => BoundedLattice t where
 bot :: t
 top :: t

Complete lattices

A lattice (L,⊑)(L,\sqsubseteq) is a complete lattice if every (possibly infinite) subset SS of LL has both a least upper bound (sup(S)\mathit{sup}(S)) and a greatest lower bound (inf(S)\mathit{inf}(S)).

Every complete lattice (L,⊑)(L,\sqsubseteq) is a bounded lattice:

  • ⊥=inf(L)\bot = \mathit{inf}(L).
  • ⊤=sup(L)\top = \mathit{sup}(L).

Monotonic functions

Given posets (X,⊑X)(X,\sqsubseteq_X) and (Y,⊑Y)(Y,\sqsubseteq_Y), a function f:X→Yf : X \to Y is monotonic or order-preserving if x⊑Xx′x \mathrel{\sqsubseteq_X} x' implies f(x)⊑Yf(x′)f(x) \mathrel{\sqsubseteq_Y} f(x').

Continuous functions

To define continuous function, we first need to define member-wise function application across sets.

Given a function f:X→Yf : X \to Y, if S⊆XS \subseteq X, then f.S={f(x)∣x∈S}f.S = \{ f(x) \mathrel{|} x \in S \}.

Alternately, f.{x1,…,xn}={f(x1),…,f(xn)}f.\{x_1,\ldots,x_n\} = \{f(x_1), \ldots, f(x_n)\}.

(In some texts, member-wise function application is not notationally distinguished from ordinary function application.)

Given lattices (X,⊑X)(X,\sqsubseteq_X) and (Y,⊑Y)(Y,\sqsubseteq_Y), a function f:X→Yf : X \to Y is Scott-continuous if S⊆XS \subseteq X implies f(sup(S))=sup(f.S)f(\mathit{sup}(S)) = \mathit{sup}(f.S).

Scott-continuous functions are also monotonic.

Fixed points

Given a function f:X→Xf : X \to X, xx is a fixed point of ff if x=f(x)x = f(x).

Regions

With respect to a monotonic function f:X→Xf : X \to X on a complete lattice (X,⊑)(X,\sqsubseteq), we can divide the set XX into regions:

  • Fix(f)={x∣x=f(x)}\mathit{Fix}(f) = \{ x \mathrel{|} x = f(x) \} is the fixed-point region.
  • Asc(f)={x∣x⊑f(x)}\mathit{Asc}(f) = \{ x \mathrel{|} x \sqsubseteq f(x) \} is the ascending region.
  • Desc(f)={x∣x⊒f(x)}\mathit{Desc}(f) = \{ x \mathrel{|} x \sqsupseteq f(x) \} is the descending region.

It is useful to distinguish the least and greatest fixed points:

  • lfp(f)=inf(Fix(f))\mathit{lfp}(f) = \mathit{inf}(\mathit{Fix}(f)).
  • gfp(f)=sup(Fix(f))\mathit{gfp}(f) = \mathit{sup}(\mathit{Fix}(f)).

The following properties hold for these regions:

  • Fix(f)=Asc(f)∩Desc(f)\mathit{Fix}(f) = \mathit{Asc}(f) \cap \mathit{Desc}(f).
  • If x∈Asc(f)x \in \mathit{Asc}(f), then f(x)∈Asc(f)f(x) \in \mathit{Asc}(f).
  • If x∈Desc(f)x \in \mathit{Desc}(f), then f(x)∈Desc(f)f(x) \in \mathit{Desc}(f).
  • ⊥∈Asc(f)\bot \in \mathit{Asc}(f).
  • ⊤∈Desc(f)\top \in \mathit{Desc}(f).

I recommend proving these properties as short exercises.

Kleene chain

Given a monotonic function f:L→Lf : L \to L on a lattice (L,⊑)(L,\sqsubseteq), the Kleene chain starting at the point x∈Lx \in L is the set K(x)K(x):

K(x)={fi(x)∣i≥0}K(x) = \{ f^i(x) \mathrel{|} i \geq 0 \}

The fif^i notation means iterated function composition:

f0(x)=x and fi(x)=fi−1(f(x)).f^0(x) = x \text{ and } f^i(x) = f^{i-1}(f(x)) \text.

If x∈Asc(f)x \in \mathit{Asc}(f), then there will be an ascending order to the chain K(x)K(x), since fi(x′)⊑fi+1(x′)f^i(x') \sqsubseteq f^{i+1}(x') for any x′∈Asc(f)x' \in \mathit{Asc}(f).

In Haskell, an infinite list represents the Kleene chain starting at bottom:

text
kleene :: (BoundedLattice t) => (t -> t) -> [t]
kleene f = bot:(map f (kleene f))

Kleene's fixed point theorem

On lattices, Kleene's fixed point theorem says:

If (L,⊑)(L,\sqsubseteq) is a complete lattice

and the function f:L→Lf : L \to L is continuous,

then lfp(f)=sup(K(⊥))\mathit{lfp}(f) = \mathit{sup}(K(\bot)).

Moreover, for a lattice of finite height, there exists a natural nn such that:

lfp(f)=fn(⊥).\mathit{lfp}(f) = f^n(\bot)\text.

This fact leads to a simple algorithm for computing fixed points.

The stable function looks for the first element in a sequence to repeat its neighbor:

text
stable :: Eq a => [a] -> a
stable (x:fx:tl) | x == fx   = x
                 | otherwise = stable (fx:tl)

So, the least fixed point function looks for a stable point in the Kleene sequence:

text
lfp :: (BoundedLattice t, Eq t) => (t -> t) -> t
lfp f = stable (kleene f)

Natural lifts for lattices

There are several natural liftings for posets and lattices across the foundational discrete data structures: flat sets, power sets, Cartesian products, sums, sequences and functions.

Pointed sets

Given a set SS, the pointed poset S⊥S_\bot contains a bottom:

S⊥=S+⊥.S_\bot = S + \bot \text.

And, the pointed poset S⊤S^\top contains a top:

S⊤=S+⊤.S^\top = S + \top \text.

If (S,⊑)(S,\sqsubseteq) is a poset, then the order is extended under pointing, so that for the lattice (S⊥⊤,⊑′)(S^\top_\bot,\sqsubseteq'):

  • ⊥⊑′x\bot \mathrel{\sqsubseteq'} x for any xx.
  • x⊑′⊤x \mathrel{\sqsubseteq'} \top for any xx.
  • x⊑′x′x \mathrel{\sqsubseteq'} x' iff x⊑x′x \mathrel{\sqsubseteq} x' for any x,x′∈Sx,x' \in S.

In Haskell:

text
data Pointed a = PTop
               | PBot
               | PEl a

instance PartialOrder a => PartialOrder (Pointed a) where
 PBot ⊑ x = True
 x ⊑ PTop = True
 (PEl a) ⊑ (PEl b) = a ⊑ b

Flat lattices

Given a set SS, we can lift SS into a flat lattice (L,⊑)(L,\sqsubseteq):

L=S⊥⊤=S+⊥+⊤.L = S^\top_\bot\text = S + \bot + \top\text.

where x⊑yx \sqsubseteq y iff:

  • x=⊥x = \bot or
  • y=⊤y = \top or
  • x=yx = y.

In Haskell:

text
data Flat a = Top
            | Bot
            | El a

instance (Eq a) => PartialOrder (Flat a) where
 Bot ⊑ x = True
 x ⊑ Top = True
 (El a) ⊑ (El b) = a == b

instance (Eq a) => Lattice (Flat a) where
 Bot ⊔ x = x
 x ⊔ Bot = x

 Top ⊔ x = Top
 x ⊔ Top = Top

 (El a) ⊔ (El b) | a == b    = El a
                 | otherwise = Top

 Bot ⊓ x = Bot
 x ⊓ Bot = Bot

 Top ⊓ x = x
 x ⊓ Top = x

 (El a) ⊓ (El b) | a == b    = El a
                 | otherwise = Bot

instance (Eq a) => BoundedLattice (Flat a) where
 bot = Bot
 top = Top

Partial orders over sums

Given a collection of posets (X1,⊑1),…,(Xn,⊑n)(X_1,\sqsubseteq_1),\ldots,(X_n,\sqsubseteq_n) we can naturally lift the partial order into the poset (X1+⋯+Xn,⊑)(X_1 + \cdots + X_n, \sqsubseteq):

x⊑x′ iff x⊑ix′ for some i.x \sqsubseteq x' \text{ iff } x \mathrel{\sqsubseteq_i} x' \text{ for some } i\text.

If the posets are lattices, then the resulting poset is not a lattice by default.

To make it into a lattice, it must pointed.

Lattices over Cartesian products

Given lattices (L1,⊑1)(L_1,\sqsubseteq_1) and (L2,⊑2)(L_2,\sqsubseteq_2), the natural component-wise Cartesian product lattice is (L1×L2,⊑)(L_1 \times L_2, \sqsubseteq):

(a,b)⊑(a′,b′) iff a⊑1a′ and b⊑2b′.(a,b) \sqsubseteq (a',b') \text { iff } a \mathrel{\sqsubseteq_1} a' \text{ and } b \mathrel{\sqsubseteq_2} b'\text.

so that:

  • (a,b)⊔(a′,b′)=(a⊔a′,b⊔b′)(a,b) \sqcup (a',b') = (a \sqcup a', b \sqcup b'); and
  • (a,b)⊓(a′,b′)=(a⊓a′,b⊓b′)(a,b) \sqcap (a',b') = (a \sqcap a', b \sqcap b'); and
  • ⊥=(⊥L1,⊥L2)\bot = (\bot_{L_1},\bot_{L_2}); and
  • ⊤=(⊤L1,⊤L2)\top = (\top_{L_1},\top_{L_2}).

In Haskell:

text
instance (PartialOrder a, PartialOrder b) => PartialOrder (a,b) where
 (x,y) ⊑ (x',y') = x ⊑ x' && y ⊑ y'

instance (Lattice a, Lattice b) => Lattice (a,b) where
 (x,y) ⊔ (x',y') = (x ⊔ x', y ⊔ y')
 (x,y) ⊓ (x',y') = (x ⊓ x', y ⊓ y')

instance (BoundedLattice a, BoundedLattice b) =>
         BoundedLattice (a,b) where
 bot = (bot,bot)
 top = (top,top)

Partial orders over sequences

Given a poset (L,⊑)(L,\sqsubseteq) the natural sequence poset is (L∗,⊑∗)(L^*,\sqsubseteq^*):

⟨x1,…,xn⟩⊑∗⟨x1′,…,xn′⟩ iff xi⊑xi′ for all i such that 0≤i≤n.\langle x_1, \ldots, x_n \rangle \mathrel{\sqsubseteq^*} \langle x_1', \ldots, x_n' \rangle \text { iff } x_i \sqsubseteq x_i' \text{ for all } i \text{ such that } 0 \leq i \leq n \text.

As with sums, sequences must be pointed to become lattices. That is, there is no sequence which is greater (or lesser) than all other sequences because sequences of different lengths are always incomparable.

In Haskell:

text
instance (PartialOrder a) => PartialOrder [a] where
 []     ⊑ []       = True
 (x:xl) ⊑ (x':xl') = (x ⊑ x') && (xl ⊑ xl')
 _      ⊑ _        = False -- different lengths

instance (Lattice a) => Lattice (Pointed [a]) where
 PBot ⊔ x = x
 x ⊔ PBot = x

 PTop ⊔ x = PTop
 x ⊔ PTop = PTop

 (PEl v) ⊔ (PEl v') = PEl [ x ⊔ x' | x <- v | x' <- v' ]

 PBot ⊓ x = PBot
 x ⊓ PBot = PBot

 PTop ⊓ x = x
 x ⊓ PTop = x

 (PEl v) ⊓ (PEl v') = PEl [ x ⊓ x' | x <- v | x' <- v' ]

instance (Lattice a) => BoundedLattice (Pointed [a]) where
 bot = PBot
 top = PTop

Inclusion lattices

Given a set SS, the subsets of SS form a lattice under inclusion: (P(S),⊆)(\mathcal{P}(S),\subseteq).

Preorder over partially ordered sets

Given a partial order (X,⊑)(X,\sqsubseteq), we can define the preordered set (P(X),⊑′)(\mathcal{P}(X), \sqsubseteq'):

S⊑′S′ iff for each x∈S, there exists x′∈S′ such that x⊑x′.S \mathrel{\sqsubseteq'} S' \text{ iff for each } x \in S, \text{ there exists } x' \in S' \text{ such that } x \sqsubseteq x' \text.

Why is this not a partial order itself?

Consider sets over the natural numbers with the natural total order. Under this regime, {1,2,3}⊑′{3}\{1,2,3\} \mathrel{\sqsubseteq'} \{3\} and {3}⊑′{1,2,3}\{3\} \mathrel{\sqsubseteq'} \{1,2,3\}, yet {1,2,3}≠{3}\{1,2,3\} \neq \{3\}, which violates antisymmetry.

Of course, it is possible to lift a preorder into a partial order over equivalences classes.

Alternatively, sets can be canonicalized, so that only that each equivalence class is represented by a single distinguished member.

Partial orders from preorders

If a preorder is not also a partial order, it is possible to lift a partial order to equivalence classes of the preorder.

Given a preordered set (X,≤)(X,\leq), the natural partial order over equivalence classes is the poset (X/≡,⊑)(X/{\equiv},\sqsubseteq), where a≡ba \equiv b iff a≤ba \leq b and b≤ab \leq a:

[x]≡⊑[y]≡ iff x≤y.[x]_\equiv \sqsubseteq [y]_\equiv \text{ iff } x \leq y \text.

Lattices over functions

Given a lattice (Y,⊑)(Y,\sqsubseteq) and a set XX, the natural point-wise lattice is the lattice (X→Y,⊑′)(X \to Y, \sqsubseteq'):

f⊑′g iff f(x)⊑g(x) for every x∈dom(f),f \mathrel{\sqsubseteq'} g \text{ iff } f(x) \sqsubseteq g(x) \text{ for every } x \in \mathit{dom}(f) \text,

so that:

(f⊔g)(x)=f(x)⊔g(x),(f \sqcup g)(x) = f(x) \sqcup g(x) \text,
and:
(f⊓g)(x)=f(x)⊓g(x).(f \sqcap g)(x) = f(x) \sqcap g(x) \text.

In Haskell:

text
instance (Ord k, PartialOrder v) => PartialOrder (k :-> v) where
 f ⊑ g = isSubmapOfBy (⊑) f g

instance (Ord k, Lattice v) => Lattice (k :-> v) where
 f ⊔ g = unionWith (⊔) f g
 f ⊓ g = unionWith (⊓) f g

instance (Ord k, BoundedLattice v) => BoundedLattice (k :-> v) where
 bot = Map.empty
 top = error "top map: not finitely representable" 

What's next?

This post scratches the surface of order theory, and it leaves out even some applications in computer science.

The two biggest omissions are reserved for future posts: the ordinals and the Scott domain theory.

The ordinals extend the natural numbers into a transfinite arithmetic and provide a powerful structure for well-ordering.

That well-ordered structure is a powerful mechanism for reasoning about program termination.

Scott domain theory gives a meaningful interpretation to the infinitely recursive domain equations that arise in denotational semantics.

If you'd like to learn Haskell, I recommend Learn You a Haskell for Great Good!: A Beginner's Guide.

And, for using Haskell, I recommend Real World Haskell.