summaryrefslogtreecommitdiffstats
path: root/content/zettel/1b1.md
blob: 14b0c84f087b6ac13eb8a81c50971af9aa891e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
+++
title = "Guarded commands"
author = "Yann Herklotz"
tags = []
categories = []
backlinks = ["1d1", "1b2", "1b"]
forwardlinks = ["1b2"]
zettelid = "1b1"
+++

Guarded commands \[1\] are an interesting construct which can be added
to languages. They look similar to `case` statements, but behave in a
parallel and nondeterministic way. Each guard has a boolean value
followed by a program which may be executed if the guard evaluates to
true. The following shows the main syntax that guards may have.

``` grammar
e ::= if gc fi | do gc od ...

gc ::= (b e) || gc
```

One reason these are interesting language constructs, is because they
allow for the encoding of commands that may be executed when a condition
is true, but that it isn't necessary. Often, when giving instructions,
one does not really specify the order, just the commands that should
eventually be executed.

The guarded commands `gc` will either return a match if a boolean
evaluates to true, or `abort`. There are two constructs that are built
around guarded commands which adds more functionality to them.
`if gc fi` matches one rule in the guarded statement and executes it. If
it does not match a rule, it then acts like `abort`. `do gc od` loops
over the guarded commands while any rule matches. If there no match is
found, it acts like `skip`.

These allow for nice encoding of common algorithms, using two other
constructs that use the guarded commands.

<div id="refs" class="references csl-bib-body" markdown="1">

<div id="ref-winskel93" class="csl-entry" markdown="1">

<span class="csl-left-margin">\[1\]
</span><span class="csl-right-inline">G. Winskel, *The formal semantics
of programming languages: An introduction*. MIT press, 1993.</span>

</div>

</div>