2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[module_ST]{The State Transformer Monad, @ST@}
7 {-# OPTIONS -fno-implicit-prelude #-}
13 runST, -- :: (All s => ST s a) -> a
14 fixST, -- :: (a -> ST s a) -> ST s a
18 -- ST is one, so you'll likely need some Monad bits
22 newSTRef, readSTRef, writeSTRef,
25 newSTArray, readSTArray, writeSTArray, Ix
30 import Unsafe ( unsafeInterleaveST )
32 import PrelBase ( Eq(..), Int, Bool, ($), ()(..) )
38 %*********************************************************
40 \subsection{Variables}
42 %*********************************************************
45 newtype STRef s a = STRef (MutableVar s a) deriving Eq
47 newSTRef :: a -> ST s (STRef s a)
48 newSTRef v = newVar v >>= \ var -> return (STRef var)
50 readSTRef :: STRef s a -> ST s a
51 readSTRef (STRef var) = readVar var
53 writeSTRef :: STRef s a -> a -> ST s ()
54 writeSTRef (STRef var) v = writeVar var v
57 %*********************************************************
61 %*********************************************************
64 type STArray s ix elt = MutableArray s ix elt
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)
75 boundsSTArray = boundsOfArray
76 readSTArray = readArray
77 writeSTArray = writeArray
78 thawSTArray = thawArray
79 freezeSTArray = freezeArray
80 unsafeFreezeSTArray = unsafeFreezeArray