How can we make it easier to verify tail call optimisation is applied #2721
Replies: 7 comments 4 replies
-
If this sounds like a good idea, I'd love to take a stab at adding support for this |
Beta Was this translation helpful? Give feedback.
-
I like this idea a lot! It is annoying to have to look at the generated code to see if the function was generated as expected. (Previously suggested in this discussion thread #1789 but doesn't have any comments.) |
Beta Was this translation helpful? Give feedback.
-
I don't like the idea of having this attribute scattered throughout Gleam code, especially since it largely does nothing and so is entirely noise then. I can see the value in checking whether functions are tail recursive, but I think this should be done through tooling instead. For example, the LSP could say on hover, or with a third party CLI tool made using the Glance parser. To make matters more complex when the optimisation is applied will vary depending on the target being used. |
Beta Was this translation helpful? Give feedback.
-
I guess it depends on what type of code you're working on, but imho having a way to enforce tail recursion is pretty nice. For example when implementing functions where exceeding the stack limit is a reasonable worry it would be somewhat insufficient to manually check it through the LSP definition/resulting compiled code. I can't think of a edit: less noisy way to enforce tail recursion on a function easily outside of an attribute 🤔 For verifying it instead, having it on LSP hover sounds pretty cool |
Beta Was this translation helpful? Give feedback.
-
I really like the idea of being able to enforce tail recursion for specific functions. It's just too easy to design a function in a tail recursive style and then have it changed by another programmer (aka future me) and lose the guarantees. Would it really be so bad to offer something like Anyway it would be great if the language server offered support for this. I'm wondering if it's possible to provide separate syntax highlighting for tail recursive functions in an editor agnostic way. BTW: Gleam looks great! Thanks for your big effort on this project :) |
Beta Was this translation helpful? Give feedback.
-
I agree with the recommendation to highlight tail recursive functions in the LSP. I was just wondering @lpil if there's any alternative syntax/system to attributes you would find acceptable for enforcing tail-recursiveness on a function or if the enforcing part should be left to third party tooling all together. In the future I can imagine myself making some sort of tool to enforce this, but if it's outside of the language it would mean I'd have to introduce some non-standard comment/naming scheme for the tool to be able to enforce it. |
Beta Was this translation helpful? Give feedback.
-
Coming from scala, I would feel uneasy using recursion instead of looping if there was no way to ask the compiler to enforce TCO 🤔 I guess there's always options like using iterators with helper functions such as fn iterator_loop(init: e, acc: acc, f: fn(acc) -> iterator.Step(e, acc)) -> e {
iterator.unfold(acc, f)
|> iterator.fold(init, fn(_, last) { last })
} |
Beta Was this translation helpful? Give feedback.
-
This would enforce any function with the attribute to be tail recursive, throwing a compiler error otherwise.
This can make ensuring a function is tail recursive much easier, as it removes the need for the programmer to manually test it, look at the compiled result etc.
Beta Was this translation helpful? Give feedback.
All reactions