Proofs: Shift-Or algorithm
These are the human proofs of Shift-Or algorithm, at full length.
Shift-Or outputs exactly the valid shifts full
This proof shows: correctness, termination.
Claim. For a text and a pattern , SHIFT-OR stops after iterations and outputs exactly the shifts , , with , in increasing order.
For , line L2 outputs every shift, which is correct. Let , and write for digit of , .
Masks. After lines L3 and L4, digit of is 0 exactly when : line L3 sets all digits to 1, and line L4 clears digit of for each and nothing else.
Invariant. After line L7 in the iteration for :
(I) for every , exactly when and .
Set , the value of line L5; it satisfies (I) for , since no exists.
Step. Let be the value before line L7 in iteration , satisfying (I) for , and the value after it. For , digit of is ; digit 0 of is 0. The AND with only removes digits . So
For : exactly when , which is (I) for . For : exactly when and , that is, by (I) for , when , and , which is exactly and . So (I) holds for .
Output. By (I) with , line L8 outputs exactly when and , that is, exactly when is a valid shift with . Each valid shift is output in the iteration , and these iterations come in increasing order of . The loop has iterations, each of constant work on a fixed number of words, so the algorithm stops.