[英]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的態射方面,即“規范嵌入函子” Hask → C
。
從這個角度來看,前兩箭法
arr id = id
arr (f >>> g) = arr f >>> arr g
只是算子法則。
現在,如果您要為類別實現Functor
實例,這意味着什么? 為什么,我敢說它只是意味着你表達的是相同的規范嵌入函子,而是通過必要表示C
回Hask(這使得它整體的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
實例的Category
, preComp v
是一個自然轉換(從yb
到ya
),因此它與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
涉及,並且左下角指出arr
和first
是獨立的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.