
On 24/03/2006, at 12:45 PM, Fritz Ruehr wrote:
What is the easiest way to name a combination of type classes, i.e., to abbreviate the fact that a certain type is an instance of several classes simultaneously? I have a vague sense that this is do-able, but that I am messing up by trying to use an empty class body as below.
So in the code below, I try to use FooBar to abbreviate the conjunction of Foo and Bar. But while f (which uses a FooBar constraint) has a valid definition, it can't be used. On the other hand, g (which uses the long-winded constraint), is both a valid defined and useable.
(In a real example, imagine that FooBar names a conjunction of a half dozen things, so that the g-like form really is onerous, whereas the f-like form would be sweet and tidy :) .)
Hi Fritz! You only need to do a couple of things to get this working. Add an instance declaration: instance (Foo a, Bar a) => FooBar a But for this to work you need to allow undecidable instances (and - fglasgow-exts). To have this type class synonym trick work you need both the class and instance declaration: class (Foo a, Bar a) => FooBar a instance (Foo a, Bar a) => FooBar a The first ensures that members of class FooBar will inherit the methods of classes Foo and Bar. The second ensures that if there is a Foo and a Bar instance then there will be a FooBar instance. You were lacking this in your code hence the error message:
f 'a'
No instance for (FooBar Char) arising from use of `f' at <interactive>:1:0 Probable fix: add an instance declaration for (FooBar Char) In the definition of `it': it = f 'a' This is a neat trick. I've also used it to reduce onerous contexts. Sean