[Haskell-beginners] Typeclasses vs. Data

Thomas haskell at phirho.com
Thu Jul 21 14:58:56 CEST 2011


Hello Antoine!

Thank you for the explanation.
I think I get closer with it.

So my understanding is this: In my example both 'a' and 'b' were of the 
same typeclass. In fact they were of the same type, too, since in the 
example the class had only one instance. However, the compiler/type 
checker could not prove this to be true.

But if this was the case then I should be able to convince the compiler 
via type annotations like
  if p then (a :: (MyTC a) => a) else (b :: (MyTC a) => a)
which does not work ('Inferred type is less polymorphic then expected.')

Probably I do not yet understand well the relationship between types and 
type classes...

Regards,
Thomas


On 21.07.2011 14:04, Antoine Latter wrote:
[...]
>
> The difference comes down to two things:
>
> 1. The type of 'if'. The haskell 'if ... then ... else ...' is conceptually
> just a function with the type (Bool ->  a ->  a ->  a). The two branches must
> be of the same type.
>
> 2. In the code:
>
>> f (if p then a else b)
>
> If somehow the 'if' could type-check with 'a' and 'b' being of different
> types, the compiler wouldn't know which type to pick for 'f'. In Haskell,
> we're not allowed to delay type-checking until run-time - the compiler
> demands that it can solve everything before running any of it.
>
> I hope I haven't confused things even more!
>
> Antoine
>




More information about the Beginners mailing list