ed3474df9ef71fc74bb3fdcf37de99abc413e6f0
[ghc-hetmet.git] / ghc / lib / glaExts / ST.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[module_ST]{The State Transformer Monad, @ST@}
5
6 \begin{code}
7 {-# OPTIONS -fno-implicit-prelude #-}
8
9 module ST (
10
11         ST,
12
13         runST,                          -- :: (All s => ST s a) -> a
14         fixST,                          -- :: (a -> ST s a) -> ST s a
15
16         unsafeInterleaveST,
17
18         -- ST is one, so you'll likely need some Monad bits
19         module Monad,
20
21         STRef,
22         newSTRef, readSTRef, writeSTRef,
23
24         STArray,
25         newSTArray, readSTArray, writeSTArray, Ix
26
27     ) where
28
29 import ArrBase
30 import Unsafe   ( unsafeInterleaveST )
31 import STBase
32 import PrelBase ( Eq(..), Int, Bool, ($), ()(..) )
33 import Monad
34 import Ix
35
36 \end{code}
37
38 %*********************************************************
39 %*                                                      *
40 \subsection{Variables}
41 %*                                                      *
42 %*********************************************************
43
44 \begin{code}
45 newtype STRef s a = STRef (MutableVar s a) deriving Eq
46
47 newSTRef :: a -> ST s (STRef s a)
48 newSTRef v = newVar v >>= \ var -> return (STRef var)
49
50 readSTRef :: STRef s a -> ST s a
51 readSTRef (STRef var) = readVar var
52
53 writeSTRef :: STRef s a -> a -> ST s ()
54 writeSTRef (STRef var) v = writeVar var v
55 \end{code}
56
57 %*********************************************************
58 %*                                                      *
59 \subsection{Arrays}
60 %*                                                      *
61 %*********************************************************
62
63 \begin{code}
64 type STArray s ix elt = MutableArray s ix elt
65
66 newSTArray              :: Ix ix => (ix,ix) -> elt -> ST s (STArray s ix elt)
67 writeSTArray            :: Ix ix => STArray s ix elt -> ix -> elt -> ST s () 
68 readSTArray             :: Ix ix => STArray s ix elt -> ix -> ST s elt 
69 boundsSTArray           :: Ix ix => STArray s ix elt -> (ix, ix)  
70 thawSTArray             :: Ix ix => Array ix elt -> ST s (STArray s ix elt)
71 freezeSTArray           :: Ix ix => STArray s ix elt -> ST s (Array ix elt)
72 unsafeFreezeSTArray     :: Ix ix => STArray s ix elt -> ST s (Array ix elt)
73
74 newSTArray              = newArray
75 boundsSTArray           = boundsOfArray
76 readSTArray             = readArray
77 writeSTArray            = writeArray
78 thawSTArray             = thawArray
79 freezeSTArray           = freezeArray
80 unsafeFreezeSTArray     = unsafeFreezeArray
81 \end{code}
82