Let's say I have the following type signature:
someFunction :: (Eq a, Eq b) => a -> b
With implementation:
someFunction x = (2 :: Int)
(Don't look in to it too far, it's just an example).
My understanding of the signature is that "someFunction
takes an argument that is an instance of the Eq
typeclass, and returns a value (that can be of a different type) that is an instance of the Eq
typeclass". Int
is an instance of Eq
, so why does GHC get upset about this implementation?
The error makes it obvious enough:
Couldn't match expected type ‘b’ with actual type ‘Int’ ‘b’ is a rigid type variable bound by the type signature for: someFunction :: forall a b. (Eq a, Eq b) => a -> b
I guess what I don't understand is the requirement that it work "forall" b
. Any code using this function should only rely on the fact that b
is an instance of Eq
, right? In my head, the implementation does match the signature. What about my implementation is breaking the expectations of this signature?
b
, as long as it isEq b
), but your implementation is clearly only returningInt
s. – Willem Van Onsem