[Haskell-beginners] IO and purity

Joel Neely joel.neely at gmail.com
Sun Apr 26 12:22:29 UTC 2015


Shishir,

I'll reply as an humble programmer, not a category theorist. For me the
clarifying example was to contrast

putStrLn (getLine ++ getLine)


with

s = square 3 + square 4


where

square x = x * x


There is absolutely nothing that "leaks" between the two sub-expressions
involving square, so I'm free to rewrite as:

a = square 3
b = square 4
s = a + b


or

b = square 4
a = square 3
s = a + b


or

x = square 7
b = square 4
a = square 3
s = a + b
y = square 8


or

hh m n = square m + square n
x = square 7
s = hh 3 4
y = square 8


without altering the value of s or any surrounding computation. In that
last case, I can now freely intermingle uses of hh and square without risk.

But if I were to attempt the same gymnastics with putStrLn and getLine, I
wouldn't have those freedoms. The order of evaluation/performance now
becomes critical.

So the monad provides a way to manage the composition, and the type system
ensures visibility to the fact that something more is going on when a
defined function includes IO behavior.

Hope that helps,
-jn-






On Sun, Apr 26, 2015 at 4:59 AM, Shishir Srivastava <
shishir.srivastava at gmail.com> wrote:

> Hi,
>
> Can someone please explain how IO operations do not fit in the pure
> category of mathematical function in that they have to be implemented via
> Monads.
>
> For e.g. the getLine function has the type IOString and it reads the
> input from the user. Now as I see it the output of getLine will always be
> same if the input remain same (i.e. for input "X" getLine will always
> return "X" ) which is the constraint on mathematical functions.
>
> Therefore I don't see why monads are necessary for implementing IO in pure
> languages.
>
> I can understand why Date and Random functions have be implemented via
> monads because their output will always change.
>
> Thanks,
> Shishir
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Beauty of style and harmony and grace and good rhythm depend on simplicity.
- Plato
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150426/7cec9393/attachment.html>


More information about the Beginners mailing list