Albert Lai: 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 newIORef c) and then dereferencing refcache in the IO Monad I get ever the same context no matter where i do it: do 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. -----------referred message:------- Message: 1 Date: 12 May 2006 00:19:28 -0400 From: Albert Lai <trebla@vex.net> Subject: Re: [Haskell-cafe] Re: Haskell-Cafe Digest, Vol 33, Issue 9 To: haskell-cafe@haskell.org Message-ID: <4u3bffq3hr.fsf@shell.vex.net> Content-Type: text/plain; charset=us-ascii "Alberto G. Corona " <agocorona@gmail.com> writes:
stmcache= newTVar 0
I will explain what this doesn't with an analogy. import Data.IORef notglobal = newIORef True main = do a <- notglobal b <- notglobal writeIORef a False x <- readIORef b print x To better show what's going on, I also provide this for contrast: import Data.IORef import System.IO.Unsafe global = unsafePerformIO (newIORef True) main = do x <- readIORef global print x writeIORef global False x <- readIORef global print x
On 5/13/06, Alberto G. Corona <agocorona@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.)
participants (2)
-
Alberto G. Corona -
Evan Martin