
On 03/16/2015 03:04 PM, Heinrich Apfelmus wrote:
Sumit Sahrawat, Maths & Computing, IIT (BHU) wrote:
On 15 March 2015 at 23:19,
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