2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[STBase]{The @ST@ and @PrimIO@ monads}
7 {-# OPTIONS -fno-implicit-prelude #-}
16 %*********************************************************
18 \subsection{The @ST@ monad}
20 %*********************************************************
22 The state-transformer monad proper. By default the monad is strict;
23 too many people got bitten by space leaks when it was lazy.
26 data State a = S# (State# a)
27 newtype ST s a = ST (State s -> (a, State s))
30 = case m (S# realWorld#) of
33 instance Monad (ST s) where
37 return x = ST $ \ s@(S# _) -> (x, s)
38 m >> k = m >>= \ _ -> k
42 case (m s) of {(r, new_s) ->
43 case (k r) of { ST k2 ->
48 -- here for backward compatibility:
50 {-# INLINE returnST #-}
51 returnST :: a -> ST s a
52 thenST :: ST s a -> (a -> ST s b) -> ST s b
53 seqST :: ST s a -> ST s b -> ST s b
59 fixST :: (a -> ST s a) -> ST s a
67 -- more backward compatibility stuff:
68 listST :: [ST s a] -> ST s [a]
69 mapST :: (a -> ST s b) -> [a] -> ST s [b]
70 mapAndUnzipST :: (a -> ST s (b,c)) -> [a] -> ST s ([b],[c])
74 mapAndUnzipST = mapAndUnzipM
79 %*********************************************************
81 \subsection{The @PrimIO@ monad}
83 %*********************************************************
86 type PrimIO a = ST RealWorld a
88 fixPrimIO :: (a -> PrimIO a) -> PrimIO a
91 -- the following functions are now there for backward compatibility mostly:
93 {-# GENERATE_SPECS returnPrimIO a #-}
94 returnPrimIO :: a -> PrimIO a
96 {-# GENERATE_SPECS thenPrimIO b #-}
97 thenPrimIO :: PrimIO a -> (a -> PrimIO b) -> PrimIO b
99 {-# GENERATE_SPECS seqPrimIO b #-}
100 seqPrimIO :: PrimIO a -> PrimIO b -> PrimIO b
102 listPrimIO :: [PrimIO a] -> PrimIO [a]
103 mapPrimIO :: (a -> PrimIO b) -> [a] -> PrimIO [b]
104 mapAndUnzipPrimIO :: (a -> PrimIO (b,c)) -> [a] -> PrimIO ([b],[c])
106 {-# INLINE returnPrimIO #-}
107 {-# INLINE thenPrimIO #-}
108 {-# INLINE seqPrimIO #-}
110 returnPrimIO = return
113 listPrimIO = accumulate
115 mapAndUnzipPrimIO = mapAndUnzipM
119 %*********************************************************
121 \subsection{Ghastly return types}
123 %*********************************************************
126 data StateAndPtr# s elt = StateAndPtr# (State# s) elt
128 data StateAndChar# s = StateAndChar# (State# s) Char#
129 data StateAndInt# s = StateAndInt# (State# s) Int#
130 data StateAndWord# s = StateAndWord# (State# s) Word#
131 data StateAndFloat# s = StateAndFloat# (State# s) Float#
132 data StateAndDouble# s = StateAndDouble# (State# s) Double#
133 data StateAndAddr# s = StateAndAddr# (State# s) Addr#