1
votes

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?

1
newtype Ntype a b = N (a -> b) then instance Functor (Ntype a) where ...Alec

1 Answers

0
votes

Suggested by Alex:

newtype Ntype a b = N (a -> b)

instance Functor (Ntype a) where
    fmap f (N g) = N (f . g)

Thanks Alex!