[Haskell-beginners] Confused by type constraints

Guy guytsalmaves-h at yahoo.com
Fri Jun 3 16:13:43 CEST 2011


On 03/06/2011 16:55, Daniel Schoepe wrote:
> On Fri, 03 Jun 2011 16:04:05 +0300, Guy<guytsalmaves-h at yahoo.com>  wrote:
>> I have some code resembling
>>
>> 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
>>
>> GHC insists on Foo f =>  in the instance declaration. However, the definition of Bar guarantees that this will always be
>> the case. Why do I have to state this explicitly?
>
> The constraints in the data type declaration only affect the
> constructors. It's a known issue, unfortunately there doesn't seem to be
> a way around it.

It seems to work for simpler examples. For example, this is OK

data X x = Show x => X x

s :: X x -> String
s (X x) = show x

I don't have to add a Show x => type constraint to s.




More information about the Beginners mailing list