I'm now learning Haskell and trying to make my data (which is similar to Maybe) an instance of Eq
, as follows.
module Main where
data MyMaybe a = MyNothing | MyJust a
instance (Eq a) => Eq (MyMaybe a) where
MyNothing == MyNothing = True
MyJust x == MyJust y = x == y
_ == _ = False
main :: IO ()
main = do
let b0 = MyJust 42 == MyJust 42 -- OK
print b0 -- True
let b1 = MyNothing == MyNothing -- Build error!
print b1
But the complier returns an error in the line let b1 = MyNothing == MyNothing
as follows.
? 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. These potential instances exist: instance Eq Ordering -- Defined in ‘GHC.Classes’
...
How can I fix it?
... And I have searched existing questions and answers. The following one seemed close to mine, but I think that my code has already solved the points that the answer suggested.