News for hackers. Stuff that matters.
Re:Stem, a stack-based language with metaprogramming and a C... by James_K on Saturday January 27, 2024 @09:29AM (#39155827)
I've never really understood stack based languages. Getting rid of variables only seems make things harder, and you always inevitably need some kind of control flow that isn't stack based (for instance the pair "[" and "]"). In this case, I suppose the advantage is that the stack is shared with C so it's easier to interact over FFI.

From what I've seen of other forths, I much prefer how this one does things. Using quoting as a sort of lambda is much nicer and more consistent than the weird compilation semantics you see in stuff like GForth.

Re:Stem, a stack-based language with metaprogramming and a C... by astrobe_ on Saturday January 27, 2024 @12:07PM (#39157357)
This [] pair for quotations is pretty common in "modern" concatenative languages. But you actually don't need them that much, at least in Forth. It is not a solution because they have misidentified the problem (due to lack of experience, generally), or created a problem ex nihilo by trying to one-up Forth on RPN purity and do control-flow like Smalltalk does.

a priori "must haves" that eventually get in your way. If I had to choose one quote from Chuck Moore (the creator of Forth), it would be "Don't anticipate, solve the problem you've got" [1].

What you need on a daily basis it to be able to reuse names so that you don't clutter your dictionary. But at the same time you do want your Forth to warn you when you define twice the same name, because that could lead to vicious bugs. So all you need to do is to hack your Forth so that e.g. names starting with underscores can be redefined and won't trigger a warning.

[1] https://www.ultratechnology.com/1xforth.htm

Re:Stem, a stack-based language with metaprogramming and a C... by James_K on Saturday January 27, 2024 @01:52PM (#39158550)
I wouldn't call people stupid just because they made a design decision you disagree with. In my view, it is a bad choice to add additional complexity to the language like compiled "if" when you can do something more simple and in-keeping with its other features. The problem isn't invented; they needed to implement conditionals, had two options for doing so, and they chose the simpler one. Do you have any concrete way that the Forth implementation with compile-only words is better? What are its benefits?

> What you need on a daily basis it to be able to reuse names so that you don't clutter your dictionary. But at the same time you do want your Forth to warn you when you define twice the same name, because that could lead to vicious bugs. So all you need to do is to hack your Forth so that e.g. names starting with underscores can be redefined and won't trigger a warning.

The problem is already solved. You just spit out a warning message when something is redefined instead of an error.

Re:Stem, a stack-based language with metaprogramming and a C... by astrobe_ on Saturday January 27, 2024 @03:23PM (#39159458)
> I wouldn't call people stupid

I don't think I did such a thing.

> Do you have any concrete [proof] that the Forth implementation with compile-only words is better? What are its benefits?

Yes I do : a Smalltalk-style forces to have the code (to be executed if the condition is true) to be on the left side of the if. Additionally, if you don't want the address of the quotation to be in your way, you are also forced to adopt an argument order like condition-quotation-if, which is even less convenient than Forth's syntax. Oh, and the else part could be a lot of fun too. Most likely, you'll add some sort of exception to avoid all that, but exceptions are the archetype of unwanted complexity. You seem to think that Smalltalk-style is simpler, but Smalltalk is an infix language with, granted, rules that are simpler than most other languages - but still more complex than those of Forth. So it is not, actually, simpler when you consider the system as a whole.

Also, it makes it more difficult to build loops from that if and recursion like Scheme does (TCO). Chuck Moore's Colorforth does exactly that.

Moreover, and perhaps more anecdotally because few people care about this these days, it is more space and CPU efficient - basically the traditional Forth if is one instruction (bytecode or whatever) followed by one parameter (the jump target) that takes one argument on the stack. A Smalltalk-style if is also one instruction, but it has to call the "if-true" code, and this code has to have a return instruction as well. I can understand that people don't care about a few more bytes and cycles here and there - what's the point for a interpreter to be able to run on a bunch of kilobytes today? - but I wonder why those same people would care about a weird RPN thing that seems to be designed to make their head hurt, with or without quotations. It's only slightly less hostile for them than BrainFuck.

Computer games and computer languages have something in common: when you design and redesign them, you realize that they are a subtle network of interactions between different parts of the system - even if, in the case of programming languages you look for "orthogonality", which is the opposite; but this concept applies more to features/semantics than to syntax/ergonomics.

> The problem is already solved. You just spit out a warning message when something is redefined instead of an error.

You didn't get what I wrote. You want the system to keep quiet when you give an hint that you do something on purpose, like casting explicitly an integer into a pointer in C. You also miss the point: of course this problem is easily solved. But I argue like Chuck Moore that you'd rather solve the specific problem you stumble upon in practice, rather than trying to solve the more general problem that you imagine you will have. Sometimes when you try to kill two birds with one stone, you end up with one bird wounded and a broken window.

Re:Stem, a stack-based language with metaprogramming and a C... by James_K on Saturday January 27, 2024 @06:44PM (#39161021)
> I don't think I did such a thing.

Yes, you were more subtle about it, calling him inexperienced rather than stupid. I see little difference between the two forms of ad hominem and that is what I wanted to point out.

> which is even less convenient than Forth's syntax

The two versions are syntactically the same. You can take a piece of Forth code and do the substitution:

  s/if/[/ 
  s/else/] [/
  s/then/] if/
You then have some Stem code that is slightly shorter. The Stem syntax is just a generalised version of the Forth syntax. The only exception I can think of is it being harder to compile, but as you said, that tiny bit of speed doesn't really matter on modern systems and additionally the code would likely be optimised to the same instructions in a JIT.

> You want the system to keep quiet when you give an hint that you do something on purpose

Why? No code that's saved in production should ever redefine variables, so it's a warning you'd get exclusively during interactive use at the REPL, where you'd be free to ignore it. It's easier to ignore a message than it is to tell the system not to give you that message.

Re:Stem, a stack-based language with metaprogramming and a C... by ret2pop on Saturday January 27, 2024 @07:42PM (#39161381)
To be fair, I am inexperienced. I haven't done much programming in stack based languages and I don't often like to pull the age card but in my defense I have not lived as long as some other people on here as I just turned 18 and have not been programming for that long. Still, it's really cool that some people like my language, and it doesn't really bother me that some people think it's worse -- I just wrote it because I thought it was interesting.
Re:Stem, a stack-based language with metaprogramming and a C... by incanus77 on Saturday January 27, 2024 @09:26PM (#39161964)
> Still, it's really cool that some people like my language, and it doesn't really bother me that some people think it's worse -- I just wrote it because I thought it was interesting.

Stick with this attitude and you'll go far.

Re:Stem, a stack-based language with metaprogramming and a C... by astrobe_ on Sunday January 28, 2024 @05:24AM (#39164256)
Sorry about that, I usually try to refrain from making negative comments on HN. I was sort of mislead by your comment in the thread that you did not make this (didn't get the joke), and thought that the author was unlikely to read that.

Don't take it personally, as I was actually pointing what I think is a common mistake. I did a lot of mistakes in this domain, so I'm trying to share my experience here more than gratuitously criticizing a project.

Re:Stem, a stack-based language with metaprogramming and a C... by astrobe_ on Sunday January 28, 2024 @06:17AM (#39164557)
> Yes, you were more subtle about it, calling him inexperienced rather than stupid. I see little difference between the two forms of ad hominem and that is what I wanted to point out.

I said that because that's how I see myself doing Forth a couple of decades ago. But there should not be any ambiguity here. Inexperience is the lack of knowledge, stupidity is a disappointing failure to use knowledge.

As for the ad homininem, that's a unfair because aside from that single remark, the subject of what I wrote is the feature, not the person who made it.

> The two versions are syntactically the same. You can take a piece of Forth code and do the substitution

True, but the if comes last and it is a significant ergonomic problem. For this to be not too unpleasant, you'll have to keep the quotations very short in practice, which is not a bad thing at all as "keep it short and simple" is a general good piece of advice which is specially true with "unfriendly" or "alien" languages, but one might be disappointed in terms of "usability": for instance nested quotes are something you will actively avoid because of that.

> The only exception I can think of is it being harder to compile, but as you said, that tiny bit of speed doesn't really matter on modern systems and additionally the code would likely be optimised to the same instructions in a JIT

You add complexity to the system in order to compensate for its deficiencies, exactly as I pointed out. Because it is more a glue language than a general purpose language as I understand it, a JIT shouldn't be needed.

BTW, the "modern systems" argument is a fallacy IMO, as many general purpose interpreted languages had to go the JIT route (JS, Python to make it viable for more demanding applications, Lua as well, Smalltalk, PLT Scheme/Racket to name a few). For glue languages, that's different because making up for the typical 10X slowdown of interpretation with the full speed native code libraries can go a long way.

Re:Stem, a stack-based language with metaprogramming and a C... by soegaard on Sunday January 28, 2024 @08:43AM (#39165645)
> BTW, the "modern systems" argument is a fallacy IMO, as many general purpose interpreted languages had to go the JIT route (... PLT Scheme/Racket ...).

Racket uses an AOT compiler these days.

The backend was rewritten and we switches to it early 2021 (since version 8.0).

Re:Stem, a stack-based language with metaprogramming and a C... by James_K on Sunday January 28, 2024 @07:49AM (#39165190)
> the if comes last and it is a significant ergonomic problem

Such is the issue with Forth in general.

> You add complexity to the system in order to compensate for its deficiencies

It is often true that to make the language simpler you must make its implementation more complex. I view this as a good thing. Having one slightly more complicated implementation is much better than many slightly more complicated programs.

Re:Stem, a stack-based language with metaprogramming and a C... by astrobe_ on Monday January 29, 2024 @01:53PM (#39180651)
> It is often true that to make the language simpler you must make its implementation more complex.

True, although it is a last resort thing to do for me, or it has a very good benefits/cost ratio. IOW, where we disagree is the assessment of the worthiness of that specific feature.

Re:Stem, a stack-based language with metaprogramming and a C... by ColonelPhantom on Saturday January 27, 2024 @04:02PM (#39159797)
I read the linked article, and found it rather confusing. For example, what does "In order to write a lot of small definitions you have to have a stack" mean? e.g. in Haskell lots of functions are very small compositions of existing functions, like "foo = filter bar . map baz". But it's definitely not stack based!

I'm also confused about the "local variables are harmful" mentality. Why? They may not suit concatenative languages, but I find naming things serves as effective 'documentation'. Or is the point that these should be put in separate functions? But that also sounds wrong, because then you greatly complicate the control flow and spread everything to many places. Furthermore, isn't something like that A register 'just' a local variable (or worse: a mutable global variable)?

I find the dismissal of things like files and encryption disturbing. Many programs need some form of persistence. How is that useless? Why does 'needed data' not need to be encrypted?!?

Re:Stem, a stack-based language with metaprogramming and a C... by mananaysiempre on Saturday January 27, 2024 @09:44AM (#39155954)
Forth-likes are very very simple to bootstrap in basically any environment (contributing to their popularity back in the age of microcomputers sold with no OS), and once you’ve accepted the syntax are perhaps the most lightweight environments for doing DSLs (which is somewhat related, as one of the typical bootstrapping paths has you writing an assembler DSL as one of the first steps). The “postfix Lisp” PostScript/Joy/Factor heritage that TFA’s language belongs to inherits the latter property.

If I had a choice of making and then using a DSL in Python, Lua, or Forth, it’s Forth all the way, no question. Now if you include Scheme, Ruby, or Tcl, then it might become a competition. (Haskell is somewhere in between.) It’s worth appreciating how much above his weight the humble granddaddy is punching here for the dozen K of memory that a running system normally takes. (Less relevant now than for the micros of old, as for interactivity that memory needs to be RAM, whereas the cheapest controllers today are usually ROM-rich and RAM-starved, relatively speaking.)

Re:Stem, a stack-based language with metaprogramming and a C... by spindle on Saturday January 27, 2024 @06:55PM (#39161087)
> you always inevitably need some kind of control flow that isn't stack based (for instance the pair "[" and "]")

FWIW (I don't think this answers your main complaint, but I do think it's fascinating), factor makes a lot of use of the pair "[" and "]" for control flow, but they're totally stack-based:

https://docs.factorcode.org/content/word-[,syntax.html

https://docs.factorcode.org/content/word-%5D%2Csyntax.html