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!
29
u/mamcx Jun 11 '25
You can see how Rust deal with functional constructs like
iterand closures.Also, there is the language https://austral-lang.org/linear-types and https://github.com/HigherOrderCO/HVM that are probably more tractable for a passion project.
(in the other hand: first finish your first language with what is easier then go for the next challenge)
19
u/probabilityzero Jun 11 '25
You can look at region-based memory management (Google search "tofte talpin regions"). That will give you automatic memory management for functional programs with no GC or reference counting. It has some downsides which you can also read about.
14
u/ineffective_topos Jun 11 '25
In a functional immutable setting with lifetimes, typically it's better to have the lifetime be a modifier on the type, rather than an explicit reference. If everything is immutable and must have bounded lifetime, beware that you'll have extreme restrictions on what you can express.
Some references to look at are MLKit, OCaml (specifically the newer work on modes). For example, MLKit actually tries to infer the lifetime of everything to put them into regions, although it still needs a garbage collector because static freeing is not possible for a general language. You effectively need a GC to free things in a timely way for general programs.
In any case, I believe you'll run into a lot of friction with yourself. The requirements of being highly performant, as well as being abstract and functional will fight each other, and you'll have to be very careful with your choices.
But also keep in mind that garbage collectors are genuinely fast, and you don't always need type system features in order to implement things like stack allocation. What the type system does is force only stack allocation to be possible. But you can infer it in the optimizer regardless. That said again, it's often not worth it because it can be slower than using a GC.
7
u/ProofMeal Jun 11 '25
the suggestions here are really good but there’s also a paper that came out from jane street last year about adding modal types to ocaml for the express purpose of reducing gc allocations which could be interesting to look at as well.
6
u/matthieum Jun 11 '25
One key aspect that is missing from this description is whether the values in your language are mutable or immutable.
Mark & Sweep GCs are very general purpose, and can handle any kind of object graph, included mutation from different threads, etc... That's great when necessary, but overkill when not.
If your language (somehow) ensures that no cycle is ever formed, this drastically simplifies memory management.
If your values are immutable, this drastically simplifies memory management.
If you have a strict separation between local (to a thread) and global (possibly shared across threads) values, you may be able to gain quite a bit of performance by avoiding atomic RMW.
That your language is functional doesn't necessarily say much, though... we'll need deeper characterization.
5
u/ianzen Jun 11 '25
The programming language ATS is basically functional Rust https://www.cs.bu.edu/~hwxi/atslangweb/.
It supports using linear types to track both stack and heap allocations. GC can be used to manage objects whose performance you don’t care about. Data can be both boxed or unboxed.
The type system of ATS is very complex though so I’m not sure if that’s what you want.
4
u/zshift Jun 12 '25
Antelang sounds right up your alley. It’s a low-level functional language currently being worked on. https://antelang.org
3
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jun 11 '25
Have you considered prototyping by emitting Rust source? It could help you to understand the domain quite well, and quickly!
3
u/AustinVelonaut Admiran Jun 11 '25
What is the evaluation strategy of the functional language you are designing -- normal order ("lazy", as in Haskell), or applicative order ("strict", as in Rust, OCaml, etc.)? That has a bearing on how you handle memory, especially having to deal with not knowing when lazy thunks are going to be evaluated (if ever), and thunk updates (a form of mutation).
2
u/SkiFire13 Jun 12 '25
Note that if you're considering to make an eager and pure functional language then cycles will be impossible anyway.
To make cycles possible you need either lazyness like in Haskell or mutability to assign the reference that completes the cycle after all the nodes have been created,
2
u/[deleted] Jun 13 '25
If you are interested in high performing functional languages there is interesting research in modifying functional programs to use in place updates.
4
u/Disjunction181 Jun 11 '25
Reachability types is an active field of research which aims to make ownership practical for functional programming. They try to bridge the "shared XOR mutable" paradigm introduced by Rust. Check them out, they would make a good research topic: https://github.com/TiarkRompf/reachability
4
u/protestor Jun 11 '25
If you want a "functional programming Rust", that is, a high performance functional programming language, you really need to look into the optimization that turns functional programs into equivalent imperative programs whenever you are the only one using a given piece of data. This can be achieved with a borrow checker (a functional program that receives an owned value and returns an owned value does the same thing as an imperative program that mutates the value in place), but you can achieve this even if this information isn't in the type system, by doing program analysis
About borrow checker: I found this https://www.reddit.com/r/ProgrammingLanguages/comments/1b3cjfq/functional_ownership_through_fractional_uniqueness/ which is about this language https://granule-project.github.io/granule.html
Also, take a look at Futhark https://futhark-lang.org/ I'm not sure if this paper is relevant to you https://futhark-lang.org/publications/sc22-mem.pdf but I'm linking anyway
4
u/Unimportant-Person Jun 11 '25
Just to clarify, the borrow checker is not a memory management scheme. It’s a compile time check to see if references are valid. The memory management scheme Rust uses is the Ownership model, where every piece of data has an owner and when the owner goes out of scope, the data’s Drop function is implicitly called. This means types in Rust are Affine, meaning you can use a piece of data zero or one time max.
For a functional language, I can imagine this working quite nicely although you’ll have to add some constructs and it’ll look a little different than most functional languages. You can also go for a linear type system. Linear types mean that data can be used only exactly once. Haskell has (kind of) support for Linear Types if you opt into it.
3
u/ericbb Jun 11 '25
https://github.com/ollef/sixten
https://www.cs.utexas.edu/~hunt/research/hash-cons/hash-cons-papers/BakerLinearLisp.pdf
https://en.wikipedia.org/wiki/Escape_analysis
3
u/_icosahedron Jun 12 '25
I'll just throw in a reference to Clean. Their uniqueness typing is interesting and may fit into your plans for your language.
3
u/superstar64 https://github.com/Superstar64/Hazy Jun 14 '25
I'm quite found of Perseus style reference counting. It's just a flat out improvement over plain reference counting. It allows functional languages to reuse memory if it's possible and have better performance with concurrency as reference counts are thread local until memory is shared. The main limitation is that your can't support mutable variables as those could form cycles.
If your unaware, if your language doesn't have mutable variables, then you can remove all your cycles by rewriting all your fixed points with a fixed combinator or by lambda lifting mutually recursive binding if you permit global cycles. This is somewhat obvious in hindsight, as functional languages should reasonably be able to be compiled to lambda calculus and you can implement lambda calculus with only reference counting.
The reason I'm enamored with reference counting is that it's pay for what you use. You could conceivably have a very convenient language that uses reference counting for most types, but still allows you to dip below and use lifetimes, manual memory, etc and not pay the consequences. This is what I'm planning with the Haskell compiler I'm writing (if I ever get off the ground that is).
2
u/GunpowderGuy Jun 11 '25
Have you looked at mutable value semantics? What about inmutability with reuse such as what lean4 does
2
u/R-O-B-I-N Jun 15 '25
Linear/Affine type systems are an alternative to borrow checking and they accomplish the same goal.
1
u/Public_Grade_2145 Jun 12 '25 edited Jun 12 '25
Tracing based garbage collector should do the job and handle cyclic structure. My strategy might not work for your case since you're implementing functional programming rust.
On mutable variable, as a workaround, we can box them and treat them as if vector and tracing GC perfectly handle it.
Assuming compiling to native, it is much like printing the objects that is on stack (like profiling) then these are the roots plus any global if the runtime using any. Once comfortable with that. You're well prepared to implement tracing based GC.
Naively, flip the allocation pointers and recursively scan the objects you can go from those roots while copying them. As graph tracing is inherently recursive.
Once you comfortable with that, you need to make the recursion explicit by maintaining the stack yourself. If you change the stack to queue, then you would get classical cheney algorithm. Yep, it is an interesting insight, personally.
Example implementation: https://github.com/taimoon/scheme-compiler/blob/9c8e3fbfad645f87fb67e394e04dfbea633c3028/runtime.c#L553-L603
Assuming your language is very static, then you may study how go did GC which is not relying on stack-tracing method like mine iinm.
0
u/[deleted] Jun 11 '25
[deleted] — view deleted comment