Proofs: Piece table
These are the human proofs of Piece table, at full length.
The piece table operations keep the invariant and implement the list operations full
This proof shows: correctness.
We use the invariant (P1) to (P3) of the page. Write for the prefix sums of the piece lengths, so and by (P2). By (P2), position of the text, for , is character of .
Lemma 1 (FIND). For , FIND returns the unique with and . For it returns .
Proof. Before the round for , : this holds for , and line L4 subtracts , which makes it . The round returns at the first with , that is , and holds since the earlier rounds did not return. By (P1) the are strictly increasing, so this is unique. If no round returns, then , so .
CHAR-AT. By Lemma 1 and the remark above, line L22 returns position of the text, and is in range by (P1).
INSERT. Let and before line L7. After line L7, , and by (P3) the strings of the old pieces do not change. Let be the text before the call. We show that the new piece list spells .
- Line L9. Here , so : position is the boundary after . The piece with names , and the new piece names . So the text becomes , as required. The new piece is in range since .
- Line L10. Again (including , ), and the new piece names , put between and .
- Line L11. Here , and . The three new pieces spell , and position is offset in by Lemma 1. All three have positive length because , and they are in range.
In each case the new pieces have length at least 1 and lie in their buffers, so (P1) holds; the text is as required and grows by , so (P2) holds; only appends were made to , so (P3) holds.
DELETE. Let and let be the text before the call. Call the characters at positions to the doomed characters. We prove that the loop keeps this claim: the current piece list spells with the doomed characters that were already removed taken out; the pieces before index hold no doomed character; the doomed characters that remain are the first characters starting at offset of and the pieces after it; and (P1) holds.
At the start, the claim holds by Lemma 1, with nothing removed yet. In a round, is the number of remaining doomed characters inside , at offsets to .
- Line L16 (, ): the whole piece is doomed and is removed; the next piece is now at index , and its offset 0 is the next doomed character.
- Line L17 (, ): the head of length is removed and the rest is kept; then , so the loop ends after this round.
- Line L18 (, ): the tail is removed; the next doomed character, if any, is at offset 0 of , which becomes the current piece.
- Line L19 (, ): the middle is removed, and the head and the tail are kept as two pieces of positive length; again .
In each case the pieces made have positive length and lie in their buffers, so (P1) holds, and line L20 updates and sets in agreement with the claim. Each round removes characters, because (by Lemma 1 for the first round, and later), so the loop ends, and at the end all doomed characters are removed. The buffers are untouched, so (P3) holds, and line L12 sets to the new length, so (P2) holds.