[Haskell-cafe] Can we come out of a monad?

Tillmann Rendel rendel at informatik.uni-marburg.de
Fri Jul 30 06:29:53 EDT 2010

C K Kashyap wrote:
> I am of the
> understanding that once you into a monad, you cant get out of it? 

That's not correct.

There are many monads, including Maybe, [], IO, ... All of these monads 
provide operations (>>=), return and fail, and do notation implemented 
in terms of these functions, as a common interface. Using just this 
common interface, you cannot "get out of the monad".

But most if not all monads also provide additional operations, specific 
to the monad in question. Often, these operations can be used to "get 
out of that monad". For example, with Maybe, you can use pattern matching:

   case do x <- return 5
           fail "some message"
           return (x + 3) of
     Just a   ->  a
     Nothing  ->  0

So we can get out of many monads, but we need to know which one it is to 
use the appropriate operation.

Kevin Jardine wrote:
> I'm still trying to understand how monads interact with types so I am
> interested in this as well.

 From my point of view, the most important fact about monads is:

   There is nothing special about monads!

The type class Monad behaves like very other type class. A monadic type 
constructor behaves like every other type constructor. The type class 
methods (>>=), return and fail behave like every other type class 
method. There is nothing special about monads.

The only speciality of monads is do notation, but do notation is only a 
syntactic convenience, and can be translated into calls of (>>=), return 
and fail, which, as noted above, are not special in any way.

So, back to your question, since there is nothing special about monads, 
monads do not interact with types in any special way.


More information about the Haskell-Cafe mailing list