Forth is a general-purpose computer programming language. It was invented in 1968 by Charles H Moore (known as 'Chuck'). Initially he used it for the control of giant telescopes in the observatory in which he worked, but it could be used for anything, so it gradually spilled out into other applications. Moore went on to found a company, Forth Inc., to develop Forth systems for lots of different computers.
Forth arose at a time when there were two types of computer language:
Interpreted languages - slow-running and unstructured, but could be used to cobble together a simple program very quickly.
Compiled languages - programs ran very quickly but took a long time to write, as each time a change was made, the program had to be recompiled, which could take five or ten minutes.
Examples of compiled languages were FORTRAN, ALGOL and COBOL, all of which Moore was familiar with. These languages tended to have so many features that you needed a huge computer to run them. The most common interpreted language was BASIC. It could run on just about anything, but was pretty crude - it lacked all the structure of other languages - and programs written in BASIC ran slowly.
Forth was designed to be the best of both worlds. It was interactive so could be used to develop programs quickly, but they tended to be quickly compiled on the spot, so that the final program would run very quickly. Forth also produced very small programs that would run on computers with very little memory.
Forth was 'flavour of the month' in the early 1980s, until people actually tried writing programs in it. Some persevered and became Forth programmers. Most just gave up. There was even a tiny computer, the Jupiter Ace, designed to run completely in Forth. This hit the shelves in 1983 and disappeared soon afterwards. Designed by two ex-Sinclair engineers, it shared many features with Sinclair's ZX81 and ZX Spectrum computers.
Moore called his new creation 'Fourth Generation Language' because he considered it the successor to the 'third generation' compiler-based languages of the day. He then abbreviated the name to 'FORTH' because the machine he was running it on could only take five letters in the name. Capital letters were the norm for computer languages in those days; in later years, it became acceptable to write the name in lower-case. The term 'Fourth Generation Language' (4GL) went on to mean something different in the computer world (a language for interrogation of relational databases), and Forth is not a 4GL in the normal sense of the term.
Advantages and Disadvantages of Forth
There were a few things that made Forth really good:
It was interactive. You could define a sequence of events, then try it out on the spot, so you could test each tiny component as you wrote it.
It was compiled in a sort of a way, so Forth programs ran very quickly compared with normal interpreted languages. Forth wasn't quite compiled into machine code. Instead, it was compiled into a list of addresses at which the machine code routines could be found. This resulted in code which is not as fast as machine code but far faster than interpreted code.
It was easily expandable. Each new function that you defined became a command in the language and could be used anywhere.
On the other hand, Forth had some peculiar features which made it instantly disliked by many people:
Because Forth uses direct manipulation of a stack for all arithmetic, most operations are performed in the reverse order to what you might expect. Instead of saying 'fetch contents of variable x', you say 'variable-x fetch-contents'. To add 1 and 2, you say '1 2 +'.
To save typing, most commonly-used commands are given very short names. For example 'fetch-contents' is actually just @. This gives Forth programs a very cryptic look. If Z is a variable, you add 1 to Z with the following sequence:
Z @ 1 + Z !
It's simple when you know what all those symbols mean, but it is not easily approachable.
Many Forth implementations use 16-bit whole numbers, so numbers bigger than 32,767 and numbers with a fractional part need special treatment.
A Flavour of Forth
It is possible to get a feel for Forth in a few minutes.
The Stack and 'Reverse Polish' Notation
For calculations, Forth uses a system known as 'Postfix' or 'Reverse Polish'. Invented by computer scientist Charles Hamblin based on work by Polish mathematician Jan Lukasiewicz, the 'Reverse Polish' system puts the numbers in first and then carries out the operation. To add 1 and 2, you put in 1, then put in 2, then tell the computer to add them up. We type the following into Forth:
1 2 +
Note that there are spaces between all the elements. This is important; Forth uses spaces to break up what you type into manageable words, which it can then think about one at a time. The numbers are stacked up as you enter them. When Forth encounters the 1, it puts it onto the stack of numbers, which will look like this:
Then when Forth comes across the 2, it adds it to the top of the stack, so that the stack now looks like this:
Finally, the '+' operation takes two numbers off the stack, adds them and puts the result back on the stack. After the '+', the stack will look like this:
If we want to see the result, we will have to display it, which is done using the '.' command. This takes the top number off the stack and displays it on the screen.
So the full interaction between you and the computer would be:
1 2 + . <enter> 3
The <enter> here indicates that you press the Enter button on your keyboard. The computer's response will appear immediately, on the same line.
The stack can hold many more than just two numbers, so more elaborate calculations can be done. To calculate ((12+13)*9 + (7*8))/3 we would type:
12 13 + 9 * 7 8 * + 3 /
If you work your way through this, you'll find that it does in fact calculate exactly the same thing. If you try it out on a Forth system, you'll find you get 93 rather than the expected 93.666667. Forth systems use whole numbers only, so 281 divided by 3 gives 93. There are ways of dealing with numbers with a decimal part, such as 93.666667 but they are more complicated and won't be discussed here.
Since all calculations depend heavily on the stack, there are a whole load of Forth commands for moving numbers around on the stack.
DUP will copy the number on top of the stack and leave that copy on the stack.
DROP will take the number off the top of the stack and throw it away.
SWAP will swap the two numbers on the top of the stack.
There are loads of other commands, including OVER, NIP, TUCK, ROT, ROLL and PICK.
Forth commands are known as 'words'. There are many built-in words, but we can define our own ones as well. Suppose we want to take two numbers, square them and add them together. Let's start by working out how to square a number.
Squaring a number means multiplying it by itself. Assume the number is on the stack at the start. We'll need to make a copy of it using DUP and then multiply the top two numbers on the stack using '*'. We can define a new word to do this:
: Square DUP * ;
The colon at the start marks the start of a word definition. 'Square' is the name we have chosen to give the word. The semicolon marks the end of the definition. The rest is just the sequence of actions that should be carried out by the word. We can then use the word as if it was a standard Forth word:
4 Square .
This will display the result 16.
Now we can define Squaresum, which takes the top two numbers on the stack, squares them and adds them together:
: Squaresum Square SWAP Square + ;
Once again, the colon and semicolon mark the beginning and end of the definition. Squaresum is the name of the word being defined and everything else is the list of actions to be performed. Note that the new word, Squaresum, uses the word we had just defined, Square, along with built-in Forth words. We can use Squaresum immediately:
3 4 Squaresum .
This should display the value 25.
The Forth 'word' is the equivalent of a procedure, function or subroutine in other programming languages. Most languages would discourage writing subroutines as small as this because of the overhead of calling them, but Forth execution is so efficient that you don't need to worry.
Conditions, Iterations and Recursion
Forth has all the usual IF/THEN/ELSE and BEGIN/WHILE/REPEAT structures that you get in any programming language, but they have an odd 'flavour'. For example, the syntax of IF is:
Note that the condition comes before the IF, rather than after it as in most other languages. Also, THEN marks the end of the if statement, rather than the action to be carried out if the condition is true. An example of an if statement is as follows:
0 = IF
." Top of stack is zero"
." Top of stack is not zero"
Here, '0 =' compares the top of the stack with zero. If the result of this is true, the message 'Top of stack is zero' is displayed using the word ." otherwise the message 'Top of stack is not zero' is displayed.
There are a number of different words that can be used for iteration:
BEGIN ... AGAIN
DO ... LOOP
BEGIN ... WHILE ... REPEAT
One word you won't find, though, is GOTO. Forth is completely structured and supports 'goto-less' programming.
Most programming languages have a lot to say about variables. By using the stack, Forth can do a lot without ever needing a variable, but they certainly exist and are used. A variable is a piece of memory with a name. A variable normally holds a number, although it can also hold other things.
You can define a variable in Forth using the word VARIABLE. This takes the next thing after the word VARIABLE and treats it as the name of the variable. You can fetch the value of a variable onto the stack using '@' and can store the value which is on the stack into the variable using '!'.
|VARIABLE X||Defines a variable called X.|
|3 X !||Stores the value 3 in the variable X.|
|X @||Fetches the value in the variable X and puts it on the stack.|
A variable is a word, the same as any other Forth command. When used on its own without '@' or '!', it puts the address of the variable, a number representing where in memory the variable is stored, onto the stack. This is useful, as it gives the programmer greater control over the variable.
More Complex Data Structures
Forth provides words which allow more complex data structures to be created. The most important of these are 'CREATE', which defines what actions are to be taken when the data structure is created, and 'DOES>', which defines what actions are to be taken when the data structure is used. By careful choice of these, all sorts of weird and wonderful structures can be created, as well as the traditional arrays, lists and records. For this reason, Forth does not provide any of the normal structures such as arrays, since they can be set up and customised so easily.
Unfortunately, you need your head screwed on well to get it around the concepts involved in using CREATE and DOES>. They are very different from the sort of thing done in traditional programming languages.
The ANSI Standard: Forth Made Boring
In the late 1970s, there were many different Forth systems, all incompatible with each other. In a bid to make Forth more uniform, standards were developed: Forth-79, Forth-83 and finally in 1994, the ANSI standard - ANS Forth. It is expected that all new Forth systems developed since then should be compatible with the ANSI standard.
Unfortunately, the standard includes many features which are rarely used. To comply with the standard, a Forth system must be much bigger than really necessary. These extra features also slow down the operation of the system considerably, so Forth starts to lose its edge over other programming languages.
Finally, it is possible to write Forth programs that are fully compatible with the standard but are little more than translations of C programs into Forth. Such programs will run, but they will probably run slowly and will take up a lot of space. Without the use of the special Forth features which are so tricky to understand for traditional programmers, the programs won't be any better than if they had been written in C.
Nowadays, processors are fast and memory is cheap - we don't worry too much about the resources a program consumes. Compilation is virtually instant in most programming languages, so the factors that led to the development of Forth are no longer particularly important.
Nevertheless, Forth is still being promoted by a group of interested people. There are free versions available for PCs and PDAs. Most of these are primitive and lack the tools to do much other than write a few messages on the screen.
Fully-functioned Forth systems are available if you want to pay, giving as much control over your computer as you would get from a full C system. Such systems tend to be produced by small companies and are not expensive: $50 - $100 (2004 US dollars) for a fully-fledged development system is peanuts compared with a development language from one of the big software suppliers. Microsoft has not produced a 'Visual Forth' - the Forth enthusiasts say this is because it would undermine the company's other products by running rings around them.
No Gentle Introduction
Forth's biggest problem is that there is no gentle introduction to it. None of the people writing about it are educators - they don't appear to be able to see the difficulty the average programmer will have in getting his or her head around the concepts of Forth. After playing around with the stack for a bit, most introductions to Forth launch into the deep end, with the internal structure of compilers, number formats and switching between compilation and execution on the fly. Admittedly, such concepts are much simpler in Forth than in other programming languages, but they still are daunting enough to put most people off. There seems to be no middle ground. Either you learn how to write simple programs and go no further, or you must understand every single little detail of how Forth works.
It seems that there is still a future for Forth. It is ideal for writing applications for computers with limited resources in a rapidly-changing market. While personal computers no longer have such limitations, there are a whole load of new devices coming on-line such as phones, handhelds and even things as simple as televisions and washing machines, which can all benefit from Forth.
What is needed is something to provide the middle ground, to bridge the gap between the basics and the full-blown complexity that is necessary to get the benefits from a Forth system. Perhaps such a breakthrough is just around the corner!