为什么是->不是函子? [英] Why is a -> a not a functor?

查看:77
本文介绍了为什么是->不是函子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体指的是> https://bartoszmilewski.com/2015/04/07 /natural-transformations/

作者说这不是函子".

我可以将fmap :: (a -> b) -> (a -> a) -> (b -> b)定义为fmap f aa = id,这似乎符合函子定律.

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

我不是说为什么它不是X语言中的Functor类型类的明确组成部分,我只是说为什么不将其视为函子.

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.

推荐答案

在Haskell的上下文中,我认为您是在谈论newtype Endo a = Endo (a -> a)(使用新类型来获取所需的* -> *类型).

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).

实际上我们可以定义

instance Functor Endo where
    fmap _ _ = Endo id

但函子定律之一是fmap id = id,即,使用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)

应该会导致Endo toUpper,但是您的代码将其设为Endo id.其中一个将'a'转换为'A',另一个将'a'转换为'a'.

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'.

这篇关于为什么是->不是函子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆