Search code examples
haskellfunctor

functors from partially applied function type


there is a quesion in Programming in Haskell that says: Complete the following declaration:

instance Functor ((->) a) where

Now as Functor Thing has a type definition of:

instance Functor Thing where    
   --fmap::(a -> b) -> Thing a -> Thing b 

I was wondering if this reduction makes sense:

instance Functor ((->) a) where
    -- fmap::(a -> b) -> ((->) a) a -> ((->) a) b 
    -- therefore
    -- fmap::(a -> b) -> a -> a -> (a -> b) 
    -- therefore
    -- fmap::b -> b

-- update --- I missed brackets, it should have been

instance Functor ((->) a) where
    -- fmap::(a -> b) -> ((->) a) a -> ((->) a) b 
    -- therefore
    -- fmap::(a -> b) -> (a -> a) -> (a -> b) 
    -- therefore
    -- I should be returning a function of a -> b

Solution

  • No, because the a in your instance declaration is not the same a as the one in the type of fmap. You need to assign a type variable in your instance declaration that avoids "capturing" the a in the type of fmap:

    instance Functor ((->) r) where
      fmap :: (a -> b) -> (r -> a) -> (r -> b)