Documentation

LeanPool.OrderMaintenance.Proof.Correct.Lists

Linked lists as succOf / predOf #

Lemmas for reading and updating the doubly linked lists of INV-S: successors and predecessors in appended and flattened lists, and sortedness under a change at one position.

succOf #

theorem OrderMaintenance.Proof.succOf_cons_cons (a b : ℕ) (t : List ℕ) (x : ℕ) :
succOf (a :: b :: t) x = if a = x then b else succOf (b :: t) x
theorem OrderMaintenance.Proof.succOf_append {l1 l2 : List ℕ} (hnd : l1.Nodup) (x : ℕ) :
succOf (l1 ++ l2) x = if x ∈ l1 then if l1.getLast? = some x then l2.headD Model.NIL else succOf l1 x else succOf l2 x

Successors in an append of disjoint lists.

theorem OrderMaintenance.Proof.succOf_mid {l1 l2 : List ℕ} {x : ℕ} (hnd : (l1 ++ x :: l2).Nodup) :
succOf (l1 ++ x :: l2) x = l2.headD Model.NIL

In l1 ++ x :: l2 with x ∉ l1, the successor of x is the head of l2.

predOf #

theorem OrderMaintenance.Proof.predOf_append {l1 l2 : List ℕ} (hnd : l2.Nodup) (x : ℕ) :
predOf (l1 ++ l2) x = if x ∈ l2 then if l2.head? = some x then l1.getLastD Model.NIL else predOf l2 x else predOf l1 x
theorem OrderMaintenance.Proof.predOf_mid {l1 l2 : List ℕ} {x : ℕ} (hnd : (l1 ++ x :: l2).Nodup) :
predOf (l1 ++ x :: l2) x = l1.getLastD Model.NIL

Blocks of a flattened list #

theorem OrderMaintenance.Proof.succOf_flatten {L : List (List ℕ)} (hnd : L.flatten.Nodup) {b : List ℕ} (hb : b ∈ L) {x : ℕ} (hx : x ∈ b) (hlast : b.getLast? ≠ some x) :

Inside a block, the successor in the flattened list is the successor in the block, unless the element is the block's last.

theorem OrderMaintenance.Proof.predOf_flatten {L : List (List ℕ)} (hnd : L.flatten.Nodup) {b : List ℕ} (hb : b ∈ L) {x : ℕ} (hx : x ∈ b) (hfirst : b.head? ≠ some x) :
theorem OrderMaintenance.Proof.succOf_flatten_of_mem {L : List (List ℕ)} (hnd : L.flatten.Nodup) (hnil : ¬Model.NIL ∈ L.flatten) {b : List ℕ} (hb : b ∈ L) {x : ℕ} (hx : x ∈ b) (hs : succOf L.flatten x ∈ b) :

If the successor of a block member lies in the same block, it is the successor in the block.

Sortedness after changing one position #

theorem OrderMaintenance.Proof.isChain_update {l : List ℕ} (hnd : l.Nodup) {f f' : ℕ → ℕ} (hs : List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) (List.map f l)) {x : ℕ} (hx : x ∈ l) (hother : ∀ (y : ℕ), y ∈ l → y ≠ x → f' y = f y) (hpred : predOf l x ≠ Model.NIL → f (predOf l x) < f' x) (hsucc : succOf l x ≠ Model.NIL → f' x < f (succOf l x)) (hnil : ¬Model.NIL ∈ l) :
List.IsChain (fun (x1 x2 : ℕ) => x1 < x2) (List.map f' l)

Changing the value at one member of a sorted list keeps it sorted when the new value lies strictly between the values at its neighbours.

theorem OrderMaintenance.Proof.isChain_insert_relation {R : ℕ → ℕ → Prop} {l1 l2 : List ℕ} {x y : ℕ} {f f' : ℕ → ℕ} (hs : List.IsChain R (List.map f (l1 ++ x :: l2))) (hagree : ∀ (z : ℕ), z ∈ l1 ++ x :: l2 → f' z = f z) (hxy : R (f x) (f' y)) (hy2 : ∀ (w : ℕ), w ∈ l2.head? → R (f' y) (f w)) :
List.IsChain R (List.map f' (l1 ++ x :: y :: l2))

IsChain survives inserting y right after x, for any relation.

Successors and predecessors after list surgery #

theorem OrderMaintenance.Proof.predOf_cons {a : ℕ} {l : List ℕ} (hnd : (a :: l).Nodup) (z : ℕ) :
theorem OrderMaintenance.Proof.succOf_insert {l1 l2 : List ℕ} {x y : ℕ} (hnd : (l1 ++ x :: y :: l2).Nodup) (z : ℕ) :
succOf (l1 ++ x :: y :: l2) z = if z = x then y else if z = y then l2.headD Model.NIL else succOf (l1 ++ x :: l2) z

Successors after inserting y right after x.

theorem OrderMaintenance.Proof.predOf_insert {l1 l2 : List ℕ} {x y : ℕ} (hnd : (l1 ++ x :: y :: l2).Nodup) (z : ℕ) :
predOf (l1 ++ x :: y :: l2) z = if z = y then x else if l2.head? = some z then y else predOf (l1 ++ x :: l2) z

Predecessors after inserting y right after x.

theorem OrderMaintenance.Proof.succOf_insert_front {l : List ℕ} {a : ℕ} (_hnd : (a :: l).Nodup) (z : ℕ) :
succOf (a :: l) z = if z = a then l.headD Model.NIL else succOf l z

Successors after inserting a at the front.

theorem OrderMaintenance.Proof.predOf_insert_front {l : List ℕ} {a : ℕ} (hnd : (a :: l).Nodup) (z : ℕ) :
predOf (a :: l) z = if l.head? = some z then a else predOf l z

Predecessors after inserting a at the front.

theorem OrderMaintenance.Proof.succOf_erase {l1 l2 : List ℕ} {x : ℕ} (hnd : (l1 ++ x :: l2).Nodup) {z : ℕ} (hzx : z ≠ x) :
succOf (l1 ++ l2) z = if l1.getLast? = some z then l2.headD Model.NIL else succOf (l1 ++ x :: l2) z

Successors after removing x from l1 ++ x :: l2.

theorem OrderMaintenance.Proof.predOf_erase {l1 l2 : List ℕ} {x : ℕ} (hnd : (l1 ++ x :: l2).Nodup) {z : ℕ} (hzx : z ≠ x) :
predOf (l1 ++ l2) z = if l2.head? = some z then l1.getLastD Model.NIL else predOf (l1 ++ x :: l2) z

Predecessors after removing x from l1 ++ x :: l2.

Linked-list updates #

theorem OrderMaintenance.Proof.LList.insert_after {nxt prv nxt' prv' : ℕ → ℕ} {hd : ℕ} {l1 l2 : List ℕ} {x y : ℕ} (h : LList nxt prv hd (l1 ++ x :: l2)) (hy : ¬y ∈ l1 ++ x :: l2) (hy0 : y ≠ Model.NIL) (hn : ∀ (z : ℕ), z ∈ l1 ++ x :: y :: l2 → nxt' z = if z = x then y else if z = y then nxt x else nxt z) (hp : ∀ (z : ℕ), z ∈ l1 ++ x :: y :: l2 → prv' z = if z = y then x else if z = nxt x then y else prv z) :
LList nxt' prv' hd (l1 ++ x :: y :: l2)

Inserting y right after x.

theorem OrderMaintenance.Proof.LList.insert_front {nxt prv nxt' prv' : ℕ → ℕ} {hd : ℕ} {l : List ℕ} {a : ℕ} (h : LList nxt prv hd l) (ha : ¬a ∈ l) (ha0 : a ≠ Model.NIL) (hn : ∀ (z : ℕ), z ∈ a :: l → nxt' z = if z = a then hd else nxt z) (hp : ∀ (z : ℕ), z ∈ a :: l → prv' z = if z = a then Model.NIL else if z = hd then a else prv z) :
LList nxt' prv' a (a :: l)

Inserting a at the front.

theorem OrderMaintenance.Proof.LList.erase {nxt prv nxt' prv' : ℕ → ℕ} {hd : ℕ} {l1 l2 : List ℕ} {x : ℕ} (h : LList nxt prv hd (l1 ++ x :: l2)) (hn : ∀ (z : ℕ), z ∈ l1 ++ l2 → nxt' z = if z = prv x then nxt x else nxt z) (hp : ∀ (z : ℕ), z ∈ l1 ++ l2 → prv' z = if z = nxt x then prv x else prv z) :
LList nxt' prv' (if prv x = Model.NIL then nxt x else hd) (l1 ++ l2)

Removing x from l1 ++ x :: l2.

theorem OrderMaintenance.Proof.LList.insert_around {nxt prv nxt' prv' : ℕ → ℕ} {hd : ℕ} {l1 l2 : List ℕ} {g a b : ℕ} (h : LList nxt prv hd (l1 ++ g :: l2)) (ha : ¬a ∈ l1 ++ g :: l2) (hb : ¬b ∈ l1 ++ g :: l2) (hab : a ≠ b) (ha0 : a ≠ Model.NIL) (hb0 : b ≠ Model.NIL) (hn : ∀ (z : ℕ), z ∈ l1 ++ a :: g :: b :: l2 → nxt' z = if z = a then g else if z = g then b else if z = b then nxt g else if z = prv g then a else nxt z) (hp : ∀ (z : ℕ), z ∈ l1 ++ a :: g :: b :: l2 → prv' z = if z = a then prv g else if z = g then a else if z = b then g else if z = nxt g then b else prv z) :
LList nxt' prv' (if prv g = Model.NIL then a else hd) (l1 ++ a :: g :: b :: l2)

Inserting a right before and b right after g.

theorem OrderMaintenance.Proof.getLastD_append_cons (l : List ℕ) (a : ℕ) (t : List ℕ) (d : ℕ) :
(l ++ a :: t).getLastD d = (a :: t).getLastD d
theorem OrderMaintenance.Proof.headD_append_cons (l : List ℕ) (a : ℕ) (t : List ℕ) (d : ℕ) :
(l ++ a :: t).headD d = l.headD a
theorem OrderMaintenance.Proof.mem_insert_iff {l1 l2 : List ℕ} {x y e : ℕ} :
e ∈ l1 ++ x :: y :: l2 ↔ e = y ∨ e ∈ l1 ++ x :: l2
theorem OrderMaintenance.Proof.succOf_ne_self {l : List ℕ} (hnd : l.Nodup) (x : ℕ) (hx : x ≠ Model.NIL) :
succOf l x ≠ x
theorem OrderMaintenance.Proof.predOf_ne_self {l : List ℕ} (hnd : l.Nodup) (x : ℕ) (hx : x ≠ Model.NIL) :
predOf l x ≠ x
theorem OrderMaintenance.Proof.predOf_ne_succOf {l : List ℕ} (hnd : l.Nodup) {x : ℕ} (hx : x ∈ l) (hp : predOf l x ≠ Model.NIL) :
predOf l x ≠ succOf l x
theorem OrderMaintenance.Proof.mem_around_iff {l1 l2 : List ℕ} {g a b z : ℕ} :
z ∈ l1 ++ a :: g :: b :: l2 ↔ z = a ∨ z = b ∨ z ∈ l1 ++ g :: l2
theorem OrderMaintenance.Proof.mem_split_of_mem {l1 l2 : List ℕ} {g z : ℕ} (h : z ∈ l1 ++ l2) :
z ∈ l1 ++ g :: l2
theorem OrderMaintenance.Proof.succOf_split {l : List ℕ} {a b : ℕ} (h : succOf l a = b) (hb : b ≠ Model.NIL) :
∃ (l1 : List ℕ), ∃ (l2 : List ℕ), l = l1 ++ a :: b :: l2

In a list without duplicates, succOf l a = b ≠ NIL splits l around the pair a, b.

theorem OrderMaintenance.Proof.predOf_split {l : List ℕ} {a b : ℕ} (h : predOf l b = a) (ha : a ≠ Model.NIL) :
∃ (l1 : List ℕ), ∃ (l2 : List ℕ), l = l1 ++ a :: b :: l2

predOf l b = a ≠ NIL splits l around the pair a, b.