haskellghcimonad-transformerswinghci

Haskell home-made monad transformer unable to Show itself in GHCi


I am dabbing with simple monad transformers as presented in http://www.cs.nott.ac.uk/~nhn/MGS2006/LectureNotes/lecture03-9up.pdf

My error-handling transformer has type

newtype ET m a = ET (m (Maybe a))

I have implemented all the necessary plumbing and I am able to couple it with the identity monad (which in my little sandbox is called I) and write/compile non trivial functions.

But I am unable to print any resulting value onscreen. Message is:

No instance for (Show (ET I Value)) arising from a use of ‘print’

Maybe is imported. Both I and Value derive Show and display on their own without problems. It is the mix with ET that won't show. I see two ways:

How can I show an ET I Value in my REPL?


Solution

  • One of the purposes of standalone deriving is that sometimes the compiler cannot infer the required constraint to make a certain instance, even though the actual code is still derived mechanically. So you just need to know what constraint to give it:

    {-# LANGUAGE StandaloneDeriving, UndecidableInstances #-} 
    
    newtype ET m a = ET (m (Maybe a))
    deriving instance Show (m (Maybe a)) => Show (ET m a)