An event is a 4-tuple which specifies a state transition. The same event can occur multiple times in a computation. Each occurrence of the event is a separate step of the computation. The following example illustrates the difference between a step and an event.
Consider an agent that receives numbers on an input
channel and sends squares of the numbers on an output channel.
The agent may receive the message 3 and send the message 9 multiple
times in a computation.
The specification of an execution of a receive
function
which receives 3 and sends 9 is specified by an event -- a 4-tuple.
Each execution in which the agent receives 3 and sends 9 is the
execution of the same event.
Each execution in which the agent receives 3 and sends 9 is a
different step of the computation.
A loop invariant is a predicate (a boolean formula) that holds at the beginning and end of each step of a computation. The following example illustrates the concept.
The algorithm computes the GCD (greatest common divisor) of positive
integers X
and Y
.
x, y = X, Y # gcd(x, y) = gcd(X, Y) while x != y: # gcd(x, y) = gcd(X, Y) if x > y: x = x- y if y > x: y = y - x # gcd(x, y) = gcd(X, Y) return xThe invariant is:
gcd(x, y) = gcd(X, Y)It holds before each step.
If the algorithm terminates then we have
x == yand so at termination:
gcd(x, y) = gcd(x, x) = x = gcd(X, Y)
set up initial state of the system # loop invariant holds while there exists an event that can be executed: # loop invariant holds execute any executable event # loop invariant holdsSo, we have to prove that the invariant holds in all initial states, and if the invariant holds before any event that is executable then it continues to hold after the event is executed.
A loop variant is a function of the state (variables) of a program. The value of the function decreases at each iteration of a loop. The function is bounded below, and so the value of the function cannot decrease forever. Therefore the loop terminates.
Let's look at the example given earlier to compute gcd(X,
Y)
.
A loop variant is x + y
.
Each iteration of the loop decreases x + y
.
It is bounded below by 2
-- the bound doesn't matter as
long as there is a bound.
Because it is bounded below and cannot decrease forever the loop terminates.
Computations shows how a while loop of a sequential program can be used to analyze distributed algorithms.
K. Mani Chandy, Emeritus Simon Ramo Professor, California Institute of Technology