do in one expression does nothing: itโs just a syntactic herald indicating that you can use a sequence of actions (which requires a monad), but if you donโt do this, it has the same effect as wrapping an expression in layers of redundant parentheses.
So your sample session is equivalent to this:
Prelude> 0 0 Prelude> return 0 0 Prelude> Just 0 Just 0 Prelude> return (Just 0) Just 0 Prelude> Nothing Nothing Prelude> return Nothing Nothing
Now the question is why return does nothing. Well, actually it is, you just do not see it, because GHCi hides implementation details. GHCi has two fundamentally different ways of interactive evaluation:
- Actions in progress
IO , then the result is print ed.- Everything else
print ed, as it is.
Unfortunately, GHCi is quite difficult to interpret everything as an IO action before continuing with 2. Therefore, if you give it an ambiguous expression like return 0 , it notices that one possible instance is IO Integer . He immediately sets this by default by default, performs this free action on the side effect, and everything that you see as a result is thus 0 . This happens only for IO , but not for other monads:
Prelude> return 0 :: IO Integer 0 Prelude> return 0 :: Maybe Integer Just 0 Prelude> return 0 :: [] Integer [0]
GHCi simply has a default value of IO when the monad is ambiguous, but in Haskell this will not happen otherwise.
source share