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
1 answer

How do I describe the invariants for this simple algorithm in Dafny?

Attempting to verify formally the following problem. https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/ Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j <…
Hath995
  • 821
  • 6
  • 12
1
vote
1 answer

how to fix hamming weight invariants

I am learning Dafny, attempting to write a specification for the hamming weight problem, aka the number of 1 bits in a number. I believe I have gotten the specification correct, but it still doesn't verify. For speed of verification I limited it to…
Hath995
  • 821
  • 6
  • 12
1
vote
1 answer

Using loop invariants to prove correctness of a simple algorithm

I'm attempting to learn the loop invariant technique to produce more efficient algorithms. My understanding of proving correctness through evaluating loop invariants is that the loop invariants must be true before iteration, and after iteration, to…
TheBMAN
  • 69
  • 5
1
vote
1 answer

Dafny: is recursive function enough to verify iterative find-and-replace?

In Dafny, I'm working with an iterative approach to find all instances of a given substring and replace them with another given substring. method findAndReplace(str:string, toFind:string, toReplace:string) returns (newString:string) requires…
twhatm9
  • 27
  • 4
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
  • 67
  • 7
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
  • 589
  • 2
  • 7
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
  • 353
  • 3
  • 9
  • 14
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
  • 496
  • 2
  • 18
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
  • 97
  • 9
1
vote
1 answer

How to find a loop-invariant of this program?

Probably it is a very simple solution and I am just dumb, but I cant find the invariant for this while loop. For proving (a+b) <= 2x you can take (x+y>a+b), so probably this is the first part of the invariant, but for the second part, so to prove…
1
vote
1 answer

dafny assertion fails is hard to explain

I'm trying to prove the correctness of a method that determines whether a sequence of size n is a permutation of 0,1,...,n-1. I managed to prove that whenever the method returns true then the sequence is indeed a valid permutation. Still, proving…
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
1
vote
4 answers

Why does optimized prime-factor counting algorithm run slower

HiI saw an online answer for counting the distinct prime-factors of a number, and it looked non-optimal. So I tried to improve it, but in a simple benchmark, my variant is much slower than the original. The algorithm counts the distinct prime…
tkruse
  • 10,222
  • 7
  • 53
  • 80