Documentation

LeanPool.OrderMaintenance.Proof.Correct.ChunkSplit

Chunk split moves preserve INV-S (Lemma 3.2: chunk-split and family moves) #

moveUnit x left takes the unit at one end of chunk x (one group, or a whole family of three groups that the guard checks to lie in x, consecutively) and appends it to the end of the sibling chunk before x (or prepends it to the one after), with guarded group labels. The group list does not change; only the chunks' runs, the moved groups' chk and labels, and the chunks' first/last pointers do.

The unit #

theorem OrderMaintenance.Proof.unitOK_three {s : Model.Core} {x a b c : ℕ} (h : Model.unitOK s x [a, b, c] = true) :
a ≠ Model.NIL ∧ b ≠ Model.NIL ∧ c ≠ Model.NIL ∧ gC s a = x ∧ gC s b = x ∧ gC s c = x ∧ gN s a = b ∧ gN s b = c
theorem OrderMaintenance.Proof.INV.run_ne_nil {s : Model.Core} {A : Abs} (h : INV s A) {x : ℕ} (hx : cF s x ≠ Model.NIL) :
x ∈ A.chunks ∧ ∃ (t : List ℕ), A.grps x = cF s x :: t
theorem OrderMaintenance.Proof.INV.grps_nodup {s : Model.Core} {A : Abs} (h : INV s A) {c : ℕ} (hc : c ∈ A.chunks) :
(A.grps c).Nodup
theorem OrderMaintenance.Proof.INV.mem_grps_of_chk {s : Model.Core} {A : Abs} (h : INV s A) {a x : ℕ} (ha : a ∈ A.glist) (hc : gC s a = x) :
a ∈ A.grps x

A group in the list whose chk is x lies in chunk x's run.

theorem OrderMaintenance.Proof.INV.gN_in_run {s : Model.Core} {A : Abs} (h : INV s A) {x a : ℕ} (hx : x ∈ A.chunks) (ha : a ∈ A.grps x) (hb0 : gN s a ≠ Model.NIL) (hbc : gC s (gN s a) = x) :
gN s a = succOf (A.grps x) a

The successor (in the group list) of a member of chunk x's run, if it is also in chunk x, is its successor in the run.

theorem OrderMaintenance.Proof.unit_prefix {s : Model.Core} {A : Abs} (hI : INV s A) {x : ℕ} (hx : cF s x ≠ Model.NIL) {u : List ℕ} (hu : u = [cF s x] ∨ ∃ (a : ℕ), ∃ (b : ℕ), u = [cF s x, a, b] ∧ Model.unitOK s x u = true) :
x ∈ A.chunks ∧ ∃ (rest : List ℕ), A.grps x = u ++ rest

The left unit is a prefix of the chunk's run.

theorem OrderMaintenance.Proof.succOf_eq_next_block {l m : List ℕ} {b z : ℕ} (hnd : (l ++ z :: m).Nodup) (hb : b ∈ l) (hs : succOf (l ++ z :: m) b = z) (hz : z ≠ Model.NIL) :
∃ (l' : List ℕ), l = l' ++ [b]

If the successor of b ∈ l in l ++ z :: m is z, then b is the last of l.

theorem OrderMaintenance.Proof.unit_suffix {s : Model.Core} {A : Abs} (hI : INV s A) {x : ℕ} (hx : cF s x ≠ Model.NIL) {u : List ℕ} (hu : u = [cLs s x] ∨ ∃ (a : ℕ), ∃ (b : ℕ), u = [a, b, cLs s x] ∧ Model.unitOK s x u = true) :
x ∈ A.chunks ∧ ∃ (rest : List ℕ), A.grps x = rest ++ u

The right unit is a suffix of the chunk's run.

Labels for moved groups #

theorem OrderMaintenance.Proof.pushBackLabs_run (s : Model.Core) (center D B k : ℕ) (prev : Option ℕ) :
(∃ (labs : List ℕ), EStateM.run (Model.pushBackLabs center D B prev k) s = EStateM.Result.ok labs s ∧ labs.length = k ∧ List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) labs ∧ ∀ (p : ℕ), prev = some p → ∀ (l : ℕ), l ∈ labs.head? → p < l) ∨ ∃ (msg : String), EStateM.run (Model.pushBackLabs center D B prev k) s = EStateM.Result.error (Model.Err.guard msg) s
theorem OrderMaintenance.Proof.pushFrontLabs_run (s : Model.Core) (center D k : ℕ) (nxt : Option ℕ) :
(∃ (labs : List ℕ), EStateM.run (Model.pushFrontLabs center D nxt k) s = EStateM.Result.ok labs s ∧ labs.length = k ∧ List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) labs ∧ ∀ (n : ℕ), nxt = some n → ∀ (l : ℕ), l ∈ labs.getLast? → l < n) ∨ ∃ (msg : String), EStateM.run (Model.pushFrontLabs center D nxt k) s = EStateM.Result.error (Model.Err.guard msg) s

Running moveUnit #

theorem OrderMaintenance.Proof.moveUnit_left_run (s : Model.Core) (x : ℕ) :
(∃ (u : List ℕ), ∃ (labs : List ℕ), cF s x ≠ Model.NIL ∧ (u = [cF s x] ∨ ∃ (a : ℕ), ∃ (b : ℕ), u = [cF s x, a, b] ∧ Model.unitOK s x u = true) ∧ cN s (s.C.getX x).l = x ∧ labs.length = u.length ∧ List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) labs ∧ (cF s (s.C.getX x).l ≠ Model.NIL → ∀ (l : ℕ), l ∈ labs.head? → gL s (cLs s (s.C.getX x).l) < l) ∧ EStateM.run (Model.moveUnit x true) s = EStateM.Result.ok ((s.C.getX x).l, u) (Model.moveGroupsLeft s x (s.C.getX x).l u labs)) ∨ ∃ (msg : String), EStateM.run (Model.moveUnit x true) s = EStateM.Result.error (Model.Err.guard msg) s
theorem OrderMaintenance.Proof.moveUnit_right_run (s : Model.Core) (x : ℕ) :
(∃ (u : List ℕ), ∃ (labs : List ℕ), cF s x ≠ Model.NIL ∧ (u = [cLs s x] ∨ ∃ (a : ℕ), ∃ (b : ℕ), u = [a, b, cLs s x] ∧ Model.unitOK s x u = true) ∧ cP s (s.C.getX x).r = x ∧ labs.length = u.length ∧ List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) labs ∧ (cF s (s.C.getX x).r ≠ Model.NIL → ∀ (l : ℕ), l ∈ labs.getLast? → l < gL s (cF s (s.C.getX x).r)) ∧ EStateM.run (Model.moveUnit x false) s = EStateM.Result.ok ((s.C.getX x).r, u) (Model.moveGroupsRight s x (s.C.getX x).r u labs)) ∨ ∃ (msg : String), EStateM.run (Model.moveUnit x false) s = EStateM.Result.error (Model.Err.guard msg) s

The records after a chunk-level move #

theorem OrderMaintenance.Proof.foldChk_getX (u labs : List ℕ) (y : ℕ) (a : Array Model.Group) (hnd : u.Nodup) (hlen : labs.length = u.length) (hsz : ∀ (g : ℕ), g ∈ u → g < a.size) (b : ℕ) :
(List.foldl (fun (a : Array Model.Group) (p : ℕ × ℕ) => a.modify p.fst fun (G : Model.Group) => { next := G.next, prev := G.prev, first := G.first, last := G.last, chk := y, lab := p.snd, size := G.size, st := G.st, src := G.src, l := G.l, r := G.r, alive := G.alive }) a (u.zip labs)).getX b = if b ∈ u then have __src := a.getX b; { next := __src.next, prev := __src.prev, first := __src.first, last := __src.last, chk := y, lab := labs.getD (List.idxOf b u) 0, size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, alive := __src.alive } else a.getX b
theorem OrderMaintenance.Proof.foldChk_size (u labs : List ℕ) (y : ℕ) (a : Array Model.Group) :
(List.foldl (fun (a : Array Model.Group) (p : ℕ × ℕ) => a.modify p.fst fun (G : Model.Group) => { next := G.next, prev := G.prev, first := G.first, last := G.last, chk := y, lab := p.snd, size := G.size, st := G.st, src := G.src, l := G.l, r := G.r, alive := G.alive }) a (u.zip labs)).size = a.size
theorem OrderMaintenance.Proof.map_idxOf_labs {u labs : List ℕ} (hnd : u.Nodup) (hlen : labs.length = u.length) :
List.map (fun (b : ℕ) => labs.getD (List.idxOf b u) 0) u = labs
theorem OrderMaintenance.Proof.moveGroupsLeft_C {s : Model.Core} {x y : ℕ} {u labs : List ℕ} (hxy : x ≠ y) (hx : x < s.C.size) (hy : y < s.C.size) (z : ℕ) :
(Model.moveGroupsLeft s x y u labs).C.getX z = if z = y then have __src := s.C.getX y; { next := __src.next, prev := __src.prev, first := if cF s y = Model.NIL then u.headD Model.NIL else cF s y, last := u.getLastD Model.NIL, size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, tag := __src.tag, alive := __src.alive } else if z = x then if cLs s x = u.getLastD Model.NIL then have __src := s.C.getX x; { next := __src.next, prev := __src.prev, size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, tag := __src.tag, alive := __src.alive } else have __src := s.C.getX x; { next := __src.next, prev := __src.prev, first := gN s (u.getLastD Model.NIL), last := __src.last, size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, tag := __src.tag, alive := __src.alive } else s.C.getX z
theorem OrderMaintenance.Proof.moveGroupsRight_C {s : Model.Core} {x z : ℕ} {u labs : List ℕ} (hxz : x ≠ z) (hx : x < s.C.size) (hz : z < s.C.size) (w : ℕ) :
(Model.moveGroupsRight s x z u labs).C.getX w = if w = z then have __src := s.C.getX z; { next := __src.next, prev := __src.prev, first := u.headD Model.NIL, last := if cF s z = Model.NIL then u.getLastD Model.NIL else cLs s z, size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, tag := __src.tag, alive := __src.alive } else if w = x then if cF s x = u.headD Model.NIL then have __src := s.C.getX x; { next := __src.next, prev := __src.prev, size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, tag := __src.tag, alive := __src.alive } else have __src := s.C.getX x; { next := __src.next, prev := __src.prev, first := __src.first, last := gP s (u.headD Model.NIL), size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, tag := __src.tag, alive := __src.alive } else s.C.getX w
theorem OrderMaintenance.Proof.moveGroups_G {s : Model.Core} {d : ℕ} {u labs : List ℕ} (hnd : u.Nodup) (hlen : labs.length = u.length) (hsz : ∀ (g : ℕ), g ∈ u → g < s.Gs.size) (b : ℕ) :
(List.foldl (fun (a : Array Model.Group) (p : ℕ × ℕ) => a.modify p.fst fun (G : Model.Group) => { next := G.next, prev := G.prev, first := G.first, last := G.last, chk := d, lab := p.snd, size := G.size, st := G.st, src := G.src, l := G.l, r := G.r, alive := G.alive }) s.Gs (u.zip labs)).getX b = if b ∈ u then have __src := s.Gs.getX b; { next := __src.next, prev := __src.prev, first := __src.first, last := __src.last, chk := d, lab := labs.getD (List.idxOf b u) 0, size := __src.size, st := __src.st, src := __src.src, l := __src.l, r := __src.r, alive := __src.alive } else s.Gs.getX b

What a chunk-level move does to the groups: the moved ones get chunk d and new labels.

theorem OrderMaintenance.Proof.INV.mem_of_cN {s : Model.Core} {A : Abs} (h : INV s A) {a b : ℕ} (hab : cN s a = b) (hb : b ≠ Model.NIL) :
theorem OrderMaintenance.Proof.INV.mem_of_cP {s : Model.Core} {A : Abs} (h : INV s A) {a b : ℕ} (hab : cP s a = b) (hb : b ≠ Model.NIL) :
theorem OrderMaintenance.Proof.moveGroupsLeft_chunkEnds {s : Model.Core} {A : Abs} (hI : INV s A) {x y : ℕ} {u rest labs : List ℕ} (hx : x ∈ A.chunks) (hy : y ∈ A.chunks) (hxy : x ≠ y) (hrun : A.grps x = u ++ rest) (hu : u ≠ []) (c : ℕ) :
c ∈ A.chunks → Ends (cF (Model.moveGroupsLeft s x y u labs) c) (cLs (Model.moveGroupsLeft s x y u labs) c) (if c = y then A.grps y ++ u else if c = x then rest else A.grps c)

Moving a prefix of groups updates exactly the source and destination chunk ends.

theorem OrderMaintenance.Proof.inv_moveGroupsLeft {s : Model.Core} {A : Abs} (hI : INV s A) {x y : ℕ} {u rest labs : List ℕ} (hx : x ∈ A.chunks) (hrun : A.grps x = u ++ rest) (hu : u ≠ []) (hadj : cN s y = x) (hlen : labs.length = u.length) (hch : List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) labs) (hlab : cF s y ≠ Model.NIL → ∀ (l : ℕ), l ∈ labs.head? → gL s (cLs s y) < l) :
∃ (A' : Abs), INV (Model.moveGroupsLeft s x y u labs) A' ∧ A'.elist = A.elist

Moving a prefix u of chunk x to the end of the chunk y before it preserves INV-S.

theorem OrderMaintenance.Proof.moveGroupsRight_chunkEnds {s : Model.Core} {A : Abs} (hI : INV s A) {x z : ℕ} {u rest labs : List ℕ} (hx : x ∈ A.chunks) (hz : z ∈ A.chunks) (hxz : x ≠ z) (hrun : A.grps x = rest ++ u) (hu : u ≠ []) (c : ℕ) :
c ∈ A.chunks → Ends (cF (Model.moveGroupsRight s x z u labs) c) (cLs (Model.moveGroupsRight s x z u labs) c) (if c = z then u ++ A.grps z else if c = x then rest else A.grps c)

Moving a suffix of groups updates exactly the source and destination chunk ends.

theorem OrderMaintenance.Proof.inv_moveGroupsRight {s : Model.Core} {A : Abs} (hI : INV s A) {x z : ℕ} {u rest labs : List ℕ} (hx : x ∈ A.chunks) (hrun : A.grps x = rest ++ u) (hu : u ≠ []) (hadj : cP s z = x) (hlen : labs.length = u.length) (hch : List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) labs) (hlab : cF s z ≠ Model.NIL → ∀ (l : ℕ), l ∈ labs.getLast? → l < gL s (cF s z)) :
∃ (A' : Abs), INV (Model.moveGroupsRight s x z u labs) A' ∧ A'.elist = A.elist

Moving a suffix u of chunk x to the front of the chunk z after it preserves INV-S.