Lattice Math
Lattices are a simple but powerful mathematic concept that can greatly simplify programming distributed systems.
Lattices For Dummies
So, what is a lattice? Lattices are conceptually very simple, so let's explain them without too much mathy language. A lattice is some type of thing that has a very special merge function. The merge function combines two things and produces an output thing. The core feature of lattices is that the merge function has some special properties: associativity, commutativity, and idempotence (abbreviated ACI).
Lets start with a simple example of something which turns out to not be a lattice: numbers and addition. Numbers are a type of thing, and addition is a function that takes two input numbers and produces an output number! But does it satisfy the ACI properties?
Let's start with commutativity. A function is commutative if it doesn't matter if you swap its two inputs:
Addition is commutative, so that property is satisfied! (In contrast, subtraction is not commutative).
Next associativity. A function is associative if, when there are multiple calls together, it does not matter in what order you evaluate the calls:
Addition satisfies associativity! (Again, subtraction is not associative -- if you subtract a subtraction then you're doing addition!)
Finally, idempotence. A function is idempotent if when you give it the same value twice it returns that same value.
Addition is NOT idempotent, because is , not just . This works for , but not for all numbers.
Now lets look at something that is a lattice. Numbers and the max
function. It is
commutiative; the max of is the same as the max of . It is associative; the max of
will always be the same no mater what order you look at them in. And, unlike addition,
it is idempotent; the max of and is always just .
The most standard, canonical example of a lattice is sets with the union function. It is commutative and associative; it doesn't matter what order you union sets together, the result will always be the same. And it is idempotent; a set unioned with itself is the same set.
Lattice Partial Order
When learning about lattices you'll hear about some sort of "partial order" relating to them. What is a partial order? It's like a normal order, where you can say " comes before ", but it is partial because sometimes say " and are incomparable" instead. As it turns out, the lattice merge function actually creates a partial order on the elements of the lattice. Specifically, we say that whenever you merge two things, the thing you get out is always bigger than each of the inputs (or it might be equal to one or both of them).
So for the number-max lattice, tells us that