Questions tagged [ioref]

For IORefs in the Haskell programming language, and their explicitly monadic equivalents in other programming languages

An IORef - originally defined in the Haskell programming language - is a reference in the runtime to a value, which can be modified any number of times at runtime.

It is closely related to mutable variables, but because IORefs are always mutated within the IO monad, in Haskell at least, type-safety of the programming language ensures that IORefs cannot be modified by pure functions (unless unsafePerformIO or similar unsafe functions are called).

45 questions
99
votes
3 answers

Difference between State, ST, IORef, and MVar

I am working through Write Yourself a Scheme in 48 Hours (I'm up to about 85hrs) and I've gotten to the part about Adding Variables and Assignments. There is a big conceptual jump in this chapter, and I wish it had been done in two steps with a good…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
65
votes
4 answers

When is it OK to use an IORef?

One thing that has always confused me is whether or not it's an okay time to use an IORef. Are there any guidelines that should be followed when deciding whether or not to use an IORef for a task? When is a good time to use the State monad over an…
Rayne
  • 31,473
  • 17
  • 86
  • 101
26
votes
1 answer

Avoiding IORefs in pure code

I noticed that Data.UnionFind uses the IO monad to provide pointers via IORefs. I imagine everyone happily calls unsafePerformIO when using it locally in pure code, since the data structure is so well understood, but .. Is there a canonical cleaner…
Jeff Burdges
  • 4,204
  • 23
  • 46
24
votes
2 answers

When to use STRef or IORef?

What exactly is the difference between STRef and IORef and when do I use each of them? As far as I can tell they both are for mutable state so whats the point of both of them existing?
user782220
  • 10,677
  • 21
  • 72
  • 135
19
votes
1 answer

What is the difference between an IORef and an MVar?

I'm having a little trouble understanding the basic difference between the IORef type and the MVar type in Haskell. Can someone help me out with this? They appear to solve the same problem. MVar seems to be targeted at multithreading, but IORef has…
Litherum
  • 22,564
  • 3
  • 23
  • 27
15
votes
1 answer

IORef in Haskell

I was wondering if there was a legitimate usage for IORef in Haskell? More specifically I would be thankful if someone could address the following or point to an appropriate place to know more about this: Is using IORef considered a bad Haskell…
user5803465
  • 343
  • 2
  • 7
11
votes
3 answers

Reasoning about IORef operation reordering in concurrent programs

The docs say: In a concurrent program, IORef operations may appear out-of-order to another thread, depending on the memory model of the underlying processor architecture...The implementation is required to ensure that reordering of memory…
jberryman
  • 16,334
  • 5
  • 42
  • 83
10
votes
3 answers

When sharing an IORef, is it safe to read with readIORef as long as I'm writing with atomicModifyIORef?

If I share an IORef among multiple threads, and use atomicModifyIORef to write to it: atomicModifyIORef ref (\_ -> (new, ())) Is it safe to read the value with plain old readIORef? Or is there a chance readIORef will return the old value in…
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
10
votes
4 answers

What is the purpose of the extra result parameter of atomicModifyIORef?

The signature of modifyIORef is straightforward enough: modifyIORef :: IORef a -> (a -> a) -> IO () Unfortunately, this is not thread safe. There is an alternative that adresses this issue: atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b What…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
9
votes
3 answers

Haskell: performance of IORefs

I have been trying to encode an algorithm in Haskell that requires using lots of mutable references, but it is (perhaps not surprisingly) very slow in comparison to purely lazy code. Consider a very simple example: module Main where import…
hpacheco
  • 235
  • 1
  • 8
8
votes
2 answers

Show for IO types

I have a data type which contains an IORef as an important element. This means there is not a clean way to make it a member of the show type class. This is not too bad as I have a print function in the IO monad for this type. But it is annoying in…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
7
votes
2 answers

Haskell: Concurrent data structure guidelines

I've been trying to get a understanding of concurrency, and I've been trying to work out what's better, one big IORef lock or many TVars. I've came to the following guidelines, comments will be appreciated, regarding whether these are roughly right…
Clinton
  • 22,361
  • 15
  • 67
  • 163
6
votes
1 answer

Haskell: generic IORef, MVar?

I made the following function which is specific for the IO monad: memoIO :: MonadIO m => m a -> IO (m a) memoIO action = do ref <- newMVar Nothing return $ do x <- maybe action return =<< liftIO (takeMVar ref) liftIO . putMVar ref $ Just…
yairchu
  • 23,680
  • 7
  • 69
  • 109
5
votes
1 answer

How come `readIORef` is a blocking operation

This was a complete surprise for me. Can someone explain what is the reason behind readIORef blocking, when there is an atomicModifyIORef in flight? I understand that the assumption is that the modifying function supplied to the latter function is…
lehins
  • 9,642
  • 2
  • 35
  • 49
5
votes
1 answer

IORef still refers to the old value after update

Background I am a Schemer starting to learn Haskell. I am trying to implement a Scheme interpreter in C, following chapter 4 of SICP. It turns out programming directly in C is too hard. So I decide to first prototype in Haskell. With the help of…
Alex Vong
  • 483
  • 4
  • 15
1
2 3