(automatic) type classes context inference

Hello everyone, I am developing a toolset in which I have several (multiparameter) type classes; It is often the case that I can only define a data-type X as an instance of one such class (say A), if X is an instance of another class (say B); The thing is that, while it is hard for me, because of all the type parameters that I have to deal with, to add 'X is an instance of B' to the context cxt_A in 'instance cxt_A => A X' ghc is always able to correctly infer all type parameters; In fact, I always get: 'Could not deduce (B X t1 ... tn) from the context cxt_A arising from ... Probable fix: add (B X t1 ... tn) to the context cxt_A ...' In my case, this is the fix that I always need: most of the times, I am just copy-pasting (B X t1 ... tn) to cxt_A! Is there a way, say a compilation option, to avoid this? can anyone please help me here? :) thank you very much -- João Paulo Fernandes Universidade do Minho www.di.uminho.pt/~jpaulo

i believe that a valid idiom is to define a class C that has no functions,
but requires any instance to also be of type classes A and B, so that you
can write: C a => blah
rather than (A a,B a)=> blah, though I don't know how often such is used in
practice
the same idea should apply more generally to multiparam type classes
On Sat, Aug 28, 2010 at 5:44 AM, João Paulo
Hello everyone,
I am developing a toolset in which I have several (multiparameter) type classes;
It is often the case that I can only define a data-type X as an instance of one such class (say A), if X is an instance of another class (say B);
The thing is that, while it is hard for me, because of all the type parameters that I have to deal with, to add
'X is an instance of B'
to the context cxt_A in
'instance cxt_A => A X'
ghc is always able to correctly infer all type parameters; In fact, I always get:
'Could not deduce (B X t1 ... tn) from the context cxt_A arising from ... Probable fix: add (B X t1 ... tn) to the context cxt_A ...'
In my case, this is the fix that I always need: most of the times, I am just copy-pasting (B X t1 ... tn) to cxt_A!
Is there a way, say a compilation option, to avoid this?
can anyone please help me here? :)
thank you very much
-- João Paulo Fernandes Universidade do Minho www.di.uminho.pt/~jpaulo
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, Aug 28, 2010 at 12:22 PM, Carter Schonwald
i believe that a valid idiom is to define a class C that has no functions, but requires any instance to also be of type classes A and B, so that you can write: C a => blah rather than (A a,B a)=> blah, though I don't know how often such is used in practice the same idea should apply more generally to multiparam type classes
I've done this. It's handy not only for reducing class context noise, but also keeping implementation details from leaking out the interface. It looks funny when some function randomly requires its parameter to be Storable just because somewhere down the line someone else needs it, and it's awkward when a whole interface requires the same 4 typeclasses on every function. Of course, if you want to write a new type that can be passed, you do need to implement those classes, so from the point of view of that user it's not an implementation detail. But from the point of view of someone just passing in data, not writing their own instances, it's awkward to have to keep writing a random bunch of class contexts that reflect the implementation of a function and may break if that implementation changes.
participants (3)
-
Carter Schonwald
-
Evan Laforge
-
João Paulo