Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

math - Why Functor class has no return function?

From categorical point of view, functor is pair of two maps (one between objects and another between arrows of categories), following some axioms.

I have assumed, what every Functor instance is similar to mathematical definition i.e. can map both objects and functions, but Haskell's Functor class has only function fmap which maps functions.

Why so?

UPD In other words:

Every Monad type M has an function return :: a -> M a.

And Functor type F has no function return :: a -> F a, but only F x constructor.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, there are two levels: types and values. As objects of Hask are types, you can map them only with type constructors, which have the * -> * kind:

  • α -> F α (for Functor F),
  • β -> M β (for Monad M).

Then for a functor you need a map on morphisms (i.e. functions, which are values): it's just fmap :: (α -> β) -> (F α -> F β).

So far, I guess, I'm not saying anything new. But important thing is that return :: α -> M α of Monad is not a mapper of a type α to the M α as you may think. Regarding to the math definition of a monad, return corresponds to a natural transformation from Id functor to the M functor. Just that this Id functor is kind of implicit. The standard definition of monad requires also another natural transformation M ? M -> M. So translating it to Haskell would be like

class Functor m => Monad m where
    return :: Id α -> m α
    join :: m (m α) -> m α

(As a side-note: these two natural transformations are actually the unit and multiplication, which make monad a monoid in the category of endofunctors)

The actual definition differs but is equivalent. See Haskell/wiki on that.

If you take the composition-like operator derived form the standard bind >>= :: m α -> (α -> m β) -> m β:

(>=>) :: Monad m => (α -> m β) -> (β -> m γ) -> (α -> m γ)
f >=> g = a => f a >>= g

you can see, that it's all actually about the Kleisli category. See also the article on nLab about monads in computer science.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...