繁体   English   中英

(\\ f - > fmap f id)总是等同于arr吗?

[英]Is (\f -> fmap f id) always equivalent to arr?

Category某些实例也是Functor实例。 例如:

{-# 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

这里arr f = fmap f id instance Arrow State 这对于所有Category实例都是如此,它们也是Functor实例吗? 类型签名是:

arr               ::                 Arrow    a  => (b -> c) -> a b c
(\f -> fmap f id) :: (Functor (a t), Category a) => (b -> c) -> a b c

在我看来,它们应该是等价的。

首先让我们清楚Arrow C含义。 嗯,这是两个完全不同的东西 - 在我的书中,

arr来自后者。 “概括” 哈斯克 这意味着从Hask类到C类的映射。 - 从数学上讲,从一个类别到另一个类别的映射正是仿函数所做的! (标准的Functor类实际上只涵盖了一种非常特殊的函子,即Hask上的endofunctors 。) arr是非endofunctor的态射方面,即“规范嵌入函子” HaskC

从这个角度来看,前两箭法

arr id = id
arr (f >>> g) = arr f >>> arr g

只是算子法则。

现在,如果您要为类别实现Functor实例,这意味着什么? 为什么,我敢说它只是意味着你表达的是相同的规范嵌入函子,而是通过必要表示CHask(这使得它整体的endofunctor)。 因此,我认为是的, \\f -> fmap f id应该等同于arr ,因为基本上它们是两种表达同一事物的方式。

这是一个补充左撇子解释的推导。 为清楚起见,我将保留(.)(->) id ,并使用(<<<)id'作为一般的Category方法。

我们从preComp开始,也称为(>>>)

preComp :: Category y => y a b -> (y b c -> y a c)
preComp v = \u -> u <<< v

fmap通过Hask endofunctors之间的自然转换进行通信。 对于也有Functor实例的CategorypreComp v是一个自然转换(从ybya ),因此它与fmap 它遵循:

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

这是我们的候选人arr 所以让我们定义arr' f = fmap f id' 我们现在可以验证arr'遵循第一条箭法......

-- arr id = id'
arr' id
fmap id id'
id'

......还有第二个:

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

我想这是我们能得到的。 其他五个箭头法则first涉及,并且左下角指出arrfirst是独立的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM