5

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?

Boann
  • 48,794
  • 16
  • 117
  • 146
damhiya
  • 473
  • 3
  • 7
  • 3
    The default Monad is IO. There is no Show for IO, but when you return an IO, GHCi will not Show it, but run it, and Show the result (an Integer here, which does have Show). – Thilo May 23 '19 at 07:22

1 Answers1

9

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 :: ()
Will Ness
  • 70,110
  • 9
  • 98
  • 181