Thanks. I got the poit more or less; Each invocation creates a new
IORef instance. UnsafePerformIO appears to generate a unique IORef
that can be shared (sorry for my imperative vocabulary, I´m sill
contaminated by al these evil languages ;).
I tried with "usafePerformIO NewTVar v" but the program fails
miserably in a memory fault. I finally did it well usign a IORef than
contains the TVar:
refcache =unsafePerformIO $ (do c <- atomically $ newTVar emptyFM
and then dereferencing refcache in the IO Monad I get ever the same
context no matter where i do it:
tvcache <- readIORef refcache
atomically $ do
finiteMap <- readTVar tvcache
(useful code here at last)...
Who said that Haskell is difficult?. Jokes apart, STM is powerful. I
will share the transactional cache when I have it tested.
Date: 12 May 2006 00:19:28 -0400
From: Albert Lai <[hidden email]>
Subject: Re: [Haskell-cafe] Re: Haskell-Cafe Digest, Vol 33, Issue 9
To: [hidden email] Message-ID: <[hidden email]>
Content-Type: text/plain; charset=us-ascii
On 5/13/06, Alberto G. Corona <[hidden email]> 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.)
Haskell-Cafe mailing list
[hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe