[commit: packages/mtl] master: Ensure strictness in the state in modify'. (56d2703)

git at git.haskell.org git at git.haskell.org
Thu Aug 3 09:58:08 UTC 2017


Repository : ssh://git@git.haskell.org/mtl

On branch  : master
Link       : http://git.haskell.org/packages/mtl.git/commitdiff/56d270389ce1b852e522ce70c93e79660852a753

>---------------------------------------------------------------

commit 56d270389ce1b852e522ce70c93e79660852a753
Author: Philipp <philipp at infinipool.com>
Date:   Thu Jun 5 13:32:58 2014 +0200

    Ensure strictness in the state in modify'.
    
    The old implementation of modify' used state f with a function f that was strict in the new state.  However, in state itself, f is applied to the state in a non-strict way, so the strictness is lost.
    
    The implementation suggested here should ensure that the state is evaluated.


>---------------------------------------------------------------

56d270389ce1b852e522ce70c93e79660852a753
 Control/Monad/State/Class.hs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Control/Monad/State/Class.hs b/Control/Monad/State/Class.hs
index 82f695f..3efe077 100644
--- a/Control/Monad/State/Class.hs
+++ b/Control/Monad/State/Class.hs
@@ -87,7 +87,9 @@ modify f = state (\s -> ((), f s))
 -- | A variant of 'modify' in which the computation is strict in the
 -- new state.
 modify' :: MonadState s m => (s -> s) -> m ()
-modify' f = state (\s -> let s' = f s in s' `seq` ((), s'))
+modify' f = do
+  s' <- liftM f get
+  s' `seq` put s'
 
 -- | Gets specific component of the state, using a projection function
 -- supplied.



More information about the ghc-commits mailing list