Linux Goodies

In Pursuit Of The Perfect O/S

Home Page Go To Small Telescope Astronomy Writing and Sci Fi humorous science shirt,comical science shirt,funny science shirt,zany science shirt,humorous math shirt,comical math shirt,Pi shirt,Pi day shirt,zany math shirt,funny linux shirt,comical linux shirt,linux nerd shirt,humorous linux shirt,computer geek shirt

A review of the GNU Gforth programming language

Home

Linux General Info
Migrating from Windows
Programming Languages
Making Documents
Linux on an Old Laptop
Linux User Survey

Linux Astronomy
Kstars Planetarium
Stellarium Planetarium
Xephem Planetarium

Window Manager Reviews
Dwm
Fluxbox
FVWM
Idesk
Icewm
Ratpoison
Window Maker

Linux Hints and Tips
Linux App Hints
Linux Distro Hints
Forth Programming Hints

Linux Lite Apps
MtPaint

Linux Language Reviews
Gforth Review
Linux Matrix Languages
Euler Math Toolbox Review
Octave Language Review
PDL Language Review
R Language Review
Scilab Language Review
Tela Language Review
Yorick Language Review

Linux Virtual Machines
Virtual Machine Intro
Qemu Review
Vbox Review
VMware Review
VM File Sharing
Freedos in DOSEMU
Freedos in QEMU

Linux Distro Reviews
Antix Review
Puppy Linux Review
  Puppy On Flash
  Frugal Puppy

Favorite Sites
Science T-shirts
Math T-shirts
Computer T-shirts
Climate Change T-shirts
Science Posters
Digital Art Posters
Witty T-shirts
Bowling T-shirts
Free Craft Howto's
Building a Dobsonian Telescope


Gforth ANSI Forth, Free and Fast

Have you tried Gforth yet? If you have not, then you are in for a treat. I've been using the Forth programming language for years, and Gforth delivers much more than most versions of Forth I have tried.

For one, Gforth has a full complement of floating point operators. The original versions of Forth did not have floating point, though they had a very clever set of integer operators designed by Forth's creator Charles Moore. With these operators, one could do a lot of fixed point math, with the ability to keep track of decimal place.

For one of the best applications of of forth, instrument control and data collection, I found the fixed point scaling utilities to be quite sufficient. I could interrogate A/D's, scale, and display data in meaningful units and precision easily with the standard fixed point operators. But I also did a lot of time series analysis work, with coordinate transformation equations that used trig functions. In such cases, I simply couldn't work with forth, though I liked the language in general.

So I found the clever fixed point operators insufficient for working on extensive math problems without so much head-scratching that I was loosing too much time, and saving time was one reason I went to Forth in the first place. I went to the exhausting effort of creating my own version of Forth, written in c, in order to have such floating point support. But now, Gforth makes all the math you'll likely need available.

Gforth is also designed to work well within a modern computer system, having plenty of file i/o routines to work with conventional files, as well as still retaining the old block file handlers and virtual memory system that gave Forth such power in the days of smaller resource computers.

Gforth also allows one to create Forth programs that can handle command line arguments much like more conventional languages, as well as handle Forth commands via the command line.

Gforth, as is common with other Forth derivations, includes a built in assembler, letting the user get down and dirty should hardware control or speed be critical elements of a program. The included assembler depends upon the platform the Gforth is running on. Gforth has already been ported to many architectures, and it's pretty easy to port. This is because most of Gforth is itself written in Gforth.

While Gforth has the primitive Forth vocabulary and a built in assembler, don't assume that it's a primitive language. Gforth also includes support for data structures, and even objects. In fact, the Forth language has often been described as a meta language, with which one creates their own languages targeted to their specific solutions.

I'm using the Debian Linux version of Gforth. To install it, I needed to only:

apt-get install Gforth

This installed the language and its extensions, the Gforth man page, and the Gforth info file. To find details about the Gforth language, I can always type info Gforth to get access to a very informative Gforth manual. You can view an online version of the manual at Gforth Manual.




Forth - A Profound Experience

I first got involved with Forth some time ago, way back in the 80's. I had a VIC 20, and was enjoying the marvel that it presented at the time. It had a pretty nice BASIC interpreter in it, and some clever graphics capabilities, though on a very low resolution screen. I happened to run across a plug-in cartridge for the VIC 20 that had a Forth interpreter/compiler on it. I bought it and started my adventure. That was pretty impressive, as I found that with Forth I could do things on a VIC 20 that I could never imagine doing in BASIC.

Then I started reading more about Forth. I recall one article that described a person's experience while at a trade show. He wasn't a Forth programmer himself, but had heard some pretty fantastic things about it. He was skeptical. He ran across a fellow demonstrating Forth on a PC, and quizzed the fellow about how to solve a graphics problem that had been perplexing him. The programmer spent a few minutes puttering around, and soon produced a graphic much like what the observer had been struggling to make. The observer wrote that he was flabbergasted. It was almost like a religious miracle to see how easily the programmer could solve the problem with Forth.

I found a similar enlightenment in one of my own applications. I struggled to get a minimal resource operating system, a supporting editor, and a compiler onto a floppy. The system had no hard disk, so was virtually worthless. I wanted it to be of use with just the available floppy. But my system floppy, once made, was now 3/4 full just housing the absolute minimal system, and was pitiful to work on. We're talking slow. It took a few minutes to go through an edit, compile, test cycle.

So I located a compatible Forth system and built a Forth system floppy, I now had a computer that booted quickly, had a built in editor and compiler, and used less that 1/4 of the disk. There was room for code, and even data. And in use it was as fast and convenient as any larger hard disk system I'd ever used. I was pretty astounded.

So I confirmed for myself that when resources were tight, Forth was an amplifier of capability that far exceeded my expectations. I was hooked.

Forth - It's All About Words

Likely, you're familiar with a number of more conventional computer languages, such as c, c++, BASIC, Fortran, and maybe Java. With these languages, or whatever else you've used, you may have just used any text editor to create your program (a common circumstance in Unix and Linux), or used a computer language GUI environment. With these languages, you work within the structure of the respective language and the limits of its compiler to create a code solution to your problem.

With conventional computer languages, the programming interface is cognizant only of the specific computer language and its syntax. The interface is never cognizant of the problem you are trying to solve. You code within the interface, compile the code, and then either execute the code from within the code development interface or temporarily leave the development system to test your code.

While the code you wrote is executing, the only thing with knowledge of the problem under consideration is the code you created. If you discover that some very useful piece of information should be printed out, you will usually have to edit the code to add the new information collection and print routines, re-compile, and re-run.

Within conventional languages, the program structure is usually that of functions. Even intrinsic commands generally operate as functions, where the function name is an action word, followed by arguments.

If your background is with more conventional languages , you may well enter into the Gforth environment with the same expectation. But there are some philosophic differences between Forth and most other languages that you should understand before you get started. If not, you will likely be frustrated, and become another voice added to the clamor that Forth is a worthless, primitive, assembly-like language -- good only if limited computer resources prevent you from using something more sophisticated.

As to programming environment, when you type in Gforth, you are presented with a rather bleak looking, empty screen. Gforth has no built-in editor. You must use an external editor, such as vi, vim, or emacs. If you happen to like emacs, then you are in luck. Emacs has support for Gforth, making a nice environment for the development of Gforth code.

When Gforth starts up, it connects you directly to the Gforth interpreter/compiler utility. Within the Gforth system is a dictionary or words that Gforth understands. You can type these words and Gforth will respond by performing the action associated with the word. Try typing in words and see all the words in the Gforth dictionary. Impressive, yes?

When I say words, I'm implying more than you might suspect. It's better in many ways to imagine Forth as a human-like language mapped onto a computer, than to view it as a conventional action-word/parameters model used by most other computer languages. With Forth, think more of verbs rather than action words, and nouns, with adjectives and adverbs used to modify nouns and verbs.

In terms of human-speak, Forth is primarily different (than English at least) in being a post-fix language, rather than pre-fix or in-fix. For example, imagine that you want to turn and control the speed of some device. The following commands are typical as to how one might express that desire in human speak, Forth, and C.

Human Speak
Turn left, set speed to fast

Forth Speak
left turn fast set-speed

C Speak
turn(left)
set_speed(fast)

As you can see in the example above, Forth uses language more similar to human-speak than does a more conventional language, with the proviso that each phrase is post-fix. Programming in Forth is accomplished by creating more targeted words from the existing vocabulary of Forth primitives, then creating even more expressive words from the extended vocabulary. Finally, one expresses the solution to one's problem using the words he/she has taught the Forth system. Then, one saves the new vocabulary and solution expression to be used whenever desired.

As you create words in Forth, you can test the words interactively by simply typing them and hitting the enter key. Forth will dutifully find the words in the dictionary and perform them as instructed. Defining a word is simple. Just enter a colon, the new word, the existing words that define the meaning of the new word, and a semi-colon. Some simplistic examples are given below (Note, expressions between parenthesis in Forth are comments):

( Note that comments begin with left-paren space)
( Add two to number on stack)
: bump-by-two 2 + ;

( Clip value to be between -10 and 10)
: clip 10 min -10 max ;

( Bump number by 2 then clip)
: bump-and-clip bump-by-two clip ;

In the simplistic example above, note that the first two words are defined by words Forth already knows. The third definition is composed of the newly defined words. These new user-defined words are now just as much a part of the Forth system as the intrinsic words the programmer started with. Conceptually, that suggests that unlike the conventional programming technique described above, the Forth programming procedure increases the vocabulary and knowledge base of the language itself.

One doesn't use a Forth interface to construct a program that holds a problem solution. Rather one increases the vocabulary of the Forth system itself to have the solution to a problem.

Zazzle Keen Designs T-Shirts and Mugs


Where to get more information

A great tutorial for programming is Leo Brodie's book Starting Forth, shown in html form here. While the book is specific to Forth 79 rather than ANSI Gforth, it presents a philosophy of Forth that is very instructive, and many of the examples in it will work with Gforth. See the Gforth info file for specifics and extensions that are contained in Gforth.

I learned Forth from Brodie's book, and have a couple of copies in my library. One I bought new when it was in print, and the other from a used book store. You can find his wonderful books on Forth at ABEbooks.

To write an executable program in Gforth, create the vocabulary definitions needed to perform the desired function and save them into a text file. Note that you must always define each new word with definitions that already exist, either in the initial dictionary or referencing words you've previously created.

Next, put the following line as the first line of your program:

#! /usr/bin/gforth path-to-program-file

Notice the space between the #! and the reference to Gforth. With the conventional batch file construct, such as with a perl script, the space isn't needed. But with Gforth, the #! is a Forth word being the equivalent of the Forth comment indicator, telling Forth to ignore the first line. The Linux system, then, handles the line as with all batch file references, calling the referenced process with the following arguments.

In the case of Gforth, usually the first argument passed to Gforth is the name of the text file containing a vocabulary extension that also includes the expression of the problem solution. Just set the file to be executable, and you have a Gforth program. The following is an example of a Hello world program written as a Gforth batch script:

!# /usr/bin/gforth
." Hello World" cr
bye

In the example, the first line, similar to batch commands in Linux except for the space after the !# symbols, tells the system to pass the file to gforth. The second line uses Gforth's print command to print Hello World and a carriage return, and the last line tells the Gforth interpreter to exit. In this example, no additional arguments, which would also occur on the first line, were needed.



How is Gforth different from other Forths?

Having worked with Fig-Forth, Forth 79, and Forth 83, I did find some significant differences between these and Gforth. Mostly, what I found is that Gforth had more things already included, like data structure support and object oriented programming support.

The dictionary design of Gforth is a bit more complex, made so in order to allow it to execute faster. In fact, Gforth runs very fast compared to interpretive languages. You may find, however, that some tricks you were able to perform in other Forths because of a simpler dictionary design may have to be re-designed to work in Gforth.

As I already mentioned, Gforth includes floating point math support, as well as more conventional file support. Within the floating point support is a full complement of transcendental math functions.

Gforth is a full 32 bit version of Forth, and thus allows one to create a very large dictionary, or create large array areas for data handling. One can pass in a memory size argument to has as much memory as your computer will allow as dictionary space.

The only down side I've found is that because of its more complex design and larger intrinsic vocabulary, Gforth is larger that the older, 16 bit versions of Forth I've used previously. Then again, computers generally have much more resource than in the days I was running Forth on 16 bit computers.




Summary

Gforth, like other versions of Forth, is highly extensible. I've created a vocabulary of words that I find useful, including words that load additional libraries of words for specific problems. Other words match up Gforth to other Forths I've used, making it easier to port my many programs over to Gforth.

If you have any interest in learning about threaded languages, or especially if you do any hardware control, I highly recommend that you give Gforth a serious look.