Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax to require recursions to be tail recursive. #1172

Open
johnynek opened this issue Mar 16, 2024 · 1 comment
Open

syntax to require recursions to be tail recursive. #1172

johnynek opened this issue Mar 16, 2024 · 1 comment

Comments

@johnynek
Copy link
Owner

In scala, there is an annotation @tailrec that makes compilation fail if the function is not tail recursive. Tail recursion ensures that the function can be compiled into a loop, and ensures we don't blow the stack.

Bosatsu has the optimization, but no way to require that it happens. This seems against the bosatsu way of biasing towards stronger guarantees.

We could have syntax like:

defloop len(lst, acc):
    recur let:
        case []: acc
        case [_, *tail]: len(tail, acc.add(1))

where defloop is just a def where all recursions are in the tail position. Since the tail check can be a kind of a lint, we can do that in parallel with other type checking so that we don't have the sequential error presentation issue.

I don't love the look of defloop, but maybe it's the best. Idk. Changing it should be pretty easy since that string can be changed with find and replace. Ideas:

defloop len(lst, acc):
  ...

loop len(lst, acc):
  ....

def len(lst, acc):
  loop lst:
    case []: ...

hmm... maybe the last one is best... it also makes the tail loop nature local to the function rather than at the top level.

@johnynek
Copy link
Owner Author

thinking more: the last option above seems the best: if the recursion is tail recursive you must use loop. you use recur only when there is non-tail recursion. This allows a reader to clearly see which case we are in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant