Examples of Custom Control Flow Compiling Words
Asked Answered
S

2

10

Forth famously allows users to alter the language by defining new words for control flow (beyond those given by the standard: DO, LOOP, BEGIN, UNTIL, WHILE, REPEAT, LEAVE IF, THEN, ELSE, CASE, ENDCASE, etc.)

Are there common examples of people actually creating their own new control flow words? What are some typical and useful examples? Or has the standard already defined everything that people actually need?

I'm hoping to find examples of useful language extensions that have gained acceptance or proved generally helpful to make the language more expressive.

Speakeasy answered 30/6, 2017 at 17:31 Comment(0)
D
7

One another big direction of control flow structures in Forth is backtracking. It is very expressive and powerful mechanism. To be implemented, it requires return address manipulation [Gas99].

Backtracking in Forth was developed as BacFORTH extension by M.L.Gassananko in ~1988-1990. First papers on this topic was in Russian.

The technique of backtracking enables one to create abstract iterator and filter modules responsible for looking over sets of all possible values and rejecting "undue" ones [Gas96b].

For some introduction see the short description: Backtracking (by mlg), also the multi-threading in Forth? discussion in comp.lang.forth can be useful (see the messages from Gassanenko).

Just one example of generator in BacFORTH:

: (0-2)=> PRO 3 0 DO I CONT LOOP ; \ generator
: test  (0-2)=>  CR . ." : " (0-2)=>  .  ;
test CR

Output:

0 : 0 1 2
1 : 0 1 2
2 : 0 1 2

The PRO and CONT are special control flow words. PRO designates generator word, and CONT calls the consumer — it is something like yield in Ruby or ECMAScript. A number of other special words is also defined in BacFORTH. You can play with BacFORTH in SP-Forth (just include ~profit/lib/bac4th.f library).

Etymology

In general, backtracking is just an algorithm for finding solutions. In Prolog this algorithm was embedded under the hood, so backtracking in Prolog is the process how it works themselves. Backtracking in BacFORTH is programming technique that is supported by a set of special control flow words.

References

Derman answered 10/7, 2017 at 16:45 Comment(3)
Thank you. This was eye-opening. Do you know if continuations, coroutines, and finite state machines are readily possible as well?Speakeasy
Can you elaborate a little bit on what backtracking is (for future readers)?Swamper
@PeterMortensen I added some info and links, please let me know if more details be appropriate here.Derman
S
7

Here's one example. CASE was a somewhat late addition to the set of Forth control flow words. In early 1980, a competition for defining the best CASE statment was announced in Forth Dimensions. It was settled later that year with a tie between three entries. One of those ended up in the Forth94 standard.

Southward answered 30/6, 2017 at 18:17 Comment(1)
To my knowledge, CATCH and THROW also lived as user defined control structures before they became standardLoge
D
7

One another big direction of control flow structures in Forth is backtracking. It is very expressive and powerful mechanism. To be implemented, it requires return address manipulation [Gas99].

Backtracking in Forth was developed as BacFORTH extension by M.L.Gassananko in ~1988-1990. First papers on this topic was in Russian.

The technique of backtracking enables one to create abstract iterator and filter modules responsible for looking over sets of all possible values and rejecting "undue" ones [Gas96b].

For some introduction see the short description: Backtracking (by mlg), also the multi-threading in Forth? discussion in comp.lang.forth can be useful (see the messages from Gassanenko).

Just one example of generator in BacFORTH:

: (0-2)=> PRO 3 0 DO I CONT LOOP ; \ generator
: test  (0-2)=>  CR . ." : " (0-2)=>  .  ;
test CR

Output:

0 : 0 1 2
1 : 0 1 2
2 : 0 1 2

The PRO and CONT are special control flow words. PRO designates generator word, and CONT calls the consumer — it is something like yield in Ruby or ECMAScript. A number of other special words is also defined in BacFORTH. You can play with BacFORTH in SP-Forth (just include ~profit/lib/bac4th.f library).

Etymology

In general, backtracking is just an algorithm for finding solutions. In Prolog this algorithm was embedded under the hood, so backtracking in Prolog is the process how it works themselves. Backtracking in BacFORTH is programming technique that is supported by a set of special control flow words.

References

Derman answered 10/7, 2017 at 16:45 Comment(3)
Thank you. This was eye-opening. Do you know if continuations, coroutines, and finite state machines are readily possible as well?Speakeasy
Can you elaborate a little bit on what backtracking is (for future readers)?Swamper
@PeterMortensen I added some info and links, please let me know if more details be appropriate here.Derman

© 2022 - 2024 — McMap. All rights reserved.