[Haskell-cafe] ST not strict enough?

Johan Tibell johan.tibell at gmail.com
Wed Nov 16 23:42:13 CET 2011


On Wed, Nov 16, 2011 at 2:23 PM, Jason Dusek <jason.dusek at gmail.com> wrote:

> > Just double checked. modifySTRef is too lazy:
> > -- |Mutate the contents of an 'STRef'
> > modifySTRef :: STRef s a -> (a -> a) -> ST s ()
> > modifySTRef ref f = writeSTRef ref . f =<< readSTRef ref
> > We need Data.STRef.Strict
>
> Tried a modifySTRef' defined this way:
>
> modifySTRef' ref f           =  do
>  val                       <-  (f $!!) <$> readSTRef ref
>  writeSTRef ref (val `seq` val)
>
> ...but there was no change in memory usage.


Why not just

    modifySTRef :: STRef s a -> (a -> a) -> ST s ()
    modifySTRef ref f = do
        x <- readSTRef ref
        writeSTRef ref $! f x

(Note that I didn't check if modifySTRef was actually a problem in this
case).

-- Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111116/1a12ff17/attachment.htm>


More information about the Haskell-Cafe mailing list