<div dir="ltr">The IO monad is defined as:<br><br>newtype IO a = IO (RealWorld -&gt; (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&#39;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">&lt;<a href="mailto:apfelmus@quantentunnel.de">apfelmus@quantentunnel.de</a>&gt;</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>
&gt;&gt; Technically, even the IO monad is pure, that&#39;s just the runtime-system<br>
&gt;&gt; &gt; that consume your &#39;main&#39; function that perform effects (and unsafeP...).<br>
&gt;<br>
&gt; But, sure the IO monad does have side-effects? I&#39;m confused as to how it<br>
&gt; could be pure. Could you explain?<br>
<br>
&gt; I&#39;m sorry, but I just don&#39;t get it. What are you trying to say? I think it<br>
&gt; would be clearer if we started to define what exactly a &quot;side-effect&quot; is<br>
&gt; (in any language) and work our definitions from there, because now I&#39;m<br>
&gt; really confused.<br>
<br>
A function say &nbsp;f :: Int -&gt; Int &nbsp;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&#39;t change anything<br>
about that. This is what is meant by &quot;IO is pure&quot;. While IO allows you to print<br>
stuff on the screen, it doesn&#39;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>
 &nbsp;putStr &nbsp;:: String -&gt; ()<br>
 &nbsp;getChar :: () -&gt; 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>