8. State and search
Sequential accumulation walks a source with an int64 or pair state. An ordinary transition receives the current occurrence and old state, computes a replacement, and updates atomically only after success.
Complete program
The program accepts an integer sequence named reference.
The program accepts an integer named candidate.
The program computes the following result.
First compute the length of the reference. Then iterate from the pair consisting of the difference of one and one using checked subtraction and that computed value using lower-bound narrowing in the reference for the candidate. Before each update, continue only when the second component of the accumulated value is greater than the first component of the accumulated value. Return the first component of the accumulated value.The lower-bound program uses pair state [low, high). Before each update, its guard checks whether high > low. The source-defined narrowing transition calculates a checked midpoint, reads the sorted sequence, and returns a fresh pair. When the guard becomes false, the first component is the insertion position.
For reference [1,3,3,3,7,9] and candidate 10, the result is 6, one past the last element. Exactly one stopping policy is required for repetition: the supported pre-update guard or a supported post-update stop. The transition frame determines whether an external sequence and parameter are required.
This implementation is binary lower-bound, not a generic search protocol. Sorted input is an author responsibility.
Exercise
Predict results for candidates 0 and 3 against [1,3,3,3,7,9].
Verified solution
Candidate 0 returns 0; candidate 3 returns the first equal position, 1.