diff options
author | Andreas Klebinger <klebinger.andreas@gmx.at> | 2021-03-30 18:02:38 +0200 |
---|---|---|
committer | Andreas Klebinger <klebinger.andreas@gmx.at> | 2021-04-05 08:56:14 -0400 |
commit | 6bc44526ec0c5ddd52016233253f2bc06e96f4d7 (patch) | |
tree | 0d373c435c6c6607a417cd629a370621b578571f | |
parent | ce706faeef3964116c6e1dd0e6ae2f2e77fde57d (diff) | |
download | haskell-wip/andreask/oneshot_state.tar.gz |
One-Shotify GHC.Utils.Monad.State (#18202)wip/andreask/oneshot_state
-rw-r--r-- | compiler/GHC/Utils/Monad/State.hs | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/compiler/GHC/Utils/Monad/State.hs b/compiler/GHC/Utils/Monad/State.hs index c7b9e3f591..997137525b 100644 --- a/compiler/GHC/Utils/Monad/State.hs +++ b/compiler/GHC/Utils/Monad/State.hs @@ -1,13 +1,27 @@ {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE UnboxedTuples #-} +{-# LANGUAGE PatternSynonyms #-} module GHC.Utils.Monad.State where import GHC.Prelude -newtype State s a = State { runState' :: s -> (# a, s #) } +import GHC.Exts (oneShot) + +newtype State s a = State' { runState' :: s -> (# a, s #) } deriving (Functor) +pattern State :: (s -> (# a, s #)) + -> State s a + +-- This pattern synonym makes the monad eta-expand, +-- which as a very beneficial effect on compiler performance +-- See #18202. +-- See Note [The one-shot state monad trick] in GHC.Utils.Monad +pattern State m <- State' m + where + State m = State' (oneShot $ \s -> m s) + instance Applicative (State s) where pure x = State $ \s -> (# x, s #) m <*> n = State $ \s -> case runState' m s of |