[Haskell-cafe] Instances and multiple inheritance

wren ng thornton wren at freegeek.org
Sun Jun 12 15:47:11 CEST 2011

On 6/12/11 8:48 AM, Patrick Browne wrote:
> On 12/06/2011 10:43, MigMit wrote:
>> I fail to understand why instantiating a four-argument class with five arguments seems obvious to you.
>>> class (Surfaces v o, Paths a b (v o)) =>  Vehicles v o a b where
> Obviously I am wrong! But my incorrect thinking is as follows:
> Surfaces takes 2 arguments, Paths take 3 arguments (giving 5).
> I do not know how to group those 5 arguments to make the required 4 for
> Vehicles. The original classes were defined in [1].

Well, the third argument to Paths is defined as being (v o). So, given 
the first two arguments to Vehicles, we implicitly know the third 
argument to Paths already (as well as the arguments to Surfaces).

It may be more helpful to think of typeclasses as functions which take 
type parameters and return proof of the implementation of whatever 
functions and associated types are in the class (colloquially refered to 
as the type class's "dictionary"). So, given the following:

     class (Surfaces v o, Paths a b (v o)) => Vehicles v o a b where...

We're defining a function "Vehicles" which takes four parameters: v, o, 
a, and b; and returns a dictionary (Vehicles v o a b), or rather a kind 
of proof that such a dictionary could be returned upon request.

Any implementation (aka instance) of this function (aka class) will call 
the function "Surfaces" with the arguments v and o in order to get the 
(Surfaces v o) dictionary. It'll then call "Paths" with the arguments a, 
b, and (v o) to get the (Paths a b (v o)) dictionary. Finally, with 
those two prerequisite dictionaries in hand the instance will package up 
whatever definitions the instance gives for the members of the typeclass.

Live well,

More information about the Haskell-Cafe mailing list