<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jan 6, 2014 at 9:33 PM, Courtney Robinson <span dir="ltr"><<a href="mailto:courtney@crlog.info" target="_blank">courtney@crlog.info</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I've found this example.</div></blockquote><div><br></div><div>An example of something you should not do; it is very fragile. Most notably, if the compiler decides to inline counter then you no longer have a single IORef. (It will work in ghci/runhaskell, which does not have an optimizer.)</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I have two things I'm failing to see about it.</div>

<div><br></div><div>1) Why doesn't counter return a newIORef with value 0, every time getUnique is called there by producing a value of 1 no matter how often getUnique is called?  Is this this because it returns an IORef so counter just becomes bound to the value it first returns and it's not evaluated again and the IORef is just returned, or something else?</div>
</div></blockquote><div><br></div><div>Anything given a name and no parameters (and not polymorphic; see the monomorphism restriction) is potentially shared. The unsafePerformIO would be run only once and its result remembered and shared between all uses --- if you're lucky.</div>
<div><br></div><div>The compiler is allowed to do anything it wants as long as it would have the same behavior; but because you hid an effect behind unsafePerformIO, thereby telling the compiler that it is a pure value that it is free to remember or recompute as it sees fit, the compiler's idea of what constitutes "the same behavior" is likely to be different from yours.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">

<div>2) Why does the lambda return a tuple and why does the example return the same value in both positions i.e. (y,y)? The only thing I could think of is because the name has "atomic" that the values in the tuple is being used for some sort of compare and swap,CAS. <br>
</div></div></blockquote><div><br></div><div>One value is the return value of atomicModifyIORef; the other is the new value to be stored. You could think of this as the Haskell version of the preincrement/postincrement operators in some languages.</div>
<div><br></div></div>-- <br><div dir="ltr"><div>brandon s allbery kf8nh                               sine nomine associates</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>                                  <a href="mailto:ballbery@sinenomine.net" target="_blank">ballbery@sinenomine.net</a></div>
<div>unix, openafs, kerberos, infrastructure, xmonad        <a href="http://sinenomine.net" target="_blank">http://sinenomine.net</a></div></div>
</div></div>