This is what I get when type return 1
in GHCi.
> return 1
1
Since I didn't specify the type of return
, type of return 1
is Monad m, Num a => m a
How does GHCi show 1
even though there is no instance for Show
?
Monad m => m
defaults to IO
, that's how.
Then the IO action is performed, does no I/O, and returns the value.
Ghci has two output modes of operation: when the evaluated value has type IO a
and when it doesn't. In the first case, the IO action is performed, and the value of type a
produced by the action is shown. In the second case, the evaluated value is just shown.
Since you're in Ghci, the type of return 1 :: (Monad m, Num a) => m a
is actually IO Integer
. m
defaults to IO
, and a
defaults to Integer
, so the Integer
1
is shown.
Enable the GHCi showing you the types, with ghci> :set +t
, then try return 1.0
. It returns Double
, and Double
s also have the Show
instance.
> return 1
1
it :: Integer
> return 1.0
1.0
it :: Double
> return "3"
"3"
it :: [Char]
> print "3"
"3"
it :: ()
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