
Excellent! This has all been very helpful. Thanks a lot everybody! :-)
-Corey
On 12/14/07, Benja Fallenstein
Hi Corey,
On Dec 14, 2007 8:44 PM, Corey O'Connor
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 functor?
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."
http://en.wikipedia.org/wiki/Functor
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 (x+1).)
Hope this helps with seeing the correspondence? :-) - Benja
-- -Corey O'Connor