Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can build safe abstractions for manual memory management in userspace with LinearTypes. In fact Haskell already has plenty of manual memory management modules in its stdlib - it's just that they live in IO.

You can do the unsafePerformIO internally and expose a pure API made safe by some combination of LinearTypes and the ST trick.



LinearTypes guarantee that all values are used exactly once if the result is used exactly once. If an exception is thrown, the result won't be used once and the memory would leak.

One cheap way is to also track the memory with the GC. This is similar to using GC finalizers on file handles - closing handles via GC is inefficient and slow but it's only the last resort.

Another option is to use exception handlers to guarantee that the Ressource is freed. Either one handler per Ressource which destroys all advantages of linear types, or one handler in total and some mutable memory to track the still open resources.


To use linear types for everything will require new libraries, no?


Depends in your definition of "everything"

If you want to manually manage all memory, then yes you'll have to not use `base` and any pure functional data structure.

There's been a lot of "this feature is going to ruin the ecosystem" FUD but it's not much more that that imo


There is `linear-base`[1] for this.

[1] https://github.com/tweag/linear-base


I wasn't suggesting this would "ruin the ecosystem". Rather, I was suggesting we might end up with two ecosystems.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: