Forth

A Forth interpreter flow chart (Source: https://www.embedded.com/introduction-to-forth/)

Forth is not a mainstream programming language. Whenever it is compared to something, the most operative word is different. It is almost like assembly language, which is how a machine would interpret code, if it used English, rather than 0s and 1s to calculate and communicate. Some refer to Forth as a virtual machine, which is software pretending to be a physical machine. In part, this is because it is not just a programming language, but also an operating system. Despite this, Forth is simple. It can run on a few kilobytes (kB) of memory. When coded appropriately, it seems to be its own independent language, but with a lot of English-like words.

While Forth was invented by Charles (Chuck) Havice Moore II (1938 – ) in 1970. It was operationalized by Elizabeth (Bess) Rather (1940 – ), who – with Moore – started Forth, Inc. in 1973. Rather refined and ported Forth to numerous platforms throughout the 1970s. She also chaired the ANSI Technical Committee that produced the ANSI Standard for Forth (1994).

Forth was made specifically for the real-time control of telescopes at the United States National Radio Astronomy Observatory and, later, at Kitt Peak National Observatory. A real-time response is one that guarantees that something will happen within a specified time period. In other words, it sets a deadline for something to happen, usually one that is relatively short. Thus, a real-time process is one that happens within defined time steps of some maximum duration.

Forth is the antithesis of Ada. Wikipedia defines Ada as “a structured, statically typed, imperative, and object-oriented high-level programming language, extended from Pascal and other languages.” In its purest form, Forth is none of these, with the exception of being imperative. Most computer languages are imperative. They use statements/ commands to change a program’s state. Ada originated in the 1970s because of US Department of Defense (DoD) concerns about the high number of programming languages being used in embedded computer systems. They wanted one language to do everything. Unfortunately, with Ada they created a monster that was far too large and complex, that was slow to compile and difficult to run. A compiler (used with a compiled language) requires code to be translated into machine language, before it can be run. In contrast, an interpreter (used with an interpreted language) directly executes instructions without requiring them to have been translated into a machine language, in advance.

Allegedly, a Forth program can be compiled, but not if it contains words that are only evaluated at runtime: DOES>, EVALUATE and INTERPRET are three such words. If even one word has to be interpreted, the entire Forth dictionary would have to be embedded inside the program. Thus, Forth should always be treated as an interpreted language.

Forth is an appealing language because of its one and only guiding principle, Keep it simple! Part of this simplicity involves how the language is used. Leo Brodie – a third main contributor to the language – explains, in Starting Forth, E2 (1987): The interpreter reads a line of input from the user input device, which is then parsed for a word using spaces as a delimiter. When the interpreter finds a word, it looks it up in the dictionary. If the word is found, the interpreter executes the code associated with the word, and then returns to parse the rest of the input stream. If the word isn’t found, the word is assumed to be a number and an attempt is made to convert it into a number and push it on the stack; if successful, the interpreter continues parsing the input stream. Otherwise, if both the lookup and the number conversion fail, the interpreter prints the word followed by an error message indicating that the word is not recognised, flushes the input stream, and waits for new user input. (p. 14) While the use of unusual words may make the above description seem complex, this is a much simpler approach than that used in most other computer languages. A graphic version is shown in the flowchart above. Parse is a word used extensively by people who construct compilers. It refers to the process of dividing a sentence (or in computing, a statement) into words/ grammatical parts and identifying the parts and their relationships to each other.

One major problem with Forth is that its dictionary, more often referred to as a library in other languages, is not uniform. Some implementations come with an adequate dictionary, others less so. Some use words the same way, others give the same word different meanings. This means that Forth implementations can produce very different results, depending on dictionary content. This weakness is probably the main reason why Forth is not treated seriously, and has not been extensively used.

Forth is a stack machine, a computer where the primary interaction is moving short-lived temporary values to and from a storage location that follows the rule: last in, first out. A stack significantly reduces the complexity of a processor. Tasks are expressed as words. Simple tasks usually involve single words . More complex tasks connect together many smaller words, that each accomplish a distinct sub-task. Thus, a large Forth program is almost like a sentence that involves a hierarchy of words, distinct modules that communicate using a stack. Data is only added to the top of the stack, and removed from the top of the stack. Each word is built and tested independently. Provided that words are chosen appropriately, a Forth program resembles an English-language description of the program’s purpose.

Forthwright

Forthright is an adjective, used to describe a plainspoken/ frank/ blunt person. A person who develops/ modifies/ corrects/ improves/ uses Forth programs is referred to as a forthwright, a noun. Both words are pronounced the same way. A wright is a person who makes or repairs something. The original ca. 700 AD Old English wryhta, referred to someone working with wood. Since then, the term has expanded to include many different occupations. Carpentier, now carpenter, was introduced into England only after the Norman conquest in 1066, effectively replacing wright to describe this role. In Scotland, wright is used much more extensively.

Raspberry Pi Pico

The traditional strength of Forth is its minimalist use of resources. This is more important than it may seem. Gordon Earle Moore (1929 – ) formulated an expectation in 1965, later termed Moore’s law, by others, that computing capacity would double every year, some say every 18 months. This doubling cannot continue indefinitely. Many, including Moore, expect it to be invalid from about 2025, giving it a life span of 60 years. Even so, this means that even the most primitive of microprocessors made today has many magnitudes of capacity compared to anything made in, say, 1970. This is why many people prefer to use computer languages that are less optimal.

In contrast to Moore’s law, Niklaus Emil Wirth (1934 – ) formulated a very different expectation in his 1995 article A Plea for Lean Software, later termed Wirth’s law, by others, that software is getting slower more rapidly than hardware is getting faster. Most computer scientists are no longer making software that optimize/ minimize resource use, because they know that ample resources are available.

The reason some few people continue to use Forth is because of their acute awareness of Wirth’s law, where they see the negative impact of software bloat, on a regular basis.

General Public Licence (GPU) and public-domain Forths exist for most modern operating systems including Windows, Linux, MacOS, Android and some Virtual Machines. Such implementations include: gForth and bigForth. Dale Schumacher forked the Raspberry Pi/ARM port of JonesGorth around 2014, and removed its dependency on Linux. It now runs bare-metal, on a Raspberry Pi, booting directly into the Forth interpreter. Many important words have been re-implemented in assembly, or as part of the built-in definitions. Note: In computing, bare or bare metal refers to a computer executing instructions directly on a processor aka logic hardware, without an intervening operating system.

Iteration #2 of Unit One (#2U1), my personal workshop, will officially commence on 2023-11-01, less than two years away. It will transform a construction-support workshop into a fabrication shop, as my career as a wright/ building constructor/ carpenter comes to an end, and my career as a millwright/ machinist begins. My primary emphasis is broad, mechatronics, but the workshop’s role is limited to fabrication. Electronics and programming will probably be done inside Cliff Cottage, while much of the thinking will take place wandering about in the woods.

The purpose of the workshop is for an old man to have fun, to build upon skills learned in the past, and to learn new 21st century skills, to keep his brain and body active. Hopefully, some useful and environmentally sensitive products will be made at it.

There are plans to use Forth as the official shop language/ operating system for computer numerical control (CNC), the automated use of machine tools, controlled by a computer. I expect to have one primary machine that can move in three dimensions, and change heads as required. The two most important heads will be a router, which can shape materials as well as drill holes, and a laser cutter that cuts more accurately and with less waste than a saw. I expect to concentrate on various types of hardwoods as my primary material focus, but not to the exclusion of other materials. These are subtractive processes that remove material. In contrast, 3D-printers are additive.

There is no need to waste money on expensive silicon if cheap silicon will do. The silicon needed to control a CNC mill will be a Raspberry Pi Pico microcontroller. It costs NOK 55 = US$ 6.05 = CA$ 7.75, on the day before publication. Any money saved on silicon will be put into better bearings, and improved versions of other machine components.

Forth is not for everyone. It is useful where there is a need for a real-time system involving mechanical movements. After milling machines, and other types of tools, robots come to mind first, including unmanned underwater vehicles and drones. It should be mentioned in all fairness that Forth is not the only language I intend to use in the future. Two others are Prolog and Lua. Prolog is a logic programming language developed in France in 1971 with a number of artificial intelligence applications. Lua is a multi-paradigm scripting language, developed in Brazil in 1993. Its basic set of features that can be extended to suit different problems.

A dangling Raspberry Pi Pico microcontroller attached to a USB-C power and data connector. On the laptop pinouts for the Pico are displayed, along with its datasheet. Photo: Raspberry Pi.

2 Replies to “Forth”

  1. Brock, I just have to say you are brilliant!!!!! Your depth and breadth of knowledge, interests, understanding and curiosity is ASTOUNDING!!!!
    Thanks for doing your blog!!!!
    Wishing you and yours a very Merry Christmas!!

    1. Thank you, Darlene.
      Some people in the household would probably want me to spend less time researching and writing weblog posts and more time doing some real work! Writing for me is relaxing. It is therapeutic.
      For me, its main benefit of writing, is that it bring clarity to a topic I want to understand better.
      Wishing you and yours a delightful Christmas as well. We will not be attending any Julebord/ Christmas parties this year.
      Brock

Leave a Reply

Your email address will not be published. Required fields are marked *