Proofs: Catenable deque
These are the human proofs of Catenable deque, at full length.
The catenable deque operations keep the invariant, compute the right sequences and run in the stated time full
This proof shows: correctness, termination, complexity.
We use the definitions of the page. For an element of level , is if , and the concatenation of the items of its children otherwise. For a tuple of elements, is the concatenation of the items of its elements. For a c-deque, and . A valid c-deque of level is a c-deque of level that satisfies (C1) to (C3). Its depth is the number of Deep levels on its spine: 0 for Shallow, and for . A valid c-deque is empty exactly when it is , because a Deep one has at least 2 elements in its front.
No line of the pseudocode writes into a tuple or a node that exists before the call: every result is a new node whose parts are new or old. So every earlier version keeps its sequence, and it suffices to prove the claims about the results.
Lemma 1 (CONS). If is a valid c-deque of level and an element of level , then CONS returns a valid c-deque of level with sequence , after at most calls of CONS.
Proof. By induction on . Line L1 returns a Shallow tuple of at most 4 elements. Line L2 returns a Deep c-deque with a front of 2 and a rear of 3 elements and an empty middle, which is valid, and its sequence is , as required. Otherwise . If , line L4 makes a front of 3 or 4 elements. If , the tuple is an element of level by (C1), and is a valid c-deque of level of depth . By the hypothesis, the call of line L6 returns a valid c-deque of level with , after at most calls. The new front has 2 elements, and the sequence is .
By symmetry, SNOC returns a valid c-deque with sequence after at most calls.
Lemma 2 (HEAD and TAIL). If is a non-empty valid c-deque of level , then HEAD is an element of level , TAIL is a valid c-deque of level , , and TAIL makes at most calls of TAIL.
Proof. By induction on . HEAD returns the first element of or of . Line L7 drops the first element of a non-empty Shallow tuple. Otherwise and HEAD is the first element of . If , line L9 leaves a front of 2 or 3 elements. Otherwise (line L10). If is not empty, the hypothesis applies to , of smaller depth: is an element of level , a tuple of 2 or 3 elements of level by (C1), and TAIL is valid with . The front of line L11 has 3 or 4 elements, and the sequence of the result is , which is without . If is empty, , and has elements, from 3 to 5. Line L13 returns it as a Shallow tuple if it has at most 4, and line L14 splits 5 elements into a front of 2 and a rear of 3. In both cases the sequence is .
By symmetry, LAST and INIT satisfy the mirror statement.
Lemma 3 (PUSH-FRONT and PUSH-BACK). If is a tuple of 2 to 4 elements of level and a valid c-deque of level , then PUSH-FRONT is a valid c-deque of level with sequence , and PUSH-BACK is one with sequence . Each makes at most two calls of CONS or SNOC at the top, and the c-deques they apply to have depth at most .
Proof. If , is itself an element of level , and line L15 is Lemma 1. If , the pairs and are elements of level , and two uses of Lemma 1 give the sequence . A CONS adds at most one Deep level, since only line L2 turns a Shallow c-deque into a Deep one, at the bottom of the spine. PUSH-BACK is symmetric.
Lemma 4 (FROM-TUPLE). For a tuple of at most 8 elements of level , FROM-TUPLE is a valid c-deque of level with sequence .
Proof. If , it is a Shallow tuple. If , the front has and the rear elements, both between 2 and 4, and the middle is empty.
Lemma 5 (CAT). If and are valid c-deques of level , then CAT returns a valid c-deque of level with sequence .
Proof. By induction on , the number of items in . Line L17: , so Lemma 4 applies. Lines L18 to L20: . If , the result is ; if , it is CONS of the one element onto , by Lemma 1. If , is a valid front, and by Lemma 3 PUSH-FRONT is a valid c-deque of level with sequence . The sequence of the result is . Lines L21 to L23 are symmetric. Line L24: both are Deep. By Lemma 3, and are valid of level , with sequences and . The c-deque holds the items of except those of , and is not empty, so , and the hypothesis applies to CAT. The result has sequence , which is .
The induction also proves that every call terminates: each recursive call of CONS and TAIL is on a c-deque of smaller depth, and each recursive call of CAT is on a first argument with fewer items.
Lemma 6 (depth). A valid c-deque of level 0 with items has depth at most when it is Deep. More generally, if a valid c-deque of level with items is Deep, then .
Proof. An element of level holds at least items, by induction on with (C1). A Deep c-deque of level has at least 4 elements of level in its front and rear, so . If a c-deque of level 0 has depth , the c-deque at the bottom of its spine of Deep levels is Deep of level and holds at most items, so .
Theorem. On valid c-deques with at most items, CONS, SNOC, TAIL and INIT take time in the worst case, HEAD and LAST , and CAT takes , where and are the sizes of and . Each operation builds new nodes and tuples of size per unit of time, so the space of operations is .
Proof. Every line except the recursive calls does work, because tuples have at most 8 elements. By Lemmas 1 and 2, CONS and TAIL make at most calls, which is by Lemma 6. In CAT, consider the call at nesting level that reaches line L24. Its arguments are valid of level and hold at most and items, since every recursive argument holds a subset of the items of the argument above it. Both are Deep, so by Lemma 6 , and there are at most levels of recursion. Each level does at most four calls of CONS or SNOC, inside PUSH-BACK and PUSH-FRONT, or in lines L19 and L22, on c-deques of at most items, each by Lemmas 1 and 6.