module Control.Monad.State.Extended
    ( module Control.Monad.State.Strict
    , liftState
    , whenS
    , unlessS
    ) where

import Control.Monad.State.Strict

liftState :: MonadState s m => State s a -> m a
liftState :: State s a -> m a
liftState = (s -> (a, s)) -> m a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state ((s -> (a, s)) -> m a)
-> (State s a -> s -> (a, s)) -> State s a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State s a -> s -> (a, s)
forall s a. State s a -> s -> (a, s)
runState

whenS :: MonadState s m => (s -> Bool) -> m () -> m ()
whenS :: (s -> Bool) -> m () -> m ()
whenS s -> Bool
predicate m ()
action = do
    Bool
condition <- (s -> Bool) -> m s -> m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> Bool
predicate m s
forall s (m :: * -> *). MonadState s m => m s
get
    Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
condition m ()
action

unlessS :: MonadState s m => (s -> Bool) -> m () -> m ()
unlessS :: (s -> Bool) -> m () -> m ()
unlessS s -> Bool
predicate = (s -> Bool) -> m () -> m ()
forall s (m :: * -> *).
MonadState s m =>
(s -> Bool) -> m () -> m ()
whenS (Bool -> Bool
not (Bool -> Bool) -> (s -> Bool) -> s -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Bool
predicate)