0

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
3
  • Did you read something like eiffel.org/doc/eiffel/ET-_Instructions or en.wikipedia.org/wiki/Loop_variant?
    – U. Windl
    Commented May 24, 2021 at 16:10
  • @U.Windl. Your proposed edits gives this in the console: "WARN: Could not find the language 'eiffel', did you forget to load/include a language module?" .. "WARN: Falling back to no-highlight mode for this block. ..." (On a side note, I think you may have missed a capital U for the title :-)
    – Scratte
    Commented May 26, 2021 at 20:27
  • @Scratte I realized that lang-eiffel isn't supported yet after I used it. According to <meta.stackexchange.com/questions/184108/…> ("Any language identifiers used in a post that go unrecognized by highlight.js will functionally default to lang-default.") the algorithm will "Guess" the language then. I suspect it's Ada.
    – U. Windl
    Commented May 27, 2021 at 9:25

1 Answer 1

1

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
3
  • thx, could you elaborate a bit about the variant part en theory?
    – Pipo
    Commented Oct 26, 2020 at 21:19
  • The loop invariant is a property of your loop that you can assert will be true while the loop is executing. The loop variant is a property of your loop that varies as the loop is executed, and will ensure the loop will terminate.
    – javierv
    Commented Oct 26, 2020 at 21:36
  • 2
    Note: the variant is a non negative integer expression and must decrease in each iteration.
    – javierv
    Commented Oct 26, 2020 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.