views:

351

answers:

2

I am trying to define an algebric type:

data MyType t = MyType t

And make it an instance of Show:

instance Show (MyType t) where
  show (MyType x) = "MyType: " ++ (show x)

GHC complains becasue it cannot deduce that type 't' in 'Show (MyType t)' is actually an instance of Show, which is needed for (show x).

I have no idea where and how do I declare 't' to be an instance of Show?

+13  A: 

Add a type constraint on the type of t:

instance Show t => Show (MyType t) where
  show (MyType x) = "MyType: " ++ (show x)
earl
+9  A: 

You could also just:

data MyType t = MyType t
    deriving Show

if you want a regular show format.

Don Stewart