[Haskell-cafe] On the purity of Haskell
Donn Cave
donn at avvanta.com
Thu Dec 29 19:04:51 CET 2011
Quoth Steve Horne <sh006d3592 at blueyonder.co.uk>,
> On 29/12/2011 08:48, Heinrich Apfelmus wrote:
...
>> Well, it's a matter of terminology: "impure" /= "has side effects".
>> The ability of a language to describe side effects is not tied to its
>> (im)purity.
>>
>> Again, purity refers to the semantics of functions (at run-time):
>> given the same argument, will a function always return the same
>> result? The answer to this question solely decides whether the
>> language is pure or impure. Note that this depends on the meaning of
>> "function" within that language. In C, side-effects are part of the
>> semantics of functions, so it's an impure language. In Haskell, on the
>> other hand, functions will always return the same result, so the
>> language is pure. You could say that side effects have been moved from
>> functions to some other type (namely IO) in Haskell.
...
> Anyway, if you're using IO actions, your code is not referentially
> transparent and is therefore impure - by your own definition of
> "impure". Causing side-effects may not be pedantically the issue, but
> the mix of causing and reacting to them - ie interacting with the
> "outside" - clearly means that some of your function results are
> dependent on what's happening "outside" your program. That includes
> side-effects "outside" your program yet caused by program program.
No, code can be referential transparent and pure and at the same time
use IO actions. In order to understand that, you need to untangle the
notion you describe above as "function result" from Haskell function
value. We can talk endlessly about what your external/execution results
might be for some IO action, but at the formulaic level of a Haskell
program it's a simple function value, e.g., IO Int.
> Again, this is nothing new - it's clear from SPJs "Tackling the Awkward
> Squad" that this is what the IO monad is meant for, and if it's evil
> then at least it's controlled evil.
IO is not evil.
Donn
More information about the Haskell-Cafe
mailing list