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

Ertugrul Soeylemez es at ertes.de
Mon Aug 2 00:51:02 EDT 2010

Ivan Miljenovic <ivan.miljenovic at gmail.com> wrote:

> On 1 August 2010 20:43, Ertugrul Soeylemez <es at ertes.de> wrote:
> > There are no functions with side effects in Haskell, unless you use
> > hacks like unsafePerformIO.  Every Haskell function is perfectly
> > referentially transparent, i.e. pure.
> At code-writing time, yes; at run-time there are side effects...
> In terms of what a function does, is readFile actually pure?

Yes, it's a pure function.  But it models a computation, which changes
the world's state.  If you happen to get a real Haskell code
representation of 'readFile', you can safely replace its call by its
body without messing things up (applying the usual lambda reduction
rules, of course).

Note that a "function" is something of type 'a -> b' for some type a and
some type b.  The result of the function 'readFile' is not of type
String, but of type IO String.  For the same file name parameter, it
always gives the same result computation.

It is really questionable whether it makes sense to use the term
"impure" for the computations, which are modelled by IO.  I don't think
we have a useful theoretical foundation for what "impure" means in this
context.  You can model IO as State Universe (regardless of the problems
with that model), in which case even the computations are perfectly pure
and representable as Haskell functions.  In fact the Clean language uses
this model, whereas Haskell leaves this abstract.


nightmare = unsafePerformIO (getWrongWife >>= sex)

More information about the Haskell-Cafe mailing list