[Haskell-cafe] Having trouble with instance context

Daniel Fischer daniel.is.fischer at googlemail.com
Wed Feb 23 16:20:04 CET 2011


On Wednesday 23 February 2011 15:42:46, Kurt Stutsman wrote:
> Antoine Latter wrote:
> > As the error says, compiling with the flag '-XFlexibleInstances' will
> > make the message go away.
> >
> > You can also add a language pragma to the top of your source file:
> >
> > {-# LANGUAGE FlexibleInstances #-}
> >
> > Antoine
>
> When I enable that flag, I then also have to enable
> -XUndecidableInstances. It does work with those extensions enabled.
>
> When I was reviewing the Haskell language specification on haskell.org,
> it certainly looked like what I was doing was supported by the language.

No, it's not. The language report says an instance head must have the form

(tyCon a1 ... an),

where tyCon is a type constructor and a1 ... an are *distinct* type 
variables (appropriate in number so that the head has the correct kind).

In instance (Enum e) => Test e where ..., the tyCon is not present.

Since this is too restrictive for many cases, most implementations have 
extensions allowing more liberal instance declarations (omitting the tyCon 
part, allowing repeated type variables, ...).

Note however, that the above instance means "all types are instances of 
Test, and using a Test method on a type which doesn't belong to Enum is a 
static error" in GHC [because the instance selection in GHC doesn't take 
the part before the '=>' into account, so it sees 'instance Test e where'].
If you want to declare any other instances of Test, you need to enable 
OverlappingInstances, which is a whole 'nother can of worms.

> I found some comments on GHC's site about the reasoning behind these
> flags, but I couldn't tell if they were restrictions GHC had added to
> their implementation or if they derive from the language spec.

They're not restrictions but extensions.

> Is this kind of instance allowed by the spec?
>
> Thanks again,
> Kurt
>




More information about the Haskell-Cafe mailing list