Std.CRDT (Cure Standard Library v0.37.2)

View Source

Conflict-free Replicated Data Types for Cure (v0.27.0).

Every CRDT in this module satisfies three laws that make it safe for eventual-consistency replication:

  1. merge(a, b) == merge(b, a) (commutative)
  2. merge(a, merge(b, c)) == merge(merge(a, b), c) (associative)
  3. merge(a, a) == a (idempotent)

The runtime implementation in :cure_std_crdt is backed by the kernel-checked join-semilattice proofs in Std.CRDT.Proof (idempotence, commutativity, and associativity of nat_max and gcounter2_merge, certified @total true in Core). For general arithmetic and equality combinators, see Std.Proof and Std.Equivalent.

Types included:

  • GCounter -- grow-only counter, increment and value.
  • PNCounter -- positive/negative counter, supports decrement.
  • ORSet(T) -- observed-remove set, add / remove / value.
  • LWWRegister(T) -- last-write-wins register.
  • MVRegister(T) -- multi-value register (set of concurrent writes).

All CRDTs carry a node_id to tag mutations so merges can reconcile conflicting updates deterministically.

Examples

use Std.CRDT

# Two replicas tick concurrently; merge converges without loss.
let a0 = g_increment(g_empty(), :n1, 3)
let b0 = g_increment(g_empty(), :n2, 2)
g_value(g_merge(a0, b0))                # => 5
use Std.CRDT

# Add-then-remove is commutative under concurrent updates.
# Each add on replica :n1 carries a tag :n1 has not used before.
let s0 = or_add(or_empty(), :n1, 1, "x")
let s1 = or_add(s0, :n1, 2, "y")
or_value(or_remove(s1, "x"))            # => ["y"]