Lecture 1 Notes Contracts: 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning
Lecture 1 Notes Contracts: 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning
Contracts
15-122: Principles of Imperative Computation (Summer 1 2015)
Frank Pfenning
Introduction
L ECTURE N OTES
Contracts
L1.2
A Mysterious Program
You are a new employee in a company, and a colleague comes to you with
the following program, written by your predecessor who was summarily
fired for being a poor programmer. Your colleague claims he has tracked a
bug in a larger project to this function. It is your job to find and correct this
bug.
int f (int x, int y) {
int r = 1;
while (y > 1) {
if (y % 2 == 1) {
r = x * r;
}
x = x * x;
y = y / 2;
}
return r * x;
}
Before you read on, you might examine this program for a while to try
to determine what it does, or is supposed to do, and see if you can spot the
problem.
L ECTURE N OTES
Contracts
L1.3
Forming a Conjecture
The first step it to execute the program on some input values to see its
results. The code is in a file called mystery.c0 so we invoke the coin interpreter to let us experiment with code.
% coin mystery.c0
C0 interpreter (coin)
Type #help for help or #quit to exit.
-->
At this point we can type in statements and they will be executed. One
form of statement is an expression, in which case coin will show its value.
For example:
--> 3+8;
11 (int)
-->
We can also use the functions in the files that we loaded when we
started coin. In this case, the mystery function is called f, so we can evaluate it on some arguments.
--> f(2,3);
8 (int)
--> f(2,4);
16 (int)
--> f(1,7);
1 (int)
--> f(3,2);
9 (int)
-->
Can you form a conjecture from these values?
L ECTURE N OTES
Contracts
L1.4
From these and similar examples, you might form the conjecture that
f (x, y) = xy , that is, x to the power y. One can confirm that with a few
more values, such as
--> f(-2,3);
-8 (int)
--> f(2,8);
256 (int)
--> f(2,10);
1024 (int)
-->
It seems to work out! Our next task is to see why this function actually
computes the power function. Understanding this is necessary so we can
try to find the error and correct it.
L ECTURE N OTES
Contracts
L1.5
Now we start to look inside the function and see how it computes.
int f (int x, int y) {
int r = 1;
while (y > 1) {
if (y % 2 == 1) {
r = x * r;
}
x = x * x;
y = y / 2;
}
return r * x;
}
We notice the conditional
if (y % 2 == 1) {
r = x * r;
}
The condition tests if y modulo 2 is 1. For positive y, this is true if y is odd.
We also observe that in the loop body, y must indeed be positive so this is
a correct test for whether y is odd.
Each time around the loop we divide y by 2, using integer division
(which rounds towards 0). It is exact division if y is even. If y starts as
a power of 2, it will remain even throughout the iteration. In this case r
will remain 1 throughout the execution of the function. Lets tabulate how
the loop works for x = 2 and y = 8. But at which point in the program do
we tabulate the values? It turns out generally the best place for a loop is just
before the exit condition is tested. By iteration 0 we mean when we enter the
loop the first time and test the condition; iteration 1 is after the loop body
has been traversed once and we are looking again at the exit condition, etc.
iteration x
0
2
1
4
2
16
3
256
y
8
4
2
1
r
1
1
1
1
Contracts
L1.6
To understand why this loop works we need to find a so-called loop invariant: a quantity that does not change throughout the loop. In this example, when y is a power of 2 then r is a loop invariant. Can you see a loop
invariant involving just x and y?
L ECTURE N OTES
Contracts
L1.7
Going back to our earlier conjecture, we are trying to show that this
function computes xy . Interestingly, after every iteration of the loop, this
quantity is exactly the same! Before the first iteration it is 28 = 256. After
the first iteration it is 44 = 256. After the second iteration it is 162 = 256.
After the third iteration is it is 2561 = 256. Lets note it down in the table.
iteration x
0
2
1
4
2
16
3
256
y
8
4
2
1
r
1
1
1
1
xy
256
256
256
256
L ECTURE N OTES
Contracts
L1.8
=
=
=
=
(x x)y/2
(x2 )y/2
x2(y/2)
xy
By definition of x0 and y 0
Since a a = a2
Since (ab )c = abc
Since 2 (a/2) = a when a is even
L ECTURE N OTES
Contracts
L1.9
The postcondition of a function is usually a statement about the result it returns. Here, the postcondition is that f (x, y) = xy . Lets recall the function:
int f (int x, int y) {
int r = 1;
while (y > 1) {
if (y % 2 == 1) {
r = x * r;
}
x = x * x;
y = y / 2;
}
return r * x;
}
If y is a power of 2, then the quantity xy never changes in the loop (as we
have just shown). Also, in that case r never changes, remaining equal to 1.
When we exit the loop, y = 1 because y starts out as some (positive) power
of 2 and is divided by 2 every time around loop. So then
r x = 1 x = x = x1 = xy
so we return the correct result, xy !
By using two loop invariant expressions (r and xy ) we were able to
show that the function returns the correct answer if it does return an answer. Does the loop always terminate?
L ECTURE N OTES
Contracts
L1.10
Termination
In this case it is easy to see that the loop always terminates. To show that
a loop always terminates, we need to define some quantity that always gets
strictly smaller during any arbitrary iteration of the loop, and that can never
become negative. This means that the loop can only run a finite number of
times.
The quantity y/2 is always less than y when y > 0, so on any arbitrary
iteration of the loop, y gets strictly smaller and it can never become negative. Therefore, we know the loop has to terminate.
(By the same token, we could identify any lower bound, not just zero,
and a quantity that strictly decreased and never passed that lower bound.
or we could identify an upper bound and a quantity that strictly increased
but never passed that upper bound!)
L ECTURE N OTES
Contracts
L1.11
A Counterexample
L ECTURE N OTES
Contracts
L1.12
Imposing a Precondition
Lets go back to a mathematical definition of the power function xy on integers x and y. We define:
x0
= 1
xy+1 = x xy for y 0
In this form it remains undefined for negative exponents. In programming,
this is captured as a precondition: we require that the second argument to f
not be negative. Preconditions are written as //@requires and come before
the body of the function.
int f (int x, int y)
//@requires y >= 0;
{
int r = 1;
while (y > 1) {
if (y % 2 == 1) {
r = x * r;
}
x = x * x;
y = y / 2;
}
return r * x;
}
This is the first part of what we call the function contract. It expresses what
the function requires of any client that calls it, namely that the second argument is positive. It is an error to call it with a negative argument; no
promises are made about what the function might return otherwise. It
might even abort the computation due to a contract violation.
But a contract usually has two sides. What does f promise? We know it
promises to compute the exponential function, so this should be formally
expressed.
L ECTURE N OTES
Contracts
10
L1.13
Promising a Postcondition
Contracts
L1.14
and post-conditions (the @requires and @ensures clauses), tells us everything we need to know. As long as we adhere to our contract and pass f a
nonnegative y, then f will adhere to its contract and return xy .
L ECTURE N OTES
Contracts
11
L1.15
Contracts
L1.16
L ECTURE N OTES
Contracts
12
L1.17
Before fixing the bug with an exponent of 0, lets figure out why the function apparently works when the exponent is odd. Our loop invariant so far
only works when y is a power of 2. It uses the basic law that b2c = (b2 )c =
(b b)c in the case where the exponent e = 2 c is even.
What about the case where the exponent is odd? Then we are trying
to compute b2c+1 . With analogous reasoning to above we obtain b2c+1 =
b b2c = b (b b)c . This means there is an additional factor of b in the
answer. We see that we exactly multiply r by b in the case that e is odd!
int f (int x, int y)
//@requires y >= 0;
//@ensures \result == POW(x,y);
{
int r = 1;
int b = x; /* base */
int e = y; /* exponent */
while (e > 1) {
if (e % 2 == 1) {
r = b * r;
}
b = b * b;
e = e / 2;
}
return r * b;
}
What quantity remains invariant now, throughout the loop? Try to form a
conjecture for a more general loop invariant before reading on.
L ECTURE N OTES
Contracts
L1.18
Lets make a table again, this time to trace a call when the exponent is
not a power of two, say, while computing 27 by calling f (2, 7).
iteration b
0
2
1
4
2
16
e
7
3
1
r be
1 128
2 64
8 16
As we can see, be is not invariant, but r be = 128 is! The extra factor from
the equation on the previous page is absorbed into r.
We now express this proposed invariant formally in C0. This requires
the @loop_invariant annotation. It must come immediately before the
loop body, but it is checked just before the loop exit condition. We would
like to say that the expression r * POW(b,e) is invariant, but this is not
possible directly.
Loop invariants in C0 are boolean expressions which must be either true
or false. We can achieve this by stating that r * POW(b,e) == POW(x,y).
Observe that x and y do not change in the loop, so this guarantees that
r * POW(b,e) never changes either. But it says a little more, stating what
the invariant quantity is in term of the original function parameters.
int f (int x, int y)
//@requires y >= 0;
//@ensures \result == POW(x,y);
{
int r = 1;
int b = x; /* base */
int e = y; /* exponent */
while (e > 1)
//@loop_invariant r * POW(b,e) == POW(x,y);
{
if (e % 2 == 1) {
r = b * r;
}
b = b * b;
e = e / 2;
}
return r * b;
}
L ECTURE N OTES
Contracts
13
L1.19
L ECTURE N OTES
Contracts
L1.20
L ECTURE N OTES
Contracts
14
L1.21
We would now like to show that the improved function is correct. That
requires two steps: one is that the loop invariant implies the postcondition;
another is that the proposed loop invariant is indeed a loop invariant. The
loop invariant, r be = xy implies that the result r = xy if we know that
e = 0 (since b0 = 1).
But how do we know that e = 0 when we exit the loop? Actually,
we dont: the loop invariant is too weak to prove that. The negation of
the exit condition only tells us that e 0. However, if we add another
loop invariant, namely that e 0, then we know e = 0 when the loop is
exited and the postcondition follows. For clarity, we also add a (redundant)
assertion to this effect after the loop and before the return statement.
int f (int x, int y)
//@requires y >= 0;
//@ensures \result == POW(x,y);
{
int r = 1;
int b = x; /* base */
int e = y; /* exponent */
while (e > 0)
//@loop_invariant e >= 0;
//@loop_invariant r * POW(b,e) == POW(x,y);
{
if (e % 2 == 1) {
r = b * r;
}
b = b * b;
e = e / 2;
}
//@assert e == 0;
return r;
}
The @assert annotation can be used to verify an expression that should
be true. If it is not, our reasoning must have been faulty somewhere else.
@assert is a useful debugging tool and sometimes helps the reader understand better what the code author intended.
L ECTURE N OTES
Contracts
15
L1.22
It seems like we have beaten this example to death: we have added pre- and
post-conditions, stated loop invariants, fixed the original bug and shown
that the loop invariants imply the postcondition. But we have not yet verified that the loop invariant actually holds! Ouch! Lets do it.
We begin with the invariant e 0. We have to demonstrate two properties.
Init: The invariant holds initially. When we enter the loop, e = y and y 0
by the precondition of the function. Done.
Preservation: Assume the invariant holds just before the exit condition is
checked. We have to show that it is true again when we reach the exit
condition after one iteration of the loop
Assumption: e 0.
To show: e0 0 where e0 = e/2, with integer division. This clearly
holds.
Next, we look at the invariant r P OW (b, e) = P OW (x, y).
Init: The invariant holds initially, because when entering the loop we have
r = 1, b = x and e = y.
Preservation: We show that the invariant is perserved on every iteration.
For this, we distinguish two cases: e is even and e is odd.
Assumption: r P OW (b, e) = P OW (x, y).
To show: r0 P OW (b0 , e0 ) = P OW (x, y), where r0 , b0 , and e0 are the
values of r, b, and e after one iteration.
Case: e is even. Then r0 = r, b0 = b b and e0 = e/2 and we reason:
r0 P OW (b0 , e0 ) = r P OW (b b, e/2)
= r P OW (b, 2 (e/2)) Since (a2 )c = a2c
= r P OW (b, e)
Since e is even
= P OW (x, y)
By assumption
Case: e is odd. Then r0 = b r, b0 = b b and e0 = (e 1)/2 (because
e is odd, integer division rounds towards 0, and e 0) and we
L ECTURE N OTES
Contracts
L1.23
reason:
r0 P OW (b0 , e0 ) =
=
=
=
=
(b r) P OW (b b, (e 1)/2)
(b r) P OW (b, 2 (e 1)/2)
(b r) P OW (b, e 1)
r P OW (b, e)
P OW (x, y)
L ECTURE N OTES
Contracts
16
L1.24
Termination
L ECTURE N OTES
Contracts
17
L1.25
A Surprise
Now, lets try our function on some larger numbers, computing some powers of 2.
% coin -d solution2e.c0
Coin 0.2.3 "Penny" (r1478, Thu Jan 20 16:14:15 EST 2011)
Type #help for help or #quit to exit.
--> f(2,30);
1073741824 (int)
--> f(2,31);
-2147483648 (int)
--> f(2,32);
0 (int)
-->
230 looks plausible, but how could 231 be negative or 232 be zero? We
claimed we just proved it correct!
The reason is that the values of type int in C0 or C and many other
languages actually do not represent arbitrarily large integers, but have a
fixed-size representation. In mathematical terms, this means we that we are
dealing with modular arithmetic. The fact that 232 = 0 provides a clue that
integers in C0 have 32 bits, and arithmetic operations implement arithmetic
modulo 232 .
In this light, the results above are actually correct. We examine modular
arithmetic in detail in the next lecture.
L ECTURE N OTES
Contracts
18
L1.26
L ECTURE N OTES
Contracts
L1.27
@ensures: At the return sites inside a function we have to prove that the
postcondition is satisfied for the given return value. We can then assume it at the call site.
@loop invariant: We have to show:
Init: The loop invariant is satisfied initially, when the loop is first
encountered.
Preservation: Assuming the loop invariant is satisfied at the beginning of the loop (just before the exit test), we have to show it still
holds when the beginning of the loop is reached again, after one
iteration of the loop.
We are then allowed to assume that the loop invariant holds after the
loop exits, together with the exit condition.
@assert: We have to show that an assertion is satisfied when it is reached
during program execution. We can then assume it for subsequent
statements.
Contracts are crucial for reasoning since (a) they express what needs to
be proved in the first place (give the programs specification), and (b) they
localize reasoning: from a big program to the conditions on the individual
functions, from the inside of a big function to each loop invariant or assertion.
L ECTURE N OTES
Contracts
L1.28
Exercises
Exercise 1 Rewrite first POW and then f so that it signals an error in case of an
overflow rather than silently working in modular arithmetic. You can use the
statement error("Overflow"); to signal an overflow.
Exercise 2 Find an input for f that fails the first guess of a loop invariant:
//@loop_invariant r * POW(b,e) == POW(x,y);
L ECTURE N OTES