Search code examples
haskellfunctorfoldtraversal

An example of a Foldable which is not a Functor (or not Traversable)?


A Foldable instance is likely to be some sort of container, and so is likely to be a Functor as well. Indeed, this says

A Foldable type is also a container (although the class does not technically require Functor, interesting Foldables are all Functors).

So is there an example of a Foldable which is not naturally a Functor or a Traversable? (which perhaps the Haskell wiki page missed :-) )


Solution

  • Here's a fully parametric example:

    data Weird a = Weird a (a -> a)
    
    instance Foldable Weird where
      foldMap f (Weird a b) = f $ b a
    

    Weird is not a Functor because a occurs in a negative position.