2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelST]{The @ST@ monad}
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 newtype ST s a = ST (State# s -> STret s a)
28 data STret s a = STret (State# s) a
30 instance Monad (ST s) where
34 return x = ST $ \ s -> STret s x
35 m >> k = m >>= \ _ -> k
39 case (m s) of { STret new_s r ->
40 case (k r) of { ST k2 ->
45 fixST :: (a -> ST s a) -> ST s a
56 %*********************************************************
58 \subsection{Ghastly return types}
60 %*********************************************************
62 The @State@ type is the return type of a _ccall_ with no result. It
63 never actually exists, since it's always deconstructed straight away;
64 the desugarer ensures this.
67 data State s = S# (State# s)
68 data StateAndPtr# s elt = StateAndPtr# (State# s) elt
70 data StateAndChar# s = StateAndChar# (State# s) Char#
71 data StateAndInt# s = StateAndInt# (State# s) Int#
72 data StateAndWord# s = StateAndWord# (State# s) Word#
73 data StateAndFloat# s = StateAndFloat# (State# s) Float#
74 data StateAndDouble# s = StateAndDouble# (State# s) Double#
75 data StateAndAddr# s = StateAndAddr# (State# s) Addr#