Search code examples
haskellfunctor

Arrow operator in functor instance declarations?


I'm going through these Haskell 'intermediate' exercises, I've made the following instances:

class Fluffy f where
  furry :: (a -> b) -> f a -> f b

instance Fluffy [] where
  furry f [] = []

instance Fluffy Maybe where
  furry f (Just e) = Just (f e)
  furry f (Nothing) = Nothing

However, the third question syntax has stumped me:

instance Fluffy ((->) t) where
  ...

I've read up on the arrow operator and also read the answer to this which explains the role of (->) in a Monad instance. However I don't quite understand how (->) works in the context of Functors?


Solution

  • We have:

    class Fluffy f where
      furry :: (a -> b) -> f a -> f b
    

    We want to define:

    instance Fluffy ((->) t) where
      furry = ...
    

    This means that in the above instance furry should have the type (a -> b) -> f a -> f b where f is ((->) t), or in other words:

    furry :: (a -> b) -> ((->) t) a -> ((->) t) b
    

    Just as ((+) 2) 3 is the same as 2 + 3, ((->) X) Y is the same as X -> Y (it's curried operator application and it even works at the type level):

    furry :: (a -> b) -> (t -> a) -> (t -> b)
    

    We can read the above signature as "given a function from a to b and a function from t to a, return a function from t to b".

    Now you just have to implement it. :-)