gleam icon indicating copy to clipboard operation
gleam copied to clipboard

How can we make it easier to verify tail call optimisation is applied

Open SoTeKie opened this issue 11 months ago • 5 comments

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.

SoTeKie avatar Mar 12 '24 20:03 SoTeKie

If this sounds like a good idea, I'd love to take a stab at adding support for this

SoTeKie avatar Mar 12 '24 20:03 SoTeKie

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 https://github.com/gleam-lang/gleam/discussions/1789 but doesn't have any comments.)

MystPi avatar Mar 12 '24 20:03 MystPi

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.

lpil avatar Mar 12 '24 21:03 lpil

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 :thinking:

For verifying it instead, having it on LSP hover sounds pretty cool

SoTeKie avatar Mar 12 '24 21:03 SoTeKie

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 @tailrec? It could still be optional for the cases in which the extra noise isn't worth the reassurance. I find this also valuable in terms of documentation.

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 :)

jdsee avatar Mar 14 '24 20:03 jdsee