Guarded Command Language

The Guarded Command Language (GCL) is a language defined by Edsger Dijkstra for predicate transformer semantics.[1] It combines programming concepts in a compact way, before the program is written in some practical programming language. Its simplicity makes proving the correctness of programs easier, using Hoare logic.

Guarded command

The guarded command is the most important element of the guarded command language. In a guarded command, just as the name says, the command is "guarded". The guard is a proposition, which must be true before the statement is executed. At the start of that statement's execution, one may assume the guard to be true. Also, if the guard is false, the statement will not be executed. The use of guarded commands makes it easier to prove the program meets the specification. The statement is often another guarded command.

Syntax

A guarded command is a statement of the form G S, where

If G is true, the guarded command may be written simply S.

Semantics

At the moment G is encountered in a calculation, it is evaluated.

Skip and Abort

Skip and Abort are very simple as well as important statements in the guarded command language. Abort is the undefined instruction: do anything. The abort statement does not even need to terminate. It is used to describe the program when formulating a proof, in which case the proof usually fails. Skip is the empty instruction: do nothing. It is used in the program itself, when the syntax requires a statement, but the programmer does not want the machine to change states.

Syntax

skip
abort

Semantics

Assignment

Assigns values to variables.

Syntax

v := E

or

v0, v1, ..., vn := E0, E1, ..., En

where

Concatenation

Statements are separated by one semicolon (;)

Selection: if

The selection (often called the "conditional statement" or "if statement") is a list of guarded commands, of which one is chosen to execute. If more than one guard is true, one statement is nondeterministically chosen to be executed. If none of the guards are true, the result is undefined. Because at least one of the guards must be true, the empty statement "skip" is often needed.

Syntax

if G0  S0
| G1  S1
...
| Gn  Sn
fi

Semantics

Upon execution of a selection all guards are evaluated. If none of the guards evaluates to true then execution of the selection aborts, otherwise one of the guards that has the value true is chosen non-deterministically and the corresponding statement is executed.[2]

Examples

Simple

In pseudocode:

if a < b then c := True
else c := False

In guarded command language:

if a < b  c := true
| a ≥ b  c := false
fi

Use of Skip

In pseudocode:

if error = True then x := 0

In guarded command language:

if error = true  x := 0
| error = false  skip
fi

If the second guard is omitted and error = False, the result is abort.

More guards true

if a ≥ b  max := a
| b ≥ a  max := b
fi

If a = b, either a or b is chosen as the new value for the maximum, with equal results. However, someone implementing this, may find that one is easier or faster than the other. Since there is no difference to the programmer, he is free to implement either way.

Repetition: do

The repetition executes the guarded commands repeatedly until none of the guards are true. Usually there is only one guard.

Syntax

do G0  S0
| G1  S1
...
| Gn  Sn
od

Semantics

Upon execution of a repetition all guards are evaluated. If all guards evaluate to false then skip is executed. Otherwise one of the guards that has value true is chosen non-deterministically and the corresponding statement is executed after which the repetition is executed again.

Examples

Original Euclidean algorithm

a, b := A, B;
do a < b  b := b - a
| b < a  a := a - b
od

This repetition ends when a = b, in which case a and b hold the greatest common divisor of A and B.

Extended Euclidean algorithm

a, b, x, y, u, v := A, B, 1, 0, 0, 1;
do b ≠ 0 
   q, r := a div b, a mod b;
   a, b, x, y, u, v := b, r, u, v, x - q*u, y - q*v
od

This repetition ends when b = 0, in which case the variables hold the solution to Bézout's identity: xA + yB = gcd(A,B) .

Applications

Programs correct by construction

Generalizing the observational congruence of Guarded Commands into a lattice has led to Refinement Calculus.[3] This has been mechanized in Formal Methods like B-Method that allow one to formally derive programs from their specifications.

Asynchronous Circuits

Guarded commands are suitable for Quasi Delay Insensitive circuit design because the repetition allows arbitrary relative delays for the selection of different commands. In this application, a logic gate driving a node y in the circuit consists of two guarded commands, as follows:

PullDownGuard  y := 0
PullUpGuard  y := 1

PullDownGuard and PullUpGuard here are functions of the logic gate's inputs, which describe when the gate pulls the output down or up, respectively. Unlike classical circuit evaluation models, the repetition for a set of guarded commands (corresponding to an asynchronous circuit) can accurately describe all possible dynamic behaviors of that circuit. Depending on the model one is willing to live with for the electrical circuit elements, additional restrictions on the guarded commands may be necessary for a guarded-command description to be entirely satisfactory. Common restrictions include stability, non-interference, and absence of self-invalidating commands.[4]

Model Checking

Guarded commands are used within the Promela programming language, which is used by the SPIN model checker. SPIN verifies correct operation of concurrent software applications.

Other

The Perl module Commands::Guarded implements a deterministic, rectifying variant on Dijkstra's guarded commands.

References

  1. Dijkstra, Edsger W. "EWD472: Guarded commands, non-determinacy and formal. derivation of programs." (PDF). Retrieved August 16, 2006.
  2. Anne Kaldewaij (1990), Programming: The Derivation of Algorithms, Prentice Hall
  3. Back, Ralph J (1978). "On the Correctness of Refinement Steps in Program Development (Phd-Thesis)" (pdf).
  4. Martin, Alain J. "Synthesis of Asynchronous VLSI Circuits".
This article is issued from Wikipedia - version of the 7/3/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.