Why Forth?
JonnyMac
Posts: 9,108
I have always enjoyed computer languages -- though that hasn't caused me to learn a bunch of them (I enjoy paintings, too, but I don't paint). I see some incredibly bright individuals promoting Forth for the Propeller and I wonder why. Sincerely. Should I learn it? It seems intriguing and if these bright people are using it, there must be good reasons. I visit the computer book section at B&N every time I go in and I don't remember seeing any books on Forth. If I decided to learn, where do I turn for guidance?
Comments
http://iweb.dl.sourceforge.net/project/thinking-forth/reprint/rel-1.0/thinking-forth-color.pdf
Take a look at my Intro web page which is really meant for someone who has loaded Tachyon onto their system. Think of it as an interactive introduction.
There are links at the bottom of the page for resources but Forth is not C, well what I mean is not the language of choice so books will be hard to find. For an MCU environment I couldn't wish for anything better (really).
http://www.forth.com/starting-forth/
I tried to get into it a few times over the years but it just gives me headache and in the end is not what I want to do. For the ultimate in fast code I want assembler if need be. For anything else I want something that is easy to write and easy to read a year or so later when I get back to it. Forth does not do that for me and I don't see what big other advantage it might offer.
Coupled with the fact that almost no one in the world uses it, so it's hard to share your work, I'm not inclined to pursue it. There are many other interesting languages out there.
Still as an educational exercise it is worth while to give it a once over. You never know what ideas it might stimulate in your mind.
Well, it's understandable if you have read the book up to that point. But I don't want to spend more time than need be looking at such an ugly mess:)
Yep, it's an ugly mess, Forth lets you do that if you are inclined. It's all in the choice of names for the words and the STARS example is very simple I thought. If you type "8 STARS" that's what you get. But again, Forth is not just a "language" if you have read my Introduction page, it's a debugging and development tool loaded right into the chip, no assembler or "language" can give you that.
I wrote a CNC controller in S8 Forth many years ago, before Zilog canned their barely-introduced Super 8 microcontroller (which had ENTER, EXIT, and NEXT native opcodes). I can't say it was painless, but it was a lot simpler to write and debug than the same program would've been in pure assembly, and I was able to cram a lot more functionality into the limited EPROM space available.
-Phil
I like the immediacy of the environment, especially when playing with microcontrollers. Like Peter said, it's an interactive, immediate, fast interface to the hardware.
typed at the command line and the LED you have on pin 16 lights up.
Have a new "word" you've been working on that you want to have run on its own cog?
just started it on COG 3.
Not working quite right?
Made a bad word? You can "forget" it, edit it and load it again.
"Starting Forth" and "Thinking Forth" are the 2 classic texts. Peter's write-up for Tachyon is very good. There are a couple of others I need to dig up references to.
Want to run multiple propellers together? PropForth is probably the easiest way to do multi-prop development and especially testing. You can build a master/slave cluster as wide as you have pins to support. If you want to daisy chain them, you can go as deep as you want. (It's turtles all the way down!)
Want to see what makes a Forth tick? You can pretty easily dig into the guts of it.
Peter, in a matter of weeks (he IS one of the incredibly bright people Jon referred to), has come up with his version of a Forth that does what he needs/wants it to do. Pretty darn amazing!
There's a learning curve, it does seem strange but it is fast, fun and interactive.
Forth is stack-centric; most other languages hide the stack.
If you like RPN (post-fix expressions), you may like Forth.
I don't know about the incredibly bright individuals, but I promote it because it is the ideal tool for work on micro controllers, and embedded systems, and real-time (time critical small systems, according to my personal needs. Its ideal for drivers. It has all the power and speed of assembler, and the easy of programming in a high level language. Its as simple as basic, bit not so slow; and is only limited by the hardware. It is (or can be) nearly the same on all hardware, differing only where it is tailored for the specific platform. If you learn forth once , it mostly applies to all implementations. If you code with hardware abstraction in mind, and you do drivers, the next layer stuff can be pretty much identical from one implementation to the next.
Me, I'm lazy, yet impatient. I need stuff to be absolutely easy, and provide lots of instant gratification. I'm not so smart as most folks around here, I can't do stuff the hard way. If its not easy, its not going to get done by me anytime soon. And it better be fun. Forth is neat because it allows somebody like me to do a bunch of cool stuff where I can concentrate on the application I'm striving for, without getting bogged down by the tool. Of course, I was so impressed by the tool, that I got involved in the kernel development process; because I developed a vision for what the "perfect tool" would look like, and now I've found a group of folks that are helping me see that goal come true. So now the tool (and its associated processes) IS the application of focus. Pretty funny when I think about it. But after only a couple yours, I find before me a nearly (85%) perfect tool for my needs. Nothing else comes close.
All the best books on forth are free (Brodie, Starting Forth, Thinking Forth are the traditional staring points). The tutorials, instructions and other online resources from several universities are also good sources and are free. Forth was one of the earliest 'net collaboration projects I encountered, and the similarities of this forum to that community was one of the things that attracted me to the stamp and the prop. If you need any help, nearly all the folks will try find you a solution or at least give you a smart Alec response. (Just like in the rest of the forums)
The PROBLEM with forth is its TOO powerful. We can do absolute anything the processor is capable of, including destroying itself (if so capable). And its easy. You can get away with horrible code habits and still get a result, this was common in the past. (That's why I'm so big on requirements of best practices these days). There are no safeties or error checking unless YOU put it in your own darn self. I find this a bonus, the app either works perfectly, or blows up completely, so there are seldom hidden errors. Very easy to debug.
If you know any other language, you have no need of forth; you can just use what you got. But it you need anything that forth offers, (interactivity, power, speed, compactness, extensibility, flexibility, simplicity, conciseness, a bunch of old men to look over your shoulder) then forth is a good choice, as it has all these thing in abundance.
If you have any questions just ask. A dozen people will have the same question. If you can work an HP calculator you can code effective in forth. It takes about day to run through the tutorials and learn the basics, and only about 15 years to master.
To distill it down to the most basic features:
1) It's interactive, being an OS and a language you type a command (FORTH WORD) and it run immediately from a simple terminal.
2) Define a new FORTH and it's immediately ready to use. No compile, link, upload needed.
3) Very efficient both in code size and speed. Has been ports to virtually every micro back to 8080 to present.
4) Supposedly at least 2-3x more productive than conventional languages like C. Opinions up to 10x.
5) It's FUN to get the immediate gratification from natural flow w/o the usual compile, lik load etc.
6) Proven mission-critical ability, used in aerospace and celestial telescope control.
7) Still alive and actively developed. Look at Newmicros.com. ISOMAX Is real-time multitasking system based on FORTH. Can't personally vouch for the new stuff but they gave me great support on a 68HC11 board circa 1995 and are still in Dallas.
To be complete why is it Not more popular with all this goodness?
1) Stack-based RPN (think HP-41 et. al.) is foreign and non-intuitive to many.
2) Not "cool" or in great commercial demand (46th) on one survey.
Just try it, you're plenty bright enough and it well wort a couple hour even if you decide it's not for you.
BTW Jon, I found Tanner's Electronics from you old Nuts and Volts columns, any other gems in the DFW area? I'm in Plano but will trek for cool tronix stuff. Too bad you got too cool for Texas, would have liked to meet you. JK about the cool stuff, I had a brief fleeting interest in the talent arena. Good luck in those endevours.
It's not about need, it's about technical curiosity. I want to be a better programmer and keep my bag loaded with tricks; Forth seems worthwhile in that regard.
@RickInTexas: When I moved back home to Los Angeles in 2006 it was not far from All Electronics, another popular supplier for hobbyists. Made a friend of mine from Austin jealous; I had Tanners in Dallas, I have All in Los Angeles.
Exactly, with forth you build what you need to solve the problem (or reuse from other projects), nothing more, the kernel remains lightweight, fast, known and extensible.
I find some of the FORTH Forums very helpful. This one supports the old Jupiter Ace computer (possibly the only computer released with FORTH packaged as its native language).
Out of curiosity is there a Fast Fourier Transform in Forth we can look at. Just want to see what it looks like.
Some folks just can't get the stack part. The coolest thing is if you make ANY mistake, it usually results in a crash (until you level up a few times at least) so its like automatic debugging. Its either completely solid or it falls apart. Most other environments end up "hiding" errors by automagically doing something "for" you, those bugs are tough to find. Usually doesn't happen in forth, auto magic stuff is too expensive and gets in the way. This is not accidental, this is the whole point. Don't fight the tool!
But if you even have to think about the stack (this guy uses 1 item and leaves 2, this guy uses 2 items and leaves 1) then you might be going about it wrong. This is supposed to be the easy part. I never even think about the stack beyond the number of items consumed and number of items deposited (unless I see under- or over-flow).
But if you're a C guy, forth is probably the opposite of the way you walk.
Brian has a list of forth materials at
http://www.wulfden.org/downloads/Forth_Resources/
Okay guys, look at the thread title, the question please.
Forth is a lousy language to teach and grade students on, unlike other languages where the syntax and structure is rigid saying "You must do it this way, yes, good, correct". So guess what gets taught then?
Forth is more like a palette and canvas versus paint by numbers. So some may complain that there are no dots and you "have to" work out which way is best yourself. What a lousy language!
You know what? The reason I love to use Forth is probably the extensibility, flexibility, interactivity, and productivity that I get which I have not found with conventional languages. Plus most of all Forth is FUN. Yes, FUN. Can I say it again, FUN.
What happens when you have fun programming? You get a lot lot more done and faster, especially with all that feedback. You know guys, forget the manuals, forget the lint, forget the debuggers, just have FUN.
Try forth not. Do forth, or do forth not. There is no try forth.
..and in closing, Yoda certainly knew the forth:
Yoda: You must unlearn what you have learned.
Ray
The Propeller is very different from any other micro and while it would be possible to get Forth to run at near assembler speeds on some micros I don't think the Prop is one of them. All bytecodes are pulled in from the hub memory and that takes time to do whereas PASM runs within its comparatively tiny 496 "words" of combined program and register space without hindrance. However as even my nascent VGA graphics demos hinted at we can get a lot of speed without having to have a dedicated cog running PASM for graphics routines. However Tachyon cannot replace PASM totally, it just makes it so much easier though to handle the majority of interfaces in the same language and environment as the application.
1. How do you handle floating point in forth? Is there a standard way to do this?
2. How do you handle structs in forth? Is there a standard way to do this?
3. How do you handle objects in forth? Is there a standard way to do this?
It seems like forth has a couple of really nice features, which are execution from the command line and creating functions that can be called from the command line. I think this has been done with interpreted C also, but I don't have any experience with it,so I don't know it capabilities. It is certainly something that is done in bash, but once again I don't have much experience with that either.
How does bash compare to forth? Can you do the same thing in bash that you can do in forth?
My answer to "Why Forth?" is going to be that it makes a person think really differently! I've never forthed before, but I'm going to explore it some after reading that excellent book referenced above. The introductory material is golden. Some basic things were clarified for me, and I found some material on means and methods presented in clear ways that helped me see Forth better. --enough to want to write some programs to see whether or not it's for me.
I suspect the answer to this question would be to actually encode the structure as words. Once the words are done, the structure is invoked by the use of them in a greater context. Want to modify the structure? Change the words. That's forthy, from what I now understand. Some memory would be defined as a word, and other words would use that one to index, select a member, etc...
Instead of defining a structure that some language element understands, Forth gets extended. You write a program that understands the structure, and that program is invoked by the word used to name it, adding to the lexicon, which then allows for the higher level task to be described.
That intrigues me. Mostly because I've not done it that way before, and man! Talk about portable. All one needs is a small Forth kernel. Take all of that, stuff it into that kernel, and suddenly a whole environment has been moved from one platform to another. Powerful, if you ask me. Different, weird, but powerful.
2) See this paper for one of a number of discussions on structures in Forth. Do a Google search for "structures Forth" for others.
3) See this paper surveying a number of object support extensions to Forth.
Note that Forth is a pretty low level language, particularly for implementations for embedded systems or microcomputers. Floating point, structures, and objects would all be extensions. Structures would be the easiest to implement since they could be handled similar to arrays with the variable name normally providing the address of a structure and the structure field name adding an offset. The usual operators would then load or store to that address.
BASH and Forth are not really comparable other than they are both languages used for programming. BASH is a fairly high level language with a lot of built-in string manipulation, implicit memory management / garbage collection, etc. Forth is a pretty low level language, essentially an interactive assembly language interpreter for an integer-based stack machine. Forth was developed when almost all computers had the sort of memory sizes and capabilities now associated with medium-sized microcontrollers (16-bit, maybe 16K RAM).
Give me a typical real-world example of how you use floating point, what structs and objects you have in mind. However this changes nothing as Forth just allows you to morph it into whatever you want, be it Floating point cruncher, Object Oriented programming, Fuzzy logic, etc. I even wrote a PLD compiler once in a single afternoon with just a single page of Forth source vs the serious and voluminous conventional language source that was my alternative (never was).
I'm asking, because it seems to me, just about anything ends up "A single page of Forth", done right.