Currently I'm learning Haskell and are stuck with the instantiation of types to typeclasses. I actually don't understand, why it's possible to create a value of the Maybe a
type with Just (+)
.
The problem why this behaves strange to me is, that the Maybe type is defined as an instance of the Eq
typeclass (see Haskell source) and that if you derive an instance for a type, all the fields of the value / data constructors of that type must be also an instance of the Eq
typeclass (here).
With this knowledge in mind, the following code shouldn't be compilable or executable, because a function is not a part of the Eq
typeclass:
let a = Just (+)
let b = Just (-)
But GHCi actually executes the code without throwing an error message. If you then try to compare these two values (which shouldn't also be possible) the interpreter comes up with the follwing error message:
a == b
<interactive>:24:1: error:
* No instance for (Eq (Integer -> Integer -> Integer))
arising from a use of `=='
(maybe you haven't applied a function to enough arguments?)
* In the expression: a == b
In an equation for `it': it = a == b
This problem also occurs if you create your own Maybe a
type.