I'm looking at the documentation for Data.Traversable
and came across fmapDefault
- https://downloads.haskell.org/~ghc/latest/docs/html/libraries/base/Data-Traversable.html#g:3
fmapDefault :: Traversable t => (a -> b) -> t a -> t b
The documentation states that -
This function may be used as a value for fmap in a Functor instance, provided that traverse is defined.
So presumably it can be used to derive an fmap
for a Traversable
instance. However, Traversable
has Functor
as a superclass.
class (Functor t, Foldable t) => Traversable t where
...
So you cannot define a Traversable
instance without defining the Functor
instance first! And wherever you have a Traversable
, you have access to an fmap
, which is equivalent to (and perhaps more efficient than) fmapDefault
.
So where would one use fmapDefault
, instead of the much more familiar fmap
?