[Haskell] ANNOUNCING: The Haskell Bookstore

Simon Marlow simonmar at microsoft.com
Fri Jun 25 07:14:26 EDT 2004


On 25 June 2004 11:30, Daan Leijen wrote:

> Is there anyone who knows why "atomicModifyIORef" has type:
> 
>   IORef a -> (a -> (a, b)) -> IO b		(1)
> 
> Instead of:
> 
>   IORef a -> (a -> a) -> IO a			(2)
> 
> 
> It seems to me that returning the old value is always good
> enough right?  Here is an implementation of "atomicModifyIORef"
> with the current type in terms of a function "proposedModifyIORef"
> with type (2).
> 
> atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b
> atomicModifyIORef ref f
>    = do old <- proposedModifyIORef ref (fst . f)
>         return (snd (f old))

Yes, but in your version you apply f to the old value twice, potentially
duplicating an expensive computation.

atomicModifyIORef isn't part of the FFI spec, BTW.

Cheers,
	Simon



More information about the FFI mailing list