
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)] deriving(Show) 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 where 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.

Doesn't 'instance Eq v => Show (WeekdayPattern v)' work?
Erik
On Sat, Mar 22, 2014 at 12:50 PM, martin
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)] deriving(Show)
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 where 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.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, Mar 22, 2014 at 12:50:14PM +0100, martin wrote:
instance Show (WeekdayPattern v) where show wdp = show $ groupBy sameValue expanded where expanded = zip (map (at wdp) [0..6]) [0..6] sameValue x y = (fst x) == (fst y) [...] 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
What's wrong with instance (Show v, Eq v) => Show (WeekdayPattern v) where .... Tom PS It helps answerers if you can post the smallest *complete* code that demonstrates the problem, without commentary interspersed.
participants (3)
-
Erik Hesselink
-
martin
-
Tom Ellis