Proofs: Soundex
These are the human proofs of Soundex, at full length.
SOUNDEX computes the code that the four Soundex rules define full
This proof shows: correctness.
Let be the name after line L1: upper-case ASCII letters only, with . Indices are 0-based. Let be the digit map of the page, with for A, E, I, O, U, Y, H and W.
The rules. For , let be the largest index with , or if there is no such . Position is kept when and . The digit string is the sequence of over the kept positions , in increasing order of . The Soundex code is followed by the first three digits of , with zeros added at the end when has fewer than three digits. This is the definition of the page: the first letter stays (rule 1), letters get digits (rule 2), a letter with the same digit as the last letter before it, with only H and W between them, is dropped (rule 3), and vowels, H and W after the first letter are dropped and the result has length 4 (rule 4).
Invariant. At the test of line L3 for the letter , with ( means the loop ends):
- , where is the largest index with or ;
- is followed by the codes of the kept positions in , and it has at most 3 digits.
Initialization. Line L2 sets and . For , , and no position in exists. Both parts hold.
Maintenance. Take the step for , with . For , the index of part 1 equals by the two definitions. If is H or W, line L5 skips the letter. H and W have code 0, so position is not kept, and for is the same as for , because is H or W. Both parts hold. Otherwise line L4 sets , and by part 1, . So the test of line L6 is true exactly when position is kept, and then line L6 appends its digit. Line L7 sets , and for is , because is not H or W. Both parts hold.
Early stop. Line L8 stops the loop when has 3 digits. These are the first three digits of by part 2, and the code uses no later digit, so the rest of the scan does not change the result.
End. If the loop ends without line L8, part 2 for says that is followed by all of , with at most 3 digits. Line L9 adds zeros to length 4. In both cases the result is followed by the first three digits of , padded with zeros. This is the Soundex code.
The loop runs at most times with work each, so the time is , and line L1 takes time linear in the length of the input.