简体   繁体   中英

Haskell type-safe space usage

I really like Haskell, particularly it's strong type system. When I get Haskell programs to compile, they're generally bug free, or at least very close to it.

However, main problem with Haskell is its unknown space usage. At least in say, C++, you can be fairly certain about a program's space usage. It's quite clear when you construct and deconstruct objects.

In Haskell, things as simple as folds can use massive amount of space in thunks if you don't write them correctly. The program crashing due to lack of memory isn't much better than some other bug, arguably worse.

I know there's ways to avoid these space leaks, but I'm looking for type-safe ways to avoid these space leaks. As in, if I get it wrong, I'll get some sort of compile error, not just hope my program crashes out of memory when it's in production. I'm happy to for example, replace standard library functions (maybe, say a fold which has a compile error if it's accumulator isn't strict for example)

Does such a thing exist in Haskell?

It is well-acknowledged that reasoning about space in Haskell in extremely difficult. The old (1987) book by Simon Peyton-Jones

http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/

has a special chapter on this topic.

Still, if one writes Haskell code in particular ways, for example, using simple generators, memory usage can be controlled. The following paper (presented at APLAS 2012) gives an example of reasoning about memory and latency for a quite complex algorithm, linear pretty printing (BTW, standard pretty-printing libraries in Haskell are far from optimal: their formatting time is not O(n), where n is the length of the input). The experimental results confirm the predictions of memory and time complexity. Please see the slides of the APLAS talk, which show plots.

http://okmij.org/ftp/continuations/PPYield/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM