9 ms·
Easy Forth (2015)
- 7thaccount 1y agoThis has showed up here a few times before (example): https://news.ycombinator.com/item?id=10634918 https://news.ycombinator.com/item?id=10634918 I'm always interested in hearing people's reactions to Forth though and every now and then you get a cool new story on these threads, so I'm not complaining.
- s-macke 1y agoRight. And once again, you’ll also notice that no one is actually coding anything useful in Forth.
- pjmlp 1y agoUnfortunately we aren't in the 1980's glory days of Forth in 8 bit home computers, and many students don't know what HP-48GX stands for.
- PaulHoule 1y agoBASIC, especially the Microsoft dialect, became the dominant language for microcomputers because it would fit in a tiny space, e.g. 4k. For that matter it was big in the minicomputer age because it was used in multitasking systems that weren't that big. Circa 1980 my high school had a PDP-8 which had three terminals and could run a three user BASIC with just 32k 12 bit words. There weren't a lot of languages which would fit in a tiny space, but FORTH was one of them. Like LISP it's a language where you can (1) implement the language without any kind of recursive parser and (2) write control structures in the language itself because each "word" in forth has both a run-time and compile-time interpretation.
- pjmlp 1y agoOriginal BASIC did not fit into tiny space, that is why while Dartmouth BASIC always compiled into machine code before execution, everyone that learnt BASIC in 8 bit systems thinks it was originally interpreted and compilers only came later, which was the compromise to make it fit into a few KB. Jupiter ACE had its followers, and it was common to see ads on Your Sinclair and similar magazines for ROM replacements using Forth instead of BASIC.
- PaulHoule 1y agoThat PDP-8 BASIC was a miracle of shoehorning as was everything else on the PDP-8. My favorite minicomputer BASIC that I got to use was on RSTS/E on the PDP-11 which had split 64k address spaces for code and data and used fairly advanced compilation techniques. Roughly the RSTS/E experience was like having your own Apple ][ but with a hard drive and a little more oomph. I grew up in New Hampshire right next door to DEC's headquarters in Massachusetts and there were DEC minicomputers everywhere. Microsoft had a compiled BASIC (like run a compiler, not compile interactively like Microware's BASIC09) on CP/M for the Z-80 which was a much better compiler target than the popular 6502. I wrote a FORTH for the TRS-80 Color Computer using the OS-9 operating system which had maybe 2000-3000 lines of assembly code. FORTHs at the time often did block I/O directly to the disk but OS-9 had an API to access files that was pretty similar to Unix and my FORTH exposed that.
- kragen 1y agoRPL isn't Forth.
- pjmlp 1y agoTechnically you are right, in practice they are close enough in concepts, exploring stack based languages.
- kragen 1y agoI don't agree at all. They do have something in common: they're stack-based. But what they don't have in common is everything else. RPL is a dynamically-typed, bounds-checked, memory-safe, garbage-collected language similar to PostScript or Python. Forth is an untyped, unsafe language with arbitrary compile-time compilation and without even a heap, traditionally. It's the minimal veneer on top of assembly language to give it arbitrary compile-time metaprogramming and nested expressions and control structures. RPL doesn't even have compile-time metaprogramming at all. These two languages represent diametrically opposed approaches to programming language design.
- johnisgood 1y agoIn Factor though, they do. https://factorcode.org/ https://factorcode.org/
- 7thaccount 1y agoIt looks like it's still being developed, but I feel like the 0.1 version number hasn't changed in 10 years. What cool projects are people making?
- jinwoo68 1y agoIts version seems to be 0.100, not 0.1. And it was released last year: https://downloads.factorcode.org/releases/ https://downloads.factorcode.org/releases/
- johnisgood 1y agoCheck out the website (I linked it) for examples. Additionally, check basis/ and extra/ on https://github.com/factor/factor https://github.com/factor/factor. You will find A LOT of goodies there. They even have a framework for Discord bots using latest Discord API version, if you are into that. In any case, you really ought to check, there are too many things to list here. As someone else pointed out, it is 0.100 which was released not that long ago, and if you compile now, it is 0.101 anyways, but regardless of their versioning, it has been actively maintained ever since, they just did not bump the version for a long time.
- kibwen 1y agoEh, that's sort of like saying that nobody's coding anything useful in assembly language. Both the JVM and WASM are stack machines akin to Forth.
- kragen 1y agoThey're akin to Forth, but they're not Forth, and they're not implemented in Forth. By contrast, assembly language underpins most software people run today: perhaps it's written in Python, which is interpreted by CPython (using a bytecode which is considerably more Forthlike than Wasm, incidentally), which is written in C, which is usually compiled by GCC or LLVM to textual assembly language in order to generate the executable.
- kragen 1y agoWell, I don't know if this qualifies as useful, but I wrote a minimal roguelike in one page of Forth last year: https://asciinema.org/a/672405 https://asciinema.org/a/672405 vdupras is here in the comments today. He's written a self-hosting interactive operating system in Forth, including a FAT16 filesystem and everything, and a compiler for a useful subset of C: https://duskos.org/ https://duskos.org/
- bell-cot 1y agoIt's a cool little niche language. If you're neither interested in the coolness, nor its little niche - there's no need to be dismissive.
- s-macke 1y agoYes, my comment came across a bit harsh, and it’s fine to pick up a few negative karma points. But I keep seeing Forth posts every two weeks where everyone has just built yet another interpreter. Actually I did a few projects with Forth and I find it very cool: [0] https://github.com/s-macke/Forthly https://github.com/s-macke/Forthly [1] https://github.com/s-macke/starflight-reverse https://github.com/s-macke/starflight-reverse [2] https://s-macke.github.io/concepts-of-programming-languages/docs/04-Forth.html https://s-macke.github.io/concepts-of-programming-languages/...
- hobs 1y agoI like that way of thinking about it - now I want my negative karma points separated into their own bucket even if the final summary is presented out, I think its an interesting signal.
- bell-cot 1y agoDang & Co. doubtless see such things - but I'd bet it'll never be shown to regular users. Too little utility, vs. too tempting for a small minority to game in unhealthy ways.
- kragen 1y agoHeh, I see you linked StoneKnifeForth there!
- bxparks 1y agoAs I like to say: "C is a language that solves a million problems. Forth is a million languages that solve almost nothing." :-P I've been reading about Forth for 30-40 years. The dual stack is easy to understand. My problem is that I cannot see how control flow works in Forth, e.g. a simple if-then-else. I think that something as fundamental as an if-then-else should be obvious in a useful language. Heck, it's obvious in assembly language. But not in Forth.
- alexisread 1y agoHave you tried looking at Sectorforth? In most languages branching is a fundamental construct, it's created here (with comments) https://github.com/cesarblum/sectorforth/blob/master/examples/01-helloworld.f https://github.com/cesarblum/sectorforth/blob/master/example... Effectively IF compiles a 0= ie. If false and then a dummy target address. THEN (aka ENDIF) compiles the real target address over the dummy one, which is the address after THEN.
- kamaal 1y ago>>Forth is a million languages that solve almost nothing." :-P That brings us to the question, when it was invented and people did use it. What kind of problems were they solving with it?
- jonjacky 1y agoForth was invented around 1970 for controlling equipment in an astronomical observatory, running on a PDP-11, a 16-bit computer with up to 64 Kbytes of memory. Its heyday was the 1970s and 80s, when it was mostly used for small embedded systems on 8- or 16- bit processors with 8 kb -- 64 kb of memory. It was possible to run an entire Forth development system along with the application on these small targets without resorting to a bigger computer for cross-development. The usual alternative to Forth on those systems was assembly language.
- EasyMark 1y agoI was dropped into one such system after it failed after like 20 years and learned forth on the fly while managers breathed down my neck lol. Not a fun experience, and it took about 3X as long as I thought it would take, so Forth is not my favorite language, but I do see why it was useful at the time, and as an exercise in a way of thinking about languages rather than the mode I usually operate in c/c++/rust/little javascript
- soapdog 1y agoWhat about openfirmware?
- idatum 1y agoIf I can find a Forth for a new chip I'm learning, I use it as a way to explore the chip with a REPL. Micropython can be similar this way, but it's more constrictive.
- nmz 1y agoEvery single coder that uses chaining operators is using concatenative programming concepts. Shell piping is also a form of concatenative programming. Just because you don't specifically use forth does not mean forth is dead.
- kragen 1y agoShell piping isn't Forth.
- nmz 1y agoI didn't say it was forth, I said it was concatenative. Forth itself is hard to categorize since it refuses to standardize but we all know what it looks like, I'd say its a low level language/VM that manages everything through stacks (usually 2) there's forthlike languages and also concatenative languages. concatenative languages can be far removed from forth, I don't mean that the shell itself is concatenative, but only the shell pipelining aspect, which you can imitate in any forth. take an object and keep passing it to subsequent functions without popping that know s how to handle that object, that's a shell pipeline to me, all of unix can be passed the /dev/std* objects and they can all modify it and pass it along to the next function/program. https://concatenative.org/ https://concatenative.org/
- kragen 1y agoWell, I agree that Forth is concatenative (like stack languages in general), and so are shell pipelines. But you said, "Just because you don't specifically use forth does not mean forth is dead," and unless I misread your intent, you included your shell-scripting point on the theory that it was relevant to that question: whether or not Forth was dead. The implication seemed to be that, as long as people were using shell pipelines, Forth wouldn't be dead. But that's wrong; Forth could be totally dead while other forms of concatenative programming were alive and well.
- fallat 1y agoI've done: - Accounting system used for real life taxation in my self-employment: https://gist.github.com/lf94/fcdf41776e14fcc289bac652ea8cb4f8 https://gist.github.com/lf94/fcdf41776e14fcc289bac652ea8cb4f... - Software shader rasterizer for image generation: https://gist.github.com/lf94/f74c927e59b4010d9de001fa2ba87910 https://gist.github.com/lf94/f74c927e59b4010d9de001fa2ba8791... - PS4 controller macro system via an RP2040 & ZeptoForth: https://youtu.be/exayMSQfyqk https://youtu.be/exayMSQfyqk, https://gist.github.com/lf94/2d64917728594516dee6caf7667d2e47 https://gist.github.com/lf94/2d64917728594516dee6caf7667d2e4... - Iambic paddle tap interpreter for morse code practice (once again running RP2040 & ZeptoForth): https://gist.github.com/lf94/95516fa39c3339b685e0fde10f17c97a https://gist.github.com/lf94/95516fa39c3339b685e0fde10f17c97... - Fixed-point number library to run computations on pretty much any CPU in existence: https://gist.github.com/lf94/ca622ebac14d48915ea976f665f832cf https://gist.github.com/lf94/ca622ebac14d48915ea976f665f832c... - 1-bit music synthesis experiments to learn how to make music with a beeper (useful in products, such as Tile): https://www.youtube.com/watch?v=IjTihhFG03o https://www.youtube.com/watch?v=IjTihhFG03o, https://www.youtube.com/watch?v=_6f8PURcPEE https://www.youtube.com/watch?v=_6f8PURcPEE And that's all in my rare spare time. Forth really shines in microcontroller or esoteric computation machines, but further more, people don't realize their C compilers are billions of dollars of development, and they'd never be able to do it in the first place. A Forth on the other hand can be developed in a month (I'm being honest-to-god realistic here. A lot of people would say "a weekend", but let's be real, anything useful will be more than a weekend. I'm trying to convince you this isn't bullshit :)). If you have any more questions let me know. I was bit by Forth about 2 years ago but had read about it long ago when I was like 16 and passed it off as too hard. It's the same shit as when FP took off: it's a different mental model, so it will take time to morph your mind. Edit: read the larger comment below, and they are totally correct: > most Forth tutorials today are written by people who don't really know Forth One day I might write a small Forth novella teaching how to actually think about Forth programs. In these 2 years I've had to just practice and write programs to see common patterns or idioms - kind of exactly like when I was learning Haskell years ago.
- kragen 1y agoThis is pretty impressive! I don't think it takes billions of dollars in development to write a C compiler, but it does seem like everyone's first C compiler does take at least a year, so US$100k is a good ballpark. I agree that Forth is about an order of magnitude easier. You probably shouldn't consider https://github.com/kragen/stoneknifeforth https://github.com/kragen/stoneknifeforth to actually be a Forth (it's not interactive and doesn't have immediate words) but it did compile itself into a working ELF executable and it did take me almost exactly a month to write (October 02008, specifically). I could probably write a real Forth now in a month. I would be very excited to read your small Forth novella. Or your small Haskell novella.
- adastra22 1y agoBitcoin’s scripting / smart contracting language is Forth. Were there anything in the crypto space is actually solving a problem is up to your own biases and prejudices. But if you pick one thing as actually trying to solve a real problem, payments over lightning is probably that. Lightning, at its core, is a state machine composed of Forth spend scripts.
- tromp 1y agoWhile bitcoin script is a stack language, it doesn't really qualify as Forth as it doesn't allow you to define any new words.
- kragen 1y agoNo, Bitcoin Script is not Forth. Bitcoin Script is designed to guarantee termination, so no Turing-complete programming language was permitted. Like BPF, Bitcoin Script doesn't have subroutine definitions or backward jumps, so each opcode executes at most once. This is like saying JSON is C++.
- deleted 1y ago[deleted]
- thomascountz 1y agoThis is a great resource and running in the browser is great for fast feedback. Thanks for sharing! I just started learning Forth a month or so ago, and I found this video from Andreas Wagner[1] fun to watch. If anyone goes through OP's book and find yourself wanting to see Forth in action, I recommend the video. [1]: https://youtu.be/mvrE2ZGe-rs https://youtu.be/mvrE2ZGe-rs
- StilesCrisis 1y agoThe automatic scrolling makes the page basically unusable on Safari.
- jillesvangurp 1y agoSame on Firefox.
- bell-cot 1y agoWith js disabled, it's perfectly usable in FF.
- cl3misch 1y agoIt's not usable because the Forth interpreter doesn't work for the interactive examples.
- susam 1y agoGlad to see Forth on HN today! For anyone who likes playing with small experimental projects, I once made a minimal, esoteric canvas colouring language inspired by Forth and Tixy: https://susam.net/fxyt.html https://susam.net/fxyt.html
- glimshe 1y agoVery cool! It would be even better if you had a Library of preset demo programs for people to hack and see the power of your tool Edit: I saw them on GitHub. But I've also seen some tools that put them in the main interface. That would be an improvement IMHO. Great work!
- susam 1y agoThank you! Glad you liked it! There are a few demos at the bottom of the help screen, which can be invoked by typing '?'. You are right that that they could be better linked directly from the main interface. In any case, here are the direct links to the demos available in the help screen: https://susam.net/fxyt.html#XYxTN1srN255pTN1sqD https://susam.net/fxyt.html#XYxTN1srN255pTN1sqD https://susam.net/fxyt.html#XYaTN1srN255pTN1sqN0 https://susam.net/fxyt.html#XYaTN1srN255pTN1sqN0 https://susam.net/fxyt.html#XYoTN1srN255pTN1sqDN0S https://susam.net/fxyt.html#XYoTN1srN255pTN1sqDN0S https://susam.net/fxyt.html#XYpTN1srN255pTN1sqD https://susam.net/fxyt.html#XYpTN1srN255pTN1sqD https://susam.net/fxyt.html#XYN256sTdrD https://susam.net/fxyt.html#XYN256sTdrD Community demos are available here: https://susam.github.io/fxyt/demo.html https://susam.github.io/fxyt/demo.html
- nottwo 1y agoThis is fun! I was going to try to draw a circle but was missing sin/sqrt. Then I thought of using a lookup table but got stumped. Do you have any pointers for drawing a circle? I'm looking at demo #4 (https://susam.net/fxyt.html#XYpTN1srN255pTN1sqD https://susam.net/fxyt.html#XYpTN1srN255pTN1sqD) to see where the circular shapes are coming from. Have you seen Forth Haiku? https://forthsalon.appspot.com/ https://forthsalon.appspot.com/
- 1y ago
- mbfg 1y ago>> The thing that separates Forth from most other languages is its use of the stack. In Forth, everything revolves around the stack I mean, that's pretty much every language. The main difference is that the programmer's access to it is unconstrained by things like method call definitions.
- vdupras 1y agoUnlike most languages, Forth has two stacks. It sounds trivial, but it changes many things. It allows for a leaner call convention. With a single stack, every function call has to "shovel forward" its arguments over the function return address, where Forth "glides" through its arguments, making function calls significantly lighter.
- addaon 1y ago> Unlike most languages, Forth has two stacks. Like Forth, Ada has two stacks. Unlike Forth, which uses two stacks to simplify the language, Ada uses two stacks to complexify the language. This generalizes to other language features.
- kragen 1y agoAda's auxiliary stack is used to permit the returning of runtime-variable-sized objects from subroutines, which is also a thing you can use the operand stack for in most Forths.
- kragen 1y agoMost languages don't have an explicit stack, and even their implicit stack is only for subroutine calls. If you're not making subroutine calls, your compiled code might not access the stack at all. So, for example, here's the strlcpy function from OpenBSD, lightly edited: size_t strlcpy (char *dst, const char *src, size_t siz) { register char *d = dst; register const char *s = src; register size_t n = siz; if (n != 0 && --n != 0) { do { if ((*d++ = *s++) == 0) break; } while (--n != 0); } if (n == 0) { if (siz != 0) *d = '\0'; while (*s++) ; } return(s - src - 1); } GCC 12.2.0 compiles this to the following 18 ARM instructions, with -mcpu=cortex-a53 -Os -S: .text .align 2 .global strlcpy .syntax unified .arm .type strlcpy, %function strlcpy: @ args = 0, pretend = 0, frame = 0 @ frame_needed = 0, uses_anonymous_args = 0 @ link register save eliminated. mov r3, r1 cmp r2, #0 beq .L6 .L14: subs r2, r2, #1 beq .L3 ldrb ip, [r3], #1 @ zero_extendqisi2 strb ip, [r0], #1 cmp ip, #0 bne .L14 .L4: sub r0, r3, r1 sub r0, r0, #1 bx lr .L3: mov r2, #0 strb r2, [r0] .L6: ldrb r2, [r3], #1 @ zero_extendqisi2 cmp r2, #0 bne .L6 b .L4 .size strlcpy, .-strlcpy If you're not familiar with ARM assembly, I'll tell you that nothing in this entire function uses the stack at all, which is possible because strlcpy doesn't call any other functions (it's a so-called "leaf subroutine", also known as a "leaf function") and because ARM, like most RISCs, puts the subroutine return address in a register (lr) instead of on the stack like amd64, or in the called subroutine like the PDP-8, which doesn't have a stack at all. And the calling convention puts arguments and return values in registers as well. So the function can just move data around between memory and registers and decrement its loop counter and increment its pointers without ever touching the stack. FORTRAN up to FORTRAN 77 didn't support recursion, including indirect recursion, so that you could implement it without a stack. By contrast, in Forth, instead of registers you use the operand stack. For loop counters you use the return stack. Sometimes you can use the operand stack instead of variables as well, although I think it's usually a better idea to use variables, especially when you're starting to learn Forth—it's much easier for beginners to get into trouble by trying too hard to use the stack instead of variables than to get into trouble by trying too hard to use variables instead of the stack.
- kragen 1y agoForth is very enjoyable, and it's always exciting to see someone new discovering it, but it has three big problems. The first is a technical problem: the forte of Forth is self-hosted developer tooling in restricted environments: say, under 256KiB of RAM, no SSD, under 1 MIPS, under 10 megabytes of hard disk or maybe just a floppy. In that kind of environment, you can't really afford to duplicate mechanism very much, and programmers have to adapt themselves to it. So you end up using the same mechanism for fairly disparate purposes, with the attendant compromises. But the results were amazing: on an 8080 with 64KiB of RAM and CP/M you could run F83, which gave you virtual memory, multithreading, a somewhat clumsy WYSIWYG screen editor, a compiler for a language with recursion and structured control flow, an assembler, and a CLI and scripting language for your application. Those environments almost don't exist today. But if you're programming, say, an MSP430 (consider as paradigmatic https://www.digikey.com/en/products/detail/texas-instruments/MSP430F249TPMR/1790141 https://www.digikey.com/en/products/detail/texas-instruments...), you have only 2KiB of RAM, and you could use Mecrisp-Stellaris https://mecrisp.sourceforge.net/ https://mecrisp.sourceforge.net/ That chip's resources are pretty limited. In a money economy, we measure resources in money; the reason to use a chip with limited resources is to avoid spending money, or to spend less money. That chip costs US$7.40. For US$5.59 you could instead get https://www.digikey.com/en/products/detail/stmicroelectronics/STM32F412REY6TR/7313111 https://www.digikey.com/en/products/detail/stmicroelectronic...: 100 megahertz, 512MiB of flash, 256KiB of RAM, 50 GPIOs, CAN bus, LINbus, SD/MMC, and so on. And according to Table 33 of https://www.st.com/content/ccc/resource/technical/document/datasheet/15/90/07/b3/8c/10/4e/95/DM00213872.pdf/files/DM00213872.pdf/jcr:content/translations/en.DM00213872.pdf https://www.st.com/content/ccc/resource/technical/document/d... it typically uses 1.8μA in standby mode at 25° at 1.7V. That's more than the MSP430's headline 0.1μA from https://www.ti.com/lit/ds/symlink/msp430f248.pdf https://www.ti.com/lit/ds/symlink/msp430f248.pdf but it's still low enough for many purposes. (A 220mAh CR2032 coin cell could theoretically supply 1.8μA for 13 years, but only has a shelf life of about 10 years, so the STM32 uses less than the battery's self-discharge current.) That is to say, the niche for such small computers is small and rapidly shrinking. Also, while the microcontroller might have only 2KiB of RAM, the keyboard and screen you use to program it are almost certainly connected to a computer with a million times more RAM and a CPU that runs a thousand times faster. So you could just program it in C or C++ or Rust and run your slow and bloated compiler on the faster computer, which will generate more efficient code for the microcontroller. The cases where you have to build the code on the target device itself are few and far between. Forth was designed to make easy things easy and hard things possible. The second problem is a social one: as a result of the first problem, the people who used Forth for that have mostly fled to greener pastures. The Forth community today consists mostly of Forth beginners who are looking for an artificial challenge: instead of making hard things possible, they want to make easy things hard. There are a few oldtimers left who keep using Forth because they've been using it since it did make hard things possible. But even those oldtimers are a different population from Forth's user base in its heyday, most of whom switched to C or VHDL. Most of us have never written a real application in Forth, and we've never had the religious-conversion experience where Forth made it possible to write something we couldn't have written without Forth. The third problem is also a social one: as a result of the second problem, most Forth tutorials today are written by people who don't really know Forth. I've only briefly skimmed this tutorial, but it seems to be an example of this. For example, I see that it doesn't explain immediate words, much less when to not use immediate words. (If it's ever easier to write something in Forth than in C, it's probably because you can define immediate words, thus extending the language into a DSL for your application in ways that are out of reach of the C preprocessor.) And it doesn't talk about string handling at all, not even the word type, even though string handling is one of the things that Forth beginners stumble over most when they start using Forth (because it doesn't inherently have a heap). So, I hope the author continues to learn Forth, and I hope they extend their tutorial to cover more aspects of it.
- codr7 1y agoI think Forth is well worth learning just for the mind expansion. It also makes a nice starting point for building your own interpreters / designing your own languages.
- georgestagg 1y agoFor those who haven’t seen it before, Jones Forth is a wonderful implementation written in literate programming style. It’s well worth reading through, even if you don’t know any assembly. [1] https://github.com/nornagon/jonesforth/blob/master/jonesforth.S https://github.com/nornagon/jonesforth/blob/master/jonesfort...
- fmajid 1y agoLeo Brodie's book "Thinking Forth" is now free: https://thinking-forth.sourceforge.net/ https://thinking-forth.sourceforge.net/