MVar API - modifyMVar

Jules Bean jules at
Fri Sep 21 05:52:59 EDT 2007

The MVar API is a bit inconsistent with the IORef one, and it catches me 
out every time.

We have:

modifyIORef :: IORef a -> (a->a) -> IO ()

which takes a nice pure modification function, and yet modifyMVar has a 
slightly weird Kleisli type:

modifyMVar :: MVar a -> (a -> IO (a, b)) -> IO b

which not only moves the modification into the IO monad but also permits 
returning an auxiliary value.

One has only to browse the source of Chan to see that this function is 
useful, but I do think it's a surprising default. Then there is the _ 

So, we currently have:

modifyMVar  :: MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()

I suggest that the following would be more consistent with the normal 
conventions for _ and M:

modifyMVar   :: MVar a -> (a -> a)         -> IO ()
modifyMVarM  :: MVar a -> (a -> IO (a, b)) -> IO b
modifyMVarM_ :: MVar a -> (a -> IO a)      -> IO ()

...however, that would break currently working code. If that seems to 
painful, then please at least supply some form of MVar a -> (a -> a) -> 
IO (), under whatever name seems suitable.


PS : Dear moderator : I subscribed and reposted, you may have a copy of 
this in your moderation queue too.

More information about the Libraries mailing list