dxf <
dxforth@gmail.com> writes:
On 7/02/2025 12:59 am, minforth wrote:
On Thu, 6 Feb 2025 12:57:12 +0000, Anton Ertl wrote:
AFAIR 200x nested definitions were justified on the grounds named
definitions were neither needed nor wanted
Really? There's a proposal to eliminate named definitions? That's
news to me.
There are cases where you need to pass an xt to some word, and where
the xt does not merit a name, e.g.,
: (where) ( "name" -- ) \ gforth-internal
(') [: over = ;] forwheres
drop -1 where-index ! ;
Of course, one could do that without quotations:
: (where-helper) over = ;
: (where) ( "name" -- ) \ gforth-internal
(') ['] (where-helper) forwheres
drop -1 where-index ! ;
But it's more convenient and readable with quotations.
and access to external
locals not necessary.
It's more that anything proper is pretty hard to implement and there
was not enough demand for that nor existing practice that anybody
wanted to propose for standardization.
However, the topic came up repeatedly in discussions, so in 2018 I sat
down to investigate the issue. After a while I had a paper design,
but had my doubts that it is worth implementing it. I let Bernd
Paysan read the paper, and he came up with further simplifications and
implemented the result; the changes were so deep that I had to mostly
rewrite the paper, resulting in our EuroForth 2018 paper
[ertl&paysan18]. And Bernd embraced using the resulting closures.
I still had my doubts about whether one really needs that, as I had
not found a short example that showed a clear advantage over the
alternatives. I asked Niklaus Wirth, who had kept access to outer
locals in his languages for many decades, but his answer was that he
finally removed that feature from Oberon 07 in 2013, and he did not
provide such an example, either.
Finally, such a usage was found by Bernd Paysan: He had implemented a
variant of the actor model
<
https://gforth.org/manual/Message-queues.html> (inspired by Heinz
Schnitter's Open Network Forth):
One task sends a message consisting of an xt to another task, and the
other task then executes it. But sending just an xt without any data
is not very useful, so one could also send integers, strings, etc.
The sender would send the data and then the xt, and the receiving task
would push the data on the stack, and then execute the xt. However,
several tasks can send messages to one task at the same time, so there
was some additional twist to avoid mixing the parts of a message of
one task with the parts of a message of another task. With closures
that all became unnecessary: The data is part of the passed xt.
Another development was pure-stack closures.
But none of this existed when quotations were accepted for
standardization.
@InProceedings{ertl&paysan18,
author = {M. Anton Ertl and Bernd Paysan},
title = {Closures --- the {Forth} way},
crossref = {euroforth18},
pages = {17--30},
url = {
https://www.complang.tuwien.ac.at/papers/ertl%26paysan.pdf},
url2 = {
http://www.euroforth.org/ef18/papers/ertl.pdf},
slides-url = {
http://www.euroforth.org/ef18/papers/ertl-slides.pdf},
video = {
https://wiki.forth-ev.de/doku.php/events:ef2018:closures},
OPTnote = {refereed},
abstract = {In Forth 200x, a quotation cannot access a local
defined outside it, and therefore cannot be
parameterized in the definition that produces its
execution token. We present Forth closures; they
lift this restriction with minimal implementation
complexity. They are based on passing parameters on
the stack when producing the execution token. The
programmer has to explicitly manage the memory of
the closure. We show a number of usage examples.
We also present the current implementation, which
takes 109~source lines of code (including some extra
features). The programmer can mechanically convert
lexical scoping (accessing a local defined outside)
into code using our closures, by applying assignment
conversion and flat-closure conversion. The result
can do everything one expects from closures,
including passing Knuth's man-or-boy test and living
beyond the end of their enclosing definitions.}
}
- anton
-- M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.htmlcomp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html New standard: https://forth-standard.org/EuroForth 2023 proceedings: http://www.euroforth.org/ef23/papers/EuroForth 2024 proceedings:
http://www.euroforth.org/ef24/papers/