psync's schedule and the outside world #
Schedule.lean proves that psync's schedule preserves the client's state. A real program also
talks to servers, and a server may change its state even on a read: a GET may bump a rate-limit
counter, refresh a session or fill a cache. No client-side analysis (tokens, MIR, anything) can see
that. So no tool can promise equivalence outright; what it can promise is equivalence relative to
a model of the server. This file makes that model explicit and proves the schedule against it.
- A reduction kernel (section
Kernel):Schedule.lean's theorems restated for any state type and any independence relation that implies commutation. Nothing in their proofs used more. - The world (section
World): a client store, a server given as an abstract labelled transition system (Server: each request label moves the server to a new state and yields a response), and a log of the response every call received. A commutativity specificationspecsays which requests commute;Soundsays it is true of the server. Independence of two statements is then: disjoint client footprints, distinct calls, and requests the spec says commute. - Theorem
world_levels_correct: if the spec is sound, every concurrent execution of psync's schedule ends with the same client state, the same server state, and the same response for every call as the sequential program. - Reads that write (section
Example): a server whose every request increments a counter. Two lookups commute (the counter ends the same and no response mentions it); a lookup and a read of the counter do not, and the spec in the example says so.
The spec is the only assumption about the server. It is explicit, and it is testable: a server-side log in which two concurrent requests were applied in an order whose result differs is a counterexample to the spec.
lean verify/World.lean # prints nothing when every proof checks
Notation used in the theorem statements #
- $T$ is a transition system (
Sys): $\mathrm{run}_a$ is step $a$, and $a \mathrel{I_T} b$ is $T$'s independence; $[\![ xs ]\!]_T(s)$ runs $xs$ in order. - A world $w = (c, \sigma, \mathit{log})$ is the client state $c$, the server state $\sigma$ and the response of every call so far. $\mathit{step}$ is the server; $\mathit{spec}$ says which calls commute; $\mathrm{Sound}(\mathit{step}, \mathit{spec})$ means every pair the spec allows really commutes at the server (same server state, same responses).
Kernel: reduction for any state and any commuting relation #
- nil {A : Type} : Interleave [] [] []
- left {A : Type} {x : A} {a b m : List A} : Interleave a b m → Interleave (x :: a) b (x :: m)
- right {A : Type} {y : A} {a b m : List A} : Interleave a b m → Interleave a (y :: b) (y :: m)
Instances For
$$(\forall x \in a,\ \forall y \in b,\ x \mathrel{I_T} y) \;\wedge\; m \in a \parallel b \implies [\![ m ]\!]_T(s) = [\![ a \mathbin{+\!\!+} b ]\!]_T(s)$$
- nil {A : Type} : InterleaveAll [] []
- cons {A : Type} {c : List A} {cs : List (List A)} {m r : List A} : InterleaveAll cs r → Interleave c r m → InterleaveAll (c :: cs) m
Instances For
$$m \in a \parallel b \implies \forall z \in m,\ z \in a \vee z \in b$$
$$\mathrm{PairwiseInd}_T(cs) \;\wedge\; m \in \textstyle\Vert_{c \in cs}\, c \implies [\![ m ]\!]_T(s) = [\![ c_1 \mathbin{+\!\!+} \cdots \mathbin{+\!\!+} c_k ]\!]_T(s)$$
- nil {S A : Type} {T : Sys S A} : T.Runs [] []
- cons {S A : Type} {T : Sys S A} {cs : List (List A)} {m : List A} {levels : List (List (List A))} {ms : List (List A)} : T.PairwiseInd cs → InterleaveAll cs m → T.Runs levels ms → T.Runs (cs :: levels) (m :: ms)
Instances For
$$\mathrm{Runs}_T(\mathit{levels}, ms) \implies [\![ \mathrm{flatten}(ms) ]\!]_T(s) = [\![ \mathrm{flatten}(\mathrm{map}\ \mathrm{flatten}\ \mathit{levels}) ]\!]_T(s)$$
Any execution of a schedule of levels equals running every chain in order.
World: client, server, and the response of every call #
A server as a labelled transition system: a request l takes state s to (step l s).1 and
answers (step l s).2. Nothing is assumed about which requests change the state: a read that
writes (a counter, a session refresh, a cache fill) is an ordinary transition.
Equations
- PsyncWorld.Server L Rsp σ = (L → σ → σ × Rsp)
Instances For
Requests a and b commute at the server: in either order, the server ends in the same state
and each request gets the same response.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A commutativity specification is sound for a server when every pair it allows commutes. This is the one assumption about the outside world.
Equations
- PsyncWorld.Sound step spec = ∀ (a b : L), spec a b → PsyncWorld.Commutes step a b
Instances For
A statement of the client program. It reads R and writes W in the client. It may send one
request, computed from what it reads (req), and then updates the client from the response
(upd). id names the call in the log.
- id : Nat
- R : Loc → Prop
- W : Loc → Prop
- req : (Loc → Val) → Option L
- upd : Option Rsp → (Loc → Val) → Loc → Val
Instances For
Independent relative to spec: disjoint client footprints (as in Schedule.lean), distinct
calls, and whatever requests the two may send are ones the spec says commute.
Equations
- One or more equations did not get rendered due to their size.
Instances For
$$(\forall l,\ l \in W_b \Rightarrow l \notin R_a) \implies \mathrm{req}_a\bigl(\mathrm{upd}_b(o, c)\bigr) = \mathrm{req}_a(c)$$
b's update does not change what a reads, so a sends the same request.
$$a \mathrel{I} b \implies \mathrm{upd}_b\bigl(o_b, \mathrm{upd}_a(o_a, c)\bigr) = \mathrm{upd}_a\bigl(o_a, \mathrm{upd}_b(o_b, c)\bigr)$$
The client updates of two statements with disjoint footprints commute, whatever the
responses (commute of Schedule.lean).
$$\mathrm{Sound}(\mathit{step}, \mathit{spec}) \;\wedge\; a \mathrel{I_{\mathit{spec}}} b \implies \mathrm{run}_b(\mathrm{run}_a(w)) = \mathrm{run}_a(\mathrm{run}_b(w)) \quad \text{for every world } w = (c, \sigma, \mathit{log})$$
Independent statements commute in the world: same client state, same server state, same response for every call. The server part is exactly the spec's soundness.
The world as a Sys: psync's statements, run against a server whose spec is sound.
Equations
- PsyncWorld.worldSys step spec hs = { run := PsyncWorld.COp.run step, Ind := PsyncWorld.CIndep spec, comm := ⋯ }
Instances For
$$\mathrm{Sound}(\mathit{step}, \mathit{spec}) \;\wedge\; \mathrm{Runs}(\mathit{levels}, ms) \implies [\![ \mathrm{flatten}(ms) ]\!](w) = [\![ \mathrm{flatten}(\mathrm{map}\ \mathrm{flatten}\ \mathit{levels}) ]\!](w)$$
The theorem. If the commutativity specification is sound for the server, then whatever interleaving each level of psync's schedule takes, the run ends exactly as the sequential program does: the same client state, the same server state, and the same response for every call.
$$\mathrm{Sound}\bigl(\mathit{step},\ \lambda\,a\,b.\ \bot\bigr) \quad \text{for every server } \mathit{step}$$
With no specification at all (spec := fun _ _ => False, psync's default for an unknown
service: every two calls to it conflict), the theorem holds for every server: only statements that
send no request, or send none to that server, may overlap.
Example: reads that write #
The spec: two gets commute. Nothing involving count does, because count's answer sees
every other request: both are "reads", and they still do not commute.
Equations
- PsyncWorld.kvSpec (PsyncWorld.Req.get k) (PsyncWorld.Req.get k_1) = True
- PsyncWorld.kvSpec x✝¹ x✝ = False
Instances For
$$\mathrm{Sound}(\mathrm{kv}, \mathrm{kvSpec})$$
psync's built-in rule, as a spec #
Without user input, psync classifies each call by its name (effects.rs) or, with compiler facts,
by its body: a service, an access (read, write, or ordered write) and a target when one is known.
Two calls commute unless one writes, they are on the same service, and their targets may be the
same (an unknown target may be any). That rule is a commutativity specification, and the
theorem above holds exactly when it is sound for the servers involved. Its weakest clause is
"two reads always commute": a read that changes server state breaks it.
psync's conflict rule, negated: when psync lets two calls overlap.
Equations
- One or more equations did not get rendered due to their size.
Instances For
How psync's names classify the example's requests: get_value(k) and fetch_count() are both
reads of the one service (get, fetch are read verbs); only get has a known target.
Equations
- PsyncWorld.kvEff (PsyncWorld.Req.get k) = { svc := 0, acc := PsyncWorld.Access.read, target := some k }
- PsyncWorld.kvEff PsyncWorld.Req.count = { svc := 0, acc := PsyncWorld.Access.read, target := none }
Instances For
$$\neg\,\mathrm{Sound}\bigl(\mathrm{kv},\ \lambda\,a\,b.\ \mathrm{psyncSpec}(\mathrm{eff}(a), \mathrm{eff}(b))\bigr)$$
The professor's point, proved. For a server whose reads change its state, psync's
built-in rule is not sound: it lets a get and a count overlap, and they do not commute. So
the guarantee is relative to the spec, and a counting server needs a spec that says so (here
kvSpec, or #[psync::seq], or effects(write(fetch_count))).