Re: [Haskell-cafe] Questions about the Functor class and it's use in "Data types à la carte"
benja.fallenstein at gmail.com
Fri Dec 14 15:37:14 EST 2007
On Dec 14, 2007 8:44 PM, Corey O'Connor <coreyoconnor at gmail.com> wrote:
> The reason I find all this odd is because I'm not sure how the type
> class Functor relates to the category theory concept of a functor. How
> does declaring a type constructor to be an instance of the Functor
> class relate to a functor? Is the type constructor considered a
Recall the definition of functor. From Wikipedia:
"A functor F from C to D is a mapping that
* associates to each object X in C an object F(X) in D,
* associates to each morphism f:X -> Y in C a morphism F(f):F(X)
-> F(Y) in D
such that the following two properties hold:
* F(idX) = idF(X) for every object X in C
* F(g . f) = F(g) . F(f) for all morphisms f:X -> Y and g:Y -> Z."
We consider C = D = the category of types. Note that any type
constructor is a mapping from types to types -- thus it associates to
each object (type) X an object (type) F(X).
Declaring a type constructor to be an instance of Functor means that
you have to provide 'fmap :: (a -> b) -> (f a -> f b)" -- that is, a
mapping that associates to each morphism (function) "fn :: a -> b" a
morphism "fmap fn :: f a -> f b".
Making sure that the two laws are fulfilled is the responsibility of
the programmer writing the instance of Functor. (I.e., you're not
supposed to do this: instance Functor Val where fmap f (Val x) = Val
Hope this helps with seeing the correspondence? :-)
More information about the Haskell-Cafe