Is it possible to exclude the use of UndecidableInstances in this instance of Show for Free Monad?

I just tried to circle my head around the free monads; As a tutorial, I managed to write an instance Showfor the following type Free:

{-# LANGUAGE FlexibleContexts, UndecidableInstances #-}

-- Free monad datatype
data Free f a = Return a | Roll (f (Free f a))

instance Functor f => Monad (Free f) where
    return = Return
    Return a >>= f = f a
    Roll ffa >>= f = Roll $ fmap (>>= f) ffa

-- Show instance for Free; requires FlexibleContexts and
-- UndecidableInstances
instance (Show (f (Free f a)), Show a) => Show (Free f a) where
    show (Return x) = "Return (" ++ show x ++ ")"
    show (Roll ffx) = "Roll (" ++ show ffx ++ ")"


-- Identity functor with Show instance
newtype Identity a = Id a deriving (Eq, Ord)

instance Show a => Show (Identity a) where
    show (Id x) = "Id (" ++ show x ++ ")"

instance Functor (Identity) where
    fmap f (Id x)= Id (f x)


-- Example computation in the Free monad
example1 :: Free Identity String
example1 = do x <- return "Hello"
              y <- return "World"
              return (x ++ " " ++ y)

Use UndecidableInstancesbothers me a bit; is there any way to do without it? All that Google gives is this blog post by Edward Kemt , which, as I understand it, has basically the same class definition Showas me.

+5
source share
1 answer

In fact, you can eliminate the UndecidableInstance requirement for Showhere, although you cannot do the same for Reador Eq.

, -, , . , :

{-# LANGUAGE FlexibleContexts #-}

module Free (Free(..)) where

, Show.

newtype Showable = Showable (Int -> ShowS)

showable :: Show a => a -> Showable
showable a = Showable $ \d -> showsPrec d a

instance Show Showable where
    showsPrec d (Showable f) = f d

, Showable, Show (f Showable) , a, Show. , . / / , , , , .

Show.

data Free f a = Pure a | Free (f (Free f a))

instance (Functor f, Show (f Showable), Show a) => Show (Free f a) where
  showsPrec d (Pure a)  = showParen (d > 10) $ showString "Pure " . showsPrec 10 a
  showsPrec d (Free as) = showParen (d > 10) $ showString "Free " . showsPrec 10 (fmap showable as)

, , FlexibleContexts, - Haskell 98, .

, ad, .

+11

All Articles