Aad*_*hah 9 haskell arrows functor category-theory category-abstractions
一些实例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
Run Code Online (Sandbox Code Playgroud)
这里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
Run Code Online (Sandbox Code Playgroud)
在我看来,它们应该是等价的.
首先让我们明确一下Arrow C是什么意思.嗯,这是两个完全不同的东西 - 在我的书中,
arr来自后者."概括" 哈斯克?这意味着只需要有从类别映射Hask来C.- 从数学上讲,从一个类别到另一个类别的映射正是仿函数所做的!(标准Functor类实际上只涵盖了一种非常特殊的函子,即Hask上的endofunctors.)arr是非endofunctor的态射方面,即"规范嵌入函子" Hask → C.
从这个角度来看,前两箭法
arr id = id
arr (f >>> g) = arr f >>> arr g
Run Code Online (Sandbox Code Playgroud)
只是算子法则.
现在,如果您要Functor为类别实现实例,这意味着什么?为什么,我敢说它只是意味着你表达的是相同的规范嵌入函子,而是通过必要表示C在后面Hask(这使得它整体的endofunctor).因此,我认为是的,\f -> fmap f id应该相当于arr,因为基本上它们是表达同一事物的两种方式.