Proofs: Skew binary random-access list
These are the human proofs of Skew binary random-access list, at full length.
The skew binary random-access list implements a persistent list full
This proof shows: correctness.
Setting. A version is a list of pairs . For a complete tree let be its items in preorder, and let the items of the version be . The version is good if (I1) each with and is a complete tree with nodes, (I2) , and (I3) .
Lemma 1 (cons). If is good with items , then CONS is good with items .
Proof. Case and (lines L1 and L2). The new tree is complete with nodes, and its preorder is , so the items are right. By (I2), , and is of the form with , so . If , the new list starts with two equal weights, which (I2) allows; any later weights are larger than . So (I2) holds, and (I3) holds with . Otherwise (line L3), the new first weight is 1. If and , then (because the first case did not apply), so after the new weight 1 the sequence is with , and (I2) holds. If or , (I2) is clear. The items are .
Lemma 2 (head and tail). If is good and not empty, HEAD is its first item, and TAIL is good with the other items.
Proof. The first item is the first item of , which is the root of (line L4). If (line L6), dropping removes exactly that item, and a suffix of a sequence that satisfies (I2) satisfies it. Otherwise with and complete of weight (line L7), and , so the new items are right. The new weights are ; since , (I2) holds. (I3) holds with .
Lemma 3 (tree walk). Let be complete with weight and . The loop of lines L10 to L12 ends at the node that holds item of .
Proof. By induction on . If the item is the root. Otherwise with and of weight . In preorder, positions are the items of , at positions of , and positions are the items of , at positions of . Line L12 goes to with when , and to with otherwise, and both are in range for a tree of weight . The claim follows by induction.
Lemma 4 (lookup and update). If is good and , LOOKUP returns item , and UPDATE returns a good version whose items are those of with item set to , while is unchanged.
Proof. Line L8 keeps the item of the current index the same: skipping the tree removes its items from the front, and lowers by . It stops at the first tree with , which exists by (I3) because . Lemma 3 finishes LOOKUP. UPDATE follows the same walk (line L14), makes a new node with item and the same children (line L15), and copies each node on the path with the new child in place of the old one (line L16). The new tree has the same shape, so it is complete of weight , and its preorder differs from the old one only at item . Line L17 rebuilds the cells in front with the same weights and trees. So the new version is good and has the right items. No line writes to an existing node or cell, so is unchanged.
Theorem. Every version made by the operations is good, every operation returns the result of the abstract persistent list on its argument, and no operation changes an existing version.
Proof. The empty list is good, with no items. By Lemmas 1, 2 and 4 and induction on the number of operations, every version is good and has the items of the abstract list. All operations build new cells and nodes that point to old ones and never write to existing memory, so each old version keeps its items.