ArticleslgStudy

science

Guarded Command Language

Guarded Command Language is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Guarded Command Language rather than just read about it. In short: The Guarded Command Language (GCL) is a programming language defined by Edsger Dijkstra for predicate transformer semantics in EWD472. It combines programming concepts in a compact way.

Key takeaways

  • Guarded Command Language belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Guarded Command Language to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Guarded Command Language from memory before moving on to harder problems.

Reference excerpt

The Guarded Command Language (GCL) is a programming language defined by Edsger Dijkstra for predicate transformer semantics in EWD472. It combines programming concepts in a compact way. It makes it easier to develop a program and its proof hand-in-hand, with the proof ideas leading the way; moreover, parts of a program can actually be calculated. An important property of GCL is nondeterminism. For example, in the if-statement, several alternatives may be true, and the choice is made at runtime, when the if-statement is executed. This frees the programmer from having to make unnecessary choices and is an aid in the formal development of programs. GCL includes the multiple assignment statement. For example, execution of the statement x, y:= y, x is done by first evaluating the righthand side values and then storing them in the lefthand variables. Thus, this statement swaps the values of x and y. The following books discuss the development of programs using GCL:

Dijkstra, Edsger W. (1976). A Discipline of Programming. Prentice Hall. ISBN 978-0132158718. Gries, D. (1981). The Science of Programming. Monographs in Computer Science (in English, Spanish, Japanese, Chinese, Italian, and Russian). New York: Springer Verlag. doi:10.1007/978-1-4612-5983-1. ISBN 978-0-387-96480-5. S2CID 37034126. Dijkstra, Edsger W.; Feijen, Wim H.J. (1988). A Method of Programming. Boston, MA: Addison-Wesley Longman Publishing Co., Inc. p. 200. ISBN 978-0-201-17536-3. Kaldewaij, Anne (1990). Programming: the derivation of algorithms. Prentice-Hall, Inc. ISBN 0132041081. Cohen, Edward (1990). David Gries (ed.). Programming in the 1990s: An introduction to the calculation of programs. Texts and Monographs in Computer Science. Springer Verlag. doi:10.1007/978-1-4613-9706-9. ISBN 978-1-4613-9706-9. S2CID 1509875.

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.

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

G is a proposition, called the guard S is a statement

Semantics If G evaluates to true, S is eligible to be executed. In most GCL constructs, multiple guarded commands may have guards that are true, and exactly one of them is chosen arbitrarily to be executed. If G is false, S will not be executed.

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

abort

Semantics skip: do nothing abort: do anything

Assignment Assigns values to variables.

Syntax v := E

or

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

where

v are program variables E are expressions of the same data type as their corresponding variables

Catenation 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 fi

Semantics 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 fi

Use 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 od

Semantics Execution of the repetition consists of executing 0 or more iterations, where an iteration consists of arbitrarily choosing a guarded command Gi → Si whose guard Gi is true and executing the command Si. 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 cycles a := a - b and b := b - a in such a way that a≥0 and b≥0 remains true.

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) .

Non-deterministic sort do a>b → a, b := b, a □ b>c → b, c := c, b □ c>d → c, d := d, c od

The program keeps on permuting elements while one of them is greater than its successor. This non-deterministic bubble 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.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Guarded Command Language

Start with the simplest possible case. Write down what Guarded Command Language claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Guarded Command Language before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Guarded Command Language ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Guarded Command Language

In research
Guarded Command Language appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Guarded Command Language in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Guarded Command Language is common in secondary-school and first-year university syllabi. It links to neighbouring topics Edsger W. Dijkstra, Logic programming, so understanding it makes those chapters shorter.
In everyday life
Look for Guarded Command Language outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Guarded Command Language” →

How to study Guarded Command Language in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Guarded Command Language means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Guarded Command Language out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Guarded Command Language in simple terms?

The Guarded Command Language (GCL) is a programming language defined by Edsger Dijkstra for predicate transformer semantics in EWD472. It combines programming concepts in a compact way.

Why does Guarded Command Language matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Guarded Command Language?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Guarded Command Language.

Tags

  • Edsger W. Dijkstra
  • Logic programming

Keep exploring