haskellmonoidsfoldable

Why does mconcat require a list rather than a Foldable?


While looking at the definition of Monoid I noticed that mconcat has the following definition (source):

mconcat :: Monoid a => [a] -> a
mconcat = foldr mappend mempty

Why would the signature limit this to [a] rather than the more generic Foldable like this?

mconcat' :: (Foldable t, Monoid a) => t a -> a
mconcat' = foldr mappend mempty

Is this for historical reasons? Or would this more generic implementation make it harder for specific types to provide an optimized version of it, as is the case e.g. for [] which uses list comprehension (source)?


Solution

  • Though I don't know of an actual discussion about such a proposal, here are some conceivable reasons: