[Haskell-cafe] Why distinct tyvars in instance declarations?

Daniel Fischer daniel.is.fischer at web.de
Sun Jun 26 13:07:39 EDT 2005


Am Samstag, 25. Juni 2005 21:22 schrieb Josh Hoyt:
> Hello,
>
> I'm a new Haskeller, and I'm running into a problem attempting to
> declare certain types as instances. I was attempting something that's
>
> effectively equivalent to:
> > class Foo a
> >
> > instance Foo (Either b b)
>
> but GHC complains:
> >     Illegal instance declaration for `Foo (Either b b)'
> >         (The instance type must be of form (T a b c)
> >          where T is not a synonym, and a,b,c are distinct type variables)
> >     In the instance declaration for `Foo (Either b b)'
>
> and so I looked in the report
>
> (http://www.haskell.org/onlinereport/decls.html#sect4.3.2) and found:
> > The general form of the corresponding instance declaration is:
> >
> > instance cx' => C (T u1 ... uk) where { d }
> >
> > where k>=0. The type (T u1 ... uk) must take the form of a type
> > constructor T applied to simple type variables u1, ... uk; furthermore, T
> > must not be a type synonym, and *the ui must all be distinct*. (emphasis
> > mine)
>
> My question is, why this restriction that the types must be distinct?
>
> In particular, I'd like to declare a very specific type (Either String
> String) as an instance. What techniques can I use to accomplish this?
>
> Josh Hoyt

I don't know, why the tyvars must be distinct in Haskell 98, but if you use 
extensions (hugs -98; -fglasgow-exts for ghc(i)), you can declare

instance Foo (Either b b) where ...

or

instance Foo (Either String String) where ...

without problems.

HTH,
Daniel


More information about the Haskell-Cafe mailing list