<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi Alex,<br><br>That does help, but raises a further question.<br><br>If (>>=) :: m a -> (a -> m b) -> m b<br><br>Then whenever the IO monad is entered it can not be escaped,<br>so in the example you gave:-<br><br>f :: IO a -> Int<br>f _ = 10<br><br><br>while f (putStrLn "foo") is a pure function evaluating to 10,<br>neither the IO Action, nor the a (from 'IO a') can be used without<br>using the (>>=) or (>>). Therefore since the parameter can not<br>be used in the function f without entering the IO monad, your<br>function would be equivalent to :-<br><br>f :: Int<br>f = 10<br><br>I have two questions to try and clarify my understanding.<br><br>1) Is it possible to have a function with an IO Action as an input<br>parameter (and not just ignore the parameter) and have a non-IO<br>return value?<br>(ie is
f :: IO a -> b possible without ignoring the IO a)<br><br>2) Is it possible to have a function which evaluates any IO Action<br>within the body and have a non-IO return value?<br>(ie is f :: a -> b possible while evaluating any IO Action<br>within the body.)<br><br><br><br>My understanding of Haskell was that both of these situations<br>would not be possible, and the return value would always be<br>of the form IO b. (For these questions I am excluding use of<br>unsafePerformIO)<br><br>Many thanks for your time.<br><br>Adrian.<br><br><br>--- On <b>Sat, 23/1/10, Alexander Dunlap <i><alexander.dunlap@gmail.com></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Alexander Dunlap <alexander.dunlap@gmail.com><br>Subject: Re: [Haskell-beginners] Re: Re: testing and the culture of Haskell<br>To: "Adrian
Adshead" <adrianadshead@yahoo.co.uk><br>Cc: beginners@haskell.org<br>Date: Saturday, 23 January, 2010, 5:22<br><br><div class="plainMail">When you put actions in a "do" block, what you are really doing under<br>the hood is using the ">>=" function to string actions together. (>>=)<br>:: m a -> (a -> m b) -> m b, so, when m ~ IO, (>>=) :: IO a -> (a -><br>IO b) -> IO b. Thus, a do block of IO actions will itself be an IO<br>action. However, consider the function:<br><br>f :: IO a -> Int<br>f _ = 10<br><br>Now the value f (putStrLn "foo") is totally pure, and has the value of<br>10. A similar thing is going on with the greetAdrian function.<br><br>Hope that helps.<br><br>Alex<br><br>On Fri, Jan 22, 2010 at 7:34 PM, Adrian Adshead<br><<a ymailto="mailto:adrianadshead@yahoo.co.uk" href="/mc/compose?to=adrianadshead@yahoo.co.uk">adrianadshead@yahoo.co.uk</a>> wrote:<br>><br>> OK, so I get the idea that
all functions are pure because they<br>> just return actions.<br>><br>> So as I understand it now...<br>><br>> All functions are pure.<br>> The evaluation of all functions not declared as IO results in no side effects.<br>> The evaluation of all functions declared as IO may or may not have side effects.<br>><br>> But how can the function greetAdrian from the example below not be an IO<br>> operation?<br>><br>><br>><br>><br>> From: Maciej Piechotka <uzytkownik2 <at> gmail.com><br>> Subject: Re: Re: testing and the culture of Haskell<br>> Newsgroups: gmane.comp.lang.haskell.beginners<br>> Date: 2010-01-23 00:31:42 GMT (2 hours and 52 minutes ago)<br>><br>> On Fri, 2010-01-22 at 20:59 +0000, Adrian Adshead wrote:<br>> > >All Haskell functions are pure without exception. For example:<br>> > ><br>> > >greet :: String -> IO ()<br>> > >greet
name = putStrLn $ "Hello, "++name<br>> > ><br>> > >This is a pure function from String to IO (). This function (like all<br>> > >Haskell functions) has no side effects. Its return value of type IO ()<br>> > >merely _represents_ an IO action. The runtime system knows how to act<br>> > >on this representation.<br>> > ><br>> > >This also means that there is no such thing in Haskell as marking a<br>> > >function as side-effecting.<br>> > ><br>> > >This distinction may be subtle, but it's important.<br>> > ><br>> > ><br>> > >Steve<br>> ><br>> > Steve,<br>> ><br>> > Please could you clarify this for me since you are making exactly<br>> > the opposite assertion than I have<br>> understood.<br>> ><br>> > I am confused by you stating "All Haskell functions are pure<br>> >
without<br>> > exception.".<br>> ><br>> > Pure functions have no impact on 'anything'. They take input<br>> > parameters (which they don't change) and return exactly the<br>> > same result whenever the same input parameters are given.<br>> ><br>> > >greet :: String -> IO ()<br>> > >greet name = putStrLn $ "Hello, "++name<br>> ><br>> > This example you gave is not a pure function since it does have<br>> > the side effect that the screen is changed by outputting the string<br>> > "Hello, " and the name passed in.<br>> ><br>> ><br>><br>> greatAdrian :: String<br>> greetAdrian = let x = greet "Adrian"<br>> in x `seq` f x<br>><br>> greet can be consider a pure function and value IO () is evaluated by<br>> seq. IO () represents an action(s) not execution of action(s). If f of
x<br>> does not use any tricks nothing will be<br>> printed.<br>><br>> IO a value it can be:<br>> - cast unsafely into a. However I guess we omit this shame for a moment<br>> - binded with other action. But the resultant type is now again IO b. So<br>> we still get a something<br>> - returned as main. Then we might consider whole Haskell program as<br>> metalanguage which returns single thing - other program. In similar way<br>> as:<br>><br>> type PythonProgram = String<br>> main :: PythonProgram<br>> main = "print \"Hello World\""<br>><br>> is pure add_impure is pure. What we do with the result is other thing.<br>><br>> Regards<br>><br>><br>><br>> _______________________________________________<br>> Beginners mailing list<br>> <a ymailto="mailto:Beginners@haskell.org" href="/mc/compose?to=Beginners@haskell.org">Beginners@haskell.org</a><br>> <a
href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>><br></div></blockquote></td></tr></table><br>