Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "return 1" show "1" in GHCi? [duplicate]

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?

like image 394
damhiya Avatar asked May 23 '19 07:05

damhiya


1 Answers

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 Doubles 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 :: ()
like image 78
Will Ness Avatar answered Nov 14 '22 07:11

Will Ness