Why is a -> a not a functor?

一笑奈何 提交于 2019-12-11 06:43:42

问题


Specifically referring to https://bartoszmilewski.com/2015/04/07/natural-transformations/

Author says "This is not a functor".

I can define fmap :: (a -> b) -> (a -> a) -> (b -> b) as fmap f aa = id, which seems to adhere to the functor laws.

I don't mean why it's not explicitly part of the Functor typeclass in X language, I just mean why it wouldn't be acknowledged as a functor.


回答1:


In the context of Haskell, I think you're talking about newtype Endo a = Endo (a -> a) (using a newtype to get the required * -> * kind).

Indeed we could define

instance Functor Endo where
    fmap _ _ = Endo id

But one of the Functor laws is fmap id = id, i.e. fmapping with id has to be the same as doing nothing. Your suggested definition violates this rule:

fmap id (Endo toUpper)

should result in Endo toUpper, but your code makes it Endo id. One of those transforms 'a' to 'A', the other turns 'a' into 'a'.



来源:https://stackoverflow.com/questions/48853854/why-is-a-a-not-a-functor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!