Live data from Hacker News

Thoughts on Forth Programming

call-with-current-continuation.org

31–40 of 97 posts

Re: Thoughts on Forth Programming

#31

> One such property is the use of reverse polish notation and the lack or eschewal of local variables. What do you gain by eschewing local variables in favor of reverse polish notation that throws values onto a stack?

There are two hard problems in computer science: cache invalidation, naming things, and off-by-one errors. Eschewing local variables means you have fewer things to name.

Re: Thoughts on Forth Programming

#32

I feel like, from the "individualism" perspective, Forth is really a tool for hackers. Hackers usually work alone or in very small groups, and the tools they need are usually handcrafted. They do not need corporate-level software, and long do they strive to be left alone, without being bothered.

What kind of projects are generally done by individuals even inside companies? I'm thinking maybe some simple device drivers but could be others.

Re: Thoughts on Forth Programming

#33
post #10

Earlier quoted context omitted.

This can work, but it's incredibly tedious and not worth the trouble. Writing in Forth is like writing in pure CIL or JVM bytecode (they're both stack machines like Forth). Sure, you can do it, but if it was so rewarding, people wouldn't be using Java or C#.

That's not true at all. I know multiple people who are incredibly productive in Forth environments, though I don't use it myself. Pretending that it's like writing in JVM bytecode (despite some overlap between the two) is completely unfounded. Your claim that C# and Java won based on merits is similarly unfounded: they won because they have major corporate backing. There are countless languages before and after that…

C# and Java won over CIL and bytecode because of corporate backing? You're ridiculous and can't even read. I didn't compare Java to Forth, I compared Java to pure JVM bytecode, both of which are backed by the same corporations.

As to people being productive in Forth, that's singular cases and/or fairytales. Let's see some real, large, valuable software written in Forth. Browsers, web frameworks, GUI frameworks, text editors for God's sake. Can't think of any?

Re: Thoughts on Forth Programming

#34
post #4

This quote stood out as sounding quite far-fetched: "on many CPUs the interpreter consists of two or three machine instructions". Can someone point to an example?

The previous reply explained things pretty much - many implementations have "Words" (i.e. functions) be invoked by just jumping from one to the next.

If you have some spare time reading this implementation is very enlightening:

https://github.com/nornagon/jonesforth/blob/master/jonesfort...

Though I appreciate it is a little low-level it explains things very well, and is readable if you have only a hazy grasp of assembly language.

The specific documentation I was thinking about is this section on calling sequential functions:

https://github.com/nornagon/jonesforth/blob/4f853252f715132e...

Re: Thoughts on Forth Programming

#35
post #2

> In Forth there is only memory - word and byte-sized cells of storage in memory, and stacks. You step down on the level of assembly language which may sound daunting, yet gives you full control over every aspect of memory layout. Other than in niche or pet projects, can this even work? If you deal with any sort of multibyte data like Unicode, doesn’t this become way harder? Or do you just punt and use ascii code pag…

On a new OS, forth often has a "first mover" advantage. E.g. some of the first apps for MacOS were developed in forth. It's also useful when limited resources are available. OO forth is quite pleasant to work in, if you really want to.

That said, I wish somebody would have created a statically typed forth.

Re: Thoughts on Forth Programming

#36

Can you please give some examples on those who are extremely productive using FORTH? I'm intrigued. I think FORTH could be very useful to someone who prefers to work alone or in small group, but I'm not sure how do they use it in daily life.

Mitch Bradley. He wrote the firmware for Sun machines, made an IEEE standard of it, open sourced it for OLPC. Highly productive guy.

Interview with him seems to have disappeared from the web? http://mitchbradley.blogspot.com/2010/08/open-firmware-and-o...

Re: Thoughts on Forth Programming

#37
post #4

This quote stood out as sounding quite far-fetched: "on many CPUs the interpreter consists of two or three machine instructions". Can someone point to an example?

FORTH has both an "inner interpreter" and an "outer interpreter" (aka the compiler, but that also works interactively like an interpreter).

The inner interpreter is the thing that threads between words, and is typically an assembly function named "NEXT", which can be just a few instructions. Here is the FIG-FORTH 6502 implementation of "NEXT" (which is a bunch of instructions since the 6502 has 8 bit registers and simple addressing modes):

https://ksquiggle.neocities.org/ff6502.htm

    0122  0244            ;
    0123  0244            ;     NEXT is the address interpreter that moves from
    0124  0244            ;     machine level word to word.
    0125  0244            ;
    0126  0244  A0 01     NEXT   LDY #1
    0127  0246  B1 AE            LDA (IP),Y    Fetch code field address pointed
    0128  0248  85 B2            STA W+1       to by IP
    0129  024A  88               DEY
    0130  024B  B1 AE            LDA (IP),Y
    0131  024D  85 B1            STA W
    0132  024F  20 6F 02         JSR TRACE   Remove this when all is well
    0133  0252  18               CLC           Increment IP by two
    0134  0253  A5 AE            LDA IP
    0135  0255  69 02            ADC #2
    0136  0257  85 AE            STA IP
    0137  0259  90 02            BCC L54
    0138  025B  E6 AF            INC IP+1
    0139  025D  4C B0 00  L54    JMP W-1     Jump to an indirect jump (W)
(This is actually self-modifying code that write the indirect address to jump to into W, the operand of an indirect JMP instruction at W-1, then does an absolute JMP to W-1 to jump indirect through W.)

Some CPUs can implement "NEXT" in one or only a few instructions, and FORTH implementations can use "indirect threading" (like the above 6502 implementation that indirectly jumps through each word's CFA (Code Field Address)) or "direct threading" where the word pointers refer directly to code, or they can even compile words directly to machine language instructions instead of threaded pointers (so there's effectively no inner interpreter, just direct machine language calls), and in that case they can even inline "NEXT" at the end of every word definition for speed, instead of jumping to a global "NEXT" implementation (subroutine threaded machine code).

https://en.wikipedia.org/wiki/Threaded_code#Threading_models

And there are other possible variations and hybrid combinations, like "subroutine threading" or "token threading", which you might want to use to implement FORTH systems in C or other higher level languages, using function pointers (like CForth) or switch statement tokens for built-in primitives (like machine-independent OpenFirmware byte code):

https://github.com/MitchBradley/cforth

https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Dir...

The outer interpreter is the parser and compiler, which is written in FORTH, and includes a bunch of words, but is extremely simple (and extremely extensible) compared to other language interpreters. The outer interpreter can be in interpret or compile mode (controlled by a variable called "STATE"): when you're typing expressions interactively in interpret state, it executes them immediately, but when you start a word definition (with ":") it goes into compile state and compiles the words instead of executing them.

Except that in compile mode it does execute specially marked "immediate" words, with which you can implement control flow and macros. There are immediate words [ and ] that switch between compile/interpret states, so in the middle of a word definition you can pop out into interpret mode and execute arbitrary computations (like macros or meta programming), then pop back into compile mode. For example you could compute a number, and then compile it as an inline literal!

And then there's , which are word defining words, that let you do certain kinds of meta-programming, implement your own domain specific languages, data types, object systems, and extend the FORTH interpreter and compiler in FORTH.

http://www.forth.org/svfig/Len/definwds.htm

>It has been said that one does not write a program in Forth. Rather, one extends Forth to make a new language specifically designed for the application at hand. An important part of this process is the defining word, by which it is possible to combine a data structure with an action to create multiple instances that differ only in detail. One thinks of a cookie-cutter; all the cookies are the same shape but have different-colored icing.

Re: Thoughts on Forth Programming

#38

I have been looking at Forth programming and one thing I would like to figure out is how to use gforth under Linux in a pipeline of shell and awk. I haven't yet found a practical use for Forth in this environment yet...

Maybe Retro forth is worth looking into in this regard (I haven't really used it myself, but the conception and minimalism around it are really appealing):

http://forthworks.com/retro

http://forthworks.com/retro/book.html#using-in-a-pipe

Re: Thoughts on Forth Programming

#39

Earlier quoted context omitted.

This can work, but it's incredibly tedious and not worth the trouble. Writing in Forth is like writing in pure CIL or JVM bytecode (they're both stack machines like Forth). Sure, you can do it, but if it was so rewarding, people wouldn't be using Java or C#.

It's not like pure CIL or JVM bytecode when the language can be extended. The process is to make what you need and only then write the program. It's remarkable how fast you can move up language levels once you grok it.

This is hard to believe, as in this thread and everywhere, the only software used to tout Forth is low-level stuff. Even if Forth was able to be squeezed into a high-level language, it doesn't seem to have any advantage, or, in fact any differences from existing ones. What would a high-level Forth bring to the table that Java or e.g. Clojure don't have?

Re: Thoughts on Forth Programming

#40
post #32

I feel like, from the "individualism" perspective, Forth is really a tool for hackers. Hackers usually work alone or in very small groups, and the tools they need are usually handcrafted. They do not need corporate-level software, and long do they strive to be left alone, without being bothered.

What kind of projects are generally done by individuals even inside companies? I'm thinking maybe some simple device drivers but could be others.

Proofs of concept/early demos. Swift at Apple was the work of one person initially.
Post reply on HN