gren-lang / gren-lang/compiler
Compiler enforced tail calls
Nobody has claimed this yet.
- Dominant language
- Haskell
- Stars
- 503
- Forks
- 29
- PR merge metrics
- No merged PRs in 30d
Description
Gren will correctly optimize tail-recursive functions to avoid the possibility of stack overflow exceptions.
countDown : Int -> Int
countDown value =
if value < 1 then
0
else
countDown (value - 1)
In the example above, countDown is recognized by the compiler as tail-recursive, and will be compiled into a while loop that doesn't consume stack space. However, if you were to somehow get this wrong then you'd get no warning from the compiler that your function isn't stack safe. For beginners, it's also not always apparent what is and isn't stack safe.
This proposal introduces the recur keyword, which is a hint to the compiler that the function is meant to be stack safe in the face of recursion. The compiler can then check, and potentially fail, if that doesn't turn out to be the case.
countDown : Int -> Int
countDown value =
if value < 1 then
0
else
recur (value - 1)
The recur keyword can also work in the face of optimizations such as tail recursion modulo cons, which makes it a bit harder to understand all the potential cases where recursion is stack safe.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files, tests, or compiler entry points are identified in the issue. Start by locating the compiler's handling of recursive calls and keywords, then define how recur is recognized and how unsafe recursion is rejected, including tail recursion modulo cons. Done means the compiler enforces the proposed stack-safety hint.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- elm
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100