Replicated Counters and Sets (GCounter, PNCounter, TwoPhaseSet, ORSet) & Impossibility #
This module formalizes concrete state-based (CvRDT) and operation-based (CmRDT) data types
together with their functional specifications and untagged set impossibility steps toward #7502
(after Shapiro, Preguiça, Baquero, and Zawirski [SPBZ11b], §§4.1–4.2,
pp. 11–12, and [SPBZ11a], §§3.1, 3.3, Specifications 6, 7, 11, 12, 15,
pp. 14–16, 21–27):
GCounter P(Grow-Only Counter, Specification 6): Per-replica vectorcounts : P → ℕwith pointwisemaxsemilattice merge and query∑ p : P, counts p, provingSemilatticeSup/OrderBot,CvRDTandCmRDTinstances, monotonicity ofGCounter.value, and exact multi-replica global sum convergence (GCounter.value_sup_of_local_counts, avoiding the lost-update anomaly of merging a single scalar counter viamax).PNCounter P(Positive-Negative Counter, Specification 7): Paired(pos, neg)GCounters evaluating to(pos.value : ℤ) - (neg.value : ℤ), proving unconditional commutativity of increments and decrements and exact net-count evaluation.Grow-Only Set (
gsetCvRDT,gsetCmRDT) andTwoPhaseSet α(2P-Set, Specification 12):(added, removed)pair ofFinset αs with queryadded \ removed, provingCvRDTandCmRDTconvergence, concurrent remove-wins (TwoPhaseSet.remove_wins_concurrent), and monotonic tombstone permanence (TwoPhaseSet.not_mem_elements_of_le_of_removed: once tombstoned, an element can never re-enterelementsat any semilattice successor).ORSet(Observed-Remove Set with UniqueP × ℕTags, Specification 15 & Figure 14):- State-based
ORSetCv P α(CvRDT):(entries, tombstones)overTaggedElem P α, whereadd p seq xinserts fresh⟨x, p, seq⟩andremoveObserved xtombstones only currently observed tags ofx, proving both concurrent add-wins (ORSetCv.mem_sup_removeObserved_add) and sequential re-addition after removal (ORSetCv.mem_add_after_removeObserved). - Op-based
orsetCmRDT(CmRDT) & Causal Add-Wins Theorem: over any valid causal history (ORSetCausalHistory) whereaddallocates unique(replica, seq)tags andremove(x)collects the tags ofxpresent in its source replica's causally delivered state, concurrent downstream effects commute (ORSetCausalHistory.concurrentCommute) and every causal linearization evaluates toorsetSurvivingTags(ORSetCausalHistory.foldl_eq_survivingTags), satisfying the exact Causal Add-Wins Specification (ORSetCausalHistory.mem_elements_iff_causal_add_wins):x ∈ orsetElements state ↔ ∃ e_add ∈ H, (∃ p n, opOf e_add = .add p n x) ∧ ¬ ∃ e_rem ∈ H, opOf e_rem = .remove x ∧ prec e_add e_rem.
- State-based
Untagged Set Impossibility (
not_exists_untagged_finset_merge,untagged_insert_erase_ne_erase_insert): Proves that no commutative binary mergeFinset α → Finset α → Finset αon plain untagged sets ([Nonempty α]) can simultaneously propagate a freshaddto an untouched peer at∅(x ∈ merge ∅ (insert x ∅)) and propagate a sequentialremove-after-addto a peer still holding{x}(x ∉ merge {x} (({x} : Finset α).erase x)), because both executions present the same unordered pair of untagged states{∅, {x}}, alongside op-based non-commutativity of naïveinsertanderase([SPBZ11a], §3.3 Figure 11, p. 21).
Main definitions #
Cslib.Distributed.CRDT.GCounter: state-based / op-based grow-only counter (P → ℕ).Cslib.Distributed.CRDT.PNCounter,Cslib.Distributed.CRDT.PNCounterOp: paired increment/decrement counter evaluating inℤ.Cslib.Distributed.CRDT.gsetCvRDT,Cslib.Distributed.CRDT.gsetCmRDT: grow-only set CRDTs onFinset α.Cslib.Distributed.CRDT.SetOp,Cslib.Distributed.CRDT.TwoPhaseSet: two-phase set(added, removed)with permanent tombstones.Cslib.Distributed.CRDT.TaggedElem,Cslib.Distributed.CRDT.ORSetOp,Cslib.Distributed.CRDT.ORSetCv,Cslib.Distributed.CRDT.ORSetEff,Cslib.Distributed.CRDT.orsetEffect,Cslib.Distributed.CRDT.orsetElements,Cslib.Distributed.CRDT.orsetCmRDT,Cslib.Distributed.CRDT.orsetSurvivingTags,Cslib.Distributed.CRDT.ORSetCausalHistory: state-based and op-based Observed-Remove Set.
Main results #
Cslib.Distributed.CRDT.GCounter.cmrdt_effectsCommute,Cslib.Distributed.CRDT.GCounter.mono_value, andCslib.Distributed.CRDT.GCounter.value_sup_of_local_counts:GCountercommutativity, monotonicity, and exact global sum convergence.Cslib.Distributed.CRDT.PNCounter.cmrdt_effectsCommuteandCslib.Distributed.CRDT.PNCounter.value_sup_of_local_counts:PNCountercommutativity and exact global difference convergence.Cslib.Distributed.CRDT.TwoPhaseSet.not_mem_elements_of_le_of_removedandCslib.Distributed.CRDT.TwoPhaseSet.remove_wins_concurrent: permanent tombstones and concurrent remove-wins inTwoPhaseSet.Cslib.Distributed.CRDT.ORSetCv.mem_sup_removeObserved_addandCslib.Distributed.CRDT.ORSetCv.mem_add_after_removeObserved: state-basedORSetCvconcurrent add-wins and sequential re-add after remove.Cslib.Distributed.CRDT.ORSetCausalHistory.concurrentCommute,Cslib.Distributed.CRDT.ORSetCausalHistory.foldl_eq_survivingTags, andCslib.Distributed.CRDT.ORSetCausalHistory.mem_elements_iff_causal_add_wins: op-basedOR-Setconcurrent commutativity and exact causal add-wins specification step.Cslib.Distributed.CRDT.not_exists_untagged_finset_mergeandCslib.Distributed.CRDT.untagged_insert_erase_ne_erase_insert: untaggedFinsetmerge impossibility and op-basedinsert/erasenon-commutativity.
References #
Increment replica p's entry by 1.
Monotonicity of GCounter.value with respect to the semilattice order.
State-based CvRDT instance for GCounter P.
Op-based CmRDT instance for GCounter P where replica p broadcasts p to increment slot
p downstream.
Exact multi-replica sum specification step (toward #7502, after
[SPBZ11a], §3.1.2, Specification 6, pp. 15–16): when each replica p
holds its own originated count (rep p).counts p = k p and no replica over-counts any peer
((rep q).counts p ≤ k p), the global semilattice merge evaluates to ∑ p : P, k p.
Update operations on a PNCounter P: increment or decrement at source replica p.
- inc {P : Type u_1} (p : P) : PNCounterOp P
Increment originated at replica
p. - dec {P : Type u_1} (p : P) : PNCounterOp P
Decrement originated at replica
p.
Increment pos at replica p.
Increment neg (decrementing net value) at replica p.
Apply a PNCounterOp to c.
State-based CvRDT instance for PNCounter P.
Op-based CmRDT instance for PNCounter P.
Exact multi-replica net difference specification step (toward #7502, after [SPBZ11a], §3.1.3, Specification 7, pp. 15–16).
GSet and TwoPhaseSet (2P-Set) #
State-based Grow-Only Set (G-Set, [SPBZ11a], §3.3.1,
Specification 11, p. 22) over Mathlib's SemilatticeSup (Finset α).
Op-based Grow-Only Set (G-Set, [SPBZ11a], §3.3.1, pp. 21–22).
Initial empty TwoPhaseSet.
Add x to s.added.
Tombstone x in s.removed.
Apply SetOp to TwoPhaseSet.
State-based CvRDT instance for TwoPhaseSet α.
Op-based CmRDT instance for TwoPhaseSet α.
Monotonic tombstone permanence step (toward #7502, after
[SPBZ11a], §3.3.2, pp. 22–23): once x ∈ s.removed, x remains
tombstoned and absent from s'.elements in every semilattice successor s' ≥ s (including after
any subsequent add x or merge).
Remove-wins under concurrent add x and remove x in TwoPhaseSet α.
ORSet: Observed-Remove Set with Unique (P × ℕ) Tags #
State-based Observed-Remove Set (ORSetCv, [SPBZ11a], §3.3.5,
p. 27) maintaining entries and tombstones over TaggedElem P α.
- entries : Finset (TaggedElem P α)
All tagged entries ever added.
- tombstones : Finset (TaggedElem P α)
Tagged entries explicitly observed and tombstoned by
remove.
Instances For
Initial empty state-based ORSetCv.
Visible set elements (s.entries \ s.tombstones).image TaggedElem.elem.
Add x with tag (p, seq) to s.entries.
Remove x by tombstoning only the tags of x currently observed in s.entries.
State-based CvRDT instance for ORSetCv P α.
State-based concurrent add-wins step (toward #7502, after
[SPBZ11a], §3.3.5, Figure 14, p. 27): if s₂ adds x with a fresh tag
⟨x, p, seq⟩ not observed at s₁ and not already tombstoned, merging s₁.removeObserved x with
s₂.add p seq x retains x.
State-based sequential re-add after remove: unlike TwoPhaseSet, adding x with a fresh tag
⟨x, p, seq⟩ after s.removeObserved x restores x ∈ elements.
Downstream effect payloads of the op-based Observed-Remove Set (OR-Set,
[SPBZ11a], §3.3.5, Specification 15, p. 26).
- addTag {P : Type u_1} {α : Type u_2} (t : TaggedElem P α) : ORSetEff P α
Downstream insertion of a uniquely tagged entry
t. - removeTags {P : Type u_1} {α : Type u_2} (R : Finset (TaggedElem P α)) : ORSetEff P α
Downstream removal of the finite set
Rof tagged entries observed at the source replica.
Downstream state transformer of the op-based OR-Set on Finset (TaggedElem P α).
Query extracting visible α elements from an op-based OR-Set payload.
Op-based CmRDT instance for the Observed-Remove Set (OR-Set, Specification 15,
[SPBZ11a], §3.3.5, p. 26).
Set of tagged entries surviving in causal history H under causal precedence prec: tagged
entries ⟨x, p, seq⟩ generated by an add p seq x event e ∈ H not causally followed (prec)
by any remove x event r ∈ H.
Well-formed finite causally closed OR-Set execution history H : Finset Ev under strict
partial order prec ([SPBZ11a], §3.3.5, Specification 15, p. 26):
His causally closed (IsCausallyClosed prec H);- distinct
addevents inHallocate distinct(replica, seq)tags (unique_add_tag); - each
add p n xprepares.addTag ⟨x, p, n⟩; and - each
remove xevente ∈ Hprepares.removeTagsfrom the tags ofxpresent in its source replica's state after some causal linearizationl_srcofe's causal pastH.filter (fun u => prec u e).
- closed : IsCausallyClosed prec H
Causal closure of
H. - opOf : Ev → ORSetOp P α
Client operation invoked at each event.
- effOf : Ev → ORSetEff P α
Downstream effect prepared at the source of each event.
- unique_add_tag (e₁ : Ev) : e₁ ∈ H → ∀ e₂ ∈ H, ∀ (p₁ : P) (n₁ : ℕ) (x₁ : α) (p₂ : P) (n₂ : ℕ) (x₂ : α), self.opOf e₁ = ORSetOp.add p₁ n₁ x₁ → self.opOf e₂ = ORSetOp.add p₂ n₂ x₂ → p₁ = p₂ → n₁ = n₂ → e₁ = e₂
Uniqueness of
(replica, seq)tags acrossaddevents inH. - eff_add (e : Ev) : e ∈ H → ∀ (p : P) (n : ℕ) (x : α), self.opOf e = ORSetOp.add p n x → self.effOf e = ORSetEff.addTag { elem := x, replica := p, seq := n }
Prepare specification for
addevents. - eff_remove (e : Ev) : e ∈ H → ∀ (x : α), self.opOf e = ORSetOp.remove x → ∃ (l_src : List Ev), IsCausalLinearization prec ({u ∈ H | prec u e}) l_src ∧ self.effOf e = ORSetEff.removeTags ({t ∈ List.foldl (fun (s : Finset (TaggedElem P α)) (u : Ev) => orsetEffect (self.effOf u) s) ∅ l_src | t.elem = x})
Prepare specification for
removeevents against the source replica's delivered causal pastH.filter (fun u => prec u e).
Op-based OR-Set concurrent commutativity step (toward #7502, after
[SPBZ11a], §3.3.5, Specification 15, pp. 26–27): in any well-formed
causal history hH, concurrent downstream OR-Set effects commute.
Op-based OR-Set exact tag-state evaluation step (toward #7502, after
[SPBZ11a], §3.3.5, Specification 15, pp. 26–27): every causal
linearization l of H evaluates to orsetSurvivingTags prec hH.opOf H.
Op-based OR-Set Causal Add-Wins Specification step (toward #7502, after
[SPBZ11a], §3.3.5, Specification 15 & Figure 14, pp. 26–27): after any
causal linearization l of H, element x belongs to orsetElements iff H contains an
add p n x event e_add that is not causally followed (prec) by any remove x event
e_rem ∈ H.
Untagged Set Merge Impossibility and Naïve Op-Based Non-Commutativity #
Untagged set state-merge impossibility (original to this library, formalizing the untagged
add/remove conflict motivation of [SPBZ11a], §3.3, p. 21):
over any nonempty element type α, there is no binary commutative merge operation
merge : Finset α → Finset α → Finset α on plain untagged sets that simultaneously:
- propagates a fresh
add(insert x ∅ = {x}) to an untouched peer at∅(∀ x, x ∈ merge ∅ (insert x ∅)), and - propagates a sequential
remove-after-add(({x} : Finset α).erase x = ∅) to a peer still holding{x}(∀ x, x ∉ merge {x} (({x} : Finset α).erase x)), because without per-addition tags or tombstones both executions yield the same unordered pair of replica states{∅, {x}}.