Hello all,
I’m currently working through the “Haskell from first principles” book and have a got a little stuck on one of the exercises concerning type classes.
The exercise is to create an Eq instance for
data EitherOr a b = Hello a | Goodbye b
which I have as
instance (Eq a, Eq b) => Eq (EitherOr a b) where
(==) (Hello a) (Hello a') = a == a'
(==) (Goodbye b) (Goodbye b') = b == b'
(==) _ _ = False
However, upon using this like
Goodbye "bye == Goodbye "bie"
I get a compilation error
Ambiguous type variable ‘a0’ arising from a use of ‘==’
prevents the constraint ‘(Eq a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
It seems that, despite technically not needing to know a type for a
(as it isn’t used in Goodbye
) it still wants one.
I’m assuming I’m missing something but also a little unsure as to how I would remedy it.
Any help appreciated, thank you.