Documentation

LeanPool.OrderMaintenance.Proof.Cost.Wrap

The wrapper: cost and Lemma W #

Every core the wrapper runs satisfies the cost invariant whenever an operation reaches it, and its element count stays within its capacity (Lemma W). So no core operation ever takes FALLBACK.

structure OrderMaintenance.Proof.NList (nxt : ℕ → ℕ) (hd : ℕ) (l : List ℕ) :

A list linked forward from hd.

Instances For
    theorem OrderMaintenance.Proof.LList.nlist {nxt prv : ℕ → ℕ} {hd : ℕ} {l : List ℕ} (h : LList nxt prv hd l) :
    NList nxt hd l
    theorem OrderMaintenance.Proof.succOf_cons_ne {a x : ℕ} {t : List ℕ} (h : a ≠ x) :
    succOf (a :: t) x = succOf t x
    theorem OrderMaintenance.Proof.NList.unique {nxt : ℕ → ℕ} {hd : ℕ} {l l' : List ℕ} :
    NList nxt hd l → NList nxt hd l' → l = l'

    A forward-linked list is determined by its head.

    theorem OrderMaintenance.Proof.INV.elist_unique {s : Model.Core} {A A' : Abs} (h : INV s A) (h' : INV s A') :

    The split at the cursor #

    The elements before the cursor.

    Equations
    Instances For

      The elements from the cursor on.

      Equations
      Instances For
        theorem OrderMaintenance.Proof.split_unique {pre post : List ℕ} {c : ℕ} (hnd : (pre ++ post).Nodup) (hnil : Model.NIL ∉ pre ++ post) (hc : c = post.headD Model.NIL) :
        preOf (pre ++ post) c = pre ∧ postOf (pre ++ post) c = post

        A split whose second part starts at c is the split at c.

        The element count of a core equals its list's length (an extra invariant the wrapper uses).

        Equations
        Instances For
          structure OrderMaintenance.Proof.CostSetup (mk : ℕ → Model.Params) (TI PI : Model.Core → Prop) (μ ν : Model.Core → ℕ) (PP : Model.Params → Prop) (Jf capf nf Rf : Model.Params → ℕ) :

          What the wrapper's cost proof assumes: the top structure's contracts (M5), the parameter conditions, and Lemma R's I5 (M5 fixes J and H).

          Instances For
            structure OrderMaintenance.Proof.CoreOK (mk : ℕ → Model.Params) (TI : Model.Core → Prop) (μ : Model.Core → ℕ) (Jf nf Rf : Model.Params → ℕ) (c : Model.Core) (L : List ℕ) :

            A core in service: the cost invariant for its element list, which it counts.

            Instances For
              theorem OrderMaintenance.Proof.CoreOK.inv {mk : ℕ → Model.Params} {TI : Model.Core → Prop} {μ : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {c : Model.Core} {L : List ℕ} (h : CoreOK mk TI μ Jf nf Rf c L) :
              ∃ (A : Abs), INV c A ∧ A.elist = L
              theorem OrderMaintenance.Proof.CoreOK.insert {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} {c : Model.Core} {L : List ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) (h : CoreOK mk TI μ Jf nf Rf c L) {x : ℕ} (hx : x ∈ L) (hcap : c.count + 1 ≤ c.P.N) (hh : ℕ) :
              ∃ (c' : Model.Core), EStateM.run (Model.insertAfter x hh) c = EStateM.Result.ok c.E.size c' ∧ EStateM.run (do let y ← Model.insertElem x hh Model.background y pure y) c = EStateM.Result.ok c.E.size c' ∧ CoreOK mk TI μ Jf nf Rf c' (Spec.insertAfterL x c.E.size L) ∧ c'.count = c.count + 1 ∧ c'.P = c.P

              Insertion into a core in service: no FALLBACK while its count stays within capacity.

              theorem OrderMaintenance.Proof.CoreOK.markDead {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} {c : Model.Core} {L : List ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) (h : CoreOK mk TI μ Jf nf Rf c L) (x : ℕ) :
              ∃ (c' : Model.Core), EStateM.run (Model.markDead x) c = EStateM.Result.ok () c' ∧ CoreOK mk TI μ Jf nf Rf c' L ∧ c'.count = c.count ∧ c'.P = c.P

              Deletion in a core in service.

              A core whose top structure is being built: it holds the sentinel alone.

              Instances For
                theorem OrderMaintenance.Proof.newCore_lazy {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) (N : ℕ) :
                ∃ (n : Model.Core), Model.newCore (mk N) true = Except.ok n ∧ PrepOK mk PI n ∧ ν n ≤ (mk N).H + 1 ∧ n.P = mk N

                A lazily built fresh core.

                theorem OrderMaintenance.Proof.newCore_eager {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) (N : ℕ) :
                ∃ (n : Model.Core), Model.newCore (mk N) false = Except.ok n ∧ CoreOK mk TI μ Jf nf Rf n initAbs.elist ∧ n.count = 1 ∧ n.P = mk N

                An eagerly built fresh core is in service.

                theorem OrderMaintenance.Proof.PrepOK.step {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {n : Model.Core} (h : PrepOK mk PI n) :
                ∃ (b : Bool) (n' : Model.Core), Model.runCore n Model.prepareStep = Except.ok (b, n') ∧ n'.count = n.count ∧ n'.P = n.P ∧ INV n' initAbs ∧ (b = true → CoreOK mk TI μ Jf nf Rf n' initAbs.elist) ∧ (b = false → PrepOK mk PI n' ∧ ν n' < ν n)

                A preparation step of a core being built: it finishes (the core is in service) or it decreases ν.

                structure OrderMaintenance.Proof.RebuildW (mk : ℕ → Model.Params) (TI PI : Model.Core → Prop) (μ ν : Model.Core → ℕ) (Jf nf Rf : Model.Params → ℕ) (om : Model.OrderMaintenance) (n : Model.Core) (c₀ live₀ I D : ℕ) :

                Lemma W's record of a rebuild in progress: c₀ elements and live₀ live ones at the trigger, I insertions and D deletions since. The work left (preparation steps, then the elements not yet scanned) shrinks by K_B − 1 per insertion's worth of operations.

                Instances For
                  structure OrderMaintenance.Proof.WC (mk : ℕ → Model.Params) (TI PI : Model.Core → Prop) (μ ν : Model.Core → ℕ) (Jf nf Rf : Model.Params → ℕ) (om : Model.OrderMaintenance) (Sp : Spec.State) :

                  The wrapper's cost invariant: Invariant R, both cores' cost invariants, and Lemma W.

                  Instances For
                    theorem OrderMaintenance.Proof.RebuildW.ins_le {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {n : Model.Core} {c₀ live₀ I D : ℕ} (h : RebuildW mk TI PI μ ν Jf nf Rf om n c₀ live₀ I D) (hkb : 4 ≤ om.KB) (hA : ∃ (A : Abs), INV om.cur A) :
                    3 * I ≤ om.cur.P.L + 2 + c₀

                    The work left bounds the insertions of a rebuild.

                    theorem OrderMaintenance.Proof.RebuildW.insertions_le {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {n : Model.Core} {c₀ live₀ I D : ℕ} (h : RebuildW mk TI PI μ ν Jf nf Rf om n c₀ live₀ I D) (hkb : 4 ≤ om.KB) {A : Abs} (hA : INV om.cur A) :
                    I ≤ (c₀ + (om.cur.P.L + 2)) / (om.KB - 1)

                    The remaining rebuild work bounds its insertion counter.

                    theorem OrderMaintenance.Proof.WC.cur_room {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {Sp : Spec.State} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) (h : WC mk TI PI μ ν Jf nf Rf om Sp) :
                    om.cur.count + 1 ≤ om.cur.P.N

                    Lemma W (1): cur has room for an insertion.

                    theorem OrderMaintenance.Proof.WC.nxt_live_le {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {Sp : Spec.State} (h : WC mk TI PI μ ν Jf nf Rf om Sp) {n : Model.Core} (hn : om.nxt = some n) (hset : om.cursorSet = true) {B : Abs} (hB : INV n B) :

                    The copies in nxt are live elements of cur, so at most the live count.

                    theorem OrderMaintenance.Proof.WC.nxt_room {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {Sp : Spec.State} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) (h : WC mk TI PI μ ν Jf nf Rf om Sp) {n : Model.Core} (hn : om.nxt = some n) (hset : om.cursorSet = true) :
                    n.count + 1 ≤ n.P.N

                    Lemma W (2): nxt has room for an insertion.

                    An insertion adds at most one element to the part from the cursor on.

                    theorem OrderMaintenance.Proof.postOf_succOf {l : List ℕ} (hnd : l.Nodup) (hnil : Model.NIL ∉ l) {c : ℕ} (hc : c ∈ l) :
                    postOf l (succOf l c) = (postOf l c).tail

                    Advancing the cursor drops one element.

                    theorem OrderMaintenance.Proof.copy_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {om : Model.OrderMaintenance} {n : Model.Core} {B : Abs} {D : ℕ} (hRB : CoreR n B) (hok : CoreOK mk TI μ Jf nf Rf n B.elist) (hfill : n.count ≤ 1 + (liveH n B.elist).length + D) (hroom : n.count + 1 ≤ n.P.N) :
                    ∃ (om' : Model.OrderMaintenance) (n' : Model.Core) (B' : Abs), Model.copyAtCursor om n = Except.ok (om', n') ∧ om'.cursor = eN om.cur om.cursor ∧ CoreR n' B' ∧ CoreOK mk TI μ Jf nf Rf n' B'.elist ∧ n'.count ≤ 1 + (liveH n' B'.elist).length + D ∧ n'.P = n.P

                    A copy step keeps the nxt core in service and its fill bound, and advances the cursor.

                    structure OrderMaintenance.Proof.LoopC (mk : ℕ → Model.Params) (TI PI : Model.Core → Prop) (μ ν : Model.Core → ℕ) (Jf nf Rf : Model.Params → ℕ) (om : Model.OrderMaintenance) (n : Model.Core) (A : Abs) (budget Φe live₀ I D N' : ℕ) :

                    The rebuild loop's cost invariant, with budget steps left, within the entry potential Φe.

                    Instances For
                      def OrderMaintenance.Proof.LoopOut (mk : ℕ → Model.Params) (TI PI : Model.Core → Prop) (μ ν : Model.Core → ℕ) (Jf nf Rf : Model.Params → ℕ) (om om' : Model.OrderMaintenance) (A : Abs) (Φe live₀ I D N' : ℕ) :

                      What the rebuild loop leaves: the rebuild goes on, having spent the whole budget, or nxt replaced cur.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        theorem OrderMaintenance.Proof.LoopC.congr {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om om' : Model.OrderMaintenance} {n : Model.Core} {A : Abs} {budget Φe live₀ I D N' : ℕ} (h : LoopC mk TI PI μ ν Jf nf Rf om n A budget Φe live₀ I D N') (hc : om'.cur = om.cur) (hs : om'.cursorSet = om.cursorSet) (hu : om'.cursor = om.cursor) (hk : om'.KB = om.KB) (hl : om'.live = om.live) :
                        LoopC mk TI PI μ ν Jf nf Rf om' n A budget Φe live₀ I D N'
                        theorem OrderMaintenance.Proof.LoopC.prepared {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {n n' : Model.Core} {A B : Abs} {budget Φe live₀ I D N' : ℕ} (h : LoopC mk TI PI μ ν Jf nf Rf om n A budget Φe live₀ I D N') (hR : CoreR om.cur A) (hs : om.cursorSet = false) (hP : n'.P = n.P) (hcnt : n'.count = n.count) (hB : INV n' B) (hok : CoreOK mk TI μ Jf nf Rf n' B.elist) :
                        LoopC mk TI PI μ ν Jf nf Rf { cur := om.cur, curIdx := om.curIdx, nxt := om.nxt, nxtIdx := om.nxtIdx, cursor := (om.cur.E.getX om.cur.head).next, cursorSet := true, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB } n' A budget Φe live₀ I D N'

                        Finishing preparation initializes the shared service invariant at the first cursor.

                        theorem OrderMaintenance.Proof.LoopC.preparing {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {n n' : Model.Core} {A : Abs} {budget Φe live₀ I D N' : ℕ} (h : LoopC mk TI PI μ ν Jf nf Rf om n A budget Φe live₀ I D N') (hs : om.cursorSet = false) (hP : n'.P = n.P) (hprep : PrepOK mk PI n') (hν : ν n' < ν n) :
                        LoopC mk TI PI μ ν Jf nf Rf om n' A (budget - 1) Φe live₀ I D N'

                        An unfinished preparation spends one budget unit as its potential decreases.

                        theorem OrderMaintenance.Proof.LoopC.copied {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om om' : Model.OrderMaintenance} {n n' : Model.Core} {A B : Abs} {budget Φe live₀ I D N' : ℕ} (h : LoopC mk TI PI μ ν Jf nf Rf om n A budget Φe live₀ I D N') (hs : om.cursorSet = true) (hc : om'.cur = om.cur) (hset : om'.cursorSet = om.cursorSet) (hl : om'.live = om.live) (hk : om'.KB = om.KB) (hP : n'.P = n.P) (hB : INV n' B) (hok : CoreOK mk TI μ Jf nf Rf n' B.elist) (hfill : n'.count ≤ 1 + (liveH n' B.elist).length + D) (hpost : (postOf A.elist om'.cursor).length + 1 = (postOf A.elist om.cursor).length) :
                        LoopC mk TI PI μ ν Jf nf Rf om' n' A (budget - 1) Φe live₀ I D N'

                        A copy preserves the service invariant when its cursor drops one remaining element.

                        theorem OrderMaintenance.Proof.LoopOut.mono {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om om1 om' : Model.OrderMaintenance} {A : Abs} {Φe live₀ I D N' : ℕ} (h : LoopOut mk TI PI μ ν Jf nf Rf om1 om' A Φe live₀ I D N') (hc : om1.cur = om.cur) (hk : om1.KB = om.KB) (hl : om1.live = om.live) :
                        LoopOut mk TI PI μ ν Jf nf Rf om om' A Φe live₀ I D N'
                        theorem OrderMaintenance.Proof.nxtR'_live_le {c : Model.Core} {A : Abs} {om : Model.OrderMaintenance} {n : Model.Core} {Sp : Spec.State} (hN : NxtR' c A om n) (hL : liveH c A.elist = Sp.list) (hset : om.cursorSet = true) {B : Abs} (hB : INV n B) :

                        The live copies are at most the live count, inside the loop.

                        theorem OrderMaintenance.Proof.rebuildLoop_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {Sp : Spec.State} (hS : Spec.Inv Sp) {Φe live₀ I D N' : ℕ} (fuel budget : ℕ) (om : Model.OrderMaintenance) (n : Model.Core) (A : Abs) :
                        CoreR om.cur A → liveH om.cur A.elist = Sp.list → IdxOK om.cur A om.curIdx Sp.list → NxtR' om.cur A om n → om.nextHandle = Sp.fresh → om.live = Sp.list.length → LoopC mk TI PI μ ν Jf nf Rf om n A budget Φe live₀ I D N' → (budget + if om.cursorSet = true then 0 else 1) ≤ fuel → ∃ (om' : Model.OrderMaintenance), Model.rebuildLoop om n fuel budget = Except.ok om' ∧ WInv om' Sp ∧ om'.live = om.live ∧ LoopOut mk TI PI μ ν Jf nf Rf om om' A Φe live₀ I D N'

                        The rebuild loop (Lemma W's length argument): every budgeted step shrinks the work left by one; the loop ends with the budget spent or with the swap.

                        structure OrderMaintenance.Proof.MidW (mk : ℕ → Model.Params) (TI PI : Model.Core → Prop) (μ ν : Model.Core → ℕ) (Jf nf Rf : Model.Params → ℕ) (om : Model.OrderMaintenance) (n : Model.Core) (c₀ live₀ I D : ℕ) :

                        Lemma W's record in the middle of an operation: this operation's insertion or deletion is counted, and its K_B rebuild steps are still to come.

                        Instances For
                          theorem OrderMaintenance.Proof.MidW.loop_entry {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {n : Model.Core} {A : Abs} {c₀ live₀ I D : ℕ} (h : MidW mk TI PI μ ν Jf nf Rf om n c₀ live₀ I D) (hok : CoreOK mk TI μ Jf nf Rf om.cur A.elist) :
                          LoopC mk TI PI μ ν Jf nf Rf { cur := om.cur, curIdx := om.curIdx, nxtIdx := om.nxtIdx, cursor := om.cursor, cursorSet := om.cursorSet, live := om.live, nextHandle := om.nextHandle, rebuilds := om.rebuilds, KB := om.KB } n A om.KB (if om.cursorSet = true then (postOf A.elist om.cursor).length else ν n + om.cur.count) live₀ I D (4 * max (live₀ + ((c₀ + (om.cur.P.L + 2)) / (om.KB - 1) + 1)) 16)

                          The middle-of-operation bounds initialize the rebuild loop's entry potential.

                          theorem OrderMaintenance.Proof.RebuildW.insert_mid {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om om' : Model.OrderMaintenance} {n n' : Model.Core} {c₀ live₀ I D : ℕ} (h : RebuildW mk TI PI μ ν Jf nf Rf om n c₀ live₀ I D) (hkb : 4 ≤ om.KB) {A : Abs} (hA : INV om.cur A) (hP : om'.cur.P = om.cur.P) (hc : om'.cur.count = om.cur.count + 1) (hl : om'.live = om.live + 1) (hk : om'.KB = om.KB) (hs : om'.cursorSet = om.cursorSet) (hnP : n'.P = n.P) (hprep : om.cursorSet = false → n' = n) (hserve : om.cursorSet = true → (∀ (B : Abs), INV n' B → CoreOK mk TI μ Jf nf Rf n' B.elist ∧ n'.count ≤ 1 + (liveH n' B.elist).length + D) ∧ ∀ (A' : Abs), INV om'.cur A' → (postOf A'.elist om'.cursor).length ≤ (postOf A.elist om.cursor).length + 1) :
                          MidW mk TI PI μ ν Jf nf Rf om' n' c₀ live₀ (I + 1) D

                          Insertion advances the counters and spends at most one operation's rebuild allowance.

                          theorem OrderMaintenance.Proof.RebuildW.delete_mid {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {Jf nf Rf : Model.Params → ℕ} {om om' : Model.OrderMaintenance} {n n' : Model.Core} {c₀ live₀ I D : ℕ} (h : RebuildW mk TI PI μ ν Jf nf Rf om n c₀ live₀ I D) (hkb : 4 ≤ om.KB) {A : Abs} (hA : INV om.cur A) (hpos : 1 ≤ om.live) (hP : om'.cur.P = om.cur.P) (hc : om'.cur.count = om.cur.count) (hl : om'.live = om.live - 1) (hk : om'.KB = om.KB) (hs : om'.cursorSet = om.cursorSet) (hnP : n'.P = n.P) (hprep : om.cursorSet = false → n' = n) (hserve : om.cursorSet = true → (∀ (B : Abs), INV n' B → CoreOK mk TI μ Jf nf Rf n' B.elist ∧ n'.count ≤ 1 + (liveH n' B.elist).length + (D + 1)) ∧ ∀ (A' : Abs), INV om'.cur A' → (postOf A'.elist om'.cursor).length ≤ (postOf A.elist om.cursor).length) :
                          MidW mk TI PI μ ν Jf nf Rf om' n' c₀ live₀ I (D + 1)

                          Deletion advances its counter without spending additional insertion allowance.

                          theorem OrderMaintenance.Proof.rebuildStep_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {om : Model.OrderMaintenance} {Sp : Spec.State} (hW : WInv om Sp) (hlv : om.live = Sp.list.length) (hkb : 4 ≤ om.KB) (hbig : 64 ≤ om.cur.P.N) (hcur : ∀ (A : Abs), INV om.cur A → CoreOK mk TI μ Jf nf Rf om.cur A.elist) (hidle : om.nxt = none → om.cur.count ≤ om.cur.P.N / 2) (hbusy : ∀ (n : Model.Core), om.nxt = some n → ∃ (c₀ : ℕ) (live₀ : ℕ) (I : ℕ) (D : ℕ), MidW mk TI PI μ ν Jf nf Rf om n c₀ live₀ I D) :
                          ∃ (om' : Model.OrderMaintenance), Model.OrderMaintenance.rebuildStep mk om = Except.ok om' ∧ WC mk TI PI μ ν Jf nf Rf om' Sp

                          The rebuild step (Lemma W): it restores the wrapper's cost invariant.

                          theorem OrderMaintenance.Proof.liveH_length_ins {c c' : Model.Core} {l : List ℕ} {a b : ℕ} (ha : a ∈ l) (hrec : ∀ e ∈ l, live c' e = live c e ∧ hdl c' e = hdl c e) (hb : live c' b = true) :

                          An insertion of a live element adds one live copy.

                          theorem OrderMaintenance.Proof.mirrorInsert_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {om : Model.OrderMaintenance} {n : Model.Core} {B : Abs} {D : ℕ} {hx : Option ℕ} {hy : ℕ} (hn : om.nxt = some n) (hset : om.cursorSet = true) (hRB : CoreR n B) (hok : CoreOK mk TI μ Jf nf Rf n B.elist) (hfill : n.count ≤ 1 + (liveH n B.elist).length + D) (hroom : n.count + 1 ≤ n.P.N) (hIB : IdxOK n B om.nxtIdx (liveH n B.elist)) (hzB : ∀ (x : ℕ), om.nxtIdx.getX x ≠ 0 → x ∈ liveH n B.elist) :
                          ∃ (om2 : Model.OrderMaintenance), om.mirrorInsert hx hy = Except.ok om2 ∧ om2.cur = om.cur ∧ om2.cursorSet = om.cursorSet ∧ om2.cursor = om.cursor ∧ om2.KB = om.KB ∧ om2.live = om.live ∧ ∃ (n' : Model.Core) (B' : Abs), om2.nxt = some n' ∧ CoreR n' B' ∧ CoreOK mk TI μ Jf nf Rf n' B'.elist ∧ n'.count ≤ 1 + (liveH n' B'.elist).length + D ∧ n'.P = n.P

                          Mirroring an insertion into nxt keeps it in service, within its fill bound.

                          theorem OrderMaintenance.Proof.insert_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {om : Model.OrderMaintenance} {Sp : Spec.State} (hW : WC mk TI PI μ ν Jf nf Rf om Sp) (hx : Option ℕ) (hv : Spec.Op.Valid Sp (insOp hx)) :
                          ∃ (om' : Model.OrderMaintenance), Model.OrderMaintenance.insert mk om hx = Except.ok (Sp.fresh, om') ∧ WC mk TI PI μ ν Jf nf Rf om' (Spec.step Sp (insOp hx)).1

                          A wrapper insertion keeps the wrapper's cost invariant: the insertions into cur and nxt take no FALLBACK, and Lemma W's record advances.

                          theorem OrderMaintenance.Proof.mirrorDelete_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {om : Model.OrderMaintenance} {n : Model.Core} {B : Abs} {D x : ℕ} (hn : om.nxt = some n) (hRB : CoreR n B) (hok : CoreOK mk TI μ Jf nf Rf n B.elist) (hfill : n.count ≤ 1 + (liveH n B.elist).length + D) (hIB : IdxOK n B om.nxtIdx (liveH n B.elist)) (hzB : ∀ (x : ℕ), om.nxtIdx.getX x ≠ 0 → x ∈ liveH n B.elist) (hndB : (liveH n B.elist).Nodup) :
                          ∃ (om2 : Model.OrderMaintenance), om.mirrorDelete x = Except.ok om2 ∧ om2.cur = om.cur ∧ om2.cursorSet = om.cursorSet ∧ om2.cursor = om.cursor ∧ om2.KB = om.KB ∧ om2.live = om.live ∧ ∃ (n' : Model.Core), om2.nxt = some n' ∧ CoreR n' B ∧ CoreOK mk TI μ Jf nf Rf n' B.elist ∧ n'.count ≤ 1 + (liveH n' B.elist).length + (D + 1) ∧ n'.P = n.P

                          Mirroring a deletion into nxt keeps it in service; the fill bound counts the deletion.

                          theorem OrderMaintenance.Proof.delete_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {om : Model.OrderMaintenance} {Sp : Spec.State} (hW : WC mk TI PI μ ν Jf nf Rf om Sp) {x : ℕ} (hv : x ∈ Sp.list) :
                          ∃ (om' : Model.OrderMaintenance), Model.OrderMaintenance.delete mk om x = Except.ok om' ∧ WC mk TI PI μ ν Jf nf Rf om' (Spec.step Sp (Spec.Op.delete x)).1

                          A wrapper deletion keeps the wrapper's cost invariant.

                          theorem OrderMaintenance.Proof.init_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {KB : ℕ} (hKB : 4 ≤ KB) :
                          ∃ (om : Model.OrderMaintenance), Model.OrderMaintenance.init mk KB = Except.ok om ∧ WC mk TI PI μ ν Jf nf Rf om Spec.State.empty

                          Initialization establishes the wrapper's cost invariant.

                          theorem OrderMaintenance.Proof.apply_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {om : Model.OrderMaintenance} {Sp : Spec.State} (hW : WC mk TI PI μ ν Jf nf Rf om Sp) {op : Spec.Op} (hv : Spec.Op.Valid Sp op) :
                          ∃ (om' : Model.OrderMaintenance), Model.OrderMaintenance.apply mk om op = Except.ok ((Spec.step Sp op).2, om') ∧ WC mk TI PI μ ν Jf nf Rf om' (Spec.step Sp op).1

                          Each operation keeps the wrapper's cost invariant.

                          theorem OrderMaintenance.Proof.wrapper_cost {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) {KB : ℕ} (hKB : 4 ≤ KB) :
                          (∃ (om : Model.OrderMaintenance), Model.OrderMaintenance.init mk KB = Except.ok om ∧ WC mk TI PI μ ν Jf nf Rf om Spec.State.empty) ∧ ∀ (om : Model.OrderMaintenance) (Sp : Spec.State) (op : Spec.Op), WC mk TI PI μ ν Jf nf Rf om Sp → Spec.Op.Valid Sp op → ∃ (om' : Model.OrderMaintenance), Model.OrderMaintenance.apply mk om op = Except.ok ((Spec.step Sp op).2, om') ∧ WC mk TI PI μ ν Jf nf Rf om' (Spec.step Sp op).1

                          The wrapper's cost theorem (Level F). Under the setup, the structure initializes into a state satisfying the cost invariant, and every valid operation keeps it. In every such state, both cores satisfy the cost invariant within their capacities, so each of their insertions takes no FALLBACK (CoreOK.insert, insertAfter_cost).

                          theorem OrderMaintenance.Proof.WC.cores {mk : ℕ → Model.Params} {TI PI : Model.Core → Prop} {μ ν : Model.Core → ℕ} {PP : Model.Params → Prop} {Jf capf nf Rf : Model.Params → ℕ} {om : Model.OrderMaintenance} {Sp : Spec.State} (S : CostSetup mk TI PI μ ν PP Jf capf nf Rf) (h : WC mk TI PI μ ν Jf nf Rf om Sp) :
                          (∀ (A : Abs), INV om.cur A → CoreOK mk TI μ Jf nf Rf om.cur A.elist) ∧ om.cur.count + 1 ≤ om.cur.P.N ∧ ∀ (n : Model.Core), om.nxt = some n → om.cursorSet = true → (∀ (B : Abs), INV n B → CoreOK mk TI μ Jf nf Rf n B.elist) ∧ n.count + 1 ≤ n.P.N

                          In a state satisfying the wrapper's cost invariant, cur is a core in service with room for an insertion, and so is nxt once its preparation is over.