Some instances of Category
are also instances of 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
. Is this true for all instances of Category
which are also instances of 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. Well, it's two quite separate things combined – in my book,
arr
comes from the latter. “Generalise” Hask? What this means is just to have a mapping from the category Hask to 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
.
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? 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). 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.