[Haskell-beginners] Equivalent of IO Monad in other functional languages?

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Mon Mar 16 14:58:27 UTC 2015


Sorry for having strayed towards a different path. I'll try to answer only
your question this time :)

An example from [1], for SML (bottom of the page) shows a function, with
the following type signature in haskell

    copyTextFile :: String -> String

One cannot write such a function in haskell that does the same thing
because the example uses IO within a function with a pure looking type.

I'm sure people experienced with other functional languages will have good
examples, so I'll leave it to them.

To answer your second question, I'm still not sure :).
I was just relaying how I understood purity, so that somebody might also
correct me if I'm wrong.

Summarizing the discussion above, a function is pure if it gives the same
output for the same input. Now a function returning 'IO a' always returns
the same IO instructions (these instructions might be impure), but the
function (by the virtue of always returning the same output for the same
inputs) is ultimately pure. Frerich has also raised an important point that
an IO computation might actually depend on some random file, which breaks
this pattern.

[1]: http://www.cs.cornell.edu/courses/cs312/2006fa/recitations/rec09.html

On 16 March 2015 at 20:15, Simon Kitching <simon at vonos.net> wrote:

> On 03/16/2015 03:04 PM, Heinrich Apfelmus wrote:
>
>> Sumit Sahrawat, Maths & Computing, IIT (BHU) wrote:
>>
>>> On 15 March 2015 at 23:19, <amindfv at gmail.com> wrote:
>>>
>>>  From that perspective isn't every language pure? Haskell's still got
>>>> "randomIO" and "print <=< readMVar"
>>>>
>>>
>>> Haskell is impure only when you use the unsafe* functions (atleast that's
>>> how I understand purity).
>>>
>>> My understanding is that a language is impure if it allows one to write
>>> arbitrary IO within a function and still give it a proper (mathematical)
>>> function type. In other words impurity arises only if you can unwrap the
>>> IO
>>> monad (which is what the unsafe functions do).
>>>
>>> The two examples you give above are pure under such a perspective, but I
>>> might be wrong.
>>>
>>
>> You're right, indeed. A language is pure if supplying a value `x` of type
>> `A` to the a function
>>
>>     A -> B
>>
>> will always returns the same result, no matter how often or in which
>> order this function is called. This is true for both
>>
>>     randomIO             :: Random a => IO a
>>     (readMVar >=> print) :: Show a   => MVar a -> IO ()
>>
>> because they return an IO action. This action will always be the same
>> given the same arguments.
>>
>> The language would be impure if these functions had the types
>>
>>     randomIO             :: Random a => a
>>     (readMVar >=> print) :: Show a   => MVar a -> ()
>>
>
> Doesn't "pure" correspond to "can write a unit test for"? When a
> function's return value only ever depends on its inputs, then I can
> write a set of test-cases for various inputs, and assert that the
> return-value has the expected content.
>
> A function that reads from a file, stdin, etc. cannot be tested in the
> same way; I cannot _assert_ that the returned value has specific content.
>
>  Sumit stated earlier that IO is "a pure program that can be executed",
> which seems similar to your description above (which I admit I
> don't yet 100% understand). How can I "assert" anything about this
> "program", or make a Haskell-based application 'safer' in any way by
> leveraging this kind of 'purity'?
>
> Thanks,
> Simon
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150316/6311bfc6/attachment.html>


More information about the Beginners mailing list