[Haskell-beginners] IO and purity

Alexey Shmalko rasen.dubi at gmail.com
Sun Apr 26 12:44:25 UTC 2015


You can't do

putStrLn (getLine ++ getLine)

so of course you can't do such transformations. I would suggest you rewrite
your IO action without any syntactic sugar (with plain >> and >>=) first
and then try to do transformations.

I general, I find it useful to think of IO actions in two ways:
- as a value of IO type that your main is (kind of instructions that
Haskell runtime should perform).
- the effects execution of this value causes.

You can think that the sole purpose of your program is to construct this IO
value, while its execution is performed kind of outside of your program. If
you think that way, IO is again pure, obeys all monad rules and
categorically valid monad.

Regards,
Alexey Shmalko

On Sun, Apr 26, 2015 at 3:22 PM Joel Neely <joel.neely at gmail.com> wrote:

> 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
>  _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150426/99e7a814/attachment-0001.html>


More information about the Beginners mailing list