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

Alexander Dunlap alexander.dunlap at gmail.com
Sat Jan 23 00:22:49 EST 2010


When you put actions in a "do" block, what you are really doing under
the hood is using the ">>=" function to string actions together. (>>=)
:: m a -> (a -> m b) -> m b, so, when m ~ IO, (>>=) :: IO a -> (a ->
IO b) -> IO b. Thus, a do block of IO actions will itself be an IO
action. However, consider the function:

f :: IO a -> Int
f _ = 10

Now the value f (putStrLn "foo") is totally pure, and has the value of
10. A similar thing is going on with the greetAdrian function.

Hope that helps.

Alex

On Fri, Jan 22, 2010 at 7:34 PM, Adrian Adshead
<adrianadshead at yahoo.co.uk> wrote:
>
> OK, so I get the idea that all functions are pure because they
> just return actions.
>
> So as I understand it now...
>
> All functions are pure.
> The evaluation of all functions not declared as IO results in no side effects.
> The evaluation of all functions declared as IO may or may not have side effects.
>
> But how can the function greetAdrian from the example below not be an IO
> operation?
>
>
>
>
> From: Maciej Piechotka <uzytkownik2 <at> gmail.com>
> Subject: Re: Re: testing and the culture of Haskell
> Newsgroups: gmane.comp.lang.haskell.beginners
> Date: 2010-01-23 00:31:42 GMT (2 hours and 52 minutes ago)
>
> 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
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list