Q:

Why doesn't this Haskell typeclass code work?

Trying to figure out Haskell typeclasses.  Why doesn't the following work?
{-# LANGUAGE FlexibleInstances #-}
class IntClass t
instance IntClass Int

intToIntClass  :: (IntClass r) => Int -> r
intToIntClass  x = x

Clearly "instance" doesn't mean what I think it should mean.  Instead, it gives the error message, which I do not understand.
Could not deduce (r ~ Int)
from the context (IntClass r)
  bound by the type signature for intToIntClass  :: IntClass r => Int -> r
  at f.hs:10:1-16
  `r' is a rigid type variable bound by
      the type signature for intToIntClass  :: IntClass r => Int -> r
      at f.hs:10:1
In the expression: x
In an equation for `intToIntClass t': intToIntClass  x = x

A:

The signature
intToIntClass  :: (IntClass r) => Int -> r

means that intToIntClass can produce a value of whichever type the caller wants, as long as that type belongs to IntClass. But the implementation can only produce Int values.
It doesn't help that Int is - so far - the only instance, as far as the compiler is concerned, there may be more instances defined in other modules, so the compiler can't accept a method that produces only Ints.

A:

The function you wrote says, "Give me something, and I will give it back to you." The type you wrote says, "I can convert an Int into any type you the caller would like, as long as that type is an instance of the IntClass typeclass."
When we put those together, we have a function that will take an Int from the caller, and give it straight back. Except that when that very same Int is returned, it will be a value of any type the caller would like as long as that type is a member of the IntClass typeclass. How can the input go from being an Int to any (constrained) type the caller would like? It can't. Since the input is given straight back, the return type must be the same as the argument type. The type checker infers this from your code, but then can't reconcile that inference with you naming the output type r. The type checker wants to work with you on this, but can't be sure that r is the same thing as Int given the sole hypothesis that r is an instance of the IntClass typeclass.
A function rather like the one you wrote is fromInteger, found in the Num typeclass. If you want to talk about all types that you can construct based on an Int, then this should be a method of your typeclass. Something like:
class IntClass a where
  intToIntClass :: Int -> a

You would define this method for every type that you want to be an instance of IntClass, and would have your desired type of intToIntClass :: IntClass r => Int -> r. This return type polymorphism depends critically on each participating type having an appropriate definition of intToIntClass.

A:

Your typeclass is fine. For example, this works:
intId :: IntClass r => r -> r
intId = id

The problem is, intToIntClass's type signature says it maps from Int to any member of IntClass. The compiler can't prove that Int is the only member of IntClass, and so since the body of intToIntClass rigidly returns Int, it's not polymorphic enough.

