[Haskell-cafe] A Very Simple Type Class Question

Jochem Berndsen jochem at functor.nl
Tue Nov 11 20:46:11 UTC 2014

Hi Larry,

On 11/11/2014 09:59 PM, Larry Lee wrote:
> Hi
> I have a very simple problem.
> I have a class and want to define a function in that class that 
> returns a different instance of the same class.
> I tried accomplishing this as follows:
>     class A a where
>       f :: A b => a -> b
> This fails however when I try to instantiate it. For example:
>     instance A String where
>       f x = x
> I get an error message that makes absolutely no sense to me:

f has type
   (A a, A b) => a -> b.

Both a and b can be thosen by the caller.
If you try to create an instance of A like you did, this will not type 
check, as you have only provided a function of type
    String -> String,
whereas a function of type
    A b => String -> b
(for *all* b) is required.

I hope that clears things up a little.

Thanks, Jochem

Jochem Berndsen | jochem at functor.nl

More information about the Haskell-Cafe mailing list