[Haskell-beginners] Re: = vs <-

Ertugrul Soeylemez es at ertes.de
Sun Aug 15 11:09:17 EDT 2010

Kyle Murphy <orclev at gmail.com> wrote:

> Ertugrul Soeylemez <es at ertes.de> wrote:
> > I think, this is very misleading.  You never get "out of a monad",
> > you get "into" it, and you can do that whenever you want.  Remember
> > that the monadic world is inside of the pure world, not the pure
> > world inside of the monadic world.  In fact, the monadic world is
> > part of it.
> Not really, it depends on how you look at things. If you want to use
> the result of something that returns an IO Monad inside of a pure
> function, you really can't do that without in turn making that
> function impure (that is, putting it inside of the IO Monad as well),
> so to one way of looking at it, values (and functions) exist inside
> Monads and with few exceptions are impossible to remove from those
> Monads. You can always embed your pure function inside a non-pure
> function, but the opposite is not true. Most of the monads do provide
> functions like fromJust, and unsafePerformIO (never use this,
> seriously) that can allow you to escape from the monad, but doing so
> is often risky. The monadic world as you put it, really isn't inside
> of the pure world except in theory. From a practical standpoint
> everything exists inside the IO monad at some level, it's just lower
> level functions that are pure (that is, functions can be pure, but at
> some point they MUST be called from the IO monad). I know
> theoretically monads are built on top of pure math, but to me the
> difference between monads as a concept and monads as implemented in
> Haskell, is similar to the difference between proving a program to be
> correct, and actually running it.

I don't agree.  The pure world is the evaluation world -- the world,
where you write your program.  The impure world is the execution world,
where your program gets run.  You cannot pull a value generated by
execution into the evaluation world, because at that point there is no
execution.  That's what people refer to as "escaping from IO".  Escaping
from IO doesn't make sense in Haskell, hence it's an unsafe thing to do
using hacks like 'unsafePerformIO'.

However, you can give those values a name, either by using (>>=) or by
using '<-' in do-notation.  This name can be referred to in the
evaluation world.  The monadic world is part of the pure world, because
monads still belong to the evaluation world.  The actual execution of
the program is independent and none is inside of the other.

There are lots of ways to pass results of IO actions to pure functions.
Trying to use a value from execution in evaluation is a completely wrong
line of thought.  It doesn't make sense, and the types don't fit anyway.
If you need random numbers, pass a random number generator or the random
numbers needed.  You can use state and reader monads to make this
passing implicit, if you wish.


nightmare = unsafePerformIO (getWrongWife >>= sex)

More information about the Beginners mailing list