[Haskell-cafe] How to print a string (lazily)

Cale Gibbard cgibbard at gmail.com
Tue Jan 3 14:32:38 EST 2006

On 03/01/06, Donn Cave <donn at drizzle.com> wrote:
> On Tue, 3 Jan 2006, Chris Kuklewicz wrote:
> ...
> > I sometimes call a function with side-effects in IO a "command".  But
> > the terms are fungible.  But calling putStr a "function" is correct.  It
> > is not a "pure function" however.
> Is that the standard party line?  I mean, we all know its type and
> semantics, whatever you want to call them, but if we want to put names
> to things, I had the impression that the IO monad is designed to work
> in a pure functional language - so that the functions are indeed actually
> pure, including putStr.  It's the monad that actually incurs the side
> effects.  Or something like that.  So it isn't at all necessary to have
> another word for functions of type IO a.
I'd say it depends on the way that you're thinking about it. Strictly
speaking, putStr is a pure function which returns an action. It's also
referentially transparent, since it always returns the same action for
the same string.

However, if you think of functions a -> IO b as the arrows a -> b in a
new category where composition is Kleisli composition:

(@@) :: Monad m => (a -> m b) -> (t -> m a) -> (t -> m b)
y @@ x = \u -> x u >>= y

Then these arrows are "effectful" and would be considered impure.

I think I prefer the first explanation, as it's a somewhat important
property that the same action is computed for the same input. This
doesn't hold of side-effectful arrows in general. Note that with
Hughes' Arrows, this restriction can (often quite cleverly) be
side-stepped in order to make some rather major optimisations.

 - Cale

More information about the Haskell-Cafe mailing list