[Haskell] Type Class Question

Cale Gibbard cgibbard at gmail.com
Mon Nov 21 19:53:19 EST 2005


data X = X deriving Show

bar :: X -> String
bar x = show x

There's no need for the class constraint at all. If it's an instance
of Show, then you're okay with just applying show to it. There's no
need to actually assert that it's actually an instance of Show again.

The only purpose of class constraints is to restrict polymorphism. If
a function isn't polymorphic to begin with, you should never need
them.

 - Cale

On 21/11/05, Paul Govereau <govereau at eecs.harvard.edu> wrote:
> Hello,
>
> I was hoping that someone could answer a question I have about the
> type class system. In Haskell, I cannot write a term with an exact
> constraint:
>
> > data X = X
> > bar :: Show X => X -> String
> > bar x = show x
>
> According to the Haskell 98 report, a qualifier can only be applied to
> type variables, but I don't see where the trouble is. The term seems
> to have reasonable type, and I don't see any reason why the
> dictionary-passing translation shouldn't work out; I am wondering what
> problems you run into if this restriction is lifted?
>
> Note, with GHC and Glasgow extensions you can write this program:
>
> > data Y a = Y
> > foo :: Show (Y a) => Y a -> String
> > foo x = show x
>
> However, the first program is still ruled out. Are there any
> type-class experts out there that can offer an explanation?
>
> Thanks,
> Paul
> _______________________________________________
> Haskell mailing list
> Haskell at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>


More information about the Haskell mailing list