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
1
vote
2 answers

How can the strongest loop invariant be found for this code?

I am trying to come up with a loop invariant for the following while-loop, but am having some trouble. After the loop invariant is decided on, I would like to put together a proof tableau and show all intermediate assertions ASSERT(k >= 0) {i = 1; …
1
vote
4 answers

Get each character from each string by column

I've been trying to get each character from every String by column but I only got the first characters of every string, I want to get every character by column from every string. For example: I have three strings from ArrayList of Strings:…
user9315910
1
vote
1 answer

How to construct and justify loop invariant, which allows to show partial correctness

I need to construct and justify a loop invariant with given specification: {n > 0} P {q = | {j: a[j]=x and 0 <= j < n} |} where |A| is a number of elements of set A. It means that q is equal to the number of elements from array a that are equal to…
whiskeyo
  • 873
  • 1
  • 9
  • 19
1
vote
1 answer

Finding a loop invariant - Hoare Triple

From the following code, I need to deduce/choose a loop invariant. (|true|) x = 0 ; s = 0 ; while ( x <= n ) { s = s + x ; x = x + 1 ; } (|s = n(n + 1)/2|) Solution given was s = (x-1)*x/2 ∧ (x ≤ n +1) I don't quite understand how it has…
user6797155
1
vote
1 answer

Is this loop invariant and its informal proof correct? (CLRS 3rd ed. exercise 2-1-3)

Given the following algorithm for linear-search (referring to index 1 as the index of the first element in an array of elements): found_idx = nil for i = 1 to A.length if A[i] == value found_idx = i return found_idx …
1
vote
0 answers

Elegant way to check loop invariant

I learned that the loop invariant should be true before the loop, at the start of the loop body, at the end of the loop body and after the loop. This 4 points can be reduced to 2 points if there are no side effects of the condition: Before the loop…
Claudio P
  • 2,133
  • 3
  • 25
  • 45
1
vote
1 answer

How to prove an iterative loop with computations in frama-c wp?

I have my test code (to study the WP loop invariants) which adds two long integers with each digit's representation in an array cell: int main(int argc, const char * argv[]) { char a[32], b[32];//size can be very big memset(a, 0,…
SeregASM
  • 75
  • 12
1
vote
1 answer

ValueError on tensorflow while_loop shape invariants

import tensorflow as tf cluster_size = tf.constant(6) # size of the cluster m = tf.constant(6) # number of contigs (column size) n = tf.constant(3) # number of points in a single contigs (column size) contigs_index = tf.reshape(tf.range(0, m, 1,…
drsbhattac
  • 109
  • 2
  • 7
1
vote
1 answer

Is this loop invariant correct?

The pseudocode for linear search loop: for j = 1 to A.length if(A[j] = v) return j; return NIL Loop invariant I've written: At the start of each iteration of the for loop, j is the next index after where A[j-1] doesn't equal v.…
Zolbayar
  • 896
  • 12
  • 29
1
vote
1 answer

Loop invariant not strong enough when manipulating (array) fields of this

UPDATED Problems on solving some dafny problems, described below with the given class and respective methods. If you need something else please tell me, thank you in advance. Also the link is updated with all this code in rise4fun. class TextEdit…
pmpc
  • 315
  • 4
  • 19
1
vote
0 answers

Write a loop invariant for partial correctness of Hoare Triple

I am new to the world of logic. I am learning Hoare Logic and Partial & Total correctness of programs. I tried alot to solve the below question but failed. Write a loop invariant P to show partial correctness for the Hoare triple {x = ¬x ∧ y = ¬y…
Ajmal Razeel
  • 1,663
  • 7
  • 27
  • 51
1
vote
1 answer

Loop invariant of simple while loop

I saw an example program which sets every value in an array to 0: int a[n]; int i = 0; while(i < n) { a[i] = 0; i++; } It said that part of the loop invariant was 0<=i
rohaldb
  • 589
  • 7
  • 24
1
vote
1 answer

Termination function definition (algorithms)

A question about the definition of termination functions. We have a relatively simple function for calculating ⌊log₂ n⌋ of an input. LOG2 Configuration: {[r, n] | Integers r ≥ 0 and n ≥ 1} [r, n] -> [r + 1, n/2] if n > 1 ∧ n even [r, n] -> [r, n −…
Thomas B
  • 61
  • 3
1
vote
0 answers

Prove the validity of a triple

To prove the validity of this valid triple: {X==U, Y==0, U > 0, N > 0} while (X > 0) { X = X - N; Y = Y + 1;}; {Y == ⌈U/N⌉} What is the loop invariant?
1
vote
1 answer

Dafny: rotated region of an array method verification

this proof gives an infinite loop in Dafnys' verifier: // Status: verifier infinite loop // rotates a region of the array by one place forward method displace(arr: array, start: nat, len: nat) returns (r: array) requires arr != null …