• Re: Recursion by name

    From Ruvim@ruvim.pinka@gmail.com to comp.lang.forth on Fri Sep 18 17:55:22 2026
    From Newsgroup: comp.lang.forth

    On 2026-07-24 15:27, Gerry Jackson wrote:
    In a Forth definition recursion is achieved by the word RECURSE. It
    doesn't seem to be possible in standard Forth to achieve recursion in a single definition by calling the word's name. However it can be done by using two definitions with the same name and exploiting Forth's
    visibility rules during compilation of the recursive definition. A new recursive definition can be defined to hide the implementation details.

    The two simplest ways to implement recurse by name seem to be:

    synonym foo recurse
    and
    : foo-a postpone recurse ; immediate

    Each followed by the recursive definition such as:
    : foo ?dup if dup >r 1- foo r> . then-a ;
    10 foo-a \ displays 1 2 3 4 5 6 7 8 9 10

    Both solutions at first sight look a bit strange and it is better to
    hide the detail behind a new defining word such as RECURSIVE:

    Definitions of RECURSIVE: for each solution are:

    Using SYNONYM we need to build a string to evaluate e.g.

    : recursive: >in @ parse-name 2dup
    -a-a s"-a recurse" <# holds holds s" synonym " holds #> evaluate
    -a-a >in ! :
    ;

    EXECUTE-PARSING can be used to simplify the definition slightly.

    Using POSTPONE RECURSE

    : recursive:-a >in @
    -a-a-a-a : [: postpone recurse ;] compile, postpone ; immediate
    -a-a-a-a >in ! :
    ;

    The example definition of foo above gives the same result.

    recursive: foo ?dup if dup >r 1- foo r> . then-a ;
    10 foo

    Both definitions display-a-a 1 2 3 4 5 6 7 8 9 10


    Elegant.

    Note: that approach does not work if the compilation word list is absent
    in the search order, or the new word is accidentally shadowed by another.

    Another issue is that it does not help in case of indirect mutual recursion.

    Therefore, as a portable module, I would implement that using a
    Recognizer API.

    Usage might look like this:

    forward( foo bar baz ){

    : foo ... bar ... ;
    : bar ... baz ... ['] baz ;
    : baz ... baz ... foo ... ;

    } \ the lexical scope where `foo` `bar` `baz` are visible.



    --
    Ruvim
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From albert@albert@spenarnc.xs4all.nl to comp.lang.forth on Sat Sep 19 00:26:18 2026
    From Newsgroup: comp.lang.forth

    In article <118jfsa$1auo3$1@dont-email.me>,
    Ruvim <ruvim.pinka@gmail.com> wrote:
    On 2026-07-24 15:27, Gerry Jackson wrote:
    In a Forth definition recursion is achieved by the word RECURSE. It
    doesn't seem to be possible in standard Forth to achieve recursion in a
    single definition by calling the word's name. However it can be done by
    using two definitions with the same name and exploiting Forth's
    visibility rules during compilation of the recursive definition. A new
    recursive definition can be defined to hide the implementation details.

    The two simplest ways to implement recurse by name seem to be:

    synonym foo recurse
    and
    : foo-a postpone recurse ; immediate

    Each followed by the recursive definition such as:
    : foo ?dup if dup >r 1- foo r> . then-a ;
    10 foo-a \ displays 1 2 3 4 5 6 7 8 9 10

    Both solutions at first sight look a bit strange and it is better to
    hide the detail behind a new defining word such as RECURSIVE:

    Definitions of RECURSIVE: for each solution are:

    Using SYNONYM we need to build a string to evaluate e.g.

    : recursive: >in @ parse-name 2dup
    -a-a s"-a recurse" <# holds holds s" synonym " holds #> evaluate
    -a-a >in ! :
    ;

    EXECUTE-PARSING can be used to simplify the definition slightly.

    Using POSTPONE RECURSE

    : recursive:-a >in @
    -a-a-a-a : [: postpone recurse ;] compile, postpone ; immediate
    -a-a-a-a >in ! :
    ;

    The example definition of foo above gives the same result.

    recursive: foo ?dup if dup >r 1- foo r> . then-a ;
    10 foo

    Both definitions display-a-a 1 2 3 4 5 6 7 8 9 10


    Elegant.

    Note: that approach does not work if the compilation word list is absent
    in the search order, or the new word is accidentally shadowed by another.

    Another issue is that it does not help in case of indirect mutual recursion.

    Good catch. So RECURSE is essentially worthless.
    So I exclusively use the mutual recursion solution.
    See below.


    Therefore, as a portable module, I would implement that using a
    Recognizer API.

    Usage might look like this:

    forward( foo bar baz ){


    } \ the lexical scope where `foo` `bar` `baz` are visible.


    Recognizer???

    A few lines in ciforth:

    ' : ALIAS :F \ more a warning than a necessity.
    : :R >IN @ NAME FOUND >R >IN !
    :
    LATEST >DFA @ R> >DFA ! \ Patch the high level code pointer.
    ;

    The example becomes
    :F foo ;
    :F bar ;
    :F baz ;
    :R foo ... bar ... ;
    :R bar ... baz ... 'baz ;
    :R baz ... baz ... foo ... ;

    You can play with a hide flag (smudge bit) to prevent
    spurious messages (not shown).

    --
    Ruvim

    Groetjes Albert
    --
    The Chinese government is satisfied with its military superiority over USA.
    The next 5 year plan has as primary goal to advance life expectancy
    over 80 years, like Western Europe.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From sjack@sjack@dontemail.me (sjack) to comp.lang.forth on Sat Sep 19 17:58:06 2026
    From Newsgroup: comp.lang.forth

    dxf <dxforth@gmail.com> wrote:
    Recursion by name means changing all the references when the name changes. ANS notes RECURSE was originally called MYSELF - a clever name if ever
    there was. Perhaps they ought to have stayed with that.


    FigForth, as is:
    : foo -dup if dup >r 1- [ smudge ] foo [ smudge ] r> . then ; OK
    10 foo 1 2 3 4 5 6 7 8 9 10 OK

    FigForth, extended:
    : bar -dup if dup >r 1- myself r> . then ; OK
    10 bar 1 2 3 4 5 6 7 8 9 10 OK
    ' myself see
    MYSELF IMMEDIATE
    134533136 LATEST
    134533140 PFA
    134533144 CFA
    134533148 ,
    134533152 ;S OK

    (Of course nothing to do with Standard Forth)
    FigForth, as is, the foo above is ok for me:
    1) Don't need another word.
    2) It shows clearly what's going on.
    However, I do have 'myself' and many other un-needed words:)
    --
    me
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Sun Sep 20 06:55:38 2026
    From Newsgroup: comp.lang.forth

    dxf <dxforth@gmail.com> writes:
    Recursion by name means changing all the references when the name changes.

    Changing the name of a word means changing all the references to that
    name, yes. With recursion by name that includes typically 1 or two
    additional references. So what?

    Gforth has had RECURSIVE to support recursion by name since the
    beginning.

    Someone mentioned indirect recursion. In general, a DEFERred word is
    good enough for that, but development Gforth has FORWARD (because we
    can; it's a really cool application of the possibilities of the new
    header), which allows eliminating the (miniscule) overhead of a
    deferred word after the forward reference has been resolved on the
    first invocation. And of course you can also use it for direct
    recursion.

    The example creates endless recursion, because that is easiest, and
    I leave it to your imagination to make productive use of this feature.

    foo \ not previously defined
    *the terminal*:1:1: error: Undefined word
    foo<<< \ not previously defined
    forward foo ok
    : foo foo ; \ resolves foo in the forward definition ok
    foo \ resolves foo in the colon definition
    *the terminal*:4:1: error: Return stack overflow
    foo<<< \ resolves foo in the colon definition
    see foo
    : foo
    foo ; ok
    simple-see foo
    <foo> call 0->0
    <foo+$8> foo
    <foo+$10> ;s 0->0 ok
    <foo> h. $7F5887CA8020 ok
    <foo+8> @ h. $7F5887CA8020 ok

    The last two lines show that the call is indeed to the colon
    definition, and not to the forward definition.

    - 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 2026 CFP: http://www.euroforth.org/ef26/cfp.html
    --- Synchronet 3.22a-Linux NewsLink 1.2