I was busy with an exercise in the "Typeclasses" chapter of Haskell Book, and I solved it as the following:
data TisAnInteger =
TisAn Integer
instance Eq TisAnInteger where
(==) (TisAn x) (TisAn y) = x == y
So that I can use it in GHCi REPL like:
λ> TisAn 9 == TisAn 9
True
λ> TisAn 9 == TisAn 8
False
but while playing with it and asking "what if?" questions, I've realized that the following compiles, too:
data TisAnInteger =
TisAn Integer
instance Eq TisAnInteger where
(==) x y = x == y
and when I try to run something similar in GHCi:
λ> TisAn 9 == TisAn 8
Interrupted.
λ> TisAn 9 == TisAn 9
Interrupted.
Well Interrupted.
because I hit Ctrl+C
after a few seconds, GHCi was not printing anything.
I'm trying to understand two things:
Any hints, pointers to docs, etc. for me so that I can enhance my understanding of the above phenomenon?
It's simply entering an infinite loop. You are defining (==) x y
as x == y
, which is equivalent to... (==) x y
. It's as if you've said f(x) = f(x)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With