Forth is a stack-based, extensible programming language without type-checking. It is probably best known for its "reverse Polish" (postfix) arithmetic notation, familiar to users of Hewlett-Packard calculators: to add two numbers in Forth, you would type 3 5 + instead of 3+5. The fundamental program unit in Forth is the "word": a named data item, subroutine, or operator. Programming in Forth consists of defining new words in terms of existing ones. The Forth statement
: SQUARED DUP * ;
SQUARED
whose function is to square a number
(multiply it by itself). Since the entire language structure is
embodied in words, the application programmer can "extend" Forth to
add new operators, program constructs, or data types at will. The
Forth "core" includes operators for integers, addresses, characters,
and Boolean values; string and floating-point operators may be
optionally added.
Forth offers the following features:
Other languages provide some of these features, but none provides all of them.
Joe Knapka has written a Welcome to Forth, which discusses some of the differences between Forth and other languages in more detail.
The name FORTH was intended to suggest software for the fourth (next) generation computers, which Moore saw as being characterized by distributed small computers. The operating system he used at the time restricted file names to five characters, so the "U" was discarded. FORTH was spelled in upper case until the late 70's because of the prevalence of of upper-case-only I/O devices. The name "Forth" was generally adopted when lower case became widely available, because the word was not an acronym.Rather, Colbourn, and Moore: The Evolution of Forth, in: History of Programming Languages (HOPL-II), ACM Press/Addison-Wesley 1996.
Note: Forth is not a 4GL (language for programming database applications).
Although invented in 1970, Forth became widely known with the advent of personal computers, where its high performance and economy of memory were attractive. These advantages still make Forth popular in embedded microcontroller systems, in locations ranging from the Space Shuttle to the bar-code reader used by your Federal Express driver. Forth's interactive nature streamlines the test and development of new hardware. Incremental development, a fast program-debug cycle, full interactive access to any level of the program, and the ability to work at a high "level of abstraction," all contribute to Forth's reputation for very high programmer productivity. These, plus the flexibility and malleability of the language, are the reasons most cited for choosing Forth for embedded systems.
One of the best answers came from Brad Rodriguez. You can find the full version at http://www.complang.tuwien.ac.at/forth/faq/why-forth. In short, Forth's advantages are that it's comprehensible, small, interactive, fast, extensible, and makes it easy to work at a high level of abstraction.
BTW, this question came from someone comparing a 10+ year old Forth system with the latest version of Borland C++. His system was really out of date, but also with respect to current Forth systems.
Not in itself. I.e., if you translate a C program literally into Forth, you will see a slow-down (e.g., a factor 4-8 with Gforth 0.5, a threaded-code system; for typical native-code systems you will see a factor of 1-3). Similarly, there is no inherent size advantage in Forth. For details see http://www.complang.tuwien.ac.at/forth/performance.html.
However, there are many reports of cases where Forth programs beat others in size and/or speed. My guess is that the added flexibility of Forth helps programmers produce faster and/or smaller programs.
An American National Standard for Forth, ANSI X3.215-1994, is accepted worldwide as the definitive Forth standard ("ANS Forth"). This standard also has been blessed as international standard (ISO/IEC 15145:1997).
IEEE Standard 1275-1994, the "Open Firmware" standard, is a Forth derivative which has been adopted by Sun Microsystems, HP, Apple, IBM, and others as the official language for writing bootstrap and driver firmware. See http://playground.sun.com/1275/home.html.
Prior Forth standards include the Forth-83 Standard and the Forth-79 Standard issued by the Forth Standards Team. The earlier FIG-Forth, while never formally offered as such, was a de facto "standard" for some years.
"FORTH STANDARDS Published standards since 1978 are Forth 79 and Forth 83 from the Forth Standard Team, and ANS Forth - document X3.215-1994 - by the X3J14 Technical Committee. The most recent standard, ANS Forth, defines a set of core words and some optional extensions and takes care to allow great freedom in how these words are implemented. The range of hardware which can support an ANS Forth Standard System is far wider than any previous Forth standard and probably wider than any programming language standard ever. See web page ftp://ftp.uu.net/vendor/minerva/uathena.htm for latest details. Copies of the standard cost $193, but the final draft of ANS Forth is free and available (subject to copyright restrictions) via ftp..." --Chris Jakeman, apvpeter.demon.co.uk
The (un)official ANS Forth document is available in various formats at http://www.taygeta.com/forthlit.html and at ftp://ftp.uu.net/vendor/minerva/x3j14/. The format I like best is the HTML version.
To get yourself on the ANS-Forth mailing list, consult the various README files at ftp://ftp.uu.net/vendor/minerva/x3j14/.
Two unofficial test suites are available for checking conformance to the ANS Standard Forth:
There is also an ANS Forth FAQ that explains the standardization process.
A Request For Interpretation. If you find something in the standard document ambiguous or unclear, you can make an RFI, and the TC (technical committee), that produced the standard, will work out a clarification. You can make an RFI by mailing it to greg@minerva.com and labeling it as RFI. The answers to earlier RFIs are available at ftp://ftp.uu.net/vendor/minerva/x3j14/queries/. They are also integrated in the HTML version of the standard.
Leo Brodie's book Thinking Forth gives some advice; a short excerpt is now available online. Forth shops have rules for their coding. Paul Bennet has published those of his company; you can find them on http://www.forth.org/forth_coding.html.
No. There are better alternatives:
The basic skill required for comfortable, efficient Forth programming is good stack management. It's hard for newcomers to learn, since it isn't a skill required in other languages, which all require the use of variables for practically everything. Having taught literally hundreds of courses over the last 25 years, I've seen newcomers wrestle with this, and have developed exercises (similar to those in Starting Forth) to help. It seems to be a skill rather like riding a bicycle: wobbly & scary at first, then suddenly a "switch is thrown" in the brain and it seems comfortable and natural ever after.Andrew Haley writes in Message-ID: <7k8lln$q3c$1@korai.cygnus.co.uk>:
Try writing all of your code using definitions one, or at most two lines long. Produce a stack digram for each word showing its inputs and its outputs. If you ever need an "intermediate" stack diagram to see what's going on, split your word at that point into two words. By doing this, you may test each half of the word on the command line, checking the stack each time. Do not use PICK and ROLL.Once you get the hang of writing code in this way you can relax these rules, but it's much better to get used to this style first.
The Forth Interest Group "FIG" was formed in 1978 to disseminate information and popularize the Forth language, and it remains the premier organization for professional Forth programmers. FIG maintains a Web page at http://www.forth.org/forth.html, with a more complete introduction to the Forth language, and links to the Web pages of many Forth vendors.
Chuck Moore discovered (as he puts it) Forth (for historical information read The Evolution of Forth). He later went on to apply his design philosophy to hardware design and designed a number of processors well-suited for executing Forth: Novix 4016, Shboom, uP20, uP21, F21, i21, ...
He also explored new ideas and refined his earlier ideas on software and Forth: his cmForth for the Novix has been quite influential. His latest developments are Machine Forth and Color Forth.
Machine Forth is a simple virtual machine consisting of 27 instructions. It is implemented in hardare in uP21 and the following chips, but has also been implemented in software on the 386 as simple native-code system. Some of the differences from ANS Forth are that each stack entry contains an extra carry bit, and that there is register A for accessing memory (instead of addressing through the top of stack).
Color Forth's most obvious feature is that it uses colour in syntactically significant ways. You can now download it and run it out on modern PCs.
You can find out more about Chuck Moore at his site and at Jeff Fox' site.
That behaviour is known as early binding (of names), and it is a feature: E.g., name collisions are mostly harmless in Forth, unlike most other languages.
So how do I change a word during debugging? The typical approach is to completely reload the program; the memory for the old version is usually reclaimed either by exiting and restarting the system, or by using MARKERs. Since Forth systems start up and compile very fast (in the 0.1s ballpark), that does not break the workflow.
And how do I implement mutual recursion? How do I implement words that change behaviour? You use deferred words.