
In category theory functors are defined between two category of C and D where every object and morphism from C is mapped to D. I am trying to make sense of the above definition with functor class in Haskell. Let say I am dealing with List type. When I define List to be a instance of a functor I am saying the source category (C) is Haskell types and the destination category is List (D). In this the "fmap" is implementation of the mapping between every morphism in my Haskell Categroy (C) to morphism in List cataegory (D). With type constructor I also have the mapping of types (objects in Haskell Category, or my source cataegroy C) to List category (D). So my functor in the catarogy sense is actually the fmap and type constructor. Am I remotely correct? If this is correct.... With this example, then can you then help me understand the transformation between functors and natural transformations? Specifically, what does it means to have two different functors between Haskell cateogry and List category? Thanks, Daryoush

You are on the right track. The usual construction is that Hask is the category (with types as objects and functions as morphisms). Functor F is then an endofunctor taking Hask to itself: a -> F a f -> fmap f So, for F = []: a -> [a] f -> map f Natural transformations are then any fully polymorphic (no context) unary function. The polymorphism is what makes them natural, since there is no method to treat one object (type) different from another. Daryoush Mehrtash wrote:
In category theory functors are defined between two category of C and D where every object and morphism from C is mapped to D.
I am trying to make sense of the above definition with functor class in Haskell. Let say I am dealing with List type. When I define List to be a instance of a functor I am saying the source category (C) is Haskell types and the destination category is List (D). In this the "fmap" is implementation of the mapping between every morphism in my Haskell Categroy (C) to morphism in List cataegory (D). With type constructor I also have the mapping of types (objects in Haskell Category, or my source cataegroy C) to List category (D). So my functor in the catarogy sense is actually the fmap and type constructor. Am I remotely correct?
If this is correct.... With this example, then can you then help me understand the transformation between functors and natural transformations? Specifically, what does it means to have two different functors between Haskell cateogry and List category?
Thanks,
Daryoush

I am not sure I follow how the endofunctor gave me the 2nd functor.
As I read the transformation there are two catagories C and D and two
functors F and G between the same two catagories. My problem is that I only
have one functor between the Hask and List catagories. So where does the
2nd functor come into picture that also maps between the same C and D
catagories?
Thanks
Daryoush
On Tue, Apr 21, 2009 at 4:01 PM, Dan Weston
You are on the right track. The usual construction is that Hask is the category (with types as objects and functions as morphisms).
Functor F is then an endofunctor taking Hask to itself:
a -> F a f -> fmap f
So, for F = []:
a -> [a] f -> map f
Natural transformations are then any fully polymorphic (no context) unary function. The polymorphism is what makes them natural, since there is no method to treat one object (type) different from another.
Daryoush Mehrtash wrote:
In category theory functors are defined between two category of C and D where every object and morphism from C is mapped to D.
I am trying to make sense of the above definition with functor class in Haskell. Let say I am dealing with List type. When I define List to be a instance of a functor I am saying the source category (C) is Haskell types and the destination category is List (D). In this the "fmap" is implementation of the mapping between every morphism in my Haskell Categroy (C) to morphism in List cataegory (D). With type constructor I also have the mapping of types (objects in Haskell Category, or my source cataegroy C) to List category (D). So my functor in the catarogy sense is actually the fmap and type constructor. Am I remotely correct?
If this is correct.... With this example, then can you then help me understand the transformation between functors and natural transformations? Specifically, what does it means to have two different functors between Haskell cateogry and List category? Thanks,
Daryoush

List is not a full subcategory of Hask, so it's a bad choice. Namely, types of functions on a list (e.g. [a] -> [a]) are not themselves lists of type [b] for some b, and so are not objects of List (though they are morphisms in it). In Hask, ([a] -> [a]) is both an object and a morphism (in fact, there is a unique object in Hask for every morphism). It turns out (I believe) that the smallest subcategory that fits the Haskell Functor type class is Hask itself. That doesn't mean that the functor is epic: (Int -> Int) is not the type of (fmap f) for any f. So C and D are the same category: Hask. Take a look at: http://en.wikibooks.org/wiki/Haskell/Category_theory#Functors Daryoush Mehrtash wrote:
I am not sure I follow how the endofunctor gave me the 2nd functor.
As I read the transformation there are two catagories C and D and two functors F and G between the same two catagories. My problem is that I only have one functor between the Hask and List catagories. So where does the 2nd functor come into picture that also maps between the same C and D catagories?
Thanks
Daryoush
On Tue, Apr 21, 2009 at 4:01 PM, Dan Weston
mailto:westondan@imageworks.com> wrote: You are on the right track. The usual construction is that Hask is the category (with types as objects and functions as morphisms).
Functor F is then an endofunctor taking Hask to itself:
a -> F a f -> fmap f
So, for F = []:
a -> [a] f -> map f
Natural transformations are then any fully polymorphic (no context) unary function. The polymorphism is what makes them natural, since there is no method to treat one object (type) different from another.
Daryoush Mehrtash wrote:
In category theory functors are defined between two category of C and D where every object and morphism from C is mapped to D.
I am trying to make sense of the above definition with functor class in Haskell. Let say I am dealing with List type. When I define List to be a instance of a functor I am saying the source category (C) is Haskell types and the destination category is List (D). In this the "fmap" is implementation of the mapping between every morphism in my Haskell Categroy (C) to morphism in List cataegory (D). With type constructor I also have the mapping of types (objects in Haskell Category, or my source cataegroy C) to List category (D). So my functor in the catarogy sense is actually the fmap and type constructor. Am I remotely correct?
If this is correct.... With this example, then can you then help me understand the transformation between functors and natural transformations? Specifically, what does it means to have two different functors between Haskell cateogry and List category? Thanks,
Daryoush

Daryoush Mehrtash wrote:
I am not sure I follow how the endofunctor gave me the 2nd functor.
As I read the transformation there are two catagories C and D and two functors F and G between the same two catagories. My problem is that I only have one functor between the Hask and List catagories. So where does the 2nd functor come into picture that also maps between the same C and D catagories?
You don't have a category List, you have the image of the List functor (in Hask). With the definition: data List a =... instance Functor List where fmap =... The type constructor "List" provides the object half of an endofunctor on Hask, and the function "fmap @List" provides the morphism half of the endofunctor. Now, if you declare another endofunctor, G: data G a =... instance Functor G where fmap =... then a natural transformation from List to G must provide a mapping (forall a. List a -> G a) for objects and a mapping (forall a b. (List a -> List b) -> (G a -> G b)) for morphisms, satisfying all the necessary laws. -- Live well, ~wren

Daryoush Mehrtash-2 wrote:
I am not sure I follow how the endofunctor gave me the 2nd functor.
As I read the transformation there are two catagories C and D and two functors F and G between the same two catagories. My problem is that I only have one functor between the Hask and List catagories. So where does the 2nd functor come into picture that also maps between the same C and D catagories?
Consider singleton :: a -> [a] singleton x = [x] Here F is the identity functor, and G is the list functor. And yes, C=D= category of (a subset of) Haskell types. -- View this message in context: http://www.nabble.com/Functor-and-Haskell-tp23166441p23170956.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Here F is the identity functor, and G is the list functor. And yes, C=D= category of (a subset of) Haskell types. Are you saying the function that goes from list functor to singleton funtor is a natural transformation? But aren't they functors to different subset of Haskell Types? The Haskell Wikibooks also sayshttp://en.wikibooks.org/wiki/Haskell/Category_theory#Functors_on_Haskthe same thing:
Functors in Haskell are from *Hask* to *func*, where *func* is the subcategory of *Hask* defined on just that functor's types. E.g. the list functor goes from *Hask* to *Lst*, where *Lst* is the category containing only *list types*, that is, [T] for any type T. The morphisms in *Lst* are functions defined on list types, that is, functions [T] -> [U] for types T, U.
So in your example there is C that is Hask. But there are two D's, D1 that
is all List types, and D2 all singleton types. In this example I guess,
the Singleton types are subset of List types which are subset of Hask.
Is that related to natural transformation or unrelated?
Daryoush
On Wed, Apr 22, 2009 at 12:18 AM, Kim-Ee Yeoh
Daryoush Mehrtash-2 wrote:
I am not sure I follow how the endofunctor gave me the 2nd functor.
As I read the transformation there are two catagories C and D and two functors F and G between the same two catagories. My problem is that I only have one functor between the Hask and List catagories. So where does the 2nd functor come into picture that also maps between the same C and D catagories?
Consider singleton :: a -> [a] singleton x = [x]
Here F is the identity functor, and G is the list functor. And yes, C=D= category of (a subset of) Haskell types.
-- View this message in context: http://www.nabble.com/Functor-and-Haskell-tp23166441p23170956.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Daryoush Weblog: http://perlustration.blogspot.com/

On Wed, Apr 22, 2009 at 03:14:03PM -0700, Daryoush Mehrtash wrote:
The Haskell Wikibooks also says the same thing:
Functors in Haskell are from Hask to func, where func is the subcategory of Hask defined on just that functor's types. E.g. the list functor goes from Hask to Lst, where Lst is the category containing only list types, that is, [T] for any type T. The morphisms in Lst are functions defined on list types, that is, functions [T] -> [U] for types T, U.
That's true, but not a particularly helpful view. Any functor F : C -> D can be viewed as a functor from C to the full subcategory of D on objects of the form F A for A an object of C. But then different functors map to different categories and you can't talk about natural transformations between them. Composing functors also becomes impossible. The simple view is that [], Maybe and Id are all functors from Hask to Hask. Then listToMaybe :: [a] -> Maybe a is a natural transformation from [] to Maybe, because fmap f . listToMaybe = listToMaybe . map f

Thanks for this example I get the point now. (at least i think i do :) )
One more question.... This all being on the same category then the functor
transformation can also be view as a simple morphism too. In this example
the listToMaybe can be viewed as morphism between list and Maybe types that
are both in the Hask categroy too. right? If so then what would viewing
the morphism as natural transformation by you?
Thanks,
Daryoush
P.S. for others interested in the same topic I found this on Haskell wikis
that add some more detail:
http://www.haskell.org/haskellwiki/Category_theory/Natural_transformation
On Wed, Apr 22, 2009 at 3:40 PM, Ross Paterson
On Wed, Apr 22, 2009 at 03:14:03PM -0700, Daryoush Mehrtash wrote:
The Haskell Wikibooks also says the same thing:
Functors in Haskell are from Hask to func, where func is the subcategory of Hask defined on just that functor's types. E.g. the list functor goes from Hask to Lst, where Lst is the category containing only list types, that is, [T] for any type T. The morphisms in Lst are functions defined on list types, that is, functions [T] -> [U] for types T, U.
That's true, but not a particularly helpful view. Any functor F : C -> D can be viewed as a functor from C to the full subcategory of D on objects of the form F A for A an object of C. But then different functors map to different categories and you can't talk about natural transformations between them. Composing functors also becomes impossible.
The simple view is that [], Maybe and Id are all functors from Hask to Hask. Then listToMaybe :: [a] -> Maybe a is a natural transformation from [] to Maybe, because
fmap f . listToMaybe = listToMaybe . map f _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Thursday 23 April 2009 2:44:48 pm Daryoush Mehrtash wrote:
Thanks for this example I get the point now. (at least i think i do :) )
One more question.... This all being on the same category then the functor transformation can also be view as a simple morphism too. In this example the listToMaybe can be viewed as morphism between list and Maybe types that are both in the Hask categroy too. right? If so then what would viewing the morphism as natural transformation by you?
listToMaybe in general wouldn't be a morphism in the category, because morphisms would be from concrete types to other concrete types. [1] So, if you'll excuse some notation I just made up (with a little help from GHC core notation :)): listToMaybe@Int :: [Int] -> Maybe Int listToMaybe@Char :: [Char] -> Maybe Char listToMaybe@String :: [String] -> Maybe String are all morphisms in the alleged Hask category. Each polymorphic function (similar to the above one, at least) defines a family of morphisms like that. *But*, that's what a natural transformation is: a family of morphisms, one for each object in the category, that commute with functor application in a certain way. Thus, one can look at the fully polymorphic listToMaybe as a natural transformation: listToMaybe :: [] -> Maybe -- Dan [1] Maybe you could make up a category where polymorphic types are objects as well, but that doesn't seem to be the way people typically go about applying category theory to Haskell.

Thanks this was helpful.
In many of Conal Elliot's writings I see that he shows that his semantic
function is a natural transformation. Is that just basically showing the
polymorphic nature of his semantic functions, or are there other benifits
you get by showing a particular function is a natural transformation?
Daryoush
On Thu, Apr 23, 2009 at 12:34 PM, Dan Doel
Thanks for this example I get the point now. (at least i think i do :) )
One more question.... This all being on the same category then the functor transformation can also be view as a simple morphism too. In this example the listToMaybe can be viewed as morphism between list and Maybe types
On Thursday 23 April 2009 2:44:48 pm Daryoush Mehrtash wrote: that
are both in the Hask categroy too. right? If so then what would viewing the morphism as natural transformation by you?
listToMaybe in general wouldn't be a morphism in the category, because morphisms would be from concrete types to other concrete types. [1] So, if you'll excuse some notation I just made up (with a little help from GHC core notation :)):
listToMaybe@Int :: [Int] -> Maybe Int listToMaybe@Char :: [Char] -> Maybe Char listToMaybe@String :: [String] -> Maybe String
are all morphisms in the alleged Hask category. Each polymorphic function (similar to the above one, at least) defines a family of morphisms like that. *But*, that's what a natural transformation is: a family of morphisms, one for each object in the category, that commute with functor application in a certain way. Thus, one can look at the fully polymorphic listToMaybe as a natural transformation:
listToMaybe :: [] -> Maybe
-- Dan
[1] Maybe you could make up a category where polymorphic types are objects as well, but that doesn't seem to be the way people typically go about applying category theory to Haskell. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Daryoush Mehrtash wrote:
Thanks this was helpful.
In many of Conal Elliot's writings I see that he shows that his semantic function is a natural transformation. Is that just basically showing the polymorphic nature of his semantic functions, or are there other benifits you get by showing a particular function is a natural transformation?
Daryoush
Natural transformations give you a lot of very regular structure to work with. This is helpful down the road when trying to prove other laws, e.g. about how your thing (that happens to be a NT) interacts with other things. For example, the catamorphism fusion law: eps : F :~> G ------------- cata_G psi . cata_F (in_G . eps) = cata_F (psi . eps) Because eps is a natural transformation we know that it can't be doing any of the bad things that would violate the equation. Often times when trying to prove something non-trivial you find yourself wishing "if only I had a very well-behaved morphism to go here". The components of NTs are frequently the exact thing you want there. -- Live well, ~wren

Daryoush Mehrtash-2 wrote:
singleton :: a -> [a] singleton x = [x]
Here F is the identity functor, and G is the list functor. And yes, C=D= category of (a subset of) Haskell types.
Are you saying the function that goes from list functor to singleton funtor is a natural transformation?
Here *singleton* is the natural transformation from the identity functor to the list functor. Daryoush Mehrtash-2 wrote:
But aren't they functors to different subset of Haskell Types?
They're usually treated as endofunctors on Hask, for reasons Ross Paterson has given. -- View this message in context: http://www.nabble.com/Functor-and-Haskell-tp23166441p23189784.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Daryoush Mehrtash
In category theory functors are defined between two category of C and D where every object and morphism from C is mapped to D.
I am trying to make sense of the above definition with functor class in Haskell. Let say I am dealing with List type. When I define List to be a instance of a functor I am saying the source category (C) is Haskell types and the destination category is List (D). In this the "fmap" is implementation of the mapping between every morphism in my Haskell Categroy (C) to morphism in List cataegory (D). With type constructor I also have the mapping of types (objects in Haskell Category, or my source cataegroy C) to List category (D). So my functor in the catarogy sense is actually the fmap and type constructor. Am I remotely correct?
The type constructors map objects from C to objects from F(C). The 'fmap' function maps morphisms from C to morphisms from F(C). You can see this immediately by looking at the type of 'fmap' (note that (->) is right-associative): fmap :: Functor f => (a -> b) -> (f a -> f b) Greets, Ertugrul. -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/
participants (7)
-
Dan Doel
-
Dan Weston
-
Daryoush Mehrtash
-
Ertugrul Soeylemez
-
Kim-Ee Yeoh
-
Ross Paterson
-
wren ng thornton