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

Erik Hesselink hesselink at gmail.com
Sat Mar 22 11:58:21 UTC 2014


Doesn't 'instance Eq v => Show (WeekdayPattern v)' work?

Erik

On Sat, Mar 22, 2014 at 12:50 PM, martin <martin.drautzburg at web.de> wrote:
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list