5 ms·
As I wrote here a month ago, I worry that the stack manipulation operators can be a cognitive danger to people who are newly learning Forth: https://news.ycombi
by kragen 6y ago
As I wrote here a month ago, I worry that the stack manipulation operators can be a cognitive danger to people who are newly learning Forth: https://news.ycombinator.com/item?id=26884231 https://news.ycombinator.com/item?id=26884231
It's cool to be able to write : CAB ( c a b -- ) - SWAP / ; but that very coolness can easily lead you into giving up on Forth. It's natural to work to do everything on the stacks, since you can, but that's not a practical way to program; this function is fine, but in sufficiently complicated cases, you end up with unreadable code where you're struggling to play mental chess with the stack. And then you sigh, decide that Forth is for people smarter than you (which is reinforced by some of the self-aggrandizing nonsense around Forth), and you go back to Python.
Maybe a better way to teach Forth would be to teach people about VARIABLEs or VALUEs first, so they can write 0 VALUE C : CAB TO C - C / ; or possibly VARIABLE C : CAB C ! - C @ / ; and then only later introduce SWAP, DUP, and the rest as shortcuts. VALUE or VARIABLE is simpler than stack manipulation operators — with either VALUE and TO, or with @, !, and VARIABLE, you can write every possible stack manipulation operator. So it's less to learn than the whole zoo of DUP, DROP, SWAP, 2DUP, 2DROP, OVER, >R, R>, TUCK, NIP, 2SWAP, ROT, 2OVER, -ROT, R@, and RDROP.
But then, later on — look! With this new SWAP shortcut, you can write this CAB function without a variable! And you probably should. But if you ever find yourself writing code with -ROT you probably should seriously consider rewriting it.
Of course mutable variables are themselves bug-prone — you can forget to set them on some control path, for example, or forget to redeclare them and so accidentally share a variable with some subroutine you're calling — but in my experience these are much less serious pitfalls than ending up with a bunch of @ ROT + SWAP and OVER + SWAP 0 >R.
On the other hand, I don't have a great track record with either writing difficult programs in Forth or teaching people things. So I might be off base here.
- retrac 6y agoIt's Forth tradition to make your own slightly incompatible Forth. Honouring that tradition, I suggest an extension to the VM. Either with a few registers, or a second stack. For pointers. That way, memory access to a single or handful of variables becomes a single word. Add things like dereference-and-increment load/store primitives and it becomes very C-like, and a rather nice target for compilers. (I can't take credit for this idea: https://www.complang.tuwien.ac.at/anton/euroforth/ef08/papers/pelc.pdf https://www.complang.tuwien.ac.at/anton/euroforth/ef08/paper... "Updating the Forth Virtual Machine" by Stephen Pelc for EuroForth 2008, which is probably also not the first time someone suggested this.)
- kragen 6y agoInteresting! Some way to distinguish pointers from integers is kind of a sine qua non for memory safety; I've been kind of thinking along the lines of Smalltalk object descriptors, where each memory allocation is in a "segment" of its own, into which you can index with integers. That would save you from debugging segfaults. (But then, so does Hindley-Milner typing, and that doesn't cost a bounds-check on every memory access.) The part that so far puzzles me is how to do this without losing too much of Forth's simplicity. Pelc's proposal is to add scratch/index registers A and B (notably, not segregating integers from pointers) and base pointers X or LP (the frame pointer) and Y or UP (thread-local data pointer). His motivation seems to be primarily efficiency and clarity rather than memory-safety. Probably you want at least LP and UP to be callee-saved. If you make LP into an additional stack rather than just a register, then you save yourself a Y> >R on entry to subroutines that allocate stack frames, and half of a R> >Y on exit. (The CPU still has to do the same amount of work, but plausibly you spend less on op dispatch overhead and code space.) Doing the same with the A and B scratch/index registers might make sense, or you could just treat them as caller-saved. At some point restoring individual shallow-bound variables one by one stops being a good tradeoff, and it starts making more sense to put them in a stack frame or instance-variable vector and access them by indexing off a base pointer. Adding a small number of local-variable registers like this is kind of a pain for hand-programming, though, as opposed to as a compiler target. Instead of having to remember whether the quantity two levels down in the stack is the voltage or the current, you have to remember whether the A register is currently the voltage or the current. Much easier to say 0 VALUE VOLTAGE 0 VALUE CURRENT, or maybe VARIABLE V VARIABLE I, and then your code is always explicit about which one it's expecting. So I doubt Pelc's proposal can be justified on the grounds of clarity, rendering it merely a speed hack.
- astrobe_ 6y agoA strange game. The only winning move is not to play. If you want memory safety guarantees from the compiler, don't use Forth. The paradigm in Forth is "correct by design", "fail-fast" and to fight complexity unguibus et rostro. The paradigm in Forth is to increase the skills of the programmer rather than to increase the skills of the compiler. That's why it is a wonderful language for advanced training, IMO.
- kragen 6y ago> 0 VALUE C : CAB TO C - C / ; or possibly VARIABLE C : CAB C ! - C @ / ; This is wrong. It should be 0 VALUE C 0 VALUE D : CAB - TO D TO C D C / ; or VARIABLE C VARIABLE D : CAB - D ! C ! D @ C @ / ; Incidentally, that's exactly how I implemented DUP, DROP, and SWAP in StoneKnifeForth: var X # 0 var Y # 0 ( temp vars ) : dup X ! X @ X @ ; : pop X ! ; : xchg X ! Y ! X @ Y @ ; ( SWAP/exch ) (The weird names are because only the first byte of the name is significant in SKF.)
- zozbot234 6y agoI'm not sure that I agree. I think stack manipulation is the most elegant idiom available given the constraints that FORTH is working with. The values and variables features seem to introduce unwanted coupling, and complicate the language's underlying model. Of course it goes without saying that most FORTH code should not contain low-level stack manipulation. But most useful stack operations can be abstracted quite readily, and this kind of abstraction is a meaningful part of FORTH idiomatic style.
- kragen 6y agoIt's an interesting point of view. When you say "The values and variables features...introduce unwanted coupling, and complicate the...model," do you mean implementing them in a Forth, or using them in your application code? I don't think I've ever seen either a Forth or a Forth program that didn't use variables, so I think the model is unavoidably going to get that complicated for any program more complicated than (-b±√(b²-4ac))/2a. (You could replace VARIABLEs (and CONSTANTs) with VALUEs, and having both is clearly unnecessary.) As for the coupling, do you mean that if one subroutine writes a variable and another reads it, then that couples them? That's true, but I was talking about using a variable inside a single subroutine, as an alternative to stack manipulations. That's not the only good way to use variables in Forth, and it's not always better than stack manipulations, but I'm saying it's a reliable way to get over the "I'm too dumb for Forth" hump that most of us hit at some point. > But most useful stack operations can be abstracted quite readily, and this kind of abstraction is a meaningful part of FORTH idiomatic style. Yes, that is surely true. Is there some Forth code you think of as exemplary that I could learn from?