Proofs: Metaphone
These are the human proofs of Metaphone, at full length.
METAPHONE computes the code that the rule table defines full
This proof shows: correctness.
The specification is the rule table of the page. Let be the letters of the input in upper case (line L1). Indices are 0-based. The prepared word comes from in two steps: (P2) remove each letter that is equal to the letter just before it in , unless the letter is C; (P3) if the result starts with KN, GN, PN, AE or WR, remove its first letter. The code of the word is the concatenation, for , of the output of the first rule in the table for the letter whose context condition holds at position of . Each context condition reads only , never the output.
Lemma 1. After line L3, the program holds the prepared word .
Proof. Line L2 scans from left to right and appends unless the list is non-empty, its last element equals , and . We show by induction on that, after the step for , the list is the result of P2 on , and its last element equals . For , the list is . For : if and , P2 removes , and the last element of the list is by the induction hypothesis, so line L2 does not append it. Otherwise P2 keeps . If , then the last element differs from and line L2 appends . If , line L2 appends it in any case. In both cases the last element is now . Line L3 is step P3 word for word.
Lemma 2. For each , the step of line L4 for position appends the output of the first rule of the table for whose condition holds at .
Proof. The table has a group of rules for each letter, and the rules of one letter are disjoint from those of another letter, so only the group of matters. Line L5 handles the vowels: the rule "first letter" outputs the vowel at , and the next rule outputs nothing. For a consonant, the branch of the program for tests the conditions of its group in the order of the table, and it takes the first test that holds. We check each group where a condition is not a direct transcription. For G, the program tests "H follows, and H is neither the last letter nor followed by a vowel", which is the first condition, then "N is the last letter after G, or the rest of the word is NED", then "D before and E, I or Y after", then "E, I or Y after", then the default K. For H, the program tests "C, G, P, S or T before, or W before at position 1", then "a vowel before and no vowel after", then the default H. For W, it tests "position 0 and H after", then "a vowel after", then the default, which outputs nothing. For C, the case "H after" outputs K when S is before and X otherwise, which is the order "S before and H after" then "H after" of the table. The other groups have one or two conditions, and the program tests them in the order of the table. The helper at returns the empty string outside , so a test on a letter before position 0 or after position is false, as the table means.
Theorem. METAPHONE returns the code of its input.
Proof. By Lemma 1, the loop of line L4 runs over the prepared word . By Lemma 2, each step appends the output of the rule of the table for that position. The steps run for in order, and no step changes , so line L8 returns the concatenation that defines the code.
Each step reads at most three letters after and one before it, so the time is linear in the length of the input.