Add modifyRef that returns a pure value

This is equivalent to the atomicModifyIORef from Haskell