summaryrefslogtreecommitdiff
path: root/compiler/GHC/Utils
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2021-04-20 16:15:44 -0400
committerBen Gamari <ben@smart-cactus.org>2021-05-29 11:58:45 -0400
commitec64624768735df24f1b6fe24a2b2e59172cc613 (patch)
treead2a86f46427a701d3d604c01cd763ed52b205cd /compiler/GHC/Utils
parent42c611cffb2387627f80e790f1d175ebad7d9992 (diff)
downloadhaskell-ec64624768735df24f1b6fe24a2b2e59172cc613.tar.gz
Use GHC's State monad consistently
GHC's internal State monad benefits from oneShot annotations on its state, allowing for more aggressive eta expansion. We currently don't have monad transformers with the same optimisation, so we only change uses of the pure State monad here. See #19657 and 19380. Metric Decrease: hie002
Diffstat (limited to 'compiler/GHC/Utils')
-rw-r--r--compiler/GHC/Utils/Monad/State/Lazy.hs7
-rw-r--r--compiler/GHC/Utils/Monad/State/Strict.hs7
2 files changed, 12 insertions, 2 deletions
diff --git a/compiler/GHC/Utils/Monad/State/Lazy.hs b/compiler/GHC/Utils/Monad/State/Lazy.hs
index 9d42fcb2ad..4f9cb034df 100644
--- a/compiler/GHC/Utils/Monad/State/Lazy.hs
+++ b/compiler/GHC/Utils/Monad/State/Lazy.hs
@@ -5,7 +5,8 @@
-- | A lazy state monad.
module GHC.Utils.Monad.State.Lazy
( -- * The State monda
- State(pattern State)
+ State(State)
+ , state
, evalState
, execState
, runState
@@ -45,6 +46,10 @@ instance Monad (State s) where
m >>= n = State $ \s -> case runState' m s of
(# r, s' #) -> runState' (n r) s'
+state :: (s -> (a, s)) -> State s a
+state f = State $ \s -> case f s of
+ (r, s') -> (# r, s' #)
+
get :: State s s
get = State $ \s -> (# s, s #)
diff --git a/compiler/GHC/Utils/Monad/State/Strict.hs b/compiler/GHC/Utils/Monad/State/Strict.hs
index 39cd6a0773..27a2755067 100644
--- a/compiler/GHC/Utils/Monad/State/Strict.hs
+++ b/compiler/GHC/Utils/Monad/State/Strict.hs
@@ -5,7 +5,8 @@
-- | A state monad which is strict in its state.
module GHC.Utils.Monad.State.Strict
( -- * The State monad
- State(pattern State)
+ State(State)
+ , state
, evalState
, execState
, runState
@@ -45,6 +46,10 @@ instance Monad (State s) where
m >>= n = State $ \s -> case runState' m s of
(# r, !s' #) -> runState' (n r) s'
+state :: (s -> (a, s)) -> State s a
+state f = State $ \s -> case f s of
+ (r, s') -> (# r, s' #)
+
get :: State s s
get = State $ \s -> (# s, s #)