9 ms·
A Forth Apologia (1988)
- mikewarot 4y agoI believe there are two big problems with Forth 1) losing track of the parameters in the stack (unlike Lisp, there are no parens to match and serve as containers) 2) everything is a number/pointer. STOIC provides strings, hash tables and more, along with parameter type checking. Those are, in my opinion, the two main factors that keep Forth from being the "dual" to Lisp.
- kragen 4y agothese are real annoyances but most people get stuck in the stack manipulation tarpit first before those become the main problems 2over should be safe, legal, and rare
- vanderZwan 4y agoWell, there is Kitten, although it hasn't seen an update in two years and was moving quite slowly before that too. https://kittenlang.org/ https://kittenlang.org/
- kragen 4y agoyeah, cat, kitten, and joy are interesting. sadly factor is abandonedish too because slava is too busy cloning kotlin
- flembat 4y agoForth is extensible, it is not complete out of the box like a C compiler. Just extend it with named local variables and use them. There is no shame and unless you are on some rare and exotic stack machine, no performance penalty using local variables. I also need to add safe strings with their own memory pool, rather than block copying bytes around in the same dictionary where my code lives. I am just not a good enough programmer to use Forth strings without crashing a lot.
- kragen 4y agofrom my non-expert viewpoint, the big penalty for named local variables in forth is that it is a barrier to breaking up a word into smaller words, because pieces of code inside the same word that communicate through a local variable will stop working if you break them into separate words bigger words impede using the forth repl as a line-by-line stepping debugger, so they diminish the interactivity that is the reason you'd use want to forth in the first place agreed about strings; one of the first things i wrote in forth was a straight port of bernstein's stralloc, last millennium
- Karrot_Kream 4y agoIf you can keep variables separate between different words, you can make sure that the variable stays local to the word. It is error-prone but can stop the variable from leaking and keep words separate.
- kragen 4y agowhat do you mean
- Karrot_Kream 4y agoDefine variables before you start defining words. Make sure you use one variable per word. It's a manual process but when composing words helps make sure variable collisions don't happen.
- Gordonjcp 4y ago> 1) losing track of the parameters in the stack (unlike Lisp, there are no parens to match and serve as containers) If you're losing parameters in the stack, you need to refactor. The whole idea of wrapping things in brackets is what makes Lisp such an unreadable mess. > 2) everything is a number/pointer. If you don't understand why everything is a number or pointer, you don't understand anything about programming languages.
- dottedmag 4y agoI have tried Forth and failed many times. The biggest problem for me was the amount of effort needed to keep track of "local" variables. Was I missing something? Is there a way to let Forth keep track of where on the stack "foo" lives if it is needed several times in a subroutine instead of always shuffling the stack or counting the elements on it?
- JadeNB 4y ago> Was I missing something? Is there a way to let Forth keep track of where on the stack "foo" lives if it is needed several times in a subroutine instead of always shuffling the stack or counting the elements on it? I'm not a Forth novice, much less an expert, but my impression is that the answer is that if you spend much time thinking about the location of variables, then you're "doing it wrong"—to steal the phrase, trying to write C in Forth, rather than to write Forth. My understanding is that the emphasis is on building highly domain-specific languages out of many small words, each of which is re-factored so much that it never needs to keep much track of any, or much, shuffling of variables on the stack.
- dottedmag 4y agoSo basically "use it as a language for combinators", right? But combinator languages are pretty hard to read no matter what one does, don't they?
- kragen 4y agono forth is ruthlessly imperative c: x = a[n-1]; n += y * x; plot(x, y, n); forth: : foo n @ 1- a @ x ! ; \ ‡ : bar y @ x @ * n +! ; : baz x @ y @ n @ plot ; : quux foo bar baz ; naming each line of code allows you to do things like bar n ? to see what n is after you bar in exchange for the somewhat rebarbative syntax and lack of typing you get turing-complete macros (with full compiler access, without the pitfalls of string-replacement macros) and an interactive repl that fits into almost the smallest microcontrollers because you have total freedom to redefine the syntax you can construct whatever dsl you prefer it is nothing like combinatory logic except that you can occasionally write a point-free function ______ ‡ this assumes a is defined as something like this : array create cells allot does> swap cells + ; 20 array a so that 3 a computes an &a[3] suitable for @ and !, etc.
- JustLurking2022 4y agoIt's been around for decades and has virtually zero traction, which is to say, Stop trying to make Forth happen.
- kragen 4y agoi mean, you're not wrong, walter, you're just it's unlikely to displace c or rust or python or unix or freertos or openocd but it does have something special, like a delorean or something; unlikely to be forgotten either
- rvense 4y agoNot all programming is about making maximal products in minimal time. I also use hand tools all the time, a lot more than if I was a carpenter billing by the hour. How artless one must be to worry about 'traction' off the clock!
- kragen 4y agothere is one art no more no less to do all things with art- -lessness (piet hein, grooks)
- eschneider 4y agoBack in the day, Forth wasn't so much software, as an amazing idea for how to make software for platforms where you had no tools/or terrible tools, which was most personal computers or embedded environments. Once you groked how Forth worked, it was quite practical to put together a language kernel in ASM or machine code and you were off to the races.
- kragen 4y agoi think maybe a lot of the difficulty i had understanding forth was that i mistakenly thought of it as a programming language, and considered as a programming language it's not that great, much like bash
- eschneider 4y agoForth is definitely a language, and depending on what you have available, it can be a great option. These days, when you can download gcc cross compilers for almost anything, there's less need to roll-your-own Forth, but it's still pretty interesting to study.
- kragen 4y agoforth definitely has a language in it but i don't think the language is what led people to prefer it to, e.g., c or a nice macro assembler (i'd say 'leads' but there don't seem to be a lot of people doing things in forth these days; duncan's 01988 estimate of 50000 'serious' forth programmers seems two orders of magnitude higher than seems likely today) i think the appeal is that in 16k or 64k you get a repl which (unixlike) doubles as a scriptable ui, a sort of source-level debugging, interactive variable inspection (and i/o port poking, memory dumping, etc.), instant turnaround for changes (no 'my code's compiling'), multithreading, virtual memory, an assembler and disassembler, an extensible compiler with a turing-complete macro system, and dramatically better code density than machine code all of that makes a pretty appealing package, especially for certain kinds of tasks, and it's only loosely coupled to the language qua language as duncan points out, part of the appeal is that the development environment is a lot more integrated than something like turbo pascal
- deleted 4y ago[deleted]
- Animats 4y agoI once wrote a robot controller in Forth, because that was the only thing available on a dinky microcontroller in the mid-1980s. Would not have done it if the C compiler for that target had been cheaper. We have better tools now. Back then, it was one of the few ways to do interactive development on really tiny machines. In that niche, it was useful.
- diffxx 4y agoForth is a mindset as much as a programming language. It is a language for minimalists. If you can solve your problem efficiently in Forth, you can be confident that your solution will work in essentially any computing environment. Unlike C, the core of Forth is small enough that a single individual can implement it in a few days at most. This is incredibly liberating because you will realize that you will never be seriously hindered by the tools provided to you because the Forth way is to just write your own tools. Of course this goes against the modern programming culture which has become extremely maximalist and makes you dependent on complex tools like VSCode + LSP in order to be productive. There is another way. The language itself leaves a lot to be desired, in my opinion, but the core idea of having a small extensible compiler around which one can build efficient problem specific DSLs is brilliant. Unfortunately I think it is too easy to get bogged down in the bad parts (or at least parts that appear bad to our modern programming sensibilities). For a good overview of what makes Forth special, I recommend Thinking in Forth https://thinking-forth.sourceforge.net https://thinking-forth.sourceforge.net even if you never expect to program in Forth. It was written in the 80s but it still holds up. Read the first 80 pages or so carefully and I can almost promise that it will change the way you think about programming in a good way. Once you have internalized the Forth mindset, you don't need to actually write Forth code. But you may be grateful that Forth taught you how to find your optimal programming language that exists somewhere inside of yourself.
- haolez 4y agoForth is an important skill for a zombie apocalypse. You can create a simple computer, with a simple instruction set, and get a high level programming language in a short time :)
- DuncanMak 4y agoKragen, how much of what you said here about Forth also applies to PostScript, do you think? I have been tinkering with PostScript but not Forth, I grok that PostScript is a lot closer to LISP. A lot of people (without actually learning these languages/systems) say Forth and PS are similar, but I know that's not really the case - their resemblance is fairly superficial.
- kragen 4y agoi agree, and though it is certainly possible to get ensnared in dup exch baling wire in ps, it isn't nearly as common, maybe because you can just 6 dict begin for some tasty recursion-safe local variables when you have the appetite or maybe because you can't >r r@ r@ rdrop, or because garbage-collected [arrays] remove the temptation to write subroutines with 6 parameters ps is fairly big compared to forth, not the 16 megs of libgs.so.9.50 but definitely a lot more than 8k what they have in common is interactive repl orientation, user-defined control structures, great flexibility, lack of static types, and of course rpn i was going to say 'closures' but of course ps's scoping is dynamic so it lacks them