Guarded command
A guarded command consists of a boolean condition or guard, and a statement "guarded" by it. The statement is only executed if the guard is true, so when reasoning about the statement, the condition can be assumed true. This makes it easier to prove the program meets a specification.
skip and abort
skip and abort are important statements in the guarded command language. abort is the undefined instruction: do anything. It 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 often used when the syntax requires a statement but the state should not change.Syntax
skip abortSemantics
* skip: do nothing * abort: do anythingSyntax
v := E or v, v, ..., v := E, E, ..., E where * v are program variables * E are expressions of the same data type as their corresponding variablesCatenation
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 whose guard is true is arbitrarily chosen to be executed. If no guard is true, the result is undefined, that is, equivalent to abort. Because at least one of the guards must be true, the empty statement skip is often needed. The statement if fi has no guarded commands, so there is never a true guard. Hence, if fi is equivalent to abort.Syntax
if G0 → S0 □ G1 → S1 ... □ Gn → Sn fiSemantics
Upon execution of a selection, the guards are evaluated. If none of the guards is ''true'', then the selection aborts, otherwise one of the clauses with a ''true'' guard is chosen arbitrarily and its statement is executed.Implementation
GCL does not specify an implementation. Since guards cannot have side effects and the choice of clause is arbitrary, an implementation may evaluate the guards in any sequence and choose the first ''true'' clause, for example.Examples
Simple
In pseudocode: if a < b then set c to True else set c to False In guarded command language: if a > b → c := true □ a < b → c := false fiUse of skip
In pseudocode: if error is True then set x to 0 In guarded command language: if error → x := 0 □ error → skip fi If the second guard is omitted and error is 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, the implementation may find that one is easier or faster than the other. Since there is no difference to the programmer, any implementation will do.Repetition: ''do''
Execution of this repetition, or loop, is shown below.Syntax
do G0 → S0 □ G1 → S1 ... □ Gn → Sn odSemantics
Execution of the repetition consists of executing 0 or more ''iterations'', where an iteration consists of arbitrarily choosing a guarded command whose guard is true and executing the command . Thus, if all guards are initially false, the repetition terminates immediately, without executing an iteration. Execution of the repetition do od, which has no guarded commands, executes 0 iterations, so do od is equivalent to skip.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. Dijkstra sees in this algorithm a way of synchronizing two infinite cyclesa := a - b
and b := b - a
in such a way that a≥0
and b≥0
remains true.
Non-deterministic sort
do abubble sort is not more efficient than its deterministic version, but easier to prove: it will not stop while the elements are not sorted and that each step it sorts at least 2 elements.Applications
Programs correct by construction
Generalizing the observational congruence of Guarded Commands into a lattice has led to Refinement Calculus. This has been mechanized inAsynchronous 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. AIModel 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 modulReferences