There is an example program in Learn You a Haskell:
instance Functor ((->) a) where
fmap = (.)
While I have trouble compiling it:
Duplicate instance declarations:
instance Functor ((->) a) -- Defined at partiallyApplied.hs:6:10
instance Functor ((->) r) -- Defined in ‘GHC.Base’
As How do you override Haskell type class instances provided by package code? mention, I should define a new type for the declaration of Functor
. I try so but fail:
newtype Ntype a = N ((->) a)
instance Functor ((->) a) where
fmap = (.)
• Expecting one more argument to ‘(->) a’
Expected a type, but ‘(->) a’ has kind ‘* -> *’
• In the type ‘(->) a’
In the definition of data constructor ‘N’
In the newtype declaration for ‘NewType’
How do I make it works?
newtype Ntype a b = N (a -> b)
theninstance Functor (Ntype a) where ...
– Alec