base: Manually unlit .lhs into .hs modules
[ghc.git] / libraries / base / GHC / STRef.hs
1 {-# LANGUAGE Unsafe #-}
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 module GHC.STRef (
20 STRef(..),
21 newSTRef, readSTRef, writeSTRef
22 ) where
23
24 import GHC.ST
25 import GHC.Base
26
27 data STRef s a = STRef (MutVar# s a)
28 -- ^ a value of type @STRef s a@ is a mutable variable in state thread @s@,
29 -- containing a value of type @a@
30
31 -- |Build a new 'STRef' in the current state thread
32 newSTRef :: a -> ST s (STRef s a)
33 newSTRef init = ST $ \s1# ->
34 case newMutVar# init s1# of { (# s2#, var# #) ->
35 (# s2#, STRef var# #) }
36
37 -- |Read the value of an 'STRef'
38 readSTRef :: STRef s a -> ST s a
39 readSTRef (STRef var#) = ST $ \s1# -> readMutVar# var# s1#
40
41 -- |Write a new value into an 'STRef'
42 writeSTRef :: STRef s a -> a -> ST s ()
43 writeSTRef (STRef var#) val = ST $ \s1# ->
44 case writeMutVar# var# val s1# of { s2# ->
45 (# s2#, () #) }
46
47 -- Just pointer equality on mutable references:
48 instance Eq (STRef s a) where
49 STRef v1# == STRef v2# = isTrue# (sameMutVar# v1# v2#)