Questions tagged [loop-invariant]

In formal program verification, loop invariants are expressed in formal predicate logic and used to prove properties of loops and, by extension, algorithms employing loops (usually correctness properties). A loop invariant should be true on entry into a loop and is guaranteed to remain true after every iteration of the loop.

In formal program verification, in particular in the Floyd-Hoare approach, loop invariants are expressed in formal predicate logic and used to prove properties of loops and, by extension, algorithms employing loops (usually correctness properties). A loop invariant should be true on entry into a loop and is guaranteed to remain true after every iteration of the loop. This means that on exit from the loop both the loop invariant and the loop termination condition can be guaranteed.

Because of the fundamental similarity of loops and recursive programs, proving partial correctness of loops with invariants is very similar to proving correctness of recursive programs via induction. In fact, the loop invariant is often the inductive property- the induction hypothesis- one has to prove of a recursive program that is equivalent to a given loop.

121 questions
2
votes
1 answer

Loop invariant for repeated calls to readLine()

I have a while loop (shown below) that continually reads from a file until EOF is reached. I am supposed to write a loop invariant for any non-trivial loop. Is this a trivial loop? If not, what would be a loop invariant for this while loop? I have…
Alex Parker
  • 1,323
  • 1
  • 15
  • 30
2
votes
1 answer

Loop Invariant for linear array search

int i = 0 boolean answer = false while (i < a.length) { if a[i] == 0 answer = true i = i + 1 where 'a' is an array of integers. I'm doing a question paper where it has asked me what the loop invariant of this is, I have already…
Sam
  • 404
  • 3
  • 16
2
votes
0 answers

Different ways of finding loop invariant

I am trying to find the loop invariant of this code. Usually I would actually go through the code with an input and try to figure it out. But this approach doesn't always work. Just wondering is there a better way to find the loop invariant? Any…
Wobblester
  • 710
  • 4
  • 8
  • 17
2
votes
3 answers

Loop invariant proof understanding

I am trying to learn about loop invariants in C. I have a code and I have the loop invariant but I do not fully understand why. Here is the code: /* 0 ≤ m < n < ASIZE AND A[m] ≥ A[m+1] ≥ ... ≥ A[n] */ void ReverseArray(int A[], int m, int n) { int…
user081608
  • 1,019
  • 3
  • 18
  • 40
2
votes
1 answer

Loop Invariant for Proving Partial Correctness

I'm trying to find a loop invariant so that we can prove this program partially-correct: { n >= 1 } pre-condition i = 1; z = 1; while (i != n) { i = i + 1; z = z + i*i; } { z = n*(n+1)*(2*n + 1)/6 } post-condition I am really stuck. Some of…
2
votes
1 answer

Frama-C/WP not able to prove loop invariant with \at

I'm having trouble proving 2 loop invariants: loop invariant \forall integer i; 0 <= i < (\at(n, Pre) - n) ==> ((char*)m2)[i] == \at(((char*)m1)[i], Pre); loop invariant \forall integer i; 0 <= i < (\at(n, Pre) - n) ==> ((char*)m1)[i] ==…
1
vote
1 answer

proof of correctness by loop invariant (induction)

I wrote my own trivial little function (php for convenience) and was hoping someone could help structure a proof by induction for it, just so I can get a very basic hang of it. function add_numbers($max) { //assume max >= 2 $index=1; …
1
vote
1 answer

What is the loop invariance/invariant that can describe this code's functionality?

def func(no_1,no_2): a=no_1 b=no_2 while(b): a,b=b,a%b return a where no_1 and no_2 are positive real numbers I've figured out that this function returns no_1 and no_2's greates common factor. First, I tried the loop…
1
vote
2 answers

finding the loop invariant for an algorithm

I am having some issues finding the invariant for the algorithm below.Also,I have to follow all the steps to proof how i find the specific invariant and I don't know how I can demonstrate that. I saw that this algorithm is a multiplication by…
1
vote
1 answer

any way to specify preconditions inside loop in frama C?

I am trying to prove the below function, where the elements of the array are added by an integer value c. it's in frama c ,but i am not able to prove some parts. Can someone help with this? #include /*@requires n>0; requires \forall…
Niresh
  • 23
  • 4
1
vote
1 answer

Dafny if an assertion is true on entry and exit of a loop is it an invariant

In Dafny I am experimenting writing iterative implementations to recursive specifications. More specifically a predicate containsMe(l:seq, me:T) that returns true when the me is an element of the sequence l. Having recently been…
david streader
  • 201
  • 1
  • 6
1
vote
0 answers

Finding out the correctness of a "while-loop" using hoare-logic

I currently am struggling to figure out, how to show that a program, which includes a loop, is correct. I am working on the basis of wp, vc and pc. The loops in question are: wp(while(i= n) wp(while(true) x=4; |x=4) where…
1
vote
4 answers

Java loop invariant

int logarithmCeiling(int x) { int power = 1; int count = 0; while (power < x) { power = 2 *power; count = count +1; } return count; } The code above is meant to be a method in Java for computing and returning…
Paradox
  • 323
  • 3
  • 9
  • 13
1
vote
1 answer

Analysis of algorithm removing marbles from jar

The following is an exercise from my algorithms and datastructures course that aims to teach something about analysis of algorithms using loop-invariants. The setup is the following; We have a jar with n marbles. A marble is either RED or BLUE. We…
sn3jd3r
  • 464
  • 2
  • 14
1
vote
0 answers

How to find a loop invariant of the spiral traversal of the square matrix beginning from center

We know that invariant is expression, that true before, in each iteration, and after the loop. So we should find out all invariants of the following code of spiral traversal of the square matrix, beginning from its center. Loop to explore: int iInd…
D7ILeucoH
  • 105
  • 7
1 2
3
8 9