[HN Gopher] What the hell is Forth? (2019)
___________________________________________________________________
What the hell is Forth? (2019)
Author : harryvederci
Score : 84 points
Date : 2023-01-13 20:15 UTC (2 hours ago)
(HTM) web link (blog.information-superhighway.net)
(TXT) w3m dump (blog.information-superhighway.net)
| drivers99 wrote:
| > A single person should be able to understand a computing system
| in its entirety, so that they can change it to fit their needs
|
| This idea combined with the notion that software written by
| someone else necessarily serves their purposes rather than yours
| has me gradually working in that direction. Really all I need to
| do is write (or run) a forth on some bare metal. It's fun to
| build your own hardware as well which is what I'm during learning
| by doing right now too. Once I realized it was possible after
| reading jonesforth, I've been interested in (obsessed with) the
| idea.
|
| To quote jonesforth:
|
| > You really can write a complete FORTH in, say, 2000 lines of
| code. I don't just mean a FORTH program, I mean a complete FORTH
| operating system, environment and language. You could boot such a
| FORTH on a bare PC and it would come up with a prompt where you
| could start doing useful work. The FORTH you have here isn't
| minimal and uses a Linux process as its 'base PC' (both for the
| purposes of making it a good tutorial). It's possible to
| completely understand the system. Who can say they completely
| understand how Linux works, or gcc?
|
| > Secondly FORTH has a peculiar bootstrapping property. By that I
| mean that after writing a little bit of assembly to talk to the
| hardware and implement a few primitives, all the rest of the
| language and compiler is written in FORTH itself.
| onlyrealcuzzo wrote:
| The part about the language being a little bootstrap and then
| mostly implemented in its own language...
|
| Are there other languages like this? At least any mainstream
| ones?
| avmich wrote:
| Well, Norvig's (et. al.) JScheme -
| http://norvig.com/jscheme.html - is implemented in Java -
| partially, and then a bunch of core functions are written in
| JScheme itself.
|
| Or, if you look at implementations of J... like this -
| https://code.jsoftware.com/wiki/Essays/Incunabulum - it's
| basically creates something closer to J in C and then writes
| the rest of the interpreter. If you squint hard enough...
| igrekel wrote:
| I remember and learned a little forth because of that console
| you could get with PowerPC Macs back in the day. I think I
| remember there was a key combination to boot in that embedded
| forth interpreter. It was pretty cool
| Jtsummers wrote:
| > A system does not have to be complex to be flexible,
| extensible, and customizable
|
| An important lesson that many teams and orgs still haven't
| internalized. Maybe one day it'll be more commonly understood.
| hlehmann wrote:
| [dead]
| carapace wrote:
| If you want to build a Forth check out R. G. Loeliger's "Threaded
| Interpretive Languages Their Design And Implementation"
| https://archive.org/details/R.G.LoeligerThreadedInterpretive...
|
| (Jonesforth has already been mentioned.)
|
| Read "Starting Forth" and "Thinking Forth" too, even if you don't
| want to write a Forth or program in Forth. It'll still expand
| your mind in useful and fun ways.
| zabzonk wrote:
| these are all great recommendations, particularly the TIL book,
| which taught me so much about Z80 assembly language, though you
| really only needed a little bit to implement a forth back then
| - i went a bit bonkers in my implementation. i also used a a
| forth-like language implemented in C++ for an "adventure"
| writing system that taught me a lot about C++. you can't go
| wrong with implementing a forth if you want to learn.
| idatum wrote:
| I still use it when exploring a microcontroller, lately with
| Mecrisp-Stellaris Forth on an STM32 device. It's a joy to use.
| nemoniac wrote:
| Wait! Is core Forth smaller than core Scheme?
| avmich wrote:
| If you think about garbage collection alone, you might guess...
| amalgamated_inc wrote:
| I believe that any programmer should at least know a little about
| Forth. It's like Lisp. Just knowing about them is extremely
| enlightening, even if you don't use them in your daily life.
|
| Obligatory link:
| https://github.com/nornagon/jonesforth/blob/master/jonesfort...
|
| This well-written tutorial will have you implement a Forth in 500
| lines of assembly and another 500 lines of Forth.
|
| You will feel like a different person after having done this.
| People will like you more. Food will taste better. You'll never
| be alone again, always knowing that you can re-create a fully-
| functional programming language from assembly within half an
| hour, if you needed to. It is said that people who know Forth die
| happy.
| amatic wrote:
| I couldn't get jonesforth to compile correctly. Not sure why. I
| found a python and a javascript implementation, not sure if I
| like them. Any recommendations?
| anyfoo wrote:
| Do you maybe want to tell us how exactly "not compile
| correctly" manifested?
| amalgamated_inc wrote:
| I made tons of typos in both the assembly and Forth part when
| I did mine, but it did end up compiling correctly. Hunting
| the typos down was hard though because you get literally 0
| feedback.
| jll29 wrote:
| Agree with the parent, it's a language worth knowing like LISP
| is worth knowing, but (unlike LISP) it is not a good language
| choice for almost all use cases in 2023 (almost all use cases
| are not that memory constrained any more, even embedded
| systems), for the same reason you shouldn't use Perl (it is
| very hard to read code).
|
| I was going to say "its main disadvantage is that the only way
| to find out what a piece of code does is to execute it
| mentally" (non-declarative) but one may say that of every
| language to an extent. Instead I will say it feels like using a
| macro-assembler for a stack VM.
|
| Clifford Stoll, author of "The Cuckoo's Egg", in which he
| described how he chased a hacker, allegedly used FORTH to
| control a radio telescope
| (https://www.amazon.co.uk/gp/product/1416507787) back in the
| days (not sure, I might have got this nugget from the book).
| slaymaker1907 wrote:
| I don't know, I think it can be a pretty solid language
| depending on what variant you use. There are few pieces of
| code in Forth that make me think "man, this seems repetitive,
| but I don't know how to actually generalize this" because
| abstraction is little more than structured copy+paste.
| avmich wrote:
| > abstraction is little more than structured copy+paste
|
| Which perhaps is logical, since Forth is a concatenative
| language. Roughly, one can split a program in half at any
| point and get two subprograms which can be called one after
| the other - in the same order - to get the same result.
|
| Like, to calculate 2 + 3 * 4 one would write 2 3 4 * + in
| Forth, this sequence can be split in two 6 different ways,
| and we can even meaningfully interpret those two parts each
| time. E.g. part 4 * + is a two-argument function which
| multiplies first (closest to the top in the stack) by 4 and
| sums the result with the other argument - in JavaScript it
| would be function(x, y) { return 4*x+y; }.
| mananaysiempre wrote:
| I don't really feel I can judge whether it's good or not to
| use generally, but it's difficult to obtain a similar degree
| of DSL-ness even on pretty powerful (e.g. Micropython-grade)
| constrained platforms. Forth, along with Lisp, is one of the
| original DSL substrates ( _Thinking Forth_ spends like half
| its pages on preaching EDSLs), and I don't think it's really
| been supplanted in that niche by anything that doesn't
| require a beefy GC.
|
| (The _extremely_ easy bootstrapping from any point down to an
| ISA manual and a ROM programmer--or a monitor that accepts
| peek /poke/goto from a UART--I also don't think has really
| been replicated anywhere else.)
| SoftTalker wrote:
| Perl doesn't seem like it should necessarily be hard to read
| (I'm not a perl programmer) but agree that most perl that
| I've seen is pretty opaque. I guess if you really get to know
| the idioms maybe it's easier.
| pkaye wrote:
| The original use for FORTH was to control radio telescopes.
| macintux wrote:
| I was delighted (and saddened) when I discovered a Forth
| manual written by W. Richard Stevens for Kitt Peak
| observatory. A great writer lost far too young.
| seanw444 wrote:
| I've also heard it cures cancer. Any truth to this?
| shrubble wrote:
| The percentage of people with cancer who are Forth users is
| very low. Take that as you wish...
| amalgamated_inc wrote:
| Results not in yet but it looks promising, yes.
| agumonkey wrote:
| It can alleviate the symptoms of javadenomas
| AlbertCory wrote:
| I didn't even see a mention of the old bumper sticker:
|
| "Forth love if honk then"
| erichocean wrote:
| In Factor: "Forth" love? [ honk ] if
|
| Not quite as lyrical, but perhaps easier to understand.
| TimMeade wrote:
| i had a room mate in the early 90's who was a forth programmer.
| At the time they wrote the code that did the airport displays. It
| was all forth. I wrote c back then and never really grasped it.
| Always seemed a little like a weird assembler.
| DonHopkins wrote:
| This is a great article! I love his description of Forth as "a
| weird backwards lisp with no parentheses".
|
| Reading the source code of "WAForth" (Forth for WebAssembly)
| really helped me learn about how WebAssembly works deep down,
| from the ground up.
|
| It demonstrates the first step of what the article says about
| bootstrapping a Forth system, and it has some beautiful hand
| written WebAssembly code implementing the primitives and even the
| compiler and JavaScript interop plumbing. We discussed the
| possibility of developing a metacompiler in the reddit
| discussion.
|
| I posted this stuff about WAForth and a link to a reddit
| discussion with its author in the hn discussion of "Ten
| influential programming languages (2020)":
|
| https://news.ycombinator.com/item?id=34056735
|
| Yes, I agree FORTH should be probably be on the list, at least if
| the list was a few languages longer, for as influential as it's
| been.
|
| WAForth for WebAssembly is beautiful and modern!
|
| https://github.com/remko/waforth
|
| It's a lovingly crafted and hand written in well commented
| WebAssembly code, using Racket as a WebAssembly macro pre-
| processor.
|
| I learned so much about WebAssembly by reading this and the
| supporting JavaScript plumbing.
|
| The amazing thing is that the FORTH compiler dynamically compiles
| FORTH words into WebAssembly byte codes, and creates lots of tiny
| little WebAssembly modules dynamically that can call each other,
| by calling back to JavaScript to dynamically create and link
| modules, which it links together in the same memory and symbol
| address space on the fly! A real eye opener to me that it was
| possible to do that kind of stuff with dynamically generated
| WebAssembly code! It has many exciting and useful applications in
| other languages than FORTH, too.
|
| Lots more discussion and links in the reddit article.
|
| But here's the beef, jump right in:
|
| https://github.com/remko/waforth/blob/master/src/waforth.wat
|
| Reddit /r/Forth discussion of WAForth:
|
| https://www.reddit.com/r/Forth/comments/zmb4eb/waforth_wasmb...
|
| remko:
|
| Author here
|
| If you can't be bothered to install VS Code, you can have a look
| at a standalone version of the example notebook (in a 26kB self-
| contained page).
|
| And if you're planning to go to FOSDEM 2023, come say hi: I'll be
| giving a talk there on WebAssembly and Forth in the Declarative
| and Minimalistic Computing devroom.
|
| DonHopkins:
|
| I really love your tour-de-force design and implementation of
| WAForth, and I have learned a lot about WebAssembly by reading
| it. Never before have I seen such beautiful meticulously hand
| written and commented WebAssembly code.
|
| Especially the compiler and runtime plumbing you've implemented
| that dynamically assembles bytecode and creates WebAssembly
| modules for every FORTH word definition, by calling back to
| JavaScript code that pulls the binary bytecode of compiled FORTH
| words out of memory and creates a new module with it pointing to
| the same function table and memory.
|
| WebAssembly is a well designed open standard that's taking over
| the world in a good way, and it also runs efficiently not just in
| most browsers and mobile smartphones and pads, but also on the
| desktop, servers, cloud edge nodes, and embedded devices. And
| those are perfect target environments for FORTH!
|
| What you've done with FORTH and WebAssembly is original,
| brilliant, audacious, and eye-opening!
|
| I'd read the WebAssembly spec before, and used and studied the
| Unity3D WebAssembly runtime and compiler to integrate Unity3D
| with JavaScript, and I also studied the AssemblyScript subset of
| TypeScript targeting WebAssembly and its runtime, and also Aaron
| Turner's awesome wasmboy WebAssembly GameBoy emulator .
|
| I first saw your project a few years ago and linked to it in this
| Hacker News discussion about Thoughts on Forth Programming
| because I thought it was cool, but it's come a long way in three
| years, and I'm glad I finally took the time to read some of your
| code, which was well worth the investment of time.
|
| Until reading your code, I didn't grasp that it was possible to
| integrate WebAssembly with JavaScript like that, and use it to
| dynamically generate code the way you have!
|
| Also, the way you used Racket as a macro assembler for
| WebAssembly was a practical and beautiful solution to the
| difficult problem of writing maintainable WebAssembly code by
| hand.
|
| Even for people not planning on using FORTH, WAForth is an
| enlightening and useful example for learning about WebAssembly
| and its runtime, and a solid proof of concept that it's possible
| to dynamically generate and run WebAssembly code on the fly, and
| integrate a whole bunch of tiny little WebAssembly modules
| together.
|
| Playing with and reading through your well commented code has
| really helped me understand WebAssembly and TypeScript and the
| surface between them at a much deeper level. Thank you for
| implementing and sharing it, and continuing to improve it too!
|
| remco:
|
| Wow, thanks a lot, I really appreciate that! It makes me very
| happy that I was able to get someone to learn something about
| WebAssembly by reading the source code, which is exactly what I
| was going for.
|
| [More links and discussion of WAForth, WebAssembly, and Forth
| Metacompilers:]
|
| https://www.reddit.com/r/Forth/comments/zmb4eb/waforth_wasmb...
| alexwennerberg wrote:
| Here are some great resources on Forth:
|
| http://www.call-with-current-continuation.org/articles/forth...
|
| https://git.sr.ht/~vdupras/duskos/tree/master/fs/doc/design/...
|
| https://www.youtube.com/watch?v=9TJuOwy4aGA
|
| Let me quote the first essay at length:
|
| > Writing "good" Forth code is very hard, or at least what I
| consider good Forth code, which may be different from what others
| consider good. Forth has been called a "write only language", but
| only because it requires an additional effort to simplify your
| code to a point where everything becomes obvious. This is an art,
| it is a moment of transcendence, which I don't claim to have ever
| reached, but sometimes I get a glimpse of it, an inkling of the
| fact that if I would work very hard on this, it will become so
| simple, so heavily factored, using such obvious and clear names
| that everything just falls into place. It is this moment that
| programmers experience every once in a while, where a short,
| simple piece of code just does what it is supposed to do, without
| any extra baggage, easily understandable. To achieve it in Forth
| is much harder, it may require more time, many rewrites, but the
| results are even more satisfying, as the result is smaller, much
| simpler, fully self-contained and not burdened by code that you
| can not trust. When you forget about that urge of productivity,
| which has become the moloch we sacrifice our children to, you may
| be able to achieve that particular thing that is called quality,
| elegance, art. It may take a lifetime, you may never reach that
| far, but still you should strive for it.
|
| > Or you remain a cog in the machine, developing on absurdly
| convoluted "software stacks", using inadequate and brittle
| languages, connect barely matching interfaces to only small parts
| of too many bloated libraries, using insanely sophisticated tools
| of incomprehensble complexity and trot on, asking yourself why
| everything sucks and why the last time you actually enjoyed
| programming and were you could be genuinely and deservedly proud
| on what you accomplished, was when you were a kid...
|
| It seems to me that a lot of ideas in mainstream computing are
| pointing towards dead ends, and the practice of software
| engineering is either stagnating or in decay. Maybe it's time for
| a more radical reconsideration of how we build software, and time
| to give Forth (or something like it) a more serious look.
___________________________________________________________________
(page generated 2023-01-13 23:00 UTC)