Changeset View
Changeset View
Standalone View
Standalone View
libraries/base/GHC/ST.hs
Show All 12 Lines | |||||
13 | -- Portability : non-portable (GHC Extensions) | 13 | -- Portability : non-portable (GHC Extensions) | ||
14 | -- | 14 | -- | ||
15 | -- The 'ST' Monad. | 15 | -- The 'ST' Monad. | ||
16 | -- | 16 | -- | ||
17 | ----------------------------------------------------------------------------- | 17 | ----------------------------------------------------------------------------- | ||
18 | 18 | | |||
19 | module GHC.ST ( | 19 | module GHC.ST ( | ||
20 | ST(..), STret(..), STRep, | 20 | ST(..), STret(..), STRep, | ||
21 | fixST, runST, | 21 | runST, | ||
22 | 22 | | |||
23 | -- * Unsafe functions | 23 | -- * Unsafe functions | ||
24 | liftST, unsafeInterleaveST, unsafeDupableInterleaveST | 24 | liftST, unsafeInterleaveST, unsafeDupableInterleaveST | ||
25 | ) where | 25 | ) where | ||
26 | 26 | | |||
27 | import GHC.Base | 27 | import GHC.Base | ||
28 | import GHC.Show | 28 | import GHC.Show | ||
29 | import qualified Control.Monad.Fail as Fail | 29 | import qualified Control.Monad.Fail as Fail | ||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Line(s) | 86 | instance Semigroup a => Semigroup (ST s a) where | |||
87 | (<>) = liftA2 (<>) | 87 | (<>) = liftA2 (<>) | ||
88 | 88 | | |||
89 | -- | @since 4.11.0.0 | 89 | -- | @since 4.11.0.0 | ||
90 | instance Monoid a => Monoid (ST s a) where | 90 | instance Monoid a => Monoid (ST s a) where | ||
91 | mempty = pure mempty | 91 | mempty = pure mempty | ||
92 | 92 | | |||
93 | data STret s a = STret (State# s) a | 93 | data STret s a = STret (State# s) a | ||
94 | 94 | | |||
95 | -- liftST is useful when we want a lifted result from an ST computation. See | 95 | -- liftST is useful when we want a lifted result from an ST computation. | ||
96 | -- fixST below. | | |||
97 | liftST :: ST s a -> State# s -> STret s a | 96 | liftST :: ST s a -> State# s -> STret s a | ||
98 | liftST (ST m) = \s -> case m s of (# s', r #) -> STret s' r | 97 | liftST (ST m) = \s -> case m s of (# s', r #) -> STret s' r | ||
99 | 98 | | |||
100 | noDuplicateST :: ST s () | 99 | noDuplicateST :: ST s () | ||
101 | noDuplicateST = ST $ \s -> (# noDuplicate# s, () #) | 100 | noDuplicateST = ST $ \s -> (# noDuplicate# s, () #) | ||
102 | 101 | | |||
103 | -- | 'unsafeInterleaveST' allows an 'ST' computation to be deferred | 102 | -- | 'unsafeInterleaveST' allows an 'ST' computation to be deferred | ||
104 | -- lazily. When passed a value of type @ST a@, the 'ST' computation will | 103 | -- lazily. When passed a value of type @ST a@, the 'ST' computation will | ||
Show All 16 Lines | |||||
121 | unsafeDupableInterleaveST :: ST s a -> ST s a | 120 | unsafeDupableInterleaveST :: ST s a -> ST s a | ||
122 | unsafeDupableInterleaveST (ST m) = ST ( \ s -> | 121 | unsafeDupableInterleaveST (ST m) = ST ( \ s -> | ||
123 | let | 122 | let | ||
124 | r = case m s of (# _, res #) -> res | 123 | r = case m s of (# _, res #) -> res | ||
125 | in | 124 | in | ||
126 | (# s, r #) | 125 | (# s, r #) | ||
127 | ) | 126 | ) | ||
128 | 127 | | |||
129 | -- | Allow the result of a state transformer computation to be used (lazily) | | |||
130 | -- inside the computation. | | |||
131 | -- Note that if @f@ is strict, @'fixST' f = _|_@. | | |||
132 | fixST :: (a -> ST s a) -> ST s a | | |||
133 | fixST k = ST $ \ s -> | | |||
134 | let ans = liftST (k r) s | | |||
135 | STret _ r = ans | | |||
136 | in | | |||
137 | case ans of STret s' x -> (# s', x #) | | |||
138 | | ||||
139 | -- | @since 2.01 | 128 | -- | @since 2.01 | ||
140 | instance Show (ST s a) where | 129 | instance Show (ST s a) where | ||
141 | showsPrec _ _ = showString "<<ST action>>" | 130 | showsPrec _ _ = showString "<<ST action>>" | ||
142 | showList = showList__ (showsPrec 0) | 131 | showList = showList__ (showsPrec 0) | ||
143 | 132 | | |||
144 | {-# INLINE runST #-} | 133 | {-# INLINE runST #-} | ||
145 | -- | Return the value computed by a state transformer computation. | 134 | -- | Return the value computed by a state transformer computation. | ||
146 | -- The @forall@ ensures that the internal state used by the 'ST' | 135 | -- The @forall@ ensures that the internal state used by the 'ST' | ||
147 | -- computation is inaccessible to the rest of the program. | 136 | -- computation is inaccessible to the rest of the program. | ||
148 | runST :: (forall s. ST s a) -> a | 137 | runST :: (forall s. ST s a) -> a | ||
149 | runST (ST st_rep) = case runRW# st_rep of (# _, a #) -> a | 138 | runST (ST st_rep) = case runRW# st_rep of (# _, a #) -> a | ||
150 | -- See Note [Definition of runRW#] in GHC.Magic | 139 | -- See Note [Definition of runRW#] in GHC.Magic |