• Closures (was: Test cases for quotations)

    From Anton Ertl@21:1/5 to minforth on Sat Mar 9 17:30:21 2024
    minforth@gmx.net (minforth) writes:
    My quotation "model" with access to upvalues works here.
    Useful now and then, but they cannot pass the man-or-boy test.

    Why not?

    What use did you find for them?

    I tried to find uses for the Gforth closures for the paper that could
    not be replaced by code without this feature, but failed to find a
    concise example where the benefit was convincing (as in: much easier
    than the alternative).

    So I asked Niklaus Wirth, who has implemented this feature in Pascal,
    Modula, Modula-2, Oberon, and Oberon-2; I thought that, with keeping
    this feature for so many decades, he must have a good reason. At
    first I had trouble reaching him, but he came to Vienna in January
    2020, and I asked him in person. He told me that he had removed this
    feature from Oberon-07 (based on Oberon) in 2013, and later also
    answered in writing.

    Anyway, in the meantime we have found a good use for closures: Gforth implements a variant of the actor model <https://gforth.org/manual/Message-queues.html>. The messages are
    actually xts that are executed by the target task.

    Originally, in order to parameterize the passed xts, there was also a
    mechanism for passing values that would be pushed on the stack of the
    target task, and finally one would pass a word that would consume
    these stack items.

    Thanks to closures the protocol could be simplified such that only xts
    are passed. If you want to parameterize the message, you pass a
    closure that includes the parameters. For extra convenience, there
    are one-shot heap-allocated closures (defined with :}h1) that FREE
    themselves when they run.

    IIRC a while ago Anton Ertl and/or Bernd Paysan have implemented
    closures for gforth:
    ...
    They introduced a special closure syntax, so quotations with
    access to locals of the enclosing function must not be confused
    with closures.

    For Gforth's closures, just say "Gforth closures". There are lots of
    other equally valid uses of the word "closure"; Gforth closures are
    based on the concept of flat closures [dybvig87,keep+12]. Gforth
    closures leave flat-closure conversion to the programmer, and that
    turns out to often be more convenient to program than defining outer
    locals and capturing them implicitly.

    @PhdThesis{dybvig87,
    author = "R. Kent Dybvig",
    school = "University of North Carolina at Chapel Hill",
    title = "Three Implementation Models for Scheme",
    year = "1987",
    url = "http://agl.cs.unm.edu/~williams/cs491/three-imp.pdf",
    brokenURL = "ftp://ftp.cs.indiana.edu/pub/scheme-repository/txt/3imp.ps.Z",
    month = apr,
    OPTannote = "Introduces flat closures under the name \emph{display
    closures} in Section 4.4.2"
    }

    @InProceedings{keep+12,
    title = "Optimizing Closures in {O}(0) Time",
    author = "Andrew W. Keep and Alex Hearn and R. Kent Dybvig",
    bibdate = "2015-05-09",
    bibsource = "DBLP,
    http://dblp.uni-trier.de/db/conf/icfp/scheme2012.html#KeepHD12",
    booktitle = "Proceedings of the 2012 Annual Workshop on Scheme and
    Functional Programming, Scheme 2012, Copenhagen,
    Denmark, September 9-15, 2012",
    publisher = "ACM",
    year = "2012",
    xbooktitle = "Scheme@ICFP",
    editor = "Olivier Danvy",
    ISBN = "978-1-4503-1895-2",
    pages = "30--35",
    URL = "http://doi.acm.org/10.1145/2661103",
    urlwithoutbibliography = "https://www.cs.indiana.edu/~dyb/pubs/closureopt.pdf",
    OPTannote = "Describes a number of optimizations for a
    flat-closure implementation of Scheme."
    }

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2023: https://euro.theforth.net/2023

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From albert@spenarnc.xs4all.nl@21:1/5 to Anton Ertl on Sat Mar 9 23:18:06 2024
    In article <2024Mar9.183021@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:

    So I asked Niklaus Wirth, who has implemented this feature in Pascal,
    Modula, Modula-2, Oberon, and Oberon-2; I thought that, with keeping
    this feature for so many decades, he must have a good reason. At
    first I had trouble reaching him, but he came to Vienna in January
    2020, and I asked him in person. He told me that he had removed this
    feature from Oberon-07 (based on Oberon) in 2013, and later also
    answered in writing.

    I find this extremely interesting. The Pascal specification is
    clear. I'd not thought that - for me - an obscure feature as
    closures is present in Pascal, much less that you could remove
    this feature.

    I always thought that closures are invented by lispers, because
    in lisp you cannot write normal programs.

    - anton
    --
    Don't praise the day before the evening. One swallow doesn't make spring.
    You must not say "hey" before you have crossed the bridge. Don't sell the
    hide of the bear until you shot it. Better one bird in the hand than ten in
    the air. First gain is a cat purring. - the Wise from Antrim -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)