Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
The control functor hierarchy
The functors in this module are called control functors, which
are different from the data functors in Data.Functor.Linear
.
This distinction and the use-cases of each group of functors is explained in this blog post.
Synopsis
- class Functor f => Functor f where
- fmap :: (a %1 -> b) %1 -> f a %1 -> f b
- (<$>) :: Functor f => (a %1 -> b) %1 -> f a %1 -> f b
- (<&>) :: Functor f => f a %1 -> (a %1 -> b) %1 -> f b
- (<$) :: (Functor f, Consumable b) => a %1 -> f b %1 -> f a
- void :: (Functor f, Consumable a) => f a %1 -> f ()
- dataFmapDefault :: Functor f => (a %1 -> b) -> f a %1 -> f b
- class (Applicative f, Functor f) => Applicative f where
- dataPureDefault :: Applicative f => a -> f a
- class Applicative m => Monad m where
- return :: Monad m => a %1 -> m a
- join :: Monad m => m (m a) %1 -> m a
- ap :: Monad m => m (a %1 -> b) %1 -> m a %1 -> m b
- foldM :: forall m a b. Monad m => (b %1 -> a %1 -> m b) -> b %1 -> [a] %1 -> m b
- class Monad m => MonadFail m where
- newtype Data f a = Data (f a)
- type Reader r = ReaderT r Identity
- reader :: Monad m => (r %1 -> a) %1 -> ReaderT r m a
- runReader :: Reader r a %1 -> r %1 -> a
- mapReader :: (a %1 -> b) %1 -> Reader r a %1 -> Reader r b
- withReader :: (r' %1 -> r) %1 -> Reader r a %1 -> Reader r' a
- newtype ReaderT r m a = ReaderT (r %1 -> m a)
- runReaderT :: ReaderT r m a %1 -> r %1 -> m a
- mapReaderT :: (m a %1 -> n b) %1 -> ReaderT r m a %1 -> ReaderT r n b
- withReaderT :: (r' %1 -> r) %1 -> ReaderT r m a %1 -> ReaderT r' m a
- ask :: Applicative m => ReaderT r m r
- local :: (r %1 -> r) %1 -> ReaderT r m a %1 -> ReaderT r m a
- asks :: Monad m => (r %1 -> a) %1 -> ReaderT r m a
- type State s = StateT s Identity
- state :: Applicative m => (s %1 -> (a, s)) %1 -> StateT s m a
- runState :: State s a %1 -> s %1 -> (a, s)
- evalState :: Consumable s => State s a %1 -> s %1 -> a
- execState :: State s () %1 -> s %1 -> s
- mapState :: ((a, s) %1 -> (b, s)) %1 -> State s a %1 -> State s b
- withState :: (s %1 -> s) %1 -> State s a %1 -> State s a
- newtype StateT s m a = StateT (s %1 -> m (a, s))
- runStateT :: StateT s m a %1 -> s %1 -> m (a, s)
- evalStateT :: (Functor m, Consumable s) => StateT s m a %1 -> s %1 -> m a
- execStateT :: Functor m => StateT s m () %1 -> s %1 -> m s
- mapStateT :: (m (a, s) %1 -> n (b, s)) %1 -> StateT s m a %1 -> StateT s n b
- withStateT :: (s %1 -> s) %1 -> StateT s m a %1 -> StateT s m a
- get :: (Applicative m, Dupable s) => StateT s m s
- put :: (Applicative m, Consumable s) => s %1 -> StateT s m ()
- modify :: Applicative m => (s %1 -> s) %1 -> StateT s m ()
- gets :: (Applicative m, Dupable s) => (s %1 -> a) %1 -> StateT s m a
- class (forall m. Monad m => Monad (t m)) => MonadTrans t where
- newtype Data f a = Data (f a)
Control functor hierarchy
class Functor f => Functor f where Source #
Control linear functors. The functor of type
f a
holds only one value of type a
and represents a computation
producing an a
with an effect. All control functors are data functors,
but not all data functors are control functors.
fmap :: (a %1 -> b) %1 -> f a %1 -> f b Source #
Map a linear function g
over a control functor f a
.
Note that g
is used linearly over the single a
in f a
.
Instances
(<$) :: (Functor f, Consumable b) => a %1 -> f b %1 -> f a infixl 4 Source #
Linearly typed replacement for the standard (<$)
function.
void :: (Functor f, Consumable a) => f a %1 -> f () Source #
Discard a consumable value stored in a control functor.
dataFmapDefault :: Functor f => (a %1 -> b) -> f a %1 -> f b Source #
Apply the control fmap
over a data functor.
class (Applicative f, Functor f) => Applicative f where Source #
Control linear applicative functors. These represent effectful
computations that could produce continuations that can be applied with
<*>
.
Inject (and consume) a value into an applicative control functor.
(<*>) :: f (a %1 -> b) %1 -> f a %1 -> f b infixl 4 Source #
Apply the linear function in a control applicative functor to the value
of type a
in another functor. This is essentialy composing two effectful
computations, one that produces a function f :: a %1-> b
and one that
produces a value of type a
into a single effectful computation that
produces a value of type b
.
liftA2 :: (a %1 -> b %1 -> c) %1 -> f a %1 -> f b %1 -> f c Source #
liftA2 g
consumes g
linearly as it lifts it
over two functors: liftA2 g :: f a %1-> f b %1-> f c
.
Instances
Applicative Identity Source # | |
Applicative IO Source # | |
Applicative RIO Source # | |
Monoid a => Applicative ((,) a) Source # | |
(Applicative m, Dupable r) => Applicative (ReaderT r m) Source # | |
Monad m => Applicative (StateT s m) Source # | |
(Functor m, Functor f) => Applicative (Stream f m) Source # | |
Applicative m => Applicative (ReaderT r m) Source # | |
(Monoid a, Monoid b) => Applicative ((,,) a b) Source # | |
(Monoid a, Monoid b, Monoid c) => Applicative ((,,,) a b c) Source # | |
dataPureDefault :: Applicative f => a -> f a Source #
Apply the control pure
over a data applicative.
class Applicative m => Monad m where Source #
Control linear monads.
A linear monad is one in which you sequence linear functions in a context,
i.e., you sequence functions of the form a %1-> m b
.
(>>=) :: m a %1 -> (a %1 -> m b) %1 -> m b infixl 1 Source #
x >>= g
applies a linear function g
linearly (i.e., using it
exactly once) on the value of type a
inside the value of type m a
join :: Monad m => m (m a) %1 -> m a Source #
Given an effect-producing computation that produces an effect-producing computation
that produces an a
, simplify it to an effect-producing
computation that produces an a
.
ap :: Monad m => m (a %1 -> b) %1 -> m a %1 -> m b Source #
Use this operator to define Applicative instances in terms of Monad instances.
foldM :: forall m a b. Monad m => (b %1 -> a %1 -> m b) -> b %1 -> [a] %1 -> m b Source #
Fold from left to right with a linear monad.
This is a linear version of foldM
.
class Monad m => MonadFail m where Source #
This class handles pattern-matching failure in do-notation. See Control.Monad.Fail for details.
This is a newtype for deriving Data.XXX classes from Control.XXX classes.
Data (f a) |
Instances
Applicative f => Applicative (Data f) Source # | |
Functor f => Functor (Data f) Source # | |
Monad transformers
ReaderT monad transformer
See here to learn about the basics of reader monads. To know about the standard reader monad functions, see the documentation of the standard reader monad here.
withReader :: (r' %1 -> r) %1 -> Reader r a %1 -> Reader r' a Source #
newtype ReaderT r m a Source #
A linear reader monad transformer. This reader monad requires that use of the read-only state is explict.
The monad instance requires that r
be Dupable
. This means that you
should use the linear reader monad just like the non-linear monad, except
that the type system ensures that you explicity use or discard the
read-only state (with the Consumable
instance).
ReaderT (r %1 -> m a) |
Instances
Dupable r => MonadTrans (ReaderT r) Source # | |
(Applicative m, Dupable r) => Applicative (ReaderT r m) Source # | |
Functor m => Functor (ReaderT r m) Source # | |
(Monad m, Dupable r) => Monad (ReaderT r m) Source # | |
(Applicative m, Dupable r) => Applicative (ReaderT r m) Source # | |
Functor m => Functor (ReaderT r m) Source # | |
runReaderT :: ReaderT r m a %1 -> r %1 -> m a Source #
Provide an intial read-only state and run the monadic computation in a reader monad transformer
mapReaderT :: (m a %1 -> n b) %1 -> ReaderT r m a %1 -> ReaderT r n b Source #
withReaderT :: (r' %1 -> r) %1 -> ReaderT r m a %1 -> ReaderT r' m a Source #
ask :: Applicative m => ReaderT r m r Source #
StateT monad
This is a linear version of the standard state monad.
The linear arrows ensure that the state is threaded linearly through
functions of the form a %1-> StateT s m a
. That is, when sequencing
f :: a %1-> StateT s m b
and g :: b %1-> StateT s m c
,
the type system enforces that state produced by $f$ is fed into g
.
For this reason, there is only one way to define (>>=)
:
instance Monad m => Applicative (StateT s m) where StateT mx >>= f = StateT $ \s -> do (x, s') <- mx s runStateT (f x) s'
To see examples and learn about all the standard state monad functions, see here. To learn the basics of the state monad, see here.
state :: Applicative m => (s %1 -> (a, s)) %1 -> StateT s m a Source #
evalState :: Consumable s => State s a %1 -> s %1 -> a Source #
Use with care! This consumes the final state, so might be costly at runtime.
A (strict) linear state monad transformer.
StateT (s %1 -> m (a, s)) |
evalStateT :: (Functor m, Consumable s) => StateT s m a %1 -> s %1 -> m a Source #
Use with care! This consumes the final state, so might be costly at runtime.
execStateT :: Functor m => StateT s m () %1 -> s %1 -> m s Source #
withStateT :: (s %1 -> s) %1 -> StateT s m a %1 -> StateT s m a Source #
put :: (Applicative m, Consumable s) => s %1 -> StateT s m () Source #
modify :: Applicative m => (s %1 -> s) %1 -> StateT s m () Source #
class (forall m. Monad m => Monad (t m)) => MonadTrans t where Source #
Instances
Dupable r => MonadTrans (ReaderT r) Source # | |
MonadTrans (StateT s) Source # | |
Functor f => MonadTrans (Stream f) Source # | |
MonadTrans (ReaderT r) Source # | |