add GHC.HetMet.{hetmet_kappa,hetmet_kappa_app}
[ghc-base.git] / GHC / STRef.lhs
1 \begin{code}
2 {-# LANGUAGE NoImplicitPrelude, MagicHash, UnboxedTuples #-}
3 {-# OPTIONS_HADDOCK hide #-}
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module      :  GHC.STRef
8 -- Copyright   :  (c) The University of Glasgow, 1994-2002
9 -- License     :  see libraries/base/LICENSE
10 -- 
11 -- Maintainer  :  cvs-ghc@haskell.org
12 -- Stability   :  internal
13 -- Portability :  non-portable (GHC Extensions)
14 --
15 -- References in the 'ST' monad.
16 --
17 -----------------------------------------------------------------------------
18
19 -- #hide
20 module GHC.STRef where
21
22 import GHC.ST
23 import GHC.Base
24
25 data STRef s a = STRef (MutVar# s a)
26 -- ^ a value of type @STRef s a@ is a mutable variable in state thread @s@,
27 -- containing a value of type @a@
28
29 -- |Build a new 'STRef' in the current state thread
30 newSTRef :: a -> ST s (STRef s a)
31 newSTRef init = ST $ \s1# ->
32     case newMutVar# init s1#            of { (# s2#, var# #) ->
33     (# s2#, STRef var# #) }
34
35 -- |Read the value of an 'STRef'
36 readSTRef :: STRef s a -> ST s a
37 readSTRef (STRef var#) = ST $ \s1# -> readMutVar# var# s1#
38
39 -- |Write a new value into an 'STRef'
40 writeSTRef :: STRef s a -> a -> ST s ()
41 writeSTRef (STRef var#) val = ST $ \s1# ->
42     case writeMutVar# var# val s1#      of { s2# ->
43     (# s2#, () #) }
44
45 -- Just pointer equality on mutable references:
46 instance Eq (STRef s a) where
47     STRef v1# == STRef v2# = sameMutVar# v1# v2#
48 \end{code}