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