[Haskell-cafe] RE: transactional cache

Alberto G. Corona agocorona at gmail.com
Fri May 12 13:57:42 EDT 2006

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:

   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 at vex.net>
Subject: Re: [Haskell-cafe] Re: Haskell-Cafe Digest, Vol 33, Issue 9
To: haskell-cafe at haskell.org
Message-ID: <4u3bffq3hr.fsf at shell.vex.net>
Content-Type: text/plain; charset=us-ascii

"Alberto G. Corona " <agocorona at 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

More information about the Haskell-Cafe mailing list