8 ms·
Retro: A Modern, Pragmatic Forth
- burlesona 7y agoI’m not too familiar with Forth, but I see posts about it and variants like this on HN from time to time. High-level, what makes Forth interesting? What are it’s major strengths and weaknesses?
- unixbeard1337 7y ago>High-level, what makes Forth interesting? What are it’s major strengths and weaknesses? Strengths: Lisp-level metaprogramming facilities while also being extremely fast and close to the metal. Weakness: the Perl problem (write-only) but worse.
- RodgerTheGreat 7y agoIt's also important to observe that Forth's approach to metaprogramming is very powerful, but completely different from Lisp's approach. It's not just another retread of the same ideas. CREATE...DOES>, immediate words, parsing words, fancy tricks with the rstack and other fun Forth stuff has at best very loose analogs in Lisp macros.
- DennisP 7y agoWhere could I get a good tutorial on Forth metaprogramming?
- RodgerTheGreat 7y agoI think the late J.V. Noble's "A Beginner's Guide to Forth" does a good job introducing the language and touching on some metaprogramming features. Near the end he demonstrates a library which extends the language to support infix math expressions (a "FORTRAN") to make scientific computing more convenient. https://www.taygeta.com/fsl/docs/551.jvn.fall01/primer.htm#create https://www.taygeta.com/fsl/docs/551.jvn.fall01/primer.htm#c...
- wrycoder 7y agoWrite-only, because only the functions (‘WORDS’) are named - no parameter names. There are also no local variables in standard Forth, you have to push (and pop!) them from the separate return stack. Accessing parameters is done by manipulating the stack, which makes the code opaque after a year or so. Named constants and variables are all global. So, it is very difficult for a team to use, since most of the legibility of software comes from insightfully selected names. Two thirds of those names, at least, are missing in Forth. Like assembler, Forth is untyped. These failings could be at least partly forgiven if Forth was fast. But it isn’t, because most words are short and all the stack manipulations are done with subroutine calls. It’s possible to write an inlining, optimizing compiler, but then the simple layout of code in memory is lost, and quick introspection using a memory dump becomes nearly impossible. But, it is a neat tool to bootstrap a working system quickly on new, embedded hardware with limited resources. Definitely a black belt tool for lightweight systems written by a single person. Hard on maintenance programmers. See Jonesforth for a tutorial example. I wrote a Forth a few decades ago. The insight gained reminds me of Nand2tetris, but the latter is far more mainstream.
- szemet 7y agoall the stack manipulations are done with subroutine calls This can have an advantage: one can stuff much more code (and functionality) in a very memory limited system (e.g. smart cards), especially with WORDS compression: "Huffman threaded code is one of the most compact representations known for a computer program" https://en.wikipedia.org/wiki/Threaded_code#Huffman_threading https://en.wikipedia.org/wiki/Threaded_code#Huffman_threadin...
- 0815test 7y ago> Write-only, because only the functions (‘WORDS’) are named - no parameter names You could use lambda notation to name the parameters of a function, while still preserving postfix function application as in FORTH. This would effectively give you De Bruijn notation, in which named lambda was often denoted `[var] expr`. Though, ironically, De Bruijn is also known for his "nameless" De Bruijn indexes, which dispense with the very naming of function parameters, replacing these names with what are in effect references to positions on some abstract argument stack.
- kazinator 7y ago> Lisp-level metaprogramming facilities Not even close. Rather more like macro-assembler-level metaprogramming facilities.
- lalaithion 7y agoA lot of language VMs use a Forth-like bytecode. Python, WebAssembly, even the JVM languages (sort of) use a stack-based bytecode. So it's a useful paradigm to understand if you're interested in working on the internals of a language like that.
- RodgerTheGreat 7y agoI think this is a bit of a misconception. A critical element of Forth is that there are two stacks: a data stack for operands, and a return stack for keeping track of what word to return to after each call completes (and other fun stuff). These stacks grow and shrink independently. Unused operands on the data stack quietly flow along to the next word that takes a peek with no overhead. There's no difference between how the stacks are used within an "expression" and between "function calls"- these concepts don't exist at all like they do in curly-brace languages! In a typical non-Forth "stack VM", there is a single stack which behaves like the C stack: allocate an activation record with locals on a function call, pop the whole thing off at the end. Intermediate calculations go on top of all this, avoiding the need to explicitly register allocate in bytecode, but operands don't naturally flow between function definitions; they're copied around explicitly as the stack grows and shrinks.
- joejev 7y agoI think they meant that the bytecode itself reads like a forth. The data stack works just like it does in forth within a single Python function.
- new4thaccount 7y agoForth is like lisp in that there are lots of implementations, but this effect is multiplied. In fact, most folks that learn forth roll their own which is what the inventor Charles "Chuck" Moore does for every project. Forth is unique as it is probably the only serious language that you can write production systems in and understand the entire compiler/interpreter, parsers, data structures, and functions (called words). Everything is pretty simple. You could use C as the base, but traditionally you would write a few dozen assembly routines and link them together and form some core words to manage a stack (there are 2 stacks and that is it) and make more complex words. It gets a lot more deep, but Forths aren't really for me. If I'm going to use something weird, I'll write some APL.
- RodgerTheGreat 7y agoAs someone who has written a considerable amount of Forth as a hobbyist, studied APL, and been paid to write K, it's fascinating to me how aesthetically similar the Forth and APL communities are, despite their languages approaching minimalism from completely different sides of the spectrum. I definitely credit learning to read Forth as helping to open my mind to the unconventionality of APLs. Spend enough time out there on the edge and you don't get as hung up on whether syntax resembles C or is "unreadable".
- Avshalom 7y agoForth and APLs both have a really strong data flow aesthetic.
- RodgerTheGreat 7y agoExactly! They're both concatenative languages, or at least languages which have a concatenative subset. Juxtaposition is composition. APL/K and Forth also have very concise syntax for declaring functions. // JS const foo = (x,y) => 1 + x * y // K, lambda with implicit arguments foo: {1+x*y} // K, points-free foo: 1+* // Forth : foo * 1+ ; It's subtle, but I think making function declarations "lightweight" in this manner is important for encouraging factoring. In APLs the scale of the semantic units you give names to is a little different, but that's its own separate discussion.
- RodgerTheGreat 7y agoYou can bring it up quickly on a new platform, be it exotic hardware or a self-styled VM. Small Forths can be totally understood by the programmer (perhaps just a few pages of code for their kernel), but still be expressive enough to actually use. Applications written in Forth are typically deeply entangled with the interpreter. You can't separate a "program" from the environment. It's more like a collection of ideas and design principles than one singular language. Most Forth programmers write their own implementation, or several. You can decide what indulgences you want. (I like having "quotations"- inline anonymous word definitions like Retro has- and special-cased string literals. I also have my own style of looping words that aren't quite like ANS Forth.) This also means Forth code can never be dropped into another project and directly re-used: you can only take ideas. Starting from a blank page every time, carrying with you ideas and writing code afresh is a good way to travel light. Self-reliance. Building human-scale programs. No one would write a web browser, or an Oracle-compatible SQL engine, in Forth, because a Forth programmer wouldn't have much of a use for those things. Solve only the problems you really have, with as little code as you can manage.
- Avshalom 7y agoSo there's this talk https://fsharpforfunandprofit.com/posts/recipe-part2/ https://fsharpforfunandprofit.com/posts/recipe-part2/ that pops up here every now and then. In it he builds these monads to allow for functions that may or may not throw errors or care about thrown errors etc. Similarly IO monads in Haskell and Mercury are often described as secretly threading the entire world through the monad and returning an entirely new world thus allowing for side effects in purely functional code. In a stack based language those aren't interesting ideas, they're just naturally emergent. The other thing is that it makes it utterly trivial to pull out or insert code in a function. allowing for some radical refactoring.
- rwmj 7y agoA long time ago I wrote a tutorial for FORTH. It's in two parts, a part written in i386 assembly: https://github.com/nornagon/jonesforth/blob/master/jonesforth.S https://github.com/nornagon/jonesforth/blob/master/jonesfort... followed by a part written in FORTH: https://github.com/nornagon/jonesforth/blob/master/jonesforth.f https://github.com/nornagon/jonesforth/blob/master/jonesfort... It builds gradually from asm primitives into a somewhat sophisticated language with features like exceptions, function inlining and a decompiler. The major strength, probably unique amongst programming languages, is that you can keep the entire system in your head (by "system", I mean both language and the simple operating system that it implements). The downside is that the code is write-only.
- 4e1a 7y agoI just wanted to say "Thank You!" For jonesforth. I don't know how many times i've read it. It's probably what made everything "click" for me, so "thanx"
- co_dh 7y agoI'd like to say thank you too. I've studied your code many times, and it helps me understand forth finally. I was attracted by colorforth first, which is harder.
- jsymolon 7y agoAnother big thank you. I've used that to build a FORTH interpreter on an AVR micro.
- tieze 7y agoI wanna thank you fourth! Such a great tutorial, and I used it to build a little Forth for the GBA. It was such a great experience.
- gaze 7y agoFORTH is less a programming language and more a religion or philosophy on how programs should be written. The philosophy basically suggests that one should make programs as simple as possible... way, way simpler than a modern programmer might think "as simple as possible" means. This philosophy clicked for me when I saw the segment in the colorforth manual saying "if the cursor doesn't appear on screen, hold up and left until it does." Why bounds check if it isn't necessary? Ultimately a language is there to make programming a CPU easier. If you believe in just absolute minimal complexity, FORTH is what you get when you find this intersection of leverage vs. minimal tolerable complexity. A FORTHer generally doesn't believe in writing device drivers so much as writing maybe 20 lines of code yourself that twiddle the registers to get done what you want. You're basically told to ask yourself over and over again "do you really need that?" The answer is usually "well I might need it tomorrow... maybe..." but in software, requirements change so fast that the abstractions we design for what we think we might need tomorrow don't actually match what's actually needed. The FORTHer solution isn't to just continue generalizing until you have a fully galaxy brain interface that could accommodate anything the future could throw at you-- it's just to not generalize at all, and to put the weight of working in a system with less abstraction on the programmer. The bet is that the weight of dealing with less abstraction is less weight than dealing with a tower of abstractions that were built for the wrong requirements. I don't know if I fully buy it, but it's worth trying to think like a FORTHer as an intellectual exercise to make you a better programmer.
- deleted 7y ago[deleted]
- jimnutt 7y agoIt's interesting (at least to me) because it's different from anything else out there. The way it runs, the way it is compiled and the language itself are nearly unique. It's also easy to bootstrap on very small machines and fairly easy to implement. It's the light saber of programming languages, "a more elegant language for a more elegant time". But then, that could just be the nostalgia talking...
- narcindin 7y agoWhat is the culture here on correcting wrong star wars quotes? "An elegant weapon for a more _civilized_ age" https://www.youtube.com/watch?v=0aRtupiY9Dw https://www.youtube.com/watch?v=0aRtupiY9Dw
- DoreenMichele 7y agoPolite pedantry is generally okay here. If done well and including a source, it may even get a bunch of upvotes (a somewhat objective, though imperfect, metric of cultural acceptance).
- justin66 7y ago> What is the culture here on correcting wrong star wars quotes? Suggested approach for dealing with those getting Star Wars wrong is the same as that for dealing with men's rights advocates, Scientologists or those linking to lesswrong.org. Politely and quietly excuse yourself and, in extreme cases, just do your best to pretend they do not exist.
- jimnutt 7y agoThank you, it didn't sound quite right, but wasn't sure why...
- deleted 7y ago[deleted]
- muriarte 7y agoThe link http://forth.works/book.html http://forth.works/book.html is broken. Maybe the correct link is: http://forthworks.com/retro/book.html http://forthworks.com/retro/book.html
- new4thaccount 7y agoAnother modern Forth that has a cross-compiler to make Windows, Linux, Mac, Android, Raspberry Pi, and iPhone executables is 8th. https://8th-dev.com/ https://8th-dev.com/ It comes bundled with the C++ based JUCE GUI framework, SQLite, ODBC, encrypted binaries, JSON support, and a lot of support for other things that you typically don't see in a tool written by one person. The more expensive licenses come with AWS and automotive Linux support, so nifty for this area. Sadly, it is closed source and commercial (although you can get source with an enterprise license I think). On the plus side, it is really cheap (there is a free version too I believe) and the main developer seems to add user requested features and fix bug reports very quickly. I obviously wouldn't write a production system in it (although I'm sure you could), but I wouldn't mind having a fun REPL based language to write simple and easy to distribute cross-platform desktop apps.
- fouc 7y agoRetro has been in development for 2 decades and has gone through quite a few changes in that time. It is inspired by Chuck Moore's forth and avoids the mistakes of the ANSI standard. It is the forth of choice for any serious forth developer.
- tempguy9999 7y ago> avoids the mistakes of the ANSI standard I'm unlikely ever to learn forth, but if there are general lessons to be learnt from the mistakes made, I'll learn from them. So, what are those mistakes? (thanks)
- astrobe_ 7y ago> It is inspired by Chuck Moore's forth Which one? Because Retro and ColorForth seem light-years apart to me.
- yiyus 7y agoColorForth is light-years apart from everything.