How to fix DatatypeContexts?

Sjoerd Visscher sjoerd at
Thu Jul 18 15:12:39 CEST 2013

On Jul 18, 2013, at 2:35 PM, harry <voldermort at> wrote:

> Sjoerd Visscher-2 wrote
>> equal pair at Pair{} = foo pair == bar pair
> Interesting solution, I didn't know you could do that. (Do all those who
> suggested GADTs - you can add a type context to the constructor of a regular
> data type as well, they don't bring you anything here.)

What it brings in my opinion is clarity, but that's subjective of course.

> 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?

No, you can only omit it where you provide Foo f in another way. With equal you provide it with the Pair value. But here you're constructing a value, so you'll have to provide the Foo f. If all the methods in the class receive a value, then you can omit it, f.e.:

class Baz f where
  baz :: f a -> a

data Bar f a = Baz f => Bar {bar :: f a}

instance Baz (Bar f) where
  baz (Bar fa) = baz fa


More information about the Glasgow-haskell-users mailing list