on fbc5b30 help sign in
46 lines 1.0 kB history raw
1{-# LANGUAGE UndecidableInstances #-}
2module Effectful.Class.State where
4import Control.Monad.Trans.Class
6import Effectful.Internal.Effect
7import Effectful.Internal.Monad
8import qualified Effectful.State as S
10-- | Compatiblity layer for a transition period from MTL-style effect handling
11-- to 'Effectful.Eff'.
12class Monad m => MonadState s m where
13 {-# MINIMAL state | get, put #-}
15 get :: m s
16 get = state (\s -> (s, s))
18 put :: s -> m ()
19 put s = state (\_ -> ((), s))
21 state :: (s -> (a, s)) -> m a
22 state f = do
23 (a, s) <- f <$> get
24 s `seq` put s
25 pure a
27-- | Generic, overlappable instance.
28instance {-# OVERLAPPABLE #-}
29 ( MonadState s m
30 , MonadTrans t
31 , Monad (t m)
32 ) => MonadState s (t m) where
33 get = lift get
34 put = lift . put
35 state = lift . state
37instance S.State s :> es => MonadState s (Eff es) where
38 get = S.get
39 put = S.put
40 state = S.state
42gets :: MonadState s m => (s -> a) -> m a
43gets f = f <$> get
45modify :: MonadState s m => (s -> s) -> m ()
46modify f = state (\s -> ((), f s))