【发布时间】:2015-07-24 19:29:31
【问题描述】:
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法律足以由instance Arrow a => Functor (a s) where fmap f v = v >>> arr f定义一个守法的Functor实例。也许这与参数化一起足以确保这也是唯一守法的实例,尽管我还没有弄清楚细节,所以我不会声称它是真的。 -
请记住,对于函数和箭头,
fmap应该等于(<<<)。 -
This comment 表明我是对的:参数化最多保证一个守法的
Functor实例。所以你的问题的答案似乎是“是”。 -
由于
arr是Arrow的方法,而不是Category,我仍然有些怀疑 - 我怀疑每个Category也是Functor需要是 @987654342 @。然而,反过来也是如此,因为每个Arrowgives not just aFunctor, but anApplicative。 -
@ØrjanJohansen “我怀疑每个
Category也是Functor需要是Arrow” - 事实上,虽然根本问题是arr和first是,正如 leftaroundabout 下面所说,“完全不同的东西结合在一起”。
标签: haskell functor category-theory arrows category-abstractions