[comp.lang.forth] Forth differences

ir230@sdcc6.ucsd.edu (john wavrik) (12/30/89)

Mitch Bradley writes:

>Furthermore, Forth programmers are anarchists at heart (otherwise they
>wouldn't buck the establishment and continue to use Forth despite all
>the pressure to use C).

While I'm sure that I have met some genuine anarchists among Forth 
programmers, the main motivation for using Forth is because it does some 
things better than 'C'. 

>If Forth invents its own look and feel, Forth will become even further
>isolated from the mainstream, where the people and the bucks are.

I assume this is the same Mitch Bradley that posts to comp.lang.lisp trying to 
pursuade LISP programmers that they'd make bigger bucks if they just would
eliminate the excessive use of parentheses 8-).

Behind this and several other recent postings is an assumption that languages 
in a certain group (FORTRAN, ALGOL, Pascal, C, Modula2, ADA, etc.) are on the 
right track and that all other languages should be made to resemble them. 

The first question is whether indeed these languages are on the right track. 
In this regard I defer to John Backus (the designer of FORTRAN) who, in 
receiving the 1977 Turing Award, characterized them as "fat and flabby". He 
feels that "conventional languages are growing ever more enormous, but not 
stronger" and proposes the creation of languages which have "a small framework 
which could accommodate a great variety of powerful features entirely as 
changeable parts". 

Backus casts doubt on the assumption that the road to computer power lies in 
fat languages with as many built-in features as memory will allow -- and he 
indicates a more promising approach (an approach, in fact, which Forth has 
taken). While inertia will continue to propel the development of languages in 
the present direction for several years, the paucity of the approach and the 
development of viable alternatives will eventually cause movement in another 
direction. [I hear Forth programmers humming "We Shall Overcome" in the 
background.] 

If I were to advise someone who is raising children, concerned about buying a 
house, making money, etc. I would suggest that he program in 'C'. At the 
moment it is a popular language and will continue to be so for the next 
several years. It does a great many of the things that we want a computer 
language to do and enjoys a great deal of support. A programmer trained in 'C' 
may well be able to earn a living for his lifetime without learning another 
language. The fact that our children will probably program in a language more 
like Forth than like 'C' does not put bread on the table today. The 
development of alternative languages should be left to those in a better 
position to take risks or who have a need for an alternative now (and are 
forced to take risks).

Forth deserves to be taken seriously as a programming language not because of 
its similarities to conventional languages, but because of its differences. 
The role Forth is destined to play in history will depend upon the ratio 
of statesmen to politicians in the Forth community. But in any case it will 
depend upon exploiting the unique characteristics of Forth. The world already 
has 'C'. It doesn't need a Reverse Polish version of 'C'. 

Perhaps an interesting and productive thread for us to start is an examination 
of the characteristics of Forth which distinguish it from conventional 
languages. 

                                   ---------

Before I give my contribution to the thread, I'd like to discuss a logical
fallacy. It goes like this:

     B was created because at the time of its creation
        the conditions were A
     The conditions no longer are A
     Therefore (we incorrectly conclude) B is obsolete

The relevance is that Charles Moore developed his language 20 years ago and 
incorporated certain features to overcome some of the limitations of the 
equipment available at the time. The fact that some of these limitations no 
longer exist does not invalidate the importance of the features he created. 

Charles Moore anticipated John Backus by almost a decade in devising 
"a small framework which could accommodate a great variety of powerful 
features entirely as changeable parts". Forth is the name for a combination 
language, language implementation, operating system, and mapping to a host 
processor.

1.
One of Forth's main features is shared by only a few other languages (e.g. APL 
and LISP): its ability to provide an interactive programming environment. The 
goal of programming in these languages is to provide an "environment": a 
collection of commands which can be executed at the keyboard and used in 
further programming. In contrast, programming in most conventional languages 
is devoted to producing a single top-level command ("run", "main", etc.) which 
is automatically executed. A language which provides an interactive 
environment and is extensible belongs to almost an entirely different world 
than the world of conventional languages. I would like to take liberties with 
a comment by Alan J Perlis in this regard (I have substituted C and Forth for 
his Pascal and LISP): 

    It would be difficult to find two languages that are the communica-
    ting coin of two more different cultures than those gathered around 
    these two languages. C is for building pyramids -- imposing, 
    breathtaking, static structures built by armies pushing heavy blocks 
    into place. Forth is for building organisms -- imposing, 
    breathtaking, dynamic structures built by squads fitting fluctuating 
    myriads of simpler organisms into place. The organizing principles 
    used are the same in both cases, except for one extraordinarily 
    important difference: The discretionary exportable functionality 
    entrusted to the individual Forth programmer is more than an order 
    of magnitude greater than that to be found within C enterprises. 
    Forth programs inflate libraries with words whose utility transcends 
    the application that produced them. 

                  modified from Foreword to "Structure and Interpretation
                  of Computer Programs"  by Abelson, Sussman, and Sussman

I must leave the reader to judge if the substitution of Forth for LISP and
C for Pascal is warranted. I believe it is.

2.
The close mapping of the Forth virtual machine to the underlying real 
processor has two main consequences -- one, the exceptional portability of 
the Forth, is also tied to other factors and will be discussed later. The 
other consequence is that it is relatively easy to use Forth for hardware 
control applications. This is the historical use of Forth. The interactive 
environment speeds the development of applications, and the high level 
character of Forth simplifies the transport and modification of applications. 
From this point of view, Forth is "assembly language done right": (1) machine 
dependencies are isolated in a few words; (2) most programming takes place in 
a machine-independent high level language; (3) an interactive environment 
simplifies debugging.

3.
In Forth it is probably more significant that IF and THEN can be defined by:

    :  IF    COMPILE ?BRANCH  HERE 0 ,  ;  IMMEDIATE
    :  THEN    HERE SWAP !  ;  IMMEDIATE
 
than it is that Forth uses a stack and Reverse Polish syntax. 

In most languages the control structures as well as the data structuring words 
are built in to the compiler/interpreter -- thus are not within the 
programmer's control. The structure of a conventional language and most of the 
language's features are therefore, from the programmer's point of view, fixed. 
By employing a simple interpreter/compiler and distributing the process of 
compilation, Forth puts the very structure of a language in the hands of the 
programmer. This is a radical shift which virtually no other language can 
offer. It makes possible a unique approach to problem solving: the programmer 
first creates a special language (with appropriate control and data 
structures) for the problem area, then uses it to solve the problems. 

The special control that Forth provides the user over the implementation of 
the language makes Forth as much a toolkit for building languages as a 
language itself. The role of Forth in software development should grow as the 
feasibility of this approach to problem solving becomes better known. 
Computers are now invading areas which have not historically been bound up 
with machine computation and which are not well served by conventional 
languages. It is quite probable that user-created special purpose languages 
will become particularly important.

The distribution of compilation allows major language features to be added to 
Forth as add-on options. This provides the user with the ability to customize 
his language and greatly simplifies the task of achieving portability. 

4.
It should be mentioned that both the simplicity and accessibility of Forth 
implementation are essential parts of the usefulness of Forth as a language-
building toolkit. A great deal of the power of Forth comes from the fact that 
a user can understand how the system works and can therefore use this 
knowledge. The power of knowledge should not be underestimated!

5.
In keeping with an underlying philosophy of simplicity, Forth uses a syntax 
that needs no parsing. The syntax follows the semantics which equips each 
word with an action which is carried out when the word is encountered. This 
suggests that the arguments for a word must be available when the word 
executes -- so the arguments must precede the word in the input stream (and be 
saved in a stack). 

The use of a stack and Reverse Polish Syntax are, therefore, corollaries of
the philosophy of simplicity. Reverse Polish Syntax is probably neither more 
nor less aesthetic or expressive than the syntax of conventional languages. 
When the language is viewed in terms of the unifying "semantics of action" the 
syntax becomes natural. 

6.
Forth should be the most highly portable of languages: (1) The "virtual 
machine" on which it is based can be easily realized on any real processor; 
(2) The core language can be made relatively small (about 100 words) and non-
controversial; (3) The simplicity and accessibility of the implementation (see 
above) allows powerful features to be added on (whereas conventional languages 
require them to be built-in).  

7.
Since Forth may be regarded as "a small framework which can accommodate a 
great variety of powerful features entirely as changeable parts". It should be 
amenable to the development of libraries (as in public libraries for books) of 
features which anyone can add to their systems if they wish.

Since Forth programming consists in defining words, there should also be an 
active commerce in "bonnes mots": while an entire application may not be of 
interest to another programmer, certain words within it may be useful.

[N.B. Something similar to this occurs in mathematics: when someone publishes 
a paper to prove a certain theorem, another mathematician may find some of the 
auxilliary results or techniques more useful than the main theorem.] 


                                                  John J Wavrik 
             jjwavrik@ucsd.edu                    Dept of Math  C-012 
                                                  Univ of Calif - San Diego 
                                                  La Jolla, CA  92093