Thinking (about) Forth

When will I ever learn to stop going against the flow? Everyone is raving about Go, or Node.js or Scala. And here I am, bashing my head against Forth. A dinosaur. An old, simple, tiny little thing in reverse polish notation. Weird. Unusual. Small. And incredibly powerful.

Forth you like if honk then. And I do like this quaint little language.

For the last few months I’ve been writing one Scheme interpreter after the other. Another weird little language I fell in love with. It’s extremely simple, yet incredibly complex and powerful. Just like Forth. But where Scheme strives to abstract as far as possible from the acutal machine, Forth tries very hard, and very successfully, to keep as close to the bare metal as possible.

Forth programs are incredibly small. The interpreter/compiler combo is incredibly simple. Oh, and it’s an incremental compiler, by the way. With an embedded REPL. And you can write your own one in just over 2000 lines of Assembly code (an excellent example of Literate Programming BTW). So it’s very simple to port, you’re not depending on third party tools that break as soon as you stress them a little, and still you have portable, clean, simple code. What’s not to like?

Well, for one, using a stack based approach is a mind bender if you’ve just become addicted to closures. Forth doesn’t provide you with Garbage Collection or even duck typing — there’s no RTTI at all. Pro: You get to do your own GC algorithm, your own type system and so on. Con: Unless you implement it, it’s not there. Scheme looks bloated in comparison. And still, I can see the benefits of using Forth, especially in constrained environments. Think ATtiny or Arduino.

My son just entered elementary school. He’s VERY interested in computers. And I’m very interested in providing him with everything his brain asks for. What about a turtle robot? Throw together a few Lego bricks, add an ATtiny and a 16×4 display on a small PCB, a PS/2 keyboard, two electric motors and tires and you’re good to go:


And so on. Maybe add a pen on its own axis? A few minutes later, you’re writing code like this:


Oh look, it’s not an exact hexagon, the angles are wrong. What could we do to correct them? Once you turn it into a game, the possibilities are endless. Logo, you say? Simpler and more powerful.

Now try that with C. Why do I need a compiler? What is “void”? Why can’t I just enter commands while the robot is running? And so on.

Forth is interesting in yet another way: It treats code and data as the same thing, just like Lisp and Tcl, but from an extremely low level perspective. Programs writing programs are as common in Forth as in Lisp, it’s just that they’re called “immediate words” instead of “macros”. Same thing, really. It’s mind bending to see how powerful such a tiny little language can be once you get the basics up and running.

Sure, there’s still the big complaint against macros or immediate words or however you decide to call the concept: Once you’ve created your own Domain Specific Language with them, other programmers will have to learn “your” language before they can work with your code. But look at it from another angle: If you get this specific language right, anyone, programmer or not, will understand the top level code without even trying.

And that’s a Nice Thing ™.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s