[Haskell-cafe] RE: transactional cache

Evan Martin martine at danga.com
Fri May 12 23:26:42 EDT 2006


On 5/13/06, Alberto G. Corona <agocorona at gmail.com> wrote:
> > notglobal = newIORef True
> > main = do a <- notglobal
> >         b <- notglobal
>
> Thanks. I got the poit more or less; Each invocation creates a new
> IORef instance.

Another way of looking at this, that might be more instructive, is
that notglobal is defined to be the action of creating a new IO ref.
You can see that in its type:
  > :t newIORef True
  newIORef True :: IO (IORef Bool)
I read that type as "an IO operation that produces an IORef Bool when executed".

Then the code in main "executes" notglobal twice.

Another way of looking at this is that you can always substitute the
right side of an equals sign in for the left side.  If you do that on
this code this makes it plain that a and b will be different.
(unsafePerformIO breaks this substitution rule.)


More information about the Haskell-Cafe mailing list