[Haskell-cafe] No instance for (Eq v) trouble

martin martin.drautzburg at web.de
Sat Mar 22 11:50:14 UTC 2014

Hello all,

how can I make a type an instance of the Show class, if the functions used to implement show require e.g. Eq ?

I have a class:

class Behavior b where
        at :: b a -> Day -> a

and an instance

data Change v = Change v [(Day,v)]

instance Behavior Change where ...

and another instance (a Behavior of Behavior)

data WeekdayPattern v = WeekdayPattern (Change v)

instance Behavior WeekdayPattern where
       at (WeekdayPattern change) day = at change (day `mod` 7)

And I wanted to write my own show function for WeekdayPattern. I started off like this:

instance Show (WeekdayPattern v) where
        show wdp = show $ groupBy sameValue expanded
                    expanded  = zip (map (at wdp)  [0..6]) [0..6]
                    sameValue x y = (fst x) == (fst y)


But I get:

No instance for (Eq v)
      arising from a use of `sameValue'

I believe the compiler wants to tell me, that it has no reason to assume that the values (the "v"s) can be compared for
equality. Indeed the class itself and its "at" function do not require this (but it wouldn't hurt to add a constraint)

I tried inserting an (Eq v) => in various places of the class or data declaration, but the compiler either didn't like
that or it did not solve the problem. There is no problem just writing a show-like method as in

xshow :: (Eq v, Show v) => (WeekdayPattern v) -> String

but I cannot make this the "show" function of the Show class.

More information about the Haskell-Cafe mailing list