-
Notifications
You must be signed in to change notification settings - Fork 1
Building blocks
This page lists various small components that occasionally come in handy for simple control flow.
2x2 blocks are fairly useful components for many Labyrinth programs. They are Labyrinth's equivalent of single-line if
s or while
s in C-family languages that allow you to omit the braces. They are both the most compact loops possible (except for very rare exceptions) and also very compact conditionals, provided that you only want to apply a couple of commands inside the conditional/loop.
This section investigates those blocks which have exactly one entry and one exit point. There are 12 such components. The abcd
represent arbitrary commands. In each of these, the entry point is on the top left, going east:
"
"ab" "ab "ab
cd cd cd
"
"
"ab "ab "ab
cd" cd cd
"
The other six are reflections of these. I will not treat these separately - in the following explanations simply swap all occurrences of "positive" and "negative" when you want to use the reflection.
The full dynamics of each of these blocks can be incredibly complicated when we're considering arbitrary stack values at arbitrary ticks, and are best worked out from case to case. However, if certain conditions are fulfilled, then these can be reliably used for simple control flow. For each of them, we'll distinguish three basic use cases:
-
Switch: The 2x2 block is only traversed once, but different values take different paths. This allows you to apply some operations only to certain values. In these cases the control flow paths will always diverge after executing
a
. -
CW loop: The block can be used as a loop with the instruction pointer going around the four commands in a clockwise order,
abdcabdc...
. This will then be the same for all input values, but the difference is in when the loop is exited. -
CCW loop: Alternatively, the block can be used as a counter-clockwise loop, executing the commands as
acdbacdb...
.
In the following we'll consider each three use cases for each of the six 2x2-block setups.
Type | Branches | Conditions |
---|---|---|
FL |
0 : b
|
b -> 0 |
± : cdb
|
b -> + |
|
FR |
0 : bd
|
d -> - |
± : cd
|
d -> 0 |
|
RL |
0 : bd
|
d -> 0 |
± : cd
|
d -> + |
|
RR |
0 : bdc
|
c -> - |
± : c
|
c -> 0 |
|
LL |
- : ()
|
none |
0 : bdca
|
a -> 0 |
|
+ : cdba
|
a -> + |
|
LR |
0 : b
|
b -> - |
± : cdb
|
b -> 0 |
"ab"
cd
(Remember that the relevant value for the conditional is the one after applying a
.)
- Distinguishes between zero and non-zero.
- Code applied to zero:
ab
- Code applied to non-zero:
acdb
- Conditions: on input zero
b
must also result in zero. On non-zero input,cdb
must result in a positive result.
- Always executes
ab
. - Loop body:
dcab
. - Terminates when:
b
results in zero. - Conditions:
a
must result in zero when entering the loop, and in a positive result in every loop iteration.
- Always executes:
acdb
. - Loop body:
acdb
. - Terminates when:
b
results in a positive value. - Conditions:
a
must always give a non-zero result.b
must always give a non-zero result.
"ab
cd"
(Remember that the relevant value for the conditional is the one after applying a
.)
- Distinguishes between zero and non-zero.
- Code applied to zero:
abd
- Code applied to non-zero:
acd
- Conditions: on input zero
bd
must give a negative result. On non-zero input,cd
must result in zero.
- Always executes
abd
. - Loop body:
cabd
. - Terminates when:
d
results in something negative. - Conditions:
a
must result in zero when entering the loop, and in a positive result in every loop iteration.d
must always give a non-zero result.
- Always executes:
acd
. - Loop body:
bacd
. - Terminates when:
d
results in zero. - Conditions:
a
must always give a non-zero result.
"ab
cd
"
(Remember that the relevant value for the conditional is the one after applying a
.)
- Distinguishes between zero and non-zero.
- Code applied to zero:
abd
- Code applied to non-zero:
acd
- Conditions: on input zero
bd
must result in a zero. On non-zero input,cd
must give a positive result.
- Always executes
abd
. - Loop body:
cabd
. - Terminates when:
d
results in zero. - Conditions:
a
must result in zero when entering the loop, and in a positive result in every loop iteration.
- Always executes:
acd
. - Loop body:
bacd
. - Terminates when:
d
gives a positive result. - Conditions:
a
must always give a non-zero result.d
must also give a non-zero result.
"ab
cd
"
(Remember that the relevant value for the conditional is the one after applying a
.)
- Distinguishes between zero and non-zero.
- Code applied to zero:
abdc
- Code applied to non-zero:
ac
- Conditions: on input zero
bdc
must give a negative result. On non-zero input,c
must result in zero.
- Always executes
abdc
. - Loop body:
abdc
. - Terminates when:
c
gives a negative result. - Conditions:
a
must result in zero when entering the loop, and in a positive result in every loop iteration.c
must always give a non-zero result.
- Always executes:
ac
. - Loop body:
dbac
. - Terminates when:
c
results in zero. - Conditions:
a
must always give a non-zero result.
"
"ab
cd
(Remember that the relevant value for the conditional is the one after applying a
.)
- Distinguishes between negative and zero and positive.
- Code applied to negative:
a
. - Code applied to zero:
abdca
- Code applied to positive:
acdba
- Conditions: on input zero
bdca
must result in zero. On positive input,cdba
must give a positive result.
- Always executes
abdca
. - Loop body:
bdca
. - Skippable when
a
gives a negative result. - Terminates when:
a
results in zero. - Conditions:
a
must result in zero when entering the loop, and in a non-negative result in every loop iteration.
- Always executes:
acdba
. - Loop body:
cdba
. - Skippable when
a
gives a negative result. - Terminates when:
a
gives a positive result. - Conditions:
a
must give a positive result when entering the loop, and a non-zero result in every loop iteration.
"
"ab
cd
(Remember that the relevant value for the conditional is the one after applying a
.)
- Distinguishes between zero and non-zero.
- Code applied to zero:
ab
- Code applied to non-zero:
acdb
- Conditions: on input zero
b
must give a negative result. On non-zero input,cdb
must result in zero.
- Always executes
ab
. - Loop body:
dcab
. - Terminates when:
b
gives a negative result. - Conditions:
a
must result in zero when entering the loop, and in a positive result in every loop iteration.b
must always give a non-zero result.
- Always executes:
acdb
. - Loop body:
acdb
. - Terminates when:
b
results in zero. - Conditions:
a
must always give a non-zero result.