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

Sort and switch methods in Dafny (Invariants error)

I am new to Dafny and wrote two methods, switch and sort. In sort I get an error in line 10 and 12 and in switch in line 24 and 26. Both based on the forall invaraiants. I'm struggling and can't figure out why. The error messages I'm getting are…
0
votes
1 answer

What will be the decreases value for multiply two integer in Dafny

Basically, my target is to learn dafny basics. But I am confused about invariant and decreases. In this code, my first while loop decreases by one so I have set decreases b but in my inner loop it is divided by 10, so when I was trying to set up…
0
votes
1 answer

multiplication of two int value in Dafny

Can anyone help me with this program? I am new to Dafny. I just need to know what will be invariant and decreases values. Thanks in advance method MultiplyTwoNumber(N: int, M: nat) returns (Result: int) ensures Result == M*N; requires N>=0 &&…
0
votes
1 answer

Struggling to find loop invariant in power function

I am struggling to find a good loop invariant for the following function, which returns a^b where a is a real number and b is a natural number: power <- function(a, b){ c <- 1 while(b > 0){ if(b %% 2 == 1){ c <- c…
Jeremy
  • 11
  • 3
0
votes
0 answers

Do I have a misunderstanding of loop invarients?

So I have an upcoming test about proofs and I was looking through some practice questions. I came across a question which I don't understand the answer to, and I was hoping I could get some input. Consider the following algorithm: Factorial(n:…
0
votes
1 answer

Dafny - Fitting digits of a number into an array

I am trying to verify a code that uses simple modulo operation in a while loop to insert the digits of a number into an array. I recieve a number, an array with defined size, and start - an index in the array from which I fit the most significant…
nimrod891
  • 3
  • 2
0
votes
1 answer

Understanding final values of Java tracing table given informal contract

I am currently in an introductory programming class and was asked to interpret the final state of the given tracing table featuring a loop invariant. The blue numbers in the bottom box are the given answers. As you can see, low is updated but I…
itscomcast
  • 11
  • 3
0
votes
2 answers

Finding an invariant for a simple loop

I have never felt so woefully inadequate as I am when trying to prove to Dafny that my program is correct, so I need your help: The given program looks as follows: method doingMath(N: int, M: int) returns (s: int) requires N <= M //given…
nitowa
  • 1,079
  • 2
  • 9
  • 21
0
votes
1 answer

how does the dafny invariant cope with datatypes

The example code will seem artificial as it is the smallest that I can find to illustrates my problem. datatype Twee = Node(value : int, left : Twee, right : Twee) | Empty method containsI(t : Twee, s : int) returns (r : bool) { var working…
david streader
  • 589
  • 2
  • 7
0
votes
2 answers

Dafny loop invariant fails even though invariant assertions work. Is this a small bug?

Hi for teaching I am setting up a mass of simple dafny questions. Mostly going fine, but... Either I have missed some detail about loop invariants in Dafny or this is a weakness/bug? method whS(a:int) returns () { var i:int := 0; while…
david streader
  • 589
  • 2
  • 7
0
votes
0 answers

How do I come up with a loop invariant for a pseudocode that calculates an odd number?

I'm struggling to come up with a loop invariant for the following piece of code: OddNumber(n) a = 2 for i = 1 to n do a = a * i return a+1 Now, I want to find a loop invariant which is correct before entering the for-loop in l.2. My idea was…
FishyK
  • 121
  • 1
  • 7
0
votes
0 answers

what is the loop invariant of this piece of code?

int y = 0; while(y 0, otherwise it equals to 0. What's the loop invariant that allows me to prove that? For the case where x > 0, I came up with the invariant i <= x and y =…
JRR
  • 6,014
  • 6
  • 39
  • 59
0
votes
1 answer

Dafny: Fast Exponent Calculation (Loops)

I'm trying to implement and compile a Fast Exponential algorithm in Dafny but I'm running into a couple issues. Context: All code is available below; The Fast Exponential (FastExp) lemma itself is iterative; An exp function is used to make sure the…
Asfourhundred
  • 3,003
  • 2
  • 13
  • 18
0
votes
1 answer

Invariant for Hoare-Logic on RandomSearch

I'm trying to proof the following RandomSeach-Algorithm and to figure out the invariant for the loop. Since the function randomIndex(..) creates a random number I cannot use an invariant like ≥ 0 ∧ < − 1 ⇒ [] ≠ e . That means, all elements…
Schrello
  • 27
  • 5
0
votes
2 answers

How to find loop invariant of Sieve of Eratosthenes Algorithm?

Can anyone help me to make loop invarients of Eratosthenes Algorithm please? Here is the peace of code: algorithm Sieve of Eratosthenes is input: an integer n > 1. output: all prime numbers from 2 through n. let A be an array of Boolean values,…