<div dir="ltr">The IO monad is defined as:<br><br>newtype IO a = IO (RealWorld -> (RealWorld, a))<br><br>Notice that it encapsulates a pure function from RealWorld to RealWord plus result. Semantically, the whole state of the world is passed into your main function and a new world state is returned containing any changes you made. However, the implementation just performs side effects on the world (I/O), but the monad type ensures that these side effects are sequential and that you can never gain access to the underlying RealWorld so it won't be duplicated. These restrictions allow us to view this implementation as a pure function.<br>
<br>Non-primitive monads like Control.Monad.State are actually implemented as pure functions passing the state around.<br><br>Cheers,<br>Tony<br><br><div class="gmail_quote">On Sat, Sep 6, 2008 at 10:18 AM, apfelmus <span dir="ltr"><<a href="mailto:apfelmus@quantentunnel.de">apfelmus@quantentunnel.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">C.M.Brown wrote:<br>
>> Technically, even the IO monad is pure, that's just the runtime-system<br>
>> > that consume your 'main' function that perform effects (and unsafeP...).<br>
><br>
> But, sure the IO monad does have side-effects? I'm confused as to how it<br>
> could be pure. Could you explain?<br>
<br>
> I'm sorry, but I just don't get it. What are you trying to say? I think it<br>
> would be clearer if we started to define what exactly a "side-effect" is<br>
> (in any language) and work our definitions from there, because now I'm<br>
> really confused.<br>
<br>
A function say f :: Int -> Int is said to be *pure* if its result depends only<br>
on the argument.<br>
<br>
In Haskell, all functions are pure and the IO monad doesn't change anything<br>
about that. This is what is meant by "IO is pure". While IO allows you to print<br>
stuff on the screen, it doesn't change the fundamental property of the language<br>
that all functions are free of side-effects.<br>
<br>
In other languages like C or even ML, there are functions like<br>
<br>
putStr :: String -> ()<br>
getChar :: () -> Char<br>
<br>
whose results depend on more things than their parameter and who can perform<br>
side-effects.<br>
<br>
<br>
Regards,<br>
apfelmus<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto: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>
</blockquote></div><br></div>