Proofs: Binary random-access list
These are the human proofs of Binary random-access list, at full length.
The binary random-access list keeps its invariant and implements a persistent list full
This proof shows: correctness, termination.
We use the pseudocode and the invariant (B1) to (B3) of the page. For a leaf tree , is the sequence of the items of its leaves from left to right, and . For a digit list , is the concatenation of over the One digits in increasing order of . We say that a digit list is valid from rank if each of has complete of rank , and its last digit, if any, is a One. A list is valid when it is valid from rank 0; this is (B1) and (B2), and (B3) is the definition of .
The facts about trees that we use: if and are complete of rank , then LINK is complete of rank and has sequence ; and a complete tree of rank has children complete of rank , with and . Both follow from the definitions.
Lemma 1 (CONS-TREE). If is valid from rank and is complete of rank , then CONS-TREE terminates and returns a list that is valid from rank with .
Proof. By induction on the length of . If is empty (line L2), , valid from rank , with sequence . If (line L3), replaces it by One , of rank ; the other digits and the last digit are unchanged (the last digit is not , since a valid list does not end in Zero), and the new first tree comes before all others, so . If (lines L4 and L5), has rank , so LINK is complete of rank with sequence . The rest of is valid from rank (it is empty or ends in a One) and is shorter, so by the hypothesis the recursive call returns valid from rank with . Then is valid from rank , since is non-empty and ends in a One, and .
Lemma 2 (UNCONS-TREE). If is non-empty and valid from rank , then UNCONS-TREE terminates and returns where is complete of rank , is valid from rank , and .
Proof. By induction on the length of . If (line L7), has rank and . If and has more digits (line L8), then , whose last digit is the last digit of , a One; so is valid from rank , and . If (line L9), then is non-empty, because a valid list does not end in Zero, and it is valid from rank . By the hypothesis, the call returns a tree complete of rank , so with and complete of rank , and a list valid from rank with . Line L10 returns and , which is valid from rank (if is empty its last digit is One ), and .
Lemma 3 (CONS, HEAD, TAIL). Let be valid with . CONS is valid with sequence . If , HEAD and TAIL is valid with sequence . If , both report an error.
Proof. A leaf is complete of rank 0, so Lemma 1 with gives the claim for CONS. A valid list is empty exactly when , since every One digit holds at least one item; then line L6 reports the error. Otherwise, by Lemma 2 with , UNCONS-TREE returns a tree of rank 0, which is a leaf , and a valid with . So and .
Lemma 4 (LOOKUP). If is valid with , then LOOKUP returns for and reports an error for .
Proof. Let the One digits of hold the trees in digit order, and let . The loop of line L13 keeps this invariant: before the digits of are examined, the variable equals , where is the input index, and . It holds at the start (). Zero digits change nothing. At the digit of , if , then , so is the item of index in , and the descent starts. Otherwise line L19 subtracts , which gives with , and the invariant holds for . If the loop ends, , and line L20 reports the error correctly.
The descent of lines L16 to L18 keeps the invariant "the wanted item is the item of index in , and ". At a node with , the item of index is in at index if , and in at index otherwise, which is line L17. Each step goes one level down, so the loop stops after at most the rank of the tree, at a leaf with , whose item is the wanted one.
Lemma 5 (UPDATE). If is valid with and , then UPDATE returns a valid list whose sequence is that of with replaced by .
Proof. UPDATE-TREE, for complete and , returns a tree of the same shape with the item of index replaced by : by induction on the rank, a leaf has (line L25), and at a node the same case split as in Lemma 4 sends the change to the correct child and keeps the other child and (lines L27 and L28). UPDATE then follows the same walk over the digits as LOOKUP, with the same invariant on , and rebuilds the digit cells before the changed tree with the same digits (lines L21 and L24) and the changed tree in the same place (line L23). Ranks, the last digit and the order of the trees do not change, so the result is valid, and its sequence differs only at position . The precondition means that line L23 is reached before the list ends.
Persistence. No line assigns to a field of an existing digit cell or tree node: lines L2, L3, L5, L7, L8, L10, L21, L23, L24, L27 and L28 build new cells and nodes, and share old ones only as children. So every list built earlier keeps its digits, trees and sequence.
Theorem. Every list built from the empty list by CONS, TAIL and UPDATE, applied to any earlier versions, is valid, and each operation returns the result that the persistent list type specifies. Each operation terminates.
Proof. The empty list is valid. By persistence, an earlier version is still valid when it is used again. Lemmas 3, 4 and 5 give the results and the validity of the new versions. Termination: CONS-TREE and UNCONS-TREE recurse on a shorter digit list, the loops of LOOKUP run over a finite digit list and down a finite tree, and UPDATE and UPDATE-TREE recurse on a shorter digit list or a lower rank.