[Haskell-cafe] Class/Instance : what am I doing wrong in this example ?

Jules Bean jules at jellybean.co.uk
Thu Dec 20 11:36:25 EST 2007


Tillmann Rendel wrote:
> david48 wrote:
>> class Gadget g where
>>   fInit  :: g -> a -> g

Tillman's two suggestions (below) are probably your answer.

Just to say what everyone else has said in a bunch of different ways: 
your class says that for ANY Gadget, fInit will work with ANY OTHER type a.

This doesn't seem to be what you want. There are three things you might 
want:

1. Maybe you want a to always be String. Easy. fInit :: g -> String -> g

2. Maybe you want lots of possible different "a"s for each "g". Then you 
make "a" a parameter of the class too.

3. Maybe you want just one particular "a" for each "g". I.e. "g" 
determines "a". Then you can proceed as for (2), but add the functional 
dependency | g -> a



> These types are incompatible, your fInit implementation should be able 
> to work with whatever type a the caller has choosen to provide. Maybe 
> you should try one of
> 
>   class Gadget g where
>     fInit  :: g -> String -> g
> 
> or
> 
>   class Gadget g a where
>     fInit  :: g -> a -> g



More information about the Haskell-Cafe mailing list