r/ProgrammingLanguages • u/Vigintillionn • Jun 11 '25
Memory management in functional languages
Hello all, I'm an undergrad student who's very interested in compilers and language design.
As a passion project I'm working on a functional language which leans a lot on the compiler. My goal is to make the functional programming Rust. The compiler does all the heavy lifting of checking and guaranteeing safety at zero cost at runtime.
I've been stuck at how I should implement memory management. I don't feel like using a garbage collector as that kind of goes against the purpose of the language. I then considered a reference counter, but that kind of makes cyclic data structures impossible to make and also requires extra run time checks. So then I figured I could maybe use a borrow checker. Now I wonder is this the right approach for a functional language? How do functional languages handle lifetimes? As everything is immutable and references are usually implicit, is it unusual for a functional language to work with explicit references? What about stack and heap allocations? I know Haskell allocates everything on the heap, but with a borrow checker I should be able to leverage the stack as well, right?
I'm hoping to get some insights into this and am thankful for every response!
View all comments
Show parent comment
5
u/probabilityzero Jun 11 '25
Closures escaping are no problem. If you try it on paper using their region inference algorithm, you'll see why it works.
They've written about some downsides, however. One example is that the lexical scope of regions means that some tail recursive functions end up growing memory, since there's no mechanism to delete or replace memory that is currently in scope. There's been much follow up research on solving these problems, such as the calculus of capabilities and the Cyclone language (which was a direct inspiration for Rust), which require manual animation rather than inference.