An Invariant Under a Transformation
An invariant is a property preserved when a specified transformation is applied. It can summarize what many distinct states have in common.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Check a swap
Swapping two positions in a list preserves the sum and the multiset of values, but can change the order. A sort postcondition needs multiset equality precisely because many swaps may be made.
Use an invariant to reject a goal
For operations that only swap positions, a target list with different frequencies cannot be reached. The frequency map supplies a direct certificate of that difference.[1]
Verify the same observable before and after each move
Swapping two positions in a sequence preserves the value multiset. For [4,1,4], swapping the first two positions gives [1,4,4], with two fours and one one. Ordered equality generally changes. See ordered and unordered equality.
Before: [4,1,4], counts {4:2, 1:1}
After: [1,4,4], counts {4:2, 1:1}
A second swap also preserves counts
Extend the argument to a sequence of moves
If every permitted move preserves the multiset, induction gives preservation for any finite sequence of them. A single aggregate such as sum cannot detect every violation of multiplicities. Check full counts when that is the claimed invariant.