0

When I try to implement the LYAH instantiation of a Writer Monad with a Monoid as the constraining type class by the book as written here I get the Output stated afterwards in my file tryouts.hs:

newtype Writer w a = Writer { runWriter :: (a, w) } 

instance (Monoid w) => Monad (Writer w) where  
    return x = Writer (x, mempty)  
    (Writer (x,v)) >>= f = let (Writer (y, v')) = f x in Writer (y, v `mappend` v')  


OUTPUT:

   * Could not deduce (Applicative (Writer w))
        arising from the superclasses of an instance declaration
      from the context: Monoid w
        bound by the instance declaration at tryouts.hs:788:10-39
    * In the instance declaration for `Monad (Writer w)'

So far I have tried to test to see if there already existed an implementation of the specific writer. That doesn't seem to be the case.

I have looked at similar posts (Make Monoid instance of Writer (Haskell)) but haven't been able to understand the answers given and suspect it is because they vary in nature compared to mine, where it seems that it is the implementation is of a Monoid rather than a Monad.

What does it mean that the Monoid can't deduce an (Applicative (Writer W))?

Isn't a Monad necessarily an instantiation of a Monoid?

How can I adjust the code to instantiate the writer Monad correctly (does the LYAH tutorial implement it wrongly)?

Piskator
  • 605
  • 1
  • 9

0 Answers0