[Haskell-cafe] Word rigid in "`a' is a rigid type variable..."

Daniel Trstenjak daniel.trstenjak at gmail.com
Thu Nov 14 11:08:25 UTC 2013

Hi Vlatko,

On Thu, Nov 14, 2013 at 11:33:15AM +0100, Vlatko Basic wrote:
> Yes, sometimes I still have the feeling I'm not thinking fully Haskellish.
> Maybe it's time form me to re-read about the type system.
> Can you recommend any resources that helped you in better understanding?

Did you understand what Brandon said, that 'a' has to be a 'String',
because it's compared by an other string with '==' and the operator
'==' demands, that both arguments have the same type?

If you try to write the same function in C++ (the same may hold for Java Generics):

template <typename A>
bool f(A a) { return a == "x"; }

Than the C++ compiler would happily read this function and wouldn't
complain in any way. Only if you would use this function with a type
that isn't comparable with a string, than it would complain.

The problem with the C++ way is, that the function isn't telling you in
any way the constraints of type 'A', only if you're using a type that
doesn't fulfill all constraints, than the compiler will tell you that with
some very indirect error messages.

Haskell is very explicit about the constraints, you have to explicitly
declare all the constraints of your type.

So looking at the function 'f':

f :: a -> Bool
f a = let b = "x" in a == b

The first thing is the usage of '==', which is an operator of the
type class 'Eq', so only types having an instance of 'Eq' are allowed:
f :: Eq a => a -> Bool
f a = let b = "x" in a == b

But you're comparing the 'a' against a variable of type 'String' and
because the type of '==' is 'a -> a -> Bool' the type of 'a' also has
to be 'String'.

f :: String -> Bool
f a = let b = "x" in a == b


More information about the Haskell-Cafe mailing list