From 2d205154d3fd565c7f88e07c4c307f48c5ade902 Mon Sep 17 00:00:00 2001
From: Sebastian Graf <sebastian.graf@kit.edu>
Date: Thu, 13 Jan 2022 15:58:52 +0100
Subject: [PATCH] Stricten the Strict State monad

I found it weird that most of the combinators weren't actually strict. Making
`pure` strict in the state should hopefully give Nested CPR an easier time to
unbox the nested state.
---
 compiler/GHC/Utils/Monad/State/Strict.hs | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/compiler/GHC/Utils/Monad/State/Strict.hs b/compiler/GHC/Utils/Monad/State/Strict.hs
index 27a275506760..d5298955a555 100644
--- a/compiler/GHC/Utils/Monad/State/Strict.hs
+++ b/compiler/GHC/Utils/Monad/State/Strict.hs
@@ -37,10 +37,10 @@ pattern State m <- State' m
     State m = State' (oneShot $ \s -> m s)
 
 instance Applicative (State s) where
-   pure x   = State $ \s -> (# x, s #)
+   pure x   = State $ \(!s) -> (# x, s #)
    m <*> n  = State $ \s -> case runState' m s of
                             (# f, !s' #) -> case runState' n s' of
-                                            (# x, s'' #) -> (# f x, s'' #)
+                                            (# x, !s'' #) -> (# f x, s'' #)
 
 instance Monad (State s) where
     m >>= n  = State $ \s -> case runState' m s of
@@ -48,19 +48,19 @@ instance Monad (State s) where
 
 state :: (s -> (a, s)) -> State s a
 state f = State $ \s -> case f s of
-                        (r, s') -> (# r, s' #)
+                        (r, !s') -> (# r, s' #)
 
 get :: State s s
-get = State $ \s -> (# s, s #)
+get = State $ \(!s) -> (# s, s #)
 
 gets :: (s -> a) -> State s a
-gets f = State $ \s -> (# f s, s #)
+gets f = State $ \(!s) -> (# f s, s #)
 
 put :: s -> State s ()
-put s' = State $ \_ -> (# (), s' #)
+put !s' = State $ \_ -> (# (), s' #)
 
 modify :: (s -> s) -> State s ()
-modify f = State $ \s -> (# (), f s #)
+modify f = State $ \s -> let !s' = f s in (# (), s' #)
 
 
 evalState :: State s a -> s -> a
@@ -75,4 +75,4 @@ execState s i = case runState' s i of
 
 runState :: State s a -> s -> (a, s)
 runState s i = case runState' s i of
-               (# a, s' #) -> (a, s')
+               (# a, !s' #) -> (a, s')
-- 
GitLab