adjunctions-4.4.3: Adjunctions and representable functors

Copyright | (c) Edward Kmett & Sjoerd Visscher 2011
License | BSD3
Maintainer | ekmett@gmail.com
Stability | experimental
Safe Haskell | Safe-Inferred
Language | Haskell2010

Control.Monad.Representable.State

Description

A generalized State monad, parameterized by a Representable functor. The representation of that functor serves as the state.

Synopsis

- type State (g :: Type -> Type) = StateT g Identity
- runState :: forall (g :: Type -> Type) a. Representable g => State g a -> Rep g -> (a, Rep g)
- evalState :: forall (g :: Type -> Type) a. Representable g => State g a -> Rep g -> a
- execState :: forall (g :: Type -> Type) a. Representable g => State g a -> Rep g -> Rep g
- mapState :: forall (g :: Type -> Type) a b. Functor g => ((a, Rep g) -> (b, Rep g)) -> State g a -> State g b
- newtype StateT (g :: Type -> Type) (m :: Type -> Type) a = StateT {
  - getStateT :: g (m (a, Rep g))
  }
- stateT :: forall (g :: Type -> Type) m a. Representable g => (Rep g -> m (a, Rep g)) -> StateT g m a
- runStateT :: forall (g :: Type -> Type) m a. Representable g => StateT g m a -> Rep g -> m (a, Rep g)
- evalStateT :: forall (g :: Type -> Type) m a. (Representable g, Monad m) => StateT g m a -> Rep g -> m a
- execStateT :: forall (g :: Type -> Type) m a. (Representable g, Monad m) => StateT g m a -> Rep g -> m (Rep g)
- mapStateT :: forall (g :: Type -> Type) m a n b. Functor g => (m (a, Rep g) -> n (b, Rep g)) -> StateT g m a -> StateT g n b
- liftCallCC :: forall (g :: Type -> Type) a m b. Representable g => ((((a, Rep g) -> m (b, Rep g)) -> m (a, Rep g)) -> m (a, Rep g)) -> ((a -> StateT g m b) -> StateT g m a) -> StateT g m a
- liftCallCC' :: forall (g :: Type -> Type) a m b. Representable g => ((((a, Rep g) -> m (b, Rep g)) -> m (a, Rep g)) -> m (a, Rep g)) -> ((a -> StateT g m b) -> StateT g m a) -> StateT g m a
- class Monad m => MonadState s (m :: Type -> Type) | m -> s where
  - get :: m s
  - put :: s -> m ()
  - state :: (s -> (a, s)) -> m a

# Documentation

type State (g :: Type -> Type) = StateT g Identity Source #

A memoized state monad parameterized by a representable functor `g`, where the representatation of `g`, `Rep g` is the state to carry.

The `return` function leaves the state unchanged, while `>>=` uses the final state of the first computation as the initial state of the second.

runState Source #

Arguments

:: forall (g :: Type -> Type) a. Representable g |
=> State g a | state-passing computation to execute
-> Rep g | initial state
-> (a, Rep g) | return value and final state

Unwrap a state monad computation as a function. (The inverse of `state`.)

evalState Source #

Arguments

:: forall (g :: Type -> Type) a. Representable g |
=> State g a | state-passing computation to execute
-> Rep g | initial value
-> a | return value of the state computation

Evaluate a state computation with the given initial state and return the final value, discarding the final state.

-     evalState m s = fst (runState m s)

execState Source #

Arguments

:: forall (g :: Type -> Type) a. Representable g |
=> State g a | state-passing computation to execute
-> Rep g | initial value
-> Rep g | final state

Evaluate a state computation with the given initial state and return the final state, discarding the final value.

-     execState m s = snd (runState m s)

mapState :: forall (g :: Type -> Type) a b. Functor g => ((a, Rep g) -> (b, Rep g)) -> State g a -> State g b Source #

Map both the return value and final state of a computation using the given function.

-     runState (mapState f m) = f . runState m

newtype StateT (g :: Type -> Type) (m :: Type -> Type) a Source #

A state transformer monad parameterized by:

- `g` - A representable functor used to memoize results for a state `Rep g`
- `m` - The inner monad.

The `return` function leaves the state unchanged, while `>>=` uses the final state of the first computation as the initial state of the second.

Constructors

StateT |

Fields

- getStateT :: g (m (a, Rep g))

#### Instances

Instances details

(Functor f, Representable g, MonadFree f m) => MonadFree f (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods wrap :: f (StateT g m a) -> StateT g m a Source #
(Representable g, MonadReader e m) => MonadReader e (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods ask :: StateT g m e Source # local :: (e -> e) -> StateT g m a -> StateT g m a Source # reader :: (e -> a) -> StateT g m a Source #
(Representable g, Monad m, Rep g ~ s) => MonadState s (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods get :: StateT g m s Source # put :: s -> StateT g m () Source # state :: (s -> (a, s)) -> StateT g m a Source #
(Representable g, MonadWriter w m) => MonadWriter w (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods writer :: (a, w) -> StateT g m a Source # tell :: w -> StateT g m () Source # listen :: StateT g m a -> StateT g m (a, w) Source # pass :: StateT g m (a, w -> w) -> StateT g m a Source #
Representable f => BindTrans (StateT f) Source # |
Instance details Defined in Control.Monad.Representable.State Methods liftB :: Bind b => b a -> StateT f b a Source #
Representable f => MonadTrans (StateT f) Source # |
Instance details Defined in Control.Monad.Representable.State Methods lift :: Monad m => m a -> StateT f m a Source #
(Representable g, Functor m, Monad m) => Applicative (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods pure :: a -> StateT g m a Source # (<*>) :: StateT g m (a -> b) -> StateT g m a -> StateT g m b Source # liftA2 :: (a -> b -> c) -> StateT g m a -> StateT g m b -> StateT g m c Source # (*>) :: StateT g m a -> StateT g m b -> StateT g m b Source # (<*) :: StateT g m a -> StateT g m b -> StateT g m a Source #
(Functor g, Functor m) => Functor (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods fmap :: (a -> b) -> StateT g m a -> StateT g m b Source # (<$) :: a -> StateT g m b -> StateT g m a Source #
(Representable g, Monad m) => Monad (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods (>>=) :: StateT g m a -> (a -> StateT g m b) -> StateT g m b Source # (>>) :: StateT g m a -> StateT g m b -> StateT g m b Source # return :: a -> StateT g m a Source #
(Representable g, MonadCont m) => MonadCont (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods callCC :: ((a -> StateT g m b) -> StateT g m a) -> StateT g m a Source #
(Representable g, Bind m) => Apply (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods (<.>) :: StateT g m (a -> b) -> StateT g m a -> StateT g m b Source # (.>) :: StateT g m a -> StateT g m b -> StateT g m b Source # (<.) :: StateT g m a -> StateT g m b -> StateT g m a Source # liftF2 :: (a -> b -> c) -> StateT g m a -> StateT g m b -> StateT g m c Source #
(Representable g, Bind m) => Bind (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods (>>-) :: StateT g m a -> (a -> StateT g m b) -> StateT g m b Source # join :: StateT g m (StateT g m a) -> StateT g m a Source #

stateT :: forall (g :: Type -> Type) m a. Representable g => (Rep g -> m (a, Rep g)) -> StateT g m a Source #

runStateT :: forall (g :: Type -> Type) m a. Representable g => StateT g m a -> Rep g -> m (a, Rep g) Source #

evalStateT :: forall (g :: Type -> Type) m a. (Representable g, Monad m) => StateT g m a -> Rep g -> m a Source #

Evaluate a state computation with the given initial state and return the final value, discarding the final state.

-     evalStateT m s = liftM fst (runStateT m s)

execStateT :: forall (g :: Type -> Type) m a. (Representable g, Monad m) => StateT g m a -> Rep g -> m (Rep g) Source #

Evaluate a state computation with the given initial state and return the final state, discarding the final value.

-     execStateT m s = liftM snd (runStateT m s)

mapStateT :: forall (g :: Type -> Type) m a n b. Functor g => (m (a, Rep g) -> n (b, Rep g)) -> StateT g m a -> StateT g n b Source #

liftCallCC :: forall (g :: Type -> Type) a m b. Representable g => ((((a, Rep g) -> m (b, Rep g)) -> m (a, Rep g)) -> m (a, Rep g)) -> ((a -> StateT g m b) -> StateT g m a) -> StateT g m a Source #

Uniform lifting of a `callCC` operation to the new monad. This version rolls back to the original state on entering the continuation.

liftCallCC' :: forall (g :: Type -> Type) a m b. Representable g => ((((a, Rep g) -> m (b, Rep g)) -> m (a, Rep g)) -> m (a, Rep g)) -> ((a -> StateT g m b) -> StateT g m a) -> StateT g m a Source #

In-situ lifting of a `callCC` operation to the new monad. This version uses the current state on entering the continuation. It does not satisfy the laws of a monad transformer.

class Monad m => MonadState s (m :: Type -> Type) | m -> s where Source #

Minimal definition is either both of `get` and `put` or just `state`

Minimal complete definition

state | get, put

Methods

get :: m s Source #

Return the state from the internals of the monad.

put :: s -> m () Source #

Replace the state inside the monad.

state :: (s -> (a, s)) -> m a Source #

Embed a simple state action into the monad.

#### Instances

Instances details

MonadState s m => MonadState s (Free m) |
Instance details Defined in Control.Monad.Free Methods get :: Free m s Source # put :: s -> Free m () Source # state :: (s -> (a, s)) -> Free m a Source #
MonadState s m => MonadState s (Free m) |
Instance details Defined in Control.Monad.Free.Ap Methods get :: Free m s Source # put :: s -> Free m () Source # state :: (s -> (a, s)) -> Free m a Source #
MonadState s m => MonadState s (F m) |
Instance details Defined in Control.Monad.Free.Church Methods get :: F m s Source # put :: s -> F m () Source # state :: (s -> (a, s)) -> F m a Source #
MonadState s m => MonadState s (IterT m) |
Instance details Defined in Control.Monad.Trans.Iter Methods get :: IterT m s Source # put :: s -> IterT m () Source # state :: (s -> (a, s)) -> IterT m a Source #
MonadState s m => MonadState s (MaybeT m) |
Instance details Defined in Control.Monad.State.Class Methods get :: MaybeT m s Source # put :: s -> MaybeT m () Source # state :: (s -> (a, s)) -> MaybeT m a Source #
(Representable g, Monad m, Rep g ~ s) => MonadState s (StateT g m) Source # |
Instance details Defined in Control.Monad.Representable.State Methods get :: StateT g m s Source # put :: s -> StateT g m () Source # state :: (s -> (a, s)) -> StateT g m a Source #
(Functor f, MonadState s m) => MonadState s (FreeT f m) |
Instance details Defined in Control.Monad.Trans.Free Methods get :: FreeT f m s Source # put :: s -> FreeT f m () Source # state :: (s -> (a, s)) -> FreeT f m a Source #
(Applicative f, MonadState s m) => MonadState s (FreeT f m) |
Instance details Defined in Control.Monad.Trans.Free.Ap Methods get :: FreeT f m s Source # put :: s -> FreeT f m () Source # state :: (s -> (a, s)) -> FreeT f m a Source #
MonadState s m => MonadState s (FT f m) |
Instance details Defined in Control.Monad.Trans.Free.Church Methods get :: FT f m s Source # put :: s -> FT f m () Source # state :: (s -> (a, s)) -> FT f m a Source #
(Monoid w, MonadState s m) => MonadState s (AccumT w m) | Since: mtl-2.3
Instance details Defined in Control.Monad.State.Class Methods get :: AccumT w m s Source # put :: s -> AccumT w m () Source # state :: (s -> (a, s)) -> AccumT w m a Source #
MonadState s m => MonadState s (ExceptT e m) | Since: mtl-2.2
Instance details Defined in Control.Monad.State.Class Methods get :: ExceptT e m s Source # put :: s -> ExceptT e m () Source # state :: (s -> (a, s)) -> ExceptT e m a Source #
MonadState s m => MonadState s (IdentityT m) |
Instance details Defined in Control.Monad.State.Class Methods get :: IdentityT m s Source # put :: s -> IdentityT m () Source # state :: (s -> (a, s)) -> IdentityT m a Source #
MonadState s m => MonadState s (ReaderT r m) |
Instance details Defined in Control.Monad.State.Class Methods get :: ReaderT r m s Source # put :: s -> ReaderT r m () Source # state :: (s -> (a, s)) -> ReaderT r m a Source #
MonadState s m => MonadState s (SelectT r m) | Since: mtl-2.3
Instance details Defined in Control.Monad.State.Class Methods get :: SelectT r m s Source # put :: s -> SelectT r m () Source # state :: (s -> (a, s)) -> SelectT r m a Source #
Monad m => MonadState s (StateT s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: StateT s m s Source # put :: s -> StateT s m () Source # state :: (s -> (a, s)) -> StateT s m a Source #
Monad m => MonadState s (StateT s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: StateT s m s Source # put :: s -> StateT s m () Source # state :: (s -> (a, s)) -> StateT s m a Source #
(Monoid w, MonadState s m) => MonadState s (WriterT w m) | Since: mtl-2.3
Instance details Defined in Control.Monad.State.Class Methods get :: WriterT w m s Source # put :: s -> WriterT w m () Source # state :: (s -> (a, s)) -> WriterT w m a Source #
(Monoid w, MonadState s m) => MonadState s (WriterT w m) |
Instance details Defined in Control.Monad.State.Class Methods get :: WriterT w m s Source # put :: s -> WriterT w m () Source # state :: (s -> (a, s)) -> WriterT w m a Source #
(Monoid w, MonadState s m) => MonadState s (WriterT w m) |
Instance details Defined in Control.Monad.State.Class Methods get :: WriterT w m s Source # put :: s -> WriterT w m () Source # state :: (s -> (a, s)) -> WriterT w m a Source #
MonadState s m => MonadState s (ContT r m) |
Instance details Defined in Control.Monad.State.Class Methods get :: ContT r m s Source # put :: s -> ContT r m () Source # state :: (s -> (a, s)) -> ContT r m a Source #
(Monad m, Monoid w) => MonadState s (RWST r w s m) | Since: mtl-2.3
Instance details Defined in Control.Monad.State.Class Methods get :: RWST r w s m s Source # put :: s -> RWST r w s m () Source # state :: (s -> (a, s)) -> RWST r w s m a Source #
(Monad m, Monoid w) => MonadState s (RWST r w s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: RWST r w s m s Source # put :: s -> RWST r w s m () Source # state :: (s -> (a, s)) -> RWST r w s m a Source #
(Monad m, Monoid w) => MonadState s (RWST r w s m) |
Instance details Defined in Control.Monad.State.Class Methods get :: RWST r w s m s Source # put :: s -> RWST r w s m () Source # state :: (s -> (a, s)) -> RWST r w s m a Source #