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.

148 questions
2
votes
1 answer

(Dafny) Sorting an array - loop invariant

Here is a simple sort algorithm written in Dafny : predicate perm(a:array, b:array) requires a != null && b != null reads a,b { multiset(a[..]) == multiset(b[..]) } predicate sorted(a:array, min:int, max:int) requires…
Dory
  • 139
  • 1
  • 9
2
votes
1 answer

How do we define a loop invariant?

We know that a loop variant is defined as a statement that is true before and after every iteration of the loop. But is that definition too loose? Let us look at a concrete example: linear search. Input: A sequence of n numbers A = (a1, a2, a3, ...,…
W. Zhu
  • 755
  • 6
  • 16
2
votes
1 answer

Possible loop invariant

Consider the following loop: y=1; x=a; //with a>=0 , b>=0 while(x>0){ y=y*b; x=x-1; } I wanna conclude y = ba I been pondering for a while and cant seem to figure out a strong enough loop invariant that allows me to conclude that. Does…
2
votes
1 answer

Dafny insert method, a postcondition might not hold on this return path

I have an array "line" which has a string contained in it of length "l" and an array "nl" which has a string contained in it of length "p". Note: "l" and "p" don't necessarily have to be the length of each correspondent array.The parameter "at"…
pmpc
  • 315
  • 4
  • 19
2
votes
1 answer

Loop invariant and using to solve algorithm?

So if I have the following code: public int sumSquares(int n){ int sum = 0; for(int i = 1; i <=n; i++){ sum += i*i; } return sum; } I must now find a loop invariant. I was told that for a loop like this, an invariant of Y = i^2 is…
Tyler Dahle
  • 77
  • 1
  • 8
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,533
  • 3
  • 16
  • 38
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
  • 454
  • 4
  • 18
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
  • 748
  • 4
  • 8
  • 18
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,093
  • 3
  • 22
  • 48
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

Find loop invariant of an algorithm by induction

Given this algorithm in pseudocode that sorts any array, how can I formulate a loop invariant that proves its correctness using induction and how do I find its time complexity ? Input: array A[0 . . . n − 1] i ← 0 while i < n do if i = 0 or…
1
vote
1 answer

Difference in loop invariant in for loop compared to while loop

The invariant used for this example comes from https://www.win.tue.nl/~kbuchin/teaching/JBP030/notebooks/loop-invariants.html I'm confused though. The code in the example uses a for loop. I translated it to a while loop, because I understand this…
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
1
vote
1 answer

Verifying Vector Addition?

I'm trying to verify a vector add function in Whiley. This is what I had so far: function add(int[] v1, int[] v2) -> (int[] v3) requires |v1| == |v2| ensures |v3| == |v1|: // v3 = v1 // for i in 0..|v3|: v3[i] = v1[i] +…
JimW
  • 57
  • 4
1 2
3
9 10