Skip to content

Support tail recursion #49

Closed
Closed

Description

@tangentstorm

Tail recursion can be detected when

  • an explicit definition is running
  • the result of the current sentence is guaranteed to be the result of the verb
  • the sentence issues a recursion to the same explicit definition
  • all tokens have been moved from the queue to the stack in the parser
  • no names are on the stack in the parser

-- @HenryHRich 13:15, 12 June 2017 (UTC)

Activity

  1. moon-chilled commented on Apr 16, 2022

    @moon-chilled
    Contributor

    I think the most interesting part of tail recursion/tail calls is not actually tearing down the stack frame early, but rather decrementing the reference counts on local variables early, in order to reduce space consumption and enable more in-placing. And decrefing can happen much earlier than the final call in a definition. E.G. this should be able to work in place:

    f=. {{ 1 (2}) y }}
    g=. {{
     a=. i.y
     b=. f a
     b }}
    g 1e8
    
  2. moon-chilled commented on Apr 16, 2022

    @moon-chilled
    Contributor

    (also: of course should be togglable, though probably orthogonal to debug mode, as you might want to include debugging without giving up performance)

  3. HenryHRich commented on Mar 29, 2026

    @HenryHRich
    Contributor

    This should be done by explicit user action. Perhaps a keyword recur. which is 'goto_0' but clearing local variables and setting new x/y.

  4. locked and limited conversation to collaborators on Mar 29, 2026
  5. converted this issue into a discussion #242 on Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions