
Adrian Adshead wrote:
add_impure :: Integer -> IO Integer add_impure x = return (x + 5)
add_pure is clearly a pure function. add_impure while it is totally side effect free and therefore fulfills the definition of purity, is impure as far as the compiler is concerned since I (the programmer) have told the compiler that I do not guarantee that the function is pure.
Please let me know where I am misunderstanding purity.
Because, actually, (IO Integer) represents an action but it'll only be executed if it's sequenced somewhere under main. e.g. main :: IO () main = do let io_action = something_impure x y z -- nothing has happened yet! io_action io_action -- now we've executed the impure action *twice*! (But some of -- the pure computation based on x y and z that determined -- what actions io_action should be, might be shared) (a simplistic example... io_action can do different things each time it's executed based on any reason it can execute in IO, such as, reading the system clock) -Isaac