I'm learning how to use typeclasses in Haskell.
Consider the following implementation of a typeclass T with a type constrained class function f.
class T t where
f :: (Eq u) => t -> u
data T_Impl = T_Impl_Bool Bool | T_Impl_Int Int | T_Impl_Float Float
instance T T_Impl where
f (T_Impl_Bool x) = x
f (T_Impl_Int x) = x
f (T_Impl_Float x) = x
When I load this into GHCI 7.10.2, I get the following error:
Couldn't match expected type ‘u’ with actual type ‘Float’ ‘u’ is a rigid type variable bound by the type signature for f :: Eq u => T_Impl -> u at generics.hs:6:5 Relevant bindings include f :: T_Impl -> u (bound at generics.hs:6:5) In the expression: x In an equation for ‘f’: f (T_Impl_Float x) = x
What am I doing/understanding wrong? It seems reasonable to me that one would want to specialize a typeclass in an instance by providing an accompaning data constructor and function implementation. The part
Couldn't match expected type '
u' with actual type 'Float'
is especially confusing. Why does u not match Float if u only has the constraint that it must qualify as an Eq type (Floats do that afaik)?
Eqtype you want from me as a result - I can provide itf :: Eq u => t -> u-tis know from the context - theuis there with an implicitforallu- it's really just what Chi pointed out in his answer and comments