
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 -> ()
Maybe I should have phrased my original question differently. Let me try again.. In an imperative language, you can write something like the following pseudocode: String transform(String name) { print "enter greeting:" greeting = read-line-from-stdin return greeting + " " + name } From the "outside", there is no way to tell whether the function "transform" performs IO, mutates global state, mutates its parameter, or anything else. And this is just _normal_ for imperative-programming; if you want to write a unit-test for a method, you need to somehow _know_ what external state it might depend on/affect. And compilers cannot reorder or omit functions because any function might have side-effects. In Haskell, the function signature clearly indicates if this kind of thing is happening - any function like the above will have a return type of IO, thus clearly separating code with side-effects from code without side-effects. I was wondering what other functional or partly-functional languages (OCaml, F#, Scheme etc) do - do they also give the user of a function a way to tell whether the function has IO side-effects (reading/writing) or does the user just need to "consult the documentation" or "read the source code"? Thanks, Simon