[Haskell-cafe] FGL instance constraint

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Sat May 1 02:08:43 EDT 2010


"Kevin Quick" <quick at sparq.org> writes:

> I need help understanding how to express the following:
>
>
>>  data (Cls a) => B a = B [a]

I think this only works if you have a forall in there.

>
>>  data GrB a b = GrB (B a)
>
>>  instance Graph GrB where ...
>
> In the methods for the instance specification, I need to perform Cls a operations on a.
>
>  * As shown, the compiler complains that it cannot deduce (Cls a) from
>  the context () on those methods.

You need to explicitly state them again (which is why putting the
constraint in the data definition is pointless).

>  * I can't redefine the Graph methods to introduce the (Cls a)
>  constraint [reasonable]

Not sure if you can.

>  * If I try to express the constraint as part of the Graph instance:
>  "instance (Cls a) => Graph GrB where ..." then it says it's an
>  ambiguous constraint because 'a' isn't mentioned.

Right, because the instance is on GrB _only_, not on the values it contains.

>  * I've tried specifying a functional constraint: "instance (Cls a) => Graph GrB | GrB -> a where ..." but that's not valid for an instance declaration.
>  * I can't include a in the GrB instance: "instance (Cls a) => Graph (GrB a b) where ..." because that's a kind conflict.
>

You're putting the constraint in the wrong places: put the "(Cls a) => "
in the actual functions where you need it.

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com


More information about the Haskell-Cafe mailing list