Re: [Haskell-beginners] Re: testing and the culture of Haskell

All Haskell functions are pure without exception. For example:
greet :: String -> IO () greet name = putStrLn $ "Hello, "++name
This is a pure function from String to IO (). This function (like all Haskell functions) has no side effects. Its return value of type IO () merely _represents_ an IO action. The runtime system knows how to act on this representation.
This also means that there is no such thing in Haskell as marking a function as side-effecting.
This distinction may be subtle, but it's important.
Steve
Steve, Please could you clarify this for me since you are making exactly the opposite assertion than I have understood. I am confused by you stating "All Haskell functions are pure without exception.". Pure functions have no impact on 'anything'. They take input parameters (which they don't change) and return exactly the same result whenever the same input parameters are given.
greet :: String -> IO () greet name = putStrLn $ "Hello, "++name
This example you gave is not a pure function since it does have the side effect that the screen is changed by outputting the string "Hello, " and the name passed in. As I understand it the IO in the type signature is the programmers indication to the compiler that the function is not guaranteed to be side effect free. add_pure :: Integer -> Integer add_pure x = x + 5 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. Many thanks Adrian.

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

On Fri, Jan 22, 2010 at 08:59:57PM +0000, Adrian Adshead wrote:
I am confused by you stating "All Haskell functions are pure without exception.".
Pure functions have no impact on 'anything'. They take input parameters (which they don't change) and return exactly the same result whenever the same input parameters are given.
greet :: String -> IO () greet name = putStrLn $ "Hello, "++name
This example you gave is not a pure function since it does have the side effect that the screen is changed by outputting the string "Hello, " and the name passed in.
This is indeed a subtle point, but this example actually *is* a pure function. When you pass it a String, it returns a value of type IO (), which is a *description* of a (side-effecting) computation to be carried out. Every time you pass it the same String, it will give you back the exact same *description* of a computation. It is only when that description is handed off to the runtime system (usually by including it somewhere in the special IO () value called 'main') that it gets run and causes actual effects. -Brent

On Fri, 2010-01-22 at 20:59 +0000, Adrian Adshead wrote:
All Haskell functions are pure without exception. For example:
greet :: String -> IO () greet name = putStrLn $ "Hello, "++name
This is a pure function from String to IO (). This function (like all Haskell functions) has no side effects. Its return value of type IO () merely _represents_ an IO action. The runtime system knows how to act on this representation.
This also means that there is no such thing in Haskell as marking a function as side-effecting.
This distinction may be subtle, but it's important.
Steve
Steve,
Please could you clarify this for me since you are making exactly the opposite assertion than I have understood.
I am confused by you stating "All Haskell functions are pure without exception.".
Pure functions have no impact on 'anything'. They take input parameters (which they don't change) and return exactly the same result whenever the same input parameters are given.
greet :: String -> IO () greet name = putStrLn $ "Hello, "++name
This example you gave is not a pure function since it does have the side effect that the screen is changed by outputting the string "Hello, " and the name passed in.
greatAdrian :: String greetAdrian = let x = greet "Adrian" in x `seq` f x greet can be consider a pure function and value IO () is evaluated by seq. IO () represents an action(s) not execution of action(s). If f of x does not use any tricks nothing will be printed. IO a value it can be: - cast unsafely into a. However I guess we omit this shame for a moment - binded with other action. But the resultant type is now again IO b. So we still get a something - returned as main. Then we might consider whole Haskell program as metalanguage which returns single thing - other program. In similar way as: type PythonProgram = String main :: PythonProgram main = "print \"Hello World\"" is pure add_impure is pure. What we do with the result is other thing. Regards
participants (4)
-
Adrian Adshead
-
Brent Yorgey
-
Isaac Dupree
-
Maciej Piechotka