Skip to content

Speed up certain Foldable NonEmpty methods

Yitzchak Gale pointed out on Reddit that some Foldable methods are allowed to take default definitions for NonEmpty when they probably shouldn't. The biggest problem appears to be foldr1, which ends up with a fairly atrocious definition. Yitz seems to think we should also provide a custom definition of null, to avoid relying on optimizations, but at present there is no obvious need for that. He doesn't mention this, but the definition of length should be changed. Indeed, the default definition of length would be better than the custom definition we currently have. I also noticed that we don't mark foldl1 INLINE. At present its unfolding is optimized, so its foldr is not exposed. Thus, for example,

foldl1 (+) $ 1 :| [2..n]

won't fuse. We can certainly mark foldl1 INLINE to fix this (INLINABLE doesn't do the trick), but it would be nice to know if there's another way.

Trac metadata
Trac field Value
Version 8.2.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Core Libraries
Test case
Differential revisions
BlockedBy
Related
Blocking
CC YitzGale, mpickering, nomeata
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information