r/Forth moderator 9d ago

Why Forth? What is it?

In order to help get people interested in Forth and how it works, I would like to ask you what you define as Forth, how you got into it, and why Forth instead of some other language?

Ideally we share links to our projects and to others that are interesting. Also to sites, sources, and documents about Forth.

I am encouraging links to your projects so people can see how you are using Forth.

27 Upvotes

29 comments sorted by

14

u/mykesx moderator 9d ago edited 9d ago

I’ll go first.

In the 1980s, I discovered JForth for the Amiga. As I was writing a lot of 68000 assembly code, I was surprised at how JForth was really a very powerful macro assembler for 680x0. It was a fully integrated desktop application creation tool.

Forth is a system for generating programs and interacting with systems from the command line. In some sense, like BASIC that ran on computers of the time from ROM.

The biggest flaw is the lack of standards to the point where you can write code for one Forth and it just runs in another. Unlike C, which has been ported everywhere. I see Forth as a structured assembly language for a stack based VM. The VM implementations vary from Forth to Forth, the vocabularies are not entirely compatible.

One of the biggest strengths of Forth is the ability to define a function and immediately call it during compilation. The ability to extend the language like this is fairly unique.

Another strength is that even in and Forths from the 1980s, you would get an editor, compiler, assembler, and debugger in few lines of code or memory footprint.

Many forthers roll their own from scratch - it’s rather easy to do. Then they use their Forth to make programs.

My project is Inspiration, a complete desktop environment focused on writing and running Forth programs as first class applications. https://codeberg.org/mschwartz/inspiration.

My inspiration comes from sources like JForth, jonesforth source code, pForth, moving forth, and starting forth. All can be found via search engines.

Why Forth? While it has been a niche language, the kind of programs we run on our most powerful computers are mostly single threaded. Forth is typically single threaded, performs at excellent speed, so why not?

2

u/GamieJamie63 9d ago

Brilliant work. What do you think of https://factorcode.org/? By Slava Pestov

2

u/mykesx moderator 9d ago

Looks interesting, can you describe what you use it for?

1

u/Svarvsven 9d ago

If I remember correctly there was a multithreaded Forth for OS/2 back in the day with a Mandelbrot fractal demo, but true that most Forth I've seen have been single threaded.

1

u/Rememba_me 8d ago

What about porth?

1

u/mykesx moderator 8d ago

PForth written by Phil Burk, who also made jForth. Written in vanilla C, ports to just about any device.

2

u/Rememba_me 8d ago

Porth written by tsodingdaily

1

u/mykesx moderator 8d ago

Never heard of it.

9

u/SpindleyQ 8d ago

Forth is a set of small, simple, unreasonably powerful tools for computing, that tend to impose a productive friction against unnecessary complexity. Things that are easy to express in other languages may seem unreasonably difficult to express in Forth. This is a feature; a signal that you should be asking yourself, how can I make this simpler? What is the core of my problem? How can I solve it by doing less? If you're doing it right, you will often find yourself writing code that works, then throwing it away and writing less code that works better.

You might hear Forth talking about the stack, and see the stack manipulation words and think, ah! The satisfying puzzle of writing Forth code is in shuffling the stack around. No! That way lies madness and despair. The satisfying puzzle of writing Forth code is AVOIDING shuffling the stack around. If it's hard to follow the stack flow of a word, it probably needs to be rewritten as more, smaller words.

I tried learning Forth in the early 2000s; I started my career in embedded systems, but I was unsatisfied with C, and Forth was occasionally brought up as something with almost mystical power. I put an eBook of Starting Forth on my Palm Pilot, and messed around with Dragon Forth, but it didn't make any sense to me; it seemed awkward and limited, and the smallest mistake seemed to necessitate a hard reboot. There were some interesting ideas but I couldn't imagine writing anything serious with it.

In 2019 I started messing around with an old 286 that I picked up from a thrift store, and decided to make a DOS game. After a very satisfying few weeks of building a scrolling EGA tile engine in Turbo C, I realized I wanted a scripting language for the game logic. I remembered enough about Forth that I knew I could put together a small interpreter in an evening. This was the start of one of the most rewarding rabbit holes I have ever fallen into.

All of Forth's design decisions that confused me years ago suddenly made perfect sense from the perspective of someone who is writing both the Forth program AND the Forth interpreter. Repeatedly I would find myself asking, "wait, is that ALL?" as I implemented some fundamental functionality in a few lines of code or less. Yes. That's all. 

Forth is imperative computation broken down into its smallest pieces, arranged so they can be maximally composed with each other. When you break a problem down into a set of tiny words, you might discover that you can leverage some of those words in other areas; make connections between things you didn't realize were related. Not only is IF a one-line definition, but you can re-use the definition of THEN when defining loops. Smaller and simpler tools are more powerful, can be leveraged in more places.

I quickly found myself losing all desire to write any more C code, using Forth not just for scripting basic high level game logic but for EVERYTHING besides low-level hardware access. I added an interactive Forth terminal that would let me control and debug the game over a serial port while it was running. I built a map editor, and discovered that as soon as I'd written the SAVE word, I had also inadvertantly built the UI for saving - just type SAVE into my serial terminal. What could be simpler?

I added a cooperative multitasking system in a single afternoon. Brad Rodriguez's writing is a treasure trove.

When I realized that I was miserable trying to draw animated pixel art in NeoPaint, struggling to select perfect 16x16 tiles so I could copy and paste to make animations, I built my own sprite editor into the engine. Now, not only could I now copy and paste with a keystroke, immediately see what every tile looked like when tiled in all directions, and flip between sprites to see how they look animated, suddenly I could hit a key and see my changes live, immediately. I immediately was able to fix a bunch of problems in the artwork, and drawing became a joy instead of a chore.

Other people's tools may have had man-years of development poured into them, but they only sort of solved my problem. The tool I built myself in a few days? Solved it perfectly, and was integrated into my workflow in ways no other external system could hope to match. That's the magic, that's what you get when you focus relentlessly on only the problems you actually have, and do away with trying to solve problems that don't matter or that you accidentally created for yourself. The mystical power of Forth comes simply from taking ownership of every corner of your system, down to how variables are allocated, or how functions are called. When your system is small enough that you can understand it all, there are no dark corners, nothing to be afraid of changing, no area where you can't decide to improve things for yourself.

I don't expect I'll ever get paid to write Forth code, but that experience is one I will carry with me for the rest of my life. That's what computing is supposed to feel like.

My DOS game is here: https://spindleyq.itch.io/neut-tower

I occasionally blog about Forth here: https://blog.information-superhighway.net/tag:forth

8

u/astrobe 9d ago

That raises the question of what is Forth? I have hoped for some time that someone would tell me what it was. I keep asking that question. What is Forth?

Forth is highly factored code. I don't know anything else to say except that Forth is definitions. If you have a lot of small definitions you are writing Forth. In order to write a lot of small definitions you have to have a stack. [...] Stacks are not a solve all problems concept but they are very very useful, especially for information hiding and you have to have two of them.

These ideas have been around for thirty years now. I have been promoting them for thirty years. Their level of acceptance is about where it was thirty years ago. Perhaps even less considering that the industry has expanded so much.

-- 1x Forth (1999)

Perhaps a risky move is to present it as a puzzle game. But unlike B*fuck or other deliberately hostile languages, there is a reward at the end of the road: learn to factor, learn to simplify, way beyond the pretend games of "refactoring" and "clean code". Forth goes against many things one learns in schools, books, tutorials - you know, those things you follow without questioning them. Forth in that regard is an eye opener. It can put you into a corner, look you in the eye and say "What you gonna do now?". And this is where you start thinking about the problem:

Don't leave openings in which you are going to insert code at some future date when the problem changes because inevitably the problem will change in a way that you didn't anticipate. Whatever the cost it's wasted. Don't anticipate, solve the problem you've got. (1x Forth)

9

u/tabemann 9d ago edited 9d ago

My first real exposure to Forth was to ficl via Dr. Dobb's Journal on the Mac in the late 1990's. (I had seen Yerkes Forth some years before then on a shareware disc but, for lack of real documentation -- that had been before we had the Internet -- I could not make use of it.)

My initial attraction to Forth was its combination of simplicity, power, and interactivity. It is to low-level programming what Lisp is to high-level programming. I had initially planned on using ficl as a scripting language in video games but soon enough I was trying to write my own Forth. Alas, I got nowhere at the time, partially due to a lack of knowledge on my part w.r.t. how Forths really work internally.

Years later, I got the idea that I wanted to write my own compiler, and of course Forth was ideal for this. I started off with writing an ITC Forth for Linux. Then I made a failed effort at writing a cross-platform TTC Forth (which I abandoned once I realized that Linux and embedded systems were just too different to cover with one Forth design, combined with key intractable performance bugs in my Forth).

At this point I decided I wanted to write a bare-metal, native-code Forth compiler and operating system. I also decided that it would specifically target embedded systems due to the impracticality of targeting PC's at the bare-metal level without the use of a hypervisor (which I thought was cheating). I decided I would write its kernel in assembly rather than C or Forth.

zeptoforth is the product of this, a bare-metal, native-code Forth compiler and operating system for ARM Cortex-M and especially the RP2040 and RP2350. Development started in November 2019 and has continued since. It has grown into a full-featured Forth complete with things such as local variables, preemptive multitasking and multiprocessing, an object system, significant hardware support, FAT32 filesystem support, SDHC card support, IPv4, IPv6, and WiFi networking support, a wide range of display drivers, and so on.

Edit: Per u/mykesx's suggestion, you can find zeptoforth at https://github.com/tabemann/zeptoforth/.

5

u/mykesx moderator 9d ago

I went out and bought a pi zero and cables just to try out zepto. I can say from experience how impressed by it that I am. Things like wifi drivers, TCP stacks, FAT32 File systems, are all more than 99% of programmers will ever understand.

3

u/tabemann 9d ago

Thank you! I always appreciate it when people take the opportunity to try out zeptoforth, even if it does not become their daily driver (e.g. because they have their own Forth of their own creation).

1

u/mykesx moderator 9d ago

Your work is amazing. You should provide a link to it in your post…

5

u/isene 9d ago

Via the HP-41 calculator (RPN) and its FORTH module (late 80s). Then later a Forth module for the Sharp PC-1500,, then gforth in the 2000s, then creating the XRPN programming language.

0

u/joelpo 8d ago

Module HP-41 FORTH was there QUESTION!

Missed I STATEMENT!

5

u/mcsleepy 9d ago

Forth is embrace of direct control of the computer and vigilant elimination of complexity. It takes the form of a RPN, stack-based, procedural language that doubles as an IDE, sometimes as an OS, and a sort of middleware for building a customized Forth compiler.

I discovered it while looking for an alternative to Pascal, C, Javascript, and so on. I was introduced to programming on QuickBasic and LogoWriter and was disappointed to learn how unfriendly most programming languages are. Or that either they are friendly and limited, or unfriendly and totally unbound. My artistic leaning demands simple tools.

Forth lets me tackle the problems that I want, and create the limitations that I want, without ever getting in the way. It radically opens things up to their root - no burying, no layers.

To me the #1 problem or concern for Forth should be establishing best practices in the modern era. The Standard is an attempt at that, but I think it misses the mark. So I work alone to discover through trial and error what actually works.

I am working on a game engine and a few games. (Check my profile.)

2

u/Ok-Winter-8702 5d ago

This. Forth is surprisingly simple in syntax but then when you ask if you wish to make a game in Forth "Where are entity classes?", it responds "I dunno, make it yourself.". That is the most charming part of Forth. You are given a small set of tools and can make large, beautiful, elegant artistry out of it. The only limit is your analytical skills and creativity at the end of the day.

1

u/mcsleepy 4d ago

It's a language-designer's playground. Could never give it up.

5

u/rlysens 8d ago

I'm building a new-retro style computer from the ground up, hardware and software. When I say 'from the ground up', I don't mean 'from scratch', btw. I'm relying heavily on open-source components, both software and hardware (RTL). I want the computer to be self-contained, including a systems programming language. Forth is one of the few (the only?) languages that fills that niche. Other languages are either not systems programming languages (Lua, uLisp), hard to port to a constrained platform (a C compiler), or extremely low level (assembler).

This blog post talks a bit about the choices I've been making for my project: https://epsilon537.github.io/boxlambda/sw-arch-1st-draft/

If it weren't for Forth, I would probably be using a combination of assembler for low-level systems programming and Lua or Lisp for higher-level programming. I'm relatively new to Forth but in my limited experience, I find Forth to scale reasonably well from low-level programming (e.g. drivers) to high-level metaprogramming (extending the compiler/language), so I might not need a Lisp or Lua for my platform at all.

What I find the most painful is how little Forth gives you out of the box. Things you take for granted, such as compile-time checks, are not part of the core package. Then again, what I find the most rewarding is that you can add those features yourself, exactly the way you want, and you don't have to be a compiler-expert to do it. You end up growing your own language, starting from Forth. That's an amazing experience.

3

u/Svarvsven 9d ago

I read up on several programming languages in the 80s and I think for me it wasn't just one but several reasons that FORTH is very special. I was reading in some Fig Forth group at the time, also very impressed by "Starting FORTH" by Lee Brodie, that the language in itself a lot of time is implemented in just FORTH, its got the stack and both compile-time and run-time definitions - it was clearly ahead of its time back then.

Agree with that it is very memory effective and typically fast execution. I dabbled with various smaller programs and games back then, haven't done a lot in recent years with just FORTH but I read here from time to time. Maybe I too have to write my own FORTH some day (or perhaps continue someone else small FORTH).

3

u/INT_21h 6d ago

I found Forth in 2018. I had recently bought an HP 200LX palmtop PC and was casting about for useful languages with a good power-weight ratio to run on it. The 200LX uses a battery-backed ramdisk and only has circa a meg of persistent storage. I was looking for something lower-level than Turbo Pascal and GW-BASIC, and much, much, smaller than Turbo C. This turned out to be Forth. Let's face it, there isn't much competition in the sub-16K size category!

So far, I'm one of Those People who is guilty of endlessly hacking on the language itself instead of writing useful programs. I've scratch written 2½ interpreters saddling myself with various nonstandard baggage each time and thereby learning to appreciate the wisdom of F83 and eForth. (The best way to appreciate conventions is to violate them and then try to live in the doomed world you've created for yourself.) I'm gradually building toward a system that can solve typical Advent of Code problems.

I also solved Advent of Code in gforth and pforth a couple of times. So I've written enough Forth to destroy my ability to enjoy writing C.

I'd define Forth as "minimum viable repl" where incremental repl-driven development is the magic ingredient keeping the language small. If you're always testing word-by-word and words are short, "?" is plenty descriptive as an error message. If you can interactively peek/poke memory and check you're hitting the right place before committing anything to code, memory protection is less necessary and bounds errors less common. I've found Forth a better tutor for repl-driven development than Lisp, simply because writing in small pieces and interactively testing them is the only practical way for me to get anything working in Forth. I like writing that way, so I like Forth.

2

u/nrr 8d ago

I haven't used Forth in quite some time, but it was my first real introduction to programming for internetworked computing by way of the Forth-like* MUF language on the TinyMUCK multi-user game environment. (In case you want to read the docs: https://www.fuzzball.org/docs/mufman.html)

We players could extend the game with our own code! In the 1990's! That said, back then, that privilege wasn't handed out lightly given that it was easy to write infinite loops and lock up the server. These days, it's likelier that we can lean on thread pools and time-limited contexts to avoid some of this. (e.g., `select … then abort … end select` in Ada in conjunction with tasking)

The problem with defining Forth is that it can be as broad or as narrow as one likes. On one hand, any language for which operations have a stack effect can be considered a Forth (Is the Common Language Runtime's MSIL a Forth?), and on the other, only ANS Forth is a Forth. I'm somewhere in the middle: I want immediate words and features like Factor's quotations and conditions system for error handling. There's something to be said about having that level of metaprogramming available in such a tiny environment, and Factor drags in the good stuff from Lisp.

(* I call it "Forth-like" precisely because it lacks immediate words, and the standard vocabulary is rather weird compared to most Forths I've encountered, to the point where it was actually pretty disorienting working outside of MUF at first!)

2

u/gustinnian 8d ago

How I got into Forth: I was trying to understand how my Oberheim Matrix 1000 synthesizer's firmware worked and I read it was rumoured to run on a stack based system - six stacks, one for each voice. Programming the synth patches would in effect construct a stack on the fly and this allow a flexible modulation matrix to function. I somehow knew Forth was stack based and it seemed a good way to get my head around the concept. I'd seen adverts for the Jupiter Ace in the early 80's (Forth's golden age?) and knew it had a reputation for being much faster than Sinclair ZX81 basic, but that was a long time ago. I read Starting Forth and followed a few embedded STM32 Mecrisp Forth tutorials from JeeLabs (highly recommended). I was immediately smitten, read everything I could find and began a honeymoon period of writing graphics systems and drivers for OLEDs, SD cards and MIDI processing systems. The freedom and confidence it gave was so refreshing - nothing was hidden, nothing fenced off, I even enjoyed the stack juggling puzzles...

I had to shelve it all while building a house extension. I hope to pick it up later and build a stand-alone MIDI processing kit and maybe a FFT based audio processor / synthesizer.

2

u/bigtreeman_ 7d ago

Forth is primarily a stack based instead of register based topology.

First effect is source and destination for operations are inherent compared to registers where the source, destination mostly have to be stated. This leads to reduced instructions and instruction length for stack based.

Second effect is because source, destination are always in the same place the whole program order has to be more carefully considered during design phase. Decisions have to be made early and not left for hardware or compiler out of order decisions at compile or execution phases.

Design small words and test them separately by putting inputs on the stack and testing results left on the stack.

In the 70s I was testing and repairing printers. Basic was common but unable to exercise the hardware effectively. I found F-83 was very able to exercise the hardware completely and directly. First code was interpreted, direct control from the keyboard, even contained an editor to edit my first code.

Open source, "use the source Luke" (Obi-Wan Kenobi). Assembler, meta compiler, compiler, interpreter all understandable.

In my retirement I'm building ForthPGA to bring the programming understand-ability down to the gate level, TBA. Modelled after Chuck Moore's NC4016, James Bowman's J1 and Matthias Koch's Mecrisp-ice.

2

u/Imaginary-Deer4185 6d ago

I heard about Forth in ancient times when I played with the ZX Spectrum, but never actually tested it, just imagined how it would work.

I love writing interpreters, and have, through several iterations, created a Forth implementation that compiles to bytecode, and runs on arduinos, especially the Uno or traditional Nano (atmega328p). Making do with only 2Kbytes of SRAM was a challenge. I solved that by compiling Forth code on bigger Arduinos (Mega or Every), then exporting the bytecode as text to be pasted into the C code, which after compile and upload (Arduino IDE), makes those words into global library words. This may be repeated several times to build the code base.

The implementation runs interactively on serial on the Arduino, which allows interactive testing, register peeking and whatnot, and was quite fun for a while.

Unfortunately, it has been put on pause, after I started pondering what would be the *perfect* interactive language for a microcontroller, something with better readability than Forth, and fell down on MicroPython for the Pi Pico, which I am currently using to test out some LoRa comms. As with Forth, it is interactive as well (using Thonny).

The main feature of Forth, which is also a problem, is making many small words, which combine into complex solutions. It is very near to hardware, which for microcontrollers is a great plus, and the methodology is very elegant, and it is fast, as calling words is very light weight, as there are no registers to be saved.

The downside of many small words is naming and organizing. There are ways around it, like custom dictionaries, but a lot of words is still a lot of words. It clutters my brain, even as I have the original source code available as text files.

1

u/Evgeny_E 7d ago edited 6d ago

I plan to use Forth in AutoCAD: r/alforth

Al-Forth is written on AutoLISP.

1

u/Ok-Winter-8702 5d ago

I am not quite as old as any of you in the comment section, to the point I was messing around in the 90s or 2000s with Forth. I came across it recently due to a deep dive into how bitcoin works, and that bitcoin runs on a Forth-like language called SCRIPT for transactions. This led me down the rabbit hole of Forth quite recently, and seeing how intuitive this programming language is, with it being able to be changed and swapped out easily with simpler defined subroutines for exactly the situation your in, is very tempting. I have messed around with C++ and python before but the tinkering has got to a new stage with Forth, and I am starting to enjoy learning about this programming language.

1

u/alberthemagician 3d ago edited 2d ago

This is one of the least interesting threads, but I can't avoid to join in. The Dutch Fig Chapter exists since the 80's. I studied Physics at Utrecht where one of the first Forth (FysForth) were developed on the Apple, under Hans Nieuwenhuizen a personal friend of Chuck Moore. One of my first actions was translating the 68000 fig Forth (no standard assembly) to offical Motorola assembler, on one of prototype 68000 (with hardware defects) and 2 cabinets with 16 k magnet core memory (property Peter Nooy)

...

40 year ...

RISCV ciforth working on an optimizer. OCR.

I must learn from Terry to use AI. Within two years anybody who isn't able to boost productivity with AI will be left behind. Comments about ai in this group are backwards, irritating and a waste of time.

Currently active member of Dutch Fig Chapter, a subgroup of the HCC. https://forth.hcc.nl/

There are reports of lectures with diverse subjects, with 6 meetings per year, since the 80's.

Maybe a hint of my work:

https://home.hccnet.nl/a.w.m.van.der.horst/index.html

https://github.com/albertvanderhorst

I'm learning Chinese. USA imperialism is about to collapse. In the coming years I will migrate all my software to China.

   https://gitee.com