RE: transactional cache

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

RE: transactional cache

Alberto G. Corona
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 <[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

"Alberto G. Corona " <[hidden email]> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: RE: transactional cache

Evan Martin
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