[Haskell-beginners] several questions: multi-param typeclasses, etc.

Michael Mossey mpm at alumni.caltech.edu
Sun Nov 1 14:31:33 EST 2009



Chaddaï Fouché wrote:
> On Sun, Nov 1, 2009 at 5:32 PM, Michael Mossey <mpm at alumni.caltech.edu> wrote:
>> On the other hand, if you added
>>
>> instance (Error e) => MonadError String (Either e)
>>
>> and didn't include the functional dependency, the compiler would still run
>> into a problem with overlapping instances and have no way to decide, which I
>> presume is still an error.
> 
> Right, in this case it is true, but supposing the MonadError instance
> for Either was rather :
> 
>> instance (Error e) => MonadError (Maybe String) (Either e)
> 
> There would be nothing a priori that would prevent you from writing
> another instance :
> 
>> instance (Error e) => MonadError String (Either e)

I think I understand that you are saying it would create a messy situation 
to write this second instance, and the functional constraint causes the 
compiler to stop with an error if it sees that second instance. Is that true?

Now I have a question that probably doesn't make sense, but what I'm really 
doing is picking your brain. I want to see how you would rephrase it:

Let's assume for a moment that two different people are involved in this 
code. One of them writes the class definition. Another one writes an instance.

In OO, which I am more familiar with, one person will write a class with a 
limited API in order to help put guarantees on the correct behavior of the 
class. In a sense, that person is saying: "I release my class to the world 
to do what it will, but before doing that I put some constraints on it so 
no one can distort my intentions."

Is this functional dependency a similar situation? Does it make sense from 
the "point of view" of the author of the class definition?

Or is it more a practical necessity?

Thanks,
Mike



More information about the Beginners mailing list