[Haskell-beginners] how do typeclasses work again?

Sylvain Henry sylvain at haskus.fr
Sat Feb 11 13:36:36 UTC 2017


On 11/02/2017 09:50, Nicholls, Mark wrote:
> I don't understand the difference
>
> forall a isx. (Is isx a, Show a) => isx -> String
> and
> (Is isx a, Show a) => isx -> String
I have used the `forall` explicitly only to fix the order of the type 
parameters (`a` and `isx`) so that we are sure to set the type of `a` 
when we write (using TypeApplications): foo4 @Y (X,Y)

In the second declaration, the `forall` is implicit.

> I think my understanding of type classes is naïve, I just thought it meant that secretly a dictionary was being passed.
Yes your understanding is correct. The issue here is that the compiler 
doesn't know the type of `a`, hence it can't select and pass the 
appropriate instances.

> the compiler would identify the specific dictionary from the call site
Even at call site, the compiler can't infer the `a` type from the `isx` 
type (nor from the return type of `foo4`).

Do you want the `a` type to be dependent on the `isx` type? I.e., to 
only be allowed to define a single `Is isx a` instance for each `isx` type.

--
Sylvain


More information about the Beginners mailing list