[Haskell-cafe] Stupid newbie question of the day: why is newMVar in the IO monad?

Danny Gratzer danny.gratzer at gmail.com
Thu Apr 17 14:26:02 UTC 2014


New `MVar` has to return a different memory location every time and this is
noticeable, it's not referentially transparent.

Consider what would happen if we made the transformation

     let a = newMVar 0
     let b = newMVar 0
     putMVar a 1
     readMVar b

to

    let a = newMVar 0
         b = a
     ...

If newMVar was referentially transparent, we can automatically share any of
it's calls with same arguments since they're supposed to return the same
thing everytime. Since it's not referentially transparent, back into the IO
monad it goes.

Also if you do that toplevel counter trick, you want NoInline otherwise GHC
might just inline it at each occurrence and you'll end up with separate
counters.

Cheers,
Danny Gratzer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140417/61b2eb21/attachment.html>


More information about the Haskell-Cafe mailing list