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 #
predOf #
Blocks of a flattened list #
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 #
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)
:
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)
:
Inserting a at the front.
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)
:
Inserting a right before and b right after g.