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
U. Windl
  • 3,480
  • 26
  • 54
Pipo
  • 4,653
  • 38
  • 47
  • Did you read something like https://www.eiffel.org/doc/eiffel/ET-_Instructions or https://en.wikipedia.org/wiki/Loop_variant? – U. Windl May 24 '21 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 May 26 '21 at 20:27
  • @Scratte I realized that `lang-eiffel` isn't supported yet after I used it. According to ("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 May 27 '21 at 09:25

1 Answers1

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
javierv
  • 159
  • 7
  • thx, could you elaborate a bit about the variant part en theory? – Pipo Oct 26 '20 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 Oct 26 '20 at 21:36
  • 2
    Note: the `variant` is a non negative integer expression and must decrease in each iteration. – javierv Oct 26 '20 at 22:00