2 {-# OPTIONS_GHC -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow, 1994-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- References in the 'ST' monad.
15 -----------------------------------------------------------------------------
18 module GHC.STRef where
23 data STRef s a = STRef (MutVar# s a)
24 -- ^ a value of type @STRef s a@ is a mutable variable in state thread @s@,
25 -- containing a value of type @a@
27 -- |Build a new 'STRef' in the current state thread
28 newSTRef :: a -> ST s (STRef s a)
29 newSTRef init = ST $ \s1# ->
30 case newMutVar# init s1# of { (# s2#, var# #) ->
31 (# s2#, STRef var# #) }
33 -- |Read the value of an 'STRef'
34 readSTRef :: STRef s a -> ST s a
35 readSTRef (STRef var#) = ST $ \s1# -> readMutVar# var# s1#
37 -- |Write a new value into an 'STRef'
38 writeSTRef :: STRef s a -> a -> ST s ()
39 writeSTRef (STRef var#) val = ST $ \s1# ->
40 case writeMutVar# var# val s1# of { s2# ->
43 -- Just pointer equality on mutable references:
44 instance Eq (STRef s a) where
45 STRef v1# == STRef v2# = sameMutVar# v1# v2#