0

I am trying to implement phantom Applicative functor from Monoid, described in section 4 in paper Applicative programming with effects:

newtype Accy o a = Acc {acc :: o}

instance Monoid o => Applicative (Accy o) where
    pure _              = Acc mempty
    Acc o1 <*> Acc o2   = Acc (o1 `mappend` o2)

However, I get following error:

    * Could not deduce (Functor (Accy o))
        arising from the superclasses of an instance declaration
      from the context: Monoid o
        bound by the instance declaration
        at phantom-applicative-functor.hs:7:10-41
    * In the instance declaration for `Applicative (Accy o)'
  |
7 | instance Monoid o => Applicative (Accy o) where
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I have tried adding language extension such as:

{-# LANGUAGE MultiParamTypeClasses #-}

But, it didn't help. Is it possible to implement the phantom applicative function as described in the paper?

1 Answers1

4

As the error says, in addition to the Applicative instance you must give a Functor instance. You can write it yourself:

instance Functor (Accy o) where fmap _ (Acc o) = Acc o

or ask GHC to write it for you:

{-# LANGUAGE DeriveFunctor #-}
newtype Accy o a = Acc {acc :: o} deriving Functor
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380