[Haskell-cafe] On the purity of Haskell
Heinrich Apfelmus
apfelmus at quantentunnel.de
Fri Dec 30 09:49:37 CET 2011
Steve Horne wrote:
> Heinrich Apfelmus wrote:
>>
>> Maybe it helps to try to find an example of a function f :: A -> B
>> for some cleverly chosen types A,B that is not pure, i.e. does not
>> return the same values for equal arguments.
>
> [..]
> For your specific challenge, place that as a left-hand argument in a
> bind...
>
> f :: Int -> IO Int
> f = getAnIntFromTheUser >>= \i -> return (i+1)
>
> Well, the value of i isn't decidable until runtime. The value of i+1 is
> not decidable until runtime. The value of return (i+1) is not decidable
> until runtime and so on. It can only be partially evaluated at
> compile-time, but when it is fully evaluated, you get a different IO
> action returned by f depending on what Int you got from the user.
The function
f :: Int -> IO Int
f x = getAnIntFromTheUser >>= \i -> return (i+x)
is pure according to the common definition of "pure" in the context of
purely functional programming. That's because
f 42 = f (43-1) = etc.
Put differently, the function always returns the same IO action, i.e.
the same value (of type IO Int) when given the same parameter.
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
More information about the Haskell-Cafe
mailing list