Order theory for computer scientists
Haskell code
The header for the Haskell code in this file is:
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 vAnd, the ghc extensions used are:
-XTypeOperators -XTypeSynonymInstances \
-XParallelListComp -XFlexibleInstancesPreorders
A binary relation on a set is a preorder iff the relation is both reflexive and transitive.
A binary relation is reflexive if for any .
A binary relation is transitive if and implies .
Equivalence relations
An equivalence relation is a preorder that is also symmetric.
A binary relation is symmetric if implies .
An equivalence relation divides a set into equivalence classes.
The equivalence class of the element is :
The set of all equivalence classes on under the relation is the set .
Equivalence relations from preorders
Every preorder induces a natural equivalence relation :
Partial orders
A partial order is a preorder that is antisymmetric.
A partially ordered set or poset is a set equipped with a partial order , often described as the pair .
A binary relation is antisymmetric if and implies .
In Haskell, we can define a partial order through type classes:
class PartialOrder t where
(⊑) :: t -> t -> BoolTotal orders
A total order is a binary relation that is total, transitive and antisymmetric.
A binary relation is total if for any two values and , or .
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 and have a greatest lower bound, denoted .
The greatest lower bound of and 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 and is also called the meet or infimum of and .
The greatest lower bound has the following properties:
- .
- .
- and implies .
Join semilattices
A join semilattice is a poset for which any two elements and have a least upper bound, denoted .
The least upper bound of and 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 and is also called the join or supremum of and .
The least upper bound has the following properties:
- .
- .
- and implies .
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:
class PartialOrder t => Lattice t where
(⊔) :: t -> t -> t
(⊓) :: t -> t -> tBounded lattices
A lattice is bounded if there exists a maximum element (top or ) and a minimum element (bottom or ) in the set .
For any element in the lattice, it must be the case that:
- and
- .
In Haskell, the type class for a bounded lattice defines a top and a bottom:
class Lattice t => BoundedLattice t where
bot :: t
top :: tComplete lattices
A lattice is a complete lattice if every (possibly infinite) subset of has both a least upper bound () and a greatest lower bound ().
Every complete lattice is a bounded lattice:
- .
- .
Monotonic functions
Given posets and , a function is monotonic or order-preserving if implies .
Continuous functions
To define continuous function, we first need to define member-wise function application across sets.
Given a function , if , then .
Alternately, .
(In some texts, member-wise function application is not notationally distinguished from ordinary function application.)
Given lattices and , a function is Scott-continuous if implies .
Scott-continuous functions are also monotonic.
Fixed points
Given a function , is a fixed point of if .
Regions
With respect to a monotonic function on a complete lattice , we can divide the set into regions:
- is the fixed-point region.
- is the ascending region.
- is the descending region.
It is useful to distinguish the least and greatest fixed points:
- .
- .
The following properties hold for these regions:
- .
- If , then .
- If , then .
- .
- .
I recommend proving these properties as short exercises.
Kleene chain
Given a monotonic function on a lattice , the Kleene chain starting at the point is the set :
The notation means iterated function composition:
If , then there will be an ascending order to the chain , since for any .
In Haskell, an infinite list represents the Kleene chain starting at bottom:
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 is a complete lattice
and the function is continuous,
then .
Moreover, for a lattice of finite height, there exists a natural such that:
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:
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:
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 , the pointed poset contains a bottom:
And, the pointed poset contains a top:
If is a poset, then the order is extended under pointing, so that for the lattice :
- for any .
- for any .
- iff for any .
In Haskell:
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 ⊑ bFlat lattices
Given a set , we can lift into a flat lattice :
where iff:
- or
- or
- .
In Haskell:
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 = TopPartial orders over sums
Given a collection of posets we can naturally lift the partial order into the poset :
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 and , the natural component-wise Cartesian product lattice is :
so that:
- ; and
- ; and
- ; and
- .
In Haskell:
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 the natural sequence poset is :
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:
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 = PTopInclusion lattices
Given a set , the subsets of form a lattice under inclusion: .
Preorder over partially ordered sets
Given a partial order , we can define the preordered set :
Why is this not a partial order itself?
Consider sets over the natural numbers with the natural total order. Under this regime, and , yet , 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 , the natural partial order over equivalence classes is the poset , where iff and :
Lattices over functions
Given a lattice and a set , the natural point-wise lattice is the lattice :
so that:
In Haskell:
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.