CS 5/6110, Software Correctness Analysis, Spring 2023
Ganesh Gopalakrishnan
School of Computing
University of Utah
Salt Lake City, UT 84112
History, Motivations
Good overviews of Hoare Logic
Let’s analyze an XOR-based swap
Gordon’s example 20 (in Gordon’s book)
Here is an
“annotated
Program”
The question is
Does the
“ensures”
Clause hold?
Gordon’s example done by hand
Gordon’s g20 example and its “LI walk-back”
S = x*y at the output
In a Hoare-logic proving approach, one annotates the program
at the “loop head” with a LOOP INVARIANT
It must be true whenever the execution reaches that point
What is it?
In a Hoare-logic proving approach, one annotates the program
at the “loop head” with a LOOP INVARIANT
It must be true whenever the execution reaches that point
What is it?
The “remaining work” is X.
Thus, S + X*y = x*y
OR S = y * (x – X)
Then upon exit, S = y * x
With nested loops, we need more annotations
Here is an “optimized” version
of the same program
Method g20(x,y : nat) returns (S:nat)
Requires x > 0
Requires y > 0
Ensures S == x * y
{ var X,Y;
X := x; Y := y; S := 0;
while(X != 0)
{ while (X % 2 == 0)
{ Y := Y + Y ; X := X / 2; }
S := S+Y;
X := X-1;
}
}
With nested loops, we need more annotations
Here is an “optimized” version
of the same program
Method g20(x,y : nat) returns (S:nat)
Requires x > 0
Requires y > 0
Ensures S == x * y
{ var X,Y;
X := x; Y := y; S := 0;
while(X != 0) [ Loop Invariant Here ? ]
{ while (X % 2 == 0)
{ Y := Y + Y ; X := X / 2; }
S := S+Y;
X := X-1;
}
}
With nested loops, we need more annotations
Here is an “optimized” version
of the same program
Method g20(x,y : nat) returns (S:nat)
Requires x > 0
Requires y > 0
Ensures S == x * y
{ var X,Y;
X := x; Y := y; S := 0;
while(X != 0) [ Loop Invariant Here ? ]
[ Remaining “work” is still measured by X ]
[ If you pretend to do that much more, and add to S,
it should match our goal ]
{ while (X % 2 == 0)
{ Y := Y + Y ; X := X / 2; }
S := S+Y;
X := X-1;
}
}
With nested loops, we need more annotations
Here is an “optimized” version
of the same program
Method g20(x,y : nat) returns (S:nat)
Requires x > 0
Requires y > 0
Ensures S == x * y
{ var X,Y;
X := x; Y := y; S := 0;
while(X != 0) [ Loop Invariant Here ? ]
{ while (X % 2 == 0)
{ Y := Y + Y ; X := X / 2; }
S := S+Y;
X := X-1;
}
}
With nested loops, we need more annotations
Here is an “optimized” version
of the same program
The inner loop does “doubling”
of Y each time X is even
The well-founded relation
(to argue termination) is also
given
in one version of Dafny, it failed with 2Y…
have correspondence with author (Rustan)
Don't know if gone now
With nested loops, we need more annotations
Let’s at least get some experience
applying the WP rules
by hand
in one version of Dafny, it failed with 2Y…
have correspondence with author (Rustan)
Don't know if gone now
Gordon’s Prover with “proof generation” added by Mantha
Gordon’s Prover with “proof generation” added by Mantha
Gordon’s Prover with “proof generation” added by Mantha
Gordon’s Prover with “proof generation” added by Mantha
Gordon’s Prover with “proof generation” added by Mantha
Run using sbcl