Reading this Wikibook about Haskell and Category Theory basics, I learn about Functors:
A functor is essentially a transformation between categories, so given categories C and D, a functor F : C -> D
maps any object A in C to F(A), in D.
maps morphisms f : A -> B in C to F(f) : F(A) -> F(B) in D.
... which sounds all nice. Later an example is provided:
Let's have a sample instance, too:
instance Functor Maybe where
fmap f (Just x) = Just (f x)
fmap _ Nothing = Nothing
Here's the key part: the type constructor Maybe takes any type T to a new type, Maybe T. Also, fmap restricted to Maybe types takes a function a -> b to a function Maybe a -> Maybe b. But that's it! We've defined two parts, something that takes objects in Hask to objects in another category (that of Maybe types and functions defined on Maybe types), and something that takes morphisms in Hask to morphisms in this category. So Maybe is a functor.
I understand how the definition of fmap
is key. I am confused about how the "type constructor Maybe" provides the first part. I would have rather expected something like pure
.
If I get it right, Maybe
rather maps C
to D
. (Thus being a morphism on category level, which might be a requirement for a Functor)
I guess you could rephrase my question like this: Is there a Functor that does not have an obvious implementation of pure
?
Functor
that does not admitpure
isdata Void a
. The instance looks likeinstance Functor Void where { fmap f x = case x of {} }
. (I'm not making this an answer because I don't think this example is particularly enlightening, even though it answers the only question you actually ask in the body.)Functor
that doesn't admitpure
: if you have any valuev
in theFunctor
you can definepure x = x <$ v
. And I think every choice forpure
is of this form, too. Of course this is not usually very unique.