News for hackers. Stuff that matters.
Re:Stack-Oriented Programming by munk-a on Tuesday January 14, 2020 @03:54PM (#22049145)
I'm a bit confused why "using postfix notation" is considered a programming paradigm - I'm semi-fond of post-fix notation (it is the second best notation after prefix after all) but[1] it's incredibly trivial to convert between prefix and postfix notation and most programming language expressions use prefix already - it's just the infix ones that are a bit weird... Allllso, expressing if statements without any infix notation is sort of terrible, I've never seen an option to do it that isn't incredibly misleading or confusable.

A core question, though, is why? Is tab-oriented programming have a wikipedia page where programs are written predominantly using tab stops instead of spaces? It's honestly a very similar sort of distinction to make.

1. I'm trying not to stray too far into all turing complete languages are turing complete and thus difference is meaningless land.

Re:Stack-Oriented Programming by triska on Tuesday January 14, 2020 @04:00PM (#22049208)
As I see it, the key characteristic of stack-oriented programming is not that it "uses postfix notation".

Rather, the key characteristic is that there is an important implicit feature available, namely the stack (or, as for example in the case of PostScript, several stacks), by which arguments can be passed along.

As a consequence, you get a form of compositionality of language elements that is not available in languages that lack this feature: A language element may consume, change and create entities on the stack, and the next language element can pick them up implicitly.

This often allows very concise and elegant code, and can also be interpreted very efficiently.

Re:Stack-Oriented Programming by jerf on Tuesday January 14, 2020 @04:34PM (#22049465)
Best advocacy I know of: http://evincarofautumn.blogspot.com/2012/02/why-concatenativ...

Presented by way of being an interesting link, and the educational nature of reading the best arguments in favor of something. I haven't done anything in concatenative programming in about 20 years, since I left math class and didn't need my HP 48G anymore.

Re:Stack-Oriented Programming by munk-a on Tuesday January 14, 2020 @04:35PM (#22049475)
That's quite helpful, it may be a lack of in-depth reading of the page but the persistence and implicit passing of computation between operations is helpful to understand the differentiation. In more classic procedural languages you'll usually have a function stack that is being pushed and popped two but actual program values are handled using entirely arbitrarily loaded references that get juggled around as needed for computation - does stack programming often have a similar concept to frame popping when an issue with I/O or something similar is encountered or do we just get a panic that aborts computation?
Re:Stack-Oriented Programming by triska on Tuesday January 14, 2020 @04:45PM (#22049558)
To get a glimpse of stack-oriented programming, I really like and recommend PostScript, because this also automatically introduces you to a widely used programming language with important practical applications, and it's also great fun to learn.

So, for instance, let's elicit an error using Ghostscript, a freely available and efficient PostScript interpreter:

    $ gs
    GPL Ghostscript 9.27 (2019-04-04)
    ...
At the "GS>" prompt, I now define "f" via:

    /f { /hello g } def
Note that "f" refers to "g", which is not yet defined.

Therefore, when I then invoke "f" via:

    GS>f
I get an error:

    Error: /undefined in g
And importantly, the stack is retained, so I can inspect it at this point, interactively, using for example "pstack":

    GS<1>pstack
    /hello
This shows that /hello is still on the stack. From there, we can resume in various ways, for example by clearing the stack (with "clear"), by redefining and reinvoking "f" etc.
Re:Stack-Oriented Programming by haolez on Tuesday January 14, 2020 @07:13PM (#22050579)
Is PostScript used beyond page description? I'm really curious :)
Re:Stack-Oriented Programming by pedrow on Wednesday January 15, 2020 @04:55AM (#22053000)
I recently saw an announcement[0] of a Z-machine in Postscript. The Z-machine is a runtime for text adventure games. Also posted on HN[1] but no comments were made.

[0]: https://groups.google.com/d/msg/rec.arts.int-fiction/HNtntxC...

[1]: https://news.ycombinator.com/item?id=21367412

Re:Stack-Oriented Programming by zzo38computer on Wednesday January 15, 2020 @04:47PM (#22059093)
Yes; I wrote that program, and the Usenet message and the message on HN (which mentions the message ID for the Usenet message). If you have comments about it, I suggest writing a follow-up message on Usenet.

PostScript is a real programming language and can be used for many things (especially graphics). I also wrote a JSON parser in PostScript (see message <[email protected]> on comp.lang.postscript), and then someone else post their JSON parser in PostScript too.

I think PostScript isn't such a good protocol or document format, but it is good as a programming language. It is what I might use if I want to make a diagram (but unfortunately Ghostscript cannot produce SVG output; it can output PDF though, as well as many raster formats).

Re:Stack-Oriented Programming by haolez on Thursday January 16, 2020 @07:59PM (#22070805)
Nice! Thanks for answering! Would you choose PostScript over Forth as a general purpose language?
Re:Stack-Oriented Programming by zzo38computer on Thursday January 16, 2020 @10:06PM (#22071493)
I think it would depend on what I am trying to make; there are advantages and disadvantages to each. In the case of the JSON parser, I think it is helpful that PostScript has data structures (such as arrays and dictionaries) and arbitrarily typed values like JavaScript and many other scripting languages have. In the case of Z-machine, Forth would probably do as well as PostScript (or probably Forth would do better, actually).

Someone was unsure how useful would be the JSON parser I wrote in PostScript, but if you want to print some diagram based on data in JSON format, then it can be helpful.

(I could implement Z-machine in Forth too if I wanted to do, but I have not done so yet because I have other things to do; I don't know if I might do so in future or not. I could implement Z-machine in any programming language with byte arrays, including assembly language (I started but haven't finished one in 6502).)

Re:Stack-Oriented Programming by PeCaN on Tuesday January 14, 2020 @08:42PM (#22051040)
Sun made a windowing system based on (Display) PostScript way back https://en.wikipedia.org/wiki/NeWS
Re:Stack-Oriented Programming by rwmj on Wednesday January 15, 2020 @05:02AM (#22053032)
https://www-cdf.fnal.gov/offline/PostScript/fractal-fern.ps

is a fractal fern. This is page description, but not in the normal way that it's used. Compare the output in a PS or PDF viewer with the source code which is under 1.5K.

Re:Stack-Oriented Programming by RodgerTheGreat on Tuesday January 14, 2020 @10:08PM (#22051458)
Once I wrote a bingo card in PostScript which would randomize its cells every time you printed it. That's almost useful.
Re:Stack-Oriented Programming by kortex on Tuesday January 14, 2020 @10:44PM (#22051603)
I found a .ps that generates timing wheel patterns (discs with alternating black and white radial bands). you'd plug in inner/outer radius, number of bands, and you'd get a printable document from that file. As in, the .ps file was code, which printed as an image. I now know this is the origin of pdf but it blew my mind.
Re:Stack-Oriented Programming by hnick on Tuesday January 14, 2020 @08:59PM (#22051132)
It's Turing-complete, which makes writing programs to analyse documents a bit of a problem at times.

But I think maybe you meant in the sense of "in the real world", and I can't think of any examples. Prefixing some postscript to a document to manipulate that file is something it can be suited for, rather than using an external language to do so.

Re:Stack-Oriented Programming by Mikhail_Edoshin on Thursday January 16, 2020 @08:12AM (#22064133)
Even in page description alone the programming capabilities are rather useful. E.g. to produce barcodes you can just generate them from data.
Re:Stack-Oriented Programming by narag on Tuesday January 14, 2020 @08:16PM (#22050911)
There's an old language, pop11, that's similar to lisp but stack oriented. A few sections of the manual will show you the kind of stack tricks that make different order useful:

https://www.cs.bham.ac.uk/research/projects/poplog/primer/#s...

Functions that return several results, implicit parameters, open stack, etc. It's a lot of fun.

Re:Stack-Oriented Programming by rixed on Wednesday January 15, 2020 @04:17AM (#22052821)
Sounds like automatic currying in FPLs is equivalent to some of that.
Re:Stack-Oriented Programming by olooney on Tuesday January 14, 2020 @04:07PM (#22049261)
For a stack oriented language, postfix is execution order. Consider the program "2 3 +". This is really three instructions:

1. Push the literal 2 onto the stack

2. Push the literal 3 onto the stack

3. Execute the Plus Operation

Therefore, there is no need to use a compiler, or even Dijkstra's shunting-yard algorithm[1], to reorder the statements for execution. This makes it extremely easy to write a compiler or interpreter for a stack-oriented language with postfix syntax: you never have to do any parsing at all, you just execute (or emit byte code) for each token as it comes, never needing to look forwards or backwards even a single token.

[1]: https://en.wikipedia.org/wiki/Shunting-yard_algorithm

Re:Stack-Oriented Programming by smabie on Tuesday January 14, 2020 @04:06PM (#22049248)
Postfix notation isn't really related to stack programming, you could just as easily read the programs right-to-left and have prefix notation. Something like K/Q but with stacks. Anyways, stack based languages are a real distinct category, just like OO or FP or array-based languages or whatever. If you look at forth code for example, especially written by Moore, it's some... pretty crazy shit. I do agree that conditionals are pretty annoying to read using stack based languages, but they are used a lot less often due to the implicit "branching" a stack context provides.
Re:Stack-Oriented Programming by astrobe_ on Tuesday January 14, 2020 @05:06PM (#22049725)
"Paradigm" is a conveniently abstract word.

I would say that RPN is part of a paradigm - and if you pardon my bias I would say the Forth paradigm, really. The stack is part of a strategy of complexity reduction that start at the compiler/interpreter level.