loopseiffeldesign-by-contract

Understanding Eiffel loop variant/invariant


I was trying to have a structure which talks himself about variants and invariants into Eiffel loops, but don't understand the variant part!

from
    l_array := <<1,2,30,60>>
    l_index := l_array.lower
invariant
    valid_local_index: l_array.valid_index (l_index) or l_index = l_array.upper + 1
until
    l_index > l_array.upper
loop
    l_item := l_array.item (l_index)
    l_index := l_index + 1
variant
    --l_index <= l_array.upper -- will never be false
    --l_index -- doesnt work
end

Solution

  • I think in your case what you want to express as part of the variant is something like this

    l_array.upper - l_index + 1