[Haskell-cafe] Do real programs need IO? (was IO is a bad example for Monads)

Conal Elliott conal at conal.net
Sun Dec 9 16:36:29 EST 2007


Thanks for the clarification.

>AFAIK, the only way to get input and produce output is via the IO monad

Now you know something different, don't you?  FRP, Pan, TV.  Also
Grapefruit, functional forms, and others I'm not thinking of or don't know
about.

As for your example, mixing the IO with the functional, as you have
interferes with composing the result.  I can think of two alternatives.  One
is to move reading & printing from the definition to the uses, as with Unix
stream filters.  Still convenient, and much more flexible.  Of course, Unix
programs were written in C and so use explicit I/O instead of lazy
functional streams.  (Though Doug McIlroy, who invented Unix pipes, knew
that pipes were equivalent to coroutines and to lazy evaluation.  See my
"modern marriage" talk (the video I mentioned) for more about Unix and TV.)

A second alternative is to use TV to explicitly capture the interface (I/O),
which could look like this:

    coolTV :: TV (String -> String)
    coolTV = tv (olambda (fileIn "Data.txt") defaultOut)
        performAwfullyCoolFunctionalStuff

where

    tv :: Output src snk a -> a -> TV src snk a

The type parameters src & snk are for various interface styles.  Then coolTV
can then be used on *either* side of a TV-style pipe, resulting in the
removal of the reading or writing half.

And yes, there are *some* uses of IO for which I'd be hard pressed at this
point to offer you an alternative.  Which is a far cry from IO being
necessary for all "real" programs, even today.

Given this state of affairs, I'd prefer the Haskell community to point
newbies away from IO and toward purely functional programming for things
like UIs and graphics and help them change their way of thinking.  Let's
also admit that we haven't yet figured out how to apply our functional
paradigm as flexibly or broadly as we'd like, and so meanwhile we have thi
monadic IO trick that let's them write nicely factored imperative code that
can call into the functional pieces.

Regards,  - Conal

On Dec 9, 2007 12:54 PM, Daniel Fischer <daniel.is.fischer at web.de> wrote:

> Am Sonntag, 9. Dezember 2007 21:29 schrieb Conal Elliott:
> > I think your real point is that some things we still haven't figured out
> > how to express functionally.  Right?
>
> That's my point, at least. Currently, AFAIK, the only way to get input and
> produce output is via the IO monad, so it is de facto necessary for all
> 'real' programmes, it need not remain so (though I cannot imagine how to
> functionally express
> 'readFile "Data.txt" >>= print . performAwfullyCoolFunctionalStuff' -
> surprise
> me :).
> Read "IO is important" as a description of current affairs, not as a claim
> of
> the inherent grandeur of it.
>
> Cheers,
> Daniel
>
> >  I would certainly agree with that
> > part. Perhaps you exaggerating when you wrote "IO is important because
> you
> > can't write any real program without using it."
> >
> > Cheers, - Conal
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20071209/764b76f0/attachment.htm


More information about the Haskell-Cafe mailing list