The Zen of Forth
cohost.org
cohost.org
It fit on two 360K floppy disks and managed to have 270 star systems and 800 fully explorable planets (well, the gas giants would kill you if you landed on them).
Forth was one of the first languages I learned because I looked up what languages my favorite games used. I knew some C/C++ and that was the most common answer. But when I saw my favorite game used Forth, surely it must be an amazing language. It is a pretty amazing language. Very fun to learn and eye-opening to me at the time as to what a language could be. It was a really fun learning experience, though I've never used it for anything very complex. I can see how it could be pretty un-scalable for teams, every Forth codebase seems to turn into its own unique language.
Also s-macke from HN reverse engineered the game:
https://github.com/nornagon/jonesforth/blob/master/jonesfort...
The runtime design is a little quirky but straightforward and you can extend the system as you execute (or even change interpreter semantics), or add optimizations such as some form of JIT quite easily.
We have megahertz and gigabytes and "embedded" systems with MMUs and filesystems at our disposal, so I'm guessing the time when forth was a solution has long since past.
It depends on the problem.
If it's education, I'd argue Forth still has a place. Writing a Forth compiler is a manageable project for a single developer.
Restricted resource creative projects, in code and other mediums, may help with teaching and creativity. I don't have research on that, and it isn't my idea, that credit should go to multiple others.
https://yosefk.com/blog/my-history-with-forth-stack-machines...
I think this part sums up the problem with Forth quite well:
> I defined the instruction set in a couple of hours. It mapped to Forth words as straightforwardly as possible [...] but it was harder than we thought. Presumably that was partly the result of not reading "Stack Computers: the new wave"
In other words: people too often confuse "simple" and "easy", think they don't need the experience of others, and then feel bad when they try and fail - and that post provides them with solace and a good excuses to quit when it happens with Forth. I don't mind them quitting Forth, but I'm more annoyed when they spread those half-baked "truths" about Forth.
As a person working on a programming language design that is a little forth-like in presentation (it’s not stack-based), is it just the anecdotal experience of Forth that leads to the seeming incompatibility with producing complex software? Or does this seem to be inherent in Forth’s minimal syntax design (similar discussions also happen in Lisp discussions)?
Most corporate software development uses languages like Java, C#, Ruby, and Python that are all very similar to one another. Sure, some of them use different punctuation, but the basic tools you use to solve problems all involve putting pretty much the same words in pretty much the same order. It's easy for someone with one language to quickly pick up another.
Forth's syntax immediately pushes it into the "weird languages" category because almost all of the words are in a different order. "1 + 1" is now "1 1 +". That sets it far enough apart from Python/Ruby/Java/etc that a tech company isn't going to use it unless they feel really strongly about it. (AFAIK there isn't any big corporate push for Forth like there is for Go and Rust and stuff.)
The main audience for Forth is therefore hobbyists. I think the main reason why you see lots of handmade Forth implementations is because Forth is really easy to implement. It's a fun hobby project. And because Forth is simple, its community biases towards people who like writing simple software. If you want to write complex software in a weird language, you'll probably gravitate towards a complex language like Haskell or something.
Incidentally, the oldest surviving language on a unix system uses this syntax (and predates C): https://en.wikipedia.org/wiki/Dc_(computer_program)
http://webarchive.loc.gov/all/20100506231949/http://cm.bell-...
> When the PDP-11 arrived, B was moved to it almost immediately. In fact, a version of the multi-precision `desk calculator' program dc was one of the earliest programs to run on the PDP-11, well before the disk arrived. However, B did not take over instantly. Only passing thought was given to rewriting the operating system in B rather than assembler, and the same was true of most of the utilities. Even the assembler was rewritten in assembler. This approach was taken mainly because of the slowness of the interpretive code. Of smaller but still real importance was the mismatch of the word-oriented B language with the byte-addressed PDP-11.
The main advantage of Forth is that it can be put in some very small places and run impressively fast.
http://www.ultratechnology.com/chips.htm
https://en.wikichip.org/wiki/novix/nc4016
> NC4016 (formerly NC4000) was a 16-bit stack machine microprocessor designed by Novix in 1985 primarily for the Forth programming language. The NC4016 directly executes the primitives of the high-level Forth programming language. The NC4016 was capable of executing typical Forth programs as much as 20 times faster than the Motorola 68000. Novix also offered the NC4000 Small C Compiler which was compatible with the NC4016 microprocessor.
> In 1988, the NC4016 design was licensed and improved by Harris Semiconductor which later rebranded the chip as the RTX2000, a radiation hardened version suitable for spacecrafts use.
https://en.wikipedia.org/wiki/RTX2010 ( https://news.ycombinator.com/item?id=8611274 )
> The RTX2010, manufactured by Intersil, is a radiation hardened stack machine microprocessor which has been used in numerous spacecraft.
(For the reasons explored already upthread.)
I like forth, I've implemented two, and yet the most I've done with it is write toy programs, and use it to script a host program a few times.
And not all research is done by university researchers. And not all of HN is solely industry practitioners. You just can't make huge assumptions about tech based on what you see on HN, basically. :)
With that said, PostScript is a stack language, x86 can be treated pretty similarly to a stack language, FreeBSD's bootloader used to be in Forth (may not be anymore, I don't know). And I hear it's used in constrained/embedded environments. So I don't mean to suggest it's not a practical language either.
As a side note, you tweeted sometime this summer about how there were a load of hobbyist compilers and a lack of hobbyist databases, then in a response mentioned the largeness of the research/conceptual space for databases. Those tweets got me to put database design and implementation onto the top of my ‘read up on’ pile. So, thanks.
Out of the box, out of the standard, there no real structured data type ala records, there's no strings, there's no modular coding construct. All of those can be added, but that's the trick. They have to be added. You also start getting into implementation specific stuff that, potentially, makes that kind of infrastructure not portable across implementations.
Not that portability is a huge concern, but it's a factor if you want to import someone else's implementation.
All of that can be overcome, but that they have to be overcome is part of the problem. Original scheme suffered from this as well, but look how far it's come over time. To the point that all "usable" schemes aren't really the 50 page R5RS versions anymore.
It also doesn't help that Forth niched its way into controllers and smaller systems. Forth had all of the availability advantages early on, perhaps even more so, than C, Pascal, etc. But, organically, Forth never caught on.
It's fair to say that Lisp suffered "not enough computer" issues early on to hinder adoption, but that's long past. But Lisp still suffers adoption, when pretty much every complaint about it is long buried -- save its syntax.
And I just think folks, again, organically, "wisdom of the crowds" don't like S-Exprs for code. They just don't like it. It's off-putting enough that folks look elsewhere.
Forth, in the end, suffers similarly. All of these demos and mini DSL examples look great. But they're contrived. Take a look at any Forth code you can find and see if you can make sense of it. Especially "modern" code, where we "know better" and aren't constrained by file size, or line size, or "screens" or "blocks".
Take a look at that Zen's github.
And try to make sense of it.
This is all barrier to entry, and most aren't willing to climb over it.
I really am drawn to a Forth—esque syntax, but i think that has more to do with the type theoretical and categorical semantics of my language and it’s projectional properties than simple appearance. Fortunately I don’t think I am ever going to release it, so I don’t have to worry about general acceptance.
There are structures, strings, etc in standard forth. And if you're using an older forth without structures here is a structures library in 2 lines:
: +FIELD OVER + SWAP CREATE , DOES> @ + ;
: FIELD: ALIGNED 1 CELLS +FIELD ;
Usage: \ Shape struct
0
FIELD: SHAPE.WIDTH
FIELD: SHAPE.HEIGHT
FIELD: SHAPE.AREA
CONSTANT %SHAPE
\ Example shape
CREATE SQUARE
1 , 1 , 1 ,
\ Get height
SQUARE SHAPE.HEIGHT ?
\ Get area
SQUARE SHAPE.AREA ?
\ Set width
2 SQUARE SHAPE.WIDTH !
2 SQUARE SHAPE.AREA !While the language is not exactly popular right now, I think they are on the right track on what a modern stack-based language should look like.
ANS Forth (the somewhat controversial Forth standard) does have strings.
ANS Forth also has structures (records). I see gforth also has its own alternative. [0][1]
I believe you're right that ANS Forth has nothing akin to namespaces or modules.
[0] https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/For...
[1] https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Str...
I believe there are a few very good Forth coders out there that have been working as consultants and much of the code is very proprietary and not available as open source. Otoh, there are tons of free Forth implementations, but they are really meant as a learning tool - it's fun and instructive to write a Forth.
However, even so, there is just very little Forth application code available as open source.
This isn't software that you're going to be sharing. It is in nearly every situation bespoke software for that specific industrial or medical application.
There are enough areas, libraries, whatnot, where it would be perfectly fine to share forth code.
I mean, look at device drivers for linux.
Doing this would really have helped the ecosystem, I suspect, because people would have tried to clean up their code a bit before throwing it on github, for instance. Not that examples that are on github today are all stellar.
The insurance issues that a manufacturer gets when people are provided the software to modify it becomes much more complicated.
It becomes much more difficult to insure a facility that has an industrial refrigeration unit where it is possible for people to change how it runs.
Note that this is an issue with industrial and medical systems - the insurance for these is such that the manufacture is liable unless they have taken every possible precaution to prevent misuse. From an insurance perspective, that includes keeping the source code closed.
This is a very different issue from consumer devices. An open source device driver tinkering causes the computer to overheat? "No warranty" is enough to disclaim that for consumer systems - not so with industrial devices where the tinkering person can kill someone and the company making the device is liable because this was a precaution they could have taken.
Is that an accurate statement of the legal liability? If the vendor clearly states in the sales contract that it disapproves of unauthorised customer modifications, that such modifications will invalidate product safety and regulatory compliance testing, and that it excludes all liability for customers who ignore those warnings – and the customer does it anyway – why should they be liable?
> This is a very different issue from consumer devices.
I think it is indeed different, but in a different way than you think. Products sold directly to ordinary consumers, courts accept that most consumers won't read or won't understand the sale contract, and so are more likely to invalidate exclusions of liability contained therein. Industrial and medical devices are not sold directly to individual consumers; their purchasers are usually corporate entities (whether a business or a not-for-profit enterprise such as a hospital), which can be assumed to have ready access to legal counsel; and the staff of the entity involved in the purchase can be expected to have a professional level of understanding of the technology, safety/regulatory/compliance risks involved, etc. Given that, courts are far more likely to uphold exclusions of liability (including exclusions of liability for unauthorised customer modifications), than for a contract of sale to individual consumers.
Furthermore, the vendor can put in the sale contract that the customer indemnifies the vendor for any liability caused by the customer's unauthorised modifications. Such an indemnity is far more likely to be upheld in a B2B contract than a B2C one. And unlike the B2C case, the customer is likely to have significant financial resources, and its own liability insurance, making such an indemnity much more meaningful in practice. The vendor can also do customer due diligence, such as asking customers to provide evidence of their own liability insurance – something which is rarely practical in the B2C space.
https://www.law.cornell.edu/wex/products_liability
https://www.biren.com/blog/2020/september/defective-machiner...
> For the latter, California law provides options to Plaintiffs who pursue products claims against manufacturers, distributors, and others responsible for a defective product. These claims may allege:
> Design defects, which can include a lack of adequate safety devices (i.e. machine guarding, shut-off valves, etc.) or other potential hazards in a product’s design that a manufacturer could and should have known would increase the risk of foreseeable harm.
If it is foreseeable that "here's the source code to the industrial refrigerator controller" that someone might alter it and update it, then the manufacturer will have a harder time not being liable.
Similarly, "here's the spinal nerve stimulation controller" may be risky to have people get the source code to to modify on their own.
---
If you modify your car, and its not a design defect and something breaks, that's on you.
If you remove a throttle on the computer's CPU and melt the solder, that's on.
If you can reprogram a controller on an industrial device - that's on the manufacturer always.
Industrial and medical equipment falls in a different category for product liability than consumer devices.
> The Court’s ruling in Demara means the consumer expectation test may effectively be used to demonstrate the inappropriateness of a design for a broad range of products – even those which perform highly complex tasks in industrial settings – and hold manufacturers to higher standards.
Note that the manufacturer of an industrial or medical device is held to a higher standard of "not letting the customer do the wrong thing with it."
The issue is that at the end of the day, the manufacturer of industrial and medical equipment is liable for damages unless they take every possible foreseeable precaution to prevent the user of the device from inadvertently causing harm to themselves or others. That liability extends through resellers, rentals, and 3rd parties.
> Note that the manufacturer of an industrial or medical device is held to a higher standard of "not letting the customer do the wrong thing with it."
> The issue is that at the end of the day, the manufacturer of industrial and medical equipment is liable for damages unless they take every possible foreseeable precaution to prevent the user of the device from inadvertently causing harm to themselves or others.
You are essentially repeating the plaintiff’s contentions in that case, and the Court rejected them.
As far as I am aware, Virginia product liability law does not treat medical devices specially - they are subject to the same fundamental principles as any other product. Can you cite any sources to the contrary?
https://www.whiteandwilliams.com/resources-alerts-Medical-De...
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6173549/
https://biotech.law.lsu.edu/courses/drugsf02/rest_3_6.htm
> (a) A manufacturer of a prescription drug or medical device who sells or otherwise distributes a defective drug or medical device is subject to liability for harm to persons caused by the defect. A prescription drug or medical device is one that may be legally sold or otherwise distributed only pursuant to a health-care provider's prescription.
> (b) For purposes of liability under Subsection (a), a prescription drug or medical device is defective if at the time of sale or other distribution the drug or medical device:
> (1) contains a manufacturing defect as defined in § 2(a) [i.e., when the product departs from its intended design even though all possible care was exercised in the preparation and marketing of the product]; or
You appear to be claiming that "strict liability" is something specific to industrial and medical products – but under the law of many US states, "strict liability" applies to design defects in all products.
In fact, in the case of Brown v. Superior Court, 44 Cal. 3d 1049, 245 Cal. Rptr. 412, 751 P.2d 470 (1988) [0], the Supreme Court of California concluded that the the "strict liability" standard which applies to design defects in ordinary products should not apply to prescription drugs, because it threatened to increase the expense and risk of new drug development to the point that it might discourage it, thereby harming public health. A number of subsequent decisions, including Hufft v. Horowitz, 4 Cal.App.4th 8 (Cal. Ct. App. 1992) [1], extended that ruling to implantable medical devices. So, contrary to your assertions that medical devices are under some special legal regime which is stricter than that applicable to ordinary products, California product liability law is actually (at least in this regard) weaker for prescription drugs and (implantable) medical devices than for consumer goods, since it exempts them from one of the product liability rules which apply to almost every other category of goods.
The big difference for me was that I couldn't even read my own Forth code after I had been away from it for a year; I had to first re-teach my brain to think like a stack machine. That was never a problem with Lisp. I can look at 10-year-old code I wrote in Lisp and re-understand it quickly (especially with the tools built in to Emacs that are quite helpful for Lisp code exploration).
"Zen" is a good way of describing Forth. Charles Moore often says things that seem like zen riddles in themselves, like Operating Systems are obsolete, you don't need local variables, you don't need structures, and a whole host of other things. It's very difficult to know what he's talking about without context, or why he thinks what he says.
I've toyed around with his colorforth, and given up. I doubt I'm the only one. It has cool stuff like you being able to change the fonts on the fly. It has a keyboard consisting of 25 keys (because 25 keys should be enough for anyone, apparently), which don't map onto a Qwerty keyboard (maybe Dvorak?) and has different modes based on colour. So I'm like "how the living fuck am I supposed to use this thing"? It's like trying to figure out the controls of some alien spacecraft.
It clearly works perfectly for Charles. He's clearly on some whole different level than the rest of us.
Forth is really fun to play with; before going back to using something that is a bit more practical. It has macro-like facilities. I find them very difficult to reason about. Forth seems like Lisp in terms of power, but at the opposite end of the spectrum in terms of size. Lisp is a complete language. Forth gives you a boxful of atoms that you assemble yourself.
I wrote a barebones Forth. Little plug: https://gitlab.com/mcturra2000/cerbo/-/tree/master/forth/v3 I haven't touched it for a year or two, though.
It incorporates the notion of a token type. The idea is that you could write a recursive-descent compiler using it, although I hadn't pursued the idea much.
To give you an idea of the malleability of Forth: I got bored with creating variables one-by-one, so I defined a word called "vars:". Now I can create several variables at once, e.g.: vars: foo bar baz
Here's an implementation (that works in my forth): : VARS: begin parse-word dup while $create 0 , repeat drop ;
I simplified the implementation and made the idea more composable: : VARS: line( $create 0 , )line ;
Done right, Forth looks like haikus. I'm not at that level, though.
Perhaps they are culturally associated?
Imagine a Forth bootstrap moving quickly on to C ABI and dynamic linking, to libpython and python ast vocabulary, and to numpy etc. Or a "parse all the languages" GLR/mixfix-expr/regex-token sandwich parser and some LLVM vocabulary. But ... that's not "doing Forth". Culturally.
Lisps could read `a[i] = 3` as sugar for `(setf (aref a i) 3)`, but don't. And for overlapping reasons, an organization for weeks struggles to even build its big CommonLisp thing (reader symbol conflict hell).
When a community doesn't much go somewhere, the path is unlikely to be smooth. And so a non-technical cultural challenge becomes something more.
Minimal syntax has value and power. Non-minimal syntax has value. So if there's one, but not the other, that seems notable, and perhaps a symptom of other issues around tooling up for complexity?
I think it all boils down to specificity vs generality. Moore is specific. He solves one problem, not many. If he has a different problem, then he uses a different solution. By being tightly focussed on his one specific problem, there's many things he doesn't need. Like, he doesn't need a file system. He just stores data and code in blocks in the disk. You end up with a system that is small, tight, minimalist, does what it needs, and then stops.
That approach doesn't work if you want to make something more general-purpose, like a Linux kernel. It's not like Linus Torvalds is a nincompoop that somehow didn't realise that 90% of the code was unnecessary. Linux is popular because it solves a broad range of problems to a broad range of people.
Forth systems are unpopular because they solve the problem of one man. Not only that, that one man must have a mental model of how his whole system works. The upside is that the system is much simpler than a general-purpose one.
One could say that Forth is not just a language, but a whole philosophy as to how and what programs should be constructed.
(Alan Kay is the Willy Wonka of computing; one can restate Joe Armstrong's banana-gorilla-jungle problem more whimsically: with Smalltalk you go into the shop to buy a candy bar but end up getting the whole chocolate factory, with everything you need to make every kind of confection staffed by Oompa-Loompas and ready to go.)
> "what software do you build in Forth?" they say "Forths"
When you learn that Forth is a broad term that can mean a shell, programming language, application, operating system. compiler, editor, block system, style, etc..., it becomes a little clearer.Those that ask of purpose of FORTH, is same as those that ask purpose of above mentioned things.
With FORTH every WORD is a little program that you use like million of times if it's any good.
Like others I indeed started with making my own Forth dialect (actually more than one try; and yes one of them could compile itself), but now it is my go-to scripting language. My latest script for instance was about converting some flat files format into an SQlite database. I also use it to prototype and test things, and I have a program that run and works for me 24/7. Can't really say more because it is partly work-related.
> Specifically, managing a dynamically changing list was basically a nightmare
So don't do it. Or do as everyone else does: use some C library/do it in C. You don't have to do everything in pure Forth, there's no law about that and being able (or not) to do anything in pure X doesn't prove anything.
Or just allocate 1Mb for your todolist file (or limit the number of entries) and accept that if you need more, you'll have to edit one constant in your program. I don't see anything wrong with that. Some of my scripts have hard-coded parameters and I can imagine a user would be reluctant to edit something in alien incantations, but I can also imagine using the flexibility of Forth to create a more familiar "syntax" and make it so that those parameters are in a separate script, so that it looks more-or-less like a configuration file to the profane.
You have no idea how we actually are narrow-minded, not creative and driven by habits about how things should be done. Forth won't get you a job, but at least it can open your mind and this reportedly is a portable (to other languages) quality.
Also, avoid "what if" - especially with practice programs you don't plan to actually use. "what if" is the FUD device by excellence.
> When I asked a group of Forth programmers why it is so difficult they just said "Forth is not suited for that problem"
The whole point of Forth is that it is, in essence, a recipe for a language you can make yourself. If your Forth is not suited to the problem, then adapt it. Forth is not a dogma.
trick is you erase record memory you don't need , which then gets reused on next record add, it checks if it is empty.
You can of course absorb these ideas and "get" what you want on a case-by-case in something like Python. You're one regex-replace away from turning `50 burgers` into `burgers(50)`, and then you call eval and you're good!
So, is the book ‘Wizard’s Bane’ actually focused on using Forth (or some ersatz programming language concept) to do magic? If so, that basically describes a huge part of an hypothetical RPG I’ve been designing on paper since college. Also, would you recommend the book to a fan of genre fiction?
At one time, it was in the Baen Free Library ( https://www.baen.com/allbooks/category/index/id/2012 ) but it appears to have gone back to a regular "you buy it book". https://www.baen.com/wizard-s-bane.html
However, if you dig in Wayback, you can find the Free Library of old - https://web.archive.org/web/20050828182931/http://www.baen.c... and the first two (IMHO the best two) books. Its old school frame stuff to get it to the screen (which works) and linkable via going to the frameless version ( https://web.archive.org/web/20050903172506fw_/http://www.bae... )
The first 3 books can be found as one volume on Kindle https://www.amazon.com/Wiz-Wizardry-combo-volumes-Book-ebook...
As to recommending the book (and series)? Yes. The first three are good. Book 4 is... getting weird and 5 is weird.
It has the most charming illustrations I've ever seen in a text book.
I taught myself some Forth, I ought to get back into it though!