Documentation

World

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.

  1. 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.
  2. 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 specification spec says which requests commute; Sound says it is true of the server. Independence of two statements is then: disjoint client footprints, distinct calls, and requests the spec says commute.
  3. 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.
  4. 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 #

Kernel: reduction for any state and any commuting relation #

structure PsyncWorld.Sys (S A : Type) :

A system: steps A acting on states S, with an independence relation under which steps commute.

  • run : A → S → S
  • Ind : A → A → Prop
  • comm (a b : A) : self.Ind a b → ∀ (s : S), self.run b (self.run a s) = self.run a (self.run b s)
Instances For
    def PsyncWorld.Sys.runAll {S A : Type} (T : Sys S A) :
    List A → S → S
    Equations
    Instances For
      theorem PsyncWorld.Sys.runAll_append {S A : Type} (T : Sys S A) (xs ys : List A) (s : S) :
      T.runAll (xs ++ ys) s = T.runAll ys (T.runAll xs s)

      $$[\![ xs \mathbin{+\!\!+} ys ]\!]_T(s) = [\![ ys ]\!]_T\bigl([\![ xs ]\!]_T(s)\bigr)$$

      theorem PsyncWorld.Sys.commute_list {S A : Type} (T : Sys S A) (y : A) (xs : List A) (h : ∀ (x : A), x ∈ xs → T.Ind x y) (s : S) :
      T.runAll xs (T.run y s) = T.run y (T.runAll xs s)

      $$(\forall x \in xs,\ x \mathrel{I_T} y) \implies [\![ xs ]\!]_T(\mathrm{run}_y(s)) = \mathrm{run}_y\bigl([\![ xs ]\!]_T(s)\bigr)$$

      inductive PsyncWorld.Interleave {A : Type} :
      List A → List A → List A → Prop
      Instances For
        theorem PsyncWorld.Sys.interleave_correct {S A : Type} (T : Sys S A) {a b m : List A} (hab : ∀ (x : A), x ∈ a → ∀ (y : A), y ∈ b → T.Ind x y) (hm : Interleave a b m) (s : S) :
        T.runAll m s = T.runAll (a ++ b) s

        $$(\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)$$

        inductive PsyncWorld.InterleaveAll {A : Type} :
        List (List A) → List A → Prop
        Instances For
          theorem PsyncWorld.interleave_mem {A : Type} {a b m : List A} (hi : Interleave a b m) (z : A) :
          z ∈ m → z ∈ a ∨ z ∈ b

          $$m \in a \parallel b \implies \forall z \in m,\ z \in a \vee z \in b$$

          theorem PsyncWorld.interleaveAll_mem {A : Type} {cs : List (List A)} {m : List A} (h : InterleaveAll cs m) (y : A) :
          y ∈ m → ∃ (c : List A), c ∈ cs ∧ y ∈ c

          $$m \in \textstyle\Vert_{c \in cs}\, c \implies \forall y \in m,\ \exists c \in cs,\ y \in c$$

          def PsyncWorld.Sys.PairwiseInd {S A : Type} (T : Sys S A) :
          List (List A) → Prop
          Equations
          Instances For
            theorem PsyncWorld.Sys.level_correct {S A : Type} (T : Sys S A) {cs : List (List A)} {m : List A} (hp : T.PairwiseInd cs) (hm : InterleaveAll cs m) (s : S) :
            T.runAll m s = T.runAll cs.flatten s

            $$\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)$$

            inductive PsyncWorld.Sys.Runs {S A : Type} (T : Sys S A) :
            List (List (List A)) → List (List A) → Prop
            Instances For
              theorem PsyncWorld.Sys.levels_correct {S A : Type} (T : Sys S A) {levels : List (List (List A))} {ms : List (List A)} (h : T.Runs levels ms) (s : S) :

              $$\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.

              inductive PsyncWorld.Sys.Reorders {S A : Type} (T : Sys S A) :
              List A → List A → Prop
              Instances For
                theorem PsyncWorld.Sys.reorders_correct {S A : Type} (T : Sys S A) {P L : List A} (h : T.Reorders P L) (s : S) :
                T.runAll L s = T.runAll P s

                $$\mathrm{Reorders}_T(P, L) \implies [\![ L ]\!]_T(s) = [\![ P ]\!]_T(s)$$

                Moving steps earlier only past independent ones preserves the result.

                World: client, server, and the response of every call #

                @[reducible, inline]
                abbrev PsyncWorld.Server (L Rsp σ : Type) :

                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
                Instances For
                  def PsyncWorld.Commutes {L Rsp σ : Type} (step : Server L Rsp σ) (a b : L) :

                  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
                    def PsyncWorld.Sound {L Rsp σ : Type} (step : Server L Rsp σ) (spec : L → L → Prop) :

                    A commutativity specification is sound for a server when every pair it allows commutes. This is the one assumption about the outside world.

                    Equations
                    Instances For
                      structure PsyncWorld.World (Loc Val Rsp σ : Type) :

                      What the program and the world consist of: the client's store, the server's state, and the response each call (by its id) received.

                      • c : Loc → Val
                      • s : σ
                      • log : Nat → Option Rsp
                      Instances For
                        structure PsyncWorld.COp (Loc Val L Rsp : Type) :

                        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
                        • frame (o : Option Rsp) (c : Loc → Val) (l : Loc) : ¬self.W l → self.upd o c l = c l
                        • req_dep (c c' : Loc → Val) : (∀ (l : Loc), self.R l → c l = c' l) → self.req c = self.req c'
                        • upd_dep (o : Option Rsp) (c c' : Loc → Val) : (∀ (l : Loc), self.R l → c l = c' l) → ∀ (l : Loc), self.W l → self.upd o c l = self.upd o c' l
                        Instances For
                          def PsyncWorld.record {Rsp : Type} (log : Nat → Option Rsp) (i : Nat) (r : Rsp) :
                          Nat → Option Rsp

                          Record response r for call i.

                          Equations
                          Instances For
                            def PsyncWorld.COp.run {Loc Val L Rsp σ : Type} (step : Server L Rsp σ) (o : COp Loc Val L Rsp) (w : World Loc Val Rsp σ) :
                            World Loc Val Rsp σ
                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              def PsyncWorld.CIndep {Loc Val L Rsp : Type} (spec : L → L → Prop) (a b : COp Loc Val L Rsp) :

                              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
                                theorem PsyncWorld.req_stable {Loc Val L Rsp : Type} (a b : COp Loc Val L Rsp) (h : ∀ (l : Loc), b.W l → ¬a.R l) (o : Option Rsp) (c : Loc → Val) :
                                a.req (b.upd o c) = a.req c

                                $$(\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.

                                theorem PsyncWorld.upd_comm {Loc Val L Rsp : Type} (a b : COp Loc Val L Rsp) (h : (∀ (l : Loc), a.W l → ¬b.R l ∧ ¬b.W l) ∧ ∀ (l : Loc), b.W l → ¬a.R l) (oa ob : Option Rsp) (c : Loc → Val) :
                                b.upd ob (a.upd oa c) = a.upd oa (b.upd ob c)

                                $$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).

                                theorem PsyncWorld.record_comm {Rsp : Type} (log : Nat → Option Rsp) {i j : Nat} (hij : i ≠ j) (ri rj : Rsp) :
                                record (record log i ri) j rj = record (record log j rj) i ri

                                $$i \neq j \implies \mathit{log}[i \mapsto r_i][j \mapsto r_j] = \mathit{log}[j \mapsto r_j][i \mapsto r_i]$$

                                theorem PsyncWorld.world_commute {Loc Val L Rsp σ : Type} (step : Server L Rsp σ) (spec : L → L → Prop) (hs : Sound step spec) (a b : COp Loc Val L Rsp) (h : CIndep spec a b) (w : World Loc Val Rsp σ) :
                                COp.run step b (COp.run step a w) = COp.run step a (COp.run step b w)

                                $$\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.

                                def PsyncWorld.worldSys {Loc Val L Rsp σ : Type} (step : Server L Rsp σ) (spec : L → L → Prop) (hs : Sound step spec) :
                                Sys (World Loc Val Rsp σ) (COp Loc Val L Rsp)

                                The world as a Sys: psync's statements, run against a server whose spec is sound.

                                Equations
                                Instances For
                                  theorem PsyncWorld.world_levels_correct {Loc Val L Rsp σ : Type} (step : Server L Rsp σ) (spec : L → L → Prop) (hs : Sound step spec) {levels : List (List (List (COp Loc Val L Rsp)))} {ms : List (List (COp Loc Val L Rsp))} (h : (worldSys step spec hs).Runs levels ms) (w : World Loc Val Rsp σ) :
                                  (worldSys step spec hs).runAll ms.flatten w = (worldSys step spec hs).runAll (List.map List.flatten levels).flatten w

                                  $$\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.

                                  theorem PsyncWorld.empty_spec_sound {L Rsp σ : Type} (step : Server L Rsp σ) :
                                  Sound step fun (x x : L) => False

                                  $$\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 #

                                  inductive PsyncWorld.Req :

                                  A key-value server that counts every request (a rate limiter): get k returns the value and bumps the counter; count returns the counter and bumps it too.

                                  Instances For
                                    structure PsyncWorld.KV :
                                    Instances For
                                      Equations
                                      Instances For

                                        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
                                        Instances For

                                          $$\mathrm{Sound}(\mathrm{kv}, \mathrm{kvSpec})$$

                                          $$\neg\,\mathrm{Commutes}_{\mathrm{kv}}(\mathrm{get}\ 0,\ \mathrm{count})$$

                                          A get and a count really do not commute: count's answer depends on whether the get came first. A spec that allowed them would be unsound, and the theorem would not apply.

                                          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.

                                          Instances For
                                            structure PsyncWorld.Eff :

                                            How psync describes a call: service, access, and target (none: unknown).

                                            Instances For

                                              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
                                                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))).

                                                  $$\forall k\,k',\ \mathrm{Commutes}_{\mathrm{kv}}(\mathrm{get}\ k,\ \mathrm{get}\ k')$$

                                                  And it is sound when the server's reads do not change what other requests see: two gets.