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 intended to satisfy those laws, but Cure does not yet emit trusted Core proof obligations for CRDTs. For genuine kernel-checked equality proofs, see Std.Proof (arithmetic laws over Std.Nat) and Std.Equivalent (the propositional-equality combinators).

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

cure
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
cure
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"]

Group tag consumed by Cure.Stdlib.Preload. Grow-only counter. Per-replica counts are summed on read.

Functions

  • # fn g_empty() -> GCounter extern

    Empty GCounter.

  • # fn g_increment(replica: Type, c: GCounter, node: replica, by: Int) -> GCounter extern

    Increment c by by on replica node (positive numbers only).

  • # fn g_merge(a: GCounter, b: GCounter) -> GCounter extern

    Merge two GCounters by taking the per-replica max.

  • # fn g_value(c: GCounter) -> Int extern

    Current value of the GCounter: sum of per-replica counts.

  • # fn lww_empty(replica: Type, node: replica) -> LWWRegister extern

    Empty LWWRegister bound to node.

  • # fn lww_merge(a: LWWRegister, b: LWWRegister) -> LWWRegister extern

    Merge by selecting the side with the higher timestamp; ties break on node id.

  • # fn lww_set(replica: Type, r: LWWRegister, value: t, stamp: Int, node: replica) -> LWWRegister extern

    Write value with timestamp stamp from replica node.

  • # fn lww_value(r: LWWRegister) -> Option(t) extern

    Current value of the register, or None() if nothing has been written. An empty register has no t to return; it used to hand back the atom :empty under a declared -> t.

  • # fn mv_empty() -> MVRegister extern

    Empty MVRegister.

  • # fn mv_merge(a: MVRegister, b: MVRegister) -> MVRegister extern

    Merge by taking the union of non-dominated writes.

  • # fn mv_values(r: MVRegister) -> List(t) extern

    List of currently-live values.

  • # fn mv_write(replica: Type, r: MVRegister, value: t, stamp: Int, node: replica) -> MVRegister extern

    Write value on replica node with timestamp stamp; concurrent writes are retained.

  • # fn or_add(replica: Type, s: ORSet, node: replica, tag: Int, elem: t) -> ORSet extern

    Add elem to the set on replica node, identifying the addition with tag so it survives a concurrent remove. tag must never be reused for the same node — see lww_set's stamp.

  • # fn or_empty() -> ORSet extern

    Empty ORSet.

  • # fn or_merge(a: ORSet, b: ORSet) -> ORSet extern

    Merge two ORSets via tag union.

  • # fn or_remove(s: ORSet, elem: t) -> ORSet extern

    Remove elem from the set; only removes tags this replica has observed, so concurrent re-adds from other replicas are retained.

  • # fn or_value(s: ORSet) -> List(t) extern

    List of current elements.

  • # fn pn_decrement(replica: Type, c: PNCounter, node: replica, by: Int) -> PNCounter extern

    Decrement by by on replica node.

  • # fn pn_empty() -> PNCounter extern

    Empty PNCounter.

  • # fn pn_increment(replica: Type, c: PNCounter, node: replica, by: Int) -> PNCounter extern

    Increment by by on replica node.

  • # fn pn_merge(a: PNCounter, b: PNCounter) -> PNCounter extern

    Merge two PNCounters component-wise.

  • # fn pn_value(c: PNCounter) -> Int extern

    Current value: sum(p) - sum(n).