### 8 releases (5 breaking)

0.5.0 | Oct 11, 2023 |
---|---|

0.4.0 | Aug 15, 2023 |

0.3.0 | Jul 4, 2023 |

0.2.0 | May 31, 2023 |

0.0.0 | May 3, 2023 |

#**531** in Network programming

**49** downloads per month

Used in hydroflow

**Apache-2.0**

125KB

3K
SLoC

# The `lattices`

Crate

`lattices`

The

crate provides ergonomic and compsable lattice types. You can also implement custom
lattices via a few simple traits.`lattices`

Lattices are an incredibly powerful mathematical concept which can greatly simplify the trickiness of distributed computing. They align very well with the reality of what happens physically in a distrubted system: messages can always arrive out-of-order or duplicated. But if that data is represented as lattices then all machines will always reach the same end result simply by merging the data together. One popular way of lattices are currently used in distributed systems is as the data underlying Conflict-free Replicated Data Types (CRDTs).

Lattices also allow us to harness the power of the CALM Theorem: "a program has a consistent, coordination-free distributed implementation if and only if it is monotonic." Lattice state is always monotonic, meaning any part of a distributed system built on lattice state can be freely distributed with no coordination overhead. The goal of the Hydro Project is to allow users to write programs that automatically scale and distribute effortlessly.

For more information on the underlying mathematics of lattices and monotonicity, take a look at Lattice Math section of the Hydroflow Book and Section 2 of the Hydroflow Thesis (2021).

Take a look at the

rustdocs.`lattice`

## Lattices

provides implementations of common lattice types:`lattices`

and`Min``<`T`>`

- totally-orderd lattices.`Max``<`T`>`

- set-union lattice of scalar values.`set_union`SetUnion`::``<`T`>`- [

] - scalar keys with nested lattice values.`map_union`MapUnion`::``<`K, Lat`>`

- union partitions of a set of scalar values.`union_find`UnionFind`::``<`K`>`

- growing`VecUnion``<`Lat`>`

of nested lattices, like`Vec`

but without missing entries.`MapUnion``<``<``usize`, Lat`>``>`

- wraps a lattice in`WithBot``<`Lat`>`

with`Option`

as the new bottom value.`None`

- wraps a lattice in`WithTop``<`Lat`>`

with`Option`

as the new`None`*top*value.- [

] - product of two nested lattices.`Pair``<`LatA, LatB`>` - [

]* - a versioned pair where the`DomPair``<`LatKey, LatVal`>`

dominates the`LatKey`

.`LatVal`

* - adds a "conflict" top to domain`Conflict``<`T`>`

. Merging inequal`T`

s results in top.`T`- [

]* - a single "point lattice" value which cannot be merged with any inequal value.`Point``<`T, *`>`

- the "unit" lattice, a "point lattice" with unit`(``)`

as the only value in the domain.`(``)`

*Special implementations which do not obey all lattice properties but are still useful under certain circumstances.

Additionally, custom lattices can be made by implementing the traits below.

## Traits

A type becomes a lattice by implementing one or more traits starting with

. These traits
are already implemented for all the provided lattice types.`Merge`

`Merge`

`Merge`

The main trait is

, which defines a lattice merge function (AKA "join" or "least upper
bound"). Implementors must define the `Merge`

method which does a merge in-place into
`Merge ::`merge

`&``mut` `self`

. The method must return `true`

if `self`

was modified (i.e. the value got larger in the
lattice partial order) and `false`

otherwise (i.e. `other`

was smaller than `self`

). The `Merge``::`merge_owned

function, which merges two owned values, is provided.The

method must be associative, commutative, and idempotent. This is not checked by the
compiler, but the implementor can use the `merge`

method to spot-check
these properties on a collection of values.`test ::`check_lattice_properties

`PartialOrd`

, `LatticeOrd`

, and `NaiveLatticeOrd`

`PartialOrd``LatticeOrd`

`NaiveLatticeOrd`

Rust already has a trait for partial orders,

, which should be implemented on lattice
types. However that trait is not specific to lattice partial orders, therefore we provide the`PartialOrd``LatticeOrd <Rhs>`

`:` `PartialOrd``<`Rhs`>`

marker trait to denote when a `PartialOrd`

implementation is a lattice partial order. `LatticeOrd`

must always agree with the `Merge`

function.Additionally, the sealed

trait is provided on all lattice types that implement
`NaiveLatticeOrd`

and `Merge`

. This trait provides a `Clone`

method which derives a lattice order from
the `naive_cmp`

function directly. However the implementation is generally slow and inefficient.`Merge`

Implementors should use the

method to check their
`test ::`check_partial_ord_properties

`PartialOrd`

implementation, and should use the `test``::`check_lattice_ord

to ensure the partial
order agrees with the `Merge`

-derived `NaiveLatticeOrd`

order.`LatticeFrom`

`LatticeFrom`

is equivalent to the `LatticeFrom`

trait but specific to lattices.
`std ::`

`convert`From

`::``LatticeFrom`

should be implemented only between different representations of the same lattice
type, e.g. between `set_union``::`SetUnionBTreeSet

and `set_union``::`SetUnionHashSet

. For compound
lattice (lattices with nested lattice types), the `LatticeFrom`

implementation should be recursive
for those nested lattices.`IsBot`

, `IsTop`

, and `Default`

`IsBot`

`IsTop`

`Default`A bottom (⊥) is strictly less than all other values. A top (⊤) is strictly greater than all other
values.

and `IsBot ::`is_bot

`IsTop``::`is_top

determine if a lattice instance is top or bottom
respectively.For lattice types,

must create a bottom value. `Default`` ::`default

`(`

`)`

`IsBot``::`is_bot`(``&``Default``::`default`(``)``)`

should always return true for all lattice types.`Atomize`

`Atomize`

converts a lattice point into a bunch of smaller lattice points. When these
"atoms" are merged together they will form the original lattice point. See the docs for more
precise semantics.`Atomize ::`atomize

#### Dependencies

~0.4–1MB

~22K SLoC