Program Construction: Roland Backhouse January 2001
Program Construction: Roland Backhouse January 2001
Program Construction: Roland Backhouse January 2001
Program Construction
Roland Backhouse
January 2001
2
Outline
• Program Specification
• Assignments
• Conditional Statements
• Sequential Composition
• Loops
3
Program Specification
Comments
When writing computer programs it is a very good idea to comment
them thoroughly in order to explain what is going on.
Comments can also be almost useless. The comment
increment i by 1
immediately preceding the C/Java statement
i++
is completely useless to the experienced programmer who can be
expected to know that “i++” means “increment i by one” in C/Java
idiom.
Useless comments simply repeat in natural language what is stated
simply and precisely in the program statements. They are
operational.
Good comments, on the other hand, should have added value. They
should supplement the program text with explanations of the
program’s function and why the code that is used achieves that
function.
4
Assertions
... {i = 0} ...
{ P} S{ Q} ,
{ i = 0} i := i+1{ i = 1} ,
{ true} i := 1{ i = 1} .
Invalid Triples
{ i = 1} i := i+1{ i = 0} ,
{ true} i := 1{ i = 0} .
8
Exercise 1 Using your current knowledge say which of the
following is a valid Hoare triple. (Shortly we show how to validate
Hoare triples formally.)
(a) { i = 1 } j := i { i = j = 1 }
(b) { i = 1 } i := j { i = j = 1 }
(c) { 0 ≤ i < N } i := i+1 { 0 < i ≤ N }
(d) { true } i := j+1 { i < j }
(e) { i = 1 } i := 0 { true }
(f ) { i = 0 } i := 1 { false }
(g) { false } i := 1 { i = 0 }
2
9
Pre and Post Conditions
{ P} S{ Q} .
{ true} S{ i = j} .
i := j
and
j := i ,
there being no way to distinguish between the two variables.
In reality one of i and j would be the input value and the other the
output value, and the requirement would be to assign a value to the
output variable so as to meet the specification leaving the value of
the input variable unchanged.
The problem is resolved informally — we state which are the input
and which are the ouput variables in the text accompanying the
formal specification.
12
Ghost Variables
{ i+j = C} S{ i+j = C} .
This says that if the sum of i and j has the value C before execution
of statement S then execution of statement S is guaranteed to
terminate in a state in which the sum of i and j still has the value C.
Ghost variables are treated just like ordinary program variables but
the program code may not refer to them in any way.
13
Assignment
It is convenient to allow simultaneous assignments. In a simultaneous
assignment, the left side is a list of variables and the right side is a
list of expressions of the same length as the list of variables.
A simultaneous assignment to three variables x, y, and z is, for
example,
x,y,z := 2×y , x+y , 3×z .
A simultaneous assignment
x0 , x1 , . . . , xn := e0 , e1 , . . . , en
x,x := 0,1
{ Q[x := e]} x := e{ Q}
Example
{ 0 = 0} i := 0{ i = 0} .
{ true} i := 0{ i = 0} .
16
Example
j+k = C ⇒ X+k+1 = C .
21
Now,
j+k
= { arithmetic — introducing “k+1” }
j+k+1−1
= { rearranging }
(j−1)+k+1 .
It thus follows that a suitable value of X is j−1. That is,
s = n2
s = n2 ⇒ s+X = (n+1)2 .
23
Now,
(n+1)2
= { arithmetic — introducing “n2 ” }
n2 + 2n + 1 .
That is,
s = n2 ⇒ s + 2n + 1 = (n+1)2 .
In this way we have calculated the required assignment statement:
{ s = n2 } s,n := s + 2n + 1 , n+1{ s = n2 } .
24
Exercise 4 Suppose there are three program variables n, s and t.
Calculate assignments to s and t that maintain invariant the
relationship
s = n2 ∧ t = n3 .
In other words, calculate X and Y such that