I suggest we ...

Enable a compiler-warning when a recursive algorithm is not tail-recursive

Add an TailRecursiveAttribute to enable a compiler-warning when a recursive algorithm is not tail-recursive. This should ideally also cover recursive seq { .. } and async { ... } expressions.

311 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
knocte shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

I am generally in favour of addressing this in F# 4.x+. I would want seq { .. } and async { … } tailcalls to also be addressed.

A more detailed design is needed and some trialling would be great. Jack’s work is a great start. However, this is not an easy piece of work to do in a non-invasive way and my own experiments in this area have not yet led to something I feel confident to include in the F# language design.

An implementation and testing would need to be provided by someone in the F# community (possibly including Microsoft or Microsoft Research, though not limited to them).

Currently, initial implementations of approved language design can be submitted as pull requests to the appropriate branch of https://github.com/Microsoft/visualfsharp. See http://fsharp.github.io/2014/06/18/fsharp-contributions.html for info on how to contribute to the F# language/core library design

I encourage you to consider continue working towards a detailed proposal and a proposed implementation. I will gladly help.

Don Syme, F# Language and Core Library Evolution

23 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Phillip Trelford commented  ·   ·  Flag as inappropriate

    @Anonymous it's desirable to be tail recursive but not always necessary, a blanket warning on all existing code might be a bit harsh. An attribute or keyword where you explicitly expect tail recursion might be more forgiving.

  • knocte commented  ·   ·  Flag as inappropriate

    @Phillip: as far as I understand, it's always desirable to be tail-call-friendly, otherwise you might get StackOverflowExceptions if there are many iterations, right?

  • Phillip Trelford commented  ·   ·  Flag as inappropriate

    A compiler warning/error would be useful when you require your function to be tail recursive but it is not, perhaps a tailrec keyword could be used in this case, i.e.

    let tailrec myfunc = // ...

2 Next →

F# Language

Feedback and Knowledge Base