shithub: MicroHs

ref: c101325aaf8ed0ec3c8c4ff4b7d425bba799825a
dir: /lib/Data/STRef.hs/

View raw version
module Data.STRef(
  STRef,
  newSTRef, readSTRef, writeSTRef, modifySTRef,
  ) where
import Prelude(); import MiniPrelude
import Control.Monad.ST_Type
import Data.IORef
import {-# SOURCE #-} Data.Typeable

newtype STRef s a = R (IORef a)
  deriving (Typeable)

newSTRef :: forall s a . a -> ST s (STRef s a)
newSTRef a = ST (R <$> newIORef a)

readSTRef :: forall s a . STRef s a -> ST s a
readSTRef (R p) = ST (readIORef p)

writeSTRef :: forall s a . STRef s a -> a -> ST s ()
writeSTRef (R p) a = ST (writeIORef p a)

modifySTRef :: forall s a . STRef s a -> (a -> a) -> ST s ()
modifySTRef (R p) f = ST (modifyIORef p f)