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 Functor (ST s) where
31 map f (ST m) = ST $ \ s ->
32 case (m s) of { STret new_s r ->
35 instance Monad (ST s) where
39 return x = ST $ \ s -> STret s x
40 m >> k = m >>= \ _ -> k
44 case (m s) of { STret new_s r ->
45 case (k r) of { ST k2 ->
50 fixST :: (a -> ST s a) -> ST s a
61 %*********************************************************
63 \subsection{Ghastly return types}
65 %*********************************************************
67 The @State@ type is the return type of a _ccall_ with no result. It
68 never actually exists, since it's always deconstructed straight away;
69 the desugarer ensures this.
72 data State s = S# (State# s)
73 data StateAndPtr# s elt = StateAndPtr# (State# s) elt
75 data StateAndChar# s = StateAndChar# (State# s) Char#
76 data StateAndInt# s = StateAndInt# (State# s) Int#
77 data StateAndWord# s = StateAndWord# (State# s) Word#
78 data StateAndFloat# s = StateAndFloat# (State# s) Float#
79 data StateAndDouble# s = StateAndDouble# (State# s) Double#
80 data StateAndAddr# s = StateAndAddr# (State# s) Addr#