{-# LANGUAGE CPP #-}
module Control.Applicative.PhantomState (
PhantomStateT
, PhantomState
, useState
, changeState
, useAndChangeState
, runPhantomStateT
, runPhantomState
) where
import Control.Applicative
import Data.Functor.Identity
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid (Monoid (..))
#endif
#if MIN_VERSION_base(4,9,0) && !MIN_VERSION_base(4,10,0)
import Data.Semigroup (Semigroup (..))
#endif
newtype PhantomStateT s m a = PhantomStateT (s -> m s)
type PhantomState s = PhantomStateT s Identity
useState :: Applicative m => (s -> m a) -> PhantomStateT s m ()
{-# INLINE useState #-}
useState :: forall (m :: * -> *) s a.
Applicative m =>
(s -> m a) -> PhantomStateT s m ()
useState s -> m a
f = forall s (m :: * -> *). (s -> m s) -> PhantomStateT s m ()
useAndChangeState forall a b. (a -> b) -> a -> b
$ \s
s -> s -> m a
f s
s forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Applicative f => a -> f a
pure s
s
changeState :: Applicative m => (s -> s) -> PhantomStateT s m ()
{-# INLINE changeState #-}
changeState :: forall (m :: * -> *) s.
Applicative m =>
(s -> s) -> PhantomStateT s m ()
changeState s -> s
f = forall s (m :: * -> *). (s -> m s) -> PhantomStateT s m ()
useAndChangeState forall a b. (a -> b) -> a -> b
$ \s
s -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (s -> s
f s
s)
useAndChangeState :: (s -> m s) -> PhantomStateT s m ()
{-# INLINE useAndChangeState #-}
useAndChangeState :: forall s (m :: * -> *). (s -> m s) -> PhantomStateT s m ()
useAndChangeState = forall s (m :: * -> *) a. (s -> m s) -> PhantomStateT s m a
PhantomStateT
runPhantomStateT :: PhantomStateT s m a
-> s
-> m s
{-# INLINE runPhantomStateT #-}
runPhantomStateT :: forall s (m :: * -> *) a. PhantomStateT s m a -> s -> m s
runPhantomStateT (PhantomStateT s -> m s
f) s
x = s -> m s
f s
x
runPhantomState :: PhantomState s a
-> s
-> s
{-# INLINE runPhantomState #-}
runPhantomState :: forall s a. PhantomState s a -> s -> s
runPhantomState PhantomState s a
f = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. PhantomStateT s m a -> s -> m s
runPhantomStateT PhantomState s a
f
instance Functor (PhantomStateT s m) where
{-# INLINE fmap #-}
fmap :: forall a b. (a -> b) -> PhantomStateT s m a -> PhantomStateT s m b
fmap a -> b
_ (PhantomStateT s -> m s
f) = forall s (m :: * -> *) a. (s -> m s) -> PhantomStateT s m a
PhantomStateT s -> m s
f
instance Monad m => Applicative (PhantomStateT s m) where
{-# INLINE pure #-}
pure :: forall a. a -> PhantomStateT s m a
pure a
_ = forall s (m :: * -> *) a. (s -> m s) -> PhantomStateT s m a
PhantomStateT forall (m :: * -> *) a. Monad m => a -> m a
return
{-# INLINE (<*>) #-}
PhantomStateT s -> m s
f <*> :: forall a b.
PhantomStateT s m (a -> b)
-> PhantomStateT s m a -> PhantomStateT s m b
<*> PhantomStateT s -> m s
g = forall s (m :: * -> *) a. (s -> m s) -> PhantomStateT s m a
PhantomStateT (\s
x -> s -> m s
f s
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m s
g)
{-# INLINE (*>) #-}
PhantomStateT s -> m s
f *> :: forall a b.
PhantomStateT s m a -> PhantomStateT s m b -> PhantomStateT s m b
*> PhantomStateT s -> m s
g = forall s (m :: * -> *) a. (s -> m s) -> PhantomStateT s m a
PhantomStateT (\s
x -> s -> m s
f s
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m s
g)
{-# INLINE (<*) #-}
PhantomStateT s -> m s
f <* :: forall a b.
PhantomStateT s m a -> PhantomStateT s m b -> PhantomStateT s m a
<* PhantomStateT s -> m s
g = forall s (m :: * -> *) a. (s -> m s) -> PhantomStateT s m a
PhantomStateT (\s
x -> s -> m s
f s
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m s
g)
instance (Monad m, Alternative m) => Alternative (PhantomStateT s m) where
{-# INLINE empty #-}
empty :: forall a. PhantomStateT s m a
empty = forall s (m :: * -> *) a. (s -> m s) -> PhantomStateT s m a
PhantomStateT (forall a b. a -> b -> a
const forall (f :: * -> *) a. Alternative f => f a
empty)
{-# INLINE (<|>) #-}
PhantomStateT s -> m s
f <|> :: forall a.
PhantomStateT s m a -> PhantomStateT s m a -> PhantomStateT s m a
<|> PhantomStateT s -> m s
g = forall s (m :: * -> *) a. (s -> m s) -> PhantomStateT s m a
PhantomStateT (\s
x -> s -> m s
f s
x forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> s -> m s
g s
x)
instance Monad m => Monoid (PhantomStateT s m a) where
{-# INLINE mempty #-}
mempty :: PhantomStateT s m a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. HasCallStack => a
undefined
#if MIN_VERSION_base(4,9,0)
instance Monad m => Semigroup (PhantomStateT s m a) where
{-# INLINE (<>) #-}
<> :: PhantomStateT s m a -> PhantomStateT s m a -> PhantomStateT s m a
(<>) = forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
#endif