Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Guarded commands. non-determinacy and a calculus for the derivation of programs. by Edsger W.Dijkstra *)
*
) Author's address:
alternative and
program components for which at least the activity evoked, but possibly even the final state, is not necessarily uniquely determined constructs, a calculus will be shown. Keywords. programming languages, sequencing primitives, program semantics, programming language semantics, non-determinacy, case-construction, repe tition, termination, correctness proof, derivation of programs, programming methodology. CR-category: 4.20, 4.22. the initial
of
these
1.
Introduction.
In section 2, two statements. an alternative construct and itive construct will definition
of be
repet
their
"
semantics.
r
both of them
by
is the sb-called
gua d ed command", a
l it
pr
e fixed
a boolean
expression: only when this boolean expression is initially true, is the statement list eligible for execution. The potential non-determinacy allows us to map otherwise (trivially) different programs on the same program text, a circumstance that seems largely responsible for the fact that
EWD418
now
programs
can
In section
nition
two theorems for each of the constructs (without proof). In section 4, it will be shown, how upon the above a formal calculus for the derivation of programs can be founded. We would like to stress that we do not present an algorithm" for the derivation of programs: we have used the term "a calculus" for a formal discipline --a set of rules- such that, if applied successfully 1)
2)
correct progrem
(In choosing the term "calculus" we have been inspired by the integral
BNF
"{ .
s:
instances of the enclosed". <guarded command>::"' < guard>- <guarded list > <guard>::"'< boolean expression> <guarded list>::= <statement>{; <statement> } <guarded command set>::"' <guarded command> {0 <guarded commend>} <alternative construct>::= if < guarded command set>fi <repetitive construct>::= do < guarded commend set> <statement>
-
in
selected for
its statements
will
be executed
successively in the order from left to right; a guarded list will only be
EWD418 - 2
selected
for
execution in
Note that
it is component of e
If the
they are arbitrarily
guarded command,
an
mutually
s epa rated
by the separator
"0"
i.e.
of a
command set.
fi".
Note.
"if
fi"
would be
(End
of note.
)
fashion-
and
assigns to
the maximum
and
:
X
.!f.
2: 2:
y
x
m::
U
fi
m:= y
".!!2.
..!:!."
Here
state
in
lead to
however,
are
true,
new selection for execution of a gua rd ed list with a true and so on. When the repetitive construct has
will take
place,
terminated
properly,
..QQ" would
be
semantically equivalent to
(End
of note.
EWD418 - 3
An is the
gr eat e r g lo ry
.I
--
program
and
Q4,
such
q1 q2 <q3 <q4
Using
can
we
program
:=
q2
0 q2 > q3 0 q3 > q4
.2
q2, q3 q3, q4
To conclude this s e c t io n
but al so the final s tat e is
should
fu
determine
such
that
(n > o)
and
a fixed
nct i on
f(i)
defined for
and N i :
0 <i
i <
k
>
n: f(k)
of
maximum.)
k:c:
do j
0;
j:= 1;
n
f=
j:= j + 1
-
D f(j) f(k)
k:: j; j:= j
an d
3.
formal
definition of
t he semantics.
the
following sections
sym bols
and
to
alternatively
satisfied by predicates
b
all
is and
true.
"F"
Two
special
"T"
play a
special role:
t he condition by
tha t, the
definition,
on d it i on that
is satisfied
all states,
den o tes,
definition,
is satisfied
EWD418 - 4
by no state at all .
[1 ],
the main
being
while Hoare
terminate),
"weakest"--
we shall introduce
pre-conditions such
mechnisms
"wp S,
R)"
where
to denote the weakest pre-condition for the initial state that activation of
5
is
because it
--
has,
by definition,
1)
For any
wp(5,
F)
:;::
( the
so-called "Law
S
of the Excluded
Miracle").
such that for all states
2)
for any
p => Cl
we have for all states wp( S,
P) =>
wp(S,
Q)
P and
3) states
For any
( wp ( S, P)
For any
5
and wp(S,
q))
wp S,
P
P
and
Q)
we have for all
4)
states
(wp(S,
P)
or wp ( S,
Q))
=> wp(S ,
Q)
Together with the rules of propositional calculus and the semantic definitions to be given below, the above four properties take over the
EWD418 -
We take the position that we know the semantics of sufficiently well wp(S, R)
m ec han ism
if
we know its
predic ate
R
transformer,
i.e.
can d eriv e
Note.
acknowledgement
the case
transformer does
complete m
description:
about the final states in which the system might find i tse l f after proper termination. (End of note.)
Example 1.
denoted by R
"skip",
are
we have
Example 2.
given by
11x:= E"
are
R)
of th e predicate defining R
in wh i h each
in which
d e not es
c op y
o ccu rence
E xample 3.
of
the variable
"x"
is
replaced by
"(E)".
ere g iven by
wp("S1;
52", R)
3.2.
In
The
alternative construct.
to define the seqantics
o r d er
of
let
"BB11
denote .
\=
i _< n:
B )
i
then,
by definition
EWD418
wp(IF, R)
(The first term
n ot
(BB
arid
Ni:
:S i n:
B.
l.
=>wp(SL.,
l.
R))
"BB"
requires
construct as
such
will
requires
e a ch
an
ecceptable final state.) from this definition we can derive --by simple
substitutions--
Theorem 1
From
i < n: that => wp( IF,
Ct/i: 1 we
can c
(Q B.) =>
l.
wp(SL., R))
l
on clu de
and
(Q
Let
end of
sys t
BB)
R)
holds
for
all
state s
. "tIt
d no e
defined on
the
state space,
e weakest
is guaranteed to lead to a
such that
t is decreased by at
"wdec" we can
l eas t
mulate the
for
Theorem
2.
0Ji:
we can
(Q - B.) end
l
=>wdec(SL., t))
l.
conlude
(Q and BB)
=> wdec(Ir,
t)
Nob
(which
t0
as
the
un
kn own .
(Here we have
t he state the
X.)
Then
tmin(X)
t
as
bound for
final
value of
is activated with
as initial state.
Then, by
definition,
t)
(tmin(X)
t(X)
1)
(tmin(X)
<
t(X))
EWD418
3.3.
As is
The
reoetitive construct.
to
be expected,
SL1
0 ... 0
- SL
n
that Let
and
we denote b y
"DO"
is more complicated.
Hk(R)
E. H0(R))
fi
11
(where then,
"IF"
by definition wp(DD, R)
(3k: k
0:
Hk(R))
(Intuitively,
anteeing proper termination after at most leaving the system in a induction we can prove final
state
satisfying
.}
Via mathematical
Theorem
3.
From
(P EB)
and
=> (wp(IF,
P)
and wdec(IF,
t))
P)=> (t
/
> o) ,
f o r Bll states
l!!:!
BB)
Because wp(S, T
by all
states, for
proper termination
repetitive
construct,
Theorem 4.
From
(P
nd
BB)
=> wp(IF,
P)
for
all
states,
states
(P In
and wp(DO,
T))
=>
wp(DD,
P and
BB)
"P11 is called
EWD418 - 8
and
"t"
is
The f
Hm:=
max ( x , y)"
--see above--
and
Now
the
Axiom of
A ssignm en t tells us
"m = x"
that
Hm:=
xH
is
"m:=
the is
x"
standard a way do
the
first term of
Will
x?:.y as
its g u ar d,
Theorem 1 tell
us that
m:=
x fi
it terminates succesfully.
will
produce the
correct result if
looking
The disad
T,
for
alternatives
assignment
the guard
p( m : = y", R)
"
y?:. x
thus
we
are led to
our pr x
ogra m
if
fi end by this time BB
m:= x m:= y
0 y ?:. x -
an
d therefore we
that th
have solved
the
problem. (In
we
have proved
R
con
maximum of two v lu
m
a es is always
has always a
n d,
viz. that
s id ere d
as equation for
solution.)
As an
construct we shall
for fixed,
and
e have
to est bli
s h the
final
relation
EWD418
x = gcd(X, Y)
The formal machinery only gets in motion, once we have chosen our
invariant relation and our variant function. The program then gets the
structure "establish the relation P of P" & Suppose that we choose for the invariant relation P:
gcd X,
to
be kept invariant;
Y)
gcd(x, y) and
> 0 and
y > 0
a relation
thet
y:=
be
y:= E1, E2
such
that
E2 > 0)
contain the
mu s
computation of that
gcd X,
Y)
E1
--for
problem-
t see to it
the
expressions
and E2
chosen,
that
gcd(E1,
if
E2
true
gdc(x, y) =
gcd E1,
E2)
In other words we are invited to massage the value pair a fashion that their gcd
y) in such
is
the place
gcd(x
y, y)
x:= x
Deriving
- y
EWD418 - 1
R)
(gcd(X, Y)
gcd{x
y, y) and
x -
implied
by
>y
of th P is concerned.
fu Besides
inv
a r i ance
of
that
we
must require
guaranteed decrease
e variant
of the choice
From
wp("x:=
wp{"x:=
y",
- "", J
t 5 t ) = 0 x + y t0)
= (x < t0)
we conclude that
tmin ,.,
therefore
wdec(":=
y", t)
= (x
<
+ y)
= (y > 0)
of
t
- y",
imposes no further
of
t)
is fully
implied
we
Y;
Xl"' X
.Q.Q.
> y -
- y .!!
Alas,
from
8B
we
x =
gcd(X, Y).
wi l
In a
the
alternative y:= y
is
- x
and
our
next effort
x -
X t
y: = Y '
x -
do
'
>y
X
)( -
y
X
0
9.
y>
y- y
-
am
non
BB
= y)
(P x
= y)
=> (x = gcd(X, Y)
t x
because
the
gdc(x, x) = x
N ote. The
gcd
choice of symmetric
+2y
nd
the
is
EWD418- 11
x:= X; y:= Y; do
X
>y
x::
'j
X
0
od The swa p
"x, y
:
y >X
- X,
y := y,
y, x"
P :
the
g u ar d of the last
t
requirement that
is effectively
In
game has been t o f i nd a large enough set of the disjunction of their guards,
su c h
guarded
BE
was suffi
ciently weak: in the case of the alternative construct the purpose is avoiding abortion, in the cas e of the re p e tit iv e
ge ttin g
BB
and BB
5.
The
tr i gge red
research,
t he
outcome of
which is
by the
re lation
x > 0 y >0
true.
It
was
only
saw
synchronizing
of
(cyclic) sequential
opP.rating
s eque n tial
t hat while for sequential programs the situation "all guards false" is
it
means
ersona l
things
o rd e r to be
able
to
were--
activjty
can
be regarded as formal
EYJD418
12
forward activity, that of a repetitive construct requires what I "the invention" of an invariant relation and a variant function. the main va lue of the calculus shown in
re gard as For me ,
ened my skepticism about some of the claims or go;;ls of 11automatic pro gramming11; me presenting this calculus should not be interpreted as me it just gives
sugges ting that all programs should be developed that way: us another handle.
however,
other defi nition techniques: thP. defini tion via predicate transforers
s eems to lend itself most radily to being forged into a tool for the goal directed activ i ty of program composition.
Finally I would like to say a word or two about the role of tential nan-determinacy.
the po
which perm its user programs to become non-deterministic pres en ts dreadful problems to the maintenance engineer: granted. ware.) " it is nat a
" facility "
to be lightly
( This
found myself willing to consider non-deteinistic programs seriously. is, however, fair to say that I could not have discovered the calculus
shown before having take n that hurdle and I leave it to the environment whether the non-determinacy is eventua lly resolved by human in terventi on or mechanic ally , in a reproducible manner or not. (It is only in an envi
ronment in which all programs should be determini stic, where non-reproducible behaviour is interpreted as machin e malfunctioning: I can easily think of
Acknowledgements.
In the irst place my acknowledgements are due to the memb ers of the IFIP Working Group W.G.2.3
a
on
" Progrcrnming
Methodology":
it was,
as
matter of fact,
during the
Blanchland
October
EWD418 - 13
this
effort
Woodger deserve my special thanks. Besides them, W.H.J.Feijen, D.E.Knuth, M.Rem and C.S.Scholten have been directly helpful in one way or
I should
elso thank
in San Diego and Luxembourg (courtesy Burroughs Corporation)-- that have played their role of critical sounding board beyond what to hope. is entitled
(1]
Hoare,
( Oct.
[2]
Comm.ACM
(Hay 1960)
299- 314
26th
June
1974
prof.dr.Edsger Burroughs
W.Dijkstra
NUENEN
Research fellow