I have a question on how GHCi assumes type of a whole number.
I was reading Yes-No type class of Learn you a Haskell.
Here is a link if you want to read the whole thing. http://learnyouahaskell.com/making-our-own-types-and-typeclasses#a-yes-no-typeclass
To put it shortly, this chapter shows that by defining my own class, I can make a function that works with a lot of types.
This book defines YesNo class with a function
yesno :: a -> Bool
and make Int
as a instance of YesNo class
instance YesNo Int where
yesno 0 = False
yesno _ = True
When I loaded this on my GHCi and typed
yesno 0
it returned error. I thought it is probably because GHCi cannot tell whether 0 is meant to be Int
or Integer
or Double
or other type in Num
class. Actually when I typed yesno (0::Int) it worked.
So just for fun I made Integer
as an instance of YesNo
class and wrote
instance YesNo Integer where
yesno 0 = True
yesno _ = False
(Note that I flipped True and False) and again, I typed
yesno 0
(without any type declaration) then GHCi showed True
.
Moreover, when I typed
yesno $ fromIntegral 0
it returned True
, which means that GHCi thinks the type of fromIntegral 0
is Integer
.
So, does this mean that when I just type a whole number on GHCi, it usually assumes its value is Integer
in stead of? I am confused because :t 0
returns Num a => a