Proofs: Array
These are the human proofs of Array, at full length.
The array operations keep the list and return the right result full
This proof shows: correctness.
Setting. The array has capacity , a count and cells . The list held by the array is .
Invariant (I). .
We show that each operation, started in a state where (I) holds and its precondition holds, ends in a state where (I) holds, holds the list that the list type asks for, and returns the right value. We write for the list before the operation, so is the old value of .
GET, . Line L1 returns , the item at position . Nothing is written, so and (I) do not change.
SET, . Line L2 writes only cell , so the new list is , and does not change.
INSERT, . The goal is the list of length .
Loop invariant (J). At each test of line L3 with the current value , where : for , and for .
- Initialization. Before the first test . No cell has been written, so for , and the second range is empty. All indices used are less than because .
- Maintenance. Assume (J) at the test with value . Line L4 sets . Since , the first part of (J) gives , so now . No other cell changes. Then becomes . For the cell is unchanged and equals . For : was just written, and for the cell is unchanged and equals by (J). So (J) holds for .
- Termination. Each iteration decreases by exactly 1, from , and the loop stops at the first test with . So it runs exactly times. At exit, (J) with gives for and for .
Line L5 writes to , which is the only index not covered by the two ranges. Line L6 sets the count to , so (I) holds. The cells now read , which is the goal.
DELETE, . The goal is to return and to leave the list of length . Line L7 saves before any cell is written.
Loop invariant (K). At each test of line L8 with the current value , where : for and for , and for .
- Initialization. Before the first test , no cell has been written, and the middle range is empty.
- Maintenance. Assume (K) at the test with value . Line L9 sets . Since lies in , (K) gives . So now , and the middle range grows to . The cells from to are unchanged. So (K) holds for .
- Termination. Each iteration increases by exactly 1, from , and the loop stops at the first test with . So it runs exactly times. At exit, (K) with gives for and for .
Line L10 sets the count to , so (I) holds, and the list is the goal. Line L11 returns .
SEARCH. Loop invariant: at the test of line S1 with value , no with equals . It holds for , and line S2 keeps it, because it returns when and otherwise . If line S2 returns , then and no earlier item equals , so is the first index of . If the loop ends, then and no item equals , so NIL is right. The loop runs at most times because increases by 1 each time. Nothing is written.