Hi all, I have written an article Make Invalid Laziness Unrepresentable explaining how to avoid thunk leaks in Haskell programs, and indirectly introducing my new library strict-wrapper, for lightweight strict versions of basic data types.
Stupid question: documentation uses foldl'
as an example, would it be possible to write a version of foldl'
that can take a regular strictness-oblivious function and make it the required amount of strict to begin with?
Something like:
foldlStrictly :: (Strictly b, Foldable t) => (b -> a -> b) -> b -> t a -> b
That would not require the user to specifically design (b -> a -> b)
to be the right kind of strict, or even fully grasp the issue, but have foldl'
automatically do the strictness boilerplate internally if possible and error at compile time if not?
Hmm, well, sort of. unstrict . strict :: Strictly b => b -> b
basically forces one level deep, so if that’s what you mean by “do the strictness boilerplate internally” then yes. But I’m not sure that is particularly practical. Another option is to replace Strictly
with NFData
and do a deepseq
each time around the loop. As mentioned in my article that is prohibitively expensive.
I think it’s just best to design one’s types correctly.