How to fix DatatypeContexts?

p.k.f.holzenspies at p.k.f.holzenspies at
Thu Jul 18 15:03:51 CEST 2013

> I've also been experiencing this a lot in class instances, such as:
> class Foo f where
>     foo :: a -> f a
> data Bar f a = Foo f => Bar {bar :: f a}
> instance Foo (Bar f) where
>     foo a = Bar (foo a)
> Is there any way to avoid repeating the Foo f constraint in the Bar f
> instance?

Dear Harry, et al,

The problem here is that GADT-constructors simply "carry" the appropriate dictionaries. When you *produce* an element of a GADT where the constructor requires a dictionary, you must provide it there. In this case, for a variable f, you don't have a dictionary, so you could read the constraint and instance head as "if you give me a Foo-dictionary for f, then I will wrap it in a Bar."

Another way of looking at it is that the type "Bar f" only 'exists' for 'f's that are 'Foo's. If you don't know whether a particular 'f' is a 'Foo', you don't know whether Bar f exists.

In short, I think that there is no such way.


More information about the Glasgow-haskell-users mailing list