[英]Is (\f -> fmap f id) always equivalent to arr?
Some instances of Category
are also instances of Functor
. Category
某些实例也是Functor
实例。 For example: 例如:
{-# LANGUAGE ExistentialQuantification, TupleSections #-}
import Prelude hiding (id, (.))
import Control.Category
import Control.Arrow
data State a b = forall s. State (s -> a -> (s, b)) s
apply :: State a b -> a -> b
apply (State f s) = snd . f s
assoc :: (a, (b, c)) -> ((a, b), c)
assoc (a, (b, c)) = ((a, b), c)
instance Category State where
id = State (,) ()
State g t . State f s = State (\(s, t) -> assoc . fmap (g t) . f s) (s, t)
(.:) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
(.:) = fmap . fmap
instance Functor (State a) where
fmap g (State f s) = State (fmap g .: f) s
instance Arrow State where
arr f = fmap f id
first (State f s) = State (\s (x, y) -> fmap (,y) (f s x)) s
Here arr f = fmap f id
for instance Arrow State
. 这里arr f = fmap f id
instance Arrow State
。 Is this true for all instances of Category
which are also instances of Functor
? 这对于所有Category
实例都是如此,它们也是Functor
实例吗? The type signatures are: 类型签名是:
arr :: Arrow a => (b -> c) -> a b c
(\f -> fmap f id) :: (Functor (a t), Category a) => (b -> c) -> a b c
It seems to me that they should be equivalent. 在我看来,它们应该是等价的。
First let's be clear what Arrow C
means. 首先让我们清楚Arrow C
含义。 Well, it's two quite separate things combined – in my book, 嗯,这是两个完全不同的东西 - 在我的书中,
arr
comes from the latter. arr
来自后者。 “Generalise” Hask ? “概括” 哈斯克 ? What this means is just to have a mapping from the category Hask to C
. 这意味着从Hask类到C
类的映射。 – And mathematically, mapping from one category to another is exactly what a functor does! - 从数学上讲,从一个类别到另一个类别的映射正是仿函数所做的! (The standard Functor
class actually covers only a very specific sort of functors, namely endofunctors on Hask .) arr
is the morphism-aspect of a non-endofunctor, namely the “canonical embedding functor” Hask → C
. (标准的Functor
类实际上只涵盖了一种非常特殊的函子,即Hask上的endofunctors 。) arr
是非endofunctor的态射方面,即“规范嵌入函子” Hask → C
。
From this point of view, the first two arrow laws 从这个角度来看,前两箭法
arr id = id
arr (f >>> g) = arr f >>> arr g
are just the functor laws. 只是算子法则。
Now, what does it mean if you're implementing a Functor
instance for a category? 现在,如果您要为类别实现Functor
实例,这意味着什么? Why, I daresay it simply means you're expressing that same canonical embedding functor, but via the necessary representation of C
back in Hask (which makes it an endofunctor overall). 为什么,我敢说它只是意味着你表达的是相同的规范嵌入函子,而是通过必要表示C
回Hask(这使得它整体的endofunctor)。 Hence I'd argue that yes, \\f -> fmap f id
should be equivalent to arr
, since basically they're two ways of expressing the same thing. 因此,我认为是的, \\f -> fmap f id
应该等同于arr
,因为基本上它们是两种表达同一事物的方式。
Here is a derivation to supplement leftaroundabout's explanation. 这是一个补充左撇子解释的推导。 For clarity, I will reserve (.)
and id
for (->)
, and use (<<<)
and id'
for the general Category
methods. 为清楚起见,我将保留(.)
和(->)
id
,并使用(<<<)
和id'
作为一般的Category
方法。
We begin with preComp
, also known as (>>>)
: 我们从preComp
开始,也称为(>>>)
:
preComp :: Category y => y a b -> (y b c -> y a c)
preComp v = \u -> u <<< v
fmap
commutes with natural transformations between Hask endofunctors. fmap
通过Hask endofunctors之间的自然转换进行通信。 For a Category
which also has a Functor
instance, preComp v
is a natural transformation (from yb
to ya
), and so it commutes with fmap
. 对于也有Functor
实例的Category
, preComp v
是一个自然转换(从yb
到ya
),因此它与fmap
。 It follows that: 它遵循:
fmap f . preComp v = preComp v . fmap f
fmap f (u <<< v) = fmap f u <<< v
fmap f (id' <<< v) = fmap f id' <<< v
fmap f v = fmap f id' <<< v
That's our candidate arr
! 这是我们的候选人arr
! So let's define arr' f = fmap f id'
. 所以让我们定义arr' f = fmap f id'
。 We can now verify that arr'
follows the first arrow law... 我们现在可以验证arr'
遵循第一条箭法......
-- arr id = id'
arr' id
fmap id id'
id'
... and the second one too: ......还有第二个:
-- arr (g . f) = arr g <<< arr f
arr' (g . f)
fmap (g . f) id'
(fmap g . fmap f) id'
fmap g (fmap f id')
fmap g (arr' f)
fmap g id' <<< arr' f -- Using the earlier result.
arr' g <<< arr' f
I suppose that is as far as we can get. 我想这是我们能得到的。 The other five arrow laws involve first
, and as leftaroundabout points out arr
and first
are independent. 其他五个箭头法则first
涉及,并且左下角指出arr
和first
是独立的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.