1

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?

Community
  • 1
  • 1
Rahn
  • 4,787
  • 4
  • 31
  • 57

1 Answers1

0

Suggested by Alex:

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

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

Thanks Alex!

Community
  • 1
  • 1
Rahn
  • 4,787
  • 4
  • 31
  • 57