Documentation

LeanPool.OrderMaintenance.Proof.Correct.Fallback

FALLBACK establishes INV-S (Lemma 3.3) #

FALLBACK reads only the element list. It cuts the list into groups and chunks with evenly spaced labels (fbLayout) and rebuilds the top structure, whose tags relabelAll checks along the chunk list (renumbering if they do not increase). The result satisfies INV-S with the same element list, and the element records keep their links, liveness and handles.

Cutting a list into pieces #

theorem OrderMaintenance.Proof.chunksOf_ne_nil {α : Type} (n : ℕ) (l p : List α) :
p ∈ Model.chunksOf n l → p ≠ []
theorem OrderMaintenance.Proof.chunksOf_getD {α : Type} (n : ℕ) (l : List α) (c : ℕ) :
(Model.chunksOf n l).getD c [] = List.take (max n 1) (List.drop (c * max n 1) l)

The pieces, in closed form.

theorem OrderMaintenance.Proof.chunksOf_length {α : Type} (n : ℕ) (l : List α) :
(Model.chunksOf n l).length = (l.length + max n 1 - 1) / max n 1

Relabelling the elements #

The element updates for one group (the k-th, numbered n + 1).

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

    Assign consecutive fallback group indices and labels to a partition of the elements.

    Equations
    Instances For
      theorem OrderMaintenance.Proof.fbE_eq_aux (P : Model.Params) (gl : List (List ℕ)) (n : ℕ) (E : Array Model.Elem) :
      List.foldl (fun (E : Array Model.Elem) (p : List ℕ × ℕ) => have step := 1 <<< P.elementBits / (p.1.length + 1); List.foldl (fun (E : Array Model.Elem) (q : ℕ × ℕ) => E.modify q.1 fun (e : Model.Elem) => { next := e.next, prev := e.prev, grp := p.2 + 1, lab := (q.2 + 1) * step, live := e.live, h := e.h }) E p.1.zipIdx) E (gl.zipIdx n) = fbE P n gl E
      theorem OrderMaintenance.Proof.foldl_zipIdx_modify {α : Type} [Inhabited α] (l : List ℕ) (f : ℕ → α → α) (E : Array α) (n : ℕ) (hnd : l.Nodup) (e : ℕ) :
      (List.foldl (fun (E : Array α) (q : ℕ × ℕ) => E.modify q.1 (f q.2)) E (l.zipIdx n)).getX e = if e ∈ l ∧ e < E.size then f (n + List.idxOf e l) (E.getX e) else E.getX e

      A fold of modifications at the members of l (one at each, in order).

      theorem OrderMaintenance.Proof.foldl_zipIdx_modify_size {α : Type} (l : List ℕ) (f : ℕ → α → α) (E : Array α) (n : ℕ) :
      (List.foldl (fun (E : Array α) (q : ℕ × ℕ) => E.modify q.1 (f q.2)) E (l.zipIdx n)).size = E.size
      theorem OrderMaintenance.Proof.assignGroup_getX (P : Model.Params) (n : ℕ) (g : List ℕ) (E : Array Model.Elem) (hnd : g.Nodup) (e : ℕ) :
      (assignGroup P n g E).getX e = if e ∈ g ∧ e < E.size then have __src := E.getX e; { next := __src.next, prev := __src.prev, grp := n + 1, lab := (List.idxOf e g + 1) * (1 <<< P.elementBits / (g.length + 1)), live := __src.live, h := __src.h } else E.getX e
      theorem OrderMaintenance.Proof.fbE_size (P : Model.Params) (gl : List (List ℕ)) (n : ℕ) (E : Array Model.Elem) :
      (fbE P n gl E).size = E.size
      theorem OrderMaintenance.Proof.fbE_other (P : Model.Params) (gl : List (List ℕ)) (n : ℕ) (E : Array Model.Elem) (hnd : gl.flatten.Nodup) (e : ℕ) :
      ((fbE P n gl E).getX e).next = (E.getX e).next ∧ ((fbE P n gl E).getX e).prev = (E.getX e).prev ∧ ((fbE P n gl E).getX e).live = (E.getX e).live ∧ ((fbE P n gl E).getX e).h = (E.getX e).h

      fbE changes only grp and lab.

      theorem OrderMaintenance.Proof.fbE_out (P : Model.Params) (gl : List (List ℕ)) (n : ℕ) (E : Array Model.Elem) (hnd : gl.flatten.Nodup) {e : ℕ} (he : e ∉ gl.flatten) :
      (fbE P n gl E).getX e = E.getX e

      fbE leaves elements outside the groups alone.

      theorem OrderMaintenance.Proof.fbE_in (P : Model.Params) (gl : List (List ℕ)) (n : ℕ) (E : Array Model.Elem) (hnd : gl.flatten.Nodup) (hsz : ∀ e ∈ gl.flatten, e < E.size) {k : ℕ} (hk : k < gl.length) {e : ℕ} (he : e ∈ gl[k]) :
      ((fbE P n gl E).getX e).grp = n + k + 1 ∧ ((fbE P n gl E).getX e).lab = (List.idxOf e gl[k] + 1) * (1 <<< P.elementBits / (gl[k].length + 1))

      The element e of the k-th group gets group n + k + 1 and label (i + 1)·step.

      Consecutive indices #

      theorem OrderMaintenance.Proof.size_listArray {α : Type} (d : α) (f : ℕ → α) (m : ℕ) :
      theorem OrderMaintenance.Proof.predOf_range' (a n z : ℕ) (hz : z ∈ List.range' a n) (ha : 1 ≤ a) :
      theorem OrderMaintenance.Proof.llist_range' {nxt prv : ℕ → ℕ} {a n : ℕ} (ha : 1 ≤ a) (hn : 1 ≤ n) (hnx : ∀ z ∈ List.range' a n, nxt z = if z + 1 < a + n then z + 1 else Model.NIL) (hpv : ∀ z ∈ List.range' a n, prv z = if z = a then Model.NIL else z - 1) :
      LList nxt prv a (List.range' a n)
      theorem OrderMaintenance.Proof.flatMap_blocks (gpc m : ℕ) (hg : 1 ≤ gpc) (nc : ℕ) :
      List.flatMap (fun (c : ℕ) => List.range' (c * gpc + 1) (min gpc (m - c * gpc))) (List.range nc) = List.range' 1 (min m (nc * gpc))

      Blocks of gpc consecutive indices, the last one shorter, cover 1 .. m.

      INV-S without the tag condition #

      The linked-list and allocation part of the core invariant before restoring labels.

      Instances For
        theorem OrderMaintenance.Proof.INV0.toINV {s : Model.Core} {A : Abs} (h : INV0 s A) (hs : List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) (List.map (cT s) A.chunks)) :
        INV s A

        The element list part, which FALLBACK reads #

        The element-list, allocation and endpoint invariants needed by a fallback rebuild.

        Instances For
          theorem OrderMaintenance.Proof.INV.elemOK {s : Model.Core} {A : Abs} (h : INV s A) (hne : A.elist ≠ []) :
          theorem OrderMaintenance.Proof.ElemOK.of_eframe {s s' : Model.Core} {l : List ℕ} (h : ElemOK s l) (hf : EFrame s s') :
          ElemOK s' l

          Arithmetic of the layout #

          theorem OrderMaintenance.Proof.map_getD_range {α : Type} (l : List α) (d : α) :
          List.map (fun (k : ℕ) => l.getD k d) (List.range l.length) = l
          theorem OrderMaintenance.Proof.isChain_range_mul (n : ℕ) {st : ℕ} (hst : 1 ≤ st) :
          List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) (List.map (fun (i : ℕ) => (i + 1) * st) (List.range n))
          theorem OrderMaintenance.Proof.isChain_spaced {r : List ℕ} (hnd : r.Nodup) {st : ℕ} (hst : 1 ≤ st) :
          List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) (List.map (fun (e : ℕ) => (List.idxOf e r + 1) * st) r)

          Evenly spaced labels along a list without duplicates increase.

          The abstract nesting of FALLBACK's layout of l.

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

            Label widths fit FALLBACK's spacing (docs/PROOF.md §3.4): every gap is at least 1.

            Equations
            Instances For
              theorem OrderMaintenance.Proof.inv0_fbLayout {s : Model.Core} {l : List ℕ} (hE : ElemOK s l) (hP : FbOK s.P) :
              INV0 (Model.fbLayout s.P l s) (fbAbs s.P l)

              FALLBACK's layout satisfies INV-S without the tag condition, for the nesting fbAbs.

              Rebuilding the top structure #

              theorem OrderMaintenance.Proof.pres_modC_tag (c t : ℕ) :
              Pres SameLinks (Model.modC c fun (C : Model.Chunk) => { next := C.next, prev := C.prev, first := C.first, last := C.last, size := C.size, st := C.st, src := C.src, l := C.l, r := C.r, tag := t, alive := C.alive })

              Walking a linked list #

              theorem OrderMaintenance.Proof.walkList_eq {nxt : ℕ → ℕ} {l : List ℕ} (hnd : l.Nodup) (hnil : Model.NIL ∉ l) (hn : ∀ x ∈ l, nxt x = succOf l x) {fuel : ℕ} (hf : l.length ≤ fuel) :
              theorem OrderMaintenance.Proof.LList.walk {nxt prv : ℕ → ℕ} {hd : ℕ} {l : List ℕ} (h : LList nxt prv hd l) {fuel : ℕ} (hf : l.length ≤ fuel) :
              Model.walkList nxt fuel hd = l
              theorem OrderMaintenance.Proof.length_lt_of_range {l : List ℕ} {n : ℕ} (hnd : l.Nodup) (h : ∀ x ∈ l, 1 ≤ x ∧ x < n) :

              A list of distinct numbers in [1, n) has length below n.

              Renumbering the chunks #

              theorem OrderMaintenance.Proof.renumber_getX (l : List ℕ) (C : Array Model.Chunk) (hnd : l.Nodup) (c : ℕ) :
              (Model.renumber l C).getX c = if c ∈ l ∧ c < C.size then have __src := C.getX c; { next := __src.next, prev := __src.prev, first := __src.first, last := __src.last, size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, tag := 1 + List.idxOf c l, alive := __src.alive } else C.getX c

              FALLBACK's top part: from INV-S without tags, relabelAll succeeds with INV-S.

              theorem OrderMaintenance.Proof.fallback_run {s : Model.Core} {l : List ℕ} (hE : ElemOK s l) (hP : FbOK s.P) :
              ∃ (s' : Model.Core), EStateM.run Model.fallback s = EStateM.Result.ok () s' ∧ EFrame s s' ∧ ∃ (A : Abs), INV s' A ∧ A.elist = l

              Lemma 3.3 (FALLBACK). From any state whose element list l is a well-formed non-empty doubly linked list, FALLBACK succeeds, keeps the element records' links, liveness and handles, and establishes INV-S with the same element list.

              Room for the retried insertion #

              Parameters for the retry: FALLBACK's element gap exceeds the append distance elementSpacing ≥ 1.

              Equations
              Instances For

                Every element has room for an insertion right after it.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  theorem OrderMaintenance.Proof.succOf_getElem {b : List ℕ} (hnd : b.Nodup) {i : ℕ} (hi : i + 1 < b.length) :
                  succOf b b[i] = b[i + 1]
                  theorem OrderMaintenance.Proof.room_fbLayout {s : Model.Core} {l : List ℕ} (hE : ElemOK s l) (hP : RoomP s.P) :
                  Room (Model.fbLayout s.P l s) (fbAbs s.P l)
                  theorem OrderMaintenance.Proof.fallback_run_room {s : Model.Core} {l : List ℕ} (hE : ElemOK s l) (hP : FbOK s.P) (hR : RoomP s.P) :
                  ∃ (s' : Model.Core), EStateM.run Model.fallback s = EStateM.Result.ok () s' ∧ EFrame s s' ∧ ∃ (A : Abs), INV s' A ∧ A.elist = l ∧ Room s' A

                  Lemma 3.3 with room: under RoomP, every element also has room after FALLBACK.