Documentation

LeanPool.OrderMaintenance.Impl.RebuildM

M6: the rebuild loop as machine code #

loopC p runs Level F's rebuildLoop om nxt (K_B + 1) K_B, with cur in slot p and nxt in slot 1 - p. It is a for loop of K_B + 1 iterations; the local bud holds the budget, and an iteration with no budget left does nothing. The swap sets the budget to 0, so the iterations after it do nothing either (the parity has changed, but they only read bud).

What each iteration needs from the run (the preconditions of the core calls) is collected along Level F's run by RLSafe.

Rules that forget the intermediate state #

theorem OrderMaintenance.Impl.wp_ite_g {c : Machine.Expr} {t f : Machine.Stmt} {Q : Machine.State → ℕ → Prop} {s : Machine.State} (v : ℕ) (hc : Machine.Expr.eval s c = some v) (h : ∀ (s' : Machine.State), s'.loc = s.loc → s'.mem = s.mem → s'.cap = s.cap → Machine.wp (if v ≠ 0 then t else f) (fun (s'' : Machine.State) (n : ℕ) => Q s'' (1 + c.cost + n)) s') :
theorem OrderMaintenance.Impl.wp_assign_g {x : Machine.Reg} {e : Machine.Expr} {Q : Machine.State → ℕ → Prop} {s : Machine.State} (v : ℕ) (he : Machine.Expr.eval s e = some v) (h : ∀ (s' : Machine.State), s'.loc = Machine.State.upd s.loc x v → s'.mem = s.mem → s'.cap = s.cap → Q s' (1 + e.cost)) :
Machine.wp (x ::= e) Q s
theorem OrderMaintenance.Impl.wp_store_g {a : Machine.Arr} {i e : Machine.Expr} {Q : Machine.State → ℕ → Prop} {s : Machine.State} (j v : ℕ) (hi : Machine.Expr.eval s i = some j) (he : Machine.Expr.eval s e = some v) (hj : j < s.cap a) (h : ∀ (s' : Machine.State), s'.loc = s.loc → s'.mem = (s.write a j v).mem → s'.cap = s.cap → Q s' (1 + i.cost + e.cost)) :

The code #

A preparation step of nxt; r = whether it is ready.

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    Copy the element at the cursor into nxt if it is live, and advance the cursor.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      The end of the rebuild: the swap, and no budget left.

      Equations
      Instances For

        Spend one unit of budget.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For

          One iteration of the rebuild loop.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For

            The rebuild loop: K_B + 1 iterations with budget K_B.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For

              What the iterations need #

              What a copy step needs: the cursor is an element of cur, and if it is live, nxt is in service with the anchor tail, and the handle is below the counter.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                What the iterations of Level F's run need, along the run.

                Equations
                Instances For
                  theorem OrderMaintenance.Impl.rebuildLoop_zero (om : Model.OrderMaintenance) (n : Model.Core) (f : ℕ) :
                  Model.rebuildLoop om n f 0 = Except.ok { cur := om.cur, curIdx := om.curIdx, nxt := some n, nxtIdx := om.nxtIdx, cursor := om.cursor, cursorSet := om.cursorSet, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB }

                  A preparation step #

                  theorem OrderMaintenance.Impl.prepW_ok {om : Model.OrderMaintenance} {m : Machine.State} {p : ℕ} (h : WEnc om m p) {n : Model.Core} (hn : om.nxt = some n) (hM : PrepM n) (hN1 : 1 ≤ n.P.N) {r : Bool} {n' : Model.Core} (hr : EStateM.run Model.prepareStep n = EStateM.Result.ok r n') (hcnt : n'.count = n.count) :
                  Machine.wp (prepC p) (fun (m' : Machine.State) (k : ℕ) => WEnc { cur := om.cur, curIdx := om.curIdx, nxt := some n', nxtIdx := om.nxtIdx, cursor := om.cursor, cursorSet := om.cursorSet, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB } m' p ∧ m'.loc "r" = b2n r ∧ (∀ (x : String), x ≠ "r" → m'.loc x = m.loc x) ∧ k ≤ 450) m

                  A copy step #

                  theorem OrderMaintenance.Impl.eval_load_var {s : Machine.State} {a : Machine.Arr} {x : Machine.Reg} {j v : ℕ} (hx : s.loc x = j) (hj : j < s.cap a) (hv : s.mem a j = v) :
                  @[irreducible]

                  The insertion bound of a core of the proof parameters (it depends only on the Ks).

                  Equations
                  Instances For
                    theorem OrderMaintenance.Impl.eval_load {s : Machine.State} {a : Machine.Arr} {i : Machine.Expr} {j v : ℕ} (hi : Machine.Expr.eval s i = some j) (hj : j < s.cap a) (hv : s.mem a j = v) :
                    theorem OrderMaintenance.Impl.copyEnd {W : Model.OrderMaintenance} {s : Machine.State} {p : ℕ} (h : WEnc W s p) {c : ℕ} (hc : s.loc "c" = c) (hcE : c < W.cur.E.size) {Q : Machine.State → ℕ → Prop} (hQ : ∀ (s' : Machine.State), WEnc { cur := W.cur, curIdx := W.curIdx, nxt := W.nxt, nxtIdx := W.nxtIdx, cursor := (W.cur.E.getX c).next, cursorSet := W.cursorSet, live := W.live, nextHandle := W.nextHandle, rebuilds := W.rebuilds, KB := W.KB } s' p → s'.loc = s.loc → Q s' (1 + (Machine.Expr.lit 0).cost + (arrF p (Fld.e EF.next))[[Machine.Expr.var "c"]].cost)) :

                    The last part of a copy step: the cursor moves to the successor.

                    theorem OrderMaintenance.Impl.copyW_ok {om : Model.OrderMaintenance} {n : Model.Core} {m : Machine.State} {p : ℕ} (h : WEnc { cur := om.cur, curIdx := om.curIdx, nxt := some n, nxtIdx := om.nxtIdx, cursor := om.cursor, cursorSet := om.cursorSet, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB } m p) (hC : CopyOK om n) {om' : Model.OrderMaintenance} {n' : Model.Core} (hcp : Model.copyAtCursor om n = Except.ok (om', n')) :
                    Machine.wp (copyC p) (fun (m' : Machine.State) (k : ℕ) => WEnc { cur := om'.cur, curIdx := om'.curIdx, nxt := some n', nxtIdx := om'.nxtIdx, cursor := om'.cursor, cursorSet := om'.cursorSet, live := om'.live, nextHandle := om'.nextHandle, rebuilds := om'.rebuilds, KB := om'.KB } m' p ∧ (∀ (x : String), x ≠ "c" → x ≠ "hh" → x ≠ "xn" → x ≠ "y2" → m'.loc x = m.loc x) ∧ k ≤ insB + 100) m

                    One iteration #

                    The loop's invariant, f iterations from the end of a run with result R: the machine holds a state of the run (with its budget in bud), or the run has ended in the swap (no budget).

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      The locals an iteration may change.

                      Equations
                      Instances For

                        The cost bound of an iteration.

                        Instances For
                          theorem OrderMaintenance.Impl.swap_with (om : Model.OrderMaintenance) (n : Model.Core) :
                          { cur := om.cur, curIdx := om.curIdx, nxt := some n, nxtIdx := om.nxtIdx, cursor := om.cursor, cursorSet := om.cursorSet, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB }.swap n = om.swap n
                          theorem OrderMaintenance.Impl.endW_ok {om : Model.OrderMaintenance} {n : Model.Core} {s : Machine.State} {p : ℕ} (h : WEnc { cur := om.cur, curIdx := om.curIdx, nxt := some n, nxtIdx := om.nxtIdx, cursor := om.cursor, cursorSet := om.cursorSet, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB } s p) :
                          Machine.wp (endC p) (fun (s' : Machine.State) (k : ℕ) => s'.loc "bud" = 0 ∧ WEnc (om.swap n) s' (1 - p) ∧ (∀ (x : String), x ≠ "bud" → s'.loc x = s.loc x) ∧ k ≤ 20) s
                          theorem OrderMaintenance.Impl.spendW_ok {s : Machine.State} {b : ℕ} (hb : s.loc "bud" = b) (hb0 : b ≠ 0) :
                          Machine.wp spendC (fun (s' : Machine.State) (k : ℕ) => s'.loc "bud" = b - 1 ∧ (∀ (x : String), x ≠ "bud" → s'.loc x = s.loc x) ∧ s'.mem = s.mem ∧ s'.cap = s.cap ∧ k ≤ 5) s
                          theorem OrderMaintenance.Impl.iterW_ok {R : Model.OrderMaintenance} {p f : ℕ} {s : Machine.State} (hI : LoopI R p (f + 1) s) :
                          Machine.wp (iterC p) (fun (s' : Machine.State) (k : ℕ) => LoopI R p f s' ∧ (∀ (x : Machine.Reg), ItLoc x → s'.loc x = s.loc x) ∧ k ≤ iterB) s

                          The loop #

                          theorem OrderMaintenance.Impl.LoopI.congr {R : Model.OrderMaintenance} {p f : ℕ} {s s' : Machine.State} (h : LoopI R p f s) (hl : s'.loc "bud" = s.loc "bud") (hm : s'.mem = s.mem) (hc : s'.cap = s.cap) :
                          LoopI R p f s'
                          theorem OrderMaintenance.Impl.loopW_ok {om : Model.OrderMaintenance} {n : Model.Core} {m : Machine.State} {p : ℕ} (h : WEnc { cur := om.cur, curIdx := om.curIdx, nxt := some n, nxtIdx := om.nxtIdx, cursor := om.cursor, cursorSet := om.cursorSet, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB } m p) {R : Model.OrderMaintenance} (hr : Model.rebuildLoop om n (om.KB + 1) om.KB = Except.ok R) (hS : RLSafe om n (om.KB + 1) om.KB) :
                          Machine.wp (loopC p) (fun (m' : Machine.State) (k : ℕ) => (WEnc R m' p ∨ WEnc R m' (1 - p)) ∧ (∀ (x : Machine.Reg), ItLoc x → x ≠ "it" → m'.loc x = m.loc x) ∧ k ≤ 20 + (om.KB + 1) * (1 + iterB)) m

                          The rebuild loop: the machine ends holding Level F's result, in one slot or the other.

                          Starting a rebuild #

                          theorem OrderMaintenance.Impl.WEnc.start {om : Model.OrderMaintenance} {m m' : Machine.State} {p : ℕ} (h : WEnc om m p) {n : Model.Core} (hE : Enc (1 - p) (capsOf n.P) n m') (hS : ∀ (b : ℕ), ¬InSlot (1 - p) b → b ≠ wIdx (1 - p) → Machine.Same b m m') (hcap : om.nextHandle + (om.cur.P.N - om.cur.count) + n.P.N ≤ m'.cap (wIdx (1 - p))) (hz : ∀ (i : ℕ), m'.mem (wIdx (1 - p)) i = 0) :
                          WEnc { cur := om.cur, curIdx := om.curIdx, nxt := some n, cursor := om.cursor, cursorSet := om.cursorSet, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB } (m'.write (wa 1) 0 1) p

                          A new nxt in slot 1 - p, with a zero handle map of room enough.

                          The trigger: count ≥ N/2, or N > minimumCapacity and live < N/8 (locals cnt, nC, lv).

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For

                            live + mirrored of the next capacity.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For

                              The next capacity, 4 · max na 16.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For

                                The capacity of the new handle map: nextHandle + (N − count) + N'.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  Start a rebuild if cur is half full or too few of its elements are live.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For

                                    The locals a rebuild start may change.

                                    Equations
                                    Instances For

                                      The state after a rebuild start.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        theorem OrderMaintenance.Impl.startW_ok {om : Model.OrderMaintenance} {m : Machine.State} {p : ℕ} (h : WEnc om m p) (hn : om.nxt = none) (hkb : 1 ≤ om.KB) {R : Model.OrderMaintenance} (hr : Model.OrderMaintenance.rebuildStep Proof.proofParams om = Except.ok R) :
                                        Machine.wp (startC p) (fun (m' : Machine.State) (k : ℕ) => WEnc R m' p ∧ (∀ (x : Machine.Reg), StLoc x → m'.loc x = m.loc x) ∧ k ≤ 3500) m